From 12d8f0b64d4118a8561c92f31205ced9d95f4384 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Fri, 14 Jan 2022 18:44:38 +0100
Subject: [PATCH] Re-factor the `stringToPDFString` helper function for UTF-16
 strings

This patch changes the function to instead utilize the `TextDecoder` for both kinds of UTF-16 BOM strings.
---
 src/shared/util.js | 47 +++++++++++++++++++---------------------------
 1 file changed, 19 insertions(+), 28 deletions(-)

diff --git a/src/shared/util.js b/src/shared/util.js
index 9d023bfce..083d2bc28 100644
--- a/src/shared/util.js
+++ b/src/shared/util.js
@@ -955,38 +955,29 @@ const PDFStringTranslateTable = [
 ];
 
 function stringToPDFString(str) {
-  const length = str.length,
-    strBuf = [];
-  // UTF-16BE BOM
-  if (str[0] === "\xFE" && str[1] === "\xFF") {
-    for (let i = 2; i < length; i += 2) {
-      strBuf.push(
-        String.fromCharCode((str.charCodeAt(i) << 8) | str.charCodeAt(i + 1))
-      );
+  if (str[0] >= "\xEF") {
+    let encoding;
+    if (str[0] === "\xFE" && str[1] === "\xFF") {
+      encoding = "utf-16be";
+    } else if (str[0] === "\xFF" && str[1] === "\xFE") {
+      encoding = "utf-16le";
+    } else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
+      encoding = "utf-8";
     }
-    return strBuf.join("");
-  }
-  // UTF-16LE BOM
-  if (str[0] === "\xFF" && str[1] === "\xFE") {
-    for (let i = 2; i < length; i += 2) {
-      strBuf.push(
-        String.fromCharCode((str.charCodeAt(i + 1) << 8) | str.charCodeAt(i))
-      );
-    }
-    return strBuf.join("");
-  }
-  // UTF-8 BOM
-  if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
-    try {
-      const decoder = new TextDecoder("utf-8", { fatal: true });
-      const buffer = stringToBytes(str);
-      return decoder.decode(buffer);
-    } catch (ex) {
-      warn(`stringToPDFString: "${ex}".`);
+
+    if (encoding) {
+      try {
+        const decoder = new TextDecoder(encoding, { fatal: true });
+        const buffer = stringToBytes(str);
+        return decoder.decode(buffer);
+      } catch (ex) {
+        warn(`stringToPDFString: "${ex}".`);
+      }
     }
   }
   // ISO Latin 1
-  for (let i = 0; i < length; ++i) {
+  const strBuf = [];
+  for (let i = 0, ii = str.length; i < ii; i++) {
     const code = PDFStringTranslateTable[str.charCodeAt(i)];
     strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
   }