2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2012-10-05 02:43:15 +02:00
|
|
|
function assertEncoding(encoding) {
|
|
|
|
if (encoding && !Buffer.isEncoding(encoding)) {
|
|
|
|
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.
|
2010-12-02 05:59:06 +01:00
|
|
|
var StringDecoder = exports.StringDecoder = function(encoding) {
|
|
|
|
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).
|
2012-05-05 15:47:24 +02:00
|
|
|
this.charBuffer = new Buffer(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
|
|
|
|
// currently works, but converting a String to a Buffer (via `new Buffer`, or
|
|
|
|
// Buffer#write) will replace incomplete surrogates with the unicode
|
|
|
|
// 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 = '';
|
|
|
|
// if our last write ended with an incomplete multibyte character
|
2012-05-05 05:22:01 +02:00
|
|
|
while (this.charLength) {
|
2010-05-18 04:25:51 +02:00
|
|
|
// determine how many remaining bytes this buffer has to offer for this char
|
2014-05-13 17:36:40 +02:00
|
|
|
var available = (buffer.length >= this.charLength - this.charReceived) ?
|
2010-12-02 05:59:06 +01:00
|
|
|
this.charLength - this.charReceived :
|
|
|
|
buffer.length;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
// add the new bytes to the char buffer
|
2014-05-13 17:36:40 +02:00
|
|
|
buffer.copy(this.charBuffer, this.charReceived, 0, available);
|
|
|
|
this.charReceived += available;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
if (this.charReceived < this.charLength) {
|
|
|
|
// still not enough chars in this buffer? wait for more ...
|
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
|
|
|
|
buffer = buffer.slice(available, buffer.length);
|
|
|
|
|
2010-05-18 04:25:51 +02:00
|
|
|
// get the character that was split
|
2012-05-05 15:47:24 +02:00
|
|
|
charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
|
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
|
2012-05-05 15:47:24 +02:00
|
|
|
var charCode = charStr.charCodeAt(charStr.length - 1);
|
|
|
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
|
|
|
|
this.charLength += this.surrogateSize;
|
|
|
|
charStr = '';
|
|
|
|
continue;
|
2012-05-05 05:22:01 +02:00
|
|
|
}
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charReceived = this.charLength = 0;
|
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
// if there are no more bytes in this buffer, just emit our char
|
2014-05-13 17:36:40 +02:00
|
|
|
if (buffer.length === 0) {
|
|
|
|
return charStr;
|
|
|
|
}
|
2012-05-05 05:22:01 +02:00
|
|
|
break;
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 17:36:40 +02:00
|
|
|
// determine and set charLength / charReceived
|
|
|
|
this.detectIncompleteChar(buffer);
|
2012-05-05 15:47:24 +02:00
|
|
|
|
|
|
|
var end = buffer.length;
|
|
|
|
if (this.charLength) {
|
|
|
|
// buffer the incomplete character bytes we got
|
2014-05-13 17:36:40 +02:00
|
|
|
buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
|
|
|
|
end -= this.charReceived;
|
2012-05-05 15:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
charStr += buffer.toString(this.encoding, 0, end);
|
|
|
|
|
|
|
|
var end = charStr.length - 1;
|
|
|
|
var 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) {
|
|
|
|
var size = this.surrogateSize;
|
|
|
|
this.charLength += size;
|
|
|
|
this.charReceived += size;
|
|
|
|
this.charBuffer.copy(this.charBuffer, size, 0, size);
|
2014-05-13 17:36:40 +02:00
|
|
|
buffer.copy(this.charBuffer, 0, 0, size);
|
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) {
|
2010-05-18 04:25:51 +02:00
|
|
|
// determine how many bytes we have to check at the end of this buffer
|
2010-06-16 03:19:25 +02:00
|
|
|
var i = (buffer.length >= 3) ? 3 : buffer.length;
|
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--) {
|
2010-12-05 00:20:34 +01:00
|
|
|
var c = buffer[buffer.length - i];
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
// See http://en.wikipedia.org/wiki/UTF-8#Description
|
|
|
|
|
|
|
|
// 110XXXXX
|
|
|
|
if (i == 1 && c >> 5 == 0x06) {
|
|
|
|
this.charLength = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1110XXXX
|
|
|
|
if (i <= 2 && c >> 4 == 0x0E) {
|
|
|
|
this.charLength = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 11110XXX
|
|
|
|
if (i <= 3 && c >> 3 == 0x1E) {
|
|
|
|
this.charLength = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 17:36:40 +02:00
|
|
|
this.charReceived = i;
|
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);
|
|
|
|
|
|
|
|
if (this.charReceived) {
|
|
|
|
var cr = this.charReceived;
|
|
|
|
var buf = this.charBuffer;
|
|
|
|
var enc = this.encoding;
|
|
|
|
res += buf.slice(0, cr).toString(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-05-13 17:36:40 +02:00
|
|
|
this.charReceived = buffer.length % 2;
|
|
|
|
this.charLength = this.charReceived ? 2 : 0;
|
2012-05-05 15:47:24 +02:00
|
|
|
}
|
2012-10-12 00:53:11 +02:00
|
|
|
|
|
|
|
function base64DetectIncompleteChar(buffer) {
|
2014-05-13 17:36:40 +02:00
|
|
|
this.charReceived = buffer.length % 3;
|
|
|
|
this.charLength = this.charReceived ? 3 : 0;
|
2012-10-12 00:53:11 +02:00
|
|
|
}
|