improved command hf mf sniff. Now it cant decode nested authentication and cant write...
[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"
534983d7 12
38c4979d 13#ifndef WIN32
873014de
M
14#include <termios.h>
15#include <sys/ioctl.h>
f397b5cc
M
16int ukbhit(void)
17{
18 int cnt = 0;
19 int error;
20 static struct termios Otty, Ntty;
21
22
23 tcgetattr( 0, &Otty);
24 Ntty = Otty;
25
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 */
31
32 if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
33 error += ioctl(0, FIONREAD, &cnt);
34 error += tcsetattr(0, TCSANOW, &Otty);
35 }
36
37 return ( error == 0 ? cnt : -1 );
38}
39
40#else
41#include <conio.h>
42int ukbhit(void) {
43 return kbhit();
44}
45#endif
46
55acbb2a
M
47// log files functions
48void AddLogLine(char *fileName, char *extData, char *c) {
49 FILE *fLog = NULL;
50
51 fLog = fopen(fileName, "a");
52 if (!fLog) {
53 printf("Could not append log file %s", fileName);
54 return;
55 }
56
57 fprintf(fLog, "%s", extData);
58 fprintf(fLog, "%s\n", c);
59 fclose(fLog);
60}
61
62void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
63 AddLogLine(fileName, extData, sprint_hex(data, len));
64}
65
66void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
67 char buf[100] = {0};
68 sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
69 AddLogLine(fileName, extData, buf);
70}
71
72void AddLogCurrentDT(char *fileName) {
73 char buff[20];
74 struct tm *curTime;
75
76 time_t now = time(0);
77 curTime = gmtime(&now);
78
79 strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime);
80 AddLogLine(fileName, "\nanticollision: ", buff);
81}
82
83void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext) {
84 char * fnameptr = fileName;
85 memset(fileName, 0x00, 200);
86
87 for (int j = 0; j < 7; j++, fnameptr += 2)
88 sprintf(fnameptr, "%02x", uid[j]);
89 sprintf(fnameptr, "%s", ext);
90
91 printf("fname:%s", fileName);
92}
93
94// printing and converting functions
f397b5cc 95
534983d7 96void print_hex(const uint8_t * data, const size_t len)
97{
98 size_t i;
99
100 for (i=0; i < len; i++)
101 printf("%02x ", data[i]);
102
103 printf("\n");
104}
105
106char * sprint_hex(const uint8_t * data, const size_t len) {
107 static char buf[1024];
108 char * tmp = buf;
109 size_t i;
110
111 for (i=0; i < len && i < 1024/3; i++, tmp += 3)
112 sprintf(tmp, "%02x ", data[i]);
113
114 return buf;
115}
f89c7050
M
116
117void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
118{
119 while (len--) {
120 dest[len] = (uint8_t) n;
121 n >>= 8;
122 }
123}
124
125uint64_t bytes_to_num(uint8_t* src, size_t len)
126{
127 uint64_t num = 0;
128 while (len--)
129 {
130 num = (num << 8) | (*src);
131 src++;
132 }
133 return num;
134}
f397b5cc 135
9ca155ba
M
136// -------------------------------------------------------------------------
137// string parameters lib
138// -------------------------------------------------------------------------
139
f397b5cc
M
140// -------------------------------------------------------------------------
141// line - param line
142// bg, en - symbol numbers in param line of beginning an ending parameter
143// paramnum - param number (from 0)
144// -------------------------------------------------------------------------
145int param_getptr(const char *line, int *bg, int *en, int paramnum)
146{
147 int i;
148 int len = strlen(line);
149
150 *bg = 0;
151 *en = 0;
152
153 // skip spaces
154 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
155 if (*bg >= len) {
156 return 1;
157 }
158
159 for (i = 0; i < paramnum; i++) {
160 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
161 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
162
163 if (line[*bg] == '\0') return 1;
164 }
165
166 *en = *bg;
167 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
168
169 (*en)--;
170
171 return 0;
172}
173
174char param_getchar(const char *line, int paramnum)
175{
176 int bg, en;
177
178 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
179
180 return line[bg];
181}
182
183uint8_t param_get8(const char *line, int paramnum)
184{
185 return param_get8ex(line, paramnum, 10, 0);
186}
187
188uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
189{
190 int bg, en;
191
192 if (!param_getptr(line, &bg, &en, paramnum))
193 return strtol(&line[bg], NULL, base) & 0xff;
194 else
195 return deflt;
196}
197
198uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
199{
200 int bg, en;
201
202 if (!param_getptr(line, &bg, &en, paramnum))
203 return strtol(&line[bg], NULL, base);
204 else
205 return deflt;
206}
207
208uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
209{
210 int bg, en;
211
212 if (!param_getptr(line, &bg, &en, paramnum))
81cd0474 213 return strtoll(&line[bg], NULL, base);
f397b5cc
M
214 else
215 return deflt;
216
217 return 0;
218}
219
220int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
221{
222 int bg, en, temp, i;
223
224 if (hexcnt % 2)
225 return 1;
226
227 if (param_getptr(line, &bg, &en, paramnum)) return 1;
228
229 if (en - bg + 1 != hexcnt)
230 return 1;
231
232 for(i = 0; i < hexcnt; i += 2) {
233 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
234
235 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
236 data[i / 2] = temp & 0xff;
237 }
238
239 return 0;
240}
aea4d766 241
242int param_getstr(const char *line, int paramnum, char * str)
243{
244 int bg, en;
245
246 if (param_getptr(line, &bg, &en, paramnum)) return 0;
247
248 memcpy(str, line + bg, en - bg + 1);
249 str[en - bg + 1] = 0;
250
251 return en - bg + 1;
252}
Impressum, Datenschutz