MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / parse_string

Function parse_string

lib/cJSON.c:524–712  ·  view source on GitHub ↗

Parse the input text into an unescaped cstring, and populate item. */

Source from the content-addressed store, hash-verified

522
523/* Parse the input text into an unescaped cstring, and populate item. */
524static const unsigned char *parse_string(cJSON *item, const unsigned char *str, const unsigned char **ep)
525{
526 const unsigned char *ptr = str + 1;
527 const unsigned char *end_ptr = str + 1;
528 unsigned char *ptr2 = NULL;
529 unsigned char *out = NULL;
530 size_t len = 0;
531 unsigned uc = 0;
532 unsigned uc2 = 0;
533
534 /* not a string! */
535 if (*str != '\"')
536 {
537 *ep = str;
538 goto fail;
539 }
540
541 while ((*end_ptr != '\"') && *end_ptr)
542 {
543 if (*end_ptr++ == '\\')
544 {
545 if (*end_ptr == '\0')
546 {
547 /* prevent buffer overflow when last input character is a backslash */
548 goto fail;
549 }
550 /* Skip escaped quotes. */
551 end_ptr++;
552 }
553 len++;
554 }
555
556 /* This is at most how long we need for the string, roughly. */
557 out = (unsigned char*)cJSON_malloc(len + 1);
558 if (!out)
559 {
560 goto fail;
561 }
562 item->valuestring = (char*)out; /* assign here so out will be deleted during cJSON_Delete() later */
563 item->type = cJSON_String;
564
565 ptr = str + 1;
566 ptr2 = out;
567 /* loop through the string literal */
568 while (ptr < end_ptr)
569 {
570 if (*ptr != '\\')
571 {
572 *ptr2++ = *ptr++;
573 }
574 /* escape sequence */
575 else
576 {
577 ptr++;
578 switch (*ptr)
579 {
580 case 'b':
581 *ptr2++ = '\b';

Callers 2

parse_valueFunction · 0.70
parse_objectFunction · 0.70

Calls 1

parse_hex4Function · 0.85

Tested by

no test coverage detected