diff options
Diffstat (limited to 'src/lib/hash/checksum/adler32')
-rw-r--r-- | src/lib/hash/checksum/adler32/adler32.cpp | 83 | ||||
-rw-r--r-- | src/lib/hash/checksum/adler32/adler32.h | 37 | ||||
-rw-r--r-- | src/lib/hash/checksum/adler32/info.txt | 1 |
3 files changed, 121 insertions, 0 deletions
diff --git a/src/lib/hash/checksum/adler32/adler32.cpp b/src/lib/hash/checksum/adler32/adler32.cpp new file mode 100644 index 000000000..f2385c5b8 --- /dev/null +++ b/src/lib/hash/checksum/adler32/adler32.cpp @@ -0,0 +1,83 @@ +/* +* Adler32 +* (C) 1999-2007 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/internal/hash_utils.h> +#include <botan/adler32.h> + +namespace Botan { + +BOTAN_REGISTER_HASH_NOARGS(Adler32); + +namespace { + +void adler32_update(const byte input[], size_t length, + u16bit& S1, u16bit& S2) + { + u32bit S1x = S1; + u32bit S2x = S2; + + while(length >= 16) + { + S1x += input[ 0]; S2x += S1x; + S1x += input[ 1]; S2x += S1x; + S1x += input[ 2]; S2x += S1x; + S1x += input[ 3]; S2x += S1x; + S1x += input[ 4]; S2x += S1x; + S1x += input[ 5]; S2x += S1x; + S1x += input[ 6]; S2x += S1x; + S1x += input[ 7]; S2x += S1x; + S1x += input[ 8]; S2x += S1x; + S1x += input[ 9]; S2x += S1x; + S1x += input[10]; S2x += S1x; + S1x += input[11]; S2x += S1x; + S1x += input[12]; S2x += S1x; + S1x += input[13]; S2x += S1x; + S1x += input[14]; S2x += S1x; + S1x += input[15]; S2x += S1x; + input += 16; + length -= 16; + } + + for(size_t j = 0; j != length; ++j) + { + S1x += input[j]; + S2x += S1x; + } + + S1 = S1x % 65521; + S2 = S2x % 65521; + } + +} + +/* +* Update an Adler32 Checksum +*/ +void Adler32::add_data(const byte input[], size_t length) + { + const size_t PROCESS_AMOUNT = 5552; + + while(length >= PROCESS_AMOUNT) + { + adler32_update(input, PROCESS_AMOUNT, S1, S2); + input += PROCESS_AMOUNT; + length -= PROCESS_AMOUNT; + } + + adler32_update(input, length, S1, S2); + } + +/* +* Finalize an Adler32 Checksum +*/ +void Adler32::final_result(byte output[]) + { + store_be(output, S2, S1); + clear(); + } + +} diff --git a/src/lib/hash/checksum/adler32/adler32.h b/src/lib/hash/checksum/adler32/adler32.h new file mode 100644 index 000000000..f3767b786 --- /dev/null +++ b/src/lib/hash/checksum/adler32/adler32.h @@ -0,0 +1,37 @@ +/* +* Adler32 +* (C) 1999-2007 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_ADLER32_H__ +#define BOTAN_ADLER32_H__ + +#include <botan/hash.h> + +namespace Botan { + +/** +* The Adler32 checksum, used in zlib +*/ +class BOTAN_DLL Adler32 : public HashFunction + { + public: + std::string name() const { return "Adler32"; } + size_t output_length() const { return 4; } + HashFunction* clone() const { return new Adler32; } + + void clear() { S1 = 1; S2 = 0; } + + Adler32() { clear(); } + ~Adler32() { clear(); } + private: + void add_data(const byte[], size_t); + void final_result(byte[]); + u16bit S1, S2; + }; + +} + +#endif diff --git a/src/lib/hash/checksum/adler32/info.txt b/src/lib/hash/checksum/adler32/info.txt new file mode 100644 index 000000000..26ca8b255 --- /dev/null +++ b/src/lib/hash/checksum/adler32/info.txt @@ -0,0 +1 @@ +define ADLER32 20131128 |