very basic numbers iwa parser

This commit is contained in:
SheetJS 2022-01-28 21:29:34 -05:00
parent d01661565a
commit bb997654ef
30 changed files with 2845 additions and 3 deletions

View File

@ -4,6 +4,10 @@ This log is intended to keep track of backwards-incompatible changes, including
but not limited to API changes and file location changes. Minor behavioral
changes may not be included if they are not expected to break existing code.
## v0.18.0
* Browser scripts only expose `XLSX` variable
## v0.17.4
* CLI script moved to `xlsx-cli` package

View File

@ -53,6 +53,10 @@ bits/01_version.js: package.json
bits/18_cfb.js: node_modules/cfb/xlscfb.flow.js
cp $^ $@
bits/83_numbers.js: modules/83_numbers.js
cp $^ $@
.PHONY: clean
clean: ## Remove targets and build artifacts
rm -f $(TARGET) $(FLOWTARGET)

View File

@ -53,6 +53,12 @@ function getzipstr(zip, file/*:string*/, safe/*:?boolean*/)/*:?string*/ {
try { return getzipstr(zip, file); } catch(e) { return null; }
}
function getzipbin(zip, file/*:string*/, safe/*:?boolean*/)/*:any*/ {
if(!safe) return getdatabin(getzipfile(zip, file));
if(!file) return null;
try { return getzipbin(zip, file); } catch(e) { return null; }
}
function zipentries(zip) {
var k = zip.FullPaths || keys(zip.files), o = [];
for(var i = 0; i < k.length; ++i) if(k[i].slice(-1) != '/') o.push(k[i]);

507
bits/83_numbers.js Executable file
View File

@ -0,0 +1,507 @@
var NUMBERS = (function() {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = function(target) {
return __defProp(target, "__esModule", { value: true });
};
var __export = function(target, all) {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = function(target, module, copyDefault, desc) {
if (module && typeof module === "object" || typeof module === "function")
for (var keys = __getOwnPropNames(module), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: function(k) {
return module[k];
}.bind(null, key), enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ function(cache) {
return function(module, temp) {
return cache && cache.get(module) || (temp = __reExport(__markAsModule({}), module, 1), cache && cache.set(module, temp), temp);
};
}(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
// 83_numbers.ts
var numbers_exports = {};
__export(numbers_exports, {
parse_numbers: function() {
return numbers_default;
}
});
// src/util.ts
var u8_to_dataview = function(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
};
var u8str = function(u8) {
return new TextDecoder().decode(u8);
};
var u8concat = function(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;
});
return out;
};
// src/proto.ts
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;
}
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;
}
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) {
case 0:
{
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
}
break;
case 5:
len = 4;
case 1:
if (!len)
len = 8;
case 2:
if (!len)
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
case 4:
default:
throw new Error("PB Type ".concat(type, " for Field ").concat(num, " at offset ").concat(off));
}
var v = { offset: off, data: res };
if (out[num] == null)
out[num] = [v];
else
out[num].push(v);
}
return 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;
}
});
}
// src/frame.ts
function deframe(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(process_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);
}
function process_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;
}
}
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;
}
// src/iwa.ts
function parse_iwa(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;
}
// src/numbers.ts
var encode_col = function(C) {
var s = "";
for (++C; C; C = Math.floor((C - 1) / 26))
s = String.fromCharCode((C - 1) % 26 + 65) + s;
return s;
};
var encode_cell = function(c) {
return "".concat(encode_col(c.c)).concat(c.r + 1);
};
var encode_range = function(r) {
return encode_cell(r.s) + ":" + encode_cell(r.e);
};
var book_new = function() {
return { Sheets: {}, SheetNames: [] };
};
var book_append_sheet = function(wb, ws, name) {
if (!name) {
for (var i = 1; i < 9999; ++i)
if (wb.SheetNames.indexOf(name = "Sheet ".concat(i)) == -1)
break;
}
wb.SheetNames.push(name);
wb.Sheets[name] = ws;
};
function parse_numbers(cfb) {
var out = [];
cfb.FileIndex.forEach(function(s) {
if (!s.name.match(/\.iwa$/))
return;
var o;
try {
o = deframe(s.content);
} catch (e) {
return console.log("?? " + s.content.length + " " + (e.message || e));
}
var packets;
try {
packets = parse_iwa(o);
} catch (e) {
return console.log("## " + (e.message || e));
}
packets.forEach(function(packet) {
out[+packet.id] = packet.messages;
});
});
if (!out.length)
throw new Error("File has no messages");
var docroot;
out.forEach(function(iwams) {
iwams.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_docroot(out, docroot);
}
var numbers_default = parse_numbers;
function parse_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 entries = pb[3];
var data = [];
entries == null ? void 0 : entries.forEach(function(entry) {
var le = parse_shallow(entry.data);
var key = varint_to_i32(le[1][0].data) >>> 0;
data[key] = u8str(le[3][0].data);
});
return data;
}
function parse_TST_TileRowInfo(u8) {
var pb = parse_shallow(u8);
var R = varint_to_i32(pb[1][0].data) >>> 0;
var storage = pb[3][0].data;
var offsets = u8_to_dataview(pb[4][0].data);
var cells = [];
for (var C = 0; C < offsets.byteLength / 2; ++C) {
var off = offsets.getUint16(C * 2, true);
if (off > storage.length)
continue;
cells[C] = storage.subarray(off, offsets.getUint16(C * 2 + 2, true));
}
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
var pb = parse_shallow(root.data);
var ri = mappa(pb[5], parse_TST_TileRowInfo);
return 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 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);
{
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_Reference(store[4][0].data)][0]);
{
var tile = parse_shallow(store[3][0].data);
var tiles = [];
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_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));
tiles.push({ id: varint_to_i32(tl[1][0].data), ref: parse_TST_Tile(M, ref) });
});
tiles.forEach(function(tile2) {
tile2.ref.forEach(function(row, R) {
row.forEach(function(buf, C) {
var dv = u8_to_dataview(buf);
var ctype = buf[2];
var addr = encode_cell({ r: R, c: C });
switch (ctype) {
case 0:
{
switch (buf[1]) {
case 3:
ws[addr] = { t: "s", v: sst[dv.getUint32(buf.length - 4, true)] };
break;
case 2:
ws[addr] = { t: "n", v: dv.getFloat64(16, true) };
break;
case 0:
break;
case 5:
break;
case 7:
break;
case 6:
ws[addr] = { t: "b", v: dv.getFloat64(buf.length - 8, true) > 0 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
}
}
break;
case 3:
{
ws[addr] = { t: "s", v: sst[dv.getUint32(16, true)] };
}
break;
case 2:
{
ws[addr] = { t: "n", v: dv.getFloat64(buf.length - 12, true) };
}
break;
case 6:
{
ws[addr] = { t: "b", v: dv.getFloat64(16, true) > 0 };
}
break;
default:
throw new Error("Unsupported cell type ".concat(ctype));
}
});
});
});
}
}
}
function parse_TST_TableInfoArchive(M, root) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var tableref = M[parse_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_sheetroot(M, root) {
var _a;
var pb = parse_shallow(root.data);
var out = [{ "!ref": "A1" }, ((_a = pb[1]) == null ? void 0 : _a[0]) ? u8str(pb[1][0].data) : ""];
var shapeoffs = mappa(pb[2], parse_Reference);
var seen = false;
shapeoffs.forEach(function(off) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 6e3) {
if (seen)
return;
out[0] = parse_TST_TableInfoArchive(M, m);
seen = true;
}
});
});
return out;
}
function parse_docroot(M, root) {
var out = book_new();
var pb = parse_shallow(root.data);
var sheetoffs = mappa(pb[1], parse_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_sheetroot(M, m);
book_append_sheet(out, root2[0], root2[1]);
}
});
});
if (out.SheetNames.length == 0)
throw new Error("Empty NUMBERS file");
return out;
}
return __toCommonJS(numbers_exports);
})();
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */

View File

@ -60,7 +60,16 @@ function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
/* UOC */
if(safegetzipfile(zip, 'objectdata.xml')) return parse_ods(zip, opts);
/* Numbers */
if(safegetzipfile(zip, 'Index/Document.iwa')) throw new Error('Unsupported NUMBERS file');
if(safegetzipfile(zip, 'Index/Document.iwa')) {
if(typeof Uint8Array == "undefined") throw new Error('NUMBERS file parsing requires Uint8Array support')
if(typeof NUMBERS != "undefined") {
if(zip.FileIndex) return NUMBERS.parse_numbers(zip);
var _zip = CFB.utils.cfb_new();
zipentries(zip).forEach(function(e) { zip_add_file(_zip, e, getzipbin(zip, e)); });
return NUMBERS.parse_numbers(_zip);
}
throw new Error('Unsupported NUMBERS file');
}
if(!safegetzipfile(zip, '[Content_Types].xml')) {
if(safegetzipfile(zip, 'index.xml.gz')) throw new Error('Unsupported NUMBERS 08 file');
if(safegetzipfile(zip, 'index.xml')) throw new Error('Unsupported NUMBERS 09 file');

View File

@ -7,5 +7,3 @@ else if(typeof define === 'function' && define.amd) define('xlsx', function() {
else make_xlsx_lib(XLSX);
/* NOTE: the following extra line is needed for "Lightning Locker Service" */
if(typeof window !== 'undefined' && !window.XLSX) try { window.XLSX = XLSX; } catch(e) {}
/*exported XLS, ODS */
var XLS = XLSX, ODS = XLSX;

10
modules/.eslintrc Normal file
View File

@ -0,0 +1,10 @@
{
"parser": "@typescript-eslint/parser",
"env": { "shared-node-browser":true },
"plugins": ["@typescript-eslint"],
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"no-var": 0,
"semi": [ 2, "always" ]
}
}

2
modules/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
test_files
numbers_to_csv.node.js

507
modules/83_numbers.js Normal file
View File

@ -0,0 +1,507 @@
var NUMBERS = (function() {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = function(target) {
return __defProp(target, "__esModule", { value: true });
};
var __export = function(target, all) {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = function(target, module, copyDefault, desc) {
if (module && typeof module === "object" || typeof module === "function")
for (var keys = __getOwnPropNames(module), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
__defProp(target, key, { get: function(k) {
return module[k];
}.bind(null, key), enumerable: !(desc = __getOwnPropDesc(module, key)) || desc.enumerable });
}
return target;
};
var __toCommonJS = /* @__PURE__ */ function(cache) {
return function(module, temp) {
return cache && cache.get(module) || (temp = __reExport(__markAsModule({}), module, 1), cache && cache.set(module, temp), temp);
};
}(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
// 83_numbers.ts
var numbers_exports = {};
__export(numbers_exports, {
parse_numbers: function() {
return numbers_default;
}
});
// src/util.ts
var u8_to_dataview = function(array) {
return new DataView(array.buffer, array.byteOffset, array.byteLength);
};
var u8str = function(u8) {
return new TextDecoder().decode(u8);
};
var u8concat = function(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;
});
return out;
};
// src/proto.ts
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;
}
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;
}
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) {
case 0:
{
var l = ptr[0];
while (buf[ptr[0]++] >= 128)
;
res = buf.slice(l, ptr[0]);
}
break;
case 5:
len = 4;
case 1:
if (!len)
len = 8;
case 2:
if (!len)
len = parse_varint49(buf, ptr);
res = buf.slice(ptr[0], ptr[0] + len);
ptr[0] += len;
break;
case 3:
case 4:
default:
throw new Error("PB Type ".concat(type, " for Field ").concat(num, " at offset ").concat(off));
}
var v = { offset: off, data: res };
if (out[num] == null)
out[num] = [v];
else
out[num].push(v);
}
return 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;
}
});
}
// src/frame.ts
function deframe(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(process_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);
}
function process_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;
}
}
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;
}
// src/iwa.ts
function parse_iwa(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;
}
// src/numbers.ts
var encode_col = function(C) {
var s = "";
for (++C; C; C = Math.floor((C - 1) / 26))
s = String.fromCharCode((C - 1) % 26 + 65) + s;
return s;
};
var encode_cell = function(c) {
return "".concat(encode_col(c.c)).concat(c.r + 1);
};
var encode_range = function(r) {
return encode_cell(r.s) + ":" + encode_cell(r.e);
};
var book_new = function() {
return { Sheets: {}, SheetNames: [] };
};
var book_append_sheet = function(wb, ws, name) {
if (!name) {
for (var i = 1; i < 9999; ++i)
if (wb.SheetNames.indexOf(name = "Sheet ".concat(i)) == -1)
break;
}
wb.SheetNames.push(name);
wb.Sheets[name] = ws;
};
function parse_numbers(cfb) {
var out = [];
cfb.FileIndex.forEach(function(s) {
if (!s.name.match(/\.iwa$/))
return;
var o;
try {
o = deframe(s.content);
} catch (e) {
return console.log("?? " + s.content.length + " " + (e.message || e));
}
var packets;
try {
packets = parse_iwa(o);
} catch (e) {
return console.log("## " + (e.message || e));
}
packets.forEach(function(packet) {
out[+packet.id] = packet.messages;
});
});
if (!out.length)
throw new Error("File has no messages");
var docroot;
out.forEach(function(iwams) {
iwams.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_docroot(out, docroot);
}
var numbers_default = parse_numbers;
function parse_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 entries = pb[3];
var data = [];
entries == null ? void 0 : entries.forEach(function(entry) {
var le = parse_shallow(entry.data);
var key = varint_to_i32(le[1][0].data) >>> 0;
data[key] = u8str(le[3][0].data);
});
return data;
}
function parse_TST_TileRowInfo(u8) {
var pb = parse_shallow(u8);
var R = varint_to_i32(pb[1][0].data) >>> 0;
var storage = pb[3][0].data;
var offsets = u8_to_dataview(pb[4][0].data);
var cells = [];
for (var C = 0; C < offsets.byteLength / 2; ++C) {
var off = offsets.getUint16(C * 2, true);
if (off > storage.length)
continue;
cells[C] = storage.subarray(off, offsets.getUint16(C * 2 + 2, true));
}
return { R: R, cells: cells };
}
function parse_TST_Tile(M, root) {
var pb = parse_shallow(root.data);
var ri = mappa(pb[5], parse_TST_TileRowInfo);
return 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 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);
{
var store = parse_shallow(pb[4][0].data);
var sst = parse_TST_TableDataList(M, M[parse_Reference(store[4][0].data)][0]);
{
var tile = parse_shallow(store[3][0].data);
var tiles = [];
tile[1].forEach(function(t) {
var tl = parse_shallow(t.data);
var ref = M[parse_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));
tiles.push({ id: varint_to_i32(tl[1][0].data), ref: parse_TST_Tile(M, ref) });
});
tiles.forEach(function(tile2) {
tile2.ref.forEach(function(row, R) {
row.forEach(function(buf, C) {
var dv = u8_to_dataview(buf);
var ctype = buf[2];
var addr = encode_cell({ r: R, c: C });
switch (ctype) {
case 0:
{
switch (buf[1]) {
case 3:
ws[addr] = { t: "s", v: sst[dv.getUint32(buf.length - 4, true)] };
break;
case 2:
ws[addr] = { t: "n", v: dv.getFloat64(16, true) };
break;
case 0:
break;
case 5:
break;
case 7:
break;
case 6:
ws[addr] = { t: "b", v: dv.getFloat64(buf.length - 8, true) > 0 };
break;
default:
throw new Error("Unsupported cell type ".concat(buf.slice(0, 4)));
}
}
break;
case 3:
{
ws[addr] = { t: "s", v: sst[dv.getUint32(16, true)] };
}
break;
case 2:
{
ws[addr] = { t: "n", v: dv.getFloat64(buf.length - 12, true) };
}
break;
case 6:
{
ws[addr] = { t: "b", v: dv.getFloat64(16, true) > 0 };
}
break;
default:
throw new Error("Unsupported cell type ".concat(ctype));
}
});
});
});
}
}
}
function parse_TST_TableInfoArchive(M, root) {
var pb = parse_shallow(root.data);
var out = { "!ref": "A1" };
var tableref = M[parse_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_sheetroot(M, root) {
var _a;
var pb = parse_shallow(root.data);
var out = [{ "!ref": "A1" }, ((_a = pb[1]) == null ? void 0 : _a[0]) ? u8str(pb[1][0].data) : ""];
var shapeoffs = mappa(pb[2], parse_Reference);
var seen = false;
shapeoffs.forEach(function(off) {
M[off].forEach(function(m) {
var mtype = varint_to_i32(m.meta[1][0].data);
if (mtype == 6e3) {
if (seen)
return;
out[0] = parse_TST_TableInfoArchive(M, m);
seen = true;
}
});
});
return out;
}
function parse_docroot(M, root) {
var out = book_new();
var pb = parse_shallow(root.data);
var sheetoffs = mappa(pb[1], parse_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_sheetroot(M, m);
book_append_sheet(out, root2[0], root2[1]);
}
});
});
if (out.SheetNames.length == 0)
throw new Error("Empty NUMBERS file");
return out;
}
return __toCommonJS(numbers_exports);
})();
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */

4
modules/83_numbers.ts Normal file
View File

@ -0,0 +1,4 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
import parse_numbers from './src/numbers';
export { parse_numbers };

20
modules/Makefile Normal file
View File

@ -0,0 +1,20 @@
LIBFILES=$(wildcard src/*.ts)
TSFILES=$(wildcard *.ts)
ENTRIES=$(subst .ts,.js,$(TSFILES))
.PHONY: all
all: $(ENTRIES)
83_numbers.js: 83_numbers.ts $(LIBFILES)
npx esbuild $< --bundle --outfile=$@ --platform=browser --format=iife --global-name=NUMBERS --target=es5
%.node.js: %.node.ts $(LIBFILES)
npx esbuild $< --bundle --external:xlsx --outfile=$@ --minify --platform=node
sed -i '' 's/ts-node/node/g' $@
%.js: %.ts $(LIBFILES)
npx esbuild $< --bundle --outfile=$@ --platform=browser --format=iife --global-name=$* --target=es5
.PHONY: clean
clean:
rm $(ENTRIES)

View File

@ -0,0 +1,13 @@
#!/usr/bin/env ts-node
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
import { read } from 'cfb';
import { utils } from 'xlsx';
import parse_numbers from './src/numbers';
var f = process.argv[2];
var cfb = read(f, {type: "file"});
var wb = parse_numbers(cfb);
var sn = process.argv[3];
if(sn && !isNaN(+sn)) sn = wb.SheetNames[+sn];
if(wb.SheetNames.indexOf(sn) == -1) sn = wb.SheetNames[0];
console.log(utils.sheet_to_csv(wb.Sheets[sn]));

5
modules/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"devDependencies": {
"esbuild": "0.14.14"
}
}

78
modules/src/frame.ts Normal file
View File

@ -0,0 +1,78 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
import { Ptr, parse_varint49 } from './proto';
import { u8concat } from './util';
function is_framed(buf: Uint8Array): boolean {
var l = 0;
while(l < buf.length) {
l++;
var len = buf[l] | (buf[l+1]<<8) | (buf[l+2] << 16); l += 3;
l += len;
}
return l == buf.length;
}
export { is_framed };
function deframe(buf: Uint8Array): Uint8Array {
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(process_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);
}
export { deframe };
function process_chunk(type: number, buf: Uint8Array): Uint8Array {
if(type != 0) throw new Error(`Unexpected Snappy chunk type ${type}`);
var ptr: Ptr = [0];
var usz = parse_varint49(buf, ptr);
var chunks = [];
while(ptr[0] < buf.length) {
var tag = buf[ptr[0]] & 0x3;
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) & 0x7) + 4;
offset = (buf[ptr[0]++] & 0xE0) << 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;
}
}
chunks.push(chunks[0].slice(-offset, -offset + length));
}
}
var o = u8concat(chunks);
if(o.length != usz) throw new Error(`Unexpected length: ${o.length} != ${usz}`);
return o;
}

40
modules/src/iwa.ts Normal file
View File

@ -0,0 +1,40 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
import { Ptr, ProtoMessage, parse_shallow, parse_varint49, varint_to_i32 } from './proto';
interface IWAMessage {
/** Metadata in .TSP.MessageInfo */
meta: ProtoMessage;
data: Uint8Array;
}
interface IWAArchiveInfo {
id?: number;
messages?: IWAMessage[];
}
export { IWAMessage, IWAArchiveInfo };
function parse_iwa(buf: Uint8Array): IWAArchiveInfo[] {
var out: IWAArchiveInfo[] = [], ptr: Ptr = [0];
while(ptr[0] < buf.length) {
/* .TSP.ArchiveInfo */
var len = parse_varint49(buf, ptr);
var ai = parse_shallow(buf.slice(ptr[0], ptr[0] + len));
ptr[0] += len;
var res: IWAArchiveInfo = {
id: varint_to_i32(ai[1][0].data),
messages: []
};
ai[2].forEach(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;
}
export { parse_iwa };

213
modules/src/numbers.ts Normal file
View File

@ -0,0 +1,213 @@
/*! sheetjs (C) 2013-present SheetJS -- http://sheetjs.com */
import { CFB$Container } from 'cfb';
import { WorkBook, WorkSheet, CellAddress, Range, CellObject } from '../../';
import { u8str, u8_to_dataview } from './util';
import { parse_shallow, varint_to_i32, parse_varint49, mappa } from './proto';
import { deframe } from './frame';
import { IWAArchiveInfo, IWAMessage, parse_iwa } from './iwa';
/* written here to avoid a full import of the 'xlsx' library */
var encode_col = (C: number): string => {
var s="";
for(++C; C; C=Math.floor((C-1)/26)) s = String.fromCharCode(((C-1)%26) + 65) + s;
return s;
};
var encode_cell = (c: CellAddress): string => `${encode_col(c.c)}${c.r+1}`;
var encode_range = (r: Range): string => encode_cell(r.s) + ":" + encode_cell(r.e);
var book_new = (): WorkBook => ({Sheets:{}, SheetNames:[]});
var book_append_sheet = (wb: WorkBook, ws: WorkSheet, name?: string): void => {
if(!name) for(var i = 1; i < 9999; ++i) if(wb.SheetNames.indexOf(name = `Sheet ${i}`) == -1) break;
wb.SheetNames.push(name); wb.Sheets[name] = ws;
};
function parse_numbers(cfb: CFB$Container): WorkBook {
var out: IWAMessage[][] = [];
/* collect entire message space */
cfb.FileIndex.forEach(s => {
if(!s.name.match(/\.iwa$/)) return;
var o: Uint8Array;
try { o = deframe(s.content as Uint8Array); } catch(e) { return console.log("?? " + s.content.length + " " + (e.message || e)); }
var packets: IWAArchiveInfo[];
try { packets = parse_iwa(o); } catch(e) { return console.log("## " + (e.message || e)); }
packets.forEach(packet => {out[+packet.id] = packet.messages;});
});
if(!out.length) throw new Error("File has no messages");
/* find document root */
var docroot: IWAMessage;
out.forEach((iwams) => {
iwams.forEach((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_docroot(out, docroot);
}
export default parse_numbers;
// .TSP.Reference
function parse_Reference(buf: Uint8Array): number {
var pb = parse_shallow(buf);
return parse_varint49(pb[1][0].data);
}
// .TST.TableDataList
function parse_TST_TableDataList(M: IWAMessage[][], root: IWAMessage): string[] {
var pb = parse_shallow(root.data);
var entries = pb[3];
var data = [];
entries?.forEach(entry => {