xref: /aosp_15_r20/system/chre/chpp/platform/shared/crc.c (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker  *
4*84e33947SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker  *
8*84e33947SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker  *
10*84e33947SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker  * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker  */
16*84e33947SAndroid Build Coastguard Worker 
17*84e33947SAndroid Build Coastguard Worker #include "chpp/crc.h"
18*84e33947SAndroid Build Coastguard Worker 
19*84e33947SAndroid Build Coastguard Worker #include <stddef.h>
20*84e33947SAndroid Build Coastguard Worker #include <stdint.h>
21*84e33947SAndroid Build Coastguard Worker 
chppCrc32(uint32_t crc,const uint8_t * buf,size_t len)22*84e33947SAndroid Build Coastguard Worker uint32_t chppCrc32(uint32_t crc, const uint8_t *buf, size_t len) {
23*84e33947SAndroid Build Coastguard Worker   // This lookup table (LUT) consumes 16 * 4 = 64 bytes. Other implementations
24*84e33947SAndroid Build Coastguard Worker   // exist with a larger LUT, with a LUT calculated on the fly, or without using
25*84e33947SAndroid Build Coastguard Worker   // a LUT altogether.
26*84e33947SAndroid Build Coastguard Worker   static const uint32_t crc32LookupTable[] = {
27*84e33947SAndroid Build Coastguard Worker       0x00000000, 0x1DB71064, 0x3B6E20C8, 0x26D930AC, 0x76DC4190, 0x6B6B51F4,
28*84e33947SAndroid Build Coastguard Worker       0x4DB26158, 0x5005713C, 0xEDB88320, 0xF00F9344, 0xD6D6A3E8, 0xCB61B38C,
29*84e33947SAndroid Build Coastguard Worker       0x9B64C2B0, 0x86D3D2D4, 0xA00AE278, 0xBDBDF21C};
30*84e33947SAndroid Build Coastguard Worker 
31*84e33947SAndroid Build Coastguard Worker   crc = ~crc;
32*84e33947SAndroid Build Coastguard Worker   for (size_t i = 0; i < len; i++) {
33*84e33947SAndroid Build Coastguard Worker     crc = crc32LookupTable[(crc ^ buf[i]) & 0x0F] ^ (crc >> 4);
34*84e33947SAndroid Build Coastguard Worker     crc = crc32LookupTable[(crc ^ (buf[i] >> 4)) & 0x0F] ^ (crc >> 4);
35*84e33947SAndroid Build Coastguard Worker   }
36*84e33947SAndroid Build Coastguard Worker   crc = ~crc;
37*84e33947SAndroid Build Coastguard Worker 
38*84e33947SAndroid Build Coastguard Worker   return crc;
39*84e33947SAndroid Build Coastguard Worker }
40