OpenOCD
crc32.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2013-2014 by Franck Jullien *
5  * elec4fun@gmail.com *
6  * *
7  * Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg *
8  * marian.buschsieweke@ovgu.de *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "crc32.h"
16 #include <stdint.h>
17 #include <stddef.h>
18 
19 static uint32_t crc_le_step(uint32_t poly, uint32_t crc, uint32_t data_in,
20  unsigned int data_bits)
21 {
22  for (unsigned int i = 0; i < data_bits; i++) {
23  uint32_t d, c;
24  d = ((data_in >> i) & 0x1) ? 0xffffffff : 0;
25  c = (crc & 0x1) ? 0xffffffff : 0;
26  crc = crc >> 1;
27  crc = crc ^ ((d ^ c) & poly);
28  }
29 
30  return crc;
31 }
32 
33 uint32_t crc32_le(uint32_t poly, uint32_t seed, const void *_data,
34  size_t data_len)
35 {
36  if (((uintptr_t)_data & 0x3) || (data_len & 0x3)) {
37  /* data is unaligned, processing data one byte at a time */
38  const uint8_t *data = _data;
39  for (size_t i = 0; i < data_len; i++)
40  seed = crc_le_step(poly, seed, data[i], 8);
41  } else {
42  /* data is aligned, processing 32 bit at a time */
43  data_len >>= 2;
44  const uint32_t *data = _data;
45  for (size_t i = 0; i < data_len; i++)
46  seed = crc_le_step(poly, seed, data[i], 32);
47  }
48 
49  return seed;
50 }
static uint32_t crc_le_step(uint32_t poly, uint32_t crc, uint32_t data_in, unsigned int data_bits)
Definition: crc32.c:19
uint32_t crc32_le(uint32_t poly, uint32_t seed, const void *_data, size_t data_len)
Calculate the CRC32 value of the given data.
Definition: crc32.c:33
A generic CRC32 implementation.