Skip to content

Commit 5f7fc96

Browse files
committed
[js] Decoding as utf8-c8
1 parent c14207e commit 5f7fc96

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

LICENSE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
220220
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
221221
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
222222
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
223+
224+
Portions Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
225+
226+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
227+
228+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
229+
230+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

src/vm/js/nqp-runtime/codecs.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
'use strict';
22

3-
/* This file contains code borrowed/adapated from iconv-lite */
3+
/* This file contains code borrowed/adapated from iconv-lite
4+
and http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
5+
*/
46

57
const NQPException = require('./nqp-exception.js');
68

9+
const graphemeBreaker = require('grapheme-breaker');
10+
711
function isSurrogate(unit) {
812
return false;
913
}
@@ -112,6 +116,105 @@ const latin1 = new SingleByteCodec('Latin-1', withASCII('€‚ƒ„…†‡ˆ‰Š
112116

113117
const ascii = new SingleByteCodec('ASCII', withASCII(''));
114118

119+
const UTF8_ACCEPT = 0;
120+
const UTF8_REJECT = 1;
121+
122+
const UTF8_DFA = Buffer.from([
123+
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, // 00..1f
124+
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, // 20..3f
125+
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..5f
126+
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, // 60..7f
127+
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, // 80..9f
128+
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, // a0..bf
129+
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, // c0..df
130+
0xa, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // e0..ef
131+
0xb, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // f0..ff
132+
0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
133+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
134+
1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
135+
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
136+
1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // s7..s8
137+
]);
138+
139+
140+
class Utf8C8 {
141+
constructor() {
142+
}
143+
144+
buildGraphemes(codePoints, chunks) {
145+
const graphemes = graphemeBreaker.break(String.fromCodePoint(...codePoints));
146+
for (const grapheme of graphemes) {
147+
if (grapheme === grapheme.normalize('NFC')) {
148+
chunks.push(grapheme);
149+
} else {
150+
let normalizedPrefix = 0;
151+
while (grapheme.slice(0, normalizedPrefix+1).normalize('NFC') === grapheme.slice(0, normalizedPrefix+1)) normalizedPrefix++;
152+
153+
chunks.push(grapheme.slice(0, normalizedPrefix));
154+
const buf = Buffer.from(grapheme.slice(normalizedPrefix), 'utf8');
155+
this.reject(buf, 0, buf.length, chunks);
156+
}
157+
}
158+
codePoints.length = 0;
159+
}
160+
161+
toDigit(n) {
162+
return n <= 9 ? String.fromCharCode('0'.charCodeAt(0) + n) : String.fromCharCode('A'.charCodeAt(0) + n - 10);
163+
}
164+
165+
reject(buf, start, end, chunks) {
166+
for (let i = start; i < end; i++) {
167+
chunks.push(
168+
'\u{10FFFD}x' + this.toDigit(buf[i] >> 4) + this.toDigit(buf[i] & 0x0F)
169+
);
170+
}
171+
}
172+
173+
decode(buf) {
174+
let state = UTF8_ACCEPT;
175+
let codePoint;
176+
const codePoints = [];
177+
178+
const chunks = [];
179+
180+
let accepted = -1;
181+
182+
for (let i = 0; i < buf.length; i++) {
183+
const byte = buf[i];
184+
const type = UTF8_DFA[byte];
185+
186+
codePoint = (state !== UTF8_ACCEPT) ?
187+
(byte & 0x3f) | (codePoint << 6) :
188+
(0xff >> type) & (byte);
189+
190+
state = UTF8_DFA[256 + state*16 + type];
191+
192+
if (state === UTF8_ACCEPT) {
193+
codePoints.push(codePoint);
194+
accepted = i;
195+
}
196+
197+
if (state === UTF8_REJECT) {
198+
this.buildGraphemes(codePoints, chunks);
199+
this.reject(buf, accepted+1, i+1, chunks);
200+
201+
state = UTF8_ACCEPT;
202+
accepted = i;
203+
}
204+
}
205+
206+
this.buildGraphemes(codePoints, chunks);
207+
208+
if (state !== UTF8_ACCEPT) {
209+
this.reject(buf, accepted+1, buf.length, chunks);
210+
}
211+
212+
return chunks.join('');
213+
}
214+
}
215+
216+
module.exports['utf8-c8'] = new Utf8C8;
217+
115218
module.exports['windows-1252'] = windows1252;
116219
module.exports.latin1 = latin1;
117220
module.exports.ascii = ascii;

src/vm/js/nqp-runtime/core.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,8 @@ op.isint = function(value) {
816816
return (value instanceof NQPInt) ? 1 : 0;
817817
};
818818

819-
/* HACK - utf8-c is a different encoding than utf8 */
820819
function renameEncoding(encoding) {
821-
return {'utf8-c8': 'utf8', 'utf16': 'utf16le', 'iso-8859-1': 'latin1'}[encoding] || encoding;
820+
return {'utf16': 'utf16le', 'iso-8859-1': 'latin1'}[encoding] || encoding;
822821
}
823822
exports.renameEncoding = renameEncoding;
824823

@@ -923,7 +922,7 @@ function bufferDifference(a, b) {
923922

924923
op.decode = function(buf, encoding) {
925924
let rawBuffer = toRawBuffer(buf);
926-
if (encoding === 'windows-1252') {
925+
if (encoding === 'windows-1252' || encoding === 'utf8-c8') {
927926
return codecs[encoding].decode(rawBuffer);
928927
} else if (encoding === 'utf8') {
929928
const decoded = rawBuffer.toString(renameEncoding(encoding));

0 commit comments

Comments
 (0)