mirror of
https://github.com/marcrobledo/RomPatcher.js.git
synced 2025-07-27 16:48:31 +00:00
added zip support
This commit is contained in:
parent
c59a8cbfd0
commit
94529e1a52
19 changed files with 473 additions and 188 deletions
266
libs/MarcFile.js
Normal file
266
libs/MarcFile.js
Normal file
|
@ -0,0 +1,266 @@
|
|||
/* MODDED VERSION OF MarcFile.js v20181020 - Marc Robledo 2014-2018 - http://www.marcrobledo.com/license */
|
||||
|
||||
function MarcFile(source, onLoad){
|
||||
if(typeof source==='object' && source.files) /* get first file only if source is input with multiple files */
|
||||
source=source.files[0];
|
||||
|
||||
this.littleEndian=false;
|
||||
this.offset=0;
|
||||
this._lastRead=null;
|
||||
|
||||
if(typeof source==='object' && source.name && source.size){ /* source is file */
|
||||
if(typeof window.FileReader!=='function')
|
||||
throw new Error('Incompatible Browser');
|
||||
|
||||
this.fileName=source.name;
|
||||
this.fileType=source.type;
|
||||
this.fileSize=source.size;
|
||||
|
||||
this._fileReader=new FileReader();
|
||||
this._fileReader.marcFile=this;
|
||||
this._fileReader.addEventListener('load',function(){
|
||||
this.marcFile._u8array=new Uint8Array(this.result);
|
||||
this.marcFile._dataView=new DataView(this.result);
|
||||
|
||||
if(onLoad)
|
||||
onLoad.call();
|
||||
},false);
|
||||
|
||||
this._fileReader.readAsArrayBuffer(source);
|
||||
|
||||
|
||||
|
||||
}else if(typeof source==='object' && typeof source.fileName==='string' && typeof source.littleEndian==='boolean'){ /* source is MarcFile */
|
||||
this.fileName=source.fileName;
|
||||
this.fileType=source.fileType;
|
||||
this.fileSize=source.fileSize;
|
||||
|
||||
var ab=new ArrayBuffer(source);
|
||||
this._u8array=new Uint8Array(this.fileType);
|
||||
this._dataView=new DataView(this.fileType);
|
||||
|
||||
source.copyToFile(this, 0);
|
||||
if(onLoad)
|
||||
onLoad.call();
|
||||
|
||||
|
||||
|
||||
}else if(typeof source==='object' && typeof source.byteLength==='number'){ /* source is ArrayBuffer or TypedArray */
|
||||
this.fileName='file.bin';
|
||||
this.fileType='application/octet-stream';
|
||||
this.fileSize=source.byteLength;
|
||||
|
||||
if(typeof source.buffer !== 'undefined')
|
||||
source=source.buffer;
|
||||
this._u8array=new Uint8Array(source);
|
||||
this._dataView=new DataView(source);
|
||||
|
||||
if(onLoad)
|
||||
onLoad.call();
|
||||
|
||||
|
||||
|
||||
}else if(typeof source==='number'){ /* source is integer (new empty file) */
|
||||
this.fileName='file.bin';
|
||||
this.fileType='application/octet-stream';
|
||||
this.fileSize=source;
|
||||
|
||||
var ab=new ArrayBuffer(source);
|
||||
this._u8array=new Uint8Array(ab);
|
||||
this._dataView=new DataView(ab);
|
||||
|
||||
if(onLoad)
|
||||
onLoad.call();
|
||||
}else{
|
||||
throw new Error('Invalid source');
|
||||
}
|
||||
}
|
||||
MarcFile.IS_MACHINE_LITTLE_ENDIAN=(function(){ /* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView#Endianness */
|
||||
var buffer=new ArrayBuffer(2);
|
||||
new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
|
||||
// Int16Array uses the platform's endianness.
|
||||
return new Int16Array(buffer)[0] === 256;
|
||||
})();
|
||||
|
||||
|
||||
|
||||
MarcFile.prototype.seek=function(offset){
|
||||
this.offset=offset;
|
||||
}
|
||||
MarcFile.prototype.skip=function(nBytes){
|
||||
this.offset+=nBytes;
|
||||
}
|
||||
MarcFile.prototype.isEOF=function(){
|
||||
return !(this.offset<this.fileSize)
|
||||
}
|
||||
|
||||
MarcFile.prototype.slice=function(offset, len){
|
||||
len=len || (this.fileSize-offset);
|
||||
|
||||
var newFile;
|
||||
|
||||
if(typeof this._u8array.buffer.slice!=='undefined'){
|
||||
newFile=new MarcFile(0);
|
||||
newFile.fileSize=len;
|
||||
newFile._u8array=new Uint8Array(this._u8array.buffer.slice(offset, offset+len));
|
||||
}else{
|
||||
newFile=new MarcFile(len);
|
||||
this.copyToFile(newFile, offset, len, 0);
|
||||
}
|
||||
newFile.fileName=this.fileName;
|
||||
newFile.fileType=this.fileType;
|
||||
newFile.littleEndian=this.littleEndian;
|
||||
return newFile;
|
||||
}
|
||||
|
||||
|
||||
MarcFile.prototype.copyToFile=function(target, offsetSource, len, offsetTarget){
|
||||
if(typeof offsetTarget==='undefined')
|
||||
offsetTarget=offsetSource;
|
||||
|
||||
len=len || (this.fileSize-offsetSource);
|
||||
|
||||
for(var i=0; i<len; i++){
|
||||
target._u8array[offsetTarget+i]=this._u8array[offsetSource+i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MarcFile.prototype.save=function(){
|
||||
var blob;
|
||||
try{
|
||||
blob=new Blob([this._u8array],{type:this.fileType});
|
||||
}catch(e){
|
||||
//old browser, use BlobBuilder
|
||||
window.BlobBuilder=window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
|
||||
if(e.name==='InvalidStateError' && window.BlobBuilder){
|
||||
var bb=new BlobBuilder();
|
||||
bb.append(this._u8array.buffer);
|
||||
blob=bb.getBlob(this.fileType);
|
||||
}else{
|
||||
throw new Error('Incompatible Browser');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
saveAs(blob,this.fileName);
|
||||
}
|
||||
|
||||
|
||||
MarcFile.prototype.readU8=function(){
|
||||
this._lastRead=this._u8array[this.offset];
|
||||
|
||||
this.offset++;
|
||||
return this._lastRead
|
||||
}
|
||||
MarcFile.prototype.readU16=function(){
|
||||
if(this.littleEndian)
|
||||
this._lastRead=this._u8array[this.offset] + (this._u8array[this.offset+1] << 8);
|
||||
else
|
||||
this._lastRead=(this._u8array[this.offset] << 8) + this._u8array[this.offset+1];
|
||||
|
||||
this.offset+=2;
|
||||
return this._lastRead >>> 0
|
||||
}
|
||||
MarcFile.prototype.readU24=function(){
|
||||
if(this.littleEndian)
|
||||
this._lastRead=this._u8array[this.offset] + (this._u8array[this.offset+1] << 8) + (this._u8array[this.offset+2] << 16);
|
||||
else
|
||||
this._lastRead=(this._u8array[this.offset] << 16) + (this._u8array[this.offset+1] << 8) + this._u8array[this.offset+2];
|
||||
|
||||
this.offset+=3;
|
||||
return this._lastRead >>> 0
|
||||
}
|
||||
MarcFile.prototype.readU32=function(){
|
||||
if(this.littleEndian)
|
||||
this._lastRead=this._u8array[this.offset] + (this._u8array[this.offset+1] << 8) + (this._u8array[this.offset+2] << 16) + (this._u8array[this.offset+3] << 24);
|
||||
else
|
||||
this._lastRead=(this._u8array[this.offset] << 24) + (this._u8array[this.offset+1] << 16) + (this._u8array[this.offset+2] << 8) + this._u8array[this.offset+3];
|
||||
|
||||
this.offset+=4;
|
||||
return this._lastRead >>> 0
|
||||
}
|
||||
|
||||
|
||||
|
||||
MarcFile.prototype.readBytes=function(len){
|
||||
this._lastRead=new Array(len);
|
||||
for(var i=0; i<len; i++){
|
||||
this._lastRead[i]=this._u8array[this.offset+i];
|
||||
}
|
||||
|
||||
this.offset+=len;
|
||||
return this._lastRead
|
||||
}
|
||||
|
||||
MarcFile.prototype.readString=function(len){
|
||||
this._lastRead='';
|
||||
for(var i=0;i<len && (this.offset+i)<this.fileSize && this._u8array[this.offset+i]>0;i++)
|
||||
this._lastRead=this._lastRead+String.fromCharCode(this._u8array[this.offset+i]);
|
||||
|
||||
this.offset+=len;
|
||||
return this._lastRead
|
||||
}
|
||||
|
||||
MarcFile.prototype.writeU8=function(u8){
|
||||
this._u8array[this.offset]=u8;
|
||||
|
||||
this.offset++;
|
||||
}
|
||||
MarcFile.prototype.writeU16=function(u16){
|
||||
if(this.littleEndian){
|
||||
this._u8array[this.offset]=u16 & 0xff;
|
||||
this._u8array[this.offset+1]=u16 >> 8;
|
||||
}else{
|
||||
this._u8array[this.offset]=u16 >> 8;
|
||||
this._u8array[this.offset+1]=u16 & 0xff;
|
||||
}
|
||||
|
||||
this.offset+=2;
|
||||
}
|
||||
MarcFile.prototype.writeU24=function(u24){
|
||||
if(this.littleEndian){
|
||||
this._u8array[this.offset]=u24 & 0x0000ff;
|
||||
this._u8array[this.offset+1]=(u24 & 0x00ff00) >> 8;
|
||||
this._u8array[this.offset+2]=(u24 & 0xff0000) >> 16;
|
||||
}else{
|
||||
this._u8array[this.offset]=(u24 & 0xff0000) >> 16;
|
||||
this._u8array[this.offset+1]=(u24 & 0x00ff00) >> 8;
|
||||
this._u8array[this.offset+2]=u24 & 0x0000ff;
|
||||
}
|
||||
|
||||
this.offset+=3;
|
||||
}
|
||||
MarcFile.prototype.writeU32=function(u32){
|
||||
if(this.littleEndian){
|
||||
this._u8array[this.offset]=u32 & 0x000000ff;
|
||||
this._u8array[this.offset+1]=(u32 & 0x0000ff00) >> 8;
|
||||
this._u8array[this.offset+2]=(u32 & 0x00ff0000) >> 16;
|
||||
this._u8array[this.offset+3]=(u32 & 0xff000000) >> 24;
|
||||
}else{
|
||||
this._u8array[this.offset]=(u32 & 0xff000000) >> 24;
|
||||
this._u8array[this.offset+1]=(u32 & 0x00ff0000) >> 16;
|
||||
this._u8array[this.offset+2]=(u32 & 0x0000ff00) >> 8;
|
||||
this._u8array[this.offset+3]=u32 & 0x000000ff;
|
||||
}
|
||||
|
||||
this.offset+=4;
|
||||
}
|
||||
|
||||
|
||||
MarcFile.prototype.writeBytes=function(a){
|
||||
for(var i=0;i<a.length;i++)
|
||||
this._u8array[this.offset+i]=a[i]
|
||||
|
||||
this.offset+=a.length;
|
||||
}
|
||||
|
||||
MarcFile.prototype.writeString=function(str,len){
|
||||
len=len || str.length;
|
||||
for(var i=0;i<str.length && i<len;i++)
|
||||
this._u8array[this.offset+i]=str.charCodeAt(i);
|
||||
|
||||
for(;i<len;i++)
|
||||
this._u8array[this.offset+i]=0x00;
|
||||
|
||||
this.offset+=len;
|
||||
}
|
36
libs/inflate.js
Normal file
36
libs/inflate.js
Normal file
File diff suppressed because one or more lines are too long
2
libs/z-worker.js
Normal file
2
libs/z-worker.js
Normal file
|
@ -0,0 +1,2 @@
|
|||
/* jshint worker:true */
|
||||
!function(c){"use strict";if(c.zWorkerInitialized)throw new Error("z-worker.js should be run only once");c.zWorkerInitialized=!0,addEventListener("message",function(t){var e,r,c=t.data,n=c.type,s=c.sn,p=o[n];if(p)try{p(c)}catch(t){e={type:n,sn:s,error:(r=t,{message:r.message,stack:r.stack})},postMessage(e)}});var o={importScripts:function(t){t.scripts&&0<t.scripts.length&&importScripts.apply(void 0,t.scripts);postMessage({type:"importScripts"})},newTask:h,append:t,flush:t},f={};function h(t){var e=c[t.codecClass],r=t.sn;if(f[r])throw Error("duplicated sn");f[r]={codec:new e(t.options),crcInput:"input"===t.crcType,crcOutput:"output"===t.crcType,crc:new n},postMessage({type:"newTask",sn:r})}var l=c.performance?c.performance.now.bind(c.performance):Date.now;function t(t){var e=t.sn,r=t.type,c=t.data,n=f[e];!n&&t.codecClass&&(h(t),n=f[e]);var s,p="append"===r,o=l();if(p)try{s=n.codec.append(c,function(t){postMessage({type:"progress",sn:e,loaded:t})})}catch(t){throw delete f[e],t}else delete f[e],s=n.codec.flush();var a=l()-o;o=l(),c&&n.crcInput&&n.crc.append(c),s&&n.crcOutput&&n.crc.append(s);var i=l()-o,u={type:r,sn:e,codecTime:a,crcTime:i},d=[];s&&(u.data=s,d.push(s.buffer)),p||!n.crcInput&&!n.crcOutput||(u.crc=n.crc.get());try{postMessage(u,d)}catch(t){postMessage(u)}}function n(){this.crc=-1}function e(){}n.prototype.append=function(t){for(var e=0|this.crc,r=this.table,c=0,n=0|t.length;c<n;c++)e=e>>>8^r[255&(e^t[c])];this.crc=e},n.prototype.get=function(){return~this.crc},n.prototype.table=function(){var t,e,r,c=[];for(t=0;t<256;t++){for(r=t,e=0;e<8;e++)1&r?r=r>>>1^3988292384:r>>>=1;c[t]=r}return c}(),(c.NOOP=e).prototype.append=function(t,e){return t},e.prototype.flush=function(){}}(this);
|
28
libs/zip.js
Normal file
28
libs/zip.js
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue