sl_crc.cpp
Go to the documentation of this file.
1 /*
2  * Slamtec LIDAR SDK
3  *
4  * Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
5  * http://www.slamtec.com
6  *
7  */
8  /*
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include "sl_crc.h"
34 
35 namespace sl {namespace crc32 {
36 
37  static sl_u32 table[256];//crc32_table
38  sl_u32 bitrev(sl_u32 input, sl_u16 bw)
39  {
40  sl_u16 i;
41  sl_u32 var;
42  var = 0;
43  for (i = 0; i < bw; i++) {
44  if (input & 0x01) {
45  var |= 1 << (bw - 1 - i);
46  }
47  input >>= 1;
48  }
49  return var;
50  }
51 
52  void init(sl_u32 poly)
53  {
54  sl_u16 i;
55  sl_u16 j;
56  sl_u32 c;
57 
58  poly = bitrev(poly, 32);
59  for (i = 0; i < 256; i++) {
60  c = i;
61  for (j = 0; j < 8; j++) {
62  if (c & 1)
63  c = poly ^ (c >> 1);
64  else
65  c = c >> 1;
66  }
67  table[i] = c;
68  }
69  }
70 
71  sl_u32 cal(sl_u32 crc, void* input, sl_u16 len)
72  {
73  sl_u16 i;
74  sl_u8 index;
75  sl_u8* pch;
76  pch = (unsigned char*)input;
77  sl_u8 leftBytes = 4 - (len & 0x3);
78 
79  for (i = 0; i < len; i++) {
80  index = (unsigned char)(crc^*pch);
81  crc = (crc >> 8) ^ table[index];
82  pch++;
83  }
84 
85  for (i = 0; i < leftBytes; i++) {//zero padding
86  index = (unsigned char)(crc ^ 0);
87  crc = (crc >> 8) ^ table[index];
88  }
89  return crc ^ 0xffffffff;
90  }
91 
92  sl_result getResult(sl_u8 *ptr, sl_u32 len)
93  {
94  static sl_u8 tmp;
95  if (tmp != 1) {
96  init(0x4C11DB7);
97  tmp = 1;
98  }
99 
100  return cal(0xFFFFFFFF, ptr, len);
101  }
102 }}
sl_crc.h
sl::crc32::cal
sl_u32 cal(sl_u32 crc, void *input, sl_u16 len)
Definition: sl_crc.cpp:71
sl
Definition: sl_crc.h:38
crc32
sl_u32 crc32
Definition: sl_lidar_cmd.h:5
sl::crc32::init
void init(sl_u32 poly)
Definition: sl_crc.cpp:52
sl::crc32::getResult
sl_result getResult(sl_u8 *ptr, sl_u32 len)
Definition: sl_crc.cpp:92
sl_result
uint32_t sl_result
Definition: sl_types.h:69
sl::crc32::table
static sl_u32 table[256]
Definition: sl_crc.cpp:37
sl::crc32::bitrev
sl_u32 bitrev(sl_u32 input, sl_u16 bw)
Definition: sl_crc.cpp:38


rplidar_ros
Author(s):
autogenerated on Fri Aug 2 2024 08:42:14