Botan 1.10.17

adler32.cpp
Go to the documentation of this file.
1 /*
2 * Adler32
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
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 byte input[], size_t length,
16 u16bit& S1, u16bit& S2)
17 {
18 u32bit S1x = S1;
19 u32bit 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 byte 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, S1, S2);
65 input += PROCESS_AMOUNT;
66 length -= PROCESS_AMOUNT;
67 }
68
69 adler32_update(input, length, S1, S2);
70 }
71
72 /*
73 * Finalize an Adler32 Checksum
74 */
75 void Adler32::final_result(byte output[])
76 {
77 store_be(output, S2, S1);
78 clear();
79 }
80
81 }
Botan::Adler32::clear
void clear()
Definition: adler32.h:25
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u16bit
unsigned short u16bit
Definition: types.h:27
Botan
Definition: algo_base.h:14
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
Botan::u32bit
unsigned int u32bit
Definition: types.h:32