From 2ff50750f507a9b74a6b1cf2678a0bcd5faed540 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Thu, 16 Sep 2021 11:34:16 +0200 Subject: [PATCH] moved md5 to slsSupportlib, added md5_helper.c --- slsDetectorSoftware/CMakeLists.txt | 3 -- slsDetectorSoftware/src/Module.cpp | 18 ++---------- slsDetectorSoftware/src/Module.h | 1 - slsSupportLib/CMakeLists.txt | 4 +++ .../{opensslMd5 => include/sls}/md5.h | 9 ++++++ slsSupportLib/include/sls/md5_helper.h | 9 ++++++ slsSupportLib/{opensslMd5 => src}/md5.c | 20 +++++++++---- slsSupportLib/src/md5_helper.cpp | 28 +++++++++++++++++++ 8 files changed, 67 insertions(+), 25 deletions(-) rename slsSupportLib/{opensslMd5 => include/sls}/md5.h (99%) create mode 100644 slsSupportLib/include/sls/md5_helper.h rename slsSupportLib/{opensslMd5 => src}/md5.c (98%) create mode 100644 slsSupportLib/src/md5_helper.cpp diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index f8085e81c..91697a939 100755 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -5,7 +5,6 @@ set(SOURCES src/CmdProxy.cpp src/CmdParser.cpp src/Pattern.cpp - ../slsSupportLib/opensslMd5/md5.c ) add_library(slsDetectorObject OBJECT @@ -14,7 +13,6 @@ add_library(slsDetectorObject OBJECT target_include_directories(slsDetectorObject PUBLIC "$" - "$" "$" ) @@ -36,7 +34,6 @@ set(PUBLICHEADERS include/sls/Detector.h include/sls/Result.h include/sls/Pattern.h - ../slsSupportLib/opensslMd5/md5.h ) #Shared library diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index b1e49ffc2..9ad65141a 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -1,11 +1,11 @@ #include "Module.h" #include "SharedMemory.h" -#include "md5.h" #include "sls/ClientSocket.h" #include "sls/ToString.h" #include "sls/bit_utils.h" #include "sls/container_utils.h" #include "sls/file_utils.h" +#include "sls/md5_helper.h" #include "sls/network_utils.h" #include "sls/sls_detector_exceptions.h" #include "sls/sls_detector_funcs.h" @@ -3406,18 +3406,6 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, return myMod; } -std::string Module::calculateChecksum(char *buffer, ssize_t bytes) { - MD5_CTX c; - MD5_Init(&c); - MD5_Update(&c, buffer, bytes); - unsigned char out[MD5_DIGEST_LENGTH]; - MD5_Final(out, &c); - std::ostringstream oss; - for (int i = 0; i != MD5_DIGEST_LENGTH; ++i) - oss << std::hex << std::setw(2) << std::setfill('0') << +out[i]; - return oss.str(); -} - void Module::programFPGAviaBlackfin(std::vector buffer) { // send program from memory to detector LOG(logINFO) << "Sending programming binary (from pof) to module " @@ -3428,7 +3416,7 @@ void Module::programFPGAviaBlackfin(std::vector buffer) { client.Send(filesize); // checksum - std::string checksum = calculateChecksum(buffer.data(), filesize); + std::string checksum = sls::md5_calculate_checksum(buffer.data(), filesize); LOG(logDEBUG1) << "Checksum:" << checksum; char cChecksum[MAX_STR_LENGTH]; memset(cChecksum, 0, MAX_STR_LENGTH); @@ -3544,7 +3532,7 @@ void Module::programFPGAviaNios(std::vector buffer) { client.Send(filesize); // checksum - std::string checksum = calculateChecksum(buffer.data(), filesize); + std::string checksum = sls::md5_calculate_checksum(buffer.data(), filesize); LOG(logDEBUG1) << "Checksum:" << checksum; char cChecksum[MAX_STR_LENGTH]; memset(cChecksum, 0, MAX_STR_LENGTH); diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index b6b1439d1..89b02d0ac 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -743,7 +743,6 @@ class Module : public virtual slsDetectorDefs { std::string getTrimbitFilename(detectorSettings settings, int e_eV); sls_detector_module readSettingsFile(const std::string &fname, bool trimbits = true); - std::string calculateChecksum(char *buffer, ssize_t bytes); void programFPGAviaBlackfin(std::vector buffer); void programFPGAviaNios(std::vector buffer); diff --git a/slsSupportLib/CMakeLists.txt b/slsSupportLib/CMakeLists.txt index cb35702bb..f97e864f5 100755 --- a/slsSupportLib/CMakeLists.txt +++ b/slsSupportLib/CMakeLists.txt @@ -11,6 +11,8 @@ set(SOURCES src/UdpRxSocket.cpp src/sls_detector_exceptions.cpp # src/sls_detector_defs.cpp + src/md5.c + src/md5_helper.cpp ) # Header files to install as a part of the library @@ -44,6 +46,8 @@ if(SLS_DEVEL_HEADERS) include/sls/versionAPI.h include/sls/ZmqSocket.h include/sls/bit_utils.h + include/sls/mdf5.h + include/sls/md5_helper.h ) endif() diff --git a/slsSupportLib/opensslMd5/md5.h b/slsSupportLib/include/sls/md5.h similarity index 99% rename from slsSupportLib/opensslMd5/md5.h rename to slsSupportLib/include/sls/md5.h index a08ab4b5c..19223e20d 100644 --- a/slsSupportLib/opensslMd5/md5.h +++ b/slsSupportLib/include/sls/md5.h @@ -222,7 +222,13 @@ # include + # ifdef __cplusplus +/* + * Modifications 2021 Paul Scherrer Institut + * namespace sls added + */ +namespace sls { extern "C" { # endif @@ -372,5 +378,8 @@ int MD5_Final(unsigned char *md, MD5_CTX *c); void md5_block_data_order(MD5_CTX *c, const void *p, size_t num); # ifdef __cplusplus } + +} // namespace sls # endif # endif + diff --git a/slsSupportLib/include/sls/md5_helper.h b/slsSupportLib/include/sls/md5_helper.h new file mode 100644 index 000000000..6e461e584 --- /dev/null +++ b/slsSupportLib/include/sls/md5_helper.h @@ -0,0 +1,9 @@ +#pragma once + +#include "sls/md5.h" + +#include + +namespace sls { +std::string md5_calculate_checksum(char *buffer, ssize_t bytes); +} // namespace sls \ No newline at end of file diff --git a/slsSupportLib/opensslMd5/md5.c b/slsSupportLib/src/md5.c similarity index 98% rename from slsSupportLib/opensslMd5/md5.c rename to slsSupportLib/src/md5.c index d6bc36cc6..7a7133fe6 100644 --- a/slsSupportLib/opensslMd5/md5.c +++ b/slsSupportLib/src/md5.c @@ -224,13 +224,17 @@ #include -/** - * Modification 2021 Paul Scherrer Institut - * Header included was md5_local.h - * and included string.h header +/** + * Modification 2021 Paul Scherrer Institut + * Header included was md5_local.h + * and string.h header was included + * sls namespace added */ +#include "sls/md5.h" #include -#include "md5.h" +#ifdef __cplusplus +namespace sls { +#endif /** * Modification 2021 Paul Scherrer Institut @@ -504,4 +508,8 @@ int HASH_FINAL(unsigned char *md, HASH_CTX *c) HASH_MAKE_STRING(c, md); return 1; -} \ No newline at end of file +} + +#ifdef __cplusplus +} // namespace sls +#endif \ No newline at end of file diff --git a/slsSupportLib/src/md5_helper.cpp b/slsSupportLib/src/md5_helper.cpp new file mode 100644 index 000000000..af9d66a87 --- /dev/null +++ b/slsSupportLib/src/md5_helper.cpp @@ -0,0 +1,28 @@ +#include "sls/md5_helper.h" + +#include +#include +#include + +namespace sls { + +std::string md5_calculate_checksum(char *buffer, ssize_t bytes) { + MD5_CTX c; + if (!MD5_Init(&c)) { + throw std::runtime_error( + "Could not calculate md5 checksum.[initializing]"); + } + if (!MD5_Update(&c, buffer, bytes)) { + throw std::runtime_error("Could not calculate md5 checksum.[Updating]"); + } + unsigned char out[MD5_DIGEST_LENGTH]; + if (!MD5_Final(out, &c)) { + throw std::runtime_error("Could not calculate md5 checksum.[Final]"); + } + std::ostringstream oss; + for (int i = 0; i != MD5_DIGEST_LENGTH; ++i) + oss << std::hex << std::setw(2) << std::setfill('0') << +out[i]; + return oss.str(); +} + +} // namespace sls