]> git.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
FIX: "LF PYRAMID CLONE" now works.
[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"
bf32dd92 12#define MAX_BIN_BREAK_LENGTH (3072+384+1)
534983d7 13
cb64309e 14#ifndef _WIN32
873014de
M
15#include <termios.h>
16#include <sys/ioctl.h>
79544b28 17
f397b5cc
M
18int ukbhit(void)
19{
20 int cnt = 0;
21 int error;
22 static struct termios Otty, Ntty;
23
d04b71c1 24 if ( tcgetattr( 0, &Otty) == -1) return -1;
f397b5cc
M
25 Ntty = Otty;
26
d04b71c1 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 */
f397b5cc
M
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>
43int ukbhit(void) {
44 return kbhit();
45}
46#endif
47
55acbb2a 48// log files functions
b915fda3 49void AddLogLine(char *file, char *extData, char *c) {
55acbb2a 50 FILE *fLog = NULL;
b915fda3 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");
55acbb2a 59 if (!fLog) {
b915fda3 60 printf("Could not append log file %s", filename);
55acbb2a
M
61 return;
62 }
63
64 fprintf(fLog, "%s", extData);
65 fprintf(fLog, "%s\n", c);
66 fclose(fLog);
67}
68
69void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
70 AddLogLine(fileName, extData, sprint_hex(data, len));
71}
72
73void 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
79void 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
e0c635d1 90void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
55acbb2a
M
91 char * fnameptr = fileName;
92 memset(fileName, 0x00, 200);
93
e0c635d1 94 for (int j = 0; j < byteCount; j++, fnameptr += 2)
55acbb2a
M
95 sprintf(fnameptr, "%02x", uid[j]);
96 sprintf(fnameptr, "%s", ext);
55acbb2a
M
97}
98
99// printing and converting functions
9827020a 100void print_hex(const uint8_t * data, const size_t len) {
534983d7 101 size_t i;
9827020a 102 for (i=0; i < len; ++i)
534983d7 103 printf("%02x ", data[i]);
9827020a 104 printf("\n");
105}
106void print_hex_break(const uint8_t *data, const size_t len, uint8_t breaks) {
a1689f41 107
108 int rownum = 0;
109 printf("[%02d] | ", rownum);
110 for (int i = 0; i < len; ++i) {
534983d7 111
9827020a 112 printf("%02X ", data[i]);
113
114 // check if a line break is needed
a1689f41 115 if ( breaks > 0 && !((i+1) % breaks) && (i+1 < len) ) {
116 ++rownum;
117 printf("\n[%02d] | ", rownum);
118 }
9827020a 119 }
534983d7 120 printf("\n");
121}
122
334cc089 123char *sprint_hex(const uint8_t *data, const size_t len) {
b915fda3 124
125 int maxLen = ( len > 1024/3) ? 1024/3 : len;
534983d7 126 static char buf[1024];
334cc089 127 memset(buf, 0x00, 1024);
534983d7 128 char * tmp = buf;
129 size_t i;
130
b915fda3 131 for (i=0; i < maxLen; ++i, tmp += 3)
334cc089 132 sprintf(tmp, "%02X ", data[i]);
534983d7 133
134 return buf;
135}
f89c7050 136
2767fc02 137char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks) {
581b31fb 138
bf32dd92 139 // make sure we don't go beyond our char array memory
581b31fb 140 size_t in_index = 0, out_index = 0;
141 int max_len;
0c97a456 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
bf32dd92 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));
2767fc02 150 char *tmp = buf;
79544b28 151
bf32dd92 152 // loop through the out_index to make sure we don't go too far
581b31fb 153 for (out_index=0; out_index < max_len-2; out_index++) {
bf32dd92 154 // set character
05164399 155 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
9332b857 156 // check if a line break is needed and we have room to print it in our array
bf32dd92 157 if ( (breaks > 0) && !((in_index+1) % breaks) && (out_index+1 != max_len) ) {
158 // increment and print line break
159 out_index++;
2767fc02 160 sprintf(tmp++, "%s","\n");
581b31fb 161 }
bf32dd92 162 in_index++;
163 }
581b31fb 164 // last char.
165 sprintf(tmp++, "%u", (unsigned int) data[in_index]);
79544b28 166 return buf;
167}
168
2767fc02 169char *sprint_bin(const uint8_t *data, const size_t len) {
170 return sprint_bin_break(data, len, 0);
171}
0c97a456 172
173char *sprint_hex_ascii(const uint8_t *data, const size_t len) {
174 static char buf[1024];
508b37ba 175 char *tmp = buf;
9827020a 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);
0c97a456 179 return buf;
180}
f89c7050
M
181void 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
189uint64_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}
f397b5cc 199
a126332a 200void 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
e1c88b09 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
224e8c1a 211uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize){
f4d0ffd1 212 uint8_t buf[64];
224e8c1a 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)];
e1c88b09 218 }
219 }
224e8c1a 220 return tmp;
e1c88b09 221}
222
f4d0ffd1 223void 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
9ca155ba
M
232// -------------------------------------------------------------------------
233// string parameters lib
234// -------------------------------------------------------------------------
235
f397b5cc
M
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// -------------------------------------------------------------------------
241int 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
31abe49f 270
f397b5cc
M
271char 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
280uint8_t param_get8(const char *line, int paramnum)
281{
60e86577 282 return param_get8ex(line, paramnum, 0, 10);
f397b5cc
M
283}
284
f6d9fb17 285/**
31abe49f 286 * @brief Reads a decimal integer (actually, 0-254, not 255)
f6d9fb17
MHS
287 * @param line
288 * @param paramnum
31abe49f 289 * @return -1 if error
f6d9fb17
MHS
290 */
291uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination)
292{
31abe49f 293 uint8_t val = param_get8ex(line, paramnum, 255, 10);
31abe49f 294 if( (int8_t) val == -1) return 1;
f6d9fb17
MHS
295 (*destination) = val;
296 return 0;
297}
298/**
299 * @brief Checks if param is decimal
300 * @param line
301 * @param paramnum
302 * @return
303 */
304uint8_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
f397b5cc
M
314uint8_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))
6c6d1ac1 319 return strtoul(&line[bg], NULL, base) & 0xff;
f397b5cc
M
320 else
321 return deflt;
322}
323
324uint32_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))
6c6d1ac1 329 return strtoul(&line[bg], NULL, base);
f397b5cc
M
330 else
331 return deflt;
332}
333
334uint64_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))
6c6d1ac1 339 return strtoull(&line[bg], NULL, base);
f397b5cc
M
340 else
341 return deflt;
342
343 return 0;
344}
345
346int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
347{
348 int bg, en, temp, i;
349
508b37ba 350 if (hexcnt & 1) return 1;
f397b5cc
M
351
352 if (param_getptr(line, &bg, &en, paramnum)) return 1;
353
508b37ba 354 if (en - bg + 1 != hexcnt) return 1;
f397b5cc
M
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}
e98572a1 365int 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;
aea4d766 377
e98572a1 378 for(i = 0; i < *hexcnt; i += 2) {
3bc7b13d 379 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
e98572a1 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}
aea4d766 387int 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}
79544b28 398
399/*
400The following methods comes from Rfidler sourcecode.
401https://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
406int 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
435int 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
447int 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
468void 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
a126332a 478uint8_t GetParity( uint8_t *bits, uint8_t type, int length)
79544b28 479{
480 int x;
ab7bb494 481 for( x = 0 ; length > 0 ; --length)
79544b28 482 x += bits[length - 1];
483 x %= 2;
79544b28 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
a126332a 488void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length)
79544b28 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}
c3c241f3 495
496void xor(unsigned char * dst, unsigned char * src, size_t len) {
497 for( ; len > 0; len--,dst++,src++)
498 *dst ^= *src;
499}
500
501int32_t le24toh (uint8_t data[3]) {
502 return (data[2] << 16) | (data[1] << 8) | data[0];
503}
ad6219fc 504
ad6219fc 505uint32_t PackBits(uint8_t start, uint8_t len, uint8_t* bits) {
9984b173 506
507 if (len > 32) return 0;
ad6219fc 508
509 int i = start;
510 int j = len-1;
ad6219fc 511 uint32_t tmp = 0;
9984b173 512
ad6219fc 513 for (; j >= 0; --j, ++i)
514 tmp |= bits[i] << j;
515
516 return tmp;
517}
9984b173 518
ab7bb494 519// RotateLeft - Ultralight, Desfire, works on byte level
520// 00-01-02 >> 01-02-00
6bb7609c 521void rol(uint8_t *data, const size_t len){
9984b173 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;
6bb7609c 527}
528
529uint32_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;
9984b173 535}
Impressum, Datenschutz