author | zecke <zecke> | 2004-02-20 17:59:17 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-02-20 17:59:17 (UTC) |
commit | d9e2ecd5619616d041ae8ac7a6f0776faee2310c (patch) (unidiff) | |
tree | 7210b88441b47f344e588e4743ecaf21fbb004c9 | |
parent | 974c01feac1a9749497bb1df50841e35cedb63b0 (diff) | |
download | opie-d9e2ecd5619616d041ae8ac7a6f0776faee2310c.zip opie-d9e2ecd5619616d041ae8ac7a6f0776faee2310c.tar.gz opie-d9e2ecd5619616d041ae8ac7a6f0776faee2310c.tar.bz2 |
Implement encodeBase64 and decodeBase64 LGPLed by KDE project
Use my version of generateUuid() ( LGPL )
return two variables.. hardly licensable
Rest will do alwin and tille! clean room
-rw-r--r-- | libopie2/opiecore/oglobal.cpp | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/libopie2/opiecore/oglobal.cpp b/libopie2/opiecore/oglobal.cpp index 3390ec0..352151b 100644 --- a/libopie2/opiecore/oglobal.cpp +++ b/libopie2/opiecore/oglobal.cpp | |||
@@ -2,4 +2,5 @@ | |||
2 | This file is part of the Opie Project | 2 | This file is part of the Opie Project |
3 | Copyright (C) 2003 Michael 'Mickey' Lauer <mickey@Vanille.de> | 3 | Copyright (C) 2003 Michael 'Mickey' Lauer <mickey@Vanille.de> |
4 | Copyright (C) 2004 Holger 'zecke' Freyther <zecke@handhelds.org> | ||
4 | =. | 5 | =. |
5 | .=l. | 6 | .=l. |
@@ -30,4 +31,40 @@ | |||
30 | #include <opie2/oglobal.h> | 31 | #include <opie2/oglobal.h> |
31 | 32 | ||
33 | #include <qfile.h> | ||
34 | #include <qtextstream.h> | ||
35 | |||
36 | static const char Base64EncMap[64] = | ||
37 | { | ||
38 | 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | ||
39 | 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, | ||
40 | 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | ||
41 | 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, | ||
42 | 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, | ||
43 | 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, | ||
44 | 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, | ||
45 | 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F | ||
46 | }; | ||
47 | |||
48 | static char Base64DecMap[128] = | ||
49 | { | ||
50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
55 | 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, | ||
56 | 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, | ||
57 | 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
58 | 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | ||
59 | 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, | ||
60 | 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, | ||
61 | 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
62 | 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, | ||
63 | 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | ||
64 | 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, | ||
65 | 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 | ||
66 | }; | ||
67 | |||
68 | |||
32 | OConfig* OGlobal::_config = 0; | 69 | OConfig* OGlobal::_config = 0; |
33 | 70 | ||
@@ -42,2 +79,227 @@ OConfig* OGlobal::config() | |||
42 | return OGlobal::_config; | 79 | return OGlobal::_config; |
43 | } | 80 | } |
81 | |||
82 | |||
83 | /** | ||
84 | * Return the internal builtin Global::Command object | ||
85 | * | ||
86 | */ | ||
87 | Global::Command* OGlobal::builtinCommands() { | ||
88 | return builtin; | ||
89 | } | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Return the internal builtin QGuardedPtr<QWidget> object | ||
94 | */ | ||
95 | QGuardedPtr<QWidget>* OGlobal::builtinRunning() { | ||
96 | return running; | ||
97 | } | ||
98 | |||
99 | |||
100 | /** | ||
101 | * \brief generate a new UUID as QString | ||
102 | * Return a new UUID as QString. UUID are global unique | ||
103 | * | ||
104 | * | ||
105 | * @return the UUID or QString::null | ||
106 | */ | ||
107 | QString OGlobal::generateUuid() { | ||
108 | QFile file( "/proc/sys/kernel/random/uuid" ); | ||
109 | if (!file.open(IO_ReadOnly ) ) | ||
110 | return QString::null; | ||
111 | |||
112 | QTextStream stream(&file); | ||
113 | |||
114 | return "{" + stream.read().stripWhiteSpace() + "}"; | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * \brief Encode a QByteArray in base64 | ||
120 | * | ||
121 | * An Implementation of the RF1521 base64 encoding. | ||
122 | * | ||
123 | * The boolean argument determines if the encoded data is | ||
124 | * going to be restricted to 76 characters or less per line | ||
125 | * as specified by RFC 2045. If @p insertLFs is true, then | ||
126 | * there will be 76 characters or less per line. | ||
127 | * | ||
128 | * If you use this to create a QCString remember that it is null terminated! | ||
129 | * \code | ||
130 | * QByteArray ar = OGlobal::encodeBase64(&array); | ||
131 | * QCString str(ar.data(),ar.size()+1); // the NUL at the end | ||
132 | * | ||
133 | * \endcode | ||
134 | * | ||
135 | * @param in The QByteArray to encode | ||
136 | * @param insertLFs Limit number of characters per line | ||
137 | * @return The argument as base64 encoded or QByteArray() if in.isEmpty() | ||
138 | * @see QByteArray | ||
139 | * @see QArray | ||
140 | * @see QMemArray | ||
141 | * @see QCString | ||
142 | */ | ||
143 | /* | ||
144 | * LGPL by Rik Hemsely of the KDE Project. taken from kmdcodec.cpp | ||
145 | */ | ||
146 | QByteArray OGlobal::encodeBase64(const QByteArray& in, bool insertLFs ) { | ||
147 | if ( in.isEmpty() ) | ||
148 | return QByteArray(); | ||
149 | |||
150 | unsigned int sidx = 0; | ||
151 | unsigned int didx = 0; | ||
152 | const char* data = in.data(); | ||
153 | const unsigned int len = in.size(); | ||
154 | |||
155 | unsigned int out_len = ((len+2)/3)*4; | ||
156 | |||
157 | // Deal with the 76 characters or less per | ||
158 | // line limit specified in RFC 2045 on a | ||
159 | // pre request basis. | ||
160 | insertLFs = (insertLFs && out_len > 76); | ||
161 | if ( insertLFs ) | ||
162 | out_len += ((out_len-1)/76); | ||
163 | |||
164 | int count = 0; | ||
165 | QByteArray out( out_len ); | ||
166 | |||
167 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion | ||
168 | if ( len > 1 ) | ||
169 | { | ||
170 | while (sidx < len-2) | ||
171 | { | ||
172 | if ( insertLFs ) | ||
173 | { | ||
174 | if ( count && (count%76) == 0 ) | ||
175 | out[didx++] = '\n'; | ||
176 | count += 4; | ||
177 | } | ||
178 | out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; | ||
179 | out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 | | ||
180 | (data[sidx] << 4) & 077]; | ||
181 | out[didx++] = Base64EncMap[(data[sidx+2] >> 6) & 003 | | ||
182 | (data[sidx+1] << 2) & 077]; | ||
183 | out[didx++] = Base64EncMap[data[sidx+2] & 077]; | ||
184 | sidx += 3; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | if (sidx < len) | ||
189 | { | ||
190 | if ( insertLFs && (count > 0) && (count%76) == 0 ) | ||
191 | out[didx++] = '\n'; | ||
192 | |||
193 | out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; | ||
194 | if (sidx < len-1) | ||
195 | { | ||
196 | out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 | | ||
197 | (data[sidx] << 4) & 077]; | ||
198 | out[didx++] = Base64EncMap[(data[sidx+1] << 2) & 077]; | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | out[didx++] = Base64EncMap[(data[sidx] << 4) & 077]; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | // Add padding | ||
207 | while (didx < out.size()) | ||
208 | { | ||
209 | out[didx] = '='; | ||
210 | didx++; | ||
211 | } | ||
212 | |||
213 | return out; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * Decodes the given data that was encoded with the base64 | ||
218 | * algorithm. | ||
219 | * | ||
220 | * | ||
221 | * @param in the encoded data to be decoded. | ||
222 | * @return the decoded QByteArray or QByteArray() in case of failure | ||
223 | * @see OGlobal::encodeBase64 | ||
224 | */ | ||
225 | QByteArray OGlobal::decodeBase64( const QByteArray& in) { | ||
226 | if ( in.isEmpty() ) | ||
227 | return QByteArray(); | ||
228 | |||
229 | QByteArray out; | ||
230 | unsigned int count = 0; | ||
231 | unsigned int len = in.size(), tail = len; | ||
232 | const char* data = in.data(); | ||
233 | |||
234 | // Deal with possible *nix "BEGIN" marker!! | ||
235 | while ( count < len && (data[count] == '\n' || data[count] == '\r' || | ||
236 | data[count] == '\t' || data[count] == ' ') ) | ||
237 | count++; | ||
238 | |||
239 | if ( strncasecmp(data+count, "begin", 5) == 0 ) | ||
240 | { | ||
241 | count += 5; | ||
242 | while ( count < len && data[count] != '\n' && data[count] != '\r' ) | ||
243 | count++; | ||
244 | |||
245 | while ( count < len && (data[count] == '\n' || data[count] == '\r') ) | ||
246 | count ++; | ||
247 | |||
248 | data += count; | ||
249 | tail = (len -= count); | ||
250 | } | ||
251 | |||
252 | // Find the tail end of the actual encoded data even if | ||
253 | // there is/are trailing CR and/or LF. | ||
254 | while ( data[tail-1] == '=' || data[tail-1] == '\n' || | ||
255 | data[tail-1] == '\r' ) | ||
256 | if ( data[--tail] != '=' ) len = tail; | ||
257 | |||
258 | unsigned int outIdx = 0; | ||
259 | out.resize( (count=len) ); | ||
260 | for (unsigned int idx = 0; idx < count; idx++) | ||
261 | { | ||
262 | // Adhere to RFC 2045 and ignore characters | ||
263 | // that are not part of the encoding table. | ||
264 | unsigned char ch = data[idx]; | ||
265 | if ( (ch > 47 && ch < 58) || (ch > 64 && ch < 91 ) || | ||
266 | (ch > 96 && ch < 123)|| ch == '+' || ch == '/' || ch == '=') | ||
267 | { | ||
268 | out[outIdx++] = Base64DecMap[ch]; | ||
269 | } | ||
270 | else | ||
271 | { | ||
272 | len--; | ||
273 | tail--; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | // kdDebug() << "Tail size = " << tail << ", Length size = " << len << endl; | ||
278 | |||
279 | // 4-byte to 3-byte conversion | ||
280 | len = (tail>(len/4)) ? tail-(len/4) : 0; | ||
281 | unsigned int sidx = 0, didx = 0; | ||
282 | if ( len > 1 ) | ||
283 | { | ||
284 | while (didx < len-2) | ||
285 | { | ||
286 | out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); | ||
287 | out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); | ||
288 | out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077)); | ||
289 | sidx += 4; | ||
290 | didx += 3; | ||
291 | } | ||
292 | } | ||
293 | |||
294 | if (didx < len) | ||
295 | out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); | ||
296 | |||
297 | if (++didx < len ) | ||
298 | out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); | ||
299 | |||
300 | // Resize the output buffer | ||
301 | if ( len == 0 || len < out.size() ) | ||
302 | out.resize(len); | ||
303 | |||
304 | return out; | ||
305 | } | ||