summaryrefslogtreecommitdiffabout
path: root/microkde/kdecore/kmdcodec.h
Unidiff
Diffstat (limited to 'microkde/kdecore/kmdcodec.h') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/kmdcodec.h572
1 files changed, 572 insertions, 0 deletions
diff --git a/microkde/kdecore/kmdcodec.h b/microkde/kdecore/kmdcodec.h
new file mode 100644
index 0000000..2c4d611
--- a/dev/null
+++ b/microkde/kdecore/kmdcodec.h
@@ -0,0 +1,572 @@
1/*
2 Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
3 Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License (LGPL)
7 version 2 as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 RFC 1321 "MD5 Message-Digest Algorithm" Copyright (C) 1991-1992.
19 RSA Data Security, Inc. Created 1991. All rights reserved.
20
21 The KMD5 class is based on a C++ implementation of
22 "RSA Data Security, Inc. MD5 Message-Digest Algorithm" by
23 Mordechai T. Abzug,Copyright (c) 1995. This implementation
24 passes the test-suite as defined in RFC 1321.
25
26 The encoding and decoding utilities in KCodecs with the exception of
27 quoted-printable are based on the java implementation in HTTPClient
28 package by Ronald Tschalär Copyright (C) 1996-1999.
29
30 The quoted-printable codec as described in RFC 2045, section 6.7. is by
31 Rik Hemsley (C) 2001.
32*/
33
34#ifndef _KMDBASE_H
35#define _KMDBASE_H
36
37#define KBase64 KCodecs
38
39#include <qglobal.h>
40#include <qstring.h>
41#include <qiodevice.h>
42
43/**
44 * A wrapper class for the most commonly used encoding and
45 * decoding algorithms. Currently there is support for encoding
46 * and decoding input using base64, uu and the quoted-printable
47 * specifications.
48 *
49 * @sect Usage:
50 *
51 * <PRE>
52 * QCString input = "Aladdin:open sesame";
53 * QCString result = KCodecs::base64Encode(input);
54 * cout << "Result: " << result.data() << endl;
55 *
56 * Output should be
57 * Result: QWxhZGRpbjpvcGVuIHNlc2FtZQ==
58 * </PRE>
59 *
60 * The above example makes use of the convenience functions
61 * (ones that accept/return null-terminated strings) to encode/decode
62 * a string. If what you need is to encode or decode binary data, then
63 * it is highly recommended that you use the functions that take an input
64 * and output QByteArray as arguments. These functions are specifically
65 * tailored for encoding and decoding binary data.
66 *
67 * @short A collection of commonly used encoding and decoding algorithms.
68 * @author Dawit Alemayehu <adawit@kde.org>
69 * @author Rik Hemsley <rik@kde.org>
70 */
71class KCodecs
72{
73public:
74
75 /**
76 * Encodes the given data using the quoted-printable algorithm.
77 *
78 * @param in data to be encoded.
79 * @param useCRLF if true the input data is expected to have
80 * CRLF line breaks and the output will have CRLF line
81 * breaks, too.
82 * @return quoted-printable encoded data.
83 */
84 static QCString quotedPrintableEncode(const QByteArray & in,
85 bool useCRLF = true);
86
87 /**
88 * @overload
89 *
90 * Same as above except it accepts a null terminated
91 * string instead an array.
92 *
93 * @param str data to be encoded.
94 * @param useCRLF if true the input data is expected to have
95 * CRLF line breaks and the output will have CRLF line
96 * breaks, too.
97 * @return quoted-printable encoded data.
98 */
99 static QCString quotedPrintableEncode(const QCString & str,
100 bool useCRLF = true);
101
102 /**
103 * Encodes the given data using the quoted-printable algorithm.
104 *
105 * Use this function if you want the result of the encoding
106 * to be placed in another array which cuts down the number
107 * of copy operation that have to be performed in the process.
108 * This is also the preferred method for encoding binary data.
109 *
110 * NOTE: the output array is first reset and then resized
111 * appropriately before use, hence, all data stored in the
112 * output array will be lost.
113 *
114 * @param in data to be encoded.
115 * @param out decoded data.
116 * @param useCRLF if true the input data is expected to have
117 * CRLF line breaks and the output will have CRLF line
118 * breaks, too.
119 * @return quoted-printable encoded data.
120 */
121 static void quotedPrintableEncode(const QByteArray & in, QByteArray& out,
122 bool useCRLF);
123
124 /**
125 * Decodes a quoted-printable encoded string.
126 *
127 * Accepts data with CRLF or standard unix line breaks.
128 *
129 * @param in the data to be decoded.
130 * @return decoded data.
131 */
132 static QCString quotedPrintableDecode(const QByteArray & in);
133
134 /**
135 * @overload
136 *
137 * Same as above except it accepts a null terminated
138 * string instead an array.
139 *
140 * @param str the data to be decoded.
141 * @return decoded data.
142 */
143 static QCString quotedPrintableDecode(const QCString & str);
144
145 /**
146 * Decodes a quoted-printable encoded data.
147 *
148 * Accepts data with CRLF or standard unix line breaks.
149 * Use this function if you want the result of the decoding
150 * to be placed in another array which cuts down the number
151 * of copy operation that have to be performed in the process.
152 * This is also the preferred method for decoding an encoded
153 * binary data.
154 *
155 * NOTE: the output array is first reset and then resized
156 * appropriately before use, hence, all data stored in the
157 * output array will be lost.
158 *
159 * @param in data to be encoded.
160 * @param out decoded data.
161 *
162 * @return quoted-printable encoded data.
163 */
164 static void quotedPrintableDecode(const QByteArray & in, QByteArray& out);
165
166
167 /**
168 * Encodes the given data using the uuencode algorithm.
169 *
170 * The output is split into lines starting with the number of
171 * encoded octets in the line and ending with a newline. No
172 * line is longer than 45 octets (60 characters), excluding the
173 * line terminator.
174 *
175 * @param in the data to be uuencoded
176 * @return a uuencoded data.
177 */
178 static QCString uuencode( const QByteArray& in );
179
180 /**
181 * @overload
182 *
183 * Same as the above functions except it accepts
184 * a null terminated string instead an array.
185 *
186 * @param str the string to be uuencoded.
187 * @return the encoded string.
188 */
189 static QCString uuencode( const QCString& str );
190
191 /**
192 * Encodes the given data using the uuencode algorithm.
193 *
194 * Use this function if you want the result of the encoding
195 * to be placed in another array and cut down the number of
196 * copy operation that have to be performed in the process.
197 * This is the preffered method for encoding binary data.
198 *
199 * NOTE: the output array is first reset and then resized
200 * appropriately before use, hence, all data stored in the
201 * output array will be lost.
202 *
203 * @param in the data to be uuencoded.
204 * @param out the container for the uudecoded data.
205 */
206 static void uuencode( const QByteArray& in, QByteArray& out );
207
208 /**
209 * Decodes the given data using the uuencode algorithm.
210 *
211 * Any 'begin' and 'end' lines like those generated by
212 * the utilities in unix and unix-like OS will be
213 * automatically ignored.
214 *
215 * @param in the data uuencoded data to be decoded.
216 * @return a decoded string.
217 */
218 static QCString uudecode( const QByteArray& in );
219
220 /**
221 * @overload
222 *
223 * Same as the above functions except it accepts
224 * a null terminated string instead an array.
225 *
226 * @param str the string to be decoded.
227 * @return a uudecoded string.
228 */
229 static QCString uudecode( const QCString& str );
230
231 /**
232 * Decodes the given data using the uudecode algorithm.
233 *
234 * Use this function if you want the result of the decoding
235 * to be placed in another array which cuts down the number
236 * of copy operation that have to be performed in the process.
237 * This is the preferred method for decoding binary data.
238 *
239 * Any 'begin' and 'end' lines like those generated by
240 * the utilities in unix and unix-like OS will be
241 * automatically ignored.
242 *
243 * NOTE: the output array is first reset and then resized
244 * appropriately before use, hence, all data stored in the
245 * output array will be lost.
246 *
247 * @param in the uuencoded-data to be decoded.
248 * @param out the container for the uudecoded data.
249 */
250 static void uudecode( const QByteArray& in, QByteArray& out );
251
252
253 /**
254 * Encodes the given data using the base64 algorithm.
255 *
256 * The boolean argument determines if the encoded data is
257 * going to be restricted to 76 characters or less per line
258 * as specified by RFC 2045. If @p insertLFs is true, then
259 * there will be 76 characters or less per line.
260 *
261 * @param in the data to be encoded.
262 * @param insertLFs limit the number of characters per line.
263 *
264 * @return a base64 encoded string.
265 */
266 static QCString base64Encode( const QByteArray& in, bool insertLFs = false);
267
268 /**
269 * @overload
270 *
271 * Same as the above functions except it accepts
272 * a null terminated string instead an array.
273 *
274 * @param str the string to be encoded.
275 * @param insertLFs limit the number of characters per line.
276 * @return the decoded string.
277 */
278 static QCString base64Encode( const QCString& str, bool insertLFs = false );
279
280 /**
281 * Encodes the given data using the base64 algorithm.
282 *
283 * Use this function if you want the result of the encoding
284 * to be placed in another array which cuts down the number
285 * of copy operation that have to be performed in the process.
286 * This is also the preferred method for encoding binary data.
287 *
288 * The boolean argument determines if the encoded data is going
289 * to be restricted to 76 characters or less per line as specified
290 * by RFC 2045. If @p insertLFs is true, then there will be 76
291 * characters or less per line.
292 *
293 * NOTE: the output array is first reset and then resized
294 * appropriately before use, hence, all data stored in the
295 * output array will be lost.
296 *
297 * @param in the data to be encoded using base64.
298 * @param out the container for the encoded data.
299 * @param insertLFs limit the number of characters per line.
300 */
301 static void base64Encode( const QByteArray& in, QByteArray& out,
302 bool insertLFs = false );
303
304 /**
305 * Decodes the given data that was encoded using the
306 * base64 algorithm.
307 *
308 * @param in the base64-encoded data to be decoded.
309 * @return the decoded data.
310 */
311 static QCString base64Decode( const QByteArray& in );
312
313 /**
314 * @overload
315 *
316 * Same as the above functions except it accepts
317 * a null terminated string instead an array.
318 *
319 * @param str the base64-encoded string.
320 * @return the decoded string.
321 */
322 static QCString base64Decode( const QCString& str );
323
324 /**
325 * Decodes the given data that was encoded with the base64
326 * algorithm.
327 *
328 * Use this function if you want the result of the decoding
329 * to be placed in another array which cuts down the number
330 * of copy operation that have to be performed in the process.
331 * This is also the preferred method for decoding an encoded
332 * binary data.
333 *
334 * NOTE: the output array is first reset and then resized
335 * appropriately before use, hence, all data stored in the
336 * output array will be lost.
337 *
338 * @param in the encoded data to be decoded.
339 * @param out the container for the decoded data.
340 */
341 static void base64Decode( const QByteArray& in, QByteArray& out );
342
343
344private:
345 KCodecs();
346
347private:
348 static const char UUEncMap[64];
349 static const char UUDecMap[128];
350 static const char Base64EncMap[64];
351 static const char Base64DecMap[128];
352 static const char hexChars[16];
353 static const unsigned int maxQPLineLength;
354};
355
356class KMD5Private;
357/**
358 * Provides an easy to use C++ implementation of RSA's
359 * MD5 algorithm.
360 *
361 * The default constructor is designed to provide much the same
362 * functionality as the most commonly used C-implementation, while
363 * the other three constructors are meant to further simplify the
364 * process of obtaining a digest by calculating the result in a
365 * single step.
366 *
367 * KMD5 is state-based, that means you can add new contents with
368 * update() as long as you didn't request the digest value yet.
369 * After the digest value was requested, the object is "finalized"
370 * and you have to call reset() to be able to do another calculation
371 * with it. The reason for this behaviour is that upon requesting
372 * the message digest KMD5 has to pad the received contents up to a
373 * 64 byte boundary to calculate its value. After this operation it
374 * is not possible to resume consuming data.
375 *
376 * @sect Usage:
377 *
378 * A common usage of this class:
379 *
380 * <PRE>
381 * const char* test1;
382 * KMD5::Digest rawResult;
383 *
384 * test1 = "This is a simple test.";
385 * KMD5 context (test1);
386 * cout << "Hex Digest output: " << context.hexDigest().data() << endl;
387 * </PRE>
388 *
389 * To cut down on the unnecessary overhead of creating multiple KMD5
390 * objects, you can simply invoke @ref reset() to reuse the same object
391 * in making another calculation:
392 *
393 * <PRE>
394 * context.reset ();
395 * context.update ("TWO");
396 * context.update ("THREE");
397 * cout << "Hex Digest output: " << context.hexDigest().data() << endl;
398 * </PRE>
399 *
400 * @short An adapted C++ implementation of RSA Data Securities MD5 algorithm.
401 * @author Dirk Mueller <mueller@kde.org>, Dawit Alemayehu <adawit@kde.org>
402 */
403
404class KMD5
405{
406public:
407
408 typedef unsigned char Digest[16];
409
410 KMD5();
411
412 /**
413 * Constructor that updates the digest for the given string.
414 *
415 * @param in C string or binary data
416 * @param len if negative, calculates the length by using
417 * strlen on the first parameter, otherwise
418 * it trusts the given length (does not stop on NUL byte).
419 */
420 KMD5(const char* in, int len = -1);
421
422 /**
423 * @overload
424 *
425 * Same as above except it accepts a QByteArray as its argument.
426 */
427 KMD5(const QByteArray& a );
428
429 /**
430 * @overload
431 *
432 * Same as above except it accepts a QByteArray as its argument.
433 */
434 KMD5(const QCString& a );
435
436 /**
437 * Updates the message to be digested. Be sure to add all data
438 * before you read the digest. After reading the digest, you
439 * can <b>not</b> add more data!
440 *
441 * @param in message to be added to digest
442 * @param len the length of the given message.
443 */
444 void update(const char* in, int len = -1) { update(reinterpret_cast<const unsigned char*>(in), len); }
445
446 /**
447 * @overload
448 */
449 void update(const unsigned char* in, int len = -1);
450
451 /**
452 * @overload
453 *
454 * @param in message to be added to the digest (QByteArray).
455 */
456 void update(const QByteArray& in );
457
458 /**
459 * @overload
460 *
461 * @param in message to be added to the digest (QByteArray).
462 */
463 void update(const QCString& in );
464
465 /**
466 * @overload
467 *
468 * reads the data from an I/O device, i.e. from a file (QFile).
469 *
470 * NOTE that the file must be open for reading.
471 *
472 * @param file a pointer to FILE as returned by calls like f{d,re}open
473 *
474 * @returns false if an error occured during reading.
475 */
476 bool update(QIODevice& file);
477
478 /**
479 * Calling this function will reset the calculated message digest.
480 * Use this method to perform another message digest calculation
481 * without recreating the KMD5 object.
482 */
483 void reset();
484
485 /**
486 * @return the raw representation of the digest
487 */
488 const Digest& rawDigest ();
489
490 /**
491 * Fills the given array with the binary representation of the
492 * message digest.
493 *
494 * Use this method if you do not want to worry about making
495 * copy of the digest once you obtain it.
496 *
497 * @param bin an array of 16 characters ( char[16] )
498 */
499 void rawDigest( KMD5::Digest& bin );
500
501 /**
502 * Returns the value of the calculated message digest in
503 * a hexadecimal representation.
504 */
505 QCString hexDigest ();
506
507 /**
508 * @overload
509 */
510 void hexDigest(QCString&);
511
512 /**
513 * Returns the value of the calculated message digest in
514 * a base64-encoded representation.
515 */
516 QCString base64Digest ();
517
518 /**
519 * returns true if the calculated digest for the given
520 * message matches the given one.
521 */
522 bool verify( const KMD5::Digest& digest);
523
524 /**
525 * @overload
526 */
527 bool verify(const QCString&);
528
529protected:
530 /**
531 * Performs the real update work. Note
532 * that length is implied to be 64.
533 */
534 void transform( const unsigned char buffer[64] );
535
536 /**
537 * finalizes the digest
538 */
539 void finalize();
540
541private:
542 KMD5(const KMD5& u);
543 KMD5& operator=(const KMD5& md);
544
545 void init();
546 void encode( unsigned char* output, Q_UINT32 *in, Q_UINT32 len );
547 void decode( Q_UINT32 *output, const unsigned char* in, Q_UINT32 len );
548
549 Q_UINT32 rotate_left( Q_UINT32 x, Q_UINT32 n );
550 Q_UINT32 F( Q_UINT32 x, Q_UINT32 y, Q_UINT32 z );
551 Q_UINT32 G( Q_UINT32 x, Q_UINT32 y, Q_UINT32 z );
552 Q_UINT32 H( Q_UINT32 x, Q_UINT32 y, Q_UINT32 z );
553 Q_UINT32 I( Q_UINT32 x, Q_UINT32 y, Q_UINT32 z );
554 void FF( Q_UINT32& a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 x,
555 Q_UINT32 s, Q_UINT32 ac );
556 void GG( Q_UINT32& a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 x,
557 Q_UINT32 s, Q_UINT32 ac );
558 void HH( Q_UINT32& a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 x,
559 Q_UINT32 s, Q_UINT32 ac );
560 void II( Q_UINT32& a, Q_UINT32 b, Q_UINT32 c, Q_UINT32 d, Q_UINT32 x,
561 Q_UINT32 s, Q_UINT32 ac );
562
563private:
564 Q_UINT32 m_state[4];
565 Q_UINT32 m_count[2];
566 Q_UINT8 m_buffer[64];
567 Digest m_digest;
568 bool m_finalized;
569
570 KMD5Private* d;
571};
572#endif