]>
Commit | Line | Data |
---|---|---|
20f9a2a1 M |
1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // utilities | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "util.h" | |
acf0582d | 12 | |
13 | #include <stdint.h> | |
14 | #include <string.h> | |
15 | #include <ctype.h> | |
16 | #include <stdlib.h> | |
17 | #include <stdio.h> | |
18 | #include <time.h> | |
03439be3 | 19 | #include <stdarg.h> |
acf0582d | 20 | |
ec9c7112 | 21 | #ifdef _WIN32 |
22 | #include <windows.h> | |
23 | #endif | |
24 | ||
ace26dbd | 25 | #define MAX_BIN_BREAK_LENGTH (3072+384+1) |
534983d7 | 26 | |
cb64309e | 27 | #ifndef _WIN32 |
873014de M |
28 | #include <termios.h> |
29 | #include <sys/ioctl.h> | |
8c0ccdef | 30 | #include <unistd.h> |
79544b28 | 31 | |
f397b5cc M |
32 | int ukbhit(void) |
33 | { | |
34 | int cnt = 0; | |
35 | int error; | |
36 | static struct termios Otty, Ntty; | |
37 | ||
8c0ccdef | 38 | if ( tcgetattr(STDIN_FILENO, &Otty) == -1 ) return -1; |
f397b5cc M |
39 | Ntty = Otty; |
40 | ||
8c0ccdef | 41 | Ntty.c_iflag = 0x0000; // input mode |
42 | Ntty.c_oflag = 0x0000; // output mode | |
43 | Ntty.c_lflag &= ~ICANON; // control mode = raw | |
44 | Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue | |
45 | Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever | |
46 | ||
47 | if (0 == (error = tcsetattr(STDIN_FILENO, TCSANOW, &Ntty))) { // set new attributes | |
48 | error += ioctl(STDIN_FILENO, FIONREAD, &cnt); // get number of characters availabe | |
49 | error += tcsetattr(STDIN_FILENO, TCSANOW, &Otty); // reset attributes | |
f397b5cc M |
50 | } |
51 | ||
52 | return ( error == 0 ? cnt : -1 ); | |
53 | } | |
54 | ||
a9104f7e | 55 | char getch(void) |
56 | { | |
57 | char c; | |
58 | int error; | |
59 | struct termios Otty, Ntty; | |
60 | if ( tcgetattr(STDIN_FILENO, &Otty) == -1 ) return -1; | |
61 | Ntty = Otty; | |
62 | Ntty.c_lflag &= ~ICANON; /* disable buffered i/o */ | |
63 | if (0 == (error = tcsetattr(STDIN_FILENO, TCSANOW, &Ntty))) { // set new attributes | |
64 | c = getchar(); | |
65 | error += tcsetattr(STDIN_FILENO, TCSANOW, &Otty); // reset attributes | |
66 | } | |
67 | return ( error == 0 ? c : -1 ); | |
68 | } | |
69 | ||
70 | #else // _WIN32 | |
acf0582d | 71 | |
f397b5cc M |
72 | #include <conio.h> |
73 | int ukbhit(void) { | |
74 | return kbhit(); | |
75 | } | |
76 | #endif | |
77 | ||
55acbb2a | 78 | // log files functions |
b915fda3 | 79 | void AddLogLine(char *file, char *extData, char *c) { |
55acbb2a | 80 | FILE *fLog = NULL; |
b915fda3 | 81 | char filename[FILE_PATH_SIZE] = {0x00}; |
82 | int len = 0; | |
83 | ||
84 | len = strlen(file); | |
85 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
86 | memcpy(filename, file, len); | |
87 | ||
88 | fLog = fopen(filename, "a"); | |
55acbb2a | 89 | if (!fLog) { |
b915fda3 | 90 | printf("Could not append log file %s", filename); |
55acbb2a M |
91 | return; |
92 | } | |
93 | ||
94 | fprintf(fLog, "%s", extData); | |
95 | fprintf(fLog, "%s\n", c); | |
96 | fclose(fLog); | |
97 | } | |
98 | ||
99 | void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){ | |
100 | AddLogLine(fileName, extData, sprint_hex(data, len)); | |
101 | } | |
102 | ||
103 | void AddLogUint64(char *fileName, char *extData, const uint64_t data) { | |
104 | char buf[100] = {0}; | |
105 | sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF)); | |
106 | AddLogLine(fileName, extData, buf); | |
107 | } | |
108 | ||
109 | void AddLogCurrentDT(char *fileName) { | |
110 | char buff[20]; | |
111 | struct tm *curTime; | |
112 | ||
113 | time_t now = time(0); | |
114 | curTime = gmtime(&now); | |
115 | ||
116 | strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime); | |
117 | AddLogLine(fileName, "\nanticollision: ", buff); | |
118 | } | |
119 | ||
e0c635d1 | 120 | void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) { |
55acbb2a M |
121 | char * fnameptr = fileName; |
122 | memset(fileName, 0x00, 200); | |
123 | ||
e0c635d1 | 124 | for (int j = 0; j < byteCount; j++, fnameptr += 2) |
d1869c33 | 125 | sprintf(fnameptr, "%02x", (unsigned int) uid[j]); |
55acbb2a | 126 | sprintf(fnameptr, "%s", ext); |
55acbb2a M |
127 | } |
128 | ||
6b882a39 OM |
129 | // fill buffer from structure [{uint8_t data, size_t length},...] |
130 | int FillBuffer(uint8_t *data, size_t maxDataLength, size_t *dataLength, ...) { | |
131 | *dataLength = 0; | |
132 | va_list valist; | |
133 | va_start(valist, dataLength); | |
134 | ||
135 | uint8_t *vdata = NULL; | |
136 | size_t vlength = 0; | |
137 | do{ | |
138 | vdata = va_arg(valist, uint8_t *); | |
139 | if (!vdata) | |
140 | break; | |
141 | ||
142 | vlength = va_arg(valist, size_t); | |
143 | if (*dataLength + vlength > maxDataLength) { | |
144 | va_end(valist); | |
145 | return 1; | |
146 | } | |
147 | ||
148 | memcpy(&data[*dataLength], vdata, vlength); | |
149 | *dataLength += vlength; | |
150 | ||
151 | } while (vdata); | |
152 | ||
153 | va_end(valist); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
0bb51450 OM |
158 | bool CheckStringIsHEXValue(const char *value) { |
159 | for (int i = 0; i < strlen(value); i++) | |
160 | if (!isxdigit(value[i])) | |
161 | return false; | |
162 | ||
163 | if (strlen(value) % 2) | |
164 | return false; | |
165 | ||
166 | return true; | |
167 | } | |
168 | ||
66efdc1f | 169 | void hex_to_buffer(const uint8_t *buf, const uint8_t *hex_data, const size_t hex_len, const size_t hex_max_len, |
170 | const size_t min_str_len, const size_t spaces_between, bool uppercase) { | |
171 | ||
172 | char *tmp = (char *)buf; | |
173 | size_t i; | |
edd4c838 | 174 | memset(tmp, 0x00, hex_max_len); |
66efdc1f | 175 | |
176 | int maxLen = ( hex_len > hex_max_len) ? hex_max_len : hex_len; | |
177 | ||
178 | for (i = 0; i < maxLen; ++i, tmp += 2 + spaces_between) { | |
179 | sprintf(tmp, (uppercase) ? "%02X" : "%02x", (unsigned int) hex_data[i]); | |
180 | ||
181 | for (int j = 0; j < spaces_between; j++) | |
182 | sprintf(tmp + 2 + j, " "); | |
183 | } | |
184 | ||
185 | i *= (2 + spaces_between); | |
186 | int minStrLen = min_str_len > i ? min_str_len : 0; | |
187 | if (minStrLen > hex_max_len) | |
188 | minStrLen = hex_max_len; | |
189 | for(; i < minStrLen; i++, tmp += 1) | |
190 | sprintf(tmp, " "); | |
191 | ||
192 | return; | |
193 | } | |
194 | ||
55acbb2a | 195 | // printing and converting functions |
f397b5cc | 196 | |
4973f23d | 197 | char *sprint_hex(const uint8_t *data, const size_t len) { |
39cc1c87 | 198 | static char buf[4097] = {0}; |
b915fda3 | 199 | |
66efdc1f | 200 | hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, 0, 1, false); |
534983d7 | 201 | |
202 | return buf; | |
203 | } | |
f89c7050 | 204 | |
3c5fce2b | 205 | char *sprint_hex_inrow_ex(const uint8_t *data, const size_t len, const size_t min_str_len) { |
39cc1c87 | 206 | static char buf[4097] = {0}; |
3c5fce2b | 207 | |
66efdc1f | 208 | hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, min_str_len, 0, false); |
3c5fce2b OM |
209 | |
210 | return buf; | |
211 | } | |
212 | ||
213 | char *sprint_hex_inrow(const uint8_t *data, const size_t len) { | |
214 | return sprint_hex_inrow_ex(data, len, 0); | |
215 | } | |
216 | ||
2767fc02 | 217 | char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) { |
ace26dbd | 218 | // make sure we don't go beyond our char array memory |
7bc6fac3 | 219 | int max_len; |
220 | if (breaks==0) | |
221 | max_len = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len; | |
222 | else | |
223 | max_len = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks); | |
224 | ||
ace26dbd | 225 | static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits |
226 | //clear memory | |
227 | memset(buf, 0x00, sizeof(buf)); | |
2767fc02 | 228 | char *tmp = buf; |
79544b28 | 229 | |
ace26dbd | 230 | size_t in_index = 0; |
231 | // loop through the out_index to make sure we don't go too far | |
b97311b1 | 232 | for (size_t out_index=0; out_index < max_len; out_index++) { |
8ea57060 | 233 | // set character - (should be binary but verify it isn't more than 1 digit) |
234 | if (data[in_index]<10) | |
d1869c33 | 235 | sprintf(tmp++, "%u", (unsigned int) data[in_index]); |
6ca1477c | 236 | // check if a line break is needed and we have room to print it in our array |
b9957414 | 237 | if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 < max_len) ) { |
ace26dbd | 238 | // increment and print line break |
239 | out_index++; | |
2767fc02 | 240 | sprintf(tmp++, "%s","\n"); |
ace26dbd | 241 | } |
242 | in_index++; | |
2767fc02 | 243 | } |
79544b28 | 244 | |
245 | return buf; | |
246 | } | |
247 | ||
2767fc02 | 248 | char *sprint_bin(const uint8_t *data, const size_t len) { |
249 | return sprint_bin_break(data, len, 0); | |
250 | } | |
59f726c9 | 251 | |
3c5fce2b | 252 | char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len) { |
59f726c9 | 253 | static char buf[1024]; |
254 | char *tmp = buf; | |
255 | memset(buf, 0x00, 1024); | |
256 | size_t max_len = (len > 1010) ? 1010 : len; | |
257 | size_t i = 0; | |
258 | while(i < max_len){ | |
259 | char c = data[i]; | |
260 | tmp[i] = ((c < 32) || (c == 127)) ? '.' : c; | |
261 | ++i; | |
262 | } | |
3c5fce2b OM |
263 | |
264 | int minStrLen = min_str_len > i ? min_str_len : 0; | |
265 | for(; i < minStrLen; ++i) | |
266 | tmp[i] = ' '; | |
267 | ||
59f726c9 | 268 | return buf; |
269 | } | |
270 | ||
f89c7050 M |
271 | void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) |
272 | { | |
273 | while (len--) { | |
274 | dest[len] = (uint8_t) n; | |
275 | n >>= 8; | |
276 | } | |
277 | } | |
278 | ||
279 | uint64_t bytes_to_num(uint8_t* src, size_t len) | |
280 | { | |
281 | uint64_t num = 0; | |
282 | while (len--) | |
283 | { | |
284 | num = (num << 8) | (*src); | |
285 | src++; | |
286 | } | |
287 | return num; | |
288 | } | |
f397b5cc | 289 | |
709665b5 | 290 | void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) { |
291 | while (len--) { | |
292 | dest[len] = n & 1; | |
293 | n >>= 1; | |
294 | } | |
295 | } | |
296 | ||
59f726c9 | 297 | //least significant bit first |
298 | void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) { | |
299 | for(int i = 0 ; i < len ; ++i) { | |
300 | dest[i] = n & 1; | |
301 | n >>= 1; | |
302 | } | |
303 | } | |
304 | ||
2d42ea1e | 305 | // Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal |
306 | // And clears the rest of the bits. | |
307 | uint32_t SwapBits(uint32_t value, int nrbits) { | |
308 | uint32_t newvalue = 0; | |
309 | for(int i = 0; i < nrbits; i++) { | |
310 | newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i); | |
311 | } | |
312 | return newvalue; | |
313 | } | |
59f726c9 | 314 | |
f9848fd6 | 315 | // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp |
316 | // to | |
317 | // hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii | |
318 | // up to 64 bytes or 512 bits | |
2b3af97d | 319 | uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){ |
320 | static uint8_t buf[64]; | |
321 | memset(buf, 0x00, 64); | |
322 | uint8_t *tmp = buf; | |
323 | for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){ | |
324 | for (size_t i = 0; i < blockSize; i++){ | |
325 | tmp[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)]; | |
f9848fd6 | 326 | } |
327 | } | |
2b3af97d | 328 | return tmp; |
f9848fd6 | 329 | } |
330 | ||
79544b28 | 331 | //assumes little endian |
332 | char * printBits(size_t const size, void const * const ptr) | |
333 | { | |
334 | unsigned char *b = (unsigned char*) ptr; | |
335 | unsigned char byte; | |
336 | static char buf[1024]; | |
337 | char * tmp = buf; | |
338 | int i, j; | |
339 | ||
340 | for (i=size-1;i>=0;i--) | |
341 | { | |
342 | for (j=7;j>=0;j--) | |
343 | { | |
344 | byte = b[i] & (1<<j); | |
345 | byte >>= j; | |
d1869c33 | 346 | sprintf(tmp, "%u", (unsigned int)byte); |
79544b28 | 347 | tmp++; |
348 | } | |
349 | } | |
350 | return buf; | |
351 | } | |
352 | ||
a37725fa OM |
353 | char * printBitsPar(const uint8_t *b, size_t len) { |
354 | static char buf1[512] = {0}; | |
355 | static char buf2[512] = {0}; | |
356 | static char *buf; | |
357 | if (buf != buf1) | |
358 | buf = buf1; | |
359 | else | |
360 | buf = buf2; | |
361 | memset(buf, 0x00, 512); | |
362 | ||
363 | for (int i = 0; i < len; i++) { | |
364 | buf[i] = ((b[i / 8] << (i % 8)) & 0x80) ? '1':'0'; | |
365 | } | |
366 | return buf; | |
367 | } | |
368 | ||
369 | ||
9ca155ba M |
370 | // ------------------------------------------------------------------------- |
371 | // string parameters lib | |
372 | // ------------------------------------------------------------------------- | |
373 | ||
f397b5cc M |
374 | // ------------------------------------------------------------------------- |
375 | // line - param line | |
3a05a1e7 | 376 | // bg, en - symbol numbers in param line of beginning and ending parameter |
f397b5cc M |
377 | // paramnum - param number (from 0) |
378 | // ------------------------------------------------------------------------- | |
379 | int param_getptr(const char *line, int *bg, int *en, int paramnum) | |
380 | { | |
381 | int i; | |
382 | int len = strlen(line); | |
383 | ||
384 | *bg = 0; | |
385 | *en = 0; | |
386 | ||
387 | // skip spaces | |
388 | while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++; | |
389 | if (*bg >= len) { | |
390 | return 1; | |
391 | } | |
392 | ||
393 | for (i = 0; i < paramnum; i++) { | |
394 | while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++; | |
395 | while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++; | |
396 | ||
397 | if (line[*bg] == '\0') return 1; | |
398 | } | |
399 | ||
400 | *en = *bg; | |
401 | while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++; | |
402 | ||
403 | (*en)--; | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
31abe49f | 408 | |
3a05a1e7 OM |
409 | int param_getlength(const char *line, int paramnum) |
410 | { | |
411 | int bg, en; | |
412 | ||
413 | if (param_getptr(line, &bg, &en, paramnum)) return 0; | |
414 | ||
415 | return en - bg + 1; | |
416 | } | |
417 | ||
8019540b | 418 | char param_getchar(const char *line, int paramnum) { |
419 | return param_getchar_indx(line, 0, paramnum); | |
420 | } | |
421 | ||
422 | char param_getchar_indx(const char *line, int indx, int paramnum) { | |
f397b5cc M |
423 | int bg, en; |
424 | ||
425 | if (param_getptr(line, &bg, &en, paramnum)) return 0x00; | |
426 | ||
8019540b | 427 | if (bg + indx > en) |
428 | return '\0'; | |
429 | ||
430 | return line[bg + indx]; | |
f397b5cc M |
431 | } |
432 | ||
433 | uint8_t param_get8(const char *line, int paramnum) | |
434 | { | |
6ca1477c | 435 | return param_get8ex(line, paramnum, 0, 10); |
f397b5cc M |
436 | } |
437 | ||
f6d9fb17 | 438 | /** |
31abe49f | 439 | * @brief Reads a decimal integer (actually, 0-254, not 255) |
f6d9fb17 MHS |
440 | * @param line |
441 | * @param paramnum | |
31abe49f | 442 | * @return -1 if error |
f6d9fb17 MHS |
443 | */ |
444 | uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination) | |
445 | { | |
31abe49f | 446 | uint8_t val = param_get8ex(line, paramnum, 255, 10); |
31abe49f | 447 | if( (int8_t) val == -1) return 1; |
f6d9fb17 MHS |
448 | (*destination) = val; |
449 | return 0; | |
450 | } | |
451 | /** | |
452 | * @brief Checks if param is decimal | |
453 | * @param line | |
454 | * @param paramnum | |
455 | * @return | |
456 | */ | |
457 | uint8_t param_isdec(const char *line, int paramnum) | |
458 | { | |
459 | int bg, en; | |
460 | //TODO, check more thorougly | |
461 | if (!param_getptr(line, &bg, &en, paramnum)) return 1; | |
462 | // return strtoul(&line[bg], NULL, 10) & 0xff; | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
f397b5cc M |
467 | uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base) |
468 | { | |
469 | int bg, en; | |
470 | ||
471 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 472 | return strtoul(&line[bg], NULL, base) & 0xff; |
f397b5cc M |
473 | else |
474 | return deflt; | |
475 | } | |
476 | ||
477 | uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base) | |
478 | { | |
479 | int bg, en; | |
480 | ||
481 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 482 | return strtoul(&line[bg], NULL, base); |
f397b5cc M |
483 | else |
484 | return deflt; | |
485 | } | |
486 | ||
487 | uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base) | |
488 | { | |
489 | int bg, en; | |
490 | ||
491 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 492 | return strtoull(&line[bg], NULL, base); |
f397b5cc M |
493 | else |
494 | return deflt; | |
f397b5cc M |
495 | } |
496 | ||
497 | int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt) | |
498 | { | |
499 | int bg, en, temp, i; | |
500 | ||
501 | if (hexcnt % 2) | |
502 | return 1; | |
503 | ||
504 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
505 | ||
506 | if (en - bg + 1 != hexcnt) | |
507 | return 1; | |
508 | ||
509 | for(i = 0; i < hexcnt; i += 2) { | |
3ded0f97 | 510 | if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1; |
f397b5cc M |
511 | |
512 | sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp); | |
513 | data[i / 2] = temp & 0xff; | |
514 | } | |
515 | ||
516 | return 0; | |
517 | } | |
1a5a73ab | 518 | int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt) |
519 | { | |
520 | int bg, en, temp, i; | |
521 | ||
522 | //if (hexcnt % 2) | |
523 | // return 1; | |
524 | ||
525 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
526 | ||
527 | *hexcnt = en - bg + 1; | |
528 | if (*hexcnt % 2) //error if not complete hex bytes | |
529 | return 1; | |
aea4d766 | 530 | |
1a5a73ab | 531 | for(i = 0; i < *hexcnt; i += 2) { |
3ded0f97 | 532 | if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1; |
1a5a73ab | 533 | |
534 | sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp); | |
535 | data[i / 2] = temp & 0xff; | |
536 | } | |
537 | ||
538 | return 0; | |
539 | } | |
8019540b | 540 | |
541 | int param_gethex_to_eol(const char *line, int paramnum, uint8_t * data, int maxdatalen, int *datalen) { | |
542 | int bg, en; | |
543 | uint32_t temp; | |
544 | char buf[5] = {0}; | |
545 | ||
546 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
547 | ||
548 | *datalen = 0; | |
549 | ||
550 | int indx = bg; | |
551 | while (line[indx]) { | |
3c5fce2b OM |
552 | if (line[indx] == '\t' || line[indx] == ' ') { |
553 | indx++; | |
8019540b | 554 | continue; |
3c5fce2b | 555 | } |
8019540b | 556 | |
3ded0f97 | 557 | if (isxdigit((unsigned char)line[indx])) { |
8019540b | 558 | buf[strlen(buf) + 1] = 0x00; |
559 | buf[strlen(buf)] = line[indx]; | |
560 | } else { | |
561 | // if we have symbols other than spaces and hex | |
562 | return 1; | |
563 | } | |
564 | ||
565 | if (*datalen >= maxdatalen) { | |
566 | // if we dont have space in buffer and have symbols to translate | |
567 | return 2; | |
568 | } | |
569 | ||
570 | if (strlen(buf) >= 2) { | |
571 | sscanf(buf, "%x", &temp); | |
572 | data[*datalen] = (uint8_t)(temp & 0xff); | |
573 | *buf = 0; | |
574 | (*datalen)++; | |
575 | } | |
576 | ||
577 | indx++; | |
578 | } | |
579 | ||
580 | if (strlen(buf) > 0) | |
581 | //error when not completed hex bytes | |
582 | return 3; | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
874572d4 | 587 | int param_getstr(const char *line, int paramnum, char * str, size_t buffersize) |
aea4d766 | 588 | { |
589 | int bg, en; | |
590 | ||
874572d4 WM |
591 | if (param_getptr(line, &bg, &en, paramnum)) { |
592 | return 0; | |
593 | } | |
594 | ||
595 | // Prevent out of bounds errors | |
596 | if (en - bg + 1 >= buffersize) { | |
c95affa8 | 597 | printf("out of bounds error: want %d bytes have %zd bytes\n", en - bg + 1 + 1, buffersize); |
874572d4 WM |
598 | return 0; |
599 | } | |
aea4d766 | 600 | |
601 | memcpy(str, line + bg, en - bg + 1); | |
602 | str[en - bg + 1] = 0; | |
603 | ||
604 | return en - bg + 1; | |
605 | } | |
79544b28 | 606 | |
607 | /* | |
608 | The following methods comes from Rfidler sourcecode. | |
609 | https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/ | |
610 | */ | |
611 | ||
612 | // convert hex to sequence of 0/1 bit values | |
613 | // returns number of bits converted | |
614 | int hextobinarray(char *target, char *source) | |
615 | { | |
616 | int length, i, count= 0; | |
874572d4 | 617 | char* start = source; |
79544b28 | 618 | char x; |
619 | ||
620 | length = strlen(source); | |
621 | // process 4 bits (1 hex digit) at a time | |
622 | while(length--) | |
623 | { | |
624 | x= *(source++); | |
625 | // capitalize | |
626 | if (x >= 'a' && x <= 'f') | |
627 | x -= 32; | |
628 | // convert to numeric value | |
629 | if (x >= '0' && x <= '9') | |
630 | x -= '0'; | |
631 | else if (x >= 'A' && x <= 'F') | |
632 | x -= 'A' - 10; | |
874572d4 | 633 | else { |
3ded0f97 | 634 | printf("Discovered unknown character %c %d at idx %tu of %s\n", x, x, source - start, start); |
79544b28 | 635 | return 0; |
874572d4 | 636 | } |
79544b28 | 637 | // output |
638 | for(i= 0 ; i < 4 ; ++i, ++count) | |
639 | *(target++)= (x >> (3 - i)) & 1; | |
640 | } | |
641 | ||
642 | return count; | |
643 | } | |
644 | ||
79544b28 | 645 | // convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source) |
646 | // return number of bits converted | |
1c4c0b06 | 647 | int binarraytohex(char *target,char *source, int length) |
79544b28 | 648 | { |
649 | unsigned char i, x; | |
650 | int j = length; | |
651 | ||
652 | if(j % 4) | |
653 | return 0; | |
654 | ||
655 | while(j) | |
656 | { | |
657 | for(i= x= 0 ; i < 4 ; ++i) | |
658 | x += ( source[i] << (3 - i)); | |
d1869c33 | 659 | sprintf(target,"%X", (unsigned int)x); |
79544b28 | 660 | ++target; |
661 | source += 4; | |
662 | j -= 4; | |
663 | } | |
664 | return length; | |
665 | } | |
666 | ||
79544b28 | 667 | // return parity bit required to match type |
709665b5 | 668 | uint8_t GetParity( uint8_t *bits, uint8_t type, int length) |
79544b28 | 669 | { |
670 | int x; | |
671 | ||
672 | for(x= 0 ; length > 0 ; --length) | |
673 | x += bits[length - 1]; | |
674 | x %= 2; | |
675 | ||
676 | return x ^ type; | |
677 | } | |
678 | ||
679 | // add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half | |
709665b5 | 680 | void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length) |
79544b28 | 681 | { |
682 | *(target++)= GetParity(source, EVEN, length / 2); | |
683 | memcpy(target, source, length); | |
684 | target += length; | |
685 | *(target)= GetParity(source + length / 2, ODD, length / 2); | |
686 | } | |
4973f23d | 687 | |
59f726c9 | 688 | // xor two arrays together for len items. The dst array contains the new xored values. |
4973f23d | 689 | void xor(unsigned char *dst, unsigned char *src, size_t len) { |
690 | for( ; len > 0; len--,dst++,src++) | |
691 | *dst ^= *src; | |
692 | } | |
693 | ||
c4c3af7c | 694 | // RotateLeft - Ultralight, Desfire, works on byte level |
695 | // 00-01-02 >> 01-02-00 | |
696 | void rol(uint8_t *data, const size_t len){ | |
697 | uint8_t first = data[0]; | |
698 | for (size_t i = 0; i < len-1; i++) { | |
699 | data[i] = data[i+1]; | |
700 | } | |
701 | data[len-1] = first; | |
702 | } | |
d172c17c JC |
703 | |
704 | ||
705 | // Replace unprintable characters with a dot in char buffer | |
706 | void clean_ascii(unsigned char *buf, size_t len) { | |
707 | for (size_t i = 0; i < len; i++) { | |
708 | if (!isprint(buf[i])) | |
709 | buf[i] = '.'; | |
710 | } | |
711 | } | |
acf0582d | 712 | |
aa757f71 OM |
713 | // replace \r \n to \0 |
714 | void strcleanrn(char *buf, size_t len) { | |
715 | strcreplace(buf, len, '\n', '\0'); | |
716 | strcreplace(buf, len, '\r', '\0'); | |
717 | } | |
acf0582d | 718 | |
aa757f71 OM |
719 | // replace char in buffer |
720 | void strcreplace(char *buf, size_t len, char from, char to) { | |
721 | for (size_t i = 0; i < len; i++) { | |
722 | if (buf[i] == from) | |
723 | buf[i] = to; | |
724 | } | |
725 | } | |
726 | ||
727 | char *strmcopy(char *buf) { | |
728 | char * str = NULL; | |
729 | if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) { | |
730 | memset(str, 0, strlen(buf) + 1); | |
731 | strcpy(str, buf); | |
732 | } | |
733 | return str; | |
734 | } | |
acf0582d | 735 | |
c48c4d78 | 736 | |
737 | // determine number of logical CPU cores (use for multithreaded functions) | |
738 | extern int num_CPUs(void) | |
739 | { | |
740 | #if defined(_WIN32) | |
741 | #include <sysinfoapi.h> | |
742 | SYSTEM_INFO sysinfo; | |
743 | GetSystemInfo(&sysinfo); | |
744 | return sysinfo.dwNumberOfProcessors; | |
745 | #elif defined(__linux__) || defined(__APPLE__) | |
746 | #include <unistd.h> | |
747 | return sysconf(_SC_NPROCESSORS_ONLN); | |
748 | #else | |
749 | return 1; | |
750 | #endif | |
751 | } | |
ec9c7112 | 752 |