Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions encodings/utf32.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions test/utf32-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
Loading