4
// Autor Konstantin Pilipchuk
5
// mailto:lostd@ukr.net
9
#if !defined(__BASE64_H_INCLUDED__)
10
#define __BASE64_H_INCLUDED__ 1
15
int _base64Chars[]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
16
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
17
'0','1','2','3','4','5','6','7','8','9',
21
#define _0000_0011 0x03
22
#define _1111_1100 0xFC
23
#define _1111_0000 0xF0
24
#define _0011_0000 0x30
25
#define _0011_1100 0x3C
26
#define _0000_1111 0x0F
27
#define _1100_0000 0xC0
28
#define _0011_1111 0x3F
30
#define _EQUAL_CHAR (-1)
31
#define _UNKNOWN_CHAR (-2)
33
#define _IOS_FAILBIT std::ios_base::failbit
34
#define _IOS_EOFBIT std::ios_base::eofbit
35
#define _IOS_BADBIT std::ios_base::badbit
36
#define _IOS_GOODBIT std::ios_base::goodbit
38
// TEMPLATE CLASS base64_put
39
template<class _E = char, class _Tr = std::char_traits<_E> >
44
typedef unsigned char byte_t;
46
typedef _Tr traits_type;
48
// base64 requires max line length <= 72 characters
49
// you can fill end of line
50
// it may be crlf, crlfsp, noline or other class like it
56
_OI operator()(_OI _To) const{
57
*_To = _Tr::to_char_type('\r'); ++_To;
58
*_To = _Tr::to_char_type('\n'); ++_To;
68
_OI operator()(_OI _To) const{
69
*_To = _Tr::to_char_type('\r'); ++_To;
70
*_To = _Tr::to_char_type('\n'); ++_To;
71
*_To = _Tr::to_char_type(' '); ++_To;
80
_OI operator()(_OI _To) const{
107
void set_0(byte_t _ch)
112
void set_1(byte_t _ch)
117
void set_2(byte_t _ch)
122
// 0000 0000 1111 1111 2222 2222
123
// xxxx xxxx xxxx xxxx xxxx xxxx
124
// 0000 0011 1111 2222 2233 3333
126
int b64_0() const {return (_data[0] & _1111_1100) >> 2;}
127
int b64_1() const {return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);}
128
int b64_2() const {return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);}
129
int b64_3() const {return (_data[2] & _0011_1111);}
131
void b64_0(int _ch) {_data[0] = ((_ch & _0011_1111) << 2) | (_0000_0011 & _data[0]);}
133
void b64_1(int _ch) {
134
_data[0] = ((_ch & _0011_0000) >> 4) | (_1111_1100 & _data[0]);
135
_data[1] = ((_ch & _0000_1111) << 4) | (_0000_1111 & _data[1]); }
137
void b64_2(int _ch) {
138
_data[1] = ((_ch & _0011_1100) >> 2) | (_1111_0000 & _data[1]);
139
_data[2] = ((_ch & _0000_0011) << 6) | (_0011_1111 & _data[2]); }
142
_data[2] = (_ch & _0011_1111) | (_1100_0000 & _data[2]);}
152
template<class _II, class _OI, class _State, class _Endline>
153
_II put(_II _First, _II _Last, _OI _To, _State& _St, _Endline _Endl) const
158
while(_First != _Last)
163
_3to4.set_0(*_First);
168
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
169
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
170
*_To = _Tr::to_char_type('='); ++_To;
171
*_To = _Tr::to_char_type('='); ++_To;
175
_3to4.set_1(*_First);
180
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
181
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
182
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
183
*_To = _Tr::to_char_type('='); ++_To;
187
_3to4.set_2(*_First);
190
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
191
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
192
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
193
*_To = _Tr::to_char_type(_base64Chars[_3to4.b64_3()]); ++_To;
195
if(line_octets == 17) // base64 ��������� ����� ������ �� ����� 72 ��������
212
template<class _II, class _OI, class _State>
213
_II get(_II _First, _II _Last, _OI _To, _State& _St) const
218
while(_First != _Last)
225
// Search next valid char...
226
while((_Char = _getCharType(*_First)) < 0 && _Char == _UNKNOWN_CHAR)
228
if(++_First == _Last)
230
_St |= _IOS_FAILBIT|_IOS_EOFBIT; return _First; // unexpected EOF
234
if(_Char == _EQUAL_CHAR){
235
// Error! First character in octet can't be '='
244
// Search next valid char...
245
while(++_First != _Last)
246
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
249
if(_First == _Last) {
250
_St |= _IOS_FAILBIT|_IOS_EOFBIT; // unexpected EOF
254
if(_Char == _EQUAL_CHAR){
255
// Error! Second character in octet can't be '='
264
// Search next valid char...
265
while(++_First != _Last)
266
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
269
if(_First == _Last) {
270
// Error! Unexpected EOF. Must be '=' or base64 character
271
_St |= _IOS_FAILBIT|_IOS_EOFBIT;
275
if(_Char == _EQUAL_CHAR){
281
if(++_First == _Last)
283
// Error! Unexpected EOF. Must be '='. Ignore it.
284
//_St |= _IOS_BADBIT|_IOS_EOFBIT;
288
if(_getCharType(*_First) != _EQUAL_CHAR)
290
// Error! Must be '='. Ignore it.
291
//_St |= _IOS_BADBIT;
294
++_First; // Skip '='
296
// write 1 byte to output
297
*_To = (byte_t) _3to4.get_0();
305
// Search next valid char...
306
while(++_First != _Last)
307
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
310
if(_First == _Last) {
311
// Unexpected EOF. It's error. But ignore it.
312
//_St |= _IOS_FAILBIT|_IOS_EOFBIT;
318
if(_Char == _EQUAL_CHAR)
323
// write to output 2 bytes
324
*_To = (byte_t) _3to4.get_0();
325
*_To = (byte_t) _3to4.get_1();
327
++_First; // set position to next character
335
// write to output 3 bytes
336
*_To = (byte_t) _3to4.get_0();
337
*_To = (byte_t) _3to4.get_1();
338
*_To = (byte_t) _3to4.get_2();
343
} // while(_First != _Last)
350
int _getCharType(int _Ch) const
352
if(_base64Chars[62] == _Ch)
355
if(_base64Chars[63] == _Ch)
358
if((_base64Chars[0] <= _Ch) && (_base64Chars[25] >= _Ch))
359
return _Ch - _base64Chars[0];
361
if((_base64Chars[26] <= _Ch) && (_base64Chars[51] >= _Ch))
362
return _Ch - _base64Chars[26] + 26;
364
if((_base64Chars[52] <= _Ch) && (_base64Chars[61] >= _Ch))
365
return _Ch - _base64Chars[52] + 52;
367
if(_Ch == _Tr::to_int_type('='))
370
return _UNKNOWN_CHAR;