]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
Added output to file for 'lf hitag list' command
[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
e0c635d1 83void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
55acbb2a
M
84 char * fnameptr = fileName;
85 memset(fileName, 0x00, 200);
86
e0c635d1 87 for (int j = 0; j < byteCount; j++, fnameptr += 2)
55acbb2a
M
88 sprintf(fnameptr, "%02x", uid[j]);
89 sprintf(fnameptr, "%s", ext);
55acbb2a
M
90}
91
92// printing and converting functions
f397b5cc 93
534983d7 94void 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
104char * 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}
f89c7050
M
114
115void 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
123uint64_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}
f397b5cc 133
9ca155ba
M
134// -------------------------------------------------------------------------
135// string parameters lib
136// -------------------------------------------------------------------------
137
f397b5cc
M
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// -------------------------------------------------------------------------
143int 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
172char 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
181uint8_t param_get8(const char *line, int paramnum)
182{
183 return param_get8ex(line, paramnum, 10, 0);
184}
185
186uint8_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 strtol(&line[bg], NULL, base) & 0xff;
192 else
193 return deflt;
194}
195
196uint32_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 strtol(&line[bg], NULL, base);
202 else
203 return deflt;
204}
205
206uint64_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))
81cd0474 211 return strtoll(&line[bg], NULL, base);
f397b5cc
M
212 else
213 return deflt;
214
215 return 0;
216}
217
218int 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}
aea4d766 239
240int 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