Main MRPT website > C++ reference for MRPT 1.5.6
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-2017, 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 "base-precomp.h" // Precompiled headers
11 
12 
13 #include <mrpt/utils/crc.h>
14 
15 using namespace mrpt::utils;
16 using namespace std;
17 
18 
19 /*---------------------------------------------------------------
20  CRC16
21  ---------------------------------------------------------------*/
22 uint16_t mrpt::utils::compute_CRC16( const std::vector<uint8_t> &data, const uint16_t gen_pol )
23 {
24  return compute_CRC16(&data[0],data.size(),gen_pol);
25 }
26 
27 uint32_t mrpt::utils::compute_CRC32( const std::vector<uint8_t> &data, const uint32_t gen_pol)
28 {
29  return compute_CRC32(&data[0],data.size(),gen_pol);
30 }
31 
32 /*---------------------------------------------------------------
33  CRC16
34  ---------------------------------------------------------------*/
36  const uint8_t *data,
37  const size_t len_,
38  const uint16_t gen_pol)
39 {
40  uint16_t uCrc16;
41  uint8_t abData[2];
42 
43  size_t len = len_;
44 
45  uCrc16 = 0;
46  abData[0] = 0;
47 
48  while(len-- )
49  {
50  abData[1] = abData[0];
51  abData[0] = *data++;
52 
53  if( uCrc16 & 0x8000 )
54  {
55  uCrc16 = (uCrc16 & 0x7fff) << 1;
56  uCrc16 ^= gen_pol;
57  }
58  else
59  {
60  uCrc16 <<= 1;
61  }
62  uCrc16 ^= (abData[0] | (abData[1]<<8));
63  }
64  return uCrc16;
65 }
66 
67 unsigned long CRC32Value(int i, const uint32_t CRC32_POLYNOMIAL)
68 {
69  unsigned long ulCRC = i;
70  for ( int j = 8 ; j > 0; j-- ) {
71  if ( ulCRC & 1 )
72  ulCRC = ( ulCRC >> 1 ) ^ CRC32_POLYNOMIAL;
73  else ulCRC >>= 1;
74  }
75  return ulCRC;
76 }
77 
78 uint32_t mrpt::utils::compute_CRC32(const uint8_t *data, const size_t len_, const uint32_t gen_pol )
79 {
80  size_t len = len_;
81  unsigned long ulCRC = 0;
82  while ( len-- != 0 ) {
83  unsigned long ulTemp1 = ( ulCRC >> 8 ) & 0x00FFFFFFL;
84  unsigned long ulTemp2 = CRC32Value( ((int) ulCRC ^ *data++ ) & 0xff, gen_pol );
85  ulCRC = ulTemp1 ^ ulTemp2;
86  }
87  return ulCRC ;
88 }
89 
unsigned __int16 uint16_t
Definition: rptypes.h:46
unsigned char uint8_t
Definition: rptypes.h:43
GLenum GLsizei len
Definition: glew.h:3955
uint16_t BASE_IMPEXP 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:22
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1284
unsigned long CRC32Value(int i, const uint32_t CRC32_POLYNOMIAL)
Definition: crc.cpp:67
uint32_t BASE_IMPEXP 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:27
unsigned __int32 uint32_t
Definition: rptypes.h:49



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018