14 #include "../minmax.h"
15 #include "../oslib/strcasestr.h"
18 * parse_file() - parses binary file to fill buffer
19 * @beg - string input, extract filename from this
20 * @out - output buffer where parsed number should be put
21 * @out_len - length of the output buffer
22 * @filled - pointer where number of bytes successfully
25 * Returns the end pointer where parsing has been stopped.
26 * In case of parsing error or lack of bytes in output buffer
27 * NULL will be returned.
29 static const char *parse_file(const char *beg, char *out,
43 end = strchr(beg, '\'');
47 file = strndup(beg, end - beg);
51 fd = open(file, O_RDONLY);
55 count = read(fd, out, out_len);
57 goto err_free_close_out;
76 * parse_string() - parses string in double quotes, like "abc"
78 * @out - output buffer where parsed number should be put
79 * @out_len - length of the output buffer
80 * @filled - pointer where number of bytes successfully
83 * Returns the end pointer where parsing has been stopped.
84 * In case of parsing error or lack of bytes in output buffer
85 * NULL will be returned.
87 static const char *parse_string(const char *beg, char *out,
98 end = strchr(beg, '"');
101 if (end - beg > out_len)
104 memcpy(out, beg, end - beg);
112 * parse_number() - parses numbers
113 * @beg - string input
114 * @out - output buffer where parsed number should be put
115 * @out_len - length of the output buffer
116 * @filled - pointer where number of bytes successfully
119 * Supports decimals in the range [INT_MIN, INT_MAX] and
120 * hexidecimals of any size, which should be started with
123 * Returns the end pointer where parsing has been stopped.
124 * In case of parsing error or lack of bytes in output buffer
125 * NULL will be returned.
127 static const char *parse_number(const char *beg, char *out,
128 unsigned int out_len,
129 unsigned int *filled)
140 sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num);
142 /* Here we are trying to parse decimal */
147 _end = strcasestr(beg, "0x");
151 lval = strntol(beg, num, &_end, 10);
153 lval = strtol(beg, &_end, 10);
154 if (beg == _end || lval > INT_MAX || lval < INT_MIN)
163 val = (unsigned int)lval;
164 for (; val && out_len; out_len--, i++, val >>= 8)
172 /* Catch up 0x prefix */
176 /* Look back, handle this combined string: 0xff0x14 */
177 if (beg[num] && !strncasecmp(&beg[num - 1], "0x", 2))
182 for (i = 0; num && out_len;
183 out_len--, i++, num -= 2, beg += 2) {
186 fmt = (num & 1 ? "%1hhx" : "%2hhx");
187 sscanf(beg, fmt, &out[i]);
203 * parse_format() - parses formats, like %o, etc
205 * @out - output buffer where space for format should be reserved
206 * @parsed - number of bytes which were already parsed so far
207 * @out_len - length of the output buffer
208 * @fmt_desc - format descritor array, what we expect to find
209 * @fmt_desc_sz - size of the format descritor array
210 * @fmt - format array, the output
211 * @fmt_sz - size of format array
213 * This function tries to find formats, e.g.:
214 * %o - offset of the block
216 * In case of successfull parsing it fills the format param
217 * with proper offset and the size of the expected value, which
218 * should be pasted into buffer using the format 'func' callback.
220 * Returns the end pointer where parsing has been stopped.
221 * In case of parsing error or lack of bytes in output buffer
222 * NULL will be returned.
224 static const char *parse_format(const char *in, char *out, unsigned int parsed,
225 unsigned int out_len, unsigned int *filled,
226 const struct pattern_fmt_desc *fmt_desc,
227 unsigned int fmt_desc_sz,
228 struct pattern_fmt *fmt, unsigned int fmt_sz)
231 struct pattern_fmt *f = NULL;
232 unsigned int len = 0;
234 if (!out_len || !fmt_desc || !fmt_desc_sz || !fmt || !fmt_sz)
239 for (i = 0; i < fmt_desc_sz; i++) {
240 const struct pattern_fmt_desc *desc;
243 len = strlen(desc->fmt);
244 if (0 == strncmp(in, desc->fmt, len)) {
254 if (f->desc->len > out_len)
257 memset(out, '\0', f->desc->len);
258 *filled = f->desc->len;
264 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
265 * numbers and pattern formats.
267 * @in_len - size of the input string
268 * @out - output buffer where parsed result will be put
269 * @out_len - lengths of the output buffer
270 * @fmt_desc - array of pattern format descriptors [input]
271 * @fmt_desc_sz - size of the format descriptor array
272 * @fmt - array of pattern formats [output]
273 * @fmt_sz - pointer where the size of pattern formats array stored [input],
274 * after successfull parsing this pointer will contain the number
275 * of parsed formats if any [output].
278 * bytes sequence in double quotes, e.g. "123".
279 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
282 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
283 * e.g. 0xff12ceff1100ff
284 * decimal - decimal number in range [INT_MIN, INT_MAX]
287 * %o - offset of block, reserved 8 bytes.
289 * Explicit examples of combined string:
291 * in="abcd" in=-1024 in=66 in=0xFF0X1
292 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
295 * in=%o in="123"0xFFeeCC
296 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
300 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
304 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
307 * in=0xfefefefefefefefefefefefefefefefefefefefefe
308 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
310 * Returns number of bytes filled or err < 0 in case of failure.
312 int parse_and_fill_pattern(const char *in, unsigned int in_len,
313 char *out, unsigned int out_len,
314 const struct pattern_fmt_desc *fmt_desc,
315 unsigned int fmt_desc_sz,
316 struct pattern_fmt *fmt,
317 unsigned int *fmt_sz_out)
319 const char *beg, *end, *out_beg = out;
320 unsigned int total = 0, fmt_rem = 0;
322 if (!in || !in_len || !out || !out_len)
325 fmt_rem = *fmt_sz_out;
337 end = parse_file(beg, out, out_len, &filled);
340 end = parse_string(beg, out, out_len, &filled);
343 end = parse_format(beg, out, out - out_beg, out_len,
344 &filled, fmt_desc, fmt_desc_sz,
349 end = parse_number(beg, out, out_len, &filled);
362 assert(end - beg <= in_len);
367 assert(filled <= out_len);
375 *fmt_sz_out -= fmt_rem;
380 * dup_pattern() - Duplicates part of the pattern all over the buffer.
382 * Returns 0 in case of success or errno < 0 in case of failure.
384 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
386 unsigned int left, len, off;
388 if (out_len <= pattern_len)
393 left = (out_len - off);
394 len = min(left, off);
396 /* Duplicate leftover */
398 memcpy(out + off, out, len);
401 len = min(left, off);
408 * cpy_pattern() - Copies pattern to the buffer.
410 * Function copies pattern along the whole buffer.
412 * Returns 0 in case of success or errno < 0 in case of failure.
414 int cpy_pattern(const char *pattern, unsigned int pattern_len,
415 char *out, unsigned int out_len)
419 if (!pattern || !pattern_len || !out || !out_len)
423 len = min(pattern_len, out_len);
424 memcpy(out, pattern, len);
426 /* Spread filled chunk all over the buffer */
427 return dup_pattern(out, out_len, pattern_len);
431 * cmp_pattern() - Compares pattern and buffer.
433 * For the sake of performance this function avoids any loops.
434 * Firstly it tries to compare the buffer itself, checking that
435 * buffer consists of repeating patterns along the buffer size.
437 * If the difference is not found then the function tries to compare
438 * buffer and pattern.
440 * Returns 0 in case of success or errno < 0 in case of failure.
442 int cmp_pattern(const char *pattern, unsigned int pattern_size,
443 unsigned int off, const char *buf, unsigned int len)
448 /* Find the difference in buffer */
449 if (len > pattern_size) {
450 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
454 /* Compare second part of the pattern with buffer */
456 size = min(len, pattern_size - off);
457 rc = memcmp(buf, pattern + off, size);
463 /* Compare first part of the pattern or the whole pattern
466 size = min(len, (off ? off : pattern_size));
467 rc = memcmp(buf, pattern, size);
476 * paste_format_inplace() - Pastes parsed formats to the pattern.
478 * This function pastes formats to the pattern. If @fmt_sz is 0
479 * function does nothing and pattern buffer is left untouched.
481 * Returns 0 in case of success or errno < 0 in case of failure.
483 int paste_format_inplace(char *pattern, unsigned int pattern_len,
484 struct pattern_fmt *fmt, unsigned int fmt_sz,
490 if (!pattern || !pattern_len || !fmt)
493 /* Paste formats for first pattern chunk */
494 for (i = 0; i < fmt_sz; i++) {
495 struct pattern_fmt *f;
498 if (pattern_len <= f->off)
500 len = min(pattern_len - f->off, f->desc->len);
501 rc = f->desc->paste(pattern + f->off, len, priv);
510 * paste_format() - Pastes parsed formats to the buffer.
512 * This function copies pattern to the buffer, pastes format
513 * into it and then duplicates pattern all over the buffer size.
515 * Returns 0 in case of success or errno < 0 in case of failure.
517 int paste_format(const char *pattern, unsigned int pattern_len,
518 struct pattern_fmt *fmt, unsigned int fmt_sz,
519 char *out, unsigned int out_len, void *priv)
524 if (!pattern || !pattern_len || !out || !out_len)
528 len = min(pattern_len, out_len);
529 memcpy(out, pattern, len);
531 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
535 /* Spread filled chunk all over the buffer */
536 return dup_pattern(out, out_len, pattern_len);