6 Copyright (c) 2005 JSON.org
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
18 The Software shall be used for Good, not Evil.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include "utf8_decode.h"
32 Very Strict UTF-8 Decoder
34 UTF-8 is a multibyte character encoding of Unicode. A character can be
35 represented by 1-4 bytes. The bit pattern of the first byte indicates the
36 number of continuation bytes.
38 Most UTF-8 decoders tend to be lenient, attempting to recover as much
39 information as possible, even from badly encoded input. This UTF-8
40 decoder is not lenient. It will reject input which does not include
41 proper continuation bytes. It will reject aliases (or suboptimal
42 codings). It will reject surrogates. (Surrogate encoding should only be
45 Code Contination Minimum Maximum
49 1110xxxx 2 2048 65535 excluding 55296 - 57343
50 11110xxx 3 65536 1114111
56 Get the next byte. It returns UTF8_END if there are no more bytes.
59 get(json_utf8_decode
*utf8
)
62 if (utf8
->the_index
>= utf8
->the_length
) {
65 c
= utf8
->the_input
[utf8
->the_index
] & 0xFF;
72 Get the 6-bit payload of the next continuation byte.
73 Return UTF8_ERROR if it is not a contination byte.
76 cont(json_utf8_decode
*utf8
)
79 return ((c
& 0xC0) == 0x80) ? (c
& 0x3F) : UTF8_ERROR
;
84 Initialize the UTF-8 decoder. The decoder is not reentrant,
87 utf8_decode_init(json_utf8_decode
*utf8
, char p
[], int length
)
91 utf8
->the_length
= length
;
98 Get the current byte offset. This is generally used in error reporting.
101 utf8_decode_at_byte(json_utf8_decode
*utf8
)
103 return utf8
->the_byte
;
108 Get the current character offset. This is generally used in error reporting.
109 The character offset matches the byte offset if the text is strictly ASCII.
112 utf8_decode_at_character(json_utf8_decode
*utf8
)
114 return utf8
->the_char
> 0 ? utf8
->the_char
- 1 : 0;
119 Extract the next character.
120 Returns: the character (between 0 and 1114111)
121 or UTF8_END (the end)
122 or UTF8_ERROR (error)
125 utf8_decode_next(json_utf8_decode
*utf8
)
127 int c
; /* the first byte of the character */
128 int r
; /* the result */
130 if (utf8
->the_index
>= utf8
->the_length
) {
131 return utf8
->the_index
== utf8
->the_length
? UTF8_END
: UTF8_ERROR
;
133 utf8
->the_byte
= utf8
->the_index
;
137 Zero continuation (0 to 127)
139 if ((c
& 0x80) == 0) {
143 One contination (128 to 2047)
145 if ((c
& 0xE0) == 0xC0) {
150 r
= ((c
& 0x1F) << 6) | c1
;
151 return r
>= 128 ? r
: UTF8_ERROR
;
154 Two continuation (2048 to 55295 and 57344 to 65535)
156 if ((c
& 0xF0) == 0xE0) {
159 if (c1
< 0 || c2
< 0) {
162 r
= ((c
& 0x0F) << 12) | (c1
<< 6) | c2
;
163 return r
>= 2048 && (r
< 55296 || r
> 57343) ? r
: UTF8_ERROR
;
166 Three continuation (65536 to 1114111)
168 if ((c
& 0xF8) == 0xF0) {
172 if (c1
< 0 || c2
< 0 || c3
< 0) {
175 r
= ((c
& 0x0F) << 18) | (c1
<< 12) | (c2
<< 6) | c3
;
176 return r
>= 65536 && r
<= 1114111 ? r
: UTF8_ERROR
;