| 
									
										
										
										
											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") { | 
					
						
							| 
									
										
										
										
											2020-06-30 18:55:42 +00:00
										 |  |  | 		case 'buffer': return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]]; | 
					
						
							| 
									
										
										
										
											2022-03-22 20:08:08 +00:00
										 |  |  | 		case 'base64': x = Base64_decode(f.slice(0,12)); break; | 
					
						
							| 
									
										
										
										
											2017-03-22 07:50:11 +00:00
										 |  |  | 		case 'binary': x = f; break; | 
					
						
							| 
									
										
										
										
											2020-06-30 18:55:42 +00:00
										 |  |  | 		case 'array':  return [f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]]; | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | 		default: throw new Error("Unrecognized type " + (o && o.type || "undefined")); | 
					
						
							| 
									
										
										
										
											2015-04-02 20:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-30 18:55:42 +00:00
										 |  |  | 	return [x.charCodeAt(0), x.charCodeAt(1), x.charCodeAt(2), x.charCodeAt(3), x.charCodeAt(4), x.charCodeAt(5), x.charCodeAt(6), x.charCodeAt(7)]; | 
					
						
							| 
									
										
										
										
											2015-04-02 20:32:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-09 22:38:23 +00:00
										 |  |  | function read_cfb(cfb/*:CFBContainer*/, opts/*:?ParseOpts*/)/*:Workbook*/ { | 
					
						
							|  |  |  | 	if(CFB.find(cfb, "EncryptedPackage")) return parse_xlsxcfb(cfb, opts); | 
					
						
							| 
									
										
										
										
											2017-04-13 01:29:38 +00:00
										 |  |  | 	return parse_xlscfb(cfb, opts); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | function read_zip(data/*:RawData*/, opts/*:?ParseOpts*/)/*:Workbook*/ { | 
					
						
							| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2019-08-04 19:50:49 +00:00
										 |  |  | 	zip = zip_read(d, o); | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 	return parse_zip(zip, o); | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | function read_plaintext(data/*:string*/, o/*:ParseOpts*/)/*:Workbook*/ { | 
					
						
							|  |  |  | 	var i = 0; | 
					
						
							|  |  |  | 	main: while(i < data.length) switch(data.charCodeAt(i)) { | 
					
						
							|  |  |  | 		case 0x0A: case 0x0D: case 0x20: ++i; break; | 
					
						
							|  |  |  | 		case 0x3C: return parse_xlml(data.slice(i),o); | 
					
						
							|  |  |  | 		default: break main; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return PRN.to_workbook(data, o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function read_plaintext_raw(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ { | 
					
						
							|  |  |  | 	var str = "", bytes = firstbyte(data, o); | 
					
						
							|  |  |  | 	switch(o.type) { | 
					
						
							| 
									
										
										
										
											2022-03-22 20:08:08 +00:00
										 |  |  | 		case 'base64': str = Base64_decode(data); break; | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | 		case 'binary': str = data; break; | 
					
						
							|  |  |  | 		case 'buffer': str = data.toString('binary'); break; | 
					
						
							|  |  |  | 		case 'array': str = cc2str(data); break; | 
					
						
							|  |  |  | 		default: throw new Error("Unrecognized type " + o.type); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) str = utf8read(str); | 
					
						
							| 
									
										
										
										
											2022-02-26 04:32:40 +00:00
										 |  |  | 	o.type = "binary"; | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | 	return read_plaintext(str, o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 06:02:02 +00:00
										 |  |  | function read_utf16(data/*:RawData*/, o/*:ParseOpts*/)/*:Workbook*/ { | 
					
						
							|  |  |  | 	var d = data; | 
					
						
							| 
									
										
										
										
											2022-03-22 20:08:08 +00:00
										 |  |  | 	if(o.type == 'base64') d = Base64_decode(d); | 
					
						
							| 
									
										
										
										
											2023-03-15 08:17:09 +00:00
										 |  |  | 	if(typeof ArrayBuffer !== "undefined" && data instanceof ArrayBuffer) d = new Uint8Array(data); | 
					
						
							|  |  |  | 	d = typeof $cptable !== "undefined" ? $cptable.utils.decode(1200, d.slice(2), 'str') : ( | 
					
						
							|  |  |  | 		(has_buf && Buffer.isBuffer(data)) ? data.slice(2).toString("utf16le") : | 
					
						
							|  |  |  | 		(typeof Uint8Array !== "undefined" && d instanceof Uint8Array) ? ( | 
					
						
							|  |  |  | 			typeof TextDecoder !== "undefined" ? new TextDecoder("utf-16le").decode(d.slice(2)) : utf16lereadu(d.slice(2)) | 
					
						
							|  |  |  | 		) : utf16leread(d.slice(2)) | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2017-04-03 06:02:02 +00:00
										 |  |  | 	o.type = "binary"; | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | 	return read_plaintext(d, o); | 
					
						
							| 
									
										
										
										
											2017-04-03 06:02:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-30 06:18:11 +00:00
										 |  |  | function bstrify(data/*:string*/)/*:string*/ { | 
					
						
							|  |  |  | 	return !data.match(/[^\x00-\x7F]/) ? data : utf8write(data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function read_prn(data, d, o, str) { | 
					
						
							|  |  |  | 	if(str) { o.type = "string"; return PRN.to_workbook(data, o); } | 
					
						
							|  |  |  | 	return PRN.to_workbook(d, o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-10 19:23:01 +00:00
										 |  |  | function readSync(data/*:RawData*/, opts/*:?ParseOpts*/)/*:Workbook*/ { | 
					
						
							| 
									
										
										
										
											2017-12-30 05:40:35 +00:00
										 |  |  | 	reset_cp(); | 
					
						
							| 
									
										
										
										
											2015-04-02 20:32:22 +00:00
										 |  |  | 	var o = opts||{}; | 
					
						
							| 
									
										
										
										
											2022-06-22 19:58:22 +00:00
										 |  |  | 	if(o.codepage && typeof $cptable === "undefined") console.error("Codepage tables are not loaded.  Non-ASCII characters may not give expected results"); | 
					
						
							| 
									
										
										
										
											2021-09-30 07:28:03 +00:00
										 |  |  | 	if(typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) return readSync(new Uint8Array(data), (o = dup(o), o.type = "array", o)); | 
					
						
							| 
									
										
										
										
											2024-07-04 19:54:34 +00:00
										 |  |  | 	/* Javet projects `byte[]` to `Int8Array` */ | 
					
						
							|  |  |  | 	if(typeof Int8Array !== 'undefined' && data instanceof Int8Array) return readSync(new Uint8Array(data.buffer, data.byteOffset, data.length), o); | 
					
						
							| 
									
										
										
										
											2022-02-13 09:35:34 +00:00
										 |  |  | 	if(typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && !o.type) o.type = typeof Deno !== "undefined" ? "buffer" : "array"; | 
					
						
							| 
									
										
										
										
											2021-09-30 07:28:03 +00:00
										 |  |  | 	var d = data, n = [0,0,0,0], str = false; | 
					
						
							| 
									
										
										
										
											2020-03-15 07:42:05 +00:00
										 |  |  | 	if(o.cellStyles) { o.cellNF = true; o.sheetStubs = true; } | 
					
						
							| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2022-02-12 06:31:47 +00:00
										 |  |  | 	if(o.type == "file") { o.type = has_buf ? "buffer" : "binary"; d = read_binary(data); if(typeof Uint8Array !== 'undefined' && !has_buf) o.type = "array"; } | 
					
						
							| 
									
										
										
										
											2018-05-20 01:34:59 +00:00
										 |  |  | 	if(o.type == "string") { str = true; o.type = "binary"; o.codepage = 65001; d = bstrify(data); } | 
					
						
							| 
									
										
										
										
											2018-01-23 09:07:51 +00:00
										 |  |  | 	if(o.type == 'array' && typeof Uint8Array !== 'undefined' && data instanceof Uint8Array && typeof ArrayBuffer !== 'undefined') { | 
					
						
							| 
									
										
										
										
											2018-01-11 08:01:25 +00:00
										 |  |  | 		// $FlowIgnore
 | 
					
						
							|  |  |  | 		var ab=new ArrayBuffer(3), vu=new Uint8Array(ab); vu.foo="bar"; | 
					
						
							|  |  |  | 		// $FlowIgnore
 | 
					
						
							|  |  |  | 		if(!vu.foo) {o=dup(o); o.type='array'; return readSync(ab2a(d), o);} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-03-22 07:50:11 +00:00
										 |  |  | 	switch((n = firstbyte(d, o))[0]) { | 
					
						
							| 
									
										
										
										
											2020-06-30 18:55:42 +00:00
										 |  |  | 		case 0xD0: if(n[1] === 0xCF && n[2] === 0x11 && n[3] === 0xE0 && n[4] === 0xA1 && n[5] === 0xB1 && n[6] === 0x1A && n[7] === 0xE1) return read_cfb(CFB.read(d, o), o); break; | 
					
						
							| 
									
										
										
										
											2021-09-16 18:32:47 +00:00
										 |  |  | 		case 0x09: if(n[1] <= 0x08) return parse_xlscfb(d, o); break; | 
					
						
							| 
									
										
										
										
											2015-04-02 20:32:22 +00:00
										 |  |  | 		case 0x3C: return parse_xlml(d, o); | 
					
						
							| 
									
										
										
										
											2021-08-11 02:53:38 +00:00
										 |  |  | 		case 0x49: | 
					
						
							|  |  |  | 			if(n[1] === 0x49 && n[2] === 0x2a && n[3] === 0x00) throw new Error("TIFF Image File is not a spreadsheet"); | 
					
						
							|  |  |  | 			if(n[1] === 0x44) return read_wb_ID(d, o); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | 		case 0x54: if(n[1] === 0x41 && n[2] === 0x42 && n[3] === 0x4C) return DIF.to_workbook(d, o); break; | 
					
						
							| 
									
										
										
										
											2018-03-06 00:34:04 +00:00
										 |  |  | 		case 0x50: return (n[1] === 0x4B && n[2] < 0x09 && n[3] < 0x09) ? read_zip(d, o) : read_prn(data, d, o, str); | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | 		case 0xEF: return n[3] === 0x3C ? parse_xlml(d, o) : read_prn(data, d, o, str); | 
					
						
							| 
									
										
										
										
											2021-12-29 09:16:02 +00:00
										 |  |  | 		case 0xFF: | 
					
						
							|  |  |  | 			if(n[1] === 0xFE) { return read_utf16(d, o); } | 
					
						
							|  |  |  | 			else if(n[1] === 0x00 && n[2] === 0x02 && n[3] === 0x00) return WK_.to_workbook(d, o); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case 0x00: | 
					
						
							|  |  |  | 			if(n[1] === 0x00) { | 
					
						
							|  |  |  | 				if(n[2] >= 0x02 && n[3] === 0x00) return WK_.to_workbook(d, o); | 
					
						
							|  |  |  | 				if(n[2] === 0x00 && (n[3] === 0x08 || n[3] === 0x09)) return WK_.to_workbook(d, o); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2017-10-17 00:14:32 +00:00
										 |  |  | 		case 0x03: case 0x83: case 0x8B: case 0x8C: return DBF.to_workbook(d, o); | 
					
						
							| 
									
										
										
										
											2022-07-07 06:30:44 +00:00
										 |  |  | 		case 0x7B: if(n[1] === 0x5C && n[2] === 0x72 && n[3] === 0x74) return rtf_to_workbook(d, o); break; | 
					
						
							| 
									
										
										
										
											2017-07-26 08:35:28 +00:00
										 |  |  | 		case 0x0A: case 0x0D: case 0x20: return read_plaintext_raw(d, o); | 
					
						
							| 
									
										
										
										
											2021-12-29 09:16:02 +00:00
										 |  |  | 		case 0x89: if(n[1] === 0x50 && n[2] === 0x4E && n[3] === 0x47) throw new Error("PNG Image File is not a spreadsheet"); break; | 
					
						
							| 
									
										
										
										
											2022-05-05 06:27:25 +00:00
										 |  |  | 		case 0x08: if(n[1] === 0xE7) throw new Error("Unsupported Multiplan 1.x file!"); break; | 
					
						
							|  |  |  | 		case 0x0C: | 
					
						
							|  |  |  | 			if(n[1] === 0xEC) throw new Error("Unsupported Multiplan 2.x file!"); | 
					
						
							|  |  |  | 			if(n[1] === 0xED) throw new Error("Unsupported Multiplan 3.x file!"); | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2015-04-02 20:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-03-12 14:05:57 +00:00
										 |  |  | 	if(DBF_SUPPORTED_VERSIONS.indexOf(n[0]) > -1 && n[2] <= 12 && n[3] <= 31) return DBF.to_workbook(d, o); | 
					
						
							| 
									
										
										
										
											2017-09-30 06:18:11 +00:00
										 |  |  | 	return read_prn(data, d, o, str); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |