6 * parse_string() - parses string in double quotes, like "abc"
8 * @out - output buffer where parsed number should be put
9 * @out_len - length of the output buffer
10 * @filled - pointer where number of bytes successfully
13 * Returns the end pointer where parsing has been stopped.
14 * In case of parsing error or lack of bytes in output buffer
15 * NULL will be returned.
17 static const char *parse_string(const char *beg, char *out,
28 end = strchr(beg, '"');
31 if (end - beg > out_len)
34 memcpy(out, beg, end - beg);
42 * parse_number() - parses numbers
44 * @out - output buffer where parsed number should be put
45 * @out_len - length of the output buffer
46 * @filled - pointer where number of bytes successfully
49 * Supports decimals in the range [INT_MIN, INT_MAX] and
50 * hexidecimals of any size, which should be started with
53 * Returns the end pointer where parsing has been stopped.
54 * In case of parsing error or lack of bytes in output buffer
55 * NULL will be returned.
57 static const char *parse_number(const char *beg, char *out,
70 sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num);
72 /* Here we are trying to parse decimal */
77 _end = strcasestr(beg, "0x");
81 lval = strntol(beg, num, &_end, 10);
83 lval = strtol(beg, &_end, 10);
84 if (beg == _end || lval > INT_MAX || lval < INT_MIN)
93 val = (unsigned int)lval;
94 for (; val && out_len; out_len--, i++, val >>= 8)
102 /* Catch up 0x prefix */
106 /* Look back, handle this combined string: 0xff0x14 */
107 if (beg[num] && !strncasecmp(&beg[num - 1], "0x", 2))
112 for (i = 0; num && out_len;
113 out_len--, i++, num -= 2, beg += 2) {
116 fmt = (num & 1 ? "%1hhx" : "%2hhx");
117 sscanf(beg, fmt, &out[i]);
133 * parse_format() - parses formats, like %o, etc
135 * @out - output buffer where space for format should be reserved
136 * @parsed - number of bytes which were already parsed so far
137 * @out_len - length of the output buffer
138 * @fmt_desc - format descritor array, what we expect to find
139 * @fmt_desc_sz - size of the format descritor array
140 * @fmt - format array, the output
141 * @fmt_sz - size of format array
143 * This function tries to find formats, e.g.:
144 * %o - offset of the block
146 * In case of successfull parsing it fills the format param
147 * with proper offset and the size of the expected value, which
148 * should be pasted into buffer using the format 'func' callback.
150 * Returns the end pointer where parsing has been stopped.
151 * In case of parsing error or lack of bytes in output buffer
152 * NULL will be returned.
154 static const char *parse_format(const char *in, char *out, unsigned int parsed,
155 unsigned int out_len, unsigned int *filled,
156 const struct pattern_fmt_desc *fmt_desc,
157 unsigned int fmt_desc_sz,
158 struct pattern_fmt *fmt, unsigned int fmt_sz)
161 struct pattern_fmt *f = NULL;
162 unsigned int len = 0;
164 if (!out_len || !fmt_desc || !fmt_desc_sz || !fmt || !fmt_sz)
169 for (i = 0; i < fmt_desc_sz; i++) {
170 const struct pattern_fmt_desc *desc;
173 len = strlen(desc->fmt);
174 if (0 == strncmp(in, desc->fmt, len)) {
184 if (f->desc->len > out_len)
187 memset(out, '\0', f->desc->len);
188 *filled = f->desc->len;
194 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
195 * numbers and pattern formats.
197 * @in_len - size of the input string
198 * @out - output buffer where parsed result will be put
199 * @out_len - lengths of the output buffer
200 * @fmt_desc - array of pattern format descriptors [input]
201 * @fmt_desc_sz - size of the format descriptor array
202 * @fmt - array of pattern formats [output]
203 * @fmt_sz - pointer where the size of pattern formats array stored [input],
204 * after successfull parsing this pointer will contain the number
205 * of parsed formats if any [output].
208 * bytes sequence in double quotes, e.g. "123".
209 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
212 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
213 * e.g. 0xff12ceff1100ff
214 * decimal - decimal number in range [INT_MIN, INT_MAX]
217 * %o - offset of block, reserved 8 bytes.
219 * Explicit examples of combined string:
221 * in="abcd" in=-1024 in=66 in=0xFF0X1
222 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
225 * in=%o in="123"0xFFeeCC
226 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
230 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
234 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
237 * in=0xfefefefefefefefefefefefefefefefefefefefefe
238 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
240 * Returns number of bytes filled or err < 0 in case of failure.
242 int parse_and_fill_pattern(const char *in, unsigned int in_len,
243 char *out, unsigned int out_len,
244 const struct pattern_fmt_desc *fmt_desc,
245 unsigned int fmt_desc_sz,
246 struct pattern_fmt *fmt,
247 unsigned int *fmt_sz_out)
249 const char *beg, *end, *out_beg = out;
250 unsigned int total = 0, fmt_rem = 0;
252 if (!in || !in_len || !out || !out_len)
255 fmt_rem = *fmt_sz_out;
267 end = parse_string(beg, out, out_len, &filled);
270 end = parse_format(beg, out, out - out_beg, out_len,
271 &filled, fmt_desc, fmt_desc_sz,
276 end = parse_number(beg, out, out_len, &filled);
289 assert(end - beg <= in_len);
294 assert(filled <= out_len);
302 *fmt_sz_out -= fmt_rem;
307 * dup_pattern() - Duplicates part of the pattern all over the buffer.
309 * Returns 0 in case of success or errno < 0 in case of failure.
311 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
313 unsigned int left, len, off;
315 if (out_len <= pattern_len)
320 left = (out_len - off);
321 len = min(left, off);
323 /* Duplicate leftover */
325 memcpy(out + off, out, len);
328 len = min(left, off);
335 * cpy_pattern() - Copies pattern to the buffer.
337 * Function copies pattern along the whole buffer.
339 * Returns 0 in case of success or errno < 0 in case of failure.
341 int cpy_pattern(const char *pattern, unsigned int pattern_len,
342 char *out, unsigned int out_len)
346 if (!pattern || !pattern_len || !out || !out_len)
350 len = min(pattern_len, out_len);
351 memcpy(out, pattern, len);
353 /* Spread filled chunk all over the buffer */
354 return dup_pattern(out, out_len, pattern_len);
358 * cmp_pattern() - Compares pattern and buffer.
360 * For the sake of performance this function avoids any loops.
361 * Firstly it tries to compare the buffer itself, checking that
362 * buffer consists of repeating patterns along the buffer size.
364 * If the difference is not found then the function tries to compare
365 * buffer and pattern.
367 * Returns 0 in case of success or errno < 0 in case of failure.
369 int cmp_pattern(const char *pattern, unsigned int pattern_size,
370 unsigned int off, const char *buf, unsigned int len)
375 /* Find the difference in buffer */
376 if (len > pattern_size) {
377 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
381 /* Compare second part of the pattern with buffer */
383 size = min(len, pattern_size - off);
384 rc = memcmp(buf, pattern + off, size);
390 /* Compare first part of the pattern or the whole pattern
393 size = min(len, (off ? off : pattern_size));
394 rc = memcmp(buf, pattern, size);
403 * paste_format_inplace() - Pastes parsed formats to the pattern.
405 * This function pastes formats to the pattern. If @fmt_sz is 0
406 * function does nothing and pattern buffer is left untouched.
408 * Returns 0 in case of success or errno < 0 in case of failure.
410 int paste_format_inplace(char *pattern, unsigned int pattern_len,
411 struct pattern_fmt *fmt, unsigned int fmt_sz,
417 if (!pattern || !pattern_len || !fmt)
420 /* Paste formats for first pattern chunk */
421 for (i = 0; i < fmt_sz; i++) {
422 struct pattern_fmt *f;
425 if (pattern_len <= f->off)
427 len = min(pattern_len - f->off, f->desc->len);
428 rc = f->desc->paste(pattern + f->off, len, priv);
437 * paste_format() - Pastes parsed formats to the buffer.
439 * This function copies pattern to the buffer, pastes format
440 * into it and then duplicates pattern all over the buffer size.
442 * Returns 0 in case of success or errno < 0 in case of failure.
444 int paste_format(const char *pattern, unsigned int pattern_len,
445 struct pattern_fmt *fmt, unsigned int fmt_sz,
446 char *out, unsigned int out_len, void *priv)
451 if (!pattern || !pattern_len || !out || !out_len)
455 len = min(pattern_len, out_len);
456 memcpy(out, pattern, len);
458 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
462 /* Spread filled chunk all over the buffer */
463 return dup_pattern(out, out_len, pattern_len);