Static Value-Flow Analysis
Classes | Macros | Typedefs | Functions | Variables
cJSON.cpp File Reference
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <float.h>
#include "Util/cJSON.h"

Go to the source code of this file.

Classes

struct  error
 
struct  internal_hooks
 
struct  parse_buffer
 
struct  printbuffer
 

Macros

#define true   ((cJSON_bool)1)
 
#define false   ((cJSON_bool)0)
 
#define isinf(d)   (isnan((d - d)) && !isnan(d))
 
#define isnan(d)   (d != d)
 
#define NAN   0.0/0.0
 
#define internal_malloc   malloc
 
#define internal_free   free
 
#define internal_realloc   realloc
 
#define static_strlen(string_literal)   (sizeof(string_literal) - sizeof(""))
 
#define can_read(buffer, size)   ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
 
#define can_access_at_index(buffer, index)   ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
 
#define cannot_access_at_index(buffer, index)   (!can_access_at_index(buffer, index))
 
#define buffer_at_offset(buffer)   ((buffer)->content + (buffer)->offset)
 
#define cjson_min(a, b)   (((a) < (b)) ? (a) : (b))
 

Typedefs

typedef struct internal_hooks internal_hooks
 

Functions

 CJSON_PUBLIC (const char *) cJSON_GetErrorPtr(void)
 
 CJSON_PUBLIC (char *) cJSON_GetStringValue(const cJSON *const item)
 
 CJSON_PUBLIC (double) cJSON_GetNumberValue(const cJSON *const item)
 
static int case_insensitive_strcmp (const unsigned char *string1, const unsigned char *string2)
 
static unsigned char * cJSON_strdup (const unsigned char *string, const internal_hooks *const hooks)
 
 CJSON_PUBLIC (void) cJSON_InitHooks(cJSON_Hooks *hooks)
 
static cJSONcJSON_New_Item (const internal_hooks *const hooks)
 
static unsigned char get_decimal_point (void)
 
static cJSON_bool parse_number (cJSON *const item, parse_buffer *const input_buffer)
 
 if (!(object->type &cJSON_String)||(object->type &cJSON_IsReference))
 
 if (strlen(valuestring)<=strlen(object->valuestring))
 
 if (copy==NULL)
 
 if (object->valuestring !=NULL)
 
static unsigned char * ensure (printbuffer *const p, size_t needed)
 
static void update_offset (printbuffer *const buffer)
 
static cJSON_bool compare_double (double a, double b)
 
static cJSON_bool print_number (const cJSON *const item, printbuffer *const output_buffer)
 
static unsigned parse_hex4 (const unsigned char *const input)
 
static unsigned char utf16_literal_to_utf8 (const unsigned char *const input_pointer, const unsigned char *const input_end, unsigned char **output_pointer)
 
static cJSON_bool parse_string (cJSON *const item, parse_buffer *const input_buffer)
 
static cJSON_bool print_string_ptr (const unsigned char *const input, printbuffer *const output_buffer)
 
static cJSON_bool print_string (const cJSON *const item, printbuffer *const p)
 
static cJSON_bool parse_value (cJSON *const item, parse_buffer *const input_buffer)
 
static cJSON_bool print_value (const cJSON *const item, printbuffer *const output_buffer)
 
static cJSON_bool parse_array (cJSON *const item, parse_buffer *const input_buffer)
 
static cJSON_bool print_array (const cJSON *const item, printbuffer *const output_buffer)
 
static cJSON_bool parse_object (cJSON *const item, parse_buffer *const input_buffer)
 
static cJSON_bool print_object (const cJSON *const item, printbuffer *const output_buffer)
 
static parse_bufferbuffer_skip_whitespace (parse_buffer *const buffer)
 
static parse_bufferskip_utf8_bom (parse_buffer *const buffer)
 
 CJSON_PUBLIC (cJSON *) cJSON_ParseWithOpts(const char *value
 
 if (NULL==value)
 
return cJSON_ParseWithLengthOpts (value, buffer_length, return_parse_end, require_null_terminated)
 
 if (value==NULL||0==buffer_length)
 
 if (item==NULL)
 
 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
 
 if (require_null_terminated)
 
 if (return_parse_end)
 
 if (value !=NULL)
 
static unsigned char * print (const cJSON *const item, cJSON_bool format, const internal_hooks *const hooks)
 
 if (prebuffer< 0)
 
 if (!p.buffer)
 
 if (!print_value(item, &p))
 
 return (char *) p.buffer
 
 CJSON_PUBLIC (cJSON_bool) cJSON_PrintPreallocated(cJSON *item
 
 if ((length< 0)||(buffer==NULL))
 
return print_value (item, &p)
 
static cJSONget_array_item (const cJSON *array, size_t index)
 
static cJSONget_object_item (const cJSON *const object, const char *const name, const cJSON_bool case_sensitive)
 
static void suffix_object (cJSON *prev, cJSON *item)
 
static cJSONcreate_reference (const cJSON *item, const internal_hooks *const hooks)
 
static cJSON_bool add_item_to_array (cJSON *array, cJSON *item)
 
static void * cast_away_const (const void *string)
 
static cJSON_bool add_item_to_object (cJSON *const object, const char *const string, cJSON *const item, const internal_hooks *const hooks, const cJSON_bool constant_key)
 
 if (add_item_to_object(object, name, null, &global_hooks, false))
 
 cJSON_Delete (null)
 
 if (add_item_to_object(object, name, true_item, &global_hooks, false))
 
 cJSON_Delete (true_item)
 
 if (add_item_to_object(object, name, false_item, &global_hooks, false))
 
 cJSON_Delete (false_item)
 
 if (add_item_to_object(object, name, bool_item, &global_hooks, false))
 
 cJSON_Delete (bool_item)
 
 if (add_item_to_object(object, name, number_item, &global_hooks, false))
 
 cJSON_Delete (number_item)
 
 if (add_item_to_object(object, name, string_item, &global_hooks, false))
 
 cJSON_Delete (string_item)
 
 if (add_item_to_object(object, name, raw_item, &global_hooks, false))
 
 cJSON_Delete (raw_item)
 
 if (add_item_to_object(object, name, object_item, &global_hooks, false))
 
 cJSON_Delete (object_item)
 
 if (add_item_to_object(object, name, array, &global_hooks, false))
 
 cJSON_Delete (array)
 
return cJSON_DetachItemViaPointer (object, to_detach)
 
 if (after_inserted==NULL)
 
 if (replacement->next !=NULL)
 
 if (parent->child==item)
 
 cJSON_Delete (item)
 
static cJSON_bool replace_item_in_object (cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
 
 if ((count< 0)||(numbers==NULL))
 
 for (i=0;a &&(i<(size_t) count);i++)
 
 if (a &&a->child)
 
 if ((count< 0)||(strings==NULL))
 
 if (!item)
 
 if (item->valuestring)
 
 while (child !=NULL)
 
 if (newitem &&newitem->child)
 
static void skip_oneline_comment (char **input)
 
static void skip_multiline_comment (char **input)
 
static void minify_string (char **input, char **output)
 

Variables

static error global_error = { NULL, 0 }
 
static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }
 
double number
 
const char * valuestring
 
 copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks)
 
const char ** return_parse_end
 
const char cJSON_bool require_null_terminated
 
 buffer_length = strlen(value) + sizeof("")
 
cJSONitem = NULL
 
global_error json = NULL
 
global_error position = 0
 
buffer content = (const unsigned char*)value
 
buffer length = buffer_length
 
buffer offset = 0
 
buffer hooks = global_hooks
 
fail __pad0__
 
return NULL
 
int prebuffer
 
int cJSON_bool fmt
 
p buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer)
 
p noalloc = false
 
p format = fmt
 
int index
 
const char *const string
 
const char *const name
 
const char *const const cJSON_bool boolean
 
const char *const const char *const raw
 
item next = NULL
 
int which
 
int cJSONnewitem
 
 after_inserted = get_array_item(array, (size_t)which)
 
newitem prev = after_inserted->prev
 
 else
 
return true
 
cJSON *const cJSONreplacement
 
int count
 
cJSONn = NULL
 
cJSONp = NULL
 
cJSONa = NULL
 
cJSON_bool recurse
 
cJSONchild = NULL
 
cJSONnewchild = NULL
 
newitem type = item->type & (~cJSON_IsReference)
 
newitem valueint = item->valueint
 
newitem valuedouble = item->valuedouble
 
fail __pad1__
 
const cJSON *const b
 

Macro Definition Documentation

◆ buffer_at_offset

#define buffer_at_offset (   buffer)    ((buffer)->content + (buffer)->offset)

Definition at line 303 of file cJSON.cpp.

◆ can_access_at_index

#define can_access_at_index (   buffer,
  index 
)    ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))

Definition at line 300 of file cJSON.cpp.

◆ can_read

#define can_read (   buffer,
  size 
)    ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))

Definition at line 298 of file cJSON.cpp.

◆ cannot_access_at_index

#define cannot_access_at_index (   buffer,
  index 
)    (!can_access_at_index(buffer, index))

Definition at line 301 of file cJSON.cpp.

◆ cjson_min

#define cjson_min (   a,
  b 
)    (((a) < (b)) ? (a) : (b))

Definition at line 1187 of file cJSON.cpp.

◆ false

#define false   ((cJSON_bool)0)

Definition at line 70 of file cJSON.cpp.

◆ internal_free

#define internal_free   free

Definition at line 180 of file cJSON.cpp.

◆ internal_malloc

#define internal_malloc   malloc

Definition at line 179 of file cJSON.cpp.

◆ internal_realloc

#define internal_realloc   realloc

Definition at line 181 of file cJSON.cpp.

◆ isinf

#define isinf (   d)    (isnan((d - d)) && !isnan(d))

Definition at line 74 of file cJSON.cpp.

◆ isnan

#define isnan (   d)    (d != d)

Definition at line 77 of file cJSON.cpp.

◆ NAN

#define NAN   0.0/0.0

Definition at line 84 of file cJSON.cpp.

◆ static_strlen

#define static_strlen (   string_literal)    (sizeof(string_literal) - sizeof(""))

Definition at line 185 of file cJSON.cpp.

◆ true

return true   ((cJSON_bool)1)

Definition at line 65 of file cJSON.cpp.

Typedef Documentation

◆ internal_hooks

Function Documentation

◆ add_item_to_array()

static cJSON_bool add_item_to_array ( cJSON array,
cJSON item 
)
static

Definition at line 1962 of file cJSON.cpp.

1963 {
1964  cJSON *child = NULL;
1965 
1966  if ((item == NULL) || (array == NULL) || (array == item))
1967  {
1968  return false;
1969  }
1970 
1971  child = array->child;
1972  /*
1973  * To find the last item in array quickly, we use prev in array
1974  */
1975  if (child == NULL)
1976  {
1977  /* list is empty, start new one */
1978  array->child = item;
1979  item->prev = item;
1980  item->next = NULL;
1981  }
1982  else
1983  {
1984  /* append to the end */
1985  if (child->prev)
1986  {
1988  array->child->prev = item;
1989  }
1990  }
1991 
1992  return true;
1993 }
return NULL
Definition: cJSON.cpp:1173
static void suffix_object(cJSON *prev, cJSON *item)
Definition: cJSON.cpp:1934
cJSON * item
Definition: cJSON.cpp:1100
cJSON * child
Definition: cJSON.cpp:2723
Definition: cJSON.h:104
struct cJSON * child
Definition: cJSON.h:109
struct cJSON * prev
Definition: cJSON.h:107
struct cJSON * next
Definition: cJSON.h:106

◆ add_item_to_object()

static cJSON_bool add_item_to_object ( cJSON *const  object,
const char *const  string,
cJSON *const  item,
const internal_hooks *const  hooks,
const cJSON_bool  constant_key 
)
static

Definition at line 2017 of file cJSON.cpp.

2018 {
2019  char *new_key = NULL;
2020  int new_type = cJSON_Invalid;
2021 
2022  if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item))
2023  {
2024  return false;
2025  }
2026 
2027  if (constant_key)
2028  {
2029  new_key = (char*)cast_away_const(string);
2030  new_type = item->type | cJSON_StringIsConst;
2031  }
2032  else
2033  {
2034  new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
2035  if (new_key == NULL)
2036  {
2037  return false;
2038  }
2039 
2040  new_type = item->type & ~cJSON_StringIsConst;
2041  }
2042 
2043  if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
2044  {
2045  hooks->deallocate(item->string);
2046  }
2047 
2048  item->string = new_key;
2049  item->type = new_type;
2050 
2051  return add_item_to_array(object, item);
2052 }
buffer hooks
Definition: cJSON.cpp:1114
static void * cast_away_const(const void *string)
Definition: cJSON.cpp:2008
static unsigned char * cJSON_strdup(const unsigned char *string, const internal_hooks *const hooks)
Definition: cJSON.cpp:189
static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
Definition: cJSON.cpp:1962
const char *const string
Definition: cJSON.cpp:1919
#define cJSON_StringIsConst
Definition: cJSON.h:100
#define cJSON_Invalid
Definition: cJSON.h:89
char * string
Definition: cJSON.h:122
int type
Definition: cJSON.h:112

◆ buffer_skip_whitespace()

static parse_buffer* buffer_skip_whitespace ( parse_buffer *const  buffer)
static

Definition at line 1040 of file cJSON.cpp.

1041 {
1042  if ((buffer == NULL) || (buffer->content == NULL))
1043  {
1044  return NULL;
1045  }
1046 
1048  {
1049  return buffer;
1050  }
1051 
1052  while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
1053  {
1054  buffer->offset++;
1055  }
1056 
1057  if (buffer->offset == buffer->length)
1058  {
1059  buffer->offset--;
1060  }
1061 
1062  return buffer;
1063 }
p buffer
Definition: cJSON.cpp:1274
#define cannot_access_at_index(buffer, index)
Definition: cJSON.cpp:301
#define buffer_at_offset(buffer)
Definition: cJSON.cpp:303
#define can_access_at_index(buffer, index)
Definition: cJSON.cpp:300

◆ case_insensitive_strcmp()

static int case_insensitive_strcmp ( const unsigned char *  string1,
const unsigned char *  string2 
)
static

Definition at line 134 of file cJSON.cpp.

135 {
136  if ((string1 == NULL) || (string2 == NULL))
137  {
138  return 1;
139  }
140 
141  if (string1 == string2)
142  {
143  return 0;
144  }
145 
146  for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
147  {
148  if (*string1 == '\0')
149  {
150  return 0;
151  }
152  }
153 
154  return tolower(*string1) - tolower(*string2);
155 }

◆ cast_away_const()

static void* cast_away_const ( const void *  string)
static

Definition at line 2008 of file cJSON.cpp.

2009 {
2010  return (void*)string;
2011 }

◆ cJSON_Delete() [1/10]

cJSON_Delete ( array  )

◆ cJSON_Delete() [2/10]

cJSON_Delete ( bool_item  )

◆ cJSON_Delete() [3/10]

cJSON_Delete ( false_item  )

◆ cJSON_Delete() [4/10]

cJSON_Delete ( item  )

◆ cJSON_Delete() [5/10]

cJSON_Delete ( null  )

◆ cJSON_Delete() [6/10]

cJSON_Delete ( number_item  )

◆ cJSON_Delete() [7/10]

cJSON_Delete ( object_item  )

◆ cJSON_Delete() [8/10]

cJSON_Delete ( raw_item  )

◆ cJSON_Delete() [9/10]

cJSON_Delete ( string_item  )

◆ cJSON_Delete() [10/10]

cJSON_Delete ( true_item  )

◆ cJSON_DetachItemViaPointer()

return cJSON_DetachItemViaPointer ( object  ,
to_detach   
)

◆ cJSON_New_Item()

static cJSON* cJSON_New_Item ( const internal_hooks *const  hooks)
static

Definition at line 242 of file cJSON.cpp.

243 {
244  cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
245  if (node)
246  {
247  memset(node, '\0', sizeof(cJSON));
248  }
249 
250  return node;
251 }

◆ cJSON_ParseWithLengthOpts()

return cJSON_ParseWithLengthOpts ( value  ,
buffer_length  ,
return_parse_end  ,
require_null_terminated   
)

◆ CJSON_PUBLIC() [1/6]

CJSON_PUBLIC ( char *  ) const

Definition at line 100 of file cJSON.cpp.

101 {
102  if (!cJSON_IsString(item))
103  {
104  return NULL;
105  }
106 
107  return item->valuestring;
108 }
char * valuestring
Definition: cJSON.h:115

◆ CJSON_PUBLIC() [2/6]

CJSON_PUBLIC ( cJSON ) const

Definition at line 1177 of file cJSON.cpp.

1178 {
1179  return cJSON_ParseWithOpts(value, 0, 0);
1180 }

◆ CJSON_PUBLIC() [3/6]

CJSON_PUBLIC ( cJSON_bool  )

Definition at line 1833 of file cJSON.cpp.

1834 {
1835  cJSON *child = NULL;
1836  size_t size = 0;
1837 
1838  if (array == NULL)
1839  {
1840  return 0;
1841  }
1842 
1843  child = array->child;
1844 
1845  while(child != NULL)
1846  {
1847  size++;
1848  child = child->next;
1849  }
1850 
1851  /* FIXME: Can overflow here. Cannot be fixed without breaking the API */
1852 
1853  return (int)size;
1854 }

◆ CJSON_PUBLIC() [4/6]

CJSON_PUBLIC ( const char *  )

Definition at line 95 of file cJSON.cpp.

96 {
97  return (const char*) (global_error.json + global_error.position);
98 }
static error global_error
Definition: cJSON.cpp:93
size_t position
Definition: cJSON.cpp:91
const unsigned char * json
Definition: cJSON.cpp:90

◆ CJSON_PUBLIC() [5/6]

CJSON_PUBLIC ( double  ) const

Definition at line 110 of file cJSON.cpp.

111 {
112  if (!cJSON_IsNumber(item))
113  {
114  return (double) NAN;
115  }
116 
117  return item->valuedouble;
118 }
#define NAN
Definition: cJSON.cpp:84
double valuedouble
Definition: cJSON.h:119

◆ CJSON_PUBLIC() [6/6]

CJSON_PUBLIC ( void  )

Definition at line 210 of file cJSON.cpp.

211 {
212  if (hooks == NULL)
213  {
214  /* Reset hooks */
215  global_hooks.allocate = malloc;
216  global_hooks.deallocate = free;
217  global_hooks.reallocate = realloc;
218  return;
219  }
220 
221  global_hooks.allocate = malloc;
222  if (hooks->malloc_fn != NULL)
223  {
224  global_hooks.allocate = hooks->malloc_fn;
225  }
226 
227  global_hooks.deallocate = free;
228  if (hooks->free_fn != NULL)
229  {
230  global_hooks.deallocate = hooks->free_fn;
231  }
232 
233  /* use realloc only if both free and malloc are used */
235  if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
236  {
237  global_hooks.reallocate = realloc;
238  }
239 }
static internal_hooks global_hooks
Definition: cJSON.cpp:187
void *CJSON_CDECL * reallocate(void *pointer, size_t size)
void *CJSON_CDECL * allocate(size_t size)

◆ cJSON_strdup()

static unsigned char* cJSON_strdup ( const unsigned char *  string,
const internal_hooks *const  hooks 
)
static

Definition at line 189 of file cJSON.cpp.

190 {
191  size_t length = 0;
192  unsigned char *copy = NULL;
193 
194  if (string == NULL)
195  {
196  return NULL;
197  }
198 
199  length = strlen((const char*)string) + sizeof("");
200  copy = (unsigned char*)hooks->allocate(length);
201  if (copy == NULL)
202  {
203  return NULL;
204  }
205  memcpy(copy, string, length);
206 
207  return copy;
208 }
copy
Definition: cJSON.cpp:414
buffer length
Definition: cJSON.cpp:1112

◆ compare_double()

static cJSON_bool compare_double ( double  a,
double  b 
)
static

Definition at line 540 of file cJSON.cpp.

541 {
542  double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
543  return (fabs(a - b) <= maxVal * DBL_EPSILON);
544 }
const cJSON *const b
Definition: cJSON.cpp:3008
cJSON * a
Definition: cJSON.cpp:2560

◆ create_reference()

static cJSON* create_reference ( const cJSON item,
const internal_hooks *const  hooks 
)
static

Definition at line 1941 of file cJSON.cpp.

1942 {
1943  cJSON *reference = NULL;
1944  if (item == NULL)
1945  {
1946  return NULL;
1947  }
1948 
1949  reference = cJSON_New_Item(hooks);
1950  if (reference == NULL)
1951  {
1952  return NULL;
1953  }
1954 
1955  memcpy(reference, item, sizeof(cJSON));
1956  reference->string = NULL;
1957  reference->type |= cJSON_IsReference;
1958  reference->next = reference->prev = NULL;
1959  return reference;
1960 }
static cJSON * cJSON_New_Item(const internal_hooks *const hooks)
Definition: cJSON.cpp:242
#define cJSON_IsReference
Definition: cJSON.h:99

◆ ensure()

static unsigned char* ensure ( printbuffer *const  p,
size_t  needed 
)
static

Definition at line 440 of file cJSON.cpp.

441 {
442  unsigned char *newbuffer = NULL;
443  size_t newsize = 0;
444 
445  if ((p == NULL) || (p->buffer == NULL))
446  {
447  return NULL;
448  }
449 
450  if ((p->length > 0) && (p->offset >= p->length))
451  {
452  /* make sure that offset is valid */
453  return NULL;
454  }
455 
456  if (needed > INT_MAX)
457  {
458  /* sizes bigger than INT_MAX are currently not supported */
459  return NULL;
460  }
461 
462  needed += p->offset + 1;
463  if (needed <= p->length)
464  {
465  return p->buffer + p->offset;
466  }
467 
468  if (p->noalloc)
469  {
470  return NULL;
471  }
472 
473  /* calculate new buffer size */
474  if (needed > (INT_MAX / 2))
475  {
476  /* overflow of int, use INT_MAX if possible */
477  if (needed <= INT_MAX)
478  {
479  newsize = INT_MAX;
480  }
481  else
482  {
483  return NULL;
484  }
485  }
486  else
487  {
488  newsize = needed * 2;
489  }
490 
491  if (p->hooks.reallocate != NULL)
492  {
493  /* reallocate with realloc if available */
494  newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
495  if (newbuffer == NULL)
496  {
497  p->hooks.deallocate(p->buffer);
498  p->length = 0;
499  p->buffer = NULL;
500 
501  return NULL;
502  }
503  }
504  else
505  {
506  /* otherwise reallocate manually */
507  newbuffer = (unsigned char*)p->hooks.allocate(newsize);
508  if (!newbuffer)
509  {
510  p->hooks.deallocate(p->buffer);
511  p->length = 0;
512  p->buffer = NULL;
513 
514  return NULL;
515  }
516 
517  memcpy(newbuffer, p->buffer, p->offset + 1);
518  p->hooks.deallocate(p->buffer);
519  }
520  p->length = newsize;
521  p->buffer = newbuffer;
522 
523  return newbuffer + p->offset;
524 }
cJSON * p
Definition: cJSON.cpp:2559

◆ for()

for ( = 0; a && (i < (size_t)count); i++)

Definition at line 2569 of file cJSON.cpp.

2570  {
2571  n = cJSON_CreateNumber(numbers[i]);
2572  if (!n)
2573  {
2574  cJSON_Delete(a);
2575  return NULL;
2576  }
2577  if(!i)
2578  {
2579  a->child = n;
2580  }
2581  else
2582  {
2583  suffix_object(p, n);
2584  }
2585  p = n;
2586  }
cJSON * n
Definition: cJSON.cpp:2558
cJSON_Delete(null)

◆ get_array_item()

static cJSON* get_array_item ( const cJSON array,
size_t  index 
)
static

Definition at line 1856 of file cJSON.cpp.

1857 {
1858  cJSON *current_child = NULL;
1859 
1860  if (array == NULL)
1861  {
1862  return NULL;
1863  }
1864 
1865  current_child = array->child;
1866  while ((current_child != NULL) && (index > 0))
1867  {
1868  index--;
1869  current_child = current_child->next;
1870  }
1871 
1872  return current_child;
1873 }
int index
Definition: cJSON.cpp:1876

◆ get_decimal_point()

static unsigned char get_decimal_point ( void  )
static

Definition at line 278 of file cJSON.cpp.

279 {
280 #ifdef ENABLE_LOCALES
281  struct lconv *lconv = localeconv();
282  return (unsigned char) lconv->decimal_point[0];
283 #else
284  return '.';
285 #endif
286 }
struct lconv * localeconv(void)
Definition: extapi.c:1203

◆ get_object_item()

static cJSON* get_object_item ( const cJSON *const  object,
const char *const  name,
const cJSON_bool  case_sensitive 
)
static

Definition at line 1885 of file cJSON.cpp.

1886 {
1887  cJSON *current_element = NULL;
1888 
1889  if ((object == NULL) || (name == NULL))
1890  {
1891  return NULL;
1892  }
1893 
1894  current_element = object->child;
1895  if (case_sensitive)
1896  {
1897  while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
1898  {
1899  current_element = current_element->next;
1900  }
1901  }
1902  else
1903  {
1904  while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
1905  {
1906  current_element = current_element->next;
1907  }
1908  }
1909 
1910  if ((current_element == NULL) || (current_element->string == NULL))
1911  {
1912  return NULL;
1913  }
1914 
1915  return current_element;
1916 }
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
Definition: cJSON.cpp:134
const char *const name
Definition: cJSON.cpp:2086
const cJSON *const const cJSON_bool case_sensitive
Definition: cJSON.h:255

◆ if() [1/33]

if ( object->type &cJSON_String)||(object->type &cJSON_IsReference)

Definition at line 405 of file cJSON.cpp.

406  {
407  return NULL;
408  }

◆ if() [2/33]

if ( item)

Definition at line 2728 of file cJSON.cpp.

2729  {
2730  goto fail;
2731  }

◆ if() [3/33]

if ( !p.  buffer)

Definition at line 1275 of file cJSON.cpp.

1276  {
1277  return NULL;
1278  }

◆ if() [4/33]

if ( parse_valueitem, buffer_skip_whitespace(skip_utf8_bom(&buffer)))

Definition at line 1122 of file cJSON.cpp.

1123  {
1124  /* parse failure. ep is set. */
1125  goto fail;
1126  }

◆ if() [5/33]

if ( print_valueitem, &p)

Definition at line 1286 of file cJSON.cpp.

1287  {
1288  global_hooks.deallocate(p.buffer);
1289  return NULL;
1290  }

◆ if() [6/33]

if ( (count< 0)||(numbers==NULL )

Definition at line 2562 of file cJSON.cpp.

2563  {
2564  return NULL;
2565  }

◆ if() [7/33]

if ( (count< 0)||(strings==NULL )

Definition at line 2685 of file cJSON.cpp.

2686  {
2687  return NULL;
2688  }

◆ if() [8/33]

if ( (length< 0)||(buffer==NULL )

Definition at line 1299 of file cJSON.cpp.

1300  {
1301  return false;
1302  }

◆ if() [9/33]

if ( a &&a->  child)

Definition at line 2588 of file cJSON.cpp.

2589  {
2590  a->child->prev = n;
2591  }

◆ if() [10/33]

if ( add_item_to_object(object, name, array, &global_hooks, false )

Definition at line 2184 of file cJSON.cpp.

2185  {
2186  return array;
2187  }

◆ if() [11/33]

if ( add_item_to_object(object, name, bool_item, &global_hooks, false )

Definition at line 2124 of file cJSON.cpp.

2125  {
2126  return bool_item;
2127  }

◆ if() [12/33]

if ( add_item_to_object(object, name, false_item, &global_hooks, false )

Definition at line 2112 of file cJSON.cpp.

2113  {
2114  return false_item;
2115  }

◆ if() [13/33]

if ( add_item_to_object(object, name, null, &global_hooks, false )

Definition at line 2088 of file cJSON.cpp.

2089  {
2090  return null;
2091  }

◆ if() [14/33]

if ( add_item_to_object(object, name, number_item, &global_hooks, false )

Definition at line 2136 of file cJSON.cpp.

2137  {
2138  return number_item;
2139  }

◆ if() [15/33]

if ( add_item_to_object(object, name, object_item, &global_hooks, false )

Definition at line 2172 of file cJSON.cpp.

2173  {
2174  return object_item;
2175  }

◆ if() [16/33]

if ( add_item_to_object(object, name, raw_item, &global_hooks, false )

Definition at line 2160 of file cJSON.cpp.

2161  {
2162  return raw_item;
2163  }

◆ if() [17/33]

if ( add_item_to_object(object, name, string_item, &global_hooks, false )

Definition at line 2148 of file cJSON.cpp.

2149  {
2150  return string_item;
2151  }

◆ if() [18/33]

if ( add_item_to_object(object, name, true_item, &global_hooks, false )

Definition at line 2100 of file cJSON.cpp.

2101  {
2102  return true_item;
2103  }

◆ if() [19/33]

if ( after_inserted  = NULL)

Definition at line 2279 of file cJSON.cpp.

2280  {
2281  return add_item_to_array(array, newitem);
2282  }
int cJSON * newitem
Definition: cJSON.cpp:2270

◆ if() [20/33]

if ( copy  = NULL)

Definition at line 415 of file cJSON.cpp.

416  {
417  return NULL;
418  }

◆ if() [21/33]

if ( item->  valuestring)

Definition at line 2742 of file cJSON.cpp.

2743  {
2744  newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
2745  if (!newitem->valuestring)
2746  {
2747  goto fail;
2748  }
2749  }

◆ if() [22/33]

if ( item  = NULL)

Definition at line 1117 of file cJSON.cpp.

1118  {
1119  goto fail;
1120  }

◆ if() [23/33]

if ( newitem &&newitem->  child)

Definition at line 2787 of file cJSON.cpp.

2788  {
2789  newitem->child->prev = newchild;
2790  }
cJSON * newchild
Definition: cJSON.cpp:2725

◆ if() [24/33]

if ( NULL  = = value)

Definition at line 1085 of file cJSON.cpp.

1086  {
1087  return NULL;
1088  }

◆ if() [25/33]

if ( object->valuestring = NULL)

Definition at line 419 of file cJSON.cpp.

420  {
421  cJSON_free(object->valuestring);
422  }

◆ if() [26/33]

if ( parent->  child = item)

Definition at line 2317 of file cJSON.cpp.

2318  {
2319  if (parent->child->prev == parent->child)
2320  {
2322  }
2323  parent->child = replacement;
2324  }
cJSON *const cJSON * replacement
Definition: cJSON.cpp:2299

◆ if() [27/33]

if ( )

Definition at line 1269 of file cJSON.cpp.

1270  {
1271  return NULL;
1272  }

◆ if() [28/33]

if ( replacement->next = NULL)

Definition at line 2313 of file cJSON.cpp.

2314  {
2316  }

◆ if() [29/33]

Definition at line 1129 of file cJSON.cpp.

1130  {
1132  if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
1133  {
1134  goto fail;
1135  }
1136  }
static parse_buffer * buffer_skip_whitespace(parse_buffer *const buffer)
Definition: cJSON.cpp:1040

◆ if() [30/33]

if ( return_parse_end  )

Definition at line 1137 of file cJSON.cpp.

1138  {
1139  *return_parse_end = (const char*)buffer_at_offset(&buffer);
1140  }
const char ** return_parse_end
Definition: cJSON.cpp:1081

◆ if() [31/33]

if ( strlen(valuestring)<=strlen(object->valuestring )

Definition at line 409 of file cJSON.cpp.

410  {
411  strcpy(object->valuestring, valuestring);
412  return object->valuestring;
413  }
const char * valuestring
Definition: cJSON.cpp:402

◆ if() [32/33]

if ( value !  = NULL)

Definition at line 1150 of file cJSON.cpp.

1151  {
1152  error local_error;
1153  local_error.json = (const unsigned char*)value;
1154  local_error.position = 0;
1155 
1156  if (buffer.offset < buffer.length)
1157  {
1158  local_error.position = buffer.offset;
1159  }
1160  else if (buffer.length > 0)
1161  {
1162  local_error.position = buffer.length - 1;
1163  }
1164 
1165  if (return_parse_end != NULL)
1166  {
1167  *return_parse_end = (const char*)local_error.json + local_error.position;
1168  }
1169 
1170  global_error = local_error;
1171  }
Definition: cJSON.cpp:89

◆ if() [33/33]

if ( value  = NULL || 0 == buffer_length)

Definition at line 1106 of file cJSON.cpp.

1107  {
1108  goto fail;
1109  }

◆ minify_string()

static void minify_string ( char **  input,
char **  output 
)
static

Definition at line 2831 of file cJSON.cpp.

2832 {
2833  (*output)[0] = (*input)[0];
2834  *input += static_strlen("\"");
2835  *output += static_strlen("\"");
2836 
2837 
2838  for (; (*input)[0] != '\0'; (void)++(*input), ++(*output))
2839  {
2840  (*output)[0] = (*input)[0];
2841 
2842  if ((*input)[0] == '\"')
2843  {
2844  (*output)[0] = '\"';
2845  *input += static_strlen("\"");
2846  *output += static_strlen("\"");
2847  return;
2848  }
2849  else if (((*input)[0] == '\\') && ((*input)[1] == '\"'))
2850  {
2851  (*output)[1] = (*input)[1];
2852  *input += static_strlen("\"");
2853  *output += static_strlen("\"");
2854  }
2855  }
2856 }
#define static_strlen(string_literal)
Definition: cJSON.cpp:185

◆ parse_array()

static cJSON_bool parse_array ( cJSON *const  item,
parse_buffer *const  input_buffer 
)
static

Definition at line 1444 of file cJSON.cpp.

1445 {
1446  cJSON *head = NULL; /* head of the linked list */
1447  cJSON *current_item = NULL;
1448 
1449  if (input_buffer->depth >= CJSON_NESTING_LIMIT)
1450  {
1451  return false; /* to deeply nested */
1452  }
1453  input_buffer->depth++;
1454 
1455  if (buffer_at_offset(input_buffer)[0] != '[')
1456  {
1457  /* not an array */
1458  goto fail;
1459  }
1460 
1461  input_buffer->offset++;
1462  buffer_skip_whitespace(input_buffer);
1463  if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
1464  {
1465  /* empty array */
1466  goto success;
1467  }
1468 
1469  /* check if we skipped to the end of the buffer */
1470  if (cannot_access_at_index(input_buffer, 0))
1471  {
1472  input_buffer->offset--;
1473  goto fail;
1474  }
1475 
1476  /* step back to character in front of the first element */
1477  input_buffer->offset--;
1478  /* loop through the comma separated array elements */
1479  do
1480  {
1481  /* allocate next item */
1482  cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1483  if (new_item == NULL)
1484  {
1485  goto fail; /* allocation failure */
1486  }
1487 
1488  /* attach next item to list */
1489  if (head == NULL)
1490  {
1491  /* start the linked list */
1492  current_item = head = new_item;
1493  }
1494  else
1495  {
1496  /* add to the end and advance */
1497  current_item->next = new_item;
1498  new_item->prev = current_item;
1499  current_item = new_item;
1500  }
1501 
1502  /* parse next value */
1503  input_buffer->offset++;
1504  buffer_skip_whitespace(input_buffer);
1505  if (!parse_value(current_item, input_buffer))
1506  {
1507  goto fail; /* failed to parse value */
1508  }
1509  buffer_skip_whitespace(input_buffer);
1510  }
1511  while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1512 
1513  if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
1514  {
1515  goto fail; /* expected end of array */
1516  }
1517 
1518 success:
1519  input_buffer->depth--;
1520 
1521  if (head != NULL)
1522  {
1523  head->prev = current_item;
1524  }
1525 
1526  item->type = cJSON_Array;
1527  item->child = head;
1528 
1529  input_buffer->offset++;
1530 
1531  return true;
1532 
1533 fail:
1534  if (head != NULL)
1535  {
1536  cJSON_Delete(head);
1537  }
1538 
1539  return false;
1540 }
static cJSON_bool parse_value(cJSON *const item, parse_buffer *const input_buffer)
Definition: cJSON.cpp:1315
#define cJSON_Array
Definition: cJSON.h:95
#define CJSON_NESTING_LIMIT
Definition: cJSON.h:137
internal_hooks hooks
Definition: cJSON.cpp:294
size_t depth
Definition: cJSON.cpp:293
size_t offset
Definition: cJSON.cpp:292

◆ parse_hex4()

static unsigned parse_hex4 ( const unsigned char *const  input)
static

Definition at line 617 of file cJSON.cpp.

618 {
619  unsigned int h = 0;
620  size_t i = 0;
621 
622  for (i = 0; i < 4; i++)
623  {
624  /* parse digit */
625  if ((input[i] >= '0') && (input[i] <= '9'))
626  {
627  h += (unsigned int) input[i] - '0';
628  }
629  else if ((input[i] >= 'A') && (input[i] <= 'F'))
630  {
631  h += (unsigned int) 10 + input[i] - 'A';
632  }
633  else if ((input[i] >= 'a') && (input[i] <= 'f'))
634  {
635  h += (unsigned int) 10 + input[i] - 'a';
636  }
637  else /* invalid */
638  {
639  return 0;
640  }
641 
642  if (i < 3)
643  {
644  /* shift left to make place for the next nibble */
645  h = h << 4;
646  }
647  }
648 
649  return h;
650 }

◆ parse_number()

static cJSON_bool parse_number ( cJSON *const  item,
parse_buffer *const  input_buffer 
)
static

Definition at line 306 of file cJSON.cpp.

307 {
308  double number = 0;
309  unsigned char *after_end = NULL;
310  unsigned char number_c_string[64];
311  unsigned char decimal_point = get_decimal_point();
312  size_t i = 0;
313 
314  if ((input_buffer == NULL) || (input_buffer->content == NULL))
315  {
316  return false;
317  }
318 
319  /* copy the number into a temporary buffer and replace '.' with the decimal point
320  * of the current locale (for strtod)
321  * This also takes care of '\0' not necessarily being available for marking the end of the input */
322  for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
323  {
324  switch (buffer_at_offset(input_buffer)[i])
325  {
326  case '0':
327  case '1':
328  case '2':
329  case '3':
330  case '4':
331  case '5':
332  case '6':
333  case '7':
334  case '8':
335  case '9':
336  case '+':
337  case '-':
338  case 'e':
339  case 'E':
340  number_c_string[i] = buffer_at_offset(input_buffer)[i];
341  break;
342 
343  case '.':
344  number_c_string[i] = decimal_point;
345  break;
346 
347  default:
348  goto loop_end;
349  }
350  }
351 loop_end:
352  number_c_string[i] = '\0';
353 
354  number = strtod((const char*)number_c_string, (char**)&after_end);
355  if (number_c_string == after_end)
356  {
357  return false; /* parse_error */
358  }
359 
361 
362  /* use saturation in case of overflow */
363  if (number >= INT_MAX)
364  {
365  item->valueint = INT_MAX;
366  }
367  else if (number <= (double)INT_MIN)
368  {
369  item->valueint = INT_MIN;
370  }
371  else
372  {
373  item->valueint = (int)number;
374  }
375 
377 
378  input_buffer->offset += (size_t)(after_end - number_c_string);
379  return true;
380 }
double number
Definition: cJSON.cpp:384
static unsigned char get_decimal_point(void)
Definition: cJSON.cpp:278
#define cJSON_Number
Definition: cJSON.h:93
double strtod(const char *str, char **endptr)
Definition: extapi.c:941
int valueint
Definition: cJSON.h:117
const unsigned char * content
Definition: cJSON.cpp:290

◆ parse_object()

static cJSON_bool parse_object ( cJSON *const  item,
parse_buffer *const  input_buffer 
)
static

Definition at line 1605 of file cJSON.cpp.

1606 {
1607  cJSON *head = NULL; /* linked list head */
1608  cJSON *current_item = NULL;
1609 
1610  if (input_buffer->depth >= CJSON_NESTING_LIMIT)
1611  {
1612  return false; /* to deeply nested */
1613  }
1614  input_buffer->depth++;
1615 
1616  if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
1617  {
1618  goto fail; /* not an object */
1619  }
1620 
1621  input_buffer->offset++;
1622  buffer_skip_whitespace(input_buffer);
1623  if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
1624  {
1625  goto success; /* empty object */
1626  }
1627 
1628  /* check if we skipped to the end of the buffer */
1629  if (cannot_access_at_index(input_buffer, 0))
1630  {
1631  input_buffer->offset--;
1632  goto fail;
1633  }
1634 
1635  /* step back to character in front of the first element */
1636  input_buffer->offset--;
1637  /* loop through the comma separated array elements */
1638  do
1639  {
1640  /* allocate next item */
1641  cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1642  if (new_item == NULL)
1643  {
1644  goto fail; /* allocation failure */
1645  }
1646 
1647  /* attach next item to list */
1648  if (head == NULL)
1649  {
1650  /* start the linked list */
1651  current_item = head = new_item;
1652  }
1653  else
1654  {
1655  /* add to the end and advance */
1656  current_item->next = new_item;
1657  new_item->prev = current_item;
1658  current_item = new_item;
1659  }
1660 
1661  /* parse the name of the child */
1662  input_buffer->offset++;
1663  buffer_skip_whitespace(input_buffer);
1664  if (!parse_string(current_item, input_buffer))
1665  {
1666  goto fail; /* failed to parse name */
1667  }
1668  buffer_skip_whitespace(input_buffer);
1669 
1670  /* swap valuestring and string, because we parsed the name */
1671  current_item->string = current_item->valuestring;
1672  current_item->valuestring = NULL;
1673 
1674  if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
1675  {
1676  goto fail; /* invalid object */
1677  }
1678 
1679  /* parse the value */
1680  input_buffer->offset++;
1681  buffer_skip_whitespace(input_buffer);
1682  if (!parse_value(current_item, input_buffer))
1683  {
1684  goto fail; /* failed to parse value */
1685  }
1686  buffer_skip_whitespace(input_buffer);
1687  }
1688  while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1689 
1690  if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
1691  {
1692  goto fail; /* expected end of object */
1693  }
1694 
1695 success:
1696  input_buffer->depth--;
1697 
1698  if (head != NULL)
1699  {
1700  head->prev = current_item;
1701  }
1702 
1703  item->type = cJSON_Object;
1704  item->child = head;
1705 
1706  input_buffer->offset++;
1707  return true;
1708 
1709 fail:
1710  if (head != NULL)
1711  {
1712  cJSON_Delete(head);
1713  }
1714 
1715  return false;
1716 }
static cJSON_bool parse_string(cJSON *const item, parse_buffer *const input_buffer)
Definition: cJSON.cpp:775
#define cJSON_Object
Definition: cJSON.h:96

◆ parse_string()

static cJSON_bool parse_string ( cJSON *const  item,
parse_buffer *const  input_buffer 
)
static

Definition at line 775 of file cJSON.cpp.

776 {
777  const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
778  const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
779  unsigned char *output_pointer = NULL;
780  unsigned char *output = NULL;
781 
782  /* not a string */
783  if (buffer_at_offset(input_buffer)[0] != '\"')
784  {
785  goto fail;
786  }
787 
788  {
789  /* calculate approximate size of the output (overestimate) */
790  size_t allocation_length = 0;
791  size_t skipped_bytes = 0;
792  while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
793  {
794  /* is escape sequence */
795  if (input_end[0] == '\\')
796  {
797  if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
798  {
799  /* prevent buffer overflow when last input character is a backslash */
800  goto fail;
801  }
802  skipped_bytes++;
803  input_end++;
804  }
805  input_end++;
806  }
807  if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
808  {
809  goto fail; /* string ended unexpectedly */
810  }
811 
812  /* This is at most how much we need for the output */
813  allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
814  output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
815  if (output == NULL)
816  {
817  goto fail; /* allocation failure */
818  }
819  }
820 
821  output_pointer = output;
822  /* loop through the string literal */
823  while (input_pointer < input_end)
824  {
825  if (*input_pointer != '\\')
826  {
827  *output_pointer++ = *input_pointer++;
828  }
829  /* escape sequence */
830  else
831  {
832  unsigned char sequence_length = 2;
833  if ((input_end - input_pointer) < 1)
834  {
835  goto fail;
836  }
837 
838  switch (input_pointer[1])
839  {
840  case 'b':
841  *output_pointer++ = '\b';
842  break;
843  case 'f':
844  *output_pointer++ = '\f';
845  break;
846  case 'n':
847  *output_pointer++ = '\n';
848  break;
849  case 'r':
850  *output_pointer++ = '\r';
851  break;
852  case 't':
853  *output_pointer++ = '\t';
854  break;
855  case '\"':
856  case '\\':
857  case '/':
858  *output_pointer++ = input_pointer[1];
859  break;
860 
861  /* UTF-16 literal */
862  case 'u':
863  sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
864  if (sequence_length == 0)
865  {
866  /* failed to convert UTF16-literal to UTF-8 */
867  goto fail;
868  }
869  break;
870 
871  default:
872  goto fail;
873  }
874  input_pointer += sequence_length;
875  }
876  }
877 
878  /* zero terminate the output */
879  *output_pointer = '\0';
880 
882  item->valuestring = (char*)output;
883 
884  input_buffer->offset = (size_t) (input_end - input_buffer->content);
885  input_buffer->offset++;
886 
887  return true;
888 
889 fail:
890  if (output != NULL)
891  {
892  input_buffer->hooks.deallocate(output);
893  }
894 
895  if (input_pointer != NULL)
896  {
897  input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
898  }
899 
900  return false;
901 }
static unsigned char utf16_literal_to_utf8(const unsigned char *const input_pointer, const unsigned char *const input_end, unsigned char **output_pointer)
Definition: cJSON.cpp:654
#define cJSON_String
Definition: cJSON.h:94
size_t length
Definition: cJSON.cpp:291

◆ parse_value()

static cJSON_bool parse_value ( cJSON *const  item,
parse_buffer *const  input_buffer 
)
static

Definition at line 1315 of file cJSON.cpp.

1316 {
1317  if ((input_buffer == NULL) || (input_buffer->content == NULL))
1318  {
1319  return false; /* no input */
1320  }
1321 
1322  /* parse the different types of values */
1323  /* null */
1324  if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
1325  {
1326  item->type = cJSON_NULL;
1327  input_buffer->offset += 4;
1328  return true;
1329  }
1330  /* false */
1331  if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
1332  {
1333  item->type = cJSON_False;
1334  input_buffer->offset += 5;
1335  return true;
1336  }
1337  /* true */
1338  if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
1339  {
1340  item->type = cJSON_True;
1341  item->valueint = 1;
1342  input_buffer->offset += 4;
1343  return true;
1344  }
1345  /* string */
1346  if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
1347  {
1348  return parse_string(item, input_buffer);
1349  }
1350  /* number */
1351  if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
1352  {
1353  return parse_number(item, input_buffer);
1354  }
1355  /* array */
1356  if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
1357  {
1358  return parse_array(item, input_buffer);
1359  }
1360  /* object */
1361  if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
1362  {
1363  return parse_object(item, input_buffer);
1364  }
1365 
1366  return false;
1367 }
static cJSON_bool parse_object(cJSON *const item, parse_buffer *const input_buffer)
Definition: cJSON.cpp:1605
#define can_read(buffer, size)
Definition: cJSON.cpp:298
static cJSON_bool parse_array(cJSON *const item, parse_buffer *const input_buffer)
Definition: cJSON.cpp:1444
static cJSON_bool parse_number(cJSON *const item, parse_buffer *const input_buffer)
Definition: cJSON.cpp:306
#define cJSON_False
Definition: cJSON.h:90
#define cJSON_True
Definition: cJSON.h:91
#define cJSON_NULL
Definition: cJSON.h:92

◆ print()

static unsigned char* print ( const cJSON *const  item,
cJSON_bool  format,
const internal_hooks *const  hooks 
)
static

Definition at line 1189 of file cJSON.cpp.

1190 {
1191  static const size_t default_buffer_size = 256;
1192  printbuffer buffer[1];
1193  unsigned char *printed = NULL;
1194 
1195  memset(buffer, 0, sizeof(buffer));
1196 
1197  /* create buffer */
1198  buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1199  buffer->length = default_buffer_size;
1200  buffer->format = format;
1201  buffer->hooks = *hooks;
1202  if (buffer->buffer == NULL)
1203  {
1204  goto fail;
1205  }
1206 
1207  /* print the value */
1208  if (!print_value(item, buffer))
1209  {
1210  goto fail;
1211  }
1213 
1214  /* check if reallocate is available */
1215  if (hooks->reallocate != NULL)
1216  {
1217  printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1218  if (printed == NULL)
1219  {
1220  goto fail;
1221  }
1222  buffer->buffer = NULL;
1223  }
1224  else /* otherwise copy the JSON over to a new buffer */
1225  {
1226  printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1227  if (printed == NULL)
1228  {
1229  goto fail;
1230  }
1231  memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1232  printed[buffer->offset] = '\0'; /* just to be sure */
1233 
1234  /* free the buffer */
1235  hooks->deallocate(buffer->buffer);
1236  }
1237 
1238  return printed;
1239 
1240 fail:
1241  if (buffer->buffer != NULL)
1242  {
1243  hooks->deallocate(buffer->buffer);
1244  }
1245 
1246  if (printed != NULL)
1247  {
1248  hooks->deallocate(printed);
1249  }
1250 
1251  return NULL;
1252 }
#define cjson_min(a, b)
Definition: cJSON.cpp:1187
static cJSON_bool print_value(const cJSON *const item, printbuffer *const output_buffer)
Definition: cJSON.cpp:1370
p format
Definition: cJSON.cpp:1283
static void update_offset(printbuffer *const buffer)
Definition: cJSON.cpp:527

◆ print_array()

static cJSON_bool print_array ( const cJSON *const  item,
printbuffer *const  output_buffer 
)
static

Definition at line 1543 of file cJSON.cpp.

1544 {
1545  unsigned char *output_pointer = NULL;
1546  size_t length = 0;
1547  cJSON *current_element = item->child;
1548 
1549  if (output_buffer == NULL)
1550  {
1551  return false;
1552  }
1553 
1554  /* Compose the output array. */
1555  /* opening square bracket */
1556  output_pointer = ensure(output_buffer, 1);
1557  if (output_pointer == NULL)
1558  {
1559  return false;
1560  }
1561 
1562  *output_pointer = '[';
1563  output_buffer->offset++;
1564  output_buffer->depth++;
1565 
1566  while (current_element != NULL)
1567  {
1568  if (!print_value(current_element, output_buffer))
1569  {
1570  return false;
1571  }
1572  update_offset(output_buffer);
1573  if (current_element->next)
1574  {
1575  length = (size_t) (output_buffer->format ? 2 : 1);
1576  output_pointer = ensure(output_buffer, length + 1);
1577  if (output_pointer == NULL)
1578  {
1579  return false;
1580  }
1581  *output_pointer++ = ',';
1582  if(output_buffer->format)
1583  {
1584  *output_pointer++ = ' ';
1585  }
1586  *output_pointer = '\0';
1587  output_buffer->offset += length;
1588  }
1589  current_element = current_element->next;
1590  }
1591 
1592  output_pointer = ensure(output_buffer, 2);
1593  if (output_pointer == NULL)
1594  {
1595  return false;
1596  }
1597  *output_pointer++ = ']';
1598  *output_pointer = '\0';
1599  output_buffer->depth--;
1600 
1601  return true;
1602 }
static unsigned char * ensure(printbuffer *const p, size_t needed)
Definition: cJSON.cpp:440
size_t offset
Definition: cJSON.cpp:432
cJSON_bool format
Definition: cJSON.cpp:435
size_t depth
Definition: cJSON.cpp:433

◆ print_number()

static cJSON_bool print_number ( const cJSON *const  item,
printbuffer *const  output_buffer 
)
static

Definition at line 547 of file cJSON.cpp.

548 {
549  unsigned char *output_pointer = NULL;
550  double d = item->valuedouble;
551  int length = 0;
552  size_t i = 0;
553  unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */
554  unsigned char decimal_point = get_decimal_point();
555  double test = 0.0;
556 
557  if (output_buffer == NULL)
558  {
559  return false;
560  }
561 
562  /* This checks for NaN and Infinity */
563  if (isnan(d) || isinf(d))
564  {
565  length = snprintf((char*)number_buffer, sizeof(number_buffer), "null");
566  }
567  else if(d == (double)item->valueint)
568  {
569  length = snprintf((char*)number_buffer, sizeof(number_buffer), "%d", item->valueint);
570  }
571  else
572  {
573  /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
574  length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.15g", d);
575 
576  /* Check whether the original double can be recovered */
577  if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
578  {
579  /* If not, print with 17 decimal places of precision */
580  length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.17g", d);
581  }
582  }
583 
584  /* sprintf failed or buffer overrun occurred */
585  if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
586  {
587  return false;
588  }
589 
590  /* reserve appropriate space in the output */
591  output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
592  if (output_pointer == NULL)
593  {
594  return false;
595  }
596 
597  /* copy the printed number to the output and replace locale
598  * dependent decimal point with '.' */
599  for (i = 0; i < ((size_t)length); i++)
600  {
601  if (number_buffer[i] == decimal_point)
602  {
603  output_pointer[i] = '.';
604  continue;
605  }
606 
607  output_pointer[i] = number_buffer[i];
608  }
609  output_pointer[i] = '\0';
610 
611  output_buffer->offset += (size_t)length;
612 
613  return true;
614 }
#define isnan(d)
Definition: cJSON.cpp:77
static cJSON_bool compare_double(double a, double b)
Definition: cJSON.cpp:540
#define isinf(d)
Definition: cJSON.cpp:74

◆ print_object()

static cJSON_bool print_object ( const cJSON *const  item,
printbuffer *const  output_buffer 
)
static

Definition at line 1719 of file cJSON.cpp.

1720 {
1721  unsigned char *output_pointer = NULL;
1722  size_t length = 0;
1723  cJSON *current_item = item->child;
1724 
1725  if (output_buffer == NULL)
1726  {
1727  return false;
1728  }
1729 
1730  /* Compose the output: */
1731  length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
1732  output_pointer = ensure(output_buffer, length + 1);
1733  if (output_pointer == NULL)
1734  {
1735  return false;
1736  }
1737 
1738  *output_pointer++ = '{';
1739  output_buffer->depth++;
1740  if (output_buffer->format)
1741  {
1742  *output_pointer++ = '\n';
1743  }
1744  output_buffer->offset += length;
1745 
1746  while (current_item)
1747  {
1748  if (output_buffer->format)
1749  {
1750  size_t i;
1751  output_pointer = ensure(output_buffer, output_buffer->depth);
1752  if (output_pointer == NULL)
1753  {
1754  return false;
1755  }
1756  for (i = 0; i < output_buffer->depth; i++)
1757  {
1758  *output_pointer++ = '\t';
1759  }
1760  output_buffer->offset += output_buffer->depth;
1761  }
1762 
1763  /* print key */
1764  if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
1765  {
1766  return false;
1767  }
1768  update_offset(output_buffer);
1769 
1770  length = (size_t) (output_buffer->format ? 2 : 1);
1771  output_pointer = ensure(output_buffer, length);
1772  if (output_pointer == NULL)
1773  {
1774  return false;
1775  }
1776  *output_pointer++ = ':';
1777  if (output_buffer->format)
1778  {
1779  *output_pointer++ = '\t';
1780  }
1781  output_buffer->offset += length;
1782 
1783  /* print value */
1784  if (!print_value(current_item, output_buffer))
1785  {
1786  return false;
1787  }
1788  update_offset(output_buffer);
1789 
1790  /* print comma if not last */
1791  length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
1792  output_pointer = ensure(output_buffer, length + 1);
1793  if (output_pointer == NULL)
1794  {
1795  return false;
1796  }
1797  if (current_item->next)
1798  {
1799  *output_pointer++ = ',';
1800  }
1801 
1802  if (output_buffer->format)
1803  {
1804  *output_pointer++ = '\n';
1805  }
1806  *output_pointer = '\0';
1807  output_buffer->offset += length;
1808 
1809  current_item = current_item->next;
1810  }
1811 
1812  output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
1813  if (output_pointer == NULL)
1814  {
1815  return false;
1816  }
1817  if (output_buffer->format)
1818  {
1819  size_t i;
1820  for (i = 0; i < (output_buffer->depth - 1); i++)
1821  {
1822  *output_pointer++ = '\t';
1823  }
1824  }
1825  *output_pointer++ = '}';
1826  *output_pointer = '\0';
1827  output_buffer->depth--;
1828 
1829  return true;
1830 }
static cJSON_bool print_string_ptr(const unsigned char *const input, printbuffer *const output_buffer)
Definition: cJSON.cpp:904

◆ print_string()

static cJSON_bool print_string ( const cJSON *const  item,
printbuffer *const  p 
)
static

Definition at line 1026 of file cJSON.cpp.

1027 {
1028  return print_string_ptr((unsigned char*)item->valuestring, p);
1029 }

◆ print_string_ptr()

static cJSON_bool print_string_ptr ( const unsigned char *const  input,
printbuffer *const  output_buffer 
)
static

Definition at line 904 of file cJSON.cpp.

905 {
906  const unsigned char *input_pointer = NULL;
907  unsigned char *output = NULL;
908  unsigned char *output_pointer = NULL;
909  size_t output_length = 0;
910  /* numbers of additional characters needed for escaping */
911  size_t escape_characters = 0;
912 
913  if (output_buffer == NULL)
914  {
915  return false;
916  }
917 
918  /* empty string */
919  if (input == NULL)
920  {
921  output = ensure(output_buffer, sizeof("\"\""));
922  if (output == NULL)
923  {
924  return false;
925  }
926  strcpy((char*)output, "\"\"");
927 
928  return true;
929  }
930 
931  /* set "flag" to 1 if something needs to be escaped */
932  for (input_pointer = input; *input_pointer; input_pointer++)
933  {
934  switch (*input_pointer)
935  {
936  case '\"':
937  case '\\':
938  case '\b':
939  case '\f':
940  case '\n':
941  case '\r':
942  case '\t':
943  /* one character escape sequence */
944  escape_characters++;
945  break;
946  default:
947  if (*input_pointer < 32)
948  {
949  /* UTF-16 escape sequence uXXXX */
950  escape_characters += 5;
951  }
952  break;
953  }
954  }
955  output_length = (size_t)(input_pointer - input) + escape_characters;
956 
957  output = ensure(output_buffer, output_length + sizeof("\"\""));
958  if (output == NULL)
959  {
960  return false;
961  }
962 
963  /* no characters have to be escaped */
964  if (escape_characters == 0)
965  {
966  output[0] = '\"';
967  memcpy(output + 1, input, output_length);
968  output[output_length + 1] = '\"';
969  output[output_length + 2] = '\0';
970 
971  return true;
972  }
973 
974  output[0] = '\"';
975  output_pointer = output + 1;
976  /* copy the string */
977  for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
978  {
979  if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
980  {
981  /* normal character, copy */
982  *output_pointer = *input_pointer;
983  }
984  else
985  {
986  /* character needs to be escaped */
987  *output_pointer++ = '\\';
988  switch (*input_pointer)
989  {
990  case '\\':
991  *output_pointer = '\\';
992  break;
993  case '\"':
994  *output_pointer = '\"';
995  break;
996  case '\b':
997  *output_pointer = 'b';
998  break;
999  case '\f':
1000  *output_pointer = 'f';
1001  break;
1002  case '\n':
1003  *output_pointer = 'n';
1004  break;
1005  case '\r':
1006  *output_pointer = 'r';
1007  break;
1008  case '\t':
1009  *output_pointer = 't';
1010  break;
1011  default:
1012  /* escape and print as unicode codepoint */
1013  snprintf((char*)output_pointer, sizeof(output_pointer), "u%04x", *input_pointer);
1014  output_pointer += 4;
1015  break;
1016  }
1017  }
1018  }
1019  output[output_length + 1] = '\"';
1020  output[output_length + 2] = '\0';
1021 
1022  return true;
1023 }

◆ print_value() [1/2]

static cJSON_bool print_value ( const cJSON *const  item,
printbuffer *const  output_buffer 
)
static

Definition at line 1370 of file cJSON.cpp.

1371 {
1372  unsigned char *output = NULL;
1373 
1374  if ((item == NULL) || (output_buffer == NULL))
1375  {
1376  return false;
1377  }
1378 
1379  switch ((item->type) & 0xFF)
1380  {
1381  case cJSON_NULL:
1382  output = ensure(output_buffer, 5);
1383  if (output == NULL)
1384  {
1385  return false;
1386  }
1387  strcpy((char*)output, "null");
1388  return true;
1389 
1390  case cJSON_False:
1391  output = ensure(output_buffer, 6);
1392  if (output == NULL)
1393  {
1394  return false;
1395  }
1396  strcpy((char*)output, "false");
1397  return true;
1398 
1399  case cJSON_True:
1400  output = ensure(output_buffer, 5);
1401  if (output == NULL)
1402  {
1403  return false;
1404  }
1405  strcpy((char*)output, "true");
1406  return true;
1407 
1408  case cJSON_Number:
1409  return print_number(item, output_buffer);
1410 
1411  case cJSON_Raw:
1412  {
1413  size_t raw_length = 0;
1414  if (item->valuestring == NULL)
1415  {
1416  return false;
1417  }
1418 
1419  raw_length = strlen(item->valuestring) + sizeof("");
1420  output = ensure(output_buffer, raw_length);
1421  if (output == NULL)
1422  {
1423  return false;
1424  }
1425  memcpy(output, item->valuestring, raw_length);
1426  return true;
1427  }
1428 
1429  case cJSON_String:
1430  return print_string(item, output_buffer);
1431 
1432  case cJSON_Array:
1433  return print_array(item, output_buffer);
1434 
1435  case cJSON_Object:
1436  return print_object(item, output_buffer);
1437 
1438  default:
1439  return false;
1440  }
1441 }
static cJSON_bool print_object(const cJSON *const item, printbuffer *const output_buffer)
Definition: cJSON.cpp:1719
static cJSON_bool print_string(const cJSON *const item, printbuffer *const p)
Definition: cJSON.cpp:1026
static cJSON_bool print_number(const cJSON *const item, printbuffer *const output_buffer)
Definition: cJSON.cpp:547
static cJSON_bool print_array(const cJSON *const item, printbuffer *const output_buffer)
Definition: cJSON.cpp:1543
#define cJSON_Raw
Definition: cJSON.h:97

◆ print_value() [2/2]

return print_value ( item  ,
p 
)

◆ replace_item_in_object()

static cJSON_bool replace_item_in_object ( cJSON object,
const char *  string,
cJSON replacement,
cJSON_bool  case_sensitive 
)
static

Definition at line 2358 of file cJSON.cpp.

2359 {
2360  if ((replacement == NULL) || (string == NULL))
2361  {
2362  return false;
2363  }
2364 
2365  /* replace the name in the replacement */
2367  {
2368  cJSON_free(replacement->string);
2369  }
2370  replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
2371  if (replacement->string == NULL)
2372  {
2373  return false;
2374  }
2375 
2377 
2378  return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
2379 }
static cJSON * get_object_item(const cJSON *const object, const char *const name, const cJSON_bool case_sensitive)
Definition: cJSON.cpp:1885

◆ return()

return ( char *  )

◆ skip_multiline_comment()

static void skip_multiline_comment ( char **  input)
static

Definition at line 2817 of file cJSON.cpp.

2818 {
2819  *input += static_strlen("/*");
2820 
2821  for (; (*input)[0] != '\0'; ++(*input))
2822  {
2823  if (((*input)[0] == '*') && ((*input)[1] == '/'))
2824  {
2825  *input += static_strlen("*/");
2826  return;
2827  }
2828  }
2829 }

◆ skip_oneline_comment()

static void skip_oneline_comment ( char **  input)
static

Definition at line 2803 of file cJSON.cpp.

2804 {
2805  *input += static_strlen("//");
2806 
2807  for (; (*input)[0] != '\0'; ++(*input))
2808  {
2809  if ((*input)[0] == '\n')
2810  {
2811  *input += static_strlen("\n");
2812  return;
2813  }
2814  }
2815 }

◆ skip_utf8_bom()

static parse_buffer* skip_utf8_bom ( parse_buffer *const  buffer)
static

Definition at line 1066 of file cJSON.cpp.

1067 {
1068  if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
1069  {
1070  return NULL;
1071  }
1072 
1073  if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
1074  {
1075  buffer->offset += 3;
1076  }
1077 
1078  return buffer;
1079 }

◆ suffix_object()

static void suffix_object ( cJSON prev,
cJSON item 
)
static

Definition at line 1934 of file cJSON.cpp.

1935 {
1936  prev->next = item;
1937  item->prev = prev;
1938 }
newitem prev
Definition: cJSON.cpp:2285

◆ update_offset()

static void update_offset ( printbuffer *const  buffer)
static

Definition at line 527 of file cJSON.cpp.

528 {
529  const unsigned char *buffer_pointer = NULL;
530  if ((buffer == NULL) || (buffer->buffer == NULL))
531  {
532  return;
533  }
534  buffer_pointer = buffer->buffer + buffer->offset;
535 
536  buffer->offset += strlen((const char*)buffer_pointer);
537 }

◆ utf16_literal_to_utf8()

static unsigned char utf16_literal_to_utf8 ( const unsigned char *const  input_pointer,
const unsigned char *const  input_end,
unsigned char **  output_pointer 
)
static

Definition at line 654 of file cJSON.cpp.

655 {
656  long unsigned int codepoint = 0;
657  unsigned int first_code = 0;
658  const unsigned char *first_sequence = input_pointer;
659  unsigned char utf8_length = 0;
660  unsigned char utf8_position = 0;
661  unsigned char sequence_length = 0;
662  unsigned char first_byte_mark = 0;
663 
664  if ((input_end - first_sequence) < 6)
665  {
666  /* input ends unexpectedly */
667  goto fail;
668  }
669 
670  /* get the first utf16 sequence */
671  first_code = parse_hex4(first_sequence + 2);
672 
673  /* check that the code is valid */
674  if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
675  {
676  goto fail;
677  }
678 
679  /* UTF16 surrogate pair */
680  if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
681  {
682  const unsigned char *second_sequence = first_sequence + 6;
683  unsigned int second_code = 0;
684  sequence_length = 12; /* \uXXXX\uXXXX */
685 
686  if ((input_end - second_sequence) < 6)
687  {
688  /* input ends unexpectedly */
689  goto fail;
690  }
691 
692  if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
693  {
694  /* missing second half of the surrogate pair */
695  goto fail;
696  }
697 
698  /* get the second utf16 sequence */
699  second_code = parse_hex4(second_sequence + 2);
700  /* check that the code is valid */
701  if ((second_code < 0xDC00) || (second_code > 0xDFFF))
702  {
703  /* invalid second half of the surrogate pair */
704  goto fail;
705  }
706 
707 
708  /* calculate the unicode codepoint from the surrogate pair */
709  codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
710  }
711  else
712  {
713  sequence_length = 6; /* \uXXXX */
714  codepoint = first_code;
715  }
716 
717  /* encode as UTF-8
718  * takes at maximum 4 bytes to encode:
719  * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
720  if (codepoint < 0x80)
721  {
722  /* normal ascii, encoding 0xxxxxxx */
723  utf8_length = 1;
724  }
725  else if (codepoint < 0x800)
726  {
727  /* two bytes, encoding 110xxxxx 10xxxxxx */
728  utf8_length = 2;
729  first_byte_mark = 0xC0; /* 11000000 */
730  }
731  else if (codepoint < 0x10000)
732  {
733  /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
734  utf8_length = 3;
735  first_byte_mark = 0xE0; /* 11100000 */
736  }
737  else if (codepoint <= 0x10FFFF)
738  {
739  /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
740  utf8_length = 4;
741  first_byte_mark = 0xF0; /* 11110000 */
742  }
743  else
744  {
745  /* invalid unicode codepoint */
746  goto fail;
747  }
748 
749  /* encode as utf8 */
750  for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
751  {
752  /* 10xxxxxx */
753  (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
754  codepoint >>= 6;
755  }
756  /* encode first byte */
757  if (utf8_length > 1)
758  {
759  (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
760  }
761  else
762  {
763  (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
764  }
765 
766  *output_pointer += utf8_length;
767 
768  return sequence_length;
769 
770 fail:
771  return 0;
772 }
static unsigned parse_hex4(const unsigned char *const input)
Definition: cJSON.cpp:617

◆ while()

while ( child = NULL)

Definition at line 2765 of file cJSON.cpp.

2766  {
2767  newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
2768  if (!newchild)
2769  {
2770  goto fail;
2771  }
2772  if (next != NULL)
2773  {
2774  /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2775  next->next = newchild;
2776  newchild->prev = next;
2777  next = newchild;
2778  }
2779  else
2780  {
2781  /* Set newitem->child and move to it */
2782  newitem->child = newchild;
2783  next = newchild;
2784  }
2785  child = child->next;
2786  }
item next
Definition: cJSON.cpp:2224

Variable Documentation

◆ __pad0__

fail __pad0__

Definition at line 1144 of file cJSON.cpp.

◆ __pad1__

fail __pad1__

Definition at line 2794 of file cJSON.cpp.

◆ a

return a = NULL

Definition at line 2560 of file cJSON.cpp.

◆ after_inserted

after_inserted = get_array_item(array, (size_t)which)

Definition at line 2278 of file cJSON.cpp.

◆ b

const cJSON* const b

Definition at line 3008 of file cJSON.cpp.

◆ boolean

const char* const const cJSON_bool boolean
Initial value:
{
cJSON *bool_item = cJSON_CreateBool(boolean)

Definition at line 2121 of file cJSON.cpp.

◆ buffer

p buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer)

Definition at line 1274 of file cJSON.cpp.

◆ buffer_length

size_t buffer_length = strlen(value) + sizeof("")

Definition at line 1091 of file cJSON.cpp.

◆ child

child = NULL

Definition at line 2723 of file cJSON.cpp.

◆ content

buffer content = (const unsigned char*)value

Definition at line 1111 of file cJSON.cpp.

◆ copy

return copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks)

Definition at line 414 of file cJSON.cpp.

◆ count

int count
Initial value:
{
size_t i = 0

Definition at line 2555 of file cJSON.cpp.

◆ else

else
Initial value:

Definition at line 2291 of file cJSON.cpp.

◆ fmt

int cJSON_bool fmt
Initial value:
{
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }

Definition at line 1265 of file cJSON.cpp.

◆ format

p format = fmt

Definition at line 1283 of file cJSON.cpp.

◆ global_error

error global_error = { NULL, 0 }
static

Definition at line 93 of file cJSON.cpp.

◆ global_hooks

Definition at line 187 of file cJSON.cpp.

◆ hooks

p hooks = global_hooks

Definition at line 1114 of file cJSON.cpp.

◆ index

int index
Initial value:
{
if (index < 0)
{
return NULL;
}
return get_array_item(array, (size_t)index)
static cJSON * get_array_item(const cJSON *array, size_t index)
Definition: cJSON.cpp:1856

Definition at line 1875 of file cJSON.cpp.

◆ item

cJSON* const item = NULL

Definition at line 1100 of file cJSON.cpp.

◆ json

Definition at line 1103 of file cJSON.cpp.

◆ length

p length = buffer_length

Definition at line 1112 of file cJSON.cpp.

◆ n

cJSON * n = NULL

Definition at line 2558 of file cJSON.cpp.

◆ name

const char* const name
Initial value:
{
cJSON *null = cJSON_CreateNull()

Definition at line 2085 of file cJSON.cpp.

◆ newchild

cJSON* newchild = NULL

Definition at line 2725 of file cJSON.cpp.

◆ newitem

return newitem
Initial value:
{
after_inserted
Definition: cJSON.cpp:2278

Definition at line 2269 of file cJSON.cpp.

◆ next

cJSON * next = NULL

Definition at line 2224 of file cJSON.cpp.

◆ noalloc

p noalloc = false

Definition at line 1282 of file cJSON.cpp.

◆ NULL

return NULL

Definition at line 1173 of file cJSON.cpp.

◆ number

const char* const const double number
Initial value:
{
if (number >= INT_MAX)
{
object->valueint = INT_MAX;
}
else if (number <= (double)INT_MIN)
{
object->valueint = INT_MIN;
}
else
{
object->valueint = (int)number;
}
return object->valuedouble = number

Definition at line 383 of file cJSON.cpp.

◆ offset

p offset = 0

Definition at line 1113 of file cJSON.cpp.

◆ p

cJSON * p = NULL

Definition at line 2559 of file cJSON.cpp.

◆ position

global_error position = 0

Definition at line 1104 of file cJSON.cpp.

◆ prebuffer

int prebuffer

Definition at line 1265 of file cJSON.cpp.

◆ prev

item prev = after_inserted->prev

Definition at line 2285 of file cJSON.cpp.

◆ raw

const char* const const char* const raw
Initial value:
{
cJSON *raw_item = cJSON_CreateRaw(raw)
const char *const const char *const raw
Definition: cJSON.cpp:2158

Definition at line 2157 of file cJSON.cpp.

◆ recurse

cJSON_bool recurse
Initial value:

Definition at line 2720 of file cJSON.cpp.

◆ replacement

cJSON* const cJSON* replacement
Initial value:
{
if ((parent == NULL) || (replacement == NULL) || (item == NULL))
{
return false;
}
if (replacement == item)
{
return true;
}

Definition at line 2298 of file cJSON.cpp.

◆ require_null_terminated

size_t const char cJSON_bool require_null_terminated
Initial value:
{
buffer_length
Definition: cJSON.cpp:1091

Definition at line 1081 of file cJSON.cpp.

◆ return_parse_end

size_t const char** return_parse_end

Definition at line 1081 of file cJSON.cpp.

◆ string

const char* string
Initial value:
{
return get_object_item(object, string, false)

Definition at line 1918 of file cJSON.cpp.

◆ true

return true

Definition at line 2295 of file cJSON.cpp.

◆ type

newitem type = item->type & (~cJSON_IsReference)

Definition at line 2739 of file cJSON.cpp.

◆ valuedouble

newitem valuedouble = item->valuedouble

Definition at line 2741 of file cJSON.cpp.

◆ valueint

newitem valueint = item->valueint

Definition at line 2740 of file cJSON.cpp.

◆ valuestring

object valuestring
Initial value:
{
char *copy = NULL

Definition at line 401 of file cJSON.cpp.

◆ which

int which
Initial value:
{
if (which < 0)
{
return NULL;
}
return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which))
return cJSON_DetachItemViaPointer(object, to_detach)
int which
Definition: cJSON.cpp:2230

Definition at line 2229 of file cJSON.cpp.