BOSS 7.0.8
BESIII Offline Software System
Loading...
Searching...
No Matches
base64.h
Go to the documentation of this file.
1
2
3// base64.hpp
4// Autor Konstantin Pilipchuk
5// mailto:[email protected]
6//
7//
8
9#if !defined(__BASE64_H_INCLUDED__)
10#define __BASE64_H_INCLUDED__ 1
11
12#ifndef MAKEDEPEND
13# include <iterator>
14#endif
15
16static
17int _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',
18 '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',
19 '0','1','2','3','4','5','6','7','8','9',
20 '+','/' };
21
22
23#define _0000_0011 0x03
24#define _1111_1100 0xFC
25#define _1111_0000 0xF0
26#define _0011_0000 0x30
27#define _0011_1100 0x3C
28#define _0000_1111 0x0F
29#define _1100_0000 0xC0
30#define _0011_1111 0x3F
31
32#define _EQUAL_CHAR (-1)
33#define _UNKNOWN_CHAR (-2)
34
35#define _IOS_FAILBIT std::ios_base::failbit
36#define _IOS_EOFBIT std::ios_base::eofbit
37#define _IOS_BADBIT std::ios_base::badbit
38#define _IOS_GOODBIT std::ios_base::goodbit
39
40// TEMPLATE CLASS base64_put
41template<class _E = char, class _Tr = std::char_traits<_E> >
42class base64
43{
44public:
45
46 typedef unsigned char byte_t;
47 typedef _E char_type;
48 typedef _Tr traits_type;
49
50 // base64 requires max line length <= 72 characters
51 // you can fill end of line
52 // it may be crlf, crlfsp, noline or other class like it
53
54
55 struct crlf
56 {
57 template<class _OI>
58 _OI operator()(_OI _To) const{
59 *_To = _Tr::to_char_type('\r'); ++_To;
60 *_To = _Tr::to_char_type('\n'); ++_To;
61
62 return (_To);
63 }
64 };
65
66
67 struct crlfsp
68 {
69 template<class _OI>
70 _OI operator()(_OI _To) const{
71 *_To = _Tr::to_char_type('\r'); ++_To;
72 *_To = _Tr::to_char_type('\n'); ++_To;
73 *_To = _Tr::to_char_type(' '); ++_To;
74
75 return (_To);
76 }
77 };
78
79 struct noline
80 {
81 template<class _OI>
82 _OI operator()(_OI _To) const{
83 return (_To);
84 }
85 };
86
88 {
89 void zero()
90 {
91 _data[0] = 0;
92 _data[1] = 0;
93 _data[2] = 0;
94 }
95
96 byte_t get_0() const
97 {
98 return _data[0];
99 }
100 byte_t get_1() const
101 {
102 return _data[1];
103 }
104 byte_t get_2() const
105 {
106 return _data[2];
107 }
108
109 void set_0(byte_t _ch)
110 {
111 _data[0] = _ch;
112 }
113
114 void set_1(byte_t _ch)
115 {
116 _data[1] = _ch;
117 }
118
119 void set_2(byte_t _ch)
120 {
121 _data[2] = _ch;
122 }
123
124 // 0000 0000 1111 1111 2222 2222
125 // xxxx xxxx xxxx xxxx xxxx xxxx
126 // 0000 0011 1111 2222 2233 3333
127
128 int b64_0() const {return (_data[0] & _1111_1100) >> 2;}
129 int b64_1() const {return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);}
130 int b64_2() const {return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);}
131 int b64_3() const {return (_data[2] & _0011_1111);}
132
133 void b64_0(int _ch) {_data[0] = ((_ch & _0011_1111) << 2) | (_0000_0011 & _data[0]);}
134
135 void b64_1(int _ch) {
136 _data[0] = ((_ch & _0011_0000) >> 4) | (_1111_1100 & _data[0]);
137 _data[1] = ((_ch & _0000_1111) << 4) | (_0000_1111 & _data[1]); }
138
139 void b64_2(int _ch) {
140 _data[1] = ((_ch & _0011_1100) >> 2) | (_1111_0000 & _data[1]);
141 _data[2] = ((_ch & _0000_0011) << 6) | (_0011_1111 & _data[2]); }
142
143 void b64_3(int _ch){
144 _data[2] = (_ch & _0011_1111) | (_1100_0000 & _data[2]);}
145
146 private:
147 byte_t _data[3];
148
149 };
150
151
152
153
154 template<class _II, class _OI, class _State, class _Endline>
155 _II put(_II _First, _II _Last, _OI _To, _State& _St, _Endline _Endl) const
156 {
157 three2four _3to4;
158 int line_octets = 0;
159
160 while(_First != _Last)
161 {
162 _3to4.zero();
163
164 // ���� �� 3 �������
165 _3to4.set_0(*_First);
166 _First++;
167
168 if(_First == _Last)
169 {
170 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
171 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
172 *_To = _Tr::to_char_type('='); ++_To;
173 *_To = _Tr::to_char_type('='); ++_To;
174 goto __end;
175 }
176
177 _3to4.set_1(*_First);
178 _First++;
179
180 if(_First == _Last)
181 {
182 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
183 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
184 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
185 *_To = _Tr::to_char_type('='); ++_To;
186 goto __end;
187 }
188
189 _3to4.set_2(*_First);
190 _First++;
191
192 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
193 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
194 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
195 *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_3()]); ++_To;
196
197 if(line_octets == 17) // base64 ��������� ����� ������ �� ����� 72 ��������
198 {
199 //_To = _Endl(_To);
200 *_To = '\n'; ++_To;
201 line_octets = 0;
202 }
203 else
204 ++line_octets;
205 }
206
207 __end: ;
208
209 return (_First);
210
211 }
212
213
214 template<class _II, class _OI, class _State>
215 _II get(_II _First, _II _Last, _OI _To, _State& _St) const
216 {
217 three2four _3to4;
218 int _Char;
219
220 while(_First != _Last)
221 {
222
223 // Take octet
224 _3to4.zero();
225
226 // -- 0 --
227 // Search next valid char...
228 while((_Char = _getCharType(*_First)) < 0 && _Char == _UNKNOWN_CHAR)
229 {
230 if(++_First == _Last)
231 {
232 _St |= _IOS_FAILBIT|_IOS_EOFBIT; return _First; // unexpected EOF
233 }
234 }
235
236 if(_Char == _EQUAL_CHAR){
237 // Error! First character in octet can't be '='
238 _St |= _IOS_FAILBIT;
239 return _First;
240 }
241 else
242 _3to4.b64_0(_Char);
243
244
245 // -- 1 --
246 // Search next valid char...
247 while(++_First != _Last)
248 if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
249 break;
250
251 if(_First == _Last) {
252 _St |= _IOS_FAILBIT|_IOS_EOFBIT; // unexpected EOF
253 return _First;
254 }
255
256 if(_Char == _EQUAL_CHAR){
257 // Error! Second character in octet can't be '='
258 _St |= _IOS_FAILBIT;
259 return _First;
260 }
261 else
262 _3to4.b64_1(_Char);
263
264
265 // -- 2 --
266 // Search next valid char...
267 while(++_First != _Last)
268 if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
269 break;
270
271 if(_First == _Last) {
272 // Error! Unexpected EOF. Must be '=' or base64 character
274 return _First;
275 }
276
277 if(_Char == _EQUAL_CHAR){
278 // OK!
279 _3to4.b64_2(0);
280 _3to4.b64_3(0);
281
282 // chek for EOF
283 if(++_First == _Last)
284 {
285 // Error! Unexpected EOF. Must be '='. Ignore it.
286 //_St |= _IOS_BADBIT|_IOS_EOFBIT;
287 _St |= _IOS_EOFBIT;
288 }
289 else
290 if(_getCharType(*_First) != _EQUAL_CHAR)
291 {
292 // Error! Must be '='. Ignore it.
293 //_St |= _IOS_BADBIT;
294 }
295 else
296 ++_First; // Skip '='
297
298 // write 1 byte to output
299 *_To = (byte_t) _3to4.get_0();
300 return _First;
301 }
302 else
303 _3to4.b64_2(_Char);
304
305
306 // -- 3 --
307 // Search next valid char...
308 while(++_First != _Last)
309 if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
310 break;
311
312 if(_First == _Last) {
313 // Unexpected EOF. It's error. But ignore it.
314 //_St |= _IOS_FAILBIT|_IOS_EOFBIT;
315 _St |= _IOS_EOFBIT;
316
317 return _First;
318 }
319
320 if(_Char == _EQUAL_CHAR)
321 {
322 // OK!
323 _3to4.b64_3(0);
324
325 // write to output 2 bytes
326 *_To = (byte_t) _3to4.get_0();
327 *_To = (byte_t) _3to4.get_1();
328
329 ++_First; // set position to next character
330
331 return _First;
332 }
333 else
334 _3to4.b64_3(_Char);
335
336
337 // write to output 3 bytes
338 *_To = (byte_t) _3to4.get_0();
339 *_To = (byte_t) _3to4.get_1();
340 *_To = (byte_t) _3to4.get_2();
341
342 ++_First;
343
344
345 } // while(_First != _Last)
346
347 return (_First);
348 }
349
350protected:
351
352 int _getCharType(int _Ch) const
353 {
354 if(_base64Chars[62] == _Ch)
355 return 62;
356
357 if(_base64Chars[63] == _Ch)
358 return 63;
359
360 if((_base64Chars[0] <= _Ch) && (_base64Chars[25] >= _Ch))
361 return _Ch - _base64Chars[0];
362
363 if((_base64Chars[26] <= _Ch) && (_base64Chars[51] >= _Ch))
364 return _Ch - _base64Chars[26] + 26;
365
366 if((_base64Chars[52] <= _Ch) && (_base64Chars[61] >= _Ch))
367 return _Ch - _base64Chars[52] + 52;
368
369 if(_Ch == _Tr::to_int_type('='))
370 return _EQUAL_CHAR;
371
372 return _UNKNOWN_CHAR;
373 }
374
375
376};
377
378
379#endif
#define _0000_1111
Definition: base64.h:28
#define _0011_0000
Definition: base64.h:26
#define _1111_1100
Definition: base64.h:24
#define _0011_1111
Definition: base64.h:30
#define _0000_0011
Definition: base64.h:23
#define _IOS_EOFBIT
Definition: base64.h:36
#define _1111_0000
Definition: base64.h:25
#define _0011_1100
Definition: base64.h:27
#define _EQUAL_CHAR
Definition: base64.h:32
#define _UNKNOWN_CHAR
Definition: base64.h:33
#define _1100_0000
Definition: base64.h:29
#define _IOS_FAILBIT
Definition: base64.h:35
Definition: base64.h:43
_II put(_II _First, _II _Last, _OI _To, _State &_St, _Endline _Endl) const
Definition: base64.h:155
unsigned char byte_t
Definition: base64.h:46
_II get(_II _First, _II _Last, _OI _To, _State &_St) const
Definition: base64.h:215
_Tr traits_type
Definition: base64.h:48
_E char_type
Definition: base64.h:47
int _getCharType(int _Ch) const
Definition: base64.h:352
_OI operator()(_OI _To) const
Definition: base64.h:58
_OI operator()(_OI _To) const
Definition: base64.h:70
_OI operator()(_OI _To) const
Definition: base64.h:82
byte_t get_0() const
Definition: base64.h:96
void set_1(byte_t _ch)
Definition: base64.h:114
byte_t get_1() const
Definition: base64.h:100
void b64_3(int _ch)
Definition: base64.h:143
byte_t get_2() const
Definition: base64.h:104
void set_0(byte_t _ch)
Definition: base64.h:109
void b64_1(int _ch)
Definition: base64.h:135
int b64_3() const
Definition: base64.h:131
void set_2(byte_t _ch)
Definition: base64.h:119
int b64_1() const
Definition: base64.h:129
int b64_2() const
Definition: base64.h:130
void b64_0(int _ch)
Definition: base64.h:133
int b64_0() const
Definition: base64.h:128
void b64_2(int _ch)
Definition: base64.h:139