3 // Multibyte codec. In this scheme, a character is represented by 1 or more bytes.
4 // Our codec supports UTF-16 surrogates, extensions for GB18030 and unicode sequences.
5 // To save memory and loading time, we read table files only when requested.
7 exports._dbcs = DBCSCodec;
13 UNASSIGNED_NODE = new Array(0x100),
16 for (var i = 0; i < 0x100; i++)
17 UNASSIGNED_NODE[i] = UNASSIGNED;
20 // Class DBCSCodec reads and initializes mapping tables.
21 function DBCSCodec(codecOptions, iconv) {
22 this.encodingName = codecOptions.encodingName;
24 throw new Error("DBCS codec is called without the data.")
25 if (!codecOptions.table)
26 throw new Error("Encoding '" + this.encodingName + "' has no data.");
29 var mappingTable = codecOptions.table();
32 // Decode tables: MBCS -> Unicode.
34 // decodeTables is a trie, encoded as an array of arrays of integers. Internal arrays are trie nodes and all have len = 256.
35 // Trie root is decodeTables[0].
36 // Values: >= 0 -> unicode character code. can be > 0xFFFF
37 // == UNASSIGNED -> unknown/unassigned sequence.
38 // == GB18030_CODE -> this is the end of a GB18030 4-byte sequence.
39 // <= NODE_START -> index of the next node in our trie to process next byte.
40 // <= SEQ_START -> index of the start of a character code sequence, in decodeTableSeq.
41 this.decodeTables = [];
42 this.decodeTables[0] = UNASSIGNED_NODE.slice(0); // Create root node.
44 // Sometimes a MBCS char corresponds to a sequence of unicode chars. We store them as arrays of integers here.
45 this.decodeTableSeq = [];
47 // Actual mapping tables consist of chunks. Use them to fill up decode tables.
48 for (var i = 0; i < mappingTable.length; i++)
49 this._addDecodeChunk(mappingTable[i]);
51 this.defaultCharUnicode = iconv.defaultCharUnicode;
54 // Encode tables: Unicode -> DBCS.
56 // `encodeTable` is array mapping from unicode char to encoded char. All its values are integers for performance.
57 // Because it can be sparse, it is represented as array of buckets by 256 chars each. Bucket can be null.
58 // Values: >= 0 -> it is a normal char. Write the value (if <=256 then 1 byte, if <=65536 then 2 bytes, etc.).
59 // == UNASSIGNED -> no conversion found. Output a default char.
60 // <= SEQ_START -> it's an index in encodeTableSeq, see below. The character starts a sequence.
61 this.encodeTable = [];
63 // `encodeTableSeq` is used when a sequence of unicode characters is encoded as a single code. We use a tree of
64 // objects where keys correspond to characters in sequence and leafs are the encoded dbcs values. A special DEF_CHAR key
65 // means end of sequence (needed when one sequence is a strict subsequence of another).
66 // Objects are kept separately from encodeTable to increase performance.
67 this.encodeTableSeq = [];
69 // Some chars can be decoded, but need not be encoded.
70 var skipEncodeChars = {};
71 if (codecOptions.encodeSkipVals)
72 for (var i = 0; i < codecOptions.encodeSkipVals.length; i++) {
73 var val = codecOptions.encodeSkipVals[i];
74 if (typeof val === 'number')
75 skipEncodeChars[val] = true;
77 for (var j = val.from; j <= val.to; j++)
78 skipEncodeChars[j] = true;
81 // Use decode trie to recursively fill out encode tables.
82 this._fillEncodeTable(0, 0, skipEncodeChars);
84 // Add more encoding pairs when needed.
85 if (codecOptions.encodeAdd) {
86 for (var uChar in codecOptions.encodeAdd)
87 if (Object.prototype.hasOwnProperty.call(codecOptions.encodeAdd, uChar))
88 this._setEncodeChar(uChar.charCodeAt(0), codecOptions.encodeAdd[uChar]);
91 this.defCharSB = this.encodeTable[0][iconv.defaultCharSingleByte.charCodeAt(0)];
92 if (this.defCharSB === UNASSIGNED) this.defCharSB = this.encodeTable[0]['?'];
93 if (this.defCharSB === UNASSIGNED) this.defCharSB = "?".charCodeAt(0);
96 // Load & create GB18030 tables when needed.
97 if (typeof codecOptions.gb18030 === 'function') {
98 this.gb18030 = codecOptions.gb18030(); // Load GB18030 ranges.
100 // Add GB18030 decode tables.
101 var thirdByteNodeIdx = this.decodeTables.length;
102 var thirdByteNode = this.decodeTables[thirdByteNodeIdx] = UNASSIGNED_NODE.slice(0);
104 var fourthByteNodeIdx = this.decodeTables.length;
105 var fourthByteNode = this.decodeTables[fourthByteNodeIdx] = UNASSIGNED_NODE.slice(0);
107 for (var i = 0x81; i <= 0xFE; i++) {
108 var secondByteNodeIdx = NODE_START - this.decodeTables[0][i];
109 var secondByteNode = this.decodeTables[secondByteNodeIdx];
110 for (var j = 0x30; j <= 0x39; j++)
111 secondByteNode[j] = NODE_START - thirdByteNodeIdx;
113 for (var i = 0x81; i <= 0xFE; i++)
114 thirdByteNode[i] = NODE_START - fourthByteNodeIdx;
115 for (var i = 0x30; i <= 0x39; i++)
116 fourthByteNode[i] = GB18030_CODE
120 DBCSCodec.prototype.encoder = DBCSEncoder;
121 DBCSCodec.prototype.decoder = DBCSDecoder;
124 DBCSCodec.prototype._getDecodeTrieNode = function(addr) {
126 for (; addr > 0; addr >>= 8)
127 bytes.push(addr & 0xFF);
128 if (bytes.length == 0)
131 var node = this.decodeTables[0];
132 for (var i = bytes.length-1; i > 0; i--) { // Traverse nodes deeper into the trie.
133 var val = node[bytes[i]];
135 if (val == UNASSIGNED) { // Create new node.
136 node[bytes[i]] = NODE_START - this.decodeTables.length;
137 this.decodeTables.push(node = UNASSIGNED_NODE.slice(0));
139 else if (val <= NODE_START) { // Existing node.
140 node = this.decodeTables[NODE_START - val];
143 throw new Error("Overwrite byte in " + this.encodingName + ", addr: " + addr.toString(16));
149 DBCSCodec.prototype._addDecodeChunk = function(chunk) {
150 // First element of chunk is the hex mbcs code where we start.
151 var curAddr = parseInt(chunk[0], 16);
153 // Choose the decoding node where we'll write our chars.
154 var writeTable = this._getDecodeTrieNode(curAddr);
155 curAddr = curAddr & 0xFF;
157 // Write all other elements of the chunk to the table.
158 for (var k = 1; k < chunk.length; k++) {
160 if (typeof part === "string") { // String, write as-is.
161 for (var l = 0; l < part.length;) {
162 var code = part.charCodeAt(l++);
163 if (0xD800 <= code && code < 0xDC00) { // Decode surrogate
164 var codeTrail = part.charCodeAt(l++);
165 if (0xDC00 <= codeTrail && codeTrail < 0xE000)
166 writeTable[curAddr++] = 0x10000 + (code - 0xD800) * 0x400 + (codeTrail - 0xDC00);
168 throw new Error("Incorrect surrogate pair in " + this.encodingName + " at chunk " + chunk[0]);
170 else if (0x0FF0 < code && code <= 0x0FFF) { // Character sequence (our own encoding used)
171 var len = 0xFFF - code + 2;
173 for (var m = 0; m < len; m++)
174 seq.push(part.charCodeAt(l++)); // Simple variation: don't support surrogates or subsequences in seq.
176 writeTable[curAddr++] = SEQ_START - this.decodeTableSeq.length;
177 this.decodeTableSeq.push(seq);
180 writeTable[curAddr++] = code; // Basic char
183 else if (typeof part === "number") { // Integer, meaning increasing sequence starting with prev character.
184 var charCode = writeTable[curAddr - 1] + 1;
185 for (var l = 0; l < part; l++)
186 writeTable[curAddr++] = charCode++;
189 throw new Error("Incorrect type '" + typeof part + "' given in " + this.encodingName + " at chunk " + chunk[0]);
192 throw new Error("Incorrect chunk in " + this.encodingName + " at addr " + chunk[0] + ": too long" + curAddr);
196 DBCSCodec.prototype._getEncodeBucket = function(uCode) {
197 var high = uCode >> 8; // This could be > 0xFF because of astral characters.
198 if (this.encodeTable[high] === undefined)
199 this.encodeTable[high] = UNASSIGNED_NODE.slice(0); // Create bucket on demand.
200 return this.encodeTable[high];
203 DBCSCodec.prototype._setEncodeChar = function(uCode, dbcsCode) {
204 var bucket = this._getEncodeBucket(uCode);
205 var low = uCode & 0xFF;
206 if (bucket[low] <= SEQ_START)
207 this.encodeTableSeq[SEQ_START-bucket[low]][DEF_CHAR] = dbcsCode; // There's already a sequence, set a single-char subsequence of it.
208 else if (bucket[low] == UNASSIGNED)
209 bucket[low] = dbcsCode;
212 DBCSCodec.prototype._setEncodeSequence = function(seq, dbcsCode) {
214 // Get the root of character tree according to first character of the sequence.
216 var bucket = this._getEncodeBucket(uCode);
217 var low = uCode & 0xFF;
220 if (bucket[low] <= SEQ_START) {
221 // There's already a sequence with - use it.
222 node = this.encodeTableSeq[SEQ_START-bucket[low]];
225 // There was no sequence object - allocate a new one.
227 if (bucket[low] !== UNASSIGNED) node[DEF_CHAR] = bucket[low]; // If a char was set before - make it a single-char subsequence.
228 bucket[low] = SEQ_START - this.encodeTableSeq.length;
229 this.encodeTableSeq.push(node);
232 // Traverse the character tree, allocating new nodes as needed.
233 for (var j = 1; j < seq.length-1; j++) {
234 var oldVal = node[uCode];
235 if (typeof oldVal === 'object')
238 node = node[uCode] = {}
239 if (oldVal !== undefined)
240 node[DEF_CHAR] = oldVal
244 // Set the leaf to given dbcsCode.
245 uCode = seq[seq.length-1];
246 node[uCode] = dbcsCode;
249 DBCSCodec.prototype._fillEncodeTable = function(nodeIdx, prefix, skipEncodeChars) {
250 var node = this.decodeTables[nodeIdx];
251 for (var i = 0; i < 0x100; i++) {
253 var mbCode = prefix + i;
254 if (skipEncodeChars[mbCode])
258 this._setEncodeChar(uCode, mbCode);
259 else if (uCode <= NODE_START)
260 this._fillEncodeTable(NODE_START - uCode, mbCode << 8, skipEncodeChars);
261 else if (uCode <= SEQ_START)
262 this._setEncodeSequence(this.decodeTableSeq[SEQ_START - uCode], mbCode);
268 // == Encoder ==================================================================
270 function DBCSEncoder(options, codec) {
272 this.leadSurrogate = -1;
273 this.seqObj = undefined;
276 this.encodeTable = codec.encodeTable;
277 this.encodeTableSeq = codec.encodeTableSeq;
278 this.defaultCharSingleByte = codec.defCharSB;
279 this.gb18030 = codec.gb18030;
282 DBCSEncoder.prototype.write = function(str) {
283 var newBuf = new Buffer(str.length * (this.gb18030 ? 4 : 3)),
284 leadSurrogate = this.leadSurrogate,
285 seqObj = this.seqObj, nextChar = -1,
289 // 0. Get next character.
290 if (nextChar === -1) {
291 if (i == str.length) break;
292 var uCode = str.charCodeAt(i++);
295 var uCode = nextChar;
299 // 1. Handle surrogates.
300 if (0xD800 <= uCode && uCode < 0xE000) { // Char is one of surrogates.
301 if (uCode < 0xDC00) { // We've got lead surrogate.
302 if (leadSurrogate === -1) {
303 leadSurrogate = uCode;
306 leadSurrogate = uCode;
307 // Double lead surrogate found.
310 } else { // We've got trail surrogate.
311 if (leadSurrogate !== -1) {
312 uCode = 0x10000 + (leadSurrogate - 0xD800) * 0x400 + (uCode - 0xDC00);
315 // Incomplete surrogate pair - only trail surrogate found.
321 else if (leadSurrogate !== -1) {
322 // Incomplete surrogate pair - only lead surrogate found.
323 nextChar = uCode; uCode = UNASSIGNED; // Write an error, then current char.
327 // 2. Convert uCode character.
328 var dbcsCode = UNASSIGNED;
329 if (seqObj !== undefined && uCode != UNASSIGNED) { // We are in the middle of the sequence
330 var resCode = seqObj[uCode];
331 if (typeof resCode === 'object') { // Sequence continues.
335 } else if (typeof resCode == 'number') { // Sequence finished. Write it.
338 } else if (resCode == undefined) { // Current character is not part of the sequence.
340 // Try default character for this sequence
341 resCode = seqObj[DEF_CHAR];
342 if (resCode !== undefined) {
343 dbcsCode = resCode; // Found. Write it.
344 nextChar = uCode; // Current character will be written too in the next iteration.
347 // TODO: What if we have no default? (resCode == undefined)
348 // Then, we should write first char of the sequence as-is and try the rest recursively.
349 // Didn't do it for now because no encoding has this situation yet.
350 // Currently, just skip the sequence and write current char.
355 else if (uCode >= 0) { // Regular character
356 var subtable = this.encodeTable[uCode >> 8];
357 if (subtable !== undefined)
358 dbcsCode = subtable[uCode & 0xFF];
360 if (dbcsCode <= SEQ_START) { // Sequence start
361 seqObj = this.encodeTableSeq[SEQ_START-dbcsCode];
365 if (dbcsCode == UNASSIGNED && this.gb18030) {
366 // Use GB18030 algorithm to find character(s) to write.
367 var idx = findIdx(this.gb18030.uChars, uCode);
369 var dbcsCode = this.gb18030.gbChars[idx] + (uCode - this.gb18030.uChars[idx]);
370 newBuf[j++] = 0x81 + Math.floor(dbcsCode / 12600); dbcsCode = dbcsCode % 12600;
371 newBuf[j++] = 0x30 + Math.floor(dbcsCode / 1260); dbcsCode = dbcsCode % 1260;
372 newBuf[j++] = 0x81 + Math.floor(dbcsCode / 10); dbcsCode = dbcsCode % 10;
373 newBuf[j++] = 0x30 + dbcsCode;
379 // 3. Write dbcsCode character.
380 if (dbcsCode === UNASSIGNED)
381 dbcsCode = this.defaultCharSingleByte;
383 if (dbcsCode < 0x100) {
384 newBuf[j++] = dbcsCode;
386 else if (dbcsCode < 0x10000) {
387 newBuf[j++] = dbcsCode >> 8; // high byte
388 newBuf[j++] = dbcsCode & 0xFF; // low byte
391 newBuf[j++] = dbcsCode >> 16;
392 newBuf[j++] = (dbcsCode >> 8) & 0xFF;
393 newBuf[j++] = dbcsCode & 0xFF;
397 this.seqObj = seqObj;
398 this.leadSurrogate = leadSurrogate;
399 return newBuf.slice(0, j);
402 DBCSEncoder.prototype.end = function() {
403 if (this.leadSurrogate === -1 && this.seqObj === undefined)
404 return; // All clean. Most often case.
406 var newBuf = new Buffer(10), j = 0;
408 if (this.seqObj) { // We're in the sequence.
409 var dbcsCode = this.seqObj[DEF_CHAR];
410 if (dbcsCode !== undefined) { // Write beginning of the sequence.
411 if (dbcsCode < 0x100) {
412 newBuf[j++] = dbcsCode;
415 newBuf[j++] = dbcsCode >> 8; // high byte
416 newBuf[j++] = dbcsCode & 0xFF; // low byte
421 this.seqObj = undefined;
424 if (this.leadSurrogate !== -1) {
425 // Incomplete surrogate pair - only lead surrogate found.
426 newBuf[j++] = this.defaultCharSingleByte;
427 this.leadSurrogate = -1;
430 return newBuf.slice(0, j);
433 // Export for testing
434 DBCSEncoder.prototype.findIdx = findIdx;
437 // == Decoder ==================================================================
439 function DBCSDecoder(options, codec) {
442 this.prevBuf = new Buffer(0);
445 this.decodeTables = codec.decodeTables;
446 this.decodeTableSeq = codec.decodeTableSeq;
447 this.defaultCharUnicode = codec.defaultCharUnicode;
448 this.gb18030 = codec.gb18030;
451 DBCSDecoder.prototype.write = function(buf) {
452 var newBuf = new Buffer(buf.length*2),
453 nodeIdx = this.nodeIdx,
454 prevBuf = this.prevBuf, prevBufOffset = this.prevBuf.length,
455 seqStart = -this.prevBuf.length, // idx of the start of current parsed sequence.
458 if (prevBufOffset > 0) // Make prev buf overlap a little to make it easier to slice later.
459 prevBuf = Buffer.concat([prevBuf, buf.slice(0, 10)]);
461 for (var i = 0, j = 0; i < buf.length; i++) {
462 var curByte = (i >= 0) ? buf[i] : prevBuf[i + prevBufOffset];
464 // Lookup in current trie node.
465 var uCode = this.decodeTables[nodeIdx][curByte];
468 // Normal character, just use it.
470 else if (uCode === UNASSIGNED) { // Unknown char.
471 // TODO: Callback with seq.
472 //var curSeq = (seqStart >= 0) ? buf.slice(seqStart, i+1) : prevBuf.slice(seqStart + prevBufOffset, i+1 + prevBufOffset);
473 i = seqStart; // Try to parse again, after skipping first byte of the sequence ('i' will be incremented by 'for' cycle).
474 uCode = this.defaultCharUnicode.charCodeAt(0);
476 else if (uCode === GB18030_CODE) {
477 var curSeq = (seqStart >= 0) ? buf.slice(seqStart, i+1) : prevBuf.slice(seqStart + prevBufOffset, i+1 + prevBufOffset);
478 var ptr = (curSeq[0]-0x81)*12600 + (curSeq[1]-0x30)*1260 + (curSeq[2]-0x81)*10 + (curSeq[3]-0x30);
479 var idx = findIdx(this.gb18030.gbChars, ptr);
480 uCode = this.gb18030.uChars[idx] + ptr - this.gb18030.gbChars[idx];
482 else if (uCode <= NODE_START) { // Go to next trie node.
483 nodeIdx = NODE_START - uCode;
486 else if (uCode <= SEQ_START) { // Output a sequence of chars.
487 var seq = this.decodeTableSeq[SEQ_START - uCode];
488 for (var k = 0; k < seq.length - 1; k++) {
490 newBuf[j++] = uCode & 0xFF;
491 newBuf[j++] = uCode >> 8;
493 uCode = seq[seq.length-1];
496 throw new Error("iconv-lite internal error: invalid decoding table value " + uCode + " at " + nodeIdx + "/" + curByte);
498 // Write the character to buffer, handling higher planes using surrogate pair.
499 if (uCode > 0xFFFF) {
501 var uCodeLead = 0xD800 + Math.floor(uCode / 0x400);
502 newBuf[j++] = uCodeLead & 0xFF;
503 newBuf[j++] = uCodeLead >> 8;
505 uCode = 0xDC00 + uCode % 0x400;
507 newBuf[j++] = uCode & 0xFF;
508 newBuf[j++] = uCode >> 8;
511 nodeIdx = 0; seqStart = i+1;
514 this.nodeIdx = nodeIdx;
515 this.prevBuf = (seqStart >= 0) ? buf.slice(seqStart) : prevBuf.slice(seqStart + prevBufOffset);
516 return newBuf.slice(0, j).toString('ucs2');
519 DBCSDecoder.prototype.end = function() {
522 // Try to parse all remaining chars.
523 while (this.prevBuf.length > 0) {
524 // Skip 1 character in the buffer.
525 ret += this.defaultCharUnicode;
526 var buf = this.prevBuf.slice(1);
528 // Parse remaining as usual.
529 this.prevBuf = new Buffer(0);
532 ret += this.write(buf);
539 // Binary search for GB18030. Returns largest i such that table[i] <= val.
540 function findIdx(table, val) {
544 var l = 0, r = table.length;
545 while (l < r-1) { // always table[l] <= val < table[r]
546 var mid = l + Math.floor((r-l+1)/2);
547 if (table[mid] <= val)