/src/botan/src/lib/hash/checksum/adler32/adler32.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
18 | 0 | uint32_t S1x = S1; |
19 | 0 | uint32_t S2x = S2; |
20 | 0 |
|
21 | 0 | while(length >= 16) |
22 | 0 | { |
23 | 0 | S1x += input[ 0]; S2x += S1x; |
24 | 0 | S1x += input[ 1]; S2x += S1x; |
25 | 0 | S1x += input[ 2]; S2x += S1x; |
26 | 0 | S1x += input[ 3]; S2x += S1x; |
27 | 0 | S1x += input[ 4]; S2x += S1x; |
28 | 0 | S1x += input[ 5]; S2x += S1x; |
29 | 0 | S1x += input[ 6]; S2x += S1x; |
30 | 0 | S1x += input[ 7]; S2x += S1x; |
31 | 0 | S1x += input[ 8]; S2x += S1x; |
32 | 0 | S1x += input[ 9]; S2x += S1x; |
33 | 0 | S1x += input[10]; S2x += S1x; |
34 | 0 | S1x += input[11]; S2x += S1x; |
35 | 0 | S1x += input[12]; S2x += S1x; |
36 | 0 | S1x += input[13]; S2x += S1x; |
37 | 0 | S1x += input[14]; S2x += S1x; |
38 | 0 | S1x += input[15]; S2x += S1x; |
39 | 0 | input += 16; |
40 | 0 | length -= 16; |
41 | 0 | } |
42 | 0 |
|
43 | 0 | for(size_t j = 0; j != length; ++j) |
44 | 0 | { |
45 | 0 | S1x += input[j]; |
46 | 0 | S2x += S1x; |
47 | 0 | } |
48 | 0 |
|
49 | 0 | S1 = S1x % 65521; |
50 | 0 | S2 = S2x % 65521; |
51 | 0 | } |
52 | | |
53 | | } |
54 | | |
55 | | /* |
56 | | * Update an Adler32 Checksum |
57 | | */ |
58 | | void Adler32::add_data(const uint8_t input[], size_t length) |
59 | 0 | { |
60 | 0 | const size_t PROCESS_AMOUNT = 5552; |
61 | 0 |
|
62 | 0 | while(length >= PROCESS_AMOUNT) |
63 | 0 | { |
64 | 0 | adler32_update(input, PROCESS_AMOUNT, m_S1, m_S2); |
65 | 0 | input += PROCESS_AMOUNT; |
66 | 0 | length -= PROCESS_AMOUNT; |
67 | 0 | } |
68 | 0 |
|
69 | 0 | adler32_update(input, length, m_S1, m_S2); |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * Finalize an Adler32 Checksum |
74 | | */ |
75 | | void Adler32::final_result(uint8_t output[]) |
76 | 0 | { |
77 | 0 | store_be(output, m_S2, m_S1); |
78 | 0 | clear(); |
79 | 0 | } |
80 | | |
81 | | std::unique_ptr<HashFunction> Adler32::copy_state() const |
82 | 0 | { |
83 | 0 | return std::unique_ptr<HashFunction>(new Adler32(*this)); |
84 | 0 | } |
85 | | |
86 | | } |