3
#define unicode_escape(buffer, character) \
4
snprintf(buf, 7, "\\u%04x", (unsigned int) (character)); \
5
rb_str_buf_cat(buffer, buf, 6);
8
* Copyright 2001-2004 Unicode, Inc.
12
* This source code is provided as is by Unicode, Inc. No claims are
13
* made as to fitness for any particular purpose. No warranties of any
14
* kind are expressed or implied. The recipient agrees to determine
15
* applicability of information provided. If this file has been
16
* purchased on magnetic or optical media from Unicode, Inc., the
17
* sole remedy for any claim will be exchange of defective media
18
* within 90 days of receipt.
20
* Limitations on Rights to Redistribute This Code
22
* Unicode, Inc. hereby grants the right to freely use the information
23
* supplied in this file in the creation of products supporting the
24
* Unicode Standard, and to make copies of this file in any form
25
* for internal or external distribution as long as this notice
30
* Index into the table below with the first byte of a UTF-8 sequence to
31
* get the number of trailing bytes that are supposed to follow it.
32
* Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
33
* left as-is for anyone who may want to do such conversion, which was
34
* allowed in earlier algorithms.
36
static const char trailingBytesForUTF8[256] = {
37
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
38
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
39
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
40
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
41
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
42
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
43
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
44
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
48
* Magic values subtracted from a buffer value during UTF8 conversion.
49
* This table contains as many values as there might be trailing bytes
50
* in a UTF-8 sequence.
52
static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
53
0x03C82080UL, 0xFA082080UL, 0x82082080UL };
56
* Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
57
* into the first byte, depending on how many bytes follow. There are
58
* as many entries in this table as there are UTF-8 sequence types.
59
* (I.e., one byte sequence, two byte... etc.). Remember that sequencs
60
* for *legal* UTF-8 will be 4 or fewer bytes total.
62
static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
65
* Utility routine to tell whether a sequence of bytes is legal UTF-8.
66
* This must be called with the length pre-determined by the first byte.
67
* If not calling this from ConvertUTF8to*, then the length can be set by:
68
* length = trailingBytesForUTF8[*source]+1;
69
* and the sequence is illegal right away if there aren't that many bytes
71
* If presented with a length > 4, this returns 0. The Unicode
72
* definition of UTF-8 goes up to 4-byte sequences.
75
inline static unsigned char isLegalUTF8(const UTF8 *source, int length)
78
const UTF8 *srcptr = source+length;
81
/* Everything else falls through when "1"... */
82
case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
83
case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return 0;
84
case 2: if ((a = (*--srcptr)) > 0xBF) return 0;
87
/* no fall-through in this inner switch */
88
case 0xE0: if (a < 0xA0) return 0; break;
89
case 0xED: if (a > 0x9F) return 0; break;
90
case 0xF0: if (a < 0x90) return 0; break;
91
case 0xF4: if (a > 0x8F) return 0; break;
92
default: if (a < 0x80) return 0;
95
case 1: if (*source >= 0x80 && *source < 0xC2) return 0;
97
if (*source > 0xF4) return 0;
101
void JSON_convert_UTF8_to_JSON(VALUE buffer, VALUE string, ConversionFlags flags)
104
const UTF8* source = (UTF8 *) RSTRING_PTR(string);
105
const UTF8* sourceEnd = source + RSTRING_LEN(string);
107
while (source < sourceEnd) {
109
unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
110
if (source + extraBytesToRead >= sourceEnd) {
111
rb_raise(rb_path2class("JSON::GeneratorError"),
112
"partial character in source, but hit end");
114
if (!isLegalUTF8(source, extraBytesToRead+1)) {
115
rb_raise(rb_path2class("JSON::GeneratorError"),
116
"source sequence is illegal/malformed");
119
* The cases all fall through. See "Note A" below.
121
switch (extraBytesToRead) {
122
case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
123
case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
124
case 3: ch += *source++; ch <<= 6;
125
case 2: ch += *source++; ch <<= 6;
126
case 1: ch += *source++; ch <<= 6;
127
case 0: ch += *source++;
129
ch -= offsetsFromUTF8[extraBytesToRead];
131
if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
132
/* UTF-16 surrogate values are illegal in UTF-32 */
133
if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
134
if (flags == strictConversion) {
135
source -= (extraBytesToRead+1); /* return to the illegal value itself */
136
rb_raise(rb_path2class("JSON::GeneratorError"),
137
"source sequence is illegal/malformed");
139
unicode_escape(buffer, UNI_REPLACEMENT_CHAR);
144
rb_str_buf_cat2(buffer, "\\\"");
145
} else if (ch == '\\') {
146
rb_str_buf_cat2(buffer, "\\\\");
147
} else if (ch == '/') {
148
rb_str_buf_cat2(buffer, "\\/");
149
} else if (ch >= 0x20 && ch <= 0x7f) {
150
rb_str_buf_cat(buffer, (char *) source - 1, 1);
151
} else if (ch == '\n') {
152
rb_str_buf_cat2(buffer, "\\n");
153
} else if (ch == '\r') {
154
rb_str_buf_cat2(buffer, "\\r");
155
} else if (ch == '\t') {
156
rb_str_buf_cat2(buffer, "\\t");
157
} else if (ch == '\f') {
158
rb_str_buf_cat2(buffer, "\\f");
159
} else if (ch == '\b') {
160
rb_str_buf_cat2(buffer, "\\b");
161
} else if (ch < 0x20) {
162
unicode_escape(buffer, (UTF16) ch);
164
unicode_escape(buffer, (UTF16) ch);
167
} else if (ch > UNI_MAX_UTF16) {
168
if (flags == strictConversion) {
169
source -= (extraBytesToRead+1); /* return to the start */
170
rb_raise(rb_path2class("JSON::GeneratorError"),
171
"source sequence is illegal/malformed");
173
unicode_escape(buffer, UNI_REPLACEMENT_CHAR);
176
/* target is a character in range 0xFFFF - 0x10FFFF. */
178
unicode_escape(buffer, (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START));
179
unicode_escape(buffer, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));