10 #include "../minmax.h"
11 #include "../oslib/strcasestr.h"
14 * parse_string() - parses string in double quotes, like "abc"
16 * @out - output buffer where parsed number should be put
17 * @out_len - length of the output buffer
18 * @filled - pointer where number of bytes successfully
21 * Returns the end pointer where parsing has been stopped.
22 * In case of parsing error or lack of bytes in output buffer
23 * NULL will be returned.
25 static const char *parse_string(const char *beg, char *out,
36 end = strchr(beg, '"');
39 if (end - beg > out_len)
42 memcpy(out, beg, end - beg);
50 * parse_number() - parses numbers
52 * @out - output buffer where parsed number should be put
53 * @out_len - length of the output buffer
54 * @filled - pointer where number of bytes successfully
57 * Supports decimals in the range [INT_MIN, INT_MAX] and
58 * hexidecimals of any size, which should be started with
61 * Returns the end pointer where parsing has been stopped.
62 * In case of parsing error or lack of bytes in output buffer
63 * NULL will be returned.
65 static const char *parse_number(const char *beg, char *out,
78 sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num);
80 /* Here we are trying to parse decimal */
85 _end = strcasestr(beg, "0x");
89 lval = strntol(beg, num, &_end, 10);
91 lval = strtol(beg, &_end, 10);
92 if (beg == _end || lval > INT_MAX || lval < INT_MIN)
101 val = (unsigned int)lval;
102 for (; val && out_len; out_len--, i++, val >>= 8)
110 /* Catch up 0x prefix */
114 /* Look back, handle this combined string: 0xff0x14 */
115 if (beg[num] && !strncasecmp(&beg[num - 1], "0x", 2))
120 for (i = 0; num && out_len;
121 out_len--, i++, num -= 2, beg += 2) {
124 fmt = (num & 1 ? "%1hhx" : "%2hhx");
125 sscanf(beg, fmt, &out[i]);
141 * parse_format() - parses formats, like %o, etc
143 * @out - output buffer where space for format should be reserved
144 * @parsed - number of bytes which were already parsed so far
145 * @out_len - length of the output buffer
146 * @fmt_desc - format descritor array, what we expect to find
147 * @fmt_desc_sz - size of the format descritor array
148 * @fmt - format array, the output
149 * @fmt_sz - size of format array
151 * This function tries to find formats, e.g.:
152 * %o - offset of the block
154 * In case of successfull parsing it fills the format param
155 * with proper offset and the size of the expected value, which
156 * should be pasted into buffer using the format 'func' callback.
158 * Returns the end pointer where parsing has been stopped.
159 * In case of parsing error or lack of bytes in output buffer
160 * NULL will be returned.
162 static const char *parse_format(const char *in, char *out, unsigned int parsed,
163 unsigned int out_len, unsigned int *filled,
164 const struct pattern_fmt_desc *fmt_desc,
165 unsigned int fmt_desc_sz,
166 struct pattern_fmt *fmt, unsigned int fmt_sz)
169 struct pattern_fmt *f = NULL;
170 unsigned int len = 0;
172 if (!out_len || !fmt_desc || !fmt_desc_sz || !fmt || !fmt_sz)
177 for (i = 0; i < fmt_desc_sz; i++) {
178 const struct pattern_fmt_desc *desc;
181 len = strlen(desc->fmt);
182 if (0 == strncmp(in, desc->fmt, len)) {
192 if (f->desc->len > out_len)
195 memset(out, '\0', f->desc->len);
196 *filled = f->desc->len;
202 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
203 * numbers and pattern formats.
205 * @in_len - size of the input string
206 * @out - output buffer where parsed result will be put
207 * @out_len - lengths of the output buffer
208 * @fmt_desc - array of pattern format descriptors [input]
209 * @fmt_desc_sz - size of the format descriptor array
210 * @fmt - array of pattern formats [output]
211 * @fmt_sz - pointer where the size of pattern formats array stored [input],
212 * after successfull parsing this pointer will contain the number
213 * of parsed formats if any [output].
216 * bytes sequence in double quotes, e.g. "123".
217 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
220 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
221 * e.g. 0xff12ceff1100ff
222 * decimal - decimal number in range [INT_MIN, INT_MAX]
225 * %o - offset of block, reserved 8 bytes.
227 * Explicit examples of combined string:
229 * in="abcd" in=-1024 in=66 in=0xFF0X1
230 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
233 * in=%o in="123"0xFFeeCC
234 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
238 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
242 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
245 * in=0xfefefefefefefefefefefefefefefefefefefefefe
246 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
248 * Returns number of bytes filled or err < 0 in case of failure.
250 int parse_and_fill_pattern(const char *in, unsigned int in_len,
251 char *out, unsigned int out_len,
252 const struct pattern_fmt_desc *fmt_desc,
253 unsigned int fmt_desc_sz,
254 struct pattern_fmt *fmt,
255 unsigned int *fmt_sz_out)
257 const char *beg, *end, *out_beg = out;
258 unsigned int total = 0, fmt_rem = 0;
260 if (!in || !in_len || !out || !out_len)
263 fmt_rem = *fmt_sz_out;
275 end = parse_string(beg, out, out_len, &filled);
278 end = parse_format(beg, out, out - out_beg, out_len,
279 &filled, fmt_desc, fmt_desc_sz,
284 end = parse_number(beg, out, out_len, &filled);
297 assert(end - beg <= in_len);
302 assert(filled <= out_len);
310 *fmt_sz_out -= fmt_rem;
315 * dup_pattern() - Duplicates part of the pattern all over the buffer.
317 * Returns 0 in case of success or errno < 0 in case of failure.
319 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
321 unsigned int left, len, off;
323 if (out_len <= pattern_len)
328 left = (out_len - off);
329 len = min(left, off);
331 /* Duplicate leftover */
333 memcpy(out + off, out, len);
336 len = min(left, off);
343 * cpy_pattern() - Copies pattern to the buffer.
345 * Function copies pattern along the whole buffer.
347 * Returns 0 in case of success or errno < 0 in case of failure.
349 int cpy_pattern(const char *pattern, unsigned int pattern_len,
350 char *out, unsigned int out_len)
354 if (!pattern || !pattern_len || !out || !out_len)
358 len = min(pattern_len, out_len);
359 memcpy(out, pattern, len);
361 /* Spread filled chunk all over the buffer */
362 return dup_pattern(out, out_len, pattern_len);
366 * cmp_pattern() - Compares pattern and buffer.
368 * For the sake of performance this function avoids any loops.
369 * Firstly it tries to compare the buffer itself, checking that
370 * buffer consists of repeating patterns along the buffer size.
372 * If the difference is not found then the function tries to compare
373 * buffer and pattern.
375 * Returns 0 in case of success or errno < 0 in case of failure.
377 int cmp_pattern(const char *pattern, unsigned int pattern_size,
378 unsigned int off, const char *buf, unsigned int len)
383 /* Find the difference in buffer */
384 if (len > pattern_size) {
385 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
389 /* Compare second part of the pattern with buffer */
391 size = min(len, pattern_size - off);
392 rc = memcmp(buf, pattern + off, size);
398 /* Compare first part of the pattern or the whole pattern
401 size = min(len, (off ? off : pattern_size));
402 rc = memcmp(buf, pattern, size);
411 * paste_format_inplace() - Pastes parsed formats to the pattern.
413 * This function pastes formats to the pattern. If @fmt_sz is 0
414 * function does nothing and pattern buffer is left untouched.
416 * Returns 0 in case of success or errno < 0 in case of failure.
418 int paste_format_inplace(char *pattern, unsigned int pattern_len,
419 struct pattern_fmt *fmt, unsigned int fmt_sz,
425 if (!pattern || !pattern_len || !fmt)
428 /* Paste formats for first pattern chunk */
429 for (i = 0; i < fmt_sz; i++) {
430 struct pattern_fmt *f;
433 if (pattern_len <= f->off)
435 len = min(pattern_len - f->off, f->desc->len);
436 rc = f->desc->paste(pattern + f->off, len, priv);
445 * paste_format() - Pastes parsed formats to the buffer.
447 * This function copies pattern to the buffer, pastes format
448 * into it and then duplicates pattern all over the buffer size.
450 * Returns 0 in case of success or errno < 0 in case of failure.
452 int paste_format(const char *pattern, unsigned int pattern_len,
453 struct pattern_fmt *fmt, unsigned int fmt_sz,
454 char *out, unsigned int out_len, void *priv)
459 if (!pattern || !pattern_len || !out || !out_len)
463 len = min(pattern_len, out_len);
464 memcpy(out, pattern, len);
466 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
470 /* Spread filled chunk all over the buffer */
471 return dup_pattern(out, out_len, pattern_len);