From 3bdc27a10428cfd22c1122bd3859f346cba07a7b Mon Sep 17 00:00:00 2001 From: Marek Niepieklo Date: Tue, 29 Jun 2021 11:12:51 +0200 Subject: [PATCH] [CP-270] Replace CRC32 library Replaced CRC-32 lib from csbruce.com/software/ with github.com/stbrumme/hash-library --- .gitmodules | 7 ++--- .../service-desktop/CMakeLists.txt | 2 +- .../endpoints/update/UpdateMuditaOS.cpp | 6 ++++- module-utils/CMakeLists.txt | 6 ++--- module-utils/Utils.cpp | 8 +++--- module-utils/bootconfig/CMakeLists.txt | 2 +- module-utils/bootconfig/src/bootconfig.cpp | 1 - third-party/CMakeLists.txt | 2 +- third-party/crc32/CMakeLists.txt | 13 --------- third-party/crc32/src | 1 - third-party/hash-library/CMakeLists.txt | 27 +++++++++++++++++++ third-party/hash-library/src | 1 + 12 files changed, 47 insertions(+), 29 deletions(-) delete mode 100644 third-party/crc32/CMakeLists.txt delete mode 160000 third-party/crc32/src create mode 100644 third-party/hash-library/CMakeLists.txt create mode 160000 third-party/hash-library/src diff --git a/.gitmodules b/.gitmodules index a5c745c472b7c171b39bae3fbe3b16a3791359b6..cea833e0fccee4e2e9c4897c7b762e878845da88 100644 --- a/.gitmodules +++ b/.gitmodules @@ -8,9 +8,6 @@ [submodule "module-utils/segger"] path = module-utils/segger url = ../segger-utils.git -[submodule "module-utils/crc32"] - path = third-party/crc32/src - url = ../crc32 [submodule "module-utils/protobuf"] path = third-party/protobuf/src url = ../protobuf.git @@ -92,3 +89,7 @@ [submodule "third-party/base64/src"] path = third-party/base64/src url = https://github.com/rafagafe/base64.git +[submodule "third-party/hash-library/src"] + path = third-party/hash-library/src + url = https://github.com/mudita/hash-library.git + branch = rt1051 diff --git a/module-services/service-desktop/CMakeLists.txt b/module-services/service-desktop/CMakeLists.txt index 548a2588a552ac36e20e363c9bfd924cc779cb43..7f38ed8f7e7d008c9d63fcc7dd8328f123e34899 100644 --- a/module-services/service-desktop/CMakeLists.txt +++ b/module-services/service-desktop/CMakeLists.txt @@ -57,7 +57,6 @@ target_include_directories(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} PRIVATE - crc32 microtar module-utils service-cellular @@ -70,6 +69,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC module-cellular eventstore + hash-library::hash-library ) if (${ENABLE_TESTS}) diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp index 4a546c81da65a6574efea52c158009470cfe08a4..415f1e6ff88df78e7ad31c92155e11da920e7ec4 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp @@ -497,6 +497,8 @@ bool UpdateMuditaOS::unpackFileToTemp(mtar_header_t &h, unsigned long *crc32) return false; } + CRC32 digest; + for (uint32_t i = 0; i < blocksToRead; i++) { if (i + 1 == blocksToRead) { sizeToRead = h.size % boot::consts::tar_buf; @@ -524,9 +526,11 @@ bool UpdateMuditaOS::unpackFileToTemp(mtar_header_t &h, unsigned long *crc32) currentExtractedBytes += dataWritten; - *crc32 = Crc32_ComputeBuf(*crc32, readBuf.get(), sizeToRead); + digest.add(readBuf.get(), sizeToRead); } std::fclose(fp); + + *crc32 = digest.getHashValue(); return true; } diff --git a/module-utils/CMakeLists.txt b/module-utils/CMakeLists.txt index f4900bf789fe122eda5724f29d78a4fee2a7c35c..6109ca5be0039814b35155ad8f61943d35caa875 100644 --- a/module-utils/CMakeLists.txt +++ b/module-utils/CMakeLists.txt @@ -22,15 +22,15 @@ add_library(${PROJECT_NAME} STATIC ${SOURCES}) include(segger/rtt/CMakeLists.txt) target_link_libraries(${PROJECT_NAME} -PUBLIC +PUBLIC board libphonenumber::libphonenumber log magic_enum - module-os + module-os module-vfs PRIVATE - crc32 + hash-library::hash-library json::json time ) diff --git a/module-utils/Utils.cpp b/module-utils/Utils.cpp index 8007ceee98b28d89469bfa0cf80d5659da28aec2..a050a5ce8738a7e9b862d8fa5f415f4b09a23cbd 100644 --- a/module-utils/Utils.cpp +++ b/module-utils/Utils.cpp @@ -19,17 +19,17 @@ namespace utils::filesystem { auto buf = std::make_unique(crc_buf_len); - unsigned long crc32 = 0; + CRC32 digestCrc32; while (!std::feof(file)) { size_t dataLen = std::fread(buf.get(), 1, crc_buf_len, file); if (dataLen == 0) { - return crc32; + break; } - crc32 = Crc32_ComputeBuf(crc32, buf.get(), dataLen); + digestCrc32.add(buf.get(), dataLen); } - return crc32; + return digestCrc32.getHashValue(); } std::string generateRandomId(std::size_t length) noexcept diff --git a/module-utils/bootconfig/CMakeLists.txt b/module-utils/bootconfig/CMakeLists.txt index f6e8e55decc0e8af86108863c9a51805400175a5..e89fcdf6e51cbd71e29af3b2116f4634221f2f1a 100644 --- a/module-utils/bootconfig/CMakeLists.txt +++ b/module-utils/bootconfig/CMakeLists.txt @@ -18,7 +18,7 @@ target_link_libraries( ${PROJECT_NAME} PRIVATE module-utils module-vfs - crc32 + hash-library::hash-library Microsoft.GSL::GSL json::json ) diff --git a/module-utils/bootconfig/src/bootconfig.cpp b/module-utils/bootconfig/src/bootconfig.cpp index 3fb2651c9bcefeff1e39f3bb2c31583edbdbb12f..c9021f1da6df29f72fabbfbe63baf18a03de677e 100644 --- a/module-utils/bootconfig/src/bootconfig.cpp +++ b/module-utils/bootconfig/src/bootconfig.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index cbbc27fa70663bbef5ba1a8731749b3a6ea66bde..b7499e1e5440bb21d1305020a78603bb4ccd4472 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -1,4 +1,3 @@ -add_subdirectory(crc32) add_subdirectory(magic_enum) add_subdirectory(microtar) add_subdirectory(date) @@ -20,3 +19,4 @@ if (${PROJECT_TARGET} STREQUAL "TARGET_RT1051") endif() add_subdirectory(fatfs) add_subdirectory(base64) +add_subdirectory(hash-library) diff --git a/third-party/crc32/CMakeLists.txt b/third-party/crc32/CMakeLists.txt deleted file mode 100644 index 1eb7432e4fcb82acf8b2f4593c7f7e2ba753333c..0000000000000000000000000000000000000000 --- a/third-party/crc32/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ - -add_library(crc32) -target_sources(crc32 - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src/crc32.c - ${CMAKE_CURRENT_SOURCE_DIR}/src/crc32.h -) - -target_include_directories(crc32 - PUBLIC - $ -) - diff --git a/third-party/crc32/src b/third-party/crc32/src deleted file mode 160000 index f38cbc9267a4cc24aeb92c04452d9ade803d168c..0000000000000000000000000000000000000000 --- a/third-party/crc32/src +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f38cbc9267a4cc24aeb92c04452d9ade803d168c diff --git a/third-party/hash-library/CMakeLists.txt b/third-party/hash-library/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f24c5ae5883bccd82091c96c2773bf86a9cd759a --- /dev/null +++ b/third-party/hash-library/CMakeLists.txt @@ -0,0 +1,27 @@ + +add_library(hash-library) +add_library(hash-library::hash-library ALIAS hash-library) +target_sources(hash-library + PRIVATE + src/crc32.cpp + src/keccak.cpp + src/md5.cpp + src/sha1.cpp + src/sha256.cpp + src/sha3.cpp + + PUBLIC + src/crc32.h + src/hmac.h + src/keccak.h + src/md5.h + src/sha1.h + src/sha256.h + src/sha3.h + ) + +target_include_directories(hash-library + PUBLIC + $ +) + diff --git a/third-party/hash-library/src b/third-party/hash-library/src new file mode 160000 index 0000000000000000000000000000000000000000..9b1d3f3fbb22bbbe0c8b2efceac8e41fb4c7957b --- /dev/null +++ b/third-party/hash-library/src @@ -0,0 +1 @@ +Subproject commit 9b1d3f3fbb22bbbe0c8b2efceac8e41fb4c7957b