43081j / id3

A JavaScript ID3 tags parser for Node & browsers.
MIT License
335 stars 63 forks source link

DataView.getStringUtf16 mangles strings when useBuffer is true #18

Closed Xenoveritas closed 5 years ago

Xenoveritas commented 9 years ago

This issue looks like it's fairly simple: getStringUtf16 builds an array of 16-bit character values in an array and then attempts to create an output string using new Buffer(str). Unfortunately that expects an array of octets and discards the high byte in each of the two-byte values given. This means the method works if every character is actually ASCII, but fails if any UTF-16 pairs are found.

The simple solution I went with was:

// str is an array of two-byte pairs
var buf = new Buffer(str.length*2);
for (var i = 0; i < str.length; i++) {
    buf.writeUInt16LE(str[i], i*2);
}
return buf.toString('utf16le');

And that solved my problem.