diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 4fbb533f8d7da0bdce181f048d715e3dbc26601f..dcfcd4c3d9ae8faba4e9e87aff69cfc397b3f680 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -47,7 +47,8 @@ target_link_libraries (ctacommon castorcommon pthread ${SQLITE3_LIBRARY_RELEASE} - uuid) + uuid + z) set (COMMON_UNIT_TESTS_LIB_SRC_FILES checksum/ByteArrayTest.cpp diff --git a/common/checksum/ByteArray.cpp b/common/checksum/ByteArray.cpp index 69562f7e2191ae3fd130fd435c21e6174689af19..745182599125f9a1c03cf14d518e3e279f88098c 100644 --- a/common/checksum/ByteArray.cpp +++ b/common/checksum/ByteArray.cpp @@ -19,6 +19,7 @@ #include "common/checksum/ByteArray.hpp" #include <ostream> +#include <zlib.h> //------------------------------------------------------------------------------ // constructor @@ -129,6 +130,14 @@ 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 c297323a3c81dae7fb78d618e5171f139c3f8804..4dfd3aa3613e3aeffdd3c8a3a79c4a407152d736 100644 --- a/common/checksum/ByteArray.hpp +++ b/common/checksum/ByteArray.hpp @@ -118,6 +118,11 @@ 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 c84949be5813c60b3d96bd9503225826731741a3..f898130e1e4db54fd19d4f6723d69d79a782d37a 100644 --- a/common/checksum/ByteArrayTest.cpp +++ b/common/checksum/ByteArrayTest.cpp @@ -206,4 +206,17 @@ 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