]>
Commit | Line | Data |
---|---|---|
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 | ||
f397b5cc M |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <ctype.h> | |
20f9a2a1 | 15 | #include "util.h" |
534983d7 | 16 | |
f397b5cc | 17 | #ifdef __linux__ |
873014de M |
18 | #include <termios.h> |
19 | #include <sys/ioctl.h> | |
f397b5cc M |
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 | ||
534983d7 | 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 | } | |
f89c7050 M |
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 | } | |
f397b5cc M |
91 | |
92 | // ------------------------------------------------------------------------- | |
93 | // line - param line | |
94 | // bg, en - symbol numbers in param line of beginning an ending parameter | |
95 | // paramnum - param number (from 0) | |
96 | // ------------------------------------------------------------------------- | |
97 | int param_getptr(const char *line, int *bg, int *en, int paramnum) | |
98 | { | |
99 | int i; | |
100 | int len = strlen(line); | |
101 | ||
102 | *bg = 0; | |
103 | *en = 0; | |
104 | ||
105 | // skip spaces | |
106 | while (line[*bg] ==' ' || line[*bg]=='\t') (*bg)++; | |
107 | if (*bg >= len) { | |
108 | return 1; | |
109 | } | |
110 | ||
111 | for (i = 0; i < paramnum; i++) { | |
112 | while (line[*bg]!=' ' && line[*bg]!='\t' && line[*bg] != '\0') (*bg)++; | |
113 | while (line[*bg]==' ' || line[*bg]=='\t') (*bg)++; | |
114 | ||
115 | if (line[*bg] == '\0') return 1; | |
116 | } | |
117 | ||
118 | *en = *bg; | |
119 | while (line[*en] != ' ' && line[*en] != '\t' && line[*en] != '\0') (*en)++; | |
120 | ||
121 | (*en)--; | |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | char param_getchar(const char *line, int paramnum) | |
127 | { | |
128 | int bg, en; | |
129 | ||
130 | if (param_getptr(line, &bg, &en, paramnum)) return 0x00; | |
131 | ||
132 | return line[bg]; | |
133 | } | |
134 | ||
135 | uint8_t param_get8(const char *line, int paramnum) | |
136 | { | |
137 | return param_get8ex(line, paramnum, 10, 0); | |
138 | } | |
139 | ||
140 | uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base) | |
141 | { | |
142 | int bg, en; | |
143 | ||
144 | if (!param_getptr(line, &bg, &en, paramnum)) | |
145 | return strtol(&line[bg], NULL, base) & 0xff; | |
146 | else | |
147 | return deflt; | |
148 | } | |
149 | ||
150 | uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base) | |
151 | { | |
152 | int bg, en; | |
153 | ||
154 | if (!param_getptr(line, &bg, &en, paramnum)) | |
155 | return strtol(&line[bg], NULL, base); | |
156 | else | |
157 | return deflt; | |
158 | } | |
159 | ||
160 | uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base) | |
161 | { | |
162 | int bg, en; | |
163 | ||
164 | if (!param_getptr(line, &bg, &en, paramnum)) | |
165 | return strtol(&line[bg], NULL, base); | |
166 | else | |
167 | return deflt; | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | int param_gethex(const char *line, int paramnum, uint8_t * data, int hexcnt) | |
173 | { | |
174 | int bg, en, temp, i; | |
175 | ||
176 | if (hexcnt % 2) | |
177 | return 1; | |
178 | ||
179 | if (param_getptr(line, &bg, &en, paramnum)) return 1; | |
180 | ||
181 | if (en - bg + 1 != hexcnt) | |
182 | return 1; | |
183 | ||
184 | for(i = 0; i < hexcnt; i += 2) { | |
185 | if (!(isxdigit(line[bg + i]) && isxdigit(line[bg + i + 1])) ) return 1; | |
186 | ||
187 | sscanf((char[]){line[bg + i], line[bg + i + 1], 0}, "%X", &temp); | |
188 | data[i / 2] = temp & 0xff; | |
189 | } | |
190 | ||
191 | return 0; | |
192 | } |