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 descriptor array, what we expect to find
208 * @fmt - format array, the output
209 * @fmt_sz - size of format array
211 * This function tries to find formats, e.g.:
212 * %o - offset of the block
214 * In case of successfull parsing it fills the format param
215 * with proper offset and the size of the expected value, which
216 * should be pasted into buffer using the format 'func' callback.
218 * Returns the end pointer where parsing has been stopped.
219 * In case of parsing error or lack of bytes in output buffer
220 * NULL will be returned.
222 static const char *parse_format(const char *in, char *out, unsigned int parsed,
223 unsigned int out_len, unsigned int *filled,
224 const struct pattern_fmt_desc *fmt_desc,
225 struct pattern_fmt *fmt, unsigned int fmt_sz)
228 struct pattern_fmt *f = NULL;
229 unsigned int len = 0;
231 if (!out_len || !fmt_desc || !fmt || !fmt_sz)
236 for (i = 0; fmt_desc[i].fmt; i++) {
237 const struct pattern_fmt_desc *desc;
240 len = strlen(desc->fmt);
241 if (0 == strncmp(in, desc->fmt, len)) {
251 if (f->desc->len > out_len)
254 memset(out, '\0', f->desc->len);
255 *filled = f->desc->len;
261 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
262 * numbers and pattern formats.
264 * @in_len - size of the input string
265 * @out - output buffer where parsed result will be put
266 * @out_len - lengths of the output buffer
267 * @fmt_desc - array of pattern format descriptors [input]
268 * @fmt - array of pattern formats [output]
269 * @fmt_sz - pointer where the size of pattern formats array stored [input],
270 * after successfull parsing this pointer will contain the number
271 * of parsed formats if any [output].
274 * bytes sequence in double quotes, e.g. "123".
275 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
278 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
279 * e.g. 0xff12ceff1100ff
280 * decimal - decimal number in range [INT_MIN, INT_MAX]
283 * %o - offset of block, reserved 8 bytes.
285 * Explicit examples of combined string:
287 * in="abcd" in=-1024 in=66 in=0xFF0X1
288 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
291 * in=%o in="123"0xFFeeCC
292 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
296 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
300 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
303 * in=0xfefefefefefefefefefefefefefefefefefefefefe
304 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
306 * Returns number of bytes filled or err < 0 in case of failure.
308 int parse_and_fill_pattern(const char *in, unsigned int in_len,
309 char *out, unsigned int out_len,
310 const struct pattern_fmt_desc *fmt_desc,
311 struct pattern_fmt *fmt,
312 unsigned int *fmt_sz_out)
314 const char *beg, *end, *out_beg = out;
315 unsigned int total = 0, fmt_rem = 0;
317 if (!in || !in_len || !out || !out_len)
320 fmt_rem = *fmt_sz_out;
332 end = parse_file(beg, out, out_len, &filled);
335 end = parse_string(beg, out, out_len, &filled);
338 end = parse_format(beg, out, out - out_beg, out_len,
339 &filled, fmt_desc, fmt, fmt_rem);
343 end = parse_number(beg, out, out_len, &filled);
356 assert(end - beg <= in_len);
361 assert(filled <= out_len);
369 *fmt_sz_out -= fmt_rem;
374 * dup_pattern() - Duplicates part of the pattern all over the buffer.
376 * Returns 0 in case of success or errno < 0 in case of failure.
378 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
380 unsigned int left, len, off;
382 if (out_len <= pattern_len)
387 left = (out_len - off);
388 len = min(left, off);
390 /* Duplicate leftover */
392 memcpy(out + off, out, len);
395 len = min(left, off);
402 * cpy_pattern() - Copies pattern to the buffer.
404 * Function copies pattern along the whole buffer.
406 * Returns 0 in case of success or errno < 0 in case of failure.
408 int cpy_pattern(const char *pattern, unsigned int pattern_len,
409 char *out, unsigned int out_len)
413 if (!pattern || !pattern_len || !out || !out_len)
417 len = min(pattern_len, out_len);
418 memcpy(out, pattern, len);
420 /* Spread filled chunk all over the buffer */
421 return dup_pattern(out, out_len, pattern_len);
425 * cmp_pattern() - Compares pattern and buffer.
427 * For the sake of performance this function avoids any loops.
428 * Firstly it tries to compare the buffer itself, checking that
429 * buffer consists of repeating patterns along the buffer size.
431 * If the difference is not found then the function tries to compare
432 * buffer and pattern.
434 * Returns 0 in case of success or errno < 0 in case of failure.
436 int cmp_pattern(const char *pattern, unsigned int pattern_size,
437 unsigned int off, const char *buf, unsigned int len)
442 /* Find the difference in buffer */
443 if (len > pattern_size) {
444 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
448 /* Compare second part of the pattern with buffer */
450 size = min(len, pattern_size - off);
451 rc = memcmp(buf, pattern + off, size);
457 /* Compare first part of the pattern or the whole pattern
460 size = min(len, (off ? off : pattern_size));
461 rc = memcmp(buf, pattern, size);
470 * paste_format_inplace() - Pastes parsed formats to the pattern.
472 * This function pastes formats to the pattern. If @fmt_sz is 0
473 * function does nothing and pattern buffer is left untouched.
475 * Returns 0 in case of success or errno < 0 in case of failure.
477 int paste_format_inplace(char *pattern, unsigned int pattern_len,
478 struct pattern_fmt *fmt, unsigned int fmt_sz,
484 if (!pattern || !pattern_len || !fmt)
487 /* Paste formats for first pattern chunk */
488 for (i = 0; i < fmt_sz; i++) {
489 struct pattern_fmt *f;
492 if (pattern_len <= f->off)
494 len = min(pattern_len - f->off, f->desc->len);
495 rc = f->desc->paste(pattern + f->off, len, priv);
504 * paste_format() - Pastes parsed formats to the buffer.
506 * This function copies pattern to the buffer, pastes format
507 * into it and then duplicates pattern all over the buffer size.
509 * Returns 0 in case of success or errno < 0 in case of failure.
511 int paste_format(const char *pattern, unsigned int pattern_len,
512 struct pattern_fmt *fmt, unsigned int fmt_sz,
513 char *out, unsigned int out_len, void *priv)
518 if (!pattern || !pattern_len || !out || !out_len)
522 len = min(pattern_len, out_len);
523 memcpy(out, pattern, len);
525 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
529 /* Spread filled chunk all over the buffer */
530 return dup_pattern(out, out_len, pattern_len);