GCC Code Coverage Report


Directory: src/gate/
File: src/gate/encode/cxx_crchash.cpp
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 62 99 62.6%
Functions: 19 31 61.3%
Branches: 21 66 31.8%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/encode/crchash.hpp"
29 #include "gate/results.hpp"
30 #include "gate/exceptions.hpp"
31 #include "gate/arrays.hpp"
32
33 namespace gate
34 {
35 namespace enc
36 {
37
38 1 Crc32Hash::Crc32Hash()
39 1 : impl(gate_crc32_t())
40 {
41 1 this->reset();
42 1 }
43 1 Crc32Hash::~Crc32Hash() noexcept
44 {
45 1 }
46 4 void Crc32Hash::reset()
47 {
48 4 gate_crc32_init(&this->impl);
49 4 }
50
51 2 void Crc32Hash::update(void const* databuffer, gate_size_t databufferlength)
52 {
53 2 gate_crc32_update(&this->impl, databuffer, (unsigned)databufferlength);
54 2 }
55 2 void Crc32Hash::update(Stream& stream)
56 {
57 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
58 gate_size_t returned;
59
3/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
2 while (0 != (returned = stream.read(buffer, sizeof(buffer))))
60 {
61
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_crc32_update(&this->impl, buffer, (unsigned)returned);
62 }
63 1 }
64 1 void Crc32Hash::update(String const& text)
65 {
66 1 gate_crc32_update(&this->impl, text.c_str(), (unsigned)text.length());
67 1 }
68
69 3 void Crc32Hash::computeResult(crc32_result_t& result)
70 {
71 3 gate_crc32_finish(&this->impl, &result);
72 3 }
73 3 String Crc32Hash::computeResult()
74 {
75 crc32_result_t result;
76
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 this->computeResult(result);
77
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 return String((char const*)&result.hash[0], sizeof(result.hash));
78 }
79
80 String Crc32Hash::compute(Stream& stream)
81 {
82 Crc32Hash hash;
83 hash.update(stream);
84 return hash.computeResult();
85 }
86 String Crc32Hash::compute(String& text)
87 {
88 Crc32Hash hash;
89 hash.update(text);
90 return hash.computeResult();
91 }
92 String Crc32Hash::compute(void const* databuffer, gate_size_t databufferlength)
93 {
94 Crc32Hash hash;
95 hash.update(databuffer, databufferlength);
96 return hash.computeResult();
97 }
98
99
100
101 Crc32HashBuilder::~Crc32HashBuilder() noexcept
102 {
103 }
104 void Crc32HashBuilder::reset()
105 {
106 this->impl.reset();
107 }
108 void Crc32HashBuilder::update(void const* data, size_t length)
109 {
110 this->impl.update(data, length);
111 }
112 Blob Crc32HashBuilder::computeHash()
113 {
114 Crc32Hash::crc32_result_t result;
115 this->impl.computeResult(result);
116 return Blob(&result.hash[0], sizeof(result.hash));
117 }
118
119
120
121
122
123 8 Crc16Hash::Crc16Hash(Crc16TypeEnum crc16_type)
124 8 : impl(gate_crc16_t()), crc_type(crc16_type)
125 {
126 8 this->reset();
127 8 }
128 8 Crc16Hash::~Crc16Hash()
129 {
130 8 }
131 23 void Crc16Hash::reset()
132 {
133 23 gate_crc16_init(&this->impl, (unsigned)this->crc_type);
134 23 }
135
136 11 void Crc16Hash::update(void const* databuffer, gate_size_t databufferlength)
137 {
138 11 gate_crc16_update(&this->impl, databuffer, (unsigned)databufferlength);
139 11 }
140 12 void Crc16Hash::update(Stream& stream)
141 {
142 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
143 gate_size_t returned;
144
3/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
12 while (0 != (returned = stream.read(buffer, sizeof(buffer))))
145 {
146
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 gate_crc16_update(&this->impl, buffer, (unsigned)returned);
147 }
148 6 }
149 6 void Crc16Hash::update(String const& text)
150 {
151 6 gate_crc16_update(&this->impl, text.c_str(), (unsigned)text.length());
152 6 }
153
154 18 void Crc16Hash::computeResult(crc16_result_t& result)
155 {
156 18 gate_crc16_finish(&this->impl, &result);
157 18 }
158 18 String Crc16Hash::computeResult()
159 {
160 crc16_result_t result;
161
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 this->computeResult(result);
162
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
36 return String((char const*)&result.hash[0], sizeof(result.hash));
163 }
164
165 1 String Crc16Hash::compute(Stream& stream, Crc16TypeEnum crc16_type)
166 {
167
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Crc16Hash hash(crc16_type);
168
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 hash.update(stream);
169
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return hash.computeResult();
170 }
171 1 String Crc16Hash::compute(String const& text, Crc16TypeEnum crc16_type)
172 {
173
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Crc16Hash hash(crc16_type);
174
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 hash.update(text);
175
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return hash.computeResult();
176 }
177 1 String Crc16Hash::compute(void const* databuffer, gate_size_t databufferlength, Crc16TypeEnum crc16_type)
178 {
179
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Crc16Hash hash(crc16_type);
180
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 hash.update(databuffer, databufferlength);
181
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return hash.computeResult();
182 }
183
184
185
186 Crc16HashBuilder::Crc16HashBuilder(Crc16Hash::Crc16TypeEnum crc16Type)
187 : impl(crc16Type)
188 {
189 }
190 Crc16HashBuilder::~Crc16HashBuilder() noexcept
191 {
192 }
193
194 void Crc16HashBuilder::reset()
195 {
196 this->impl.reset();
197 }
198 void Crc16HashBuilder::update(void const* data, size_t length)
199 {
200 this->impl.update(data, length);
201 }
202 Blob Crc16HashBuilder::computeHash()
203 {
204 Crc16Hash::crc16_result_t result;
205 this->impl.computeResult(result);
206 return Blob(&result.hash[0], sizeof(result.hash));
207 }
208
209
210 } // end of namespace enc
211 } // end of namespace gate
212