sheetjs/modules/83_numbers.js

679 lines
20 KiB
JavaScript
Raw Normal View History

/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
2022-03-21 01:39:16 +00:00
function u8_to_dataview(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
2022-03-21 01:39:16 +00:00
}
function u8str(u8) {
return typeof TextDecoder != "undefined" ? new TextDecoder().decode(u8) : utf8read(a2s(u8));
2022-03-21 01:39:16 +00:00
}
function u8concat(u8a) {
var len = u8a.reduce(function(acc, x) {
return acc + x.length;
}, 0);
var out = new Uint8Array(len);
var off = 0;
u8a.forEach(function(u8) {
out.set(u8, off);
off += u8.length;
2022-01-29 02:29:34 +00:00
});
return out;
2022-03-21 01:39:16 +00:00
}
function popcnt(x) {
x -= x >> 1 & 1431655765;
x = (x & 858993459) + (x >> 2 & 858993459);
return (x + (x >> 4) & 252645135) * 16843009 >>> 24;
2022-03-21 01:39:16 +00:00
}
function readDecimal128LE(buf, offset) {
var exp = (buf[offset + 15] & 127) << 7 | buf[offset + 14] >> 1;
var mantissa = buf[offset + 14] & 1;
for (var j = offset + 13; j >= offset; --j)
mantissa = mantissa * 256 + buf[j];
return (buf[offset + 15] & 128 ? -mantissa : mantissa) * Math.pow(10, exp - 6176);
2022-03-21 01:39:16 +00:00
}
function parse_varint49(buf, ptr) {
var l = ptr ? ptr[0] : 0;
var usz = buf[l] & 127;
varint:
if (buf[l++] >= 128) {
usz |= (buf[l] & 127) << 7;
if (buf[l++] < 128)
break varint;
usz |= (buf[l] & 127) << 14;
if (buf[l++] < 128)
break varint;
usz |= (buf[l] & 127) << 21;
if (buf[l++] < 128)
break varint;
usz += (buf[l] & 127) * Math.pow(2, 28);
++l;
if (buf[l++] < 128)
break varint;
usz += (buf[l] & 127) * Math.pow(2, 35);
++l;
if (buf[l++] < 128)
break varint;
usz += (buf[l] & 127) * Math.pow(2, 42);
++l;
if (buf[l++] < 128)
break varint;
}
if (ptr)
ptr[0] = l;
return usz;
}
2022-03-21 01:39:16 +00:00
function write_varint49(v) {
var usz = new Uint8Array(7);
usz[0] = v & 127;
var L = 1;
sz:
if (v > 127) {
usz[L - 1] |= 128;
usz[L] = v >> 7 & 127;
++L;
if (v <= 16383)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 14 & 127;
++L;
if (v <= 2097151)
break sz;
usz[L - 1] |= 128;
usz[L] = v >> 21 & 127;
++L;
if (v <= 268435455)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 256 >>> 21 & 127;
++L;
if (v <= 34359738367)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 65536 >>> 21 & 127;
++L;
if (v <= 4398046511103)
break sz;
usz[L - 1] |= 128;
usz[L] = v / 16777216 >>> 21 & 127;
++L;
}
return usz.slice(0, L);
}
function varint_to_i32(buf) {
var l = 0, i32 = buf[l] & 127;
varint:
if (buf[l++] >= 128) {
i32 |= (buf[l] & 127) << 7;
if (buf[l++] < 128)
break varint;
i32 |= (buf[l] & 127) << 14;
if (buf[l++] < 128)
break varint;
i32 |= (buf[l] & 127) << 21;
if (buf[l++] < 128)
break varint;
i32 |= (buf[l] & 127) << 28;
2022-01-31 11:40:30 +00:00
}
return i32;
}
function parse_shallow(buf) {
var out = [], ptr = [0];
while (ptr[0] < buf.length) {
var off = ptr[0];
var num = parse_varint49(buf, ptr);
var type = num & 7;
num = Math.floor(num / 8);
var len = 0;
var res;
if (num == 0)
break;
switch (type) {
2022-01-31 11:40:30 +00:00
case 0:
{
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
}
break;
case 5:
len = 4;
res = buf.slice(ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 1:
len = 8;
res = buf.slice(ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 2:
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
ptr[0] += len;
break;
2022-01-31 11:40:30 +00:00
case 3:
case 4:
default:
throw new Error("PB Type ".concat(type, " for Field ").concat(num, " at offset ").concat(off));
2022-01-31 11:40:30 +00:00
}
var v = { offset: off, data: res, type: type };
if (out[num] == null)
out[num] = [v];
else
out[num].push(v);
2022-01-31 11:40:30 +00:00
}
return out;
}
function write_shallow(proto) {
var out = [];
proto.forEach(function(field, idx) {
field.forEach(function(item) {
out.push(write_varint49(idx * 8 + item.type));
if (item.type == 2)
out.push(write_varint49(item.data.length));
out.push(item.data);
});
});
return u8concat(out);
}
function mappa(data, cb) {
if (!data)
return [];
return data.map(function(d) {
var _a;
try {
return cb(d.data);
} catch (e) {
var m = (_a = e.message) == null ? void 0 : _a.match(/at offset (\d+)/);
if (m)
e.message = e.message.replace(/at offset (\d+)/, "at offset " + (+m[1] + d.offset));
throw e;
}
});
}
function parse_iwa_file(buf) {
var out = [], ptr = [0];
while (ptr[0] < buf.length) {
var len = parse_varint49(buf, ptr);
var ai = parse_shallow(buf.slice(ptr[0], ptr[0] + len));
ptr[0] += len;
var res = {
id: varint_to_i32(ai[1][0].data),
messages: []
};
ai[2].forEach(function(b) {
var mi = parse_shallow(b.data);
var fl = varint_to_i32(mi[3][0].data);
res.messages.push({
meta: mi,
data: buf.slice(ptr[0], ptr[0] + fl)
});
ptr[0] += fl;
});
out.push(res);
}
return out;
}
function parse_snappy_chunk(type, buf) {
if (type != 0)
throw new Error("Unexpected Snappy chunk type ".concat(type));
var ptr = [0];
var usz = parse_varint49(buf, ptr);
var chunks = [];
while (ptr[0] < buf.length) {
var tag = buf[ptr[0]] & 3;
if (tag == 0) {
var len = buf[ptr[0]++] >> 2;
if (len < 60)
++len;
else {
var c = len - 59;
len = buf[ptr[0]];
if (c > 1)
len |= buf[ptr[0] + 1] << 8;
if (c > 2)
len |= buf[ptr[0] + 2] << 16;
if (c > 3)
len |= buf[ptr[0] + 3] << 24;
len >>>= 0;
len++;
ptr[0] += c;
}
chunks.push(buf.slice(ptr[0], ptr[0] + len));
ptr[0] += len;
continue;
} else {
var offset = 0, length = 0;
if (tag == 1) {
length = (buf[ptr[0]] >> 2 & 7) + 4;
offset = (buf[ptr[0]++] & 224) << 3;
offset |= buf[ptr[0]++];
} else {
length = (buf[ptr[0]++] >> 2) + 1;
if (tag == 2) {
offset = buf[ptr[0]] | buf[ptr[0] + 1] << 8;
ptr[0] += 2;
} else {
offset = (buf[ptr[0]] | buf[ptr[0] + 1] << 8 | buf[ptr[0] + 2] << 16 | buf[ptr[0] + 3] << 24) >>> 0;
ptr[0] += 4;
}
}
chunks = [u8concat(chunks)];
if (offset == 0)
throw new Error("Invalid offset 0");
if (offset > chunks[0].length)
throw new Error("Invalid offset beyond length");
if (length >= offset) {
chunks.push(chunks[0].slice(-offset));
length -= offset;
while (length >= chunks[chunks.length - 1].length) {
chunks.push(chunks[chunks.length - 1]);
length -= chunks[chunks.length - 1].length;
}
2022-01-29 02:29:34 +00:00
}
chunks.push(chunks[0].slice(-offset, -offset + length));
}
}
var o = u8concat(chunks);
if (o.length != usz)
throw new Error("Unexpected length: ".concat(o.length, " != ").concat(usz));
return o;
}
2022-03-21 01:39:16 +00:00
function decompress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
var t = buf[l++];
var len = buf[l] | buf[l + 1] << 8 | buf[l + 2] << 16;
l += 3;
out.push(parse_snappy_chunk(t, buf.slice(l, l + len)));
l += len;
}
if (l !== buf.length)
throw new Error("data is not a valid framed stream!");
return u8concat(out);
}
2022-03-21 01:39:16 +00:00
function compress_iwa_file(buf) {
var out = [];
var l = 0;
while (l < buf.length) {
var c = Math.min(buf.length - l, 268435455);
var frame = new Uint8Array(4);
out.push(frame);
var usz = write_varint49(c);
var L = usz.length;
out.push(usz);
if (c <= 60) {
L++;
out.push(new Uint8Array([c - 1 << 2]));
} else if (c <= 256) {
L += 2;
out.push(new Uint8Array([240, c - 1 & 255]));
} else if (c <= 65536) {
L += 3;
out.push(new Uint8Array([244, c - 1 & 255, c - 1 >> 8 & 255]));
} else if (c <= 16777216) {
L += 4;
out.push(new Uint8Array([248, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255]));
} else if (c <= 4294967296) {
L += 5;
out.push(new Uint8Array([252, c - 1 & 255, c - 1 >> 8 & 255, c - 1 >> 16 & 255, c - 1 >>> 24 & 255]));
}
out.push(buf.slice(l, l + c));
L += c;
frame[0] = 0;
frame[1] = L & 255;
frame[2] = L >> 8 & 255;
frame[3] = L >> 16 & 255;
l += c;
}
return u8concat(out);
}
function parse_old_storage(buf, sst, rsst, v) {
var dv = u8_to_dataview(buf);
var flags = dv.getUint32(4, true);
2022-03-21 01:39:16 +00:00
var data_offset = (v > 1 ? 12 : 8) + popcnt(flags & (v > 1 ? 3470 : 398)) * 4;
var ridx = -1, sidx = -1, ieee = NaN, dt = new Date(2001, 0, 1);
if (flags & 512) {
ridx = dv.getUint32(data_offset, true);
data_offset += 4;
}
2022-03-21 01:39:16 +00:00
data_offset += popcnt(flags & (v > 1 ? 12288 : 4096)) * 4;
if (flags & 16) {
sidx = dv.getUint32(data_offset, true);
data_offset += 4;
}
if (flags & 32) {
ieee = dv.getFloat64(data_offset, true);
data_offset += 8;
}
if (flags & 64) {
dt.setTime(dt.getTime() + dv.getFloat64(data_offset, true) * 1e3);
data_offset += 8;
}
var ret;
2022-03-21 01:39:16 +00:00
switch (buf[2]) {
case 0:
break;
case 2:
ret = { t: "n", v: ieee };
break;
case 3:
ret = { t: "s", v: sst[sidx] };
break;
case 5:
ret = { t: "d", v: dt };
break;
case 6:
ret = { t: "b", v: ieee > 0 };
break;
case 7:
ret = { t: "n", v: ieee };
break;
case 8:
ret = { t: "e", v: 0 };
break;
case 9:
{
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else if (sidx > -1)
ret = { t: "s", v: sst[sidx] };
else if (!isNaN(ieee))
ret = { t: "n", v: ieee };
else
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
2022-01-29 02:29:34 +00:00
}
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
}
return ret;
}
function parse_storage(buf, sst, rsst) {
var dv = u8_to_dataview(buf);
var flags = dv.getUint32(8, true);
var data_offset = 12;
var ridx = -1, sidx = -1, d128 = NaN, ieee = NaN, dt = new Date(2001, 0, 1);
if (flags & 1) {
d128 = readDecimal128LE(buf, data_offset);
data_offset += 16;
}
if (flags & 2) {
ieee = dv.getFloat64(data_offset, true);
data_offset += 8;
}
if (flags & 4) {
dt.setTime(dt.getTime() + dv.getFloat64(data_offset, true) * 1e3);
data_offset += 8;
2022-01-29 02:29:34 +00:00
}
if (flags & 8) {
sidx = dv.getUint32(data_offset, true);
data_offset += 4;
2022-01-29 02:29:34 +00:00
}
if (flags & 16) {
ridx = dv.getUint32(data_offset, true);
data_offset += 4;
}
var ret;
2022-03-21 01:39:16 +00:00
switch (buf[1]) {
case 0:
break;
case 2:
ret = { t: "n", v: d128 };
break;
case 3:
ret = { t: "s", v: sst[sidx] };
break;
case 5:
ret = { t: "d", v: dt };
break;
case 6:
ret = { t: "b", v: ieee > 0 };
break;
case 7:
ret = { t: "n", v: ieee };
break;
case 8:
ret = { t: "e", v: 0 };
break;
case 9:
{
if (ridx > -1)
ret = { t: "s", v: rsst[ridx] };
else
2022-03-21 01:39:16 +00:00
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
}
break;
case 10:
ret = { t: "n", v: d128 };
break;
default:
2022-03-21 01:39:16 +00:00
throw new Error("Unsupported cell type ".concat(buf[1], " : ").concat(flags & 31, " : ").concat(buf.slice(0, 4)));
2022-01-29 02:29:34 +00:00
}
return ret;
}
function parse_cell_storage(buf, sst, rsst) {
switch (buf[0]) {
2022-03-21 01:39:16 +00:00
case 0:
case 1:
case 2:
case 3:
2022-03-21 01:39:16 +00:00
return parse_old_storage(buf, sst, rsst, buf[0]);
case 5:
return parse_storage(buf, sst, rsst);
default:
throw new Error("Unsupported payload version ".concat(buf[0]));
}
}
function parse_TSP_Reference(buf) {
var pb = parse_shallow(buf);
return parse_varint49(pb[1][0].data);
}
function parse_TST_TableDataList(M, root) {
var pb = parse_shallow(root.data);
var type = varint_to_i32(pb[1][0].data);
var entries = pb[3];
var data = [];
(entries || []).forEach(function(entry) {
var le = parse_shallow(entry.data);
var key = varint_to_i32(le[1][0].data) >>> 0;
switch (type) {
case 1:
data[key] = u8str(le[3][0].data);
break;
case 8:
{
var rt = M[parse_TSP_Reference(le[9][0].data)][0];
var rtp = parse_shallow(rt.data);
var rtpref = M[parse_TSP_Reference(rtp[1][0].data)][0];
var mtype = varint_to_i32(rtpref.meta[1][0].data);
if (mtype != 2001)
throw new Error("2000 unexpected reference to ".concat(mtype));
var tswpsa = parse_shallow(rtpref.data);
data[key] = tswpsa[3].map(function(x) {
return u8str(x.data);
}).join("");
}
break;
}
});
return data;
}
2022-03-21 01:39:16 +00:00
function parse_TST_TileRowInfo(u8, type) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
var pb = parse_shallow(u8);
var R = varint_to_i32(pb[1][0].data) >>> 0;
2022-03-21 01:39:16 +00:00
var cnt = varint_to_i32(pb[2][0].data) >>> 0;
var wide_offsets = ((_b = (_a = pb[8]) == null ? void 0 : _a[0]) == null ? void 0 : _b.data) && varint_to_i32(pb[8][0].data) > 0 || false;
var used_storage_u8, used_storage;
if (((_d = (_c = pb[7]) == null ? void 0 : _c[0]) == null ? void 0 : _d.data) && type != 0) {
used_storage_u8 = (_f = (_e = pb[7]) == null ? void 0 : _e[0]) == null ? void 0 : _f.data;
used_storage = (_h = (_g = pb[6]) == null ? void 0 : _g[0]) == null ? void 0 : _h.data;
} else if (((_j = (_i = pb[4]) == null ? void 0 : _i[0]) == null ? void 0 : _j.data) && type != 1) {
used_storage_u8 = (_l = (_k = pb[4]) == null ? void 0 : _k[0]) == null ? void 0 : _l.data;
used_storage = (_n = (_m = pb[3]) == null ? void 0 : _m[0]) == null ? void 0 : _n.data;
} else
throw "NUMBERS Tile missing ".concat(type, " cell storage");
var width = wide_offsets ? 4 : 1;
2022-03-21 01:39:16 +00:00
var used_storage_offsets = u8_to_dataview(used_storage_u8);
var offsets = [];
for (var C = 0; C < used_storage_u8.length / 2; ++C) {
var off = used_storage_offsets.getUint16(C * 2, true);
if (off < 65535)
offsets.push([C, off]);
2022-01-29 02:29:34 +00:00
}
2022-03-21 01:39:16 +00:00
if (offsets.length != cnt)
throw "Expected ".concat(cnt, " cells, found ").concat(offsets.length);
var cells = [];
for (C = 0; C < offsets.length - 1; ++C)
cells[offsets[C][0]] = used_storage.subarray(offsets[C][1] * width, offsets[C + 1][1] * width);
cells[offsets[offsets.length - 1][0]] = used_storage.subarray(offsets[offsets.length - 1][1] * width);
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
2022-03-21 01:39:16 +00:00
var _a;
var pb = parse_shallow(root.data);
2022-03-21 01:39:16 +00:00
var storage = ((_a = pb == null ? void 0 : pb[7]) == null ? void 0 : _a[0]) ? varint_to_i32(pb[7][0].data) >>> 0 > 0 ? 1 : 0 : -1;
var ri = mappa(pb[5], function(u8) {
return parse_TST_TileRowInfo(u8, storage);
});
return {
nrows: varint_to_i32(pb[4][0].data) >>> 0,
data: ri.reduce(function(acc, x) {
if (!acc[x.R])
acc[x.R] = [];
x.cells.forEach(function(cell, C) {
if (acc[x.R][C])
throw new Error("Duplicate cell r=".concat(x.R, " c=").concat(C));
acc[x.R][C] = cell;
});
return acc;
}, [])
};
}
function parse_TST_TableModelArchive(M, root, ws) {
var _a;
var pb = parse_shallow(root.data);
var range = { s: { r: 0, c: 0 }, e: { r: 0, c: 0 } };
range.e.r = (varint_to_i32(pb[6][0].data) >>> 0) - 1;
if (range.e.r < 0)
throw new Error("Invalid row varint ".concat(pb[6][0].data));
range.e.c = (varint_to_i32(pb[7][0].data) >>> 0) - 1;
if (range.e.c < 0)
throw new Error("Invalid col varint ".concat(pb[7][0].data));
ws["!ref"] = encode_range(range);
2022-03-21 01:39:16 +00:00
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_TSP_Reference(store[4][0].data)][0]);
var rsst = ((_a = store[17]) == null ? void 0 : _a[0]) ? parse_TST_TableDataList(M, M[parse_TSP_Reference(store[17][0].data)][0]) : [];
var tile = parse_shallow(store[3][0].data);
var _R = 0;
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_TSP_Reference(tl[2][0].data)][0];
var mtype = varint_to_i32(ref.meta[1][0].data);
if (mtype != 6002)
throw new Error("6001 unexpected reference to ".concat(mtype));
var _tile = parse_TST_Tile(M, ref);
_tile.data.forEach(function(row, R) {
row.forEach(function(buf, C) {
var addr = encode_cell({ r: _R + R, c: C });
var res = parse_cell_storage(buf, sst, rsst);
if (res)
ws[addr] = res;
});
2022-03-21 01:39:16 +00:00
});
_R += _tile.nrows;
});
}
function parse_TST_TableInfoArchive(M, root) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var tableref = M[parse_TSP_Reference(pb[2][0].data)];
var mtype = varint_to_i32(tableref[0].meta[1][0].data);
if (mtype != 6001)
throw new Error("6000 unexpected reference to ".concat(mtype));
parse_TST_TableModelArchive(M, tableref[0], out);
return out;
}
function parse_TN_SheetArchive(M, root) {
var _a;
var pb = parse_shallow(root.data);
var out = {
name: ((_a = pb[1]) == null ? void 0 : _a[0]) ? u8str(pb[1][0].data) : "",
sheets: []
};
var shapeoffs = mappa(pb[2], parse_TSP_Reference);
shapeoffs.forEach(function(off) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 6e3)
out.sheets.push(parse_TST_TableInfoArchive(M, m));
2022-01-29 02:29:34 +00:00
});
});
return out;
}
function parse_TN_DocumentArchive(M, root) {
var out = book_new();
var pb = parse_shallow(root.data);
var sheetoffs = mappa(pb[1], parse_TSP_Reference);
sheetoffs.forEach(function(off) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 2) {
var root2 = parse_TN_SheetArchive(M, m);
2022-03-21 01:39:16 +00:00
root2.sheets.forEach(function(sheet, idx) {
book_append_sheet(out, sheet, idx == 0 ? root2.name : root2.name + "_" + idx, true);
});
}
2022-01-29 02:29:34 +00:00
});
});
if (out.SheetNames.length == 0)
throw new Error("Empty NUMBERS file");
return out;
}
function parse_numbers_iwa(cfb) {
2022-03-21 01:39:16 +00:00
var _a, _b, _c, _d;
var out = {}, indices = [];
cfb.FullPaths.forEach(function(p) {
if (p.match(/\.iwpv2/))
throw new Error("Unsupported password protection");
});
cfb.FileIndex.forEach(function(s) {
if (!s.name.match(/\.iwa$/))
return;
var o;
try {
2022-03-21 01:39:16 +00:00
o = decompress_iwa_file(s.content);
} catch (e) {
return console.log("?? " + s.content.length + " " + (e.message || e));
}
var packets;
try {
packets = parse_iwa_file(o);
} catch (e) {
return console.log("## " + (e.message || e));
}
packets.forEach(function(packet) {
2022-03-21 01:39:16 +00:00
out[packet.id] = packet.messages;
indices.push(packet.id);
});
});
2022-03-21 01:39:16 +00:00
if (!indices.length)
throw new Error("File has no messages");
2022-03-21 01:39:16 +00:00
var docroot = ((_d = (_c = (_b = (_a = out == null ? void 0 : out[1]) == null ? void 0 : _a[0]) == null ? void 0 : _b.meta) == null ? void 0 : _c[1]) == null ? void 0 : _d[0].data) && varint_to_i32(out[1][0].meta[1][0].data) == 1 && out[1][0];
if (!docroot)
indices.forEach(function(idx) {
out[idx].forEach(function(iwam) {
var mtype = varint_to_i32(iwam.meta[1][0].data) >>> 0;
if (mtype == 1) {
if (!docroot)
docroot = iwam;
else
throw new Error("Document has multiple roots");
}
});
});
if (!docroot)
throw new Error("Cannot find Document root");
return parse_TN_DocumentArchive(out, docroot);
}