From 35d59c57bf881cb5df0c1413c8d6300bd78661cb Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 9 Apr 2018 02:33:22 -0400 Subject: [PATCH] version bump 1.0.6: performance - `unsafe` option to `cfb_add` for bulk write (see #2) - use `lastIndexOf` to save operations in BFP queue --- CHANGELOG.md | 4 ++++ README.md | 1 + bits/31_version.js | 2 +- bits/43_rbtree.js | 4 ++-- bits/85_api.js | 7 ++++--- cfb.flow.js | 13 +++++++------ cfb.js | 13 +++++++------ dist/cfb.js | 13 +++++++------ dist/cfb.min.js | 2 +- dist/cfb.min.map | 2 +- dist/xlscfb.js | 13 +++++++------ package.json | 2 +- shim.js | 6 ++++++ types/index.d.ts | 8 +++++++- types/roundtrip.ts | 2 +- xlscfb.flow.js | 13 +++++++------ xlscfb.js | 13 +++++++------ 17 files changed, 71 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e83b5f4..157d868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This log is intended to keep track of backwards-incompatible changes, including but not limited to API changes and file location changes. Minor behavioral changes may not be included if they are not expected to break existing code. +## 1.0.6 (2018-04-09) + +* `lastIndexOf` in FAT builder enables larger file parsing at minor cost + ## 1.0.0 (2017-11-05) * Actually walk mini-fat diff --git a/README.md b/README.md index 33c1c04..899d702 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ accept a `name` argument strictly deal with absolute file names: - `.cfb_new(?opts)` creates a new container object. - `.cfb_add(cfb, name, ?content, ?opts)` adds a new file to the `cfb`. + Set the option `{unsafe:true}` to skip existence checks (for bulk additions) - `.cfb_del(cfb, name)` deletes the specified file - `.cfb_mov(cfb, old_name, new_name)` moves the old file to new path and name diff --git a/bits/31_version.js b/bits/31_version.js index 062399d..b0da39d 100644 --- a/bits/31_version.js +++ b/bits/31_version.js @@ -1 +1 @@ -exports.version = '1.0.5'; +exports.version = '1.0.6'; diff --git a/bits/43_rbtree.js b/bits/43_rbtree.js index 52cc1f0..f601cb8 100644 --- a/bits/43_rbtree.js +++ b/bits/43_rbtree.js @@ -13,8 +13,8 @@ function build_full_paths(FI/*:CFBFileIndex*/, FP/*:Array*/, Paths/*:Arr if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; diff --git a/bits/85_api.js b/bits/85_api.js index 0d2cb4c..ff172c3 100644 --- a/bits/85_api.js +++ b/bits/85_api.js @@ -5,8 +5,9 @@ function cfb_new(opts/*:?any*/)/*:CFBContainer*/ { } function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, opts/*:?any*/)/*:CFBEntry*/ { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath/*:string*/ = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -17,7 +18,7 @@ function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, o file = ({name: filename(name), type: 2}/*:any*/); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } /*:: if(!file) throw new Error("unreachable"); */ file.content = (content/*:any*/); diff --git a/cfb.flow.js b/cfb.flow.js index e15022d..05a40e0 100644 --- a/cfb.flow.js +++ b/cfb.flow.js @@ -177,7 +177,7 @@ type CFBFiles = {[n:string]:CFBEntry}; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.0.5'; +exports.version = '1.0.6'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -355,8 +355,8 @@ function build_full_paths(FI/*:CFBFileIndex*/, FP/*:Array*/, Paths/*:Arr if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -823,8 +823,9 @@ function cfb_new(opts/*:?any*/)/*:CFBContainer*/ { } function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, opts/*:?any*/)/*:CFBEntry*/ { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath/*:string*/ = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -835,7 +836,7 @@ function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, o file = ({name: filename(name), type: 2}/*:any*/); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } /*:: if(!file) throw new Error("unreachable"); */ file.content = (content/*:any*/); diff --git a/cfb.js b/cfb.js index 8bc6fd0..8cb52e2 100644 --- a/cfb.js +++ b/cfb.js @@ -159,7 +159,7 @@ function new_buf(sz) { /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.0.5'; +exports.version = '1.0.6'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -337,8 +337,8 @@ function build_full_paths(FI, FP, Paths) { if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -799,8 +799,9 @@ function cfb_new(opts) { } function cfb_add(cfb, name, content, opts) { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -811,7 +812,7 @@ function cfb_add(cfb, name, content, opts) { file = ({name: filename(name), type: 2}); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } file.content = (content); file.size = content ? content.length : 0; diff --git a/dist/cfb.js b/dist/cfb.js index 8bc6fd0..8cb52e2 100644 --- a/dist/cfb.js +++ b/dist/cfb.js @@ -159,7 +159,7 @@ function new_buf(sz) { /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.0.5'; +exports.version = '1.0.6'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -337,8 +337,8 @@ function build_full_paths(FI, FP, Paths) { if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -799,8 +799,9 @@ function cfb_new(opts) { } function cfb_add(cfb, name, content, opts) { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -811,7 +812,7 @@ function cfb_add(cfb, name, content, opts) { file = ({name: filename(name), type: 2}); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } file.content = (content); file.size = content ? content.length : 0; diff --git a/dist/cfb.min.js b/dist/cfb.min.js index 14a7ee5..9317d84 100644 --- a/dist/cfb.min.js +++ b/dist/cfb.min.js @@ -1,2 +1,2 @@ /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */ -var Base64=function e(){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";return{encode:function(r){var t="";var i=0,n=0,a=0,f=0,s=0,h=0,l=0;for(var o=0;o>2;n=r.charCodeAt(o++);s=(i&3)<<4|n>>4;a=r.charCodeAt(o++);h=(n&15)<<2|a>>6;l=a&63;if(isNaN(n)){h=l=64}else if(isNaN(a)){l=64}t+=e.charAt(f)+e.charAt(s)+e.charAt(h)+e.charAt(l)}return t},decode:function r(t){var i="";var n=0,a=0,f=0,s=0,h=0,l=0,o=0;t=t.replace(/[^\w\+\/\=]/g,"");for(var c=0;c>4;i+=String.fromCharCode(n);l=e.indexOf(t.charAt(c++));a=(h&15)<<4|l>>2;if(l!==64){i+=String.fromCharCode(a)}o=e.indexOf(t.charAt(c++));f=(l&3)<<6|o;if(o!==64){i+=String.fromCharCode(f)}}return i}}}();var has_buf=typeof Buffer!=="undefined"&&typeof process!=="undefined"&&typeof process.versions!=="undefined"&&process.versions.node;function new_raw_buf(e){return new(has_buf?Buffer:Array)(e)}var s2a=function r(e){if(has_buf)return new Buffer(e,"binary");return e.split("").map(function(e){return e.charCodeAt(0)&255})};var chr0=/\u0000/g,chr1=/[\u0001-\u0006]/g;var __toBuffer=function(e){var r=[];for(var t=0;t0&&Buffer.isBuffer(e[0][0])?Buffer.concat(e[0]):___toBuffer(e)};s2a=function(e){return new Buffer(e,"binary")};bconcat=function(e){return Buffer.isBuffer(e[0])?Buffer.concat(e):__bconcat(e)}}var __readUInt8=function(e,r){return e[r]};var __readUInt16LE=function(e,r){return e[r+1]*(1<<8)+e[r]};var __readInt16LE=function(e,r){var t=e[r+1]*(1<<8)+e[r];return t<32768?t:(65535-t+1)*-1};var __readUInt32LE=function(e,r){return e[r+3]*(1<<24)+(e[r+2]<<16)+(e[r+1]<<8)+e[r]};var __readInt32LE=function(e,r){return(e[r+3]<<24)+(e[r+2]<<16)+(e[r+1]<<8)+e[r]};function ReadShift(e,r){var t,i,n=0;switch(e){case 1:t=__readUInt8(this,this.l);break;case 2:t=(r!=="i"?__readUInt16LE:__readInt16LE)(this,this.l);break;case 4:t=__readInt32LE(this,this.l);break;case 16:n=2;i=__hexlify(this,this.l,e);}this.l+=e;if(n===0)return t;return i}var __writeUInt32LE=function(e,r,t){e[t]=r&255;e[t+1]=r>>>8&255;e[t+2]=r>>>16&255;e[t+3]=r>>>24&255};var __writeInt32LE=function(e,r,t){e[t]=r&255;e[t+1]=r>>8&255;e[t+2]=r>>16&255;e[t+3]=r>>24&255};function WriteShift(e,r,t){var i=0,n=0;switch(t){case"hex":for(;n>8}while(this.l>>=8;this[this.l+1]=r&255;break;case 4:i=4;__writeUInt32LE(this,r,this.l);break;case-4:i=4;__writeInt32LE(this,r,this.l);break;}this.l+=i;return this}function CheckField(e,r){var t=__hexlify(this,this.l,e.length>>1);if(t!==e)throw new Error(r+"Expected "+e+" saw "+t);this.l+=e.length>>1}function prep_blob(e,r){e.l=r;e.read_shift=ReadShift;e.chk=CheckField;e.write_shift=WriteShift}function new_buf(e){var r=new_raw_buf(e);prep_blob(r,0);return r}var CFB=function t(){var e={};e.version="1.0.5";function r(e,r){var t=e.split("/"),i=r.split("/");for(var n=0,a=0,f=Math.min(t.length,i.length);n0&&c!==y)C[c].name="!MiniFAT";C[w[0]].name="!FAT";C.fat_addrs=w;C.ssz=i;var S={},A=[],m=[],B=[];v(f,C,E,A,n,S,m,c);o(m,B,A);A.shift();var L={FileIndex:m,FullPaths:B};if(r&&r.raw)L.raw={header:g,sectors:E};return L}function s(e){e.chk(S,"Header Signature: ");e.chk(m,"CLSID: ");var r=e.read_shift(2,"u");return[e.read_shift(2,"u"),r]}function h(e,r){var t=9;e.l+=2;switch(t=e.read_shift(2)){case 9:if(r!=3)throw new Error("Sector Shift: Expected 9 saw "+t);break;case 12:if(r!=4)throw new Error("Sector Shift: Expected 12 saw "+t);break;default:throw new Error("Sector Shift: Expected 9 or 12 saw "+t);}e.chk("0600","Mini Sector Shift: ");e.chk("000000000000","Reserved: ")}function l(e,r){var t=Math.ceil(e.length/r)-1;var i=[];for(var n=1;n0&&f>=0){a.push(r.slice(f*C,f*C+C));n-=C;f=__readInt32LE(t,f*4)}if(a.length===0)return new_buf(0);return bconcat(a).slice(0,e.size)}function u(e,r,t,i,n){var a=y;if(e===y){if(r!==0)throw new Error("DIFAT chain shorter than expected")}else if(e!==-1){var f=t[e],s=(i>>>2)-1;if(!f)return;for(var h=0;h=0;){n[h]=true;a[a.length]=h;f.push(e[h]);var o=t[Math.floor(h*4/i)];l=h*4&s;if(i<4+l)throw new Error("FAT boundary crossed: "+h+" 4 "+i);if(!e[o])break;h=__readInt32LE(e[o],l)}return{nodes:a,data:__toBuffer([f])}}function d(e,r,t,i){var n=e.length,a=[];var f=[],s=[],h=[];var l=i-1,o=0,c=0,u=0,_=0;for(o=0;o=n)u-=n;if(f[u])continue;h=[];for(c=u;c>=0;){f[c]=true;s[s.length]=c;h.push(e[c]);var d=t[Math.floor(c*4/i)];_=c*4&l;if(i<4+_)throw new Error("FAT boundary crossed: "+c+" 4 "+i);if(!e[d])break;c=__readInt32LE(e[d],_)}a[u]={nodes:s,data:__toBuffer([h])}}return a}function v(e,r,t,i,n,a,f,s){var h=0,l=i.length?2:0;var o=r[e].data;var u=0,d=0,v;for(;u0&&h!==y)r[h].name="!StreamData"}else if(F.size>=4096){F.storage="fat";if(r[F.start]===undefined)r[F.start]=_(t,F.start,r.fat_addrs,r.ssz);r[F.start].name=F.name;F.content=r[F.start].data.slice(0,F.size)}else{F.storage="minifat";if(F.size<0)F.size=0;else if(h!==y&&F.start!==y&&r[h]){F.content=c(F,r[h].data,(r[s]||{}).data)}}if(F.content)prep_blob(F.content,0);a[v]=F;f.push(F)}}function w(e,r){return new Date((__readUInt32LE(e,r+4)/1e7*Math.pow(2,32)+__readUInt32LE(e,r)/1e7-11644473600)*1e3)}function p(e,r){a();return f(n.readFileSync(e),r)}function F(e,r){switch(r&&r.type||"base64"){case"file":return p(e,r);case"base64":return f(s2a(Base64.decode(e)),r);case"binary":return f(s2a(e),r);}return f(e,r)}function g(e,r){var t=r||{},i=t.root||"Root Entry";if(!e.FullPaths)e.FullPaths=[];if(!e.FileIndex)e.FileIndex=[];if(e.FullPaths.length!==e.FileIndex.length)throw new Error("inconsistent CFB structure");if(e.FullPaths.length===0){e.FullPaths[0]=i+"/";e.FileIndex[0]={name:i,type:5}}if(t.CLSID)e.FileIndex[0].clsid=t.CLSID;I(e)}function I(e){var r="Sh33tJ5";if(CFB.find(e,"/"+r))return;var t=new_buf(4);t[0]=55;t[1]=t[3]=50;t[2]=54;e.FileIndex.push({name:r,type:2,content:t,size:4,L:69,R:69,C:69});e.FullPaths.push(e.FullPaths[0]+r);b(e)}function b(e,n){g(e);var a=false,f=false;for(var s=e.FullPaths.length-1;s>=0;--s){var h=e.FileIndex[s];switch(h.type){case 0:if(f)a=true;else{e.FileIndex.pop();e.FullPaths.pop()}break;case 1:;case 2:;case 5:f=true;if(isNaN(h.R*h.L*h.C))a=true;if(h.R>-1&&h.L>-1&&h.R==h.L)a=true;break;default:a=true;break;}}if(!a&&!n)return;var l=new Date(1987,1,19),o=0;var c=[];for(s=0;s1?1:-1;_.size=0;_.type=5}else if(d.slice(-1)=="/"){for(o=s+1;o=c.length?-1:o;for(o=s+1;o=c.length?-1:o;_.type=1}else{if(t(e.FullPaths[s+1]||"")==t(d))_.R=s+1;_.type=2}}}function x(e,r){var t=r||{};b(e);var i=function(e){var r=0,t=0;for(var i=0;i0){if(a<4096)r+=a+63>>6;else t+=a+511>>9}}var f=e.FullPaths.length+3>>2;var s=r+7>>3;var h=r+127>>7;var l=s+t+f+h;var o=l+127>>7;var c=o<=109?0:Math.ceil((o-109)/127);while(l+o+c+127>>7>o)c=++o<=109?0:Math.ceil((o-109)/127);var u=[1,c,o,h,f,t,r,0];e.FileIndex[0].size=r<<6;u[7]=(e.FileIndex[0].start=u[0]+u[1]+u[2]+u[3]+u[4]+u[5])+(u[6]+7>>3);return u}(e);var n=new_buf(i[7]<<9);var a=0,f=0;{for(a=0;a<8;++a)n.write_shift(1,A[a]);for(a=0;a<8;++a)n.write_shift(2,0);n.write_shift(2,62);n.write_shift(2,3);n.write_shift(2,65534);n.write_shift(2,9);n.write_shift(2,6);for(a=0;a<3;++a)n.write_shift(2,0);n.write_shift(4,0);n.write_shift(4,i[2]);n.write_shift(4,i[0]+i[1]+i[2]+i[3]-1);n.write_shift(4,0);n.write_shift(4,1<<12);n.write_shift(4,i[3]?i[0]+i[1]+i[2]-1:y);n.write_shift(4,i[3]);n.write_shift(-4,i[1]?i[0]-1:y);n.write_shift(4,i[1]);for(a=0;a<109;++a)n.write_shift(-4,a>9)}s(i[6]+7>>3);while(n.l&511)n.write_shift(-4,B.ENDOFCHAIN);f=a=0;for(h=0;h=4096)continue;o.start=f;s(l+63>>6)}while(n.l&511)n.write_shift(-4,B.ENDOFCHAIN);for(a=0;a=4096){n.l=o.start+1<<9;for(h=0;h0&&o.size<4096){for(h=0;h>2;n=r.charCodeAt(o++);s=(i&3)<<4|n>>4;a=r.charCodeAt(o++);h=(n&15)<<2|a>>6;l=a&63;if(isNaN(n)){h=l=64}else if(isNaN(a)){l=64}t+=e.charAt(f)+e.charAt(s)+e.charAt(h)+e.charAt(l)}return t},decode:function r(t){var i="";var n=0,a=0,f=0,s=0,h=0,l=0,o=0;t=t.replace(/[^\w\+\/\=]/g,"");for(var c=0;c>4;i+=String.fromCharCode(n);l=e.indexOf(t.charAt(c++));a=(h&15)<<4|l>>2;if(l!==64){i+=String.fromCharCode(a)}o=e.indexOf(t.charAt(c++));f=(l&3)<<6|o;if(o!==64){i+=String.fromCharCode(f)}}return i}}}();var has_buf=typeof Buffer!=="undefined"&&typeof process!=="undefined"&&typeof process.versions!=="undefined"&&process.versions.node;function new_raw_buf(e){return new(has_buf?Buffer:Array)(e)}var s2a=function r(e){if(has_buf)return new Buffer(e,"binary");return e.split("").map(function(e){return e.charCodeAt(0)&255})};var chr0=/\u0000/g,chr1=/[\u0001-\u0006]/g;var __toBuffer=function(e){var r=[];for(var t=0;t0&&Buffer.isBuffer(e[0][0])?Buffer.concat(e[0]):___toBuffer(e)};s2a=function(e){return new Buffer(e,"binary")};bconcat=function(e){return Buffer.isBuffer(e[0])?Buffer.concat(e):__bconcat(e)}}var __readUInt8=function(e,r){return e[r]};var __readUInt16LE=function(e,r){return e[r+1]*(1<<8)+e[r]};var __readInt16LE=function(e,r){var t=e[r+1]*(1<<8)+e[r];return t<32768?t:(65535-t+1)*-1};var __readUInt32LE=function(e,r){return e[r+3]*(1<<24)+(e[r+2]<<16)+(e[r+1]<<8)+e[r]};var __readInt32LE=function(e,r){return(e[r+3]<<24)+(e[r+2]<<16)+(e[r+1]<<8)+e[r]};function ReadShift(e,r){var t,i,n=0;switch(e){case 1:t=__readUInt8(this,this.l);break;case 2:t=(r!=="i"?__readUInt16LE:__readInt16LE)(this,this.l);break;case 4:t=__readInt32LE(this,this.l);break;case 16:n=2;i=__hexlify(this,this.l,e);}this.l+=e;if(n===0)return t;return i}var __writeUInt32LE=function(e,r,t){e[t]=r&255;e[t+1]=r>>>8&255;e[t+2]=r>>>16&255;e[t+3]=r>>>24&255};var __writeInt32LE=function(e,r,t){e[t]=r&255;e[t+1]=r>>8&255;e[t+2]=r>>16&255;e[t+3]=r>>24&255};function WriteShift(e,r,t){var i=0,n=0;switch(t){case"hex":for(;n>8}while(this.l>>=8;this[this.l+1]=r&255;break;case 4:i=4;__writeUInt32LE(this,r,this.l);break;case-4:i=4;__writeInt32LE(this,r,this.l);break;}this.l+=i;return this}function CheckField(e,r){var t=__hexlify(this,this.l,e.length>>1);if(t!==e)throw new Error(r+"Expected "+e+" saw "+t);this.l+=e.length>>1}function prep_blob(e,r){e.l=r;e.read_shift=ReadShift;e.chk=CheckField;e.write_shift=WriteShift}function new_buf(e){var r=new_raw_buf(e);prep_blob(r,0);return r}var CFB=function t(){var e={};e.version="1.0.6";function r(e,r){var t=e.split("/"),i=r.split("/");for(var n=0,a=0,f=Math.min(t.length,i.length);n0&&c!==y)C[c].name="!MiniFAT";C[w[0]].name="!FAT";C.fat_addrs=w;C.ssz=i;var S={},A=[],m=[],B=[];v(f,C,E,A,n,S,m,c);o(m,B,A);A.shift();var L={FileIndex:m,FullPaths:B};if(r&&r.raw)L.raw={header:g,sectors:E};return L}function s(e){e.chk(S,"Header Signature: ");e.chk(m,"CLSID: ");var r=e.read_shift(2,"u");return[e.read_shift(2,"u"),r]}function h(e,r){var t=9;e.l+=2;switch(t=e.read_shift(2)){case 9:if(r!=3)throw new Error("Sector Shift: Expected 9 saw "+t);break;case 12:if(r!=4)throw new Error("Sector Shift: Expected 12 saw "+t);break;default:throw new Error("Sector Shift: Expected 9 or 12 saw "+t);}e.chk("0600","Mini Sector Shift: ");e.chk("000000000000","Reserved: ")}function l(e,r){var t=Math.ceil(e.length/r)-1;var i=[];for(var n=1;n0&&f>=0){a.push(r.slice(f*C,f*C+C));n-=C;f=__readInt32LE(t,f*4)}if(a.length===0)return new_buf(0);return bconcat(a).slice(0,e.size)}function u(e,r,t,i,n){var a=y;if(e===y){if(r!==0)throw new Error("DIFAT chain shorter than expected")}else if(e!==-1){var f=t[e],s=(i>>>2)-1;if(!f)return;for(var h=0;h=0;){n[h]=true;a[a.length]=h;f.push(e[h]);var o=t[Math.floor(h*4/i)];l=h*4&s;if(i<4+l)throw new Error("FAT boundary crossed: "+h+" 4 "+i);if(!e[o])break;h=__readInt32LE(e[o],l)}return{nodes:a,data:__toBuffer([f])}}function d(e,r,t,i){var n=e.length,a=[];var f=[],s=[],h=[];var l=i-1,o=0,c=0,u=0,_=0;for(o=0;o=n)u-=n;if(f[u])continue;h=[];for(c=u;c>=0;){f[c]=true;s[s.length]=c;h.push(e[c]);var d=t[Math.floor(c*4/i)];_=c*4&l;if(i<4+_)throw new Error("FAT boundary crossed: "+c+" 4 "+i);if(!e[d])break;c=__readInt32LE(e[d],_)}a[u]={nodes:s,data:__toBuffer([h])}}return a}function v(e,r,t,i,n,a,f,s){var h=0,l=i.length?2:0;var o=r[e].data;var u=0,d=0,v;for(;u0&&h!==y)r[h].name="!StreamData"}else if(F.size>=4096){F.storage="fat";if(r[F.start]===undefined)r[F.start]=_(t,F.start,r.fat_addrs,r.ssz);r[F.start].name=F.name;F.content=r[F.start].data.slice(0,F.size)}else{F.storage="minifat";if(F.size<0)F.size=0;else if(h!==y&&F.start!==y&&r[h]){F.content=c(F,r[h].data,(r[s]||{}).data)}}if(F.content)prep_blob(F.content,0);a[v]=F;f.push(F)}}function w(e,r){return new Date((__readUInt32LE(e,r+4)/1e7*Math.pow(2,32)+__readUInt32LE(e,r)/1e7-11644473600)*1e3)}function p(e,r){a();return f(n.readFileSync(e),r)}function F(e,r){switch(r&&r.type||"base64"){case"file":return p(e,r);case"base64":return f(s2a(Base64.decode(e)),r);case"binary":return f(s2a(e),r);}return f(e,r)}function g(e,r){var t=r||{},i=t.root||"Root Entry";if(!e.FullPaths)e.FullPaths=[];if(!e.FileIndex)e.FileIndex=[];if(e.FullPaths.length!==e.FileIndex.length)throw new Error("inconsistent CFB structure");if(e.FullPaths.length===0){e.FullPaths[0]=i+"/";e.FileIndex[0]={name:i,type:5}}if(t.CLSID)e.FileIndex[0].clsid=t.CLSID;I(e)}function I(e){var r="Sh33tJ5";if(CFB.find(e,"/"+r))return;var t=new_buf(4);t[0]=55;t[1]=t[3]=50;t[2]=54;e.FileIndex.push({name:r,type:2,content:t,size:4,L:69,R:69,C:69});e.FullPaths.push(e.FullPaths[0]+r);b(e)}function b(e,n){g(e);var a=false,f=false;for(var s=e.FullPaths.length-1;s>=0;--s){var h=e.FileIndex[s];switch(h.type){case 0:if(f)a=true;else{e.FileIndex.pop();e.FullPaths.pop()}break;case 1:;case 2:;case 5:f=true;if(isNaN(h.R*h.L*h.C))a=true;if(h.R>-1&&h.L>-1&&h.R==h.L)a=true;break;default:a=true;break;}}if(!a&&!n)return;var l=new Date(1987,1,19),o=0;var c=[];for(s=0;s1?1:-1;_.size=0;_.type=5}else if(d.slice(-1)=="/"){for(o=s+1;o=c.length?-1:o;for(o=s+1;o=c.length?-1:o;_.type=1}else{if(t(e.FullPaths[s+1]||"")==t(d))_.R=s+1;_.type=2}}}function x(e,r){var t=r||{};b(e);var i=function(e){var r=0,t=0;for(var i=0;i0){if(a<4096)r+=a+63>>6;else t+=a+511>>9}}var f=e.FullPaths.length+3>>2;var s=r+7>>3;var h=r+127>>7;var l=s+t+f+h;var o=l+127>>7;var c=o<=109?0:Math.ceil((o-109)/127);while(l+o+c+127>>7>o)c=++o<=109?0:Math.ceil((o-109)/127);var u=[1,c,o,h,f,t,r,0];e.FileIndex[0].size=r<<6;u[7]=(e.FileIndex[0].start=u[0]+u[1]+u[2]+u[3]+u[4]+u[5])+(u[6]+7>>3);return u}(e);var n=new_buf(i[7]<<9);var a=0,f=0;{for(a=0;a<8;++a)n.write_shift(1,A[a]);for(a=0;a<8;++a)n.write_shift(2,0);n.write_shift(2,62);n.write_shift(2,3);n.write_shift(2,65534);n.write_shift(2,9);n.write_shift(2,6);for(a=0;a<3;++a)n.write_shift(2,0);n.write_shift(4,0);n.write_shift(4,i[2]);n.write_shift(4,i[0]+i[1]+i[2]+i[3]-1);n.write_shift(4,0);n.write_shift(4,1<<12);n.write_shift(4,i[3]?i[0]+i[1]+i[2]-1:y);n.write_shift(4,i[3]);n.write_shift(-4,i[1]?i[0]-1:y);n.write_shift(4,i[1]);for(a=0;a<109;++a)n.write_shift(-4,a>9)}s(i[6]+7>>3);while(n.l&511)n.write_shift(-4,B.ENDOFCHAIN);f=a=0;for(h=0;h=4096)continue;o.start=f;s(l+63>>6)}while(n.l&511)n.write_shift(-4,B.ENDOFCHAIN);for(a=0;a=4096){n.l=o.start+1<<9;for(h=0;h0&&o.size<4096){for(h=0;h*/, Paths/*:Arr if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -684,8 +684,9 @@ function cfb_new(opts/*:?any*/)/*:CFBContainer*/ { } function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, opts/*:?any*/)/*:CFBEntry*/ { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath/*:string*/ = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -696,7 +697,7 @@ function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, o file = ({name: filename(name), type: 2}/*:any*/); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } /*:: if(!file) throw new Error("unreachable"); */ file.content = (content/*:any*/); diff --git a/package.json b/package.json index 4ec1091..02c34c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cfb", - "version": "1.0.5", + "version": "1.0.6", "author": "sheetjs", "description": "Compound File Binary File Format extractor", "keywords": [ diff --git a/shim.js b/shim.js index 61a6eaf..ba84f82 100644 --- a/shim.js +++ b/shim.js @@ -18,6 +18,12 @@ if(!Array.prototype.indexOf) Array.prototype.indexOf = function(needle) { return -1; }; +if(!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(needle) { + var len = (this.length>>>0), i = len - 1; + for(; i>=0; --i) if(this[i] === needle) return i; + return -1; +}; + if(!Array.isArray) Array.isArray = function(obj) { return Object.prototype.toString.call(obj) === "[object Array]"; }; if(typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) ArrayBuffer.prototype.slice = function(start, end) { diff --git a/types/index.d.ts b/types/index.d.ts index 9550556..76113ff 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -91,10 +91,16 @@ export interface CFB$Container { }; } +/** cfb_add options */ +export interface CFB$AddOpts { + /** Skip existence and safety checks (best for bulk write operations) */ + unsafe?: boolean; +} + /** General utilities */ export interface CFB$Utils { cfb_new(opts?: any): CFB$Container; - cfb_add(cfb: CFB$Container, name: string, content: any, opts?: any): CFB$Entry; + cfb_add(cfb: CFB$Container, name: string, content: any, opts?: CFB$AddOpts): CFB$Entry; cfb_del(cfb: CFB$Container, name: string): boolean; cfb_mov(cfb: CFB$Container, old_name: string, new_name: string): boolean; cfb_gc(cfb: CFB$Container): void; diff --git a/types/roundtrip.ts b/types/roundtrip.ts index a9dfc84..23f8cda 100644 --- a/types/roundtrip.ts +++ b/types/roundtrip.ts @@ -61,7 +61,7 @@ const old_cfb = CFB.read("t2.xls", {type:"file"}); const new_cfb = CFB.utils.cfb_new({root:"R", clsid: old_cfb.FileIndex[0].clsid }); old_cfb.FullPaths.forEach((p, i) => { if(p.slice(-1) === "/") return; - CFB.utils.cfb_add(new_cfb, p.replace(/^[^/]*/,"R"), old_cfb.FileIndex[i].content); + CFB.utils.cfb_add(new_cfb, p.replace(/^[^/]*/,"R"), old_cfb.FileIndex[i].content, {unsafe: true}); }); dumpit(new_cfb); CFB.writeFile(new_cfb, "t3.xls"); diff --git a/xlscfb.flow.js b/xlscfb.flow.js index 7324ed9..b4db579 100644 --- a/xlscfb.flow.js +++ b/xlscfb.flow.js @@ -38,7 +38,7 @@ type CFBFiles = {[n:string]:CFBEntry}; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.0.5'; +exports.version = '1.0.6'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -216,8 +216,8 @@ function build_full_paths(FI/*:CFBFileIndex*/, FP/*:Array*/, Paths/*:Arr if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -684,8 +684,9 @@ function cfb_new(opts/*:?any*/)/*:CFBContainer*/ { } function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, opts/*:?any*/)/*:CFBEntry*/ { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath/*:string*/ = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -696,7 +697,7 @@ function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, o file = ({name: filename(name), type: 2}/*:any*/); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } /*:: if(!file) throw new Error("unreachable"); */ file.content = (content/*:any*/); diff --git a/xlscfb.js b/xlscfb.js index c6d9b58..9b426e3 100644 --- a/xlscfb.js +++ b/xlscfb.js @@ -8,7 +8,7 @@ var DO_NOT_EXPORT_CFB = true; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.0.5'; +exports.version = '1.0.6'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -186,8 +186,8 @@ function build_full_paths(FI, FP, Paths) { if(R !== -1 && dad[R] !== R) dad[i] = dad[R]; } if(C !== -1 /*NOSTREAM*/) dad[C] = i; - if(L !== -1) { dad[L] = dad[i]; q.push(L); } - if(R !== -1) { dad[R] = dad[i]; q.push(R); } + if(L !== -1) { dad[L] = dad[i]; if(q.lastIndexOf(L) < j) q.push(L); } + if(R !== -1) { dad[R] = dad[i]; if(q.lastIndexOf(R) < j) q.push(R); } } for(i=1; i < pl; ++i) if(dad[i] === i) { if(R !== -1 /*NOSTREAM*/ && dad[R] !== R) dad[i] = dad[R]; @@ -648,8 +648,9 @@ function cfb_new(opts) { } function cfb_add(cfb, name, content, opts) { - init_cfb(cfb); - var file = CFB.find(cfb, name); + var unsafe = opts && opts.unsafe; + if(!unsafe) init_cfb(cfb); + var file = !unsafe && CFB.find(cfb, name); if(!file) { var fpath = cfb.FullPaths[0]; if(name.slice(0, fpath.length) == fpath) fpath = name; @@ -660,7 +661,7 @@ function cfb_add(cfb, name, content, opts) { file = ({name: filename(name), type: 2}); cfb.FileIndex.push(file); cfb.FullPaths.push(fpath); - CFB.utils.cfb_gc(cfb); + if(!unsafe) CFB.utils.cfb_gc(cfb); } file.content = (content); file.size = content ? content.length : 0;