]>
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 | ||
acf0582d | 11 | #if !defined(_WIN32) |
12 | #define _POSIX_C_SOURCE 199309L // need nanosleep() | |
13 | #endif | |
14 | ||
20f9a2a1 | 15 | #include "util.h" |
acf0582d | 16 | |
17 | #include <stdint.h> | |
18 | #include <string.h> | |
19 | #include <ctype.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <time.h> | |
23 | #include "data.h" | |
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> | |
79544b28 | 30 | |
f397b5cc M |
31 | int ukbhit(void) |
32 | { | |
33 | int cnt = 0; | |
34 | int error; | |
35 | static struct termios Otty, Ntty; | |
36 | ||
37 | ||
8ea57060 | 38 | if ( tcgetattr( 0, &Otty) == -1 ) return -1; |
f397b5cc M |
39 | Ntty = Otty; |
40 | ||
41 | Ntty.c_iflag = 0; /* input mode */ | |
42 | Ntty.c_oflag = 0; /* output mode */ | |
43 | Ntty.c_lflag &= ~ICANON; /* raw mode */ | |
44 | Ntty.c_cc[VMIN] = CMIN; /* minimum time to wait */ | |
45 | Ntty.c_cc[VTIME] = CTIME; /* minimum characters to wait for */ | |
46 | ||
47 | if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) { | |
48 | error += ioctl(0, FIONREAD, &cnt); | |
49 | error += tcsetattr(0, TCSANOW, &Otty); | |
50 | } | |
51 | ||
52 | return ( error == 0 ? cnt : -1 ); | |
53 | } | |
54 | ||
55 | #else | |
acf0582d | 56 | |
f397b5cc M |
57 | #include <conio.h> |
58 | int ukbhit(void) { | |
59 | return kbhit(); | |
60 | } | |
61 | #endif | |
62 | ||
55acbb2a | 63 | // log files functions |
b915fda3 | 64 | void AddLogLine(char *file, char *extData, char *c) { |
55acbb2a | 65 | FILE *fLog = NULL; |
b915fda3 | 66 | char filename[FILE_PATH_SIZE] = {0x00}; |
67 | int len = 0; | |
68 | ||
69 | len = strlen(file); | |
70 | if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE; | |
71 | memcpy(filename, file, len); | |
72 | ||
73 | fLog = fopen(filename, "a"); | |
55acbb2a | 74 | if (!fLog) { |
b915fda3 | 75 | printf("Could not append log file %s", filename); |
55acbb2a M |
76 | return; |
77 | } | |
78 | ||
79 | fprintf(fLog, "%s", extData); | |
80 | fprintf(fLog, "%s\n", c); | |
81 | fclose(fLog); | |
82 | } | |
83 | ||
84 | void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){ | |
85 | AddLogLine(fileName, extData, sprint_hex(data, len)); | |
86 | } | |
87 | ||
88 | void AddLogUint64(char *fileName, char *extData, const uint64_t data) { | |
89 | char buf[100] = {0}; | |
90 | sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF)); | |
91 | AddLogLine(fileName, extData, buf); | |
92 | } | |
93 | ||
94 | void AddLogCurrentDT(char *fileName) { | |
95 | char buff[20]; | |
96 | struct tm *curTime; | |
97 | ||
98 | time_t now = time(0); | |
99 | curTime = gmtime(&now); | |
100 | ||
101 | strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime); | |
102 | AddLogLine(fileName, "\nanticollision: ", buff); | |
103 | } | |
104 | ||
e0c635d1 | 105 | void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) { |
55acbb2a M |
106 | char * fnameptr = fileName; |
107 | memset(fileName, 0x00, 200); | |
108 | ||
e0c635d1 | 109 | for (int j = 0; j < byteCount; j++, fnameptr += 2) |
d1869c33 | 110 | sprintf(fnameptr, "%02x", (unsigned int) uid[j]); |
55acbb2a | 111 | sprintf(fnameptr, "%s", ext); |
55acbb2a M |
112 | } |
113 | ||
114 | // printing and converting functions | |
f397b5cc | 115 | |
534983d7 | 116 | void print_hex(const uint8_t * data, const size_t len) |
117 | { | |
118 | size_t i; | |
119 | ||
120 | for (i=0; i < len; i++) | |
121 | printf("%02x ", data[i]); | |
122 | ||
123 | printf("\n"); | |
124 | } | |
125 | ||
59f726c9 | 126 | void print_hex_break(const uint8_t *data, const size_t len, uint8_t breaks) { |
127 | ||
128 | int rownum = 0; | |
129 | printf("[%02d] | ", rownum); | |
130 | for (int i = 0; i < len; ++i) { | |
131 | ||
132 | printf("%02X ", data[i]); | |
133 | ||
134 | // check if a line break is needed | |
135 | if ( breaks > 0 && !((i+1) % breaks) && (i+1 < len) ) { | |
136 | ++rownum; | |
137 | printf("\n[%02d] | ", rownum); | |
138 | } | |
139 | } | |
140 | printf("\n"); | |
141 | } | |
142 | ||
4973f23d | 143 | char *sprint_hex(const uint8_t *data, const size_t len) { |
b915fda3 | 144 | |
145 | int maxLen = ( len > 1024/3) ? 1024/3 : len; | |
534983d7 | 146 | static char buf[1024]; |
c585a5cf | 147 | memset(buf, 0x00, 1024); |
4973f23d | 148 | char *tmp = buf; |
534983d7 | 149 | size_t i; |
150 | ||
b915fda3 | 151 | for (i=0; i < maxLen; ++i, tmp += 3) |
d1869c33 | 152 | sprintf(tmp, "%02x ", (unsigned int) data[i]); |
534983d7 | 153 | |
154 | return buf; | |
155 | } | |
f89c7050 | 156 | |
2767fc02 | 157 | char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) { |
ace26dbd | 158 | // make sure we don't go beyond our char array memory |
7bc6fac3 | 159 | int max_len; |
160 | if (breaks==0) | |
161 | max_len = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len; | |
162 | else | |
163 | max_len = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks); | |
164 | ||
ace26dbd | 165 | static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits |
166 | //clear memory | |
167 | memset(buf, 0x00, sizeof(buf)); | |
2767fc02 | 168 | char *tmp = buf; |
79544b28 | 169 | |
ace26dbd | 170 | size_t in_index = 0; |
171 | // loop through the out_index to make sure we don't go too far | |
59f726c9 | 172 | for (size_t out_index=0; out_index < max_len-1; out_index++) { |
8ea57060 | 173 | // set character - (should be binary but verify it isn't more than 1 digit) |
174 | if (data[in_index]<10) | |
d1869c33 | 175 | sprintf(tmp++, "%u", (unsigned int) data[in_index]); |
6ca1477c | 176 | // check if a line break is needed and we have room to print it in our array |
ace26dbd | 177 | if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 != max_len) ) { |
178 | // increment and print line break | |
179 | out_index++; | |
2767fc02 | 180 | sprintf(tmp++, "%s","\n"); |
ace26dbd | 181 | } |
182 | in_index++; | |
2767fc02 | 183 | } |
79544b28 | 184 | |
185 | return buf; | |
186 | } | |
187 | ||
2767fc02 | 188 | char *sprint_bin(const uint8_t *data, const size_t len) { |
189 | return sprint_bin_break(data, len, 0); | |
190 | } | |
59f726c9 | 191 | |
192 | char *sprint_hex_ascii(const uint8_t *data, const size_t len) { | |
193 | static char buf[1024]; | |
194 | char *tmp = buf; | |
195 | memset(buf, 0x00, 1024); | |
196 | size_t max_len = (len > 1010) ? 1010 : len; | |
197 | ||
198 | sprintf(tmp, "%s| ", sprint_hex(data, max_len) ); | |
199 | ||
200 | size_t i = 0; | |
201 | size_t pos = (max_len * 3)+2; | |
202 | while(i < max_len){ | |
203 | char c = data[i]; | |
204 | if ( (c < 32) || (c == 127)) | |
205 | c = '.'; | |
206 | sprintf(tmp+pos+i, "%c", c); | |
207 | ++i; | |
208 | } | |
209 | return buf; | |
210 | } | |
211 | ||
212 | char *sprint_ascii(const uint8_t *data, const size_t len) { | |
213 | static char buf[1024]; | |
214 | char *tmp = buf; | |
215 | memset(buf, 0x00, 1024); | |
216 | size_t max_len = (len > 1010) ? 1010 : len; | |
217 | size_t i = 0; | |
218 | while(i < max_len){ | |
219 | char c = data[i]; | |
220 | tmp[i] = ((c < 32) || (c == 127)) ? '.' : c; | |
221 | ++i; | |
222 | } | |
223 | return buf; | |
224 | } | |
225 | ||
f89c7050 M |
226 | void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) |
227 | { | |
228 | while (len--) { | |
229 | dest[len] = (uint8_t) n; | |
230 | n >>= 8; | |
231 | } | |
232 | } | |
233 | ||
234 | uint64_t bytes_to_num(uint8_t* src, size_t len) | |
235 | { | |
236 | uint64_t num = 0; | |
237 | while (len--) | |
238 | { | |
239 | num = (num << 8) | (*src); | |
240 | src++; | |
241 | } | |
242 | return num; | |
243 | } | |
f397b5cc | 244 | |
709665b5 | 245 | void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) { |
246 | while (len--) { | |
247 | dest[len] = n & 1; | |
248 | n >>= 1; | |
249 | } | |
250 | } | |
251 | ||
59f726c9 | 252 | //least significant bit first |
253 | void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) { | |
254 | for(int i = 0 ; i < len ; ++i) { | |
255 | dest[i] = n & 1; | |
256 | n >>= 1; | |
257 | } | |
258 | } | |
259 | ||
260 | ||
f9848fd6 | 261 | // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp |
262 | // to | |
263 | // hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii | |
264 | // up to 64 bytes or 512 bits | |
2b3af97d | 265 | uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){ |
266 | static uint8_t buf[64]; | |
267 | memset(buf, 0x00, 64); | |
268 | uint8_t *tmp = buf; | |
269 | for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){ | |
270 | for (size_t i = 0; i < blockSize; i++){ | |
271 | tmp[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)]; | |
f9848fd6 | 272 | } |
273 | } | |
2b3af97d | 274 | return tmp; |
f9848fd6 | 275 | } |
276 | ||
59f726c9 | 277 | // takes a uint8_t src array, for len items and reverses the byte order in blocksizes (8,16,32,64), |
278 | // returns: the dest array contains the reordered src array. | |
279 | void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest){ | |
280 | for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){ | |
281 | for (size_t i = 0; i < blockSize; i++){ | |
282 | dest[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)]; | |
283 | } | |
284 | } | |
285 | } | |
286 | ||
79544b28 | 287 | //assumes little endian |
288 | char * printBits(size_t const size, void const * const ptr) | |
289 | { | |
290 | unsigned char *b = (unsigned char*) ptr; | |
291 | unsigned char byte; | |
292 | static char buf[1024]; | |
293 | char * tmp = buf; | |
294 | int i, j; | |
295 | ||
296 | for (i=size-1;i>=0;i--) | |
297 | { | |
298 | for (j=7;j>=0;j--) | |
299 | { | |
300 | byte = b[i] & (1<<j); | |
301 | byte >>= j; | |
d1869c33 | 302 | sprintf(tmp, "%u", (unsigned int)byte); |
79544b28 | 303 | tmp++; |
304 | } | |
305 | } | |
306 | return buf; | |
307 | } | |
308 | ||
9ca155ba M |
309 | // ------------------------------------------------------------------------- |
310 | // string parameters lib | |
311 | // ------------------------------------------------------------------------- | |
312 | ||
f397b5cc M |
313 | // ------------------------------------------------------------------------- |
314 | // line - param line | |
315 | // bg, en - symbol numbers in param line of beginning an ending parameter | |
316 | // paramnum - param number (from 0) | |
317 | // ------------------------------------------------------------------------- | |
318 | int param_getptr(const char *line, int *bg, int *en, int paramnum) | |
319 | { | |
320 | int i; | |
321 | int len = strlen(line); | |
322 | ||
323 | *bg = 0; | |
324 | *en = 0; | |
325 | ||
326 | // skip spaces | |
327 | while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++; | |
328 | if (*bg >= len) { | |
329 | return 1; | |
330 | } | |
331 | ||
332 | for (i = 0; i < paramnum; i++) { | |
333 | while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++; | |
334 | while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++; | |
335 | ||
336 | if (line[*bg] == '\0') return 1; | |
337 | } | |
338 | ||
339 | *en = *bg; | |
340 | while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++; | |
341 | ||
342 | (*en)--; | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
31abe49f | 347 | |
f397b5cc M |
348 | char param_getchar(const char *line, int paramnum) |
349 | { | |
350 | int bg, en; | |
351 | ||
352 | if (param_getptr(line, &bg, &en, paramnum)) return 0x00; | |
353 | ||
354 | return line[bg]; | |
355 | } | |
356 | ||
357 | uint8_t param_get8(const char *line, int paramnum) | |
358 | { | |
6ca1477c | 359 | return param_get8ex(line, paramnum, 0, 10); |
f397b5cc M |
360 | } |
361 | ||
f6d9fb17 | 362 | /** |
31abe49f | 363 | * @brief Reads a decimal integer (actually, 0-254, not 255) |
f6d9fb17 MHS |
364 | * @param line |
365 | * @param paramnum | |
31abe49f | 366 | * @return -1 if error |
f6d9fb17 MHS |
367 | */ |
368 | uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination) | |
369 | { | |
31abe49f | 370 | uint8_t val = param_get8ex(line, paramnum, 255, 10); |
31abe49f | 371 | if( (int8_t) val == -1) return 1; |
f6d9fb17 MHS |
372 | (*destination) = val; |
373 | return 0; | |
374 | } | |
375 | /** | |
376 | * @brief Checks if param is decimal | |
377 | * @param line | |
378 | * @param paramnum | |
379 | * @return | |
380 | */ | |
381 | uint8_t param_isdec(const char *line, int paramnum) | |
382 | { | |
383 | int bg, en; | |
384 | //TODO, check more thorougly | |
385 | if (!param_getptr(line, &bg, &en, paramnum)) return 1; | |
386 | // return strtoul(&line[bg], NULL, 10) & 0xff; | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
f397b5cc M |
391 | uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base) |
392 | { | |
393 | int bg, en; | |
394 | ||
395 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 396 | return strtoul(&line[bg], NULL, base) & 0xff; |
f397b5cc M |
397 | else |
398 | return deflt; | |
399 | } | |
400 | ||
401 | uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base) | |
402 | { | |
403 | int bg, en; | |
404 | ||
405 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 406 | return strtoul(&line[bg], NULL, base); |
f397b5cc M |
407 | else |
408 | return deflt; | |
409 | } | |
410 | ||
411 | uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base) | |
412 | { | |
413 | int bg, en; | |
414 | ||
415 | if (!param_getptr(line, &bg, &en, paramnum)) | |
6c6d1ac1 | 416 | return strtoull(&line[bg], NULL, base); |
f397b5cc M |
417 | else |
418 | return deflt; | |
f397b5cc M |
419 | } |
420 | ||
421 | int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt) | |
422 | { | |
423 | int bg, en, temp, i; | |
424 | ||
425 | if (hexcnt % 2) | |
426 | return 1; | |
427 | ||
428 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
429 | ||
430 | if (en - bg + 1 != hexcnt) | |
431 | return 1; | |
432 | ||
433 | for(i = 0; i < hexcnt; i += 2) { | |
434 | if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1; | |
435 | ||
436 | sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp); | |
437 | data[i / 2] = temp & 0xff; | |
438 | } | |
439 | ||
440 | return 0; | |
441 | } | |
1a5a73ab | 442 | int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt) |
443 | { | |
444 | int bg, en, temp, i; | |
445 | ||
446 | //if (hexcnt % 2) | |
447 | // return 1; | |
448 | ||
449 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
450 | ||
451 | *hexcnt = en - bg + 1; | |
452 | if (*hexcnt % 2) //error if not complete hex bytes | |
453 | return 1; | |
aea4d766 | 454 | |
1a5a73ab | 455 | for(i = 0; i < *hexcnt; i += 2) { |
456 | if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1; | |
457 | ||
458 | sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp); | |
459 | data[i / 2] = temp & 0xff; | |
460 | } | |
461 | ||
462 | return 0; | |
463 | } | |
aea4d766 | 464 | int param_getstr(const char *line, int paramnum, char * str) |
465 | { | |
466 | int bg, en; | |
467 | ||
468 | if (param_getptr(line, &bg, &en, paramnum)) return 0; | |
469 | ||
470 | memcpy(str, line + bg, en - bg + 1); | |
471 | str[en - bg + 1] = 0; | |
472 | ||
473 | return en - bg + 1; | |
474 | } | |
79544b28 | 475 | |
476 | /* | |
477 | The following methods comes from Rfidler sourcecode. | |
478 | https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/ | |
479 | */ | |
480 | ||
481 | // convert hex to sequence of 0/1 bit values | |
482 | // returns number of bits converted | |
483 | int hextobinarray(char *target, char *source) | |
484 | { | |
485 | int length, i, count= 0; | |
486 | char x; | |
487 | ||
488 | length = strlen(source); | |
489 | // process 4 bits (1 hex digit) at a time | |
490 | while(length--) | |
491 | { | |
492 | x= *(source++); | |
493 | // capitalize | |
494 | if (x >= 'a' && x <= 'f') | |
495 | x -= 32; | |
496 | // convert to numeric value | |
497 | if (x >= '0' && x <= '9') | |
498 | x -= '0'; | |
499 | else if (x >= 'A' && x <= 'F') | |
500 | x -= 'A' - 10; | |
501 | else | |
502 | return 0; | |
503 | // output | |
504 | for(i= 0 ; i < 4 ; ++i, ++count) | |
505 | *(target++)= (x >> (3 - i)) & 1; | |
506 | } | |
507 | ||
508 | return count; | |
509 | } | |
510 | ||
511 | // convert hex to human readable binary string | |
512 | int hextobinstring(char *target, char *source) | |
513 | { | |
514 | int length; | |
515 | ||
516 | if(!(length= hextobinarray(target, source))) | |
517 | return 0; | |
518 | binarraytobinstring(target, target, length); | |
519 | return length; | |
520 | } | |
521 | ||
522 | // convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source) | |
523 | // return number of bits converted | |
1c4c0b06 | 524 | int binarraytohex(char *target,char *source, int length) |
79544b28 | 525 | { |
526 | unsigned char i, x; | |
527 | int j = length; | |
528 | ||
529 | if(j % 4) | |
530 | return 0; | |
531 | ||
532 | while(j) | |
533 | { | |
534 | for(i= x= 0 ; i < 4 ; ++i) | |
535 | x += ( source[i] << (3 - i)); | |
d1869c33 | 536 | sprintf(target,"%X", (unsigned int)x); |
79544b28 | 537 | ++target; |
538 | source += 4; | |
539 | j -= 4; | |
540 | } | |
541 | return length; | |
542 | } | |
543 | ||
544 | // convert binary array to human readable binary | |
545 | void binarraytobinstring(char *target, char *source, int length) | |
546 | { | |
547 | int i; | |
548 | ||
549 | for(i= 0 ; i < length ; ++i) | |
550 | *(target++)= *(source++) + '0'; | |
551 | *target= '\0'; | |
552 | } | |
553 | ||
554 | // return parity bit required to match type | |
709665b5 | 555 | uint8_t GetParity( uint8_t *bits, uint8_t type, int length) |
79544b28 | 556 | { |
557 | int x; | |
558 | ||
559 | for(x= 0 ; length > 0 ; --length) | |
560 | x += bits[length - 1]; | |
561 | x %= 2; | |
562 | ||
563 | return x ^ type; | |
564 | } | |
565 | ||
566 | // add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half | |
709665b5 | 567 | void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length) |
79544b28 | 568 | { |
569 | *(target++)= GetParity(source, EVEN, length / 2); | |
570 | memcpy(target, source, length); | |
571 | target += length; | |
572 | *(target)= GetParity(source + length / 2, ODD, length / 2); | |
573 | } | |
4973f23d | 574 | |
59f726c9 | 575 | // xor two arrays together for len items. The dst array contains the new xored values. |
4973f23d | 576 | void xor(unsigned char *dst, unsigned char *src, size_t len) { |
577 | for( ; len > 0; len--,dst++,src++) | |
578 | *dst ^= *src; | |
579 | } | |
580 | ||
581 | int32_t le24toh (uint8_t data[3]) { | |
582 | return (data[2] << 16) | (data[1] << 8) | data[0]; | |
583 | } | |
c872d8c1 | 584 | uint32_t le32toh (uint8_t *data) { |
585 | return (uint32_t)( (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | data[0]); | |
586 | } | |
c4c3af7c | 587 | |
588 | // RotateLeft - Ultralight, Desfire, works on byte level | |
589 | // 00-01-02 >> 01-02-00 | |
590 | void rol(uint8_t *data, const size_t len){ | |
591 | uint8_t first = data[0]; | |
592 | for (size_t i = 0; i < len-1; i++) { | |
593 | data[i] = data[i+1]; | |
594 | } | |
595 | data[len-1] = first; | |
596 | } | |
d172c17c JC |
597 | |
598 | ||
599 | // Replace unprintable characters with a dot in char buffer | |
600 | void clean_ascii(unsigned char *buf, size_t len) { | |
601 | for (size_t i = 0; i < len; i++) { | |
602 | if (!isprint(buf[i])) | |
603 | buf[i] = '.'; | |
604 | } | |
605 | } | |
acf0582d | 606 | |
607 | ||
608 | // Timer functions | |
609 | #if !defined (_WIN32) | |
610 | #include <errno.h> | |
611 | ||
612 | static void nsleep(uint64_t n) { | |
613 | struct timespec timeout; | |
614 | timeout.tv_sec = n/1000000000; | |
615 | timeout.tv_nsec = n%1000000000; | |
616 | while (nanosleep(&timeout, &timeout) && errno == EINTR); | |
617 | } | |
618 | ||
619 | void msleep(uint32_t n) { | |
620 | nsleep(1000000 * n); | |
621 | } | |
622 | ||
623 | #endif // _WIN32 | |
624 | ||
625 | // a milliseconds timer for performance measurement | |
626 | uint64_t msclock() { | |
627 | #if defined(_WIN32) | |
628 | #include <sys/types.h> | |
629 | struct _timeb t; | |
630 | if (_ftime_s(&t)) { | |
631 | return 0; | |
632 | } else { | |
633 | return 1000 * t.time + t.millitm; | |
634 | } | |
635 | #else | |
636 | struct timespec t; | |
637 | clock_gettime(CLOCK_MONOTONIC, &t); | |
638 | return (t.tv_sec * 1000 + t.tv_nsec / 1000000); | |
639 | #endif | |
640 | } |