Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Base64.cpp

00001 
00002 // *********************************************************************
00003 // * Base64 - a simple base64 encoder and decoder.
00004 // *
00005 // *     Copyright (c) 1999, Bob Withers - bwit@pobox.com
00006 // *
00007 // * This code may be freely used for any purpose, either personal
00008 // * or commercial, provided the authors copyright notice remains
00009 // * intact.
00010 // *
00011 // * Enhancements by Stanley Yamane:
00012 // *     o reverse lookup table for the decode function 
00013 // *     o reserve string buffer space in advance
00014 // *
00015 // *********************************************************************
00016 
00017 #include<ImLib3D/Base64.hpp>
00018 
00019 
00020 static const char          fillchar = '=';
00021 static const std::string::size_type np = std::string::npos;
00022 
00023 // 0000000000111111111122222222223333333333444444444455555555556666
00024 // 0123456789012345678901234567890123456789012345678901234567890123
00025 static const std::string Base64Table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
00026 
00027 // Decode Table gives the index of any valid base64 character in the Base64 table]
00028 // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
00029 
00030                                                // 0  1  2  3  4  5  6  7  8  9 
00031 const std::string::size_type DecodeTable[] = {np,np,np,np,np,np,np,np,np,np,  // 0 - 9
00032                                               np,np,np,np,np,np,np,np,np,np,  //10 -19
00033                                               np,np,np,np,np,np,np,np,np,np,  //20 -29
00034                                               np,np,np,np,np,np,np,np,np,np,  //30 -39
00035                                               np,np,np,62,np,np,np,63,52,53,  //40 -49
00036                                               54,55,56,57,58,59,60,61,np,np,  //50 -59
00037                                               np,np,np,np,np, 0, 1, 2, 3, 4,  //60 -69
00038                                               5, 6, 7, 8, 9,10,11,12,13,14,  //70 -79
00039                                               15,16,17,18,19,20,21,22,23,24,  //80 -89
00040                                               25,np,np,np,np,np,np,26,27,28,  //90 -99
00041                                               29,30,31,32,33,34,35,36,37,38,  //100 -109
00042                                               39,40,41,42,43,44,45,46,47,48,  //110 -119
00043                                               49,50,51,np,np,np,np,np,np,np,  //120 -129
00044                                               np,np,np,np,np,np,np,np,np,np,  //130 -139
00045                                               np,np,np,np,np,np,np,np,np,np,  //140 -149
00046                                               np,np,np,np,np,np,np,np,np,np,  //150 -159
00047                                               np,np,np,np,np,np,np,np,np,np,  //160 -169
00048                                               np,np,np,np,np,np,np,np,np,np,  //170 -179
00049                                               np,np,np,np,np,np,np,np,np,np,  //180 -189
00050                                               np,np,np,np,np,np,np,np,np,np,  //190 -199
00051                                               np,np,np,np,np,np,np,np,np,np,  //200 -209
00052                                               np,np,np,np,np,np,np,np,np,np,  //210 -219
00053                                               np,np,np,np,np,np,np,np,np,np,  //220 -229
00054                                               np,np,np,np,np,np,np,np,np,np,  //230 -239
00055                                               np,np,np,np,np,np,np,np,np,np,  //240 -249
00056                                               np,np,np,np,np,np               //250 -256
00057 };
00058 
00059 
00060 std::string 
00061 Base64::Encode(const std::string& data)
00062 {
00063     std::string::size_type  i;
00064     char               c;
00065     std::string::size_type  len = data.length();
00066     std::string             ret;
00067 
00068     ret.reserve(len * 2);
00069 
00070     for (i = 0; i < len; ++i)
00071     {
00072         c = (data[i] >> 2) & 0x3f;
00073         ret.append(1, Base64Table[c]);
00074         c = (data[i] << 4) & 0x3f;
00075         if (++i < len)
00076             c |= (data[i] >> 4) & 0x0f;
00077 
00078         ret.append(1, Base64Table[c]);
00079         if (i < len)
00080         {
00081             c = (data[i] << 2) & 0x3f;
00082             if (++i < len)
00083                 c |= (data[i] >> 6) & 0x03;
00084 
00085             ret.append(1, Base64Table[c]);
00086         }
00087         else
00088         {
00089             ++i;
00090             ret.append(1, fillchar);
00091         }
00092 
00093         if (i < len)
00094         {
00095             c = data[i] & 0x3f;
00096             ret.append(1, Base64Table[c]);
00097         }
00098         else
00099         {
00100             ret.append(1, fillchar);
00101         }
00102     }
00103 
00104     return(ret);
00105 }
00106 
00107 std::string 
00108 Base64::Decode(const std::string& data)
00109 {
00110     std::string::size_type  i;
00111     char               c;
00112     char               c1;
00113     std::string::size_type  len = data.length();
00114     std::string             ret;
00115 
00116     ret.reserve(len);
00117 
00118     for (i = 0; i < len; ++i)
00119     {
00120         c = (char) DecodeTable[(unsigned char)data[i]];
00121         ++i;
00122         c1 = (char) DecodeTable[(unsigned char)data[i]];
00123         c = (c << 2) | ((c1 >> 4) & 0x3);
00124         ret.append(1, c);
00125         if (++i < len)
00126         {
00127             c = data[i];
00128             if (fillchar == c)
00129                 break;
00130 
00131             c = (char) DecodeTable[(unsigned char)data[i]];
00132             c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
00133             ret.append(1, c1);
00134         }
00135 
00136         if (++i < len)
00137         {
00138             c1 = data[i];
00139             if (fillchar == c1)
00140                 break;
00141 
00142             c1 = (char) DecodeTable[(unsigned char)data[i]];
00143             c = ((c << 6) & 0xc0) | c1;
00144             ret.append(1, c);
00145         }
00146     }
00147 
00148     return(ret);
00149 }

Generated on Fri Jun 17 13:35:14 2005 for ImLib3D by  doxygen 1.4.2