Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong number in converting number to utf8 #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions c_src/jsonx_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ ucs_to_utf8(unsigned char* ptr, unsigned ucs){
*(ptr++) = (unsigned char) 0xC0 + (ucs >> 6);
*(ptr++) = (unsigned char) 0x80 + (ucs & 0x3F);
return ptr;
}else if(ucs < 0x1000) {
}else if(ucs < 0x10000) {
// 1110xxxx 10xyyyyy 10yyyyyy
if(ucs < 0xD800 || (ucs > 0xDFFF && ucs < 0xFFFE)) {
*(ptr++) = (unsigned char) 0xE0 + (ucs >> 12);
Expand All @@ -124,6 +124,35 @@ ucs_to_utf8(unsigned char* ptr, unsigned ucs){
return NULL;
}

static inline int
is_surrogate_start(int val){
return ((0xD800 <= val) && (val <= 0xDBFF));
}

static inline int
next_surrogate_end(unsigned char *str){
unsigned char *src = str;
src+=4;
if(*src == '\\'){
unsigned hval;
src++;
if(*src == 'u'){
src++;
if(ucs_from_4hex(src, &hval)) {
if((0xDC00 <= hval) && (hval <= 0xDFFF)){
return hval;
}
}
}
}
return 0;
}

static inline int
parse_surrogate(int start, int end){
return (((start - 0xD800) << 10) + (end - 0xDC00) + 0x0010000);
}

static inline int
check_with_unescape_jstr(unsigned char *str, unsigned char **endstr, unsigned char **endptr){
unsigned char c, k;
Expand Down Expand Up @@ -174,8 +203,13 @@ check_with_unescape_jstr(unsigned char *str, unsigned char **endstr, unsigned ch
case '\\': {src++; *dst++ = 92U; continue;}
case 'u': {
unsigned hval;
src++;
if(!ucs_from_4hex(src, &hval)) {goto error;}
src++;
if(!ucs_from_4hex(src, &hval)) {goto error;}
unsigned sur_end;
if(is_surrogate_start(hval) && (sur_end = next_surrogate_end(src))){
hval = parse_surrogate(hval, sur_end);
src += 6;
}
if(!(dst = ucs_to_utf8(dst, hval))) {goto error;}
src += 4;
continue;
Expand Down
2 changes: 2 additions & 0 deletions test/str_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ encutf4_test() ->
encutf5_test() ->
{no_match,<<248,128,128,128,128>>} = jsonx:encode(<<248, 128, 128, 128, 128>>).

decutf_test() ->
<<237,129,172,235,166,176,236,138,164,237,139,177>> = jsonx:decode(<<"\"\\ud06c\\ub9b0\\uc2a4\\ud2f1\"">>).

%% Test decode atoms
dectrue_test() ->
Expand Down