Skip to content

Commit 3331bbc

Browse files
committedJun 28, 2020
Fix minor issue in UTF-32 decoder.
In streaming mode, if the first chunk is < 32 bytes, but there are more chunks written with total size of the stream > 32 bytes, then we were losing initial chunk. This looks like an unlikely scenario, so not a major issue. Note, this is happening only in UTF-32 decoder, not UTF-32LE or BE, as the problem was in the code that detects encoding.
1 parent 148b6bc commit 3331bbc

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
 

‎encodings/utf32.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Utf32Decoder.prototype.end = function() {
164164
// Encoder prepends BOM (which can be overridden with (addBOM: false}).
165165

166166
exports.utf32 = Utf32AutoCodec;
167-
exports.ucs4 = Utf32AutoCodec;
167+
exports.ucs4 = 'utf32';
168168

169169
function Utf32AutoCodec(options, iconv) {
170170
this.iconv = iconv;
@@ -212,8 +212,8 @@ Utf32AutoDecoder.prototype.write = function(buf) {
212212
return '';
213213

214214
// We have enough bytes -> detect endianness.
215-
var buf2 = Buffer.concat(this.initialBytes),
216-
encoding = detectEncoding(buf2, this.options.defaultEncoding);
215+
var buf = Buffer.concat(this.initialBytes),
216+
encoding = detectEncoding(buf, this.options.defaultEncoding);
217217
this.decoder = this.iconv.getDecoder(encoding, this.options);
218218
this.initialBytes.length = this.initialBytesLen = 0;
219219
}

‎test/streams-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,15 @@ describe("Streaming mode", function() {
297297
input: [Buffer.from('&T2'), Buffer.from('BZf'), Buffer.from('Q hei&AN8-t')],
298298
output: '\u4F60\u597D heißt'
299299
}));
300+
301+
it("Decoding of chunks in UTF-32 auto mode does not lose chunks", checkDecodeStream({
302+
encoding: "UTF-32",
303+
input: [
304+
[0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x64],
305+
[0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x64],
306+
],
307+
output: "abcdabcd",
308+
}));
300309
});
301310

302311
describe("Streaming sugar", function() {

0 commit comments

Comments
 (0)
Please sign in to comment.