Commit bc0858aa authored by Jussi Kivilinna's avatar Jussi Kivilinna Committed by Pekka Niemimaa
Browse files

netutils/cJSON: remove old parsing and printing functionality, replaced by...

netutils/cJSON: remove old parsing and printing functionality, replaced by stream engine based string handling

From github:thingsee/thingsee-sdk pull request:

 "Improve cJSON to allow parse/print from/to stream (caller supplied getc/putc functions) for reducing peak memory usage"

 https://github.com/thingsee/thingsee-sdk/pull/6

Signed-off-by: default avatarJussi Kivilinna <jussi.kivilinna@haltian.com>
parent a4907154
......@@ -58,22 +58,10 @@
* Private Data
****************************************************************************/
static const char *ep;
static const unsigned char firstByteMark[7] =
{ 0x00, 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc };
/****************************************************************************
* Private Prototypes
****************************************************************************/
static const char *parse_value(cJSON *item, const char *value);
static char *print_value(cJSON *item, int depth, int fmt);
static const char *parse_array(cJSON *item, const char *value);
static char *print_array(cJSON *item, int depth, int fmt);
static const char *parse_object(cJSON *item, const char *value);
static char *print_object(cJSON *item, int depth, int fmt);
/****************************************************************************
* Private Functions
****************************************************************************/
......@@ -101,980 +89,6 @@ static int cJSON_strcasecmp(const char *s1, const char *s2)
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}
/* 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, sign = 1, scale = 0;
int subscale = 0, signsubscale = 1;
/* Could use sscanf for this? */
/* 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++;
if (*num == '+')
{
num++;
}
/* With sign? */
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;
}
/* Render the number nicely from the given item into a string. */
static char *print_number(cJSON *item)
{
char *str;
double d = item->valuedouble;
double id = item->valueint;
size_t slen;
int type;
/* Get type and needed length output string. */
if ((d <= INT_MAX && d >= INT_MIN) &&
(fabs(id - d) <= DBL_EPSILON || fabs(floor(d) - d) <= DBL_EPSILON))
{
slen = snprintf(NULL, 0, "%d", item->valueint);
type = 1;
}
else if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
{
slen = snprintf(NULL, 0, "%e", d);
type = -1;
}
else
{
slen = snprintf(NULL, 0, "%.10f", d);
type = 0;
}
/* Allocate and fill output. */
slen += 1;
str = (char *)cJSON_malloc(slen);
if (!str)
return NULL;
if (type > 0)
{
snprintf(str, slen, "%d", item->valueint);
}
else if (type < 0)
{
snprintf(str, slen, "%e", d);
}
else
{
snprintf(str, slen, "%.10f", d);
if (strchr(str, '.'))
{
/* Remove trailing zeros. */
slen -= 1;
while (slen > 0)
{
if (str[--slen] != '0')
break;
str[slen] = '\0';
}
/* Remove trailing dot. */
if (slen > 0 && str[slen] == '.')
str[slen] = '\0';
}
}
return str;
}
/* Parse the input text into an unescaped cstring, and populate item. */
static const char *parse_string(cJSON *item, const char *str)
{
const char *ptr = str + 1;
char *ptr2;
char *out;
int len = 0;
unsigned uc;
unsigned uc2;
if (*str != '\"')
{
/* not a string! */
ep = str;
return 0;
}
while (*ptr != '\"' && *ptr && ++len)
{
/* Skip escaped quotes. */
if (*ptr++ == '\\')
{
ptr++;
}
}
/* This is how long we need for the string, roughly. */
out = (char *)cJSON_malloc(len + 1);
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. */
/* Get the unicode char. */
sscanf(ptr + 1, "%4x", &uc);
ptr += 4;
/* Check for invalid. */
if ((uc >= 0xdc00 && uc <= 0xdfff) || uc == 0)
{
break;
}
if (uc >= 0xd800 && uc <= 0xdbff) /* UTF16 surrogate pairs. */
{
/* missing second-half of surrogate. */
if (ptr[1] != '\\' || ptr[2] != 'u')
{
break;
}
sscanf(ptr + 3, "%4x", &uc2);
ptr += 6;
if (uc2 < 0xdc00 || uc2 > 0xdfff)
{
/* Invalid second-half of surrogate. */
break;
}
uc = 0x10000 | ((uc & 0x3ff) << 10) | (uc2 & 0x3ff);
}
len = 4;
if (uc < 0x80)
{
len = 1;
}
else if (uc < 0x800)
{
len = 2;
}
else if (uc < 0x10000)
{
len = 3;
}
ptr2 += len;
switch (len)
{
case 4:
*--ptr2 = ((uc | 0x80) & 0xbf);
uc >>= 6;
/* no break */
case 3:
*--ptr2 = ((uc | 0x80) & 0xbf);
uc >>= 6;
/* no break */
case 2:
*--ptr2 = ((uc | 0x80) & 0xbf);
uc >>= 6;
/* no break */
case 1:
*--ptr2 = (uc | firstByteMark[len]);
break;
}
ptr2 += len;
break;
default:
*ptr2++ = *ptr;
break;
}
ptr++;
}
}
*ptr2 = 0;
if (*ptr == '\"')
{
ptr++;
}
item->valuestring = out;
item->type = cJSON_String;
return ptr;
}
/* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str)
{
const char *ptr;
char *ptr2, *out;
int len = 0;
unsigned char token;
if (!str)
{
return cJSON_strdup("");
}
ptr = str;
while ((token = *ptr) && ++len)
{
if (strchr("\"\\\b\f\n\r\t", token))
{
len++;
}
else if (token < 32)
{
len += 5;
}
ptr++;
}
out = (char *)cJSON_malloc(len + 3);
if (!out)
{
return 0;
}
ptr2 = out;
ptr = str;
*ptr2++ = '\"';
while (*ptr)
{
if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\')
{
*ptr2++ = *ptr++;
}
else
{
*ptr2++ = '\\';
switch (token = *ptr++)
{
case '\\':
*ptr2++ = '\\';
break;
case '\"':
*ptr2++ = '\"';
break;
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;
default:
/* Escape and print */
sprintf(ptr2, "u%04x", token);
ptr2 += 5;
break;
}
}
}
*ptr2++ = '\"';
*ptr2++ = 0;
return out;
}
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item)
{
return print_string_ptr(item->valuestring);
}
/* Utility to jump whitespace and cr/lf */
static const char *skip(const char *in)
{
while (in && *in && (unsigned char)*in <= 32)
{
in++;
}
return in;
}
/* Parser core - when encountering text, process appropriately. */
static const char *parse_value(cJSON *item, const char *value)
{
if (!value)
{
/* Fail on null. */
return 0;
}
if (!strncmp(value, "null", 4))
{
item->type = cJSON_NULL;
return value + 4;
}
if (!strncmp(value, "false", 5))
{
item->type = cJSON_False;
return value + 5;
}
if (!strncmp(value, "true", 4))
{
item->type = cJSON_True;
item->valueint = 1;
return value + 4;
}
if (*value == '\"')
{
return parse_string(item, value);
}
if (*value == '-' || (*value >= '0' && *value <= '9'))
{
return parse_number(item, value);
}
if (*value == '[')
{
return parse_array(item, value);
}
if (*value == '{')
{
return parse_object(item, value);
}
/* Failure. */
ep = value;
return 0;
}
/* Render a value to text. */
static char *print_value(cJSON *item, int depth, int fmt)
{
char *out = 0;
if (!item)
{
return 0;
}
switch ((item->type) & 255)
{
case cJSON_NULL:
out = cJSON_strdup("null");
break;
case cJSON_False:
out = cJSON_strdup("false");
break;
case cJSON_True:
out = cJSON_strdup("true");
break;
case cJSON_Number:
out = print_number(item);
break;
case cJSON_String:
out = print_string(item);
break;
case cJSON_Array:
out = print_array(item, depth, fmt);
break;
case cJSON_Object:
out = print_object(item, depth, fmt);
break;
}
return out;
}
/* Build an array from input text. */
static const char *parse_array(cJSON *item, const char *value)
{
cJSON *child;
if (*value != '[')
{
/* not an array! */
ep = value;
return 0;
}
item->type = cJSON_Array;
value = skip(value + 1);
if (*value == ']')
{
/* Empty array. */
return value + 1;
}
item->child = child = cJSON_New_Item();
if (!item->child)
{
/* Memory fail */
return 0;
}
/* Skip any spacing, get the value. */
value = skip(parse_value(child, skip(value)));
if (!value)
{
return 0;
}
while (*value == ',')
{
cJSON *new_item;
if (!(new_item = cJSON_New_Item()))
{
/* <emory fail */
return 0;
}
child->next = new_item;
new_item->prev = child;
child = new_item;
value = skip(parse_value(child, skip(value + 1)));
if (!value)
{
/* Memory fail */
return 0;
}
}
if (*value == ']')
{
/* End of array */
return value + 1;
}
/* Malformed */
ep = value;
return 0;
}
/* Render an array to text */
static char *print_array(cJSON *item, int depth, int fmt)