diff --git a/src/lib/mhtml-to-html/parse.js b/src/lib/mhtml-to-html/parse.js
index f69418322..91060fa0f 100644
--- a/src/lib/mhtml-to-html/parse.js
+++ b/src/lib/mhtml-to-html/parse.js
@@ -185,9 +185,9 @@ function parse(mhtml, { DOMParser } = { DOMParser: globalThis.DOMParser }, conte
}
if (resource.transferEncoding === QUOTED_PRINTABLE_ENCODING) {
if (resource.data.length > 2 && resource.data[resource.data.length - 3] === 0x3D && endsWithCRLF(next)) {
- resource.data.splice(resource.data.length - 3, 3);
+ resource.data.length -= 3;
} else if (resource.data.length > 1 && resource.data[resource.data.length - 2] === 0x3D && endsWithLF(next)) {
- resource.data.splice(resource.data.length - 2, 2);
+ resource.data.length -= 2;
}
} else if (resource.transferEncoding === BASE64_ENCODING) {
if (endsWithCRLF(next)) {
@@ -196,7 +196,9 @@ function parse(mhtml, { DOMParser } = { DOMParser: globalThis.DOMParser }, conte
next = next.slice(0, next.length - 1);
}
}
- resource.data.splice(resource.data.length, 0, ...next);
+ for (let i = 0; i < next.length; i++) {
+ resource.data.push(next[i]);
+ }
if (!boundaryFound) {
next = getLine(transferEncoding);
}
diff --git a/src/lib/mhtml-to-html/util.js b/src/lib/mhtml-to-html/util.js
index 4649dba2a..38e6880de 100644
--- a/src/lib/mhtml-to-html/util.js
+++ b/src/lib/mhtml-to-html/util.js
@@ -162,16 +162,26 @@ function decodeQuotedPrintable(array) {
}
function decodeBinary(array) {
- let data = "";
- for (let indexData = 0; indexData < array.length; indexData++) {
- data += String.fromCharCode(array[indexData]);
+ const CHUNK_SIZE = 8192;
+ const parts = [];
+ for (let i = 0; i < array.length; i += CHUNK_SIZE) {
+ parts.push(String.fromCharCode.apply(null, array.subarray(i, Math.min(i + CHUNK_SIZE, array.length))));
}
- return btoa(data);
+ return btoa(parts.join(""));
}
function decodeBase64(value, charset) {
- const decodedData = new Uint8Array(atob(value).split("").map(char => char.charCodeAt(0)));
- return new TextDecoder(charset).decode(decodedData);
+ try {
+ const binaryString = atob(value);
+ const bytes = new Uint8Array(binaryString.length);
+ for (let i = 0; i < binaryString.length; i++) {
+ bytes[i] = binaryString.charCodeAt(i);
+ }
+ return new TextDecoder(charset).decode(bytes);
+ } catch (_) {
+ // eslint-disable-next-line no-unused-vars
+ return value;
+ }
}
function decodeMimeHeader(encodedSubject) {