cJSON_Parse{,WithOpts}: Skip UTF-8 (Byte Order Marks)
This commit is contained in:
19
cJSON.c
19
cJSON.c
@@ -958,6 +958,22 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
|
||||
static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
|
||||
{
|
||||
if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
|
||||
{
|
||||
buffer->offset += 3;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
@@ -984,7 +1000,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!parse_value(item, buffer_skip_whitespace(&buffer)))
|
||||
if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
|
||||
{
|
||||
/* parse failure. ep is set. */
|
||||
goto fail;
|
||||
@@ -1222,7 +1238,6 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf
|
||||
return parse_object(item, input_buffer);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user