/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(std::span<const uint8_t> input) { | 
72  | 0  |    const size_t PROCESS_AMOUNT = 5552;  | 
73  |  | 
  | 
74  | 0  |    while(input.size() >= PROCESS_AMOUNT) { | 
75  | 0  |       adler32_update(input.data(), PROCESS_AMOUNT, m_S1, m_S2);  | 
76  | 0  |       input = input.last(input.size() - PROCESS_AMOUNT);  | 
77  | 0  |    }  | 
78  |  | 
  | 
79  | 0  |    adler32_update(input.data(), input.size(), m_S1, m_S2);  | 
80  | 0  | }  | 
81  |  |  | 
82  |  | /*  | 
83  |  | * Finalize an Adler32 Checksum  | 
84  |  | */  | 
85  | 0  | void Adler32::final_result(std::span<uint8_t> output) { | 
86  | 0  |    store_be(output.data(), m_S2, m_S1);  | 
87  | 0  |    clear();  | 
88  | 0  | }  | 
89  |  |  | 
90  | 0  | std::unique_ptr<HashFunction> Adler32::copy_state() const { | 
91  | 0  |    return std::make_unique<Adler32>(*this);  | 
92  | 0  | }  | 
93  |  |  | 
94  |  | }  // namespace Botan  |