/*
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"
/* Determine the number of bits that an integer has using the preprocessor */
#if INT_MAX == 32767
/* 16 bits */
#define INTEGER_SIZE 0x0010
#elif INT_MAX == 2147483647
/* 32 bits */
#define INTEGER_SIZE 0x0100
#elif INT_MAX == 9223372036854775807
/* 64 bits */
#define INTEGER_SIZE 0x1000
#else
#error "Failed to determine the size of an integer"
#endif
/* define our own boolean type */
typedef int cjbool;
#define true ((cjbool)1)
#define false ((cjbool)0)
static const char *global_ep = NULL;
const char *cJSON_GetErrorPtr(void)
{
return global_ep;
}
/* case insensitive strcmp */
static int cJSON_strcasecmp(const char *s1, const char *s2)
{
if (!s1)
{
return (s1 == s2) ? 0 : 1; /* both NULL? */
}
if (!s2)
{
return 1;
}
for(; tolower(*(const unsigned char *)s1) == tolower(*(const unsigned char *)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 = 0;
char *copy = NULL;
len = strlen(str) + 1;
if (!(copy = (char*)cJSON_malloc(len)))
{
return NULL;
}
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(void)
{
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 = NULL;
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->type & cJSON_StringIsConst) && 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)
{
double n = 0;
double sign = 1;
double scale = 0;
int subscale = 0;
int signsubscale = 1;
/* Has sign? */
if (*num == '-')
{
sign = -1;
num++;
}
/* is zero */
if (*num == '0')
{
num++;
}
/* Number? */
if ((*num >= '1') && (*num <= '9'))
{
do
{
n = (n * 10.0) + (*num++ - '0');
}
while ((*num >= '0') && (*num<='9'));
}
/* Fractional part? */
if ((*num == '.') && (num[1] >= '0') && (num[1] <= '9'))
{
num++;
do
{
n = (n *10.0) + (*num++ - '0');
scale--;
} while ((*num >= '0') && (*num <= '9'));
}
/* Exponent? */
if ((*num == 'e') || (*num == 'E'))
{
num++;
/* With sign? */
if (*num == '+')
{
num++;
}
else if (*num == '-')
{
signsubscale = -1;
num++;
}
/* Number? */
while ((*num>='0') && (*num<='9'))
{
subscale = (subscale * 10) + (*num++ - '0');
}
}
/* number = +/- number.fraction * 10^+/- exponent */
n = sign * n * pow(10.0, (scale + subscale * signsubscale));
item->valuedouble = n;
item->valueint = (int)n;
item->type = cJSON_Number;
return num;
}
/* calculate the next largest power of 2 */
static int pow2gt (int x)
{
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
#if INTEGER_SIZE & 0x1110 /* at least 16 bit */
x |= x >> 8;
#endif
#if INTEGER_SIZE & 0x1100 /* at least 32 bit */
x |= x >> 16;
#endif
#if INT_SIZE & 0x1000 /* 64 bit */
x |= x >> 32;
#endif
return x + 1;
}
typedef struct
{
char *buffer;
int length;
int offset;
cjbool noalloc;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
static char* ensure(printbuffer *p, int needed)
{
char *newbuffer = NULL;
int newsize = 0;
if (!p || !p->buffer)
{
return NULL;
}
needed += p->offset;
if (needed <= p->length)
{
return p->buffer + p->offset;
}
if (p->noalloc) {
return NULL;
}
newsize = pow2gt(needed);
newbuffer = (char*)cJSON_malloc(newsize);
if (!newbuffer)
{
cJSON_free(p->buffer);
p->length = 0;
p->buffer = NULL;
return NULL;
}
if (newbuffer)
{
memcpy(newbuffer, p->buffer, p->length);
}
cJSON_free(p->buffer);
p->length = newsize;
p->buffer = newbuffer;
return newbuffer + p->offset;
}
/* calculate the new length of the string in a printbuffer */
static int update(const printbuffer *p)
{
char *str = NULL;
if (!p || !p->buffer)
{
return 0;
}
str = p->buffer + p->offset;
return p->offset + strlen(str);
}
/* Render the number nicely from the given item into a string. */
static char *print_number(const cJSON *item, printbuffer *p)
{
char *str = NULL;
double d = item->valuedouble;
/* special case for 0. */
if (d == 0)
{
if (p)
{
str = ensure(p, 2);
}
else
{
str = (char*)cJSON_malloc(2);
}
if (str)
{
strcpy(str,"0");
}
}
/* value is an int */
else if ((fabs(((double)item->valueint) - d) <= DBL_EPSILON) && (d <= INT_MAX) && (d >= INT_MIN))
{
if (p)
{
str = ensure(p, 21);
}
else
{
/* 2^64+1 can be represented in 21 chars. */
str = (char*)cJSON_malloc(21);
}
if (str)
{
sprintf(str, "%d", item->valueint);
}
}
/* value is a floating point number */
else
{
if (p)
{
/* This is a nice tradeoff. */
str = ensure(p, 64);
}
else
{
/* This is a nice tradeoff. */
str=(char*)cJSON_malloc(64);
}
if (str)
{
/* This checks for NaN and Infinity */