Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
5baa77f8
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
大约 1 年 前同步成功
通知
6
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5baa77f8
编写于
7月 03, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cJSON_Parse{,WithOpts}: Skip UTF-8 (Byte Order Marks)
上级
b26e71f9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
2 deletion
+60
-2
cJSON.c
cJSON.c
+17
-2
tests/misc_tests.c
tests/misc_tests.c
+26
-0
tests/parse_with_opts.c
tests/parse_with_opts.c
+17
-0
未找到文件。
cJSON.c
浏览文件 @
5baa77f8
...
...
@@ -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
;
}
...
...
tests/misc_tests.c
浏览文件 @
5baa77f8
...
...
@@ -410,6 +410,30 @@ static void cjson_functions_shouldnt_crash_with_null_pointers(void)
cJSON_Delete
(
item
);
}
static
void
skip_utf8_bom_should_skip_bom
(
void
)
{
const
unsigned
char
string
[]
=
"
\xEF\xBB\xBF
{}"
;
parse_buffer
buffer
=
{
0
,
0
,
0
,
0
,
{
0
,
0
,
0
}
};
buffer
.
content
=
string
;
buffer
.
length
=
sizeof
(
string
);
buffer
.
hooks
=
global_hooks
;
TEST_ASSERT_TRUE
(
skip_utf8_bom
(
&
buffer
)
==
&
buffer
);
TEST_ASSERT_EQUAL_UINT
(
3U
,
(
unsigned
int
)
buffer
.
offset
);
}
static
void
skip_utf8_bom_should_not_skip_bom_if_not_at_beginning
(
void
)
{
const
unsigned
char
string
[]
=
"
\xEF\xBB\xBF
{}"
;
parse_buffer
buffer
=
{
0
,
0
,
0
,
0
,
{
0
,
0
,
0
}
};
buffer
.
content
=
string
;
buffer
.
length
=
sizeof
(
string
);
buffer
.
hooks
=
global_hooks
;
buffer
.
offset
=
1
;
TEST_ASSERT_NULL
(
skip_utf8_bom
(
&
buffer
));
}
int
main
(
void
)
{
UNITY_BEGIN
();
...
...
@@ -425,6 +449,8 @@ int main(void)
RUN_TEST
(
cjson_replace_item_via_pointer_should_replace_items
);
RUN_TEST
(
cjson_replace_item_in_object_should_preserve_name
);
RUN_TEST
(
cjson_functions_shouldnt_crash_with_null_pointers
);
RUN_TEST
(
skip_utf8_bom_should_skip_bom
);
RUN_TEST
(
skip_utf8_bom_should_not_skip_bom_if_not_at_beginning
);
return
UNITY_END
();
}
tests/parse_with_opts.c
浏览文件 @
5baa77f8
...
...
@@ -69,6 +69,22 @@ static void parse_with_opts_should_return_parse_end(void)
cJSON_Delete
(
item
);
}
static
void
parse_with_opts_should_parse_utf8_bom
(
void
)
{
cJSON
*
with_bom
=
NULL
;
cJSON
*
without_bom
=
NULL
;
with_bom
=
cJSON_ParseWithOpts
(
"
\xEF\xBB\xBF
{}"
,
NULL
,
true
);
TEST_ASSERT_NOT_NULL
(
with_bom
);
without_bom
=
cJSON_ParseWithOpts
(
"{}"
,
NULL
,
true
);
TEST_ASSERT_NOT_NULL
(
with_bom
);
TEST_ASSERT_TRUE
(
cJSON_Compare
(
with_bom
,
without_bom
,
true
));
cJSON_Delete
(
with_bom
);
cJSON_Delete
(
without_bom
);
}
int
main
(
void
)
{
UNITY_BEGIN
();
...
...
@@ -77,6 +93,7 @@ int main(void)
RUN_TEST
(
parse_with_opts_should_handle_empty_strings
);
RUN_TEST
(
parse_with_opts_should_require_null_if_requested
);
RUN_TEST
(
parse_with_opts_should_return_parse_end
);
RUN_TEST
(
parse_with_opts_should_parse_utf8_bom
);
return
UNITY_END
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录