/src/gpsd/gpsd-3.28.1~dev/libgps/json.c
Line | Count | Source |
1 | | /**************************************************************************** |
2 | | * |
3 | | * NAME |
4 | | * json.c - parse JSON into fixed-extent data structures |
5 | | * |
6 | | * DESCRIPTION |
7 | | * This module parses a large subset of JSON (JavaScript Object |
8 | | * Notation). Unlike more general JSON parsers, it doesn't use malloc(3) |
9 | | * and doesn't support polymorphism; you need to give it a set of |
10 | | * template structures describing the expected shape of the incoming |
11 | | * JSON, and it will error out if that shape is not matched. When the |
12 | | * parse succeeds, attribute values will be extracted into static |
13 | | * locations specified in the template structures. |
14 | | * |
15 | | * The "shape" of a JSON object in the type signature of its |
16 | | * attributes (and attribute values, and so on recursively down through |
17 | | * all nestings of objects and arrays). This parser is indifferent to |
18 | | * the order of attributes at any level, but you have to tell it in |
19 | | * advance what the type of each attribute value will be and where the |
20 | | * parsed value will be stored. The template structures may supply |
21 | | * default values to be used when an expected attribute is omitted. |
22 | | * |
23 | | * The preceding paragraph told one fib. A single attribute may |
24 | | * actually have a span of multiple specifications with different |
25 | | * syntactically distinguishable types (e.g. string vs. real vs. integer |
26 | | * vs. boolean, but not signed integer vs. unsigned integer). The parser |
27 | | * will match the right spec against the actual data. |
28 | | * |
29 | | * The dialect this parses has some limitations. First, it cannot |
30 | | * recognize the JSON "null" value. Secondly, arrays may not have |
31 | | * character values as elements (this limitation could be easily removed |
32 | | * if required). Third, all elements of an array must be of the same |
33 | | * type. Fourth, it can not handle NaN's in doubles (Issue 53150). |
34 | | * |
35 | | * There are separate entry points for beginning a parse of either |
36 | | * JSON object or a JSON array. JSON "float" quantities are actually |
37 | | * stored as doubles. |
38 | | * |
39 | | * This parser processes object arrays in one of two different ways, |
40 | | * defending on whether the array subtype is declared as object or |
41 | | * structobject. |
42 | | * |
43 | | * Object arrays take one base address per object subfield, and are |
44 | | * mapped into parallel C arrays (one per subfield). Strings are not |
45 | | * supported in this kind of array, as they don't have a "natural" size |
46 | | * to use as an offset multiplier. |
47 | | * |
48 | | * Structobjects arrays are a way to parse a list of objects to a set |
49 | | * of modifications to a corresponding array of C structs. The trick is |
50 | | * that the array object initialization has to specify both the C struct |
51 | | * array's base address and the stride length (the size of the C struct). |
52 | | * If you initialize the offset fields with the correct offsetof calls, |
53 | | * everything will work. Strings are supported but all string storage |
54 | | * has to be inline in the struct. |
55 | | * |
56 | | * NOTE |
57 | | * This code has been spun out, packaged, and documented as a |
58 | | * reusable module; search for "microjson". |
59 | | * |
60 | | * PERMISSIONS |
61 | | * This file is Copyright by the GPSD project |
62 | | * SPDX-License-Identifier: BSD-2-clause |
63 | | * |
64 | | ***************************************************************************/ |
65 | | #include "../include/gpsd_config.h" // must be before all includes |
66 | | |
67 | | #include <math.h> // for HUGE_VAL |
68 | | #include <stdarg.h> // for va_arg(), etc. |
69 | | #include <stdbool.h> |
70 | | #include <stdio.h> |
71 | | #include <stdlib.h> |
72 | | #include <string.h> |
73 | | |
74 | | #include "../include/compiler.h" // for FALLTHROUGH |
75 | | #include "../include/os_compat.h" |
76 | | #include "../include/json.h" |
77 | | |
78 | | #include "../include/gps.h" // for safe_atof() prototype |
79 | | #include "../include/strfuncs.h" |
80 | | #include "../include/timespec.h" |
81 | | |
82 | | static int debuglevel = 0; |
83 | | static FILE *debugjsfp = NULL; |
84 | | |
85 | | // control the level and destination of debug trace messages |
86 | | void json_enable_debug(int level, FILE *fp) |
87 | 0 | { |
88 | 0 | debuglevel = level; |
89 | 0 | debugjsfp = fp; |
90 | 0 | } |
91 | | |
92 | | static void json_trace(const char *, ...); |
93 | | |
94 | | // assemble command in printf(3) style |
95 | | static void json_trace(const char *fmt, ...) |
96 | 0 | { |
97 | 0 | va_list ap; |
98 | |
|
99 | 0 | va_start(ap, fmt); |
100 | 0 | (void)vfprintf(debugjsfp, fmt, ap); |
101 | 0 | va_end(ap); |
102 | 0 | } |
103 | | |
104 | | // HOTCODE! Do not change without profiling. |
105 | | // major speedup by checking debuglvl before callin json_trace() |
106 | | #define json_debug_trace(lvl, fmt, ...) \ |
107 | 0 | do { \ |
108 | 0 | if (unlikely((lvl) <= debuglevel && \ |
109 | 0 | NULL != debugjsfp) ) { \ |
110 | 0 | json_trace(fmt, __VA_ARGS__); \ |
111 | 0 | } \ |
112 | 0 | } while (0) |
113 | | |
114 | | |
115 | | static char *json_target_address(const struct json_attr_t *cursor, |
116 | | const struct json_array_t |
117 | | *parent, int offset) |
118 | 0 | { |
119 | 0 | char *targetaddr = NULL; |
120 | 0 | if (NULL == parent || |
121 | 0 | parent->element_type != t_structobject) { |
122 | | // ordinary case - use the address in the cursor structure |
123 | 0 | switch (cursor->type) { |
124 | 0 | case t_byte: |
125 | 0 | targetaddr = (char *)&cursor->addr.byte[offset]; |
126 | 0 | break; |
127 | 0 | case t_ubyte: |
128 | 0 | targetaddr = (char *)&cursor->addr.ubyte[offset]; |
129 | 0 | break; |
130 | 0 | case t_ignore: |
131 | 0 | targetaddr = NULL; |
132 | 0 | break; |
133 | 0 | case t_integer: |
134 | 0 | targetaddr = (char *)&cursor->addr.integer[offset]; |
135 | 0 | break; |
136 | 0 | case t_uinteger: |
137 | 0 | targetaddr = (char *)&cursor->addr.uinteger[offset]; |
138 | 0 | break; |
139 | 0 | case t_longint: |
140 | 0 | targetaddr = (char *)&cursor->addr.longint[offset]; |
141 | 0 | break; |
142 | 0 | case t_ulongint: |
143 | 0 | targetaddr = (char *)&cursor->addr.ulongint[offset]; |
144 | 0 | break; |
145 | 0 | case t_short: |
146 | 0 | targetaddr = (char *)&cursor->addr.shortint[offset]; |
147 | 0 | break; |
148 | 0 | case t_ushort: |
149 | 0 | targetaddr = (char *)&cursor->addr.ushortint[offset]; |
150 | 0 | break; |
151 | 0 | case t_time: |
152 | 0 | targetaddr = (char *)&cursor->addr.ts[offset]; |
153 | 0 | break; |
154 | 0 | case t_timespec: |
155 | 0 | targetaddr = (char *)&cursor->addr.ts[offset]; |
156 | 0 | break; |
157 | 0 | case t_real: |
158 | 0 | targetaddr = (char *)&cursor->addr.real[offset]; |
159 | 0 | break; |
160 | 0 | case t_string: |
161 | 0 | targetaddr = cursor->addr.string; |
162 | 0 | break; |
163 | 0 | case t_boolean: |
164 | 0 | targetaddr = (char *)&cursor->addr.boolean[offset]; |
165 | 0 | break; |
166 | 0 | case t_character: |
167 | 0 | targetaddr = (char *)&cursor->addr.character[offset]; |
168 | 0 | break; |
169 | 0 | case t_array: |
170 | 0 | FALLTHROUGH |
171 | 0 | case t_check: |
172 | 0 | FALLTHROUGH |
173 | 0 | case t_object: |
174 | 0 | FALLTHROUGH |
175 | 0 | case t_structobject: |
176 | 0 | FALLTHROUGH |
177 | 0 | default: |
178 | 0 | targetaddr = NULL; |
179 | 0 | break; |
180 | 0 | } |
181 | 0 | } else { |
182 | | // tricky case - hacking a member in an array of structures |
183 | 0 | targetaddr = |
184 | 0 | parent->arr.objects.base + (offset * parent->arr.objects.stride) + |
185 | 0 | cursor->addr.offset; |
186 | 0 | } |
187 | 0 | json_debug_trace(1, "json: Target address for %s (offset %d) is %p\n", |
188 | 0 | cursor->attribute, offset, targetaddr); |
189 | 0 | return targetaddr; |
190 | 0 | } |
191 | | |
192 | | |
193 | | static int json_internal_read_object(const char *cp, |
194 | | const struct json_attr_t *attrs, |
195 | | const struct json_array_t *parent, |
196 | | int offset, |
197 | | const char **end) |
198 | 0 | { |
199 | 0 | enum { |
200 | 0 | init, await_attr, in_attr, await_value, in_val_string, |
201 | 0 | in_escape, in_val_token, post_val, post_element, in_ignore_array |
202 | 0 | } state = 0; |
203 | 0 | char *statenames[] = { |
204 | 0 | "init", "await_attr", "in_attr", "await_value", "in_val_string", |
205 | 0 | "in_escape", "in_val_token", "post_val", "post_element", |
206 | 0 | "in_ignore_array" |
207 | 0 | }; |
208 | 0 | char attrbuf[JSON_ATTR_MAX + 1], *pattr = NULL; |
209 | 0 | char valbuf[JSON_VAL_MAX + 1], *pval = NULL; |
210 | 0 | bool value_quoted = false; |
211 | 0 | char uescape[5]; // enough space for 4 hex digits and a NUL |
212 | 0 | const struct json_attr_t *cursor; |
213 | 0 | int substatus, maxlen = 0; |
214 | 0 | unsigned int u; |
215 | 0 | const struct json_enum_t *mp; |
216 | 0 | char *lptr; |
217 | |
|
218 | 0 | if (NULL != end) { |
219 | 0 | *end = NULL; // give it a well-defined value on parse failure |
220 | 0 | } |
221 | 0 | memset(valbuf, 0, sizeof(valbuf)); |
222 | | |
223 | | // stuff fields with defaults in case they're omitted in the JSON input |
224 | 0 | for (cursor = attrs; cursor->attribute != NULL; cursor++) { |
225 | 0 | if (!cursor->nodefault) { |
226 | 0 | lptr = json_target_address(cursor, parent, offset); |
227 | 0 | if (NULL != lptr) { |
228 | 0 | switch (cursor->type) { |
229 | 0 | case t_byte: |
230 | 0 | lptr[0] = cursor->dflt.byte; |
231 | 0 | break; |
232 | 0 | case t_ubyte: |
233 | 0 | lptr[0] = cursor->dflt.ubyte; |
234 | 0 | break; |
235 | 0 | case t_integer: |
236 | 0 | memcpy(lptr, &cursor->dflt.integer, sizeof(int)); |
237 | 0 | break; |
238 | 0 | case t_uinteger: |
239 | 0 | memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int)); |
240 | 0 | break; |
241 | 0 | case t_longint: |
242 | 0 | memcpy(lptr, &cursor->dflt.longint, sizeof(long)); |
243 | 0 | break; |
244 | 0 | case t_ulongint: |
245 | 0 | memcpy(lptr, &cursor->dflt.ulongint, |
246 | 0 | sizeof(unsigned long)); |
247 | 0 | break; |
248 | 0 | case t_short: |
249 | 0 | memcpy(lptr, &cursor->dflt.shortint, sizeof(short)); |
250 | 0 | break; |
251 | 0 | case t_ushort: |
252 | 0 | memcpy(lptr, &cursor->dflt.ushortint, |
253 | 0 | sizeof(unsigned short)); |
254 | 0 | break; |
255 | 0 | case t_time: |
256 | 0 | memcpy(lptr, &cursor->dflt.ts, sizeof(timespec_t)); |
257 | 0 | break; |
258 | 0 | case t_timespec: |
259 | 0 | memcpy(lptr, &cursor->dflt.ts, sizeof(timespec_t)); |
260 | 0 | break; |
261 | 0 | case t_real: |
262 | 0 | memcpy(lptr, &cursor->dflt.real, sizeof(double)); |
263 | 0 | break; |
264 | 0 | case t_string: |
265 | 0 | if (parent != NULL |
266 | 0 | && parent->element_type != t_structobject |
267 | 0 | && offset > 0) { |
268 | 0 | return JSON_ERR_NOPARSTR; |
269 | 0 | } |
270 | 0 | lptr[0] = '\0'; |
271 | 0 | break; |
272 | 0 | case t_boolean: |
273 | 0 | memcpy(lptr, &cursor->dflt.boolean, sizeof(bool)); |
274 | 0 | break; |
275 | 0 | case t_character: |
276 | 0 | lptr[0] = cursor->dflt.character; |
277 | 0 | break; |
278 | 0 | case t_object: // silences a compiler warning |
279 | 0 | FALLTHROUGH |
280 | 0 | case t_structobject: |
281 | 0 | FALLTHROUGH |
282 | 0 | case t_array: |
283 | 0 | FALLTHROUGH |
284 | 0 | case t_check: |
285 | 0 | FALLTHROUGH |
286 | 0 | case t_ignore: |
287 | 0 | break; |
288 | 0 | } |
289 | 0 | } |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 0 | json_debug_trace(1, "json: JSON parse of '%s' begins.\n", cp); |
294 | | |
295 | | // parse input JSON |
296 | 0 | for (; *cp != '\0'; cp++) { |
297 | 0 | json_debug_trace(2, "json: State %-14s, looking at '%c' (%p)\n", |
298 | 0 | statenames[state], *cp, cp); |
299 | 0 | switch (state) { |
300 | 0 | case init: |
301 | 0 | if (ISSPACE(*cp)) { |
302 | 0 | continue; |
303 | 0 | } |
304 | 0 | if (*cp == '{') { |
305 | 0 | state = await_attr; |
306 | 0 | } else { |
307 | 0 | json_debug_trace(1, "json: %s", |
308 | 0 | "Non-WS when expecting object start.\n"); |
309 | 0 | if (NULL != end) { |
310 | 0 | *end = cp; |
311 | 0 | } |
312 | 0 | return JSON_ERR_OBSTART; |
313 | 0 | } |
314 | 0 | break; |
315 | 0 | case await_attr: |
316 | 0 | if (ISSPACE(*cp)) { |
317 | 0 | continue; |
318 | 0 | } |
319 | 0 | if (*cp == '"') { |
320 | 0 | state = in_attr; |
321 | 0 | pattr = attrbuf; |
322 | 0 | if (NULL != end) { |
323 | 0 | *end = cp; |
324 | 0 | } |
325 | 0 | } else if (*cp == '}') { |
326 | 0 | break; |
327 | 0 | } else { |
328 | 0 | json_debug_trace(1, "json: %s", |
329 | 0 | "Non-WS when expecting attribute.\n"); |
330 | 0 | if (NULL != end) { |
331 | 0 | *end = cp; |
332 | 0 | } |
333 | 0 | return JSON_ERR_ATTRSTART; |
334 | 0 | } |
335 | 0 | break; |
336 | 0 | case in_attr: |
337 | 0 | if (NULL == pattr) { |
338 | | // don't update end here, leave at attribute start |
339 | 0 | return JSON_ERR_NULLPTR; |
340 | 0 | } |
341 | 0 | if (*cp == '"') { |
342 | 0 | *pattr++ = '\0'; |
343 | 0 | json_debug_trace(1, "json: Collected attribute name %s\n", |
344 | 0 | attrbuf); |
345 | 0 | for (cursor = attrs; cursor->attribute != NULL; cursor++) { |
346 | 0 | json_debug_trace(2, "json: Checking against %s\n", |
347 | 0 | cursor->attribute); |
348 | 0 | if (strcmp(cursor->attribute, attrbuf) == 0) { |
349 | 0 | break; |
350 | 0 | } |
351 | 0 | if (cursor->type == t_ignore && |
352 | 0 | strncmp(cursor->attribute, "", 1) == 0) { |
353 | 0 | break; |
354 | 0 | } |
355 | 0 | } |
356 | 0 | if (NULL == cursor->attribute) { |
357 | 0 | json_debug_trace(1, |
358 | 0 | "json: Unknown attribute name '%s'" |
359 | 0 | " (attributes begin with '%s').\n", |
360 | 0 | attrbuf, attrs->attribute); |
361 | | // don't update end here, leave at attribute start |
362 | 0 | return JSON_ERR_BADATTR; |
363 | 0 | } |
364 | 0 | state = await_value; |
365 | 0 | if (cursor->type == t_string) { |
366 | 0 | maxlen = (int)cursor->len - 1; |
367 | 0 | } else if (cursor->type == t_check) { |
368 | 0 | maxlen = (int)strnlen(cursor->dflt.check, JSON_VAL_MAX); |
369 | 0 | } else if (cursor->type == t_time || |
370 | 0 | cursor->type == t_ignore) { |
371 | 0 | maxlen = JSON_VAL_MAX; |
372 | 0 | } else if (NULL != cursor->map) { |
373 | 0 | maxlen = (int)sizeof(valbuf) - 1; |
374 | 0 | } |
375 | 0 | pval = valbuf; |
376 | 0 | } else if (pattr >= attrbuf + JSON_ATTR_MAX - 1) { |
377 | 0 | json_debug_trace(1, "json: %s","Attribute name too long.\n"); |
378 | | // don't update end here, leave at attribute start |
379 | 0 | return JSON_ERR_ATTRLEN; |
380 | 0 | } else { |
381 | 0 | *pattr++ = *cp; |
382 | 0 | } |
383 | 0 | break; |
384 | 0 | case await_value: |
385 | 0 | if (ISSPACE(*cp) || |
386 | 0 | *cp == ':') { |
387 | 0 | continue; |
388 | 0 | } |
389 | 0 | if (*cp == '[') { |
390 | 0 | if (cursor->type == t_ignore) { |
391 | | /* skip to terminating ], not being fooled by |
392 | | * sub arrays, etc. |
393 | | * FIXME: someday */ |
394 | 0 | state = in_ignore_array; |
395 | 0 | ; |
396 | 0 | value_quoted = false; |
397 | 0 | break; |
398 | 0 | } |
399 | 0 | if (cursor->type != t_array) { |
400 | 0 | json_debug_trace(1,"json: %s", |
401 | 0 | "Saw [ when not expecting array.\n"); |
402 | 0 | if (NULL != end) { |
403 | 0 | *end = cp; |
404 | 0 | } |
405 | 0 | return JSON_ERR_NOARRAY; |
406 | 0 | } |
407 | 0 | substatus = json_read_array(cp, &cursor->addr.array, &cp); |
408 | 0 | if (substatus != 0) { |
409 | 0 | return substatus; |
410 | 0 | } |
411 | 0 | state = post_element; |
412 | 0 | } else if (cursor->type == t_array) { |
413 | 0 | json_debug_trace(1, "json: %s", |
414 | 0 | "Array element was specified, but no [.\n"); |
415 | 0 | if (NULL != end) { |
416 | 0 | *end = cp; |
417 | 0 | } |
418 | 0 | return JSON_ERR_NOBRAK; |
419 | 0 | } else if (*cp == '"') { |
420 | 0 | value_quoted = true; |
421 | 0 | state = in_val_string; |
422 | 0 | pval = valbuf; |
423 | 0 | } else { |
424 | 0 | value_quoted = false; |
425 | 0 | state = in_val_token; |
426 | 0 | pval = valbuf; |
427 | 0 | *pval++ = *cp; |
428 | 0 | } |
429 | 0 | break; |
430 | 0 | case in_ignore_array: |
431 | 0 | if (*cp == '"') { |
432 | 0 | if (value_quoted == false) { |
433 | | // now in quotes |
434 | 0 | value_quoted = true; |
435 | 0 | } else { |
436 | 0 | value_quoted = false; |
437 | 0 | } |
438 | 0 | } else if (*cp == ']') { |
439 | | // FIXME: does not handle sub arrays, etc. |
440 | 0 | if (value_quoted == false) { |
441 | 0 | state = post_val; |
442 | 0 | } |
443 | | // else in quotes, ignore it. |
444 | 0 | } |
445 | 0 | break; |
446 | 0 | case in_val_string: |
447 | 0 | if (NULL == pval) { |
448 | | // don't update end here, leave at value start |
449 | 0 | return JSON_ERR_NULLPTR; |
450 | 0 | } |
451 | 0 | if (*cp == '\\') { |
452 | 0 | state = in_escape; |
453 | 0 | } else if (*cp == '"') { |
454 | 0 | *pval++ = '\0'; |
455 | 0 | json_debug_trace(1, "json: Collected string value %s\n", valbuf); |
456 | 0 | state = post_val; |
457 | 0 | } else if (pval > valbuf + JSON_VAL_MAX - 1 || |
458 | 0 | pval > valbuf + maxlen - 1) { |
459 | 0 | json_debug_trace(1, "json: %s", "String value too long.\n"); |
460 | | // don't update end here, leave at value start |
461 | 0 | return JSON_ERR_STRLONG; |
462 | 0 | } else { |
463 | 0 | *pval++ = *cp; |
464 | 0 | } |
465 | 0 | break; |
466 | 0 | case in_escape: |
467 | 0 | if (NULL == pval) { |
468 | | // don't update end here, leave at value start |
469 | 0 | return JSON_ERR_NULLPTR; |
470 | 0 | } |
471 | 0 | if (pval > valbuf + JSON_VAL_MAX - 1 || |
472 | 0 | pval > valbuf + maxlen) { |
473 | 0 | json_debug_trace(1, "json: %s", "String value too long.\n"); |
474 | | // don't update end here, leave at value start |
475 | 0 | return JSON_ERR_STRLONG; |
476 | 0 | } |
477 | 0 | switch (*cp) { |
478 | 0 | case 'b': |
479 | 0 | *pval++ = '\b'; |
480 | 0 | break; |
481 | 0 | case 'f': |
482 | 0 | *pval++ = '\f'; |
483 | 0 | break; |
484 | 0 | case 'n': |
485 | 0 | *pval++ = '\n'; |
486 | 0 | break; |
487 | 0 | case 'r': |
488 | 0 | *pval++ = '\r'; |
489 | 0 | break; |
490 | 0 | case 't': |
491 | 0 | *pval++ = '\t'; |
492 | 0 | break; |
493 | 0 | case 'u': { |
494 | 0 | unsigned n; |
495 | |
|
496 | 0 | cp++; // skip the 'u' |
497 | | // NetBSD 6 wants the cast |
498 | 0 | for (n = 0; n < 4 && ISXDIGIT(*cp); n++) { |
499 | 0 | uescape[n] = *cp++; |
500 | 0 | } |
501 | 0 | uescape[n] = '\0'; // terminate |
502 | 0 | --cp; |
503 | | // ECMA-404 says JSON \u must have 4 hex digits |
504 | 0 | if ((4 != n) || |
505 | 0 | (1 != sscanf(uescape, "%4x", &u))) { |
506 | 0 | return JSON_ERR_BADSTRING; |
507 | 0 | } |
508 | | // truncate values above 0xff |
509 | 0 | *pval++ = (unsigned char)u; |
510 | 0 | } |
511 | 0 | break; |
512 | 0 | default: // handles double quote and solidus |
513 | 0 | *pval++ = *cp; |
514 | 0 | break; |
515 | 0 | } |
516 | 0 | state = in_val_string; |
517 | 0 | break; |
518 | 0 | case in_val_token: |
519 | 0 | if (NULL == pval) { |
520 | | // don't update end here, leave at value start |
521 | 0 | return JSON_ERR_NULLPTR; |
522 | 0 | } |
523 | 0 | if (ISSPACE(*cp) || |
524 | 0 | *cp == ',' || |
525 | 0 | *cp == '}') { |
526 | 0 | *pval = '\0'; |
527 | 0 | json_debug_trace(1, "json: Collected token valuen %s\n", |
528 | 0 | valbuf); |
529 | 0 | state = post_val; |
530 | 0 | if (*cp == '}' || |
531 | 0 | *cp == ',') { |
532 | 0 | --cp; |
533 | 0 | } |
534 | 0 | } else if (pval > valbuf + JSON_VAL_MAX - 1) { |
535 | 0 | json_debug_trace(1, "json: %s", "Token value too long.\n"); |
536 | | // don't update end here, leave at value start |
537 | 0 | return JSON_ERR_TOKLONG; |
538 | 0 | } else { |
539 | 0 | *pval++ = *cp; |
540 | 0 | } |
541 | 0 | break; |
542 | | // coverity[unterminated_case] |
543 | 0 | case post_val: |
544 | | // Ignore whitespace after either string or token values. |
545 | 0 | if (ISSPACE(*cp)) { |
546 | 0 | while (*cp != '\0' && ISSPACE(*cp)) { |
547 | 0 | ++cp; |
548 | 0 | } |
549 | 0 | json_debug_trace(1, |
550 | 0 | "json: Skipped trailing whitespace: value \"%s\"\n", valbuf); |
551 | 0 | } |
552 | | /* |
553 | | * We know that cursor points at the first spec matching |
554 | | * the current attribute. We don't know that it's *the* |
555 | | * correct spec; our dialect allows there to be any number |
556 | | * of adjacent ones with the same attrname but different |
557 | | * types. Here's where we try to seek forward for a |
558 | | * matching type/attr pair if we're not looking at one. |
559 | | */ |
560 | 0 | for (;;) { |
561 | 0 | int seeking = cursor->type; |
562 | |
|
563 | 0 | if (value_quoted && |
564 | 0 | (cursor->type == t_string || |
565 | 0 | cursor->type == t_time)) { |
566 | 0 | break; |
567 | 0 | } |
568 | 0 | if ((strcmp(valbuf, "true") == 0 || |
569 | 0 | strcmp(valbuf, "false") == 0) && |
570 | 0 | seeking == t_boolean) { |
571 | 0 | break; |
572 | 0 | } |
573 | 0 | if (ISDIGIT(valbuf[0])) { |
574 | 0 | bool decimal = strchr(valbuf, '.') != NULL; |
575 | |
|
576 | 0 | if (decimal && |
577 | 0 | seeking == t_real) { |
578 | 0 | break; |
579 | 0 | } |
580 | 0 | if (!decimal && (seeking == t_byte || |
581 | 0 | seeking == t_ubyte || |
582 | 0 | seeking == t_integer || |
583 | 0 | seeking == t_uinteger || |
584 | 0 | seeking == t_longint || |
585 | 0 | seeking == t_ulongint || |
586 | 0 | seeking == t_time || |
587 | 0 | seeking == t_short || |
588 | 0 | seeking == t_ushort)) { |
589 | 0 | break; |
590 | 0 | } |
591 | 0 | } |
592 | 0 | if (NULL == cursor[1].attribute) { // out of possibilities |
593 | 0 | break; |
594 | 0 | } |
595 | 0 | if (0 != strcmp(cursor[1].attribute, attrbuf)) { |
596 | 0 | break; |
597 | 0 | } |
598 | 0 | ++cursor; |
599 | 0 | } |
600 | 0 | if (value_quoted && |
601 | 0 | (cursor->type != t_string && |
602 | 0 | cursor->type != t_character && |
603 | 0 | cursor->type != t_check && |
604 | 0 | cursor->type != t_time && |
605 | 0 | cursor->type != t_ignore && |
606 | 0 | cursor->map == 0)) { |
607 | 0 | json_debug_trace(1, "json: %s", "Saw quoted value when expecting" |
608 | 0 | " non-string.\n"); |
609 | 0 | return JSON_ERR_QNONSTRING; |
610 | 0 | } |
611 | 0 | if (!value_quoted && |
612 | 0 | (cursor->type == t_string || |
613 | 0 | cursor->type == t_check || |
614 | 0 | cursor->map != 0)) { |
615 | 0 | json_debug_trace(1, "json: %s", |
616 | 0 | "Didn't see quoted value when expecting" |
617 | 0 | " string.\n"); |
618 | 0 | return JSON_ERR_NONQSTRING; |
619 | 0 | } |
620 | 0 | if (cursor->map != 0) { |
621 | 0 | for (mp = cursor->map; mp->name != NULL; mp++) { |
622 | 0 | if (strcmp(mp->name, valbuf) == 0) { |
623 | 0 | goto foundit; |
624 | 0 | } |
625 | 0 | } |
626 | 0 | json_debug_trace(1, |
627 | 0 | "json: Invalid enumerated value string %s.\n", |
628 | 0 | valbuf); |
629 | 0 | return JSON_ERR_BADENUM; |
630 | 0 | foundit: |
631 | 0 | (void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value); |
632 | 0 | } |
633 | 0 | if (cursor->type == t_check) { |
634 | 0 | lptr = cursor->dflt.check; |
635 | 0 | } else { |
636 | 0 | lptr = json_target_address(cursor, parent, offset); |
637 | 0 | } |
638 | 0 | if (NULL != lptr) { |
639 | 0 | switch (cursor->type) { |
640 | 0 | case t_byte: { |
641 | 0 | int tmp = atoi(valbuf); |
642 | 0 | lptr[0] = (char)tmp; |
643 | 0 | } |
644 | 0 | break; |
645 | 0 | case t_ubyte: { |
646 | 0 | int tmp = atoi(valbuf); |
647 | 0 | lptr[0] = (unsigned char)tmp; |
648 | 0 | } |
649 | 0 | break; |
650 | 0 | case t_integer: { |
651 | 0 | int tmp = atoi(valbuf); |
652 | 0 | memcpy(lptr, &tmp, sizeof(int)); |
653 | 0 | } |
654 | 0 | break; |
655 | 0 | case t_uinteger: { |
656 | 0 | unsigned int tmp = (unsigned int)atol(valbuf); |
657 | 0 | memcpy(lptr, &tmp, sizeof(unsigned int)); |
658 | 0 | } |
659 | 0 | break; |
660 | 0 | case t_longint: { |
661 | 0 | long tmp = atol(valbuf); |
662 | 0 | memcpy(lptr, &tmp, sizeof(long)); |
663 | 0 | } |
664 | 0 | break; |
665 | 0 | case t_ulongint: { |
666 | 0 | unsigned long tmp = (unsigned long)atoll(valbuf); |
667 | 0 | memcpy(lptr, &tmp, sizeof(unsigned long)); |
668 | 0 | } |
669 | 0 | break; |
670 | 0 | case t_short: { |
671 | 0 | short tmp = atoi(valbuf); |
672 | 0 | memcpy(lptr, &tmp, sizeof(short)); |
673 | 0 | } |
674 | 0 | break; |
675 | 0 | case t_ushort: { |
676 | 0 | unsigned short tmp = (unsigned int)atoi(valbuf); |
677 | 0 | memcpy(lptr, &tmp, sizeof(unsigned short)); |
678 | 0 | } |
679 | 0 | break; |
680 | 0 | case t_time: { |
681 | 0 | timespec_t ts_tmp = {0, 0}; |
682 | |
|
683 | 0 | if (value_quoted) { |
684 | 0 | ts_tmp = iso8601_to_timespec(valbuf); |
685 | 0 | } else if (NULL == strchr(valbuf, '.')) { |
686 | | // integer |
687 | 0 | ts_tmp.tv_sec = (time_t)atoll(valbuf); |
688 | 0 | } else { |
689 | | // real |
690 | 0 | double sec_tmp = safe_atof(valbuf); |
691 | 0 | if (0 != isfinite(sec_tmp)) { |
692 | 0 | DTOTS(&ts_tmp, sec_tmp); |
693 | 0 | } // else, leave at default |
694 | 0 | } |
695 | 0 | memcpy(lptr, &ts_tmp, sizeof(timespec_t)); |
696 | 0 | } |
697 | 0 | break; |
698 | 0 | case t_timespec: { |
699 | 0 | double sec_tmp = safe_atof(valbuf); |
700 | 0 | timespec_t ts_tmp; |
701 | 0 | if (0 != isfinite(sec_tmp)) { |
702 | 0 | DTOTS(&ts_tmp, sec_tmp); |
703 | 0 | memcpy(lptr, &ts_tmp, sizeof(timespec_t)); |
704 | 0 | } // else leave at .dflt |
705 | 0 | } |
706 | 0 | break; |
707 | 0 | case t_real: { |
708 | 0 | double tmp = safe_atof(valbuf); |
709 | 0 | if (0 != isfinite(tmp)) { |
710 | 0 | memcpy(lptr, &tmp, sizeof(double)); |
711 | 0 | } // else leave at .dflt |
712 | 0 | } |
713 | 0 | break; |
714 | 0 | case t_string: |
715 | 0 | if (NULL != parent && |
716 | 0 | parent->element_type != t_structobject && |
717 | 0 | offset > 0) { |
718 | 0 | return JSON_ERR_NOPARSTR; |
719 | 0 | } |
720 | 0 | (void)strlcpy(lptr, valbuf, cursor->len); |
721 | 0 | break; |
722 | 0 | case t_boolean: { |
723 | 0 | bool tmp = (strcmp(valbuf, "true") == 0); |
724 | 0 | memcpy(lptr, &tmp, sizeof(bool)); |
725 | 0 | } |
726 | 0 | break; |
727 | 0 | case t_character: |
728 | 0 | if (strnlen(valbuf, 2) > 1) { |
729 | | // don't update end here, leave at value start |
730 | 0 | return JSON_ERR_STRLONG; |
731 | 0 | } else { |
732 | 0 | lptr[0] = valbuf[0]; |
733 | 0 | } |
734 | 0 | break; |
735 | 0 | case t_ignore: // silences a compiler warning |
736 | 0 | FALLTHROUGH |
737 | 0 | case t_object: // silences a compiler warning |
738 | 0 | FALLTHROUGH |
739 | 0 | case t_structobject: |
740 | 0 | FALLTHROUGH |
741 | 0 | case t_array: |
742 | 0 | break; |
743 | 0 | case t_check: |
744 | 0 | if (strcmp(cursor->dflt.check, valbuf) != 0) { |
745 | 0 | json_debug_trace(1, "json: Required attribute value %s" |
746 | 0 | " not present.\n", |
747 | 0 | cursor->dflt.check); |
748 | | // don't update end here, leave at start of attribute |
749 | 0 | return JSON_ERR_CHECKFAIL; |
750 | 0 | } |
751 | 0 | break; |
752 | 0 | } |
753 | 0 | } |
754 | 0 | FALLTHROUGH |
755 | 0 | case post_element: |
756 | 0 | if (ISSPACE(*cp)) { |
757 | 0 | continue; |
758 | 0 | } |
759 | 0 | if (*cp == ',') { |
760 | 0 | state = await_attr; |
761 | 0 | } else if (*cp == '}') { |
762 | 0 | ++cp; |
763 | 0 | goto good_parse; |
764 | 0 | } else { |
765 | 0 | json_debug_trace(1, "json: %s", |
766 | 0 | "Garbage while expecting comma or }\n"); |
767 | 0 | if (NULL != end) { |
768 | 0 | *end = cp; |
769 | 0 | } |
770 | 0 | return JSON_ERR_BADTRAIL; |
771 | 0 | } |
772 | 0 | break; |
773 | 0 | } |
774 | 0 | } |
775 | 0 | if (state == init) { |
776 | 0 | json_debug_trace(1, "json: %s", "Input was empty or white-space only\n"); |
777 | 0 | return JSON_ERR_EMPTY; |
778 | 0 | } |
779 | | |
780 | 0 | good_parse: |
781 | | // in case there's another object following, consume trailing WS |
782 | 0 | while (ISSPACE(*cp)) { |
783 | 0 | ++cp; |
784 | 0 | } |
785 | 0 | if (NULL != end) { |
786 | 0 | *end = cp; |
787 | 0 | } |
788 | 0 | json_debug_trace(1, "%s", "json: JSON parse ends.\n"); |
789 | 0 | return 0; |
790 | 0 | } |
791 | | |
792 | | int json_read_array(const char *cp, const struct json_array_t *arr, |
793 | | const char **end) |
794 | 0 | { |
795 | 0 | int substatus; |
796 | 0 | unsigned offset, arrcount; |
797 | 0 | char *tp; |
798 | |
|
799 | 0 | if (NULL != end) { |
800 | 0 | *end = NULL; // give it a well-defined value on parse failure |
801 | 0 | } |
802 | |
|
803 | 0 | json_debug_trace(1, "json: %s", "Entered json_read_array()\n"); |
804 | |
|
805 | 0 | while (ISSPACE(*cp)) { |
806 | 0 | cp++; |
807 | 0 | } |
808 | 0 | if (*cp != '[') { |
809 | 0 | json_debug_trace(1, "json: %s", "Didn't find expected array start\n"); |
810 | 0 | return JSON_ERR_ARRAYSTART; |
811 | 0 | } |
812 | 0 | cp++; |
813 | |
|
814 | 0 | tp = arr->arr.strings.store; |
815 | 0 | arrcount = 0; |
816 | | |
817 | | // Check for empty array |
818 | 0 | while (ISSPACE(*cp)) { |
819 | 0 | cp++; |
820 | 0 | } |
821 | 0 | if (*cp == ']') { |
822 | 0 | goto breakout; |
823 | 0 | } |
824 | | |
825 | 0 | for (offset = 0; offset < arr->maxlen; offset++) { |
826 | 0 | char *ep = NULL; |
827 | |
|
828 | 0 | json_debug_trace(1, "json: Looking at %s\n", cp); |
829 | 0 | switch (arr->element_type) { |
830 | 0 | case t_string: |
831 | 0 | if (ISSPACE(*cp)) { |
832 | 0 | cp++; |
833 | 0 | } |
834 | 0 | if (*cp != '"') { |
835 | 0 | return JSON_ERR_BADSTRING; |
836 | 0 | } else { |
837 | 0 | ++cp; |
838 | 0 | } |
839 | 0 | arr->arr.strings.ptrs[offset] = tp; |
840 | 0 | for (; tp - arr->arr.strings.store < arr->arr.strings.storelen; |
841 | 0 | tp++) { |
842 | 0 | if (*cp == '"') { |
843 | 0 | ++cp; |
844 | 0 | *tp++ = '\0'; |
845 | 0 | goto stringend; |
846 | 0 | } else if (*cp == '\0') { |
847 | 0 | json_debug_trace(1, "json: %s", |
848 | 0 | "Bad string syntax in string list.\n"); |
849 | 0 | return JSON_ERR_BADSTRING; |
850 | 0 | } else { |
851 | 0 | *tp = *cp++; |
852 | 0 | } |
853 | 0 | } |
854 | 0 | json_debug_trace(1, "json: %s", |
855 | 0 | "Bad string syntax in string list.\n"); |
856 | 0 | return JSON_ERR_BADSTRING; |
857 | 0 | stringend: |
858 | 0 | break; |
859 | 0 | case t_object: |
860 | 0 | FALLTHROUGH |
861 | 0 | case t_structobject: |
862 | 0 | substatus = |
863 | 0 | json_internal_read_object(cp, arr->arr.objects.subtype, arr, |
864 | 0 | offset, &cp); |
865 | 0 | if (substatus != 0) { |
866 | 0 | if (NULL != end) { |
867 | 0 | *end = cp; |
868 | 0 | } |
869 | 0 | return substatus; |
870 | 0 | } |
871 | 0 | break; |
872 | 0 | case t_integer: |
873 | 0 | arr->arr.integers.store[offset] = (int)strtol(cp, &ep, 0); |
874 | 0 | if (ep == cp) { |
875 | 0 | return JSON_ERR_BADNUM; |
876 | 0 | } |
877 | 0 | cp = ep; |
878 | 0 | break; |
879 | 0 | case t_uinteger: |
880 | 0 | arr->arr.uintegers.store[offset] = (unsigned int)strtoul(cp, |
881 | 0 | &ep, 0); |
882 | 0 | if (ep == cp) { |
883 | 0 | return JSON_ERR_BADNUM; |
884 | 0 | } |
885 | 0 | cp = ep; |
886 | 0 | break; |
887 | 0 | case t_longint: |
888 | 0 | arr->arr.longint.store[offset] = strtol(cp, &ep, 0); |
889 | 0 | if (ep == cp) { |
890 | 0 | return JSON_ERR_BADNUM; |
891 | 0 | } |
892 | 0 | cp = ep; |
893 | 0 | break; |
894 | 0 | case t_ulongint: |
895 | 0 | arr->arr.ulongint.store[offset] = strtoul(cp, &ep, 0); |
896 | 0 | if (ep == cp) { |
897 | 0 | return JSON_ERR_BADNUM; |
898 | 0 | } |
899 | 0 | cp = ep; |
900 | 0 | break; |
901 | 0 | case t_byte: |
902 | 0 | arr->arr.bytes.store[offset] = (char)strtol(cp, &ep, 0); |
903 | 0 | if (ep == cp) { |
904 | 0 | return JSON_ERR_BADNUM; |
905 | 0 | } |
906 | 0 | cp = ep; |
907 | 0 | break; |
908 | 0 | case t_ubyte: |
909 | 0 | arr->arr.ubytes.store[offset] = (unsigned char)strtoul(cp, |
910 | 0 | &ep, 0); |
911 | 0 | if (ep == cp) { |
912 | 0 | return JSON_ERR_BADNUM; |
913 | 0 | } |
914 | 0 | cp = ep; |
915 | 0 | break; |
916 | 0 | case t_short: |
917 | 0 | arr->arr.shorts.store[offset] = (short)strtol(cp, &ep, 0); |
918 | 0 | if (ep == cp) { |
919 | 0 | return JSON_ERR_BADNUM; |
920 | 0 | } |
921 | 0 | cp = ep; |
922 | 0 | break; |
923 | 0 | case t_ushort: |
924 | 0 | arr->arr.ushorts.store[offset] = (unsigned short)strtoul(cp, |
925 | 0 | &ep, 0); |
926 | 0 | if (ep == cp) { |
927 | 0 | return JSON_ERR_BADNUM; |
928 | 0 | } |
929 | 0 | cp = ep; |
930 | 0 | break; |
931 | 0 | case t_time: { |
932 | 0 | timespec_t ts_tmp; |
933 | 0 | if (*cp != '"') { |
934 | 0 | return JSON_ERR_BADSTRING; |
935 | 0 | } else { |
936 | 0 | ++cp; |
937 | 0 | } |
938 | 0 | ts_tmp = iso8601_to_timespec(cp); |
939 | 0 | arr->arr.timespecs.store[offset] = ts_tmp; |
940 | 0 | while (*cp && |
941 | 0 | *cp != '"') { |
942 | 0 | cp++; |
943 | 0 | } |
944 | 0 | if (*cp != '"') { |
945 | 0 | return JSON_ERR_BADSTRING; |
946 | 0 | } |
947 | 0 | ++cp; |
948 | 0 | } |
949 | 0 | break; |
950 | 0 | case t_timespec: |
951 | | // TODO not sure how to implement this |
952 | 0 | return JSON_ERR_BADNUM; |
953 | 0 | break; |
954 | 0 | case t_real: |
955 | 0 | arr->arr.reals.store[offset] = strtod(cp, &ep); |
956 | 0 | if (ep == cp) { |
957 | 0 | return JSON_ERR_BADNUM; |
958 | 0 | } |
959 | 0 | cp = ep; |
960 | 0 | break; |
961 | 0 | case t_boolean: |
962 | 0 | if (str_starts_with(cp, "true")) { |
963 | 0 | arr->arr.booleans.store[offset] = true; |
964 | 0 | cp += 4; |
965 | 0 | } else if (str_starts_with(cp, "false")) { |
966 | 0 | arr->arr.booleans.store[offset] = false; |
967 | 0 | cp += 5; |
968 | 0 | } |
969 | 0 | break; |
970 | 0 | case t_character: |
971 | 0 | FALLTHROUGH |
972 | 0 | case t_array: |
973 | 0 | FALLTHROUGH |
974 | 0 | case t_check: |
975 | 0 | FALLTHROUGH |
976 | 0 | case t_ignore: |
977 | 0 | json_debug_trace(1, "json: %s", "Invalid array subtype.\n"); |
978 | 0 | return JSON_ERR_SUBTYPE; |
979 | 0 | } |
980 | 0 | arrcount++; |
981 | 0 | if (ISSPACE(*cp)) { |
982 | 0 | cp++; |
983 | 0 | } |
984 | 0 | if (*cp == ']') { |
985 | 0 | json_debug_trace(1, "json: %s", "End of array found.\n"); |
986 | 0 | goto breakout; |
987 | 0 | } |
988 | 0 | if (*cp == ',') { |
989 | 0 | cp++; |
990 | 0 | } else { |
991 | 0 | json_debug_trace(1, "json: %s", "Bad trailing syntax on array.\n"); |
992 | 0 | return JSON_ERR_BADSUBTRAIL; |
993 | 0 | } |
994 | 0 | } |
995 | 0 | json_debug_trace(1, "json: %s", "Too many elements in array.\n"); |
996 | 0 | if (NULL != end) { |
997 | 0 | *end = cp; |
998 | 0 | } |
999 | 0 | return JSON_ERR_SUBTOOLONG; |
1000 | 0 | breakout: |
1001 | 0 | if (NULL != arr->count) { |
1002 | 0 | *(arr->count) = arrcount; |
1003 | 0 | } |
1004 | 0 | if (NULL != end) { |
1005 | 0 | *end = cp; |
1006 | 0 | } |
1007 | 0 | json_debug_trace(1, "json: leaving json_read_array() with %d elements\n", |
1008 | 0 | arrcount); |
1009 | 0 | return 0; |
1010 | 0 | } |
1011 | | |
1012 | | int json_read_object(const char *cp, const struct json_attr_t *attrs, |
1013 | | const char **end) |
1014 | 0 | { |
1015 | 0 | int st; |
1016 | |
|
1017 | 0 | json_debug_trace(1, "json: json_read_object() sees '%s'\n", cp); |
1018 | 0 | st = json_internal_read_object(cp, attrs, NULL, 0, end); |
1019 | 0 | return st; |
1020 | 0 | } |
1021 | | |
1022 | | const char *json_error_string(int err) |
1023 | 0 | { |
1024 | 0 | const char *errors[] = { |
1025 | 0 | "unknown error while parsing JSON", |
1026 | 0 | "non-whitespace when expecting object start", |
1027 | 0 | "non-whitespace when expecting attribute start", |
1028 | 0 | "unknown attribute name", |
1029 | 0 | "attribute name too long", |
1030 | 0 | "saw [ when not expecting array", |
1031 | 0 | "array element specified, but no [", |
1032 | 0 | "string value too long", |
1033 | 0 | "token value too long", |
1034 | 0 | "garbage while expecting comma or } or ]", |
1035 | 0 | "didn't find expected array start", |
1036 | 0 | "error while parsing object array", |
1037 | 0 | "too many array elements", |
1038 | 0 | "garbage while expecting array comma", |
1039 | 0 | "unsupported array element type", |
1040 | 0 | "error while string parsing", |
1041 | 0 | "check attribute not matched", |
1042 | 0 | "can't support strings in parallel arrays", |
1043 | 0 | "invalid enumerated value", |
1044 | 0 | "saw quoted value when expecting nonstring", |
1045 | 0 | "didn't see quoted value when expecting string", |
1046 | 0 | "other data conversion error", |
1047 | 0 | "unexpected null value or attribute pointer", |
1048 | 0 | "object element specified, but no {", |
1049 | 0 | "input was empty or white-space only", |
1050 | 0 | }; |
1051 | |
|
1052 | 0 | if (err <= 0 || |
1053 | 0 | err >= (int)(sizeof(errors) / sizeof(errors[0]))) { |
1054 | 0 | return errors[0]; |
1055 | 0 | } |
1056 | 0 | return errors[err]; |
1057 | 0 | } |
1058 | | |
1059 | | /* quote a JSON string so it can be used as a simple JSON string. |
1060 | | * Used to output the JSON as a literal JSON string |
1061 | | * escape control chars, escape double quote. |
1062 | | * stop at NUL, in_len or bad unicode char |
1063 | | */ |
1064 | | char *json_quote(const char *in_buffer, char *out_buffer, size_t in_len, |
1065 | | size_t out_len) |
1066 | 0 | { |
1067 | 0 | const char *escape_match = "'\"/\\\b\f\n\r\t"; |
1068 | 0 | const char *escaped_bit = "'\"/\\bfnrt"; |
1069 | 0 | unsigned out_index = 0; |
1070 | 0 | const char *escape_ptr; |
1071 | 0 | unsigned in_index = 0; |
1072 | 0 | unsigned to_copy = 0; |
1073 | |
|
1074 | 0 | out_buffer[0] = '\0'; |
1075 | | |
1076 | | // check in string, stop at NUL, done in_len, or out_buffer full |
1077 | 0 | for (in_index = 0; in_buffer[in_index] != '\0'; in_index++) { |
1078 | |
|
1079 | 0 | if (in_index >= in_len) { |
1080 | | // got all from input buffer |
1081 | 0 | break; |
1082 | 0 | } |
1083 | | |
1084 | 0 | if (out_index > (out_len - 8) ) { |
1085 | | /* output out_buffer full. Not enough space for a 4-byte UTF + NUL, |
1086 | | * or \uxxxx + NUL. Safer to check once, at the top, |
1087 | | * than a lot of specific size checks later in the loop. |
1088 | | */ |
1089 | 0 | break; |
1090 | 0 | } |
1091 | | |
1092 | 0 | if (in_buffer[in_index] & 0x80) { |
1093 | | // highbit set. assume unicode |
1094 | 0 | to_copy = 0; // always reset before use, to shut up coverity |
1095 | | |
1096 | | // check in_len so we don't overrun in_buffer |
1097 | 0 | if ((in_len > (in_index + 1)) && |
1098 | 0 | (0xC0 == (0xE0 & (uint8_t)in_buffer[in_index])) && |
1099 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1]))) { |
1100 | | // utf-8 ish 16bit rune - deg, plusm, mplus etc. |
1101 | 0 | to_copy = 2; |
1102 | 0 | } else if ((in_len > (in_index + 2)) && |
1103 | 0 | (0xE0 == (0xF0 & (uint8_t)in_buffer[in_index])) && |
1104 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1])) && |
1105 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 2]))) { |
1106 | | // utf-8 ish 24 bit rune - (double) prime etc. |
1107 | 0 | to_copy = 3; |
1108 | 0 | } else if ((in_len > (in_index + 3)) && |
1109 | 0 | (0xF0 == (0xF8 & (uint8_t)in_buffer[in_index])) && |
1110 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1])) && |
1111 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 2])) && |
1112 | 0 | (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 3]))) { |
1113 | | // utf-8 ish 32 bit rune - musical symbol g clef etc. |
1114 | 0 | to_copy = 4; |
1115 | 0 | } else { |
1116 | | // WTF?? Short UTF? Bad UTF? |
1117 | 0 | str_appendf(out_buffer, out_len, |
1118 | 0 | "\\u%04x", in_buffer[in_index] & 0x0ff); |
1119 | 0 | out_index += 6; |
1120 | 0 | continue; |
1121 | 0 | } |
1122 | | |
1123 | 0 | memcpy(&out_buffer[out_index], &in_buffer[in_index], to_copy); |
1124 | 0 | out_index += to_copy; |
1125 | | // minus one as the for loop does in_index++ |
1126 | 0 | in_index += to_copy - 1; |
1127 | 0 | out_buffer[out_index] = '\0'; |
1128 | 0 | continue; |
1129 | 0 | } |
1130 | | |
1131 | | /* Try to find current byte from in buffer in string escape |
1132 | | * match if it is there append '\', the corresponding byte |
1133 | | * from escaped bit, and a null byte to end of out buffer. |
1134 | | */ |
1135 | 0 | escape_ptr = strchr(escape_match, in_buffer[in_index]); |
1136 | 0 | if (escape_ptr >= escape_match) { |
1137 | 0 | out_buffer[out_index++] = '\\'; |
1138 | 0 | out_buffer[out_index++] = escaped_bit[escape_ptr - escape_match]; |
1139 | 0 | out_buffer[out_index] = 0; |
1140 | 0 | continue; |
1141 | 0 | } |
1142 | | |
1143 | | // Escape 0-31 and 127 if not previously handled (0-x01f,x7f) |
1144 | 0 | if ('\x1f' >= in_buffer[in_index] || '\x7f' == in_buffer[in_index]) { |
1145 | 0 | str_appendf(out_buffer, out_len, "\\u%04x", |
1146 | 0 | in_buffer[in_index] & 0x0ff); |
1147 | 0 | out_index += 6; |
1148 | 0 | continue; |
1149 | 0 | } |
1150 | | // pass through everything not escaped. |
1151 | 0 | out_buffer[out_index++] = in_buffer[in_index]; |
1152 | 0 | out_buffer[out_index] = '\0'; |
1153 | 0 | } |
1154 | 0 | return out_buffer; |
1155 | 0 | } |
1156 | | |
1157 | | // vim: set expandtab shiftwidth=4 |