Base64.cpp

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 #include "PreCompiled.h"
00028 
00029 #ifndef _PreComp_
00030 # include <iostream>
00031 # include <string>
00032 #endif
00033 
00034 #include "Base64.h"
00035 
00036 
00037 
00038 static const std::string base64_chars = 
00039              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00040              "abcdefghijklmnopqrstuvwxyz"
00041              "0123456789+/";
00042 
00043 
00044 static inline bool is_base64(unsigned char c) {
00045   return (isalnum(c) || (c == '+') || (c == '/'));
00046 }
00047 
00048 std::string Base::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
00049   std::string ret;
00050   int i = 0;
00051   int j = 0;
00052   unsigned char char_array_3[3];
00053   unsigned char char_array_4[4];
00054 
00055   while (in_len--) {
00056     char_array_3[i++] = *(bytes_to_encode++);
00057     if (i == 3) {
00058       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00059       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00060       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00061       char_array_4[3] = char_array_3[2] & 0x3f;
00062 
00063       for(i = 0; (i <4) ; i++)
00064         ret += base64_chars[char_array_4[i]];
00065       i = 0;
00066     }
00067   }
00068 
00069   if (i)
00070   {
00071     for(j = i; j < 3; j++)
00072       char_array_3[j] = '\0';
00073 
00074     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
00075     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
00076     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
00077     char_array_4[3] = char_array_3[2] & 0x3f;
00078 
00079     for (j = 0; (j < i + 1); j++)
00080       ret += base64_chars[char_array_4[j]];
00081 
00082     while((i++ < 3))
00083       ret += '=';
00084 
00085   }
00086 
00087   return ret;
00088 
00089 }
00090 
00091 std::string Base::base64_decode(std::string const& encoded_string) {
00092   int in_len = encoded_string.size();
00093   int i = 0;
00094   int j = 0;
00095   int in_ = 0;
00096   unsigned char char_array_4[4], char_array_3[3];
00097   std::string ret;
00098 
00099   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
00100     char_array_4[i++] = encoded_string[in_]; in_++;
00101     if (i ==4) {
00102       for (i = 0; i <4; i++)
00103         char_array_4[i] = base64_chars.find(char_array_4[i]);
00104 
00105       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00106       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00107       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00108 
00109       for (i = 0; (i < 3); i++)
00110         ret += char_array_3[i];
00111       i = 0;
00112     }
00113   }
00114 
00115   if (i) {
00116     for (j = i; j <4; j++)
00117       char_array_4[j] = 0;
00118 
00119     for (j = 0; j <4; j++)
00120       char_array_4[j] = base64_chars.find(char_array_4[j]);
00121 
00122     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
00123     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
00124     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
00125 
00126     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
00127   }
00128 
00129   return ret;
00130 }

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1