|  | @@ -120,6 +120,7 @@ typedef struct internal_configuration
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  |      size_t buffer_size;
 |  |      size_t buffer_size;
 | 
											
												
													
														|  |      cJSON_bool format;
 |  |      cJSON_bool format;
 | 
											
												
													
														|  | 
 |  | +    cJSON_bool allow_data_after_json;
 | 
											
												
													
														|  |      void *(*allocate)(size_t size);
 |  |      void *(*allocate)(size_t size);
 | 
											
												
													
														|  |      void (*deallocate)(void *pointer);
 |  |      void (*deallocate)(void *pointer);
 | 
											
												
													
														|  |      void *(*reallocate)(void *pointer, size_t size);
 |  |      void *(*reallocate)(void *pointer, size_t size);
 | 
											
										
											
												
													
														|  | @@ -148,6 +149,7 @@ static void *internal_realloc(void *pointer, size_t size)
 | 
											
												
													
														|  |  static internal_configuration global_configuration = {
 |  |  static internal_configuration global_configuration = {
 | 
											
												
													
														|  |      256, /* default buffer size */
 |  |      256, /* default buffer size */
 | 
											
												
													
														|  |      true, /* enable formatting by default */
 |  |      true, /* enable formatting by default */
 | 
											
												
													
														|  | 
 |  | +    true, /* allow data after the JSON by default */
 | 
											
												
													
														|  |      internal_malloc,
 |  |      internal_malloc,
 | 
											
												
													
														|  |      internal_free,
 |  |      internal_free,
 | 
											
												
													
														|  |      internal_realloc
 |  |      internal_realloc
 | 
											
										
											
												
													
														|  | @@ -1005,39 +1007,38 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  /* Parse an object - create a new root, and populate. */
 |  |  /* Parse an object - create a new root, and populate. */
 | 
											
												
													
														|  | -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
 |  | 
 | 
											
												
													
														|  | 
 |  | +static cJSON *parse(const char * const json, const internal_configuration * const configuration, size_t *end_position)
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  | -    parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
 |  | 
 | 
											
												
													
														|  | 
 |  | +    parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } };
 | 
											
												
													
														|  |      cJSON *item = NULL;
 |  |      cJSON *item = NULL;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    /* reset error position */
 |  | 
 | 
											
												
													
														|  | 
 |  | +    /* reset global error position */
 | 
											
												
													
														|  |      global_error.json = NULL;
 |  |      global_error.json = NULL;
 | 
											
												
													
														|  |      global_error.position = 0;
 |  |      global_error.position = 0;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    if (value == NULL)
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (json == NULL)
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  |          goto fail;
 |  |          goto fail;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    buffer.content = (const unsigned char*)value;
 |  | 
 | 
											
												
													
														|  | -    buffer.length = strlen(value) + sizeof("");
 |  | 
 | 
											
												
													
														|  | 
 |  | +    buffer.content = (const unsigned char*)json;
 | 
											
												
													
														|  | 
 |  | +    buffer.length = strlen(json) + sizeof("");
 | 
											
												
													
														|  |      buffer.offset = 0;
 |  |      buffer.offset = 0;
 | 
											
												
													
														|  | -    buffer.configuration = global_configuration;
 |  | 
 | 
											
												
													
														|  | 
 |  | +    buffer.configuration = *configuration;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    item = create_item(&global_configuration);
 |  | 
 | 
											
												
													
														|  | -    if (item == NULL) /* memory fail */
 |  | 
 | 
											
												
													
														|  | 
 |  | +    item = create_item(configuration);
 | 
											
												
													
														|  | 
 |  | +    if (item == NULL)
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  |          goto fail;
 |  |          goto fail;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
 |  |      if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  | -        /* parse failure. ep is set. */
 |  | 
 | 
											
												
													
														|  | 
 |  | +        /* parse failure. error position is set. */
 | 
											
												
													
														|  |          goto fail;
 |  |          goto fail;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
 |  | 
 | 
											
												
													
														|  | -    if (require_null_terminated)
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (!configuration->allow_data_after_json)
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  |          buffer_skip_whitespace(&buffer);
 |  |          buffer_skip_whitespace(&buffer);
 | 
											
												
													
														|  |          if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
 |  |          if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
 | 
											
										
											
												
													
														|  | @@ -1045,23 +1046,20 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
 | 
											
												
													
														|  |              goto fail;
 |  |              goto fail;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  | -    if (return_parse_end)
 |  | 
 | 
											
												
													
														|  | -    {
 |  | 
 | 
											
												
													
														|  | -        *return_parse_end = (const char*)buffer_at_offset(&buffer);
 |  | 
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | 
 |  | +    *end_position = buffer.offset;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      return item;
 |  |      return item;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  fail:
 |  |  fail:
 | 
											
												
													
														|  |      if (item != NULL)
 |  |      if (item != NULL)
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  | -        delete_item(item, &global_configuration);
 |  | 
 | 
											
												
													
														|  | 
 |  | +        delete_item(item, configuration);
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    if (value != NULL)
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (json != NULL)
 | 
											
												
													
														|  |      {
 |  |      {
 | 
											
												
													
														|  |          error local_error;
 |  |          error local_error;
 | 
											
												
													
														|  | -        local_error.json = (const unsigned char*)value;
 |  | 
 | 
											
												
													
														|  | 
 |  | +        local_error.json = (const unsigned char*)json;
 | 
											
												
													
														|  |          local_error.position = 0;
 |  |          local_error.position = 0;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |          if (buffer.offset < buffer.length)
 |  |          if (buffer.offset < buffer.length)
 | 
											
										
											
												
													
														|  | @@ -1073,21 +1071,36 @@ fail:
 | 
											
												
													
														|  |              local_error.position = buffer.length - 1;
 |  |              local_error.position = buffer.length - 1;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        if (return_parse_end != NULL)
 |  | 
 | 
											
												
													
														|  | -        {
 |  | 
 | 
											
												
													
														|  | -            *return_parse_end = (const char*)local_error.json + local_error.position;
 |  | 
 | 
											
												
													
														|  | -        }
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | 
 |  | +        *end_position = local_error.position;
 | 
											
												
													
														|  |          global_error = local_error;
 |  |          global_error = local_error;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      return NULL;
 |  |      return NULL;
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +/* Parse an object - create a new root, and populate. */
 | 
											
												
													
														|  | 
 |  | +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated)
 | 
											
												
													
														|  | 
 |  | +{
 | 
											
												
													
														|  | 
 |  | +    size_t end_position = 0;
 | 
											
												
													
														|  | 
 |  | +    internal_configuration configuration = global_configuration;
 | 
											
												
													
														|  | 
 |  | +    cJSON *item = NULL;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    configuration.allow_data_after_json = !require_null_terminated;
 | 
											
												
													
														|  | 
 |  | +    item = parse(json, &configuration, &end_position);
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    if (return_parse_end != NULL)
 | 
											
												
													
														|  | 
 |  | +    {
 | 
											
												
													
														|  | 
 |  | +        *return_parse_end = json + end_position;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    return item;
 | 
											
												
													
														|  | 
 |  | +}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  /* Default options for cJSON_Parse */
 |  |  /* Default options for cJSON_Parse */
 | 
											
												
													
														|  | -CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
 |  | 
 | 
											
												
													
														|  | 
 |  | +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *json)
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  | -    return cJSON_ParseWithOpts(value, 0, 0);
 |  | 
 | 
											
												
													
														|  | 
 |  | +    size_t end_position = 0;
 | 
											
												
													
														|  | 
 |  | +    return parse(json, &global_configuration, &end_position);
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
 |  |  #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
 | 
											
										
											
												
													
														|  | @@ -1186,7 +1199,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
 |  |  CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
 | 
											
												
													
														|  |  {
 |  |  {
 | 
											
												
													
														|  | -    printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
 |  | 
 | 
											
												
													
														|  | 
 |  | +    printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } };
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      if ((length < 0) || (buffer == NULL))
 |  |      if ((length < 0) || (buffer == NULL))
 | 
											
												
													
														|  |      {
 |  |      {
 |