summaryrefslogtreecommitdiffstats
path: root/base64.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-03-05 17:39:48 +0100
committermanuel <manuel@mausz.at>2013-03-05 17:39:48 +0100
commit310a2c101b32a5e71a616027b6a1b788a341bc02 (patch)
treea871e1dda8b1de141ae1400ba666fe3b1de96361 /base64.cpp
downloadtsclient-310a2c101b32a5e71a616027b6a1b788a341bc02.tar.gz
tsclient-310a2c101b32a5e71a616027b6a1b788a341bc02.tar.bz2
tsclient-310a2c101b32a5e71a616027b6a1b788a341bc02.zip
initial GPLv2 releaseHEADmaster
Diffstat (limited to 'base64.cpp')
-rw-r--r--base64.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/base64.cpp b/base64.cpp
new file mode 100644
index 0000000..38837fe
--- /dev/null
+++ b/base64.cpp
@@ -0,0 +1,126 @@
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14 *
15 * Authors: Manuel Mausz (manuel@mausz.at)
16 * Christian Raschko (c.raschko@netcore.at)
17 */
18
19//Header
20#include "base64.h"
21
22//Libraries
23#include <ctype.h>
24
25static const wxString base64_chars =
26 _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
27 _T("abcdefghijklmnopqrstuvwxyz")
28 _T("0123456789+/");
29
30
31static inline bool is_base64(unsigned char c)
32{
33 return (isalnum(c) || (c == '+') || (c == '/'));
34}
35
36wxString Base64Encode(char const* bytes_to_encode, unsigned int in_len)
37{
38 wxString ret;
39 int i = 0;
40 int j = 0;
41 unsigned char char_array_3[3];
42 unsigned char char_array_4[4];
43
44 while (in_len--)
45 {
46 char_array_3[i++] = *(bytes_to_encode++);
47 if (i == 3) {
48 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
49 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
50 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
51 char_array_4[3] = char_array_3[2] & 0x3f;
52
53 for(i = 0; i < 4; i++)
54 ret += base64_chars[char_array_4[i]];
55 i = 0;
56 }
57 }
58
59 if (i)
60 {
61 for(j = i; j < 3; j++)
62 char_array_3[j] = '\0';
63
64 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
65 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
66 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
67 char_array_4[3] = char_array_3[2] & 0x3f;
68
69 for (j = 0; j < i + 1; j++)
70 ret += base64_chars[char_array_4[j]];
71
72 while(i++ < 3)
73 ret += '=';
74 }
75
76 return ret;
77
78}
79
80wxString Base64Decode(wxString const& encoded_string)
81{
82 int in_len = encoded_string.size();
83 int i = 0;
84 int j = 0;
85 int in_ = 0;
86 unsigned char char_array_4[4], char_array_3[3];
87 wxString ret;
88
89 while (in_len-- && encoded_string[in_] != '=' && is_base64(encoded_string[in_]))
90 {
91 char_array_4[i++] = encoded_string[in_];
92 in_++;
93 if (i ==4)
94 {
95 for (i = 0; i < 4; i++)
96 char_array_4[i] = base64_chars.find(char_array_4[i]);
97
98 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
99 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
100 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
101
102 for (i = 0; i < 3; i++)
103 ret += char_array_3[i];
104 i = 0;
105 }
106 }
107
108 if (i)
109 {
110 for (j = i; j < 4; j++)
111 char_array_4[j] = 0;
112
113 for (j = 0; j < 4; j++)
114 char_array_4[j] = base64_chars.find(char_array_4[j]);
115
116 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
117 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
118 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
119
120 for (j = 0; j < i - 1; j++)
121 ret += char_array_3[j];
122 }
123
124 return ret;
125}
126