comparison AES/Adler16.h @ 1:0b3630a29ad8

Initial version based on previous repository. Project was ported to QT6 and in now cmake based.
author Ideenmodellierer <tiefenrauscher@web.de>
date Thu, 27 Nov 2025 18:40:28 +0100
parents
children
comparison
equal deleted inserted replaced
0:76ccd6ce50c0 1:0b3630a29ad8
1 //////////////////////////////////////////////////////////////////////////////
2 /// \file Adler16.h
3 /// \brief Adler checksum, on 2x8 bits.
4 /// \author JD Gascuel.
5 /// \copyright (c) 2011-2016 JD Gascuel. All rights reserved.
6 /// $Id$
7 //////////////////////////////////////////////////////////////////////////////
8 //
9 // BSD 2-Clause License:
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions
13 // are met:
14 //
15 // 1. Redistributions of source code must retain the above copyright notice,
16 // this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 // THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 //////////////////////////////////////////////////////////////////////////////
35 // HISTORY
36 // 2015-07-31 Creation.
37 // 2016-05-24 jDG: BSD-2 version.
38
39 #ifndef ADLER16_H
40 #define ADLER16_H
41
42 #include <QByteArray>
43
44 //////////////////////////////////////////////////////////////////////////////
45 /// \brief Addler checksum, on 2xN bits.
46 template <typename T>
47 class AdlerTemplate
48 {
49 T _a, _b;
50
51 public:
52 //------------------------------------------------------------------------
53 /// \brief Initialize checksum with a given seed.
54 inline AdlerTemplate(T a, T b)
55 : _a(a), _b(b)
56 {}
57
58 //------------------------------------------------------------------------
59 /// \brief Re-initialize checksum with a given seed.
60 inline void reset(T a, T b)
61 { _a = a;
62 _b = b;
63 }
64
65 //------------------------------------------------------------------------
66 /// \brief Hash a single byte/word
67 inline void add(int c)
68 {
69 _a += (T)(c & 0xFF);
70 _b += _a;
71 }
72
73 //------------------------------------------------------------------------
74 /// \brief Hash a block of bytes.
75 /// \note \a size is always in BYTE units.
76 inline void add(const void* buffer, size_t size)
77 {
78 for(size_t i = 0; i<size; ++i)
79 add( ((const unsigned char*)buffer)[i] );
80 }
81
82 //------------------------------------------------------------------------
83 /// \brief Hash all the byte/words for a Qt buffer.
84 inline void add(const QByteArray& buffer)
85 {
86 add(buffer.data(), buffer.size());
87 }
88
89 //------------------------------------------------------------------------
90 /// \brief Returns hash result.
91 inline T a() const { return _a; }
92 inline T b() const { return _b; }
93
94 //------------------------------------------------------------------------
95 /// Check Hash result.
96 /// \returns TRUE if has do match the given values.
97 inline bool check(T a, T b)
98 {
99 return (_a == a) && (_b == b);
100 }
101 };
102
103 /// \brief 2x8 bits implementation.
104 typedef AdlerTemplate<unsigned char> Adler16;
105
106 /// \brief 2x16 bits implementation.
107 typedef AdlerTemplate<unsigned short> Adler32;
108
109 #endif // ADDLER16_H
110