diff --git a/common/Utils.cpp b/common/Utils.cpp index ba6368a5a65e63d1780663116be68d74836c76b3..e1536439894e3ca5ec4bdecb849d9f25a1f2e339 100644 --- a/common/Utils.cpp +++ b/common/Utils.cpp @@ -29,6 +29,7 @@ #include <strings.h> #include <sys/types.h> #include <uuid/uuid.h> +#include <zlib.h> using cta::exception::Exception; @@ -472,3 +473,12 @@ bool cta::Utils::isValidUInt(const std::string &str) return true; } + +//------------------------------------------------------------------------------ +// getAdler32 +//------------------------------------------------------------------------------ +uint32_t cta::Utils::getAdler32(const char *buf, const uint32_t len) + throw() { + const uint32_t checksum = adler32(0L, Z_NULL, 0); + return adler32(checksum, (const Bytef*)buf, len); +} diff --git a/common/Utils.hpp b/common/Utils.hpp index 7d3bade4af8d20dd556c65ebd5d71050829dfc7a..b5c1504f46e675cef09311680c6be66b2e9f936a 100644 --- a/common/Utils.hpp +++ b/common/Utils.hpp @@ -20,6 +20,7 @@ #include <list> #include <sstream> +#include <stdint.h> #include <string> #include <unistd.h> #include <vector> @@ -207,6 +208,15 @@ public: */ static bool isValidUInt(const std::string &str) throw(); + /** + * Returns the alder32 checksum of the specified buffer. + * + * @param buf The buffer. + * @param len The length of the buffer in bytes. + * @return the alder32 checksum of the specified buffer. + */ + static uint32_t getAdler32(const char *buf, const uint32_t len) throw(); + }; // class Utils } // namespace cta diff --git a/common/UtilsTest.cpp b/common/UtilsTest.cpp index f1f8bb718492fa21e95fff3bd63986c9f0963696..44e027e2fc5c7d5815ea6e9d6e4ff1dc8d79512d 100644 --- a/common/UtilsTest.cpp +++ b/common/UtilsTest.cpp @@ -452,4 +452,19 @@ TEST_F(cta_UtilsTest, isValidUInt_not_a_number) { ASSERT_FALSE(Utils::isValidUInt("one")); } +TEST_F(cta_UtilsTest, adler32_empty_buf) { + using namespace cta; + + // The adler32 of an empty buffer is 1 + ASSERT_EQ((uint32_t)1, Utils::getAdler32(NULL, 0)); +} + +TEST_F(cta_UtilsTest, adler32_buf_of_character_1) { + using namespace cta; + + const char buf = '1'; + ASSERT_EQ((uint32_t)0x320032, Utils::getAdler32(&buf, 1)); +} + + } // namespace unitTests diff --git a/common/checksum/ByteArray.cpp b/common/checksum/ByteArray.cpp index 745182599125f9a1c03cf14d518e3e279f88098c..69562f7e2191ae3fd130fd435c21e6174689af19 100644 --- a/common/checksum/ByteArray.cpp +++ b/common/checksum/ByteArray.cpp @@ -19,7 +19,6 @@ #include "common/checksum/ByteArray.hpp" #include <ostream> -#include <zlib.h> //------------------------------------------------------------------------------ // constructor @@ -130,14 +129,6 @@ const uint8_t *cta::ByteArray::getBytes() const throw() { return m_bytes; } -//------------------------------------------------------------------------------ -// getAdler32 -//------------------------------------------------------------------------------ -uint32_t cta::ByteArray::getAdler32() const { - const uint32_t checksum = adler32(0L, Z_NULL, 0); - return adler32(checksum, m_bytes, m_size); -} - //------------------------------------------------------------------------------ // operator<< //------------------------------------------------------------------------------ diff --git a/common/checksum/ByteArray.hpp b/common/checksum/ByteArray.hpp index 4dfd3aa3613e3aeffdd3c8a3a79c4a407152d736..c297323a3c81dae7fb78d618e5171f139c3f8804 100644 --- a/common/checksum/ByteArray.hpp +++ b/common/checksum/ByteArray.hpp @@ -118,11 +118,6 @@ public: */ const uint8_t *getBytes() const throw(); - /** - * Returns the adler 32 checksum of the array of bytes. - */ - uint32_t getAdler32() const; - private: /** diff --git a/common/checksum/ByteArrayTest.cpp b/common/checksum/ByteArrayTest.cpp index f898130e1e4db54fd19d4f6723d69d79a782d37a..c84949be5813c60b3d96bd9503225826731741a3 100644 --- a/common/checksum/ByteArrayTest.cpp +++ b/common/checksum/ByteArrayTest.cpp @@ -206,17 +206,4 @@ TEST_F(cta_ByteArrayTest, equality_operator_ne) { ASSERT_NE(byteArray1, byteArray2); } -TEST_F(cta_ByteArrayTest, adler32_empty_array) { - // The adler32 of an empty buffer is 1 - cta::ByteArray byteArray; - ASSERT_EQ((uint32_t)1, byteArray.getAdler32()); -} - -TEST_F(cta_ByteArrayTest, adler32_array_of_character_1) { - // The adler32 of an empty buffer is 1 - const uint8_t c = '1'; - cta::ByteArray byteArray(1, &c); - ASSERT_EQ((uint32_t)0x320032, byteArray.getAdler32()); -} - } // namespace unitTests