]> git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
ae8e4fecdf526af18fa46d088d7fbfd35fdb78b0
[proxmark3-svn] / client / util.c
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"
12 #define MAX_BIN_BREAK_LENGTH (3072+384+1)
13
14 #ifndef _WIN32
15 #include <termios.h>
16 #include <sys/ioctl.h>
17
18 int ukbhit(void)
19 {
20 int cnt = 0;
21 int error;
22 static struct termios Otty, Ntty;
23
24 if ( tcgetattr( 0, &Otty) == -1) return -1;
25 Ntty = Otty;
26
27 Ntty.c_iflag = 0; /* input mode */
28 Ntty.c_oflag = 0; /* output mode */
29 Ntty.c_lflag &= ~ICANON; /* raw mode */
30 Ntty.c_cc[VMIN] = CMIN; /* minimum time to wait */
31 Ntty.c_cc[VTIME] = CTIME; /* minimum characters to wait for */
32
33 if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
34 error += ioctl(0, FIONREAD, &cnt);
35 error += tcsetattr(0, TCSANOW, &Otty);
36 }
37
38 return ( error == 0 ? cnt : -1 );
39 }
40
41 #else
42 #include <conio.h>
43 int ukbhit(void) {
44 return kbhit();
45 }
46 #endif
47
48 // log files functions
49 void AddLogLine(char *file, char *extData, char *c) {
50 FILE *fLog = NULL;
51 char filename[FILE_PATH_SIZE] = {0x00};
52 int len = 0;
53
54 len = strlen(file);
55 if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
56 memcpy(filename, file, len);
57
58 fLog = fopen(filename, "a");
59 if (!fLog) {
60 printf("Could not append log file %s", filename);
61 return;
62 }
63
64 fprintf(fLog, "%s", extData);
65 fprintf(fLog, "%s\n", c);
66 fclose(fLog);
67 }
68
69 void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
70 AddLogLine(fileName, extData, sprint_hex(data, len));
71 }
72
73 void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
74 char buf[100] = {0};
75 sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
76 AddLogLine(fileName, extData, buf);
77 }
78
79 void AddLogCurrentDT(char *fileName) {
80 char buff[20];
81 struct tm *curTime;
82
83 time_t now = time(0);
84 curTime = gmtime(&now);
85
86 strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime);
87 AddLogLine(fileName, "\nanticollision: ", buff);
88 }
89
90 void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
91 char * fnameptr = fileName;
92 memset(fileName, 0x00, 200);
93
94 for (int j = 0; j < byteCount; j++, fnameptr += 2)
95 sprintf(fnameptr, "%02x", uid[j]);
96 sprintf(fnameptr, "%s", ext);
97 }
98
99 // printing and converting functions
100 void print_hex(const uint8_t * data, const size_t len) {
101 size_t i;
102 for (i=0; i < len; ++i)
103 printf("%02x ", data[i]);
104 printf("\n");
105 }
106 void print_hex_break(const uint8_t *data, const size_t len, uint8_t breaks) {
107
108 int rownum = 0;
109 printf("[%02d] | ", rownum);
110 for (int i = 0; i < len; ++i) {
111
112 printf("%02X ", data[i]);
113
114 // check if a line break is needed
115 if ( breaks > 0 && !((i+1) % breaks) && (i+1 < len) ) {
116 ++rownum;
117 printf("\n[%02d] | ", rownum);
118 }
119 }
120 printf("\n");
121 }
122
123 char *sprint_hex(const uint8_t *data, const size_t len) {
124
125 int maxLen = ( len > 1024/3) ? 1024/3 : len;
126 static char buf[1024];
127 memset(buf, 0x00, 1024);
128 char * tmp = buf;
129 size_t i;
130
131 for (i=0; i < maxLen; ++i, tmp += 3)
132 sprintf(tmp, "%02X ", data[i]);
133
134 return buf;
135 }
136
137 char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) {
138
139 // make sure we don't go beyond our char array memory
140 size_t in_index = 0, out_index = 0;
141 int max_len;
142 if (breaks==0)
143 max_len = ( len > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len;
144 else
145 max_len = ( len+(len/breaks) > MAX_BIN_BREAK_LENGTH ) ? MAX_BIN_BREAK_LENGTH : len+(len/breaks);
146
147 static char buf[MAX_BIN_BREAK_LENGTH]; // 3072 + end of line characters if broken at 8 bits
148 //clear memory
149 memset(buf, 0x00, sizeof(buf));
150 char *tmp = buf;
151
152 // loop through the out_index to make sure we don't go too far
153 for (out_index=0; out_index < max_len-2; out_index++) {
154 // set character
155 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
156 // check if a line break is needed and we have room to print it in our array
157 if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 != max_len) ) {
158 // increment and print line break
159 out_index++;
160 sprintf(tmp++, "%s","\n");
161 }
162 in_index++;
163 }
164 // last char.
165 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
166 return buf;
167 }
168
169 char *sprint_bin(const uint8_t *data, const size_t len) {
170 return sprint_bin_break(data, len, 0);
171 }
172
173 char *sprint_hex_ascii(const uint8_t *data, const size_t len) {
174 static char buf[1024];
175 char *tmp = buf;
176 memset(buf, 0x00, 1024);
177 size_t max_len = (len > 1010) ? 1010 : len;
178 sprintf(tmp, "%s| %s", sprint_hex(data, max_len) , data);
179 return buf;
180 }
181 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
182 {
183 while (len--) {
184 dest[len] = (uint8_t) n;
185 n >>= 8;
186 }
187 }
188
189 uint64_t bytes_to_num(uint8_t* src, size_t len)
190 {
191 uint64_t num = 0;
192 while (len--)
193 {
194 num = (num << 8) | (*src);
195 src++;
196 }
197 return num;
198 }
199
200 void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) {
201 while (len--) {
202 dest[len] = n & 1;
203 n >>= 1;
204 }
205 }
206
207 // aa,bb,cc,dd,ee,ff,gg,hh, ii,jj,kk,ll,mm,nn,oo,pp
208 // to
209 // hh,gg,ff,ee,dd,cc,bb,aa, pp,oo,nn,mm,ll,kk,jj,ii
210 // up to 64 bytes or 512 bits
211 uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){
212 uint8_t buf[64];
213 memset(buf, 0x00, 64);
214 uint8_t *tmp = buf;
215 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
216 for (size_t i = 0; i < blockSize; i++){
217 tmp[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)];
218 }
219 }
220 return tmp;
221 }
222
223 void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest){
224 for (uint8_t block=0; block < (uint8_t)(len/blockSize); block++){
225 for (size_t i = 0; i < blockSize; i++){
226 dest[i+(blockSize*block)] = src[(blockSize-1-i)+(blockSize*block)];
227 }
228 }
229 }
230
231
232 // -------------------------------------------------------------------------
233 // string parameters lib
234 // -------------------------------------------------------------------------
235
236 // -------------------------------------------------------------------------
237 // line - param line
238 // bg, en - symbol numbers in param line of beginning an ending parameter
239 // paramnum - param number (from 0)
240 // -------------------------------------------------------------------------
241 int param_getptr(const char *line, int *bg, int *en, int paramnum)
242 {
243 int i;
244 int len = strlen(line);
245
246 *bg = 0;
247 *en = 0;
248
249 // skip spaces
250 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
251 if (*bg >= len) {
252 return 1;
253 }
254
255 for (i = 0; i < paramnum; i++) {
256 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
257 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
258
259 if (line[*bg] == '\0') return 1;
260 }
261
262 *en = *bg;
263 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
264
265 (*en)--;
266
267 return 0;
268 }
269
270
271 char param_getchar(const char *line, int paramnum)
272 {
273 int bg, en;
274
275 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
276
277 return line[bg];
278 }
279
280 uint8_t param_get8(const char *line, int paramnum)
281 {
282 return param_get8ex(line, paramnum, 0, 10);
283 }
284
285 /**
286 * @brief Reads a decimal integer (actually, 0-254, not 255)
287 * @param line
288 * @param paramnum
289 * @return -1 if error
290 */
291 uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination)
292 {
293 uint8_t val = param_get8ex(line, paramnum, 255, 10);
294 if( (int8_t) val == -1) return 1;
295 (*destination) = val;
296 return 0;
297 }
298 /**
299 * @brief Checks if param is decimal
300 * @param line
301 * @param paramnum
302 * @return
303 */
304 uint8_t param_isdec(const char *line, int paramnum)
305 {
306 int bg, en;
307 //TODO, check more thorougly
308 if (!param_getptr(line, &bg, &en, paramnum)) return 1;
309 // return strtoul(&line[bg], NULL, 10) & 0xff;
310
311 return 0;
312 }
313
314 uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
315 {
316 int bg, en;
317
318 if (!param_getptr(line, &bg, &en, paramnum))
319 return strtoul(&line[bg], NULL, base) & 0xff;
320 else
321 return deflt;
322 }
323
324 uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
325 {
326 int bg, en;
327
328 if (!param_getptr(line, &bg, &en, paramnum))
329 return strtoul(&line[bg], NULL, base);
330 else
331 return deflt;
332 }
333
334 uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
335 {
336 int bg, en;
337
338 if (!param_getptr(line, &bg, &en, paramnum))
339 return strtoull(&line[bg], NULL, base);
340 else
341 return deflt;
342
343 return 0;
344 }
345
346 int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
347 {
348 int bg, en, temp, i;
349
350 if (hexcnt & 1) return 1;
351
352 if (param_getptr(line, &bg, &en, paramnum)) return 1;
353
354 if (en - bg + 1 != hexcnt) return 1;
355
356 for(i = 0; i < hexcnt; i += 2) {
357 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
358
359 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
360 data[i / 2] = temp & 0xff;
361 }
362
363 return 0;
364 }
365 int param_gethex_ex(const char *line, int paramnum, uint8_t * data, int *hexcnt)
366 {
367 int bg, en, temp, i;
368
369 //if (hexcnt % 2)
370 // return 1;
371
372 if (param_getptr(line, &bg, &en, paramnum)) return 1;
373
374 *hexcnt = en - bg + 1;
375 if (*hexcnt % 2) //error if not complete hex bytes
376 return 1;
377
378 for(i = 0; i < *hexcnt; i += 2) {
379 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
380
381 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
382 data[i / 2] = temp & 0xff;
383 }
384
385 return 0;
386 }
387 int param_getstr(const char *line, int paramnum, char * str)
388 {
389 int bg, en;
390
391 if (param_getptr(line, &bg, &en, paramnum)) return 0;
392
393 memcpy(str, line + bg, en - bg + 1);
394 str[en - bg + 1] = 0;
395
396 return en - bg + 1;
397 }
398
399 /*
400 The following methods comes from Rfidler sourcecode.
401 https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
402 */
403
404 // convert hex to sequence of 0/1 bit values
405 // returns number of bits converted
406 int hextobinarray(char *target, char *source)
407 {
408 int length, i, count= 0;
409 char x;
410
411 length = strlen(source);
412 // process 4 bits (1 hex digit) at a time
413 while(length--)
414 {
415 x= *(source++);
416 // capitalize
417 if (x >= 'a' && x <= 'f')
418 x -= 32;
419 // convert to numeric value
420 if (x >= '0' && x <= '9')
421 x -= '0';
422 else if (x >= 'A' && x <= 'F')
423 x -= 'A' - 10;
424 else
425 return 0;
426 // output
427 for(i= 0 ; i < 4 ; ++i, ++count)
428 *(target++)= (x >> (3 - i)) & 1;
429 }
430
431 return count;
432 }
433
434 // convert hex to human readable binary string
435 int hextobinstring(char *target, char *source)
436 {
437 int length;
438
439 if(!(length= hextobinarray(target, source)))
440 return 0;
441 binarraytobinstring(target, target, length);
442 return length;
443 }
444
445 // convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source)
446 // return number of bits converted
447 int binarraytohex(char *target, char *source, int length)
448 {
449 unsigned char i, x;
450 int j = length;
451
452 if(j % 4)
453 return 0;
454
455 while(j)
456 {
457 for(i= x= 0 ; i < 4 ; ++i)
458 x += ( source[i] << (3 - i));
459 sprintf(target,"%X", x);
460 ++target;
461 source += 4;
462 j -= 4;
463 }
464 return length;
465 }
466
467 // convert binary array to human readable binary
468 void binarraytobinstring(char *target, char *source, int length)
469 {
470 int i;
471
472 for(i= 0 ; i < length ; ++i)
473 *(target++)= *(source++) + '0';
474 *target= '\0';
475 }
476
477 // return parity bit required to match type
478 uint8_t GetParity( uint8_t *bits, uint8_t type, int length)
479 {
480 int x;
481 for( x = 0 ; length > 0 ; --length)
482 x += bits[length - 1];
483 x %= 2;
484 return x ^ type;
485 }
486
487 // add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
488 void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length)
489 {
490 *(target++)= GetParity(source, EVEN, length / 2);
491 memcpy(target, source, length);
492 target += length;
493 *(target)= GetParity(source + length / 2, ODD, length / 2);
494 }
495
496 void xor(unsigned char * dst, unsigned char * src, size_t len) {
497 for( ; len > 0; len--,dst++,src++)
498 *dst ^= *src;
499 }
500
501 int32_t le24toh (uint8_t data[3]) {
502 return (data[2] << 16) | (data[1] << 8) | data[0];
503 }
504
505 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits) {
506
507 if (len > 32) return 0;
508
509 int i = start;
510 int j = len-1;
511 uint32_t tmp = 0;
512
513 for (; j >= 0; --j, ++i)
514 tmp |= bits[i] << j;
515
516 return tmp;
517 }
518
519 // RotateLeft - Ultralight, Desfire, works on byte level
520 // 00-01-02 >> 01-02-00
521 void rol(uint8_t *data, const size_t len){
522 uint8_t first = data[0];
523 for (size_t i = 0; i < len-1; i++) {
524 data[i] = data[i+1];
525 }
526 data[len-1] = first;
527 }
528
529 uint32_t SwapBits(uint32_t value, int nrbits) {
530 uint32_t newvalue = 0;
531 for(int i = 0; i < nrbits; i++) {
532 newvalue ^= ((value >> i) & 1) << (nrbits - 1 - i);
533 }
534 return newvalue;
535 }
Impressum, Datenschutz