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