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