2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "bkup_crc32.h"
19 #include "bkup_util.h"
22 * CRC-32C lookup table
24 * Polynomial 0x1EDC6F41
26 static const uint32_t kBkupCrc32Lookup[4][256] = {
38 * compute CRC-32C (Slicing-by-4)
40 uint32_t BkupCrc32(const void *data, size_t length) {
41 uint32_t crc = 0xFFFFFFFF;
42 const uint32_t *current = (const uint32_t *)data;
48 uint32_t one = *current++ ^ crc;
49 crc = kBkupCrc32Lookup[0][(one >> 24) & 0xFF] ^
50 kBkupCrc32Lookup[1][(one >> 16) & 0xFF] ^
51 kBkupCrc32Lookup[2][(one >> 8) & 0xFF] ^
52 kBkupCrc32Lookup[3][one & 0xFF];
56 const uint8_t *current_char = (const uint8_t *)current;
57 while (length-- > 0) {
58 crc = (crc >> 8) ^ kBkupCrc32Lookup[0][(crc & 0xFF) ^ *current_char++];