32static const uint8_t ENCODE_BASE64[64] = {
33 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
34 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
35 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
36 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
37 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
38 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
39 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
40 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F,
43static const uint8_t
E = -1;
45static const uint8_t DECODE_BASE64[128] = {
47 E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
48 E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
49 E,
E,
E,
E,
E,
E,
E,
E,
E,
E,
E, 62,
E,
E,
E, 63,
50 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
E,
E,
E,
E,
E,
E,
51 E, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
52 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
E,
E,
E,
E,
E,
53 E, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
54 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
E,
E,
E,
E,
E,
63 return 4 * ( ( aInputLen + 2 ) / 3 ) + ( aInputLen + 2 ) % 3 - 2;
69 if( aInputLen % 4 == 1 )
72 return 3 * ( ( aInputLen + 2 ) / 4 ) + ( aInputLen + 2 ) % 4 - 2;
76 void encode(
const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
78 size_t end = ( aInput.size() / 3 ) * 3;
81 for(
size_t i = 0; i < end; i += 3 )
83 unsigned value = ( aInput[i] << 16 ) | ( aInput[i + 1] << 8 ) | aInput[i + 2] ;
85 aOutput.emplace_back( ENCODE_BASE64[( value >> 18 ) & 0x3F] );
86 aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
87 aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
88 aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
91 size_t remainder = aInput.size() - end;
95 unsigned value = aInput[end];
99 value = ( value << 10 ) | ( aInput[end + 1] << 2 );
100 aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
107 aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
108 aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
113 void decode(
const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
115 size_t end = ( aInput.size() / 4 ) * 4;
121 aOutput.reserve( decode_size );
123 for(
size_t i = 0; i < end; i++ )
125 unsigned value = ( DECODE_BASE64[aInput[i] & 0x7F] << 18 ) |
126 ( DECODE_BASE64[aInput[i + 1] & 0x7F] << 12 ) |
127 ( DECODE_BASE64[aInput[i + 2] & 0x7F] << 6 ) |
128 DECODE_BASE64[aInput[i + 3] & 0x7F];
130 aOutput.emplace_back( ( value >> 16 ) & 0xff );
131 aOutput.emplace_back( ( value >> 8 ) & 0xff );
132 aOutput.emplace_back( value & 0xff );
135 size_t remainder = aInput.size() - end;
139 unsigned value = ( ( DECODE_BASE64[aInput[end] & 0x7F] ) << 6 ) |
140 ( DECODE_BASE64[aInput[end + 1] & 0x7F] );
144 value = ( value << 6 ) | ( DECODE_BASE64[aInput[end + 2] & 0x7F] );
147 aOutput.emplace_back( ( value >> 8 ) & 0xff );
153 aOutput[0] = value & 0xff ;
void encode(const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
size_t decode_length(size_t aInputLen)
size_t encode_length(size_t aInputLen)
void decode(const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)