]> gitweb.hamatoma.de Git - reqt/commitdiff
dayly work
authorhama <hama@siduction.net>
Tue, 17 Nov 2015 23:19:18 +0000 (00:19 +0100)
committerhama <hama@siduction.net>
Tue, 17 Nov 2015 23:19:18 +0000 (00:19 +0100)
base/ReProcess.cpp [new file with mode: 0644]
base/ReProcess.hpp [new file with mode: 0644]
base/ReRandomizer.cpp
base/ReRandomizer.hpp
base/ReStringUtils.cpp
base/ReTest.cpp
base/rebase.hpp
base/retrace.hpp
cunit/allTests.cpp
cunit/cuReRandomizer.cpp
cunit/cunit.pro

diff --git a/base/ReProcess.cpp b/base/ReProcess.cpp
new file mode 100644 (file)
index 0000000..c9ccd1c
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#include "base/rebase.hpp"
+
+ReProcess::ReProcess()
+{
+
+}
+
+/**
+ * Executes an external program and return its output.
+ *
+ * @param program      the program to execute
+ * @param args         the program arguments
+ * @param timeout      the maximal count of seconds waiting for program's end
+ * @return                     the output (stdout) of the command
+ */
+QByteArray ReProcess::executeAndRead(const QString& program,
+               const QStringList& args, int timeout)
+{
+       QProcess process;
+       process.start(program, args, QIODevice::ReadOnly);
+       process.waitForFinished(timeout * 1000);
+       QByteArray rc = process.readAllStandardOutput();
+       return rc;
+}
+
+/**
+ * Executes an external program and return its output.
+ *
+ * @param command      command to execute
+ * @param timeout      the maximal count of seconds waiting for program's end
+ * @return                     the output (stdout) of the command
+ */
+QByteArray ReProcess::executeAndRead(const QByteArray& command, int timeout)
+{
+       QStringList args = QString(command).split(' ');
+       QString program = args.at(0);
+       args.removeAt(0);
+       QByteArray rc = executeAndRead(program, args, timeout);
+       return rc;
+}
+
diff --git a/base/ReProcess.hpp b/base/ReProcess.hpp
new file mode 100644 (file)
index 0000000..c549d71
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#ifndef REPROCESS_HPP
+#define REPROCESS_HPP
+
+
+class ReProcess
+{
+public:
+       ReProcess();
+
+public:
+       static QByteArray executeAndRead(const QString& program,
+                       const QStringList& args, int timeout = 60);
+       static QByteArray executeAndRead(const QByteArray& command, int timeout = 60);
+       static QByteArray executeAndFilter(const char* command, const QString& regExpr);
+
+};
+
+#endif // REPROCESS_HPP
index 0a1503b4986fd14570e58d0506106ddc83c81ac3..8ef7b7a0aa344c1cec20486648f92125b28cc6d8 100644 (file)
@@ -10,7 +10,7 @@
  */
 
 #include "base/rebase.hpp"
-//#define WITH_TRACE
+#define WITH_TRACE
 #include "retrace.hpp"
 enum {
        LOC_READ_1 = LOC_FIRST_OF(LOC_RANDOMIZER), // 12201
@@ -957,11 +957,7 @@ ReKISSRandomizer::ReKISSRandomizer() :
  * Dumps the state of the generator.
  */
 void ReKISSRandomizer::dump() {
-       printf("%2d: f: %016llx i: %016llx: c: %016llx x: %016llx y: %016llx z: %016llx\n",
-               m_counter,
-               (long long) m_factor, (long long) m_increment,
-                  (long long) m_params.m_c, (long long) m_params.m_x,
-                  (long long)  m_params.m_y, (long long) m_params.m_z);
+       printf("%s\n", state().constData());
        printf(
                "    f: %016llx i: %016llx: c: %016llx x: %016llx y: %016llx z: %016llx\n",
                (long long) m_factor, (long long) m_increment,
@@ -999,6 +995,7 @@ ReRandomizer::seed_t ReKISSRandomizer::nextSeed64() {
  */
 void ReKISSRandomizer::modifySeed(int64_t seed) {
        m_params.m_x ^= seed;
+       TRACE1("modifySeed() %s\n", state().constData());
 }
 /**
  * Sets the seed to the start point (defined with setSeed()).
@@ -1006,6 +1003,7 @@ void ReKISSRandomizer::modifySeed(int64_t seed) {
 void ReKISSRandomizer::reset() {
        m_params = m_startParams;
        m_counter = 0;
+       TRACE1("reset() %s\n", state().constData());
 }
 
 /**
@@ -1028,6 +1026,22 @@ void ReKISSRandomizer::saveSeed(QByteArray& seed) const {
        save(m_params, seed);
 }
 
+/**
+ * Returns the internal state as string.
+ *
+ * @return     the internal state
+ */
+QByteArray ReKISSRandomizer::state() const{
+       char buffer[512];
+       snprintf(buffer, sizeof buffer,
+                        "%2d: f: %016llx i: %016llx: c: %016llx x: %016llx y: %016llx z: %016llx",
+               m_counter,
+               (long long) m_factor, (long long) m_increment,
+                  (long long) m_params.m_c, (long long) m_params.m_x,
+                  (long long)  m_params.m_y, (long long) m_params.m_z);
+       return QByteArray(buffer);
+}
+
 /**
  * Converts a text (e.g. password) into the generator specific seed.
  *
@@ -1049,7 +1063,6 @@ void ReKISSRandomizer::textToSeed(const QByteArray& text) {
  */
 ReByteScrambler::ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger) :
        m_contentRandom(contentRandom),
-       m_realRandom(),
        m_contentSeed(),
        m_buffer(),
        m_header(),
@@ -1059,7 +1072,38 @@ ReByteScrambler::ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger)
        m_salt.m_int = 0;
        m_contentRandom.saveSeed(m_contentSeed);
        m_buffer.reserve(256);
-       m_realRandom.nearTrueRandom();
+}
+
+/**
+ * Copy constructor.
+ *
+ * @param source       the source to copy
+ */
+ReByteScrambler::ReByteScrambler(const ReByteScrambler& source) :
+       m_contentRandom(source.m_contentRandom),
+       m_contentSeed(source.m_contentSeed),
+       m_buffer(source.m_buffer),
+       m_header(source.m_header),
+       m_logger(source.m_logger),
+       m_salt(source.m_salt)
+{
+}
+
+/**
+ * Asignment operator.
+ *
+ * @param source       source to copy
+ * @return                     the instance
+ */
+ReByteScrambler&ReByteScrambler::operator =(const ReByteScrambler& source)
+{
+       m_contentRandom = source.m_contentRandom;
+       m_contentSeed = source.m_contentSeed;
+       m_buffer = source.m_buffer;
+       m_header = source.m_header;
+       m_logger = source.m_logger;
+       m_salt = source.m_salt;
+       return *this;
 }
 
 /**
@@ -1071,8 +1115,7 @@ ReByteScrambler::ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger)
 ReRandomizer& ReByteScrambler::contentRandom(bool doReset)
 {
        if (doReset){
-               m_contentRandom.reset();
-               m_contentRandom.modifySeed(m_salt.m_int);
+               randomReset();
        }
        return m_contentRandom;
 }
@@ -1108,18 +1151,22 @@ bool ReByteScrambler::initFromHeader(int reservedLength, int markerLength,
        } else {
                m_salt.fromBytes(reinterpret_cast<const uint8_t*>(header->constData()));
                randomReset();
+               TRACE1("salt: %08lx\n", m_salt.m_int);
                QByteArray expectedMarker;
                if (markerLength > 0)
                        m_contentRandom.nextString(markerLength, markerLength, expectedMarker);
-               if (encryptedFrom < m_header.length()){
+               if (encryptedFrom < header->length()){
+                       randomReset();
                        uint8_t* start = reinterpret_cast<uint8_t*>(header->data()) + encryptedFrom;
+                       TRACE1("info before decoding: %s\n", hexBytes(start, 16).constData());
                        m_contentRandom.codec(start, start, header->length() - encryptedFrom);
+                       TRACE1("info after decoding: %s\n", hexBytes(start, 16).constData());
                }
                if (markerLength > 0){
                        QByteArray marker;
-                       marker.append(reinterpret_cast<const char*>(
-                                       header->constData()+ sizeof(int64_t) + reservedLength),
+                       marker = header->mid(sizeof(int64_t) + reservedLength,
                                        markerLength);
+                       TRACE1("marker: %s\n", marker.constData());
                        if (marker != expectedMarker){
                                m_logger->logv(LOG_ERROR, LOC_DECODE_CONTENT_2,
                                        "invalid marker: %s / %s",
@@ -1130,7 +1177,6 @@ bool ReByteScrambler::initFromHeader(int reservedLength, int markerLength,
                                rc = false;
                        }
                }
-               TRACE1("info: Length: %d\n", length);
                info.resize(0);
                if (infoLength > 0)
                        info = header->mid(sizeof(int64_t) + reservedLength + markerLength,
@@ -1163,32 +1209,34 @@ void ReByteScrambler::initHeader(int reservedLength, int markerLength,
        TRACE("initHeader():\n");
        encryptedFrom = max(encryptedFrom, encryptedFrom < (int) sizeof(int64_t)
                                                + reservedLength + markerLength);
-       m_salt.m_int = m_realRandom.nextSeed64();
+       m_salt.m_int = ReRandomizer::nearTrueRandom();
        m_contentRandom.reset();
        m_contentRandom.modifySeed(m_salt.m_int);
        int headerLength = sizeof(int64_t) + reservedLength + markerLength + infoLength;
        m_header.fill(' ', headerLength);
        m_salt.toBytes(reinterpret_cast<uint8_t*>(m_header.data()));
+       TRACE2("salt: %08lx %s\n", m_salt.m_int, hexBytes(m_header.constData(), 8).constData());
        if (markerLength > 0){
                m_buffer.resize(0);
                m_contentRandom.nextString(markerLength, markerLength, m_buffer);
                memcpy(m_header.data() + sizeof(int64_t) + reservedLength,
                           m_buffer, markerLength);
+               TRACE1("marker: %s\n", m_buffer.constData());
        }
        char* trg;
        if (info.length() > 0){
-               int offset = sizeof(int64_t) + markerLength;
+               int offset = sizeof(int64_t) + reservedLength + markerLength;
                trg = reinterpret_cast<char*>(m_header.data() + offset);
                memcpy(trg, info, min(m_header.length() - offset, info.length()));
        }
        if (encryptedFrom < m_header.length()){
                randomReset();
                uint8_t* start = reinterpret_cast<uint8_t*>(m_header.data() + encryptedFrom);
+               TRACE1("info before encoding: %s\n", hexBytes(start, 16).constData());
                m_contentRandom.codec(start, start, m_header.length() - encryptedFrom);
+               TRACE1("info after encoding: %s\n", hexBytes(start, 16).constData());
        }
-       TRACE_IT(("random: %016lx marker: %s\n", m_salt.m_int,
-                          m_buffer.constData()));
-       IF_TRACE(m_contentRandom.dump());
+       TRACE1("header: %s\n", hexBytes(m_header.constData(), 16).constData());
 }
 
 /**
@@ -1196,5 +1244,15 @@ void ReByteScrambler::initHeader(int reservedLength, int markerLength,
  */
 void ReByteScrambler::randomReset()
 {
+       m_contentRandom.reset();
        m_contentRandom.modifySeed(m_salt.m_int);
 }
+
+/**
+ * Returns the internal stored header.
+ * @return     the internal header
+ */
+QByteArray& ReByteScrambler::header()
+{
+       return m_header;
+}
index 77e977769630fb180f1ca5d83a0a15364c178a4c..7fd75d113db65e8166b9a2f8e8146ae23cd5e699 100644 (file)
@@ -111,7 +111,6 @@ public:
                          target.length() - start);
        }
        const QByteArray& name() const;
-       seed_t nearTrueRandom();
        char nextChar();
        QByteArray& nextData(int minLength, int maxLength, QByteArray& buffer);
        int nextInt(int maxValue = INT_MAX, int minValue = 0);
@@ -156,7 +155,7 @@ public:
        static seed_t hash(const QByteArray& text);
        static void hash(const QByteArray& text, QByteArray& seed);
        static seed_t pseudoTrueRandom();
-
+       static seed_t nearTrueRandom();
 protected:
        QByteArray m_name;
        int m_counter;
@@ -296,6 +295,7 @@ public:
        virtual void reset();
        virtual void restoreSeed(const QByteArray& seed);
        virtual void saveSeed(QByteArray& seed) const;
+       virtual QByteArray state() const;
        virtual void textToSeed(const QByteArray& text);
 private:
        /** Stores a parameter set into a buffer.
@@ -333,6 +333,8 @@ private:
 class ReByteScrambler{
 public:
        ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger);
+       ReByteScrambler(const ReByteScrambler& source);
+       ReByteScrambler& operator =(const ReByteScrambler& source);
 public:
        ReRandomizer& contentRandom(bool doReset);
        bool initFromHeader(int reservedLength, int markerLength,
@@ -341,9 +343,10 @@ public:
        void initHeader(int reservedLength, int markerLength, int infoLength,
                        int encryptFrom, const QByteArray& info = ReStringUtils::m_empty);
        void randomReset();
+       QByteArray& header();
+
 protected:
        ReRandomizer& m_contentRandom;
-       ReKISSRandomizer m_realRandom;
        QByteArray m_contentSeed;
        QByteArray m_buffer;
        QByteArray m_header;
index 9f08a75358e0808adf52a015c0f0c9448cd89a19..d6600742149b52b10b91b35dc84ad3267c3d37d5 100644 (file)
@@ -637,7 +637,7 @@ bool ReCharSet::fillIndexOf(const char* charSet, char minChar, char maxChar,
 {
        bool rc = true;
        int length = maxChar - minChar + 1;
-       if (length != sizeIndexOf / sizeof*indexOf)
+       if (length != int(sizeIndexOf / sizeof*indexOf))
                rc = false;
        else {
                int ix = 0;
index 20356259a4f39f1e77ed172e8d8ece1042e9177d..40dd4ecc7f84797bf402641b77216cea783c4b59 100644 (file)
@@ -247,7 +247,7 @@ bool ReTest::assertEquals(const QList<QByteArray>& expected,
  */
 bool ReTest::assertEquals(const QByteArray& expected, const QByteArray& current,
        const char* file, int lineNo) {
-       return assertEquals(expected.data(), current.data(), file, lineNo);
+       return assertEquals(expected.constData(), current.constData(), file, lineNo);
 }
 
 /**
index ca87e1e191fd74fac480ab993f77b30aab755271..1c5a26fe8f64d5232ef3bace9d2fbc9ece717a21 100644 (file)
@@ -46,6 +46,7 @@
 #include <QMainWindow>
 #include <QComboBox>
 #include <QComboBox>
+#include <QProcess>
 
 typedef unsigned char uint8_t;
 #if !defined __linux__
@@ -151,6 +152,7 @@ inline int roundInt(double value) {
 #define ReUseParameter(var) (void) var
 
 #include "remodules.hpp"
+#include "base/ReProcess.hpp"
 #include "base/ReByteStorage.hpp"
 #include "base/ReCharPtrMap.hpp"
 #include "base/ReWriter.hpp"
index 31e5fd93c56e252d76ba9625431a63e091c166eb..00641ab6298cfeaef155558429b4d2b52400b664 100644 (file)
 #define IF_TRACE(statem)
 #endif
 
+QByteArray hexBytes(const void* arg, int length = 8){
+       char buffer[16+1];
+       const unsigned char* src = reinterpret_cast<const unsigned char*>(arg);
+       QByteArray rc;
+       rc.reserve(length * 2);
+       for (int ii = 0; ii < length; ii++){
+               snprintf(buffer, sizeof buffer, "%02x", src[ii]);
+               rc += buffer;
+       }
+       return rc;
+}
 #endif // RETRACEACTIVE_HPP
 
index 192e6a26c840925c3a22ade95952ccd6014289d6..69a4d1014c6f520458cc9934a49645cc60620547 100644 (file)
@@ -29,6 +29,7 @@ static void testGui() {
 }
 
 static void testBase() {
+       void testReProcess();
        void testReRandomizer();
        void testReByteStorage();
        void testReCharPtrMap();
@@ -41,12 +42,14 @@ static void testBase() {
        void testReWriter();
        void testReFile();
        void testReMatcher();
+       testReProcess();
        testReRandomizer();
        testReFileUtils();
        testReMatcher();
        testReQStringUtil();
        testReFile();
        if (s_allTest) {
+               testReProcess();
                testReRandomizer();
                testReByteStorage();
                testReCharPtrMap();
index f29cd40b6a1e9a72baecca6350f077b59faf18eb..e8ab901e2ab1492b06e6a0231c7a89eaad24f928 100644 (file)
@@ -164,18 +164,21 @@ public:
                QByteArray trg;
                QByteArray trg2;
                int markerLength = 4;
-               int metaInfoLength = 8;
+               ReByteScrambler scrambler2(scrambler);
                ReKISSRandomizer rand;
                rand.nearTrueRandom();
                QByteArray info;
                rand.nextString(10, 20, info);
-               scrambler.initHeader(0, markerLength, metaInfoLength, 0, info);
+               int infoLength = info.length();
+               scrambler.initHeader(0, markerLength, infoLength, 0, info);
                scrambler.contentRandom(true).codec(trg, src);
                QByteArray info2;
-               scrambler.initFromHeader(0, markerLength, metaInfoLength, 0, NULL, info2);
-               scrambler.contentRandom(true).codec(trg2, trg);
+               QByteArray header2 = scrambler.header();
+               info2.fill(' ', infoLength);
+               scrambler2.initFromHeader(0, markerLength, infoLength, 0, &header2, info2);
+               scrambler2.contentRandom(true).codec(trg2, trg);
                checkEqu(src, trg2);
-               checkT(info.startsWith(info2));
+               checkEqu(info, info2);
        }
        void testContentEncoding(){
                ReKISSRandomizer dataRandom;
@@ -207,7 +210,6 @@ public:
                scrambler.contentRandom(true).codec(src, trg2, 8);
                checkEqu("1234XY78", trg2.mid(0, 8));
                checkEqu(trg.mid(8), trg2.mid(8));
-
        }
        void testShuffle(){
                ReKISSRandomizer random;
@@ -428,8 +430,22 @@ public:
                         mbytes / duration);
 
        }
+       void testScrambler(){
+               ReKISSRandomizer random;
+               QByteArray info("abcd12345678abcd1234");
+               ReByteScrambler scrambler(random, &m_logger);
+               scrambler.initHeader(8, 4, 4 + 16, 8*2+4, info);
+
+               QByteArray info2;
+               ReByteScrambler scrambler2(random, &m_logger);
+               checkT(scrambler2.initFromHeader(8, 4, 4+16, 8*2+4,
+                               &scrambler.header(), info2));
+               checkEqu(info, info2);
+       }
 
        virtual void run(void) {
+               testContentEncoding();
+               testScrambler();
                testCodec();
                special();
                testReHmHash64();
@@ -438,7 +454,6 @@ public:
                testRealRandom();
                testShuffle();
                special();
-               testContentEncoding();
                testModifySeed();
                testTextToSeed();
                testWrite1m();
index 85044ea759c991e977d8595dd73a5298d6712921..66cd3e1ec9c87a627ec43e78dacb77a7d03748d7 100644 (file)
@@ -53,7 +53,9 @@ SOURCES += main.cpp \
        cuReStateStorage.cpp \
        cuReSettings.cpp \
        cuReMatcher.cpp \
-        allTests.cpp
+        allTests.cpp \
+       ../base/ReProcess.cpp \
+       cuReProcess.cpp
 
 HEADERS += \
         ../base/ReFile.hpp \
@@ -61,5 +63,6 @@ HEADERS += \
        ../gui/ReEdit.hpp \
        ../math/ReMatrix.hpp \
        ../os/reos.hpp \
-       ../math/remath.hpp
+       ../math/remath.hpp \
+       ../base/ReProcess.hpp