Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / backup_manager / server / src / bkup_crc32.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cstdio>
18 #include "bkup_crc32.h"
19 #include "bkup_util.h"
20
21 /*
22  * CRC-32C lookup table
23  *
24  * Polynomial 0x1EDC6F41
25  */
26 static const uint32_t kBkupCrc32Lookup[4][256] = {
27   {
28   },
29   {
30   },
31   {
32   },
33   {
34   }
35 };
36
37 /**
38  * compute CRC-32C (Slicing-by-4)
39  */
40 uint32_t BkupCrc32(const void *data, size_t length) {
41   uint32_t crc = 0xFFFFFFFF;
42   const uint32_t *current = (const uint32_t *)data;
43
44   if (data == NULL) {
45     return -1;
46   }
47   while (length >= 4) {
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];
53     length -= 4;
54   }
55
56   const uint8_t *current_char = (const uint8_t *)current;
57   while (length-- > 0) {
58     crc = (crc >> 8) ^ kBkupCrc32Lookup[0][(crc & 0xFF) ^ *current_char++];
59   }
60
61   return ~crc;
62 }