From e6fddaa9238c815daaf5e61d541a45bc5ef63105 Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 19:17:22 -0500 Subject: [PATCH] do not encode partial code units to utf16 --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 -- 2.34.1