]> git.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
Revert "dbg: Watchdog resets while 'hw tune lf'"
[proxmark3-svn] / client / util.c
CommitLineData
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>
19#include "data.h"
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
32int 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
55#else
acf0582d 56
f397b5cc
M
57#include <conio.h>
58int ukbhit(void) {
59 return kbhit();
60}
61#endif
62
55acbb2a 63// log files functions
b915fda3 64void 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
84void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
85 AddLogLine(fileName, extData, sprint_hex(data, len));
86}
87
88void 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
94void 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 105void 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
66efdc1f 114void hex_to_buffer(const uint8_t *buf, const uint8_t *hex_data, const size_t hex_len, const size_t hex_max_len,
115 const size_t min_str_len, const size_t spaces_between, bool uppercase) {
116
117 char *tmp = (char *)buf;
118 size_t i;
119
120 int maxLen = ( hex_len > hex_max_len) ? hex_max_len : hex_len;
121
122 for (i = 0; i < maxLen; ++i, tmp += 2 + spaces_between) {
123 sprintf(tmp, (uppercase) ? "%02X" : "%02x", (unsigned int) hex_data[i]);
124
125 for (int j = 0; j < spaces_between; j++)
126 sprintf(tmp + 2 + j, " ");
127 }
128
129 i *= (2 + spaces_between);
130 int minStrLen = min_str_len > i ? min_str_len : 0;
131 if (minStrLen > hex_max_len)
132 minStrLen = hex_max_len;
133 for(; i < minStrLen; i++, tmp += 1)
134 sprintf(tmp, " ");
135
136 return;
137}
138
55acbb2a 139// printing and converting functions
f397b5cc 140
534983d7 141void print_hex(const uint8_t * data, const size_t len)
142{
143 size_t i;
144
145 for (i=0; i < len; i++)
146 printf("%02x ", data[i]);
147
148 printf("\n");
149}
150
59f726c9 151void print_hex_break(const uint8_t *data, const size_t len, uint8_t breaks) {
152
153 int rownum = 0;
154 printf("[%02d] | ", rownum);
155 for (int i = 0; i < len; ++i) {
156
157 printf("%02X ", data[i]);
158
159 // check if a line break is needed
160 if ( breaks > 0 && !((i+1) % breaks) && (i+1 < len) ) {
161 ++rownum;
162 printf("\n[%02d] | ", rownum);
163 }
164 }
165 printf("\n");
166}
167
4973f23d 168char *sprint_hex(const uint8_t *data, const size_t len) {
66efdc1f 169 static char buf[1025] = {0};
b915fda3 170
66efdc1f 171 hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, 0, 1, false);
534983d7 172
173 return buf;
174}
f89c7050 175
3c5fce2b 176char *sprint_hex_inrow_ex(const uint8_t *data, const size_t len, const size_t min_str_len) {
66efdc1f 177 static char buf[1025] = {0};
3c5fce2b 178
66efdc1f 179 hex_to_buffer((uint8_t *)buf, data, len, sizeof(buf) - 1, min_str_len, 0, false);
3c5fce2b
OM
180
181 return buf;
182}
183
184char *sprint_hex_inrow(const uint8_t *data, const size_t len) {
185 return sprint_hex_inrow_ex(data, len, 0);
186}
187
2767fc02 188char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) {
ace26dbd 189 // make sure we don't go beyond our char array memory
7bc6fac3 190 int max_len;
191 if (breaks==0)
192 max_len = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len;
193 else
194 max_len = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks);
195
ace26dbd 196 static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits
197 //clear memory
198 memset(buf, 0x00, sizeof(buf));
2767fc02 199 char *tmp = buf;
79544b28 200
ace26dbd 201 size_t in_index = 0;
202 // loop through the out_index to make sure we don't go too far
b97311b1 203 for (size_t out_index=0; out_index < max_len; out_index++) {
8ea57060 204 // set character - (should be binary but verify it isn't more than 1 digit)
205 if (data[in_index]<10)
d1869c33 206 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
6ca1477c 207 // check if a line break is needed and we have room to print it in our array
b9957414 208 if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 < max_len) ) {
ace26dbd 209 // increment and print line break
210 out_index++;
2767fc02 211 sprintf(tmp++, "%s","\n");
ace26dbd 212 }
213 in_index++;
2767fc02 214 }
79544b28 215
216 return buf;
217}
218
2767fc02 219char *sprint_bin(const uint8_t *data, const size_t len) {
220 return sprint_bin_break(data, len, 0);
221}
59f726c9 222
223char *sprint_hex_ascii(const uint8_t *data, const size_t len) {
224 static char buf[1024];
225 char *tmp = buf;
226 memset(buf, 0x00, 1024);
bf824347 227 size_t max_len = (len > 255) ? 255 : len;
228 // max 255 bytes * 3 + 2 characters = 767 in buffer
59f726c9 229 sprintf(tmp, "%s| ", sprint_hex(data, max_len) );
230
231 size_t i = 0;
232 size_t pos = (max_len * 3)+2;
bf824347 233 // add another 255 characters ascii = 1020 characters of buffer used
234 while(i < max_len) {
59f726c9 235 char c = data[i];
236 if ( (c < 32) || (c == 127))
237 c = '.';
238 sprintf(tmp+pos+i, "%c", c);
239 ++i;
240 }
241 return buf;
242}
243
3c5fce2b 244char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len) {
59f726c9 245 static char buf[1024];
246 char *tmp = buf;
247 memset(buf, 0x00, 1024);
248 size_t max_len = (len > 1010) ? 1010 : len;
249 size_t i = 0;
250 while(i < max_len){
251 char c = data[i];
252 tmp[i] = ((c < 32) || (c == 127)) ? '.' : c;
253 ++i;
254 }
3c5fce2b
OM
255
256 int minStrLen = min_str_len > i ? min_str_len : 0;
257 for(; i < minStrLen; ++i)
258 tmp[i] = ' ';
259
59f726c9 260 return buf;
261}
262
3c5fce2b
OM
263char *sprint_ascii(const uint8_t *data, const size_t len) {
264 return sprint_ascii_ex(data, len, 0);
265}
266
f89c7050
M
267void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
268{
269 while (len--) {
270 dest[len] = (uint8_t) n;
271 n >>= 8;
272 }
273}
274
275uint64_t bytes_to_num(uint8_t* src, size_t len)
276{
277 uint64_t num = 0;
278 while (len--)
279 {
280 num = (num << 8) | (*src);
281 src++;
282 }
283 return num;
284}
f397b5cc 285
709665b5 286void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) {
287 while (len--) {
288 dest[len] = n & 1;
289 n >>= 1;
290 }
291}
292
59f726c9 293//least significant bit first
294void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest) {
295 for(int i = 0 ; i < len ; ++i) {
296 dest[i] = n & 1;
297 n >>= 1;
298 }
299}
300
2d42ea1e 301// Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal
302// And clears the rest of the bits.
303uint32_t SwapBits(uint32_t value, int nrbits) {
304 uint32_t newvalue = 0;
305 for(int i = 0; i < nrbits; i++) {
306 newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i);
307 }
308 return newvalue;
309}
59f726c9 310
f9848fd6 311// aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp
312// to
313// hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii
314// up to 64 bytes or 512 bits
2b3af97d 315uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){
316 static uint8_t buf[64];
317 memset(buf, 0x00, 64);
318 uint8_t *tmp = buf;
319 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
320 for (size_t i = 0; i < blockSize; i++){
321 tmp[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)];
f9848fd6 322 }
323 }
2b3af97d 324 return tmp;
f9848fd6 325}
326
59f726c9 327// takes a uint8_t src array, for len items and reverses the byte order in blocksizes (8,16,32,64),
328// returns: the dest array contains the reordered src array.
329void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest){
330 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
331 for (size_t i = 0; i < blockSize; i++){
332 dest[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)];
333 }
334 }
335}
336
79544b28 337//assumes little endian
338char * printBits(size_t const size, void const * const ptr)
339{
340 unsigned char *b = (unsigned char*) ptr;
341 unsigned char byte;
342 static char buf[1024];
343 char * tmp = buf;
344 int i, j;
345
346 for (i=size-1;i>=0;i--)
347 {
348 for (j=7;j>=0;j--)
349 {
350 byte = b[i] & (1<<j);
351 byte >>= j;
d1869c33 352 sprintf(tmp, "%u", (unsigned int)byte);
79544b28 353 tmp++;
354 }
355 }
356 return buf;
357}
358
9ca155ba
M
359// -------------------------------------------------------------------------
360// string parameters lib
361// -------------------------------------------------------------------------
362
f397b5cc
M
363// -------------------------------------------------------------------------
364// line - param line
3a05a1e7 365// bg, en - symbol numbers in param line of beginning and ending parameter
f397b5cc
M
366// paramnum - param number (from 0)
367// -------------------------------------------------------------------------
368int param_getptr(const char *line, int *bg, int *en, int paramnum)
369{
370 int i;
371 int len = strlen(line);
372
373 *bg = 0;
374 *en = 0;
375
376 // skip spaces
377 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
378 if (*bg >= len) {
379 return 1;
380 }
381
382 for (i = 0; i < paramnum; i++) {
383 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
384 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
385
386 if (line[*bg] == '\0') return 1;
387 }
388
389 *en = *bg;
390 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
391
392 (*en)--;
393
394 return 0;
395}
396
31abe49f 397
3a05a1e7
OM
398int param_getlength(const char *line, int paramnum)
399{
400 int bg, en;
401
402 if (param_getptr(line, &bg, &en, paramnum)) return 0;
403
404 return en - bg + 1;
405}
406
8019540b 407char param_getchar(const char *line, int paramnum) {
408 return param_getchar_indx(line, 0, paramnum);
409}
410
411char param_getchar_indx(const char *line, int indx, int paramnum) {
f397b5cc
M
412 int bg, en;
413
414 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
415
8019540b 416 if (bg + indx > en)
417 return '\0';
418
419 return line[bg + indx];
f397b5cc
M
420}
421
422uint8_t param_get8(const char *line, int paramnum)
423{
6ca1477c 424 return param_get8ex(line, paramnum, 0, 10);
f397b5cc
M
425}
426
f6d9fb17 427/**
31abe49f 428 * @brief Reads a decimal integer (actually, 0-254, not 255)
f6d9fb17
MHS
429 * @param line
430 * @param paramnum
31abe49f 431 * @return -1 if error
f6d9fb17
MHS
432 */
433uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination)
434{
31abe49f 435 uint8_t val = param_get8ex(line, paramnum, 255, 10);
31abe49f 436 if( (int8_t) val == -1) return 1;
f6d9fb17
MHS
437 (*destination) = val;
438 return 0;
439}
440/**
441 * @brief Checks if param is decimal
442 * @param line
443 * @param paramnum
444 * @return
445 */
446uint8_t param_isdec(const char *line, int paramnum)
447{
448 int bg, en;
449 //TODO, check more thorougly
450 if (!param_getptr(line, &bg, &en, paramnum)) return 1;
451 // return strtoul(&line[bg], NULL, 10) & 0xff;
452
453 return 0;
454}
455
f397b5cc
M
456uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
457{
458 int bg, en;
459
460 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 461 return strtoul(&line[bg], NULL, base) & 0xff;
f397b5cc
M
462 else
463 return deflt;
464}
465
466uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
467{
468 int bg, en;
469
470 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 471 return strtoul(&line[bg], NULL, base);
f397b5cc
M
472 else
473 return deflt;
474}
475
476uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
477{
478 int bg, en;
479
480 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 481 return strtoull(&line[bg], NULL, base);
f397b5cc
M
482 else
483 return deflt;
f397b5cc
M
484}
485
486int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
487{
488 int bg, en, temp, i;
489
490 if (hexcnt % 2)
491 return 1;
492
493 if (param_getptr(line, &bg, &en, paramnum)) return 1;
494
495 if (en - bg + 1 != hexcnt)
496 return 1;
497
498 for(i = 0; i < hexcnt; i += 2) {
3ded0f97 499 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
f397b5cc
M
500
501 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
502 data[i / 2] = temp & 0xff;
503 }
504
505 return 0;
506}
1a5a73ab 507int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt)
508{
509 int bg, en, temp, i;
510
511 //if (hexcnt % 2)
512 // return 1;
513
514 if (param_getptr(line, &bg, &en, paramnum)) return 1;
515
516 *hexcnt = en - bg + 1;
517 if (*hexcnt % 2) //error if not complete hex bytes
518 return 1;
aea4d766 519
1a5a73ab 520 for(i = 0; i < *hexcnt; i += 2) {
3ded0f97 521 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
1a5a73ab 522
523 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
524 data[i / 2] = temp & 0xff;
525 }
526
527 return 0;
528}
8019540b 529
530int param_gethex_to_eol(const char *line, int paramnum, uint8_t * data, int maxdatalen, int *datalen) {
531 int bg, en;
532 uint32_t temp;
533 char buf[5] = {0};
534
535 if (param_getptr(line, &bg, &en, paramnum)) return 1;
536
537 *datalen = 0;
538
539 int indx = bg;
540 while (line[indx]) {
3c5fce2b
OM
541 if (line[indx] == '\t' || line[indx] == ' ') {
542 indx++;
8019540b 543 continue;
3c5fce2b 544 }
8019540b 545
3ded0f97 546 if (isxdigit((unsigned char)line[indx])) {
8019540b 547 buf[strlen(buf) + 1] = 0x00;
548 buf[strlen(buf)] = line[indx];
549 } else {
550 // if we have symbols other than spaces and hex
551 return 1;
552 }
553
554 if (*datalen >= maxdatalen) {
555 // if we dont have space in buffer and have symbols to translate
556 return 2;
557 }
558
559 if (strlen(buf) >= 2) {
560 sscanf(buf, "%x", &temp);
561 data[*datalen] = (uint8_t)(temp & 0xff);
562 *buf = 0;
563 (*datalen)++;
564 }
565
566 indx++;
567 }
568
569 if (strlen(buf) > 0)
570 //error when not completed hex bytes
571 return 3;
572
573 return 0;
574}
575
874572d4 576int param_getstr(const char *line, int paramnum, char * str, size_t buffersize)
aea4d766 577{
578 int bg, en;
579
874572d4
WM
580 if (param_getptr(line, &bg, &en, paramnum)) {
581 return 0;
582 }
583
584 // Prevent out of bounds errors
585 if (en - bg + 1 >= buffersize) {
c95affa8 586 printf("out of bounds error: want %d bytes have %zd bytes\n", en - bg + 1 + 1, buffersize);
874572d4
WM
587 return 0;
588 }
aea4d766 589
590 memcpy(str, line + bg, en - bg + 1);
591 str[en - bg + 1] = 0;
592
593 return en - bg + 1;
594}
79544b28 595
596/*
597The following methods comes from Rfidler sourcecode.
598https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
599*/
600
601// convert hex to sequence of 0/1 bit values
602// returns number of bits converted
603int hextobinarray(char *target, char *source)
604{
605 int length, i, count= 0;
874572d4 606 char* start = source;
79544b28 607 char x;
608
609 length = strlen(source);
610 // process 4 bits (1 hex digit) at a time
611 while(length--)
612 {
613 x= *(source++);
614 // capitalize
615 if (x >= 'a' && x <= 'f')
616 x -= 32;
617 // convert to numeric value
618 if (x >= '0' && x <= '9')
619 x -= '0';
620 else if (x >= 'A' && x <= 'F')
621 x -= 'A' - 10;
874572d4 622 else {
3ded0f97 623 printf("Discovered unknown character %c %d at idx %tu of %s\n", x, x, source - start, start);
79544b28 624 return 0;
874572d4 625 }
79544b28 626 // output
627 for(i= 0 ; i < 4 ; ++i, ++count)
628 *(target++)= (x >> (3 - i)) & 1;
629 }
630
631 return count;
632}
633
634// convert hex to human readable binary string
635int hextobinstring(char *target, char *source)
636{
637 int length;
638
639 if(!(length= hextobinarray(target, source)))
640 return 0;
641 binarraytobinstring(target, target, length);
642 return length;
643}
644
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 647int 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
667// convert binary array to human readable binary
668void binarraytobinstring(char *target, char *source, int length)
669{
670 int i;
671
672 for(i= 0 ; i < length ; ++i)
673 *(target++)= *(source++) + '0';
674 *target= '\0';
675}
676
677// return parity bit required to match type
709665b5 678uint8_t GetParity( uint8_t *bits, uint8_t type, int length)
79544b28 679{
680 int x;
681
682 for(x= 0 ; length > 0 ; --length)
683 x += bits[length - 1];
684 x %= 2;
685
686 return x ^ type;
687}
688
689// add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
709665b5 690void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length)
79544b28 691{
692 *(target++)= GetParity(source, EVEN, length / 2);
693 memcpy(target, source, length);
694 target += length;
695 *(target)= GetParity(source + length / 2, ODD, length / 2);
696}
4973f23d 697
59f726c9 698// xor two arrays together for len items. The dst array contains the new xored values.
4973f23d 699void xor(unsigned char *dst, unsigned char *src, size_t len) {
700 for( ; len > 0; len--,dst++,src++)
701 *dst ^= *src;
702}
703
704int32_t le24toh (uint8_t data[3]) {
705 return (data[2] << 16) | (data[1] << 8) | data[0];
706}
c872d8c1 707uint32_t le32toh (uint8_t *data) {
708 return (uint32_t)( (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | data[0]);
709}
c4c3af7c 710
711// RotateLeft - Ultralight, Desfire, works on byte level
712// 00-01-02 >> 01-02-00
713void rol(uint8_t *data, const size_t len){
714 uint8_t first = data[0];
715 for (size_t i = 0; i < len-1; i++) {
716 data[i] = data[i+1];
717 }
718 data[len-1] = first;
719}
d172c17c
JC
720
721
722// Replace unprintable characters with a dot in char buffer
723void clean_ascii(unsigned char *buf, size_t len) {
724 for (size_t i = 0; i < len; i++) {
725 if (!isprint(buf[i]))
726 buf[i] = '.';
727 }
728}
acf0582d 729
aa757f71
OM
730// replace \r \n to \0
731void strcleanrn(char *buf, size_t len) {
732 strcreplace(buf, len, '\n', '\0');
733 strcreplace(buf, len, '\r', '\0');
734}
acf0582d 735
aa757f71
OM
736// replace char in buffer
737void strcreplace(char *buf, size_t len, char from, char to) {
738 for (size_t i = 0; i < len; i++) {
739 if (buf[i] == from)
740 buf[i] = to;
741 }
742}
743
744char *strmcopy(char *buf) {
745 char * str = NULL;
746 if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) {
747 memset(str, 0, strlen(buf) + 1);
748 strcpy(str, buf);
749 }
750 return str;
751}
acf0582d 752
c48c4d78 753
754// determine number of logical CPU cores (use for multithreaded functions)
755extern int num_CPUs(void)
756{
757#if defined(_WIN32)
758 #include <sysinfoapi.h>
759 SYSTEM_INFO sysinfo;
760 GetSystemInfo(&sysinfo);
761 return sysinfo.dwNumberOfProcessors;
762#elif defined(__linux__) || defined(__APPLE__)
763 #include <unistd.h>
764 return sysconf(_SC_NPROCESSORS_ONLN);
765#else
766 return 1;
767#endif
768}
ec9c7112 769
Impressum, Datenschutz