From ed17ed2919e5eb03bcec0b96c76bb798571a382c Mon Sep 17 00:00:00 2001 From: bwhitn Date: Tue, 20 Nov 2018 22:12:38 -0500 Subject: [PATCH] Moved quotedprintable to own library, completed a little work on IMF parsing --- src/core/lib/QuotedPrintable.mjs | 37 +++++++++++++++++++++ src/core/operations/FromQuotedPrintable.mjs | 21 ++---------- src/core/operations/ParseIMF.mjs | 11 +++--- 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/core/lib/QuotedPrintable.mjs diff --git a/src/core/lib/QuotedPrintable.mjs b/src/core/lib/QuotedPrintable.mjs new file mode 100644 index 00000000..13e78df4 --- /dev/null +++ b/src/core/lib/QuotedPrintable.mjs @@ -0,0 +1,37 @@ +/** + * Some parts taken from mimelib (http://github.com/andris9/mimelib) + * @author Andris Reinman + * @license MIT + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2016 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; + +/** + * @param {string} input + * @returns {byteArray} + */ +export function decodeQuotedPrintable(input) { + const str = input.replace(/=(?:\r?\n|$)/g, ""); + + const encodedBytesCount = (str.match(/=[\da-fA-F]{2}/g) || []).length, + bufferLength = str.length - encodedBytesCount * 2, + buffer = new Array(bufferLength); + let chr, hex, + bufferPos = 0; + + for (let i = 0, len = str.length; i < len; i++) { + chr = str.charAt(i); + if (chr === "=" && (hex = str.substr(i + 1, 2)) && /[\da-fA-F]{2}/.test(hex)) { + buffer[bufferPos++] = parseInt(hex, 16); + i += 2; + continue; + } + buffer[bufferPos++] = chr.charCodeAt(0); + } + + return buffer; +} diff --git a/src/core/operations/FromQuotedPrintable.mjs b/src/core/operations/FromQuotedPrintable.mjs index ee079ec6..10cedb35 100644 --- a/src/core/operations/FromQuotedPrintable.mjs +++ b/src/core/operations/FromQuotedPrintable.mjs @@ -9,6 +9,7 @@ */ import Operation from "../Operation"; +import {decodeQuotedPrintable} from "../lib/QuotedPrintable" /** * From Quoted Printable operation @@ -43,25 +44,7 @@ class FromQuotedPrintable extends Operation { * @returns {byteArray} */ run(input, args) { - const str = input.replace(/=(?:\r?\n|$)/g, ""); - - const encodedBytesCount = (str.match(/=[\da-fA-F]{2}/g) || []).length, - bufferLength = str.length - encodedBytesCount * 2, - buffer = new Array(bufferLength); - let chr, hex, - bufferPos = 0; - - for (let i = 0, len = str.length; i < len; i++) { - chr = str.charAt(i); - if (chr === "=" && (hex = str.substr(i + 1, 2)) && /[\da-fA-F]{2}/.test(hex)) { - buffer[bufferPos++] = parseInt(hex, 16); - i += 2; - continue; - } - buffer[bufferPos++] = chr.charCodeAt(0); - } - - return buffer; + return decodeQuotedPrintable(input); } } diff --git a/src/core/operations/ParseIMF.mjs b/src/core/operations/ParseIMF.mjs index e0127de4..803631c1 100644 --- a/src/core/operations/ParseIMF.mjs +++ b/src/core/operations/ParseIMF.mjs @@ -8,6 +8,7 @@ import OperationError from "../errors/OperationError"; import cptable from "../vendor/js-codepage/cptable.js"; import {fromBase64} from "../lib/Base64"; + import {decodeQuotedPrintable} from "../lib/QuotedPrintable" import {MIME_FORMAT} from "../lib/ChrEnc"; @@ -35,10 +36,10 @@ //TODO: should 8 bit and 7 bit be treated the same? const DECODER = { "base64": function (input) { - return Utils.fromBase64(input, Base64.ALPHABET, "string", true); + return fromBase64(input, Base64.ALPHABET, "string", true); }, "quoted-printable": function (input) { - return QuotedPrintable.mimeDecode(input); + return decodeQuotedPrintable(input); }, "7bit": function (input) { return input; @@ -79,11 +80,11 @@ if (!input) { return; } - let headerBody = Email._splitHeaderFromBody(input); + let headerBody = this.splitHeaderFromBody(input); let header = headerBody[0]; - let headerArray = Email._parseHeader(header); + let headerArray = this.parseHeader(header); if (args[0]) { - header = Email._replaceDecodeWord(header); + header = this.replaceDecodeWord(header); } return JSON.stringify(headerArray); }