]> git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
debug in progress
[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
13 #ifndef _WIN32
14 #include <termios.h>
15 #include <sys/ioctl.h>
16 int 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>
42 int ukbhit(void) {
43 return kbhit();
44 }
45 #endif
46
47 // log files functions
48 void 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
62 void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
63 AddLogLine(fileName, extData, sprint_hex(data, len));
64 }
65
66 void 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
72 void 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
83 void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
84 char * fnameptr = fileName;
85 memset(fileName, 0x00, 200);
86
87 for (int j = 0; j < byteCount; j++, fnameptr += 2)
88 sprintf(fnameptr, "%02x", uid[j]);
89 sprintf(fnameptr, "%s", ext);
90 }
91
92 // printing and converting functions
93
94 void print_hex(const uint8_t * data, const size_t len)
95 {
96 size_t i;
97
98 for (i=0; i < len; i++)
99 printf("%02x ", data[i]);
100
101 printf("\n");
102 }
103
104 char * sprint_hex(const uint8_t * data, const size_t len) {
105 static char buf[1024];
106 char * tmp = buf;
107 size_t i;
108
109 for (i=0; i < len && i < 1024/3; i++, tmp += 3)
110 sprintf(tmp, "%02x ", data[i]);
111
112 return buf;
113 }
114
115 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
116 {
117 while (len--) {
118 dest[len] = (uint8_t) n;
119 n >>= 8;
120 }
121 }
122
123 uint64_t bytes_to_num(uint8_t* src, size_t len)
124 {
125 uint64_t num = 0;
126 while (len--)
127 {
128 num = (num << 8) | (*src);
129 src++;
130 }
131 return num;
132 }
133
134 // -------------------------------------------------------------------------
135 // string parameters lib
136 // -------------------------------------------------------------------------
137
138 // -------------------------------------------------------------------------
139 // line - param line
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)
144 {
145 int i;
146 int len = strlen(line);
147
148 *bg = 0;
149 *en = 0;
150
151 // skip spaces
152 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
153 if (*bg >= len) {
154 return 1;
155 }
156
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)++;
160
161 if (line[*bg] == '\0') return 1;
162 }
163
164 *en = *bg;
165 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
166
167 (*en)--;
168
169 return 0;
170 }
171
172 char param_getchar(const char *line, int paramnum)
173 {
174 int bg, en;
175
176 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
177
178 return line[bg];
179 }
180
181 uint8_t param_get8(const char *line, int paramnum)
182 {
183 return param_get8ex(line, paramnum, 10, 0);
184 }
185
186 uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
187 {
188 int bg, en;
189
190 if (!param_getptr(line, &bg, &en, paramnum))
191 return strtoul(&line[bg], NULL, base) & 0xff;
192 else
193 return deflt;
194 }
195
196 uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
197 {
198 int bg, en;
199
200 if (!param_getptr(line, &bg, &en, paramnum))
201 return strtoul(&line[bg], NULL, base);
202 else
203 return deflt;
204 }
205
206 uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
207 {
208 int bg, en;
209
210 if (!param_getptr(line, &bg, &en, paramnum))
211 return strtoull(&line[bg], NULL, base);
212 else
213 return deflt;
214
215 return 0;
216 }
217
218 int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
219 {
220 int bg, en, temp, i;
221
222 if (hexcnt % 2)
223 return 1;
224
225 if (param_getptr(line, &bg, &en, paramnum)) return 1;
226
227 if (en - bg + 1 != hexcnt)
228 return 1;
229
230 for(i = 0; i < hexcnt; i += 2) {
231 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
232
233 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
234 data[i / 2] = temp & 0xff;
235 }
236
237 return 0;
238 }
239
240 int param_getstr(const char *line, int paramnum, char * str)
241 {
242 int bg, en;
243
244 if (param_getptr(line, &bg, &en, paramnum)) return 0;
245
246 memcpy(str, line + bg, en - bg + 1);
247 str[en - bg + 1] = 0;
248
249 return en - bg + 1;
250 }
Impressum, Datenschutz