-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
231 lines (185 loc) · 6.41 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "utils.h"
FILE *compressed_data_log_file = NULL;
FILE *decompressed_data_log_file = NULL;
FILE *decompressed_data_file = NULL;
cJSON *compressed_data_json = NULL;
cJSON *decompressed_data_json = NULL;
unsigned char print_data_verbose = 0;
unsigned int compressed_data_print_num_count = 0;
unsigned int decompressed_data_print_num_count = 0;
unsigned int compressed_data_print_data_count = 0;
unsigned int decompressed_data_print_data_count = 0;
unsigned char compressed_data_print_buffer[200] = {0};
unsigned char decompressed_data_print_buffer[200] = {0};
unsigned int adler32_checksum = 1;
char *print_level_tabel[] = {"",
"\t",
"\t\t",
"\t\t\t",
"\t\t\t\t",
"\t\t\t\t\t",
"\t\t\t\t\t\t",
"\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t\t\t\t",
};
unsigned int swap_uint32(unsigned int val)
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}
void print_to_compressed_log(char *fmt, ...)
{
va_list args;
if (compressed_data_log_file) {
va_start(args, fmt);
vfprintf(compressed_data_log_file, fmt, args);
va_end(args);
}
}
void print_to_decompressed_log(char *fmt, ...)
{
va_list args;
if (decompressed_data_log_file) {
va_start(args, fmt);
vfprintf(decompressed_data_log_file, fmt, args);
va_end(args);
}
}
void print_compressed_data_hex(int data_val, cJSON* json)
{
if (print_data_verbose && compressed_data_log_file) {
cJSON* item = cJSON_CreateNumber(data_val);
cJSON_AddItemToArray(json, item);
}
}
void print_compressed_data_dec(int data_val, int print_level)
{
if (print_data_verbose) {
compressed_data_print_num_count += sprintf(compressed_data_print_buffer
+ compressed_data_print_num_count, "%d ", data_val);
compressed_data_print_data_count++;
if (compressed_data_print_data_count == 16) {
print_to_compressed_log("%s\"%s\",\n", print_level_tabel[print_level],
compressed_data_print_buffer);
memset(compressed_data_print_buffer, 0,
compressed_data_print_num_count);
compressed_data_print_data_count = 0;
compressed_data_print_num_count = 0;
}
}
}
void print_decompressed_data_hex(int data_val, cJSON* json)
{
if (print_data_verbose && decompressed_data_file) {
cJSON* item = cJSON_CreateNumber(data_val);
cJSON_AddItemToArray(json, item);
}
}
void print_log_to_both(char *fmt, ...)
{
va_list args;
if (compressed_data_log_file) {
va_start(args, fmt);
vfprintf(compressed_data_log_file, fmt, args);
va_end(args);
}
if (decompressed_data_log_file) {
va_start(args, fmt);
vfprintf(decompressed_data_log_file, fmt, args);
va_end(args);
}
}
void addStringToObjectFormatted(cJSON* json, const char *const name, const char *const format, ...)
{
char buffer[256];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
cJSON_AddStringToObject(json, name, buffer);
}
void dump_data_to_number_array_json(cJSON* json, const char *const name, unsigned char *buffer, unsigned int num)
{
unsigned char print_buffer[200] = {0};
unsigned int print_count = 0;
unsigned int lines, remain, i, j;
unsigned char data_val;
lines = num >> 4;
remain = num & 0xF;
cJSON *array = cJSON_AddArrayToObject(json, name);
for (i = 0; i < num; i++) {
data_val = *(buffer + i);
cJSON* item = cJSON_CreateNumber(data_val);
cJSON_AddItemToArray(array, item);
}
}
void dump_data_to_string_json(cJSON* json, const char *const name, unsigned char *buffer, unsigned int num)
{
unsigned char print_buffer[200] = {0};
unsigned int print_count = 0;
unsigned int lines, remain, i, j;
unsigned char data_val;
lines = num >> 4;
remain = num & 0xF;
cJSON *array = cJSON_AddArrayToObject(json, name);
for (i = 0; i < lines; i++) {
for (j = 0; j < 16; j++) {
data_val = *(buffer + (i << 4) + j);
print_count += sprintf(print_buffer + print_count, "0x%02x ", data_val);
}
cJSON *item = cJSON_CreateString(print_buffer);
cJSON_AddItemToArray(array, item);
memset(print_buffer, 0, print_count);
print_count = 0;
}
for (j = 0; j < remain; j++) {
data_val = *(buffer + (lines << 4) + j);
print_count += sprintf(print_buffer + print_count, "0x%02x ", data_val);
cJSON *item = cJSON_CreateString(print_buffer);
cJSON_AddItemToArray(array, item);
}
}
void dump_data_to_json(cJSON* json, const char *const name, unsigned char *buffer, unsigned int num)
{
dump_data_to_number_array_json(json, name, buffer, num);
}
void print_hex_with_buffer(unsigned char *buffer, unsigned int num, int print_level)
{
unsigned char print_buffer[200] = {0};
unsigned int print_count = 0;
unsigned int lines, remain, i, j;
unsigned char data_val;
lines = num >> 4;
remain = num & 0xF;
for (i = 0; i < lines; i++) {
for (j = 0; j < 16; j++) {
data_val = *(buffer + (i << 4) + j);
print_count += sprintf(print_buffer + print_count, "0x%02x ", data_val);
}
print_log_to_both("%s\"%s\",\n", print_level_tabel[print_level], print_buffer);
memset(print_buffer, 0, print_count);
print_count = 0;
}
for (j = 0; j < remain; j++) {
data_val = *(buffer + (lines << 4) + j);
print_count += sprintf(print_buffer + print_count, "0x%02x ", data_val);
}
print_log_to_both("%s\"%s\"\n", print_level_tabel[print_level], print_buffer);
}
void adler32(unsigned char data_val)
{
unsigned int upper_word, lower_word;
unsigned int base = 65521U;
upper_word = (adler32_checksum >> 16) & 0xffff;
lower_word = adler32_checksum & 0xffff;
lower_word += data_val;
if (lower_word >= base)
lower_word -= base;
upper_word += lower_word;
if (upper_word >= base)
upper_word -= base;
adler32_checksum = lower_word | (upper_word << 16);
}