From 9b0fda390a83a9f4f3cd9d03dfed718701776625 Mon Sep 17 00:00:00 2001 From: hama Date: Sat, 21 Nov 2015 02:16:15 +0100 Subject: [PATCH] writing crypt dir works --- base/ReRandomizer.cpp | 30 +++++- base/ReRandomizer.hpp | 16 +++ base/ReStringUtils.hpp | 11 ++ base/retrace.hpp | 8 +- cunit/allTests.cpp | 6 +- cunit/cuReCryptFileSystem.cpp | 14 ++- cunit/cuReLexer.cpp | 58 +++++------ cunit/cunit.pro | 3 + expr/ReLexer.cpp | 190 +++++++++++++++++----------------- os/ReCryptFileSystem.cpp | 28 +++-- os/ReCryptFileSystem.hpp | 2 +- os/ReFileSystem.hpp | 1 + 12 files changed, 219 insertions(+), 148 deletions(-) diff --git a/base/ReRandomizer.cpp b/base/ReRandomizer.cpp index 2bf9595..286bddf 100644 --- a/base/ReRandomizer.cpp +++ b/base/ReRandomizer.cpp @@ -711,6 +711,32 @@ void ReSingleSeedRandomizer::textToSeed(const QByteArray& text) { setSeed(hash(text)); } +/** + * Constructor. + */ +ReNullRandomizer::ReNullRandomizer() : + ReSingleSeedRandomizer("dummy") +{ +} + +/** + * Modifies the seed. + * @param seed the modifier + */ +void ReNullRandomizer::modifySeed(int64_t seed) +{ + ReUseParameter(seed); + // nothing to do! +} + +/** + * Returns the next seed. + * @return the next seed: 0L + */ +ReRandomizer::seed_t ReNullRandomizer::nextSeed64() +{ + return 0; +} /** * @brief Constructor. */ @@ -1223,14 +1249,12 @@ void ReByteScrambler::initHeader(int reservedLength, int markerLength, m_buffer, markerLength); TRACE1("marker: %s\n", m_buffer.constData()); } - TRACE1("header-2 %s\n", hexBytes(m_header.constData(), 8).constData()); char* trg; if (info.length() > 0){ int offset = sizeof(int64_t) + reservedLength + markerLength; trg = reinterpret_cast(m_header.data() + offset); memcpy(trg, info, min(m_header.length() - offset, info.length())); } - TRACE1("header-3 %s\n", hexBytes(m_header.constData(), 8).constData()); if (encryptedFrom < m_header.length()){ randomReset(); uint8_t* start = reinterpret_cast(m_header.data() + encryptedFrom); @@ -1258,3 +1282,5 @@ QByteArray& ReByteScrambler::header() { return m_header; } + + diff --git a/base/ReRandomizer.hpp b/base/ReRandomizer.hpp index c56039c..515defb 100644 --- a/base/ReRandomizer.hpp +++ b/base/ReRandomizer.hpp @@ -184,6 +184,22 @@ protected: seed_t m_lastSetSeed; }; +/** + * A pseudo random generator returning constantly 0. + * + * This is useful for testing. + */ +class ReNullRandomizer: public ReSingleSeedRandomizer{ +public: + ReNullRandomizer(); +public: + + // ReRandomizer interface +public: + virtual void modifySeed(int64_t seed); + virtual seed_t nextSeed64(); +}; + class ReCongruentialGeneratorBase { public: ReCongruentialGeneratorBase(); diff --git a/base/ReStringUtils.hpp b/base/ReStringUtils.hpp index 26e54a7..ba2306b 100644 --- a/base/ReStringUtils.hpp +++ b/base/ReStringUtils.hpp @@ -62,6 +62,17 @@ public: static char findCsvSeparator(FILE* fp, char* buffer, size_t bufferSize); static int lengthOfUInt64(const char* text, int radix, quint64* pValue); static int lengthOfReal(const char* text, qreal* pValue); + /** Returns the integer value of a hexadecimal digit. + * @param cc the digit to convert + * @return -1: invalid digit
+ * otherwise: the integer value of the digit + */ + inline static int valueOfHexDigit(char cc){ + int rc = isdigit(cc) ? cc - '0' + : (cc = tolower(cc)) >= 'A' && cc <= 'F' ? cc - 'A' + 10 : -1; + return rc; + } + public: static const QByteArray m_empty; }; diff --git a/base/retrace.hpp b/base/retrace.hpp index 00641ab..ea3c792 100644 --- a/base/retrace.hpp +++ b/base/retrace.hpp @@ -7,8 +7,8 @@ */ -#ifndef RETRACEACTIVE_HPP -#define RETRACEACTIVE_HPP +#ifndef RETRACE_HPP +#define RETRACE_HPP #ifdef WITH_TRACE #define TRACE(format) printf(format); @@ -24,7 +24,7 @@ #define IF_TRACE(statem) #endif -QByteArray hexBytes(const void* arg, int length = 8){ +static QByteArray hexBytes(const void* arg, int length = 8){ char buffer[16+1]; const unsigned char* src = reinterpret_cast(arg); QByteArray rc; @@ -35,5 +35,5 @@ QByteArray hexBytes(const void* arg, int length = 8){ } return rc; } -#endif // RETRACEACTIVE_HPP +#endif // RETRACE_HPP diff --git a/cunit/allTests.cpp b/cunit/allTests.cpp index d126387..e0bdf0c 100644 --- a/cunit/allTests.cpp +++ b/cunit/allTests.cpp @@ -11,6 +11,7 @@ #include "../base/rebase.hpp" #include "../math/remath.hpp" #include "../net/renet.hpp" +#include "../expr/reexpr.hpp" //#include "os/reos.hpp" #include @@ -65,7 +66,6 @@ static void testMath() { } static void testExpr() { - /* extern void testReMFParser(); extern void testRplBenchmark(); extern void testReVM(); @@ -75,6 +75,8 @@ static void testExpr() { extern void testReASTree(); extern void testReVM(); + testReLexer(); + /* //testRplBenchmark(); if (s_allTest){ testReVM(); @@ -97,13 +99,13 @@ static void testOs() { testReFileSystem(); } void allTests() { + testExpr(); testOs(); testBase(); testGui(); if (s_allTest) { testBase(); testMath(); - testExpr(); testNet(); testOs(); } diff --git a/cunit/cuReCryptFileSystem.cpp b/cunit/cuReCryptFileSystem.cpp index 32dee6a..b622349 100644 --- a/cunit/cuReCryptFileSystem.cpp +++ b/cunit/cuReCryptFileSystem.cpp @@ -28,7 +28,7 @@ public: log("run"); } - void testRead(){ + void testDirRead(){ readMetaFile(); ReFileMetaDataList list; checkEqu(3, listInfos(ReIncludeExcludeMatcher::allMatcher(), list)); @@ -39,7 +39,7 @@ public: entry = list.at(2); checkEqu("tiger.in.india.mov", entry.m_node); } - void testWrite(){ + void testDirWrite(){ addFile("Homunculus.txt"); addFile("NewYork.png"); addFile("tiger.in.india.mov"); @@ -87,19 +87,17 @@ protected: m_hostFs = NULL; m_cryptFs = NULL; } - void testWriteRead(){ + void testDirWriteRead(){ MyReCryptFileSystem cryptFs1(*m_hostFs, m_contentRandom, &m_logger); - cryptFs1.testWrite(); + cryptFs1.testDirWrite(); MyReCryptFileSystem cryptFs2(*m_hostFs, m_contentRandom, &m_logger); - cryptFs2.testRead(); - - + cryptFs2.testDirRead(); } virtual void run() { init(); - testWriteRead(); + testDirWriteRead(); destroy(); } }; diff --git a/cunit/cuReLexer.cpp b/cunit/cuReLexer.cpp index 41cf43b..cdbfb93 100644 --- a/cunit/cuReLexer.cpp +++ b/cunit/cuReLexer.cpp @@ -16,15 +16,15 @@ #include "base/rebase.hpp" #include "expr/reexpr.hpp" -class TestRplLexer: public ReTest, public ReToken { +class TestReLexer: public ReTest, public ReToken { public: - TestRplLexer() : - ReTest("ReLexer"), - ReToken(TOKEN_ID) { + TestReLexer() : + ReTest("ReLexer"), + ReToken(TOKEN_ID) { } public: - void testRplToken() { + void testReToken() { // test constructor values: checkEqu(TOKEN_ID, tokenType()); checkEqu(0, m_value.m_id); @@ -53,7 +53,7 @@ public: } ReToken* checkToken(ReToken* token, RplTokenType type, int id = 0, - const char* string = NULL) { + const char* string = NULL) { checkEqu(type, token->tokenType()); if (id != 0) checkEqu(id, token->id()); @@ -100,8 +100,8 @@ public: reader.addSource("
", BLANKS1 BLANKS2); source.addReader(&reader); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_DECIMAL, ReLexer::SF_TICK, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_DECIMAL, ReLexer::SF_TICK, + ReLexer::STORE_ALL); checkToken(lex.nextToken(), TOKEN_SPACE, 0, BLANKS1); checkToken(lex.nextToken(), TOKEN_SPACE, 0, BLANKS2); } @@ -112,8 +112,8 @@ public: reader.addSource("
", blanks); source.addReader(&reader); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_TICK, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_TICK, + ReLexer::STORE_ALL); ReToken* token = checkToken(lex.nextToken(), TOKEN_NUMBER); checkEqu(321, token->asInteger()); token = checkToken(lex.nextNonSpaceToken(), TOKEN_NUMBER); @@ -142,8 +142,8 @@ public: RBRACKET }; ReLexer lex(&source, KEYWORDS, ops, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_TICK, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_TICK, + ReLexer::STORE_ALL); checkToken(lex.nextNonSpaceToken(), TOKEN_OPERATOR, SHIFT); checkToken(lex.nextNonSpaceToken(), TOKEN_OPERATOR, LT); checkToken(lex.nextNonSpaceToken(), TOKEN_OPERATOR, SHIFT2); @@ -177,19 +177,19 @@ public: COMMENT_1 }; ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, + ReLexer::STORE_ALL); checkToken(lex.nextToken(), TOKEN_COMMENT_START, COMMENT_MULTILINE, - "/**/"); + "/**/"); checkToken(lex.nextToken(), TOKEN_NUMBER); checkToken(lex.nextToken(), TOKEN_COMMENT_START, COMMENT_1, "//\n"); checkToken(lex.nextToken(), TOKEN_NUMBER); checkToken(lex.nextToken(), TOKEN_COMMENT_START, COMMENT_MULTILINE, - "/***/"); + "/***/"); checkToken(lex.nextToken(), TOKEN_NUMBER); checkToken(lex.nextToken(), TOKEN_COMMENT_START, COMMENT_1, "// wow\n"); checkToken(lex.nextToken(), TOKEN_COMMENT_START, COMMENT_MULTILINE, - "/*\n*\n*\n**/"); + "/*\n*\n*\n**/"); } void testStrings() { ReSource source; @@ -199,8 +199,8 @@ public: source.addReader(&reader); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, + ReLexer::STORE_ALL); checkToken(lex.nextToken(), TOKEN_STRING, '"', "abc\t\r\n\a\v"); checkToken(lex.nextToken(), TOKEN_STRING, '\'', "1\tZ!A\t"); } @@ -212,8 +212,8 @@ public: source.addReader(&reader); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, + ReLexer::STORE_ALL); checkToken(lex.nextToken(), TOKEN_KEYWORD, KEY_IF); checkToken(lex.nextNonSpaceToken(), TOKEN_KEYWORD, KEY_THEN); checkToken(lex.nextNonSpaceToken(), TOKEN_KEYWORD, KEY_ELSE); @@ -230,12 +230,12 @@ public: source.addReader(&reader); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, + ReLexer::STORE_ALL); checkToken(lex.nextToken(), TOKEN_ID, 0, "i"); checkToken(lex.nextNonSpaceToken(), TOKEN_ID, 0, "ifs"); checkToken(lex.nextNonSpaceToken(), TOKEN_ID, 0, - "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); + "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); } void testBasic() { @@ -244,8 +244,8 @@ public: source.addReader(&reader); reader.addSource("
", "if i>1 then i=1+2*_x9 fi"); ReLexer lex(&source, KEYWORDS, OPERATORS, "=", COMMENTS, "A-Za-z_", - "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, - ReLexer::STORE_ALL); + "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, ReLexer::SF_LIKE_C, + ReLexer::STORE_ALL); ReToken* token; checkToken(lex.nextToken(), TOKEN_KEYWORD, KEY_IF); checkToken(lex.nextToken(), TOKEN_SPACE, 0); @@ -273,7 +273,7 @@ public: }; ReLexer lex(&source, KEYWORDS, "=\n+ -\n* /", "=", COMMENTS, "A-Za-z_", "A-Za-z0-9_", ReLexer::NUMTYPE_ALL, - ReLexer::SF_LIKE_C, ReLexer::STORE_ALL); + ReLexer::SF_LIKE_C, ReLexer::STORE_ALL); checkT(lex.prioOfOp(O_ASSIGN) < lex.prioOfOp(O_PLUS)); checkEqu(lex.prioOfOp(O_PLUS), lex.prioOfOp(O_MINUS)); checkT(lex.prioOfOp(O_MINUS) < lex.prioOfOp(O_TIMES)); @@ -290,10 +290,10 @@ public: testOperators(); testNumeric(); testSpace(); - testRplToken(); + testReToken(); } }; void testReLexer() { - TestRplLexer test; + TestReLexer test; test.run(); } diff --git a/cunit/cunit.pro b/cunit/cunit.pro index 66cd3e1..824a079 100644 --- a/cunit/cunit.pro +++ b/cunit/cunit.pro @@ -16,6 +16,7 @@ TEMPLATE = app INCLUDEPATH = .. SOURCES += main.cpp \ + cuReLexer.cpp \ cuReFileSystem.cpp \ cuReCryptFileSystem.cpp \ cuReRandomizer.cpp \ @@ -25,6 +26,8 @@ SOURCES += main.cpp \ cuReFileUtils.cpp \ cuReByteStorage.cpp \ cuReException.cpp \ + ../expr/ReSource.cpp \ + ../expr/ReLexer.cpp \ ../base/ReByteStorage.cpp \ ../base/ReCharPtrMap.cpp \ ../base/ReConfig.cpp \ diff --git a/expr/ReLexer.cpp b/expr/ReLexer.cpp index cb82611..5dd5a8b 100644 --- a/expr/ReLexer.cpp +++ b/expr/ReLexer.cpp @@ -34,8 +34,8 @@ * @param ... the values for the placeholders in the format. */ ReLexException::ReLexException(const ReSourcePosition& position, - const char* format, ...) : - ReException("") { + const char* format, ...) : + ReException("") { char buffer[64000]; m_message = position.toString().toUtf8(); va_list ap; @@ -55,9 +55,9 @@ ReLexException::ReLexException(const ReSourcePosition& position, * @param type token type */ ReToken::ReToken(RplTokenType type) : - m_tokenType(type), - m_string(), - m_printableString() + m_tokenType(type), + m_string(), + m_printableString() // m_value { memset(&m_value, 0, sizeof m_value); @@ -74,10 +74,10 @@ ReToken::~ReToken() { * @param source source to copy */ ReToken::ReToken(const ReToken& source) : - m_tokenType(source.m_tokenType), - m_string(source.m_string), - m_printableString(source.m_printableString), - m_value(source.m_value) { + m_tokenType(source.m_tokenType), + m_string(source.m_string), + m_printableString(source.m_printableString), + m_value(source.m_value) { } /** * @brief Assignment operator. @@ -186,7 +186,7 @@ bool ReToken::isTokenType(RplTokenType expected) const { */ bool ReToken::isOperator(int expected, int alternative) const { return m_tokenType == TOKEN_OPERATOR - && (m_value.m_id == expected || m_value.m_id == alternative); + && (m_value.m_id == expected || m_value.m_id == alternative); } /** @@ -201,7 +201,7 @@ bool ReToken::isOperator(int expected, int alternative) const { bool ReToken::isKeyword(int expected, int alternative) const { return m_tokenType == TOKEN_KEYWORD - && (m_value.m_id == expected || m_value.m_id == alternative); + && (m_value.m_id == expected || m_value.m_id == alternative); } /** @@ -222,7 +222,7 @@ void ReToken::clear() { */ bool ReToken::isCapitalizedId() const { bool rc = m_tokenType == TOKEN_ID && isupper(m_string.at(0)) - && (m_string.length() == 1 || islower(m_string.at(1))); + && (m_string.length() == 1 || islower(m_string.at(1))); return rc; } @@ -246,7 +246,7 @@ QByteArray ReToken::asUtf8() const { break; case TOKEN_STRING: qsnprintf(buffer, sizeof buffer, "'%.*s'", int(sizeof buffer) - 1, - m_printableString.constData()); + m_printableString.constData()); break; case TOKEN_NUMBER: qsnprintf(buffer, sizeof buffer, "%lld", m_value.m_integer); @@ -260,7 +260,7 @@ QByteArray ReToken::asUtf8() const { break; case TOKEN_ID: qsnprintf(buffer, sizeof buffer, "'%.*s'", int(sizeof buffer) - 1, - m_string.constData()); + m_string.constData()); break; case TOKEN_COMMENT_REST_OF_LINE: case TOKEN_COMMENT_START: @@ -333,8 +333,8 @@ const char* ReToken::nameOfType(RplTokenType type) { */ static void itemsToVector(const char* items, ReLexer::StringList& vector, - int firstCharFlag, int secondCharFlag, int thirdCharFlag, int restCharFlag, - int charInfo[]) { + int firstCharFlag, int secondCharFlag, int thirdCharFlag, int restCharFlag, + int charInfo[]) { QByteArray array2(items); QList < QByteArray > list = array2.split(' '); QList::iterator it; @@ -370,7 +370,7 @@ static void itemsToVector(const char* items, ReLexer::StringList& vector, } static void charClassToCharInfo(const char* charClass, int flag, - int charInfo[]) { + int charInfo[]) { for (int ix = 0; charClass[ix] != '\0'; ix++) { unsigned char cc = (unsigned char) charClass[ix]; if (cc < 128) @@ -381,7 +381,7 @@ static void charClassToCharInfo(const char* charClass, int flag, charInfo['-'] |= flag; else if (cc >= ubound) throw new ReException("wrong character class range: %c-%c (%s)", - cc, ubound, charClass); + cc, ubound, charClass); else { for (int ii = cc + 1; ii <= ubound; ii++) { charInfo[ii] |= flag; @@ -418,45 +418,45 @@ static void charClassToCharInfo(const char* charClass, int flag, * S_ORG_STRINGS | S_COMMENTS | S_BLANKS */ ReLexer::ReLexer(ReSource* source, const char* keywords, const char* operators, - const char* rightAssociatives, const char* comments, - const char* firstCharsId, const char* restCharsId, int numericTypes, - int stringFeatures, int storageFlags) : - m_source(source), - m_keywords(), - m_operators(), - m_commentStarts(), - m_commentEnds(), - //m_charInfo() - m_idFirstRare(), - m_idRestRare(), - m_numericTypes(numericTypes), - m_idRest2(), - m_currentToken(&m_token1), - m_waitingToken(NULL), - m_waitingToken2(NULL), - m_token1(TOKEN_UNDEF), - m_token2(TOKEN_UNDEF), - m_currentPosition(NULL), - m_waitingPosition1(NULL), - m_waitingPosition2(NULL), - m_maxTokenLength(64), - m_input(), - m_currentCol(0), - m_hasMoreInput(false), - m_stringFeatures(stringFeatures), - m_storageFlags(storageFlags), - // m_prioOfOp - // m_assocOfOp + const char* rightAssociatives, const char* comments, + const char* firstCharsId, const char* restCharsId, int numericTypes, + int stringFeatures, int storageFlags) : + m_source(source), + m_keywords(), + m_operators(), + m_commentStarts(), + m_commentEnds(), + //m_charInfo() + m_idFirstRare(), + m_idRestRare(), + m_numericTypes(numericTypes), + m_idRest2(), + m_currentToken(&m_token1), + m_waitingToken(NULL), + m_waitingToken2(NULL), + m_token1(TOKEN_UNDEF), + m_token2(TOKEN_UNDEF), + m_currentPosition(NULL), + m_waitingPosition1(NULL), + m_waitingPosition2(NULL), + m_maxTokenLength(64), + m_input(), + m_currentCol(0), + m_hasMoreInput(false), + m_stringFeatures(stringFeatures), + m_storageFlags(storageFlags), + // m_prioOfOp + // m_assocOfOp #if defined (RPL_LEXER_TRACE) - m_trace(true), + m_trace(true), #endif - m_opNames() { + m_opNames() { memset(m_prioOfOp, 0, sizeof m_prioOfOp); memset(m_assocOfOp, 0, sizeof m_assocOfOp); memset(m_charInfo, 0, sizeof m_charInfo); itemsToVector(keywords, m_keywords, CC_FIRST_KEYWORD, CC_2nd_KEYWORD, - CC_3rd_KEYWORD, CC_REST_KEYWORD, m_charInfo); + CC_3rd_KEYWORD, CC_REST_KEYWORD, m_charInfo); prepareOperators(operators, rightAssociatives); charClassToCharInfo(firstCharsId, CC_FIRST_ID, m_charInfo); charClassToCharInfo(restCharsId, CC_REST_ID, m_charInfo); @@ -494,20 +494,20 @@ int countBlanks(const char* start, const char* end) { * Lower position means lower priority */ void ReLexer::prepareOperators(const char* operators, - const char* rightAssociatives) { + const char* rightAssociatives) { QByteArray op2(operators); QByteArray rightAssociatives2(" "); rightAssociatives2 += rightAssociatives; op2.replace("\n", " "); itemsToVector(op2.constData(), m_operators, CC_FIRST_OP, CC_2nd_OP, - CC_3rd_OP, CC_REST_OP, m_charInfo); + CC_3rd_OP, CC_REST_OP, m_charInfo); // m_operators is now sorted: // test whether the successor of 1 char operators is starting with this char: // if not this operator will be marked with CC_OP_1_ONLY: for (int ix = 0; ix < m_operators.size() - 1; ix++) { // the entries of m_operators end with ' ' and id: if (m_operators.at(ix).size() == 1 + 2 - && m_operators.at(ix).at(0) != m_operators.at(ix + 1).at(0)) { + && m_operators.at(ix).at(0) != m_operators.at(ix + 1).at(0)) { int cc = (char) m_operators[ix].at(0); m_charInfo[cc] |= CC_OP_1_ONLY; } @@ -549,8 +549,8 @@ void ReLexer::initializeComments(const char* comments) { int ix = comments2.indexOf(" "); if (ix >= 0) throw ReException( - "more than one blank between comment pair(s): col %d %s", - ix + 1, comments + ix); + "more than one blank between comment pair(s): col %d %s", + ix + 1, comments + ix); // the index of m_commentEnds is the position number: we need a dummy entry: m_commentEnds.append(""); @@ -569,8 +569,8 @@ void ReLexer::initializeComments(const char* comments) { if (ix % 2 != 0) throw ReException("not only pairs in the comment list"); itemsToVector(starters, m_commentStarts, CC_FIRST_COMMENT_START, - CC_2nd_COMMENT_START, CC_3rd_COMMENT_START, CC_REST_COMMENT_START, - m_charInfo); + CC_2nd_COMMENT_START, CC_3rd_COMMENT_START, CC_REST_COMMENT_START, + m_charInfo); } } /** @@ -630,13 +630,13 @@ bool ReLexer::fillInput() { if (m_hasMoreInput) { if (m_input.size() < m_maxTokenLength) { m_source->currentReader()->fillBuffer(m_maxTokenLength, m_input, - m_hasMoreInput); + m_hasMoreInput); } } while (m_input.size() == 0 && m_source->currentReader() != NULL) { if (m_source->currentReader()->nextLine(m_maxTokenLength, m_input, - m_hasMoreInput)) { + m_hasMoreInput)) { m_currentCol = 0; } } @@ -655,7 +655,7 @@ bool ReLexer::fillInput() { * otherwise: the token */ ReToken* ReLexer::findTokenWithId(RplTokenType tokenType, int flag2, - StringList& names) { + StringList& names) { int length = 1; int inputLength = m_input.size(); int cc; @@ -684,7 +684,7 @@ ReToken* ReLexer::findTokenWithId(RplTokenType tokenType, int flag2, } ReToken* rc = NULL; if (!(tokenType == TOKEN_KEYWORD && length < inputLength && (cc = - m_input[length]) < CHAR_INFO_SIZE && (m_charInfo[cc] & CC_REST_ID))) { + m_input[length]) < CHAR_INFO_SIZE && (m_charInfo[cc] & CC_REST_ID))) { int id; // the length could be too long: the CC_2nd_.. flag could be ambigous while ((id = findInVector(length, names)) <= 0) { @@ -698,7 +698,7 @@ ReToken* ReLexer::findTokenWithId(RplTokenType tokenType, int flag2, rc->m_tokenType = tokenType; rc->m_value.m_id = id; if (tokenType == TOKEN_COMMENT_START - && (m_storageFlags & STORE_COMMENT) != 0) + && (m_storageFlags & STORE_COMMENT) != 0) rc->m_string.append(m_input.mid(0, length)); m_input.remove(0, length); m_currentCol += length; @@ -718,23 +718,23 @@ ReToken* ReLexer::scanNumber() { int length; quint64 value = 0; if ((cc = m_input[0]) == '0' && inputLength > 1 - && (m_numericTypes & NUMTYPE_HEXADECIMAL) - && (m_input[1] == 'x' || m_input[1] == 'X')) { - length = ReStringUtil::lengthOfUInt64(m_input.constData() + 2, 16, - &value); + && (m_numericTypes & NUMTYPE_HEXADECIMAL) + && (m_input[1] == 'x' || m_input[1] == 'X')) { + length = ReStringUtils::lengthOfUInt64(m_input.constData() + 2, 16, + &value); if (length > 0) length += 2; else throw ReException("invalid hexadecimal number: no digit behind 'x"); } else if (cc == '0' && (m_numericTypes & NUMTYPE_OCTAL) - && inputLength > 1) { + && inputLength > 1) { length = 1; while (length < inputLength) { if ((cc = m_input[length]) >= '0' && cc <= '7') value = value * 8 + cc - '0'; else if (cc >= '8' && cc <= '9') throw ReLexException(*m_currentPosition, - "invalid octal digit: %c", cc); + "invalid octal digit: %c", cc); else break; length++; @@ -753,10 +753,10 @@ ReToken* ReLexer::scanNumber() { m_currentToken->m_value.m_integer = value; m_currentToken->m_tokenType = TOKEN_NUMBER; if (length + 1 < inputLength - && ((cc = m_input[length]) == '.' || toupper(cc) == 'E')) { + && ((cc = m_input[length]) == '.' || toupper(cc) == 'E')) { qreal realValue; - int realLength = ReStringUtil::lengthOfReal(m_input.constData(), - &realValue); + int realLength = ReStringUtils::lengthOfReal(m_input.constData(), + &realValue); if (realLength > length) { m_currentToken->m_tokenType = TOKEN_REAL; m_currentToken->m_value.m_real = realValue; @@ -784,27 +784,27 @@ ReToken*ReLexer::scanString() { while (length < inputLength && (cc = m_input[length]) != delim) { length++; if (cc != '\\' - || (m_stringFeatures - & (SF_C_ESCAPING | SF_C_HEX_CHARS | SF_C_SPECIAL)) == 0) { + || (m_stringFeatures + & (SF_C_ESCAPING | SF_C_HEX_CHARS | SF_C_SPECIAL)) == 0) { m_currentToken->m_string.append(QChar(cc)); } else { if (length >= inputLength) throw ReLexException(*m_currentPosition, - "backslash without following character"); + "backslash without following character"); cc = m_input[length++]; if ((m_stringFeatures & SF_C_HEX_CHARS) && toupper(cc) == 'X') { if (length >= inputLength) throw ReLexException(*m_currentPosition, - "missing hexadecimal digit behind \\x"); + "missing hexadecimal digit behind \\x"); cc = m_input[length++]; - int hexVal = ReQStringUtil::valueOfHexDigit(cc); + int hexVal = ReStringUtils::valueOfHexDigit(cc); if (hexVal < 0) throw ReLexException(*m_currentPosition, - "not a hexadecimal digit behind \\x: %lc", - QChar(cc)); + "not a hexadecimal digit behind \\x: %lc", + QChar(cc)); if (length < inputLength) { cc = m_input[length]; - int nibble = ReQStringUtil::valueOfHexDigit(cc); + int nibble = ReStringUtils::valueOfHexDigit(cc); if (nibble >= 0) { length++; hexVal = hexVal * 16 + nibble; @@ -844,7 +844,7 @@ ReToken*ReLexer::scanString() { length++; } if ((m_stringFeatures & SF_DOUBLE_DELIM) && length < inputLength - && m_input[length] == (char) delim) { + && m_input[length] == (char) delim) { m_currentToken->m_printableString.append(delim); length++; again = true; @@ -880,7 +880,7 @@ void ReLexer::scanComment() { m_input.clear(); if (!fillInput()) throw ReLexException(*m_currentPosition, - "comment end not found"); + "comment end not found"); } length = ix + commentEnd.size(); if (m_storageFlags & STORE_COMMENT) @@ -961,18 +961,18 @@ ReToken* ReLexer::nextToken() { } else if (isdigit(cc)) { rc = scanNumber(); } else if ((cc == '"' && (m_stringFeatures & SF_QUOTE) != 0) - || (cc == '\'' && (m_stringFeatures & SF_TICK) != 0)) { + || (cc == '\'' && (m_stringFeatures & SF_TICK) != 0)) { rc = scanString(); } else { if (cc >= CHAR_INFO_SIZE) throw ReLexException(*m_currentPosition, - "no lexical symbol can start with this char: %lc", - cc); + "no lexical symbol can start with this char: %lc", + cc); else { if (rc == NULL - && (m_charInfo[cc] & CC_FIRST_COMMENT_START)) { + && (m_charInfo[cc] & CC_FIRST_COMMENT_START)) { rc = findTokenWithId(TOKEN_COMMENT_START, - CC_2nd_COMMENT_START, m_commentStarts); + CC_2nd_COMMENT_START, m_commentStarts); if (rc != NULL) scanComment(); //waitingPosition = m_currentPosition; @@ -981,7 +981,7 @@ ReToken* ReLexer::nextToken() { if (rc == NULL && (m_charInfo[cc] & CC_FIRST_OP)) { if ((m_charInfo[cc] & CC_OP_1_ONLY) == 0) { rc = findTokenWithId(TOKEN_OPERATOR, CC_2nd_OP, - m_operators); + m_operators); } else { rc = m_currentToken; rc->m_tokenType = TOKEN_OPERATOR; @@ -992,13 +992,13 @@ ReToken* ReLexer::nextToken() { } if (rc == NULL && (m_charInfo[cc] & CC_FIRST_KEYWORD)) { rc = findTokenWithId(TOKEN_KEYWORD, CC_2nd_KEYWORD, - m_keywords); + m_keywords); } if (rc == NULL && (m_charInfo[cc] & CC_FIRST_ID)) { int length = 1; while (length < m_input.size() && (cc = - m_input[length]) < CHAR_INFO_SIZE - && (m_charInfo[cc] & CC_REST_ID) != 0) + m_input[length]) < CHAR_INFO_SIZE + && (m_charInfo[cc] & CC_REST_ID) != 0) length++; rc = m_currentToken; rc->m_tokenType = TOKEN_ID; @@ -1019,7 +1019,7 @@ ReToken* ReLexer::nextToken() { } else { QByteArray symbol = m_input.mid(0, qMin(20, m_input.size() - 1)); throw ReLexException(*m_currentPosition, - "unknown lexical symbol: %s", symbol.constData()); + "unknown lexical symbol: %s", symbol.constData()); } } #if defined (RPL_LEXER_TRACE) @@ -1119,8 +1119,8 @@ ReToken* ReLexer::nextNonSpaceToken() { do { rc = nextToken(); } while ((type = m_currentToken->tokenType()) == TOKEN_SPACE - || type == TOKEN_COMMENT_START || type == TOKEN_COMMENT_END - || type == TOKEN_COMMENT_REST_OF_LINE); + || type == TOKEN_COMMENT_START || type == TOKEN_COMMENT_END + || type == TOKEN_COMMENT_REST_OF_LINE); return rc; } @@ -1154,8 +1154,8 @@ ReSource* ReLexer::source() { */ int ReLexer::prioOfOp(int op) const { int rc = - op > 0 && (unsigned) op < sizeof m_prioOfOp / sizeof m_prioOfOp[0] ? - m_prioOfOp[op] : 0; + op > 0 && (unsigned) op < sizeof m_prioOfOp / sizeof m_prioOfOp[0] ? + m_prioOfOp[op] : 0; return rc; } diff --git a/os/ReCryptFileSystem.cpp b/os/ReCryptFileSystem.cpp index eaaa9b9..99e4096 100644 --- a/os/ReCryptFileSystem.cpp +++ b/os/ReCryptFileSystem.cpp @@ -9,7 +9,8 @@ #include "base/rebase.hpp" #include "os/reos.hpp" - +//#define WITH_TRACE +#include "base/retrace.hpp" /** * @file * @@ -134,6 +135,8 @@ int ReCryptFileSystem::listInfos(const ReIncludeExcludeMatcher& matcher, ReFileMetaDataList::const_iterator it; bool withDirs = (options & LO_DIRS) != 0; bool withFiles = (options & LO_FILES) != 0; + if (! withDirs && ! withFiles) + withDirs = withFiles = true; bool filterDirs = (options & LO_NAME_FILTER_FOR_DIRS); list.clear(); for (it = m_list.cbegin(); it != m_list.cend(); ++it){ @@ -453,10 +456,12 @@ bool ReCryptDirectory::readMetaFile() rc = initFromHeader(0, MARKER_LENGTH, META_INFO_LENGTH, 0, &header, info); if (rc){ const MetaInfo_t* meta = reinterpret_cast(info.constData()); + TRACE2("count: %d size: %d\n", meta->m_countFiles, meta->m_size); if (meta->m_countFiles > 0){ m_fileBuffer.resize(m_blockSize); m_entryBuffer.resize(0); int sumLength = 0; + randomReset(); while ( (nRead = fread(m_fileBuffer.data(), 1, m_blockSize, fp)) > 0){ sumLength += nRead; @@ -464,7 +469,7 @@ bool ReCryptDirectory::readMetaFile() m_fileBuffer.resize(nRead); m_contentRandom.codec(m_fileBuffer); m_entryBuffer.append(m_fileBuffer); - splitBlock(sumLength < meta->m_size, + splitBlock(sumLength >= meta->m_size, m_entryBuffer); } if (sumLength != meta->m_size){ @@ -487,8 +492,8 @@ bool ReCryptDirectory::readMetaFile() */ bool ReCryptDirectory::writeMetaFile() { + TRACE("writeMetaFile:\n"); bool rc = true; - QByteArray header; QByteArray meta; meta.resize(sizeof(MetaInfo_t)); MetaInfo_t* meta2 = reinterpret_cast(meta.data()); @@ -499,6 +504,7 @@ bool ReCryptDirectory::writeMetaFile() int length = it->m_node.toUtf8().length(); meta2->m_size += length + (length < 256 ? 0 : 1); } + TRACE2("count: %d size: %d\n", meta2->m_countFiles, meta2->m_size); initHeader(0, MARKER_LENGTH, META_INFO_LENGTH, 0, meta); QByteArray node; for (it = m_list.cbegin(); it != m_list.cend(); ++it){ @@ -513,8 +519,11 @@ bool ReCryptDirectory::writeMetaFile() m_logger->logv(LOG_ERROR, LOC_WRITE_META_1, "cannot write (%d): %s", errno, fnMetaFile.constData()); } else { - m_fileBuffer.append(header); + m_fileBuffer.resize(0); + m_fileBuffer.append(m_header); + int offset = m_header.length(); int ixList = 0; + randomReset(); while (ixList < m_list.length()){ const ReFileMetaData& file = m_list.at(ixList++); FileEntry_t trg; @@ -529,19 +538,21 @@ bool ReCryptDirectory::writeMetaFile() int length = node.length(); trg.m_nodeLength = length < 256 ? length : 0; m_fileBuffer.append(reinterpret_cast(&trg), sizeof trg); + TRACE2("%2d: %s", ixList, hexBytes(&trg, sizeof trg).constData()); m_fileBuffer.append(node); + TRACE2(" length: %d %s\n", length, node.constData()); if (length >= 256) m_fileBuffer.append('\0'); int blockLength = m_fileBuffer.length(); bool lastBlock = ixList >= m_list.length(); - if (lastBlock ||blockLength >= m_blockSize - 512){ + if (lastBlock || blockLength >= m_blockSize - 512){ if (! lastBlock && blockLength % sizeof(int64_t) != 0){ int newLength = blockLength - blockLength % sizeof(int64_t); m_smallBuffer = m_fileBuffer.mid(newLength); m_fileBuffer.resize(newLength); } - int offset = ixList <= 1 ? META_DIR_HEADER_LENGTH : 0; m_contentRandom.codec(m_fileBuffer, m_fileBuffer, offset); + offset = 0; int nWritten = fwrite(m_fileBuffer.constData(), 1, m_fileBuffer.length(), fp); if (nWritten != m_fileBuffer.length()){ @@ -574,11 +585,13 @@ void ReCryptDirectory::splitBlock(bool isLast, QByteArray& block){ const FileEntry_t* src = reinterpret_cast (block.constData()); ReFileMetaData file; - int position = 0; + TRACE("splitBlock:\n"); + IF_TRACE(int ix = 0); const char* srcPtr = reinterpret_cast(block.constData()); const char* endPtr = srcPtr + block.length() - (isLast ? 0 : MAX_ENTRY_SIZE); while (srcPtr < endPtr){ const FileEntry_t* src = reinterpret_cast(srcPtr); + TRACE2("%2d: %s", ++ix, hexBytes(&src, sizeof src).constData()); file.m_created.setMSecsSinceEpoch(src->m_created); file.m_modified.setMSecsSinceEpoch(src->m_modified); file.m_owner = src->m_owner; @@ -591,6 +604,7 @@ void ReCryptDirectory::splitBlock(bool isLast, QByteArray& block){ srcPtr += sizeof(FileEntry_t); int nodeLength = src->m_nodeLength != 0 ? src->m_nodeLength : strlen(srcPtr); QByteArray node(srcPtr, nodeLength); + TRACE2(" Length: %d %s\n", nodeLength, node.constData()); file.m_node = node; m_list.append(file); srcPtr += nodeLength + (src->m_nodeLength != 0 ? 0 : 1); diff --git a/os/ReCryptFileSystem.hpp b/os/ReCryptFileSystem.hpp index 6a92c21..45ed45d 100644 --- a/os/ReCryptFileSystem.hpp +++ b/os/ReCryptFileSystem.hpp @@ -138,7 +138,7 @@ public: virtual void close(); virtual bool exists(const QString& node, ReFileMetaData* metaInfo) const; virtual int listInfos(const ReIncludeExcludeMatcher& matcher, - ReFileMetaDataList& list, ListOptions options = LO_UNDEF); + ReFileMetaDataList& list, ListOptions options = LO_ALL); virtual ErrorCode makeDir(const QString& node); virtual ErrorCode read(const ReFileMetaData& source, int64_t offset, int size, QByteArray& buffer); diff --git a/os/ReFileSystem.hpp b/os/ReFileSystem.hpp index 8c6dd00..122b113 100644 --- a/os/ReFileSystem.hpp +++ b/os/ReFileSystem.hpp @@ -52,6 +52,7 @@ public: LO_UNDEF = 0, LO_FILES = 1, LO_DIRS = 2, + LO_ALL = 3, LO_NAME_FILTER_FOR_DIRS = 4, }; -- 2.39.5