From 7c1980741a3a07aa218721cee0f5355ffb143303 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Fri, 13 Mar 2020 01:58:41 -0400 Subject: [PATCH] version bump 1.1.4: detect sector cycle (fixes #8) --- README.md | 6 +- bits/31_version.js | 2 +- bits/45_readfat.js | 3 + cfb.flow.js | 5 +- cfb.js | 5 +- dist/LICENSE | 190 +++++++++++++++++++++++++++++++++++++++++++- dist/cfb.js | 5 +- dist/cfb.min.js | 2 +- dist/cfb.min.map | 2 +- dist/xlscfb.js | 5 +- index.html | 2 +- package.json | 2 +- test.js | 4 +- types/tsconfig.json | 1 + xlscfb.flow.js | 5 +- xlscfb.js | 5 +- 16 files changed, 227 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 948f176..8255369 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ accept a `name` argument strictly deal with absolute file names: Set the option `{unsafe:true}` to skip existence checks (for bulk additions) - `.cfb_del(cfb, name)` deletes the specified file - `.cfb_mov(cfb, old_name, new_name)` moves the old file to new path and name -- `.use_zlib(require("zlib"))` loads a nodejs zlib instance. +- `.use_zlib(require("zlib"))` loads a nodejs `zlib` instance. By default, the library uses a pure JS inflate/deflate implementation. NodeJS `zlib.InflateRaw` exposes the number of bytes read in versions after `8.11.0`. @@ -169,7 +169,7 @@ granted by the Apache 2.0 License are reserved by the Original Author. ## References - - [MS-CFB]: Compound File Binary File Format - - ZIP `APPNOTE.TXT`: https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.3.4.TXT + - `MS-CFB`: Compound File Binary File Format + - ZIP `APPNOTE.TXT`: .ZIP File Format Specification - RFC1951: https://www.ietf.org/rfc/rfc1951.txt diff --git a/bits/31_version.js b/bits/31_version.js index 6b86b01..96a8b06 100644 --- a/bits/31_version.js +++ b/bits/31_version.js @@ -1 +1 @@ -exports.version = '1.1.3'; +exports.version = '1.1.4'; diff --git a/bits/45_readfat.js b/bits/45_readfat.js index 3be6784..b1b4f5d 100644 --- a/bits/45_readfat.js +++ b/bits/45_readfat.js @@ -43,7 +43,9 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -52,6 +54,7 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/); } diff --git a/cfb.flow.js b/cfb.flow.js index 7af0430..5eaa3e9 100644 --- a/cfb.flow.js +++ b/cfb.flow.js @@ -295,7 +295,7 @@ CRC32.str = crc32_str; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.1.3'; +exports.version = '1.1.4'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -626,7 +626,9 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -635,6 +637,7 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/); } diff --git a/cfb.js b/cfb.js index d185c9e..5999dbf 100644 --- a/cfb.js +++ b/cfb.js @@ -277,7 +277,7 @@ CRC32.str = crc32_str; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.1.3'; +exports.version = '1.1.4'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -608,7 +608,9 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -617,6 +619,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}); } diff --git a/dist/LICENSE b/dist/LICENSE index a5fc80a..ac1d91a 100644 --- a/dist/LICENSE +++ b/dist/LICENSE @@ -1,4 +1,192 @@ -Copyright (C) 2013-present SheetJS + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (C) 2013-present SheetJS LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/dist/cfb.js b/dist/cfb.js index d185c9e..5999dbf 100644 --- a/dist/cfb.js +++ b/dist/cfb.js @@ -277,7 +277,7 @@ CRC32.str = crc32_str; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.1.3'; +exports.version = '1.1.4'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -608,7 +608,9 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -617,6 +619,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}); } diff --git a/dist/cfb.min.js b/dist/cfb.min.js index bdb320a..e24123d 100644 --- a/dist/cfb.min.js +++ b/dist/cfb.min.js @@ -1,2 +1,2 @@ /* cfb.js (C) 2013-present SheetJS -- http://sheetjs.com */ -var Base64=function 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>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>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;t0&&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>8}while(this.l>>=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>>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>>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>>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>>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>>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>>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.3";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>>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.l+=16;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;n0&&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=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)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(;u0&&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;s1?1:-1;_.size=0;_.type=5}else if(v.slice(-1)=="/"){for(o=s+1;o=u.length?-1:o;for(o=s+1;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;i0){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>9)}s(i[6]+7>>3);while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);a=f=0;for(h=0;h=4096)continue;o.start=a;s(l+63>>6)}while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);for(f=0;f=4096){n.l=o.start+1<<9;for(h=0;h0&&o.size<4096){for(h=0;h>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<>>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(;n0)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>i-u;for(a=(1<=0;--a)e[s|a<>8-v;for(var d=(1<<7-v)-1;d>=0;--d)lr[_|d<>>=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>>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>>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>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>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;t0&&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>8}while(this.l>>=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>>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>>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>>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>>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>>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>>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.4";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>>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.l+=16;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;n0&&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=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)c-=n;if(a[c])continue;h=[];var v=[];for(u=c;u>=0;){v[u]=true;a[u]=true;s[s.length]=u;h.push(r[u]);var d=t[Math.floor(u*4/i)];_=u*4&l;if(i<4+_)throw new Error("FAT boundary crossed: "+u+" 4 "+i);if(!r[d])break;u=__readInt32LE(r[d],_);if(v[u])break}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(;u0&&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;s1?1:-1;_.size=0;_.type=5}else if(v.slice(-1)=="/"){for(o=s+1;o=u.length?-1:o;for(o=s+1;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;i0){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>9)}s(i[6]+7>>3);while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);a=f=0;for(h=0;h=4096)continue;o.start=a;s(l+63>>6)}while(n.l&511)n.write_shift(-4,z.ENDOFCHAIN);for(f=0;f=4096){n.l=o.start+1<<9;for(h=0;h0&&o.size<4096){for(h=0;h>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<>>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(;n0)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>i-u;for(a=(1<=0;--a)e[s|a<>8-v;for(var d=(1<<7-v)-1;d>=0;--d)lr[_|d<>>=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>>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>>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*/, dir_start/*:number*/, fat k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -482,6 +484,7 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/); } diff --git a/index.html b/index.html index 372a793..0a67c47 100644 --- a/index.html +++ b/index.html @@ -46,7 +46,7 @@ Use readAsBinaryString: (when available) /*jshint browser:true */ /* eslint-env browser */ -/*global Uint8Array, ArrayBuffer, console, alert */ +/*global Uint8Array, ArrayBuffer */ /*global CFB, out, PRINTJ, saveAs */ /* exported savefile, download_file */ /* eslint no-use-before-define:0 */ diff --git a/package.json b/package.json index 769d80c..7d21c0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cfb", - "version": "1.1.3", + "version": "1.1.4", "author": "sheetjs", "description": "Compound File Binary File Format extractor", "keywords": [ diff --git a/test.js b/test.js index 24a87c8..a69e1a6 100644 --- a/test.js +++ b/test.js @@ -213,8 +213,8 @@ describe('output formats', function() { if(typeof r[1] != "string") throw new Error(r[0] + " binary write failed"); var good = false; for(var i = 0; i < r[1].length; ++i) { - if(r[1].charCodeAt(i) == 0x00) good = true; - else if(r[1].charCodeAt(i) > 0xFF) { good = false; break; } + if(/*::((*/r[1]/*:: :any):string)*/.charCodeAt(i) == 0x00) good = true; + else if(/*::((*/r[1]/*:: :any):string)*/.charCodeAt(i) > 0xFF) { good = false; break; } } if(!good) throw new Error(r[0] + " binary write failed"); }); diff --git a/types/tsconfig.json b/types/tsconfig.json index 2b6ed3c..54324b0 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -9,6 +9,7 @@ "paths": { "cfb": ["."] }, "types": [], "noEmit": true, + "strictFunctionTypes": true, "forceConsistentCasingInFileNames": true } } diff --git a/xlscfb.flow.js b/xlscfb.flow.js index b07b24e..067ae18 100644 --- a/xlscfb.flow.js +++ b/xlscfb.flow.js @@ -142,7 +142,7 @@ CRC32.str = crc32_str; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports/*:CFBModule*/ = /*::(*/{}/*:: :any)*/; -exports.version = '1.1.3'; +exports.version = '1.1.4'; /* [MS-CFB] 2.6.4 */ function namecmp(l/*:string*/, r/*:string*/)/*:number*/ { var L = l.split("/"), R = r.split("/"); @@ -473,7 +473,9 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -482,6 +484,7 @@ function make_sector_list(sectors/*:Array*/, dir_start/*:number*/, fat if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}/*:SectorEntry*/); } diff --git a/xlscfb.js b/xlscfb.js index 3b654eb..a4a9ad6 100644 --- a/xlscfb.js +++ b/xlscfb.js @@ -109,7 +109,7 @@ CRC32.str = crc32_str; /* [MS-CFB] v20171201 */ var CFB = (function _CFB(){ var exports = {}; -exports.version = '1.1.3'; +exports.version = '1.1.4'; /* [MS-CFB] 2.6.4 */ function namecmp(l, r) { var L = l.split("/"), R = r.split("/"); @@ -440,7 +440,9 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { k = (i + dir_start); if(k >= sl) k-=sl; if(chkd[k]) continue; buf_chain = []; + var seen = []; for(j=k; j>=0;) { + seen[j] = true; chkd[j] = true; buf[buf.length] = j; buf_chain.push(sectors[j]); @@ -449,6 +451,7 @@ function make_sector_list(sectors, dir_start, fat_addrs, ssz) { if(ssz < 4 + jj) throw new Error("FAT boundary crossed: " + j + " 4 "+ssz); if(!sectors[addr]) break; j = __readInt32LE(sectors[addr], jj); + if(seen[j]) break; } sector_list[k] = ({nodes: buf, data:__toBuffer([buf_chain])}); }