updating to 0.14.0

This commit is contained in:
SheetJS 2018-09-06 04:16:21 -04:00
parent 2e374e3d3a
commit 65d629e316
4 changed files with 778 additions and 48 deletions

@ -306,7 +306,7 @@ function parsetest(x/*:string*/, wb/*:Workbook*/, full/*:boolean*/, ext/*:?strin
var jsonf = getfile(dir, x, i, ".json");
if(fs.existsSync(jsonf)) it('#' + i + ' (' + ws + ')', function() {
var file = fs.readFileSync(jsonf, 'utf-8');
var json = X.utils.make_json(wb.Sheets[ws]);
var json = X.utils.make_json(wb.Sheets[ws], {raw:false});
assert.equal(JSON.stringify(json), fixjson(file), "JSON badness");
});
});
@ -1103,7 +1103,7 @@ describe('parse features', function() {
var sheetName = 'Sheet1';
wb = X.read(fs.readFileSync(paths.dtxlsx), {type:TYPE});
ws = wb.Sheets[sheetName];
var sheet = X.utils.sheet_to_json(ws);
var sheet = X.utils.sheet_to_json(ws, {raw: false});
assert.equal(sheet[3]['てすと'], '2/14/14');
});
it('cellDates should not affect formatted text', function() {
@ -1639,7 +1639,7 @@ describe('roundtrip features', function() {
{a:true, c:false},
{c:fixdate}
];
var o = X.utils.sheet_to_json(X.utils.json_to_sheet(data, {cellDates:true}), {raw:true});
var o = X.utils.sheet_to_json(X.utils.json_to_sheet(data, {cellDates:true}));
data.forEach(function(row, i) {
Object.keys(row).forEach(function(k) { assert.equal(row[k], o[i][k]); });
});
@ -1707,7 +1707,7 @@ describe('json output', function() {
if(typeof before != 'undefined') before(bef);
else it('before', bef);
it('should use first-row headers and full sheet by default', function() {
var json = X.utils.sheet_to_json(ws);
var json = X.utils.sheet_to_json(ws, {raw: null});
assert.equal(json.length, data.length - 1);
assert.equal(json[0][1], "TRUE");
assert.equal(json[1][2], "bar");
@ -1716,7 +1716,7 @@ describe('json output', function() {
assert.throws(function() { seeker(json, [1,2,3], "baz"); });
});
it('should create array of arrays if header == 1', function() {
var json = X.utils.sheet_to_json(ws, {header:1});
var json = X.utils.sheet_to_json(ws, {header:1, raw:""});
assert.equal(json.length, data.length);
assert.equal(json[1][0], "TRUE");
assert.equal(json[2][1], "bar");
@ -1726,7 +1726,7 @@ describe('json output', function() {
assert.throws(function() { seeker(json, [0,1,2], "baz"); });
});
it('should use column names if header == "A"', function() {
var json = X.utils.sheet_to_json(ws, {header:'A'});
var json = X.utils.sheet_to_json(ws, {header:'A', raw:false});
assert.equal(json.length, data.length);
assert.equal(json[1].A, "TRUE");
assert.equal(json[2].B, "bar");
@ -1736,7 +1736,7 @@ describe('json output', function() {
assert.throws(function() { seeker(json, "ABC", "baz"); });
});
it('should use column labels if specified', function() {
var json = X.utils.sheet_to_json(ws, {header:["O","D","I","N"]});
var json = X.utils.sheet_to_json(ws, {header:["O","D","I","N"], raw:0});
assert.equal(json.length, data.length);
assert.equal(json[1].O, "TRUE");
assert.equal(json[2].D, "bar");
@ -1749,7 +1749,7 @@ describe('json output', function() {
it('should accept custom ' + w[0] + ' range', function() {
var json = X.utils.sheet_to_json(ws, {header:1, range:w[1]});
assert.equal(json.length, 3);
assert.equal(json[0][0], "TRUE");
assert.equal(json[0][0], true);
assert.equal(json[1][1], "bar");
assert.equal(json[2][2], "qux");
assert.doesNotThrow(function() { seeker(json, [0,1,2], "sheetjs"); });
@ -1760,7 +1760,7 @@ describe('json output', function() {
it('should use defval if requested', function() {
var json = X.utils.sheet_to_json(ws, {defval: 'jimjin'});
assert.equal(json.length, data.length - 1);
assert.equal(json[0][1], "TRUE");
assert.equal(json[0][1], true);
assert.equal(json[1][2], "bar");
assert.equal(json[2][3], "qux");
assert.equal(json[2][2], "jimjin");

31
xlsx.core.min.js vendored

File diff suppressed because one or more lines are too long

34
xlsx.full.min.js vendored

File diff suppressed because one or more lines are too long

743
xlsx.js

@ -4,7 +4,7 @@
/*global global, exports, module, require:false, process:false, Buffer:false, ArrayBuffer:false */
var XLSX = {};
function make_xlsx_lib(XLSX){
XLSX.version = '0.13.5';
XLSX.version = '0.14.0';
var current_codepage = 1200, current_ansi = 1252;
/*global cptable:true, window */
if(typeof module !== "undefined" && typeof require !== 'undefined') {
@ -125,7 +125,7 @@ var Base64 = (function make_b64(){
}
};
})();
var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && process.versions.node);
var has_buf = (typeof Buffer !== 'undefined' && typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node);
var Buffer_from = function(){};
@ -135,6 +135,8 @@ if(typeof Buffer !== 'undefined') {
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) { return new Buffer(n); };
// $FlowIgnore
if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
}
function new_raw_buf(len) {
@ -143,6 +145,12 @@ function new_raw_buf(len) {
/* jshint +W056 */
}
function new_unsafe_buf(len) {
/* jshint -W056 */
return has_buf ? Buffer.allocUnsafe(len) : new Array(len);
/* jshint +W056 */
}
var s2a = function s2a(s) {
// $FlowIgnore
if(has_buf) return Buffer_from(s, "binary");
@ -1132,12 +1140,113 @@ var DO_NOT_EXPORT_CFB = true;
/* vim: set ts=2: */
/*jshint eqnull:true */
/*exported CFB */
/*global module, require:false, process:false, Buffer:false, Uint8Array:false */
/*global module, require:false, process:false, Buffer:false, Uint8Array:false, Uint16Array:false */
/* 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 */
/*global Int32Array */
function signed_crc_table() {
var c = 0, table = new Array(256);
for(var n =0; n != 256; ++n){
c = n;
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
var T = signed_crc_table();
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;
}
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_str(str, seed) {
var C = seed ^ -1;
for(var i = 0, L=str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if(c < 0x80) {
C = (C>>>8) ^ T[(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];
} 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];
} 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];
}
}
return C ^ -1;
}
CRC32.table = T;
CRC32.bstr = crc32_bstr;
CRC32.buf = crc32_buf;
CRC32.str = crc32_str;
}));
/* [MS-CFB] v20171201 */
var CFB = (function _CFB(){
var exports = {};
exports.version = '1.0.8';
exports.version = '1.1.0';
/* [MS-CFB] 2.6.4 */
function namecmp(l, r) {
var L = l.split("/"), R = r.split("/");
@ -1158,9 +1267,75 @@ function filename(p) {
var c = p.lastIndexOf("/");
return (c === -1) ? p : p.slice(c+1);
}
/* -------------------------------------------------------------------------- */
/* DOS Date format:
high|YYYYYYYm.mmmddddd.HHHHHMMM.MMMSSSSS|low
add 1980 to stored year
stored second should be doubled
*/
/* write JS date to buf as a DOS date */
function write_dos_date(buf, date) {
if(typeof date === "string") date = new Date(date);
var hms = date.getHours();
hms = hms << 6 | date.getMinutes();
hms = hms << 5 | (date.getSeconds()>>>1);
buf.write_shift(2, hms);
var ymd = (date.getFullYear() - 1980);
ymd = ymd << 4 | (date.getMonth()+1);
ymd = ymd << 5 | date.getDate();
buf.write_shift(2, ymd);
}
/* read four bytes from buf and interpret as a DOS date */
function parse_dos_date(buf) {
var hms = buf.read_shift(2) & 0xFFFF;
var ymd = buf.read_shift(2) & 0xFFFF;
var val = new Date();
var d = ymd & 0x1F; ymd >>>= 5;
var m = ymd & 0x0F; ymd >>>= 4;
val.setMilliseconds(0);
val.setFullYear(ymd + 1980);
val.setMonth(m-1);
val.setDate(d);
var S = hms & 0x1F; hms >>>= 5;
var M = hms & 0x3F; hms >>>= 6;
val.setHours(hms);
val.setMinutes(M);
val.setSeconds(S<<1);
return val;
}
function parse_extra_field(blob) {
prep_blob(blob, 0);
var o = {};
var flags = 0;
while(blob.l <= blob.length - 4) {
var type = blob.read_shift(2);
var sz = blob.read_shift(2), tgt = blob.l + sz;
var p = {};
switch(type) {
/* UNIX-style Timestamps */
case 0x5455: {
flags = blob.read_shift(1);
if(flags & 1) p.mtime = blob.read_shift(4);
/* for some reason, CD flag corresponds to LFH */
if(sz > 5) {
if(flags & 2) p.atime = blob.read_shift(4);
if(flags & 4) p.ctime = blob.read_shift(4);
}
if(p.mtime) p.mt = new Date(p.mtime*1000);
}
break;
}
blob.l = tgt;
o[type] = p;
}
return o;
}
var fs;
function get_fs() { return fs || (fs = require('fs')); }
function parse(file, options) {
if(file[0] == 0x50 && file[1] == 0x4b) return parse_zip(file, options);
if(file.length < 512) throw new Error("CFB file size " + file.length + " < 512");
var mver = 3;
var ssz = 512;
@ -1181,6 +1356,8 @@ var mv = check_get_mver(blob);
mver = mv[0];
switch(mver) {
case 3: ssz = 512; break; case 4: ssz = 4096; break;
case 0: if(mv[1] == 0) return parse_zip(file, options);
/* falls through */
default: throw new Error("Major Version: Expected 3 or 4 saw " + mver);
}
@ -1259,6 +1436,7 @@ return o;
/* [MS-CFB] 2.2 Compound File Header -- read up to major version */
function check_get_mver(blob) {
if(blob[blob.l] == 0x50 && blob[blob.l + 1] == 0x4b) return [0, 0];
// header signature 8
blob.chk(HEADER_SIGNATURE, 'Header Signature: ');
@ -1576,6 +1754,7 @@ function rebuild_cfb(cfb, f) {
function _write(cfb, options) {
var _opts = options || {};
rebuild_cfb(cfb);
if(_opts.fileType == 'zip') return write_zip(cfb, _opts);
var L = (function(cfb){
var mini_size = 0, fat_size = 0;
for(var i = 0; i < cfb.FileIndex.length; ++i) {
@ -1770,6 +1949,551 @@ function write(cfb, options) {
}
return o;
}
/* node < 8.1 zlib does not expose bytesRead, so default to pure JS */
var _zlib;
function use_zlib(zlib) { try {
var InflateRaw = zlib.InflateRaw;
var InflRaw = new InflateRaw();
InflRaw._processChunk(new Uint8Array([3, 0]), InflRaw._finishFlushFlag);
if(InflRaw.bytesRead) _zlib = zlib;
else throw new Error("zlib does not expose bytesRead");
} catch(e) {console.error("cannot use native zlib: " + (e.message || e)); } }
function _inflateRawSync(payload, usz) {
if(!_zlib) return _inflate(payload, usz);
var InflateRaw = _zlib.InflateRaw;
var InflRaw = new InflateRaw();
var out = InflRaw._processChunk(payload.slice(payload.l), InflRaw._finishFlushFlag);
payload.l += InflRaw.bytesRead;
return out;
}
function _deflateRawSync(payload) {
return _zlib ? _zlib.deflateRawSync(payload) : _deflate(payload);
}
var CLEN_ORDER = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
/* LEN_ID = [ 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285 ]; */
var LEN_LN = [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13 , 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 ];
/* DST_ID = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ]; */
var DST_LN = [ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 ];
function bit_swap_8(n) { var t = (((((n<<1)|(n<<11)) & 0x22110) | (((n<<5)|(n<<15)) & 0x88440))); return ((t>>16) | (t>>8) |t)&0xFF; }
var use_typed_arrays = typeof Uint8Array !== 'undefined';
var bitswap8 = use_typed_arrays ? new Uint8Array(1<<8) : [];
for(var q = 0; q < (1<<8); ++q) bitswap8[q] = bit_swap_8(q);
function bit_swap_n(n, b) {
var rev = bitswap8[n & 0xFF];
if(b <= 8) return rev >>> (8-b);
rev = (rev << 8) | bitswap8[(n>>8)&0xFF];
if(b <= 16) return rev >>> (16-b);
rev = (rev << 8) | bitswap8[(n>>16)&0xFF];
return rev >>> (24-b);
}
/* helpers for unaligned bit reads */
function read_bits_2(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 6 ? 0 : buf[h+1]<<8))>>>w)& 0x03; }
function read_bits_3(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 5 ? 0 : buf[h+1]<<8))>>>w)& 0x07; }
function read_bits_4(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 4 ? 0 : buf[h+1]<<8))>>>w)& 0x0F; }
function read_bits_5(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 3 ? 0 : buf[h+1]<<8))>>>w)& 0x1F; }
function read_bits_7(buf, bl) { var w = (bl&7), h = (bl>>>3); return ((buf[h]|(w <= 1 ? 0 : buf[h+1]<<8))>>>w)& 0x7F; }
/* works up to n = 3 * 8 + 1 = 25 */
function read_bits_n(buf, bl, n) {
var w = (bl&7), h = (bl>>>3), f = ((1<<n)-1);
var v = buf[h] >>> w;
if(n < 8 - w) return v & f;
v |= buf[h+1]<<(8-w);
if(n < 16 - w) return v & f;
v |= buf[h+2]<<(16-w);
if(n < 24 - w) return v & f;
v |= buf[h+3]<<(24-w);
return v & f;
}
/* until ArrayBuffer#realloc is a thing, fake a realloc */
function realloc(b, sz) {
var L = b.length, M = 2*L > sz ? 2*L : sz + 5, i = 0;
if(L >= sz) return b;
if(has_buf) {
var o = new_unsafe_buf(M);
// $FlowIgnore
if(b.copy) b.copy(o);
else for(; i < b.length; ++i) o[i] = b[i];
return o;
} else if(use_typed_arrays) {
var a = new Uint8Array(M);
if(a.set) a.set(b);
else for(; i < b.length; ++i) a[i] = b[i];
return a;
}
b.length = M;
return b;
}
/* zero-filled arrays for older browsers */
function zero_fill_array(n) {
var o = new Array(n);
for(var i = 0; i < n; ++i) o[i] = 0;
return o;
}var _deflate = (function() {
var _deflateRaw = (function() {
return function deflateRaw(data, out) {
var boff = 0;
while(boff < data.length) {
var L = Math.min(0xFFFF, data.length - boff);
var h = boff + L == data.length;
/* TODO: this is only type 0 stored */
out.write_shift(1, +h);
out.write_shift(2, L);
out.write_shift(2, (~L) & 0xFFFF);
while(L-- > 0) out[out.l++] = data[boff++];
}
return out.l;
};
})();
return function(data) {
var buf = new_buf(50+Math.floor(data.length*1.1));
var off = _deflateRaw(data, buf);
return buf.slice(0, off);
};
})();
/* modified inflate function also moves original read head */
/* build tree (used for literals and lengths) */
function build_tree(clens, cmap, MAX) {
var maxlen = 1, w = 0, i = 0, j = 0, ccode = 0, L = clens.length;
var bl_count = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
for(i = 0; i < 32; ++i) bl_count[i] = 0;
for(i = L; i < MAX; ++i) clens[i] = 0;
L = clens.length;
var ctree = use_typed_arrays ? new Uint16Array(L) : zero_fill_array(L); // []
/* build code tree */
for(i = 0; i < L; ++i) {
bl_count[(w = clens[i])]++;
if(maxlen < w) maxlen = w;
ctree[i] = 0;
}
bl_count[0] = 0;
for(i = 1; i <= maxlen; ++i) bl_count[i+16] = (ccode = (ccode + bl_count[i-1])<<1);
for(i = 0; i < L; ++i) {
ccode = clens[i];
if(ccode != 0) ctree[i] = bl_count[ccode+16]++;
}
/* cmap[maxlen + 4 bits] = (off&15) + (lit<<4) reverse mapping */
var cleni = 0;
for(i = 0; i < L; ++i) {
cleni = clens[i];
if(cleni != 0) {
ccode = bit_swap_n(ctree[i], maxlen)>>(maxlen-cleni);
for(j = (1<<(maxlen + 4 - cleni)) - 1; j>=0; --j)
cmap[ccode|(j<<cleni)] = (cleni&15) | (i<<4);
}
}
return maxlen;
}
var fix_lmap = use_typed_arrays ? new Uint16Array(512) : zero_fill_array(512);
var fix_dmap = use_typed_arrays ? new Uint16Array(32) : zero_fill_array(32);
if(!use_typed_arrays) {
for(var i = 0; i < 512; ++i) fix_lmap[i] = 0;
for(i = 0; i < 32; ++i) fix_dmap[i] = 0;
}
(function() {
var dlens = [];
var i = 0;
for(;i<32; i++) dlens.push(5);
build_tree(dlens, fix_dmap, 32);
var clens = [];
i = 0;
for(; i<=143; i++) clens.push(8);
for(; i<=255; i++) clens.push(9);
for(; i<=279; i++) clens.push(7);
for(; i<=287; i++) clens.push(8);
build_tree(clens, fix_lmap, 288);
})();
var dyn_lmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
var dyn_dmap = use_typed_arrays ? new Uint16Array(32768) : zero_fill_array(32768);
var dyn_cmap = use_typed_arrays ? new Uint16Array(128) : zero_fill_array(128);
var dyn_len_1 = 1, dyn_len_2 = 1;
/* 5.5.3 Expanding Huffman Codes */
function dyn(data, boff) {
/* nomenclature from RFC1951 refers to bit values; these are offset by the implicit constant */
var _HLIT = read_bits_5(data, boff) + 257; boff += 5;
var _HDIST = read_bits_5(data, boff) + 1; boff += 5;
var _HCLEN = read_bits_4(data, boff) + 4; boff += 4;
var w = 0;
/* grab and store code lengths */
var clens = use_typed_arrays ? new Uint8Array(19) : zero_fill_array(19);
var ctree = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
var maxlen = 1;
var bl_count = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
var next_code = use_typed_arrays ? new Uint8Array(8) : zero_fill_array(8);
var L = clens.length; /* 19 */
for(var i = 0; i < _HCLEN; ++i) {
clens[CLEN_ORDER[i]] = w = read_bits_3(data, boff);
if(maxlen < w) maxlen = w;
bl_count[w]++;
boff += 3;
}
/* build code tree */
var ccode = 0;
bl_count[0] = 0;
for(i = 1; i <= maxlen; ++i) next_code[i] = ccode = (ccode + bl_count[i-1])<<1;
for(i = 0; i < L; ++i) if((ccode = clens[i]) != 0) ctree[i] = next_code[ccode]++;
/* cmap[7 bits from stream] = (off&7) + (lit<<3) */
var cleni = 0;
for(i = 0; i < L; ++i) {
cleni = clens[i];
if(cleni != 0) {
ccode = bitswap8[ctree[i]]>>(8-cleni);
for(var j = (1<<(7-cleni))-1; j>=0; --j) dyn_cmap[ccode|(j<<cleni)] = (cleni&7) | (i<<3);
}
}
/* read literal and dist codes at once */
var hcodes = [];
maxlen = 1;
for(; hcodes.length < _HLIT + _HDIST;) {
ccode = dyn_cmap[read_bits_7(data, boff)];
boff += ccode & 7;
switch((ccode >>>= 3)) {
case 16:
w = 3 + read_bits_2(data, boff); boff += 2;
ccode = hcodes[hcodes.length - 1];
while(w-- > 0) hcodes.push(ccode);
break;
case 17:
w = 3 + read_bits_3(data, boff); boff += 3;
while(w-- > 0) hcodes.push(0);
break;
case 18:
w = 11 + read_bits_7(data, boff); boff += 7;
while(w -- > 0) hcodes.push(0);
break;
default:
hcodes.push(ccode);
if(maxlen < ccode) maxlen = ccode;
break;
}
}
/* build literal / length trees */
var h1 = hcodes.slice(0, _HLIT), h2 = hcodes.slice(_HLIT);
for(i = _HLIT; i < 286; ++i) h1[i] = 0;
for(i = _HDIST; i < 30; ++i) h2[i] = 0;
dyn_len_1 = build_tree(h1, dyn_lmap, 286);
dyn_len_2 = build_tree(h2, dyn_dmap, 30);
return boff;
}
/* return [ data, bytesRead ] */
function inflate(data, usz) {
/* shortcircuit for empty buffer [0x03, 0x00] */
if(data[0] == 3 && !(data[1] & 0x3)) { return [new_raw_buf(usz), 2]; }
/* bit offset */
var boff = 0;
/* header includes final bit and type bits */
var header = 0;
var outbuf = new_unsafe_buf(usz ? usz : (1<<18));
var woff = 0;
var OL = outbuf.length>>>0;
var max_len_1 = 0, max_len_2 = 0;
while((header&1) == 0) {
header = read_bits_3(data, boff); boff += 3;
if((header >>> 1) == 0) {
/* Stored block */
if(boff & 7) boff += 8 - (boff&7);
/* 2 bytes sz, 2 bytes bit inverse */
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; }
continue;
} else if((header >>> 1) == 1) {
/* Fixed Huffman */
max_len_1 = 9; max_len_2 = 5;
} else {
/* Dynamic Huffman */
boff = dyn(data, boff);
max_len_1 = dyn_len_1; max_len_2 = dyn_len_2;
}
if(!usz && (OL < woff + 32767)) { outbuf = realloc(outbuf, woff + 32767); OL = outbuf.length; }
for(;;) { // while(true) is apparently out of vogue in modern JS circles
/* ingest code and move read head */
var bits = read_bits_n(data, boff, max_len_1);
var code = (header>>>1) == 1 ? fix_lmap[bits] : dyn_lmap[bits];
boff += code & 15; code >>>= 4;
/* 0-255 are literals, 256 is end of block token, 257+ are copy tokens */
if(((code>>>8)&0xFF) === 0) outbuf[woff++] = code;
else if(code == 256) break;
else {
code -= 257;
var len_eb = (code < 8) ? 0 : ((code-4)>>2); if(len_eb > 5) len_eb = 0;
var tgt = woff + LEN_LN[code];
/* length extra bits */
if(len_eb > 0) {
tgt += read_bits_n(data, boff, len_eb);
boff += len_eb;
}
/* dist code */
bits = read_bits_n(data, boff, max_len_2);
code = (header>>>1) == 1 ? fix_dmap[bits] : dyn_dmap[bits];
boff += code & 15; code >>>= 4;
var dst_eb = (code < 4 ? 0 : (code-2)>>1);
var dst = DST_LN[code];
/* dist extra bits */
if(dst_eb > 0) {
dst += read_bits_n(data, boff, dst_eb);
boff += dst_eb;
}
/* in the common case, manual byte copy is faster than TA set / Buffer copy */
if(!usz && OL < tgt) { outbuf = realloc(outbuf, tgt); OL = outbuf.length; }
while(woff < tgt) { outbuf[woff] = outbuf[woff - dst]; ++woff; }
}
}
}
return [usz ? outbuf : outbuf.slice(0, woff), (boff+7)>>>3];
}
function _inflate(payload, usz) {
var data = payload.slice(payload.l||0);
var out = inflate(data, usz);
payload.l += out[1];
return out[0];
}
function warn_or_throw(wrn, msg) {
if(wrn) { if(typeof console !== 'undefined') console.error(msg); }
else throw new Error(msg);
}
function parse_zip(file, options) {
var blob = file;
prep_blob(blob, 0);
var FileIndex = [], FullPaths = [];
var o = {
FileIndex: FileIndex,
FullPaths: FullPaths
};
init_cfb(o, { root: options.root });
/* find end of central directory, start just after signature */
var i = blob.length - 4;
while((blob[i] != 0x50 || blob[i+1] != 0x4b || blob[i+2] != 0x05 || blob[i+3] != 0x06) && i >= 0) --i;
blob.l = i + 4;
/* parse end of central directory */
blob.l += 4;
var fcnt = blob.read_shift(2);
blob.l += 6;
var start_cd = blob.read_shift(4);
/* parse central directory */
blob.l = start_cd;
for(i = 0; i < fcnt; ++i) {
/* trust local file header instead of CD entry */
blob.l += 20;
var csz = blob.read_shift(4);
var usz = blob.read_shift(4);
var namelen = blob.read_shift(2);
var efsz = blob.read_shift(2);
var fcsz = blob.read_shift(2);
blob.l += 8;
var offset = blob.read_shift(4);
var EF = parse_extra_field(blob.slice(blob.l+namelen, blob.l+namelen+efsz));
blob.l += namelen + efsz + fcsz;
var L = blob.l;
blob.l = offset + 4;
parse_local_file(blob, csz, usz, o, EF);
blob.l = L;
}
return o;
}
/* head starts just after local file header signature */
function parse_local_file(blob, csz, usz, o, EF) {
/* [local file header] */
blob.l += 2;
var flags = blob.read_shift(2);
var meth = blob.read_shift(2);
var date = parse_dos_date(blob);
if(flags & 0x2041) throw new Error("Unsupported ZIP encryption");
var crc32 = blob.read_shift(4);
var _csz = blob.read_shift(4);
var _usz = blob.read_shift(4);
var namelen = blob.read_shift(2);
var efsz = blob.read_shift(2);
// TODO: flags & (1<<11) // UTF8
var name = ""; for(var i = 0; i < namelen; ++i) name += String.fromCharCode(blob[blob.l++]);
if(efsz) {
var ef = parse_extra_field(blob.slice(blob.l, blob.l + efsz));
if((ef[0x5455]||{}).mt) date = ef[0x5455].mt;
if(((EF||{})[0x5455]||{}).mt) date = EF[0x5455].mt;
}
blob.l += efsz;
/* [encryption header] */
/* [file data] */
var data = blob.slice(blob.l, blob.l + _csz);
switch(meth) {
case 8: data = _inflateRawSync(blob, _usz); break;
case 0: break;
default: throw new Error("Unsupported ZIP Compression method " + meth);
}
/* [data descriptor] */
var wrn = false;
if(flags & 8) {
crc32 = blob.read_shift(4);
if(crc32 == 0x08074b50) { crc32 = blob.read_shift(4); wrn = true; }
_csz = blob.read_shift(4);
_usz = blob.read_shift(4);
}
if(_csz != csz) warn_or_throw(wrn, "Bad compressed size: " + csz + " != " + _csz);
if(_usz != usz) warn_or_throw(wrn, "Bad uncompressed size: " + usz + " != " + _usz);
var _crc32 = CRC32.buf(data, 0);
if(crc32 != _crc32) warn_or_throw(wrn, "Bad CRC32 checksum: " + crc32 + " != " + _crc32);
cfb_add(o, name, data, {unsafe: true, mt: date});
}
function write_zip(cfb, options) {
var _opts = options || {};
var out = [], cdirs = [];
var o = new_buf(1);
var method = (_opts.compression ? 8 : 0), flags = 0;
var desc = false;
if(desc) flags |= 8;
var i = 0, j = 0;
var start_cd = 0, fcnt = 0;
var root = cfb.FullPaths[0], fp = root, fi = cfb.FileIndex[0];
var crcs = [];
var sz_cd = 0;
for(i = 1; i < cfb.FullPaths.length; ++i) {
fp = cfb.FullPaths[i].slice(root.length); fi = cfb.FileIndex[i];
if(!fi.size || !fi.content || fp == "\u0001Sh33tJ5") continue;
var start = start_cd;
/* TODO: CP437 filename */
var namebuf = new_buf(fp.length);
for(j = 0; j < fp.length; ++j) namebuf.write_shift(1, fp.charCodeAt(j) & 0x7F);
namebuf = namebuf.slice(0, namebuf.l);
crcs[fcnt] = CRC32.buf(fi.content, 0);
var outbuf = fi.content;
if(method == 8) outbuf = _deflateRawSync(outbuf);
/* local file header */
o = new_buf(30);
o.write_shift(4, 0x04034b50);
o.write_shift(2, 20);
o.write_shift(2, flags);
o.write_shift(2, method);
/* TODO: last mod file time/date */
if(fi.mt) write_dos_date(o, fi.mt);
else o.write_shift(4, 0);
o.write_shift(-4, (flags & 8) ? 0 : crcs[fcnt]);
o.write_shift(4, (flags & 8) ? 0 : outbuf.length);
o.write_shift(4, (flags & 8) ? 0 : fi.content.length);
o.write_shift(2, namebuf.length);
o.write_shift(2, 0);
start_cd += o.length;
out.push(o);
start_cd += namebuf.length;
out.push(namebuf);
/* TODO: encryption header ? */
start_cd += outbuf.length;
out.push(outbuf);
/* data descriptor */
if(flags & 8) {
o = new_buf(12);
o.write_shift(-4, crcs[fcnt]);
o.write_shift(4, outbuf.length);
o.write_shift(4, fi.content.length);
start_cd += o.l;
out.push(o);
}
/* central directory */
o = new_buf(46);
o.write_shift(4, 0x02014b50);
o.write_shift(2, 0);
o.write_shift(2, 20);
o.write_shift(2, flags);
o.write_shift(2, method);
o.write_shift(4, 0); /* TODO: last mod file time/date */
o.write_shift(-4, crcs[fcnt]);
o.write_shift(4, outbuf.length);
o.write_shift(4, fi.content.length);
o.write_shift(2, namebuf.length);
o.write_shift(2, 0);
o.write_shift(2, 0);
o.write_shift(2, 0);
o.write_shift(2, 0);
o.write_shift(4, 0);
o.write_shift(4, start);
sz_cd += o.l;
cdirs.push(o);
sz_cd += namebuf.length;
cdirs.push(namebuf);
++fcnt;
}
/* end of central directory */
o = new_buf(22);
o.write_shift(4, 0x06054b50);
o.write_shift(2, 0);
o.write_shift(2, 0);
o.write_shift(2, fcnt);
o.write_shift(2, fcnt);
o.write_shift(4, sz_cd);
o.write_shift(4, start_cd);
o.write_shift(2, 0);
return bconcat(([bconcat((out)), bconcat(cdirs), o]));
}
function cfb_new(opts) {
var o = ({});
init_cfb(o, opts);
@ -1796,6 +2520,8 @@ file.content = (content);
file.size = content ? content.length : 0;
if(opts) {
if(opts.CLSID) file.clsid = opts.CLSID;
if(opts.mt) file.mt = opts.mt;
if(opts.ct) file.ct = opts.ct;
}
return file;
}
@ -1839,6 +2565,9 @@ exports.utils = {
CheckField: CheckField,
prep_blob: prep_blob,
bconcat: bconcat,
use_zlib: use_zlib,
_deflateRaw: _deflate,
_inflateRaw: _inflate,
consts: consts
};
@ -6094,7 +6823,7 @@ function sheet_to_dbf(ws, opts) {
var o = opts || {};
if(o.type == "string") throw new Error("Cannot write DBF to JS string");
var ba = buf_array();
var aoa = sheet_to_json(ws, {header:1, raw:true, cellDates:true});
var aoa = sheet_to_json(ws, {header:1, cellDates:true});
var headers = aoa[0], data = aoa.slice(1);
var i = 0, j = 0, hcnt = 0, rlen = 1;
for(i = 0; i < headers.length; ++i) {
@ -17950,7 +18679,7 @@ function write_ws_biff8(idx, opts, wb) {
write_biff_rec(ba, "HCenter", writebool(false));
write_biff_rec(ba, "VCenter", writebool(false));
/* ... */
write_biff_rec(ba, "Dimensions", write_Dimensions(range, opts));
write_biff_rec(ba, 0x200, write_Dimensions(range, opts));
/* ... */
if(b8) ws['!links'] = [];
@ -19737,7 +20466,7 @@ function writeFileAsync(filename, wb, opts, cb) {
}
function make_json_row(sheet, r, R, cols, header, hdr, dense, o) {
var rr = encode_row(R);
var defval = o.defval, raw = o.raw;
var defval = o.defval, raw = o.raw || !o.hasOwnProperty("raw");
var isempty = true;
var row = (header === 1) ? [] : {};
if(header !== 1) {