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