MRPT  1.9.9
crc.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
13 #include <mrpt/system/crc.h>
14 
16  const std::vector<uint8_t>& data, const uint16_t gen_pol)
17 {
18  ASSERT_(!data.empty());
19  return compute_CRC16(&data[0], data.size(), gen_pol);
20 }
21 
23  const std::vector<uint8_t>& data, const uint32_t gen_pol)
24 {
25  ASSERT_(!data.empty());
26  return compute_CRC32(&data[0], data.size(), gen_pol);
27 }
28 
30  const uint8_t* data, const size_t len_, const uint16_t gen_pol)
31 {
32  uint16_t uCrc16;
33  uint8_t abData[2];
34 
35  size_t len = len_;
36 
37  uCrc16 = 0;
38  abData[0] = 0;
39 
40  while (len--)
41  {
42  abData[1] = abData[0];
43  abData[0] = *data++;
44 
45  if (uCrc16 & 0x8000)
46  {
47  uCrc16 = (uCrc16 & 0x7fff) << 1;
48  uCrc16 ^= gen_pol;
49  }
50  else
51  {
52  uCrc16 <<= 1;
53  }
54  uCrc16 ^= (abData[0] | (abData[1] << 8));
55  }
56  return uCrc16;
57 }
58 
59 unsigned long CRC32Value(int i, const uint32_t CRC32_POLYNOMIAL)
60 {
61  unsigned long ulCRC = i;
62  for (int j = 8; j > 0; j--)
63  {
64  if (ulCRC & 1)
65  ulCRC = (ulCRC >> 1) ^ CRC32_POLYNOMIAL;
66  else
67  ulCRC >>= 1;
68  }
69  return ulCRC;
70 }
71 
73  const uint8_t* data, const size_t len_, const uint32_t gen_pol)
74 {
75  size_t len = len_;
76  unsigned long ulCRC = 0;
77  while (len-- != 0)
78  {
79  unsigned long ulTemp1 = (ulCRC >> 8) & 0x00FFFFFFL;
80  unsigned long ulTemp2 =
81  CRC32Value(((int)ulCRC ^ *data++) & 0xff, gen_pol);
82  ulCRC = ulTemp1 ^ ulTemp2;
83  }
84  return ulCRC;
85 }
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
uint32_t compute_CRC32(const std::vector< uint8_t > &data, const uint32_t gen_pol=0xEDB88320L)
Computes the CRC32 checksum of a block of data.
Definition: crc.cpp:22
unsigned long CRC32Value(int i, const uint32_t CRC32_POLYNOMIAL)
Definition: crc.cpp:59
uint16_t compute_CRC16(const std::vector< uint8_t > &data, const uint16_t gen_pol=0x8005)
Computes the CRC16 checksum of a block of data.
Definition: crc.cpp:15
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020