4 #include "../oslib/strcasestr.h"
7 * parse_string() - parses string in double quotes, like "abc"
9 * @out - output buffer where parsed number should be put
10 * @out_len - length of the output buffer
11 * @filled - pointer where number of bytes successfully
14 * Returns the end pointer where parsing has been stopped.
15 * In case of parsing error or lack of bytes in output buffer
16 * NULL will be returned.
18 static const char *parse_string(const char *beg, char *out,
29 end = strchr(beg, '"');
32 if (end - beg > out_len)
35 memcpy(out, beg, end - beg);
43 * parse_number() - parses numbers
45 * @out - output buffer where parsed number should be put
46 * @out_len - length of the output buffer
47 * @filled - pointer where number of bytes successfully
50 * Supports decimals in the range [INT_MIN, INT_MAX] and
51 * hexidecimals of any size, which should be started with
54 * Returns the end pointer where parsing has been stopped.
55 * In case of parsing error or lack of bytes in output buffer
56 * NULL will be returned.
58 static const char *parse_number(const char *beg, char *out,
71 sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num);
73 /* Here we are trying to parse decimal */
78 _end = strcasestr(beg, "0x");
82 lval = strntol(beg, num, &_end, 10);
84 lval = strtol(beg, &_end, 10);
85 if (beg == _end || lval > INT_MAX || lval < INT_MIN)
94 val = (unsigned int)lval;
95 for (; val && out_len; out_len--, i++, val >>= 8)
103 /* Catch up 0x prefix */
107 /* Look back, handle this combined string: 0xff0x14 */
108 if (beg[num] && !strncasecmp(&beg[num - 1], "0x", 2))
113 for (i = 0; num && out_len;
114 out_len--, i++, num -= 2, beg += 2) {
117 fmt = (num & 1 ? "%1hhx" : "%2hhx");
118 sscanf(beg, fmt, &out[i]);
134 * parse_format() - parses formats, like %o, etc
136 * @out - output buffer where space for format should be reserved
137 * @parsed - number of bytes which were already parsed so far
138 * @out_len - length of the output buffer
139 * @fmt_desc - format descritor array, what we expect to find
140 * @fmt_desc_sz - size of the format descritor array
141 * @fmt - format array, the output
142 * @fmt_sz - size of format array
144 * This function tries to find formats, e.g.:
145 * %o - offset of the block
147 * In case of successfull parsing it fills the format param
148 * with proper offset and the size of the expected value, which
149 * should be pasted into buffer using the format 'func' callback.
151 * Returns the end pointer where parsing has been stopped.
152 * In case of parsing error or lack of bytes in output buffer
153 * NULL will be returned.
155 static const char *parse_format(const char *in, char *out, unsigned int parsed,
156 unsigned int out_len, unsigned int *filled,
157 const struct pattern_fmt_desc *fmt_desc,
158 unsigned int fmt_desc_sz,
159 struct pattern_fmt *fmt, unsigned int fmt_sz)
162 struct pattern_fmt *f = NULL;
163 unsigned int len = 0;
165 if (!out_len || !fmt_desc || !fmt_desc_sz || !fmt || !fmt_sz)
170 for (i = 0; i < fmt_desc_sz; i++) {
171 const struct pattern_fmt_desc *desc;
174 len = strlen(desc->fmt);
175 if (0 == strncmp(in, desc->fmt, len)) {
185 if (f->desc->len > out_len)
188 memset(out, '\0', f->desc->len);
189 *filled = f->desc->len;
195 * parse_and_fill_pattern() - Parses combined input, which consists of strings,
196 * numbers and pattern formats.
198 * @in_len - size of the input string
199 * @out - output buffer where parsed result will be put
200 * @out_len - lengths of the output buffer
201 * @fmt_desc - array of pattern format descriptors [input]
202 * @fmt_desc_sz - size of the format descriptor array
203 * @fmt - array of pattern formats [output]
204 * @fmt_sz - pointer where the size of pattern formats array stored [input],
205 * after successfull parsing this pointer will contain the number
206 * of parsed formats if any [output].
209 * bytes sequence in double quotes, e.g. "123".
210 * NOTE: there is no way to escape quote, so "123\"abc" does not work.
213 * hexidecimal - sequence of hex bytes starting from 0x or 0X prefix,
214 * e.g. 0xff12ceff1100ff
215 * decimal - decimal number in range [INT_MIN, INT_MAX]
218 * %o - offset of block, reserved 8 bytes.
220 * Explicit examples of combined string:
222 * in="abcd" in=-1024 in=66 in=0xFF0X1
223 * out=61 62 63 64 out=00 fc ff ff out=42 out=ff 01
226 * in=%o in="123"0xFFeeCC
227 * out=00 00 00 00 00 00 00 00 out=31 32 33 ff ec cc
231 * out=f6 ff ff ff ab 31 00 00 00 00 00 00 00 00 32
235 * out=00 00 00 00 00 00 00 00 de ad be ef 00 00 00 00 00 00 00 00
238 * in=0xfefefefefefefefefefefefefefefefefefefefefe
239 * out=fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
241 * Returns number of bytes filled or err < 0 in case of failure.
243 int parse_and_fill_pattern(const char *in, unsigned int in_len,
244 char *out, unsigned int out_len,
245 const struct pattern_fmt_desc *fmt_desc,
246 unsigned int fmt_desc_sz,
247 struct pattern_fmt *fmt,
248 unsigned int *fmt_sz_out)
250 const char *beg, *end, *out_beg = out;
251 unsigned int total = 0, fmt_rem = 0;
253 if (!in || !in_len || !out || !out_len)
256 fmt_rem = *fmt_sz_out;
268 end = parse_string(beg, out, out_len, &filled);
271 end = parse_format(beg, out, out - out_beg, out_len,
272 &filled, fmt_desc, fmt_desc_sz,
277 end = parse_number(beg, out, out_len, &filled);
290 assert(end - beg <= in_len);
295 assert(filled <= out_len);
303 *fmt_sz_out -= fmt_rem;
308 * dup_pattern() - Duplicates part of the pattern all over the buffer.
310 * Returns 0 in case of success or errno < 0 in case of failure.
312 static int dup_pattern(char *out, unsigned int out_len, unsigned int pattern_len)
314 unsigned int left, len, off;
316 if (out_len <= pattern_len)
321 left = (out_len - off);
322 len = min(left, off);
324 /* Duplicate leftover */
326 memcpy(out + off, out, len);
329 len = min(left, off);
336 * cpy_pattern() - Copies pattern to the buffer.
338 * Function copies pattern along the whole buffer.
340 * Returns 0 in case of success or errno < 0 in case of failure.
342 int cpy_pattern(const char *pattern, unsigned int pattern_len,
343 char *out, unsigned int out_len)
347 if (!pattern || !pattern_len || !out || !out_len)
351 len = min(pattern_len, out_len);
352 memcpy(out, pattern, len);
354 /* Spread filled chunk all over the buffer */
355 return dup_pattern(out, out_len, pattern_len);
359 * cmp_pattern() - Compares pattern and buffer.
361 * For the sake of performance this function avoids any loops.
362 * Firstly it tries to compare the buffer itself, checking that
363 * buffer consists of repeating patterns along the buffer size.
365 * If the difference is not found then the function tries to compare
366 * buffer and pattern.
368 * Returns 0 in case of success or errno < 0 in case of failure.
370 int cmp_pattern(const char *pattern, unsigned int pattern_size,
371 unsigned int off, const char *buf, unsigned int len)
376 /* Find the difference in buffer */
377 if (len > pattern_size) {
378 rc = memcmp(buf, buf + pattern_size, len - pattern_size);
382 /* Compare second part of the pattern with buffer */
384 size = min(len, pattern_size - off);
385 rc = memcmp(buf, pattern + off, size);
391 /* Compare first part of the pattern or the whole pattern
394 size = min(len, (off ? off : pattern_size));
395 rc = memcmp(buf, pattern, size);
404 * paste_format_inplace() - Pastes parsed formats to the pattern.
406 * This function pastes formats to the pattern. If @fmt_sz is 0
407 * function does nothing and pattern buffer is left untouched.
409 * Returns 0 in case of success or errno < 0 in case of failure.
411 int paste_format_inplace(char *pattern, unsigned int pattern_len,
412 struct pattern_fmt *fmt, unsigned int fmt_sz,
418 if (!pattern || !pattern_len || !fmt)
421 /* Paste formats for first pattern chunk */
422 for (i = 0; i < fmt_sz; i++) {
423 struct pattern_fmt *f;
426 if (pattern_len <= f->off)
428 len = min(pattern_len - f->off, f->desc->len);
429 rc = f->desc->paste(pattern + f->off, len, priv);
438 * paste_format() - Pastes parsed formats to the buffer.
440 * This function copies pattern to the buffer, pastes format
441 * into it and then duplicates pattern all over the buffer size.
443 * Returns 0 in case of success or errno < 0 in case of failure.
445 int paste_format(const char *pattern, unsigned int pattern_len,
446 struct pattern_fmt *fmt, unsigned int fmt_sz,
447 char *out, unsigned int out_len, void *priv)
452 if (!pattern || !pattern_len || !out || !out_len)
456 len = min(pattern_len, out_len);
457 memcpy(out, pattern, len);
459 rc = paste_format_inplace(out, len, fmt, fmt_sz, priv);
463 /* Spread filled chunk all over the buffer */
464 return dup_pattern(out, out_len, pattern_len);