]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
update travis config (#662)
[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>
acf0582d 19
ec9c7112 20#ifdef _WIN32
21#include <windows.h>
22#endif
23
ace26dbd 24#define MAX_BIN_BREAK_LENGTH (3072+384+1)
534983d7 25
cb64309e 26#ifndef _WIN32
873014de
M
27#include <termios.h>
28#include <sys/ioctl.h>
8c0ccdef 29#include <unistd.h>
79544b28 30
f397b5cc
M
31int ukbhit(void)
32{
33 int cnt = 0;
34 int error;
35 static struct termios Otty, Ntty;
36
8c0ccdef 37 if ( tcgetattr(STDIN_FILENO, &Otty) == -1 ) return -1;
f397b5cc
M
38 Ntty = Otty;
39
8c0ccdef 40 Ntty.c_iflag = 0x0000; // input mode
41 Ntty.c_oflag = 0x0000; // output mode
42 Ntty.c_lflag &= ~ICANON; // control mode = raw
43 Ntty.c_cc[VMIN] = 1; // return if at least 1 character is in the queue
44 Ntty.c_cc[VTIME] = 0; // no timeout. Wait forever
45
46 if (0 == (error = tcsetattr(STDIN_FILENO, TCSANOW, &Ntty))) { // set new attributes
47 error += ioctl(STDIN_FILENO, FIONREAD, &cnt); // get number of characters availabe
48 error += tcsetattr(STDIN_FILENO, TCSANOW, &Otty); // reset attributes
f397b5cc
M
49 }
50
51 return ( error == 0 ? cnt : -1 );
52}
53
54#else
acf0582d 55
f397b5cc
M
56#include <conio.h>
57int ukbhit(void) {
58 return kbhit();
59}
60#endif
61
55acbb2a 62// log files functions
b915fda3 63void AddLogLine(char *file, char *extData, char *c) {
55acbb2a 64 FILE *fLog = NULL;
b915fda3 65 char filename[FILE_PATH_SIZE] = {0x00};
66 int len = 0;
67
68 len = strlen(file);
69 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
70 memcpy(filename, file, len);
71
72 fLog = fopen(filename, "a");
55acbb2a 73 if (!fLog) {
b915fda3 74 printf("Could not append log file %s", filename);
55acbb2a
M
75 return;
76 }
77
78 fprintf(fLog, "%s", extData);
79 fprintf(fLog, "%s\n", c);
80 fclose(fLog);
81}
82
83void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
84 AddLogLine(fileName, extData, sprint_hex(data, len));
85}
86
87void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
88 char buf[100] = {0};
89 sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
90 AddLogLine(fileName, extData, buf);
91}
92
93void AddLogCurrentDT(char *fileName) {
94 char buff[20];
95 struct tm *curTime;
96
97 time_t now = time(0);
98 curTime = gmtime(&now);
99
100 strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime);
101 AddLogLine(fileName, "\nanticollision: ", buff);
102}
103
e0c635d1 104void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
55acbb2a
M
105 char * fnameptr = fileName;
106 memset(fileName, 0x00, 200);
107
e0c635d1 108 for (int j = 0; j < byteCount; j++, fnameptr += 2)
d1869c33 109 sprintf(fnameptr, "%02x", (unsigned int) uid[j]);
55acbb2a 110 sprintf(fnameptr, "%s", ext);
55acbb2a
M
111}
112
66efdc1f 113void hex_to_buffer(const uint8_t *buf, const uint8_t *hex_data, const size_t hex_len, const size_t hex_max_len,
114 const size_t min_str_len, const size_t spaces_between, bool uppercase) {
115
116 char *tmp = (char *)buf;
117 size_t i;
edd4c838 118 memset(tmp, 0x00, hex_max_len);
66efdc1f 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
00349d82 229 sprintf(tmp, "%.765s| ", sprint_hex(data, max_len) );
59f726c9 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
a37725fa
OM
359char * printBitsPar(const uint8_t *b, size_t len) {
360 static char buf1[512] = {0};
361 static char buf2[512] = {0};
362 static char *buf;
363 if (buf != buf1)
364 buf = buf1;
365 else
366 buf = buf2;
367 memset(buf, 0x00, 512);
368
369 for (int i = 0; i < len; i++) {
370 buf[i] = ((b[i / 8] << (i % 8)) & 0x80) ? '1':'0';
371 }
372 return buf;
373}
374
375
9ca155ba
M
376// -------------------------------------------------------------------------
377// string parameters lib
378// -------------------------------------------------------------------------
379
f397b5cc
M
380// -------------------------------------------------------------------------
381// line - param line
3a05a1e7 382// bg, en - symbol numbers in param line of beginning and ending parameter
f397b5cc
M
383// paramnum - param number (from 0)
384// -------------------------------------------------------------------------
385int param_getptr(const char *line, int *bg, int *en, int paramnum)
386{
387 int i;
388 int len = strlen(line);
389
390 *bg = 0;
391 *en = 0;
392
393 // skip spaces
394 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
395 if (*bg >= len) {
396 return 1;
397 }
398
399 for (i = 0; i < paramnum; i++) {
400 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
401 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
402
403 if (line[*bg] == '\0') return 1;
404 }
405
406 *en = *bg;
407 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
408
409 (*en)--;
410
411 return 0;
412}
413
31abe49f 414
3a05a1e7
OM
415int param_getlength(const char *line, int paramnum)
416{
417 int bg, en;
418
419 if (param_getptr(line, &bg, &en, paramnum)) return 0;
420
421 return en - bg + 1;
422}
423
8019540b 424char param_getchar(const char *line, int paramnum) {
425 return param_getchar_indx(line, 0, paramnum);
426}
427
428char param_getchar_indx(const char *line, int indx, int paramnum) {
f397b5cc
M
429 int bg, en;
430
431 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
432
8019540b 433 if (bg + indx > en)
434 return '\0';
435
436 return line[bg + indx];
f397b5cc
M
437}
438
439uint8_t param_get8(const char *line, int paramnum)
440{
6ca1477c 441 return param_get8ex(line, paramnum, 0, 10);
f397b5cc
M
442}
443
f6d9fb17 444/**
31abe49f 445 * @brief Reads a decimal integer (actually, 0-254, not 255)
f6d9fb17
MHS
446 * @param line
447 * @param paramnum
31abe49f 448 * @return -1 if error
f6d9fb17
MHS
449 */
450uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination)
451{
31abe49f 452 uint8_t val = param_get8ex(line, paramnum, 255, 10);
31abe49f 453 if( (int8_t) val == -1) return 1;
f6d9fb17
MHS
454 (*destination) = val;
455 return 0;
456}
457/**
458 * @brief Checks if param is decimal
459 * @param line
460 * @param paramnum
461 * @return
462 */
463uint8_t param_isdec(const char *line, int paramnum)
464{
465 int bg, en;
466 //TODO, check more thorougly
467 if (!param_getptr(line, &bg, &en, paramnum)) return 1;
468 // return strtoul(&line[bg], NULL, 10) & 0xff;
469
470 return 0;
471}
472
f397b5cc
M
473uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
474{
475 int bg, en;
476
477 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 478 return strtoul(&line[bg], NULL, base) & 0xff;
f397b5cc
M
479 else
480 return deflt;
481}
482
483uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
484{
485 int bg, en;
486
487 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 488 return strtoul(&line[bg], NULL, base);
f397b5cc
M
489 else
490 return deflt;
491}
492
493uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
494{
495 int bg, en;
496
497 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 498 return strtoull(&line[bg], NULL, base);
f397b5cc
M
499 else
500 return deflt;
f397b5cc
M
501}
502
503int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
504{
505 int bg, en, temp, i;
506
507 if (hexcnt % 2)
508 return 1;
509
510 if (param_getptr(line, &bg, &en, paramnum)) return 1;
511
512 if (en - bg + 1 != hexcnt)
513 return 1;
514
515 for(i = 0; i < hexcnt; i += 2) {
3ded0f97 516 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
f397b5cc
M
517
518 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
519 data[i / 2] = temp & 0xff;
520 }
521
522 return 0;
523}
1a5a73ab 524int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt)
525{
526 int bg, en, temp, i;
527
528 //if (hexcnt % 2)
529 // return 1;
530
531 if (param_getptr(line, &bg, &en, paramnum)) return 1;
532
533 *hexcnt = en - bg + 1;
534 if (*hexcnt % 2) //error if not complete hex bytes
535 return 1;
aea4d766 536
1a5a73ab 537 for(i = 0; i < *hexcnt; i += 2) {
3ded0f97 538 if (!(isxdigit((unsigned char)line[bg + i]) && isxdigit((unsigned char)line[bg + i + 1])) ) return 1;
1a5a73ab 539
540 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
541 data[i / 2] = temp & 0xff;
542 }
543
544 return 0;
545}
8019540b 546
547int param_gethex_to_eol(const char *line, int paramnum, uint8_t * data, int maxdatalen, int *datalen) {
548 int bg, en;
549 uint32_t temp;
550 char buf[5] = {0};
551
552 if (param_getptr(line, &bg, &en, paramnum)) return 1;
553
554 *datalen = 0;
555
556 int indx = bg;
557 while (line[indx]) {
3c5fce2b
OM
558 if (line[indx] == '\t' || line[indx] == ' ') {
559 indx++;
8019540b 560 continue;
3c5fce2b 561 }
8019540b 562
3ded0f97 563 if (isxdigit((unsigned char)line[indx])) {
8019540b 564 buf[strlen(buf) + 1] = 0x00;
565 buf[strlen(buf)] = line[indx];
566 } else {
567 // if we have symbols other than spaces and hex
568 return 1;
569 }
570
571 if (*datalen >= maxdatalen) {
572 // if we dont have space in buffer and have symbols to translate
573 return 2;
574 }
575
576 if (strlen(buf) >= 2) {
577 sscanf(buf, "%x", &temp);
578 data[*datalen] = (uint8_t)(temp & 0xff);
579 *buf = 0;
580 (*datalen)++;
581 }
582
583 indx++;
584 }
585
586 if (strlen(buf) > 0)
587 //error when not completed hex bytes
588 return 3;
589
590 return 0;
591}
592
874572d4 593int param_getstr(const char *line, int paramnum, char * str, size_t buffersize)
aea4d766 594{
595 int bg, en;
596
874572d4
WM
597 if (param_getptr(line, &bg, &en, paramnum)) {
598 return 0;
599 }
600
601 // Prevent out of bounds errors
602 if (en - bg + 1 >= buffersize) {
c95affa8 603 printf("out of bounds error: want %d bytes have %zd bytes\n", en - bg + 1 + 1, buffersize);
874572d4
WM
604 return 0;
605 }
aea4d766 606
607 memcpy(str, line + bg, en - bg + 1);
608 str[en - bg + 1] = 0;
609
610 return en - bg + 1;
611}
79544b28 612
613/*
614The following methods comes from Rfidler sourcecode.
615https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
616*/
617
618// convert hex to sequence of 0/1 bit values
619// returns number of bits converted
620int hextobinarray(char *target, char *source)
621{
622 int length, i, count= 0;
874572d4 623 char* start = source;
79544b28 624 char x;
625
626 length = strlen(source);
627 // process 4 bits (1 hex digit) at a time
628 while(length--)
629 {
630 x= *(source++);
631 // capitalize
632 if (x >= 'a' && x <= 'f')
633 x -= 32;
634 // convert to numeric value
635 if (x >= '0' && x <= '9')
636 x -= '0';
637 else if (x >= 'A' && x <= 'F')
638 x -= 'A' - 10;
874572d4 639 else {
3ded0f97 640 printf("Discovered unknown character %c %d at idx %tu of %s\n", x, x, source - start, start);
79544b28 641 return 0;
874572d4 642 }
79544b28 643 // output
644 for(i= 0 ; i < 4 ; ++i, ++count)
645 *(target++)= (x >> (3 - i)) & 1;
646 }
647
648 return count;
649}
650
651// convert hex to human readable binary string
652int hextobinstring(char *target, char *source)
653{
654 int length;
655
656 if(!(length= hextobinarray(target, source)))
657 return 0;
658 binarraytobinstring(target, target, length);
659 return length;
660}
661
662// convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source)
663// return number of bits converted
1c4c0b06 664int binarraytohex(char *target,char *source, int length)
79544b28 665{
666 unsigned char i, x;
667 int j = length;
668
669 if(j % 4)
670 return 0;
671
672 while(j)
673 {
674 for(i= x= 0 ; i < 4 ; ++i)
675 x += ( source[i] << (3 - i));
d1869c33 676 sprintf(target,"%X", (unsigned int)x);
79544b28 677 ++target;
678 source += 4;
679 j -= 4;
680 }
681 return length;
682}
683
684// convert binary array to human readable binary
685void binarraytobinstring(char *target, char *source, int length)
686{
687 int i;
688
689 for(i= 0 ; i < length ; ++i)
690 *(target++)= *(source++) + '0';
691 *target= '\0';
692}
693
694// return parity bit required to match type
709665b5 695uint8_t GetParity( uint8_t *bits, uint8_t type, int length)
79544b28 696{
697 int x;
698
699 for(x= 0 ; length > 0 ; --length)
700 x += bits[length - 1];
701 x %= 2;
702
703 return x ^ type;
704}
705
706// add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
709665b5 707void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length)
79544b28 708{
709 *(target++)= GetParity(source, EVEN, length / 2);
710 memcpy(target, source, length);
711 target += length;
712 *(target)= GetParity(source + length / 2, ODD, length / 2);
713}
4973f23d 714
59f726c9 715// xor two arrays together for len items. The dst array contains the new xored values.
4973f23d 716void xor(unsigned char *dst, unsigned char *src, size_t len) {
717 for( ; len > 0; len--,dst++,src++)
718 *dst ^= *src;
719}
720
721int32_t le24toh (uint8_t data[3]) {
722 return (data[2] << 16) | (data[1] << 8) | data[0];
723}
c872d8c1 724uint32_t le32toh (uint8_t *data) {
725 return (uint32_t)( (data[3]<<24) | (data[2]<<16) | (data[1]<<8) | data[0]);
726}
c4c3af7c 727
728// RotateLeft - Ultralight, Desfire, works on byte level
729// 00-01-02 >> 01-02-00
730void rol(uint8_t *data, const size_t len){
731 uint8_t first = data[0];
732 for (size_t i = 0; i < len-1; i++) {
733 data[i] = data[i+1];
734 }
735 data[len-1] = first;
736}
d172c17c
JC
737
738
739// Replace unprintable characters with a dot in char buffer
740void clean_ascii(unsigned char *buf, size_t len) {
741 for (size_t i = 0; i < len; i++) {
742 if (!isprint(buf[i]))
743 buf[i] = '.';
744 }
745}
acf0582d 746
aa757f71
OM
747// replace \r \n to \0
748void strcleanrn(char *buf, size_t len) {
749 strcreplace(buf, len, '\n', '\0');
750 strcreplace(buf, len, '\r', '\0');
751}
acf0582d 752
aa757f71
OM
753// replace char in buffer
754void strcreplace(char *buf, size_t len, char from, char to) {
755 for (size_t i = 0; i < len; i++) {
756 if (buf[i] == from)
757 buf[i] = to;
758 }
759}
760
761char *strmcopy(char *buf) {
762 char * str = NULL;
763 if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) {
764 memset(str, 0, strlen(buf) + 1);
765 strcpy(str, buf);
766 }
767 return str;
768}
acf0582d 769
c48c4d78 770
771// determine number of logical CPU cores (use for multithreaded functions)
772extern int num_CPUs(void)
773{
774#if defined(_WIN32)
775 #include <sysinfoapi.h>
776 SYSTEM_INFO sysinfo;
777 GetSystemInfo(&sysinfo);
778 return sysinfo.dwNumberOfProcessors;
779#elif defined(__linux__) || defined(__APPLE__)
780 #include <unistd.h>
781 return sysconf(_SC_NPROCESSORS_ONLN);
782#else
783 return 1;
784#endif
785}
ec9c7112 786
Impressum, Datenschutz