| 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 | 5 | Crc32Hash::Crc32Hash() | |
| 39 | 5 | : impl(gate_crc32_t()) | |
| 40 | { | ||
| 41 | 5 | this->reset(); | |
| 42 | 5 | } | |
| 43 | 5 | Crc32Hash::~Crc32Hash() noexcept | |
| 44 | { | ||
| 45 | 5 | } | |
| 46 | 12 | void Crc32Hash::reset() | |
| 47 | { | ||
| 48 | 12 | gate_crc32_init(&this->impl); | |
| 49 | 12 | } | |
| 50 | |||
| 51 | 7 | void Crc32Hash::update(void const* databuffer, gate_size_t databufferlength) | |
| 52 | { | ||
| 53 | 7 | gate_crc32_update(&this->impl, databuffer, (unsigned)databufferlength); | |
| 54 | 7 | } | |
| 55 | 4 | void Crc32Hash::update(Stream& stream) | |
| 56 | { | ||
| 57 | char buffer[GATE_MAX_COPYBUFFER_LENGTH]; | ||
| 58 | gate_size_t returned; | ||
| 59 |
3/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | while (0 != (returned = stream.read(buffer, sizeof(buffer)))) |
| 60 | { | ||
| 61 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_crc32_update(&this->impl, buffer, (unsigned)returned); |
| 62 | } | ||
| 63 | 2 | } | |
| 64 | 2 | void Crc32Hash::update(String const& text) | |
| 65 | { | ||
| 66 | 2 | gate_crc32_update(&this->impl, text.c_str(), (unsigned)text.length()); | |
| 67 | 2 | } | |
| 68 | |||
| 69 | 10 | void Crc32Hash::computeResult(crc32_result_t& result) | |
| 70 | { | ||
| 71 | 10 | gate_crc32_finish(&this->impl, &result); | |
| 72 | 10 | } | |
| 73 | 6 | String Crc32Hash::computeResult() | |
| 74 | { | ||
| 75 | crc32_result_t result; | ||
| 76 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | this->computeResult(result); |
| 77 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
12 | return String((char const*)&result.hash[0], sizeof(result.hash)); |
| 78 | } | ||
| 79 | |||
| 80 | 1 | String Crc32Hash::compute(Stream& stream) | |
| 81 | { | ||
| 82 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | Crc32Hash hash; |
| 83 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | hash.update(stream); |
| 84 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return hash.computeResult(); |
| 85 | } | ||
| 86 | 1 | String Crc32Hash::compute(String const& text) | |
| 87 | { | ||
| 88 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | Crc32Hash hash; |
| 89 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | hash.update(text); |
| 90 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return hash.computeResult(); |
| 91 | } | ||
| 92 | 1 | String Crc32Hash::compute(void const* databuffer, gate_size_t databufferlength) | |
| 93 | { | ||
| 94 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | Crc32Hash hash; |
| 95 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | hash.update(databuffer, databufferlength); |
| 96 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return hash.computeResult(); |
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | 2 | Crc32HashBuilder::~Crc32HashBuilder() noexcept | |
| 102 | { | ||
| 103 | 2 | } | |
| 104 | 4 | void Crc32HashBuilder::reset() | |
| 105 | { | ||
| 106 | 4 | this->impl.reset(); | |
| 107 | 4 | } | |
| 108 | 4 | void Crc32HashBuilder::update(void const* data, size_t length) | |
| 109 | { | ||
| 110 | 4 | this->impl.update(data, length); | |
| 111 | 4 | } | |
| 112 | 4 | Blob Crc32HashBuilder::computeHash() | |
| 113 | { | ||
| 114 | Crc32Hash::crc32_result_t result; | ||
| 115 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | this->impl.computeResult(result); |
| 116 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | return Blob(&result.hash[0], sizeof(result.hash)); |
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | 9 | Crc16Hash::Crc16Hash(Crc16TypeEnum crc16_type) | |
| 124 | 9 | : impl(gate_crc16_t()), crc_type(crc16_type) | |
| 125 | { | ||
| 126 | 9 | this->reset(); | |
| 127 | 9 | } | |
| 128 | 9 | Crc16Hash::~Crc16Hash() | |
| 129 | { | ||
| 130 | 9 | } | |
| 131 | 28 | void Crc16Hash::reset() | |
| 132 | { | ||
| 133 | 28 | gate_crc16_init(&this->impl, (unsigned)this->crc_type); | |
| 134 | 28 | } | |
| 135 | |||
| 136 | 15 | void Crc16Hash::update(void const* databuffer, gate_size_t databufferlength) | |
| 137 | { | ||
| 138 | 15 | gate_crc16_update(&this->impl, databuffer, (unsigned)databufferlength); | |
| 139 | 15 | } | |
| 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 | 22 | void Crc16Hash::computeResult(crc16_result_t& result) | |
| 155 | { | ||
| 156 | 22 | gate_crc16_finish(&this->impl, &result); | |
| 157 | 22 | } | |
| 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 | 1 | Crc16HashBuilder::Crc16HashBuilder(Crc16Hash::Crc16TypeEnum crc16Type) | |
| 187 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | : impl(crc16Type) |
| 188 | { | ||
| 189 | 1 | } | |
| 190 | 2 | Crc16HashBuilder::~Crc16HashBuilder() noexcept | |
| 191 | { | ||
| 192 | 2 | } | |
| 193 | |||
| 194 | 4 | void Crc16HashBuilder::reset() | |
| 195 | { | ||
| 196 | 4 | this->impl.reset(); | |
| 197 | 4 | } | |
| 198 | 4 | void Crc16HashBuilder::update(void const* data, size_t length) | |
| 199 | { | ||
| 200 | 4 | this->impl.update(data, length); | |
| 201 | 4 | } | |
| 202 | 4 | Blob Crc16HashBuilder::computeHash() | |
| 203 | { | ||
| 204 | Crc16Hash::crc16_result_t result; | ||
| 205 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | this->impl.computeResult(result); |
| 206 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | return Blob(&result.hash[0], sizeof(result.hash)); |
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | } // end of namespace enc | ||
| 211 | } // end of namespace gate | ||
| 212 |