147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*********************************************************************
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//* Base64 - a simple base64 encoder and decoder.
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*     Copyright (c) 1999, Bob Withers - bwit@pobox.com
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//* This code may be freely used for any purpose, either personal
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//* or commercial, provided the authors copyright notice remains
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//* intact.
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//* Enhancements by Stanley Yamane:
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*     o reverse lookup table for the decode function
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*     o reserve string buffer space in advance
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//*********************************************************************
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/base64.h"
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <string.h>
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/common.h"
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgusing std::vector;
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const char kPad = '=';
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const unsigned char pd = 0xFD;  // Padding
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const unsigned char sp = 0xFE;  // Whitespace
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const unsigned char il = 0xFF;  // Illegal base64 character
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char Base64::Base64Table[] =
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// 0000000000111111111122222222223333333333444444444455555555556666
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// 0123456789012345678901234567890123456789012345678901234567890123
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Decode Table gives the index of any valid base64 character in the
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Base64 table
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// 65 == A, 97 == a, 48 == 0, 43 == +, 47 == /
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst unsigned char Base64::DecodeTable[] = {
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// 0  1  2  3  4  5  6  7  8  9
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,sp,  //   0 -   9
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  sp,sp,sp,sp,il,il,il,il,il,il,  //  10 -  19
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  //  20 -  29
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,sp,il,il,il,il,il,il,il,  //  30 -  39
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,62,il,il,il,63,52,53,  //  40 -  49
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  54,55,56,57,58,59,60,61,il,il,  //  50 -  59
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,pd,il,il,il, 0, 1, 2, 3, 4,  //  60 -  69
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org   5, 6, 7, 8, 9,10,11,12,13,14,  //  70 -  79
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  15,16,17,18,19,20,21,22,23,24,  //  80 -  89
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  25,il,il,il,il,il,il,26,27,28,  //  90 -  99
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  29,30,31,32,33,34,35,36,37,38,  // 100 - 109
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  39,40,41,42,43,44,45,46,47,48,  // 110 - 119
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  49,50,51,il,il,il,il,il,il,il,  // 120 - 129
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 130 - 139
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 140 - 149
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 150 - 159
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 160 - 169
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 170 - 179
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 180 - 189
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 190 - 199
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 200 - 209
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 210 - 219
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 220 - 229
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 230 - 239
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il,il,il,il,il,  // 240 - 249
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  il,il,il,il,il,il               // 250 - 255
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::IsBase64Char(char ch) {
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return (('A' <= ch) && (ch <= 'Z')) ||
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org         (('a' <= ch) && (ch <= 'z')) ||
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org         (('0' <= ch) && (ch <= '9')) ||
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org         (ch == '+') || (ch == '/');
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::GetNextBase64Char(char ch, char* next_ch) {
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (next_ch == NULL) {
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return false;
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const char* p = strchr(Base64Table, ch);
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!p)
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return false;
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ++p;
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  *next_ch = (*p) ? *p : Base64Table[0];
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return true;
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::IsBase64Encoded(const std::string& str) {
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (size_t i = 0; i < str.size(); ++i) {
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!IsBase64Char(str.at(i)))
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return false;
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return true;
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Base64::EncodeFromArray(const void* data, size_t len,
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                             std::string* result) {
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(NULL != result);
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  result->clear();
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  result->resize(((len + 2) / 3) * 4);
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const unsigned char* byte_data = static_cast<const unsigned char*>(data);
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  unsigned char c;
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t i = 0;
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t dest_ix = 0;
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  while (i < len) {
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    c = (byte_data[i] >> 2) & 0x3f;
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    (*result)[dest_ix++] = Base64Table[c];
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    c = (byte_data[i] << 4) & 0x3f;
11347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (++i < len) {
11447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      c |= (byte_data[i] >> 4) & 0x0f;
11547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    (*result)[dest_ix++] = Base64Table[c];
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (i < len) {
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      c = (byte_data[i] << 2) & 0x3f;
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (++i < len) {
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        c |= (byte_data[i] >> 6) & 0x03;
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      (*result)[dest_ix++] = Base64Table[c];
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      (*result)[dest_ix++] = kPad;
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (i < len) {
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      c = byte_data[i] & 0x3f;
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      (*result)[dest_ix++] = Base64Table[c];
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ++i;
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      (*result)[dest_ix++] = kPad;
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgsize_t Base64::GetNextQuantum(DecodeFlags parse_flags, bool illegal_pads,
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                              const char* data, size_t len, size_t* dpos,
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                              unsigned char qbuf[4], bool* padded)
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org{
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t byte_len = 0, pad_len = 0, pad_start = 0;
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (; (byte_len < 4) && (*dpos < len); ++*dpos) {
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    qbuf[byte_len] = DecodeTable[static_cast<unsigned char>(data[*dpos])];
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if ((il == qbuf[byte_len]) || (illegal_pads && (pd == qbuf[byte_len]))) {
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (parse_flags != DO_PARSE_ANY)
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Ignore illegal characters
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (sp == qbuf[byte_len]) {
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (parse_flags == DO_PARSE_STRICT)
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Ignore spaces
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (pd == qbuf[byte_len]) {
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (byte_len < 2) {
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (parse_flags != DO_PARSE_ANY)
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          break;
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        // Ignore unexpected padding
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      } else if (byte_len + pad_len >= 4) {
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (parse_flags != DO_PARSE_ANY)
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          break;
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        // Ignore extra pads
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      } else {
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (1 == ++pad_len) {
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          pad_start = *dpos;
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        }
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
16747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
16847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (pad_len > 0) {
16947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (parse_flags != DO_PARSE_ANY)
17047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          break;
17147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        // Ignore pads which are followed by data
17247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        pad_len = 0;
17347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
17447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ++byte_len;
17547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
17647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
17747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (size_t i = byte_len; i < 4; ++i) {
17847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    qbuf[i] = 0;
17947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
18047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (4 == byte_len + pad_len) {
18147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    *padded = true;
18247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
18347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    *padded = false;
18447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (pad_len) {
18547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Roll back illegal padding
18647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      *dpos = pad_start;
18747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
18847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
18947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return byte_len;
19047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
19147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
19247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
19347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                             std::string* result, size_t* data_used) {
19447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return DecodeFromArrayTemplate<std::string>(
19547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      data, len, flags, result, data_used);
19647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
19747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
19847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::DecodeFromArray(const char* data, size_t len, DecodeFlags flags,
19947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                             vector<char>* result, size_t* data_used) {
20047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return DecodeFromArrayTemplate<vector<char> >(data, len, flags, result,
20147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                                data_used);
20247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
20347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
20447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgtemplate<typename T>
20547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Base64::DecodeFromArrayTemplate(const char* data, size_t len,
20647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                     DecodeFlags flags, T* result,
20747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                     size_t* data_used)
20847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org{
20947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(NULL != result);
21047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(flags <= (DO_PARSE_MASK | DO_PAD_MASK | DO_TERM_MASK));
21147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const DecodeFlags parse_flags = flags & DO_PARSE_MASK;
21347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const DecodeFlags pad_flags   = flags & DO_PAD_MASK;
21447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const DecodeFlags term_flags  = flags & DO_TERM_MASK;
21547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(0 != parse_flags);
21647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(0 != pad_flags);
21747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(0 != term_flags);
21847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  result->clear();
22047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  result->reserve(len);
22147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
22247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t dpos = 0;
22347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool success = true, padded;
22447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  unsigned char c, qbuf[4];
22547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  while (dpos < len) {
22647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    size_t qlen = GetNextQuantum(parse_flags, (DO_PAD_NO == pad_flags),
22747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                 data, len, &dpos, qbuf, &padded);
22847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    c = (qbuf[0] << 2) | ((qbuf[1] >> 4) & 0x3);
22947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (qlen >= 2) {
23047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      result->push_back(c);
23147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      c = ((qbuf[1] << 4) & 0xf0) | ((qbuf[2] >> 2) & 0xf);
23247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (qlen >= 3) {
23347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        result->push_back(c);
23447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        c = ((qbuf[2] << 6) & 0xc0) | qbuf[3];
23547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (qlen >= 4) {
23647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          result->push_back(c);
23747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          c = 0;
23847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        }
23947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
24047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
24147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (qlen < 4) {
24247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if ((DO_TERM_ANY != term_flags) && (0 != c)) {
24347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        success = false;  // unused bits
24447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
24547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if ((DO_PAD_YES == pad_flags) && !padded) {
24647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        success = false;  // expected padding
24747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
24847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
24947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
25047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
25147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if ((DO_TERM_BUFFER == term_flags) && (dpos != len)) {
25247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    success = false;  // unused chars
25347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
25447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (data_used) {
25547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    *data_used = dpos;
25647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
25747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return success;
25847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
25947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
26047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org} // namespace rtc
261