Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_UtilityCenter / src / ns_util_crc.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 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NS_UtilityCenter
19 /// \brief    This file contains implementation of APIs to calcuate 16-bit and 32-bit CRC checksum of file.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_util_crc.h>
27 #include <stdio.h>
28
29 // buffer size for reading from file
30 #define MAX_BUFFER_SIZE 4096
31
32 // Static 32 bit CRC lookup table
33 UI_16 g_arr_crc16_table[256] = {
34 };
35
36 // Static 32 bit CRC lookup table
37 UI_32 g_arr_crc32_table[256] = {
38 };
39
40
41 ////////////////////////////////////////////////////////////////////////////////////////////////
42 /// CalculateCRC16
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;
47
48   CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0};
49   size_t l_ui_bytes_read = 0;
50   FILE *l_p_file = NULL;
51
52   if (NULL != f_c_file_name) {
53     f_ui_check_sum = 0xFFFF;
54
55     // Open the file
56     l_p_file = fopen(f_c_file_name, "rbe");
57
58     if (NULL == l_p_file) {
59       l_e_status = eFrameworkunifiedStatusFileLoadError;
60     } else {
61       l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
62
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));
67         }
68
69         l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
70       }
71
72       fclose(l_p_file);
73       l_p_file = NULL;
74     }
75
76     f_ui_check_sum = static_cast<UI_16>(~f_ui_check_sum);
77   } else {
78     l_e_status = eFrameworkunifiedStatusInvldParam;
79   }
80
81   return l_e_status;
82 }
83
84 ////////////////////////////////////////////////////////////////////////////////////////////////
85 /// CalculateCRC32
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;
90
91   CHAR l_c_buffer[MAX_BUFFER_SIZE] = {0};
92   size_t l_ui_bytes_read = 0;
93   FILE *l_p_file = NULL;
94
95   if (NULL != f_c_file_name) {
96     f_ui_check_sum = 0xFFFFFFFF;
97
98     // Open the file
99     l_p_file = fopen(f_c_file_name, "rbe");
100
101     if (NULL == l_p_file) {
102       l_e_status = eFrameworkunifiedStatusFileLoadError;
103     } else {
104       l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
105
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];
110         }
111
112         l_ui_bytes_read = fread(l_c_buffer, 1, sizeof(l_c_buffer), l_p_file);
113       }
114
115       fclose(l_p_file);
116       l_p_file = NULL;
117     }
118
119     f_ui_check_sum = ~f_ui_check_sum;
120   } else {
121     l_e_status = eFrameworkunifiedStatusInvldParam;
122   }
123
124   return l_e_status;
125 }