~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

Viewing changes to third-party/qca/qca/include/QtCrypto/qca_tools.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * qca_tools.h - Qt Cryptographic Architecture
 
3
 * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
 
4
 * Copyright (C) 2004,2005  Brad Hards <bradh@frogmouth.net>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
19
 *
 
20
 */
 
21
 
 
22
/**
 
23
   \file qca_tools.h
 
24
 
 
25
   Header file for "tool" classes used in %QCA
 
26
 
 
27
   These classes differ from those in qca_support.h, in that they have
 
28
   some cryptographic relationship, and require secure memory.
 
29
 
 
30
   \Note You should not use this header directly from an
 
31
   application. You should just use <tt> \#include \<QtCrypto>
 
32
   </tt> instead.
 
33
*/
 
34
 
 
35
#ifndef QCA_TOOLS_H
 
36
#define QCA_TOOLS_H
 
37
 
 
38
#include <QSharedData>
 
39
#include <QSharedDataPointer>
 
40
#include <QMetaType>
 
41
#include "qca_export.h"
 
42
 
 
43
class QString;
 
44
class QByteArray;
 
45
class QTextStream;
 
46
 
 
47
/**
 
48
   Allocate a block of memory from the secure memory pool.
 
49
 
 
50
   This is intended to be used when working with C libraries.
 
51
 
 
52
   \param bytes the number of bytes to allocate
 
53
*/
 
54
QCA_EXPORT void *qca_secure_alloc(int bytes);
 
55
 
 
56
/**
 
57
   Free (de-allocate) a block of memory that has been previously
 
58
   allocated from the secure memory pool.
 
59
 
 
60
   This is intended to be used when working with C libraries.
 
61
 
 
62
   \param p pointer to the block of memory to be free'd
 
63
*/
 
64
QCA_EXPORT void qca_secure_free(void *p);
 
65
 
 
66
/**
 
67
   Resize (re-allocate) a block of memory that has been previously
 
68
   allocated from the secure memory pool.
 
69
 
 
70
   \param p pointer to the block of memory to be resized.
 
71
   \param bytes the new size that is required.
 
72
*/
 
73
QCA_EXPORT void *qca_secure_realloc(void *p, int bytes);
 
74
 
 
75
namespace QCA {
 
76
 
 
77
/**
 
78
   \class MemoryRegion qca_tools.h QtCrypto
 
79
 
 
80
   Array of bytes that may be optionally secured
 
81
 
 
82
   This class is mostly unusable on its own.  Either use it as a SecureArray
 
83
   subclass or call toByteArray() to convert to QByteArray.
 
84
 
 
85
   Note that this class is implicitly shared (that is, copy on write).
 
86
 
 
87
   \ingroup UserAPI
 
88
*/
 
89
class QCA_EXPORT MemoryRegion
 
90
{
 
91
public:
 
92
        MemoryRegion();
 
93
 
 
94
        /**
 
95
           Constructs a new Memory Region from a null terminated 
 
96
           character array
 
97
 
 
98
           \param str pointer to the array of data to copy
 
99
        */
 
100
        MemoryRegion(const char *str);
 
101
 
 
102
        /**
 
103
           Constructs a new MemoryRegion from the data in a 
 
104
           byte array
 
105
 
 
106
           \param from the QByteArray to copy from
 
107
        */
 
108
        MemoryRegion(const QByteArray &from);
 
109
 
 
110
        /**
 
111
           Standard copy constructor
 
112
 
 
113
           \param from the MemoryRegion to copy from
 
114
        */
 
115
        MemoryRegion(const MemoryRegion &from);
 
116
        ~MemoryRegion();
 
117
 
 
118
        /**
 
119
           Standard assignment operator
 
120
 
 
121
           \param from the MemoryRegion to copy from
 
122
        */
 
123
        MemoryRegion & operator=(const MemoryRegion &from);
 
124
 
 
125
        /**
 
126
           Standard assignment operator
 
127
 
 
128
           \param from the QByteArray to copy from
 
129
        */
 
130
        MemoryRegion & operator=(const QByteArray &from);
 
131
 
 
132
        /**
 
133
           Test if the MemoryRegion is null (i.e. was created
 
134
           as a null array, and hasn't been resized).
 
135
 
 
136
           This is probably not what you are trying to do. If
 
137
           you are trying to determine whether there are any
 
138
           bytes in the array, use isEmpty() instead.
 
139
        */
 
140
        bool isNull() const;
 
141
 
 
142
        /**
 
143
           Test if the MemoryRegion is using secure memory, or not.
 
144
 
 
145
           In this context, memory is secure if it will not be paged
 
146
           out to disk.
 
147
 
 
148
           \return true if the memory region is secure
 
149
        */
 
150
        bool isSecure() const;
 
151
 
 
152
        /**
 
153
           Convert this memory region to a byte array.
 
154
 
 
155
           \note For secure data, this will make it insecure
 
156
 
 
157
           \sa data() and constData() for other ways to convert
 
158
           to an "accessible" format.
 
159
        */
 
160
        QByteArray toByteArray() const;
 
161
 
 
162
        /**
 
163
           Returns true if the size of the memory region is zero.
 
164
        */
 
165
        bool isEmpty() const;
 
166
 
 
167
        /**
 
168
           Returns the number of bytes in the memory region.
 
169
        */
 
170
        int size() const;
 
171
 
 
172
        /**
 
173
           Convert the contents of the memory region to 
 
174
           a C-compatible character array. This consists
 
175
           of size() bytes, followed by a null terminator.
 
176
 
 
177
           \sa toByteArray for an alternative approach.
 
178
           \sa constData, which is equivalent to this method, but avoids
 
179
           the possibility that the compiler picks the wrong version.
 
180
        */
 
181
        const char *data() const;
 
182
 
 
183
        /**
 
184
           Convert the contents of the memory region to 
 
185
           a C-compatible character array. This consists
 
186
           of size() bytes, followed by a null terminator.
 
187
 
 
188
           \sa toByteArray for an alternative approach.
 
189
           \sa data which is equivalent to this method
 
190
        */
 
191
        const char *constData() const;
 
192
 
 
193
        /**
 
194
           Obtain the value of the memory location at the specified
 
195
           position.
 
196
           
 
197
           \param index the offset into the memory region.
 
198
 
 
199
           \note The contents of a memory region are between
 
200
           0 and size()-1. The content at position size() is 
 
201
           always a null terminator.
 
202
        */
 
203
        const char & at(int index) const;
 
204
 
 
205
protected:
 
206
        /**
 
207
           Create a memory region, optionally using secure
 
208
           storage.
 
209
 
 
210
           \param secure if this is true, the memory region
 
211
           will use secure storage.
 
212
 
 
213
           \note This will create a memory region without
 
214
           any content (i.e. both isNull() and isEmpty() will
 
215
           return true.
 
216
        */
 
217
        MemoryRegion(bool secure);
 
218
 
 
219
        /**
 
220
           Create a memory region, optionally using secure
 
221
           storage.
 
222
 
 
223
           \param size the number of bytes in the memory
 
224
           region.
 
225
           \param secure if this is true, the memory region
 
226
           will use secure storage.
 
227
        */
 
228
        MemoryRegion(int size, bool secure);
 
229
 
 
230
        /**
 
231
           Create a memory region, optionally using secure
 
232
           storage.
 
233
 
 
234
           This constructor variant allows you to 
 
235
           initialize the memory region from an existing
 
236
           array.
 
237
 
 
238
           \param from the byte array to copy from.
 
239
           \param secure if this is true, the memory region
 
240
           will use secure storage.
 
241
        */
 
242
        MemoryRegion(const QByteArray &from, bool secure);
 
243
 
 
244
        /**
 
245
           Convert the contents of the memory region to 
 
246
           a C-compatible character array. This consists
 
247
           of size() bytes, followed by a null terminator.
 
248
        */
 
249
        char *data();
 
250
 
 
251
        /**
 
252
           Obtain the value of the memory location at the specified
 
253
           position.
 
254
           
 
255
           \param index the offset into the memory region.
 
256
 
 
257
           \note The contents of a memory region are between
 
258
           0 and size()-1. The content at position size() is 
 
259
           always a null terminator.
 
260
        */
 
261
        char & at(int index);
 
262
 
 
263
        /**
 
264
           Resize the memory region to the specified size.
 
265
 
 
266
           \param size the new size of the region.
 
267
        */
 
268
        bool resize(int size);
 
269
 
 
270
        /**
 
271
           Modify the memory region to match a specified
 
272
           byte array. This resizes the memory region
 
273
           as required to match the byte array size.
 
274
 
 
275
           \param from the byte array to copy from.
 
276
           \param secure if this is true, the memory region
 
277
           will use secure storage.
 
278
        */
 
279
        void set(const QByteArray &from, bool secure);
 
280
 
 
281
        /**
 
282
           Convert the memory region to use the specified
 
283
           memory type.
 
284
 
 
285
           This may involve copying data from secure to
 
286
           insecure storage, or from insecure to secure
 
287
           storage.
 
288
 
 
289
           \param secure if true, use secure memory; otherwise
 
290
           use insecure memory.
 
291
        */
 
292
        void setSecure(bool secure);
 
293
 
 
294
private:
 
295
        bool _secure;
 
296
        class Private;
 
297
        QSharedDataPointer<Private> d;
 
298
};
 
299
 
 
300
/**
 
301
   \class SecureArray qca_tools.h QtCrypto
 
302
 
 
303
   Secure array of bytes
 
304
 
 
305
   The %SecureArray provides an array of memory from a pool that is,
 
306
   at least partly, secure. In this sense, secure means that the contents
 
307
   of the memory should not be made available to other applications. By
 
308
   comparison, a QByteArray or QString may be held in pages that might be
 
309
   swapped to disk or free'd without being cleared first.
 
310
 
 
311
   Note that this class is implicitly shared (that is, copy on write).
 
312
 
 
313
   \ingroup UserAPI
 
314
*/
 
315
class QCA_EXPORT SecureArray : public MemoryRegion
 
316
{
 
317
public:
 
318
        /**
 
319
           Construct a secure byte array, zero length
 
320
        */
 
321
        SecureArray();
 
322
 
 
323
        /**
 
324
           Construct a secure byte array of the specified length
 
325
 
 
326
           \param size the number of bytes in the array
 
327
           \param ch the value every byte should be set to
 
328
        */
 
329
        explicit SecureArray(int size, char ch = 0);
 
330
 
 
331
        /**
 
332
           Construct a secure byte array from a string
 
333
 
 
334
           Note that this copies, rather than references the source array.
 
335
 
 
336
           \param str the source of the data (as a null terminated string).
 
337
        */
 
338
        SecureArray(const char *str);
 
339
 
 
340
        /**
 
341
           Construct a secure byte array from a QByteArray
 
342
 
 
343
           Note that this copies, rather than references the source array.
 
344
 
 
345
           \param a the source of the data.
 
346
 
 
347
           \sa operator=()
 
348
        */
 
349
        SecureArray(const QByteArray &a);
 
350
 
 
351
        /**
 
352
           Construct a secure byte array from a MemoryRegion
 
353
 
 
354
           Note that this copies, rather than references the source array
 
355
 
 
356
           \param a the source of the data.
 
357
 
 
358
           \sa operator=()
 
359
        */
 
360
        SecureArray(const MemoryRegion &a);
 
361
 
 
362
        /**
 
363
           Construct a (shallow) copy of another secure byte array
 
364
 
 
365
           \param from the source of the data and length.
 
366
        */
 
367
        SecureArray(const SecureArray &from);
 
368
 
 
369
        ~SecureArray();
 
370
 
 
371
        /**
 
372
           Creates a reference, rather than a deep copy.
 
373
 
 
374
           \param from the array to reference
 
375
        */
 
376
        SecureArray & operator=(const SecureArray &from);
 
377
 
 
378
        /**
 
379
           Creates a copy, rather than references
 
380
 
 
381
           \param a the array to copy from
 
382
        */
 
383
        SecureArray & operator=(const QByteArray &a);
 
384
 
 
385
        /**
 
386
           Clears the contents of the array and makes it empty
 
387
        */
 
388
        void clear();
 
389
 
 
390
        /**
 
391
           Returns a reference to the byte at the index position
 
392
 
 
393
           \param index the zero-based offset to obtain
 
394
        */
 
395
        char & operator[](int index);
 
396
 
 
397
        /**
 
398
           Returns a reference to the byte at the index position
 
399
 
 
400
           \param index the zero-based offset to obtain
 
401
        */
 
402
        const char & operator[](int index) const;
 
403
 
 
404
        /**
 
405
           Pointer to the data in the secure array
 
406
 
 
407
           You can use this for memcpy and similar functions. If you are trying
 
408
           to obtain data at a particular offset, you might be better off using
 
409
           at() or operator[]
 
410
        */
 
411
        char *data();
 
412
 
 
413
        /**
 
414
           Pointer to the data in the secure array
 
415
 
 
416
           You can use this for memcpy and similar functions. If you are trying
 
417
           to obtain data at a particular offset, you might be better off using
 
418
           at() or operator[]
 
419
        */
 
420
        const char *data() const;
 
421
 
 
422
        /**
 
423
           Pointer to the data in the secure array
 
424
 
 
425
           You can use this for memcpy and similar functions. If you are trying
 
426
           to obtain data at a particular offset, you might be better off using
 
427
           at() or operator[]
 
428
        */
 
429
        const char *constData() const;
 
430
 
 
431
        /**
 
432
           Returns a reference to the byte at the index position
 
433
 
 
434
           \param index the zero-based offset to obtain
 
435
        */
 
436
        char & at(int index);
 
437
 
 
438
        /**
 
439
           Returns a reference to the byte at the index position
 
440
 
 
441
           \param index the zero-based offset to obtain
 
442
        */
 
443
        const char & at(int index) const;
 
444
 
 
445
        /**
 
446
           Returns the number of bytes in the array
 
447
        */
 
448
        int size() const;
 
449
 
 
450
        /**
 
451
           Test if the array contains any bytes.
 
452
 
 
453
           This is equivalent to testing (size() != 0). Note that if
 
454
           the array is allocated, isEmpty() is false (even if no data
 
455
           has been added)
 
456
 
 
457
           \return true if the array has zero length, otherwise false
 
458
        */
 
459
        bool isEmpty() const;
 
460
 
 
461
        /**
 
462
           Change the length of this array
 
463
           If the new length is less than the old length, the extra information
 
464
           is (safely) discarded. If the new length is equal to or greater than
 
465
           the old length, the existing data is copied into the array.
 
466
 
 
467
           \param size the new length
 
468
        */
 
469
        bool resize(int size);
 
470
 
 
471
        /**
 
472
           Fill the data array with a specified character
 
473
 
 
474
           \param fillChar the character to use as the fill
 
475
           \param fillToPosition the number of characters to fill
 
476
           to. If not specified (or -1), fills array to
 
477
           current length.
 
478
 
 
479
           \note This function does not extend the array - if
 
480
           you ask for fill beyond the current length, only
 
481
           the current length will be used.
 
482
           \note The number of characters is 1 based, so if
 
483
           you ask for fill('x', 10), it will fill from
 
484
        */
 
485
        void fill(char fillChar, int fillToPosition = -1);
 
486
 
 
487
        /**
 
488
           Copy the contents of the secure array out to a 
 
489
           standard QByteArray. Note that this performs a deep copy
 
490
           of the data.
 
491
        */
 
492
        QByteArray toByteArray() const;
 
493
 
 
494
        /**
 
495
           Append a secure byte array to the end of this array
 
496
 
 
497
           \param a the array to append to this array
 
498
        */
 
499
        SecureArray & append(const SecureArray &a);
 
500
 
 
501
        /**
 
502
           Equality operator. Returns true if both arrays have the same
 
503
           data (and the same length, of course).
 
504
 
 
505
           \param other the MemoryRegion to compare to
 
506
        */
 
507
        bool operator==(const MemoryRegion &other) const;
 
508
        
 
509
        /**
 
510
           Inequality operator. Returns true if both arrays have different
 
511
           length, or the same length but different data.
 
512
 
 
513
           \param other the MemoryRegion to compare to
 
514
        */
 
515
        inline bool operator!=(const MemoryRegion &other) const
 
516
        {
 
517
                return !(*this == other);
 
518
        }
 
519
 
 
520
        /**
 
521
           Append a secure byte array to the end of this array
 
522
 
 
523
           \param a the array to append to this array
 
524
        */
 
525
        SecureArray & operator+=(const SecureArray &a);
 
526
 
 
527
protected:
 
528
        /**
 
529
           Assign the contents of a provided byte array to this
 
530
           object.
 
531
 
 
532
           \param from the byte array to copy
 
533
        */
 
534
        void set(const SecureArray &from);
 
535
 
 
536
        /**
 
537
           Assign the contents of a provided byte array to this
 
538
           object.
 
539
 
 
540
           \param from the byte array to copy
 
541
        */
 
542
        void set(const QByteArray &from);
 
543
};
 
544
 
 
545
/**
 
546
   Returns an array that is the result of concatenating a and b
 
547
 
 
548
   \param a the string to put at the start of the result
 
549
   \param b the string to put at the end of the result
 
550
*/
 
551
QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
 
552
 
 
553
/**
 
554
   \class BigInteger qca_tools.h QtCrypto
 
555
 
 
556
   Arbitrary precision integer
 
557
 
 
558
   BigInteger provides arbitrary precision integers.
 
559
   \code
 
560
if ( BigInteger("3499543804349") == 
 
561
        BigInteger("38493290803248") + BigInteger( 343 ) )
 
562
{
 
563
        // do something
 
564
}
 
565
   \endcode
 
566
 
 
567
   \ingroup UserAPI
 
568
*/
 
569
class QCA_EXPORT BigInteger
 
570
{
 
571
public:
 
572
        /**
 
573
           Constructor. Creates a new BigInteger, initialised to zero.
 
574
        */
 
575
        BigInteger();
 
576
 
 
577
        /**
 
578
           \overload
 
579
 
 
580
           \param n an alternative integer initialisation value.
 
581
        */
 
582
        BigInteger(int n);
 
583
 
 
584
        /**
 
585
           \overload
 
586
 
 
587
           \param c an alternative initialisation value, encoded as a character array
 
588
 
 
589
           \code
 
590
BigInteger b ( "9890343" );
 
591
           \endcode
 
592
        */
 
593
        BigInteger(const char *c);
 
594
 
 
595
        /**
 
596
           \overload
 
597
 
 
598
           \param s an alternative initialisation value, encoded as a string
 
599
        */
 
600
        BigInteger(const QString &s);
 
601
 
 
602
        /**
 
603
           \overload
 
604
 
 
605
           \param a an alternative initialisation value, encoded as SecureArray
 
606
        */
 
607
        BigInteger(const QCA::SecureArray &a);
 
608
 
 
609
        /**
 
610
           \overload
 
611
 
 
612
           \param from an alternative initialisation value, encoded as a %BigInteger
 
613
        */
 
614
        BigInteger(const BigInteger &from);
 
615
 
 
616
        ~BigInteger();
 
617
 
 
618
        /**
 
619
           Assignment operator
 
620
 
 
621
           \param from the BigInteger to copy from
 
622
 
 
623
           \code
 
624
BigInteger a; // a is zero
 
625
BigInteger b( 500 );
 
626
a = b; // a is now 500
 
627
           \endcode
 
628
        */
 
629
        BigInteger & operator=(const BigInteger &from);
 
630
 
 
631
        /**
 
632
           \overload
 
633
 
 
634
           \param s the QString containing an integer representation
 
635
 
 
636
           \sa bool fromString(const QString &s)
 
637
 
 
638
           \note it is the application's responsibility to make sure
 
639
           that the QString represents a valid integer (ie it only
 
640
           contains numbers and an optional minus sign at the start)
 
641
        */
 
642
        BigInteger & operator=(const QString &s);
 
643
 
 
644
        /**
 
645
           Increment in place operator
 
646
 
 
647
           \param b the amount to increment by
 
648
 
 
649
           \code
 
650
BigInteger a; // a is zero
 
651
BigInteger b( 500 );
 
652
a += b; // a is now 500
 
653
a += b; // a is now 1000
 
654
           \endcode
 
655
        */
 
656
        BigInteger & operator+=(const BigInteger &b);
 
657
 
 
658
        /**
 
659
           Decrement in place operator
 
660
 
 
661
           \param b the amount to decrement by
 
662
 
 
663
           \code
 
664
BigInteger a; // a is zero
 
665
BigInteger b( 500 );
 
666
a -= b; // a is now -500
 
667
a -= b; // a is now -1000
 
668
           \endcode
 
669
        */
 
670
        BigInteger & operator-=(const BigInteger &b);
 
671
 
 
672
        /**
 
673
           Multiply in place operator
 
674
 
 
675
           \param b the amount to multiply by
 
676
        */
 
677
        BigInteger & operator*=(const BigInteger &b);
 
678
 
 
679
        /**
 
680
           Divide in place operator
 
681
 
 
682
           \param b the amount to divide by
 
683
        */
 
684
        BigInteger & operator/=(const BigInteger &b);
 
685
 
 
686
        /**
 
687
           Modulo in place operator
 
688
 
 
689
           \param b the amount to divide by
 
690
        */
 
691
        BigInteger & operator%=(const BigInteger &b);
 
692
 
 
693
        /**
 
694
           Output %BigInteger as a byte array, useful for storage or
 
695
           transmission.  The format is a binary integer in sign-extended
 
696
           network-byte-order.
 
697
 
 
698
           \sa void fromArray(const SecureArray &a);
 
699
        */
 
700
        QCA::SecureArray toArray() const;
 
701
 
 
702
        /**
 
703
           Assign from an array.  The input is expected to be a binary integer
 
704
           in sign-extended network-byte-order.
 
705
 
 
706
           \param a a SecureArray that represents an integer
 
707
 
 
708
           \sa BigInteger(const SecureArray &a);
 
709
           \sa SecureArray toArray() const;
 
710
        */
 
711
        void fromArray(const QCA::SecureArray &a);
 
712
 
 
713
        /**
 
714
           Convert %BigInteger to a QString
 
715
 
 
716
           \code
 
717
QString aString;
 
718
BigInteger aBiggishInteger( 5878990 );
 
719
aString = aBiggishInteger.toString(); // aString is now "5878990"
 
720
           \endcode
 
721
        */
 
722
        QString toString() const;
 
723
 
 
724
        /**
 
725
           Assign from a QString
 
726
 
 
727
           \param s a QString that represents an integer
 
728
 
 
729
           \note it is the application's responsibility to make sure
 
730
           that the QString represents a valid integer (ie it only
 
731
           contains numbers and an optional minus sign at the start)
 
732
 
 
733
           \sa BigInteger(const QString &s)
 
734
           \sa BigInteger & operator=(const QString &s)
 
735
        */
 
736
        bool fromString(const QString &s);
 
737
 
 
738
        /**
 
739
           Compare this value with another %BigInteger
 
740
 
 
741
           Normally it is more readable to use one of the operator overloads,
 
742
           so you don't need to use this method directly.
 
743
 
 
744
           \param n the BigInteger to compare with
 
745
 
 
746
           \return zero if the values are the same, negative if the argument
 
747
           is less than the value of this BigInteger, and positive if the
 
748
           argument value is greater than this BigInteger
 
749
 
 
750
           \code
 
751
BigInteger a( "400" );
 
752
BigInteger b( "-400" );
 
753
BigInteger c( " 200 " );
 
754
int result;
 
755
result = a.compare( b );        // return positive 400 > -400
 
756
result = a.compare( c );        // return positive,  400 > 200
 
757
result = b.compare( c );        // return negative, -400 < 200
 
758
           \endcode
 
759
        */
 
760
        int compare(const BigInteger &n) const;
 
761
 
 
762
        /**
 
763
           Equality operator. Returns true if the two BigInteger values
 
764
           are the same, including having the same sign.
 
765
 
 
766
           \param other the BigInteger to compare to
 
767
        */
 
768
        inline bool operator==(const BigInteger &other) const
 
769
        {
 
770
                return (compare(other) == 0);
 
771
        }
 
772
 
 
773
        /**
 
774
           Inequality operator. Returns true if the two BigInteger values
 
775
           are different in magnitude, sign or both.
 
776
 
 
777
           \param other the BigInteger to compare to
 
778
        */
 
779
        inline bool operator!=(const BigInteger &other) const
 
780
        {
 
781
                return !(*this == other);
 
782
        }
 
783
 
 
784
        /**
 
785
           Less than or equal operator. Returns true if the BigInteger value
 
786
           on the left hand side is equal to or less than the BigInteger
 
787
           value on the right hand side.
 
788
 
 
789
           \param other the BigInteger to compare to
 
790
        */
 
791
        inline bool operator<=(const BigInteger &other) const
 
792
        {
 
793
                return (compare(other) <= 0);
 
794
        }
 
795
 
 
796
        /**
 
797
           Greater than or equal operator. Returns true if the BigInteger
 
798
           value on the left hand side is equal to or greater than the
 
799
           BigInteger value on the right hand side.
 
800
 
 
801
           \param other the BigInteger to compare to
 
802
        */
 
803
        inline bool operator>=(const BigInteger &other) const
 
804
        {
 
805
                return (compare(other) >= 0);
 
806
        }
 
807
 
 
808
        /**
 
809
           Less than operator. Returns true if the BigInteger value
 
810
           on the left hand side is less than the BigInteger value
 
811
           on the right hand side.
 
812
 
 
813
           \param other the BigInteger to compare to
 
814
        */
 
815
        inline bool operator<(const BigInteger &other) const
 
816
        {
 
817
                return (compare(other) < 0);
 
818
        }
 
819
 
 
820
        /**
 
821
           Greater than operator. Returns true if the BigInteger value
 
822
           on the left hand side is greater than the BigInteger value
 
823
           on the right hand side.
 
824
 
 
825
           \param other the BigInteger to compare to
 
826
        */
 
827
        inline bool operator>(const BigInteger &other) const
 
828
        {
 
829
                return (compare(other) > 0);
 
830
        }
 
831
 
 
832
private:
 
833
        class Private;
 
834
        QSharedDataPointer<Private> d;
 
835
};
 
836
 
 
837
 
 
838
 
 
839
/**
 
840
   Stream operator
 
841
 
 
842
   \param stream the stream to write to
 
843
   \param b the integer to write to the stream
 
844
 
 
845
   \relates BigInteger
 
846
*/
 
847
QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
 
848
 
 
849
 
 
850
}
 
851
 
 
852
#endif