AK-21/Graphite-Industrial-Intelligence
0
1"use strict"2var Buffer = require("safer-buffer").Buffer3 4// UTF-7 codec, according to https://tools.ietf.org/html/rfc21525// See also below a UTF-7-IMAP codec, according to http://tools.ietf.org/html/rfc3501#section-5.1.36 7exports.utf7 = Utf7Codec8exports.unicode11utf7 = "utf7" // Alias UNICODE-1-1-UTF-79function Utf7Codec (codecOptions, iconv) {10 this.iconv = iconv11};12 13Utf7Codec.prototype.encoder = Utf7Encoder14Utf7Codec.prototype.decoder = Utf7Decoder15Utf7Codec.prototype.bomAware = true16 17// -- Encoding18 19// Why scape ()?./?20// eslint-disable-next-line no-useless-escape21var nonDirectChars = /[^A-Za-z0-9'\(\),-\.\/:\? \n\r\t]+/g22 23function Utf7Encoder (options, codec) {24 this.iconv = codec.iconv25}26 27Utf7Encoder.prototype.write = function (str) {28 // Naive implementation.29 // Non-direct chars are encoded as "+<base64>-"; single "+" char is encoded as "+-".30 return Buffer.from(str.replace(nonDirectChars, function (chunk) {31 return "+" + (chunk === "+"32 ? ""33 : this.iconv.encode(chunk, "utf16-be").toString("base64").replace(/=+$/, "")) +34 "-"35 }.bind(this)))36}37 38Utf7Encoder.prototype.end = function () {39}40 41// -- Decoding42 43function Utf7Decoder (options, codec) {44 this.iconv = codec.iconv45 this.inBase64 = false46 this.base64Accum = ""47}48 49// Why scape /?50// eslint-disable-next-line no-useless-escape51var base64Regex = /[A-Za-z0-9\/+]/52var base64Chars = []53for (var i = 0; i < 256; i++) { base64Chars[i] = base64Regex.test(String.fromCharCode(i)) }54 55var plusChar = "+".charCodeAt(0)56var minusChar = "-".charCodeAt(0)57var andChar = "&".charCodeAt(0)58 59Utf7Decoder.prototype.write = function (buf) {60 var res = ""; var lastI = 061 var inBase64 = this.inBase6462 var base64Accum = this.base64Accum63 64 // The decoder is more involved as we must handle chunks in stream.65 66 for (var i = 0; i < buf.length; i++) {67 if (!inBase64) { // We're in direct mode.68 // Write direct chars until '+'69 if (buf[i] == plusChar) {70 res += this.iconv.decode(buf.slice(lastI, i), "ascii") // Write direct chars.71 lastI = i + 172 inBase64 = true73 }74 } else { // We decode base64.75 if (!base64Chars[buf[i]]) { // Base64 ended.76 if (i == lastI && buf[i] == minusChar) { // "+-" -> "+"77 res += "+"78 } else {79 var b64str = base64Accum + this.iconv.decode(buf.slice(lastI, i), "ascii")80 res += this.iconv.decode(Buffer.from(b64str, "base64"), "utf16-be")81 }82 83 if (buf[i] != minusChar) // Minus is absorbed after base64.84 { i-- }85 86 lastI = i + 187 inBase64 = false88 base64Accum = ""89 }90 }91 }92 93 if (!inBase64) {94 res += this.iconv.decode(buf.slice(lastI), "ascii") // Write direct chars.95 } else {96 var b64str = base64Accum + this.iconv.decode(buf.slice(lastI), "ascii")97 98 var canBeDecoded = b64str.length - (b64str.length % 8) // Minimal chunk: 2 quads -> 2x3 bytes -> 3 chars.99 base64Accum = b64str.slice(canBeDecoded) // The rest will be decoded in future.100 b64str = b64str.slice(0, canBeDecoded)101 102 res += this.iconv.decode(Buffer.from(b64str, "base64"), "utf16-be")103 }104 105 this.inBase64 = inBase64106 this.base64Accum = base64Accum107 108 return res109}110 111Utf7Decoder.prototype.end = function () {112 var res = ""113 if (this.inBase64 && this.base64Accum.length > 0) { res = this.iconv.decode(Buffer.from(this.base64Accum, "base64"), "utf16-be") }114 115 this.inBase64 = false116 this.base64Accum = ""117 return res118}119 120// UTF-7-IMAP codec.121// RFC3501 Sec. 5.1.3 Modified UTF-7 (http://tools.ietf.org/html/rfc3501#section-5.1.3)122// Differences:123// * Base64 part is started by "&" instead of "+"124// * Direct characters are 0x20-0x7E, except "&" (0x26)125// * In Base64, "," is used instead of "/"126// * Base64 must not be used to represent direct characters.127// * No implicit shift back from Base64 (should always end with '-')128// * String must end in non-shifted position.129// * "-&" while in base64 is not allowed.130 131exports.utf7imap = Utf7IMAPCodec132function Utf7IMAPCodec (codecOptions, iconv) {133 this.iconv = iconv134};135 136Utf7IMAPCodec.prototype.encoder = Utf7IMAPEncoder137Utf7IMAPCodec.prototype.decoder = Utf7IMAPDecoder138Utf7IMAPCodec.prototype.bomAware = true139 140// -- Encoding141 142function Utf7IMAPEncoder (options, codec) {143 this.iconv = codec.iconv144 this.inBase64 = false145 this.base64Accum = Buffer.alloc(6)146 this.base64AccumIdx = 0147}148 149Utf7IMAPEncoder.prototype.write = function (str) {150 var inBase64 = this.inBase64151 var base64Accum = this.base64Accum152 var base64AccumIdx = this.base64AccumIdx153 var buf = Buffer.alloc(str.length * 5 + 10); var bufIdx = 0154 155 for (var i = 0; i < str.length; i++) {156 var uChar = str.charCodeAt(i)157 if (uChar >= 0x20 && uChar <= 0x7E) { // Direct character or '&'.158 if (inBase64) {159 if (base64AccumIdx > 0) {160 bufIdx += buf.write(base64Accum.slice(0, base64AccumIdx).toString("base64").replace(/\//g, ",").replace(/=+$/, ""), bufIdx)161 base64AccumIdx = 0162 }163 164 buf[bufIdx++] = minusChar // Write '-', then go to direct mode.165 inBase64 = false166 }167 168 if (!inBase64) {169 buf[bufIdx++] = uChar // Write direct character170 171 if (uChar === andChar) // Ampersand -> '&-'172 { buf[bufIdx++] = minusChar }173 }174 } else { // Non-direct character175 if (!inBase64) {176 buf[bufIdx++] = andChar // Write '&', then go to base64 mode.177 inBase64 = true178 }179 if (inBase64) {180 base64Accum[base64AccumIdx++] = uChar >> 8181 base64Accum[base64AccumIdx++] = uChar & 0xFF182 183 if (base64AccumIdx == base64Accum.length) {184 bufIdx += buf.write(base64Accum.toString("base64").replace(/\//g, ","), bufIdx)185 base64AccumIdx = 0186 }187 }188 }189 }190 191 this.inBase64 = inBase64192 this.base64AccumIdx = base64AccumIdx193 194 return buf.slice(0, bufIdx)195}196 197Utf7IMAPEncoder.prototype.end = function () {198 var buf = Buffer.alloc(10); var bufIdx = 0199 if (this.inBase64) {200 if (this.base64AccumIdx > 0) {201 bufIdx += buf.write(this.base64Accum.slice(0, this.base64AccumIdx).toString("base64").replace(/\//g, ",").replace(/=+$/, ""), bufIdx)202 this.base64AccumIdx = 0203 }204 205 buf[bufIdx++] = minusChar // Write '-', then go to direct mode.206 this.inBase64 = false207 }208 209 return buf.slice(0, bufIdx)210}211 212// -- Decoding213 214function Utf7IMAPDecoder (options, codec) {215 this.iconv = codec.iconv216 this.inBase64 = false217 this.base64Accum = ""218}219 220var base64IMAPChars = base64Chars.slice()221base64IMAPChars[",".charCodeAt(0)] = true222 223Utf7IMAPDecoder.prototype.write = function (buf) {224 var res = ""; var lastI = 0225 var inBase64 = this.inBase64226 var base64Accum = this.base64Accum227 228 // The decoder is more involved as we must handle chunks in stream.229 // It is forgiving, closer to standard UTF-7 (for example, '-' is optional at the end).230 231 for (var i = 0; i < buf.length; i++) {232 if (!inBase64) { // We're in direct mode.233 // Write direct chars until '&'234 if (buf[i] == andChar) {235 res += this.iconv.decode(buf.slice(lastI, i), "ascii") // Write direct chars.236 lastI = i + 1237 inBase64 = true238 }239 } else { // We decode base64.240 if (!base64IMAPChars[buf[i]]) { // Base64 ended.241 if (i == lastI && buf[i] == minusChar) { // "&-" -> "&"242 res += "&"243 } else {244 var b64str = base64Accum + this.iconv.decode(buf.slice(lastI, i), "ascii").replace(/,/g, "/")245 res += this.iconv.decode(Buffer.from(b64str, "base64"), "utf16-be")246 }247 248 if (buf[i] != minusChar) // Minus may be absorbed after base64.249 { i-- }250 251 lastI = i + 1252 inBase64 = false253 base64Accum = ""254 }255 }256 }257 258 if (!inBase64) {259 res += this.iconv.decode(buf.slice(lastI), "ascii") // Write direct chars.260 } else {261 var b64str = base64Accum + this.iconv.decode(buf.slice(lastI), "ascii").replace(/,/g, "/")262 263 var canBeDecoded = b64str.length - (b64str.length % 8) // Minimal chunk: 2 quads -> 2x3 bytes -> 3 chars.264 base64Accum = b64str.slice(canBeDecoded) // The rest will be decoded in future.265 b64str = b64str.slice(0, canBeDecoded)266 267 res += this.iconv.decode(Buffer.from(b64str, "base64"), "utf16-be")268 }269 270 this.inBase64 = inBase64271 this.base64Accum = base64Accum272 273 return res274}275 276Utf7IMAPDecoder.prototype.end = function () {277 var res = ""278 if (this.inBase64 && this.base64Accum.length > 0) { res = this.iconv.decode(Buffer.from(this.base64Accum, "base64"), "utf16-be") }279 280 this.inBase64 = false281 this.base64Accum = ""282 return res283}284 