]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/util.c
CHG: minor code clean up
[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
cb64309e 13#ifndef _WIN32
873014de
M
14#include <termios.h>
15#include <sys/ioctl.h>
79544b28 16
f397b5cc
M
17int ukbhit(void)
18{
19 int cnt = 0;
20 int error;
21 static struct termios Otty, Ntty;
22
23
24 tcgetattr( 0, &Otty);
25 Ntty = Otty;
26
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 */
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
M
48// log files functions
49void AddLogLine(char *fileName, char *extData, char *c) {
50 FILE *fLog = NULL;
51
52 fLog = fopen(fileName, "a");
53 if (!fLog) {
54 printf("Could not append log file %s", fileName);
55 return;
56 }
57
58 fprintf(fLog, "%s", extData);
59 fprintf(fLog, "%s\n", c);
60 fclose(fLog);
61}
62
63void AddLogHex(char *fileName, char *extData, const uint8_t * data, const size_t len){
64 AddLogLine(fileName, extData, sprint_hex(data, len));
65}
66
67void AddLogUint64(char *fileName, char *extData, const uint64_t data) {
68 char buf[100] = {0};
69 sprintf(buf, "%x%x", (unsigned int)((data & 0xFFFFFFFF00000000) >> 32), (unsigned int)(data & 0xFFFFFFFF));
70 AddLogLine(fileName, extData, buf);
71}
72
73void AddLogCurrentDT(char *fileName) {
74 char buff[20];
75 struct tm *curTime;
76
77 time_t now = time(0);
78 curTime = gmtime(&now);
79
80 strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", curTime);
81 AddLogLine(fileName, "\nanticollision: ", buff);
82}
83
e0c635d1 84void FillFileNameByUID(char *fileName, uint8_t * uid, char *ext, int byteCount) {
55acbb2a
M
85 char * fnameptr = fileName;
86 memset(fileName, 0x00, 200);
87
e0c635d1 88 for (int j = 0; j < byteCount; j++, fnameptr += 2)
55acbb2a
M
89 sprintf(fnameptr, "%02x", uid[j]);
90 sprintf(fnameptr, "%s", ext);
55acbb2a
M
91}
92
93// printing and converting functions
f397b5cc 94
534983d7 95void print_hex(const uint8_t * data, const size_t len)
96{
97 size_t i;
98
99 for (i=0; i < len; i++)
100 printf("%02x ", data[i]);
101
102 printf("\n");
103}
104
105char * sprint_hex(const uint8_t * data, const size_t len) {
106 static char buf[1024];
107 char * tmp = buf;
108 size_t i;
109
110 for (i=0; i < len && i < 1024/3; i++, tmp += 3)
111 sprintf(tmp, "%02x ", data[i]);
112
113 return buf;
114}
f89c7050 115
79544b28 116char * sprint_bin(const uint8_t * data, const size_t len) {
117
118 int maxLen = ( len > 1024) ? 1024 : len;
119 static char buf[1024];
120 char * tmp = buf;
121 size_t i;
122
123 for (i=0; i < maxLen; ++i, ++tmp)
124 sprintf(tmp, "%u", data[i]);
125
126 return buf;
127}
128
f89c7050
M
129void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
130{
131 while (len--) {
132 dest[len] = (uint8_t) n;
133 n >>= 8;
134 }
135}
136
137uint64_t bytes_to_num(uint8_t* src, size_t len)
138{
139 uint64_t num = 0;
140 while (len--)
141 {
142 num = (num << 8) | (*src);
143 src++;
144 }
145 return num;
146}
f397b5cc 147
79544b28 148//assumes little endian
149char * printBits(size_t const size, void const * const ptr)
150{
151 unsigned char *b = (unsigned char*) ptr;
152 unsigned char byte;
153 static char buf[1024];
154 char * tmp = buf;
155 int i, j;
156
157 for (i=size-1;i>=0;i--)
158 {
159 for (j=7;j>=0;j--)
160 {
161 byte = b[i] & (1<<j);
162 byte >>= j;
163 sprintf(tmp, "%u", byte);
164 tmp++;
165 }
166 }
167 return buf;
168}
169
9ca155ba
M
170// -------------------------------------------------------------------------
171// string parameters lib
172// -------------------------------------------------------------------------
173
f397b5cc
M
174// -------------------------------------------------------------------------
175// line - param line
176// bg, en - symbol numbers in param line of beginning an ending parameter
177// paramnum - param number (from 0)
178// -------------------------------------------------------------------------
179int param_getptr(const char *line, int *bg, int *en, int paramnum)
180{
181 int i;
182 int len = strlen(line);
183
184 *bg = 0;
185 *en = 0;
186
187 // skip spaces
188 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
189 if (*bg >= len) {
190 return 1;
191 }
192
193 for (i = 0; i < paramnum; i++) {
194 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
195 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
196
197 if (line[*bg] == '\0') return 1;
198 }
199
200 *en = *bg;
201 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
202
203 (*en)--;
204
205 return 0;
206}
207
208char param_getchar(const char *line, int paramnum)
209{
210 int bg, en;
211
212 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
213
214 return line[bg];
215}
216
217uint8_t param_get8(const char *line, int paramnum)
218{
219 return param_get8ex(line, paramnum, 10, 0);
220}
221
222uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
223{
224 int bg, en;
225
226 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 227 return strtoul(&line[bg], NULL, base) & 0xff;
f397b5cc
M
228 else
229 return deflt;
230}
231
232uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
233{
234 int bg, en;
235
236 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 237 return strtoul(&line[bg], NULL, base);
f397b5cc
M
238 else
239 return deflt;
240}
241
242uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
243{
244 int bg, en;
245
246 if (!param_getptr(line, &bg, &en, paramnum))
6c6d1ac1 247 return strtoull(&line[bg], NULL, base);
f397b5cc
M
248 else
249 return deflt;
250
251 return 0;
252}
253
254int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
255{
256 int bg, en, temp, i;
257
258 if (hexcnt % 2)
259 return 1;
260
261 if (param_getptr(line, &bg, &en, paramnum)) return 1;
262
263 if (en - bg + 1 != hexcnt)
264 return 1;
265
266 for(i = 0; i < hexcnt; i += 2) {
267 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
268
269 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
270 data[i / 2] = temp & 0xff;
271 }
272
273 return 0;
274}
aea4d766 275
276int param_getstr(const char *line, int paramnum, char * str)
277{
278 int bg, en;
279
280 if (param_getptr(line, &bg, &en, paramnum)) return 0;
281
282 memcpy(str, line + bg, en - bg + 1);
283 str[en - bg + 1] = 0;
284
285 return en - bg + 1;
286}
79544b28 287
288/*
289The following methods comes from Rfidler sourcecode.
290https://github.com/ApertureLabsLtd/RFIDler/blob/master/firmware/Pic32/RFIDler.X/src/
291*/
292
293// convert hex to sequence of 0/1 bit values
294// returns number of bits converted
295int hextobinarray(char *target, char *source)
296{
297 int length, i, count= 0;
298 char x;
299
300 length = strlen(source);
301 // process 4 bits (1 hex digit) at a time
302 while(length--)
303 {
304 x= *(source++);
305 // capitalize
306 if (x >= 'a' && x <= 'f')
307 x -= 32;
308 // convert to numeric value
309 if (x >= '0' && x <= '9')
310 x -= '0';
311 else if (x >= 'A' && x <= 'F')
312 x -= 'A' - 10;
313 else
314 return 0;
315 // output
316 for(i= 0 ; i < 4 ; ++i, ++count)
317 *(target++)= (x >> (3 - i)) & 1;
318 }
319
320 return count;
321}
322
323// convert hex to human readable binary string
324int hextobinstring(char *target, char *source)
325{
326 int length;
327
328 if(!(length= hextobinarray(target, source)))
329 return 0;
330 binarraytobinstring(target, target, length);
331 return length;
332}
333
334// convert binary array of 0x00/0x01 values to hex (safe to do in place as target will always be shorter than source)
335// return number of bits converted
336int binarraytohex(char *target, char *source, int length)
337{
338 unsigned char i, x;
339 int j = length;
340
341 if(j % 4)
342 return 0;
343
344 while(j)
345 {
346 for(i= x= 0 ; i < 4 ; ++i)
347 x += ( source[i] << (3 - i));
348 sprintf(target,"%X", x);
349 ++target;
350 source += 4;
351 j -= 4;
352 }
353 return length;
354}
355
356// convert binary array to human readable binary
357void binarraytobinstring(char *target, char *source, int length)
358{
359 int i;
360
361 for(i= 0 ; i < length ; ++i)
362 *(target++)= *(source++) + '0';
363 *target= '\0';
364}
365
366// return parity bit required to match type
367uint8_t GetParity( char *bits, uint8_t type, int length)
368{
369 int x;
370
371 for(x= 0 ; length > 0 ; --length)
372 x += bits[length - 1];
373 x %= 2;
374
375 return x ^ type;
376}
377
378// add HID parity to binary array: EVEN prefix for 1st half of ID, ODD suffix for 2nd half
379void wiegand_add_parity(char *target, char *source, char length)
380{
381 *(target++)= GetParity(source, EVEN, length / 2);
382 memcpy(target, source, length);
383 target += length;
384 *(target)= GetParity(source + length / 2, ODD, length / 2);
385}
Impressum, Datenschutz