00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00035 #ifndef QCA_TOOLS_H
00036 #define QCA_TOOLS_H
00037
00038 #include <QSharedData>
00039 #include <QSharedDataPointer>
00040 #include <QMetaType>
00041 #include "qca_export.h"
00042
00043 class QString;
00044 class QByteArray;
00045 class QTextStream;
00046
00054 QCA_EXPORT void *qca_secure_alloc(int bytes);
00055
00064 QCA_EXPORT void qca_secure_free(void *p);
00065
00073 QCA_EXPORT void *qca_secure_realloc(void *p, int bytes);
00074
00075 namespace QCA {
00076
00089 class QCA_EXPORT MemoryRegion
00090 {
00091 public:
00092 MemoryRegion();
00093
00100 MemoryRegion(const char *str);
00101
00108 MemoryRegion(const QByteArray &from);
00109
00115 MemoryRegion(const MemoryRegion &from);
00116 ~MemoryRegion();
00117
00123 MemoryRegion & operator=(const MemoryRegion &from);
00124
00130 MemoryRegion & operator=(const QByteArray &from);
00131
00140 bool isNull() const;
00141
00150 bool isSecure() const;
00151
00160 QByteArray toByteArray() const;
00161
00165 bool isEmpty() const;
00166
00170 int size() const;
00171
00181 const char *data() const;
00182
00191 const char *constData() const;
00192
00203 const char & at(int index) const;
00204
00205 protected:
00217 MemoryRegion(bool secure);
00218
00228 MemoryRegion(int size, bool secure);
00229
00242 MemoryRegion(const QByteArray &from, bool secure);
00243
00249 char *data();
00250
00261 char & at(int index);
00262
00268 bool resize(int size);
00269
00279 void set(const QByteArray &from, bool secure);
00280
00292 void setSecure(bool secure);
00293
00294 private:
00295 bool _secure;
00296 class Private;
00297 QSharedDataPointer<Private> d;
00298 };
00299
00315 class QCA_EXPORT SecureArray : public MemoryRegion
00316 {
00317 public:
00321 SecureArray();
00322
00329 explicit SecureArray(int size, char ch = 0);
00330
00338 SecureArray(const char *str);
00339
00349 SecureArray(const QByteArray &a);
00350
00360 SecureArray(const MemoryRegion &a);
00361
00367 SecureArray(const SecureArray &from);
00368
00369 ~SecureArray();
00370
00376 SecureArray & operator=(const SecureArray &from);
00377
00383 SecureArray & operator=(const QByteArray &a);
00384
00388 void clear();
00389
00395 char & operator[](int index);
00396
00402 const char & operator[](int index) const;
00403
00411 char *data();
00412
00420 const char *data() const;
00421
00429 const char *constData() const;
00430
00436 char & at(int index);
00437
00443 const char & at(int index) const;
00444
00448 int size() const;
00449
00459 bool isEmpty() const;
00460
00469 bool resize(int size);
00470
00485 void fill(char fillChar, int fillToPosition = -1);
00486
00492 QByteArray toByteArray() const;
00493
00499 SecureArray & append(const SecureArray &a);
00500
00507 bool operator==(const MemoryRegion &other) const;
00508
00515 inline bool operator!=(const MemoryRegion &other) const
00516 {
00517 return !(*this == other);
00518 }
00519
00525 SecureArray & operator+=(const SecureArray &a);
00526
00527 protected:
00534 void set(const SecureArray &from);
00535
00542 void set(const QByteArray &from);
00543 };
00544
00551 QCA_EXPORT const SecureArray operator+(const SecureArray &a, const SecureArray &b);
00552
00569 class QCA_EXPORT BigInteger
00570 {
00571 public:
00575 BigInteger();
00576
00582 BigInteger(int n);
00583
00593 BigInteger(const char *c);
00594
00600 BigInteger(const QString &s);
00601
00607 BigInteger(const QCA::SecureArray &a);
00608
00614 BigInteger(const BigInteger &from);
00615
00616 ~BigInteger();
00617
00629 BigInteger & operator=(const BigInteger &from);
00630
00642 BigInteger & operator=(const QString &s);
00643
00656 BigInteger & operator+=(const BigInteger &b);
00657
00670 BigInteger & operator-=(const BigInteger &b);
00671
00677 BigInteger & operator*=(const BigInteger &b);
00678
00684 BigInteger & operator/=(const BigInteger &b);
00685
00691 BigInteger & operator%=(const BigInteger &b);
00692
00700 QCA::SecureArray toArray() const;
00701
00711 void fromArray(const QCA::SecureArray &a);
00712
00722 QString toString() const;
00723
00736 bool fromString(const QString &s);
00737
00760 int compare(const BigInteger &n) const;
00761
00768 inline bool operator==(const BigInteger &other) const
00769 {
00770 return (compare(other) == 0);
00771 }
00772
00779 inline bool operator!=(const BigInteger &other) const
00780 {
00781 return !(*this == other);
00782 }
00783
00791 inline bool operator<=(const BigInteger &other) const
00792 {
00793 return (compare(other) <= 0);
00794 }
00795
00803 inline bool operator>=(const BigInteger &other) const
00804 {
00805 return (compare(other) >= 0);
00806 }
00807
00815 inline bool operator<(const BigInteger &other) const
00816 {
00817 return (compare(other) < 0);
00818 }
00819
00827 inline bool operator>(const BigInteger &other) const
00828 {
00829 return (compare(other) > 0);
00830 }
00831
00832 private:
00833 class Private;
00834 QSharedDataPointer<Private> d;
00835 };
00836
00837
00838
00847 QCA_EXPORT QTextStream &operator<<(QTextStream &stream, const BigInteger &b);
00848
00849
00850 }
00851
00852 #endif