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