Botan  2.8.0
Crypto and TLS for C++11
adler32.cpp
Go to the documentation of this file.
1 /*
2 * Adler32
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/adler32.h>
9 #include <botan/loadstor.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 void adler32_update(const uint8_t input[], size_t length,
16  uint16_t& S1, uint16_t& S2)
17  {
18  uint32_t S1x = S1;
19  uint32_t S2x = S2;
20 
21  while(length >= 16)
22  {
23  S1x += input[ 0]; S2x += S1x;
24  S1x += input[ 1]; S2x += S1x;
25  S1x += input[ 2]; S2x += S1x;
26  S1x += input[ 3]; S2x += S1x;
27  S1x += input[ 4]; S2x += S1x;
28  S1x += input[ 5]; S2x += S1x;
29  S1x += input[ 6]; S2x += S1x;
30  S1x += input[ 7]; S2x += S1x;
31  S1x += input[ 8]; S2x += S1x;
32  S1x += input[ 9]; S2x += S1x;
33  S1x += input[10]; S2x += S1x;
34  S1x += input[11]; S2x += S1x;
35  S1x += input[12]; S2x += S1x;
36  S1x += input[13]; S2x += S1x;
37  S1x += input[14]; S2x += S1x;
38  S1x += input[15]; S2x += S1x;
39  input += 16;
40  length -= 16;
41  }
42 
43  for(size_t j = 0; j != length; ++j)
44  {
45  S1x += input[j];
46  S2x += S1x;
47  }
48 
49  S1 = S1x % 65521;
50  S2 = S2x % 65521;
51  }
52 
53 }
54 
55 /*
56 * Update an Adler32 Checksum
57 */
58 void Adler32::add_data(const uint8_t input[], size_t length)
59  {
60  const size_t PROCESS_AMOUNT = 5552;
61 
62  while(length >= PROCESS_AMOUNT)
63  {
64  adler32_update(input, PROCESS_AMOUNT, m_S1, m_S2);
65  input += PROCESS_AMOUNT;
66  length -= PROCESS_AMOUNT;
67  }
68 
69  adler32_update(input, length, m_S1, m_S2);
70  }
71 
72 /*
73 * Finalize an Adler32 Checksum
74 */
75 void Adler32::final_result(uint8_t output[])
76  {
77  store_be(output, m_S2, m_S1);
78  clear();
79  }
80 
81 std::unique_ptr<HashFunction> Adler32::copy_state() const
82  {
83  return std::unique_ptr<HashFunction>(new Adler32(*this));
84  }
85 
86 }
void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:434
void clear() override
Definition: adler32.h:26
Definition: alg_id.cpp:13
std::unique_ptr< HashFunction > copy_state() const override
Definition: adler32.cpp:81