From: Jesse Tane Date: Tue, 16 Dec 2014 00:17:22 +0000 (-0500) Subject: do not encode partial code units to utf16 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=e6fddaa9238c815daaf5e61d541a45bc5ef63105;p=buffer.git do not encode partial code units to utf16 --- diff --git a/index.js b/index.js index 38b20ba..1a7144a 100644 --- a/index.js +++ b/index.js @@ -360,7 +360,7 @@ function base64Write (buf, string, offset, length) { } function utf16leWrite (buf, string, offset, length) { - var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length, 2) + var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length, 2) return charsWritten } @@ -1198,10 +1198,13 @@ function asciiToBytes (str) { return byteArray } -function utf16leToBytes (str) { +function utf16leToBytes (str, units) { var c, hi, lo var byteArray = [] for (var i = 0; i < str.length; i++) { + + if ((units -= 2) < 0) break + c = str.charCodeAt(i) hi = c >> 8 lo = c % 256