24
|
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 <string.h>
|
|
30
|
|
31 static const std::string base64_chars =
|
|
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
33 "abcdefghijklmnopqrstuvwxyz"
|
|
34 "0123456789+/";
|
|
35
|
|
36
|
|
37 static inline bool is_base64(unsigned char c) {
|
|
38 return (isalnum(c) || (c == '+') || (c == '/'));
|
|
39 }
|
|
40
|
86
|
41 std::string base64_encode(const std::string& stringToEncode)
|
|
42 {
|
|
43 const unsigned char* bytes_to_encode = reinterpret_cast<const unsigned char*>
|
|
44 (stringToEncode.size() > 0 ? &stringToEncode[0] : NULL);
|
|
45 unsigned int in_len = stringToEncode.size();
|
24
|
46
|
|
47 std::string ret;
|
|
48 int i = 0;
|
|
49 int j = 0;
|
|
50 unsigned char char_array_3[3];
|
|
51 unsigned char char_array_4[4];
|
|
52
|
|
53 while (in_len--) {
|
|
54 char_array_3[i++] = *(bytes_to_encode++);
|
|
55 if (i == 3) {
|
|
56 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
57 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
58 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
59 char_array_4[3] = char_array_3[2] & 0x3f;
|
|
60
|
|
61 for(i = 0; (i <4) ; i++)
|
|
62 ret += base64_chars[char_array_4[i]];
|
|
63 i = 0;
|
|
64 }
|
|
65 }
|
|
66
|
|
67 if (i)
|
|
68 {
|
|
69 for(j = i; j < 3; j++)
|
|
70 char_array_3[j] = '\0';
|
|
71
|
|
72 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
|
73 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
|
74 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
|
75 char_array_4[3] = char_array_3[2] & 0x3f;
|
|
76
|
|
77 for (j = 0; (j < i + 1); j++)
|
|
78 ret += base64_chars[char_array_4[j]];
|
|
79
|
|
80 while((i++ < 3))
|
|
81 ret += '=';
|
|
82
|
|
83 }
|
|
84
|
|
85 return ret;
|
86
|
86 }
|
24
|
87
|
|
88
|
|
89 std::string base64_decode(const std::string& encoded_string) {
|
|
90 int in_len = encoded_string.size();
|
|
91 int i = 0;
|
|
92 int j = 0;
|
|
93 int in_ = 0;
|
|
94 unsigned char char_array_4[4], char_array_3[3];
|
|
95 std::string ret;
|
|
96
|
|
97 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
|
|
98 char_array_4[i++] = encoded_string[in_]; in_++;
|
|
99 if (i ==4) {
|
|
100 for (i = 0; i <4; i++)
|
|
101 char_array_4[i] = base64_chars.find(char_array_4[i]);
|
|
102
|
|
103 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
|
104 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
|
105 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
|
106
|
|
107 for (i = 0; (i < 3); i++)
|
|
108 ret += char_array_3[i];
|
|
109 i = 0;
|
|
110 }
|
|
111 }
|
|
112
|
|
113 if (i) {
|
|
114 for (j = i; j <4; j++)
|
|
115 char_array_4[j] = 0;
|
|
116
|
|
117 for (j = 0; j <4; j++)
|
|
118 char_array_4[j] = base64_chars.find(char_array_4[j]);
|
|
119
|
|
120 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
|
121 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
|
122 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
|
123
|
|
124 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
|
|
125 }
|
|
126
|
|
127 return ret;
|
|
128 }
|