js-cfb/dist/cfb.min.js

3 lines
27 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */
var Base64=function r(){var r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";return{encode:function(e){var t="";var i=0,n=0,f=0,a=0,s=0,h=0,l=0;for(var o=0;o<e.length;){i=e.charCodeAt(o++);a=i>>2;n=e.charCodeAt(o++);s=(i&3)<<4|n>>4;f=e.charCodeAt(o++);h=(n&15)<<2|f>>6;l=f&63;if(isNaN(n)){h=l=64}else if(isNaN(f)){l=64}t+=r.charAt(a)+r.charAt(s)+r.charAt(h)+r.charAt(l)}return t},decode:function e(t){var i="";var n=0,f=0,a=0,s=0,h=0,l=0,o=0;t=t.replace(/[^\w\+\/\=]/g,"");for(var u=0;u<t.length;){s=r.indexOf(t.charAt(u++));h=r.indexOf(t.charAt(u++));n=s<<2|h>>4;i+=String.fromCharCode(n);l=r.indexOf(t.charAt(u++));f=(h&15)<<4|l>>2;if(l!==64){i+=String.fromCharCode(f)}o=r.indexOf(t.charAt(u++));a=(l&3)<<6|o;if(o!==64){i+=String.fromCharCode(a)}}return i}}}();var has_buf=typeof Buffer!=="undefined"&&typeof process!=="undefined"&&typeof process.versions!=="undefined"&&process.versions.node;var Buffer_from=function(){};if(typeof Buffer!=="undefined"){var nbfs=!Buffer.from;if(!nbfs)try{Buffer.from("foo","utf8")}catch(e){nbfs=true}Buffer_from=nbfs?function(r,e){return e?new Buffer(r,e):new Buffer(r)}:Buffer.from.bind(Buffer);if(!Buffer.alloc)Buffer.alloc=function(r){return new Buffer(r)};if(!Buffer.allocUnsafe)Buffer.allocUnsafe=function(r){return new Buffer(r)}}function new_raw_buf(r){return has_buf?Buffer.alloc(r):new Array(r)}function new_unsafe_buf(r){return has_buf?Buffer.allocUnsafe(r):new Array(r)}var s2a=function e(r){if(has_buf)return Buffer_from(r,"binary");return r.split("").map(function(r){return r.charCodeAt(0)&255})};var chr0=/\u0000/g,chr1=/[\u0001-\u0006]/g;var __toBuffer=function(r){var e=[];for(var t=0;t<r[0].length;++t){e.push.apply(e,r[0][t])}return e};var ___toBuffer=__toBuffer;var __utf16le=function(r,e,t){var i=[];for(var n=e;n<t;n+=2)i.push(String.fromCharCode(__readUInt16LE(r,n)));return i.join("").replace(chr0,"")};var ___utf16le=__utf16le;var __hexlify=function(r,e,t){var i=[];for(var n=e;n<e+t;++n)i.push(("0"+r[n].toString(16)).slice(-2));return i.join("")};var ___hexlify=__hexlify;var __bconcat=function(r){if(Array.isArray(r[0]))return[].concat.apply([],r);var e=0,t=0;for(t=0;t<r.length;++t)e+=r[t].length;var i=new Uint8Array(e);for(t=0,e=0;t<r.length;e+=r[t].length,++t)i.set(r[t],e);return i};var bconcat=__bconcat;if(has_buf){__utf16le=function(r,e,t){if(!Buffer.isBuffer(r))return ___utf16le(r,e,t);return r.toString("utf16le",e,t).replace(chr0,"")};__hexlify=function(r,e,t){return Buffer.isBuffer(r)?r.toString("hex",e,e+t):___hexlify(r,e,t)};__toBuffer=function(r){return r[0].length>0&&Buffer.isBuffer(r[0][0])?Buffer.concat(r[0]):___toBuffer(r)};s2a=function(r){return Buffer_from(r,"binary")};bconcat=function(r){return Buffer.isBuffer(r[0])?Buffer.concat(r):__bconcat(r)}}var __readUInt8=function(r,e){return r[e]};var __readUInt16LE=function(r,e){return r[e+1]*(1<<8)+r[e]};var __readInt16LE=function(r,e){var t=r[e+1]*(1<<8)+r[e];return t<32768?t:(65535-t+1)*-1};var __readUInt32LE=function(r,e){return r[e+3]*(1<<24)+(r[e+2]<<16)+(r[e+1]<<8)+r[e]};var __readInt32LE=function(r,e){return(r[e+3]<<24)+(r[e+2]<<16)+(r[e+1]<<8)+r[e]};function ReadShift(r,e){var t,i,n=0;switch(r){case 1:t=__readUInt8(this,this.l);break;case 2:t=(e!=="i"?__readUInt16LE:__readInt16LE)(this,this.l);break;case 4:t=__readInt32LE(this,this.l);break;case 16:n=2;i=__hexlify(this,this.l,r);}this.l+=r;if(n===0)return t;return i}var __writeUInt32LE=function(r,e,t){r[t]=e&255;r[t+1]=e>>>8&255;r[t+2]=e>>>16&255;r[t+3]=e>>>24&255};var __writeInt32LE=function(r,e,t){r[t]=e&255;r[t+1]=e>>8&255;r[t+2]=e>>16&255;r[t+3]=e>>24&255};function WriteShift(r,e,t){var i=0,n=0;switch(t){case"hex":for(;n<r;++n){this[this.l++]=parseInt(e.slice(2*n,2*n+2),16)||0}return this;case"utf16le":var f=this.l+r;for(n=0;n<Math.min(e.length,r);++n){var a=e.charCodeAt(n);this[this.l++]=a&255;this[this.l++]=a>>8}while(this.l<f)this[this.l++]=0;return this;}switch(r){case 1:i=1;this[this.l]=e&255;break;case 2:i=2;this[this.l]=e&255;e>>>=8;this[this.l+1]=e&255;break;case 4:i=4;__writeUInt32LE(this,e,this.l);break;case-4:i=4;__writeInt32LE(this,e,this.l);break;}this.l+=i;return this}function CheckField(r,e){var t=__hexlify(this,this.l,r.length>>1);if(t!==r)throw new Error(e+"Expected "+r+" saw "+t);this.l+=r.length>>1}function prep_blob(r,e){r.l=e;r.read_shift=ReadShift;r.chk=CheckField;r.write_shift=WriteShift}function new_buf(r){var e=new_raw_buf(r);prep_blob(e,0);return e}var CRC32;(function(r){r(CRC32={})})(function(r){r.version="1.2.0";function e(){var r=0,e=new Array(256);for(var t=0;t!=256;++t){r=t;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;r=r&1?-306674912^r>>>1:r>>>1;e[t]=r}return typeof Int32Array!=="undefined"?new Int32Array(e):e}var t=e();function i(r,e){var i=e^-1,n=r.length-1;for(var f=0;f<n;){i=i>>>8^t[(i^r.charCodeAt(f++))&255];i=i>>>8^t[(i^r.charCodeAt(f++))&255]}if(f===n)i=i>>>8^t[(i^r.charCodeAt(f))&255];return i^-1}function n(r,e){if(r.length>1e4)return f(r,e);var i=e^-1,n=r.length-3;for(var a=0;a<n;){i=i>>>8^t[(i^r[a++])&255];i=i>>>8^t[(i^r[a++])&255];i=i>>>8^t[(i^r[a++])&255];i=i>>>8^t[(i^r[a++])&255]}while(a<n+3)i=i>>>8^t[(i^r[a++])&255];return i^-1}function f(r,e){var i=e^-1,n=r.length-7;for(var f=0;f<n;){i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255];i=i>>>8^t[(i^r[f++])&255]}while(f<n+7)i=i>>>8^t[(i^r[f++])&255];return i^-1}function a(r,e){var i=e^-1;for(var n=0,f=r.length,a,s;n<f;){a=r.charCodeAt(n++);if(a<128){i=i>>>8^t[(i^a)&255]}else if(a<2048){i=i>>>8^t[(i^(192|a>>6&31))&255];i=i>>>8^t[(i^(128|a&63))&255]}else if(a>=55296&&a<57344){a=(a&1023)+64;s=r.charCodeAt(n++)&1023;i=i>>>8^t[(i^(240|a>>8&7))&255];i=i>>>8^t[(i^(128|a>>2&63))&255];i=i>>>8^t[(i^(128|s>>6&15|(a&3)<<4))&255];i=i>>>8^t[(i^(128|s&63))&255]}else{i=i>>>8^t[(i^(224|a>>12&15))&255];i=i>>>8^t[(i^(128|a>>6&63))&255];i=i>>>8^t[(i^(128|a&63))&255]}}return i^-1}r.table=t;r.bstr=i;r.buf=n;r.str=a});var CFB=function t(){var r={};r.version="1.1.0";function e(r,e){var t=r.split("/"),i=e.split("/");for(var n=0,f=0,a=Math.min(t.length,i.length);n<a;++n){if(f=t[n].length-i[n].length)return f;if(t[n]!=i[n])return t[n]<i[n]?-1:1}return t.length-i.length}function t(r){if(r.charAt(r.length-1)=="/")return r.slice(0,-1).indexOf("/")===-1?r:t(r.slice(0,-1));var e=r.lastIndexOf("/");return e===-1?r:r.slice(0,e+1)}function i(r){if(r.charAt(r.length-1)=="/")return i(r.slice(0,-1));var e=r.lastIndexOf("/");return e===-1?r:r.slice(e+1)}function n(r,e){if(typeof e==="string")e=new Date(e);var t=e.getHours();t=t<<6|e.getMinutes();t=t<<5|e.getSeconds()>>>1;r.write_shift(2,t);var i=e.getFullYear()-1980;i=i<<4|e.getMonth()+1;i=i<<5|e.getDate();r.write_shift(2,i)}function f(r){var e=r.read_shift(2)&65535;var t=r.read_shift(2)&65535;var i=new Date;var n=t&31;t>>>=5;var f=t&15;t>>>=4;i.setMilliseconds(0);i.setFullYear(t+1980);i.setMonth(f-1);i.setDate(n);var a=e&31;e>>>=5;var s=e&63;e>>>=6;i.setHours(e);i.setMinutes(s);i.setSeconds(a<<1);return i}function a(r){prep_blob(r,0);var e={};var t=0;while(r.l<=r.length-4){var i=r.read_shift(2);var n=r.read_shift(2),f=r.l+n;var a={};switch(i){case 21589:{t=r.read_shift(1);if(t&1)a.mtime=r.read_shift(4);if(n>5){if(t&2)a.atime=r.read_shift(4);if(t&4)a.ctime=r.read_shift(4)}if(a.mtime)a.mt=new Date(a.mtime*1e3)}break;}r.l=f;e[i]=a}return e}var s;function h(){return s||(s=require("fs"))}function l(r,e){if(r[0]==80&&r[1]==75)return wr(r,e);if(r.length<512)throw new Error("CFB file size "+r.length+" < 512");var t=3;var i=512;var n=0;var f=0;var a=0;var s=0;var h=0;var l=[];var v=r.slice(0,512);prep_blob(v,0);var w=o(v);t=w[0];switch(t){case 3:i=512;break;case 4:i=4096;break;case 0:if(w[1]==0)return wr(r,e);default:throw new Error("Major Version: Expected 3 or 4 saw "+t);}if(i!==512){v=r.slice(0,i);prep_blob(v,28)}var b=r.slice(0,i);u(v,t);var F=v.read_shift(4,"i");if(t===3&&F!==0)throw new Error("# Directory Sectors: Expected 0 saw "+F);v.l+=4;a=v.read_shift(4,"i");v.l+=4;v.chk("00100000","Mini Stream Cutoff Size: ");s=v.read_shift(4,"i");n=v.read_shift(4,"i");h=v.read_shift(4,"i");f=v.read_shift(4,"i");for(var y=-1,I=0;I<109;++I){y=v.read_shift(4,"i");if(y<0)break;l[I]=y}var C=c(r,i);d(h,f,C,i,l);var x=p(C,a,l,i);x[a].name="!Directory";if(n>0&&s!==B)x[s].name="!MiniFAT";x[l[0]].name="!FAT";x.fat_addrs=l;x.ssz=i;var m={},E=[],A=[],S=[];g(a,x,C,E,n,m,A,s);_(A,S,E);E.shift();var k={FileIndex:A,FullPaths:S};if(e&&e.raw)k.raw={header:b,sectors:C};return k}function o(r){if(r[r.l]==80&&r[r.l+1]==75)return[0,0];r.chk(S,"Header Signature: ");r.chk(R,"CLSID: ");var e=r.read_shift(2,"u");return[r.read_shift(2,"u"),e]}function u(r,e){var t=9;r.l+=2;switch(t=r.read_shift(2)){case 9:if(e!=3)throw new Error("Sector Shift: Expected 9 saw "+t);break;case 12:if(e!=4)throw new Error("Sector Shift: Expected 12 saw "+t);break;default:throw new Error("Sector Shift: Expected 9 or 12 saw "+t);}r.chk("0600","Mini Sector Shift: ");r.chk("000000000000","Reserved: ")}function c(r,e){var t=Math.ceil(r.length/e)-1;var i=[];for(var n=1;n<t;++n)i[n-1]=r.slice(n*e,(n+1)*e);i[t-1]=r.slice(t*e);return i}function _(r,e,t){var i=0,n=0,f=0,a=0,s=0,h=t.length;var l=[],o=[];for(;i<h;++i){l[i]=o[i]=i;e[i]=t[i]}for(;s<o.length;++s){i=o[s];n=r[i].L;f=r[i].R;a=r[i].C;if(l[i]===i){if(n!==-1&&l[n]!==n)l[i]=l[n];if(f!==-1&&l[f]!==f)l[i]=l[f]}if(a!==-1)l[a]=i;if(n!==-1){l[n]=l[i];if(o.lastIndexOf(n)<s)o.push(n)}if(f!==-1){l[f]=l[i];if(o.lastIndexOf(f)<s)o.push(f)}}for(i=1;i<h;++i)if(l[i]===i){if(f!==-1&&l[f]!==f)l[i]=l[f];else if(n!==-1&&l[n]!==n)l[i]=l[n]}for(i=1;i<h;++i){if(r[i].type===0)continue;s=l[i];if(s===0)e[i]=e[0]+"/"+e[i];else while(s!==0&&s!==l[s]){e[i]=e[s]+"/"+e[i];s=l[s]}l[i]=0}e[0]+="/";for(i=1;i<h;++i){if(r[i].type!==2)e[i]+="/"}}function v(r,e,t){var i=r.start,n=r.size;var f=[];var a=i;while(t&&n>0&&a>=0){f.push(e.slice(a*A,a*A+A));n-=A;a=__readInt32LE(t,a*4)}if(f.length===0)return new_buf(0);return bconcat(f).slice(0,r.size)}function d(r,e,t,i,n){var f=B;if(r===B){if(e!==0)throw new Error("DIFAT chain shorter than expected")}else if(r!==-1){var a=t[r],s=(i>>>2)-1;if(!a)return;for(var h=0;h<s;++h){if((f=__readInt32LE(a,h*4))===B)break;n.push(f)}d(__readInt32LE(a,i-4),e-1,t,i,n)}}function w(r,e,t,i,n){var f=[],a=[];if(!n)n=[];var s=i-1,h=0,l=0;for(h=e;h>=0;){n[h]=true;f[f.length]=h;a.push(r[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(!r[o])break;h=__readInt32LE(r[o],l)}return{nodes:f,data:__toBuffer([a])}}function p(r,e,t,i){var n=r.length,f=[];var a=[],s=[],h=[];var l=i-1,o=0,u=0,c=0,_=0;for(o=0;o<n;++o){s=[];c=o+e;if(c>=n)c-=n;if(a[c])continue;h=[];for(u=c;u>=0;){a[u]=true;s[s.length]=u;h.push(r[u]);var v=t[Math.floor(u*4/i)];_=u*4&l;if(i<4+_)throw new Error("FAT boundary crossed: "+u+" 4 "+i);if(!r[v])break;u=__readInt32LE(r[v],_)}f[c]={nodes:s,data:__toBuffer([h])}}return f}function g(r,e,t,i,n,f,a,s){var h=0,l=i.length?2:0;var o=e[r].data;var u=0,c=0,_;for(;u<o.length;u+=128){var d=o.slice(u,u+128);prep_blob(d,64);c=d.read_shift(2);_=__utf16le(d,0,c-l);i.push(_);var p={name:_,type:d.read_shift(1),color:d.read_shift(1),L:d.read_shift(4,"i"),R:d.read_shift(4,"i"),C:d.read_shift(4,"i"),clsid:d.read_shift(16),state:d.read_shift(4,"i"),start:0,size:0};var g=d.read_shift(2)+d.read_shift(2)+d.read_shift(2)+d.read_shift(2);if(g!==0)p.ct=b(d,d.l-8);var F=d.read_shift(2)+d.read_shift(2)+d.read_shift(2)+d.read_shift(2);if(F!==0)p.mt=b(d,d.l-8);p.start=d.read_shift(4,"i");p.size=d.read_shift(4,"i");if(p.size<0&&p.start<0){p.size=p.type=0;p.start=B;p.name=""}if(p.type===5){h=p.start;if(n>0&&h!==B)e[h].name="!StreamData"}else if(p.size>=4096){p.storage="fat";if(e[p.start]===undefined)e[p.start]=w(t,p.start,e.fat_addrs,e.ssz);e[p.start].name=p.name;p.content=e[p.start].data.slice(0,p.size)}else{p.storage="minifat";if(p.size<0)p.size=0;else if(h!==B&&p.start!==B&&e[h]){p.content=v(p,e[h].data,(e[s]||{}).data)}}if(p.content)prep_blob(p.content,0);f[_]=p;a.push(p)}}function b(r,e){return new Date((__readUInt32LE(r,e+4)/1e7*Math.pow(2,32)+__readUInt32LE(r,e)/1e7-11644473600)*1e3)}function F(r,e){h();return l(s.readFileSync(r),e)}function y(r,e){switch(e&&e.type||"base64"){case"file":return F(r,e);case"base64":return l(s2a(Base64.decode(r)),e);case"binary":return l(s2a(r),e);}return l(r,e)}function I(r,e){var t=e||{},i=t.root||"Root Entry";if(!r.FullPaths)r.FullPaths=[];if(!r.FileIndex)r.FileIndex=[];if(r.FullPaths.length!==r.FileIndex.length)throw new Error("inconsistent CFB structure");if(r.FullPaths.length===0){r.FullPaths[0]=i+"/";r.FileIndex[0]={name:i,type:5}}if(t.CLSID)r.FileIndex[0].clsid=t.CLSID;C(r)}function C(r){var e="Sh33tJ5";if(CFB.find(r,"/"+e))return;var t=new_buf(4);t[0]=55;t[1]=t[3]=50;t[2]=54;r.FileIndex.push({name:e,type:2,content:t,size:4,L:69,R:69,C:69});r.FullPaths.push(r.FullPaths[0]+e);x(r)}function x(r,n){I(r);var f=false,a=false;for(var s=r.FullPaths.length-1;s>=0;--s){var h=r.FileIndex[s];switch(h.type){case 0:if(a)f=true;else{r.FileIndex.pop();r.FullPaths.pop()}break;case 1:;case 2:;case 5:a=true;if(isNaN(h.R*h.L*h.C))f=true;if(h.R>-1&&h.L>-1&&h.R==h.L)f=true;break;default:f=true;break;}}if(!f&&!n)return;var l=new Date(1987,1,19),o=0;var u=[];for(s=0;s<r.FullPaths.length;++s){if(r.FileIndex[s].type===0)continue;u.push([r.FullPaths[s],r.FileIndex[s]])}for(s=0;s<u.length;++s){var c=t(u[s][0]);a=false;for(o=0;o<u.length;++o)if(u[o][0]===c)a=true;if(!a)u.push([c,{name:i(c).replace("/",""),type:1,clsid:R,ct:l,mt:l,content:null}])}u.sort(function(r,t){return e(r[0],t[0])});r.FullPaths=[];r.FileIndex=[];for(s=0;s<u.length;++s){r.FullPaths[s]=u[s][0];r.FileIndex[s]=u[s][1]}for(s=0;s<u.length;++s){var _=r.FileIndex[s];var v=r.FullPaths[s];_.name=i(v).replace("/","");_.L=_.R=_.C=-(_.color=1);_.size=_.content?_.content.length:0;_.start=0;_.clsid=_.clsid||R;if(s===0){_.C=u.length>1?1:-1;_.size=0;_.type=5}else if(v.slice(-1)=="/"){for(o=s+1;o<u.length;++o)if(t(r.FullPaths[o])==v)break;_.C=o>=u.length?-1:o;for(o=s+1;o<u.length;++o)if(t(r.FullPaths[o])==t(v))break;_.R=o>=u.length?-1:o;_.type=1}else{if(t(r.FullPaths[s+1]||"")==t(v))_.R=s+1;_.type=2}}}function m(r,e){var t=e||{};x(r);if(t.fileType=="zip")return gr(r,t);var i=function(r){var e=0,t=0;for(var i=0;i<r.FileIndex.length;++i){var n=r.FileIndex[i];if(!n.content)continue;var f=n.content.length;if(f>0){if(f<4096)e+=f+63>>6;else t+=f+511>>9}}var a=r.FullPaths.length+3>>2;var s=e+7>>3;var h=e+127>>7;var l=s+t+a+h;var o=l+127>>7;var u=o<=109?0:Math.ceil((o-109)/127);while(l+o+u+127>>7>o)u=++o<=109?0:Math.ceil((o-109)/127);var c=[1,u,o,h,a,t,e,0];r.FileIndex[0].size=e<<6;c[7]=(r.FileIndex[0].start=c[0]+c[1]+c[2]+c[3]+c[4]+c[5])+(c[6]+7>>3);return c}(r);var n=new_buf(i[7]<<9);var f=0,a=0;{for(f=0;f<8;++f)n.write_shift(1,k[f]);for(f=0;f<8;++f)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(f=0;f<3;++f)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:B);n.write_shift(4,i[3]);n.write_shift(-4,i[1]?i[0]-1:B);n.write_shift(4,i[1]);for(f=0;f<109;++f)n.write_shift(-4,f<i[2]?i[1]+f:-1)}if(i[1]){for(a=0;a<i[1];++a){for(;f<236+a*127;++f)n.write_shift(-4,f<i[2]?i[1]+f:-1);n.write_shift(-4,a===i[1]-1?B:a+1)}}var s=function(r){for(a+=r;f<a-1;++f)n.write_shift(-4,f+1);if(r){++f;n.write_shift(-4,B)}};a=f=0;for(a+=i[1];f<a;++f)n.write_shift(-4,z.DIFSECT);for(a+=i[2];f<a;++f)n.write_shift(-4,z.FATSECT);s(i[3]);s(i[4]);var h=0,l=0;var o=r.FileIndex[0];for(;h<r.FileIndex.length;++h){o=r.FileIndex[h];if(!o.content)continue;l=o.content.length;if(l<4096)continue;o.start=a;s(l+511>>9)}s(i[6]+7>>3);while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);a=f=0;for(h=0;h<r.FileIndex.length;++h){o=r.FileIndex[h];if(!o.content)continue;l=o.content.length;if(!l||l>=4096)continue;o.start=a;s(l+63>>6)}while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);for(f=0;f<i[4]<<2;++f){var u=r.FullPaths[f];if(!u||u.length===0){for(h=0;h<17;++h)n.write_shift(4,0);for(h=0;h<3;++h)n.write_shift(4,-1);for(h=0;h<12;++h)n.write_shift(4,0);continue}o=r.FileIndex[f];if(f===0)o.start=o.size?o.start-1:B;var c=f===0&&t.root||o.name;l=2*(c.length+1);n.write_shift(64,c,"utf16le");n.write_shift(2,l);n.write_shift(1,o.type);n.write_shift(1,o.color);n.write_shift(-4,o.L);n.write_shift(-4,o.R);n.write_shift(-4,o.C);if(!o.clsid)for(h=0;h<4;++h)n.write_shift(4,0);else n.write_shift(16,o.clsid,"hex");n.write_shift(4,o.state||0);n.write_shift(4,0);n.write_shift(4,0);n.write_shift(4,0);n.write_shift(4,0);n.write_shift(4,o.start);n.write_shift(4,o.size);n.write_shift(4,0)}for(f=1;f<r.FileIndex.length;++f){o=r.FileIndex[f];if(o.size>=4096){n.l=o.start+1<<9;for(h=0;h<o.size;++h)n.write_shift(1,o.content[h]);for(;h&511;++h)n.write_shift(1,0)}}for(f=1;f<r.FileIndex.length;++f){o=r.FileIndex[f];if(o.size>0&&o.size<4096){for(h=0;h<o.size;++h)n.write_shift(1,o.content[h]);for(;h&63;++h)n.write_shift(1,0)}}while(n.l<n.length)n.write_shift(1,0);return n}function E(r,e){var t=r.FullPaths.map(function(r){return r.toUpperCase()});var i=t.map(function(r){var e=r.split("/");return e[e.length-(r.slice(-1)=="/"?2:1)]});var n=false;if(e.charCodeAt(0)===47){n=true;e=t[0].slice(0,-1)+e}else n=e.indexOf("/")!==-1;var f=e.toUpperCase();var a=n===true?t.indexOf(f):i.indexOf(f);if(a!==-1)return r.FileIndex[a];var s=!f.match(chr1);f=f.replace(chr0,"");if(s)f=f.replace(chr1,"!");for(a=0;a<t.length;++a){if((s?t[a].replace(chr1,"!"):t[a]).replace(chr0,"")==f)return r.FileIndex[a];if((s?i[a].replace(chr1,"!"):i[a]).replace(chr0,"")==f)return r.FileIndex[a]}return null}var A=64;var B=-2;var S="d0cf11e0a1b11ae1";var k=[208,207,17,224,161,177,26,225];var R="00000000000000000000000000000000";var z={MAXREGSECT:-6,DIFSECT:-4,FATSECT:-3,ENDOFCHAIN:B,FREESECT:-1,HEADER_SIGNATURE:S,HEADER_MINOR_VERSION:"3e00",MAXREGSID:-6,NOSTREAM:-1,HEADER_CLSID:R,EntryTypes:["unknown","storage","stream","lockbytes","property","root"]};function L(r,e,t){h();var i=m(r,t);s.writeFileSync(e,i)}function U(r){var e=new Array(r.length);for(var t=0;t<r.length;++t)e[t]=String.fromCharCode(r[t]);return e.join("")}function P(r,e){var t=m(r,e);switch(e&&e.type){case"file":h();s.writeFileSync(e.filename,t);return t;case"binary":return U(t);case"base64":return Base64.encode(U(t));}return t}var D;function M(r){try{var e=r.InflateRaw;var t=new e;t._processChunk(new Uint8Array([3,0]),t._finishFlushFlag);if(t.bytesRead)D=r;else throw new Error("zlib does not expose bytesRead")}catch(i){console.error("cannot use native zlib: "+(i.message||i))}}function O(r,e){if(!D)return vr(r,e);var t=D.InflateRaw;var i=new t;var n=i._processChunk(r.slice(r.l),i._finishFlushFlag);r.l+=i.bytesRead;return n}function T(r){return D?D.deflateRawSync(r):tr(r)}var N=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];var H=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258];var j=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577];function G(r){var e=(r<<1|r<<11)&139536|(r<<5|r<<15)&558144;return(e>>16|e>>8|e)&255}var X=typeof Uint8Array!=="undefined";var q=X?new Uint8Array(1<<8):[];for(var J=0;J<1<<8;++J)q[J]=G(J);function V(r,e){var t=q[r&255];if(e<=8)return t>>>8-e;t=t<<8|q[r>>8&255];if(e<=16)return t>>>16-e;t=t<<8|q[r>>16&255];return t>>>24-e}function W(r,e){var t=e&7,i=e>>>3;return(r[i]|(t<=6?0:r[i+1]<<8))>>>t&3}function Y(r,e){var t=e&7,i=e>>>3;return(r[i]|(t<=5?0:r[i+1]<<8))>>>t&7}function Z(r,e){var t=e&7,i=e>>>3;return(r[i]|(t<=4?0:r[i+1]<<8))>>>t&15}function K(r,e){var t=e&7,i=e>>>3;return(r[i]|(t<=3?0:r[i+1]<<8))>>>t&31}function Q(r,e){var t=e&7,i=e>>>3;return(r[i]|(t<=1?0:r[i+1]<<8))>>>t&127}function $(r,e,t){var i=e&7,n=e>>>3,f=(1<<t)-1;var a=r[n]>>>i;if(t<8-i)return a&f;a|=r[n+1]<<8-i;if(t<16-i)return a&f;a|=r[n+2]<<16-i;if(t<24-i)return a&f;a|=r[n+3]<<24-i;return a&f}function rr(r,e){var t=r.length,i=2*t>e?2*t:e+5,n=0;if(t>=e)return r;if(has_buf){var f=new_unsafe_buf(i);if(r.copy)r.copy(f);else for(;n<r.length;++n)f[n]=r[n];return f}else if(X){var a=new Uint8Array(i);if(a.set)a.set(r);else for(;n<r.length;++n)a[n]=r[n];return a}r.length=i;return r}function er(r){var e=new Array(r);for(var t=0;t<r;++t)e[t]=0;return e}var tr=function(){var r=function(){return function r(e,t){var i=0;while(i<e.length){var n=Math.min(65535,e.length-i);var f=i+n==e.length;t.write_shift(1,+f);t.write_shift(2,n);t.write_shift(2,~n&65535);while(n-- >0)t[t.l++]=e[i++]}return t.l}}();return function(e){var t=new_buf(50+Math.floor(e.length*1.1));var i=r(e,t);return t.slice(0,i)}}();function ir(r,e,t){var i=1,n=0,f=0,a=0,s=0,h=r.length;var l=X?new Uint16Array(32):er(32);for(f=0;f<32;++f)l[f]=0;for(f=h;f<t;++f)r[f]=0;h=r.length;var o=X?new Uint16Array(h):er(h);for(f=0;f<h;++f){l[n=r[f]]++;if(i<n)i=n;o[f]=0}l[0]=0;for(f=1;f<=i;++f)l[f+16]=s=s+l[f-1]<<1;for(f=0;f<h;++f){s=r[f];if(s!=0)o[f]=l[s+16]++}var u=0;for(f=0;f<h;++f){u=r[f];if(u!=0){s=V(o[f],i)>>i-u;for(a=(1<<i+4-u)-1;a>=0;--a)e[s|a<<u]=u&15|f<<4}}return i}var nr=X?new Uint16Array(512):er(512);var fr=X?new Uint16Array(32):er(32);if(!X){for(var ar=0;ar<512;++ar)nr[ar]=0;for(ar=0;ar<32;++ar)fr[ar]=0}(function(){var r=[];var e=0;for(;e<32;e++)r.push(5);ir(r,fr,32);var t=[];e=0;for(;e<=143;e++)t.push(8);for(;e<=255;e++)t.push(9);for(;e<=279;e++)t.push(7);for(;e<=287;e++)t.push(8);ir(t,nr,288)})();var sr=X?new Uint16Array(32768):er(32768);var hr=X?new Uint16Array(32768):er(32768);var lr=X?new Uint16Array(128):er(128);var or=1,ur=1;function cr(r,e){var t=K(r,e)+257;e+=5;var i=K(r,e)+1;e+=5;var n=Z(r,e)+4;e+=4;var f=0;var a=X?new Uint8Array(19):er(19);var s=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];var h=1;var l=X?new Uint8Array(8):er(8);var o=X?new Uint8Array(8):er(8);var u=a.length;for(var c=0;c<n;++c){a[N[c]]=f=Y(r,e);if(h<f)h=f;l[f]++;e+=3}var _=0;l[0]=0;for(c=1;c<=h;++c)o[c]=_=_+l[c-1]<<1;for(c=0;c<u;++c)if((_=a[c])!=0)s[c]=o[_]++;var v=0;for(c=0;c<u;++c){v=a[c];if(v!=0){_=q[s[c]]>>8-v;for(var d=(1<<7-v)-1;d>=0;--d)lr[_|d<<v]=v&7|c<<3}}var w=[];h=1;for(;w.length<t+i;){_=lr[Q(r,e)];e+=_&7;switch(_>>>=3){case 16:f=3+W(r,e);e+=2;_=w[w.length-1];while(f-- >0)w.push(_);break;case 17:f=3+Y(r,e);e+=3;while(f-- >0)w.push(0);break;case 18:f=11+Q(r,e);e+=7;while(f-- >0)w.push(0);break;default:w.push(_);if(h<_)h=_;break;}}var p=w.slice(0,t),g=w.slice(t);for(c=t;c<286;++c)p[c]=0;for(c=i;c<30;++c)g[c]=0;or=ir(p,sr,286);ur=ir(g,hr,30);return e}function _r(r,e){if(r[0]==3&&!(r[1]&3)){return[new_raw_buf(e),2]}var t=0;var i=0;var n=new_unsafe_buf(e?e:1<<18);var f=0;var a=n.length>>>0;var s=0,h=0;while((i&1)==0){i=Y(r,t);t+=3;if(i>>>1==0){if(t&7)t+=8-(t&7);var l=r[t>>>3]|r[(t>>>3)+1]<<8;t+=32;if(!e&&a<f+l){n=rr(n,f+l);a=n.length}if(typeof r.copy==="function"){r.copy(n,f,t>>>3,(t>>>3)+l);f+=l;t+=8*l}else while(l-- >0){n[f++]=r[t>>>3];t+=8}continue}else if(i>>>1==1){s=9;h=5}else{t=cr(r,t);s=or;h=ur}if(!e&&a<f+32767){n=rr(n,f+32767);a=n.length}for(;;){var o=$(r,t,s);var u=i>>>1==1?nr[o]:sr[o];t+=u&15;u>>>=4;if((u>>>8&255)===0)n[f++]=u;else if(u==256)break;else{u-=257;var c=u<8?0:u-4>>2;if(c>5)c=0;var _=f+H[u];if(c>0){_+=$(r,t,c);t+=c}o=$(r,t,h);u=i>>>1==1?fr[o]:hr[o];t+=u&15;u>>>=4;var v=u<4?0:u-2>>1;var d=j[u];if(v>0){d+=$(r,t,v);t+=v}if(!e&&a<_){n=rr(n,_);a=n.length}while(f<_){n[f]=n[f-d];++f}}}}return[e?n:n.slice(0,f),t+7>>>3]}function vr(r,e){var t=r.slice(r.l||0);var i=_r(t,e);r.l+=i[1];return i[0]}function dr(r,e){if(r){if(typeof console!=="undefined")console.error(e)}else throw new Error(e)}function wr(r,e){var t=r;prep_blob(t,0);var i=[],n=[];var f={FileIndex:i,FullPaths:n};I(f,{root:e.root});var s=t.length-4;while((t[s]!=80||t[s+1]!=75||t[s+2]!=5||t[s+3]!=6)&&s>=0)--s;t.l=s+4;t.l+=4;var h=t.read_shift(2);t.l+=6;var l=t.read_shift(4);t.l=l;for(s=0;s<h;++s){t.l+=20;var o=t.read_shift(4);var u=t.read_shift(4);var c=t.read_shift(2);var _=t.read_shift(2);var v=t.read_shift(2);t.l+=8;var d=t.read_shift(4);var w=a(t.slice(t.l+c,t.l+c+_));t.l+=c+_+v;var p=t.l;t.l=d+4;pr(t,o,u,f,w);t.l=p}return f}function pr(r,e,t,i,n){r.l+=2;var s=r.read_shift(2);var h=r.read_shift(2);var l=f(r);if(s&8257)throw new Error("Unsupported ZIP encryption");var o=r.read_shift(4);var u=r.read_shift(4);var c=r.read_shift(4);var _=r.read_shift(2);var v=r.read_shift(2);var d="";for(var w=0;w<_;++w)d+=String.fromCharCode(r[r.l++]);if(v){var p=a(r.slice(r.l,r.l+v));if((p[21589]||{}).mt)l=p[21589].mt;if(((n||{})[21589]||{}).mt)l=n[21589].mt}r.l+=v;var g=r.slice(r.l,r.l+u);switch(h){case 8:g=O(r,c);break;case 0:break;default:throw new Error("Unsupported ZIP Compression method "+h);}var b=false;if(s&8){o=r.read_shift(4);if(o==134695760){o=r.read_shift(4);b=true}u=r.read_shift(4);c=r.read_shift(4)}if(u!=e)dr(b,"Bad compressed size: "+e+" != "+u);if(c!=t)dr(b,"Bad uncompressed size: "+t+" != "+c);var F=CRC32.buf(g,0);if(o!=F)dr(b,"Bad CRC32 checksum: "+o+" != "+F);Fr(i,d,g,{unsafe:true,mt:l})}function gr(r,e){var t=e||{};var i=[],f=[];var a=new_buf(1);var s=t.compression?8:0,h=0;var l=false;if(l)h|=8;var o=0,u=0;var c=0,_=0;var v=r.FullPaths[0],d=v,w=r.FileIndex[0];var p=[];var g=0;for(o=1;o<r.FullPaths.length;++o){d=r.FullPaths[o].slice(v.length);w=r.FileIndex[o];if(!w.size||!w.content||d=="Sh33tJ5")continue;var b=c;var F=new_buf(d.length);for(u=0;u<d.length;++u)F.write_shift(1,d.charCodeAt(u)&127);F=F.slice(0,F.l);p[_]=CRC32.buf(w.content,0);var y=w.content;if(s==8)y=T(y);a=new_buf(30);a.write_shift(4,67324752);a.write_shift(2,20);a.write_shift(2,h);a.write_shift(2,s);if(w.mt)n(a,w.mt);else a.write_shift(4,0);a.write_shift(-4,h&8?0:p[_]);a.write_shift(4,h&8?0:y.length);a.write_shift(4,h&8?0:w.content.length);a.write_shift(2,F.length);a.write_shift(2,0);c+=a.length;i.push(a);c+=F.length;i.push(F);c+=y.length;i.push(y);if(h&8){a=new_buf(12);a.write_shift(-4,p[_]);a.write_shift(4,y.length);a.write_shift(4,w.content.length);c+=a.l;i.push(a)}a=new_buf(46);a.write_shift(4,33639248);a.write_shift(2,0);a.write_shift(2,20);a.write_shift(2,h);a.write_shift(2,s);a.write_shift(4,0);a.write_shift(-4,p[_]);a.write_shift(4,y.length);a.write_shift(4,w.content.length);a.write_shift(2,F.length);a.write_shift(2,0);a.write_shift(2,0);a.write_shift(2,0);a.write_shift(2,0);a.write_shift(4,0);a.write_shift(4,b);g+=a.l;f.push(a);g+=F.length;f.push(F);++_}a=new_buf(22);a.write_shift(4,101010256);a.write_shift(2,0);a.write_shift(2,0);a.write_shift(2,_);a.write_shift(2,_);a.write_shift(4,g);a.write_shift(4,c);a.write_shift(2,0);return bconcat([bconcat(i),bconcat(f),a])}function br(r){var e={};I(e,r);return e}function Fr(r,e,t,n){var f=n&&n.unsafe;if(!f)I(r);var a=!f&&CFB.find(r,e);if(!a){var s=r.FullPaths[0];if(e.slice(0,s.length)==s)s=e;else{if(s.slice(-1)!="/")s+="/";s=(s+e).replace("//","/")}a={name:i(e),type:2};r.FileIndex.push(a);r.FullPaths.push(s);if(!f)CFB.utils.cfb_gc(r)}a.content=t;a.size=t?t.length:0;if(n){if(n.CLSID)a.clsid=n.CLSID;if(n.mt)a.mt=n.mt;if(n.ct)a.ct=n.ct}return a}function yr(r,e){I(r);var t=CFB.find(r,e);if(t)for(var i=0;i<r.FileIndex.length;++i)if(r.FileIndex[i]==t){r.FileIndex.splice(i,1);r.FullPaths.splice(i,1);return true}return false}function Ir(r,e,t){I(r);var n=CFB.find(r,e);if(n)for(var f=0;f<r.FileIndex.length;++f)if(r.FileIndex[f]==n){r.FileIndex[f].name=i(t);r.FullPaths[f]=t;return true}return false}function Cr(r){x(r,true)}r.find=E;r.read=y;r.parse=l;r.write=P;r.writeFile=L;r.utils={cfb_new:br,cfb_add:Fr,cfb_del:yr,cfb_mov:Ir,cfb_gc:Cr,ReadShift:ReadShift,CheckField:CheckField,prep_blob:prep_blob,bconcat:bconcat,use_zlib:M,_deflateRaw:tr,_inflateRaw:vr,consts:z};return r}();if(typeof require!=="undefined"&&typeof module!=="undefined"&&typeof DO_NOT_EXPORT_CFB==="undefined"){module.exports=CFB}