From 05cd1f137fe1fc5ddea75c3fee480589dd55a75b Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 2 Aug 2015 23:54:18 +0200 Subject: [PATCH] deleteTree * deleteTree() counts objects * dirtool sync logs deleted objects * clock() measures instead of time() --- base/ReByteArray.cpp | 7 +- base/ReByteArray.hpp | 29 +- base/ReDirectory.cpp | 75 +-- base/ReDirectory.hpp | 38 +- base/ReSerializable.cpp | 4 +- base/ReStringList.hpp | 4 +- base/ReTestUnit.cpp | 15 +- base/ReTestUnit.hpp | 3 +- base/ReThread.cpp | 2 +- base/rebase.hpp | 4 +- cunit/cuReDirTools.cpp | 46 +- cunit/cuReDirectory.cpp | 7 +- cunit/cuReFileUtils.cpp | 11 +- cunit/cuReSerializable.cpp | 2 +- cunit/cuReTCP.cpp | 16 +- cunit/testall.cpp | 1 - dirtool.cpp | 23 +- math/ReMD5.cpp | 1002 ++++++++++++++++++------------------ net/ReTCP.cpp | 3 +- net/ReUdpConnection.cpp | 4 +- net/ReUdpConnection.hpp | 3 +- os/ReDirTools.cpp | 70 ++- os/ReDirTools.hpp | 5 +- os/ReRemoteDir.cpp | 4 +- os/ReTraverser.cpp | 5 +- 25 files changed, 713 insertions(+), 670 deletions(-) diff --git a/base/ReByteArray.cpp b/base/ReByteArray.cpp index 590eb2f..da29849 100644 --- a/base/ReByteArray.cpp +++ b/base/ReByteArray.cpp @@ -690,8 +690,8 @@ int ReByteArray::indexOf(const Byte* toFind, size_t toFindLength, int start, * @return truethe instance is a prefix of the source * and the length is at least minLength bytes */ -bool ReByteArray::isPrefixOf(const char* source, size_t length, - bool ignoreCase, int minLength) { +bool ReByteArray::isPrefixOf(const char* source, size_t length, bool ignoreCase, + int minLength) { if (length == (size_t) -1) length = strlen(source); bool rc = length >= m_length && m_length <= length; @@ -918,8 +918,7 @@ bool ReByteArray::startsWith(const Byte* head, size_t headLength, * @param prefix NULL or a prefix of the output * @return buffer.str() (for chaining) */ -const char* ReByteArray::status(ReByteArray& buffer, - const char* prefix) const { +const char* ReByteArray::status(ReByteArray& buffer, const char* prefix) const { if (prefix != NULL) buffer.append(prefix); buffer.append("capacity:").appendInt(m_capacity).append(" length: ") diff --git a/base/ReByteArray.hpp b/base/ReByteArray.hpp index 40310d0..e5b4134 100644 --- a/base/ReByteArray.hpp +++ b/base/ReByteArray.hpp @@ -165,7 +165,7 @@ public: /**@brief Sets the length to 0. * @return the instance (for chaining) */ - ReByteArray& clear(){ + ReByteArray& clear() { m_length = 0; m_buffer[0] = '\0'; return *this; @@ -173,7 +173,7 @@ public: /** @brief Returns a immutable C string. * @return a pointer to the buffer content */ - const char* constData() const{ + const char* constData() const { return reinterpret_cast(m_buffer); } /**@brief Returns the minimum allocation unit. @@ -193,14 +193,14 @@ public: * @param part array to test * @return true: part is a part of the content */ - inline bool contains(const char* part) const{ + inline bool contains(const char* part) const { return indexOf(part, -1) >= 0; } /** @brief Tests whether a byte array is part of the instance's content. * @param part array to test * @return true: part is a part of the content */ - inline bool contains(const ReByteArray& part) const{ + inline bool contains(const ReByteArray& part) const { return indexOf(part.constData(), part.length()) >= 0; } int count(const char* toCount, size_t lengthToCount = -1); @@ -218,8 +218,8 @@ public: * @return true: tail is found at the end */ inline - bool endsWith(char tail, bool ignoreCase = false) const{ - return m_length > 0 && tolower(tail) == tolower(m_buffer[m_length-1]); + bool endsWith(char tail, bool ignoreCase = false) const { + return m_length > 0 && tolower(tail) == tolower(m_buffer[m_length - 1]); } /** @brief Test whether a given byte array is the end of the instance's content. * @param tail the array to test @@ -227,16 +227,16 @@ public: * @return true: tail is found at the end */ inline - bool endsWith(const ReByteArray& tail, bool ignoreCase = false) const{ - return endsWith((const Byte*) tail.str(), (size_t) tail.length(), ignoreCase); + bool endsWith(const ReByteArray& tail, bool ignoreCase = false) const { + return endsWith((const Byte*) tail.str(), (size_t) tail.length(), + ignoreCase); } void ensureSize(size_t size); /** After the call the last character is the given. * @param aChar the character which will be the last * @return *this (for chaining) */ - inline - ReByteArray& ensureLastChar(char aChar) { + inline ReByteArray& ensureLastChar(char aChar) { if (lastChar() != aChar) appendChar(aChar); return *this; @@ -248,8 +248,8 @@ public: * @return true: the buffer's contents are equal */ inline - bool equals(const char* data, size_t length = (size_t) -1, - bool ignoreCase = false) const { + bool equals(const char* data, size_t length = (size_t) -1, bool ignoreCase = + false) const { if (length == (size_t) -1) length = strlen(data); bool rc = length == m_length @@ -344,10 +344,9 @@ public: * @param start only characters behind this index will be replaced * @return *this (for chaining) */ - inline - ReByteArray& replace(char toFind, char replacement, int start = 0){ + inline ReByteArray& replace(char toFind, char replacement, int start = 0) { return replaceAll(reinterpret_cast(&toFind), 1, - reinterpret_cast(&replacement), 1, start); + reinterpret_cast(&replacement), 1, start); } ReByteArray& replaceAll(const Byte* toFind, size_t toFindLength, const Byte* replacement, size_t replacementLength, int start = 0); diff --git a/base/ReDirectory.cpp b/base/ReDirectory.cpp index 67912df..13c27ec 100644 --- a/base/ReDirectory.cpp +++ b/base/ReDirectory.cpp @@ -40,21 +40,21 @@ ReDirectory::ReDirectory() : * @param path The path of the directory. */ ReDirectory::ReDirectory(const char* path) : - m_path(), - m_pattern(), - m_stat(), - m_currentFull(), - #if defined __linux__ - m_dir(NULL), - m_entry(NULL), - m_regExpr(), - m_regExprFlags(0), - m_regExprIsInitialized(false), - m_isRegExpr(false), - #elif defined __WIN32__ - m_handle(INVALID_HANDLE_VALUE), - m_data(), - #endif + m_path(), + m_pattern(), + m_stat(), + m_currentFull(), +#if defined __linux__ + m_dir(NULL), + m_entry(NULL), + m_regExpr(), + m_regExprFlags(0), + m_regExprIsInitialized(false), + m_isRegExpr(false), +#elif defined __WIN32__ + m_handle(INVALID_HANDLE_VALUE), + m_data(), +#endif m_valid(false) { #if defined __linux__ memset(&m_regExpr, 0, sizeof m_regExpr); @@ -105,8 +105,8 @@ const char* ReDirectory::currentNode() { * * @param the status info given by lstat() */ -struct stat& ReDirectory::currentLStat(){ - if (m_stat.st_gid == (mode_t)-1 && filetimeIsUndefined(m_stat.st_mtim)){ +struct stat& ReDirectory::currentLStat() { + if (m_stat.st_gid == (mode_t) -1 && filetimeIsUndefined(m_stat.st_mtim)) { lstat(currentFull().str(), &m_stat); } return m_stat; @@ -117,7 +117,7 @@ struct stat& ReDirectory::currentLStat(){ * * @return true: the current file is a directory */ -bool ReDirectory::currentIsDir(){ +bool ReDirectory::currentIsDir() { #if defined __linux__ return S_ISDIR(currentLStat().st_mode); #elif defined __WIN32__ @@ -126,11 +126,11 @@ bool ReDirectory::currentIsDir(){ } /** - * Returns whether the current file is a directory. + * Returns the modification date of the current entry. * - * @return true: the current file is a directory + * @return the modification date of the current entry */ -ReFileTime_t ReDirectory::currentModified(){ +ReFileTime_t ReDirectory::currentModified() { #if defined __linux__ return currentLStat().st_mtim; #elif defined __WIN32__ @@ -138,6 +138,19 @@ ReFileTime_t ReDirectory::currentModified(){ #endif } +/** + * Returns the size of the current entry. + * + * @return the filesize of the current entry + */ +int64_t ReDirectory::currentSize() { +#if defined __linux__ + return currentLStat().st_size; +#elif defined __WIN32__ + return +#endif +} + /** * Deletes a directory tree. * @@ -145,7 +158,8 @@ ReFileTime_t ReDirectory::currentModified(){ * @param deleteBaseToo true: the directory itself will be deleted * false: only the files and subdirs in the dir will be deleted */ -void ReDirectory::deleteTree(const char* base, bool deleteBaseToo) { +void ReDirectory::deleteTree(const char* base, bool deleteBaseToo, + int* deletedDirs, int* deletedFiles, int64_t* deletedSize) { if (true) { ReDirectory dir(base); if (dir.findFirst(ALL_FILES, false)) { @@ -156,16 +170,17 @@ void ReDirectory::deleteTree(const char* base, bool deleteBaseToo) { && (node[1] == '\0' || (node[1] == '.' && node[2] == '\0')))) && stat(dir.currentFull().str(), &info) == 0) { if (S_ISDIR(info.st_mode)) - deleteTree(dir.currentFull().str(), true); - else - _unlink(dir.currentFull().str()); + deleteTree(dir.currentFull().str(), true, deletedDirs, + deletedFiles, deletedSize); + else if (unlink(dir.currentFull().str()) + == 0&& deletedFiles != NULL) + ++*deletedFiles; } } while (dir.findNext()); } dir.close(); - if (deleteBaseToo) { - _rmdir(base); - } + if (deleteBaseToo && _rmdir(base) && deletedDirs != NULL) + ++*deletedDirs; } } @@ -178,11 +193,11 @@ void ReDirectory::deleteTree(const char* base, bool deleteBaseToo) { * @param accessed OUT: the modification date. May be NULL */ bool ReDirectory::filetime(const char* filename, ReFileTime_t* modified, - ReFileTime_t* created, ReFileTime_t* accessed){ + ReFileTime_t* created, ReFileTime_t* accessed) { #if defined __linux__ struct stat info; bool rc = stat(filename, &info) == 0; - if (rc){ + if (rc) { if (modified != NULL) ; } diff --git a/base/ReDirectory.hpp b/base/ReDirectory.hpp index 6a19272..3f07d66 100644 --- a/base/ReDirectory.hpp +++ b/base/ReDirectory.hpp @@ -23,7 +23,7 @@ public: /** @brief Returns the filename of the current file with path. * @return the full filename with path */ - const ReByteArray& currentFull(){ + const ReByteArray& currentFull() { if (m_currentFull.empty()) m_currentFull.set(m_path).append(currentNode()); return m_currentFull; @@ -32,6 +32,7 @@ public: bool currentIsDir(); struct stat& currentLStat(); ReFileTime_t currentModified(); + int64_t currentSize(); bool findFirst(const char* pattern, bool isRegExpr); bool findNext(); bool findYoungest(ReByteArray& filename); @@ -41,9 +42,11 @@ public: void setDir(const char* path); void setRegExprFlags(int flags); public: - static void deleteTree(const char* base, bool deleteBaseToo = false); + static void deleteTree(const char* base, bool deleteBaseToo = false, + int* deletedDirs = NULL, int* deletedFiles = NULL, + int64_t* deletedSize = NULL); static bool filetime(const char* filename, ReFileTime_t* modified, - ReFileTime_t* created = NULL, ReFileTime_t* accessed = NULL); + ReFileTime_t* created = NULL, ReFileTime_t* accessed = NULL); public: static const char* ALL_FILES; private: @@ -94,12 +97,12 @@ inline void setFiletimeUndef(ReFileTime_t& time) { * @param op2 second operand * @return true: op1 == op2 */ -inline bool operator ==(const ReFileTime_t& op1, const ReFileTime_t& op2){ +inline bool operator ==(const ReFileTime_t& op1, const ReFileTime_t& op2) { #if defined __linux__ return op1.tv_sec == op2.tv_sec && op1.tv_nsec == op2.tv_nsec; #else return time1.dwHighDateTime == time2.dwHighDateTime - && time1.dwLowDateTime == time2.dwLowDateTime; + && time1.dwLowDateTime == time2.dwLowDateTime; #endif } /** Returns whether a filetime is equal than another. @@ -107,21 +110,22 @@ inline bool operator ==(const ReFileTime_t& op1, const ReFileTime_t& op2){ * @param op2 second operand * @return true: op1 == op2 */ -inline bool operator !=(const ReFileTime_t& op1, const ReFileTime_t& op2){ - return ! (op1 == op2); +inline bool operator !=(const ReFileTime_t& op1, const ReFileTime_t& op2) { + return !(op1 == op2); } /** Returns whether a filetime is greater (younger) than another. * @param op1 first operand * @param op2 second operand * @return true: op1 > op2 */ -inline bool operator >(const ReFileTime_t& op1, const ReFileTime_t& op2){ +inline bool operator >(const ReFileTime_t& op1, const ReFileTime_t& op2) { #if defined __linux__ - return op1.tv_sec > op2.tv_sec || op1.tv_sec == op2.tv_sec && op1.tv_nsec > op2.tv_nsec; + return op1.tv_sec > op2.tv_sec + || op1.tv_sec == op2.tv_sec && op1.tv_nsec > op2.tv_nsec; #else return time1.dwHighDateTime > time2.dwHighDateTime - || (time1.dwHighDateTime == time2.dwHighDateTime - && time1.dwLowDateTime > time2.dwLowDateTime); + || (time1.dwHighDateTime == time2.dwHighDateTime + && time1.dwLowDateTime > time2.dwLowDateTime); #endif } /** Returns whether a filetime is greater (younger) or equal than another. @@ -129,9 +133,10 @@ inline bool operator >(const ReFileTime_t& op1, const ReFileTime_t& op2){ * @param op2 second operand * @return true: op1 > op2 */ -inline bool operator >=(const ReFileTime_t& op1, const ReFileTime_t& op2){ +inline bool operator >=(const ReFileTime_t& op1, const ReFileTime_t& op2) { #if defined __linux__ - return op1.tv_sec > op2.tv_sec || op1.tv_sec == op2.tv_sec && op1.tv_nsec >= op2.tv_nsec; + return op1.tv_sec > op2.tv_sec + || op1.tv_sec == op2.tv_sec && op1.tv_nsec >= op2.tv_nsec; #else return time1.dwHighDateTime > time2.dwHighDateTime || (time1.dwHighDateTime == time2.dwHighDateTime @@ -143,17 +148,16 @@ inline bool operator >=(const ReFileTime_t& op1, const ReFileTime_t& op2){ * @param op2 second operand * @return true: op1 > op2 */ -inline bool operator <(const ReFileTime_t& op1, const ReFileTime_t& op2){ - return ! (op1 >= op2); +inline bool operator <(const ReFileTime_t& op1, const ReFileTime_t& op2) { + return !(op1 >= op2); } /** Returns whether a filetime is lower (older) or equal than another. * @param op1 first operand * @param op2 second operand * @return true: op1 > op2 */ -inline bool operator <=(const ReFileTime_t& op1, const ReFileTime_t& op2){ +inline bool operator <=(const ReFileTime_t& op1, const ReFileTime_t& op2) { return op1 == op2 || op1 < op2; } - #endif /* REDIRECTORY_H_ */ diff --git a/base/ReSerializable.cpp b/base/ReSerializable.cpp index 59404c1..b1a4485 100644 --- a/base/ReSerializable.cpp +++ b/base/ReSerializable.cpp @@ -89,7 +89,7 @@ void ReSerializable::packFileTime(ReByteArray& sequence, sequence.appendBits64(int64_t(value)); #elif defined __WIN32__ //#error "missing impl" - assert(false); + assert(false); #endif } /** @@ -122,6 +122,6 @@ void ReSerializable::unpackFileTime(const uint8_t*& sequence, size_t& length, } #elif defined __WIN32__ //#error "missing impl" - assert(false); + assert(false); #endif } diff --git a/base/ReStringList.hpp b/base/ReStringList.hpp index 84d3da6..d6ebc45 100644 --- a/base/ReStringList.hpp +++ b/base/ReStringList.hpp @@ -48,8 +48,8 @@ public: Index indexOf(const char* toFind, bool ignoreCase = false, Index start = 0) const; void insert(Index index, const char* source, Tag tag = 0); - ReByteArray& join(const char* separator, ReByteArray& result, - bool append = false) const; + ReByteArray& join(const char* separator, ReByteArray& result, bool append = + false) const; Index nextStartingWith(Index index, const char* prefix, bool ignoreCase = false); bool readFromFile(const char* filename, bool cutNewline = true); diff --git a/base/ReTestUnit.cpp b/base/ReTestUnit.cpp index e4a8763..315fd3b 100644 --- a/base/ReTestUnit.cpp +++ b/base/ReTestUnit.cpp @@ -199,7 +199,6 @@ void ReTestUnit::assertEqualFiles(const char* name1, const char* name2, } - /** @brief Checks whether a file exists. If not this will be logged. * * @param name the filename @@ -385,7 +384,8 @@ const char* ReTestUnit::testDir() { * @param content expected content: each line can contain one or more '*'s * used as wildcards (any count of any character) */ -void ReTestUnit::testFileContentWithWildcards(ReByteArray& filename, const char* content) { +void ReTestUnit::testFileContentWithWildcards(ReByteArray& filename, + const char* content) { ReStringList current; current.readFromFile(filename.str()); ReStringList expected; @@ -401,16 +401,17 @@ void ReTestUnit::testFileContentWithWildcards(ReByteArray& filename, const char* if (!line.startsWith(col1)) checkEqu(col1, line); int count = cols.count(); - if (count > 1){ + if (count > 1) { const char* col2; - if (count > 2){ + if (count > 2) { int startIx = cols.strLengthOf(0) + 1; int lastIndex = line.length() - cols.strLengthOf(count - 1); - for (int ix = 1; ix < count - 1; ix++){ + for (int ix = 1; ix < count - 1; ix++) { col2 = cols.strOf(ix); - int found = line.indexOf(col2, cols.strLengthOf(ix), startIx); - if (found < 0 || found >= lastIndex){ + int found = line.indexOf(col2, cols.strLengthOf(ix), + startIx); + if (found < 0 || found >= lastIndex) { checkEqu(col2, line.str() + startIx); break; } diff --git a/base/ReTestUnit.hpp b/base/ReTestUnit.hpp index 63c4c54..1025770 100644 --- a/base/ReTestUnit.hpp +++ b/base/ReTestUnit.hpp @@ -48,7 +48,8 @@ public: virtual bool logF(bool isError, const char* format, ...); int milliSecSince(int64_t start); const char* testDir(); - void testFileContentWithWildcards(ReByteArray& filename, const char* content); + void testFileContentWithWildcards(ReByteArray& filename, + const char* content); int64_t timer(); private: void createTestDir(); diff --git a/base/ReThread.cpp b/base/ReThread.cpp index 1e8333c..c5ec5bf 100644 --- a/base/ReThread.cpp +++ b/base/ReThread.cpp @@ -153,7 +153,7 @@ ReThreadPool::ReThreadPool(int maxThreads, ReLogger* logger) : m_maxThreads(maxThreads), m_maxKillTimeSec(3), m_mutexThreads(LC_MUTEX_THREADS), - m_shouldStop(false){ + m_shouldStop(false) { m_threads = new ReThread*[maxThreads]; memset(m_threads, 0, maxThreads * sizeof *m_threads); } diff --git a/base/rebase.hpp b/base/rebase.hpp index 48f6ea3..49a933d 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -85,8 +85,8 @@ typedef FILETIME ReFileTime_t; inline int getLastOSError() { return GetLastError(); } -inline void bzero(void* ptr, size_t size){ - memset(ptr, 0, size); +inline void bzero(void* ptr, size_t size) { + memset(ptr, 0, size); } #endif // avoids warning like "parameter not used" diff --git a/cunit/cuReDirTools.cpp b/cunit/cuReDirTools.cpp index ddfadd6..b6c0299 100644 --- a/cunit/cuReDirTools.cpp +++ b/cunit/cuReDirTools.cpp @@ -220,7 +220,7 @@ private: ReByteArray buffer; buffer.ensureSize(5); ReDirSync::copyFile(src.str(), false, NULL, trg.str(), buffer, - ReLogger::globalLogger()); + ReLogger::globalLogger()); checkFileEqu(src.str(), trg.str()); #else log(false, "testCopyFile not implemented"); @@ -460,14 +460,15 @@ private: target.append("synctest", -1); _mkdir(target.str(), ALLPERMS); ReDirectory::deleteTree(target.str(), false); - const char* argv[] = { "dt", "sync", "-P;*;-cache", "-p;*.txt", "-v3", optOutput.str(), source - .str(), target.str() }; + const char* argv[] = { "dt", "sync", "-P;*;-cache", "-p;*.txt", "-v3", + optOutput.str(), source.str(), target.str() }; tools.main(8, (char**) argv); target.appendChar(OS_SEPARATOR_CHAR); - for(const char** pFile = s_allFiles; *pFile != NULL; pFile++){ + for (const char** pFile = s_allFiles; *pFile != NULL; pFile++) { ReByteArray fullTarget(target); fullTarget.append(*pFile); - if (fullTarget.endsWith(".txt") || ! fullTarget.contains("cache") >= 0){ + if (fullTarget.endsWith(".txt") + || !fullTarget.contains("cache") >= 0) { checkT(stat(fullTarget.str(), &info)); } else { checkF(stat(fullTarget.str(), &info)); @@ -475,11 +476,12 @@ private: } static const char* content = "+dir1_2/dir1_2_1/x1.txt\n" - "+dir1_2/dir1_2_1/x2.txt\n" - "+cache/cache.txt\n" - "-/tmp/retestunit/synctest/dir1/cache/cache.txt\n" - "=== copied: * sec 3 file(s) 0.000074 MByte (* MB/sec).\n" - "=== tree: 5 dir(s) 3 file(s) 0.000074 MByte"; + "+dir1_2/dir1_2_1/x2.txt\n" + "+cache/cache.txt\n" + "-/tmp/retestunit/synctest/dir1/cache/cache.txt\n" + "=== copied: * sec 3 file(s) 0.000074 MByte (* MB/sec).\n" + "=== tree: 5 dir(s) 3 file(s) 0.000074 MByte\n" + "=== deleted: 1 dir(s) 1 file(s) 0.000020 MByte"; ReByteArray content2(content); content2.replace('/', OS_SEPARATOR_CHAR); testFileContentWithWildcards(nameCurrent, content2.str()); @@ -498,18 +500,18 @@ private: tools.main(6, (char**) argv); } /* - " 1.txt", // - "*dir1", // - "*dir2", // - "*dir1/cache", // - "*dir1/dir1_1", // - "*dir1/dir1_2", // - "*dir1/dir1_2/dir1_2_1", // - " dir1/dir1_2/dir1_2_1/x1.txt", // - " dir1/dir1_2/dir1_2_1/x2.txt", // - " dir2/2.x", // - " dir1/cache/cache.txt", // - NULL }; + " 1.txt", // + "*dir1", // + "*dir2", // + "*dir1/cache", // + "*dir1/dir1_1", // + "*dir1/dir1_2", // + "*dir1/dir1_2/dir1_2_1", // + " dir1/dir1_2/dir1_2_1/x1.txt", // + " dir1/dir1_2/dir1_2_1/x2.txt", // + " dir2/2.x", // + " dir1/cache/cache.txt", // + NULL }; */ }; diff --git a/cunit/cuReDirectory.cpp b/cunit/cuReDirectory.cpp index 2c0dbf4..c547a9a 100644 --- a/cunit/cuReDirectory.cpp +++ b/cunit/cuReDirectory.cpp @@ -21,7 +21,8 @@ private: testFileTime(); testBase(); } - void checkOrder(ReFileTime_t& time1, ReFileTime_t& time2, ReFileTime_t& time3){ + void checkOrder(ReFileTime_t& time1, ReFileTime_t& time2, + ReFileTime_t& time3) { checkT(time1 < time2); checkF(time1 == time2); checkT(time1 != time2); @@ -35,7 +36,7 @@ private: checkT(time2 >= time3); } - void testFileTime(){ + void testFileTime() { // time1 < time2 == time3 ReFileTime_t time1; ReFileTime_t time2; @@ -69,7 +70,7 @@ private: setFiletimeUndef(time1); checkT(filetimeIsUndefined(time1)); } - void testBase(){ + void testBase() { ReByteArray dir; dir.set(testDir(), -1); ReByteArray file1 = dir; diff --git a/cunit/cuReFileUtils.cpp b/cunit/cuReFileUtils.cpp index 68f5092..fcdc7c8 100644 --- a/cunit/cuReFileUtils.cpp +++ b/cunit/cuReFileUtils.cpp @@ -24,7 +24,7 @@ private: testSetFiles(); testTempFile(); } - void testReadWrite(){ + void testReadWrite() { ReByteArray name = ReFileUtils::tempFile("refiletest.$$$"); const char* content = "123\nabc\n"; ReFileUtils::writeString(name.str(), content); @@ -36,7 +36,7 @@ private: ReFileUtils::readString("/./././.", buffer); checkEqu("", buffer); } - void testSetFiles(){ + void testSetFiles() { ReByteArray name = ReFileUtils::tempFile("setfile_test.dat"); struct stat info; unlink(name.str()); @@ -55,13 +55,13 @@ private: ReFileUtils::timeToFiletime(time2, time3); ReFileTime_t time4; int diff = 86400 + 2 * 3600 + 3 * 60 + 2; - ReFileUtils::timeToFiletime(time2 + diff, time4); + ReFileUtils::timeToFiletime(time2 + diff, time4); ReFileUtils::setTimes(name.str(), time3, &time4, NULL); checkEqu(0, stat(name.str(), &info)); checkT(time2 == info.st_mtim.tv_sec); checkT(time2 + diff == info.st_atim.tv_sec); } - void testTempFile(){ + void testTempFile() { const char* subdir = "refileutiltest"; const char* node = "test1.$$.txt"; ReByteArray dir = ReFileUtils::tempDir(subdir); @@ -81,6 +81,3 @@ void testReFileUtils(void) { TestReFileUtils unit; } - - - diff --git a/cunit/cuReSerializable.cpp b/cunit/cuReSerializable.cpp index 3fa4567..a9e51e6 100644 --- a/cunit/cuReSerializable.cpp +++ b/cunit/cuReSerializable.cpp @@ -57,7 +57,7 @@ public: packString255(buffer, m_string255); packString64k(buffer, m_string64k); packString4t(buffer, m_string4t); - return buffer; + return buffer; } const char* toString(ReByteArray& buffer) { buffer.setLength(0).append("id: ").appendInt(m_serialId); diff --git a/cunit/cuReTCP.cpp b/cunit/cuReTCP.cpp index d246f15..92e3aff 100644 --- a/cunit/cuReTCP.cpp +++ b/cunit/cuReTCP.cpp @@ -71,7 +71,7 @@ public: data.appendInt(size); request = "filldata"; } - time_t start2 = time(NULL); + time_t start2 = clock(); int64_t start = ReBaseUtils::timer(); int count = 100; for (int ii = 0; ii < count; ii++) { @@ -79,16 +79,16 @@ public: client.receive(command, answer); } int64_t duration = ReBaseUtils::milliSecSince(start); - int duration2 = int(time(NULL) - start2); - if (duration2 == 0) - duration2 = 1; + double duration2 = double(clock() - start2) / CLOCKS_PER_SEC; + if (duration2 == 0.0) + duration2 = 0.001; char msg[256]; int miByte = count * (size / (1024 * 1024)); _snprintf(msg, sizeof msg, - "%s: %d MiByte in %s/%d sec: %.3f (%.3f) MiByte/sec", direction, - miByte, ReByteArray("").appendMilliSec((int) duration).str(), - duration2, miByte * 1000.0 / (double) duration, - miByte / (double) duration2); + "%s: %d MiByte in %s/%.3f sec: %.3f (%.3f) MiByte/sec", + direction, miByte, + ReByteArray("").appendMilliSec((int) duration).str(), duration2, + miByte * 1000.0 / (double) duration, miByte / duration2); logger->say(LOG_INFO | CAT_LIB, location, msg); } } diff --git a/cunit/testall.cpp b/cunit/testall.cpp index b400777..2bd063d 100644 --- a/cunit/testall.cpp +++ b/cunit/testall.cpp @@ -93,7 +93,6 @@ void testAll() { try { testOs(); testBase(); - testNet(); if (s_testAll) { testString(); testMath(); diff --git a/dirtool.cpp b/dirtool.cpp index 807f131..253827d 100644 --- a/dirtool.cpp +++ b/dirtool.cpp @@ -8,15 +8,14 @@ * You also can use this license: http://www.wtfpl.net * The latest sources: https://github.com/republib */ -// dirtool.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung. -// -#include "base/rebase.hpp" -#include "os/reos.hpp" - -int _tmain(int argc, _TCHAR* argv[]) -{ - ReDirTools tool; - tool.main(argc, argv); - return 0; -} - +// dirtool.cpp : Definiert den Einstiegspunkt f�r die Konsolenanwendung. +// +#include "base/rebase.hpp" +#include "os/reos.hpp" + +int _tmain(int argc, _TCHAR* argv[]) { + ReDirTools tool; + tool.main(argc, argv); + return 0; +} + diff --git a/math/ReMD5.cpp b/math/ReMD5.cpp index a98c7f4..105da87 100644 --- a/math/ReMD5.cpp +++ b/math/ReMD5.cpp @@ -8,513 +8,513 @@ * You also can use this license: http://www.wtfpl.net * The latest sources: https://github.com/republib */ - -#include "base/rebase.hpp" -#include "math/remath.hpp" - -const int ReMD5::m_s[RE_DIGEST_CHUNK_SIZE] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, - 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, - 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, - 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 }; -// for x in [1..64] : int(2**32 * sin(x)) -const uint32_t ReMD5::m_K[RE_DIGEST_CHUNK_SIZE] = { 0xd76aa478, 0xe8c7b756, - 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, - 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, - 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, - 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, - 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, - 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, - 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, - 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, - 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, - 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, - 0x2ad7d2bb, 0xeb86d391 }; - -/** - * Constructor. - * - * @param digest the buffer for the binary checksum - * @param digestSize the length of digest - * @param waiting a buffer for a chunk. Must have space for at least 2*chunksize bytes - * NULL: an intrinsic buffer will be used - * @param chunkSize the length of one full input block - */ -ReDigest::ReDigest(uint8_t* digest, size_t digestSize, uint8_t* waiting, - size_t chunkSize) : - m_digest(digest), - m_digestSize(digestSize), - // m_waitingBuffer[RE_DIGEST_CHUNK_SIZE]; - m_waiting(waiting != NULL ? waiting : m_waitingBuffer), - m_lengthWaiting(0), - m_chunkSize(chunkSize), - m_length(0), - m_finalized(false), - m_salt(0) { -} -/** - * Destructor. - */ -ReDigest::~ReDigest() { -} -/** - * Returns the binary digest value. - * - * @return the binary digest (16 byte array) - */ -const uint8_t* ReDigest::digest() { - if (!m_finalized) { - finalize(); - } - return m_digest; -} - -/** - * Returns the binary digest value. - * - * @return the binary digest (16 byte array) - */ -const ReByteArray& ReDigest::hexDigest() { - if (m_hexDigest.empty()) { - digest(); - for (size_t ix = 0; ix < m_digestSize; ix++) { - m_hexDigest.appendInt(m_digest[ix], "%02x"); - } - } - return m_hexDigest; -} - -/** - * Processes a 64 byte block. - * - * @param block a block which should be added to the digest - * @param blockLength the length of block - */ -void ReDigest::update(const uint8_t* block, int blockLength) { - if (blockLength == -1) - blockLength = strlen((const char*) block); - // process the "waiting" input (incomplete chunk): - m_length += blockLength; - if (m_lengthWaiting > 0) { - int rest = m_chunkSize - m_lengthWaiting; - if (rest > blockLength) - rest = blockLength; - memcpy(m_waiting + m_lengthWaiting, block, rest); - blockLength -= rest; - block += rest; - m_lengthWaiting += rest; - // Is the chunk complete? - if (m_lengthWaiting == m_chunkSize) { - processChunk(m_waiting); - m_lengthWaiting = 0; - } - } - // process full 512 bit chunks (64 byte blocks): - for (int ix = blockLength / m_chunkSize; ix > 0; ix--) { - processChunk(block); - block += m_chunkSize; - } - blockLength %= m_chunkSize; - if (blockLength != 0) { - assert(m_lengthWaiting == 0); - memcpy(m_waiting, block, blockLength); - m_lengthWaiting = blockLength; - } -} - -/** - * Sets the salt of the checksum algorithm. - * - * Important: set the salt before you make the first update()! - * - * The salt makes that the checksum of the same input is (extremly) different - * to another salt. - * - * @param salt the salt to set - */ -void ReDigest::setSalt(uint64_t salt) { - m_salt = salt; -} -/** - * Constructor. - */ -ReMD5::ReMD5() : - ReDigest(m_digest, sizeof m_digest), - m_a0(0x67452301), - m_b0(0xefcdab89), - m_c0(0x98badcfe), - m_d0(0x10325476) { -} - -/** - * Destructor. - */ -ReMD5::~ReMD5() { -} - -/** - * Finalizes the digest. - * - * Handles the rest block (< 64 byte) and append a special tail: a "1" bit - * and the length of the total input length (in bits). - * - * @param block the rest input (incomplete chunk) - * @param blockLength the length of the block: 0..63 - */ -void ReMD5::finalize() { - uint8_t* block = m_waiting; - int blockLength = m_lengthWaiting; - // append "1" bit to message - // Notice: the input bytes are considered as bits strings, - // where the first bit is the most significant bit of the byte. - block[blockLength++] = 0x80; - //Pre-processing: padding with zeros - //append "0" bit until message length in bits ≡ 448 (mod 512) - // fill the rest of the chunk with '\0'. - // the last 8 bytes is set to the length in bits (length in bytes * 8) - int restLength = (m_length + 1) % RE_DIGEST_CHUNK_SIZE; - // 0 -> 56, 1 -> 55, 2 -> 54, ... 55 -> 1, 56 -> 0, - // 57 -> 63, 58 -> 62, ... 63 -> 57 - int zeros = restLength <= 56 ? 56 - restLength : 120 - restLength; - memset(block + blockLength, 0, zeros); - blockLength += zeros; - //append original length in bits mod (2 pow 64) to message - uint64_t lengthBits = 8LL * m_length + m_salt; -#if defined __LITTLE_ENDIAN__ - memcpy(block + blockLength, &lengthBits, 8); - blockLength += 8; -#else - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; - lengthBits >>= 8; - block[blockLength++] = lengthBits; -#endif - processChunk(block); - if (blockLength > RE_DIGEST_CHUNK_SIZE) - processChunk(block + RE_DIGEST_CHUNK_SIZE); -#if defined __LITTLE_ENDIAN__ - memcpy(m_digest, &m_a0, 4); - memcpy(m_digest + 4, &m_b0, 4); - memcpy(m_digest + 8, &m_c0, 4); - memcpy(m_digest + 12, &m_d0, 4); -#else + +#include "base/rebase.hpp" +#include "math/remath.hpp" + +const int ReMD5::m_s[RE_DIGEST_CHUNK_SIZE] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, + 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, + 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, + 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 }; +// for x in [1..64] : int(2**32 * sin(x)) +const uint32_t ReMD5::m_K[RE_DIGEST_CHUNK_SIZE] = { 0xd76aa478, 0xe8c7b756, + 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, + 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, + 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, + 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, + 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, + 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, + 0x2ad7d2bb, 0xeb86d391 }; + +/** + * Constructor. + * + * @param digest the buffer for the binary checksum + * @param digestSize the length of digest + * @param waiting a buffer for a chunk. Must have space for at least 2*chunksize bytes + * NULL: an intrinsic buffer will be used + * @param chunkSize the length of one full input block + */ +ReDigest::ReDigest(uint8_t* digest, size_t digestSize, uint8_t* waiting, + size_t chunkSize) : + m_digest(digest), + m_digestSize(digestSize), + // m_waitingBuffer[RE_DIGEST_CHUNK_SIZE]; + m_waiting(waiting != NULL ? waiting : m_waitingBuffer), + m_lengthWaiting(0), + m_chunkSize(chunkSize), + m_length(0), + m_finalized(false), + m_salt(0) { +} +/** + * Destructor. + */ +ReDigest::~ReDigest() { +} +/** + * Returns the binary digest value. + * + * @return the binary digest (16 byte array) + */ +const uint8_t* ReDigest::digest() { + if (!m_finalized) { + finalize(); + } + return m_digest; +} + +/** + * Returns the binary digest value. + * + * @return the binary digest (16 byte array) + */ +const ReByteArray& ReDigest::hexDigest() { + if (m_hexDigest.empty()) { + digest(); + for (size_t ix = 0; ix < m_digestSize; ix++) { + m_hexDigest.appendInt(m_digest[ix], "%02x"); + } + } + return m_hexDigest; +} + +/** + * Processes a 64 byte block. + * + * @param block a block which should be added to the digest + * @param blockLength the length of block + */ +void ReDigest::update(const uint8_t* block, int blockLength) { + if (blockLength == -1) + blockLength = strlen((const char*) block); + // process the "waiting" input (incomplete chunk): + m_length += blockLength; + if (m_lengthWaiting > 0) { + int rest = m_chunkSize - m_lengthWaiting; + if (rest > blockLength) + rest = blockLength; + memcpy(m_waiting + m_lengthWaiting, block, rest); + blockLength -= rest; + block += rest; + m_lengthWaiting += rest; + // Is the chunk complete? + if (m_lengthWaiting == m_chunkSize) { + processChunk(m_waiting); + m_lengthWaiting = 0; + } + } + // process full 512 bit chunks (64 byte blocks): + for (int ix = blockLength / m_chunkSize; ix > 0; ix--) { + processChunk(block); + block += m_chunkSize; + } + blockLength %= m_chunkSize; + if (blockLength != 0) { + assert(m_lengthWaiting == 0); + memcpy(m_waiting, block, blockLength); + m_lengthWaiting = blockLength; + } +} + +/** + * Sets the salt of the checksum algorithm. + * + * Important: set the salt before you make the first update()! + * + * The salt makes that the checksum of the same input is (extremly) different + * to another salt. + * + * @param salt the salt to set + */ +void ReDigest::setSalt(uint64_t salt) { + m_salt = salt; +} +/** + * Constructor. + */ +ReMD5::ReMD5() : + ReDigest(m_digest, sizeof m_digest), + m_a0(0x67452301), + m_b0(0xefcdab89), + m_c0(0x98badcfe), + m_d0(0x10325476) { +} + +/** + * Destructor. + */ +ReMD5::~ReMD5() { +} + +/** + * Finalizes the digest. + * + * Handles the rest block (< 64 byte) and append a special tail: a "1" bit + * and the length of the total input length (in bits). + * + * @param block the rest input (incomplete chunk) + * @param blockLength the length of the block: 0..63 + */ +void ReMD5::finalize() { + uint8_t* block = m_waiting; + int blockLength = m_lengthWaiting; + // append "1" bit to message + // Notice: the input bytes are considered as bits strings, + // where the first bit is the most significant bit of the byte. + block[blockLength++] = 0x80; + //Pre-processing: padding with zeros + //append "0" bit until message length in bits ≡ 448 (mod 512) + // fill the rest of the chunk with '\0'. + // the last 8 bytes is set to the length in bits (length in bytes * 8) + int restLength = (m_length + 1) % RE_DIGEST_CHUNK_SIZE; + // 0 -> 56, 1 -> 55, 2 -> 54, ... 55 -> 1, 56 -> 0, + // 57 -> 63, 58 -> 62, ... 63 -> 57 + int zeros = restLength <= 56 ? 56 - restLength : 120 - restLength; + memset(block + blockLength, 0, zeros); + blockLength += zeros; + //append original length in bits mod (2 pow 64) to message + uint64_t lengthBits = 8LL * m_length + m_salt; +#if defined __LITTLE_ENDIAN__ + memcpy(block + blockLength, &lengthBits, 8); + blockLength += 8; +#else + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; + lengthBits >>= 8; + block[blockLength++] = lengthBits; +#endif + processChunk(block); + if (blockLength > RE_DIGEST_CHUNK_SIZE) + processChunk(block + RE_DIGEST_CHUNK_SIZE); +#if defined __LITTLE_ENDIAN__ + memcpy(m_digest, &m_a0, 4); + memcpy(m_digest + 4, &m_b0, 4); + memcpy(m_digest + 8, &m_c0, 4); + memcpy(m_digest + 12, &m_d0, 4); +#else #define oneWord(word, ix) m_digest[ix] = word; word >>= 8; \ m_digest[ix + 1] = word; word >>= 8; m_digest[ix + 2] = word; word >>= 8; \ - m_digest[ix + 3] = word - oneWord(m_a0, 0); - oneWord(m_b0, 4); - oneWord(m_c0, 8); - oneWord(m_d0, 12); -#endif -} - -/** - * Processes a 512 bit block ("chunk"). - * - * This method is a direct programming of the algorithm described in wikipedia. - */ -void ReMD5::processChunk2(const uint8_t block[RE_DIGEST_CHUNK_SIZE]) { - uint32_t M[16]; - // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 - for (int ix = 0; ix < 16; ix++) { - uint32_t x = block[3]; - for (int jj = 2; jj >= 0; jj--) { - x = (x << 8) + block[jj]; - } - M[ix] = x; - block += 4; - } - //Initialize hash value for this chunk: - uint32_t A = m_a0; - uint32_t B = m_b0; - uint32_t C = m_c0; - uint32_t D = m_d0; - //Main loop: - - int F, g; -//#define TRACE_MD5 -#if defined TRACE_MD5 - printf("neu: (%s)\n", block); -#endif - for (int i = 0; i < RE_DIGEST_CHUNK_SIZE; i++) { -#if defined TRACE_MD5 - if (i < 8) - printf("%2d: A: %08x B: %08x C: %08x D%08x\n", i, A, B, C, D); -#endif - if (i < 16) { -# define F1(B, C, D) ((B & C) | (~ B & D)) - // F := (B and C) or ((not B) and D) - F = F1(B, C, D); - g = i; - } else if (i < 32) { - // F := (D and B) or (C and (not D)) - // g := (5×i + 1) mod 16 -# define F2(B, C, D) ((D & B) | (C & ~ D)) - F = F2(B, C, D); - g = (5 * i + 1) % 16; - } else if (i < 48) { - // F := B xor C xor D - // g := (3×i + 5) mod 16 -# define F3(B, C, D) ((B ^ C) ^ D) - F = F3(B, C, D); - g = (3 * i + 5) % 16; - } else { - // F := C xor (B or (not D)) -# define F4(B, C, D) (C ^ (B | ~ D)) - // g := (7×i) mod 16 - F = F4(B, C, D); - g = (7 * i) % 16; - } -#if defined TRACE_MD5 - if (i < 8) - printf(" K[%2d]: %08x M[%2d]: %08x shift: %02d\n", - i, m_K[i], g, M[g], m_s[i]); -#endif - uint32_t dTemp = D; - D = C; - C = B; - // B := B + leftrotate((A + F + K[i] + M[g]), s[i]) - uint32_t x = (A + F + m_K[i] + M[g]); - int shift = m_s[i]; - B += (x << shift) | (x >> (32 - shift)); - A = dTemp; - } - //Add this chunk's hash to result so far: - m_a0 += A; - m_b0 += B; - m_c0 += C; - m_d0 += D; -} -/** ---------------------- - */ -#if defined OPTIMIZER_WORKS_GREAT -inline void rotate_left_and_add(uint32_t& rc, uint32_t data, int shift, uint32_t term) { - rc = ((data << shift) | (data >> (32-shift))) + term; -} -//#define TRACE_MD5 -#if defined TRACE_MD5 -static int s_ix = 0; -#endif -inline void X1(uint32_t &var, uint32_t x, uint32_t y, uint32_t z, - uint32_t data, uint32_t aConst, uint32_t shift) { -#if defined TRACE_MD5 - printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); - printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", - s_ix - 1, aConst, data, shift); -#endif - rotate_left_and_add(var, var + F1(x, y, z) + data + aConst, shift, x); -} -inline void X2(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, - uint32_t data, uint32_t aConst, uint32_t shift) { -#if defined TRACE_MD5 - printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); - printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", - s_ix - 1, aConst, data, shift); -#endif - rotate_left_and_add(var, var + F2(x, y, z) + data + aConst, shift, x); -} - -inline void X3(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, - uint32_t data, uint32_t aConst, uint32_t shift) { -#if defined TRACE_MD5 - printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); - printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", - s_ix - 1, aConst, data, shift); -#endif - rotate_left_and_add(var, var + F3(x, y, z) + data + aConst, shift, x); -} - -inline void X4(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, - uint32_t data, uint32_t aConst, uint32_t shift) { -#if defined TRACE_MD5 - printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); - printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", - s_ix - 1, aConst, data, shift); -#endif - rotate_left_and_add(var, var + F4(x, y, z) + data + aConst, shift, x); -} -#else + m_digest[ix + 3] = word + oneWord(m_a0, 0); + oneWord(m_b0, 4); + oneWord(m_c0, 8); + oneWord(m_d0, 12); +#endif +} + +/** + * Processes a 512 bit block ("chunk"). + * + * This method is a direct programming of the algorithm described in wikipedia. + */ +void ReMD5::processChunk2(const uint8_t block[RE_DIGEST_CHUNK_SIZE]) { + uint32_t M[16]; + // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 + for (int ix = 0; ix < 16; ix++) { + uint32_t x = block[3]; + for (int jj = 2; jj >= 0; jj--) { + x = (x << 8) + block[jj]; + } + M[ix] = x; + block += 4; + } + //Initialize hash value for this chunk: + uint32_t A = m_a0; + uint32_t B = m_b0; + uint32_t C = m_c0; + uint32_t D = m_d0; + //Main loop: + + int F, g; +//#define TRACE_MD5 +#if defined TRACE_MD5 + printf("neu: (%s)\n", block); +#endif + for (int i = 0; i < RE_DIGEST_CHUNK_SIZE; i++) { +#if defined TRACE_MD5 + if (i < 8) + printf("%2d: A: %08x B: %08x C: %08x D%08x\n", i, A, B, C, D); +#endif + if (i < 16) { +# define F1(B, C, D) ((B & C) | (~ B & D)) + // F := (B and C) or ((not B) and D) + F = F1(B, C, D); + g = i; + } else if (i < 32) { + // F := (D and B) or (C and (not D)) + // g := (5×i + 1) mod 16 +# define F2(B, C, D) ((D & B) | (C & ~ D)) + F = F2(B, C, D); + g = (5 * i + 1) % 16; + } else if (i < 48) { + // F := B xor C xor D + // g := (3×i + 5) mod 16 +# define F3(B, C, D) ((B ^ C) ^ D) + F = F3(B, C, D); + g = (3 * i + 5) % 16; + } else { + // F := C xor (B or (not D)) +# define F4(B, C, D) (C ^ (B | ~ D)) + // g := (7×i) mod 16 + F = F4(B, C, D); + g = (7 * i) % 16; + } +#if defined TRACE_MD5 + if (i < 8) + printf(" K[%2d]: %08x M[%2d]: %08x shift: %02d\n", + i, m_K[i], g, M[g], m_s[i]); +#endif + uint32_t dTemp = D; + D = C; + C = B; + // B := B + leftrotate((A + F + K[i] + M[g]), s[i]) + uint32_t x = (A + F + m_K[i] + M[g]); + int shift = m_s[i]; + B += (x << shift) | (x >> (32 - shift)); + A = dTemp; + } + //Add this chunk's hash to result so far: + m_a0 += A; + m_b0 += B; + m_c0 += C; + m_d0 += D; +} +/** ---------------------- + */ +#if defined OPTIMIZER_WORKS_GREAT +inline void rotate_left_and_add(uint32_t& rc, uint32_t data, int shift, uint32_t term) { + rc = ((data << shift) | (data >> (32-shift))) + term; +} +//#define TRACE_MD5 +#if defined TRACE_MD5 +static int s_ix = 0; +#endif +inline void X1(uint32_t &var, uint32_t x, uint32_t y, uint32_t z, + uint32_t data, uint32_t aConst, uint32_t shift) { +#if defined TRACE_MD5 + printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); + printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", + s_ix - 1, aConst, data, shift); +#endif + rotate_left_and_add(var, var + F1(x, y, z) + data + aConst, shift, x); +} +inline void X2(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, + uint32_t data, uint32_t aConst, uint32_t shift) { +#if defined TRACE_MD5 + printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); + printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", + s_ix - 1, aConst, data, shift); +#endif + rotate_left_and_add(var, var + F2(x, y, z) + data + aConst, shift, x); +} + +inline void X3(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, + uint32_t data, uint32_t aConst, uint32_t shift) { +#if defined TRACE_MD5 + printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); + printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", + s_ix - 1, aConst, data, shift); +#endif + rotate_left_and_add(var, var + F3(x, y, z) + data + aConst, shift, x); +} + +inline void X4(uint32_t& var, uint32_t x, uint32_t y, uint32_t z, + uint32_t data, uint32_t aConst, uint32_t shift) { +#if defined TRACE_MD5 + printf("%2d: A: %08x B: %08x C: %08x D%08x\n", s_ix++ % 16, var, x, y, z); + printf(" K[%2d]: %08x M[?]: %08x shift: %02d\n", + s_ix - 1, aConst, data, shift); +#endif + rotate_left_and_add(var, var + F4(x, y, z) + data + aConst, shift, x); +} +#else #define rotate_left_and_add(var, data, shift, term) { \ uint32_t val = data; \ var = ((val << shift) | (val >> (32-shift))) + term; \ -} +} #define X1(var, x, y, z, data, aConst, shift) \ - rotate_left_and_add(var, var + F1(x, y, z) + data + aConst, shift, x) + rotate_left_and_add(var, var + F1(x, y, z) + data + aConst, shift, x) #define X2(var, x, y, z, data, aConst, shift) \ - rotate_left_and_add(var, var + F2(x, y, z) + data + aConst, shift, x) + rotate_left_and_add(var, var + F2(x, y, z) + data + aConst, shift, x) #define X3(var, x, y, z, data, aConst, shift) \ - rotate_left_and_add(var, var + F3(x, y, z) + data + aConst, shift, x) + rotate_left_and_add(var, var + F3(x, y, z) + data + aConst, shift, x) #define X4(var, x, y, z, data, aConst, shift) \ - rotate_left_and_add(var, var + F4(x, y, z) + data + aConst, shift, x) -#endif /* OPTIMIZER_WORKS_GREAT */ - -/** - * Processes a 512 bit block ("chunk"). - * - * This is a optimized version, derived from the method above. - * We unroll the loop, this brings speed with factor 2. - *
- * B := B + leftrotate((A + F + K[i] + M[g]), s[i])
- *	D := C;
- *	C := B;
- *  B := B + leftrotate((A + F + K[i] + M[g]), s[i])
- *  A := D(old)
- * (D, C, B, A) = (C, B, B + leftrotate((A + F + K[i] + M[g]), s[i]), D)
- * ==> (A, B, C, D) = (D, B + leftrotate((A + F + K[i] + M[g]), s[i]), B, A)
- * The unrolled loop:
- * i = g = 0;
- * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[0] + M[0]), s[0]), B, A)
- * only one var must be calculated, the other 3 are exchanged only.
- * i = g = 1;
- * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[1] + M[1]), s[1]), B, A)
- * i = g = 2;
- * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[2] + M[2]), s[2]), B, A)
- * i = g = 3;
- * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[3] + M[3]), s[3]), B, A)
- * in each of the 4 statements another variable (of A, B, C and D) will be calculated
- * so we do not exchange in each step, we calculate in the end position
- * we define a function to do this:
- * void X1(uint32_t &var, uint32_t x, uint32_t y, uint32_t z, uint32_t data, uint32_t shift, uint32_t aConst){
- *	 var = rotate_left(var+ F1(x, y, z) + data + aConst, shift) + x;
- * }
- * Note: the input parameter of X1 must respect the exchange:
- * A -> D -> C -> B ...
- * X1(A, B, C, D,  M[0], K[0], s[0]);
- * X1(D, A, B, C,  M[1], K[1], s[1]);
- * X1(C, D, A, B,  M[2], K[2], s[2]);
- * X1(B, C, D, A,  M[3], K[3], s[3]);
- * ...
- * 
- */ -void ReMD5::processChunk(const uint8_t block[RE_DIGEST_CHUNK_SIZE]) { - uint32_t M[16]; - // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 -#ifdef __LITTLE_ENDIAN__ - for (int ix = 0; ix < 16; ix++) { - //memcpy(&M[ix], block + ix * 4, 4); - M[ix] = *(uint32_t*) (block + ix * 4); - } -#elif defined __BIG_ENDIAN__ - for (int ix = 0; ix < 16; ix++) { - uint32_t x = block[3]; - for (int jj = 2; jj >= 0; jj--) { - x = (x << 8) + block[jj]; - } - M[ix] = x; - block += 4; - } -#else -# error "missing __LITTLE_ENDIAN__ or __BIG_ENDIAN__" -#endif - //Initialize hash value for this chunk: - uint32_t A = m_a0; - uint32_t B = m_b0; - uint32_t C = m_c0; - uint32_t D = m_d0; -#if defined NeverAndNeverAndNeverAgain - // Derivation of the optimization: - -#endif - /* Round 1 */ - X1(A, B, C, D, M[0], 0xd76aa478, 7); - X1(D, A, B, C, M[1], 0xe8c7b756, 12); - X1(C, D, A, B, M[2], 0x242070db, 17); - X1(B, C, D, A, M[3], 0xc1bdceee, 22); - X1(A, B, C, D, M[4], 0xf57c0faf, 7); - X1(D, A, B, C, M[5], 0x4787c62a, 12); - X1(C, D, A, B, M[6], 0xa8304613, 17); - X1(B, C, D, A, M[7], 0xfd469501, 22); - X1(A, B, C, D, M[8], 0x698098d8, 7); - X1(D, A, B, C, M[9], 0x8b44f7af, 12); - X1(C, D, A, B, M[10], 0xffff5bb1, 17); - X1(B, C, D, A, M[11], 0x895cd7be, 22); - X1(A, B, C, D, M[12], 0x6b901122, 7); - X1(D, A, B, C, M[13], 0xfd987193, 12); - X1(C, D, A, B, M[14], 0xa679438e, 17); - X1(B, C, D, A, M[15], 0x49b40821, 22); - - /* Round 2 */ - X2(A, B, C, D, M[1], 0xf61e2562, 5); - X2(D, A, B, C, M[6], 0xc040b340, 9); - X2(C, D, A, B, M[11], 0x265e5a51, 14); - X2(B, C, D, A, M[0], 0xe9b6c7aa, 20); - X2(A, B, C, D, M[5], 0xd62f105d, 5); - X2(D, A, B, C, M[10], 0x02441453, 9); - X2(C, D, A, B, M[15], 0xd8a1e681, 14); - X2(B, C, D, A, M[4], 0xe7d3fbc8, 20); - X2(A, B, C, D, M[9], 0x21e1cde6, 5); - X2(D, A, B, C, M[14], 0xc33707d6, 9); - X2(C, D, A, B, M[3], 0xf4d50d87, 14); - X2(B, C, D, A, M[8], 0x455a14ed, 20); - X2(A, B, C, D, M[13], 0xa9e3e905, 5); - X2(D, A, B, C, M[2], 0xfcefa3f8, 9); - X2(C, D, A, B, M[7], 0x676f02d9, 14); - X2(B, C, D, A, M[12], 0x8d2a4c8a, 20); - - /* Round 3 */ - X3(A, B, C, D, M[5], 0xfffa3942, 4); - X3(D, A, B, C, M[8], 0x8771f681, 11); - X3(C, D, A, B, M[11], 0x6d9d6122, 16); - X3(B, C, D, A, M[14], 0xfde5380c, 23); - X3(A, B, C, D, M[1], 0xa4beea44, 4); - X3(D, A, B, C, M[4], 0x4bdecfa9, 11); - X3(C, D, A, B, M[7], 0xf6bb4b60, 16); - X3(B, C, D, A, M[10], 0xbebfbc70, 23); - X3(A, B, C, D, M[13], 0x289b7ec6, 4); - X3(D, A, B, C, M[0], 0xeaa127fa, 11); - X3(C, D, A, B, M[3], 0xd4ef3085, 16); - X3(B, C, D, A, M[6], 0x04881d05, 23); - X3(A, B, C, D, M[9], 0xd9d4d039, 4); - X3(D, A, B, C, M[12], 0xe6db99e5, 11); - X3(C, D, A, B, M[15], 0x1fa27cf8, 16); - X3(B, C, D, A, M[2], 0xc4ac5665, 23); - - /* Round 4 */ - X4(A, B, C, D, M[0], 0xf4292244, 6); - X4(D, A, B, C, M[7], 0x432aff97, 10); - X4(C, D, A, B, M[14], 0xab9423a7, 15); - X4(B, C, D, A, M[5], 0xfc93a039, 21); - X4(A, B, C, D, M[12], 0x655b59c3, 6); - X4(D, A, B, C, M[3], 0x8f0ccc92, 10); - X4(C, D, A, B, M[10], 0xffeff47d, 15); - X4(B, C, D, A, M[1], 0x85845dd1, 21); - X4(A, B, C, D, M[8], 0x6fa87e4f, 6); - X4(D, A, B, C, M[15], 0xfe2ce6e0, 10); - X4(C, D, A, B, M[6], 0xa3014314, 15); - X4(B, C, D, A, M[13], 0x4e0811a1, 21); - X4(A, B, C, D, M[4], 0xf7537e82, 6); - X4(D, A, B, C, M[11], 0xbd3af235, 10); - X4(C, D, A, B, M[2], 0x2ad7d2bb, 15); - X4(B, C, D, A, M[9], 0xeb86d391, 21); - - //Add this chunk's hash to result so far: - m_a0 += A; - m_b0 += B; - m_c0 += C; - m_d0 += D; -} -/** - * Prepares the instance for a new checksum. - */ -void ReMD5::reset() { - m_a0 = 0x67452301; - m_b0 = 0xefcdab89; - m_c0 = 0x98badcfe; - m_d0 = 0x10325476; - memset(m_digest, 0, sizeof m_digest); - memset(m_waiting, 0, m_chunkSize); - m_lengthWaiting = 0; - m_length = 0; - m_hexDigest.setLength(0); - m_finalized = false; -} + rotate_left_and_add(var, var + F4(x, y, z) + data + aConst, shift, x) +#endif /* OPTIMIZER_WORKS_GREAT */ + +/** + * Processes a 512 bit block ("chunk"). + * + * This is a optimized version, derived from the method above. + * We unroll the loop, this brings speed with factor 2. + *
+ * B := B + leftrotate((A + F + K[i] + M[g]), s[i])
+ *	D := C;
+ *	C := B;
+ *  B := B + leftrotate((A + F + K[i] + M[g]), s[i])
+ *  A := D(old)
+ * (D, C, B, A) = (C, B, B + leftrotate((A + F + K[i] + M[g]), s[i]), D)
+ * ==> (A, B, C, D) = (D, B + leftrotate((A + F + K[i] + M[g]), s[i]), B, A)
+ * The unrolled loop:
+ * i = g = 0;
+ * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[0] + M[0]), s[0]), B, A)
+ * only one var must be calculated, the other 3 are exchanged only.
+ * i = g = 1;
+ * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[1] + M[1]), s[1]), B, A)
+ * i = g = 2;
+ * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[2] + M[2]), s[2]), B, A)
+ * i = g = 3;
+ * (A, B, C, D) = (D, B + leftrotate((A +  F1(B, C, D) + K[3] + M[3]), s[3]), B, A)
+ * in each of the 4 statements another variable (of A, B, C and D) will be calculated
+ * so we do not exchange in each step, we calculate in the end position
+ * we define a function to do this:
+ * void X1(uint32_t &var, uint32_t x, uint32_t y, uint32_t z, uint32_t data, uint32_t shift, uint32_t aConst){
+ *	 var = rotate_left(var+ F1(x, y, z) + data + aConst, shift) + x;
+ * }
+ * Note: the input parameter of X1 must respect the exchange:
+ * A -> D -> C -> B ...
+ * X1(A, B, C, D,  M[0], K[0], s[0]);
+ * X1(D, A, B, C,  M[1], K[1], s[1]);
+ * X1(C, D, A, B,  M[2], K[2], s[2]);
+ * X1(B, C, D, A,  M[3], K[3], s[3]);
+ * ...
+ * 
+ */ +void ReMD5::processChunk(const uint8_t block[RE_DIGEST_CHUNK_SIZE]) { + uint32_t M[16]; + // break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15 +#ifdef __LITTLE_ENDIAN__ + for (int ix = 0; ix < 16; ix++) { + //memcpy(&M[ix], block + ix * 4, 4); + M[ix] = *(uint32_t*) (block + ix * 4); + } +#elif defined __BIG_ENDIAN__ + for (int ix = 0; ix < 16; ix++) { + uint32_t x = block[3]; + for (int jj = 2; jj >= 0; jj--) { + x = (x << 8) + block[jj]; + } + M[ix] = x; + block += 4; + } +#else +# error "missing __LITTLE_ENDIAN__ or __BIG_ENDIAN__" +#endif + //Initialize hash value for this chunk: + uint32_t A = m_a0; + uint32_t B = m_b0; + uint32_t C = m_c0; + uint32_t D = m_d0; +#if defined NeverAndNeverAndNeverAgain + // Derivation of the optimization: + +#endif + /* Round 1 */ + X1(A, B, C, D, M[0], 0xd76aa478, 7); + X1(D, A, B, C, M[1], 0xe8c7b756, 12); + X1(C, D, A, B, M[2], 0x242070db, 17); + X1(B, C, D, A, M[3], 0xc1bdceee, 22); + X1(A, B, C, D, M[4], 0xf57c0faf, 7); + X1(D, A, B, C, M[5], 0x4787c62a, 12); + X1(C, D, A, B, M[6], 0xa8304613, 17); + X1(B, C, D, A, M[7], 0xfd469501, 22); + X1(A, B, C, D, M[8], 0x698098d8, 7); + X1(D, A, B, C, M[9], 0x8b44f7af, 12); + X1(C, D, A, B, M[10], 0xffff5bb1, 17); + X1(B, C, D, A, M[11], 0x895cd7be, 22); + X1(A, B, C, D, M[12], 0x6b901122, 7); + X1(D, A, B, C, M[13], 0xfd987193, 12); + X1(C, D, A, B, M[14], 0xa679438e, 17); + X1(B, C, D, A, M[15], 0x49b40821, 22); + + /* Round 2 */ + X2(A, B, C, D, M[1], 0xf61e2562, 5); + X2(D, A, B, C, M[6], 0xc040b340, 9); + X2(C, D, A, B, M[11], 0x265e5a51, 14); + X2(B, C, D, A, M[0], 0xe9b6c7aa, 20); + X2(A, B, C, D, M[5], 0xd62f105d, 5); + X2(D, A, B, C, M[10], 0x02441453, 9); + X2(C, D, A, B, M[15], 0xd8a1e681, 14); + X2(B, C, D, A, M[4], 0xe7d3fbc8, 20); + X2(A, B, C, D, M[9], 0x21e1cde6, 5); + X2(D, A, B, C, M[14], 0xc33707d6, 9); + X2(C, D, A, B, M[3], 0xf4d50d87, 14); + X2(B, C, D, A, M[8], 0x455a14ed, 20); + X2(A, B, C, D, M[13], 0xa9e3e905, 5); + X2(D, A, B, C, M[2], 0xfcefa3f8, 9); + X2(C, D, A, B, M[7], 0x676f02d9, 14); + X2(B, C, D, A, M[12], 0x8d2a4c8a, 20); + + /* Round 3 */ + X3(A, B, C, D, M[5], 0xfffa3942, 4); + X3(D, A, B, C, M[8], 0x8771f681, 11); + X3(C, D, A, B, M[11], 0x6d9d6122, 16); + X3(B, C, D, A, M[14], 0xfde5380c, 23); + X3(A, B, C, D, M[1], 0xa4beea44, 4); + X3(D, A, B, C, M[4], 0x4bdecfa9, 11); + X3(C, D, A, B, M[7], 0xf6bb4b60, 16); + X3(B, C, D, A, M[10], 0xbebfbc70, 23); + X3(A, B, C, D, M[13], 0x289b7ec6, 4); + X3(D, A, B, C, M[0], 0xeaa127fa, 11); + X3(C, D, A, B, M[3], 0xd4ef3085, 16); + X3(B, C, D, A, M[6], 0x04881d05, 23); + X3(A, B, C, D, M[9], 0xd9d4d039, 4); + X3(D, A, B, C, M[12], 0xe6db99e5, 11); + X3(C, D, A, B, M[15], 0x1fa27cf8, 16); + X3(B, C, D, A, M[2], 0xc4ac5665, 23); + + /* Round 4 */ + X4(A, B, C, D, M[0], 0xf4292244, 6); + X4(D, A, B, C, M[7], 0x432aff97, 10); + X4(C, D, A, B, M[14], 0xab9423a7, 15); + X4(B, C, D, A, M[5], 0xfc93a039, 21); + X4(A, B, C, D, M[12], 0x655b59c3, 6); + X4(D, A, B, C, M[3], 0x8f0ccc92, 10); + X4(C, D, A, B, M[10], 0xffeff47d, 15); + X4(B, C, D, A, M[1], 0x85845dd1, 21); + X4(A, B, C, D, M[8], 0x6fa87e4f, 6); + X4(D, A, B, C, M[15], 0xfe2ce6e0, 10); + X4(C, D, A, B, M[6], 0xa3014314, 15); + X4(B, C, D, A, M[13], 0x4e0811a1, 21); + X4(A, B, C, D, M[4], 0xf7537e82, 6); + X4(D, A, B, C, M[11], 0xbd3af235, 10); + X4(C, D, A, B, M[2], 0x2ad7d2bb, 15); + X4(B, C, D, A, M[9], 0xeb86d391, 21); + + //Add this chunk's hash to result so far: + m_a0 += A; + m_b0 += B; + m_c0 += C; + m_d0 += D; +} +/** + * Prepares the instance for a new checksum. + */ +void ReMD5::reset() { + m_a0 = 0x67452301; + m_b0 = 0xefcdab89; + m_c0 = 0x98badcfe; + m_d0 = 0x10325476; + memset(m_digest, 0, sizeof m_digest); + memset(m_waiting, 0, m_chunkSize); + m_lengthWaiting = 0; + m_length = 0; + m_hexDigest.setLength(0); + m_finalized = false; +} diff --git a/net/ReTCP.cpp b/net/ReTCP.cpp index 1b1059c..fe3e5c9 100644 --- a/net/ReTCP.cpp +++ b/net/ReTCP.cpp @@ -540,8 +540,7 @@ bool ReTCPServer::listenForAll() { memcpy(m_ip, "0.0.0.0", 8); m_name.set(m_ip).appendChar(':').appendInt(m_port); - getaddrinfo(NULL, ReByteArray().appendInt(m_port).str(), &hints, - &addrInfo); + getaddrinfo(NULL, ReByteArray().appendInt(m_port).str(), &hints, &addrInfo); m_family = addrInfo->ai_family; // make a socket: m_handleSocket = socket(addrInfo->ai_family, addrInfo->ai_socktype, diff --git a/net/ReUdpConnection.cpp b/net/ReUdpConnection.cpp index 5afca30..846abda 100644 --- a/net/ReUdpConnection.cpp +++ b/net/ReUdpConnection.cpp @@ -151,9 +151,9 @@ void ReUdpConnection::close() { LC_UDPCONNECTION_CLOSE_1, i18n("Connection has been closed: $1:$2")) .arg(address()).arg(m_port).end(); #if defined __linux__ - int rc = ::close(m_socket); + int rc = ::close(m_socket); #elif defined __WIN32__ - int rc = closesocket(m_socket); + int rc = closesocket(m_socket); #endif if (rc != 0) m_logger->sayF(LOG_ERROR | GRAN_USER | CAT_NETWORK, diff --git a/net/ReUdpConnection.hpp b/net/ReUdpConnection.hpp index 3208da2..22f57e2 100644 --- a/net/ReUdpConnection.hpp +++ b/net/ReUdpConnection.hpp @@ -24,8 +24,7 @@ public: int port() const { return m_port; } - int receive(int timeout = 0, ReByteArray* buffer = NULL, - bool doLog = true); + int receive(int timeout = 0, ReByteArray* buffer = NULL, bool doLog = true); int send(const char* buffer, int bufferLength = -1); void close(); ReByteArray& buffer(); diff --git a/os/ReDirTools.cpp b/os/ReDirTools.cpp index 6757543..0db363e 100644 --- a/os/ReDirTools.cpp +++ b/os/ReDirTools.cpp @@ -744,7 +744,7 @@ ReTool::~ReTool() { */ bool ReTool::trace(const char* currentFile) { ReByteArray buffer(" "); - int duration = int(time(NULL) - m_startTime); + int duration = int((clock() - m_startTime) / CLOCKS_PER_SEC); buffer.appendInt(duration / 60).appendInt(duration % 60, ":%02d: "); buffer.appendInt(m_files).appendChar('/').appendInt( m_traverser.directories()).append(" dir(s)"); @@ -1195,7 +1195,7 @@ void ReDirChecksum::buildStorage(const char* path, const char* storageFile) { if (fp == NULL) { m_logger->sayF(LOG_ERROR | CAT_FILE, LC_BUILD_DIRECTORY_1, i18n("cannot open file: $1 (errno: $2)")).arg(storageFile).arg( - errno).end(); + errno).end(); } else { int level; @@ -1982,7 +1982,7 @@ ReErrNo_t ReDirTouch::touch(const char* filename, const ReFileTime_t& modified, if (rc != 0 && logger != NULL) logger->sayF(LOG_ERROR | CAT_FILE, LC_TOUCH_1, i18n("cannot change filetime: $1 (errno: $2)")).arg(filename).arg( - errno).end(); + errno).end(); return rc; } @@ -2042,7 +2042,10 @@ void ReDirSync::makeDirWithParents(ReByteArray& path, int minWidth, */ ReDirSync::ReDirSync(ReLogger* logger) : ReTool(s_syncUsage, s_syncExamples, 2, 0, 1, false, logger), - m_buffer() { + m_buffer(), + m_deletedDirs(-1), + m_deletedFiles(0), + m_deletedSumSizes(0) { // standard short options: D d O o P p T t v y Z z m_buffer.ensureSize(4u * 1024u * 1024u); m_programArgs.addBool("add", @@ -2093,7 +2096,7 @@ ReDirSync::ReDirSync(ReLogger* logger) : * @param trgProps NULL or the properties of the (existing) target file */ void ReDirSync::copyFile(bool dry, ReDirStatus_t* entry, const char* target, - const char* targetName, ReFileProperties_t* trgProps) { + const char* targetName, struct stat* trgProps) { ReFileProperties_t* props; #ifdef __linux__ props = entry->getStatus(); @@ -2219,7 +2222,7 @@ bool ReDirSync::copyFile(const char* source, bool isLink, if (logger != NULL) logger->sayF(LOG_ERROR | CAT_FILE, LC_COPY_FILE_1, i18n("could not find: $1 (errno: $2)")).arg(source).arg( - errno).end(); + errno).end(); } } if (isLink) { @@ -2230,7 +2233,7 @@ bool ReDirSync::copyFile(const char* source, bool isLink, if (logger != NULL) logger->sayF(LOG_ERROR | CAT_FILE, LC_COPY_FILE_2, i18n("cannot read link $1 (errno: $2)")).arg(source).arg( - errno).end(); + errno).end(); } else { reference[referenceLength] = '\0'; unlink(target); @@ -2254,7 +2257,7 @@ bool ReDirSync::copyFile(const char* source, bool isLink, if (logger != NULL) logger->sayF(LOG_ERROR | CAT_FILE, LC_COPY_FILE_5, i18n("cannot open $1 (errno: $2)")).arg(target).arg( - errno).end(); + errno).end(); } else { while (size > 0) { size_t blockSize = buffer.capacity(); @@ -2304,6 +2307,7 @@ bool ReDirSync::copyFile(const char* source, bool isLink, */ void ReDirSync::deleteSuperfluous(const ReByteArray& source, const ReByteArray& target, const ReFileTime_t& deleteTime) { + m_deletedDirs++; ReTraceUnit& tracer = *this; ReDirectory trg(target.str()); if (trg.findFirst(ReDirectory::ALL_FILES, false)) { @@ -2327,7 +2331,9 @@ void ReDirSync::deleteSuperfluous(const ReByteArray& source, } else if (ignoreTime || trg.currentModified() >= deleteTime) { if (trg.currentIsDir()) { if (!dry) - ReDirectory::deleteTree(trg.currentFull().str(), true); + ReDirectory::deleteTree(trg.currentFull().str(), true, + &m_deletedDirs, &m_deletedFiles, + &m_deletedSumSizes); if (m_verboseLevel >= V_NORMAL) fprintf(m_output, "-%s%s\n", trg.currentFull().str(), dry ? " would be deleted (dir)" : ""); @@ -2337,9 +2343,14 @@ void ReDirSync::deleteSuperfluous(const ReByteArray& source, LC_DELETE_SUPERFLUOUS_1, i18n("cannot delete file: $1 (errno: $2)")).arg( trg.currentFull()).arg(errno).end(); - else if (m_verboseLevel >= V_NORMAL) - fprintf(m_output, "-%s%s\n", trg.currentFull().str(), - dry ? " would be deleted" : ""); + else { + if (m_verboseLevel >= V_NORMAL) + fprintf(m_output, "-%s%s\n", + trg.currentFull().str(), + dry ? " would be deleted" : ""); + m_deletedFiles++; + m_deletedSumSizes += trg.currentSize(); + } } } } while (trg.findNext()); @@ -2445,14 +2456,28 @@ bool ReDirSync::makeDirectory(const char* directory, int minLength, } static void printStatus(FILE* fp, double duration, int files, int sumSizes, - int treeDirs, int treeFiles, int64_t treeSumSizes) { + int treeDirs, int treeFiles, int64_t treeSumSizes, int deletedDirs, + int deletedFiles, int64_t deletedSumSizes) { + ReByteArray duration2; + if (duration >= 3600.0) { + duration2.appendInt(int(trunc(duration)) / 3600, "%02d").append(':') + .append(int(trunc(duration)) % 3600 / 60, "%02d").append(':').append( + int(trunc(duration)) % 3600, "%02d"); + } else if (duration >= 5.0) { + duration2.append(int(trunc(duration)) % 3600 / 60, " %02d").append( + ':').append(int(trunc(duration)) % 3600, "%02d"); + } else { + duration2.append(duration, "%9.3f"); + } fprintf(fp, - i18n( - "=== copied: %02d:%02d sec %7d file(s) %12.6f MByte (%.3f MB/sec).\n" - "=== tree: %5d dir(s) %7d file(s) %12.6f MByte\n"), - int(duration) / 60, int(duration) % 60, files, sumSizes / 1E6, + i18n("=== copied: %s sec %7d file(s) %12.6f MByte (%.3f MB/sec).\n" + "=== tree: %5d dir(s) %7d file(s) %12.6f MByte\n"), + duration2.str(), files, sumSizes / 1E6, sumSizes / 1E6 / (duration == 0.0 ? 0.001 : duration), treeDirs, treeFiles, treeSumSizes / 1E6); + if (deletedDirs >= 0) + fprintf(fp, i18n("=== deleted: %5d dir(s) %7d file(s) %12.6f MByte\n"), + deletedDirs, deletedFiles, deletedSumSizes / 1E6); } /** * Synchronizes two directory trees. @@ -2483,6 +2508,8 @@ void ReDirSync::doIt() { if (!buffer.empty()) deleteDate = checkDate(buffer.str()); } + if (deleteTarget) + m_deletedDirs = 0; int64_t sumSizes = 0; int files = 0; @@ -2508,8 +2535,7 @@ void ReDirSync::doIt() { } size_t ixSourceRelative = source.length(); size_t ixTargetRelative = target.length(); - - if (deleteTarget && deleteBefore) + if (deleteTarget && deleteBefore && !endsWithSlash) deleteSuperfluous(source, target, deleteDate); m_traverser.changeBase(source.str()); m_traverser.setPropertiesFromFilter(&filter); @@ -2562,7 +2588,7 @@ void ReDirSync::doIt() { files++; sumSizes += entry->fileSize(); copyFile(dry, entry, targetFile.str(), targetRelativePath, - &targetInfo); + targetExists ? &targetInfo : NULL); } if (deleteTarget && !deleteBefore) deleteSuperfluous(source, target, deleteDate); @@ -2573,7 +2599,7 @@ void ReDirSync::doIt() { double duration = double((clock() - m_start)) / CLOCKS_PER_SEC; if (m_verboseLevel >= V_SUMMARY) { printStatus(m_output, duration, files, sumSizes, treeDirs, treeFiles, - treeSumSizes); + treeSumSizes, m_deletedDirs, m_deletedFiles, m_deletedSumSizes); } if (m_programArgs.getString("editor", buffer) != NULL) { ReByteArray tempFile = ReFileUtils::tempDir("redirtool.status."); @@ -2586,7 +2612,7 @@ void ReDirSync::doIt() { else { fprintf(fp, i18n("backup finished!\n\n")); printStatus(fp, duration, files, sumSizes, treeDirs, treeFiles, - treeSumSizes); + treeSumSizes, m_deletedDirs, m_deletedFiles, m_deletedSumSizes); fclose(fp); buffer.insert(0, "\"", -1); buffer.append("\" ").append(tempFile); diff --git a/os/ReDirTools.hpp b/os/ReDirTools.hpp index f50b218..b0a8577 100644 --- a/os/ReDirTools.hpp +++ b/os/ReDirTools.hpp @@ -273,7 +273,7 @@ public: protected: virtual void doIt(); void copyFile(bool dry, ReDirStatus_t* entry, const char* target, - const char* targetRelative, ReFileProperties_t* trgProps); + const char* targetRelative, struct stat* trgProps); void deleteSuperfluous(const ReByteArray& source, const ReByteArray& target, const ReFileTime_t& deleteTime); void makeDirWithParents(ReByteArray& path, int minWidth, @@ -292,6 +292,9 @@ public: ReFileProperties_t* properties, ReLogger* logger = NULL); protected: ReByteArray m_buffer; + int m_deletedDirs; + int m_deletedFiles; + int64_t m_deletedSumSizes; }; /** diff --git a/os/ReRemoteDir.cpp b/os/ReRemoteDir.cpp index 9dc789f..18fe971 100644 --- a/os/ReRemoteDir.cpp +++ b/os/ReRemoteDir.cpp @@ -58,7 +58,7 @@ void ReRemoteDir::populate(const char* path) { * @param sequence IN/OUT: the place for the byte sequence */ ReByteArray& ReRemoteDir::serialize(ReByteArray& sequence) { - return sequence; + return sequence; } /** @@ -74,6 +74,6 @@ ReRemoteDirService::~ReRemoteDirService() { ReNetCommandHandler::ProcessingState ReRemoteDirService::handleNetCommand( ReByteArray& command, ReByteArray& data, ReTCPConnection* connection) { - return ReNetCommandHandler::PS_UNDEF; + return ReNetCommandHandler::PS_UNDEF; } diff --git a/os/ReTraverser.cpp b/os/ReTraverser.cpp index 971d565..ea48c55 100644 --- a/os/ReTraverser.cpp +++ b/os/ReTraverser.cpp @@ -91,7 +91,6 @@ const char* ReDirStatus_t::filetimeAsString(ReByteArray& buffer) { return ReFileUtils::filetimeToString(modified(), buffer).str(); } - /** * Loads the info about the first file into the instance. * @@ -588,11 +587,11 @@ ReByteArray& ReDirEntryFilter::serialize(ReByteArray& sequence) { value = (m_minAge.tv_sec << 32) + m_minAge.tv_nsec; #elif defined __WIN32__ // #error "missing impl" - assert(false); + assert(false); #endif sequence.appendBits64(int64_t(value)); packBool(sequence, m_allDirectories); - return sequence; + return sequence; } /** -- 2.39.5