File: encoder.js

package info (click to toggle)
firegpg 0.8-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,392 kB
  • ctags: 630
  • sloc: makefile: 132; xml: 19
file content (494 lines) | stat: -rw-r--r-- 16,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*

***** BEGIN LICENSE BLOCK *****

Version: MPL 1.1/GPL 2.0/LGPL 2.1

The contents of this source code are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this source code except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.

The Original Code is the FireGPG extension.

The Initial Developer of the Original Code is Maximilien Cuony.

Portions created by the Initial Developer are Copyright (C) 2007
the Initial Developer. All Rights Reserved.

Contributor(s):

Alternatively, the contents of this source code may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this source code
only under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this source code under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this source code
under the terms of any one of the MPL, the GPL or the LGPL.

Portions created by Gmail S/MIME are Copyright (C) 2005-2007 Richard Jones, 2007-2008 Sean Leonard of SeanTek(R). All Rights Reserved.

***** END LICENSE BLOCK *****

Base of the code :

Copyright (C) 2005-2006 Richard Jones.
Copyright (C) 2007-2008 Sean Leonard of SeanTek(R).
This file was a part of Gmail S/MIME (adapted)

{DOUBLY FREE SOFTWARE PUBLIC LICENSE}

*/

if (typeof(FireGPG)=='undefined') { FireGPG = {}; }
if (typeof(FireGPG.Mime)=='undefined') { FireGPG.Mime = {}; }

FireGPG.Mime.Encoder = function(multiPart,partNum)
{
	this.init(multiPart,partNum);
}

FireGPG.Mime.Encoder.prototype =
{
	multipartStream: null,
	// TODO: generate mostly random.
	CRLF: "\r\n",


	addStringToStream : function(str)
	{
		var strInp=
		Components.classes["@mozilla.org/io/string-input-stream;1"].
		createInstance(Components.interfaces.nsIStringInputStream);
		strInp.setData(str,str.length);
		this.multipartStream.appendStream(strInp);
	},

	addValuePair: function(name,value)
	{
		var str = "--"+this.boundaryString+this.CRLF +
		"Content-Disposition: form-data; name=\"" +
		encodeURIComponent(name)+"\"" + this.CRLF + this.CRLF +
		value + this.CRLF;
		this.addStringToStream(str);
	},

	addFilePairFromString: function(str,fileNameStr,contentType, disposition,encoding)
	{
		var strInp=Components.classes["@mozilla.org/io/string-input-stream;1"].
		createInstance(Components.interfaces.nsIStringInputStream);
		strInp.setData(str,str.length);
		this.addFilePairFromStream(strInp,fileNameStr,contentType,
		disposition,null,encoding);
	},

	addFilePairFromStream: function(fileStream,fileNameStr, contentType,disposition,contentDescription, encoding)
	{
		str = this.CRLF + "--" + this.boundaryString + this.CRLF;
		str += "Content-Transfer-Encoding: "+encoding+ this.CRLF;
		str += "Content-Disposition: "+disposition+";" + this.CRLF + "\tfilename=\"" +
			this.makeRFC2047(fileNameStr) + "\"" + this.CRLF;
		if (contentDescription != null)
			str += "Content-Discription: "+contentDescription+this.CRLF;
		str += "Content-Type: " + contentType + this.CRLF + this.CRLF;

		this.addStringToStream(str);

		if (encoding == "base64")
		{
			try
			{
				var binInpStream =
				Components.classes[
				"@mozilla.org/binaryinputstream;1"].
				createInstance(Components.interfaces.nsIBinaryInputStream);
				binInpStream.setInputStream(fileStream);
				var bytes = binInpStream.readBytes(fileStream.available());
				var base64coded = FireGPG.Mime.breakLines(btoa(bytes));
				this.addStringToStream(base64coded);
			}
			catch (e)
			{
				alert("Error converting file to base64:"+e);
			}
		}
		else
		{ // not base64
			this.multipartStream.appendStream(fileStream);
		}

		// this.addStringToStream(this.CRLF);
	}, // end addFilePairFromStream

	addSignature: function(s)
	{
		this.addPlainStr(s,
						 'application/pkcs7-signature; name="smime.p7s"',
						 "base64",
						 'attachment; filename="smime.p7s"',
						 "S/MIME Cryptographic Signature");
	},

	addPlainStr: function(str,contentType,encoding,disposition, description)
	{
		var part = "";
		if (this.multiPart)
			part = "\r\n--"+this.boundaryString+this.CRLF;
		if (contentType != null)
			part += "Content-Type: "+contentType+this.CRLF;
		if (encoding != null)
			part += "Content-Transfer-Encoding: "+encoding+this.CRLF;
		if (disposition != null)
			part += "Content-Disposition: "+disposition+this.CRLF;
		if (description != null)
			part += "Content-Description: "+description+this.CRLF;
		part += this.CRLF + str;
		this.addStringToStream(part);
	},

	addLiteralPart: function(str)
	{
		var part = "";
		if (this.multiPart)
			part = this.CRLF + "--" + this.boundaryString+this.CRLF;
		part+=str; // do we need another CRLF here? PROBABLY NOT.
		this.addStringToStream(part);
	},
	addLiteral: function(str)
	{
		this.addStringToStream(str);
	},

	addBase64String: function(str,contentType,disposition, prefs)
	{
		var uChars = false;
		// NOTE: the str must be canonicalized from LF to CRLF
		// but JavaScript doesn't support Lookbehind /(?<!a)\n/g
		// but we can fake it with /([^\r])(?=\n)/g

		// TODO: deal with excessively long octet strings
		// without CRLF. But most mail clients seem to
		// handle...
		if (/[^\n]{999,}/.test(str))
		{
			// this forces base64 encoding, because we don't
			// want to deal with adding line breaks
			uChars = "us-ascii";
		}
		if (str.charAt(0) == "\n")
		{
			str = this.CRLF +
			str.substring(1).replace(/([^\r])(?=\n)/g,"$1\r");
		}
		else
		{
			str = str.replace(/([^\r])(?=\n)/g,"$1\r");
		}
		// now must turn Unicode into UTF-8. Fortunately,
		// there's a shortcut (from a coding standpoint;
		// sadly, it is not efficient) compare
		// decodeURIComponent(escape(
		//		unescape(encodeURIComponent("TEXT"))))
		/*for (var i = 0; i < str.length; i++)
		{
			if (str.charCodeAt(i) > 0xFF)
			{
				str = unescape(encodeURIComponent(str));
				uChars = "UTF-8";
				break;

			}
			else if (str.charCodeAt(i) > 0x7F)
			{
				// we can still get away with this because of
				// the identity transformation 0x80-0xFF
				uChars = "ISO-8859-1";
			}
		}*/

        //FireGPG: Force utf8
        str = unescape(encodeURIComponent(str));
		uChars = "UTF-8";


		var part="";
		if (this.multiPart)
		{
			// NOTE: SHOULD include leading CRLF in most cases, but we know
			part += "--"+this.boundaryString+this.CRLF;
		}
		// that this CRLF does not count because it's at the front of the stream in Gmail S/MIME.
		// examine
		if (contentType != null)
		{
			part += "Content-Type: "+contentType;
			if (uChars)
			{
				// ARGH! signText appears to only take ASCII
				// characters! So, we have to base64 it.
				part += "; charset=" +
					uChars + "\r\nContent-Transfer-Encoding: base64";
				str = btoa(str);
				str = FireGPG.Mime.breakLines(str);
			}
			part += this.CRLF;
		}
		if (disposition != null)
			part += "Content-Disposition: "+ disposition +this.CRLF;
		part += this.CRLF + str;





		this.addStringToStream(part);
	},

	makeRFC2047: function(s)
	{
		for (var i = 0; i < s.length; i++)
		{
			if (s.charCodeAt(i) > 0x7F)
			{
				return "=?utf-8?B?" + btoa(unescape(encodeURIComponent(s))) + "?=";
			}
		}
		return s;
	},

	/**
	 * @function addFilePair adds an attachment, specifies an encoding to use for that attachment, and specifies the disposition of the attachment.
	 * @param disposition: Valid values are defined in RFC2183. Namely, "inline" and "attachment".
	 * @param attachment: Any abstract attachment Object that is supported by this implementation. Valid values are:
	 *   nsIFile [partial support]
	 *   nsILocalFile
	 *   XMLHttpRequest (in this version, must already be processed with status code 200 OK)
	 *     TODO: check to see if contentType (inferred from Gmail HTML etc.) differs from the headers returned by the actual mail service HTTP request
	 *   nsIHttpChannel
	 *   [TODO:] nsIChannel (infer filename from URI)
	 *   String -> treated as nsILocalFile
	 */
	addFilePair: function(disposition, attachment, encoding)
	{
		var file;
		if (typeof(attachment) == "string") {
			file = Components.classes["@mozilla.org/file/local;1"].
			createInstance(Components.interfaces.nsILocalFile);
			try
			{
				file.initWithPath(attachment);
			}
			// if the fileName is spurious, then just don't try to add the file
			catch (e)
			{
				if (e.result === 0x80520001) return; // NS_ERROR_FILE_UNRECOGNIZED_PATH
				else throw e;
			}
		}
		else if (typeof(attachment) == "object")
		{
			if (attachment instanceof XMLHttpRequest)
			{
				if (attachment.status != 200) return false;
				var cD = new String(attachment.getResponseHeader("Content-Disposition"));
				cD.semicolon = cD.indexOf(";");
				cD.type = (cD.semicolon == -1) ? cD : cD.substring(0,cD.semicolon);
				if (cD.semicolon == -1 || (cD.fnpos = cD.indexOf("filename=")) == -1) cD.filename = null;
				else
				{
					// cd.filename should already be RFC 2047-compliant
					cD.filename = cD.substring((cD.q = cD[cD.fnpos+9] == "\"") ? cD.fnpos+10 : cD.fnpos+9);
					cD.filename = cD.filename.substring(0, cD.q ? cD.filename.indexOf("\"") : cD.filename.search(/\s/));
				}
				var cT = new String(attachment.getResponseHeader("Content-Type"));
				cT.semicolon = cT.indexOf(";");
				cT.type = (cT.semicolon == -1) ? cT : cT.substring(0,cT.semicolon);
				cT.type += ";"+this.CRLF+
				 "\tname=\""+ cD.filename +"\"";
				this.addFilePairFromString(attachment.responseText,cD.filename,cT.type, cD.type, encoding)
				return true;
			}
			else if ("channel" in attachment && "data" in attachment && attachment.channel instanceof Components.interfaces.nsIHttpChannel) // this is modified with a .data attached.
			{
				var cD = new String(attachment.channel.getResponseHeader("Content-Disposition"));
				cD.semicolon = cD.indexOf(";");
				cD.type = (cD.semicolon == -1) ? cD : cD.substring(0,cD.semicolon);
				if (cD.semicolon == -1 || (cD.fnpos = cD.indexOf("filename=")) == -1) cD.filename = null;
				else
				{
					// cd.filename should already be RFC 2047-compliant
					cD.filename = cD.substring((cD.q = cD[cD.fnpos+9] == "\"") ? cD.fnpos+10 : cD.fnpos+9);
					cD.filename = cD.filename.substring(0, cD.q ? cD.filename.indexOf("\"") : cD.filename.search(/\s/));
				}
				var cT = new String(attachment.channel.getResponseHeader("Content-Type"));
				cT.semicolon = cT.indexOf(";");
				cT.type = (cT.semicolon == -1) ? cT : cT.substring(0,cT.semicolon);
				cT.type += ";"+this.CRLF+
				 "\tname=\""+ cD.filename +"\"";

				str = this.CRLF + "--" + this.boundaryString + this.CRLF;
				str += "Content-Transfer-Encoding: "+encoding+ this.CRLF;
				str += "Content-Disposition: "+cD.type+";" + this.CRLF + "\tfilename=\"" +
					this.makeRFC2047(cD.filename) + "\"" + this.CRLF;
				str += "Content-Type: " + cT.type + this.CRLF + this.CRLF;

				str += FireGPG.Mime.breakLines(btoa(attachment.data));
				this.addStringToStream(str);
				return true;
			}
			else if (!(attachment instanceof Components.interfaces.nsIFile)) return false;
			file = attachment;
		}
		var contentType = "";

		var mimeService =
		Components.classes["@mozilla.org/mime;1"].
		getService(Components.interfaces.nsIMIMEService);
		try
		{
			contentType = mimeService.getTypeFromFile(file);
		}
		catch (e)
		{
			// common throw: 0x80040111 NS_ERROR_NOT_AVAILABLE
			contentType = "application/octet-stream";
		}
		contentType += ";"+this.CRLF+
		"\tname=\""+ this.makeRFC2047(file.leafName) +"\"";
		var fileInputStream=
		Components.classes["@mozilla.org/network/file-input-stream;1"].
		createInstance(Components.interfaces.nsIFileInputStream);

		fileInputStream.init(file,0x01,0600,fileInputStream.CLOSE_ON_EOF);
		var buffer =
		Components.classes["@mozilla.org/network/buffered-input-stream;1"].
			createInstance(Components.interfaces.nsIBufferedInputStream);
			buffer.init(fileInputStream,8192);
			this.addFilePairFromStream(buffer,file.leafName,contentType,disposition,null,encoding);
		return true;
	},

	// Get stream without the multipart dividers or extra header,
	// used for once off
	// encoding of single parts.
	getSinglePartStream: function()
	{
		return this.multipartStream;
	},

	getMultipartStream: function(multipartType,isLengthRequired)
	{
		this.addStringToStream(this.CRLF + "--" + this.boundaryString + "--" + this.CRLF);
		var mimeStream =
		Components.classes["@mozilla.org/network/mime-input-stream;1"].
		createInstance(Components.interfaces.nsIMIMEInputStream);
		mimeStream.addContentLength = isLengthRequired;
		mimeStream.addHeader("Content-Type",
							 multipartType+"; boundary=\""+this.boundaryString + "\"");
		mimeStream.setData(this.multipartStream);
		return mimeStream;
	},

	streamFromString: function(str,file,fstream)
	{
		var foStream =
		Components.classes["@mozilla.org/network/file-output-stream;1"]
		.createInstance(Components.interfaces.nsIFileOutputStream);
		// write, create, truncate
		foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0);

		var binOutStream = Components.classes[
											  "@mozilla.org/binaryoutputstream;1"].
		createInstance(Components.interfaces.nsIBinaryOutputStream);
		binOutStream.setOutputStream(foStream);
		for (var i=0;i<str.length;i++)
		{
			binOutStream.write8(str.charCodeAt(i) & 0xFF);
		}

		binOutStream.close();
		// Does previous close do this? XXX
		foStream.close();

		fstream.init(file, 1, 0, false);

		var bufStream =
		Components.classes["@mozilla.org/network/buffered-input-stream;1"]
		.createInstance(Components.interfaces.nsIBufferedInputStream);
		bufStream.init(fstream, 512);

		return bufStream;
	},

	upload: function(url,referer,targetName,tmpfile,tmpfilestream)
	{
		var ioService = Components.classes["@mozilla.org/network/io-service;1"]
		.getService(Components.interfaces.nsIIOService);

		var contentType = null;

		var uri		= ioService.newURI(url,null,null);
		var referrerUri		= ioService.newURI(referer,null,null);

		ioChannel = ioService.newChannelFromURI(uri);
		var httpChannel =
		ioChannel.QueryInterface(
								 Components.interfaces.nsIHttpChannel
								 );
		httpChannel.requestMethod = "POST";
		httpChannel.setRequestHeader("Referer",referer,false);
		var uploadChannel =
		httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
		uploadChannel.
		setUploadStream(
						this.getMultipartStream("multipart/form-data",true),
						contentType,-1);

		httpChannel.requestMethod = "POST";
		httpChannel.setRequestHeader("Referer",referer,false);

		uploadChannel.
		asyncOpen(
				  {
				  onStartRequest: function(request, context)
				  {
						  //		  alert("Started upload");
				  },
				  onStopRequest: function(request, context, status, errorMsg)
				  {
						//alert("Finished upload");
					  tmpfilestream.close();
					  tmpfile.remove(false);
				  },
				  onDataAvailable: function(request, context, inStream, offset, count)
				  {
				  }
				  }
				  , null);
	},

	init: function(multiPart,partNum)
	{
		// the boundary string should be unique, especially if
		// one forwards and encapsulates previous messages generated
		// with Gmail S/MIME. A good approximation is to use a nonce-like value,
		// but one that is a little bit different from non-encrypted values,
		// like the Message-ID.
		this.boundaryString = "firegpg" + FireGPG.Const.VersionA + "eq" + (Math.round(Math.random()*99)+(new Date()).getTime()).toString(36) +
		(99+Math.round(46656*46656*46635*36*Math.random())).toString(36) +
		+partNum;
		this.multiPart = multiPart;
		this.multipartStream=
		Components.classes["@mozilla.org/io/multiplex-input-stream;1"].
		createInstance(Components.interfaces.nsIMultiplexInputStream);
	}
}; // end FireGPG.Mime.Encoder class