-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsing_plug.c
305 lines (268 loc) · 6.87 KB
/
parsing_plug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- c-basic-offset: 2 -*- */
/* Copyright © 2015 Alexander Cherepanov <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*/
/* for size_t */
#include <stddef.h>
/* for uint32_t */
#include <stdint.h>
/* for isdigit etc. */
#include <ctype.h>
/* for va_list etc. */
#include <stdarg.h>
/* for strlen */
#include <string.h>
#include "parsing.h"
#pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
static int initialized = 0;
static unsigned char dec[256], hex[256];
static void init()
{
int i;
for (i = 0; i < 10; i++) {
dec['0' + i] = i;
hex['0' + i] = i;
}
for (i = 10; i < 16; i++) {
hex['A' + (i - 10)] = i;
hex['a' + (i - 10)] = i;
}
}
/**********************************************************************/
/*
decimal number, positive, without leading zeroes (except for zero itself)
(uint32_t only)
empty string is not allowed
gobbles all digits
*/
static const unsigned char *num_valid(const unsigned char *s, uint32_t min, uint32_t max, uint32_t *number)
{
if (!s)
return 0;
/* convert */
/* leading zeroes */
const unsigned char *s0 = s; /* start of the string */
while (*s == '0')
s++;
/* further digits */
const unsigned char *s1 = s; /* after leading zeroes */
uint32_t n = 0;
while (isdigit(*s))
n = n * 10 + dec[*s++];
/* check */
/* only two good cases: one 0 and no other digits or no 0 and there are other digits*/
if (!((s1 == s0 + 1 && s == s1) || (s1 == s0 && s > s1)))
return 0;
if (s - s1 > 10) /* too many digits for uint32_t */
return 0;
if (s - s1 == 10 && (*s1 > '4' || n < 1000000000u)) /* overflow in exactly 10 digits */
return 0;
if (n < min || n > max)
return 0;
/* results */
if (number)
*number = n;
return s;
}
static const unsigned char *num_extract(const unsigned char *s, uint32_t *number)
{
/* convert */
uint32_t n = 0;
while (isdigit(*s))
n = n * 10 + dec[*s++];
/* results */
if (number)
*number = n;
return s;
}
/**********************************************************************/
/*
binary data of variable length in hex
empty string is allowed
gobbles all hex digits
stores length and data
size should be <= SIZE_MAX / 2
*/
static const unsigned char *hex_var_valid(const unsigned char *s, size_t size, size_t *length)
{
if (!s)
return 0;
/* convert */
const unsigned char *s0 = s;
while (isxdigit(*s))
s++;
/* check */
size_t len = s - s0; /* casted from ptrdiff_t to size_t */
if (len % 2 != 0 || len > size * 2)
return 0;
/* results */
if (length)
*length = len / 2;
return s;
}
static const unsigned char *hex_var_extract(const unsigned char *s, size_t *length, unsigned char *buffer)
{
/* convert */
unsigned char *p = buffer;
while (isxdigit(s[0])) {
*p++ = hex[s[0]] * 16 + hex[s[1]];
s += 2;
}
/* results */
*length = p - buffer; /* casted from ptrdiff_t to size_t */
return s;
}
/**********************************************************************/
/*
generic parsing function -- validation
*/
int proc_valid(const char *s0, const char *format0, ...)
{
if (!initialized)
init();
va_list ap;
const unsigned char *s = (const unsigned char *)s0;
const unsigned char *format = (const unsigned char *)format0;
va_start(ap, format0);
/* for %l */
int have_length = 0;
uint32_t length = 0; /* silence -Wmaybe-uninitialized */
const unsigned char *p = format;
while (*p && s) {
if (*p == ' ') {
/* ignore spaces */
p++;
} else if (*p != '%') {
/* fixed */
/* printf(" %c", *p); */
if (*s == *p)
s++;
else
s = 0;
p++;
} else {
p++; /* skip % */
/* numbers like in %40h or %1-64d */
size_t size1, size2 = UINT32_MAX;
if (*p == '*') {
size1 = va_arg(ap, int);
p++;
} else {
size1 = 0;
while (isdigit(*p))
size1 = size1 * 10 + dec[*p++];
}
if (*p == '-') { /* range */
p++;
if (*p == '*') {
size2 = va_arg(ap, int);
p++;
} else {
size2 = 0;
while (isdigit(*p))
size2 = size2 * 10 + dec[*p++];
}
}
/* specifiers */
switch (*p) {
case 'd': /* number */
/* printf(" %%(%zu-%zu)d", size1, size2); */
s = num_valid(s, size1, size2, 0);
break;
case 'l': /* length of the next data field of variable length*/
/* printf(" %%l"); */
have_length = 1;
s = num_valid(s, 0, (uint32_t)-1, &length);
break;
case 'h': /* hex data */
/* printf(" %%(%zu)h", size1); */
{
size_t len = 0;
/* printf("hi there 1\n"); */
/* printf("hi there 4\n"); */
/* printf("%p %d\n", s, size1); */
/* printf("%s\n", s); */
/* printf("hi there 3\n"); */
s = hex_var_valid(s, size1, &len);
/* printf("hi there 2\n"); */
if (have_length && len != length)
s = 0;
else
have_length = 0;
}
break;
}
p++;
}
}
/* puts(""); */
va_end(ap);
return *p == 0 && s && *s == 0;
}
/*
generic parsing function -- extraction
*/
void proc_extract(const char *s0, const char *format0, ...)
{
if (!initialized)
init();
va_list ap;
const unsigned char *s = (const unsigned char *)s0;
const unsigned char *format = (const unsigned char *)format0;
va_start(ap, format0);
const unsigned char *p = format;
while (*p) {
if (*p == ' ') {
/* ignore spaces */
p++;
} else if (*p != '%') {
/* fixed */
/* printf(" %c", *p); */
s++;
p++;
} else {
p++; /* skip % */
if (*p == '*') {
p++;
} else {
while (isdigit(*p))
p++;
}
if (*p == '-') { /* range */
p++;
if (*p == '*') {
p++;
} else {
while (isdigit(*p))
p++;
}
}
/* specifiers */
switch (*p) {
case 'd': /* number */
/* printf(" %%(*-*)d"); */
s = num_extract(s, va_arg(ap, uint32_t *));
break;
case 'l': /* length of the next data field of variable length*/
/* printf(" %%l"); */
s = num_extract(s, 0);
break;
case 'h': /* hex data */
/* printf(" %%(*)h"); */
{
size_t *t1 = va_arg(ap, size_t *);
unsigned char *t2 = va_arg(ap, unsigned char *);
/* printf("11\n"); */
s = hex_var_extract(s, t1, t2);
/* printf("12\n"); */
}
break;
}
p++;
}
}
/* puts(""); */
va_end(ap);
}