Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
asterisk
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Voice
asterisk
Commits
5b70ae04
Commit
5b70ae04
authored
8 years ago
by
zuul
Committed by
Gerrit Code Review
8 years ago
Browse files
Options
Downloads
Plain Diff
Merge "json: Add UTF-8 check call."
parents
5acac798
1c4c6c08
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/asterisk/json.h
+35
-0
35 additions, 0 deletions
include/asterisk/json.h
main/json.c
+121
-0
121 additions, 0 deletions
main/json.c
with
156 additions
and
0 deletions
include/asterisk/json.h
+
35
−
0
View file @
5b70ae04
...
@@ -216,6 +216,41 @@ const char *ast_json_typename(enum ast_json_type type);
...
@@ -216,6 +216,41 @@ const char *ast_json_typename(enum ast_json_type type);
/*!@{*/
/*!@{*/
/*!
* \brief Check the string of the given length for UTF-8 format.
* \since 13.12.0
*
* \param str String to check.
* \param len Length of string to check.
*
* \retval 0 if not UTF-8 encoded or str is NULL.
* \retval 1 if UTF-8 encoded.
*/
int
ast_json_utf8_check_len
(
const
char
*
str
,
size_t
len
);
/*!
* \brief Check the nul terminated string for UTF-8 format.
* \since 13.12.0
*
* \param str String to check.
*
* \retval 0 if not UTF-8 encoded or str is NULL.
* \retval 1 if UTF-8 encoded.
*/
int
ast_json_utf8_check
(
const
char
*
str
);
/*!
* \brief Check str for UTF-8 and replace with an empty string if fails the check.
*
* \note The convenience macro is normally used with ast_json_pack()
* or a function wrapper that calls ast_json_vpack().
*/
#define AST_JSON_UTF8_VALIDATE(str) (ast_json_utf8_check(str) ? (str) : "")
/*!@}*/
/*!@{*/
/*!
/*!
* \brief Get the JSON true value.
* \brief Get the JSON true value.
* \since 12.0.0
* \since 12.0.0
...
...
This diff is collapsed.
Click to expand it.
main/json.c
+
121
−
0
View file @
5b70ae04
...
@@ -269,6 +269,127 @@ const char *ast_json_typename(enum ast_json_type type)
...
@@ -269,6 +269,127 @@ const char *ast_json_typename(enum ast_json_type type)
return
"?"
;
return
"?"
;
}
}
/* Ported from libjansson utf.c:utf8_check_first() */
static
size_t
json_utf8_check_first
(
char
byte
)
{
unsigned
char
ch
=
(
unsigned
char
)
byte
;
if
(
ch
<
0x80
)
{
return
1
;
}
if
(
0x80
<=
ch
&&
ch
<=
0xBF
)
{
/* second, third or fourth byte of a multi-byte
sequence, i.e. a "continuation byte" */
return
0
;
}
else
if
(
ch
==
0xC0
||
ch
==
0xC1
)
{
/* overlong encoding of an ASCII byte */
return
0
;
}
else
if
(
0xC2
<=
ch
&&
ch
<=
0xDF
)
{
/* 2-byte sequence */
return
2
;
}
else
if
(
0xE0
<=
ch
&&
ch
<=
0xEF
)
{
/* 3-byte sequence */
return
3
;
}
else
if
(
0xF0
<=
ch
&&
ch
<=
0xF4
)
{
/* 4-byte sequence */
return
4
;
}
else
{
/* ch >= 0xF5 */
/* Restricted (start of 4-, 5- or 6-byte sequence) or invalid
UTF-8 */
return
0
;
}
}
/* Ported from libjansson utf.c:utf8_check_full() */
static
size_t
json_utf8_check_full
(
const
char
*
str
,
size_t
len
)
{
size_t
pos
;
int32_t
value
;
unsigned
char
ch
=
(
unsigned
char
)
str
[
0
];
if
(
len
==
2
)
{
value
=
ch
&
0x1F
;
}
else
if
(
len
==
3
)
{
value
=
ch
&
0xF
;
}
else
if
(
len
==
4
)
{
value
=
ch
&
0x7
;
}
else
{
return
0
;
}
for
(
pos
=
1
;
pos
<
len
;
++
pos
)
{
ch
=
(
unsigned
char
)
str
[
pos
];
if
(
ch
<
0x80
||
ch
>
0xBF
)
{
/* not a continuation byte */
return
0
;
}
value
=
(
value
<<
6
)
+
(
ch
&
0x3F
);
}
if
(
value
>
0x10FFFF
)
{
/* not in Unicode range */
return
0
;
}
else
if
(
0xD800
<=
value
&&
value
<=
0xDFFF
)
{
/* invalid code point (UTF-16 surrogate halves) */
return
0
;
}
else
if
((
len
==
2
&&
value
<
0x80
)
||
(
len
==
3
&&
value
<
0x800
)
||
(
len
==
4
&&
value
<
0x10000
))
{
/* overlong encoding */
return
0
;
}
return
1
;
}
int
ast_json_utf8_check_len
(
const
char
*
str
,
size_t
len
)
{
size_t
pos
;
size_t
count
;
int
res
=
1
;
if
(
!
str
)
{
return
0
;
}
/*
* Since the json library does not make the check function
* public we recreate/copy the function in our interface
* module.
*
* Loop ported from libjansson utf.c:utf8_check_string()
*/
for
(
pos
=
0
;
pos
<
len
;
pos
+=
count
)
{
count
=
json_utf8_check_first
(
str
[
pos
]);
if
(
count
==
0
)
{
res
=
0
;
break
;
}
else
if
(
count
>
1
)
{
if
(
count
>
len
-
pos
)
{
/* UTF-8 needs more than we have left in the string. */
res
=
0
;
break
;
}
if
(
!
json_utf8_check_full
(
&
str
[
pos
],
count
))
{
res
=
0
;
break
;
}
}
}
if
(
!
res
)
{
ast_debug
(
1
,
"String '%.*s' is not UTF-8 for json conversion
\n
"
,
(
int
)
len
,
str
);
}
return
res
;
}
int
ast_json_utf8_check
(
const
char
*
str
)
{
return
str
?
ast_json_utf8_check_len
(
str
,
strlen
(
str
))
:
0
;
}
struct
ast_json
*
ast_json_true
(
void
)
struct
ast_json
*
ast_json_true
(
void
)
{
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment