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