--- /dev/null
+/*
+ * 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;
+}
+
*/
#include "base/rebase.hpp"
-//#define WITH_TRACE
+#define WITH_TRACE
#include "retrace.hpp"
enum {
LOC_READ_1 = LOC_FIRST_OF(LOC_RANDOMIZER), // 12201
* 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,
*/
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()).
void ReKISSRandomizer::reset() {
m_params = m_startParams;
m_counter = 0;
+ TRACE1("reset() %s\n", state().constData());
}
/**
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.
*
*/
ReByteScrambler::ReByteScrambler(ReRandomizer& contentRandom, ReLogger* logger) :
m_contentRandom(contentRandom),
- m_realRandom(),
m_contentSeed(),
m_buffer(),
m_header(),
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;
}
/**
ReRandomizer& ReByteScrambler::contentRandom(bool doReset)
{
if (doReset){
- m_contentRandom.reset();
- m_contentRandom.modifySeed(m_salt.m_int);
+ randomReset();
}
return m_contentRandom;
}
} 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",
rc = false;
}
}
- TRACE1("info: Length: %d\n", length);
info.resize(0);
if (infoLength > 0)
info = header->mid(sizeof(int64_t) + reservedLength + 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());
}
/**
*/
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;
+}
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);
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;
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.
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,
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;
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;
scrambler.contentRandom(true).codec(src, trg2, 8);
checkEqu("1234XY78", trg2.mid(0, 8));
checkEqu(trg.mid(8), trg2.mid(8));
-
}
void testShuffle(){
ReKISSRandomizer random;
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();
testRealRandom();
testShuffle();
special();
- testContentEncoding();
testModifySeed();
testTextToSeed();
testWrite1m();