Moved quotedprintable to own library, completed a little work on IMF parsing

This commit is contained in:
bwhitn 2018-11-20 22:12:38 -05:00
parent 7ae0b08b4d
commit ed17ed2919
3 changed files with 45 additions and 24 deletions

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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);
}