Home | History | Annotate | Download | only in base
      1 
      2 //*********************************************************************
      3 //* Base64 - a simple base64 encoder and decoder.
      4 //*
      5 //*     Copyright (c) 1999, Bob Withers - bwit (at) pobox.com
      6 //*
      7 //* This code may be freely used for any purpose, either personal
      8 //* or commercial, provided the authors copyright notice remains
      9 //* intact.
     10 //*
     11 //* Enhancements by Stanley Yamane:
     12 //*     o reverse lookup table for the decode function
     13 //*     o reserve string buffer space in advance
     14 //*
     15 //*********************************************************************
     16 
     17 #include "talk/base/base64.h"
     18 #include "talk/base/common.h"
     19 
     20 using std::string;
     21 using std::vector;
     22 
     23 namespace talk_base {
     24 
     25 static const char kPad = '=';
     26 static const unsigned char pd = 0xFD;  // Padding
     27 static const unsigned char sp = 0xFE;  // Whitespace
     28 static const unsigned char il = 0xFF;  // Illegal base64 character
     29 
     30 const string Base64::Base64Table(
     31 // 0000000000111111111122222222223333333333444444444455555555556666
     32 // 0123456789012345678901234567890123456789012345678901234567890123
     33   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
     34 
     35 // Decode Table gives the index of any valid base64 character in the
     36 // Base64 table
     37 // 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
     38 
     39 const unsigned char Base64::DecodeTable[] = {
     40 // 0  1  2  3  4  5  6  7  8  9
     41   il,il,il,il,il,il,il,il,il,sp,  //   0 -   9
     42   sp,sp,sp,sp,il,il,il,il,il,il,  //  10 -  19
     43   il,il,il,il,il,il,il,il,il,il,  //  20 -  29
     44   il,il,sp,il,il,il,il,il,il,il,  //  30 -  39
     45   il,il,il,62,il,il,il,63,52,53,  //  40 -  49
     46   54,55,56,57,58,59,60,61,il,il,  //  50 -  59
     47   il,pd,il,il,il, 0, 1, 2, 3, 4,  //  60 -  69
     48    5, 6, 7, 8, 9,10,11,12,13,14,  //  70 -  79
     49   15,16,17,18,19,20,21,22,23,24,  //  80 -  89
     50   25,il,il,il,il,il,il,26,27,28,  //  90 -  99
     51   29,30,31,32,33,34,35,36,37,38,  // 100 - 109
     52   39,40,41,42,43,44,45,46,47,48,  // 110 - 119
     53   49,50,51,il,il,il,il,il,il,il,  // 120 - 129
     54   il,il,il,il,il,il,il,il,il,il,  // 130 - 139
     55   il,il,il,il,il,il,il,il,il,il,  // 140 - 149
     56   il,il,il,il,il,il,il,il,il,il,  // 150 - 159
     57   il,il,il,il,il,il,il,il,il,il,  // 160 - 169
     58   il,il,il,il,il,il,il,il,il,il,  // 170 - 179
     59   il,il,il,il,il,il,il,il,il,il,  // 180 - 189
     60   il,il,il,il,il,il,il,il,il,il,  // 190 - 199
     61   il,il,il,il,il,il,il,il,il,il,  // 200 - 209
     62   il,il,il,il,il,il,il,il,il,il,  // 210 - 219
     63   il,il,il,il,il,il,il,il,il,il,  // 220 - 229
     64   il,il,il,il,il,il,il,il,il,il,  // 230 - 239
     65   il,il,il,il,il,il,il,il,il,il,  // 240 - 249
     66   il,il,il,il,il,il               // 250 - 255
     67 };
     68 
     69 bool Base64::IsBase64Char(char ch) {
     70   return (('A' <= ch) && (ch <= 'Z')) ||
     71          (('a' <= ch) && (ch <= 'z')) ||
     72          (('0' <= ch) && (ch <= '9')) ||
     73          (ch == '+') || (ch == '/');
     74 }
     75 
     76 bool Base64::IsBase64Encoded(const std::string& str) {
     77   for (size_t i = 0; i < str.size(); ++i) {
     78     if (!IsBase64Char(str.at(i)))
     79       return false;
     80   }
     81   return true;
     82 }
     83 
     84 void Base64::EncodeFromArray(const void* data, size_t len, string* result) {
     85   ASSERT(NULL != result);
     86   result->clear();
     87   result->reserve(((len + 2) / 3) * 4);
     88   const unsigned char* byte_data = static_cast<const unsigned char*>(data);
     89 
     90   unsigned char c;
     91   size_t i = 0;
     92   while (i < len) {
     93     c = (byte_data[i] >> 2) & 0x3f;
     94     result->push_back(Base64Table[c]);
     95 
     96     c = (byte_data[i] << 4) & 0x3f;
     97     if (++i < len) {
     98       c |= (byte_data[i] >> 4) & 0x0f;
     99     }
    100     result->push_back(Base64Table[c]);
    101 
    102     if (i < len) {
    103       c = (byte_data[i] << 2) & 0x3f;
    104       if (++i < len) {
    105         c |= (byte_data[i] >> 6) & 0x03;
    106       }
    107       result->push_back(Base64Table[c]);
    108     } else {
    109       result->push_back(kPad);
    110     }
    111 
    112     if (i < len) {
    113       c = byte_data[i] & 0x3f;
    114       result->push_back(Base64Table[c]);
    115       ++i;
    116     } else {
    117       result->push_back(kPad);
    118     }
    119   }
    120 }
    121 
    122 size_t Base64::GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads,
    123                               const char* data, size_t len, size_t* dpos,
    124                               unsigned char qbuf[4], bool* padded)
    125 {
    126   size_t byte_len = 0, pad_len = 0, pad_start = 0;
    127   for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
    128     qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
    129     if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
    130       if (parse_flags != DO_PARSE_ANY)
    131         break;
    132       // Ignore illegal characters
    133     } else if (sp == qbuf[byte_len]) {
    134       if (parse_flags == DO_PARSE_STRICT)
    135         break;
    136       // Ignore spaces
    137     } else if (pd == qbuf[byte_len]) {
    138       if (byte_len < 2) {
    139         if (parse_flags != DO_PARSE_ANY)
    140           break;
    141         // Ignore unexpected padding
    142       } else if (byte_len + pad_len >= 4) {
    143         if (parse_flags != DO_PARSE_ANY)
    144           break;
    145         // Ignore extra pads
    146       } else {
    147         if (1 == ++pad_len) {
    148           pad_start = *dpos;
    149         }
    150       }
    151     } else {
    152       if (pad_len > 0) {
    153         if (parse_flags != DO_PARSE_ANY)
    154           break;
    155         // Ignore pads which are followed by data
    156         pad_len = 0;
    157       }
    158       ++byte_len;
    159     }
    160   }
    161   for (size_t i = byte_len; i < 4; ++i) {
    162     qbuf[i] = 0;
    163   }
    164   if (4 == byte_len + pad_len) {
    165     *padded = true;
    166   } else {
    167     *padded = false;
    168     if (pad_len) {
    169       // Roll back illegal padding
    170       *dpos = pad_start;
    171     }
    172   }
    173   return byte_len;
    174 }
    175 
    176 bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
    177                              string* result, size_t* data_used) {
    178   return DecodeFromArrayTemplate<string>(data, len, flags, result, data_used);
    179 }
    180 
    181 bool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
    182                              vector<char>* result, size_t* data_used) {
    183   return DecodeFromArrayTemplate<vector<char> >(data, len, flags, result,
    184                                                 data_used);
    185 }
    186 
    187 template<typename T>
    188 bool Base64::DecodeFromArrayTemplate(const char* data, size_t len,
    189                                      DecodeFlags flags, T* result,
    190                                      size_t* data_used)
    191 {
    192   ASSERT(NULL != result);
    193   ASSERT(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
    194 
    195   const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
    196   const DecodeFlags pad_flags   = flags & DO_PAD_MASK;
    197   const DecodeFlags term_flags  = flags & DO_TERM_MASK;
    198   ASSERT(0 != parse_flags);
    199   ASSERT(0 != pad_flags);
    200   ASSERT(0 != term_flags);
    201 
    202   result->clear();
    203   result->reserve(len);
    204 
    205   size_t dpos = 0;
    206   bool success = true, padded;
    207   unsigned char c, qbuf[4];
    208   while (dpos < len) {
    209     size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags),
    210                                  data, len, &dpos, qbuf, &padded);
    211     c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
    212     if (qlen >= 2) {
    213       result->push_back(c);
    214       c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
    215       if (qlen >= 3) {
    216         result->push_back(c);
    217         c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
    218         if (qlen >= 4) {
    219           result->push_back(c);
    220           c = 0;
    221         }
    222       }
    223     }
    224     if (qlen < 4) {
    225       if ((DO_TERM_ANY != term_flags) && (0 != c)) {
    226         success = false;  // unused bits
    227       }
    228       if ((DO_PAD_YES == pad_flags) && !padded) {
    229         success = false;  // expected padding
    230       }
    231       break;
    232     }
    233   }
    234   if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
    235     success = false;  // unused chars
    236   }
    237   if (data_used) {
    238     *data_used = dpos;
    239   }
    240   return success;
    241 }
    242 
    243 } // namespace talk_base
    244