[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdesupport/qca
From:       Justin Karneges <infiniti () affinix ! com>
Date:       2004-10-27 0:02:09
Message-ID: 20041027000209.1964616CA8 () office ! kde ! org
[Download RAW message or body]

CVS commit by infiniti: 

secure array copy-on-write


  M +0 -3      TODO   1.37
  M +7 -1      src/qca.h   1.46
  M +57 -14    src/qca_tools.cpp   1.7


--- kdesupport/qca/TODO  #1.36:1.37
@@ -2,8 +2,5 @@
 * API documentation
 
-* securearray: do copy-on-write instead of all the time, don't use SecureVector
-* base64 filter code
 * default provider with random, sha1, and md5 ??
-* wrap the Botan namespace in the QCA namespace (to avoid symbol conflict)
 
 QCA 2 TODO:

--- kdesupport/qca/src/qca.h  #1.45:1.46
@@ -107,7 +107,10 @@ public:
         QSecureArray & operator=(const QSecureArray &from);
         QSecureArray & operator=(const QByteArray &a);
-        char & operator[](int index) const;
+        char & operator[](int index);
+        const char & operator[](int index) const;
 
         char *data() const;
+        const char & at(uint index) const;
+        char & at(uint index);
         uint size() const;
         bool isEmpty() const;
@@ -115,4 +118,5 @@ public:
 
         QSecureArray copy() const;
+        void detach();
         QByteArray toByteArray() const;
 
@@ -120,4 +124,6 @@ private:
         class Private;
         Private *d;
+
+        void reset();
 };
 

--- kdesupport/qca/src/qca_tools.cpp  #1.6:1.7
@@ -102,8 +102,9 @@ class QSecureArray::Private
 {
 public:
-        Private(uint size) : buf((Botan::u32bit)size) {}
-        Private(const Botan::SecureVector<Botan::byte> &a) : buf(a) {}
+        Private(uint size) : buf((Botan::u32bit)size), refs(1) {}
+        Private(const Botan::SecureVector<Botan::byte> &a) : buf(a), refs(1) {}
 
         Botan::SecureVector<Botan::byte> buf;
+        int refs;
 };
 
@@ -135,15 +136,27 @@ QSecureArray::QSecureArray(const QSecure
 QSecureArray::~QSecureArray()
 {
-        delete d;
+        reset();
 }
 
-QSecureArray & QSecureArray::operator=(const QSecureArray &from)
+void QSecureArray::reset()
 {
+        if(d)
+        {
+                --d->refs;
+                if(d->refs == 0)
         delete d;
         d = 0;
+        }
+}
 
-        if(from.d)
-                d = new Private(from.d->buf);
+QSecureArray & QSecureArray::operator=(const QSecureArray &from)
+{
+        reset();
 
+        if(from.d)
+        {
+                d = from.d;
+                ++d->refs;
+        }
         return *this;
 }
@@ -151,8 +164,8 @@ QSecureArray & QSecureArray::operator=(c
 QSecureArray & QSecureArray::operator=(const QByteArray &from)
 {
-        delete d;
-        d = 0;
+        reset();
 
-        if(!from.isEmpty()) {
+        if(!from.isEmpty())
+        {
                 d = new Private(from.size());
                 Botan::byte *p = (Botan::byte *)d->buf;
@@ -163,9 +176,25 @@ QSecureArray & QSecureArray::operator=(c
 }
 
-char & QSecureArray::operator[](int index) const
+char & QSecureArray::operator[](int index)
+{
+        return at(index);
+}
+
+const char & QSecureArray::operator[](int index) const
+{
+        return at(index);
+}
+
+const char & QSecureArray::at(uint index) const
 {
         return (char &)(*((Botan::byte *)d->buf + index));
 }
 
+char & QSecureArray::at(uint index)
+{
+        detach();
+        return (char &)(*((Botan::byte *)d->buf + index));
+}
+
 char *QSecureArray::data() const
 {
@@ -192,8 +221,12 @@ bool QSecureArray::resize(uint size)
                 return true;
 
-        if(size > 0) {
+        detach();
+
+        if(size > 0)
+        {
                 Private *d2 = new Private(size);
                 Botan::byte *p2 = (Botan::byte *)d2->buf;
-                if(d) {
+                if(d)
+                {
                         Botan::byte *p = (Botan::byte *)d->buf;
                         memcpy(p2, p, cur_size);
@@ -211,5 +244,15 @@ bool QSecureArray::resize(uint size)
 QSecureArray QSecureArray::copy() const
 {
-        return *this;
+        QSecureArray a = *this;
+        a.detach();
+        return a;
+}
+
+void QSecureArray::detach()
+{
+        if(!d || d->refs <= 1)
+                return;
+        --d->refs;
+        d = new Private(d->buf);
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic