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.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NS_UtilityCenter
19 /// \brief This file contains implementation of APIs to calcuate 16-bit and 32-bit CRC checksum of file.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_util_crc.h>
29 // buffer size for reading from file
30 #define MAX_BUFFER_SIZE 4096
32 // Static 32 bit CRC lookup table
33 UI_16 g_arr_crc16_table[256] = {
36 // Static 32 bit CRC lookup table
37 UI_32 g_arr_crc32_table[256] = {
41 ////////////////////////////////////////////////////////////////////////////////////////////////
43 /// This API calculates the 16 bit CRC checksum of a file
44 ////////////////////////////////////////////////////////////////////////////////////////////////
45 EFrameworkunifiedStatus CalculateCRC16(PCSTR f_c_file_name, UI_16 &f_ui_check_sum) { // NOLINT (readability/nolint)
46 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
48 CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0};
49 size_t l_ui_bytes_read = 0;
50 FILE *l_p_file = NULL;
52 if (NULL != f_c_file_name) {
53 f_ui_check_sum = 0xFFFF;
56 l_p_file = fopen(f_c_file_name, "rbe");
58 if (NULL == l_p_file) {
59 l_e_status = eFrameworkunifiedStatusFileLoadError;
61 l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
63 while (0 != l_ui_bytes_read) { // LCOV_EXCL_BR_LINE 11: not a branch
64 for (UI_32 l_ui_bytes_cnt = 0; l_ui_bytes_cnt < l_ui_bytes_read; l_ui_bytes_cnt++) { // LCOV_EXCL_BR_LINE 11: not a branch
65 f_ui_check_sum = static_cast<UI_16>(
66 g_arr_crc16_table[((f_ui_check_sum >> 8) ^ l_c_buffer[l_ui_bytes_cnt]) & 0xFF] ^ (f_ui_check_sum << 8));
69 l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
76 f_ui_check_sum = static_cast<UI_16>(~f_ui_check_sum);
78 l_e_status = eFrameworkunifiedStatusInvldParam;
84 ////////////////////////////////////////////////////////////////////////////////////////////////
86 /// This API calculates the 32 bit CRC checksum of a file
87 ////////////////////////////////////////////////////////////////////////////////////////////////
88 EFrameworkunifiedStatus CalculateCRC32(PCSTR f_c_file_name, UI_32 &f_ui_check_sum) { // NOLINT (readability/nolint)
89 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
91 CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0};
92 size_t l_ui_bytes_read = 0;
93 FILE *l_p_file = NULL;
95 if (NULL != f_c_file_name) {
96 f_ui_check_sum = 0xFFFFFFFF;
99 l_p_file = fopen(f_c_file_name, "rbe");
101 if (NULL == l_p_file) {
102 l_e_status = eFrameworkunifiedStatusFileLoadError;
104 l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
106 while (0 != l_ui_bytes_read) { // LCOV_EXCL_BR_LINE 11: not a branch
107 for (UI_32 l_ui_bytes_cnt = 0; l_ui_bytes_cnt < l_ui_bytes_read; l_ui_bytes_cnt++) { // LCOV_EXCL_BR_LINE 11: not a branch
108 f_ui_check_sum = (f_ui_check_sum >> 8)
109 ^ g_arr_crc32_table[(l_c_buffer[l_ui_bytes_cnt] ^ f_ui_check_sum) & 0xFF];
112 l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
119 f_ui_check_sum = ~f_ui_check_sum;
121 l_e_status = eFrameworkunifiedStatusInvldParam;