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


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:07