CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibFormats/SiPixelObjects/src/PixelBase64.cc

Go to the documentation of this file.
00001 /* 
00002    base64.cpp and base64.h
00003 
00004    Copyright (C) 2004-2008 René Nyffenegger
00005 
00006    This source code is provided 'as-is', without any express or implied
00007    warranty. In no event will the author be held liable for any damages
00008    arising from the use of this software.
00009 
00010    Permission is granted to anyone to use this software for any purpose,
00011    including commercial applications, and to alter it and redistribute it
00012    freely, subject to the following restrictions:
00013 
00014    1. The origin of this source code must not be misrepresented; you must not
00015       claim that you wrote the original source code. If you use this source code
00016       in a product, an acknowledgment in the product documentation would be
00017       appreciated but is not required.
00018 
00019    2. Altered source versions must be plainly marked as such, and must not be
00020       misrepresented as being the original source code.
00021 
00022    3. This notice may not be removed or altered from any source distribution.
00023 
00024    René Nyffenegger rene.nyffenegger@adp-gmbh.ch
00025 
00026 */
00027 
00028 #include "CalibFormats/SiPixelObjects/interface/PixelBase64.h"
00029 #include <iostream>
00030 
00031 static const std::string base64_chars = 
00032              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00033              "abcdefghijklmnopqrstuvwxyz"
00034              "0123456789+/";
00035 
00036 
00037 static inline bool is_base64(unsigned char c) {
00038   return (isalnum(c) || (c == '+') || (c == '/'));
00039 }
00040 
00041 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
00042   std::string ret;
00043   int i = 0;
00044   int j = 0;
00045   unsigned char char_array_3[3];
00046   unsigned char char_array_4[4];
00047 
00048   while (in_len--) {
00049     char_array_3[i++] = *(bytes_to_encode++);
00050     if (i == 3) {
00051       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00052       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00053       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00054       char_array_4[3] = char_array_3[2] & 0x3f;
00055 
00056       for(i = 0; (i <4) ; i++)
00057         ret += base64_chars[char_array_4[i]];
00058       i = 0;
00059     }
00060   }
00061 
00062   if (i)
00063   {
00064     for(j = i; j < 3; j++)
00065       char_array_3[j] = '\0';
00066 
00067     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00068     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00069     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00070     char_array_4[3] = char_array_3[2] & 0x3f;
00071 
00072     for (j = 0; (j < i + 1); j++)
00073       ret += base64_chars[char_array_4[j]];
00074 
00075     while((i++ < 3))
00076       ret += '=';
00077 
00078   }
00079 
00080   return ret;
00081 
00082 }
00083 
00084 std::string base64_decode(std::string const& encoded_string) {
00085   int in_len = encoded_string.size();
00086   int i = 0;
00087   int j = 0;
00088   int in_ = 0;
00089   unsigned char char_array_4[4], char_array_3[3];
00090   std::string ret;
00091 
00092   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
00093     char_array_4[i++] = encoded_string[in_]; in_++;
00094     if (i ==4) {
00095       for (i = 0; i <4; i++)
00096         char_array_4[i] = base64_chars.find(char_array_4[i]);
00097 
00098       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00099       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00100       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00101 
00102       for (i = 0; (i < 3); i++)
00103         ret += char_array_3[i];
00104       i = 0;
00105     }
00106   }
00107 
00108   if (i) {
00109     for (j = i; j <4; j++)
00110       char_array_4[j] = 0;
00111 
00112     for (j = 0; j <4; j++)
00113       char_array_4[j] = base64_chars.find(char_array_4[j]);
00114 
00115     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00116     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00117     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00118 
00119     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
00120   }
00121 
00122   return ret;
00123 }