Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
utf7.js284 linesDownload Raw Back to encodings
1"use strict"
2var Buffer = require("safer-buffer").Buffer
3
4// UTF-7 codec, according to https://tools.ietf.org/html/rfc2152
5// See also below a UTF-7-IMAP codec, according to http://tools.ietf.org/html/rfc3501#section-5.1.3
6
7exports.utf7 = Utf7Codec
8exports.unicode11utf7 = "utf7" // Alias UNICODE-1-1-UTF-7
9function Utf7Codec (codecOptions, iconv) {
10  this.iconv = iconv
11};
12
13Utf7Codec.prototype.encoder = Utf7Encoder
14Utf7Codec.prototype.decoder = Utf7Decoder
15Utf7Codec.prototype.bomAware = true
16
17// -- Encoding
18
19// Why scape ()?./?
20// eslint-disable-next-line no-useless-escape
21var nonDirectChars = /[^A-Za-z0-9'\(\),-\.\/:\? \n\r\t]+/g
22
23function Utf7Encoder (options, codec) {
24  this.iconv = codec.iconv
25}
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// -- Decoding
42
43function Utf7Decoder (options, codec) {
44  this.iconv = codec.iconv
45  this.inBase64 = false
46  this.base64Accum = ""
47}
48
49// Why scape /?
50// eslint-disable-next-line no-useless-escape
51var 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 = 0
61  var inBase64 = this.inBase64
62  var base64Accum = this.base64Accum
63
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 + 1
72        inBase64 = true
73      }
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 + 1
87        inBase64 = false
88        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 = inBase64
106  this.base64Accum = base64Accum
107
108  return res
109}
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 = false
116  this.base64Accum = ""
117  return res
118}
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 = Utf7IMAPCodec
132function Utf7IMAPCodec (codecOptions, iconv) {
133  this.iconv = iconv
134};
135
136Utf7IMAPCodec.prototype.encoder = Utf7IMAPEncoder
137Utf7IMAPCodec.prototype.decoder = Utf7IMAPDecoder
138Utf7IMAPCodec.prototype.bomAware = true
139
140// -- Encoding
141
142function Utf7IMAPEncoder (options, codec) {
143  this.iconv = codec.iconv
144  this.inBase64 = false
145  this.base64Accum = Buffer.alloc(6)
146  this.base64AccumIdx = 0
147}
148
149Utf7IMAPEncoder.prototype.write = function (str) {
150  var inBase64 = this.inBase64
151  var base64Accum = this.base64Accum
152  var base64AccumIdx = this.base64AccumIdx
153  var buf = Buffer.alloc(str.length * 5 + 10); var bufIdx = 0
154
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 = 0
162        }
163
164        buf[bufIdx++] = minusChar // Write '-', then go to direct mode.
165        inBase64 = false
166      }
167
168      if (!inBase64) {
169        buf[bufIdx++] = uChar // Write direct character
170
171        if (uChar === andChar)  // Ampersand -> '&-'
172        { buf[bufIdx++] = minusChar }
173      }
174    } else { // Non-direct character
175      if (!inBase64) {
176        buf[bufIdx++] = andChar // Write '&', then go to base64 mode.
177        inBase64 = true
178      }
179      if (inBase64) {
180        base64Accum[base64AccumIdx++] = uChar >> 8
181        base64Accum[base64AccumIdx++] = uChar & 0xFF
182
183        if (base64AccumIdx == base64Accum.length) {
184          bufIdx += buf.write(base64Accum.toString("base64").replace(/\//g, ","), bufIdx)
185          base64AccumIdx = 0
186        }
187      }
188    }
189  }
190
191  this.inBase64 = inBase64
192  this.base64AccumIdx = base64AccumIdx
193
194  return buf.slice(0, bufIdx)
195}
196
197Utf7IMAPEncoder.prototype.end = function () {
198  var buf = Buffer.alloc(10); var bufIdx = 0
199  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 = 0
203    }
204
205    buf[bufIdx++] = minusChar // Write '-', then go to direct mode.
206    this.inBase64 = false
207  }
208
209  return buf.slice(0, bufIdx)
210}
211
212// -- Decoding
213
214function Utf7IMAPDecoder (options, codec) {
215  this.iconv = codec.iconv
216  this.inBase64 = false
217  this.base64Accum = ""
218}
219
220var base64IMAPChars = base64Chars.slice()
221base64IMAPChars[",".charCodeAt(0)] = true
222
223Utf7IMAPDecoder.prototype.write = function (buf) {
224  var res = ""; var lastI = 0
225  var inBase64 = this.inBase64
226  var base64Accum = this.base64Accum
227
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 + 1
237        inBase64 = true
238      }
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 + 1
252        inBase64 = false
253        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 = inBase64
271  this.base64Accum = base64Accum
272
273  return res
274}
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 = false
281  this.base64Accum = ""
282  return res
283}
284 
codekingpro/portable-devtools · Team Ai