12 #include "../minmax.h"
13 #include "../oslib/strcasestr.h"
14 #include "../oslib/strndup.h"
17 * parse_file() - parses binary file to fill buffer
18 * @beg - string input, extract filename from this
19 * @out - output buffer where parsed number should be put
20 * @out_len - length of the output buffer
21 * @filled - pointer where number of bytes successfully
24 * Returns the end pointer where parsing has been stopped.
25 * In case of parsing error or lack of bytes in output buffer
26 * NULL will be returned.
28 static const char *parse_file(const char *beg, char *out,
42 end = strchr(beg, '\'');
46 file = strndup(beg, end - beg);
50 fd = open(file, O_RDONLY);
54 count = read(fd, out, out_len);
56 goto err_free_close_out;
75 * parse_string() - parses string in double quotes, like "abc"
77 * @out - output buffer where parsed number should be put
78 * @out_len - length of the output buffer
79 * @filled - pointer where number of bytes successfully
82 * Returns the end pointer where parsing has been stopped.
83 * In case of parsing error or lack of bytes in output buffer
84 * NULL will be returned.
86 static const char *parse_string(const char *beg, char *out,
97 end = strchr(beg, '"');
100 if (end - beg > out_len)
103 memcpy(out, beg, end - beg);
111 * parse_number() - parses numbers
112 * @beg - string input
113 * @out - output buffer where parsed number should be put
114 * @out_len - length of the output buffer
115 * @filled - pointer where number of bytes successfully
118 * Supports decimals in the range [INT_MIN, INT_MAX] and
119 * hexidecimals of any size, which should be started with
122 * Returns the end pointer where parsing has been stopped.
123 * In case of parsing error or lack of bytes in output buffer
124 * NULL will be returned.
126 static const char *parse_number(const char *beg, char *out,
127 unsigned int out_len,
128 unsigned int *filled)
139 sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num);
141 /* Here we are trying to parse decimal */
146 _end = strcasestr(beg, "0x");
150 lval = strntol(beg, num, &_end, 10);
152 lval = strtol(beg, &_end, 10);
153 if (beg == _end || lval > INT_MAX || lval < INT_MIN)
162 val = (unsigned int)lval;
163 for (; val && out_len; out_len--, i++, val >>= 8)
171 /* Catch up 0x prefix */
175 /* Look back, handle this combined string: 0xff0x14 */
176 if (beg[num] && !strncasecmp(&beg[num - 1], "0x", 2))
181 for (i = 0; num && out_len;
182 out_len--, i++, num -= 2, beg += 2) {
185 fmt = (num & 1 ? "%1hhx" : "%2hhx");
186 sscanf(beg, fmt, &out[i]);
202 * parse_format() - parses formats, like %o, etc
204 * @out - output buffer where space for format should be reserved
205 * @parsed - number of bytes which were already parsed so far
206 * @out_len - length of the output buffer
207 * @fmt_desc - format descritor array, what we expect to find
208 * @fmt_desc_sz - size of the format descritor array
209 * @fmt - format array, the output
210 * @fmt_sz - size of format array
212 * This function tries to find formats, e.g.:
213 * %o - offset of the block
215 * In case of successfull parsing it fills the format param
216 * with proper offset and the size of the expected value, which
217 * should be pasted into buffer using the format 'func' callback.
219 * Returns the end pointer where parsing has been stopped.
220 * In case of parsing error or lack of bytes in output buffer
221 * NULL will be returned.
223 static const char *parse_format(const char *in, char *out, unsigned int parsed,
224 unsigned int out_len, unsigned int *filled,
225 const struct pattern_fmt_desc *fmt_desc,
226 unsigned int fmt_desc_sz,
227 struct pattern_fmt *fmt, unsigned int fmt_sz)
230 struct pattern_fmt *f = NULL;
231 unsigned int len = 0;
233 if (!out_len || !fmt_desc || !fmt_desc_sz || !fmt || !fmt_sz)
238 for (i = 0; i < fmt_desc_sz; i++) {
239 const struct pattern_fmt_desc *desc;
242 len = strlen(desc->fmt);
243 if (0 == strncmp(in, desc->fmt, len)) {
253 if (f->desc->len > out_len)
256 memset(out, '\0', f->desc->len);
257 *filled = f->desc->len;
263 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
264 * numbers and pattern formats.
266 * @in_len - size of the input string
267 * @out - output buffer where parsed result will be put
268 * @out_len - lengths of the output buffer
269 * @fmt_desc - array of pattern format descriptors [input]
270 * @fmt_desc_sz - size of the format descriptor array
271 * @fmt - array of pattern formats [output]
272 * @fmt_sz - pointer where the size of pattern formats array stored [input],
273 * after successfull parsing this pointer will contain the number
274 * of parsed formats if any [output].
277 * bytes sequence in double quotes, e.g. "123".
278 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
281 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
282 * e.g. 0xff12ceff1100ff
283 * decimal - decimal number in range [INT_MIN, INT_MAX]
286 * %o - offset of block, reserved 8 bytes.
288 * Explicit examples of combined string:
290 * in="abcd" in=-1024 in=66 in=0xFF0X1
291 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
294 * in=%o in="123"0xFFeeCC
295 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
299 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
303 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
306 * in=0xfefefefefefefefefefefefefefefefefefefefefe
307 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
309 * Returns number of bytes filled or err < 0 in case of failure.
311 int parse_and_fill_pattern(const char *in, unsigned int in_len,
312 char *out, unsigned int out_len,
313 const struct pattern_fmt_desc *fmt_desc,
314 unsigned int fmt_desc_sz,
315 struct pattern_fmt *fmt,
316 unsigned int *fmt_sz_out)
318 const char *beg, *end, *out_beg = out;
319 unsigned int total = 0, fmt_rem = 0;
321 if (!in || !in_len || !out || !out_len)
324 fmt_rem = *fmt_sz_out;
336 end = parse_file(beg, out, out_len, &filled);
339 end = parse_string(beg, out, out_len, &filled);
342 end = parse_format(beg, out, out - out_beg, out_len,
343 &filled, fmt_desc, fmt_desc_sz,
348 end = parse_number(beg, out, out_len, &filled);
361 assert(end - beg <= in_len);
366 assert(filled <= out_len);
374 *fmt_sz_out -= fmt_rem;
379 * dup_pattern() - Duplicates part of the pattern all over the buffer.
381 * Returns 0 in case of success or errno < 0 in case of failure.
383 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
385 unsigned int left, len, off;
387 if (out_len <= pattern_len)
392 left = (out_len - off);
393 len = min(left, off);
395 /* Duplicate leftover */
397 memcpy(out + off, out, len);
400 len = min(left, off);
407 * cpy_pattern() - Copies pattern to the buffer.
409 * Function copies pattern along the whole buffer.
411 * Returns 0 in case of success or errno < 0 in case of failure.
413 int cpy_pattern(const char *pattern, unsigned int pattern_len,
414 char *out, unsigned int out_len)
418 if (!pattern || !pattern_len || !out || !out_len)
422 len = min(pattern_len, out_len);
423 memcpy(out, pattern, len);
425 /* Spread filled chunk all over the buffer */
426 return dup_pattern(out, out_len, pattern_len);
430 * cmp_pattern() - Compares pattern and buffer.
432 * For the sake of performance this function avoids any loops.
433 * Firstly it tries to compare the buffer itself, checking that
434 * buffer consists of repeating patterns along the buffer size.
436 * If the difference is not found then the function tries to compare
437 * buffer and pattern.
439 * Returns 0 in case of success or errno < 0 in case of failure.
441 int cmp_pattern(const char *pattern, unsigned int pattern_size,
442 unsigned int off, const char *buf, unsigned int len)
447 /* Find the difference in buffer */
448 if (len > pattern_size) {
449 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
453 /* Compare second part of the pattern with buffer */
455 size = min(len, pattern_size - off);
456 rc = memcmp(buf, pattern + off, size);
462 /* Compare first part of the pattern or the whole pattern
465 size = min(len, (off ? off : pattern_size));
466 rc = memcmp(buf, pattern, size);
475 * paste_format_inplace() - Pastes parsed formats to the pattern.
477 * This function pastes formats to the pattern. If @fmt_sz is 0
478 * function does nothing and pattern buffer is left untouched.
480 * Returns 0 in case of success or errno < 0 in case of failure.
482 int paste_format_inplace(char *pattern, unsigned int pattern_len,
483 struct pattern_fmt *fmt, unsigned int fmt_sz,
489 if (!pattern || !pattern_len || !fmt)
492 /* Paste formats for first pattern chunk */
493 for (i = 0; i < fmt_sz; i++) {
494 struct pattern_fmt *f;
497 if (pattern_len <= f->off)
499 len = min(pattern_len - f->off, f->desc->len);
500 rc = f->desc->paste(pattern + f->off, len, priv);
509 * paste_format() - Pastes parsed formats to the buffer.
511 * This function copies pattern to the buffer, pastes format
512 * into it and then duplicates pattern all over the buffer size.
514 * Returns 0 in case of success or errno < 0 in case of failure.
516 int paste_format(const char *pattern, unsigned int pattern_len,
517 struct pattern_fmt *fmt, unsigned int fmt_sz,
518 char *out, unsigned int out_len, void *priv)
523 if (!pattern || !pattern_len || !out || !out_len)
527 len = min(pattern_len, out_len);
528 memcpy(out, pattern, len);
530 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
534 /* Spread filled chunk all over the buffer */
535 return dup_pattern(out, out_len, pattern_len);