From ce2a11ca61ec72c34df7fdd68e7bb552b477c7e4 Mon Sep 17 00:00:00 2001 From: mmso Date: Wed, 14 Feb 2018 13:55:43 +0100 Subject: [PATCH 1/2] Add method to build bzip2 cleanly --- build.js | 27 +++++++++++++++++++++++++++ package.json | 5 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 build.js diff --git a/build.js b/build.js new file mode 100644 index 0000000..5f87932 --- /dev/null +++ b/build.js @@ -0,0 +1,27 @@ +({ + baseUrl: "./lib", + optimize: "uglify", + out: "bin/bzip2.build.js", + name: 'Bzip2', + wrap: false, + preserveLicenseComments: false, + keepAmdefine: false, + findNestedDependencies: true, + onModuleBundleComplete: function (data) { + // Clean any AMD usage. + var fs = module.require('fs'), + amdclean = module.require('amdclean'), + outputFile = data.path, + cleanedCode = amdclean.clean({ + 'filePath': outputFile + }); + // Remove the compatibility usage. + cleanedCode = cleanedCode.replace(/if \(true\) {\n var define = amdefine\(module\);\n}/g, ''); + // Remove the wrap around. + cleanedCode = cleanedCode.replace(';(function() {', ''); + // Replace with a module.exports. + cleanedCode = cleanedCode.replace('}());', 'module.exports = Bzip2;'); + + fs.writeFileSync(outputFile, cleanedCode); + } +}); diff --git a/package.json b/package.json index faafcee..16fb828 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,12 @@ "commander": "~2.8.1" }, "devDependencies": { - "mocha": "~2.2.5" + "amdclean": "^2.7.0", + "mocha": "~2.2.5", + "requirejs": "^2.3.5" }, "scripts": { + "build": "r.js -o build.js", "test": "mocha" }, "amd": {}, From 7c93f0aa85389513d0488de5695b4efbd00ef609 Mon Sep 17 00:00:00 2001 From: mmso Date: Wed, 14 Feb 2018 13:55:49 +0100 Subject: [PATCH 2/2] Add bzip2 build --- bin/bzip2.build.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 bin/bzip2.build.js diff --git a/bin/bzip2.build.js b/bin/bzip2.build.js new file mode 100644 index 0000000..f228160 --- /dev/null +++ b/bin/bzip2.build.js @@ -0,0 +1 @@ +var freeze,Stream,BitStream,Util,BWT,CRC32,HuffmanAllocator,Bzip2;freeze=function(){return Object.freeze?Object.freeze:function(e){return e}}(),Stream=function(e){var t=function(){};return t.prototype.readByte=function(){var e=[0];return 0===this.read(e,0,1)?(this._eof=!0,-1):e[0]},t.prototype.read=function(e,t,r){for(var n,i=0;i>>3,r=e-8*t;this.seek(t),this._eof=!1,this.readBits(r)},this.tellBit=function(){for(var e=8*t.tell(),n=r;0!=(255&n);)e--,n<<=1;return e},this.readByte=function(){return 0==(255&r)?t.readByte():this.readBits(8)},this.seek=function(e){t.seek(e),r=256}}).call(this),function(){var e=1;this.writeBit=function(r){e<<=1,r&&(e|=1),256&e&&(t.writeByte(255&e),e=1)},this.writeByte=function(r){1===e?t.writeByte(r):t.writeBits(8,r)},this.flush=function(){for(;1!==e;)this.writeBit(0);t.flush&&t.flush()}}.call(this)};return t.EOF=e.EOF,t.prototype=Object.create(e.prototype),t.prototype.readBits=function(e){var t,r=0;if(e>31)return(r=65536*this.readBits(e-16))+this.readBits(16);for(t=0;t0&&r++;return r},t.prototype.writeBits=function(e,t){if(e>32){var r=65535&t,n=(t-r)/65536;return this.writeBits(e-16,n),void this.writeBits(16,r)}var i;for(i=e-1;i>=0;i--)this.writeBit(t>>>i&1)},t}(Stream),Util=function(e,t){var r=Object.create(null),n=t.EOF;r.coerceInputStream=function(e,r){if("readByte"in e){if(r&&!("read"in e)){var i=e;e=new t,e.readByte=function(){var e=i.readByte();return e===n&&(this._eof=!0),e},"size"in i&&(e.size=i.size),"seek"in i&&(e.seek=function(e){i.seek(e),this._eof=!1}),"tell"in i&&(e.tell=i.tell.bind(i))}}else{var o=e;e=new t,e.size=o.length,e.pos=0,e.readByte=function(){return this.pos>=this.size?n:o[this.pos++]},e.read=function(e,t,r){for(var n=0;n=o.length}}return e};var i=function(e,t){this.buffer=e,this.resizeOk=t,this.pos=0};i.prototype=Object.create(t.prototype),i.prototype.writeByte=function(e){if(this.resizeOk&&this.pos>=this.buffer.length){var t=r.makeU8Buffer(2*this.buffer.length);t.set(this.buffer),this.buffer=t}this.buffer[this.pos++]=e},i.prototype.getBuffer=function(){if(this.pos!==this.buffer.length){if(!this.resizeOk)throw new TypeError("outputsize does not match decoded input");var e=r.makeU8Buffer(this.pos);e.set(this.buffer.subarray(0,this.pos)),this.buffer=e}return this.buffer},r.coerceOutputStream=function(e,t){var n={stream:e,retval:e};if(e){if("object"==typeof e&&"writeByte"in e)return n;"number"==typeof t?(console.assert(t>=0),n.stream=new i(r.makeU8Buffer(t),!1)):n.stream=new i(e,!1)}else n.stream=new i(r.makeU8Buffer(16384),!0);return Object.defineProperty(n,"retval",{get:n.stream.getBuffer.bind(n.stream)}),n},r.compressFileHelper=function(e,t,n){return function(i,o,f){i=r.coerceInputStream(i);var a=r.coerceOutputStream(o,o);o=a.stream;var u;for(u=0;u=0?i.size:-1,n){var c=r.coerceOutputStream([]);for(r.writeUnsignedNumber(c.stream,s+1),c=c.retval,u=0;u=0);var r,n=[];do{n.push(127&t),t=Math.floor(t/128)}while(0!==t);for(n[0]|=128,r=n.length-1;r>=0;r--)e.writeByte(n[r]);return e},r.readUnsignedNumber=function(e){for(var t,r=0;;){if(128&(t=e.readByte())){r+=127&t;break}r=128*(r+t)}return r};var o=function(e){for(var t=0,r=e.length;t=t.length);for(var r=0,n=t.length;r=0),e>4294967295?32+s(Math.floor(e/4294967296)):0!=(4294901760&e)?0!=(4278190080&e)?24+u[e>>>24&255]:16+u[e>>>16]:0!=(65280&e)?8+u[e>>>8]:u[e]};return r.log2c=function(e){return 0===e?-1:s(e-1)},e(r)}(freeze,Stream),BWT=function(e,t){var r=console.assert.bind(console),n=function(e,t,r,n){var i;for(i=0;i0?(r(e[h]>=e[h+1]),(l=e[h])!==d&&(f[d]=s,s=f[d=l]),r(c=0;c--)(h=t[c])>0&&(r(e[h]<=e[h+1]),(l=e[h])!==d&&(f[d]=s,s=f[d=l]),r(s<=c),h--,t[--s]=e[h]>d?~(h+1):h,t[c]=0)},f=function(e,t,n,i){var o,f,a,u,s,c,h,l,d,B;for(r(n>0),o=0;(a=t[o])<0;o++)t[o]=~a,r(o+1=0&&(l=e[o])>=d);for(;o>=0;){do{d=l}while(--o>=0&&(l=e[o])<=d);if(o>=0){t[i+(o+1>>>1)]=f-o,f=o+1;do{d=l}while(--o>=0&&(l=e[o])>=d)}}for(o=0,h=0,u=n,c=0;o>>1)],B=!0,s===c&&u+s>>1)]=h}return h},a=function(e,t,o,f,a,u){var s,c,h,l,d;for(o===f&&n(e,o,a,u),i(o,f,u,!1),h=a-1,s=f[d=e[h]],t[s++]=h>0&&e[h-1]0&&(h--,r(e[h]>=e[h+1]),(l=e[h])!==d&&(f[d]=s,s=f[d=l]),r(c0&&e[h-1]=0;c--)(h=t[c])>0?(h--,r(e[h]<=e[h+1]),(l=e[h])!==d&&(f[d]=s,s=f[d=l]),r(s<=c),t[--s]=0===h||e[h-1]>d?~h:h):t[c]=~h},u=function(e,t,o,f,a,u){var s,c,h,l,d,B=-1;for(o===f&&n(e,o,a,u),i(o,f,u,!1),h=a-1,s=f[d=e[h]],t[s++]=h>0&&e[h-1]0?(h--,r(e[h]>=e[h+1]),t[c]=~(l=e[h]),l!==d&&(f[d]=s,s=f[d=l]),r(c0&&e[h-1]=0;c--)(h=t[c])>0?(h--,r(e[h]<=e[h+1]),t[c]=l=e[h],l!==d&&(f[d]=s,s=f[d=l]),r(s<=c),t[--s]=h>0&&e[h-1]>d?~e[h-1]:h):0!==h?t[c]=~h:B=c;return B},s=function(e,c,h,l,d,B){var p,v,m,w,E,g,_,b,y,R,C,k,T,O=0,S=0;for(d<=256?(p=t.makeS32Buffer(d),d<=h?(v=c.subarray(l+h-d),S=1):(v=t.makeS32Buffer(d),S=3)):d<=h?(p=c.subarray(l+h-d),d<=h-d?(v=c.subarray(l+h-2*d),S=0):d<=1024?(v=t.makeS32Buffer(d),S=2):(v=p,S=8)):(p=v=t.makeS32Buffer(d),S=12),n(e,p,l,d),i(p,v,d,!0),w=0;w=0&&(k=e[w])>=T);for(;w>=0;){do{T=k}while(--w>=0&&(k=e[w])<=T);if(w>=0){g>=0&&(c[g]=E),g=--v[T],E=w,++_;do{T=k}while(--w>=0&&(k=e[w])>=T)}}if(_>1?(o(e,c,p,v,l,d),R=f(e,c,l,_)):1===_?(c[g]=E+1,R=1):R=0,R<_){for(0!=(4&S)&&(p=null,v=null),0!=(2&S)&&(v=null),C=l+h-2*_,0==(13&S)&&(d+R<=C?C-=d:S|=8),r(l>>>1<=C+_),w=_+(l>>>1)-1,E=2*_+C-1;_<=w;w--)0!==c[w]&&(c[E--]=c[w]-1);m=c.subarray(_+C),s(m,c,C,_,R,!1),m=null,w=l-1,E=2*_-1,k=e[l-1];do{T=k}while(--w>=0&&(k=e[w])>=T);for(;w>=0;){do{T=k}while(--w>=0&&(k=e[w])<=T);if(w>=0){c[E--]=w+1;do{T=k}while(--w>=0&&(k=e[w])>=T)}}for(w=0;w<_;w++)c[w]=c[_+c[w]];0!=(4&S)&&(p=v=t.makeS32Buffer(d)),0!=(2&S)&&(v=t.makeS32Buffer(d))}if(0!=(8&S)&&n(e,p,l,d),_>1){i(p,v,d,!0),w=_-1,E=l,b=c[_-1],T=e[b];do{for(y=v[k=T];y=0);for(;E>0;)c[--E]=0}return B?O=u(e,c,p,v,l,d):a(e,c,p,v,l,d),p=null,v=null,O},c=Object.create(null);return c.suffixsort=function(e,t,n,i){if(r(e&&t&&e.length>=n&&t.length>=n),n<=1)return 1===n&&(t[0]=0),0;if(!i)if(1===e.BYTES_PER_ELEMENT)i=256;else{if(2!==e.BYTES_PER_ELEMENT)throw new Error("Need to specify alphabetSize");i=65536}return r(i>0),e.BYTES_PER_ELEMENT&&r(i<=1<<8*e.BYTES_PER_ELEMENT),s(e,t,0,n,i,!1)},c.bwtransform=function(e,t,n,i,o){var f,a;if(r(e&&t&&n),r(e.length>=i&&t.length>=i&&n.length>=i),i<=1)return 1===i&&(t[0]=e[0]),i;if(!o)if(1===e.BYTES_PER_ELEMENT)o=256;else{if(2!==e.BYTES_PER_ELEMENT)throw new Error("Need to specify alphabetSize");o=65536}for(r(o>0),e.BYTES_PER_ELEMENT&&r(o<=1<<8*e.BYTES_PER_ELEMENT),a=s(e,n,0,i,o,!0),t[0]=e[i-1],f=0;f=0;f--)a=n[a]+u[r[f]=e[a]],a+=a=i&&n.length>=i),i<=1)return 1===i&&(n[0]=e[0]),0;if(!o)if(1===e.BYTES_PER_ELEMENT)o=256;else{if(2!==e.BYTES_PER_ELEMENT)throw new Error("Need to specify alphabetSize");o=65536}r(o>0),e.BYTES_PER_ELEMENT&&r(o<=1<<8*e.BYTES_PER_ELEMENT);var c;if((c=e.length>=2*i?e:o<=256?t.makeU8Buffer(2*i):o<=65536?t.makeU16Buffer(2*i):t.makeU32Buffer(2*i))!==e)for(f=0;f>>0},this.updateCRC=function(r){e=e<<8^t[255&(e>>>24^r)]},this.updateCRCRun=function(r,n){for(;n-- >0;)e=e<<8^t[255&(e>>>24^r)]}}}(Util),HuffmanAllocator=function(e,t){var r=function(e,t,r){for(var n=e.length,i=t,o=e.length-2;t>=r&&e[t]%n>i;)o=t,t-=i-t+1;for(t=Math.max(r-1,t);o>t+1;){var f=t+o>>1;e[f]%n>i?o=f:t=f}return o},n=function(e){var t=e.length;e[0]+=e[1];var r,n,i,o;for(r=0,n=1,i=2;n=t||e[r]=t||r1;n++)i=r(e,i-1,0);return i},o=function(e){var t,n,i,o,f=e.length-2,a=e.length-1;for(t=1,n=2;n>0;t++){for(i=f,f=r(e,i-1,0),o=n-(i-f);o>0;o--)e[a--]=t;n=i-f<<1}},f=function(e,t,n){var i,o,f,a,u=e.length-2,s=e.length-1,c=1==n?2:1,h=1==n?t-2:t;for(i=c<<1;i>0;c++){for(o=u,u=u<=t?u:r(e,o-1,t),f=0,c>=n?f=Math.min(h,1<0;a--)e[s--]=c;h-=f,i=o-u+f<<1}};return e({allocateHuffmanCodeLengths:function(e,r){switch(e.length){case 2:e[1]=1;case 1:return void(e[0]=1)}n(e);var a=i(e,r);if(e[0]%e.length>=a)o(e);else{var u=r-t.fls(a-1);f(e,a,u)}}})}(freeze,Util),Bzip2=function(e,t,r,n,i,o,f){var a=o.EOF,u=function(e,t){var r,n=e[t];for(r=t;r>0;r--)e[r]=e[r-1];return e[0]=n,n},s={OK:0,LAST_BLOCK:-1,NOT_BZIP_DATA:-2,UNEXPECTED_INPUT_EOF:-3,UNEXPECTED_OUTPUT_EOF:-4,DATA_ERROR:-5,OUT_OF_MEMORY:-6,OBSOLETE_INPUT:-7,END_OF_BLOCK:-8},c={};c[s.LAST_BLOCK]="Bad file checksum",c[s.NOT_BZIP_DATA]="Not bzip data",c[s.UNEXPECTED_INPUT_EOF]="Unexpected input EOF",c[s.UNEXPECTED_OUTPUT_EOF]="Unexpected output EOF",c[s.DATA_ERROR]="Data error",c[s.OUT_OF_MEMORY]="Out of memory",c[s.OBSOLETE_INPUT]="Obsolete (pre 0.9.5) bzip format not supported.";var h=function(e,t){var r=c[e]||"unknown error";t&&(r+=": "+t);var n=new TypeError(r);throw n.errorCode=e,n},l=function(e,t){this.writePos=this.writeCurrent=this.writeCount=0,this._start_bunzip(e,t)};l.prototype._init_block=function(){return this._get_next_block()?(this.blockCRC=new n,!0):(this.writeCount=-1,!1)},l.prototype._start_bunzip=function(e,r){var n=f.makeU8Buffer(4);4===e.read(n,0,4)&&"BZh"===String.fromCharCode(n[0],n[1],n[2])||h(s.NOT_BZIP_DATA,"bad magic");var i=n[3]-48;(i<1||i>9)&&h(s.NOT_BZIP_DATA,"level out of range"),this.reader=new t(e),this.dbufSize=1e5*i,this.nextoutput=0,this.outputStream=r,this.streamCRC=0},l.prototype._get_next_block=function(){var e,t,r,n=this.reader,i=n.readBits(48);if(25779555029136===i)return!1;54156738319193!==i&&h(s.NOT_BZIP_DATA),this.targetBlockCRC=n.readBits(32),this.streamCRC=(this.targetBlockCRC^(this.streamCRC<<1|this.streamCRC>>>31))>>>0,n.readBits(1)&&h(s.OBSOLETE_INPUT);var o=n.readBits(24);o>this.dbufSize&&h(s.DATA_ERROR,"initial position out of bounds");var a=n.readBits(16),c=f.makeU8Buffer(256),l=0;for(e=0;e<16;e++)if(a&1<<15-e){var d=16*e;for(r=n.readBits(16),t=0;t<16;t++)r&1<<15-t&&(c[l++]=d+t)}var B=n.readBits(3);(B<2||B>6)&&h(s.DATA_ERROR);var p=n.readBits(15);0===p&&h(s.DATA_ERROR);var v=f.makeU8Buffer(256);for(e=0;e=B&&h(s.DATA_ERROR);m[e]=u(v,t)}var w,E=l+2,g=[];for(t=0;t20)&&h(s.DATA_ERROR),n.readBits(1);)n.readBits(1)?a--:a++;_[e]=a}var y,R;for(y=R=_[0],e=1;eR?R=_[e]:_[e]=p&&h(s.DATA_ERROR),w=g[m[U++]]),e=w.minLen,t=n.readBits(e);e>w.maxLen&&h(s.DATA_ERROR),!(t<=w.limit[e]);e++)t=t<<1|n.readBits(1);t-=w.base[e],(t<0||t>=258)&&h(s.DATA_ERROR);var z=w.permute[t];if(0!==z&&1!==z){if(O)for(O=0,S+a>this.dbufSize&&h(s.DATA_ERROR),T=c[v[0]],k[T]+=a;a--;)A[S++]=T;if(z>l)break;S>=this.dbufSize&&h(s.DATA_ERROR),e=z-1,T=u(v,e),T=c[T],k[T]++,A[S++]=T}else O||(O=1,a=0),a+=0===z?O:2*O,O<<=1}for((o<0||o>=S)&&h(s.DATA_ERROR),t=0,e=0;e<256;e++)r=t+k[e],k[e]=t,t=r;for(e=0;e>=8,P=-1),this.writePos=N,this.writeCurrent=L,this.writeCount=S,this.writeRun=P,!0},l.prototype._read_bunzip=function(e,t){var r,n,i;if(this.writeCount<0)return 0;for(var o=this.dbuf,f=this.writePos,a=this.writeCurrent,u=this.writeCount,c=(this.outputsize,this.writeRun);u;){for(u--,n=a,f=o[f],a=255&f,f>>=8,3==c++?(r=a,i=n,a=-1):(r=1,i=a),this.blockCRC.updateCRCRun(i,r);r--;)this.outputStream.writeByte(i),this.nextoutput++;a!=n&&(c=0)}return this.writeCount=u,this.blockCRC.getCRC()!==this.targetBlockCRC&&h(s.DATA_ERROR,"Bad block CRC (got "+this.blockCRC.getCRC().toString(16)+" expected "+this.targetBlockCRC.toString(16)+")"),this.nextoutput},l.Err=s,l.decode=function(e,t,r){for(var n=f.coerceInputStream(e),i=f.coerceOutputStream(t,t),o=i.stream,a=new l(n,o);;){if("eof"in n&&n.eof())break;if(a._init_block())a._read_bunzip();else{var u=a.reader.readBits(32);if(u!==a.streamCRC&&h(s.DATA_ERROR,"Bad stream CRC (got "+a.streamCRC.toString(16)+" expected "+u.toString(16)+")"),!(r&&"eof"in n)||n.eof())break;a._start_bunzip(n,o)}}return i.retval},l.decodeBlock=function(e,t,r){var i=f.coerceInputStream(e),o=f.coerceOutputStream(r,r),a=o.stream,u=new l(i,a);return u.reader.seekBit(t),u._get_next_block()&&(u.blockCRC=new n,u.writeCopies=0,u._read_bunzip()),o.retval},l.table=function(e,t,r){var n=new o;n.delegate=f.coerceInputStream(e),n.pos=0,n.readByte=function(){return this.pos++,this.delegate.readByte()},n.tell=function(){return this.pos},n.delegate.eof&&(n.eof=n.delegate.eof.bind(n.delegate));var i=new o;i.pos=0,i.writeByte=function(){this.pos++};for(var a=new l(n,i),u=a.dbufSize;;){if("eof"in n&&n.eof())break;var s=a.reader.tellBit();if(a._init_block()){var c=i.pos;a._read_bunzip(),t(s,i.pos-c)}else{a.reader.readBits(32);if(!(r&&"eof"in n)||n.eof())break;a._start_bunzip(n,i),console.assert(a.dbufSize===u,"shouldn't change block size within multistream file")}}};var d=function(e,t){var r,n=[];for(r=0;r>>9});for(i.allocateHuffmanCodeLengths(o,20),this.codeLengths=f.makeU8Buffer(t),r=0;r>>9,a=511&r[e];console.assert(i<=o),n<<=o-i,this.code[a]=n++,i=o}},d.prototype.cost=function(e,t,r){var n,i=0;for(n=0;n0&&o<=20),r0;)e.writeBits(2,n);e.writeBit(0),r=o}},d.prototype.encode=function(e,t){e.writeBits(this.codeLengths[t],this.code[t])};var B=function(e,t,r,n){for(var i=0,o=-1,f=0;i=r));){var u=e.readByte();if(u===a)break;if(n.updateCRC(u),u!==o)o=u,f=1;else if(++f>4){if(f<256){t[i-1]++;continue}f=1}t[i++]=u}return i},p=function(e,t,r){var n,i,o;for(n=0,o=0;n>>1;o>>4]=!0;for(o=0;o<16;o++)n.writeBit(!!B[o]);for(o=0;o<16;o++)if(B[o])for(a=0;a<16;a++)n.writeBit(!!l[o<<4|a]);var m=0;for(o=0;o<256;o++)l[o]&&m++;var w=f.makeU16Buffer(t+1),E=m+1,g=[];for(o=0;o<=E;o++)g[o]=0;var _=f.makeU8Buffer(m);for(o=0,a=0;o<256;o++)l[o]&&(_[a++]=o);l=null,B=null;var b=0,y=0,R=function(e){w[b++]=e,g[e]++},C=function(){for(;0!==y;)1&y?(R(0),y-=1):(R(1),y-=2),y>>>=1};for(o=0;o=2400?6:b>=1200?5:b>=600?4:b>=200?3:2,T.push(new d(g,E+1)),o=0;o<=E;o++)g[o]=1;T.push(new d(g,E+1)),g=null;var O=f.makeU8Buffer(Math.ceil(b/50));for(v(T,k,w,O,E+1),p(O,T,w),console.assert(T.length>=2&&T.length<=6),n.writeBits(3,T.length),n.writeBits(15,O.length),o=0;o0;a--)n.writeBit(1);n.writeBit(0)}for(o=0;o9)throw new Error("Invalid block size multiplier");var u=1e5*a;u-=19,r.writeByte("B".charCodeAt(0)),r.writeByte("Z".charCodeAt(0)),r.writeByte("h".charCodeAt(0)),r.writeByte("0".charCodeAt(0)+a);var s,c=f.makeU8Buffer(u),h=0;do{var l=new n;s=B(e,c,u,l),s>0&&(h=((h<<1|h>>>31)^l.getCRC())>>>0,r.writeBits(48,54156738319193),r.writeBits(32,l.getCRC()),m(c,s,r))}while(s===u);return r.writeBits(48,25779555029136),r.writeBits(32,h),r.flush(),o.retval},w.decompressFile=l.decode,w.decompressBlock=l.decodeBlock,w.table=l.table,w}(0,BitStream,BWT,CRC32,HuffmanAllocator,Stream,Util),module.exports=Bzip2; \ No newline at end of file