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