fix missing gitignore
[RRG-proxmark3.git] / common / crc32.c
blob17b12640932c1b6c2b656f3066e3ccba0eb37480
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 #include "crc32.h"
18 #define htole32(x) (x)
19 #define CRC32_PRESET 0xFFFFFFFF
21 static void crc32_byte(uint32_t *crc, const uint8_t value);
23 static void crc32_byte(uint32_t *crc, const uint8_t value) {
24 /* x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 */
25 const uint32_t poly = 0xEDB88320;
27 *crc ^= value;
28 for (int current_bit = 7; current_bit >= 0; current_bit--) {
29 int bit_out = (*crc) & 0x00000001;
30 *crc >>= 1;
31 if (bit_out)
32 *crc ^= poly;
36 void crc32_ex(const uint8_t *d, const size_t n, uint8_t *crc) {
37 uint32_t c = CRC32_PRESET;
38 for (size_t i = 0; i < n; i++) {
39 crc32_byte(&c, d[i]);
41 crc[0] = (uint8_t) c;
42 crc[1] = (uint8_t)(c >> 8);
43 crc[2] = (uint8_t)(c >> 16);
44 crc[3] = (uint8_t)(c >> 24);
48 void crc32_append(uint8_t *d, const size_t n) {
49 crc32_ex(d, n, d + n);