test utf8

This commit is contained in:
dota17
2020-01-08 19:42:05 +08:00
parent e651e96a5b
commit eca74dcccf
4 changed files with 111 additions and 3 deletions

View File

@@ -83,6 +83,7 @@ static const char* json_tokener_errors[] = {
"object value separator ',' expected",
"invalid string sequence",
"expected comment",
"invalid utf-8 string",
"buffer size overflow"
};
@@ -222,8 +223,12 @@ struct json_object* json_tokener_parse_verbose(const char *str,
: \
(((tok)->err = json_tokener_continue), 0) \
) : \
(((dest) = *str), 1) \
)
(((tok->flags & JSON_TOKENER_STRICT) && \
(!json_tokener_validate_utf8(*str, nBytesp)))? \
((tok->err = json_tokener_error_parse_utf8_string), 0) \
: \
(((dest) = *str), 1) \
))
/* ADVANCE_CHAR() macro:
* Increments str & tok->char_offset.
@@ -242,6 +247,9 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
{
struct json_object *obj = NULL;
char c = '\1';
unsigned int nBytes = 0;
unsigned int *nBytesp = &nBytes;
#ifdef HAVE_USELOCALE
locale_t oldlocale = uselocale(NULL);
locale_t newloc;
@@ -948,6 +956,10 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
} /* while(PEEK_CHAR) */
out:
if ((tok->flags & JSON_TOKENER_STRICT) && (nBytes != 0))
{
tok->err = json_tokener_error_parse_utf8_string;
}
if (c &&
(state == json_tokener_state_finish) &&
(tok->depth == 0) &&
@@ -985,6 +997,37 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
return NULL;
}
json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes)
{
unsigned char chr = c;
if (*nBytes == 0)
{
if (chr >= 0x80)
{
if(chr >= 0xFC && chr <= 0xFd)
*nBytes = 6;
else if (chr >= 0xF8)
*nBytes = 5;
else if (chr >= 0xF0)
*nBytes = 4;
else if (chr >= 0xE0)
*nBytes = 3;
else if (chr >= 0xC0)
*nBytes = 2;
else
return 0;
(*nBytes)--;
}
}
else
{
if ((chr & 0xC0) != 0x80)
return 0;
(*nBytes)--;
}
return 1;
}
void json_tokener_set_flags(struct json_tokener *tok, int flags)
{
tok->flags = flags;