diff --git a/encodings/utf32.js b/encodings/utf32.js index 72317893..aae73751 100644 --- a/encodings/utf32.js +++ b/encodings/utf32.js @@ -113,9 +113,9 @@ Utf32Decoder.prototype.write = function (src) { // NOTE: codepoint is a signed int32 and can be negative. // NOTE: We copied this block from below to help V8 optimize it (it works with array, not buffer). if (isLE) { - codepoint = overflow[i] | (overflow[i + 1] << 8) | (overflow[i + 2] << 16) | (overflow[i + 3] << 24) + codepoint = overflow[0] | (overflow[1] << 8) | (overflow[2] << 16) | (overflow[3] << 24) } else { - codepoint = overflow[i + 3] | (overflow[i + 2] << 8) | (overflow[i + 1] << 16) | (overflow[i] << 24) + codepoint = overflow[3] | (overflow[2] << 8) | (overflow[1] << 16) | (overflow[0] << 24) } overflow.length = 0 diff --git a/test/utf32-test.js b/test/utf32-test.js index f43156c5..0635ecdd 100644 --- a/test/utf32-test.js +++ b/test/utf32-test.js @@ -63,6 +63,14 @@ describe("UTF-32LE codec", function () { assert.equal(iconv.decode(Buffer.from([0x61, 0, 0, 0, 0]), "UTF32-LE"), "a") }) + it("decodes correctly when codepoints are split across stream chunks", function () { + for (var at = 1; at < utf32leBuf.length; at++) { + var decoder = iconv.getDecoder("utf-32le") + var res = decoder.write(utf32leBuf.slice(0, at)) + decoder.write(utf32leBuf.slice(at)) + (decoder.end() || "") + assert.equal(res, testStr, "split at byte " + at) + } + }) + it("handles invalid surrogates gracefully", function () { var encoded = iconv.encode(testStr2, "UTF32-LE") assert.equal(escape(iconv.decode(encoded, "UTF32-LE")), escape(testStr2)) @@ -114,6 +122,14 @@ describe("UTF-32BE codec", function () { assert.equal(iconv.decode(Buffer.from([0, 0, 0, 0x61, 0]), "UTF32-BE"), "a") }) + it("decodes correctly when codepoints are split across stream chunks", function () { + for (var at = 1; at < utf32beBuf.length; at++) { + var decoder = iconv.getDecoder("utf-32be") + var res = decoder.write(utf32beBuf.slice(0, at)) + decoder.write(utf32beBuf.slice(at)) + (decoder.end() || "") + assert.equal(res, testStr, "split at byte " + at) + } + }) + it("handles invalid surrogates gracefully", function () { var encoded = iconv.encode(testStr2, "UTF32-BE") assert.equal(escape(iconv.decode(encoded, "UTF32-BE")), escape(testStr2))