-
Notifications
You must be signed in to change notification settings - Fork 1
/
bigint_parser.c
349 lines (304 loc) · 9.82 KB
/
bigint_parser.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "ByteQueue.h"
#include "utils.h"
#include "bigint_parser.h"
static uint8_t byte_mask[8] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
};
static uint8_t byte_hex_mask[] = {
0xa, 0xb, 0xc, 0xd, 0xe, 0xf
};
static uint64_t pow10[10] = {
1, 10, 100, 1000, 10000,
100000, 1000000, 10000000, 100000000, 1000000000
};
static uint8_t update_byte(uint8_t byte, uint8_t bit, uint64_t index) {
switch (bit) {
case 0:
byte &= (~ byte_mask[index]);
break;
case 1:
byte |= ( byte_mask[index]);
break;
}
return byte;
}
uint8_t* malloc_one_time_base10_array(char* string, uint64_t string_len) {
uint8_t* array = (uint8_t*)malloc(sizeof(uint8_t) * string_len);
for (uint64_t i = 0; i < string_len; i++) {
array[i] = string[i] - '0';
}
return array;
}
bool is_base10_array_zero(uint8_t* array, uint64_t array_len) {
for (uint64_t i = 0; i < array_len; i++) {
if (array[i] != 0) {
return false;
}
}
return true;
}
uint8_t base10_array_divide_by_2(uint8_t* array, uint64_t array_size) {
uint64_t next_additive = 0;
uint64_t additive;
uint8_t remainder = (array[array_size - 1] % 2 == 0) ? 0 : 1;
for (uint64_t i = 0; i < array_size; i++) {
additive = next_additive;
if (array[i] % 2 != 0) {
next_additive = 5;
} else {
next_additive = 0;
}
array[i] = (array[i] / 2) + additive;
}
return remainder;
}
ByteQueue bytequeue_from_base10_string(uint8_t* array, uint64_t array_len) {
uint64_t i = 0;
uint8_t remainder = 0;
uint8_t byte = 0x0;
ByteQueue queue = Byte_Queue();
while (is_base10_array_zero(array, array_len) == false) {
remainder = base10_array_divide_by_2(array, array_len);
byte = update_byte(byte, remainder, i % 8);
if (i % 8 == 7) {
queue.append_left(&queue, byte);
byte = 0x0;
}
i += 1;
}
if (byte != 0) { queue.append_left(&queue, byte); }
return queue;
}
ByteQueue bytequeue_from_hex_string(char* string, uint64_t string_len) {
ByteQueue queue = Byte_Queue();
uint8_t byte = 0x0;
uint64_t i = string_len;
uint64_t even_flag = (string_len % 2 == 0) ? 1 : 0;
while (i > 0) {
byte <<= 4;
if ((string[i - 1] >= 'A') && (string[i - 1] <= 'F')) {
byte |= byte_hex_mask[string[i - 1] - 'A'];
} else if ((string[i - 1] >= 'a') && (string[i - 1] <= 'f')) {
byte |= byte_hex_mask[string[i - 1] - 'a'];
} else if ((string[i - 1] >= '0') && (string[i - 1] <= '9')) {
byte |= string[i - 1] - '0';
} else {
printlnc("err: at bigintparser.c/bytequeue_from_hex_string():", red);
printf("%c\n", string[i - 1]);
}
if (i % 2 == even_flag) {
queue.append_left(&queue, byte << 4 | byte >> 4);
byte = 0x0;
}
i -= 1;
}
if (byte != 0) { queue.append_left(&queue, byte); }
return queue;
}
ByteQueue bytequeue_from_binary_string(char* string, uint64_t string_len) {
ByteQueue queue = Byte_Queue();
uint8_t byte = 0x0;
uint64_t i = 0;
uint64_t even_flag = (string_len % 2 == 0) ? 1 : 0;
while (i < string_len) {
if ((string[string_len - i - 1] == '0') || (string[string_len - i - 1] == '1')) {
byte |= (string[string_len - i - 1] - '0') << (i % 8);
} else {
printlnc("err: at bigintparser.c/bytequeue_from_binary_string():", red);
printf("%c\n", string[string_len - i - 1]);
}
if (i % 8 == 7) {
queue.append_left(&queue, byte);
byte = 0x0;
}
i += 1;
}
if (byte != 0) {queue.append_left(&queue, byte);}
return queue;
}
bool base10_string_check(char* string, uint64_t string_len) {
for (uint64_t i = 0; i < string_len; i++) {
if (((string[i] >= '0') && (string[i] <= '9')) == false) {
return false;
}
}
return true;
}
void base10_string_error_check(char* string, uint64_t string_len) {
print("- ", red);
uint64_t i = 0;
while (i < string_len) {
if (((string[i] >= '0') && (string[i] <= '9')) == false) {
printf("\x1b[33m%c\x1b[0m", string[i]);
} else {
printf("\x1b[32m%c\x1b[0m", string[i]);
}
i += 1;
}
printf("\n");
}
bool hex_string_check(char* string, uint64_t string_len) {
for (uint64_t i = 2; i < string_len; i++) {
if (((string[i] >= '0') && (string[i] <= '9')) ||
((string[i] >= 'a') && (string[i] <= 'f')) ||
((string[i] >= 'A') && (string[i] <= 'F'))) {
continue;
} else {
return false;
}
}
return true;
}
void hex_string_error_check(char* string, uint64_t string_len) {
print("- ", red);
uint64_t i = 0;
while (i < string_len) {
if (((string[i] >= '0') && (string[i] <= '9')) ||
((string[i] >= 'a') && (string[i] <= 'f')) ||
((string[i] >= 'A') && (string[i] <= 'F'))) {
printf("\x1b[32m%c\x1b[0m", string[i]);
} else {
printf("\x1b[33m%c\x1b[0m", string[i]);
}
i += 1;
}
printf("\n");
}
bool binary_string_check(char* string, uint64_t string_len) {
for (uint64_t i = 2; i < string_len; i++) {
if ((string[i] == '1') || (string[i] == '0')) {
continue;
} else {
return false;
}
}
return true;
}
void binary_string_error_check(char* string, uint64_t string_len) {
print("- ", red);
uint64_t i = 0;
while (i < string_len) {
if ((string[i] == '1') || (string[i] == '0')) {
printf("\x1b[32m%c\x1b[0m", string[i]);
} else {
printf("\x1b[33m%c\x1b[0m", string[i]);
}
i += 1;
}
printf("\n");
}
void one_time_string_to_bytequeue_error() {
print("- err: bad string error: ", red);
printlnc("at bigint_parser/one_time_string_to_bytequeue().", cyan);
print("- ", red);
printlnc("A string with illegal characters has been passed that ", yellow);
printlnc(" does not match any of the formats like 0x00 (hex) or 0b00 (binary) or base10:", yellow);
}
ByteQueue one_time_string_to_bytequeue(char* string, uint64_t string_len) {
if (string_len < 2) {
println("string len is less than 2.");
return Byte_Queue();
}
uint64_t MAX = 0xffffffffffffffff - 1;
if (string_len > MAX) {
println("string len is greater than max.");
return Byte_Queue();
}
char string_type[3];
string_type[0] = string[0];
string_type[1] = string[1];
ByteQueue queue = Byte_Queue();
queue.size = 0;
bool empty_queue_flag = true;
uint64_t i = 0;
if (string_type[0] == '0' && string_type[1] == 'x') {
if(hex_string_check(string, string_len) == true) {
i = 2;
while ((string[i] == '0') && (i < string_len)) {
i += 1;
}
queue = bytequeue_from_hex_string(string + i, string_len - i);
empty_queue_flag = false;
} else {
one_time_string_to_bytequeue_error();
hex_string_error_check(string, string_len);
}
} else if (string_type[0] == '0' && string_type[1] == 'b') {
if (binary_string_check(string, string_len) == true) {
i = 2;
while ((string[i] == '0') && (i < string_len)) {
i += 1;
}
queue = bytequeue_from_binary_string(string + i, string_len - i);
empty_queue_flag = false;
} else {
one_time_string_to_bytequeue_error();
binary_string_error_check(string, string_len);
}
} else {
if (base10_string_check(string, string_len) == true) {
while ((string[i] == '0') && (i < string_len) ) {
i += 1;
}
uint8_t* array = malloc_one_time_base10_array(string + i, string_len - i);
queue = bytequeue_from_base10_string(array, string_len - i);
free(array);
empty_queue_flag = false;
} else {
one_time_string_to_bytequeue_error();
base10_string_error_check(string, string_len);
}
}
if (empty_queue_flag == true) {
exit(-1);
}
return queue;
}
bool bigint_string_check(char* string, uint64_t string_len) {
bool check = false;
if (string_len <= 0) {
return true;
}
uint64_t MAX = 0xffffffffffffffff - 1;
if (string_len > MAX) {
println("string len is greater than max.");
return false;
}
if (string_len == 1) {
check = base10_string_check(string, string_len);
if (check == false) {
base10_string_error_check(string, string_len);
}
} else if (string[0] == '0' && string[1] == 'x') {
check = hex_string_check(string, string_len);
if (check == false) {
hex_string_error_check(string, string_len);
}
} else if (string[0] == '0' && string[1] == 'b') {
check = binary_string_check(string, string_len);
if (check == false) {
binary_string_error_check(string, string_len);
}
} else {
check = base10_string_check(string, string_len);
if (check == false) {
base10_string_error_check(string, string_len);
}
}
return check;
}
StringType bigint_string_type_check(char* string, uint64_t string_len) {
if (string_len == 1) {
return decimal;
} else if (string[0] == '0' && string[1] == 'x') {
return hexadecimal;
} else if (string[0] == '0' && string[1] == 'b') {
return binary;
}
return decimal;
}