Merge pull request #272 from laktak/compress

Compression Support
This commit is contained in:
Tankred Hase 2014-12-09 15:36:41 +01:00
commit bed393063d
5 changed files with 35 additions and 1301 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ build/
.DS_Store .DS_Store
node_modules/ node_modules/
npm* npm*
src/compression/
test/lib/ test/lib/
dist/ dist/
openpgp.store/ openpgp.store/

View File

@ -118,6 +118,12 @@ module.exports = function(grunt) {
cwd: 'node_modules/', cwd: 'node_modules/',
src: ['mocha/mocha.css', 'mocha/mocha.js', 'chai/chai.js'], src: ['mocha/mocha.css', 'mocha/mocha.js', 'chai/chai.js'],
dest: 'test/lib/' dest: 'test/lib/'
},
zlib: {
expand: true,
cwd: 'node_modules/zlibjs/bin/',
src: ['rawdeflate.min.js','rawinflate.min.js','zlib.min.js'],
dest: 'src/compression/'
} }
}, },
clean: ['dist/'], clean: ['dist/'],
@ -145,7 +151,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-connect'); grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('default', 'Build OpenPGP.js', function() { grunt.registerTask('default', 'Build OpenPGP.js', function() {
grunt.task.run(['clean', 'browserify', 'replace', 'uglify', 'npm_pack']); grunt.task.run(['clean', 'copy:zlib', 'browserify', 'replace', 'uglify', 'npm_pack']);
//TODO jshint is not run because of too many discovered issues, once these are addressed it should autorun //TODO jshint is not run because of too many discovered issues, once these are addressed it should autorun
grunt.log.ok('Before Submitting a Pull Request please also run `grunt jshint`.'); grunt.log.ok('Before Submitting a Pull Request please also run `grunt jshint`.');
}); });
@ -182,5 +188,5 @@ module.exports = function(grunt) {
}); });
// Test/Dev tasks // Test/Dev tasks
grunt.registerTask('test', ['copy', 'mochaTest', 'mocha_phantomjs']); grunt.registerTask('test', ['copy:npm', 'mochaTest', 'mocha_phantomjs']);
}; };

View File

@ -47,7 +47,8 @@
}, },
"dependencies": { "dependencies": {
"es6-promise": "^1.0.0", "es6-promise": "^1.0.0",
"node-localstorage": "~0.3.4" "node-localstorage": "~0.3.4",
"zlibjs": "^0.2.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

File diff suppressed because it is too large Load Diff

View File

@ -21,17 +21,21 @@
* {@link http://tools.ietf.org/html/rfc4880#section-5.6|RFC4880 5.6}: The Compressed Data packet contains compressed data. Typically, * {@link http://tools.ietf.org/html/rfc4880#section-5.6|RFC4880 5.6}: The Compressed Data packet contains compressed data. Typically,
* this packet is found as the contents of an encrypted packet, or following * this packet is found as the contents of an encrypted packet, or following
* a Signature or One-Pass Signature packet, and contains a literal data packet. * a Signature or One-Pass Signature packet, and contains a literal data packet.
* @requires compression/jxg * @requires compression/zlib
* @requires encoding/base64 * @requires compression/rawinflate
* @requires compression/rawdeflate
* @requires enums * @requires enums
* @requires util
* @module packet/compressed * @module packet/compressed
*/ */
module.exports = Compressed; module.exports = Compressed;
var enums = require('../enums.js'), var enums = require('../enums.js'),
JXG = require('../compression/jxg.js'), util = require('../util.js'),
base64 = require('../encoding/base64.js'); Zlib = require('../compression/zlib.min.js'),
RawInflate = require('../compression/rawinflate.min.js'),
RawDeflate = require('../compression/rawdeflate.min.js');
/** /**
* @constructor * @constructor
@ -51,7 +55,7 @@ function Compressed() {
* Compression algorithm * Compression algorithm
* @type {compression} * @type {compression}
*/ */
this.algorithm = 'uncompressed'; this.algorithm = 'zip';
/** /**
* Compressed packet data * Compressed packet data
@ -93,7 +97,7 @@ Compressed.prototype.write = function () {
* read by read_packet * read by read_packet
*/ */
Compressed.prototype.decompress = function () { Compressed.prototype.decompress = function () {
var decompressed, compdata, radix; var decompressed;
switch (this.algorithm) { switch (this.algorithm) {
case 'uncompressed': case 'uncompressed':
@ -101,35 +105,13 @@ Compressed.prototype.decompress = function () {
break; break;
case 'zip': case 'zip':
compData = this.compressed; var inflate = new RawInflate.Zlib.RawInflate(util.str2Uint8Array(this.compressed));
decompressed = util.Uint8Array2str(inflate.decompress());
radix = base64.encode(compData).replace(/\n/g, "");
// no header in this case, directly call deflate
var jxg_obj = new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(radix));
decompressed = unescape(jxg_obj.deflate()[0][0]);
break; break;
case 'zlib': case 'zlib':
//RFC 1950. Bits 0-3 Compression Method var inflate = new Zlib.Zlib.Inflate(util.str2Uint8Array(this.compressed));
var compressionMethod = this.compressed.charCodeAt(0) % 0x10; decompressed = util.Uint8Array2str(inflate.decompress());
//Bits 4-7 RFC 1950 are LZ77 Window. Generally this value is 7 == 32k window size.
// 2nd Byte in RFC 1950 is for "FLAGs" Allows for a Dictionary
// (how is this defined). Basic checksum, and compression level.
if (compressionMethod == 8) { //CM 8 is for DEFLATE, RFC 1951
// remove 4 bytes ADLER32 checksum from the end
compData = this.compressed.substring(0, this.compressed.length - 4);
radix = base64.encode(compData).replace(/\n/g, "");
//TODO check ADLER32 checksum
decompressed = JXG.decompress(radix);
break;
} else {
throw new Error("Compression algorithm ZLIB only supports " +
"DEFLATE compression method.");
}
break; break;
case 'bzip2': case 'bzip2':
@ -147,21 +129,27 @@ Compressed.prototype.decompress = function () {
* Compress the packet data (member decompressedData) * Compress the packet data (member decompressedData)
*/ */
Compressed.prototype.compress = function () { Compressed.prototype.compress = function () {
var uncompressed, deflate;
uncompressed = this.packets.write();
switch (this.algorithm) { switch (this.algorithm) {
case 'uncompressed': case 'uncompressed':
// - Uncompressed // - Uncompressed
this.compressed = this.packets.write(); this.compressed = uncompressed;
break; break;
case 'zip': case 'zip':
// - ZIP [RFC1951] // - ZIP [RFC1951]
throw new Error("Compression algorithm ZIP [RFC1951] is not implemented."); deflate = new RawDeflate.Zlib.RawDeflate(util.str2Uint8Array(uncompressed));
this.compressed = util.Uint8Array2str(deflate.compress());
break;
case 'zlib': case 'zlib':
// - ZLIB [RFC1950] // - ZLIB [RFC1950]
// TODO: need to implement this deflate = new Zlib.Zlib.Deflate(util.str2Uint8Array(uncompressed));
throw new Error("Compression algorithm ZLIB [RFC1950] is not implemented."); this.compressed = util.Uint8Array2str(deflate.compress());
break;
case 'bzip2': case 'bzip2':
// - BZip2 [BZ2] // - BZip2 [BZ2]