-
Notifications
You must be signed in to change notification settings - Fork 3
/
base64.cpp
209 lines (191 loc) · 3.8 KB
/
base64.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* base64.cpp
*
* Created on: 30/04/2011
* Author: nicholas
*/
#include "base64.h"
#include <cctype>
#include <cstdint>
#include <algorithm>
namespace base64
{
namespace
{
static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t encoded_size(size_t raw)
{
switch((raw*8)%6)
{
case 0:
return (raw*8)/6;
case 2:
return ((raw*8)/6) + 3;
case 4:
return ((raw*8)/6) + 2;
}
return raw;
}
size_t decoded_size(size_t unpadded)
{
return (unpadded*6)/8;
}
int base64_index(std::string::value_type c)
{
if(c >= 'A' && c <= 'Z')
return c-'A';
else if(c >= 'a' && c <= 'z')
return c-'a' + 26;
else if(c >= '0' && c <= '9')
return c-'0' + 52;
else if(c == '+')
return 62;
else if(c == '/')
return 63;
else
return -1;
}
}
encode_t::encode_t(std::string::size_type size)
: state(zero), remainder(0)
{
encoded.reserve(encoded_size(size));
}
/*
State zero
8 bits input, zero remaining from last
6 bits consumed, 2 remaining
=> two
State two
8 bits input, 2 remaining from last
4 bits consumed, 4 remaining
=> four
State four
8 bits input, 4 remaining from last
2 bits consumed, 6 remaining
6 bits input, 6 remaining from last
6 bits consumed, 0 remaining
=> zero
*/
void encode_t::operator() (std::string::value_type c)
{
unsigned char value(0);
switch(state)
{
case zero:
value = (c & 0xfc) >> 2;
remainder = c & 0x3;
encoded.push_back(BASE64_CHARS[value]);
state = two;
break;
case two:
value = (remainder << 4) | ((c & 0xf0) >> 4);
remainder = c & 0xf;
encoded.push_back(BASE64_CHARS[value]);
state = four;
break;
case four:
value = (remainder << 2) | ((c & 0xc0) >> 6);
remainder = c & 0x3f;
encoded.push_back(BASE64_CHARS[value]);
value = remainder;
encoded.push_back(BASE64_CHARS[value]);
state = zero;
break;
}
}
std::string encode_t::str()
{
unsigned char value(0);
switch(state)
{
case zero:
break;
case two:
value = remainder << 4;
encoded.push_back(BASE64_CHARS[value]);
encoded.push_back('=');
encoded.push_back('=');
state = zero;
break;
case four:
value = remainder << 2;
encoded.push_back(BASE64_CHARS[value]);
encoded.push_back('=');
state = zero;
break;
}
return encoded;
}
decode_t::decode_t(std::string::size_type size)
: state(zero), remainder(0)
{
decoded.reserve(decoded_size(size));
}
/*
State zero
6 bits input, zero remaining from last
6 bits consumed, zero remaining
=> six
State six
6 bits input, 6 remaining from last
write 1 byte, 4 remaining
=> four
State four
6 bits input, 4 remaining from last
write 1 byte, 2 remaining
=> two
State two
6 bits input, 2 remaining from last
write 1 byte, 0 remaining
=> zero
*/
void decode_t::operator() (std::string::value_type c)
{
unsigned char value(0);
int index = base64_index(c);
if(index == -1)
return;
switch(state)
{
case zero:
remainder = index;
state = six;
break;
case six:
value = (remainder << 2) | ((index & 0x30) >> 4);
remainder = index & 0xf;
decoded.push_back(value);
state = four;
break;
case four:
value = (remainder << 4) | ((index & 0x3c) >> 2);
remainder = index & 0x3;
decoded.push_back(value);
state = two;
break;
case two:
value = (remainder << 6) | index;
decoded.push_back(value);
state = zero;
break;
}
}
std::string decode_t::str() const
{
return decoded;
}
std::string encode(const std::string& str)
{
return std::for_each(str.begin(), str.end(), encode_t(str.size())).str();
}
std::string decode(const std::string& str)
{
size_t unpadded_size = str.size();
if(str.size() > 0 && str[str.size()-1] == '=')
unpadded_size -= 1;
if(str.size() > 1 && str[str.size()-2] == '=')
unpadded_size -= 1;
return std::for_each(str.begin(), str.end(), decode_t(unpadded_size)).str();
}
}