From: hama Date: Tue, 17 Nov 2015 23:19:18 +0000 (+0100) Subject: dayly work X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=db713ca89fe352571147663de398779c275cc5cf;p=reqt dayly work --- diff --git a/base/ReProcess.cpp b/base/ReProcess.cpp new file mode 100644 index 0000000..c9ccd1c --- /dev/null +++ b/base/ReProcess.cpp @@ -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 index 0000000..c549d71 --- /dev/null +++ b/base/ReProcess.hpp @@ -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 diff --git a/base/ReRandomizer.cpp b/base/ReRandomizer.cpp index 0a1503b..8ef7b7a 100644 --- a/base/ReRandomizer.cpp +++ b/base/ReRandomizer.cpp @@ -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(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(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( - 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(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(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(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; +} diff --git a/base/ReRandomizer.hpp b/base/ReRandomizer.hpp index 77e9777..7fd75d1 100644 --- a/base/ReRandomizer.hpp +++ b/base/ReRandomizer.hpp @@ -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; diff --git a/base/ReStringUtils.cpp b/base/ReStringUtils.cpp index 9f08a75..d660074 100644 --- a/base/ReStringUtils.cpp +++ b/base/ReStringUtils.cpp @@ -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; diff --git a/base/ReTest.cpp b/base/ReTest.cpp index 2035625..40dd4ec 100644 --- a/base/ReTest.cpp +++ b/base/ReTest.cpp @@ -247,7 +247,7 @@ bool ReTest::assertEquals(const QList& 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); } /** diff --git a/base/rebase.hpp b/base/rebase.hpp index ca87e1e..1c5a26f 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -46,6 +46,7 @@ #include #include #include +#include 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" diff --git a/base/retrace.hpp b/base/retrace.hpp index 31e5fd9..00641ab 100644 --- a/base/retrace.hpp +++ b/base/retrace.hpp @@ -24,5 +24,16 @@ #define IF_TRACE(statem) #endif +QByteArray hexBytes(const void* arg, int length = 8){ + char buffer[16+1]; + const unsigned char* src = reinterpret_cast(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 diff --git a/cunit/allTests.cpp b/cunit/allTests.cpp index 192e6a2..69a4d10 100644 --- a/cunit/allTests.cpp +++ b/cunit/allTests.cpp @@ -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(); diff --git a/cunit/cuReRandomizer.cpp b/cunit/cuReRandomizer.cpp index f29cd40..e8ab901 100644 --- a/cunit/cuReRandomizer.cpp +++ b/cunit/cuReRandomizer.cpp @@ -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(); diff --git a/cunit/cunit.pro b/cunit/cunit.pro index 85044ea..66cd3e1 100644 --- a/cunit/cunit.pro +++ b/cunit/cunit.pro @@ -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