From ad93f76a36f893362efe96f738a327c290c1f2a9 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Wed, 6 Apr 2022 02:45:11 -0400 Subject: [PATCH] version bump 1.2.2 --- Makefile | 32 +-- bits/05_buf.js | 29 +-- bits/31_version.js | 2 +- cfb.flow.js | 286 ++++++++++++------------- cfb.js | 31 +-- dist/cfb.js | 275 ++++++++++++------------ dist/cfb.min.js | 4 +- dist/cfb.min.map | 2 +- dist/xlscfb.js | 179 ++++++++-------- index.html | 21 +- misc/{suppress_export.js => xlscfb.js} | 2 - package.json | 10 +- shim.js | 101 ++++++++- xlscfb.flow.js | 179 ++++++++-------- xlscfb.js | 166 +++++++------- 15 files changed, 720 insertions(+), 599 deletions(-) rename misc/{suppress_export.js => xlscfb.js} (89%) diff --git a/Makefile b/Makefile index 068b4b0..4787093 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ bits/31_version.js: package.json .PHONY: clean clean: ## Remove targets and build artifacts - rm -f $(TARGET) $(FLOWTARGET) + rm -f $(TARGET) $(FLOWTARGET) xlscfb.js xlscfb.flow.js .PHONY: clean-data clean-data: @@ -46,13 +46,15 @@ init: ## Initial setup for development .PHONY: dist dist: dist-deps $(TARGET) ## Prepare JS files for distribution - cp $(TARGET) dist/ + mkdir -p dist cp LICENSE dist/ + cp $(TARGET) dist/ uglifyjs $(TARGET) $(UGLIFYOPTS) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)" misc/strip_sourcemap.sh dist/$(LIB).min.js .PHONY: dist-deps dist-deps: xlscfb.js ## Copy dependencies for distribution + mkdir -p dist cp xlscfb.flow.js dist/xlscfb.js .PHONY: aux @@ -61,15 +63,15 @@ aux: $(AUXTARGETS) .PHONY: xls xls: xlscfb.js -XLSSKIP=bits/08_blob.js bits/04_base64.js bits/05_buf.js -XLSDEPS=misc/suppress_export.js $(filter-out $(XLSSKIP),$(DEPS)) +XLSSKIP=bits/08_blob.js bits/04_base64.js bits/05_buf.js bits/98_exports.js +XLSDEPS=misc/xlscfb.js $(filter-out $(XLSSKIP),$(DEPS)) xlscfb.flow.js: $(XLSDEPS) ## Build support library - cat $^ | tr -d '\15\32' > $@ + cat $^ | tr -d '\15\32' | grep -v DO_NOT_EXPORT_CFB > $@ BYTEFILE=dist/cfb.min.js dist/xlscfb.js .PHONY: bytes bytes: ## Display minified and gzipped file sizes - for i in $(BYTEFILE); do printj "%-30s %7d %10d" $$i $$(wc -c < $$i) $$(gzip --best --stdout $$i | wc -c); done + for i in $(BYTEFILE); do npx printj "%-30s %7d %10d" $$i $$(wc -c < $$i) $$(gzip --best --stdout $$i | wc -c); done ## Testing @@ -93,23 +95,23 @@ fullint: lint old-lint tslint flow mdlint ## Run all checks .PHONY: lint lint: $(TARGET) $(AUXTARGETS) ## Run eslint checks - @eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(CMDS) $(HTMLLINT) package.json - if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi + @./node_modules/.bin/eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(CMDS) $(HTMLLINT) package.json + @if [ -x "$(CLOSURE)" ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi .PHONY: old-lint old-lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks - @jshint --show-non-errors $(TARGET) $(AUXTARGETS) - @jshint --show-non-errors $(CMDS) - @jshint --show-non-errors package.json test.js - @jshint --show-non-errors --extract=always $(HTMLLINT) - @jscs $(TARGET) $(AUXTARGETS) test.js - if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi + @./node_modules/.bin/jscs $(TARGET) $(AUXTARGETS) test.js + @./node_modules/.bin/jshint --show-non-errors $(TARGET) $(AUXTARGETS) + @./node_modules/.bin/jshint --show-non-errors $(CMDS) + @./node_modules/.bin/jshint --show-non-errors package.json test.js + @./node_modules/.bin/jshint --show-non-errors --extract=always $(HTMLLINT) + @if [ -x "$(CLOSURE)" ]; then java -jar $(CLOSURE) $(REQS) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi .PHONY: tslint tslint: $(TARGET) ## Run typescript checks #@npm install dtslint typescript #@npm run-script dtslint - dtslint types + ./node_modules/.bin/dtslint types .PHONY: flow flow: lint ## Run flow checker diff --git a/bits/05_buf.js b/bits/05_buf.js index 378e99b..b7b2fa3 100644 --- a/bits/05_buf.js +++ b/bits/05_buf.js @@ -1,26 +1,29 @@ -var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && process.versions.node); +var has_buf = /*#__PURE__*/(function() { return typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node; })(); -var Buffer_from = /*::(*/function(){}/*:: :any)*/; +var Buffer_from = /*#__PURE__*/(function() { + if(typeof Buffer !== 'undefined') { + var nbfs = !Buffer.from; + if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } + return nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); + } + return function() {}; +})(); -if(typeof Buffer !== 'undefined') { - var nbfs = !Buffer.from; - if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } - Buffer_from = /*::((*/nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer)/*::) :any)*/; - // $FlowIgnore - if(!Buffer.alloc) Buffer.alloc = function(n) { var b = new Buffer(n); b.fill(0); return b; }; - // $FlowIgnore - if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); }; -} function new_raw_buf(len/*:number*/) { /* jshint -W056 */ - return has_buf ? Buffer.alloc(len) : new Array(len); + if(has_buf) { + if(Buffer.alloc) return Buffer.alloc(len); + var b = new Buffer(len); b.fill(0); return b; + } + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } function new_unsafe_buf(len/*:number*/) { /* jshint -W056 */ - return has_buf ? Buffer.allocUnsafe(len) : new Array(len); + if(has_buf) return Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : new Buffer(len); + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } diff --git a/bits/31_version.js b/bits/31_version.js index 668f4a1..1e7b7ef 100644 --- a/bits/31_version.js +++ b/bits/31_version.js @@ -1 +1 @@ -exports.version = '1.2.1'; +exports.version = '1.2.2'; diff --git a/cfb.flow.js b/cfb.flow.js index 0143a34..454939a 100644 --- a/cfb.flow.js +++ b/cfb.flow.js @@ -4,73 +4,68 @@ /*exported CFB */ /*global module, require:false, process:false, Buffer:false, Uint8Array:false, Uint16Array:false */ -var Base64 = (function make_b64(){ - var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - return { - encode: function(input/*:string*/)/*:string*/ { - var o = ""; - var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0; - for(var i = 0; i < input.length; ) { - c1 = input.charCodeAt(i++); - e1 = (c1 >> 2); - - c2 = input.charCodeAt(i++); - e2 = ((c1 & 3) << 4) | (c2 >> 4); - - c3 = input.charCodeAt(i++); - e3 = ((c2 & 15) << 2) | (c3 >> 6); - e4 = (c3 & 63); - if (isNaN(c2)) { e3 = e4 = 64; } - else if (isNaN(c3)) { e4 = 64; } - o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4); - } - return o; - }, - decode: function b64_decode(input/*:string*/)/*:string*/ { - var o = ""; - var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0; - input = input.replace(/[^\w\+\/\=]/g, ""); - for(var i = 0; i < input.length;) { - e1 = map.indexOf(input.charAt(i++)); - e2 = map.indexOf(input.charAt(i++)); - c1 = (e1 << 2) | (e2 >> 4); - o += String.fromCharCode(c1); - - e3 = map.indexOf(input.charAt(i++)); - c2 = ((e2 & 15) << 4) | (e3 >> 2); - if (e3 !== 64) { o += String.fromCharCode(c2); } - - e4 = map.indexOf(input.charAt(i++)); - c3 = ((e3 & 3) << 6) | e4; - if (e4 !== 64) { o += String.fromCharCode(c3); } - } - return o; - } - }; -})(); -var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && process.versions.node); - -var Buffer_from = /*::(*/function(){}/*:: :any)*/; - -if(typeof Buffer !== 'undefined') { - var nbfs = !Buffer.from; - if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } - Buffer_from = /*::((*/nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer)/*::) :any)*/; - // $FlowIgnore - if(!Buffer.alloc) Buffer.alloc = function(n) { var b = new Buffer(n); b.fill(0); return b; }; - // $FlowIgnore - if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); }; +var Base64_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +function Base64_encode(input/*:string*/)/*:string*/ { + var o = ""; + var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0; + for (var i = 0; i < input.length; ) { + c1 = input.charCodeAt(i++); + e1 = (c1 >> 2); + c2 = input.charCodeAt(i++); + e2 = ((c1 & 3) << 4) | (c2 >> 4); + c3 = input.charCodeAt(i++); + e3 = ((c2 & 15) << 2) | (c3 >> 6); + e4 = (c3 & 63); + if (isNaN(c2)) e3 = e4 = 64; + else if (isNaN(c3)) e4 = 64; + o += Base64_map.charAt(e1) + Base64_map.charAt(e2) + Base64_map.charAt(e3) + Base64_map.charAt(e4); + } + return o; } +function Base64_decode(input/*:string*/)/*:string*/ { + var o = ""; + var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0; + input = input.replace(/[^\w\+\/\=]/g, ""); + for (var i = 0; i < input.length;) { + e1 = Base64_map.indexOf(input.charAt(i++)); + e2 = Base64_map.indexOf(input.charAt(i++)); + c1 = (e1 << 2) | (e2 >> 4); + o += String.fromCharCode(c1); + e3 = Base64_map.indexOf(input.charAt(i++)); + c2 = ((e2 & 15) << 4) | (e3 >> 2); + if (e3 !== 64) o += String.fromCharCode(c2); + e4 = Base64_map.indexOf(input.charAt(i++)); + c3 = ((e3 & 3) << 6) | e4; + if (e4 !== 64) o += String.fromCharCode(c3); + } + return o; +} +var has_buf = /*#__PURE__*/(function() { return typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node; })(); + +var Buffer_from = /*#__PURE__*/(function() { + if(typeof Buffer !== 'undefined') { + var nbfs = !Buffer.from; + if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } + return nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); + } + return function() {}; +})(); + function new_raw_buf(len/*:number*/) { /* jshint -W056 */ - return has_buf ? Buffer.alloc(len) : new Array(len); + if(has_buf) { + if(Buffer.alloc) return Buffer.alloc(len); + var b = new Buffer(len); b.fill(0); return b; + } + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } function new_unsafe_buf(len/*:number*/) { /* jshint -W056 */ - return has_buf ? Buffer.allocUnsafe(len) : new Array(len); + if(has_buf) return Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : new Buffer(len); + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } @@ -191,21 +186,18 @@ type SectorList = { } type CFBFiles = {[n:string]:CFBEntry}; */ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ -var CRC32; -(function (factory) { - /*jshint ignore:start */ - /*eslint-disable */ - factory(CRC32 = {}); - /*eslint-enable */ - /*jshint ignore:end */ -}(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +var CRC32 = /*#__PURE__*/(function() { +var CRC32 = {}; +CRC32.version = '1.2.1'; +/*:: +type ABuf = Array | Buffer | Uint8Array; +type CRC32TableType = Array | Int32Array; +*/ /*global Int32Array */ -function signed_crc_table()/*:any*/ { +function signed_crc_table()/*:CRC32TableType*/ { var c = 0, table/*:Array*/ = new Array(256); for(var n =0; n != 256; ++n){ @@ -224,78 +216,77 @@ function signed_crc_table()/*:any*/ { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); -function crc32_bstr(bstr/*:string*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; +var T0 = signed_crc_table(); +function slice_by_16_tables(T) { + var c = 0, v = 0, n = 0, table/*:Array*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; + + for(n = 0; n != 256; ++n) table[n] = T[n]; + for(n = 0; n != 256; ++n) { + v = T[n]; + for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF]; } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + var out = []; + for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256); + return out; +} +var TT = slice_by_16_tables(T0); +var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4]; +var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9]; +var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14]; +function crc32_bstr(bstr/*:string*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf/*:Uint8Array|Array*/, seed/*:number*/)/*:number*/ { - if(buf.length > 10000) return crc32_buf_8(buf, seed); - var C = seed ^ -1, L = buf.length - 3; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; +function crc32_buf(B/*:ABuf*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1, L = B.length - 15, i = 0; + for(; i < L;) C = + Tf[B[i++] ^ (C & 255)] ^ + Te[B[i++] ^ ((C >> 8) & 255)] ^ + Td[B[i++] ^ ((C >> 16) & 255)] ^ + Tc[B[i++] ^ (C >>> 24)] ^ + Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^ + T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^ + T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]]; + L += 15; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } -function crc32_buf_8(buf/*:Uint8Array|Array*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1, L = buf.length - 7; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - -function crc32_str(str/*:string*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1; - for(var i = 0, L=str.length, c, d; i < L;) { +function crc32_str(str/*:string*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1; + for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } else if(c >= 0xD800 && c < 0xE000) { c = (c&1023)+64; d = str.charCodeAt(i++)&1023; - C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF]; } else { - C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; CRC32.bstr = crc32_bstr; CRC32.buf = crc32_buf; CRC32.str = crc32_str; -})); +return CRC32; +})(); /* [MS-CFB] v20171201 */ -var CFB = (function _CFB(){ +var CFB = /*#__PURE__*/(function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.2.1'; +exports.version = '1.2.2'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -595,7 +586,7 @@ function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array*/, if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break; fat_addrs.push(q); } - sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); + if(cnt >= 1) sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); } } @@ -713,7 +704,7 @@ function read(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) { } switch(type || "base64") { case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return read_file(blob, options); - case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options); + case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64_decode(blob)), options); case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options); } return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options); @@ -771,7 +762,9 @@ function rebuild_cfb(cfb/*:CFBContainer*/, f/*:?boolean*/)/*:void*/ { for(i = 0; i < data.length; ++i) { var dad = dirname(data[i][0]); s = fullPaths[dad]; - if(!s) { + while(!s) { + while(dirname(dad) && !fullPaths[dirname(dad)]) dad = dirname(dad); + data.push([dad, ({ name: filename(dad).replace("/",""), type: 1, @@ -779,8 +772,12 @@ function rebuild_cfb(cfb/*:CFBContainer*/, f/*:?boolean*/)/*:void*/ { ct: now, mt: now, content: null }/*:any*/)]); + // Add name to set fullPaths[dad] = true; + + dad = dirname(data[i][0]); + s = fullPaths[dad]; } } @@ -828,7 +825,6 @@ function _write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|strin for(var i = 0; i < cfb.FileIndex.length; ++i) { var file = cfb.FileIndex[i]; if(!file.content) continue; - /*:: if(file.content == null) throw new Error("unreachable"); */ var flen = file.content.length; if(flen > 0){ if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6; @@ -919,6 +915,10 @@ function _write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|strin file = cfb.FileIndex[i]; if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN; var _nm/*:string*/ = (i === 0 && _opts.root) || file.name; + if(_nm.length > 32) { + console.error("Name " + _nm + " will be truncated to " + _nm.slice(0,32)); + _nm = _nm.slice(0, 32); + } flen = 2*(_nm.length+1); o.write_shift(64, _nm, "utf16le"); o.write_shift(2, flen); @@ -1036,7 +1036,7 @@ function write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|string switch(options && options.type || "buffer") { case "file": get_fs(); fs.writeFileSync(options.filename, (o/*:any*/)); return o; case "binary": return typeof o == "string" ? o : a2s(o); - case "base64": return Base64.encode(typeof o == "string" ? o : a2s(o)); + case "base64": return Base64_encode(typeof o == "string" ? o : a2s(o)); case "buffer": if(has_buf) return Buffer.isBuffer(o) ? o : Buffer_from(o); /* falls through */ case "array": return typeof o == "string" ? s2a(o) : o; @@ -1226,15 +1226,16 @@ if(!use_typed_arrays) { for(; i<=279; i++) clens.push(7); for(; i<=287; i++) clens.push(8); build_tree(clens, fix_lmap, 288); -})();var _deflateRaw = (function() { +})();var _deflateRaw = /*#__PURE__*/(function _deflateRawIIFE() { var DST_LN_RE = use_typed_arrays ? new Uint8Array(0x8000) : []; - for(var j = 0, k = 0; j < DST_LN.length; ++j) { + var j = 0, k = 0; + for(; j < DST_LN.length - 1; ++j) { for(; k < DST_LN[j+1]; ++k) DST_LN_RE[k] = j; } for(;k < 32768; ++k) DST_LN_RE[k] = 29; - var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x102) : []; - for(j = 0, k = 0; j < LEN_LN.length; ++j) { + var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x103) : []; + for(j = 0, k = 0; j < LEN_LN.length - 1; ++j) { for(; k < LEN_LN[j+1]; ++k) LEN_LN_RE[k] = j; } @@ -1447,14 +1448,12 @@ function inflate(data, usz/*:number*/) { var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8; boff += 32; /* push sz bytes */ - if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } - if(typeof data.copy === 'function') { - // $FlowIgnore - data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz); - woff += sz; boff += 8*sz; - } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + if(sz > 0) { + if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } + while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + } continue; - } else if((header >>> 1) == 1) { + } else if((header >> 1) == 1) { /* Fixed Huffman */ max_len_1 = 9; max_len_2 = 5; } else { @@ -1499,7 +1498,8 @@ function inflate(data, usz/*:number*/) { } } } - return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3]; + if(usz) return [outbuf, (boff+7)>>>3]; + return [outbuf.slice(0, woff), (boff+7)>>>3]; } function _inflate(payload, usz) { @@ -1746,7 +1746,7 @@ function get_content_type(fi/*:CFBEntry*/, fp/*:string*/)/*:string*/ { /* 76 character chunks TODO: intertwine encoding */ function write_base64_76(bstr/*:string*/)/*:string*/ { - var data = Base64.encode(bstr); + var data = Base64_encode(bstr); var o = []; for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76)); return o.join("\r\n") + "\r\n"; @@ -1807,7 +1807,7 @@ function parse_quoted_printable(data/*:Array*/)/*:RawBytes*/ { } /* decode */ - for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/=[0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); + for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/[=][0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); return s2a(o.join("\r\n")); } @@ -1827,7 +1827,7 @@ function parse_mime(cfb/*:CFBContainer*/, data/*:Array*/, root/*:string* } ++di; switch(cte.toLowerCase()) { - case 'base64': fdata = s2a(Base64.decode(data.slice(di).join(""))); break; + case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break; case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break; default: throw new Error("Unsupported Content-Transfer-Encoding " + cte); } diff --git a/cfb.js b/cfb.js index 8f168e7..dec6197 100644 --- a/cfb.js +++ b/cfb.js @@ -40,29 +40,32 @@ function Base64_decode(input) { } return o; } -var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && process.versions.node); +var has_buf = (function() { return typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node; })(); -var Buffer_from = function(){}; +var Buffer_from = (function() { + if(typeof Buffer !== 'undefined') { + var nbfs = !Buffer.from; + if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } + return nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); + } + return function() {}; +})(); -if(typeof Buffer !== 'undefined') { - var nbfs = !Buffer.from; - if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; } - Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); - // $FlowIgnore - if(!Buffer.alloc) Buffer.alloc = function(n) { var b = new Buffer(n); b.fill(0); return b; }; - // $FlowIgnore - if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); }; -} function new_raw_buf(len) { /* jshint -W056 */ - return has_buf ? Buffer.alloc(len) : new Array(len); + if(has_buf) { + if(Buffer.alloc) return Buffer.alloc(len); + var b = new Buffer(len); b.fill(0); return b; + } + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } function new_unsafe_buf(len) { /* jshint -W056 */ - return has_buf ? Buffer.allocUnsafe(len) : new Array(len); + if(has_buf) return Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : new Buffer(len); + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } @@ -261,7 +264,7 @@ return CRC32; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.2.1'; +exports.version = '1.2.2'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); diff --git a/dist/cfb.js b/dist/cfb.js index fb58011..dec6197 100644 --- a/dist/cfb.js +++ b/dist/cfb.js @@ -4,73 +4,68 @@ /*exported CFB */ /*global module, require:false, process:false, Buffer:false, Uint8Array:false, Uint16Array:false */ -var Base64 = (function make_b64(){ - var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - return { - encode: function(input) { - var o = ""; - var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0; - for(var i = 0; i < input.length; ) { - c1 = input.charCodeAt(i++); - e1 = (c1 >> 2); - - c2 = input.charCodeAt(i++); - e2 = ((c1 & 3) << 4) | (c2 >> 4); - - c3 = input.charCodeAt(i++); - e3 = ((c2 & 15) << 2) | (c3 >> 6); - e4 = (c3 & 63); - if (isNaN(c2)) { e3 = e4 = 64; } - else if (isNaN(c3)) { e4 = 64; } - o += map.charAt(e1) + map.charAt(e2) + map.charAt(e3) + map.charAt(e4); - } - return o; - }, - decode: function b64_decode(input) { - var o = ""; - var c1=0, c2=0, c3=0, e1=0, e2=0, e3=0, e4=0; - input = input.replace(/[^\w\+\/\=]/g, ""); - for(var i = 0; i < input.length;) { - e1 = map.indexOf(input.charAt(i++)); - e2 = map.indexOf(input.charAt(i++)); - c1 = (e1 << 2) | (e2 >> 4); - o += String.fromCharCode(c1); - - e3 = map.indexOf(input.charAt(i++)); - c2 = ((e2 & 15) << 4) | (e3 >> 2); - if (e3 !== 64) { o += String.fromCharCode(c2); } - - e4 = map.indexOf(input.charAt(i++)); - c3 = ((e3 & 3) << 6) | e4; - if (e4 !== 64) { o += String.fromCharCode(c3); } - } - return o; - } - }; -})(); -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(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); - // $FlowIgnore - if(!Buffer.alloc) Buffer.alloc = function(n) { var b = new Buffer(n); b.fill(0); return b; }; - // $FlowIgnore - if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); }; +var Base64_map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +function Base64_encode(input) { + var o = ""; + var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0; + for (var i = 0; i < input.length; ) { + c1 = input.charCodeAt(i++); + e1 = (c1 >> 2); + c2 = input.charCodeAt(i++); + e2 = ((c1 & 3) << 4) | (c2 >> 4); + c3 = input.charCodeAt(i++); + e3 = ((c2 & 15) << 2) | (c3 >> 6); + e4 = (c3 & 63); + if (isNaN(c2)) e3 = e4 = 64; + else if (isNaN(c3)) e4 = 64; + o += Base64_map.charAt(e1) + Base64_map.charAt(e2) + Base64_map.charAt(e3) + Base64_map.charAt(e4); + } + return o; } +function Base64_decode(input) { + var o = ""; + var c1 = 0, c2 = 0, c3 = 0, e1 = 0, e2 = 0, e3 = 0, e4 = 0; + input = input.replace(/[^\w\+\/\=]/g, ""); + for (var i = 0; i < input.length;) { + e1 = Base64_map.indexOf(input.charAt(i++)); + e2 = Base64_map.indexOf(input.charAt(i++)); + c1 = (e1 << 2) | (e2 >> 4); + o += String.fromCharCode(c1); + e3 = Base64_map.indexOf(input.charAt(i++)); + c2 = ((e2 & 15) << 4) | (e3 >> 2); + if (e3 !== 64) o += String.fromCharCode(c2); + e4 = Base64_map.indexOf(input.charAt(i++)); + c3 = ((e3 & 3) << 6) | e4; + if (e4 !== 64) o += String.fromCharCode(c3); + } + return o; +} +var has_buf = (function() { return 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; } + return nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer); + } + return function() {}; +})(); + function new_raw_buf(len) { /* jshint -W056 */ - return has_buf ? Buffer.alloc(len) : new Array(len); + if(has_buf) { + if(Buffer.alloc) return Buffer.alloc(len); + var b = new Buffer(len); b.fill(0); return b; + } + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } function new_unsafe_buf(len) { /* jshint -W056 */ - return has_buf ? Buffer.allocUnsafe(len) : new Array(len); + if(has_buf) return Buffer.allocUnsafe ? Buffer.allocUnsafe(len) : new Buffer(len); + return typeof Uint8Array != "undefined" ? new Uint8Array(len) : new Array(len); /* jshint +W056 */ } @@ -173,19 +168,12 @@ function new_buf(sz) { return o; } -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ -var CRC32; -(function (factory) { - /*jshint ignore:start */ - /*eslint-disable */ - factory(CRC32 = {}); - /*eslint-enable */ - /*jshint ignore:end */ -}(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +var CRC32 = (function() { +var CRC32 = {}; +CRC32.version = '1.2.1'; /*global Int32Array */ function signed_crc_table() { var c = 0, table = new Array(256); @@ -206,78 +194,77 @@ function signed_crc_table() { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); +var T0 = signed_crc_table(); +function slice_by_16_tables(T) { + var c = 0, v = 0, n = 0, table = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; + + for(n = 0; n != 256; ++n) table[n] = T[n]; + for(n = 0; n != 256; ++n) { + v = T[n]; + for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF]; + } + var out = []; + for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256); + return out; +} +var TT = slice_by_16_tables(T0); +var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4]; +var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9]; +var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14]; function crc32_bstr(bstr, seed) { - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + var C = seed ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf, seed) { - if(buf.length > 10000) return crc32_buf_8(buf, seed); - var C = seed ^ -1, L = buf.length - 3; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - -function crc32_buf_8(buf, seed) { - var C = seed ^ -1, L = buf.length - 7; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; +function crc32_buf(B, seed) { + var C = seed ^ -1, L = B.length - 15, i = 0; + for(; i < L;) C = + Tf[B[i++] ^ (C & 255)] ^ + Te[B[i++] ^ ((C >> 8) & 255)] ^ + Td[B[i++] ^ ((C >> 16) & 255)] ^ + Tc[B[i++] ^ (C >>> 24)] ^ + Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^ + T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^ + T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]]; + L += 15; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } function crc32_str(str, seed) { var C = seed ^ -1; - for(var i = 0, L=str.length, c, d; i < L;) { + for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } else if(c >= 0xD800 && c < 0xE000) { c = (c&1023)+64; d = str.charCodeAt(i++)&1023; - C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF]; } else { - C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; CRC32.bstr = crc32_bstr; CRC32.buf = crc32_buf; CRC32.str = crc32_str; -})); +return CRC32; +})(); /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.2.1'; +exports.version = '1.2.2'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -577,7 +564,7 @@ function sleuth_fat(idx, cnt, sectors, ssz, fat_addrs) { if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break; fat_addrs.push(q); } - sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); + if(cnt >= 1) sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); } } @@ -695,7 +682,7 @@ function read(blob, options) { } switch(type || "base64") { case "file": return read_file(blob, options); - case "base64": return parse(s2a(Base64.decode(blob)), options); + case "base64": return parse(s2a(Base64_decode(blob)), options); case "binary": return parse(s2a(blob), options); } return parse(blob, options); @@ -753,7 +740,9 @@ function rebuild_cfb(cfb, f) { for(i = 0; i < data.length; ++i) { var dad = dirname(data[i][0]); s = fullPaths[dad]; - if(!s) { + while(!s) { + while(dirname(dad) && !fullPaths[dirname(dad)]) dad = dirname(dad); + data.push([dad, ({ name: filename(dad).replace("/",""), type: 1, @@ -761,8 +750,12 @@ function rebuild_cfb(cfb, f) { ct: now, mt: now, content: null })]); + // Add name to set fullPaths[dad] = true; + + dad = dirname(data[i][0]); + s = fullPaths[dad]; } } @@ -810,7 +803,7 @@ function _write(cfb, options) { for(var i = 0; i < cfb.FileIndex.length; ++i) { var file = cfb.FileIndex[i]; if(!file.content) continue; -var flen = file.content.length; + var flen = file.content.length; if(flen > 0){ if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6; else fat_size += (flen + 0x01FF) >> 9; @@ -898,6 +891,10 @@ flen = file.content.length; file = cfb.FileIndex[i]; if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN; var _nm = (i === 0 && _opts.root) || file.name; + if(_nm.length > 32) { + console.error("Name " + _nm + " will be truncated to " + _nm.slice(0,32)); + _nm = _nm.slice(0, 32); + } flen = 2*(_nm.length+1); o.write_shift(64, _nm, "utf16le"); o.write_shift(2, flen); @@ -1012,7 +1009,7 @@ function write(cfb, options) { switch(options && options.type || "buffer") { case "file": get_fs(); fs.writeFileSync(options.filename, (o)); return o; case "binary": return typeof o == "string" ? o : a2s(o); - case "base64": return Base64.encode(typeof o == "string" ? o : a2s(o)); + case "base64": return Base64_encode(typeof o == "string" ? o : a2s(o)); case "buffer": if(has_buf) return Buffer.isBuffer(o) ? o : Buffer_from(o); /* falls through */ case "array": return typeof o == "string" ? s2a(o) : o; @@ -1202,15 +1199,16 @@ if(!use_typed_arrays) { for(; i<=279; i++) clens.push(7); for(; i<=287; i++) clens.push(8); build_tree(clens, fix_lmap, 288); -})();var _deflateRaw = (function() { +})();var _deflateRaw = (function _deflateRawIIFE() { var DST_LN_RE = use_typed_arrays ? new Uint8Array(0x8000) : []; - for(var j = 0, k = 0; j < DST_LN.length; ++j) { + var j = 0, k = 0; + for(; j < DST_LN.length - 1; ++j) { for(; k < DST_LN[j+1]; ++k) DST_LN_RE[k] = j; } for(;k < 32768; ++k) DST_LN_RE[k] = 29; - var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x102) : []; - for(j = 0, k = 0; j < LEN_LN.length; ++j) { + var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x103) : []; + for(j = 0, k = 0; j < LEN_LN.length - 1; ++j) { for(; k < LEN_LN[j+1]; ++k) LEN_LN_RE[k] = j; } @@ -1423,14 +1421,12 @@ function inflate(data, usz) { var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8; boff += 32; /* push sz bytes */ - if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } - if(typeof data.copy === 'function') { - // $FlowIgnore - data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz); - woff += sz; boff += 8*sz; - } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + if(sz > 0) { + if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } + while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + } continue; - } else if((header >>> 1) == 1) { + } else if((header >> 1) == 1) { /* Fixed Huffman */ max_len_1 = 9; max_len_2 = 5; } else { @@ -1475,7 +1471,8 @@ function inflate(data, usz) { } } } - return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3]; + if(usz) return [outbuf, (boff+7)>>>3]; + return [outbuf.slice(0, woff), (boff+7)>>>3]; } function _inflate(payload, usz) { @@ -1722,7 +1719,7 @@ function get_content_type(fi, fp) { /* 76 character chunks TODO: intertwine encoding */ function write_base64_76(bstr) { - var data = Base64.encode(bstr); + var data = Base64_encode(bstr); var o = []; for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76)); return o.join("\r\n") + "\r\n"; @@ -1783,7 +1780,7 @@ function parse_quoted_printable(data) { } /* decode */ - for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/=[0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); + for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/[=][0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); return s2a(o.join("\r\n")); } @@ -1803,7 +1800,7 @@ function parse_mime(cfb, data, root) { } ++di; switch(cte.toLowerCase()) { - case 'base64': fdata = s2a(Base64.decode(data.slice(di).join(""))); break; + case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break; case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break; default: throw new Error("Unsupported Content-Transfer-Encoding " + cte); } diff --git a/dist/cfb.min.js b/dist/cfb.min.js index bdc7632..4d3af6e 100644 --- a/dist/cfb.min.js +++ b/dist/cfb.min.js @@ -1,3 +1,3 @@ /* 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,l=0,h=0;for(var o=0;o>2;n=r.charCodeAt(o++);s=(i&3)<<4|n>>4;a=r.charCodeAt(o++);l=(n&15)<<2|a>>6;h=a&63;if(isNaN(n)){l=h=64}else if(isNaN(a)){h=64}t+=e.charAt(f)+e.charAt(s)+e.charAt(l)+e.charAt(h)}return t},decode:function r(t){var i="";var n=0,a=0,f=0,s=0,l=0,h=0,o=0;t=t.replace(/[^\w\+\/\=]/g,"");for(var u=0;u>4;i+=String.fromCharCode(n);h=e.indexOf(t.charAt(u++));a=(l&15)<<4|h>>2;if(h!==64){i+=String.fromCharCode(a)}o=e.indexOf(t.charAt(u++));f=(h&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;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(e,r){return r?new Buffer(e,r):new Buffer(e)}:Buffer.from.bind(Buffer);if(!Buffer.alloc)Buffer.alloc=function(e){var r=new Buffer(e);r.fill(0);return r};if(!Buffer.allocUnsafe)Buffer.allocUnsafe=function(e){return new Buffer(e)}}function new_raw_buf(e){return has_buf?Buffer.alloc(e):new Array(e)}function new_unsafe_buf(e){return has_buf?Buffer.allocUnsafe(e):new Array(e)}var s2a=function r(e){if(has_buf)return Buffer_from(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 Buffer_from(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 CRC32;(function(e){e(CRC32={})})(function(e){e.version="1.2.0";function r(){var e=0,r=new Array(256);for(var t=0;t!=256;++t){e=t;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;r[t]=e}return typeof Int32Array!=="undefined"?new Int32Array(r):r}var t=r();function i(e,r){var i=r^-1,n=e.length-1;for(var a=0;a>>8^t[(i^e.charCodeAt(a++))&255];i=i>>>8^t[(i^e.charCodeAt(a++))&255]}if(a===n)i=i>>>8^t[(i^e.charCodeAt(a))&255];return i^-1}function n(e,r){if(e.length>1e4)return a(e,r);var i=r^-1,n=e.length-3;for(var f=0;f>>8^t[(i^e[f++])&255];i=i>>>8^t[(i^e[f++])&255];i=i>>>8^t[(i^e[f++])&255];i=i>>>8^t[(i^e[f++])&255]}while(f>>8^t[(i^e[f++])&255];return i^-1}function a(e,r){var i=r^-1,n=e.length-7;for(var a=0;a>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255];i=i>>>8^t[(i^e[a++])&255]}while(a>>8^t[(i^e[a++])&255];return i^-1}function f(e,r){var i=r^-1;for(var n=0,a=e.length,f,s;n>>8^t[(i^f)&255]}else if(f<2048){i=i>>>8^t[(i^(192|f>>6&31))&255];i=i>>>8^t[(i^(128|f&63))&255]}else if(f>=55296&&f<57344){f=(f&1023)+64;s=e.charCodeAt(n++)&1023;i=i>>>8^t[(i^(240|f>>8&7))&255];i=i>>>8^t[(i^(128|f>>2&63))&255];i=i>>>8^t[(i^(128|s>>6&15|(f&3)<<4))&255];i=i>>>8^t[(i^(128|s&63))&255]}else{i=i>>>8^t[(i^(224|f>>12&15))&255];i=i>>>8^t[(i^(128|f>>6&63))&255];i=i>>>8^t[(i^(128|f&63))&255]}}return i^-1}e.table=t;e.bstr=i;e.buf=n;e.str=f});var CFB=function t(){var e={};e.version="1.2.1";function r(e,r){var t=e.split("/"),i=r.split("/");for(var n=0,a=0,f=Math.min(t.length,i.length);n>>1;e.write_shift(2,t);var i=r.getFullYear()-1980;i=i<<4|r.getMonth()+1;i=i<<5|r.getDate();e.write_shift(2,i)}function a(e){var r=e.read_shift(2)&65535;var t=e.read_shift(2)&65535;var i=new Date;var n=t&31;t>>>=5;var a=t&15;t>>>=4;i.setMilliseconds(0);i.setFullYear(t+1980);i.setMonth(a-1);i.setDate(n);var f=r&31;r>>>=5;var s=r&63;r>>>=6;i.setHours(r);i.setMinutes(s);i.setSeconds(f<<1);return i}function f(e){prep_blob(e,0);var r={};var t=0;while(e.l<=e.length-4){var i=e.read_shift(2);var n=e.read_shift(2),a=e.l+n;var f={};switch(i){case 21589:{t=e.read_shift(1);if(t&1)f.mtime=e.read_shift(4);if(n>5){if(t&2)f.atime=e.read_shift(4);if(t&4)f.ctime=e.read_shift(4)}if(f.mtime)f.mt=new Date(f.mtime*1e3)}break;}e.l=a;r[i]=f}return r}var s;function l(){return s||(s=require("fs"))}function h(e,r){if(e[0]==80&&e[1]==75)return ye(e,r);if((e[0]|32)==109&&(e[1]|32)==105)return ke(e,r);if(e.length<512)throw new Error("CFB file size "+e.length+" < 512");var t=3;var i=512;var n=0;var a=0;var f=0;var s=0;var l=0;var h=[];var _=e.slice(0,512);prep_blob(_,0);var w=o(_);t=w[0];switch(t){case 3:i=512;break;case 4:i=4096;break;case 0:if(w[1]==0)return ye(e,r);default:throw new Error("Major Version: Expected 3 or 4 saw "+t);}if(i!==512){_=e.slice(0,i);prep_blob(_,28)}var b=e.slice(0,i);u(_,t);var F=_.read_shift(4,"i");if(t===3&&F!==0)throw new Error("# Directory Sectors: Expected 0 saw "+F);_.l+=4;f=_.read_shift(4,"i");_.l+=4;_.chk("00100000","Mini Stream Cutoff Size: ");s=_.read_shift(4,"i");n=_.read_shift(4,"i");l=_.read_shift(4,"i");a=_.read_shift(4,"i");for(var y=-1,m=0;m<109;++m){y=_.read_shift(4,"i");if(y<0)break;h[m]=y}var x=c(e,i);d(l,a,x,i,h);var C=p(x,f,h,i);C[f].name="!Directory";if(n>0&&s!==B)C[s].name="!MiniFAT";C[h[0]].name="!FAT";C.fat_addrs=h;C.ssz=i;var I={},A=[],E=[],S=[];g(f,C,x,A,n,I,E,s);v(E,S,A);A.shift();var k={FileIndex:E,FullPaths:S};if(r&&r.raw)k.raw={header:b,sectors:x};return k}function o(e){if(e[e.l]==80&&e[e.l+1]==75)return[0,0];e.chk(S,"Header Signature: ");e.l+=16;var r=e.read_shift(2,"u");return[e.read_shift(2,"u"),r]}function u(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 c(e,r){var t=Math.ceil(e.length/r)-1;var i=[];for(var n=1;n0&&f>=0){a.push(r.slice(f*E,f*E+E));n-=E;f=__readInt32LE(t,f*4)}if(a.length===0)return new_buf(0);return bconcat(a).slice(0,e.size)}function d(e,r,t,i,n){var a=B;if(e===B){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 l=0;l=0;){n[l]=true;a[a.length]=l;f.push(e[l]);var o=t[Math.floor(l*4/i)];h=l*4&s;if(i<4+h)throw new Error("FAT boundary crossed: "+l+" 4 "+i);if(!e[o])break;l=__readInt32LE(e[o],h)}return{nodes:a,data:__toBuffer([f])}}function p(e,r,t,i){var n=e.length,a=[];var f=[],s=[],l=[];var h=i-1,o=0,u=0,c=0,v=0;for(o=0;o=n)c-=n;if(f[c])continue;l=[];var _=[];for(u=c;u>=0;){_[u]=true;f[u]=true;s[s.length]=u;l.push(e[u]);var d=t[Math.floor(u*4/i)];v=u*4&h;if(i<4+v)throw new Error("FAT boundary crossed: "+u+" 4 "+i);if(!e[d])break;u=__readInt32LE(e[d],v);if(_[u])break}a[c]={nodes:s,data:__toBuffer([l])}}return a}function g(e,r,t,i,n,a,f,s){var l=0,h=i.length?2:0;var o=r[e].data;var u=0,c=0,v;for(;u0&&l!==B)r[l].name="!StreamData"}else if(p.size>=4096){p.storage="fat";if(r[p.start]===undefined)r[p.start]=w(t,p.start,r.fat_addrs,r.ssz);r[p.start].name=p.name;p.content=r[p.start].data.slice(0,p.size)}else{p.storage="minifat";if(p.size<0)p.size=0;else if(l!==B&&p.start!==B&&r[l]){p.content=_(p,r[l].data,(r[s]||{}).data)}}if(p.content)prep_blob(p.content,0);a[v]=p;f.push(p)}}function b(e,r){return new Date((__readUInt32LE(e,r+4)/1e7*Math.pow(2,32)+__readUInt32LE(e,r)/1e7-11644473600)*1e3)}function F(e,r){l();return h(s.readFileSync(e),r)}function y(e,r){var t=r&&r.type;if(!t){if(has_buf&&Buffer.isBuffer(e))t="buffer"}switch(t||"base64"){case"file":return F(e,r);case"base64":return h(s2a(Base64.decode(e)),r);case"binary":return h(s2a(e),r);}return h(e,r)}function m(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;x(e)}function x(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);C(e)}function C(e,n){m(e);var a=false,f=false;for(var s=e.FullPaths.length-1;s>=0;--s){var l=e.FileIndex[s];switch(l.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(l.R*l.L*l.C))a=true;if(l.R>-1&&l.L>-1&&l.R==l.L)a=true;break;default:a=true;break;}}if(!a&&!n)return;var h=new Date(1987,1,19),o=0;var u=Object.create?Object.create(null):{};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 I(e,r){var t=r||{};if(t.fileType=="mad")return Re(e,t);C(e);switch(t.fileType){case"zip":return xe(e,t);}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 l=r+127>>7;var h=s+t+f+l;var o=h+127>>7;var u=o<=109?0:Math.ceil((o-109)/127);while(h+o+u+127>>7>o)u=++o<=109?0:Math.ceil((o-109)/127);var c=[1,u,o,l,f,t,r,0];e.FileIndex[0].size=r<<6;c[7]=(e.FileIndex[0].start=c[0]+c[1]+c[2]+c[3]+c[4]+c[5])+(c[6]+7>>3);return c}(e);var n=new_buf(i[7]<<9);var a=0,f=0;{for(a=0;a<8;++a)n.write_shift(1,k[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:B);n.write_shift(4,i[3]);n.write_shift(-4,i[1]?i[0]-1:B);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,U.ENDOFCHAIN);f=a=0;for(l=0;l=4096)continue;o.start=f;s(h+63>>6)}while(n.l&511)n.write_shift(-4,U.ENDOFCHAIN);for(a=0;a=4096){n.l=o.start+1<<9;if(has_buf&&Buffer.isBuffer(o.content)){o.content.copy(n,n.l,0,o.size);n.l+=o.size+511&-512}else{for(l=0;l0&&o.size<4096){if(has_buf&&Buffer.isBuffer(o.content)){o.content.copy(n,n.l,0,o.size);n.l+=o.size+63&-64}else{for(l=0;l>16|r>>8|r)&255}var J=typeof Uint8Array!=="undefined";var q=J?new Uint8Array(1<<8):[];for(var G=0;G<1<<8;++G)q[G]=$(G);function V(e,r){var t=q[e&255];if(r<=8)return t>>>8-r;t=t<<8|q[e>>8&255];if(r<=16)return t>>>16-r;t=t<<8|q[e>>16&255];return t>>>24-r}function X(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=6?0:e[i+1]<<8))>>>t&3}function W(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=5?0:e[i+1]<<8))>>>t&7}function Y(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=4?0:e[i+1]<<8))>>>t&15}function Z(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=3?0:e[i+1]<<8))>>>t&31}function K(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=1?0:e[i+1]<<8))>>>t&127}function Q(e,r,t){var i=r&7,n=r>>>3,a=(1<>>i;if(t<8-i)return f&a;f|=e[n+1]<<8-i;if(t<16-i)return f&a;f|=e[n+2]<<16-i;if(t<24-i)return f&a;f|=e[n+3]<<24-i;return f&a}function ee(e,r,t){var i=r&7,n=r>>>3;if(i<=5)e[n]|=(t&7)<>8-i}return r+3}function re(e,r,t){var i=r&7,n=r>>>3;t=(t&1)<>>3;t<<=i;e[n]|=t&255;t>>>=8;e[n+1]=t;return r+8}function ie(e,r,t){var i=r&7,n=r>>>3;t<<=i;e[n]|=t&255;t>>>=8;e[n+1]=t&255;e[n+2]=t>>>8;return r+16}function ne(e,r){var t=e.length,i=2*t>r?2*t:r+5,n=0;if(t>=r)return e;if(has_buf){var a=new_unsafe_buf(i);if(e.copy)e.copy(a);else for(;n>i-u;for(f=(1<=0;--f)r[s|f<0)r[r.l++]=e[t++]}return r.l}function a(r,t){var n=0;var a=0;var f=J?new Uint16Array(32768):[];while(a0)t[t.l++]=r[a++];n=t.l*8;continue}n=ee(t,n,+!!(a+s==r.length)+2);var l=0;while(s-- >0){var h=r[a];l=(l<<5^h)&32767;var o=-1,u=0;if(o=f[l]){o|=a&~32767;if(o>a)o-=32768;if(o2){h=i[u];if(h<=22)n=te(t,n,q[h+1]>>1)-1;else{te(t,n,3);n+=5;te(t,n,q[h-23]>>5);n+=3}var c=h<8?0:h-4>>2;if(c>0){ie(t,n,u-j[h]);n+=c}h=e[a-o];n=te(t,n,q[h]>>3);n-=3;var v=h<4?0:h-2>>1;if(v>0){ie(t,n,a-o-H[h]);n+=v}for(var _=0;_>8-_;for(var d=(1<<7-_)-1;d>=0;--d)_e[v|d<<_]=_&7|c<<3}}var w=[];l=1;for(;w.length>>=3){case 16:a=3+X(e,r);r+=2;v=w[w.length-1];while(a-- >0)w.push(v);break;case 17:a=3+W(e,r);r+=3;while(a-- >0)w.push(0);break;case 18:a=11+K(e,r);r+=7;while(a-- >0)w.push(0);break;default:w.push(v);if(l>>0;var s=0,l=0;while((i&1)==0){i=W(e,t);t+=3;if(i>>>1==0){if(t&7)t+=8-(t&7);var h=e[t>>>3]|e[(t>>>3)+1]<<8;t+=32;if(!r&&f>>3,(t>>>3)+h);a+=h;t+=8*h}else while(h-- >0){n[a++]=e[t>>>3];t+=8}continue}else if(i>>>1==1){s=9;l=5}else{t=pe(e,t);s=de;l=we}for(;;){if(!r&&f>>1==1?se[o]:ce[o];t+=u&15;u>>>=4;if((u>>>8&255)===0)n[a++]=u;else if(u==256)break;else{u-=257;var c=u<8?0:u-4>>2;if(c>5)c=0;var v=a+j[u];if(c>0){v+=Q(e,t,c);t+=c}o=Q(e,t,l);u=i>>>1==1?le[o]:ve[o];t+=u&15;u>>>=4;var _=u<4?0:u-2>>1;var d=H[u];if(_>0){d+=Q(e,t,_);t+=_}if(!r&&f>>3]}function be(e,r){var t=e.slice(e.l||0);var i=ge(t,r);e.l+=i[1];return i[0]}function Fe(e,r){if(e){if(typeof console!=="undefined")console.error(r)}else throw new Error(r)}function ye(e,r){var t=e;prep_blob(t,0);var i=[],n=[];var a={FileIndex:i,FullPaths:n};m(a,{root:r.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 l=t.read_shift(2);t.l+=6;var h=t.read_shift(4);t.l=h;for(s=0;s0){t=t.slice(0,t.length-1);t=t.slice(0,t.lastIndexOf("/")+1);if(a.slice(0,t.length)==t)break}}var f=(i[1]||"").match(/boundary="(.*?)"/);if(!f)throw new Error("MAD cannot find boundary");var s="--"+(f[1]||"");var l=[],h=[];var o={FileIndex:l,FullPaths:h};m(o);var u,c=0;for(n=0;n=32&&v<128)++u;var d=u>=c*4/5;n.push(i);n.push("Content-Location: "+(t.root||"file:///C:/SheetJS/")+f);n.push("Content-Transfer-Encoding: "+(d?"quoted-printable":"base64"));n.push("Content-Type: "+Ie(s,f));n.push("");n.push(d?Ee(o):Ae(o))}n.push(i+"--\r\n");return n.join("\r\n")}function Ue(e){var r={};m(r,e);return r}function ze(e,r,t,n){var a=n&&n.unsafe;if(!a)m(e);var f=!a&&CFB.find(e,r);if(!f){var s=e.FullPaths[0];if(r.slice(0,s.length)==s)s=r;else{if(s.slice(-1)!="/")s+="/";s=(s+r).replace("//","/")}f={name:i(r),type:2};e.FileIndex.push(f);e.FullPaths.push(s);if(!a)CFB.utils.cfb_gc(e)}f.content=t;f.size=t?t.length:0;if(n){if(n.CLSID)f.clsid=n.CLSID;if(n.mt)f.mt=n.mt;if(n.ct)f.ct=n.ct}return f}function Le(e,r){m(e);var t=CFB.find(e,r);if(t)for(var i=0;i>2;i=e.charCodeAt(h++);f=(t&3)<<4|i>>4;n=e.charCodeAt(h++);s=(i&15)<<2|n>>6;l=n&63;if(isNaN(i))s=l=64;else if(isNaN(n))l=64;r+=Base64_map.charAt(a)+Base64_map.charAt(f)+Base64_map.charAt(s)+Base64_map.charAt(l)}return r}function Base64_decode(e){var r="";var t=0,i=0,n=0,a=0,f=0,s=0,l=0;e=e.replace(/[^\w\+\/\=]/g,"");for(var h=0;h>4;r+=String.fromCharCode(t);s=Base64_map.indexOf(e.charAt(h++));i=(f&15)<<4|s>>2;if(s!==64)r+=String.fromCharCode(i);l=Base64_map.indexOf(e.charAt(h++));n=(s&3)<<6|l;if(l!==64)r+=String.fromCharCode(n)}return r}var has_buf=function(){return typeof Buffer!=="undefined"&&typeof process!=="undefined"&&typeof process.versions!=="undefined"&&!!process.versions.node}();var Buffer_from=function(){if(typeof Buffer!=="undefined"){var e=!Buffer.from;if(!e)try{Buffer.from("foo","utf8")}catch(r){e=true}return e?function(e,r){return r?new Buffer(e,r):new Buffer(e)}:Buffer.from.bind(Buffer)}return function(){}}();function new_raw_buf(e){if(has_buf){if(Buffer.alloc)return Buffer.alloc(e);var r=new Buffer(e);r.fill(0);return r}return typeof Uint8Array!="undefined"?new Uint8Array(e):new Array(e)}function new_unsafe_buf(e){if(has_buf)return Buffer.allocUnsafe?Buffer.allocUnsafe(e):new Buffer(e);return typeof Uint8Array!="undefined"?new Uint8Array(e):new Array(e)}var s2a=function e(r){if(has_buf)return Buffer_from(r,"binary");return r.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 Buffer_from(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 CRC32=function(){var e={};e.version="1.2.1";function r(){var e=0,r=new Array(256);for(var t=0;t!=256;++t){e=t;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;e=e&1?-306674912^e>>>1:e>>>1;r[t]=e}return typeof Int32Array!=="undefined"?new Int32Array(r):r}var t=r();function i(e){var r=0,t=0,i=0,n=typeof Int32Array!=="undefined"?new Int32Array(4096):new Array(4096);for(i=0;i!=256;++i)n[i]=e[i];for(i=0;i!=256;++i){t=e[i];for(r=256+i;r<4096;r+=256)t=n[r]=t>>>8^e[t&255]}var a=[];for(i=1;i!=16;++i)a[i-1]=typeof Int32Array!=="undefined"?n.subarray(i*256,i*256+256):n.slice(i*256,i*256+256);return a}var n=i(t);var a=n[0],f=n[1],s=n[2],l=n[3],h=n[4];var o=n[5],u=n[6],c=n[7],v=n[8],_=n[9];var d=n[10],w=n[11],p=n[12],g=n[13],b=n[14];function F(e,r){var i=r^-1;for(var n=0,a=e.length;n>>8^t[(i^e.charCodeAt(n++))&255];return~i}function y(e,r){var i=r^-1,n=e.length-15,F=0;for(;F>8&255]^p[e[F++]^i>>16&255]^w[e[F++]^i>>>24]^d[e[F++]]^_[e[F++]]^v[e[F++]]^c[e[F++]]^u[e[F++]]^o[e[F++]]^h[e[F++]]^l[e[F++]]^s[e[F++]]^f[e[F++]]^a[e[F++]]^t[e[F++]];n+=15;while(F>>8^t[(i^e[F++])&255];return~i}function m(e,r){var i=r^-1;for(var n=0,a=e.length,f=0,s=0;n>>8^t[(i^f)&255]}else if(f<2048){i=i>>>8^t[(i^(192|f>>6&31))&255];i=i>>>8^t[(i^(128|f&63))&255]}else if(f>=55296&&f<57344){f=(f&1023)+64;s=e.charCodeAt(n++)&1023;i=i>>>8^t[(i^(240|f>>8&7))&255];i=i>>>8^t[(i^(128|f>>2&63))&255];i=i>>>8^t[(i^(128|s>>6&15|(f&3)<<4))&255];i=i>>>8^t[(i^(128|s&63))&255]}else{i=i>>>8^t[(i^(224|f>>12&15))&255];i=i>>>8^t[(i^(128|f>>6&63))&255];i=i>>>8^t[(i^(128|f&63))&255]}}return~i}e.table=t;e.bstr=F;e.buf=y;e.str=m;return e}();var CFB=function r(){var e={};e.version="1.2.2";function r(e,r){var t=e.split("/"),i=r.split("/");for(var n=0,a=0,f=Math.min(t.length,i.length);n>>1;e.write_shift(2,t);var i=r.getFullYear()-1980;i=i<<4|r.getMonth()+1;i=i<<5|r.getDate();e.write_shift(2,i)}function a(e){var r=e.read_shift(2)&65535;var t=e.read_shift(2)&65535;var i=new Date;var n=t&31;t>>>=5;var a=t&15;t>>>=4;i.setMilliseconds(0);i.setFullYear(t+1980);i.setMonth(a-1);i.setDate(n);var f=r&31;r>>>=5;var s=r&63;r>>>=6;i.setHours(r);i.setMinutes(s);i.setSeconds(f<<1);return i}function f(e){prep_blob(e,0);var r={};var t=0;while(e.l<=e.length-4){var i=e.read_shift(2);var n=e.read_shift(2),a=e.l+n;var f={};switch(i){case 21589:{t=e.read_shift(1);if(t&1)f.mtime=e.read_shift(4);if(n>5){if(t&2)f.atime=e.read_shift(4);if(t&4)f.ctime=e.read_shift(4)}if(f.mtime)f.mt=new Date(f.mtime*1e3)}break;}e.l=a;r[i]=f}return r}var s;function l(){return s||(s=require("fs"))}function h(e,r){if(e[0]==80&&e[1]==75)return ye(e,r);if((e[0]|32)==109&&(e[1]|32)==105)return ke(e,r);if(e.length<512)throw new Error("CFB file size "+e.length+" < 512");var t=3;var i=512;var n=0;var a=0;var f=0;var s=0;var l=0;var h=[];var _=e.slice(0,512);prep_blob(_,0);var w=o(_);t=w[0];switch(t){case 3:i=512;break;case 4:i=4096;break;case 0:if(w[1]==0)return ye(e,r);default:throw new Error("Major Version: Expected 3 or 4 saw "+t);}if(i!==512){_=e.slice(0,i);prep_blob(_,28)}var b=e.slice(0,i);u(_,t);var F=_.read_shift(4,"i");if(t===3&&F!==0)throw new Error("# Directory Sectors: Expected 0 saw "+F);_.l+=4;f=_.read_shift(4,"i");_.l+=4;_.chk("00100000","Mini Stream Cutoff Size: ");s=_.read_shift(4,"i");n=_.read_shift(4,"i");l=_.read_shift(4,"i");a=_.read_shift(4,"i");for(var y=-1,m=0;m<109;++m){y=_.read_shift(4,"i");if(y<0)break;h[m]=y}var x=c(e,i);d(l,a,x,i,h);var I=p(x,f,h,i);I[f].name="!Directory";if(n>0&&s!==E)I[s].name="!MiniFAT";I[h[0]].name="!FAT";I.fat_addrs=h;I.ssz=i;var C={},A=[],B=[],S=[];g(f,I,x,A,n,C,B,s);v(B,S,A);A.shift();var k={FileIndex:B,FullPaths:S};if(r&&r.raw)k.raw={header:b,sectors:x};return k}function o(e){if(e[e.l]==80&&e[e.l+1]==75)return[0,0];e.chk(S,"Header Signature: ");e.l+=16;var r=e.read_shift(2,"u");return[e.read_shift(2,"u"),r]}function u(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 c(e,r){var t=Math.ceil(e.length/r)-1;var i=[];for(var n=1;n0&&f>=0){a.push(r.slice(f*B,f*B+B));n-=B;f=__readInt32LE(t,f*4)}if(a.length===0)return new_buf(0);return bconcat(a).slice(0,e.size)}function d(e,r,t,i,n){var a=E;if(e===E){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 l=0;l=1)d(__readInt32LE(f,i-4),r-1,t,i,n)}}function w(e,r,t,i,n){var a=[],f=[];if(!n)n=[];var s=i-1,l=0,h=0;for(l=r;l>=0;){n[l]=true;a[a.length]=l;f.push(e[l]);var o=t[Math.floor(l*4/i)];h=l*4&s;if(i<4+h)throw new Error("FAT boundary crossed: "+l+" 4 "+i);if(!e[o])break;l=__readInt32LE(e[o],h)}return{nodes:a,data:__toBuffer([f])}}function p(e,r,t,i){var n=e.length,a=[];var f=[],s=[],l=[];var h=i-1,o=0,u=0,c=0,v=0;for(o=0;o=n)c-=n;if(f[c])continue;l=[];var _=[];for(u=c;u>=0;){_[u]=true;f[u]=true;s[s.length]=u;l.push(e[u]);var d=t[Math.floor(u*4/i)];v=u*4&h;if(i<4+v)throw new Error("FAT boundary crossed: "+u+" 4 "+i);if(!e[d])break;u=__readInt32LE(e[d],v);if(_[u])break}a[c]={nodes:s,data:__toBuffer([l])}}return a}function g(e,r,t,i,n,a,f,s){var l=0,h=i.length?2:0;var o=r[e].data;var u=0,c=0,v;for(;u0&&l!==E)r[l].name="!StreamData"}else if(p.size>=4096){p.storage="fat";if(r[p.start]===undefined)r[p.start]=w(t,p.start,r.fat_addrs,r.ssz);r[p.start].name=p.name;p.content=r[p.start].data.slice(0,p.size)}else{p.storage="minifat";if(p.size<0)p.size=0;else if(l!==E&&p.start!==E&&r[l]){p.content=_(p,r[l].data,(r[s]||{}).data)}}if(p.content)prep_blob(p.content,0);a[v]=p;f.push(p)}}function b(e,r){return new Date((__readUInt32LE(e,r+4)/1e7*Math.pow(2,32)+__readUInt32LE(e,r)/1e7-11644473600)*1e3)}function F(e,r){l();return h(s.readFileSync(e),r)}function y(e,r){var t=r&&r.type;if(!t){if(has_buf&&Buffer.isBuffer(e))t="buffer"}switch(t||"base64"){case"file":return F(e,r);case"base64":return h(s2a(Base64_decode(e)),r);case"binary":return h(s2a(e),r);}return h(e,r)}function m(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;x(e)}function x(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);I(e)}function I(e,n){m(e);var a=false,f=false;for(var s=e.FullPaths.length-1;s>=0;--s){var l=e.FileIndex[s];switch(l.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(l.R*l.L*l.C))a=true;if(l.R>-1&&l.L>-1&&l.R==l.L)a=true;break;default:a=true;break;}}if(!a&&!n)return;var h=new Date(1987,1,19),o=0;var u=Object.create?Object.create(null):{};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 C(e,r){var t=r||{};if(t.fileType=="mad")return Ue(e,t);I(e);switch(t.fileType){case"zip":return xe(e,t);}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 l=r+127>>7;var h=s+t+f+l;var o=h+127>>7;var u=o<=109?0:Math.ceil((o-109)/127);while(h+o+u+127>>7>o)u=++o<=109?0:Math.ceil((o-109)/127);var c=[1,u,o,l,f,t,r,0];e.FileIndex[0].size=r<<6;c[7]=(e.FileIndex[0].start=c[0]+c[1]+c[2]+c[3]+c[4]+c[5])+(c[6]+7>>3);return c}(e);var n=new_buf(i[7]<<9);var a=0,f=0;{for(a=0;a<8;++a)n.write_shift(1,k[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:E);n.write_shift(4,i[3]);n.write_shift(-4,i[1]?i[0]-1:E);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,R.ENDOFCHAIN);f=a=0;for(l=0;l=4096)continue;o.start=f;s(h+63>>6)}while(n.l&511)n.write_shift(-4,R.ENDOFCHAIN);for(a=0;a32){console.error("Name "+c+" will be truncated to "+c.slice(0,32));c=c.slice(0,32)}h=2*(c.length+1);n.write_shift(64,c,"utf16le");n.write_shift(2,h);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(l=0;l<4;++l)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(a=1;a=4096){n.l=o.start+1<<9;if(has_buf&&Buffer.isBuffer(o.content)){o.content.copy(n,n.l,0,o.size);n.l+=o.size+511&-512}else{for(l=0;l0&&o.size<4096){if(has_buf&&Buffer.isBuffer(o.content)){o.content.copy(n,n.l,0,o.size);n.l+=o.size+63&-64}else{for(l=0;l>16|r>>8|r)&255}var J=typeof Uint8Array!=="undefined";var q=J?new Uint8Array(1<<8):[];for(var G=0;G<1<<8;++G)q[G]=$(G);function V(e,r){var t=q[e&255];if(r<=8)return t>>>8-r;t=t<<8|q[e>>8&255];if(r<=16)return t>>>16-r;t=t<<8|q[e>>16&255];return t>>>24-r}function X(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=6?0:e[i+1]<<8))>>>t&3}function W(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=5?0:e[i+1]<<8))>>>t&7}function Y(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=4?0:e[i+1]<<8))>>>t&15}function Z(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=3?0:e[i+1]<<8))>>>t&31}function K(e,r){var t=r&7,i=r>>>3;return(e[i]|(t<=1?0:e[i+1]<<8))>>>t&127}function Q(e,r,t){var i=r&7,n=r>>>3,a=(1<>>i;if(t<8-i)return f&a;f|=e[n+1]<<8-i;if(t<16-i)return f&a;f|=e[n+2]<<16-i;if(t<24-i)return f&a;f|=e[n+3]<<24-i;return f&a}function ee(e,r,t){var i=r&7,n=r>>>3;if(i<=5)e[n]|=(t&7)<>8-i}return r+3}function re(e,r,t){var i=r&7,n=r>>>3;t=(t&1)<>>3;t<<=i;e[n]|=t&255;t>>>=8;e[n+1]=t;return r+8}function ie(e,r,t){var i=r&7,n=r>>>3;t<<=i;e[n]|=t&255;t>>>=8;e[n+1]=t&255;e[n+2]=t>>>8;return r+16}function ne(e,r){var t=e.length,i=2*t>r?2*t:r+5,n=0;if(t>=r)return e;if(has_buf){var a=new_unsafe_buf(i);if(e.copy)e.copy(a);else for(;n>i-u;for(f=(1<=0;--f)r[s|f<0)r[r.l++]=e[t++]}return r.l}function a(r,t){var n=0;var a=0;var f=J?new Uint16Array(32768):[];while(a0)t[t.l++]=r[a++];n=t.l*8;continue}n=ee(t,n,+!!(a+s==r.length)+2);var l=0;while(s-- >0){var h=r[a];l=(l<<5^h)&32767;var o=-1,u=0;if(o=f[l]){o|=a&~32767;if(o>a)o-=32768;if(o2){h=i[u];if(h<=22)n=te(t,n,q[h+1]>>1)-1;else{te(t,n,3);n+=5;te(t,n,q[h-23]>>5);n+=3}var c=h<8?0:h-4>>2;if(c>0){ie(t,n,u-j[h]);n+=c}h=e[a-o];n=te(t,n,q[h]>>3);n-=3;var v=h<4?0:h-2>>1;if(v>0){ie(t,n,a-o-H[h]);n+=v}for(var _=0;_>8-_;for(var d=(1<<7-_)-1;d>=0;--d)_e[v|d<<_]=_&7|c<<3}}var w=[];l=1;for(;w.length>>=3){case 16:a=3+X(e,r);r+=2;v=w[w.length-1];while(a-- >0)w.push(v);break;case 17:a=3+W(e,r);r+=3;while(a-- >0)w.push(0);break;case 18:a=11+K(e,r);r+=7;while(a-- >0)w.push(0);break;default:w.push(v);if(l>>0;var s=0,l=0;while((i&1)==0){i=W(e,t);t+=3;if(i>>>1==0){if(t&7)t+=8-(t&7);var h=e[t>>>3]|e[(t>>>3)+1]<<8;t+=32;if(h>0){if(!r&&f0){n[a++]=e[t>>>3];t+=8}}continue}else if(i>>1==1){s=9;l=5}else{t=pe(e,t);s=de;l=we}for(;;){if(!r&&f>>1==1?se[o]:ce[o];t+=u&15;u>>>=4;if((u>>>8&255)===0)n[a++]=u;else if(u==256)break;else{u-=257;var c=u<8?0:u-4>>2;if(c>5)c=0;var v=a+j[u];if(c>0){v+=Q(e,t,c);t+=c}o=Q(e,t,l);u=i>>>1==1?le[o]:ve[o];t+=u&15;u>>>=4;var _=u<4?0:u-2>>1;var d=H[u];if(_>0){d+=Q(e,t,_);t+=_}if(!r&&f>>3];return[n.slice(0,a),t+7>>>3]}function be(e,r){var t=e.slice(e.l||0);var i=ge(t,r);e.l+=i[1];return i[0]}function Fe(e,r){if(e){if(typeof console!=="undefined")console.error(r)}else throw new Error(r)}function ye(e,r){var t=e;prep_blob(t,0);var i=[],n=[];var a={FileIndex:i,FullPaths:n};m(a,{root:r.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 l=t.read_shift(2);t.l+=6;var h=t.read_shift(4);t.l=h;for(s=0;s0){t=t.slice(0,t.length-1);t=t.slice(0,t.lastIndexOf("/")+1);if(a.slice(0,t.length)==t)break}}var f=(i[1]||"").match(/boundary="(.*?)"/);if(!f)throw new Error("MAD cannot find boundary");var s="--"+(f[1]||"");var l=[],h=[];var o={FileIndex:l,FullPaths:h};m(o);var u,c=0;for(n=0;n=32&&v<128)++u;var d=u>=c*4/5;n.push(i);n.push("Content-Location: "+(t.root||"file:///C:/SheetJS/")+f);n.push("Content-Transfer-Encoding: "+(d?"quoted-printable":"base64"));n.push("Content-Type: "+Ce(s,f));n.push("");n.push(d?Be(o):Ae(o))}n.push(i+"--\r\n");return n.join("\r\n")}function Re(e){var r={};m(r,e);return r}function ze(e,r,t,n){var a=n&&n.unsafe;if(!a)m(e);var f=!a&&CFB.find(e,r);if(!f){var s=e.FullPaths[0];if(r.slice(0,s.length)==s)s=r;else{if(s.slice(-1)!="/")s+="/";s=(s+r).replace("//","/")}f={name:i(r),type:2};e.FileIndex.push(f);e.FullPaths.push(s);if(!a)CFB.utils.cfb_gc(e)}f.content=t;f.size=t?t.length:0;if(n){if(n.CLSID)f.clsid=n.CLSID;if(n.mt)f.mt=n.mt;if(n.ct)f.ct=n.ct}return f}function Le(e,r){m(e);var t=CFB.find(e,r);if(t)for(var i=0;i; @@ -39,21 +36,18 @@ type SectorList = { } type CFBFiles = {[n:string]:CFBEntry}; */ -/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ +/*! crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2: */ /*exported CRC32 */ -var CRC32; -(function (factory) { - /*jshint ignore:start */ - /*eslint-disable */ - factory(CRC32 = {}); - /*eslint-enable */ - /*jshint ignore:end */ -}(function(CRC32) { -CRC32.version = '1.2.0'; -/* see perf/crc32table.js */ +var CRC32 = /*#__PURE__*/(function() { +var CRC32 = {}; +CRC32.version = '1.2.1'; +/*:: +type ABuf = Array | Buffer | Uint8Array; +type CRC32TableType = Array | Int32Array; +*/ /*global Int32Array */ -function signed_crc_table()/*:any*/ { +function signed_crc_table()/*:CRC32TableType*/ { var c = 0, table/*:Array*/ = new Array(256); for(var n =0; n != 256; ++n){ @@ -72,78 +66,77 @@ function signed_crc_table()/*:any*/ { return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table; } -var T = signed_crc_table(); -function crc32_bstr(bstr/*:string*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1, L = bstr.length - 1; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; - C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF]; +var T0 = signed_crc_table(); +function slice_by_16_tables(T) { + var c = 0, v = 0, n = 0, table/*:Array*/ = typeof Int32Array !== 'undefined' ? new Int32Array(4096) : new Array(4096) ; + + for(n = 0; n != 256; ++n) table[n] = T[n]; + for(n = 0; n != 256; ++n) { + v = T[n]; + for(c = 256 + n; c < 4096; c += 256) v = table[c] = (v >>> 8) ^ T[v & 0xFF]; } - if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF]; - return C ^ -1; + var out = []; + for(n = 1; n != 16; ++n) out[n - 1] = typeof Int32Array !== 'undefined' ? table.subarray(n * 256, n * 256 + 256) : table.slice(n * 256, n * 256 + 256); + return out; +} +var TT = slice_by_16_tables(T0); +var T1 = TT[0], T2 = TT[1], T3 = TT[2], T4 = TT[3], T5 = TT[4]; +var T6 = TT[5], T7 = TT[6], T8 = TT[7], T9 = TT[8], Ta = TT[9]; +var Tb = TT[10], Tc = TT[11], Td = TT[12], Te = TT[13], Tf = TT[14]; +function crc32_bstr(bstr/*:string*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1; + for(var i = 0, L = bstr.length; i < L;) C = (C>>>8) ^ T0[(C^bstr.charCodeAt(i++))&0xFF]; + return ~C; } -function crc32_buf(buf/*:Uint8Array|Array*/, seed/*:number*/)/*:number*/ { - if(buf.length > 10000) return crc32_buf_8(buf, seed); - var C = seed ^ -1, L = buf.length - 3; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; +function crc32_buf(B/*:ABuf*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1, L = B.length - 15, i = 0; + for(; i < L;) C = + Tf[B[i++] ^ (C & 255)] ^ + Te[B[i++] ^ ((C >> 8) & 255)] ^ + Td[B[i++] ^ ((C >> 16) & 255)] ^ + Tc[B[i++] ^ (C >>> 24)] ^ + Tb[B[i++]] ^ Ta[B[i++]] ^ T9[B[i++]] ^ T8[B[i++]] ^ + T7[B[i++]] ^ T6[B[i++]] ^ T5[B[i++]] ^ T4[B[i++]] ^ + T3[B[i++]] ^ T2[B[i++]] ^ T1[B[i++]] ^ T0[B[i++]]; + L += 15; + while(i < L) C = (C>>>8) ^ T0[(C^B[i++])&0xFF]; + return ~C; } -function crc32_buf_8(buf/*:Uint8Array|Array*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1, L = buf.length - 7; - for(var i = 0; i < L;) { - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - } - while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF]; - return C ^ -1; -} - -function crc32_str(str/*:string*/, seed/*:number*/)/*:number*/ { - var C = seed ^ -1; - for(var i = 0, L=str.length, c, d; i < L;) { +function crc32_str(str/*:string*/, seed/*:?number*/)/*:number*/ { + var C = seed/*:: ? 0 : 0 */ ^ -1; + for(var i = 0, L = str.length, c = 0, d = 0; i < L;) { c = str.charCodeAt(i++); if(c < 0x80) { - C = (C>>>8) ^ T[(C ^ c)&0xFF]; + C = (C>>>8) ^ T0[(C^c)&0xFF]; } else if(c < 0x800) { - C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (192|((c>>6)&31)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } else if(c >= 0xD800 && c < 0xE000) { c = (c&1023)+64; d = str.charCodeAt(i++)&1023; - C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (240|((c>>8)&7)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>2)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(d&63)))&0xFF]; } else { - C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF]; - C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (224|((c>>12)&15)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|((c>>6)&63)))&0xFF]; + C = (C>>>8) ^ T0[(C ^ (128|(c&63)))&0xFF]; } } - return C ^ -1; + return ~C; } -CRC32.table = T; +CRC32.table = T0; CRC32.bstr = crc32_bstr; CRC32.buf = crc32_buf; CRC32.str = crc32_str; -})); +return CRC32; +})(); /* [MS-CFB] v20171201 */ -var CFB = (function _CFB(){ +var CFB = /*#__PURE__*/(function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.2.1'; +exports.version = '1.2.2'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -443,7 +436,7 @@ function sleuth_fat(idx/*:number*/, cnt/*:number*/, sectors/*:Array*/, if((q = __readInt32LE(sector,i*4)) === ENDOFCHAIN) break; fat_addrs.push(q); } - sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); + if(cnt >= 1) sleuth_fat(__readInt32LE(sector,ssz-4),cnt - 1, sectors, ssz, fat_addrs); } } @@ -561,7 +554,7 @@ function read(blob/*:RawBytes|string*/, options/*:CFBReadOpts*/) { } switch(type || "base64") { case "file": /*:: if(typeof blob !== 'string') throw "Must pass a filename when type='file'"; */return read_file(blob, options); - case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64.decode(blob)), options); + case "base64": /*:: if(typeof blob !== 'string') throw "Must pass a base64-encoded binary string when type='file'"; */return parse(s2a(Base64_decode(blob)), options); case "binary": /*:: if(typeof blob !== 'string') throw "Must pass a binary string when type='file'"; */return parse(s2a(blob), options); } return parse(/*::typeof blob == 'string' ? new Buffer(blob, 'utf-8') : */blob, options); @@ -619,7 +612,9 @@ function rebuild_cfb(cfb/*:CFBContainer*/, f/*:?boolean*/)/*:void*/ { for(i = 0; i < data.length; ++i) { var dad = dirname(data[i][0]); s = fullPaths[dad]; - if(!s) { + while(!s) { + while(dirname(dad) && !fullPaths[dirname(dad)]) dad = dirname(dad); + data.push([dad, ({ name: filename(dad).replace("/",""), type: 1, @@ -627,8 +622,12 @@ function rebuild_cfb(cfb/*:CFBContainer*/, f/*:?boolean*/)/*:void*/ { ct: now, mt: now, content: null }/*:any*/)]); + // Add name to set fullPaths[dad] = true; + + dad = dirname(data[i][0]); + s = fullPaths[dad]; } } @@ -676,7 +675,6 @@ function _write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|strin for(var i = 0; i < cfb.FileIndex.length; ++i) { var file = cfb.FileIndex[i]; if(!file.content) continue; - /*:: if(file.content == null) throw new Error("unreachable"); */ var flen = file.content.length; if(flen > 0){ if(flen < 0x1000) mini_size += (flen + 0x3F) >> 6; @@ -767,6 +765,10 @@ function _write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|strin file = cfb.FileIndex[i]; if(i === 0) file.start = file.size ? file.start - 1 : ENDOFCHAIN; var _nm/*:string*/ = (i === 0 && _opts.root) || file.name; + if(_nm.length > 32) { + console.error("Name " + _nm + " will be truncated to " + _nm.slice(0,32)); + _nm = _nm.slice(0, 32); + } flen = 2*(_nm.length+1); o.write_shift(64, _nm, "utf16le"); o.write_shift(2, flen); @@ -884,7 +886,7 @@ function write(cfb/*:CFBContainer*/, options/*:CFBWriteOpts*/)/*:RawBytes|string switch(options && options.type || "buffer") { case "file": get_fs(); fs.writeFileSync(options.filename, (o/*:any*/)); return o; case "binary": return typeof o == "string" ? o : a2s(o); - case "base64": return Base64.encode(typeof o == "string" ? o : a2s(o)); + case "base64": return Base64_encode(typeof o == "string" ? o : a2s(o)); case "buffer": if(has_buf) return Buffer.isBuffer(o) ? o : Buffer_from(o); /* falls through */ case "array": return typeof o == "string" ? s2a(o) : o; @@ -1074,15 +1076,16 @@ if(!use_typed_arrays) { for(; i<=279; i++) clens.push(7); for(; i<=287; i++) clens.push(8); build_tree(clens, fix_lmap, 288); -})();var _deflateRaw = (function() { +})();var _deflateRaw = /*#__PURE__*/(function _deflateRawIIFE() { var DST_LN_RE = use_typed_arrays ? new Uint8Array(0x8000) : []; - for(var j = 0, k = 0; j < DST_LN.length; ++j) { + var j = 0, k = 0; + for(; j < DST_LN.length - 1; ++j) { for(; k < DST_LN[j+1]; ++k) DST_LN_RE[k] = j; } for(;k < 32768; ++k) DST_LN_RE[k] = 29; - var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x102) : []; - for(j = 0, k = 0; j < LEN_LN.length; ++j) { + var LEN_LN_RE = use_typed_arrays ? new Uint8Array(0x103) : []; + for(j = 0, k = 0; j < LEN_LN.length - 1; ++j) { for(; k < LEN_LN[j+1]; ++k) LEN_LN_RE[k] = j; } @@ -1295,14 +1298,12 @@ function inflate(data, usz/*:number*/) { var sz = data[boff>>>3] | data[(boff>>>3)+1]<<8; boff += 32; /* push sz bytes */ - if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } - if(typeof data.copy === 'function') { - // $FlowIgnore - data.copy(outbuf, woff, boff>>>3, (boff>>>3)+sz); - woff += sz; boff += 8*sz; - } else while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + if(sz > 0) { + if(!usz && OL < woff + sz) { outbuf = realloc(outbuf, woff + sz); OL = outbuf.length; } + while(sz-- > 0) { outbuf[woff++] = data[boff>>>3]; boff += 8; } + } continue; - } else if((header >>> 1) == 1) { + } else if((header >> 1) == 1) { /* Fixed Huffman */ max_len_1 = 9; max_len_2 = 5; } else { @@ -1347,7 +1348,8 @@ function inflate(data, usz/*:number*/) { } } } - return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3]; + if(usz) return [outbuf, (boff+7)>>>3]; + return [outbuf.slice(0, woff), (boff+7)>>>3]; } function _inflate(payload, usz) { @@ -1594,7 +1596,7 @@ function get_content_type(fi/*:CFBEntry*/, fp/*:string*/)/*:string*/ { /* 76 character chunks TODO: intertwine encoding */ function write_base64_76(bstr/*:string*/)/*:string*/ { - var data = Base64.encode(bstr); + var data = Base64_encode(bstr); var o = []; for(var i = 0; i < data.length; i+= 76) o.push(data.slice(i, i+76)); return o.join("\r\n") + "\r\n"; @@ -1655,7 +1657,7 @@ function parse_quoted_printable(data/*:Array*/)/*:RawBytes*/ { } /* decode */ - for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/=[0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); + for(var oi = 0; oi < o.length; ++oi) o[oi] = o[oi].replace(/[=][0-9A-Fa-f]{2}/g, function($$) { return String.fromCharCode(parseInt($$.slice(1), 16)); }); return s2a(o.join("\r\n")); } @@ -1675,7 +1677,7 @@ function parse_mime(cfb/*:CFBContainer*/, data/*:Array*/, root/*:string* } ++di; switch(cte.toLowerCase()) { - case 'base64': fdata = s2a(Base64.decode(data.slice(di).join(""))); break; + case 'base64': fdata = s2a(Base64_decode(data.slice(di).join(""))); break; case 'quoted-printable': fdata = parse_quoted_printable(data.slice(di)); break; default: throw new Error("Unsupported Content-Transfer-Encoding " + cte); } @@ -1852,4 +1854,3 @@ exports.utils = { return exports; })(); -if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; } diff --git a/index.html b/index.html index 5ff18fc..8c7a61d 100644 --- a/index.html +++ b/index.html @@ -3,6 +3,7 @@ + JS-CFB Live Demo