~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to .pc/0001-License-Declaration.patch/src/xmlrpcpp/base64.h

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

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