]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
9dab4655b73f6da99ba467ea366868e4bbac992f
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
25 #define MAX_BIN_BREAK_LENGTH (3072+384+1)
29 #include <sys/ioctl.h>
36 static struct termios Otty
, Ntty
;
38 if ( tcgetattr(STDIN_FILENO
, &Otty
) == -1 ) return -1;
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
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
52 return ( error
== 0 ? cnt
: -1 );
63 // log files functions
64 void AddLogLine(char *file
, char *extData
, char *c
) {
66 char filename
[FILE_PATH_SIZE
] = {0x00};
70 if (len
> FILE_PATH_SIZE
) len
= FILE_PATH_SIZE
;
71 memcpy(filename
, file
, len
);
73 fLog
= fopen(filename
, "a");
75 printf("Could not append log file %s", filename
);
79 fprintf(fLog
, "%s", extData
);
80 fprintf(fLog
, "%s\n", c
);
84 void AddLogHex(char *fileName
, char *extData
, const uint8_t * data
, const size_t len
){
85 AddLogLine(fileName
, extData
, sprint_hex(data
, len
));
88 void AddLogUint64(char *fileName
, char *extData
, const uint64_t data
) {
90 sprintf(buf
, "%x%x", (unsigned int)((data
& 0xFFFFFFFF00000000) >> 32), (unsigned int)(data
& 0xFFFFFFFF));
91 AddLogLine(fileName
, extData
, buf
);
94 void AddLogCurrentDT(char *fileName
) {
99 curTime
= gmtime(&now
);
101 strftime (buff
, sizeof(buff
), "%Y-%m-%d %H:%M:%S", curTime
);
102 AddLogLine(fileName
, "\nanticollision: ", buff
);
105 void FillFileNameByUID(char *fileName
, uint8_t * uid
, char *ext
, int byteCount
) {
106 char * fnameptr
= fileName
;
107 memset(fileName
, 0x00, 200);
109 for (int j
= 0; j
< byteCount
; j
++, fnameptr
+= 2)
110 sprintf(fnameptr
, "%02x", (unsigned int) uid
[j
]);
111 sprintf(fnameptr
, "%s", ext
);
114 // printing and converting functions
116 void print_hex(const uint8_t * data
, const size_t len
)
120 for (i
=0; i
< len
; i
++)
121 printf("%02x ", data
[i
]);
126 void print_hex_break(const uint8_t *data
, const size_t len
, uint8_t breaks
) {
129 printf("[%02d] | ", rownum
);
130 for (int i
= 0; i
< len
; ++i
) {
132 printf("%02X ", data
[i
]);
134 // check if a line break is needed
135 if ( breaks
> 0 && !((i
+1) % breaks
) && (i
+1 < len
) ) {
137 printf("\n[%02d] | ", rownum
);
143 char *sprint_hex(const uint8_t *data
, const size_t len
) {
145 int maxLen
= ( len
> 1024/3) ? 1024/3 : len
;
146 static char buf
[1024];
147 memset(buf
, 0x00, 1024);
151 for (i
=0; i
< maxLen
; ++i
, tmp
+= 3)
152 sprintf(tmp
, "%02x ", (unsigned int) data
[i
]);
157 char *sprint_bin_break(const uint8_t *data
, const size_t len
, const uint8_t breaks
) {
158 // make sure we don't go beyond our char array memory
161 max_len
= ( len
> MAX_BIN_BREAK_LENGTH
) ? MAX_BIN_BREAK_LENGTH
: len
;
163 max_len
= ( len
+(len
/breaks
) > MAX_BIN_BREAK_LENGTH
) ? MAX_BIN_BREAK_LENGTH
: len
+(len
/breaks
);
165 static char buf
[MAX_BIN_BREAK_LENGTH
]; // 3072 + end of line characters if broken at 8 bits
167 memset(buf
, 0x00, sizeof(buf
));
171 // loop through the out_index to make sure we don't go too far
172 for (size_t out_index
=0; out_index
< max_len
; out_index
++) {
173 // set character - (should be binary but verify it isn't more than 1 digit)
174 if (data
[in_index
]<10)
175 sprintf(tmp
++, "%u", (unsigned int) data
[in_index
]);
176 // check if a line break is needed and we have room to print it in our array
177 if ( (breaks
> 0) && !((in_index
+1) % breaks
) && (out_index
+1 < max_len
) ) {
178 // increment and print line break
180 sprintf(tmp
++, "%s","\n");
188 char *sprint_bin(const uint8_t *data
, const size_t len
) {
189 return sprint_bin_break(data
, len
, 0);
192 char *sprint_hex_ascii(const uint8_t *data
, const size_t len
) {
193 static char buf
[1024];
195 memset(buf
, 0x00, 1024);
196 size_t max_len
= (len
> 255) ? 255 : len
;
197 // max 255 bytes * 3 + 2 characters = 767 in buffer
198 sprintf(tmp
, "%s| ", sprint_hex(data
, max_len
) );
201 size_t pos
= (max_len
* 3)+2;
202 // add another 255 characters ascii = 1020 characters of buffer used
205 if ( (c
< 32) || (c
== 127))
207 sprintf(tmp
+pos
+i
, "%c", c
);
213 char *sprint_ascii(const uint8_t *data
, const size_t len
) {
214 static char buf
[1024];
216 memset(buf
, 0x00, 1024);
217 size_t max_len
= (len
> 1010) ? 1010 : len
;
221 tmp
[i
] = ((c
< 32) || (c
== 127)) ? '.' : c
;
227 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
230 dest
[len
] = (uint8_t) n
;
235 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
240 num
= (num
<< 8) | (*src
);
246 void num_to_bytebits(uint64_t n
, size_t len
, uint8_t *dest
) {
253 //least significant bit first
254 void num_to_bytebitsLSBF(uint64_t n
, size_t len
, uint8_t *dest
) {
255 for(int i
= 0 ; i
< len
; ++i
) {
261 // Swap bit order on a uint32_t value. Can be limited by nrbits just use say 8bits reversal
262 // And clears the rest of the bits.
263 uint32_t SwapBits(uint32_t value
, int nrbits
) {
264 uint32_t newvalue
= 0;
265 for(int i
= 0; i
< nrbits
; i
++) {
266 newvalue
^= ((value
>> i
) & 1) << (nrbits
- 1 - i
);
271 // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp
273 // hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii
274 // up to 64 bytes or 512 bits
275 uint8_t *SwapEndian64(const uint8_t *src
, const size_t len
, const uint8_t blockSize
){
276 static uint8_t buf
[64];
277 memset(buf
, 0x00, 64);
279 for (uint8_t block
=0; block
< (uint8_t)(len
/blockSize
); block
++){
280 for (size_t i
= 0; i
< blockSize
; i
++){
281 tmp
[i
+(blockSize
*block
)] = src
[(blockSize
-1-i
)+(blockSize
*block
)];
287 // takes a uint8_t src array, for len items and reverses the byte order in blocksizes (8,16,32,64),
288 // returns: the dest array contains the reordered src array.
289 void SwapEndian64ex(const uint8_t *src
, const size_t len
, const uint8_t blockSize
, uint8_t *dest
){
290 for (uint8_t block
=0; block
< (uint8_t)(len
/blockSize
); block
++){
291 for (size_t i
= 0; i
< blockSize
; i
++){
292 dest
[i
+(blockSize
*block
)] = src
[(blockSize
-1-i
)+(blockSize
*block
)];
297 //assumes little endian
298 char * printBits(size_t const size
, void const * const ptr
)
300 unsigned char *b
= (unsigned char*) ptr
;
302 static char buf
[1024];
306 for (i
=size
-1;i
>=0;i
--)
310 byte
= b
[i
] & (1<<j
);
312 sprintf(tmp
, "%u", (unsigned int)byte
);
319 // -------------------------------------------------------------------------
320 // string parameters lib
321 // -------------------------------------------------------------------------
323 // -------------------------------------------------------------------------
325 // bg, en - symbol numbers in param line of beginning and ending parameter
326 // paramnum - param number (from 0)
327 // -------------------------------------------------------------------------
328 int param_getptr(const char *line
, int *bg
, int *en
, int paramnum
)
331 int len
= strlen(line
);
337 while (line
[*bg
] ==' ' || line
[*bg
]=='\t') (*bg
)++;
342 for (i
= 0; i
< paramnum
; i
++) {
343 while (line
[*bg
]!=' ' && line
[*bg
]!='\t' && line
[*bg
] != '\0') (*bg
)++;
344 while (line
[*bg
]==' ' || line
[*bg
]=='\t') (*bg
)++;
346 if (line
[*bg
] == '\0') return 1;
350 while (line
[*en
] != ' ' && line
[*en
] != '\t' && line
[*en
] != '\0') (*en
)++;
358 int param_getlength(const char *line
, int paramnum
)
362 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0;
367 char param_getchar(const char *line
, int paramnum
) {
368 return param_getchar_indx(line
, 0, paramnum
);
371 char param_getchar_indx(const char *line
, int indx
, int paramnum
) {
374 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0x00;
379 return line
[bg
+ indx
];
382 uint8_t param_get8(const char *line
, int paramnum
)
384 return param_get8ex(line
, paramnum
, 0, 10);
388 * @brief Reads a decimal integer (actually, 0-254, not 255)
391 * @return -1 if error
393 uint8_t param_getdec(const char *line
, int paramnum
, uint8_t *destination
)
395 uint8_t val
= param_get8ex(line
, paramnum
, 255, 10);
396 if( (int8_t) val
== -1) return 1;
397 (*destination
) = val
;
401 * @brief Checks if param is decimal
406 uint8_t param_isdec(const char *line
, int paramnum
)
409 //TODO, check more thorougly
410 if (!param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
411 // return strtoul(&line[bg], NULL, 10) & 0xff;
416 uint8_t param_get8ex(const char *line
, int paramnum
, int deflt
, int base
)
420 if (!param_getptr(line
, &bg
, &en
, paramnum
))
421 return strtoul(&line
[bg
], NULL
, base
) & 0xff;
426 uint32_t param_get32ex(const char *line
, int paramnum
, int deflt
, int base
)
430 if (!param_getptr(line
, &bg
, &en
, paramnum
))
431 return strtoul(&line
[bg
], NULL
, base
);
436 uint64_t param_get64ex(const char *line
, int paramnum
, int deflt
, int base
)
440 if (!param_getptr(line
, &bg
, &en
, paramnum
))
441 return strtoull(&line
[bg
], NULL
, base
);
446 int param_gethex(const char *line
, int paramnum
, uint8_t * data
, int hexcnt
)
453 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
455 if (en
- bg
+ 1 != hexcnt
)
458 for(i
= 0; i
< hexcnt
; i
+= 2) {
459 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
461 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
462 data
[i
/ 2] = temp
& 0xff;
467 int param_gethex_ex(const char *line
, int paramnum
, uint8_t * data
, int *hexcnt
)
474 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
476 *hexcnt
= en
- bg
+ 1;
477 if (*hexcnt
% 2) //error if not complete hex bytes
480 for(i
= 0; i
< *hexcnt
; i
+= 2) {
481 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
483 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
484 data
[i
/ 2] = temp
& 0xff;
490 int param_gethex_to_eol(const char *line
, int paramnum
, uint8_t * data
, int maxdatalen
, int *datalen
) {
495 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
501 if (line
[indx
] == '\t' || line
[indx
] == ' ')
504 if (isxdigit(line
[indx
])) {
505 buf
[strlen(buf
) + 1] = 0x00;
506 buf
[strlen(buf
)] = line
[indx
];
508 // if we have symbols other than spaces and hex
512 if (*datalen
>= maxdatalen
) {
513 // if we dont have space in buffer and have symbols to translate
517 if (strlen(buf
) >= 2) {
518 sscanf(buf
, "%x", &temp
);
519 data
[*datalen
] = (uint8_t)(temp
& 0xff);
528 //error when not completed hex bytes
534 int param_getstr(const char *line
, int paramnum
, char * str
)
538 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0;
540 memcpy(str
, line
+ bg
, en
- bg
+ 1);
541 str
[en
- bg
+ 1] = 0;
547 The following methods comes from Rfidler sourcecode.
548 https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
551 // convert hex to sequence of 0/1 bit values
552 // returns number of bits converted
553 int hextobinarray(char *target
, char *source
)
555 int length
, i
, count
= 0;
558 length
= strlen(source
);
559 // process 4 bits (1 hex digit) at a time
564 if (x
>= 'a' && x
<= 'f')
566 // convert to numeric value
567 if (x
>= '0' && x
<= '9')
569 else if (x
>= 'A' && x
<= 'F')
574 for(i
= 0 ; i
< 4 ; ++i
, ++count
)
575 *(target
++)= (x
>> (3 - i
)) & 1;
581 // convert hex to human readable binary string
582 int hextobinstring(char *target
, char *source
)
586 if(!(length
= hextobinarray(target
, source
)))
588 binarraytobinstring(target
, target
, length
);
592 // convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source)
593 // return number of bits converted
594 int binarraytohex(char *target
,char *source
, int length
)
604 for(i
= x
= 0 ; i
< 4 ; ++i
)
605 x
+= ( source
[i
] << (3 - i
));
606 sprintf(target
,"%X", (unsigned int)x
);
614 // convert binary array to human readable binary
615 void binarraytobinstring(char *target
, char *source
, int length
)
619 for(i
= 0 ; i
< length
; ++i
)
620 *(target
++)= *(source
++) + '0';
624 // return parity bit required to match type
625 uint8_t GetParity( uint8_t *bits
, uint8_t type
, int length
)
629 for(x
= 0 ; length
> 0 ; --length
)
630 x
+= bits
[length
- 1];
636 // add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
637 void wiegand_add_parity(uint8_t *target
, uint8_t *source
, uint8_t length
)
639 *(target
++)= GetParity(source
, EVEN
, length
/ 2);
640 memcpy(target
, source
, length
);
642 *(target
)= GetParity(source
+ length
/ 2, ODD
, length
/ 2);
645 // xor two arrays together for len items. The dst array contains the new xored values.
646 void xor(unsigned char *dst
, unsigned char *src
, size_t len
) {
647 for( ; len
> 0; len
--,dst
++,src
++)
651 int32_t le24toh (uint8_t data
[3]) {
652 return (data
[2] << 16) | (data
[1] << 8) | data
[0];
654 uint32_t le32toh (uint8_t *data
) {
655 return (uint32_t)( (data
[3]<<24) | (data
[2]<<16) | (data
[1]<<8) | data
[0]);
658 // RotateLeft - Ultralight, Desfire, works on byte level
659 // 00-01-02 >> 01-02-00
660 void rol(uint8_t *data
, const size_t len
){
661 uint8_t first
= data
[0];
662 for (size_t i
= 0; i
< len
-1; i
++) {
669 // Replace unprintable characters with a dot in char buffer
670 void clean_ascii(unsigned char *buf
, size_t len
) {
671 for (size_t i
= 0; i
< len
; i
++) {
672 if (!isprint(buf
[i
]))
677 // replace \r \n to \0
678 void strcleanrn(char *buf
, size_t len
) {
679 strcreplace(buf
, len
, '\n', '\0');
680 strcreplace(buf
, len
, '\r', '\0');
683 // replace char in buffer
684 void strcreplace(char *buf
, size_t len
, char from
, char to
) {
685 for (size_t i
= 0; i
< len
; i
++) {
691 char *strmcopy(char *buf
) {
693 if ((str
= (char*) malloc(strlen(buf
) + 1)) != NULL
) {
694 memset(str
, 0, strlen(buf
) + 1);
701 // determine number of logical CPU cores (use for multithreaded functions)
702 extern int num_CPUs(void)
705 #include <sysinfoapi.h>
707 GetSystemInfo(&sysinfo
);
708 return sysinfo
.dwNumberOfProcessors
;
709 #elif defined(__linux__) || defined(__APPLE__)
711 return sysconf(_SC_NPROCESSORS_ONLN
);