changeset 9:2ad9a96518e3

Actually parse all elements in the list
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 12 Feb 2014 18:45:13 +0000
parents c7da699f0310
children fe39d93f1261
files tests/certlistparsertest.cpp ui/certificatelist.cpp ui/certificatelist.h ui/listutil.c
diffstat 4 files changed, 54 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/tests/certlistparsertest.cpp	Wed Feb 12 17:17:44 2014 +0000
+++ b/tests/certlistparsertest.cpp	Wed Feb 12 18:45:13 2014 +0000
@@ -57,7 +57,7 @@
 {
     const char *fname = "list-valid-signed.txt";
 
-    QBENCHMARK {
+    QBENCHMARK_ONCE {
         CertificateList *certList = testWithFile(fname);
         delete certList;
     }
--- a/ui/certificatelist.cpp	Wed Feb 12 17:17:44 2014 +0000
+++ b/ui/certificatelist.cpp	Wed Feb 12 18:45:13 2014 +0000
@@ -2,6 +2,8 @@
 
 #include <QDebug>
 
+#define PARSER_VERSION "1"
+
 CertificateList::CertificateList(const char *fileName)
 {
     char *data = NULL;
@@ -9,7 +11,46 @@
 
     mStatus = readAndVerifyList(fileName, &data, &size);
 
-    if (isValid()) {
-        mFileContent = QByteArray::fromRawData(data, size);
+    if (!isValid()) {
+        return;
+    }
+
+    // Take the data into the Qt Universe where memory is plentiful
+    // and CPU's are fast :)
+    QStringList lines = QString::fromLatin1(data, size).split("\n");
+    free(data);
+    data = NULL;
+
+    for (int i = 0; i < lines.size(); ++i) {
+        QString curLine = lines[i].trimmed();
+        if (curLine.startsWith("F:")) {
+            if (curLine.right(1) != PARSER_VERSION) {
+                qDebug() << "Invalid Format Version";
+                mStatus = IncompatibleVersion;
+                return;
+            }
+        } else if (curLine.startsWith("D:")) {
+            bool ok = false;
+            qlonglong timestamp = 0;
+
+            curLine.remove(0, 2);
+            timestamp = curLine.toLongLong(&ok);
+            if (!ok) {
+                qDebug() << "Invalid Date";
+                mStatus = InvalidFormat;
+                return;
+            }
+
+            mDate = QDateTime::fromMSecsSinceEpoch(timestamp * 1000);
+        } else if (curLine.startsWith("I:")) {
+            mCertificatesInstall << QByteArray::fromBase64(curLine.remove(0,2).toLatin1());
+        } else if (curLine.startsWith("R:")) {
+            mCertificatesInstall << QByteArray::fromBase64(curLine.remove(0,2).toLatin1());
+        } else if (curLine.startsWith("S:")) {
+            // Signature is verified in readAndVerifyList
+            continue;
+        } else if (!curLine.isEmpty()){
+            qDebug () << "Don't know how to handle: " << curLine;
+        }
     }
 }
--- a/ui/certificatelist.h	Wed Feb 12 17:17:44 2014 +0000
+++ b/ui/certificatelist.h	Wed Feb 12 18:45:13 2014 +0000
@@ -18,7 +18,7 @@
 class QByteArray;
 
 #include <QString>
-#include <QDate>
+#include <QDateTime>
 #include <QObject>
 
 #include "listutil.h"
@@ -35,8 +35,9 @@
 
 private:
     QString mFileName;
-    QByteArray mFileContent;
+    QList<QByteArray> mCertificatesInstall;
+    QList<QByteArray> mCertificatesRemove;
     list_status_t mStatus;
-    QDate mDate;
+    QDateTime mDate;
 };
 #endif
--- a/ui/listutil.c	Wed Feb 12 17:17:44 2014 +0000
+++ b/ui/listutil.c	Wed Feb 12 18:45:13 2014 +0000
@@ -59,7 +59,7 @@
 
     *data = (char*) malloc(*size);
 
-    if (data == NULL) {
+    if (*data == NULL) {
         printf("Malloc failed\n");
         retval = UnknownError;
         goto cleanup;
@@ -96,7 +96,7 @@
 list_status_t readAndVerifyList(const char *fileName, char **data, size_t *size)
 {
 //    int validSig = 0;
-    char ** firstChar = NULL;
+    char * signature = NULL;
 
     list_status_t retval = UnknownError;
     *data = NULL;
@@ -111,14 +111,12 @@
 
     if (!data || !*size) {
         // should not have happend if readList works as specified
-        printf ("No data or no size\n");
-        printf ("%ld\n", (long)data);
         return UnknownError;
     }
 
-    firstChar = (char**) data;
+    signature = *data;
 
-    if (**firstChar != 'S') {
+    if (*signature != 'S') {
         printf("Does not start with S\n");
         retval = InvalidFormat;
         goto cleanup;
@@ -127,6 +125,8 @@
 // TODO VERIFIY
 retval = Valid;
 
+// Maybe check if all bytes are < 127 and > 0
+
 cleanup:
     if (retval != Valid && *data) {
         free(*data);

http://wald.intevation.org/projects/trustbridge/