1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "l2cap_crc.h"
17
18 #define G_CRCTABLENUM 256
19 static const uint16_t G_CRCTABLE[G_CRCTABLENUM] = {
20 0x0000, 0xc0c1,
21 0xc181, 0x0140,
22 0xc301, 0x03c0,
23 0x0280, 0xc241,
24 0xc601, 0x06c0,
25 0x0780, 0xc741,
26 0x0500, 0xc5c1,
27 0xc481, 0x0440,
28 0xcc01, 0x0cc0,
29 0x0d80, 0xcd41,
30 0x0f00, 0xcfc1,
31 0xce81, 0x0e40,
32 0x0a00, 0xcac1,
33 0xcb81, 0x0b40,
34 0xc901, 0x09c0,
35 0x0880, 0xc841,
36 0xd801, 0x18c0,
37 0x1980, 0xd941,
38 0x1b00, 0xdbc1,
39 0xda81, 0x1a40,
40 0x1e00, 0xdec1,
41 0xdf81, 0x1f40,
42 0xdd01, 0x1dc0,
43 0x1c80, 0xdc41,
44 0x1400, 0xd4c1,
45 0xd581, 0x1540,
46 0xd701, 0x17c0,
47 0x1680, 0xd641,
48 0xd201, 0x12c0,
49 0x1380, 0xd341,
50 0x1100, 0xd1c1,
51 0xd081, 0x1040,
52 0xf001, 0x30c0,
53 0x3180, 0xf141,
54 0x3300, 0xf3c1,
55 0xf281, 0x3240,
56 0x3600, 0xf6c1,
57 0xf781, 0x3740,
58 0xf501, 0x35c0,
59 0x3480, 0xf441,
60 0x3c00, 0xfcc1,
61 0xfd81, 0x3d40,
62 0xff01, 0x3fc0,
63 0x3e80, 0xfe41,
64 0xfa01, 0x3ac0,
65 0x3b80, 0xfb41,
66 0x3900, 0xf9c1,
67 0xf881, 0x3840,
68 0x2800, 0xe8c1,
69 0xe981, 0x2940,
70 0xeb01, 0x2bc0,
71 0x2a80, 0xea41,
72 0xee01, 0x2ec0,
73 0x2f80, 0xef41,
74 0x2d00, 0xedc1,
75 0xec81, 0x2c40,
76 0xe401, 0x24c0,
77 0x2580, 0xe541,
78 0x2700, 0xe7c1,
79 0xe681, 0x2640,
80 0x2200, 0xe2c1,
81 0xe381, 0x2340,
82 0xe101, 0x21c0,
83 0x2080, 0xe041,
84 0xa001, 0x60c0,
85 0x6180, 0xa141,
86 0x6300, 0xa3c1,
87 0xa281, 0x6240,
88 0x6600, 0xa6c1,
89 0xa781, 0x6740,
90 0xa501, 0x65c0,
91 0x6480, 0xa441,
92 0x6c00, 0xacc1,
93 0xad81, 0x6d40,
94 0xaf01, 0x6fc0,
95 0x6e80, 0xae41,
96 0xaa01, 0x6ac0,
97 0x6b80, 0xab41,
98 0x6900, 0xa9c1,
99 0xa881, 0x6840,
100 0x7800, 0xb8c1,
101 0xb981, 0x7940,
102 0xbb01, 0x7bc0,
103 0x7a80, 0xba41,
104 0xbe01, 0x7ec0,
105 0x7f80, 0xbf41,
106 0x7d00, 0xbdc1,
107 0xbc81, 0x7c40,
108 0xb401, 0x74c0,
109 0x7580, 0xb541,
110 0x7700, 0xb7c1,
111 0xb681, 0x7640,
112 0x7200, 0xb2c1,
113 0xb381, 0x7340,
114 0xb101, 0x71c0,
115 0x7080, 0xb041,
116 0x5000, 0x90c1,
117 0x9181, 0x5140,
118 0x9301, 0x53c0,
119 0x5280, 0x9241,
120 0x9601, 0x56c0,
121 0x5780, 0x9741,
122 0x5500, 0x95c1,
123 0x9481, 0x5440,
124 0x9c01, 0x5cc0,
125 0x5d80, 0x9d41,
126 0x5f00, 0x9fc1,
127 0x9e81, 0x5e40,
128 0x5a00, 0x9ac1,
129 0x9b81, 0x5b40,
130 0x9901, 0x59c0,
131 0x5880, 0x9841,
132 0x8801, 0x48c0,
133 0x4980, 0x8941,
134 0x4b00, 0x8bc1,
135 0x8a81, 0x4a40,
136 0x4e00, 0x8ec1,
137 0x8f81, 0x4f40,
138 0x8d01, 0x4dc0,
139 0x4c80, 0x8c41,
140 0x4400, 0x84c1,
141 0x8581, 0x4540,
142 0x8701, 0x47c0,
143 0x4680, 0x8641,
144 0x8201, 0x42c0,
145 0x4380, 0x8341,
146 0x4100, 0x81c1,
147 0x8081, 0x4040,
148 };
149
CalCrc16WithPrev(uint8_t data,uint16_t preCrc)150 uint16_t CalCrc16WithPrev(uint8_t data, uint16_t preCrc)
151 {
152 return ((preCrc >> 0x08) & 0x00ff) ^ G_CRCTABLE[(uint8_t)(preCrc & 0x00ff) ^ data];
153 }