Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:13

0001 /* 
0002    base64.cpp and base64.h
0003 
0004    Copyright (C) 2004-2008 René Nyffenegger
0005 
0006    This source code is provided 'as-is', without any express or implied
0007    warranty. In no event will the author be held liable for any damages
0008    arising from the use of this software.
0009 
0010    Permission is granted to anyone to use this software for any purpose,
0011    including commercial applications, and to alter it and redistribute it
0012    freely, subject to the following restrictions:
0013 
0014    1. The origin of this source code must not be misrepresented; you must not
0015       claim that you wrote the original source code. If you use this source code
0016       in a product, an acknowledgment in the product documentation would be
0017       appreciated but is not required.
0018 
0019    2. Altered source versions must be plainly marked as such, and must not be
0020       misrepresented as being the original source code.
0021 
0022    3. This notice may not be removed or altered from any source distribution.
0023 
0024    René Nyffenegger rene.nyffenegger@adp-gmbh.ch
0025 
0026 */
0027 
0028 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
0029 #include <iostream>
0030 
0031 static const std::string base64_chars =
0032     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
0033     "abcdefghijklmnopqrstuvwxyz"
0034     "0123456789+/";
0035 
0036 static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
0037 
0038 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
0039   std::string ret;
0040   int i = 0;
0041   int j = 0;
0042   unsigned char char_array_3[3];
0043   unsigned char char_array_4[4];
0044 
0045   while (in_len--) {
0046     char_array_3[i++] = *(bytes_to_encode++);
0047     if (i == 3) {
0048       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
0049       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
0050       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
0051       char_array_4[3] = char_array_3[2] & 0x3f;
0052 
0053       for (i = 0; (i < 4); i++)
0054         ret += base64_chars[char_array_4[i]];
0055       i = 0;
0056     }
0057   }
0058 
0059   if (i) {
0060     for (j = i; j < 3; j++)
0061       char_array_3[j] = '\0';
0062 
0063     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
0064     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
0065     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
0066     char_array_4[3] = char_array_3[2] & 0x3f;
0067 
0068     for (j = 0; (j < i + 1); j++)
0069       ret += base64_chars[char_array_4[j]];
0070 
0071     while ((i++ < 3))
0072       ret += '=';
0073   }
0074 
0075   return ret;
0076 }
0077 
0078 std::string base64_decode(std::string const& encoded_string) {
0079   int in_len = encoded_string.size();
0080   int i = 0;
0081   int j = 0;
0082   int in_ = 0;
0083   unsigned char char_array_4[4], char_array_3[3];
0084   std::string ret;
0085 
0086   while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
0087     char_array_4[i++] = encoded_string[in_];
0088     in_++;
0089     if (i == 4) {
0090       for (i = 0; i < 4; i++)
0091         char_array_4[i] = base64_chars.find(char_array_4[i]);
0092 
0093       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
0094       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
0095       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
0096 
0097       for (i = 0; (i < 3); i++)
0098         ret += char_array_3[i];
0099       i = 0;
0100     }
0101   }
0102 
0103   if (i) {
0104     for (j = i; j < 4; j++)
0105       char_array_4[j] = 0;
0106 
0107     for (j = 0; j < 4; j++)
0108       char_array_4[j] = base64_chars.find(char_array_4[j]);
0109 
0110     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
0111     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
0112     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
0113 
0114     for (j = 0; (j < i - 1); j++)
0115       ret += char_array_3[j];
0116   }
0117 
0118   return ret;
0119 }