3 // UTF-7 codec, according to https://tools.ietf.org/html/rfc2152
4 // See also below a UTF-7-IMAP codec, according to http://tools.ietf.org/html/rfc3501#section-5.1.3
6 exports.utf7 = Utf7Codec;
7 exports.unicode11utf7 = 'utf7'; // Alias UNICODE-1-1-UTF-7
8 function Utf7Codec(codecOptions, iconv) {
12 Utf7Codec.prototype.encoder = Utf7Encoder;
13 Utf7Codec.prototype.decoder = Utf7Decoder;
14 Utf7Codec.prototype.bomAware = true;
19 var nonDirectChars = /[^A-Za-z0-9'\(\),-\.\/:\? \n\r\t]+/g;
21 function Utf7Encoder(options, codec) {
22 this.iconv = codec.iconv;
25 Utf7Encoder.prototype.write = function(str) {
26 // Naive implementation.
27 // Non-direct chars are encoded as "+<base64>-"; single "+" char is encoded as "+-".
28 return new Buffer(str.replace(nonDirectChars, function(chunk) {
29 return "+" + (chunk === '+' ? '' :
30 this.iconv.encode(chunk, 'utf16-be').toString('base64').replace(/=+$/, ''))
35 Utf7Encoder.prototype.end = function() {
41 function Utf7Decoder(options, codec) {
42 this.iconv = codec.iconv;
43 this.inBase64 = false;
44 this.base64Accum = '';
47 var base64Regex = /[A-Za-z0-9\/+]/;
49 for (var i = 0; i < 256; i++)
50 base64Chars[i] = base64Regex.test(String.fromCharCode(i));
52 var plusChar = '+'.charCodeAt(0),
53 minusChar = '-'.charCodeAt(0),
54 andChar = '&'.charCodeAt(0);
56 Utf7Decoder.prototype.write = function(buf) {
57 var res = "", lastI = 0,
58 inBase64 = this.inBase64,
59 base64Accum = this.base64Accum;
61 // The decoder is more involved as we must handle chunks in stream.
63 for (var i = 0; i < buf.length; i++) {
64 if (!inBase64) { // We're in direct mode.
65 // Write direct chars until '+'
66 if (buf[i] == plusChar) {
67 res += this.iconv.decode(buf.slice(lastI, i), "ascii"); // Write direct chars.
71 } else { // We decode base64.
72 if (!base64Chars[buf[i]]) { // Base64 ended.
73 if (i == lastI && buf[i] == minusChar) {// "+-" -> "+"
76 var b64str = base64Accum + buf.slice(lastI, i).toString();
77 res += this.iconv.decode(new Buffer(b64str, 'base64'), "utf16-be");
80 if (buf[i] != minusChar) // Minus is absorbed after base64.
91 res += this.iconv.decode(buf.slice(lastI), "ascii"); // Write direct chars.
93 var b64str = base64Accum + buf.slice(lastI).toString();
95 var canBeDecoded = b64str.length - (b64str.length % 8); // Minimal chunk: 2 quads -> 2x3 bytes -> 3 chars.
96 base64Accum = b64str.slice(canBeDecoded); // The rest will be decoded in future.
97 b64str = b64str.slice(0, canBeDecoded);
99 res += this.iconv.decode(new Buffer(b64str, 'base64'), "utf16-be");
102 this.inBase64 = inBase64;
103 this.base64Accum = base64Accum;
108 Utf7Decoder.prototype.end = function() {
110 if (this.inBase64 && this.base64Accum.length > 0)
111 res = this.iconv.decode(new Buffer(this.base64Accum, 'base64'), "utf16-be");
113 this.inBase64 = false;
114 this.base64Accum = '';
120 // RFC3501 Sec. 5.1.3 Modified UTF-7 (http://tools.ietf.org/html/rfc3501#section-5.1.3)
122 // * Base64 part is started by "&" instead of "+"
123 // * Direct characters are 0x20-0x7E, except "&" (0x26)
124 // * In Base64, "," is used instead of "/"
125 // * Base64 must not be used to represent direct characters.
126 // * No implicit shift back from Base64 (should always end with '-')
127 // * String must end in non-shifted position.
128 // * "-&" while in base64 is not allowed.
131 exports.utf7imap = Utf7IMAPCodec;
132 function Utf7IMAPCodec(codecOptions, iconv) {
136 Utf7IMAPCodec.prototype.encoder = Utf7IMAPEncoder;
137 Utf7IMAPCodec.prototype.decoder = Utf7IMAPDecoder;
138 Utf7IMAPCodec.prototype.bomAware = true;
143 function Utf7IMAPEncoder(options, codec) {
144 this.iconv = codec.iconv;
145 this.inBase64 = false;
146 this.base64Accum = new Buffer(6);
147 this.base64AccumIdx = 0;
150 Utf7IMAPEncoder.prototype.write = function(str) {
151 var inBase64 = this.inBase64,
152 base64Accum = this.base64Accum,
153 base64AccumIdx = this.base64AccumIdx,
154 buf = new Buffer(str.length*5 + 10), bufIdx = 0;
156 for (var i = 0; i < str.length; i++) {
157 var uChar = str.charCodeAt(i);
158 if (0x20 <= uChar && uChar <= 0x7E) { // Direct character or '&'.
160 if (base64AccumIdx > 0) {
161 bufIdx += buf.write(base64Accum.slice(0, base64AccumIdx).toString('base64').replace(/\//g, ',').replace(/=+$/, ''), bufIdx);
165 buf[bufIdx++] = minusChar; // Write '-', then go to direct mode.
170 buf[bufIdx++] = uChar; // Write direct character
172 if (uChar === andChar) // Ampersand -> '&-'
173 buf[bufIdx++] = minusChar;
176 } else { // Non-direct character
178 buf[bufIdx++] = andChar; // Write '&', then go to base64 mode.
182 base64Accum[base64AccumIdx++] = uChar >> 8;
183 base64Accum[base64AccumIdx++] = uChar & 0xFF;
185 if (base64AccumIdx == base64Accum.length) {
186 bufIdx += buf.write(base64Accum.toString('base64').replace(/\//g, ','), bufIdx);
193 this.inBase64 = inBase64;
194 this.base64AccumIdx = base64AccumIdx;
196 return buf.slice(0, bufIdx);
199 Utf7IMAPEncoder.prototype.end = function() {
200 var buf = new Buffer(10), bufIdx = 0;
202 if (this.base64AccumIdx > 0) {
203 bufIdx += buf.write(this.base64Accum.slice(0, this.base64AccumIdx).toString('base64').replace(/\//g, ',').replace(/=+$/, ''), bufIdx);
204 this.base64AccumIdx = 0;
207 buf[bufIdx++] = minusChar; // Write '-', then go to direct mode.
208 this.inBase64 = false;
211 return buf.slice(0, bufIdx);
217 function Utf7IMAPDecoder(options, codec) {
218 this.iconv = codec.iconv;
219 this.inBase64 = false;
220 this.base64Accum = '';
223 var base64IMAPChars = base64Chars.slice();
224 base64IMAPChars[','.charCodeAt(0)] = true;
226 Utf7IMAPDecoder.prototype.write = function(buf) {
227 var res = "", lastI = 0,
228 inBase64 = this.inBase64,
229 base64Accum = this.base64Accum;
231 // The decoder is more involved as we must handle chunks in stream.
232 // It is forgiving, closer to standard UTF-7 (for example, '-' is optional at the end).
234 for (var i = 0; i < buf.length; i++) {
235 if (!inBase64) { // We're in direct mode.
236 // Write direct chars until '&'
237 if (buf[i] == andChar) {
238 res += this.iconv.decode(buf.slice(lastI, i), "ascii"); // Write direct chars.
242 } else { // We decode base64.
243 if (!base64IMAPChars[buf[i]]) { // Base64 ended.
244 if (i == lastI && buf[i] == minusChar) { // "&-" -> "&"
247 var b64str = base64Accum + buf.slice(lastI, i).toString().replace(/,/g, '/');
248 res += this.iconv.decode(new Buffer(b64str, 'base64'), "utf16-be");
251 if (buf[i] != minusChar) // Minus may be absorbed after base64.
262 res += this.iconv.decode(buf.slice(lastI), "ascii"); // Write direct chars.
264 var b64str = base64Accum + buf.slice(lastI).toString().replace(/,/g, '/');
266 var canBeDecoded = b64str.length - (b64str.length % 8); // Minimal chunk: 2 quads -> 2x3 bytes -> 3 chars.
267 base64Accum = b64str.slice(canBeDecoded); // The rest will be decoded in future.
268 b64str = b64str.slice(0, canBeDecoded);
270 res += this.iconv.decode(new Buffer(b64str, 'base64'), "utf16-be");
273 this.inBase64 = inBase64;
274 this.base64Accum = base64Accum;
279 Utf7IMAPDecoder.prototype.end = function() {
281 if (this.inBase64 && this.base64Accum.length > 0)
282 res = this.iconv.decode(new Buffer(this.base64Accum, 'base64'), "utf16-be");
284 this.inBase64 = false;
285 this.base64Accum = '';