Debian Codegraph @

rudeconfig/src/Base64Encoder.cpp

raw
1 // Base64Encoder.cpp
2 //
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Matthew Flood
4 // See file AUTHORS for contact information
5 //
6 // This file is part of RudeConfig.
7 //
8 // RudeConfig is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // RudeConfig is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with RudeConfig; (see COPYING) if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 // 02111-1307, USA.
22 //------------------------------------------------------------------------
23
24 #include "../config.h"
25
26 #include "Base64Encoder.h"
27
28 #ifndef INCLUDED_CSTRING
29 #include <string.h>
30 #define INCLUDED_CSTRING
31 #endif
32
33 namespace rude{
34 namespace config{
35
36 // translates a 6 bit char into a base-64 character
37 //
38 inline char Base64Encoder::c_encode(char uc)
39 {
40 if (uc < 26)
41 {
42 return 'A'+uc;
43 }
44 if (uc < 52)
45 {
46 return 'a'+(uc-26);
47 }
48 if (uc < 62)
49 {
50 return '0'+(uc-52);
51 }
52 if (uc == 62)
53 {
54 return '+';
55 }
56 return '/';
57 }
58
59 // translates one base-64 character into a six-bit pattern
60 //
61 inline unsigned char Base64Encoder::c_decode(char c)
62 {
63 if (c >= 'A' && c <= 'Z')
64 {
65 return c - 'A';
66 }
67 if (c >= 'a' && c <= 'z')
68 {
69 return c - 'a' + 26;
70 }
71 if (c >= '0' && c <= '9')
72 {
73 return c - '0' + 52;
74 }
75 if (c == '+')
76 {
77 return 62;
78 };
79 return 63;
80 }
81
82
83 char * Base64Encoder::encode(const char *data, int datalength, int &outlength)
84 {
85
86
87 int linelength=0;
88 outlength=0;
89
90 if (data == (char*) 0 || datalength == 0)
91 {
92 return (char*) 0;
93 };
94
95 char *retval=new char[datalength * 2];
96 const char *crlf="\n";
97 int crlflength=strlen(crlf);
98 int maxlinelength=76;
99
100 for (int i=0; i<datalength; i+=3)
101 {
102 unsigned char by1, by2, by3, by4, by5, by6, by7;
103 by1 = data[i];
104
105 if (i+1 < datalength)
106 {
107 by2 = data[i+1];
108 }
109 else
110 {
111 by2=0;
112 }
113
114 if (i+2 < datalength)
115 {
116 by3 = data[i+2];
117 }
118 else
119 {
120 by3=0;
121 }
122
123 by4 = by1>>2;
124 by5 = ((by1 & 0x3)<<4)|( by2>>4 );
125 by6 = ((by2 & 0xf)<<2)|( by3>>6 );
126 by7 = by3 & 0x3f;
127
128
129
130 retval[outlength++] = c_encode(by4);
131 linelength++;
132
133 retval[outlength++] = c_encode(by5);
134 linelength++;
135
136 if (i+1 < datalength)
137 {
138 retval[outlength++]= c_encode(by6);
139 linelength++;
140 }
141 else
142 {
143 retval[outlength++]= '=';
144 linelength++;
145 }
146
147 if (i+2<datalength)
148 {
149 retval[outlength++]= c_encode(by7);
150 linelength++;
151 }
152 else
153 {
154 retval[outlength++]= '=';
155 linelength++;
156 };
157
158 // only set linelength if maxlinelength > 0
159 //
160 if (maxlinelength != 0 && linelength >= maxlinelength)
161 {
162 for(int y=0; y< crlflength; y++)
163 {
164 retval[outlength++]= crlf[y];
165 }
166
167 // we just created a line - reset current linelength
168 //
169 linelength=0;
170 }
171 };
172 retval[outlength]=(char) NULL;
173 return retval;
174 }
175
176
177 char * Base64Encoder::decode(const char *data, int datalength, int &outlength)
178 {
179 // Error if incoming data is NULL
180 // Error if nothing to decode
181 //
182 if(data == (char *) 0 || datalength == 0)
183 {
184 return (char*) 0;
185 }
186
187
188 outlength=0;
189
190 // rawlength is the length of the encoded data excluding
191 // any non-base64 characters
192 //
193 int rawlength=0;
194
195
196 // create return buffer
197 //
198 char *outbuffer=new char[datalength];
199
200 // copy all base64 characters into outbuffer,
201 // in other words, strip away CRLF's and non-b64 characters...
202 //
203 for (int j=0;j < datalength; j++)
204 {
205 if (IsBase64(data[j]))
206 {
207 outbuffer[rawlength++]= data[j];
208 }
209 }
210
211 // there's no base64 characters to decode
212 //
213 if (rawlength == 0 || datalength == 0)
214 {
215 delete [] outbuffer;
216 return (char*) NULL;
217 }
218
219
220
221 for (int i=0; i<rawlength; i+=4)
222
223 {
224
225
226
227 char c1 = outbuffer[i];
228 char c2='A',c3='A',c4='A';
229
230 if (i+1 < rawlength)
231
232 {
233
234 c2 = outbuffer[i+1];
235
236 };
237
238 if (i+2 < rawlength)
239
240 {
241
242 c3 = outbuffer[i+2];
243
244 };
245
246 if (i+3 < rawlength)
247
248 {
249
250 c4 = outbuffer[i+3];
251
252 };
253
254
255
256 unsigned char by1,by2,by3,by4;
257
258 by1 = c_decode(c1);
259
260 by2 = c_decode(c2);
261
262 by3 = c_decode(c3);
263
264 by4 = c_decode(c4);
265
266
267
268 outbuffer[outlength]=( (by1<<2)|(by2>>4) );
269 outlength++;
270
271
272 if (c3 != '=')
273
274 {
275
276 outbuffer[outlength]=( ((by2&0xf)<<4)|(by3>>2) );
277 outlength++;
278 }
279
280
281
282 if (c4 != '=')
283
284 {
285
286 outbuffer[outlength]=( ((by3&0x3)<<6)|by4 );
287 outlength++;
288 };
289
290 };
291 // NULL terminate decoded data
292 // in case caller ignores outlength
293 // for text data
294 //
295 outbuffer[outlength]=(char) 0;
296 return outbuffer;
297 }
298
299 //The last helper function returns true is a character is a valid
300 //base-64 character and false otherwise.
301 inline bool Base64Encoder::IsBase64(char c)
302 {
303 if (c >= 'A' && c <= 'Z')
304 {
305 return true;
306 }
307 if (c >= 'a' && c <= 'z')
308 {
309 return true;
310 }
311 if (c >= '0' && c <= '9')
312 {
313 return true;
314 }
315 if (c == '+')
316 {
317 return true;
318 };
319 if (c == '/')
320 {
321 return true;
322 };
323 if (c == '=')
324 {
325 return true;
326 };
327 return false;
328 }
329
330 }}
331