@@ -0,0 +1,150 @@
9+static const uint8_t utf8d[] = {
10+ // The first part of the table maps bytes to character classes that
11+ // to reduce the size of the transition table and create bitmasks.
12+ 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,
13+ 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,
14+ 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,
15+ 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,
16+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
17+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
18+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
19+ 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
21+ // The second part is a transition table that maps a combination
22+ // of a state of the automaton and a character class to a state.
23+ 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
24+ 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
25+ 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
26+ 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
27+ 12,36,12,12,12,12,12,12,12,12,12,12,
30+static inline uint32_t decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
31+ uint32_t type = utf8d[byte];
33+ *codep = (*state != UTF8_ACCEPT) ?
34+ (byte & 0x3fu) | (*codep << 6) :
35+ (0xff >> type) & (byte);
37+ *state = utf8d[256 + *state + type];
41+static inline uint16_t decode_hex(uint32_t c)
43+ if (c >= '0' && c <= '9') return c - '0';
44+ else if (c >= 'a' && c <= 'f') return c - 'a' + 10;
45+ else if (c >= 'A' && c <= 'F') return c - 'A' + 10;
46+ return 0xFFFF; // Should not happen
49+// Decode, return non-zero value on error
50+int _js_decode_string(uint16_t *const dest, size_t *destoff,
51+ const uint8_t *s, const uint8_t *const srcend)
53+ uint16_t *d = dest + *destoff;
57+ uint8_t surrogate = 0;
58+ uint16_t temp_hex = 0;
61+ // Optimized version of dispatch when just an ASCII char is expected
62+ #define DISPATCH_ASCII(label) {\
71+ // Test end of stream
75+ else if (decode(&state, &codepoint, *s++) != UTF8_ACCEPT) {
76+ if (state == UTF8_REJECT) { return -1; }
80+ if (codepoint == '\\')
81+ DISPATCH_ASCII(backslash)
82+ else if (codepoint <= 0xffff)
83+ *d++ = (uint16_t) codepoint;
85+ *d++ = (uint16_t) (0xD7C0 + (codepoint >> 10));
86+ *d++ = (uint16_t) (0xDC00 + (codepoint & 0x3FF));
91+ return (state != UTF8_ACCEPT);
97+ *d++ = (uint16_t) codepoint;
100+ case 'b': *d++ = '\b';goto standard;
101+ case 'f': *d++ = '\f';goto standard;
102+ case 'n': *d++ = '\n';goto standard;
103+ case 'r': *d++ = '\r';goto standard;
104+ case 't': *d++ = '\t';goto standard;
105+ case 'u': DISPATCH_ASCII(unicode1);;break;
110+ temp_hex = decode_hex(codepoint);
111+ if (temp_hex == 0xFFFF) { return -1; }
112+ else unidata = temp_hex << 12;
113+ DISPATCH_ASCII(unicode2);
115+ temp_hex = decode_hex(codepoint);
116+ if (temp_hex == 0xFFFF) { return -1; }
117+ else unidata |= temp_hex << 8;
118+ DISPATCH_ASCII(unicode3);
120+ temp_hex = decode_hex(codepoint);
121+ if (temp_hex == 0xFFFF) { return -1; }
122+ else unidata |= temp_hex << 4;
123+ DISPATCH_ASCII(unicode4);
125+ temp_hex = decode_hex(codepoint);
126+ if (temp_hex == 0xFFFF) { return -1; }
127+ else unidata |= temp_hex;
128+ *d++ = (uint16_t) unidata;
131+ if (unidata <= 0xDC00 || unidata >= 0xDFFF) // is not low surrogate
134+ } else if (unidata >= 0xD800 && unidata <= 0xDBFF ) { // is high surrogate
136+ DISPATCH_ASCII(surrogate1);
137+ } else if (unidata >= 0xDC00 && unidata <= 0xDFFF) { // is low surrogate
142+ if (codepoint != '\\')
144+ DISPATCH_ASCII(surrogate2)
146+ if (codepoint != 'u')
148+ DISPATCH_ASCII(unicode1)