2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-29 19:35:43 +02:00
|
|
|
const Buffer = require('buffer').Buffer;
|
|
|
|
|
2012-10-05 02:43:15 +02:00
|
|
|
function assertEncoding(encoding) {
|
2015-04-28 16:53:06 +02:00
|
|
|
// Do not cache `Buffer.isEncoding`, some modules monkey-patch it to support
|
|
|
|
// additional encodings
|
|
|
|
if (encoding && !Buffer.isEncoding(encoding)) {
|
2012-10-05 02:43:15 +02:00
|
|
|
throw new Error('Unknown encoding: ' + encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:42:48 +02:00
|
|
|
// StringDecoder provides an interface for efficiently splitting a series of
|
|
|
|
// buffers into a series of JS strings without breaking apart multi-byte
|
|
|
|
// characters. CESU-8 is handled as part of the UTF-8 encoding.
|
|
|
|
//
|
|
|
|
// @TODO Handling all encodings inside a single object makes it very difficult
|
|
|
|
// to reason about this code, so it should be split up in the future.
|
|
|
|
// @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
|
|
|
|
// points as used by CESU-8.
|
2015-01-21 17:36:59 +01:00
|
|
|
const StringDecoder = exports.StringDecoder = function(encoding) {
|
2010-12-02 05:59:06 +01:00
|
|
|
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
|
2012-10-05 02:43:15 +02:00
|
|
|
assertEncoding(encoding);
|
2012-05-05 15:47:24 +02:00
|
|
|
switch (this.encoding) {
|
|
|
|
case 'utf8':
|
|
|
|
// CESU-8 represents each of Surrogate Pair by 3-bytes
|
|
|
|
this.surrogateSize = 3;
|
|
|
|
break;
|
|
|
|
case 'ucs2':
|
|
|
|
case 'utf16le':
|
|
|
|
// UTF-16 represents each of Surrogate Pair by 2-bytes
|
|
|
|
this.surrogateSize = 2;
|
|
|
|
this.detectIncompleteChar = utf16DetectIncompleteChar;
|
|
|
|
break;
|
2012-10-12 00:53:11 +02:00
|
|
|
case 'base64':
|
|
|
|
// Base-64 stores 3 bytes in 4 chars, and pads the remainder.
|
|
|
|
this.surrogateSize = 3;
|
|
|
|
this.detectIncompleteChar = base64DetectIncompleteChar;
|
|
|
|
break;
|
2012-05-05 15:47:24 +02:00
|
|
|
default:
|
|
|
|
this.write = passThroughWrite;
|
|
|
|
return;
|
2010-06-16 03:19:25 +02:00
|
|
|
}
|
2012-05-05 15:47:24 +02:00
|
|
|
|
2014-05-13 17:42:48 +02:00
|
|
|
// Enough space to store all bytes of a single character. UTF-8 needs 4
|
|
|
|
// bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
|
2016-01-26 00:00:06 +01:00
|
|
|
this.charBuffer = Buffer.allocUnsafe(6);
|
2014-05-13 17:42:48 +02:00
|
|
|
// Number of bytes received for the current incomplete multi-byte character.
|
2012-05-05 15:47:24 +02:00
|
|
|
this.charReceived = 0;
|
2014-05-13 17:42:48 +02:00
|
|
|
// Number of bytes expected for the current incomplete multi-byte character.
|
2012-05-05 15:47:24 +02:00
|
|
|
this.charLength = 0;
|
2010-05-18 04:25:51 +02:00
|
|
|
};
|
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
|
2014-05-13 17:42:48 +02:00
|
|
|
// write decodes the given buffer and returns it as JS string that is
|
|
|
|
// guaranteed to not contain any partial multi-byte characters. Any partial
|
|
|
|
// character found at the end of the buffer is buffered up, and will be
|
|
|
|
// returned when calling write again with the remaining bytes.
|
|
|
|
//
|
|
|
|
// Note: Converting a Buffer containing an orphan surrogate to a String
|
2016-04-25 04:36:57 +02:00
|
|
|
// currently works, but converting a String to a Buffer (via `Buffer.from()`,
|
|
|
|
// or Buffer#write) will replace incomplete surrogates with the unicode
|
2014-05-13 17:42:48 +02:00
|
|
|
// replacement character. See https://codereview.chromium.org/121173009/ .
|
2010-12-02 05:59:06 +01:00
|
|
|
StringDecoder.prototype.write = function(buffer) {
|
2010-05-18 04:25:51 +02:00
|
|
|
var charStr = '';
|
2015-03-19 22:31:34 +01:00
|
|
|
var buflen = buffer.length;
|
|
|
|
var charBuffer = this.charBuffer;
|
|
|
|
var charLength = this.charLength;
|
|
|
|
var charReceived = this.charReceived;
|
|
|
|
var surrogateSize = this.surrogateSize;
|
|
|
|
var encoding = this.encoding;
|
2016-02-11 16:31:47 +01:00
|
|
|
var charCode;
|
2010-05-18 04:25:51 +02:00
|
|
|
// if our last write ended with an incomplete multibyte character
|
2015-03-19 22:31:34 +01:00
|
|
|
while (charLength) {
|
2010-05-18 04:25:51 +02:00
|
|
|
// determine how many remaining bytes this buffer has to offer for this char
|
2015-03-19 22:31:34 +01:00
|
|
|
var diff = charLength - charReceived;
|
|
|
|
var available = (buflen >= diff) ? diff : buflen;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
// add the new bytes to the char buffer
|
2015-03-19 22:31:34 +01:00
|
|
|
buffer.copy(charBuffer, charReceived, 0, available);
|
|
|
|
charReceived += available;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2015-03-19 22:31:34 +01:00
|
|
|
if (charReceived < charLength) {
|
2010-05-18 04:25:51 +02:00
|
|
|
// still not enough chars in this buffer? wait for more ...
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
this.charLength = charLength;
|
|
|
|
this.charReceived = charReceived;
|
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
return '';
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
// remove bytes belonging to the current character from the buffer
|
2015-03-19 22:31:34 +01:00
|
|
|
buffer = buffer.slice(available, buflen);
|
|
|
|
buflen = buffer.length;
|
2014-05-13 17:36:40 +02:00
|
|
|
|
2010-05-18 04:25:51 +02:00
|
|
|
// get the character that was split
|
2015-03-19 22:31:34 +01:00
|
|
|
charStr = charBuffer.toString(encoding, 0, charLength);
|
2012-05-05 05:22:01 +02:00
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
|
2016-02-11 16:31:47 +01:00
|
|
|
charCode = charStr.charCodeAt(charStr.length - 1);
|
2012-05-05 15:47:24 +02:00
|
|
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
|
2015-03-19 22:31:34 +01:00
|
|
|
charLength += surrogateSize;
|
2012-05-05 15:47:24 +02:00
|
|
|
charStr = '';
|
|
|
|
continue;
|
2012-05-05 05:22:01 +02:00
|
|
|
}
|
2015-03-19 22:31:34 +01:00
|
|
|
charReceived = charLength = 0;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
// if there are no more bytes in this buffer, just emit our char
|
2015-03-19 22:31:34 +01:00
|
|
|
if (buflen === 0) {
|
|
|
|
this.charLength = charLength;
|
|
|
|
this.charReceived = charReceived;
|
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
return charStr;
|
|
|
|
}
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
// determine and set charLength / charReceived
|
2015-03-19 22:31:34 +01:00
|
|
|
if (this.detectIncompleteChar(buffer))
|
|
|
|
charLength = this.charLength;
|
|
|
|
charReceived = this.charReceived;
|
2012-05-05 15:47:24 +02:00
|
|
|
|
2015-03-19 22:31:34 +01:00
|
|
|
var end = buflen;
|
|
|
|
if (charLength) {
|
2012-05-05 15:47:24 +02:00
|
|
|
// buffer the incomplete character bytes we got
|
2015-03-19 22:31:34 +01:00
|
|
|
buffer.copy(charBuffer, 0, buflen - charReceived, end);
|
|
|
|
end -= charReceived;
|
2012-05-05 15:47:24 +02:00
|
|
|
}
|
|
|
|
|
2015-03-19 22:31:34 +01:00
|
|
|
this.charLength = charLength;
|
|
|
|
charStr += buffer.toString(encoding, 0, end);
|
2012-05-05 15:47:24 +02:00
|
|
|
|
2016-01-31 00:08:56 +01:00
|
|
|
end = charStr.length - 1;
|
2016-02-11 16:31:47 +01:00
|
|
|
charCode = charStr.charCodeAt(end);
|
2014-05-13 17:36:40 +02:00
|
|
|
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
|
2012-05-05 15:47:24 +02:00
|
|
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
|
2015-03-19 22:31:34 +01:00
|
|
|
charLength += surrogateSize;
|
|
|
|
charReceived += surrogateSize;
|
|
|
|
charBuffer.copy(charBuffer, surrogateSize, 0, surrogateSize);
|
|
|
|
buffer.copy(charBuffer, 0, 0, surrogateSize);
|
|
|
|
|
|
|
|
this.charLength = charLength;
|
|
|
|
this.charReceived = charReceived;
|
|
|
|
|
2012-05-05 15:47:24 +02:00
|
|
|
return charStr.substring(0, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
// or just emit the charStr
|
|
|
|
return charStr;
|
|
|
|
};
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2014-05-13 17:42:48 +02:00
|
|
|
// detectIncompleteChar determines if there is an incomplete UTF-8 character at
|
|
|
|
// the end of the given buffer. If so, it sets this.charLength to the byte
|
|
|
|
// length that character, and sets this.charReceived to the number of bytes
|
|
|
|
// that are available for this character.
|
2012-05-05 15:47:24 +02:00
|
|
|
StringDecoder.prototype.detectIncompleteChar = function(buffer) {
|
2015-03-19 22:31:34 +01:00
|
|
|
var buflen = buffer.length;
|
2010-05-18 04:25:51 +02:00
|
|
|
// determine how many bytes we have to check at the end of this buffer
|
2015-03-19 22:31:34 +01:00
|
|
|
var i = (buflen >= 3) ? 3 : buflen;
|
|
|
|
var newlen = false;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
// Figure out if one of the last i bytes of our buffer announces an
|
|
|
|
// incomplete char.
|
2010-05-18 04:25:51 +02:00
|
|
|
for (; i > 0; i--) {
|
2015-03-19 22:31:34 +01:00
|
|
|
var c = buffer[buflen - i];
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
// See http://en.wikipedia.org/wiki/UTF-8#Description
|
|
|
|
|
|
|
|
// 110XXXXX
|
2015-03-19 22:31:34 +01:00
|
|
|
if (i === 1 && c >> 5 === 0x06) {
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charLength = 2;
|
2015-03-19 22:31:34 +01:00
|
|
|
newlen = true;
|
2010-05-18 04:25:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1110XXXX
|
2015-03-19 22:31:34 +01:00
|
|
|
if (i <= 2 && c >> 4 === 0x0E) {
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charLength = 3;
|
2015-03-19 22:31:34 +01:00
|
|
|
newlen = true;
|
2010-05-18 04:25:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 11110XXX
|
2015-03-19 22:31:34 +01:00
|
|
|
if (i <= 3 && c >> 3 === 0x1E) {
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charLength = 4;
|
2015-03-19 22:31:34 +01:00
|
|
|
newlen = true;
|
2010-05-18 04:25:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 22:31:34 +01:00
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
this.charReceived = i;
|
2015-03-19 22:31:34 +01:00
|
|
|
|
|
|
|
return newlen;
|
2012-05-05 15:47:24 +02:00
|
|
|
};
|
2012-05-05 05:22:01 +02:00
|
|
|
|
2012-10-12 00:53:11 +02:00
|
|
|
StringDecoder.prototype.end = function(buffer) {
|
|
|
|
var res = '';
|
|
|
|
if (buffer && buffer.length)
|
|
|
|
res = this.write(buffer);
|
|
|
|
|
2015-03-19 22:31:34 +01:00
|
|
|
var charReceived = this.charReceived;
|
|
|
|
if (charReceived) {
|
|
|
|
var cr = charReceived;
|
2012-10-12 00:53:11 +02:00
|
|
|
var buf = this.charBuffer;
|
|
|
|
var enc = this.encoding;
|
2015-03-19 22:31:34 +01:00
|
|
|
res += buf.toString(enc, 0, cr);
|
2012-10-12 00:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2012-05-05 15:47:24 +02:00
|
|
|
function passThroughWrite(buffer) {
|
|
|
|
return buffer.toString(this.encoding);
|
|
|
|
}
|
2010-06-16 03:19:25 +02:00
|
|
|
|
2012-05-05 15:47:24 +02:00
|
|
|
function utf16DetectIncompleteChar(buffer) {
|
2015-03-19 22:31:34 +01:00
|
|
|
var charReceived = this.charReceived = buffer.length % 2;
|
|
|
|
this.charLength = charReceived ? 2 : 0;
|
|
|
|
return true;
|
2012-05-05 15:47:24 +02:00
|
|
|
}
|
2012-10-12 00:53:11 +02:00
|
|
|
|
|
|
|
function base64DetectIncompleteChar(buffer) {
|
2015-03-19 22:31:34 +01:00
|
|
|
var charReceived = this.charReceived = buffer.length % 3;
|
|
|
|
this.charLength = charReceived ? 3 : 0;
|
|
|
|
return true;
|
2012-10-12 00:53:11 +02:00
|
|
|
}
|