summaryrefslogtreecommitdiff
path: root/lib/string_decoder.js
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2016-12-29 12:02:31 -0500
committerMichaƫl Zasso <targos@protonmail.com>2017-01-26 22:46:18 +0100
commit24ef1e67757514db9ee1aeded555d4fb336ca817 (patch)
tree4ef325470465c33c73ae6a7566dd04a0d9772149 /lib/string_decoder.js
parent007386ee81ceeffd65c2248869717b0717db3e46 (diff)
downloadandroid-node-v8-24ef1e67757514db9ee1aeded555d4fb336ca817.tar.gz
android-node-v8-24ef1e67757514db9ee1aeded555d4fb336ca817.tar.bz2
android-node-v8-24ef1e67757514db9ee1aeded555d4fb336ca817.zip
string_decoder: align UTF-8 handling with V8
V8 5.5 changed how invalid characters are handled and it now appears to follow the WHATWG Encoding standard, where all of an invalid character's bytes are replaced by a single replacement character (\ufffd) instead of replacing each invalid byte with separate replacement characters. Example: the byte sequence 0xF0,0xB8,0x41 is decoded as '\ufffdA' in V8 5.5, but is decoded as '\ufffd\ufffdA' in previous versions of V8. PR-URL: https://github.com/nodejs/node/pull/9618 Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib/string_decoder.js')
-rw-r--r--lib/string_decoder.js22
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/string_decoder.js b/lib/string_decoder.js
index cb9fbe409f..672ba185cc 100644
--- a/lib/string_decoder.js
+++ b/lib/string_decoder.js
@@ -81,7 +81,7 @@ StringDecoder.prototype.fillLast = function(buf) {
};
// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
-// continuation byte.
+// continuation byte. If an invalid byte is detected, -2 is returned.
function utf8CheckByte(byte) {
if (byte <= 0x7F)
return 0;
@@ -91,7 +91,7 @@ function utf8CheckByte(byte) {
return 3;
else if (byte >> 3 === 0x1E)
return 4;
- return -1;
+ return (byte >> 6 === 0x02 ? -1 : -2);
}
// Checks at most 3 bytes at the end of a Buffer in order to detect an
@@ -107,7 +107,7 @@ function utf8CheckIncomplete(self, buf, i) {
self.lastNeed = nb - 1;
return nb;
}
- if (--j < i)
+ if (--j < i || nb === -2)
return 0;
nb = utf8CheckByte(buf[j]);
if (nb >= 0) {
@@ -115,7 +115,7 @@ function utf8CheckIncomplete(self, buf, i) {
self.lastNeed = nb - 2;
return nb;
}
- if (--j < i)
+ if (--j < i || nb === -2)
return 0;
nb = utf8CheckByte(buf[j]);
if (nb >= 0) {
@@ -133,7 +133,7 @@ function utf8CheckIncomplete(self, buf, i) {
// Validates as many continuation bytes for a multi-byte UTF-8 character as
// needed or are available. If we see a non-continuation byte where we expect
// one, we "replace" the validated continuation bytes we've seen so far with
-// UTF-8 replacement characters ('\ufffd'), to match v8's UTF-8 decoding
+// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
// behavior. The continuation byte check is included three times in the case
// where all of the continuation bytes for a character exist in the same buffer.
// It is also done this way as a slight performance increase instead of using a
@@ -141,17 +141,17 @@ function utf8CheckIncomplete(self, buf, i) {
function utf8CheckExtraBytes(self, buf, p) {
if ((buf[0] & 0xC0) !== 0x80) {
self.lastNeed = 0;
- return '\ufffd'.repeat(p);
+ return '\ufffd';
}
if (self.lastNeed > 1 && buf.length > 1) {
if ((buf[1] & 0xC0) !== 0x80) {
self.lastNeed = 1;
- return '\ufffd'.repeat(p + 1);
+ return '\ufffd';
}
if (self.lastNeed > 2 && buf.length > 2) {
if ((buf[2] & 0xC0) !== 0x80) {
self.lastNeed = 2;
- return '\ufffd'.repeat(p + 2);
+ return '\ufffd';
}
}
}
@@ -184,12 +184,12 @@ function utf8Text(buf, i) {
return buf.toString('utf8', i, end);
}
-// For UTF-8, a replacement character for each buffered byte of a (partial)
-// character needs to be added to the output.
+// For UTF-8, a replacement character is added when ending on a partial
+// character.
function utf8End(buf) {
const r = (buf && buf.length ? this.write(buf) : '');
if (this.lastNeed)
- return r + '\ufffd'.repeat(this.lastTotal - this.lastNeed);
+ return r + '\ufffd';
return r;
}