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