MRPT  1.9.9
crc.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/crc.h>
13 #include <mrpt/core/exceptions.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 }
unsigned __int16 uint16_t
Definition: rptypes.h:44
GLenum GLsizei len
Definition: glext.h:4712
unsigned char uint8_t
Definition: rptypes.h:41
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
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
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020