base64.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // base64.hpp
  2. // Autor Konstantin Pilipchuk
  3. // mailto:lostd@ukr.net
  4. //
  5. //
  6. #if !defined(__BASE64_HPP_INCLUDED__)
  7. #define __BASE64_HPP_INCLUDED__ 1
  8. #pragma once
  9. #include <iterator>
  10. static
  11. 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',
  12. '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',
  13. '0','1','2','3','4','5','6','7','8','9',
  14. '+','/' };
  15. #define _0000_0011 0x03
  16. #define _1111_1100 0xFC
  17. #define _1111_0000 0xF0
  18. #define _0011_0000 0x30
  19. #define _0011_1100 0x3C
  20. #define _0000_1111 0x0F
  21. #define _1100_0000 0xC0
  22. #define _0011_1111 0x3F
  23. #define _EQUAL_CHAR (-1)
  24. #define _UNKNOWN_CHAR (-2)
  25. #define _IOS_FAILBIT std::ios_base::failbit
  26. #define _IOS_EOFBIT std::ios_base::eofbit
  27. #define _IOS_BADBIT std::ios_base::badbit
  28. #define _IOS_GOODBIT std::ios_base::goodbit
  29. // TEMPLATE CLASS base64_put
  30. template<class _E = char, class _Tr = std::char_traits<_E> >
  31. class base64
  32. {
  33. public:
  34. typedef unsigned char byte_t;
  35. typedef _E char_type;
  36. typedef _Tr traits_type;
  37. // base64 requires max line length <= 72 characters
  38. // you can fill end of line
  39. // it may be crlf, crlfsp, noline or other class like it
  40. struct lf
  41. {
  42. template<class _OI>
  43. _OI operator()(_OI _To) const{
  44. *_To = _Tr::to_char_type('\n'); ++_To;
  45. return (_To);
  46. }
  47. };
  48. struct crlf
  49. {
  50. template<class _OI>
  51. _OI operator()(_OI _To) const{
  52. *_To = _Tr::to_char_type('\r'); ++_To;
  53. *_To = _Tr::to_char_type('\n'); ++_To;
  54. return (_To);
  55. }
  56. };
  57. struct crlfsp
  58. {
  59. template<class _OI>
  60. _OI operator()(_OI _To) const{
  61. *_To = _Tr::to_char_type('\r'); ++_To;
  62. *_To = _Tr::to_char_type('\n'); ++_To;
  63. *_To = _Tr::to_char_type(' '); ++_To;
  64. return (_To);
  65. }
  66. };
  67. struct noline
  68. {
  69. template<class _OI>
  70. _OI operator()(_OI _To) const{
  71. return (_To);
  72. }
  73. };
  74. struct three2four
  75. {
  76. void zero()
  77. {
  78. _data[0] = 0;
  79. _data[1] = 0;
  80. _data[2] = 0;
  81. }
  82. byte_t get_0() const
  83. {
  84. return _data[0];
  85. }
  86. byte_t get_1() const
  87. {
  88. return _data[1];
  89. }
  90. byte_t get_2() const
  91. {
  92. return _data[2];
  93. }
  94. void set_0(byte_t _ch)
  95. {
  96. _data[0] = _ch;
  97. }
  98. void set_1(byte_t _ch)
  99. {
  100. _data[1] = _ch;
  101. }
  102. void set_2(byte_t _ch)
  103. {
  104. _data[2] = _ch;
  105. }
  106. // 0000 0000 1111 1111 2222 2222
  107. // xxxx xxxx xxxx xxxx xxxx xxxx
  108. // 0000 0011 1111 2222 2233 3333
  109. int b64_0() const {return (_data[0] & _1111_1100) >> 2;}
  110. int b64_1() const {return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);}
  111. int b64_2() const {return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);}
  112. int b64_3() const {return (_data[2] & _0011_1111);}
  113. void b64_0(int _ch) {_data[0] = ((_ch & _0011_1111) << 2) | (_0000_0011 & _data[0]);}
  114. void b64_1(int _ch) {
  115. _data[0] = ((_ch & _0011_0000) >> 4) | (_1111_1100 & _data[0]);
  116. _data[1] = ((_ch & _0000_1111) << 4) | (_0000_1111 & _data[1]); }
  117. void b64_2(int _ch) {
  118. _data[1] = ((_ch & _0011_1100) >> 2) | (_1111_0000 & _data[1]);
  119. _data[2] = ((_ch & _0000_0011) << 6) | (_0011_1111 & _data[2]); }
  120. void b64_3(int _ch){
  121. _data[2] = (_ch & _0011_1111) | (_1100_0000 & _data[2]);}
  122. private:
  123. byte_t _data[3];
  124. };
  125. template<class _II, class _OI, class _State, class _Endline>
  126. _II put(_II _First, _II _Last, _OI _To, _State& _St, _Endline _Endl) const
  127. {
  128. three2four _3to4;
  129. int line_octets = 0;
  130. while(_First != _Last)
  131. {
  132. _3to4.zero();
  133. // áåð¸ì ïî 3 ñèìâîëà
  134. _3to4.set_0(*_First);
  135. _First++;
  136. if(_First == _Last)
  137. {
  138. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
  139. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
  140. *_To = _Tr::to_char_type('='); ++_To;
  141. *_To = _Tr::to_char_type('='); ++_To;
  142. goto __end;
  143. }
  144. _3to4.set_1(*_First);
  145. _First++;
  146. if(_First == _Last)
  147. {
  148. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
  149. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
  150. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
  151. *_To = _Tr::to_char_type('='); ++_To;
  152. goto __end;
  153. }
  154. _3to4.set_2(*_First);
  155. _First++;
  156. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
  157. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
  158. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
  159. *_To = _Tr::to_char_type(_base64Chars[_3to4.b64_3()]); ++_To;
  160. if(line_octets == 17) // base64 ïîçâîëÿåò äëèíó ñòðîêè íå áîëåå 72 ñèìâîëîâ
  161. {
  162. _To = _Endl(_To);
  163. line_octets = 0;
  164. }
  165. else
  166. ++line_octets;
  167. }
  168. __end: ;
  169. return (_First);
  170. }
  171. template<class _II, class _OI, class _State>
  172. _II get(_II _First, _II _Last, _OI _To, _State& _St) const
  173. {
  174. three2four _3to4;
  175. int _Char;
  176. while(_First != _Last)
  177. {
  178. // Take octet
  179. _3to4.zero();
  180. // -- 0 --
  181. // Search next valid char...
  182. while((_Char = _getCharType(*_First)) < 0 && _Char == _UNKNOWN_CHAR)
  183. {
  184. if(++_First == _Last)
  185. {
  186. _St |= _IOS_FAILBIT|_IOS_EOFBIT; return _First; // unexpected EOF
  187. }
  188. }
  189. if(_Char == _EQUAL_CHAR){
  190. // Error! First character in octet can't be '='
  191. _St |= _IOS_FAILBIT;
  192. return _First;
  193. }
  194. else
  195. _3to4.b64_0(_Char);
  196. // -- 1 --
  197. // Search next valid char...
  198. while(++_First != _Last)
  199. if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
  200. break;
  201. if(_First == _Last) {
  202. _St |= _IOS_FAILBIT|_IOS_EOFBIT; // unexpected EOF
  203. return _First;
  204. }
  205. if(_Char == _EQUAL_CHAR){
  206. // Error! Second character in octet can't be '='
  207. _St |= _IOS_FAILBIT;
  208. return _First;
  209. }
  210. else
  211. _3to4.b64_1(_Char);
  212. // -- 2 --
  213. // Search next valid char...
  214. while(++_First != _Last)
  215. if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
  216. break;
  217. if(_First == _Last) {
  218. // Error! Unexpected EOF. Must be '=' or base64 character
  219. _St |= _IOS_FAILBIT|_IOS_EOFBIT;
  220. return _First;
  221. }
  222. if(_Char == _EQUAL_CHAR){
  223. // OK!
  224. _3to4.b64_2(0);
  225. _3to4.b64_3(0);
  226. // chek for EOF
  227. if(++_First == _Last)
  228. {
  229. // Error! Unexpected EOF. Must be '='. Ignore it.
  230. //_St |= _IOS_BADBIT|_IOS_EOFBIT;
  231. _St |= _IOS_EOFBIT;
  232. }
  233. else
  234. if(_getCharType(*_First) != _EQUAL_CHAR)
  235. {
  236. // Error! Must be '='. Ignore it.
  237. //_St |= _IOS_BADBIT;
  238. }
  239. else
  240. ++_First; // Skip '='
  241. // write 1 byte to output
  242. *_To = (byte_t) _3to4.get_0();
  243. return _First;
  244. }
  245. else
  246. _3to4.b64_2(_Char);
  247. // -- 3 --
  248. // Search next valid char...
  249. while(++_First != _Last)
  250. if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
  251. break;
  252. if(_First == _Last) {
  253. // Unexpected EOF. It's error. But ignore it.
  254. //_St |= _IOS_FAILBIT|_IOS_EOFBIT;
  255. _St |= _IOS_EOFBIT;
  256. return _First;
  257. }
  258. if(_Char == _EQUAL_CHAR)
  259. {
  260. // OK!
  261. _3to4.b64_3(0);
  262. // write to output 2 bytes
  263. *_To = (byte_t) _3to4.get_0();
  264. *_To = (byte_t) _3to4.get_1();
  265. ++_First; // set position to next character
  266. return _First;
  267. }
  268. else
  269. _3to4.b64_3(_Char);
  270. // write to output 3 bytes
  271. *_To = (byte_t) _3to4.get_0();
  272. *_To = (byte_t) _3to4.get_1();
  273. *_To = (byte_t) _3to4.get_2();
  274. ++_First;
  275. } // while(_First != _Last)
  276. return (_First);
  277. }
  278. protected:
  279. int _getCharType(int C) const
  280. {
  281. if(_base64Chars[62] == C)
  282. return 62;
  283. if(_base64Chars[63] == C)
  284. return 63;
  285. if((_base64Chars[0] <= C) && (_base64Chars[25] >= C))
  286. return C - _base64Chars[0];
  287. if((_base64Chars[26] <= C) && (_base64Chars[51] >= C))
  288. return C - _base64Chars[26] + 26;
  289. if((_base64Chars[52] <= C) && (_base64Chars[61] >= C))
  290. return C - _base64Chars[52] + 52;
  291. if(C == _Tr::to_int_type('='))
  292. return _EQUAL_CHAR;
  293. return _UNKNOWN_CHAR;
  294. }
  295. };
  296. #endif