]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
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 //-----------------------------------------------------------------------------
15 #include <sys/ioctl.h>
20 static struct termios Otty
, Ntty
;
26 Ntty
.c_iflag
= 0; /* input mode */
27 Ntty
.c_oflag
= 0; /* output mode */
28 Ntty
.c_lflag
&= ~ICANON
; /* raw mode */
29 Ntty
.c_cc
[VMIN
] = CMIN
; /* minimum time to wait */
30 Ntty
.c_cc
[VTIME
] = CTIME
; /* minimum characters to wait for */
32 if (0 == (error
= tcsetattr(0, TCSANOW
, &Ntty
))) {
33 error
+= ioctl(0, FIONREAD
, &cnt
);
34 error
+= tcsetattr(0, TCSANOW
, &Otty
);
37 return ( error
== 0 ? cnt
: -1 );
47 // log files functions
48 void AddLogLine(char *fileName
, char *extData
, char *c
) {
51 fLog
= fopen(fileName
, "a");
53 printf("Could not append log file %s", fileName
);
57 fprintf(fLog
, "%s", extData
);
58 fprintf(fLog
, "%s\n", c
);
62 void AddLogHex(char *fileName
, char *extData
, const uint8_t * data
, const size_t len
){
63 AddLogLine(fileName
, extData
, sprint_hex(data
, len
));
66 void AddLogUint64(char *fileName
, char *extData
, const uint64_t data
) {
68 sprintf(buf
, "%x%x", (unsigned int)((data
& 0xFFFFFFFF00000000) >> 32), (unsigned int)(data
& 0xFFFFFFFF));
69 AddLogLine(fileName
, extData
, buf
);
72 void AddLogCurrentDT(char *fileName
) {
77 curTime
= gmtime(&now
);
79 strftime (buff
, sizeof(buff
), "%Y-%m-%d %H:%M:%S", curTime
);
80 AddLogLine(fileName
, "\nanticollision: ", buff
);
83 void FillFileNameByUID(char *fileName
, uint8_t * uid
, char *ext
, int byteCount
) {
84 char * fnameptr
= fileName
;
85 memset(fileName
, 0x00, 200);
87 for (int j
= 0; j
< byteCount
; j
++, fnameptr
+= 2)
88 sprintf(fnameptr
, "%02x", uid
[j
]);
89 sprintf(fnameptr
, "%s", ext
);
92 // printing and converting functions
94 void print_hex(const uint8_t * data
, const size_t len
)
98 for (i
=0; i
< len
; i
++)
99 printf("%02x ", data
[i
]);
104 char * sprint_hex(const uint8_t * data
, const size_t len
) {
105 static char buf
[1024];
109 for (i
=0; i
< len
&& i
< 1024/3; i
++, tmp
+= 3)
110 sprintf(tmp
, "%02x ", data
[i
]);
115 void num_to_bytes(uint64_t n
, size_t len
, uint8_t* dest
)
118 dest
[len
] = (uint8_t) n
;
123 uint64_t bytes_to_num(uint8_t* src
, size_t len
)
128 num
= (num
<< 8) | (*src
);
134 // -------------------------------------------------------------------------
135 // string parameters lib
136 // -------------------------------------------------------------------------
138 // -------------------------------------------------------------------------
140 // bg, en - symbol numbers in param line of beginning an ending parameter
141 // paramnum - param number (from 0)
142 // -------------------------------------------------------------------------
143 int param_getptr(const char *line
, int *bg
, int *en
, int paramnum
)
146 int len
= strlen(line
);
152 while (line
[*bg
] ==' ' || line
[*bg
]=='\t') (*bg
)++;
157 for (i
= 0; i
< paramnum
; i
++) {
158 while (line
[*bg
]!=' ' && line
[*bg
]!='\t' && line
[*bg
] != '\0') (*bg
)++;
159 while (line
[*bg
]==' ' || line
[*bg
]=='\t') (*bg
)++;
161 if (line
[*bg
] == '\0') return 1;
165 while (line
[*en
] != ' ' && line
[*en
] != '\t' && line
[*en
] != '\0') (*en
)++;
172 char param_getchar(const char *line
, int paramnum
)
176 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0x00;
181 uint8_t param_get8(const char *line
, int paramnum
)
183 return param_get8ex(line
, paramnum
, 10, 0);
186 uint8_t param_get8ex(const char *line
, int paramnum
, int deflt
, int base
)
190 if (!param_getptr(line
, &bg
, &en
, paramnum
))
191 return strtoul(&line
[bg
], NULL
, base
) & 0xff;
196 uint32_t param_get32ex(const char *line
, int paramnum
, int deflt
, int base
)
200 if (!param_getptr(line
, &bg
, &en
, paramnum
))
201 return strtoul(&line
[bg
], NULL
, base
);
206 uint64_t param_get64ex(const char *line
, int paramnum
, int deflt
, int base
)
210 if (!param_getptr(line
, &bg
, &en
, paramnum
))
211 return strtoull(&line
[bg
], NULL
, base
);
218 int param_gethex(const char *line
, int paramnum
, uint8_t * data
, int hexcnt
)
225 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 1;
227 if (en
- bg
+ 1 != hexcnt
)
230 for(i
= 0; i
< hexcnt
; i
+= 2) {
231 if (!(isxdigit(line
[bg
+ i
]) && isxdigit(line
[bg
+ i
+ 1])) ) return 1;
233 sscanf((char[]){line
[bg
+ i
], line
[bg
+ i
+ 1], 0}, "%X", &temp
);
234 data
[i
/ 2] = temp
& 0xff;
240 int param_getstr(const char *line
, int paramnum
, char * str
)
244 if (param_getptr(line
, &bg
, &en
, paramnum
)) return 0;
246 memcpy(str
, line
+ bg
, en
- bg
+ 1);
247 str
[en
- bg
+ 1] = 0;