Line data Source code
1 : /*
2 : * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
3 : *
4 : * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 : * Michael Clark <michael@metaparadigm.com>
6 : *
7 : * This library is free software; you can redistribute it and/or modify
8 : * it under the terms of the MIT license. See COPYING for details.
9 : *
10 : *
11 : * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
12 : * The copyrights to the contents of this file are licensed under the MIT License
13 : * (http://www.opensource.org/licenses/mit-license.php)
14 : */
15 :
16 : #include "config.h"
17 :
18 : #include "math_compat.h"
19 : #include <assert.h>
20 : #include <ctype.h>
21 : #include <limits.h>
22 : #include <math.h>
23 : #include <stddef.h>
24 : #include <stdio.h>
25 : #include <stdlib.h>
26 : #include <string.h>
27 :
28 : #include "debug.h"
29 : #include "json_inttypes.h"
30 : #include "json_object.h"
31 : #include "json_object_private.h"
32 : #include "json_tokener.h"
33 : #include "json_util.h"
34 : #include "printbuf.h"
35 : #include "strdup_compat.h"
36 :
37 : #ifdef HAVE_LOCALE_H
38 : #include <locale.h>
39 : #endif /* HAVE_LOCALE_H */
40 : #ifdef HAVE_XLOCALE_H
41 : #include <xlocale.h>
42 : #endif
43 : #ifdef HAVE_STRINGS_H
44 : #include <strings.h>
45 : #endif /* HAVE_STRINGS_H */
46 :
47 : #include "cpl_string.h"
48 :
49 : #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x)&7) + 9)
50 :
51 : #if !HAVE_STRNCASECMP && defined(_MSC_VER)
52 : /* MSC has the version as _strnicmp */
53 : #define strncasecmp _strnicmp
54 : #elif !HAVE_STRNCASECMP
55 : #error You do not have strncasecmp on your system.
56 : #endif /* HAVE_STRNCASECMP */
57 :
58 : /* Use C99 NAN by default; if not available, nan("") should work too. */
59 : #ifndef NAN
60 : #define NAN nan("")
61 : #endif /* !NAN */
62 :
63 : static const char json_null_str[] = "null";
64 : static const int json_null_str_len = sizeof(json_null_str) - 1;
65 : static const char json_inf_str[] = "Infinity";
66 : static const char json_inf_str_lower[] = "infinity";
67 : static const unsigned int json_inf_str_len = sizeof(json_inf_str) - 1;
68 : static const char json_nan_str[] = "NaN";
69 : static const int json_nan_str_len = sizeof(json_nan_str) - 1;
70 : static const char json_true_str[] = "true";
71 : static const int json_true_str_len = sizeof(json_true_str) - 1;
72 : static const char json_false_str[] = "false";
73 : static const int json_false_str_len = sizeof(json_false_str) - 1;
74 :
75 : /* clang-format off */
76 : static const char *json_tokener_errors[] = {
77 : "success",
78 : "continue",
79 : "nesting too deep",
80 : "unexpected end of data",
81 : "unexpected character",
82 : "null expected",
83 : "boolean expected",
84 : "number expected",
85 : "array value separator ',' expected",
86 : "quoted object property name expected",
87 : "object property name separator ':' expected",
88 : "object value separator ',' expected",
89 : "invalid string sequence",
90 : "expected comment",
91 : "invalid utf-8 string",
92 : "buffer size overflow"
93 : };
94 : /* clang-format on */
95 :
96 : /**
97 : * validete the utf-8 string in strict model.
98 : * if not utf-8 format, return err.
99 : */
100 : static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes);
101 :
102 : static int json_tokener_parse_double(const char *buf, int len, double *retval);
103 :
104 25 : const char *json_tokener_error_desc(enum json_tokener_error jerr)
105 : {
106 25 : int jerr_int = (int)jerr;
107 25 : if (jerr_int < 0 ||
108 : jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
109 0 : return "Unknown error, "
110 : "invalid json_tokener_error value passed to json_tokener_error_desc()";
111 25 : return json_tokener_errors[jerr];
112 : }
113 :
114 8 : enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
115 : {
116 8 : return tok->err;
117 : }
118 :
119 : /* Stuff for decoding unicode sequences */
120 : #define IS_HIGH_SURROGATE(uc) (((uc)&0xFC00) == 0xD800)
121 : #define IS_LOW_SURROGATE(uc) (((uc)&0xFC00) == 0xDC00)
122 : #define DECODE_SURROGATE_PAIR(hi, lo) ((((hi)&0x3FF) << 10) + ((lo)&0x3FF) + 0x10000)
123 : static unsigned char utf8_replacement_char[3] = {0xEF, 0xBF, 0xBD};
124 :
125 7990 : struct json_tokener *json_tokener_new_ex(int depth)
126 : {
127 : struct json_tokener *tok;
128 :
129 7990 : tok = (struct json_tokener *)calloc(1, sizeof(struct json_tokener));
130 7990 : if (!tok)
131 0 : return NULL;
132 7990 : tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
133 7990 : if (!tok->stack)
134 : {
135 0 : free(tok);
136 0 : return NULL;
137 : }
138 7990 : tok->pb = printbuf_new();
139 7990 : tok->max_depth = depth;
140 7990 : json_tokener_reset(tok);
141 7990 : return tok;
142 : }
143 :
144 7988 : struct json_tokener *json_tokener_new(void)
145 : {
146 7988 : return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
147 : }
148 :
149 7990 : void json_tokener_free(struct json_tokener *tok)
150 : {
151 7990 : json_tokener_reset(tok);
152 7990 : if (tok->pb)
153 7990 : printbuf_free(tok->pb);
154 7990 : free(tok->stack);
155 7990 : free(tok);
156 7990 : }
157 :
158 4411040 : static void json_tokener_reset_level(struct json_tokener *tok, int depth)
159 : {
160 4411040 : tok->stack[depth].state = json_tokener_state_eatws;
161 4411040 : tok->stack[depth].saved_state = json_tokener_state_start;
162 4411040 : json_object_put(tok->stack[depth].current);
163 4411040 : tok->stack[depth].current = NULL;
164 4411040 : free(tok->stack[depth].obj_field_name);
165 4411040 : tok->stack[depth].obj_field_name = NULL;
166 4411040 : }
167 :
168 15980 : void json_tokener_reset(struct json_tokener *tok)
169 : {
170 : int i;
171 15980 : if (!tok)
172 0 : return;
173 :
174 31960 : for (i = tok->depth; i >= 0; i--)
175 15980 : json_tokener_reset_level(tok, i);
176 15980 : tok->depth = 0;
177 15980 : tok->err = json_tokener_success;
178 : }
179 :
180 60 : struct json_object *json_tokener_parse(const char *str)
181 : {
182 : enum json_tokener_error jerr_ignored;
183 : struct json_object *obj;
184 60 : obj = json_tokener_parse_verbose(str, &jerr_ignored);
185 60 : return obj;
186 : }
187 :
188 60 : struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokener_error *error)
189 : {
190 : struct json_tokener *tok;
191 : struct json_object *obj;
192 :
193 60 : tok = json_tokener_new();
194 60 : if (!tok)
195 0 : return NULL;
196 60 : obj = json_tokener_parse_ex(tok, str, -1);
197 60 : *error = tok->err;
198 60 : if (tok->err != json_tokener_success
199 : #if 0
200 : /* This would be a more sensible default, and cause parsing
201 : * things like "null123" to fail when the caller can't know
202 : * where the parsing left off, but starting to fail would
203 : * be a notable behavior change. Save for a 1.0 release.
204 : */
205 : || json_tokener_get_parse_end(tok) != strlen(str)
206 : #endif
207 : )
208 :
209 : {
210 0 : if (obj != NULL)
211 0 : json_object_put(obj);
212 0 : obj = NULL;
213 : }
214 :
215 60 : json_tokener_free(tok);
216 60 : return obj;
217 : }
218 :
219 : #define state tok->stack[tok->depth].state
220 : #define saved_state tok->stack[tok->depth].saved_state
221 : #define current tok->stack[tok->depth].current
222 : #define obj_field_name tok->stack[tok->depth].obj_field_name
223 :
224 : /* Optimization:
225 : * json_tokener_parse_ex() consumed a lot of CPU in its main loop,
226 : * iterating character-by character. A large performance boost is
227 : * achieved by using tighter loops to locally handle units such as
228 : * comments and strings. Loops that handle an entire token within
229 : * their scope also gather entire strings and pass them to
230 : * printbuf_memappend() in a single call, rather than calling
231 : * printbuf_memappend() one char at a time.
232 : *
233 : * PEEK_CHAR() and ADVANCE_CHAR() macros are used for code that is
234 : * common to both the main loop and the tighter loops.
235 : */
236 :
237 : /* PEEK_CHAR(dest, tok) macro:
238 : * Peeks at the current char and stores it in dest.
239 : * Returns 1 on success, sets tok->err and returns 0 if no more chars.
240 : * Implicit inputs: str, len, nBytesp vars
241 : */
242 : #define PEEK_CHAR(dest, tok) \
243 : (((tok)->char_offset == len) \
244 : ? (((tok)->depth == 0 && state == json_tokener_state_eatws && \
245 : saved_state == json_tokener_state_finish) \
246 : ? (((tok)->err = json_tokener_success), 0) \
247 : : (((tok)->err = json_tokener_continue), 0)) \
248 : : (((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && \
249 : (!json_tokener_validate_utf8(*str, nBytesp))) \
250 : ? ((tok->err = json_tokener_error_parse_utf8_string), 0) \
251 : : (((dest) = *str), 1)))
252 :
253 : /* ADVANCE_CHAR() macro:
254 : * Increments str & tok->char_offset.
255 : * For convenience of existing conditionals, returns the old value of c (0 on eof)
256 : * Implicit inputs: c var
257 : */
258 : #define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
259 :
260 : /* End optimization macro defs */
261 :
262 7993 : struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
263 : {
264 7993 : struct json_object *obj = NULL;
265 7993 : char c = '\1';
266 7993 : unsigned int nBytes = 0;
267 7993 : unsigned int *nBytesp = &nBytes;
268 :
269 : #ifdef HAVE_USELOCALE
270 7993 : locale_t oldlocale = uselocale(NULL);
271 : locale_t newloc;
272 : #elif defined(HAVE_SETLOCALE)
273 : char *oldlocale = NULL;
274 : #endif
275 :
276 7993 : tok->char_offset = 0;
277 7993 : tok->err = json_tokener_success;
278 :
279 : /* this interface is presently not 64-bit clean due to the int len argument
280 : * and the internal printbuf interface that takes 32-bit int len arguments
281 : * so the function limits the maximum string size to INT32_MAX (2GB).
282 : * If the function is called with len == -1 then strlen is called to check
283 : * the string length is less than INT32_MAX (2GB)
284 : */
285 7993 : if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX))
286 : {
287 0 : tok->err = json_tokener_error_size;
288 0 : return NULL;
289 : }
290 :
291 : #ifdef HAVE_USELOCALE
292 : {
293 7993 : locale_t duploc = duplocale(oldlocale);
294 7993 : newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
295 7993 : if (newloc == NULL)
296 : {
297 0 : freelocale(duploc);
298 0 : return NULL;
299 : }
300 7993 : uselocale(newloc);
301 : }
302 : #elif defined(HAVE_SETLOCALE)
303 : {
304 : char *tmplocale;
305 : tmplocale = setlocale(LC_NUMERIC, NULL);
306 : if (tmplocale)
307 : oldlocale = strdup(tmplocale);
308 : setlocale(LC_NUMERIC, "C");
309 : }
310 : #endif
311 :
312 10197700 : while (PEEK_CHAR(c, tok)) // Note: c might be '\0' !
313 : {
314 :
315 10190700 : redo_char:
316 29169400 : switch (state)
317 : {
318 :
319 11456900 : case json_tokener_state_eatws:
320 : /* Advance until we change state */
321 46848200 : while (isspace((unsigned char)c))
322 : {
323 35392100 : if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
324 764 : goto out;
325 : }
326 11456100 : if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
327 : {
328 0 : printbuf_reset(tok->pb);
329 0 : printbuf_memappend_fast(tok->pb, &c, 1);
330 0 : state = json_tokener_state_comment_start;
331 : }
332 : else
333 : {
334 11456100 : state = saved_state;
335 11456100 : goto redo_char;
336 : }
337 0 : break;
338 :
339 2201540 : case json_tokener_state_start:
340 2201540 : switch (c)
341 : {
342 323599 : case '{':
343 323599 : state = json_tokener_state_eatws;
344 323599 : saved_state = json_tokener_state_object_field_start;
345 323599 : current = json_object_new_object();
346 323599 : if (current == NULL)
347 0 : goto out;
348 323599 : break;
349 189747 : case '[':
350 189747 : state = json_tokener_state_eatws;
351 189747 : saved_state = json_tokener_state_array;
352 189747 : current = json_object_new_array();
353 189747 : if (current == NULL)
354 0 : goto out;
355 189747 : break;
356 8 : case 'I':
357 : case 'i':
358 8 : state = json_tokener_state_inf;
359 8 : printbuf_reset(tok->pb);
360 8 : tok->st_pos = 0;
361 8 : goto redo_char;
362 3393 : case 'N':
363 : case 'n':
364 3393 : state = json_tokener_state_null; // or NaN
365 3393 : printbuf_reset(tok->pb);
366 3393 : tok->st_pos = 0;
367 3393 : goto redo_char;
368 4 : case '\'':
369 4 : if (tok->flags & JSON_TOKENER_STRICT)
370 : {
371 : /* in STRICT mode only double-quote are allowed */
372 0 : tok->err = json_tokener_error_parse_unexpected;
373 0 : goto out;
374 : }
375 : /* FALLTHRU */
376 : case '"':
377 1217200 : state = json_tokener_state_string;
378 1217200 : printbuf_reset(tok->pb);
379 1217200 : tok->quote_char = c;
380 1217200 : break;
381 38215 : case 'T':
382 : case 't':
383 : case 'F':
384 : case 'f':
385 38215 : state = json_tokener_state_boolean;
386 38215 : printbuf_reset(tok->pb);
387 38215 : tok->st_pos = 0;
388 38215 : goto redo_char;
389 429372 : case '0':
390 : case '1':
391 : case '2':
392 : case '3':
393 : case '4':
394 : case '5':
395 : case '6':
396 : case '7':
397 : case '8':
398 : case '9':
399 : case '-':
400 429372 : state = json_tokener_state_number;
401 429372 : printbuf_reset(tok->pb);
402 429372 : tok->is_double = 0;
403 429372 : goto redo_char;
404 5 : default: tok->err = json_tokener_error_parse_unexpected; goto out;
405 : }
406 1730550 : break;
407 :
408 2193730 : case json_tokener_state_finish:
409 2193730 : if (tok->depth == 0)
410 182 : goto out;
411 2193550 : obj = json_object_get(current);
412 2193550 : json_tokener_reset_level(tok, tok->depth);
413 2193550 : tok->depth--;
414 2193550 : goto redo_char;
415 :
416 11 : case json_tokener_state_inf: /* aka starts with 'i' (or 'I', or "-i", or "-I") */
417 : {
418 : /* If we were guaranteed to have len set, then we could (usually) handle
419 : * the entire "Infinity" check in a single strncmp (strncasecmp), but
420 : * since len might be -1 (i.e. "read until \0"), we need to check it
421 : * a character at a time.
422 : * Trying to handle it both ways would make this code considerably more
423 : * complicated with likely little performance benefit.
424 : */
425 11 : int is_negative = 0;
426 11 : const char *_json_inf_str = json_inf_str;
427 11 : if (!(tok->flags & JSON_TOKENER_STRICT))
428 11 : _json_inf_str = json_inf_str_lower;
429 :
430 : /* Note: tok->st_pos must be 0 when state is set to json_tokener_state_inf */
431 69 : while (tok->st_pos < (int)json_inf_str_len)
432 : {
433 63 : char inf_char = *str;
434 63 : if (!(tok->flags & JSON_TOKENER_STRICT))
435 63 : inf_char = tolower((unsigned char)*str);
436 63 : if (inf_char != _json_inf_str[tok->st_pos])
437 : {
438 5 : tok->err = json_tokener_error_parse_unexpected;
439 5 : goto out;
440 : }
441 58 : tok->st_pos++;
442 58 : (void)ADVANCE_CHAR(str, tok);
443 58 : if (!PEEK_CHAR(c, tok))
444 : {
445 : /* out of input chars, for now at least */
446 0 : goto out;
447 : }
448 : }
449 : /* We checked the full length of "Infinity", so create the object.
450 : * When handling -Infinity, the number parsing code will have dropped
451 : * the "-" into tok->pb for us, so check it now.
452 : */
453 6 : if (printbuf_length(tok->pb) > 0 && *(tok->pb->buf) == '-')
454 : {
455 3 : is_negative = 1;
456 : }
457 6 : current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
458 6 : if (current == NULL)
459 0 : goto out;
460 6 : saved_state = json_tokener_state_finish;
461 6 : state = json_tokener_state_eatws;
462 6 : goto redo_char;
463 : }
464 : break;
465 16959 : case json_tokener_state_null: /* aka starts with 'n' */
466 : {
467 : int size;
468 : int size_nan;
469 16959 : printbuf_memappend_fast(tok->pb, &c, 1);
470 16959 : size = json_min(tok->st_pos + 1, json_null_str_len);
471 16959 : size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
472 16959 : if ((!(tok->flags & JSON_TOKENER_STRICT) &&
473 16959 : strncasecmp(json_null_str, tok->pb->buf, size) == 0) ||
474 9 : (strncmp(json_null_str, tok->pb->buf, size) == 0))
475 : {
476 16950 : if (tok->st_pos == json_null_str_len)
477 : {
478 3387 : current = NULL;
479 3387 : saved_state = json_tokener_state_finish;
480 3387 : state = json_tokener_state_eatws;
481 3387 : goto redo_char;
482 : }
483 : }
484 9 : else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
485 9 : strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) ||
486 0 : (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0))
487 : {
488 9 : if (tok->st_pos == json_nan_str_len)
489 : {
490 3 : current = json_object_new_double(NAN);
491 3 : if (current == NULL)
492 0 : goto out;
493 3 : saved_state = json_tokener_state_finish;
494 3 : state = json_tokener_state_eatws;
495 3 : goto redo_char;
496 : }
497 : }
498 : else
499 : {
500 0 : tok->err = json_tokener_error_parse_null;
501 0 : goto out;
502 : }
503 13569 : tok->st_pos++;
504 : }
505 13569 : break;
506 :
507 0 : case json_tokener_state_comment_start:
508 0 : if (c == '*')
509 : {
510 0 : state = json_tokener_state_comment;
511 : }
512 0 : else if (c == '/')
513 : {
514 0 : state = json_tokener_state_comment_eol;
515 : }
516 : else
517 : {
518 0 : tok->err = json_tokener_error_parse_comment;
519 0 : goto out;
520 : }
521 0 : printbuf_memappend_fast(tok->pb, &c, 1);
522 0 : break;
523 :
524 0 : case json_tokener_state_comment:
525 : {
526 : /* Advance until we change state */
527 0 : const char *case_start = str;
528 0 : while (c != '*')
529 : {
530 0 : if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
531 : {
532 0 : printbuf_memappend_fast(tok->pb, case_start,
533 : (int)(str - case_start));
534 0 : goto out;
535 : }
536 : }
537 0 : printbuf_memappend_fast(tok->pb, case_start, 1 + (int)(str - case_start));
538 0 : state = json_tokener_state_comment_end;
539 : }
540 0 : break;
541 :
542 0 : case json_tokener_state_comment_eol:
543 : {
544 : /* Advance until we change state */
545 0 : const char *case_start = str;
546 0 : while (c != '\n')
547 : {
548 0 : if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
549 : {
550 0 : printbuf_memappend_fast(tok->pb, case_start,
551 : (int)(str - case_start));
552 0 : goto out;
553 : }
554 : }
555 0 : printbuf_memappend_fast(tok->pb, case_start, (int)(str - case_start));
556 : MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
557 0 : state = json_tokener_state_eatws;
558 : }
559 0 : break;
560 :
561 0 : case json_tokener_state_comment_end:
562 0 : printbuf_memappend_fast(tok->pb, &c, 1);
563 0 : if (c == '/')
564 : {
565 : MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
566 0 : state = json_tokener_state_eatws;
567 : }
568 : else
569 : {
570 0 : state = json_tokener_state_comment;
571 : }
572 0 : break;
573 :
574 1652260 : case json_tokener_state_string:
575 : {
576 : /* Advance until we change state */
577 1652260 : const char *case_start = str;
578 : while (1)
579 : {
580 55187100 : if (c == tok->quote_char)
581 : {
582 1217200 : printbuf_memappend_fast(tok->pb, case_start,
583 : (int)(str - case_start));
584 2434400 : current =
585 1217200 : json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
586 1217200 : if (current == NULL)
587 0 : goto out;
588 1217200 : saved_state = json_tokener_state_finish;
589 1217200 : state = json_tokener_state_eatws;
590 1217200 : break;
591 : }
592 53969900 : else if (c == '\\')
593 : {
594 435055 : printbuf_memappend_fast(tok->pb, case_start,
595 : (int)(str - case_start));
596 435055 : saved_state = json_tokener_state_string;
597 435055 : state = json_tokener_state_string_escape;
598 435055 : break;
599 : }
600 53534900 : if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
601 : {
602 0 : printbuf_memappend_fast(tok->pb, case_start,
603 : (int)(str - case_start));
604 0 : goto out;
605 : }
606 : }
607 : }
608 1652260 : break;
609 :
610 435704 : case json_tokener_state_string_escape:
611 435704 : switch (c)
612 : {
613 291490 : case '"':
614 : case '\\':
615 : case '/':
616 291490 : printbuf_memappend_fast(tok->pb, &c, 1);
617 291490 : state = saved_state;
618 291490 : break;
619 144039 : case 'b':
620 : case 'n':
621 : case 'r':
622 : case 't':
623 : case 'f':
624 144039 : if (c == 'b')
625 0 : printbuf_memappend_fast(tok->pb, "\b", 1);
626 144039 : else if (c == 'n')
627 144039 : printbuf_memappend_fast(tok->pb, "\n", 1);
628 0 : else if (c == 'r')
629 0 : printbuf_memappend_fast(tok->pb, "\r", 1);
630 0 : else if (c == 't')
631 0 : printbuf_memappend_fast(tok->pb, "\t", 1);
632 0 : else if (c == 'f')
633 0 : printbuf_memappend_fast(tok->pb, "\f", 1);
634 144039 : state = saved_state;
635 144039 : break;
636 175 : case 'u':
637 175 : tok->ucs_char = 0;
638 175 : tok->st_pos = 0;
639 175 : state = json_tokener_state_escape_unicode;
640 175 : break;
641 0 : default: tok->err = json_tokener_error_parse_string; goto out;
642 : }
643 435704 : break;
644 :
645 : // ===================================================
646 :
647 700 : case json_tokener_state_escape_unicode:
648 : {
649 : /* Handle a 4-byte \uNNNN sequence, or two sequences if a surrogate pair */
650 : while (1)
651 : {
652 700 : if (!c || !strchr(json_hex_chars, c))
653 : {
654 0 : tok->err = json_tokener_error_parse_string;
655 0 : goto out;
656 : }
657 1400 : tok->ucs_char |=
658 700 : ((unsigned int)jt_hexdigit(c) << ((3 - tok->st_pos) * 4));
659 700 : tok->st_pos++;
660 700 : if (tok->st_pos >= 4)
661 175 : break;
662 :
663 525 : (void)ADVANCE_CHAR(str, tok);
664 525 : if (!PEEK_CHAR(c, tok))
665 : {
666 : /*
667 : * We're out of characters in the current call to
668 : * json_tokener_parse(), but a subsequent call might
669 : * provide us with more, so leave our current state
670 : * as-is (including tok->high_surrogate) and return.
671 : */
672 0 : goto out;
673 : }
674 : }
675 175 : tok->st_pos = 0;
676 :
677 : /* Now, we have a full \uNNNN sequence in tok->ucs_char */
678 :
679 : /* If the *previous* sequence was a high surrogate ... */
680 175 : if (tok->high_surrogate)
681 : {
682 0 : if (IS_LOW_SURROGATE(tok->ucs_char))
683 : {
684 : /* Recalculate the ucs_char, then fall thru to process normally */
685 0 : tok->ucs_char = DECODE_SURROGATE_PAIR(tok->high_surrogate,
686 : tok->ucs_char);
687 : }
688 : else
689 : {
690 : /* High surrogate was not followed by a low surrogate
691 : * Replace the high and process the rest normally
692 : */
693 0 : printbuf_memappend_fast(tok->pb,
694 : (char *)utf8_replacement_char, 3);
695 : }
696 0 : tok->high_surrogate = 0;
697 : }
698 :
699 175 : if (tok->ucs_char < 0x80)
700 : {
701 : unsigned char unescaped_utf[1];
702 135 : unescaped_utf[0] = tok->ucs_char;
703 135 : printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 1);
704 : }
705 40 : else if (tok->ucs_char < 0x800)
706 : {
707 : unsigned char unescaped_utf[2];
708 0 : unescaped_utf[0] = 0xc0 | (tok->ucs_char >> 6);
709 0 : unescaped_utf[1] = 0x80 | (tok->ucs_char & 0x3f);
710 0 : printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 2);
711 : }
712 40 : else if (IS_HIGH_SURROGATE(tok->ucs_char))
713 : {
714 : /*
715 : * The next two characters should be \u, HOWEVER,
716 : * we can't simply peek ahead here, because the
717 : * characters we need might not be passed to us
718 : * until a subsequent call to json_tokener_parse.
719 : * Instead, transition through a couple of states.
720 : * (now):
721 : * _escape_unicode => _unicode_need_escape
722 : * (see a '\\' char):
723 : * _unicode_need_escape => _unicode_need_u
724 : * (see a 'u' char):
725 : * _unicode_need_u => _escape_unicode
726 : * ...and we'll end up back around here.
727 : */
728 0 : tok->high_surrogate = tok->ucs_char;
729 0 : tok->ucs_char = 0;
730 0 : state = json_tokener_state_escape_unicode_need_escape;
731 0 : break;
732 : }
733 40 : else if (IS_LOW_SURROGATE(tok->ucs_char))
734 : {
735 : /* Got a low surrogate not preceded by a high */
736 0 : printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
737 : }
738 40 : else if (tok->ucs_char < 0x10000)
739 : {
740 : unsigned char unescaped_utf[3];
741 40 : unescaped_utf[0] = 0xe0 | (tok->ucs_char >> 12);
742 40 : unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
743 40 : unescaped_utf[2] = 0x80 | (tok->ucs_char & 0x3f);
744 40 : printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 3);
745 : }
746 0 : else if (tok->ucs_char < 0x110000)
747 : {
748 : unsigned char unescaped_utf[4];
749 0 : unescaped_utf[0] = 0xf0 | ((tok->ucs_char >> 18) & 0x07);
750 0 : unescaped_utf[1] = 0x80 | ((tok->ucs_char >> 12) & 0x3f);
751 0 : unescaped_utf[2] = 0x80 | ((tok->ucs_char >> 6) & 0x3f);
752 0 : unescaped_utf[3] = 0x80 | (tok->ucs_char & 0x3f);
753 0 : printbuf_memappend_fast(tok->pb, (char *)unescaped_utf, 4);
754 : }
755 : else
756 : {
757 : /* Don't know what we got--insert the replacement char */
758 0 : printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
759 : }
760 175 : state = saved_state; // i.e. _state_string or _state_object_field
761 : }
762 175 : break;
763 :
764 0 : case json_tokener_state_escape_unicode_need_escape:
765 : // We get here after processing a high_surrogate
766 : // require a '\\' char
767 0 : if (!c || c != '\\')
768 : {
769 : /* Got a high surrogate without another sequence following
770 : * it. Put a replacement char in for the high surrogate
771 : * and pop back up to _state_string or _state_object_field.
772 : */
773 0 : printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
774 0 : tok->high_surrogate = 0;
775 0 : tok->ucs_char = 0;
776 0 : tok->st_pos = 0;
777 0 : state = saved_state;
778 0 : goto redo_char;
779 : }
780 0 : state = json_tokener_state_escape_unicode_need_u;
781 0 : break;
782 :
783 0 : case json_tokener_state_escape_unicode_need_u:
784 : /* We already had a \ char, check that it's \u */
785 0 : if (!c || c != 'u')
786 : {
787 : /* Got a high surrogate with some non-unicode escape
788 : * sequence following it.
789 : * Put a replacement char in for the high surrogate
790 : * and handle the escape sequence normally.
791 : */
792 0 : printbuf_memappend_fast(tok->pb, (char *)utf8_replacement_char, 3);
793 0 : tok->high_surrogate = 0;
794 0 : tok->ucs_char = 0;
795 0 : tok->st_pos = 0;
796 0 : state = json_tokener_state_string_escape;
797 0 : goto redo_char;
798 : }
799 0 : state = json_tokener_state_escape_unicode;
800 0 : break;
801 :
802 : // ===================================================
803 :
804 192015 : case json_tokener_state_boolean:
805 : {
806 : int size1, size2;
807 192015 : printbuf_memappend_fast(tok->pb, &c, 1);
808 192015 : size1 = json_min(tok->st_pos + 1, json_true_str_len);
809 192015 : size2 = json_min(tok->st_pos + 1, json_false_str_len);
810 192015 : if ((!(tok->flags & JSON_TOKENER_STRICT) &&
811 192015 : strncasecmp(json_true_str, tok->pb->buf, size1) == 0) ||
812 5725 : (strncmp(json_true_str, tok->pb->buf, size1) == 0))
813 : {
814 186290 : if (tok->st_pos == json_true_str_len)
815 : {
816 37258 : current = json_object_new_boolean(1);
817 37258 : if (current == NULL)
818 0 : goto out;
819 37258 : saved_state = json_tokener_state_finish;
820 37258 : state = json_tokener_state_eatws;
821 37258 : goto redo_char;
822 : }
823 : }
824 5725 : else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
825 5725 : strncasecmp(json_false_str, tok->pb->buf, size2) == 0) ||
826 4 : (strncmp(json_false_str, tok->pb->buf, size2) == 0))
827 : {
828 5721 : if (tok->st_pos == json_false_str_len)
829 : {
830 952 : current = json_object_new_boolean(0);
831 952 : if (current == NULL)
832 0 : goto out;
833 952 : saved_state = json_tokener_state_finish;
834 952 : state = json_tokener_state_eatws;
835 952 : goto redo_char;
836 : }
837 : }
838 : else
839 : {
840 4 : tok->err = json_tokener_error_parse_boolean;
841 4 : goto out;
842 : }
843 153801 : tok->st_pos++;
844 : }
845 153801 : break;
846 :
847 429372 : case json_tokener_state_number:
848 : {
849 : /* Advance until we change state */
850 429372 : const char *case_start = str;
851 429372 : int case_len = 0;
852 429372 : int is_exponent = 0;
853 429372 : int neg_sign_ok = 1;
854 429372 : int pos_sign_ok = 0;
855 429372 : if (printbuf_length(tok->pb) > 0)
856 : {
857 : /* We don't save all state from the previous incremental parse
858 : so we need to re-generate it based on the saved string so far.
859 : */
860 0 : char *e_loc = strchr(tok->pb->buf, 'e');
861 0 : if (!e_loc)
862 0 : e_loc = strchr(tok->pb->buf, 'E');
863 0 : if (e_loc)
864 : {
865 0 : char *last_saved_char =
866 0 : &tok->pb->buf[printbuf_length(tok->pb) - 1];
867 0 : is_exponent = 1;
868 0 : pos_sign_ok = neg_sign_ok = 1;
869 : /* If the "e" isn't at the end, we can't start with a '-' */
870 0 : if (e_loc != last_saved_char)
871 : {
872 0 : neg_sign_ok = 0;
873 0 : pos_sign_ok = 0;
874 : }
875 : // else leave it set to 1, i.e. start of the new input
876 : }
877 : }
878 :
879 2855110 : while (c && ((c >= '0' && c <= '9') ||
880 585805 : (!is_exponent && (c == 'e' || c == 'E')) ||
881 585768 : (neg_sign_ok && c == '-') || (pos_sign_ok && c == '+') ||
882 563924 : (!tok->is_double && c == '.')))
883 : {
884 2425740 : pos_sign_ok = neg_sign_ok = 0;
885 2425740 : ++case_len;
886 :
887 : /* non-digit characters checks */
888 : /* note: since the main loop condition to get here was
889 : * an input starting with 0-9 or '-', we are
890 : * protected from input starting with '.' or
891 : * e/E.
892 : */
893 2425740 : switch (c)
894 : {
895 134552 : case '.':
896 134552 : tok->is_double = 1;
897 134552 : pos_sign_ok = 1;
898 134552 : neg_sign_ok = 1;
899 134552 : break;
900 37 : case 'e': /* FALLTHRU */
901 : case 'E':
902 37 : is_exponent = 1;
903 37 : tok->is_double = 1;
904 : /* the exponent part can begin with a negative sign */
905 37 : pos_sign_ok = neg_sign_ok = 1;
906 37 : break;
907 2291150 : default: break;
908 : }
909 :
910 2425740 : if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
911 : {
912 0 : printbuf_memappend_fast(tok->pb, case_start, case_len);
913 0 : goto out;
914 : }
915 : }
916 : /*
917 : Now we know c isn't a valid number char, but check whether
918 : it might have been intended to be, and return a potentially
919 : more understandable error right away.
920 : However, if we're at the top-level, use the number as-is
921 : because c can be part of a new object to parse on the
922 : next call to json_tokener_parse().
923 : */
924 429372 : if (tok->depth > 0 && c != ',' && c != ']' && c != '}' && c != '/' &&
925 41085 : c != 'I' && c != 'i' && !isspace((unsigned char)c))
926 : {
927 0 : tok->err = json_tokener_error_parse_number;
928 0 : goto out;
929 : }
930 429372 : if (case_len > 0)
931 429372 : printbuf_memappend_fast(tok->pb, case_start, case_len);
932 :
933 : // Check for -Infinity
934 429372 : if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
935 : {
936 3 : state = json_tokener_state_inf;
937 3 : tok->st_pos = 0;
938 3 : goto redo_char;
939 : }
940 429369 : if (tok->is_double && !(tok->flags & JSON_TOKENER_STRICT))
941 : {
942 : /* Trim some chars off the end, to allow things
943 : like "123e+" to parse ok. */
944 134557 : while (printbuf_length(tok->pb) > 1)
945 : {
946 134557 : char last_char = tok->pb->buf[printbuf_length(tok->pb) - 1];
947 134557 : if (last_char != 'e' && last_char != 'E' &&
948 134557 : last_char != '-' && last_char != '+')
949 : {
950 134557 : break;
951 : }
952 0 : tok->pb->buf[printbuf_length(tok->pb) - 1] = '\0';
953 0 : printbuf_length(tok->pb)--;
954 : }
955 : }
956 : }
957 : {
958 : int64_t num64;
959 : uint64_t numuint64;
960 : double numd;
961 442672 : if (!tok->is_double && tok->pb->buf[0] == '-' &&
962 13303 : json_parse_int64(tok->pb->buf, &num64) == 0)
963 : {
964 13303 : current = json_object_new_int64(num64);
965 13303 : if (current == NULL)
966 0 : goto out;
967 : }
968 697575 : else if (!tok->is_double && tok->pb->buf[0] != '-' &&
969 281509 : json_parse_uint64(tok->pb->buf, &numuint64) == 0)
970 : {
971 281509 : if (numuint64 && tok->pb->buf[0] == '0' &&
972 0 : (tok->flags & JSON_TOKENER_STRICT))
973 : {
974 0 : tok->err = json_tokener_error_parse_number;
975 0 : goto out;
976 : }
977 281509 : if (numuint64 <= INT64_MAX)
978 : {
979 281508 : num64 = (uint64_t)numuint64;
980 281508 : current = json_object_new_int64(num64);
981 281508 : if (current == NULL)
982 0 : goto out;
983 : }
984 : else
985 : {
986 1 : current = json_object_new_uint64(numuint64);
987 1 : if (current == NULL)
988 0 : goto out;
989 : }
990 : }
991 269114 : else if (tok->is_double &&
992 134557 : json_tokener_parse_double(
993 134557 : tok->pb->buf, printbuf_length(tok->pb), &numd) == 0)
994 : {
995 134557 : current = json_object_new_double_s(numd, tok->pb->buf);
996 134557 : if (current == NULL)
997 0 : goto out;
998 : }
999 : else
1000 : {
1001 0 : tok->err = json_tokener_error_parse_number;
1002 0 : goto out;
1003 : }
1004 429369 : saved_state = json_tokener_state_finish;
1005 429369 : state = json_tokener_state_eatws;
1006 429369 : goto redo_char;
1007 : }
1008 : break;
1009 :
1010 858761 : case json_tokener_state_array_after_sep:
1011 : case json_tokener_state_array:
1012 858761 : if (c == ']')
1013 : {
1014 : // Minimize memory usage; assume parsed objs are unlikely to be changed
1015 964 : json_object_array_shrink(current, 0);
1016 :
1017 964 : if (state == json_tokener_state_array_after_sep &&
1018 28 : (tok->flags & JSON_TOKENER_STRICT))
1019 : {
1020 0 : tok->err = json_tokener_error_parse_unexpected;
1021 0 : goto out;
1022 : }
1023 964 : saved_state = json_tokener_state_finish;
1024 964 : state = json_tokener_state_eatws;
1025 : }
1026 : else
1027 : {
1028 857797 : if (tok->depth >= tok->max_depth - 1)
1029 : {
1030 0 : tok->err = json_tokener_error_depth;
1031 0 : goto out;
1032 : }
1033 857797 : state = json_tokener_state_array_add;
1034 857797 : tok->depth++;
1035 857797 : json_tokener_reset_level(tok, tok->depth);
1036 857797 : goto redo_char;
1037 : }
1038 964 : break;
1039 :
1040 857797 : case json_tokener_state_array_add:
1041 857797 : if (json_object_array_add(current, obj) != 0)
1042 0 : goto out;
1043 857797 : saved_state = json_tokener_state_array_sep;
1044 857797 : state = json_tokener_state_eatws;
1045 857797 : goto redo_char;
1046 :
1047 857797 : case json_tokener_state_array_sep:
1048 857797 : if (c == ']')
1049 : {
1050 : // Minimize memory usage; assume parsed objs are unlikely to be changed
1051 188783 : json_object_array_shrink(current, 0);
1052 :
1053 188783 : saved_state = json_tokener_state_finish;
1054 188783 : state = json_tokener_state_eatws;
1055 : }
1056 669014 : else if (c == ',')
1057 : {
1058 669014 : saved_state = json_tokener_state_array_after_sep;
1059 669014 : state = json_tokener_state_eatws;
1060 : }
1061 : else
1062 : {
1063 0 : tok->err = json_tokener_error_parse_array;
1064 0 : goto out;
1065 : }
1066 857797 : break;
1067 :
1068 1337020 : case json_tokener_state_object_field_start:
1069 : case json_tokener_state_object_field_start_after_sep:
1070 1337020 : if (c == '}')
1071 : {
1072 1255 : if (state == json_tokener_state_object_field_start_after_sep &&
1073 148 : (tok->flags & JSON_TOKENER_STRICT))
1074 : {
1075 0 : tok->err = json_tokener_error_parse_unexpected;
1076 0 : goto out;
1077 : }
1078 1255 : saved_state = json_tokener_state_finish;
1079 1255 : state = json_tokener_state_eatws;
1080 : }
1081 1335760 : else if (c == '"' || c == '\'')
1082 : {
1083 1335760 : tok->quote_char = c;
1084 1335760 : printbuf_reset(tok->pb);
1085 1335760 : state = json_tokener_state_object_field;
1086 : }
1087 : else
1088 : {
1089 5 : tok->err = json_tokener_error_parse_object_key_name;
1090 5 : goto out;
1091 : }
1092 1337010 : break;
1093 :
1094 1336400 : case json_tokener_state_object_field:
1095 : {
1096 : /* Advance until we change state */
1097 1336400 : const char *case_start = str;
1098 : while (1)
1099 : {
1100 8128210 : if (c == tok->quote_char)
1101 : {
1102 1335760 : printbuf_memappend_fast(tok->pb, case_start,
1103 : (int)(str - case_start));
1104 1335760 : obj_field_name = strdup(tok->pb->buf);
1105 1335760 : saved_state = json_tokener_state_object_field_end;
1106 1335760 : state = json_tokener_state_eatws;
1107 1335760 : break;
1108 : }
1109 6792460 : else if (c == '\\')
1110 : {
1111 649 : printbuf_memappend_fast(tok->pb, case_start,
1112 : (int)(str - case_start));
1113 649 : saved_state = json_tokener_state_object_field;
1114 649 : state = json_tokener_state_string_escape;
1115 649 : break;
1116 : }
1117 6791810 : if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
1118 : {
1119 1 : printbuf_memappend_fast(tok->pb, case_start,
1120 : (int)(str - case_start));
1121 1 : goto out;
1122 : }
1123 : }
1124 : }
1125 1336400 : break;
1126 :
1127 1335760 : case json_tokener_state_object_field_end:
1128 1335760 : if (c == ':')
1129 : {
1130 1335760 : saved_state = json_tokener_state_object_value;
1131 1335760 : state = json_tokener_state_eatws;
1132 : }
1133 : else
1134 : {
1135 0 : tok->err = json_tokener_error_parse_object_key_sep;
1136 0 : goto out;
1137 : }
1138 1335760 : break;
1139 :
1140 1335760 : case json_tokener_state_object_value:
1141 1335760 : if (tok->depth >= tok->max_depth - 1)
1142 : {
1143 0 : tok->err = json_tokener_error_depth;
1144 0 : goto out;
1145 : }
1146 1335760 : state = json_tokener_state_object_value_add;
1147 1335760 : tok->depth++;
1148 1335760 : json_tokener_reset_level(tok, tok->depth);
1149 1335760 : goto redo_char;
1150 :
1151 1335760 : case json_tokener_state_object_value_add:
1152 1335760 : json_object_object_add(current, obj_field_name, obj);
1153 1335760 : free(obj_field_name);
1154 1335760 : obj_field_name = NULL;
1155 1335760 : saved_state = json_tokener_state_object_sep;
1156 1335760 : state = json_tokener_state_eatws;
1157 1335760 : goto redo_char;
1158 :
1159 1335760 : case json_tokener_state_object_sep:
1160 : /* { */
1161 1335760 : if (c == '}')
1162 : {
1163 322335 : saved_state = json_tokener_state_finish;
1164 322335 : state = json_tokener_state_eatws;
1165 : }
1166 1013420 : else if (c == ',')
1167 : {
1168 1013420 : saved_state = json_tokener_state_object_field_start_after_sep;
1169 1013420 : state = json_tokener_state_eatws;
1170 : }
1171 : else
1172 : {
1173 1 : tok->err = json_tokener_error_parse_object_value_sep;
1174 1 : goto out;
1175 : }
1176 1335750 : break;
1177 : }
1178 10189700 : (void)ADVANCE_CHAR(str, tok);
1179 10189700 : if (!c) // This is the char *before* advancing
1180 0 : break;
1181 : } /* while(PEEK_CHAR) */
1182 :
1183 7026 : out:
1184 7993 : if ((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && (nBytes != 0))
1185 : {
1186 0 : tok->err = json_tokener_error_parse_utf8_string;
1187 : }
1188 7993 : if (c && (state == json_tokener_state_finish) && (tok->depth == 0) &&
1189 3 : (tok->flags & (JSON_TOKENER_STRICT | JSON_TOKENER_ALLOW_TRAILING_CHARS)) ==
1190 : JSON_TOKENER_STRICT)
1191 : {
1192 : /* unexpected char after JSON data */
1193 0 : tok->err = json_tokener_error_parse_unexpected;
1194 : }
1195 7993 : if (!c)
1196 : {
1197 : /* We hit an eof char (0) */
1198 180 : if (state != json_tokener_state_finish && saved_state != json_tokener_state_finish)
1199 1 : tok->err = json_tokener_error_parse_eof;
1200 : }
1201 :
1202 : #ifdef HAVE_USELOCALE
1203 7993 : uselocale(oldlocale);
1204 7993 : freelocale(newloc);
1205 : #elif defined(HAVE_SETLOCALE)
1206 : setlocale(LC_NUMERIC, oldlocale);
1207 : free(oldlocale);
1208 : #endif
1209 :
1210 7993 : if (tok->err == json_tokener_success)
1211 : {
1212 7961 : json_object *ret = json_object_get(current);
1213 : int ii;
1214 :
1215 : /* Partially reset, so we parse additional objects on subsequent calls. */
1216 15922 : for (ii = tok->depth; ii >= 0; ii--)
1217 7961 : json_tokener_reset_level(tok, ii);
1218 7961 : return ret;
1219 : }
1220 :
1221 : MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", json_tokener_errors[tok->err],
1222 : tok->char_offset);
1223 32 : return NULL;
1224 : }
1225 :
1226 0 : static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes)
1227 : {
1228 0 : unsigned char chr = c;
1229 0 : if (*nBytes == 0)
1230 : {
1231 0 : if (chr >= 0x80)
1232 : {
1233 0 : if ((chr & 0xe0) == 0xc0)
1234 0 : *nBytes = 1;
1235 0 : else if ((chr & 0xf0) == 0xe0)
1236 0 : *nBytes = 2;
1237 0 : else if ((chr & 0xf8) == 0xf0)
1238 0 : *nBytes = 3;
1239 : else
1240 0 : return 0;
1241 : }
1242 : }
1243 : else
1244 : {
1245 0 : if ((chr & 0xC0) != 0x80)
1246 0 : return 0;
1247 0 : (*nBytes)--;
1248 : }
1249 0 : return 1;
1250 : }
1251 :
1252 0 : void json_tokener_set_flags(struct json_tokener *tok, int flags)
1253 : {
1254 0 : tok->flags = flags;
1255 0 : }
1256 :
1257 0 : size_t json_tokener_get_parse_end(struct json_tokener *tok)
1258 : {
1259 0 : assert(tok->char_offset >= 0); /* Drop this line when char_offset becomes a size_t */
1260 0 : return (size_t)tok->char_offset;
1261 : }
1262 :
1263 134557 : static int json_tokener_parse_double(const char *buf, int len, double *retval)
1264 : {
1265 : char *end;
1266 134557 : *retval = CPLStrtod(buf, &end);
1267 134557 : if (buf + len == end)
1268 134557 : return 0; // It worked
1269 0 : return 1;
1270 : }
|