]> git.zerfleddert.de Git - proxmark3-svn/blob - client/util.c
8e7ef01b10d63d37f4beb75bfeaba10baaafa246
[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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include "util.h"
16
17 #ifdef __linux__
18 int ukbhit(void)
19 {
20 int cnt = 0;
21 int error;
22 static struct termios Otty, Ntty;
23
24
25 tcgetattr( 0, &Otty);
26 Ntty = Otty;
27
28 Ntty.c_iflag = 0; /* input mode */
29 Ntty.c_oflag = 0; /* output mode */
30 Ntty.c_lflag &= ~ICANON; /* raw mode */
31 Ntty.c_cc[VMIN] = CMIN; /* minimum time to wait */
32 Ntty.c_cc[VTIME] = CTIME; /* minimum characters to wait for */
33
34 if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
35 error += ioctl(0, FIONREAD, &cnt);
36 error += tcsetattr(0, TCSANOW, &Otty);
37 }
38
39 return ( error == 0 ? cnt : -1 );
40 }
41
42 #else
43 #include <conio.h>
44 int ukbhit(void) {
45 return kbhit();
46 }
47 #endif
48
49
50 void print_hex(const uint8_t * data, const size_t len)
51 {
52 size_t i;
53
54 for (i=0; i < len; i++)
55 printf("%02x ", data[i]);
56
57 printf("\n");
58 }
59
60 char * sprint_hex(const uint8_t * data, const size_t len) {
61 static char buf[1024];
62 char * tmp = buf;
63 size_t i;
64
65 for (i=0; i < len && i < 1024/3; i++, tmp += 3)
66 sprintf(tmp, "%02x ", data[i]);
67
68 return buf;
69 }
70
71 void num_to_bytes(uint64_t n, size_t len, uint8_t* dest)
72 {
73 while (len--) {
74 dest[len] = (uint8_t) n;
75 n >>= 8;
76 }
77 }
78
79 uint64_t bytes_to_num(uint8_t* src, size_t len)
80 {
81 uint64_t num = 0;
82 while (len--)
83 {
84 num = (num << 8) | (*src);
85 src++;
86 }
87 return num;
88 }
89
90 // -------------------------------------------------------------------------
91 // line - param line
92 // bg, en - symbol numbers in param line of beginning an ending parameter
93 // paramnum - param number (from 0)
94 // -------------------------------------------------------------------------
95 int param_getptr(const char *line, int *bg, int *en, int paramnum)
96 {
97 int i;
98 int len = strlen(line);
99
100 *bg = 0;
101 *en = 0;
102
103 // skip spaces
104 while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++;
105 if (*bg >= len) {
106 return 1;
107 }
108
109 for (i = 0; i < paramnum; i++) {
110 while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++;
111 while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++;
112
113 if (line[*bg] == '\0') return 1;
114 }
115
116 *en = *bg;
117 while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++;
118
119 (*en)--;
120
121 return 0;
122 }
123
124 char param_getchar(const char *line, int paramnum)
125 {
126 int bg, en;
127
128 if (param_getptr(line, &bg, &en, paramnum)) return 0x00;
129
130 return line[bg];
131 }
132
133 uint8_t param_get8(const char *line, int paramnum)
134 {
135 return param_get8ex(line, paramnum, 10, 0);
136 }
137
138 uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base)
139 {
140 int bg, en;
141
142 if (!param_getptr(line, &bg, &en, paramnum))
143 return strtol(&line[bg], NULL, base) & 0xff;
144 else
145 return deflt;
146 }
147
148 uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base)
149 {
150 int bg, en;
151
152 if (!param_getptr(line, &bg, &en, paramnum))
153 return strtol(&line[bg], NULL, base);
154 else
155 return deflt;
156 }
157
158 uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base)
159 {
160 int bg, en;
161
162 if (!param_getptr(line, &bg, &en, paramnum))
163 return strtol(&line[bg], NULL, base);
164 else
165 return deflt;
166
167 return 0;
168 }
169
170 int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt)
171 {
172 int bg, en, temp, i;
173
174 if (hexcnt % 2)
175 return 1;
176
177 if (param_getptr(line, &bg, &en, paramnum)) return 1;
178
179 if (en - bg + 1 != hexcnt)
180 return 1;
181
182 for(i = 0; i < hexcnt; i += 2) {
183 if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1;
184
185 sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp);
186 data[i / 2] = temp & 0xff;
187 }
188
189 return 0;
190 }
Impressum, Datenschutz