|
| 1 | +/** |
| 2 | + * @author mshwed [[email protected]] |
| 3 | + * @copyright Crown Copyright 2019 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import Utils from "../Utils.mjs"; |
| 10 | +import { fromHex } from "../lib/Hex.mjs"; |
| 11 | +import { fromBase64 } from "../lib/Base64.mjs"; |
| 12 | +import cptable from "codepage"; |
| 13 | + |
| 14 | +/** |
| 15 | + * MIME Decoding operation |
| 16 | + */ |
| 17 | +class MIMEDecoding extends Operation { |
| 18 | + |
| 19 | + /** |
| 20 | + * MIMEDecoding constructor |
| 21 | + */ |
| 22 | + constructor() { |
| 23 | + super(); |
| 24 | + |
| 25 | + this.name = "MIME Decoding"; |
| 26 | + this.module = "Default"; |
| 27 | + this.description = "Enables the decoding of MIME message header extensions for non-ASCII text"; |
| 28 | + this.infoURL = "https://tools.ietf.org/html/rfc2047"; |
| 29 | + this.inputType = "byteArray"; |
| 30 | + this.outputType = "string"; |
| 31 | + this.args = []; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param {byteArray} input |
| 36 | + * @param {Object[]} args |
| 37 | + * @returns {string} |
| 38 | + */ |
| 39 | + run(input, args) { |
| 40 | + const mimeEncodedText = Utils.byteArrayToUtf8(input); |
| 41 | + const encodedHeaders = mimeEncodedText.replace(/\r\n/g, "\n"); |
| 42 | + |
| 43 | + const decodedHeader = this.decodeHeaders(encodedHeaders); |
| 44 | + |
| 45 | + return decodedHeader; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Decode MIME header strings |
| 50 | + * |
| 51 | + * @param headerString |
| 52 | + */ |
| 53 | + decodeHeaders(headerString) { |
| 54 | + // No encoded words detected |
| 55 | + let i = headerString.indexOf("=?"); |
| 56 | + if (i === -1) return headerString; |
| 57 | + |
| 58 | + let decodedHeaders = headerString.slice(0, i); |
| 59 | + let header = headerString.slice(i); |
| 60 | + |
| 61 | + let isBetweenWords = false; |
| 62 | + let start, cur, charset, encoding, j, end, text; |
| 63 | + while (header.length > -1) { |
| 64 | + start = header.indexOf("=?"); |
| 65 | + if (start === -1) break; |
| 66 | + cur = start + "=?".length; |
| 67 | + |
| 68 | + i = header.slice(cur).indexOf("?"); |
| 69 | + if (i === -1) break; |
| 70 | + |
| 71 | + charset = header.slice(cur, cur + i); |
| 72 | + cur += i + "?".length; |
| 73 | + |
| 74 | + if (header.length < cur + "Q??=".length) break; |
| 75 | + |
| 76 | + encoding = header[cur]; |
| 77 | + cur += 1; |
| 78 | + |
| 79 | + if (header[cur] !== "?") break; |
| 80 | + |
| 81 | + cur += 1; |
| 82 | + |
| 83 | + j = header.slice(cur).indexOf("?="); |
| 84 | + if (j === -1) break; |
| 85 | + |
| 86 | + text = header.slice(cur, cur + j); |
| 87 | + end = cur + j + "?=".length; |
| 88 | + |
| 89 | + if (encoding.toLowerCase() === "b") { |
| 90 | + text = fromBase64(text); |
| 91 | + } else if (encoding.toLowerCase() === "q") { |
| 92 | + text = this.parseQEncodedWord(text); |
| 93 | + } else { |
| 94 | + isBetweenWords = false; |
| 95 | + decodedHeaders += header.slice(0, start + 2); |
| 96 | + header = header.slice(start + 2); |
| 97 | + } |
| 98 | + |
| 99 | + if (start > 0 && (!isBetweenWords || header.slice(0, start).search(/\S/g) > -1)) { |
| 100 | + decodedHeaders += header.slice(0, start); |
| 101 | + } |
| 102 | + |
| 103 | + decodedHeaders += this.convertFromCharset(charset, text); |
| 104 | + |
| 105 | + header = header.slice(end); |
| 106 | + isBetweenWords = true; |
| 107 | + } |
| 108 | + |
| 109 | + if (header.length > 0) { |
| 110 | + decodedHeaders += header; |
| 111 | + } |
| 112 | + |
| 113 | + return decodedHeaders; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Converts decoded text for supported charsets. |
| 118 | + * Supports UTF-8, US-ASCII, ISO-8859-* |
| 119 | + * |
| 120 | + * @param encodedWord |
| 121 | + */ |
| 122 | + convertFromCharset(charset, encodedText) { |
| 123 | + charset = charset.toLowerCase(); |
| 124 | + const parsedCharset = charset.split("-"); |
| 125 | + |
| 126 | + if (parsedCharset.length === 2 && parsedCharset[0] === "utf" && charset === "utf-8") { |
| 127 | + return cptable.utils.decode(65001, encodedText); |
| 128 | + } else if (parsedCharset.length === 2 && charset === "us-ascii") { |
| 129 | + return cptable.utils.decode(20127, encodedText); |
| 130 | + } else if (parsedCharset.length === 3 && parsedCharset[0] === "iso" && parsedCharset[1] === "8859") { |
| 131 | + const isoCharset = parseInt(parsedCharset[2], 10); |
| 132 | + if (isoCharset >= 1 && isoCharset <= 16) { |
| 133 | + return cptable.utils.decode(28590 + isoCharset, encodedText); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + throw new OperationError("Unhandled Charset"); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Parses a Q encoded word |
| 142 | + * |
| 143 | + * @param encodedWord |
| 144 | + */ |
| 145 | + parseQEncodedWord(encodedWord) { |
| 146 | + let decodedWord = ""; |
| 147 | + for (let i = 0; i < encodedWord.length; i++) { |
| 148 | + if (encodedWord[i] === "_") { |
| 149 | + decodedWord += " "; |
| 150 | + // Parse hex encoding |
| 151 | + } else if (encodedWord[i] === "=") { |
| 152 | + if ((i + 2) >= encodedWord.length) throw new OperationError("Incorrectly Encoded Word"); |
| 153 | + const decodedHex = Utils.byteArrayToChars(fromHex(encodedWord.substring(i + 1, i + 3))); |
| 154 | + decodedWord += decodedHex; |
| 155 | + i += 2; |
| 156 | + } else if ( |
| 157 | + (encodedWord[i].charCodeAt(0) >= " ".charCodeAt(0) && encodedWord[i].charCodeAt(0) <= "~".charCodeAt(0)) || |
| 158 | + encodedWord[i] === "\n" || |
| 159 | + encodedWord[i] === "\r" || |
| 160 | + encodedWord[i] === "\t") { |
| 161 | + decodedWord += encodedWord[i]; |
| 162 | + } else { |
| 163 | + throw new OperationError("Incorrectly Encoded Word"); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return decodedWord; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export default MIMEDecoding; |
0 commit comments