-rw-r--r-- | microkde/kdecore/kmdcodec.cpp | 139 |
1 files changed, 69 insertions, 70 deletions
diff --git a/microkde/kdecore/kmdcodec.cpp b/microkde/kdecore/kmdcodec.cpp index bc03569..db11e52 100644 --- a/microkde/kdecore/kmdcodec.cpp +++ b/microkde/kdecore/kmdcodec.cpp | |||
@@ -18,48 +18,50 @@ | |||
18 | RFC 1321 "MD5 Message-Digest Algorithm" Copyright (C) 1991-1992. | 18 | RFC 1321 "MD5 Message-Digest Algorithm" Copyright (C) 1991-1992. |
19 | RSA Data Security, Inc. Created 1991. All rights reserved. | 19 | RSA Data Security, Inc. Created 1991. All rights reserved. |
20 | 20 | ||
21 | The KMD5 class is based on a C++ implementation of | 21 | The KMD5 class is based on a C++ implementation of |
22 | "RSA Data Security, Inc. MD5 Message-Digest Algorithm" by | 22 | "RSA Data Security, Inc. MD5 Message-Digest Algorithm" by |
23 | Mordechai T. Abzug,Copyright (c) 1995. This implementation | 23 | Mordechai T. Abzug,Copyright (c) 1995. This implementation |
24 | passes the test-suite as defined in RFC 1321. | 24 | passes the test-suite as defined in RFC 1321. |
25 | 25 | ||
26 | The encoding and decoding utilities in KCodecs with the exception of | 26 | The encoding and decoding utilities in KCodecs with the exception of |
27 | quoted-printable are based on the java implementation in HTTPClient | 27 | quoted-printable are based on the java implementation in HTTPClient |
28 | package by Ronald Tschal� Copyright (C) 1996-1999. | 28 | package by Ronald Tschal� Copyright (C) 1996-1999. |
29 | 29 | ||
30 | The quoted-printable codec as described in RFC 2045, section 6.7. is by | 30 | The quoted-printable codec as described in RFC 2045, section 6.7. is by |
31 | Rik Hemsley (C) 2001. | 31 | Rik Hemsley (C) 2001. |
32 | */ | 32 | */ |
33 | 33 | ||
34 | //US #include <config.h> | 34 | //US #include <config.h> |
35 | 35 | ||
36 | #include <stdio.h> | 36 | #include <stdio.h> |
37 | #include <string.h> | 37 | #include <string.h> |
38 | #include <stdlib.h> | 38 | #include <stdlib.h> |
39 | 39 | ||
40 | #include <kdebug.h> | 40 | #include <kdebug.h> |
41 | #include "kmdcodec.h" | 41 | #include "kmdcodec.h" |
42 | //Added by qt3to4: | ||
43 | #include <Q3CString> | ||
42 | 44 | ||
43 | #define KMD5_S11 7 | 45 | #define KMD5_S11 7 |
44 | #define KMD5_S12 12 | 46 | #define KMD5_S12 12 |
45 | #define KMD5_S13 17 | 47 | #define KMD5_S13 17 |
46 | #define KMD5_S14 22 | 48 | #define KMD5_S14 22 |
47 | #define KMD5_S21 5 | 49 | #define KMD5_S21 5 |
48 | #define KMD5_S22 9 | 50 | #define KMD5_S22 9 |
49 | #define KMD5_S23 14 | 51 | #define KMD5_S23 14 |
50 | #define KMD5_S24 20 | 52 | #define KMD5_S24 20 |
51 | #define KMD5_S31 4 | 53 | #define KMD5_S31 4 |
52 | #define KMD5_S32 11 | 54 | #define KMD5_S32 11 |
53 | #define KMD5_S33 16 | 55 | #define KMD5_S33 16 |
54 | #define KMD5_S34 23 | 56 | #define KMD5_S34 23 |
55 | #define KMD5_S41 6 | 57 | #define KMD5_S41 6 |
56 | #define KMD5_S42 10 | 58 | #define KMD5_S42 10 |
57 | #define KMD5_S43 15 | 59 | #define KMD5_S43 15 |
58 | #define KMD5_S44 21 | 60 | #define KMD5_S44 21 |
59 | 61 | ||
60 | const char KCodecs::Base64EncMap[64] = | 62 | const char KCodecs::Base64EncMap[64] = |
61 | { | 63 | { |
62 | 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | 64 | 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, |
63 | 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, | 65 | 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, |
64 | 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | 66 | 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, |
65 | 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, | 67 | 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
@@ -126,56 +128,56 @@ const char KCodecs::hexChars[16] = | |||
126 | '0', '1', '2', '3', '4', '5', '6', '7', | 128 | '0', '1', '2', '3', '4', '5', '6', '7', |
127 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' | 129 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
128 | }; | 130 | }; |
129 | 131 | ||
130 | const unsigned int KCodecs::maxQPLineLength = 70; | 132 | const unsigned int KCodecs::maxQPLineLength = 70; |
131 | 133 | ||
132 | 134 | ||
133 | /******************************** KCodecs ********************************/ | 135 | /******************************** KCodecs ********************************/ |
134 | // strchr(3) for broken systems. | 136 | // strchr(3) for broken systems. |
135 | static int rikFindChar(register const char * _s, const char c) | 137 | static int rikFindChar(register const char * _s, const char c) |
136 | { | 138 | { |
137 | register const char * s = _s; | 139 | register const char * s = _s; |
138 | 140 | ||
139 | while (true) | 141 | while (true) |
140 | { | 142 | { |
141 | if ((0 == *s) || (c == *s)) break; ++s; | 143 | if ((0 == *s) || (c == *s)) break; ++s; |
142 | if ((0 == *s) || (c == *s)) break; ++s; | 144 | if ((0 == *s) || (c == *s)) break; ++s; |
143 | if ((0 == *s) || (c == *s)) break; ++s; | 145 | if ((0 == *s) || (c == *s)) break; ++s; |
144 | if ((0 == *s) || (c == *s)) break; ++s; | 146 | if ((0 == *s) || (c == *s)) break; ++s; |
145 | } | 147 | } |
146 | 148 | ||
147 | return s - _s; | 149 | return s - _s; |
148 | } | 150 | } |
149 | 151 | ||
150 | QCString KCodecs::quotedPrintableEncode(const QByteArray& in, bool useCRLF) | 152 | Q3CString KCodecs::quotedPrintableEncode(const QByteArray& in, bool useCRLF) |
151 | { | 153 | { |
152 | QByteArray out; | 154 | QByteArray out; |
153 | quotedPrintableEncode (in, out, useCRLF); | 155 | quotedPrintableEncode (in, out, useCRLF); |
154 | return QCString (out.data(), out.size()+1); | 156 | return Q3CString (out.data(), out.size()+1); |
155 | } | 157 | } |
156 | 158 | ||
157 | QCString KCodecs::quotedPrintableEncode(const QCString& str, bool useCRLF) | 159 | Q3CString KCodecs::quotedPrintableEncode(const Q3CString& str, bool useCRLF) |
158 | { | 160 | { |
159 | if (str.isEmpty()) | 161 | if (str.isEmpty()) |
160 | return ""; | 162 | return ""; |
161 | 163 | ||
162 | QByteArray in (str.length()); | 164 | QByteArray in (str.length()); |
163 | memcpy (in.data(), str.data(), str.length()); | 165 | memcpy (in.data(), str.data(), str.length()); |
164 | return quotedPrintableEncode(in, useCRLF); | 166 | return quotedPrintableEncode(in, useCRLF); |
165 | } | 167 | } |
166 | 168 | ||
167 | void KCodecs::quotedPrintableEncode(const QByteArray& in, QByteArray& out, bool useCRLF) | 169 | void KCodecs::quotedPrintableEncode(const QByteArray& in, QByteArray& out, bool useCRLF) |
168 | { | 170 | { |
169 | out.resize (0); | 171 | out.resize (0); |
170 | if (in.isEmpty()) | 172 | if (in.isEmpty()) |
171 | return; | 173 | return; |
172 | 174 | ||
173 | char *cursor; | 175 | char *cursor; |
174 | const char *data; | 176 | const char *data; |
175 | unsigned int lineLength; | 177 | unsigned int lineLength; |
176 | unsigned int pos; | 178 | unsigned int pos; |
177 | 179 | ||
178 | const unsigned int length = in.size(); | 180 | const unsigned int length = in.size(); |
179 | const unsigned int end = length - 1; | 181 | const unsigned int end = length - 1; |
180 | 182 | ||
181 | 183 | ||
@@ -259,56 +261,56 @@ void KCodecs::quotedPrintableEncode(const QByteArray& in, QByteArray& out, bool | |||
259 | 261 | ||
260 | lineLength += 3; | 262 | lineLength += 3; |
261 | } | 263 | } |
262 | 264 | ||
263 | // If we're approaching the maximum line length, do a soft line break. | 265 | // If we're approaching the maximum line length, do a soft line break. |
264 | 266 | ||
265 | if ((lineLength > maxQPLineLength) && (i < end)) | 267 | if ((lineLength > maxQPLineLength) && (i < end)) |
266 | { | 268 | { |
267 | if (useCRLF) { | 269 | if (useCRLF) { |
268 | *cursor++ = '='; | 270 | *cursor++ = '='; |
269 | *cursor++ = '\r'; | 271 | *cursor++ = '\r'; |
270 | *cursor++ = '\n'; | 272 | *cursor++ = '\n'; |
271 | } else { | 273 | } else { |
272 | *cursor++ = '='; | 274 | *cursor++ = '='; |
273 | *cursor++ = '\n'; | 275 | *cursor++ = '\n'; |
274 | } | 276 | } |
275 | 277 | ||
276 | lineLength = 0; | 278 | lineLength = 0; |
277 | } | 279 | } |
278 | } | 280 | } |
279 | 281 | ||
280 | out.truncate(cursor - out.data()); | 282 | out.truncate(cursor - out.data()); |
281 | } | 283 | } |
282 | 284 | ||
283 | QCString KCodecs::quotedPrintableDecode(const QByteArray & in) | 285 | Q3CString KCodecs::quotedPrintableDecode(const QByteArray & in) |
284 | { | 286 | { |
285 | QByteArray out; | 287 | QByteArray out; |
286 | quotedPrintableDecode (in, out); | 288 | quotedPrintableDecode (in, out); |
287 | return QCString (out.data(), out.size()+1); | 289 | return Q3CString (out.data(), out.size()+1); |
288 | } | 290 | } |
289 | 291 | ||
290 | QCString KCodecs::quotedPrintableDecode(const QCString & str) | 292 | Q3CString KCodecs::quotedPrintableDecode(const Q3CString & str) |
291 | { | 293 | { |
292 | if (str.isEmpty()) | 294 | if (str.isEmpty()) |
293 | return ""; | 295 | return ""; |
294 | 296 | ||
295 | QByteArray in (str.length()); | 297 | QByteArray in (str.length()); |
296 | memcpy (in.data(), str.data(), str.length()); | 298 | memcpy (in.data(), str.data(), str.length()); |
297 | return quotedPrintableDecode (in); | 299 | return quotedPrintableDecode (in); |
298 | } | 300 | } |
299 | 301 | ||
300 | void KCodecs::quotedPrintableDecode(const QByteArray& in, QByteArray& out) | 302 | void KCodecs::quotedPrintableDecode(const QByteArray& in, QByteArray& out) |
301 | { | 303 | { |
302 | // clear out the output buffer | 304 | // clear out the output buffer |
303 | out.resize (0); | 305 | out.resize (0); |
304 | if (in.isEmpty()) | 306 | if (in.isEmpty()) |
305 | return; | 307 | return; |
306 | 308 | ||
307 | char *cursor; | 309 | char *cursor; |
308 | const char *data; | 310 | const char *data; |
309 | const unsigned int length = in.size(); | 311 | const unsigned int length = in.size(); |
310 | 312 | ||
311 | data = in.data(); | 313 | data = in.data(); |
312 | out.resize (length); | 314 | out.resize (length); |
313 | cursor = out.data(); | 315 | cursor = out.data(); |
314 | 316 | ||
@@ -334,348 +336,345 @@ void KCodecs::quotedPrintableDecode(const QByteArray& in, QByteArray& out) | |||
334 | else | 336 | else |
335 | { | 337 | { |
336 | // =XX encoded byte. | 338 | // =XX encoded byte. |
337 | 339 | ||
338 | int hexChar0 = rikFindChar(hexChars, c1); | 340 | int hexChar0 = rikFindChar(hexChars, c1); |
339 | int hexChar1 = rikFindChar(hexChars, c2); | 341 | int hexChar1 = rikFindChar(hexChars, c2); |
340 | 342 | ||
341 | if (hexChar0 < 16 && hexChar1 < 16) | 343 | if (hexChar0 < 16 && hexChar1 < 16) |
342 | { | 344 | { |
343 | *cursor++ = char((hexChar0 * 16) | hexChar1); | 345 | *cursor++ = char((hexChar0 * 16) | hexChar1); |
344 | i += 2; | 346 | i += 2; |
345 | } | 347 | } |
346 | } | 348 | } |
347 | } | 349 | } |
348 | } | 350 | } |
349 | else | 351 | else |
350 | { | 352 | { |
351 | *cursor++ = c; | 353 | *cursor++ = c; |
352 | } | 354 | } |
353 | } | 355 | } |
354 | 356 | ||
355 | out.truncate(cursor - out.data()); | 357 | out.truncate(cursor - out.data()); |
356 | } | 358 | } |
357 | 359 | ||
358 | QCString KCodecs::base64Encode( const QCString& str, bool insertLFs ) | 360 | Q3CString KCodecs::base64Encode( const Q3CString& str, bool insertLFs ) |
359 | { | 361 | { |
360 | if ( str.isEmpty() ) | 362 | if ( str.isEmpty() ) |
361 | return ""; | 363 | return ""; |
362 | 364 | ||
363 | QByteArray in (str.length()); | 365 | QByteArray in (str.length()); |
364 | memcpy( in.data(), str.data(), str.length() ); | 366 | memcpy( in.data(), str.data(), str.length() ); |
365 | return base64Encode( in, insertLFs ); | 367 | return base64Encode( in, insertLFs ); |
366 | } | 368 | } |
367 | 369 | ||
368 | QCString KCodecs::base64Encode( const QByteArray& in, bool insertLFs ) | 370 | Q3CString KCodecs::base64Encode( const QByteArray& in, bool insertLFs ) |
369 | { | 371 | { |
370 | QByteArray out; | 372 | QByteArray out; |
371 | base64Encode( in, out, insertLFs ); | 373 | base64Encode( in, out, insertLFs ); |
372 | return QCString( out.data(), out.size()+1 ); | 374 | return Q3CString( out.data(), out.size()+1 ); |
373 | } | 375 | } |
374 | 376 | ||
375 | void KCodecs::base64Encode( const QByteArray& in, QByteArray& out, | 377 | void KCodecs::base64Encode( const QByteArray& in, QByteArray& out, |
376 | bool insertLFs ) | 378 | bool insertLFs ) |
377 | { | 379 | { |
378 | // clear out the output buffer | 380 | // clear out the output buffer |
379 | out.resize (0); | 381 | out.resize (0); |
380 | if ( in.isEmpty() ) | 382 | if ( in.isEmpty() ) |
381 | return; | 383 | return; |
382 | 384 | ||
383 | unsigned int sidx = 0; | 385 | unsigned int sidx = 0; |
384 | unsigned int didx = 0; | 386 | unsigned int didx = 0; |
385 | const char* data = in.data(); | 387 | const char* data = in.data(); |
386 | const unsigned int len = in.size(); | 388 | const unsigned int len = in.size(); |
387 | 389 | ||
388 | unsigned int out_len = ((len+2)/3)*4; | 390 | unsigned int out_len = ((len+2)/3)*4; |
389 | 391 | ||
390 | // Deal with the 76 characters or less per | 392 | // Deal with the 76 characters or less per |
391 | // line limit specified in RFC 2045 on a | 393 | // line limit specified in RFC 2045 on a |
392 | // pre request basis. | 394 | // pre request basis. |
393 | insertLFs = (insertLFs && out_len > 76); | 395 | insertLFs = (insertLFs && out_len > 76); |
394 | if ( insertLFs ) | 396 | if ( insertLFs ) |
395 | out_len += ((out_len-1)/76); | 397 | out_len += ((out_len-1)/76); |
396 | 398 | ||
397 | int count = 0; | 399 | int count = 0; |
398 | out.resize( out_len ); | 400 | out.resize( out_len ); |
399 | 401 | ||
400 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion | 402 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion |
401 | if ( len > 1 ) | 403 | if ( len > 1 ) |
402 | { | 404 | { |
403 | while (sidx < len-2) | 405 | while (sidx < len-2) |
404 | { | 406 | { |
405 | if ( insertLFs ) | 407 | if ( insertLFs ) |
406 | { | 408 | { |
407 | if ( count && (count%76) == 0 ) | 409 | if ( count && (count%76) == 0 ) |
408 | out.at(didx++) = '\n'; | 410 | out[didx++] = '\n'; |
409 | count += 4; | 411 | count += 4; |
410 | } | 412 | } |
411 | out.at(didx++) = Base64EncMap[(data[sidx] >> 2) & 077]; | 413 | out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; |
412 | out.at(didx++) = Base64EncMap[(data[sidx+1] >> 4) & 017 | | 414 | out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 | |
413 | (data[sidx] << 4) & 077]; | 415 | (data[sidx] << 4) & 077]; |
414 | out.at(didx++) = Base64EncMap[(data[sidx+2] >> 6) & 003 | | 416 | out[didx++] = Base64EncMap[(data[sidx+2] >> 6) & 003 | |
415 | (data[sidx+1] << 2) & 077]; | 417 | (data[sidx+1] << 2) & 077]; |
416 | out.at(didx++) = Base64EncMap[data[sidx+2] & 077]; | 418 | out[didx++] = Base64EncMap[data[sidx+2] & 077]; |
417 | sidx += 3; | 419 | sidx += 3; |
418 | } | 420 | } |
419 | } | 421 | } |
420 | 422 | ||
421 | if (sidx < len) | 423 | if (sidx < len) |
422 | { | 424 | { |
423 | if ( insertLFs && (count > 0) && (count%76) == 0 ) | 425 | if ( insertLFs && (count > 0) && (count%76) == 0 ) |
424 | out.at(didx++) = '\n'; | 426 | out[didx++] = '\n'; |
425 | 427 | ||
426 | out.at(didx++) = Base64EncMap[(data[sidx] >> 2) & 077]; | 428 | out[didx++] = Base64EncMap[(data[sidx] >> 2) & 077]; |
427 | if (sidx < len-1) | 429 | if (sidx < len-1) |
428 | { | 430 | { |
429 | out.at(didx++) = Base64EncMap[(data[sidx+1] >> 4) & 017 | | 431 | out[didx++] = Base64EncMap[(data[sidx+1] >> 4) & 017 | |
430 | (data[sidx] << 4) & 077]; | 432 | (data[sidx] << 4) & 077]; |
431 | out.at(didx++) = Base64EncMap[(data[sidx+1] << 2) & 077]; | 433 | out[didx++] = Base64EncMap[(data[sidx+1] << 2) & 077]; |
432 | } | 434 | } |
433 | else | 435 | else |
434 | { | 436 | { |
435 | out.at(didx++) = Base64EncMap[(data[sidx] << 4) & 077]; | 437 | out[didx++] = Base64EncMap[(data[sidx] << 4) & 077]; |
436 | } | 438 | } |
437 | } | 439 | } |
438 | 440 | ||
439 | // Add padding | 441 | // Add padding |
440 | while (didx < out.size()) | 442 | while (didx < out.size()) |
441 | { | 443 | out[didx++] = '='; |
442 | out.at(didx) = '='; | ||
443 | didx++; | ||
444 | } | ||
445 | } | 444 | } |
446 | 445 | ||
447 | QCString KCodecs::base64Decode( const QCString& str ) | 446 | Q3CString KCodecs::base64Decode( const Q3CString& str ) |
448 | { | 447 | { |
449 | if ( str.isEmpty() ) | 448 | if ( str.isEmpty() ) |
450 | return ""; | 449 | return ""; |
451 | 450 | ||
452 | QByteArray in( str.length() ); | 451 | QByteArray in( str.length() ); |
453 | memcpy( in.data(), str.data(), str.length() ); | 452 | memcpy( in.data(), str.data(), str.length() ); |
454 | return base64Decode( in ); | 453 | return base64Decode( in ); |
455 | } | 454 | } |
456 | 455 | ||
457 | QCString KCodecs::base64Decode( const QByteArray& in ) | 456 | Q3CString KCodecs::base64Decode( const QByteArray& in ) |
458 | { | 457 | { |
459 | QByteArray out; | 458 | QByteArray out; |
460 | base64Decode( in, out ); | 459 | base64Decode( in, out ); |
461 | return QCString( out.data(), out.size()+1 ); | 460 | return Q3CString( out.data(), out.size()+1 ); |
462 | } | 461 | } |
463 | 462 | ||
464 | void KCodecs::base64Decode( const QByteArray& in, QByteArray& out ) | 463 | void KCodecs::base64Decode( const QByteArray& in, QByteArray& out ) |
465 | { | 464 | { |
466 | out.resize(0); | 465 | out.resize(0); |
467 | if ( in.isEmpty() ) | 466 | if ( in.isEmpty() ) |
468 | return; | 467 | return; |
469 | 468 | ||
470 | unsigned int count = 0; | 469 | unsigned int count = 0; |
471 | unsigned int len = in.size(), tail = len; | 470 | unsigned int len = in.size(), tail = len; |
472 | const char* data = in.data(); | 471 | const char* data = in.data(); |
473 | 472 | ||
474 | // Deal with possible *nix "BEGIN" marker!! | 473 | // Deal with possible *nix "BEGIN" marker!! |
475 | while ( count < len && (data[count] == '\n' || data[count] == '\r' || | 474 | while ( count < len && (data[count] == '\n' || data[count] == '\r' || |
476 | data[count] == '\t' || data[count] == ' ') ) | 475 | data[count] == '\t' || data[count] == ' ') ) |
477 | count++; | 476 | count++; |
478 | 477 | ||
479 | if ( QString(data+count).left(5).lower() == "begin" ) | 478 | if ( QString(data+count).left(5).lower() == "begin" ) |
480 | { | 479 | { |
481 | count += 5; | 480 | count += 5; |
482 | while ( count < len && data[count] != '\n' && data[count] != '\r' ) | 481 | while ( count < len && data[count] != '\n' && data[count] != '\r' ) |
483 | count++; | 482 | count++; |
484 | 483 | ||
485 | while ( count < len && (data[count] == '\n' || data[count] == '\r') ) | 484 | while ( count < len && (data[count] == '\n' || data[count] == '\r') ) |
486 | count ++; | 485 | count ++; |
487 | 486 | ||
488 | data += count; | 487 | data += count; |
489 | tail = (len -= count); | 488 | tail = (len -= count); |
490 | } | 489 | } |
491 | 490 | ||
492 | // Find the tail end of the actual encoded data even if | 491 | // Find the tail end of the actual encoded data even if |
493 | // there is/are trailing CR and/or LF. | 492 | // there is/are trailing CR and/or LF. |
494 | while ( data[tail-1] == '=' || data[tail-1] == '\n' || | 493 | while ( data[tail-1] == '=' || data[tail-1] == '\n' || |
495 | data[tail-1] == '\r' ) | 494 | data[tail-1] == '\r' ) |
496 | if ( data[--tail] != '=' ) len = tail; | 495 | if ( data[--tail] != '=' ) len = tail; |
497 | 496 | ||
498 | unsigned int outIdx = 0; | 497 | unsigned int outIdx = 0; |
499 | out.resize( (count=len) ); | 498 | out.resize( (count=len) ); |
500 | for (unsigned int idx = 0; idx < count; idx++) | 499 | for (unsigned int idx = 0; idx < count; idx++) |
501 | { | 500 | { |
502 | // Adhere to RFC 2045 and ignore characters | 501 | // Adhere to RFC 2045 and ignore characters |
503 | // that are not part of the encoding table. | 502 | // that are not part of the encoding table. |
504 | unsigned char ch = data[idx]; | 503 | unsigned char ch = data[idx]; |
505 | if ((ch > 47 && ch < 58) || (ch > 64 && ch < 91) || | 504 | if ((ch > 47 && ch < 58) || (ch > 64 && ch < 91) || |
506 | (ch > 96 && ch < 123) || ch == '+' || ch == '/' || ch == '=') | 505 | (ch > 96 && ch < 123) || ch == '+' || ch == '/' || ch == '=') |
507 | { | 506 | { |
508 | out.at(outIdx++) = Base64DecMap[ch]; | 507 | out[outIdx++] = Base64DecMap[ch]; |
509 | } | 508 | } |
510 | else | 509 | else |
511 | { | 510 | { |
512 | len--; | 511 | len--; |
513 | tail--; | 512 | tail--; |
514 | } | 513 | } |
515 | } | 514 | } |
516 | 515 | ||
517 | // kdDebug() << "Tail size = " << tail << ", Length size = " << len << endl; | 516 | // kdDebug() << "Tail size = " << tail << ", Length size = " << len << endl; |
518 | 517 | ||
519 | // 4-byte to 3-byte conversion | 518 | // 4-byte to 3-byte conversion |
520 | len = (tail>(len/4)) ? tail-(len/4) : 0; | 519 | len = (tail>(len/4)) ? tail-(len/4) : 0; |
521 | unsigned int sidx = 0, didx = 0; | 520 | unsigned int sidx = 0, didx = 0; |
522 | if ( len > 1 ) | 521 | if ( len > 1 ) |
523 | { | 522 | { |
524 | while (didx < len-2) | 523 | while (didx < len-2) |
525 | { | 524 | { |
526 | out.at(didx) = (((out.at(sidx) << 2) & 255) | ((out.at(sidx+1) >> 4) & 003)); | 525 | out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); |
527 | out.at(didx+1) = (((out.at(sidx+1) << 4) & 255) | ((out.at(sidx+2) >> 2) & 017)); | 526 | out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); |
528 | out.at(didx+2) = (((out.at(sidx+2) << 6) & 255) | (out.at(sidx+3) & 077)); | 527 | out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077)); |
529 | sidx += 4; | 528 | sidx += 4; |
530 | didx += 3; | 529 | didx += 3; |
531 | } | 530 | } |
532 | } | 531 | } |
533 | 532 | ||
534 | if (didx < len) | 533 | if (didx < len) |
535 | out.at(didx) = (((out.at(sidx) << 2) & 255) | ((out.at(sidx+1) >> 4) & 003)); | 534 | out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003)); |
536 | 535 | ||
537 | if (++didx < len ) | 536 | if (++didx < len ) |
538 | out.at(didx) = (((out.at(sidx+1) << 4) & 255) | ((out.at(sidx+2) >> 2) & 017)); | 537 | out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017)); |
539 | 538 | ||
540 | // Resize the output buffer | 539 | // Resize the output buffer |
541 | if ( len == 0 || len < out.size() ) | 540 | if ( len == 0 || len < out.size() ) |
542 | out.resize(len); | 541 | out.resize(len); |
543 | } | 542 | } |
544 | 543 | ||
545 | QCString KCodecs::uuencode( const QCString& str ) | 544 | Q3CString KCodecs::uuencode( const Q3CString& str ) |
546 | { | 545 | { |
547 | if ( str.isEmpty() ) | 546 | if ( str.isEmpty() ) |
548 | return ""; | 547 | return ""; |
549 | 548 | ||
550 | QByteArray in; | 549 | QByteArray in; |
551 | in.resize( str.length() ); | 550 | in.resize( str.length() ); |
552 | memcpy( in.data(), str.data(), str.length() ); | 551 | memcpy( in.data(), str.data(), str.length() ); |
553 | return uuencode( in ); | 552 | return uuencode( in ); |
554 | } | 553 | } |
555 | 554 | ||
556 | QCString KCodecs::uuencode( const QByteArray& in ) | 555 | Q3CString KCodecs::uuencode( const QByteArray& in ) |
557 | { | 556 | { |
558 | QByteArray out; | 557 | QByteArray out; |
559 | uuencode( in, out ); | 558 | uuencode( in, out ); |
560 | return QCString( out.data(), out.size()+1 ); | 559 | return Q3CString( out.data(), out.size()+1 ); |
561 | } | 560 | } |
562 | 561 | ||
563 | void KCodecs::uuencode( const QByteArray& in, QByteArray& out ) | 562 | void KCodecs::uuencode( const QByteArray& in, QByteArray& out ) |
564 | { | 563 | { |
565 | out.resize( 0 ); | 564 | out.resize( 0 ); |
566 | if( in.isEmpty() ) | 565 | if( in.isEmpty() ) |
567 | return; | 566 | return; |
568 | 567 | ||
569 | unsigned int sidx = 0; | 568 | unsigned int sidx = 0; |
570 | unsigned int didx = 0; | 569 | unsigned int didx = 0; |
571 | unsigned int line_len = 45; | 570 | unsigned int line_len = 45; |
572 | 571 | ||
573 | const char nl[] = "\n"; | 572 | const char nl[] = "\n"; |
574 | const char* data = in.data(); | 573 | const char* data = in.data(); |
575 | const unsigned int nl_len = strlen(nl); | 574 | const unsigned int nl_len = strlen(nl); |
576 | const unsigned int len = in.size(); | 575 | const unsigned int len = in.size(); |
577 | 576 | ||
578 | out.resize( (len+2)/3*4 + ((len+line_len-1)/line_len)*(nl_len+1) ); | 577 | out.resize( (len+2)/3*4 + ((len+line_len-1)/line_len)*(nl_len+1) ); |
579 | // split into lines, adding line-length and line terminator | 578 | // split into lines, adding line-length and line terminator |
580 | while (sidx+line_len < len) | 579 | while (sidx+line_len < len) |
581 | { | 580 | { |
582 | // line length | 581 | // line length |
583 | out.at(didx++) = UUEncMap[line_len]; | 582 | out[didx++] = UUEncMap[line_len]; |
584 | 583 | ||
585 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion | 584 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion |
586 | for (unsigned int end = sidx+line_len; sidx < end; sidx += 3) | 585 | for (unsigned int end = sidx+line_len; sidx < end; sidx += 3) |
587 | { | 586 | { |
588 | out.at(didx++) = UUEncMap[(data[sidx] >> 2) & 077]; | 587 | out[didx++] = UUEncMap[(data[sidx] >> 2) & 077]; |
589 | out.at(didx++) = UUEncMap[(data[sidx+1] >> 4) & 017 | | 588 | out[didx++] = UUEncMap[(data[sidx+1] >> 4) & 017 | |
590 | (data[sidx] << 4) & 077]; | 589 | (data[sidx] << 4) & 077]; |
591 | out.at(didx++) = UUEncMap[(data[sidx+2] >> 6) & 003 | | 590 | out[didx++] = UUEncMap[(data[sidx+2] >> 6) & 003 | |
592 | (data[sidx+1] << 2) & 077]; | 591 | (data[sidx+1] << 2) & 077]; |
593 | out.at(didx++) = UUEncMap[data[sidx+2] & 077]; | 592 | out[didx++] = UUEncMap[data[sidx+2] & 077]; |
594 | } | 593 | } |
595 | 594 | ||
596 | // line terminator | 595 | // line terminator |
597 | //for (unsigned int idx=0; idx < nl_len; idx++) | 596 | //for (unsigned int idx=0; idx < nl_len; idx++) |
598 | //out.at(didx++) = nl[idx]; | 597 | //out[didx++] = nl[idx]; |
599 | memcpy(out.data()+didx, nl, nl_len); | 598 | memcpy(out.data()+didx, nl, nl_len); |
600 | didx += nl_len; | 599 | didx += nl_len; |
601 | } | 600 | } |
602 | 601 | ||
603 | // line length | 602 | // line length |
604 | out.at(didx++) = UUEncMap[len-sidx]; | 603 | out[didx++] = UUEncMap[len-sidx]; |
605 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion | 604 | // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion |
606 | while (sidx+2 < len) | 605 | while (sidx+2 < len) |
607 | { | 606 | { |
608 | out.at(didx++) = UUEncMap[(data[sidx] >> 2) & 077]; | 607 | out[didx++] = UUEncMap[(data[sidx] >> 2) & 077]; |
609 | out.at(didx++) = UUEncMap[(data[sidx+1] >> 4) & 017 | | 608 | out[didx++] = UUEncMap[(data[sidx+1] >> 4) & 017 | |
610 | (data[sidx] << 4) & 077]; | 609 | (data[sidx] << 4) & 077]; |
611 | out.at(didx++) = UUEncMap[(data[sidx+2] >> 6) & 003 | | 610 | out[didx++] = UUEncMap[(data[sidx+2] >> 6) & 003 | |
612 | (data[sidx+1] << 2) & 077]; | 611 | (data[sidx+1] << 2) & 077]; |
613 | out.at(didx++) = UUEncMap[data[sidx+2] & 077]; | 612 | out[didx++] = UUEncMap[data[sidx+2] & 077]; |
614 | sidx += 3; | 613 | sidx += 3; |
615 | } | 614 | } |
616 | 615 | ||
617 | if (sidx < len-1) | 616 | if (sidx < len-1) |
618 | { | 617 | { |
619 | out.at(didx++) = UUEncMap[(data[sidx] >> 2) & 077]; | 618 | out[didx++] = UUEncMap[(data[sidx] >> 2) & 077]; |
620 | out.at(didx++) = UUEncMap[(data[sidx+1] >> 4) & 017 | | 619 | out[didx++] = UUEncMap[(data[sidx+1] >> 4) & 017 | |
621 | (data[sidx] << 4) & 077]; | 620 | (data[sidx] << 4) & 077]; |
622 | out.at(didx++) = UUEncMap[(data[sidx+1] << 2) & 077]; | 621 | out[didx++] = UUEncMap[(data[sidx+1] << 2) & 077]; |
623 | out.at(didx++) = UUEncMap[0]; | 622 | out[didx++] = UUEncMap[0]; |
624 | } | 623 | } |
625 | else if (sidx < len) | 624 | else if (sidx < len) |
626 | { | 625 | { |
627 | out.at(didx++) = UUEncMap[(data[sidx] >> 2) & 077]; | 626 | out[didx++] = UUEncMap[(data[sidx] >> 2) & 077]; |
628 | out.at(didx++) = UUEncMap[(data[sidx] << 4) & 077]; | 627 | out[didx++] = UUEncMap[(data[sidx] << 4) & 077]; |
629 | out.at(didx++) = UUEncMap[0]; | 628 | out[didx++] = UUEncMap[0]; |
630 | out.at(didx++) = UUEncMap[0]; | 629 | out[didx++] = UUEncMap[0]; |
631 | } | 630 | } |
632 | 631 | ||
633 | // line terminator | 632 | // line terminator |
634 | memcpy(out.data()+didx, nl, nl_len); | 633 | memcpy(out.data()+didx, nl, nl_len); |
635 | didx += nl_len; | 634 | didx += nl_len; |
636 | 635 | ||
637 | // sanity check | 636 | // sanity check |
638 | if ( didx != out.size() ) | 637 | if ( didx != out.size() ) |
639 | out.resize( 0 ); | 638 | out.resize( 0 ); |
640 | } | 639 | } |
641 | 640 | ||
642 | QCString KCodecs::uudecode( const QCString& str ) | 641 | Q3CString KCodecs::uudecode( const Q3CString& str ) |
643 | { | 642 | { |
644 | if ( str.isEmpty() ) | 643 | if ( str.isEmpty() ) |
645 | return ""; | 644 | return ""; |
646 | 645 | ||
647 | QByteArray in; | 646 | QByteArray in; |
648 | in.resize( str.length() ); | 647 | in.resize( str.length() ); |
649 | memcpy( in.data(), str.data(), str.length() ); | 648 | memcpy( in.data(), str.data(), str.length() ); |
650 | return uudecode( in ); | 649 | return uudecode( in ); |
651 | } | 650 | } |
652 | 651 | ||
653 | QCString KCodecs::uudecode( const QByteArray& in ) | 652 | Q3CString KCodecs::uudecode( const QByteArray& in ) |
654 | { | 653 | { |
655 | QByteArray out; | 654 | QByteArray out; |
656 | uudecode( in, out ); | 655 | uudecode( in, out ); |
657 | return QCString( out.data(), out.size()+1 ); | 656 | return Q3CString( out.data(), out.size()+1 ); |
658 | } | 657 | } |
659 | 658 | ||
660 | void KCodecs::uudecode( const QByteArray& in, QByteArray& out ) | 659 | void KCodecs::uudecode( const QByteArray& in, QByteArray& out ) |
661 | { | 660 | { |
662 | out.resize( 0 ); | 661 | out.resize( 0 ); |
663 | if( in.isEmpty() ) | 662 | if( in.isEmpty() ) |
664 | return; | 663 | return; |
665 | 664 | ||
666 | unsigned int sidx = 0; | 665 | unsigned int sidx = 0; |
667 | unsigned int didx = 0; | 666 | unsigned int didx = 0; |
668 | unsigned int len = in.size(); | 667 | unsigned int len = in.size(); |
669 | unsigned int line_len, end; | 668 | unsigned int line_len, end; |
670 | const char* data = in.data(); | 669 | const char* data = in.data(); |
671 | 670 | ||
672 | // Deal with *nix "BEGIN"/"END" separators!! | 671 | // Deal with *nix "BEGIN"/"END" separators!! |
673 | unsigned int count = 0; | 672 | unsigned int count = 0; |
674 | while ( count < len && (data[count] == '\n' || data[count] == '\r' || | 673 | while ( count < len && (data[count] == '\n' || data[count] == '\r' || |
675 | data[count] == '\t' || data[count] == ' ') ) | 674 | data[count] == '\t' || data[count] == ' ') ) |
676 | count ++; | 675 | count ++; |
677 | 676 | ||
678 | bool hasLF = false; | 677 | bool hasLF = false; |
679 | if ( QString( data+count).left(5).lower() == "begin" ) | 678 | if ( QString( data+count).left(5).lower() == "begin" ) |
680 | { | 679 | { |
681 | count += 5; | 680 | count += 5; |
@@ -684,116 +683,116 @@ void KCodecs::uudecode( const QByteArray& in, QByteArray& out ) | |||
684 | 683 | ||
685 | while ( count < len && (data[count] == '\n' || data[count] == '\r') ) | 684 | while ( count < len && (data[count] == '\n' || data[count] == '\r') ) |
686 | count ++; | 685 | count ++; |
687 | 686 | ||
688 | data += count; | 687 | data += count; |
689 | len -= count; | 688 | len -= count; |
690 | hasLF = true; | 689 | hasLF = true; |
691 | } | 690 | } |
692 | 691 | ||
693 | out.resize( len/4*3 ); | 692 | out.resize( len/4*3 ); |
694 | while ( sidx < len ) | 693 | while ( sidx < len ) |
695 | { | 694 | { |
696 | // get line length (in number of encoded octets) | 695 | // get line length (in number of encoded octets) |
697 | line_len = UUDecMap[ (unsigned char) data[sidx++]]; | 696 | line_len = UUDecMap[ (unsigned char) data[sidx++]]; |
698 | // ascii printable to 0-63 and 4-byte to 3-byte conversion | 697 | // ascii printable to 0-63 and 4-byte to 3-byte conversion |
699 | end = didx+line_len; | 698 | end = didx+line_len; |
700 | char A, B, C, D; | 699 | char A, B, C, D; |
701 | if (end > 2) { | 700 | if (end > 2) { |
702 | while (didx < end-2) | 701 | while (didx < end-2) |
703 | { | 702 | { |
704 | A = UUDecMap[(unsigned char) data[sidx]]; | 703 | A = UUDecMap[(unsigned char) data[sidx]]; |
705 | B = UUDecMap[(unsigned char) data[sidx+1]]; | 704 | B = UUDecMap[(unsigned char) data[sidx+1]]; |
706 | C = UUDecMap[(unsigned char) data[sidx+2]]; | 705 | C = UUDecMap[(unsigned char) data[sidx+2]]; |
707 | D = UUDecMap[(unsigned char) data[sidx+3]]; | 706 | D = UUDecMap[(unsigned char) data[sidx+3]]; |
708 | out.at(didx++) = ( ((A << 2) & 255) | ((B >> 4) & 003) ); | 707 | out[didx++] = ( ((A << 2) & 255) | ((B >> 4) & 003) ); |
709 | out.at(didx++) = ( ((B << 4) & 255) | ((C >> 2) & 017) ); | 708 | out[didx++] = ( ((B << 4) & 255) | ((C >> 2) & 017) ); |
710 | out.at(didx++) = ( ((C << 6) & 255) | (D & 077) ); | 709 | out[didx++] = ( ((C << 6) & 255) | (D & 077) ); |
711 | sidx += 4; | 710 | sidx += 4; |
712 | } | 711 | } |
713 | } | 712 | } |
714 | 713 | ||
715 | if (didx < end) | 714 | if (didx < end) |
716 | { | 715 | { |
717 | A = UUDecMap[(unsigned char) data[sidx]]; | 716 | A = UUDecMap[(unsigned char) data[sidx]]; |
718 | B = UUDecMap[(unsigned char) data[sidx+1]]; | 717 | B = UUDecMap[(unsigned char) data[sidx+1]]; |
719 | out.at(didx++) = ( ((A << 2) & 255) | ((B >> 4) & 003) ); | 718 | out[didx++] = ( ((A << 2) & 255) | ((B >> 4) & 003) ); |
720 | } | 719 | } |
721 | 720 | ||
722 | if (didx < end) | 721 | if (didx < end) |
723 | { | 722 | { |
724 | B = UUDecMap[(unsigned char) data[sidx+1]]; | 723 | B = UUDecMap[(unsigned char) data[sidx+1]]; |
725 | C = UUDecMap[(unsigned char) data[sidx+2]]; | 724 | C = UUDecMap[(unsigned char) data[sidx+2]]; |
726 | out.at(didx++) = ( ((B << 4) & 255) | ((C >> 2) & 017) ); | 725 | out[didx++] = ( ((B << 4) & 255) | ((C >> 2) & 017) ); |
727 | } | 726 | } |
728 | 727 | ||
729 | // skip padding | 728 | // skip padding |
730 | while (sidx < len && data[sidx] != '\n' && data[sidx] != '\r') | 729 | while (sidx < len && data[sidx] != '\n' && data[sidx] != '\r') |
731 | sidx++; | 730 | sidx++; |
732 | 731 | ||
733 | // skip end of line | 732 | // skip end of line |
734 | while (sidx < len && (data[sidx] == '\n' || data[sidx] == '\r')) | 733 | while (sidx < len && (data[sidx] == '\n' || data[sidx] == '\r')) |
735 | sidx++; | 734 | sidx++; |
736 | 735 | ||
737 | // skip the "END" separator when present. | 736 | // skip the "END" separator when present. |
738 | if ( hasLF && QString( data+sidx).left(3).lower() == "end" ) | 737 | if ( hasLF && QString( data+sidx).left(3).lower() == "end" ) |
739 | break; | 738 | break; |
740 | } | 739 | } |
741 | 740 | ||
742 | if ( didx < out.size() ) | 741 | if ( didx < out.size() ) |
743 | out.resize( didx ); | 742 | out.resize( didx ); |
744 | } | 743 | } |
745 | 744 | ||
746 | /******************************** KMD5 ********************************/ | 745 | /******************************** KMD5 ********************************/ |
747 | KMD5::KMD5() | 746 | KMD5::KMD5() |
748 | { | 747 | { |
749 | init(); | 748 | init(); |
750 | } | 749 | } |
751 | 750 | ||
752 | KMD5::KMD5(const char *in, int len) | 751 | KMD5::KMD5(const char *in, int len) |
753 | { | 752 | { |
754 | init(); | 753 | init(); |
755 | update(in, len); | 754 | update(in, len); |
756 | } | 755 | } |
757 | 756 | ||
758 | KMD5::KMD5(const QByteArray& in) | 757 | KMD5::KMD5(const QByteArray& in) |
759 | { | 758 | { |
760 | init(); | 759 | init(); |
761 | update( in ); | 760 | update( in ); |
762 | } | 761 | } |
763 | 762 | ||
764 | KMD5::KMD5(const QCString& in) | 763 | KMD5::KMD5(const Q3CString& in) |
765 | { | 764 | { |
766 | init(); | 765 | init(); |
767 | update( in ); | 766 | update( in ); |
768 | } | 767 | } |
769 | 768 | ||
770 | void KMD5::update(const QByteArray& in) | 769 | void KMD5::update(const QByteArray& in) |
771 | { | 770 | { |
772 | update(in.data(), int(in.size())); | 771 | update(in.data(), int(in.size())); |
773 | } | 772 | } |
774 | 773 | ||
775 | void KMD5::update(const QCString& in) | 774 | void KMD5::update(const Q3CString& in) |
776 | { | 775 | { |
777 | update(in.data(), int(in.length())); | 776 | update(in.data(), int(in.length())); |
778 | } | 777 | } |
779 | 778 | ||
780 | void KMD5::update(const unsigned char* in, int len) | 779 | void KMD5::update(const unsigned char* in, int len) |
781 | { | 780 | { |
782 | if (len < 0) | 781 | if (len < 0) |
783 | len = qstrlen(reinterpret_cast<const char*>(in)); | 782 | len = qstrlen(reinterpret_cast<const char*>(in)); |
784 | 783 | ||
785 | if (!len) | 784 | if (!len) |
786 | return; | 785 | return; |
787 | 786 | ||
788 | if (m_finalized) { | 787 | if (m_finalized) { |
789 | kdWarning() << "KMD5::update called after state was finalized!" << endl; | 788 | kdWarning() << "KMD5::update called after state was finalized!" << endl; |
790 | return; | 789 | return; |
791 | } | 790 | } |
792 | 791 | ||
793 | Q_UINT32 in_index; | 792 | Q_UINT32 in_index; |
794 | Q_UINT32 buffer_index; | 793 | Q_UINT32 buffer_index; |
795 | Q_UINT32 buffer_space; | 794 | Q_UINT32 buffer_space; |
796 | Q_UINT32 in_length = static_cast<Q_UINT32>( len ); | 795 | Q_UINT32 in_length = static_cast<Q_UINT32>( len ); |
797 | 796 | ||
798 | buffer_index = static_cast<Q_UINT32>((m_count[0] >> 3) & 0x3F); | 797 | buffer_index = static_cast<Q_UINT32>((m_count[0] >> 3) & 0x3F); |
799 | 798 | ||
@@ -852,132 +851,132 @@ void KMD5::finalize () | |||
852 | index = static_cast<Q_UINT32>((m_count[0] >> 3) & 0x3f); | 851 | index = static_cast<Q_UINT32>((m_count[0] >> 3) & 0x3f); |
853 | padLen = (index < 56) ? (56 - index) : (120 - index); | 852 | padLen = (index < 56) ? (56 - index) : (120 - index); |
854 | update (reinterpret_cast<const char*>(PADDING), padLen); | 853 | update (reinterpret_cast<const char*>(PADDING), padLen); |
855 | 854 | ||
856 | // Append length (before padding) | 855 | // Append length (before padding) |
857 | update (reinterpret_cast<const char*>(bits), 8); | 856 | update (reinterpret_cast<const char*>(bits), 8); |
858 | 857 | ||
859 | // Store state in digest | 858 | // Store state in digest |
860 | encode (m_digest, m_state, 16); | 859 | encode (m_digest, m_state, 16); |
861 | //memcpy( m_digest, m_state, 16 ); | 860 | //memcpy( m_digest, m_state, 16 ); |
862 | 861 | ||
863 | // Fill sensitive information with zero's | 862 | // Fill sensitive information with zero's |
864 | memset ( (void *)m_buffer, 0, sizeof(*m_buffer)); | 863 | memset ( (void *)m_buffer, 0, sizeof(*m_buffer)); |
865 | 864 | ||
866 | m_finalized = true; | 865 | m_finalized = true; |
867 | } | 866 | } |
868 | 867 | ||
869 | 868 | ||
870 | bool KMD5::verify( const KMD5::Digest& digest) | 869 | bool KMD5::verify( const KMD5::Digest& digest) |
871 | { | 870 | { |
872 | finalize(); | 871 | finalize(); |
873 | return (0 == memcmp(rawDigest(), digest, sizeof(KMD5::Digest))); | 872 | return (0 == memcmp(rawDigest(), digest, sizeof(KMD5::Digest))); |
874 | } | 873 | } |
875 | 874 | ||
876 | bool KMD5::verify( const QCString& hexdigest) | 875 | bool KMD5::verify( const Q3CString& hexdigest) |
877 | { | 876 | { |
878 | finalize(); | 877 | finalize(); |
879 | return (0 == strcmp(hexDigest().data(), hexdigest)); | 878 | return (0 == strcmp(hexDigest().data(), hexdigest)); |
880 | } | 879 | } |
881 | 880 | ||
882 | const KMD5::Digest& KMD5::rawDigest() | 881 | const KMD5::Digest& KMD5::rawDigest() |
883 | { | 882 | { |
884 | finalize(); | 883 | finalize(); |
885 | return m_digest; | 884 | return m_digest; |
886 | } | 885 | } |
887 | 886 | ||
888 | void KMD5::rawDigest( KMD5::Digest& bin ) | 887 | void KMD5::rawDigest( KMD5::Digest& bin ) |
889 | { | 888 | { |
890 | finalize(); | 889 | finalize(); |
891 | memcpy( bin, m_digest, 16 ); | 890 | memcpy( bin, m_digest, 16 ); |
892 | } | 891 | } |
893 | 892 | ||
894 | 893 | ||
895 | QCString KMD5::hexDigest() | 894 | Q3CString KMD5::hexDigest() |
896 | { | 895 | { |
897 | QCString s(33); | 896 | Q3CString s(33); |
898 | 897 | ||
899 | finalize(); | 898 | finalize(); |
900 | sprintf(s.data(), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", | 899 | sprintf(s.data(), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
901 | m_digest[0], m_digest[1], m_digest[2], m_digest[3], m_digest[4], m_digest[5], | 900 | m_digest[0], m_digest[1], m_digest[2], m_digest[3], m_digest[4], m_digest[5], |
902 | m_digest[6], m_digest[7], m_digest[8], m_digest[9], m_digest[10], m_digest[11], | 901 | m_digest[6], m_digest[7], m_digest[8], m_digest[9], m_digest[10], m_digest[11], |
903 | m_digest[12], m_digest[13], m_digest[14], m_digest[15]); | 902 | m_digest[12], m_digest[13], m_digest[14], m_digest[15]); |
904 | 903 | ||
905 | return s; | 904 | return s; |
906 | } | 905 | } |
907 | 906 | ||
908 | void KMD5::hexDigest(QCString& s) | 907 | void KMD5::hexDigest(Q3CString& s) |
909 | { | 908 | { |
910 | finalize(); | 909 | finalize(); |
911 | s.resize(33); | 910 | s.resize(33); |
912 | sprintf(s.data(), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", | 911 | sprintf(s.data(), "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
913 | m_digest[0], m_digest[1], m_digest[2], m_digest[3], m_digest[4], m_digest[5], | 912 | m_digest[0], m_digest[1], m_digest[2], m_digest[3], m_digest[4], m_digest[5], |
914 | m_digest[6], m_digest[7], m_digest[8], m_digest[9], m_digest[10], m_digest[11], | 913 | m_digest[6], m_digest[7], m_digest[8], m_digest[9], m_digest[10], m_digest[11], |
915 | m_digest[12], m_digest[13], m_digest[14], m_digest[15]); | 914 | m_digest[12], m_digest[13], m_digest[14], m_digest[15]); |
916 | } | 915 | } |
917 | 916 | ||
918 | QCString KMD5::base64Digest() | 917 | Q3CString KMD5::base64Digest() |
919 | { | 918 | { |
920 | QByteArray ba(16); | 919 | QByteArray ba(16); |
921 | 920 | ||
922 | finalize(); | 921 | finalize(); |
923 | memcpy(ba.data(), m_digest, 16); | 922 | memcpy(ba.data(), m_digest, 16); |
924 | return KCodecs::base64Encode(ba); | 923 | return KCodecs::base64Encode(ba); |
925 | } | 924 | } |
926 | 925 | ||
927 | 926 | ||
928 | void KMD5::init() | 927 | void KMD5::init() |
929 | { | 928 | { |
930 | d = 0; | 929 | d = 0; |
931 | reset(); | 930 | reset(); |
932 | } | 931 | } |
933 | 932 | ||
934 | void KMD5::reset() | 933 | void KMD5::reset() |
935 | { | 934 | { |
936 | m_finalized = false; | 935 | m_finalized = false; |
937 | 936 | ||
938 | m_count[0] = 0; | 937 | m_count[0] = 0; |
939 | m_count[1] = 0; | 938 | m_count[1] = 0; |
940 | 939 | ||
941 | m_state[0] = 0x67452301; | 940 | m_state[0] = 0x67452301; |
942 | m_state[1] = 0xefcdab89; | 941 | m_state[1] = 0xefcdab89; |
943 | m_state[2] = 0x98badcfe; | 942 | m_state[2] = 0x98badcfe; |
944 | m_state[3] = 0x10325476; | 943 | m_state[3] = 0x10325476; |
945 | 944 | ||
946 | memset ( m_buffer, 0, sizeof(*m_buffer)); | 945 | memset ( m_buffer, 0, sizeof(*m_buffer)); |
947 | memset ( m_digest, 0, sizeof(*m_digest)); | 946 | memset ( m_digest, 0, sizeof(*m_digest)); |
948 | } | 947 | } |
949 | 948 | ||
950 | void KMD5::transform( const unsigned char block[64] ) | 949 | void KMD5::transform( const unsigned char block[64] ) |
951 | { | 950 | { |
952 | 951 | ||
953 | Q_UINT32 a = m_state[0], b = m_state[1], c = m_state[2], d = m_state[3], x[16]; | 952 | Q_UINT32 a = m_state[0], b = m_state[1], c = m_state[2], d = m_state[3], x[16]; |
954 | 953 | ||
955 | decode (x, block, 64); | 954 | decode (x, block, 64); |
956 | //memcpy( x, block, 64 ); | 955 | //memcpy( x, block, 64 ); |
957 | 956 | ||
958 | //US Q_ASSERT(!m_finalized); // not just a user error, since the method is private | 957 | //US Q_ASSERT(!m_finalized); // not just a user error, since the method is private |
959 | ASSERT(!m_finalized); // not just a user error, since the method is private | 958 | Q_ASSERT(!m_finalized); // not just a user error, since the method is private |
960 | 959 | ||
961 | /* Round 1 */ | 960 | /* Round 1 */ |
962 | FF (a, b, c, d, x[ 0], KMD5_S11, 0xd76aa478); /* 1 */ | 961 | FF (a, b, c, d, x[ 0], KMD5_S11, 0xd76aa478); /* 1 */ |
963 | FF (d, a, b, c, x[ 1], KMD5_S12, 0xe8c7b756); /* 2 */ | 962 | FF (d, a, b, c, x[ 1], KMD5_S12, 0xe8c7b756); /* 2 */ |
964 | FF (c, d, a, b, x[ 2], KMD5_S13, 0x242070db); /* 3 */ | 963 | FF (c, d, a, b, x[ 2], KMD5_S13, 0x242070db); /* 3 */ |
965 | FF (b, c, d, a, x[ 3], KMD5_S14, 0xc1bdceee); /* 4 */ | 964 | FF (b, c, d, a, x[ 3], KMD5_S14, 0xc1bdceee); /* 4 */ |
966 | FF (a, b, c, d, x[ 4], KMD5_S11, 0xf57c0faf); /* 5 */ | 965 | FF (a, b, c, d, x[ 4], KMD5_S11, 0xf57c0faf); /* 5 */ |
967 | FF (d, a, b, c, x[ 5], KMD5_S12, 0x4787c62a); /* 6 */ | 966 | FF (d, a, b, c, x[ 5], KMD5_S12, 0x4787c62a); /* 6 */ |
968 | FF (c, d, a, b, x[ 6], KMD5_S13, 0xa8304613); /* 7 */ | 967 | FF (c, d, a, b, x[ 6], KMD5_S13, 0xa8304613); /* 7 */ |
969 | FF (b, c, d, a, x[ 7], KMD5_S14, 0xfd469501); /* 8 */ | 968 | FF (b, c, d, a, x[ 7], KMD5_S14, 0xfd469501); /* 8 */ |
970 | FF (a, b, c, d, x[ 8], KMD5_S11, 0x698098d8); /* 9 */ | 969 | FF (a, b, c, d, x[ 8], KMD5_S11, 0x698098d8); /* 9 */ |
971 | FF (d, a, b, c, x[ 9], KMD5_S12, 0x8b44f7af); /* 10 */ | 970 | FF (d, a, b, c, x[ 9], KMD5_S12, 0x8b44f7af); /* 10 */ |
972 | FF (c, d, a, b, x[10], KMD5_S13, 0xffff5bb1); /* 11 */ | 971 | FF (c, d, a, b, x[10], KMD5_S13, 0xffff5bb1); /* 11 */ |
973 | FF (b, c, d, a, x[11], KMD5_S14, 0x895cd7be); /* 12 */ | 972 | FF (b, c, d, a, x[11], KMD5_S14, 0x895cd7be); /* 12 */ |
974 | FF (a, b, c, d, x[12], KMD5_S11, 0x6b901122); /* 13 */ | 973 | FF (a, b, c, d, x[12], KMD5_S11, 0x6b901122); /* 13 */ |
975 | FF (d, a, b, c, x[13], KMD5_S12, 0xfd987193); /* 14 */ | 974 | FF (d, a, b, c, x[13], KMD5_S12, 0xfd987193); /* 14 */ |
976 | FF (c, d, a, b, x[14], KMD5_S13, 0xa679438e); /* 15 */ | 975 | FF (c, d, a, b, x[14], KMD5_S13, 0xa679438e); /* 15 */ |
977 | FF (b, c, d, a, x[15], KMD5_S14, 0x49b40821); /* 16 */ | 976 | FF (b, c, d, a, x[15], KMD5_S14, 0x49b40821); /* 16 */ |
978 | 977 | ||
979 | /* Round 2 */ | 978 | /* Round 2 */ |
980 | GG (a, b, c, d, x[ 1], KMD5_S21, 0xf61e2562); /* 17 */ | 979 | GG (a, b, c, d, x[ 1], KMD5_S21, 0xf61e2562); /* 17 */ |
981 | GG (d, a, b, c, x[ 6], KMD5_S22, 0xc040b340); /* 18 */ | 980 | GG (d, a, b, c, x[ 6], KMD5_S22, 0xc040b340); /* 18 */ |
982 | GG (c, d, a, b, x[11], KMD5_S23, 0x265e5a51); /* 19 */ | 981 | GG (c, d, a, b, x[11], KMD5_S23, 0x265e5a51); /* 19 */ |
983 | GG (b, c, d, a, x[ 0], KMD5_S24, 0xe9b6c7aa); /* 20 */ | 982 | GG (b, c, d, a, x[ 0], KMD5_S24, 0xe9b6c7aa); /* 20 */ |