/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* cJSON */
/* JSON parser in C. */
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"
#ifndef INT_MAX
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
#define UINT_MAX 4294967295U
#endif
/* remove global variable for thread safe. --by Bwar on 2020-11-15
static const char *ep;
const char *cJSON_GetErrorPtr()
{
return ep;
}
*/
static int cJSON_strcasecmp(const char *s1, const char *s2)
{
if (!s1)
return (s1 == s2) ? 0 : 1;
if (!s2)
return 1;
for (; tolower(*s1) == tolower(*s2); ++s1, ++s2)
if (*s1 == 0)
return 0;
return tolower(*(const unsigned char *)s1)
- tolower(*(const unsigned char *)s2);
}
static void *(*cJSON_malloc)(size_t sz) = malloc;
static void (*cJSON_free)(void *ptr) = free;
static char* cJSON_strdup(const char* str)
{
size_t len;
char* copy;
len = strlen(str) + 1;
if (!(copy = (char*) cJSON_malloc(len)))
return 0;
memcpy(copy, str, len);
return copy;
}
void cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (!hooks)
{ /* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn) ? hooks->malloc_fn : malloc;
cJSON_free = (hooks->free_fn) ? hooks->free_fn : free;
}
/* Internal constructor. */
static cJSON *cJSON_New_Item()
{
cJSON* node = (cJSON*) cJSON_malloc(sizeof(cJSON));
if (node)
memset(node, 0, sizeof(cJSON));
return node;
}
/* Delete a cJSON structure. */
void cJSON_Delete(cJSON *c)
{
cJSON *next;
while (c)
{
next = c->next;
if (!(c->type & cJSON_IsReference) && c->child)
cJSON_Delete(c->child);
if (!(c->type & cJSON_IsReference) && c->valuestring)
cJSON_free(c->valuestring);
if (c->string)
cJSON_free(c->string);
cJSON_free(c);
c = next;
}
}
/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item, const char *num)
{
long double n = 0, scale = 0;
int subscale = 0, signsubscale = 1;
item->sign = 1;
/* Could use sscanf for this? */
if (*num == '-')
item->sign = -1, num++; /* Has sign? */
if (*num == '0')
num++; /* is zero */
if (*num >= '1' && *num <= '9')
do
n = (n * 10.0) + (*num++ - '0');
while (*num >= '0' && *num <= '9'); /* Number? */
if (*num == '.' && num[1] >= '0' && num[1] <= '9')
{
num++;
do
n = (n * 10.0) + (*num++ - '0'), scale--;
while (*num >= '0' && *num <= '9');
} /* Fractional part? */
if (*num == 'e' || *num == 'E') /* Exponent? */
{
num++;
if (*num == '+')
num++;
else if (*num == '-')
signsubscale = -1, num++; /* With sign? */
while (*num >= '0' && *num <= '9')
subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
}
if (scale == 0 && subscale == 0)
{
item->valuedouble = (double)(item->sign * n);
item->valueint = item->sign * (int64)n;
item->type = cJSON_Int;
}
else
{
n = item->sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
item->valuedouble = (double)n;
item->valueint = (int64)n;
item->type = cJSON_Double;
}
return num;
}
/* Render the number nicely from the given item into a string. */
static char *print_double(cJSON *item)
{
char *str;
double d = item->valuedouble;
str = (char*) cJSON_malloc(64); /* This is a nice tradeoff. */
if (str)
{
if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
sprintf(str, "%lf", d);
else
sprintf(str, "%f", d);
}
return str;
}
static char *print_int(cJSON *item)
{
char *str;
str = (char*) cJSON_malloc(22); /* 2^64+1 can be represented in 21 chars. */
if (str)
{
if (item->sign == -1)
{
if ((int64)item->valueint <= (int64)INT_MAX && (int64)item->valueint >= (int64)INT_MIN)
{
sprintf(str, "%d", (int32)item->valueint);
}
else
{
sprintf(str, "%ld", (int64)item->valueint);
}
}
else
{
if (item->valueint <= (uint64)UINT_MAX)
{
sprintf(str, "%u", (uint32)item->valueint);
}
else
{
sprintf(str, "%lu", item->valueint);
}
}
}
return str;
}
/* Parse the input text into an unescaped cstring, and populate item. */
static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0,
0xF8, 0xFC };
static const char *parse_string(cJSON *item, const char *str, const char **ep)
{
const char *ptr = str + 1;
char *ptr2;
char *out;
int len = 0;
unsigned uc, uc2;
if (*str != '\"')
{
*ep = str;
return 0;
} /* not a string! */
while (*ptr != '\"' && *ptr && ++len)
if (*ptr++ == '\\')
ptr++; /* Skip escaped quotes. */
out = (char*) cJSON_malloc(len + 1); /* This is how long we need for the string, roughly. */
if (!out)
return 0;
ptr = str + 1;
ptr2 = out;
while (*ptr != '\"' && *ptr)
{
if (*ptr != '\\')
*ptr2++ = *ptr++;
else
{
ptr++;
switch (*ptr)
{
case 'b':
*ptr2++ = '\b';
break;
case 'f':
*ptr2++ = '\f';
break;
case 'n':
*ptr2++ = '\n';
break;
case 'r':
*ptr2++ = '\r';
break;
case 't':
*ptr2++ = '\t';
break;
case 'u': /* transcode utf16 to utf8. */
sscanf(ptr + 1, "%4x", &uc);
ptr += 4; /* get the unicode char. */
if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0)
break; // check for invalid.
if (uc >= 0xD800 && uc <= 0xDBFF) // UTF16 surrogate pairs.
{
if (ptr[1] != '\\' || ptr[2] != 'u')
break; // missing second-half of surrogate.
sscanf(ptr + 3, "%4x", &uc2);
ptr += 6;
if (