v0.2.2 release
[yandexfotkisp] / src / base64.cpp
1 /*\r
2    base64.cpp and base64.h\r
3 \r
4    Copyright (C) 2004-2008 RenĂ© Nyffenegger\r
5 \r
6    This source code is provided 'as-is', without any express or implied\r
7    warranty. In no event will the author be held liable for any damages\r
8    arising from the use of this software.\r
9 \r
10    Permission is granted to anyone to use this software for any purpose,\r
11    including commercial applications, and to alter it and redistribute it\r
12    freely, subject to the following restrictions:\r
13 \r
14    1. The origin of this source code must not be misrepresented; you must not\r
15       claim that you wrote the original source code. If you use this source code\r
16       in a product, an acknowledgment in the product documentation would be\r
17       appreciated but is not required.\r
18 \r
19    2. Altered source versions must be plainly marked as such, and must not be\r
20       misrepresented as being the original source code.\r
21 \r
22    3. This notice may not be removed or altered from any source distribution.\r
23 \r
24    RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch\r
25 \r
26 */\r
27 \r
28 #include "base64.h"\r
29 #include <iostream>\r
30 \r
31 static const std::string base64_chars =\r
32              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\r
33              "abcdefghijklmnopqrstuvwxyz"\r
34              "0123456789+/";\r
35 \r
36 \r
37 static inline bool is_base64(unsigned char c) {\r
38   return (isalnum(c) || (c == '+') || (c == '/'));\r
39 }\r
40 \r
41 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {\r
42   std::string ret;\r
43   int i = 0;\r
44   int j = 0;\r
45   unsigned char char_array_3[3];\r
46   unsigned char char_array_4[4];\r
47 \r
48   while (in_len--) {\r
49     char_array_3[i++] = *(bytes_to_encode++);\r
50     if (i == 3) {\r
51       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;\r
52       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);\r
53       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);\r
54       char_array_4[3] = char_array_3[2] & 0x3f;\r
55 \r
56       for(i = 0; (i <4) ; i++)\r
57         ret += base64_chars[char_array_4[i]];\r
58       i = 0;\r
59     }\r
60   }\r
61 \r
62   if (i)\r
63   {\r
64     for(j = i; j < 3; j++)\r
65       char_array_3[j] = '\0';\r
66 \r
67     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;\r
68     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);\r
69     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);\r
70     char_array_4[3] = char_array_3[2] & 0x3f;\r
71 \r
72     for (j = 0; (j < i + 1); j++)\r
73       ret += base64_chars[char_array_4[j]];\r
74 \r
75     while((i++ < 3))\r
76       ret += '=';\r
77 \r
78   }\r
79 \r
80   return ret;\r
81 \r
82 }\r
83 \r
84 std::string base64_decode(std::string const& encoded_string) {\r
85   int in_len = encoded_string.size();\r
86   int i = 0;\r
87   int j = 0;\r
88   int in_ = 0;\r
89   unsigned char char_array_4[4], char_array_3[3];\r
90   std::string ret;\r
91 \r
92   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {\r
93     char_array_4[i++] = encoded_string[in_]; in_++;\r
94     if (i ==4) {\r
95       for (i = 0; i <4; i++)\r
96         char_array_4[i] = base64_chars.find(char_array_4[i]);\r
97 \r
98       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);\r
99       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);\r
100       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];\r
101 \r
102       for (i = 0; (i < 3); i++)\r
103                   ret.append (1,(char)char_array_3[i]);\r
104       i = 0;\r
105     }\r
106   }\r
107 \r
108   if (i) {\r
109     for (j = i; j <4; j++)\r
110       char_array_4[j] = 0;\r
111 \r
112     for (j = 0; j <4; j++)\r
113       char_array_4[j] = base64_chars.find(char_array_4[j]);\r
114 \r
115     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);\r
116     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);\r
117     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];\r
118 \r
119         for (j = 0; (j < i - 1); j++) ret.append (1,char_array_3[j]);\r
120   }\r
121 \r
122   return ret;\r
123 }\r