2017-03-22 07:50:11 +00:00
|
|
|
function firstbyte(f/*:RawData*/,o/*:?TypeOpts*/)/*:Array<number>*/ {
|
|
|
|
var x = "";
|
2015-04-02 20:32:22 +00:00
|
|
|
switch((o||{}).type || "base64") {
|
2017-03-22 07:50:11 +00:00
|
|
|
case 'buffer': return [f[0], f[1], f[2], f[3]];
|
|
|
|
case 'base64': x = Base64.decode(f.substr(0,24)); break;
|
|
|
|
case 'binary': x = f; break;
|
|
|
|
case 'array': return [f[0], f[1], f[2], f[3]];
|
2017-02-10 19:23:01 +00:00
|
|
|
default: throw new Error("Unrecognized type " + (o ? o.type : "undefined"));
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
2017-03-22 07:50:11 +00:00
|
|
|
return [x.charCodeAt(0), x.charCodeAt(1), x.charCodeAt(2), x.charCodeAt(3)];
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 01:29:38 +00:00
|
|
|
function read_cfb(cfb, opts/*:?ParseOpts*/)/*:Workbook*/ {
|
|
|
|
if(cfb.find("EncryptedPackage")) return parse_xlsxcfb(cfb, opts);
|
|
|
|
return parse_xlscfb(cfb, opts);
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function read_zip(data/*:RawData*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
|
|
|
|
/*:: if(!jszip) throw new Error("JSZip is not available"); */
|
2014-01-28 16:38:02 +00:00
|
|
|
var zip, d = data;
|
2014-05-16 00:33:34 +00:00
|
|
|
var o = opts||{};
|
2014-07-28 13:22:32 +00:00
|
|
|
if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
|
2014-05-16 00:33:34 +00:00
|
|
|
switch(o.type) {
|
2014-01-28 16:38:02 +00:00
|
|
|
case "base64": zip = new jszip(d, { base64:true }); break;
|
2015-04-02 20:32:22 +00:00
|
|
|
case "binary": case "array": zip = new jszip(d, { base64:false }); break;
|
2014-05-16 00:33:34 +00:00
|
|
|
case "buffer": zip = new jszip(d); break;
|
|
|
|
default: throw new Error("Unrecognized type " + o.type);
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
2014-05-16 00:33:34 +00:00
|
|
|
return parse_zip(zip, o);
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 06:02:02 +00:00
|
|
|
function read_utf16(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ {
|
|
|
|
var d = data;
|
|
|
|
if(o.type == 'base64') d = Base64.decode(d);
|
|
|
|
d = cptable.utils.decode(1200, d.slice(2));
|
|
|
|
o.type = "binary";
|
|
|
|
if(d.charCodeAt(0) == 0x3C) return parse_xlml(d,o);
|
|
|
|
return PRN.to_workbook(d, o);
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function readSync(data/*:RawData*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
|
2017-03-22 07:50:11 +00:00
|
|
|
var zip, d = data, n=[0];
|
2015-04-02 20:32:22 +00:00
|
|
|
var o = opts||{};
|
2017-04-30 16:27:03 +00:00
|
|
|
_ssfopts = {};
|
|
|
|
if(o.dateNF) _ssfopts.dateNF = o.dateNF;
|
2015-04-02 20:32:22 +00:00
|
|
|
if(!o.type) o.type = (has_buf && Buffer.isBuffer(data)) ? "buffer" : "base64";
|
2017-02-10 19:23:01 +00:00
|
|
|
if(o.type == "file") { o.type = "buffer"; d = _fs.readFileSync(data); }
|
2017-03-22 07:50:11 +00:00
|
|
|
switch((n = firstbyte(d, o))[0]) {
|
2017-04-13 01:29:38 +00:00
|
|
|
case 0xD0: return read_cfb(CFB.read(d, o), o);
|
2017-02-10 19:23:01 +00:00
|
|
|
case 0x09: return parse_xlscfb(s2a(o.type === 'base64' ? Base64.decode(d) : d), o);
|
2015-04-02 20:32:22 +00:00
|
|
|
case 0x3C: return parse_xlml(d, o);
|
2017-04-01 07:32:12 +00:00
|
|
|
case 0x49: if(n[1] == 0x44) return SYLK.to_workbook(d, o); break;
|
|
|
|
case 0x54: if(n[1] == 0x41 && n[2] == 0x42 && n[3] == 0x4C) return DIF.to_workbook(d, o); break;
|
2017-03-22 07:50:11 +00:00
|
|
|
case 0x50: if(n[1] == 0x4B && n[2] < 0x20 && n[3] < 0x20) return read_zip(d, o); break;
|
2017-05-11 07:29:59 +00:00
|
|
|
case 0xEF: return n[3] == 0x3C ? parse_xlml(d, o) : PRN.to_workbook(d,o);
|
2017-04-03 06:02:02 +00:00
|
|
|
case 0xFF: if(n[1] == 0xFE){ return read_utf16(d, o); } break;
|
2017-04-04 16:09:41 +00:00
|
|
|
case 0x00: if(n[1] == 0x00 && n[2] >= 0x02 && n[3] == 0x00) return WK_.to_workbook(d, o); break;
|
2017-03-28 04:41:01 +00:00
|
|
|
case 0x03: case 0x83: case 0x8B: return DBF.to_workbook(d, o);
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
2017-04-03 00:16:03 +00:00
|
|
|
if(n[2] <= 12 && n[3] <= 31) return DBF.to_workbook(d, o);
|
|
|
|
if(0x20>n[0]||n[0]>0x7F) throw new Error("Unsupported file " + n.join("|"));
|
|
|
|
return PRN.to_workbook(d, o);
|
2015-04-02 20:32:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function readFileSync(filename/*:string*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
|
2014-05-16 00:33:34 +00:00
|
|
|
var o = opts||{}; o.type = 'file';
|
2017-02-10 19:23:01 +00:00
|
|
|
return readSync(filename, o);
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|