xls-reader 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/cli.mjs +232 -197
- package/dist/index.cjs +223 -187
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -21
- package/dist/index.d.mts +8 -21
- package/dist/index.mjs +223 -186
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
//#region src/types.ts
|
|
2
|
+
// src/types.ts
|
|
4
3
|
var CellError = class {
|
|
5
4
|
constructor(code) {
|
|
6
5
|
this.code = code;
|
|
@@ -10,8 +9,7 @@ var CellError = class {
|
|
|
10
9
|
}
|
|
11
10
|
};
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
//#region src/errors.ts
|
|
12
|
+
// src/errors.ts
|
|
15
13
|
var XlsError = class extends Error {
|
|
16
14
|
constructor(message) {
|
|
17
15
|
super(message);
|
|
@@ -19,8 +17,30 @@ var XlsError = class extends Error {
|
|
|
19
17
|
}
|
|
20
18
|
};
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
// src/biff/record-types.ts
|
|
21
|
+
const RecordType = {
|
|
22
|
+
BOF: 2057,
|
|
23
|
+
EOF: 10,
|
|
24
|
+
BOUNDSHEET8: 133,
|
|
25
|
+
DATEMODE: 34,
|
|
26
|
+
FORMAT: 1054,
|
|
27
|
+
XF: 224,
|
|
28
|
+
SST: 252,
|
|
29
|
+
CONTINUE: 60,
|
|
30
|
+
LABELSST: 253,
|
|
31
|
+
LABEL: 516,
|
|
32
|
+
RSTRING: 214,
|
|
33
|
+
NUMBER: 515,
|
|
34
|
+
RK: 638,
|
|
35
|
+
MULRK: 189,
|
|
36
|
+
BLANK: 513,
|
|
37
|
+
MULBLANK: 190,
|
|
38
|
+
BOOLERR: 517,
|
|
39
|
+
FORMULA: 6,
|
|
40
|
+
STRING: 519
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/biff/record-stream.ts
|
|
24
44
|
function readRecords(stream) {
|
|
25
45
|
const view = new DataView(stream.buffer, stream.byteOffset, stream.byteLength);
|
|
26
46
|
const records = [];
|
|
@@ -38,9 +58,26 @@ function readRecords(stream) {
|
|
|
38
58
|
}
|
|
39
59
|
return records;
|
|
40
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns the data of `records[index]` followed by the data of every CONTINUE
|
|
63
|
+
* record immediately after it, as ordered chunks for ContinuedReader. BIFF8
|
|
64
|
+
* caps a record at 8224 data bytes, so long payloads (the SST, a long formula
|
|
65
|
+
* STRING result) spill into CONTINUE records.
|
|
66
|
+
*
|
|
67
|
+
* @example collectContinuedChunks(records, sstIndex) // [sstData, continue1, ...]
|
|
68
|
+
*/
|
|
69
|
+
function collectContinuedChunks(records, index) {
|
|
70
|
+
const first = records[index];
|
|
71
|
+
const chunks = first ? [first.data] : [];
|
|
72
|
+
for (let j = index + 1; j < records.length; j++) {
|
|
73
|
+
const record = records[j];
|
|
74
|
+
if (record === void 0 || record.type !== RecordType.CONTINUE) break;
|
|
75
|
+
chunks.push(record.data);
|
|
76
|
+
}
|
|
77
|
+
return chunks;
|
|
78
|
+
}
|
|
41
79
|
|
|
42
|
-
|
|
43
|
-
//#region src/byte-reader.ts
|
|
80
|
+
// src/byte-reader.ts
|
|
44
81
|
var ByteReader = class {
|
|
45
82
|
constructor(bytes, start = 0) {
|
|
46
83
|
this.bytes = bytes;
|
|
@@ -97,8 +134,7 @@ var ByteReader = class {
|
|
|
97
134
|
}
|
|
98
135
|
};
|
|
99
136
|
|
|
100
|
-
|
|
101
|
-
//#region src/biff/error-code.ts
|
|
137
|
+
// src/biff/error-code.ts
|
|
102
138
|
const CODE_BY_BYTE = /* @__PURE__ */ new Map([
|
|
103
139
|
[0, "#NULL!"],
|
|
104
140
|
[7, "#DIV/0!"],
|
|
@@ -113,8 +149,7 @@ function errorFromByte(byte) {
|
|
|
113
149
|
return code === void 0 ? null : new CellError(code);
|
|
114
150
|
}
|
|
115
151
|
|
|
116
|
-
|
|
117
|
-
//#region src/biff/rk-number.ts
|
|
152
|
+
// src/biff/rk-number.ts
|
|
118
153
|
function decodeRk(rk) {
|
|
119
154
|
const dividedBy100 = (rk & 1) !== 0;
|
|
120
155
|
const value = (rk & 2) !== 0 ? rk >> 2 : rkAsDouble(rk);
|
|
@@ -122,13 +157,12 @@ function decodeRk(rk) {
|
|
|
122
157
|
}
|
|
123
158
|
function rkAsDouble(rk) {
|
|
124
159
|
const view = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8));
|
|
125
|
-
view.setUint32(4, rk &
|
|
160
|
+
view.setUint32(4, rk & 0xfffffffc, true);
|
|
126
161
|
return view.getFloat64(0, true);
|
|
127
162
|
}
|
|
128
163
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const F_WIDE$1 = 1;
|
|
164
|
+
// src/biff/unicode-string.ts
|
|
165
|
+
const F_WIDE$2 = 1;
|
|
132
166
|
/**
|
|
133
167
|
* Reads a self-contained BIFF8 unicode string (XLUnicodeString or
|
|
134
168
|
* ShortXLUnicodeString): a length, a flags byte, then the characters. Used for
|
|
@@ -144,14 +178,13 @@ const F_WIDE$1 = 1;
|
|
|
144
178
|
*/
|
|
145
179
|
function readUnicodeString(reader, shortLength) {
|
|
146
180
|
const charCount = shortLength ? reader.u8() : reader.u16();
|
|
147
|
-
const wide = (reader.u8() & F_WIDE$
|
|
181
|
+
const wide = (reader.u8() & F_WIDE$2) !== 0;
|
|
148
182
|
let text = "";
|
|
149
183
|
for (let i = 0; i < charCount; i++) text += String.fromCharCode(wide ? reader.u16() : reader.u8());
|
|
150
184
|
return text;
|
|
151
185
|
}
|
|
152
186
|
|
|
153
|
-
|
|
154
|
-
//#region src/biff/cell-records.ts
|
|
187
|
+
// src/biff/cell-records.ts
|
|
155
188
|
function decodeLabelSst(data, ctx) {
|
|
156
189
|
const reader = new ByteReader(data);
|
|
157
190
|
const { row, col } = readHead(reader);
|
|
@@ -284,32 +317,95 @@ function readHead(reader) {
|
|
|
284
317
|
};
|
|
285
318
|
}
|
|
286
319
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
320
|
+
// src/biff/continued-reader.ts
|
|
321
|
+
var ContinuedReader = class {
|
|
322
|
+
constructor(chunks) {
|
|
323
|
+
this.chunks = chunks;
|
|
324
|
+
this.chunkIndex = 0;
|
|
325
|
+
this.offset = 0;
|
|
326
|
+
}
|
|
327
|
+
get done() {
|
|
328
|
+
this.settle();
|
|
329
|
+
return this.chunkIndex >= this.chunks.length;
|
|
330
|
+
}
|
|
331
|
+
u8() {
|
|
332
|
+
this.settle();
|
|
333
|
+
return this.rawByte();
|
|
334
|
+
}
|
|
335
|
+
u16() {
|
|
336
|
+
return this.u8() | this.u8() << 8;
|
|
337
|
+
}
|
|
338
|
+
u32() {
|
|
339
|
+
return (this.u16() | this.u16() << 16) >>> 0;
|
|
340
|
+
}
|
|
341
|
+
readChars(count, wide) {
|
|
342
|
+
let text = "";
|
|
343
|
+
let isWide = wide;
|
|
344
|
+
for (let i = 0; i < count; i++) {
|
|
345
|
+
if (this.atChunkEnd()) isWide = (this.crossToNextChunk() & 1) !== 0;
|
|
346
|
+
text += String.fromCharCode(isWide ? this.rawByte() | this.rawByte() << 8 : this.rawByte());
|
|
347
|
+
}
|
|
348
|
+
return text;
|
|
349
|
+
}
|
|
350
|
+
skip(length) {
|
|
351
|
+
let left = length;
|
|
352
|
+
while (left > 0) {
|
|
353
|
+
this.settle();
|
|
354
|
+
const chunk = this.currentChunk();
|
|
355
|
+
const take = Math.min(left, chunk.length - this.offset);
|
|
356
|
+
this.offset += take;
|
|
357
|
+
left -= take;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
settle() {
|
|
361
|
+
while (this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength()) {
|
|
362
|
+
this.chunkIndex += 1;
|
|
363
|
+
this.offset = 0;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
atChunkEnd() {
|
|
367
|
+
return this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength();
|
|
368
|
+
}
|
|
369
|
+
crossToNextChunk() {
|
|
370
|
+
this.chunkIndex += 1;
|
|
371
|
+
this.offset = 0;
|
|
372
|
+
return this.rawByte();
|
|
373
|
+
}
|
|
374
|
+
rawByte() {
|
|
375
|
+
const value = this.currentChunk()[this.offset];
|
|
376
|
+
if (value === void 0) throw new XlsError(`Continued-record cursor past end of chunk ${this.chunkIndex} (offset ${this.offset})`);
|
|
377
|
+
this.offset += 1;
|
|
378
|
+
return value;
|
|
379
|
+
}
|
|
380
|
+
chunkLength() {
|
|
381
|
+
return this.currentChunk().length;
|
|
382
|
+
}
|
|
383
|
+
currentChunk() {
|
|
384
|
+
const chunk = this.chunks[this.chunkIndex];
|
|
385
|
+
if (chunk === void 0) throw new XlsError(`Continued-record reader ran past its ${this.chunks.length} chunk(s)`);
|
|
386
|
+
return chunk;
|
|
387
|
+
}
|
|
309
388
|
};
|
|
310
389
|
|
|
311
|
-
|
|
312
|
-
|
|
390
|
+
// src/biff/formula-string.ts
|
|
391
|
+
const F_WIDE$1 = 1;
|
|
392
|
+
/**
|
|
393
|
+
* Reads the cached string result of a FORMULA from its STRING record plus any
|
|
394
|
+
* CONTINUE records after it. A result longer than one record (8224 bytes) is
|
|
395
|
+
* split like an SST string: each CONTINUE starts with a fresh grbit that says
|
|
396
|
+
* whether the remaining characters are 8- or 16-bit. LibreOffice writes this
|
|
397
|
+
* for e.g. `=REPT("a";10000)`; xlrd handles it the same way.
|
|
398
|
+
*
|
|
399
|
+
* @example readFormulaString([stringData, continueData]) // "aaaa…" (10000 chars)
|
|
400
|
+
*/
|
|
401
|
+
function readFormulaString(chunks) {
|
|
402
|
+
const reader = new ContinuedReader(chunks);
|
|
403
|
+
const charCount = reader.u16();
|
|
404
|
+
const wide = (reader.u8() & F_WIDE$1) !== 0;
|
|
405
|
+
return reader.readChars(charCount, wide);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/biff/sheet-decoder.ts
|
|
313
409
|
const DECODERS = /* @__PURE__ */ new Map([
|
|
314
410
|
[RecordType.LABELSST, (d, c) => [decodeLabelSst(d, c)]],
|
|
315
411
|
[RecordType.LABEL, (d) => [decodeLabel(d)]],
|
|
@@ -340,15 +436,18 @@ function collectCells(records, start, ctx) {
|
|
|
340
436
|
for (let i = start + 1; i < records.length; i++) {
|
|
341
437
|
const record = records[i];
|
|
342
438
|
if (record === void 0 || record.type === RecordType.EOF) break;
|
|
343
|
-
pending = handleRecord(
|
|
439
|
+
pending = handleRecord(records, i, cells, pending, ctx);
|
|
344
440
|
}
|
|
345
441
|
return cells;
|
|
346
442
|
}
|
|
347
|
-
function handleRecord(
|
|
443
|
+
function handleRecord(records, index, cells, pending, ctx) {
|
|
444
|
+
const record = records[index];
|
|
445
|
+
if (record === void 0) return pending;
|
|
348
446
|
if (record.type === RecordType.STRING && pending) {
|
|
447
|
+
const value = readFormulaString(collectContinuedChunks(records, index));
|
|
349
448
|
cells.push({
|
|
350
449
|
...pending,
|
|
351
|
-
value
|
|
450
|
+
value
|
|
352
451
|
});
|
|
353
452
|
return null;
|
|
354
453
|
}
|
|
@@ -383,15 +482,13 @@ function toGrid(cells) {
|
|
|
383
482
|
return rows;
|
|
384
483
|
}
|
|
385
484
|
|
|
386
|
-
|
|
387
|
-
//#region src/excel-serial-date.ts
|
|
485
|
+
// src/excel-serial-date.ts
|
|
388
486
|
const MS_PER_DAY = 864e5;
|
|
389
487
|
function excelSerialToDate(serial, date1904) {
|
|
390
488
|
return new Date((date1904 ? Date.UTC(1904, 0, 1) : Date.UTC(1899, 11, 30)) + serial * MS_PER_DAY);
|
|
391
489
|
}
|
|
392
490
|
|
|
393
|
-
|
|
394
|
-
//#region src/biff/number-format.ts
|
|
491
|
+
// src/biff/number-format.ts
|
|
395
492
|
const BUILTIN_DATE_FORMAT_IDS = /* @__PURE__ */ new Set([
|
|
396
493
|
14,
|
|
397
494
|
15,
|
|
@@ -435,79 +532,7 @@ function looksLikeDateFormat(format) {
|
|
|
435
532
|
return /[ymdhs]/i.test(stripped);
|
|
436
533
|
}
|
|
437
534
|
|
|
438
|
-
|
|
439
|
-
//#region src/biff/continued-reader.ts
|
|
440
|
-
var ContinuedReader = class {
|
|
441
|
-
constructor(chunks) {
|
|
442
|
-
this.chunks = chunks;
|
|
443
|
-
this.chunkIndex = 0;
|
|
444
|
-
this.offset = 0;
|
|
445
|
-
}
|
|
446
|
-
get done() {
|
|
447
|
-
this.settle();
|
|
448
|
-
return this.chunkIndex >= this.chunks.length;
|
|
449
|
-
}
|
|
450
|
-
u8() {
|
|
451
|
-
this.settle();
|
|
452
|
-
return this.rawByte();
|
|
453
|
-
}
|
|
454
|
-
u16() {
|
|
455
|
-
return this.u8() | this.u8() << 8;
|
|
456
|
-
}
|
|
457
|
-
u32() {
|
|
458
|
-
return (this.u16() | this.u16() << 16) >>> 0;
|
|
459
|
-
}
|
|
460
|
-
readChars(count, wide) {
|
|
461
|
-
let text = "";
|
|
462
|
-
let isWide = wide;
|
|
463
|
-
for (let i = 0; i < count; i++) {
|
|
464
|
-
if (this.atChunkEnd()) isWide = (this.crossToNextChunk() & 1) !== 0;
|
|
465
|
-
text += String.fromCharCode(isWide ? this.rawByte() | this.rawByte() << 8 : this.rawByte());
|
|
466
|
-
}
|
|
467
|
-
return text;
|
|
468
|
-
}
|
|
469
|
-
skip(length) {
|
|
470
|
-
let left = length;
|
|
471
|
-
while (left > 0) {
|
|
472
|
-
this.settle();
|
|
473
|
-
const chunk = this.currentChunk();
|
|
474
|
-
const take = Math.min(left, chunk.length - this.offset);
|
|
475
|
-
this.offset += take;
|
|
476
|
-
left -= take;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
settle() {
|
|
480
|
-
while (this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength()) {
|
|
481
|
-
this.chunkIndex += 1;
|
|
482
|
-
this.offset = 0;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
atChunkEnd() {
|
|
486
|
-
return this.chunkIndex < this.chunks.length && this.offset >= this.chunkLength();
|
|
487
|
-
}
|
|
488
|
-
crossToNextChunk() {
|
|
489
|
-
this.chunkIndex += 1;
|
|
490
|
-
this.offset = 0;
|
|
491
|
-
return this.rawByte();
|
|
492
|
-
}
|
|
493
|
-
rawByte() {
|
|
494
|
-
const value = this.currentChunk()[this.offset];
|
|
495
|
-
if (value === void 0) throw new XlsError(`SST cursor past end of chunk ${this.chunkIndex} (offset ${this.offset})`);
|
|
496
|
-
this.offset += 1;
|
|
497
|
-
return value;
|
|
498
|
-
}
|
|
499
|
-
chunkLength() {
|
|
500
|
-
return this.currentChunk().length;
|
|
501
|
-
}
|
|
502
|
-
currentChunk() {
|
|
503
|
-
const chunk = this.chunks[this.chunkIndex];
|
|
504
|
-
if (chunk === void 0) throw new XlsError(`SST reader ran past its ${this.chunks.length} chunk(s)`);
|
|
505
|
-
return chunk;
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
//#endregion
|
|
510
|
-
//#region src/biff/shared-strings.ts
|
|
535
|
+
// src/biff/shared-strings.ts
|
|
511
536
|
const F_WIDE = 1;
|
|
512
537
|
const F_EXT = 4;
|
|
513
538
|
const F_RICH = 8;
|
|
@@ -530,8 +555,7 @@ function readString(reader) {
|
|
|
530
555
|
return text;
|
|
531
556
|
}
|
|
532
557
|
|
|
533
|
-
|
|
534
|
-
//#region src/biff/workbook-globals.ts
|
|
558
|
+
// src/biff/workbook-globals.ts
|
|
535
559
|
function parseGlobals(records) {
|
|
536
560
|
let date1904 = false;
|
|
537
561
|
const customFormats = /* @__PURE__ */ new Map();
|
|
@@ -544,7 +568,7 @@ function parseGlobals(records) {
|
|
|
544
568
|
if (record.type === RecordType.DATEMODE) date1904 = new ByteReader(record.data).u16() === 1;
|
|
545
569
|
else if (record.type === RecordType.FORMAT) addFormat(record.data, customFormats);
|
|
546
570
|
else if (record.type === RecordType.XF) xfFormatIndex.push(new ByteReader(record.data, 2).u16());
|
|
547
|
-
else if (record.type === RecordType.SST) sharedStrings = parseSharedStrings(
|
|
571
|
+
else if (record.type === RecordType.SST) sharedStrings = parseSharedStrings(collectContinuedChunks(records, i));
|
|
548
572
|
else if (record.type === RecordType.BOUNDSHEET8) boundSheets.push(parseBoundSheet(record.data));
|
|
549
573
|
}
|
|
550
574
|
return {
|
|
@@ -558,16 +582,6 @@ function addFormat(data, into) {
|
|
|
558
582
|
const formatIndex = reader.u16();
|
|
559
583
|
into.set(formatIndex, readUnicodeString(reader, false));
|
|
560
584
|
}
|
|
561
|
-
function collectSstChunks(records, sstIndex) {
|
|
562
|
-
const first = records[sstIndex];
|
|
563
|
-
const chunks = first ? [first.data] : [];
|
|
564
|
-
for (let j = sstIndex + 1; j < records.length; j++) {
|
|
565
|
-
const record = records[j];
|
|
566
|
-
if (record === void 0 || record.type !== RecordType.CONTINUE) break;
|
|
567
|
-
chunks.push(record.data);
|
|
568
|
-
}
|
|
569
|
-
return chunks;
|
|
570
|
-
}
|
|
571
585
|
function parseBoundSheet(data) {
|
|
572
586
|
const reader = new ByteReader(data);
|
|
573
587
|
const offset = reader.u32();
|
|
@@ -591,12 +605,12 @@ const VISIBILITY_BY_STATE = {
|
|
|
591
605
|
};
|
|
592
606
|
function makeNumeric(xfFormatIndex, customFormats, date1904) {
|
|
593
607
|
return (xfIndex, value) => {
|
|
594
|
-
|
|
608
|
+
const formatIndex = xfFormatIndex[xfIndex] ?? 0;
|
|
609
|
+
return isDateFormatIndex(formatIndex, customFormats) ? excelSerialToDate(value, date1904) : value;
|
|
595
610
|
};
|
|
596
611
|
}
|
|
597
612
|
|
|
598
|
-
|
|
599
|
-
//#region src/biff/parse-workbook.ts
|
|
613
|
+
// src/biff/parse-workbook.ts
|
|
600
614
|
function parseWorkbook(stream) {
|
|
601
615
|
const records = readRecords(stream);
|
|
602
616
|
const globals = parseGlobals(records);
|
|
@@ -604,8 +618,7 @@ function parseWorkbook(stream) {
|
|
|
604
618
|
return { sheets: globals.boundSheets.filter((sheet) => sheet.isWorksheet).map((sheet) => decodeSheet(records, sheet, offsetToIndex, globals)) };
|
|
605
619
|
}
|
|
606
620
|
|
|
607
|
-
|
|
608
|
-
//#region src/cfb/cfb-header.ts
|
|
621
|
+
// src/cfb/cfb-header.ts
|
|
609
622
|
const SIGNATURE = [
|
|
610
623
|
208,
|
|
611
624
|
207,
|
|
@@ -651,11 +664,13 @@ function readInitialDifat(bytes) {
|
|
|
651
664
|
}
|
|
652
665
|
function assertSignature(bytes) {
|
|
653
666
|
if (bytes.length < 512) throw new XlsError(`File too small to be an OLE2 container: ${bytes.length} bytes (need >= 512)`);
|
|
654
|
-
for (let i = 0; i < SIGNATURE.length; i++) if (bytes[i] !== SIGNATURE[i])
|
|
667
|
+
for (let i = 0; i < SIGNATURE.length; i++) if (bytes[i] !== SIGNATURE[i]) {
|
|
668
|
+
const got = Array.from(bytes.slice(0, 8), (b) => b.toString(16).padStart(2, "0")).join(" ");
|
|
669
|
+
throw new XlsError(`Not an OLE2 / .xls file: expected magic D0CF11E0A1B11AE1, got ${got}`);
|
|
670
|
+
}
|
|
655
671
|
}
|
|
656
672
|
|
|
657
|
-
|
|
658
|
-
//#region src/cfb/directory.ts
|
|
673
|
+
// src/cfb/directory.ts
|
|
659
674
|
const DIRECTORY_ENTRY_SIZE = 128;
|
|
660
675
|
const OBJECT_TYPE_ROOT = 5;
|
|
661
676
|
function parseDirectory(directoryBytes) {
|
|
@@ -694,15 +709,26 @@ function decodeName(nameBytes, byteLength) {
|
|
|
694
709
|
return name;
|
|
695
710
|
}
|
|
696
711
|
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
const
|
|
700
|
-
const FREESECT = 4294967295;
|
|
712
|
+
// src/cfb/sector-chains.ts
|
|
713
|
+
const ENDOFCHAIN = 0xfffffffe;
|
|
714
|
+
const FREESECT = 0xffffffff;
|
|
701
715
|
function sectorOffset(id, sectorSize) {
|
|
702
716
|
return (id + 1) * sectorSize;
|
|
703
717
|
}
|
|
718
|
+
/**
|
|
719
|
+
* How many whole-or-partial sectors follow the header in the file: valid sector
|
|
720
|
+
* ids are 0..count-1. Every chain and the FAT itself are capped by this, because
|
|
721
|
+
* the FAT is attacker-controlled and can describe sectors the file doesn't have
|
|
722
|
+
* — sizing buffers by those claims let a ~400 KB file allocate hundreds of MB
|
|
723
|
+
* (GHSA-29jh-wp5p-965w).
|
|
724
|
+
*
|
|
725
|
+
* @example fileSectorCount(bytes, 512) // 3 for a 2048-byte v3 file
|
|
726
|
+
*/
|
|
727
|
+
function fileSectorCount(bytes, sectorSize) {
|
|
728
|
+
return Math.max(0, Math.ceil(bytes.length / sectorSize) - 1);
|
|
729
|
+
}
|
|
704
730
|
function buildFat(bytes, header) {
|
|
705
|
-
const fatSectorIds = collectFatSectorIds(bytes, header);
|
|
731
|
+
const fatSectorIds = collectFatSectorIds(bytes, header).slice(0, fileSectorCount(bytes, header.sectorSize));
|
|
706
732
|
const fat = [];
|
|
707
733
|
const perSector = header.sectorSize / 4;
|
|
708
734
|
for (const id of fatSectorIds) {
|
|
@@ -727,39 +753,53 @@ function collectFatSectorIds(bytes, header) {
|
|
|
727
753
|
}
|
|
728
754
|
return ids;
|
|
729
755
|
}
|
|
730
|
-
function followChain(fat, start) {
|
|
756
|
+
function followChain(fat, start, sectorLimit) {
|
|
731
757
|
const chain = [];
|
|
732
758
|
const seen = /* @__PURE__ */ new Set();
|
|
759
|
+
const limit = Math.min(fat.length, sectorLimit);
|
|
733
760
|
let current = start;
|
|
734
|
-
while (current !== ENDOFCHAIN && current <
|
|
761
|
+
while (current !== ENDOFCHAIN && current < limit && !seen.has(current)) {
|
|
735
762
|
seen.add(current);
|
|
736
763
|
chain.push(current);
|
|
737
764
|
current = fat[current] ?? ENDOFCHAIN;
|
|
738
765
|
}
|
|
739
766
|
return chain;
|
|
740
767
|
}
|
|
741
|
-
|
|
742
|
-
|
|
768
|
+
/**
|
|
769
|
+
* Concatenates the bytes of a sector chain, up to `size`. Used for the
|
|
770
|
+
* directory, the mini-FAT and regular (non-mini) streams; also for mini streams
|
|
771
|
+
* with the mini-stream container as `bytes` and `firstSectorAt = 0`. Allocates
|
|
772
|
+
* at most `min(size, chain length × sectorSize)` — never the declared size alone
|
|
773
|
+
* (a u32 from the file) nor a full chain past `size`.
|
|
774
|
+
*
|
|
775
|
+
* @example readChainBytes(bytes, [3, 4], 512, 700) // 700 bytes from sectors 3, 4
|
|
776
|
+
*/
|
|
777
|
+
function readChainBytes(bytes, chain, sectorSize, size, firstSectorAt = sectorSize) {
|
|
778
|
+
const out = new Uint8Array(Math.max(0, Math.min(size, chain.length * sectorSize)));
|
|
743
779
|
chain.forEach((id, index) => {
|
|
744
|
-
const
|
|
745
|
-
|
|
780
|
+
const at = index * sectorSize;
|
|
781
|
+
if (at >= out.length) return;
|
|
782
|
+
const start = firstSectorAt + id * sectorSize;
|
|
783
|
+
out.set(bytes.subarray(start, start + Math.min(sectorSize, out.length - at)), at);
|
|
746
784
|
});
|
|
747
|
-
return out
|
|
785
|
+
return out;
|
|
748
786
|
}
|
|
749
787
|
|
|
750
|
-
|
|
751
|
-
//#region src/cfb/compound-file.ts
|
|
788
|
+
// src/cfb/compound-file.ts
|
|
752
789
|
function openCompoundFile(bytes) {
|
|
753
790
|
const header = parseCfbHeader(bytes);
|
|
754
|
-
const
|
|
755
|
-
const directory = parseDirectory(directoryBytes(bytes, header, fat));
|
|
756
|
-
const file = {
|
|
791
|
+
const base = {
|
|
757
792
|
bytes,
|
|
758
793
|
header,
|
|
759
|
-
fat,
|
|
794
|
+
fat: buildFat(bytes, header),
|
|
795
|
+
sectorLimit: fileSectorCount(bytes, header.sectorSize)
|
|
796
|
+
};
|
|
797
|
+
const directory = parseDirectory(directoryBytes(base));
|
|
798
|
+
const file = {
|
|
799
|
+
...base,
|
|
760
800
|
directory,
|
|
761
|
-
miniStream: readMiniStreamContainer(
|
|
762
|
-
miniFat: buildMiniFat(
|
|
801
|
+
miniStream: readMiniStreamContainer(base, directory),
|
|
802
|
+
miniFat: buildMiniFat(base)
|
|
763
803
|
};
|
|
764
804
|
return { readStream(name) {
|
|
765
805
|
const entry = directory.find((e) => e.name === name);
|
|
@@ -767,32 +807,32 @@ function openCompoundFile(bytes) {
|
|
|
767
807
|
return entry.size < header.miniStreamCutoff ? readMiniStream(file, entry) : readRegularStream(file, entry);
|
|
768
808
|
} };
|
|
769
809
|
}
|
|
770
|
-
function
|
|
771
|
-
const chain = followChain(fat,
|
|
772
|
-
|
|
810
|
+
function wholeChainBytes(base, start) {
|
|
811
|
+
const chain = followChain(base.fat, start, base.sectorLimit);
|
|
812
|
+
const { sectorSize } = base.header;
|
|
813
|
+
return readChainBytes(base.bytes, chain, sectorSize, chain.length * sectorSize);
|
|
814
|
+
}
|
|
815
|
+
function directoryBytes(base) {
|
|
816
|
+
return wholeChainBytes(base, base.header.firstDirSector);
|
|
773
817
|
}
|
|
774
818
|
function readRegularStream(file, entry) {
|
|
775
|
-
const chain = followChain(file.fat, entry.startSector);
|
|
819
|
+
const chain = followChain(file.fat, entry.startSector, file.sectorLimit);
|
|
776
820
|
return readChainBytes(file.bytes, chain, file.header.sectorSize, entry.size);
|
|
777
821
|
}
|
|
778
|
-
function readMiniStreamContainer(
|
|
822
|
+
function readMiniStreamContainer(base, directory) {
|
|
779
823
|
const root = findRoot(directory);
|
|
780
824
|
if (!root) return /* @__PURE__ */ new Uint8Array(0);
|
|
781
|
-
|
|
825
|
+
const chain = followChain(base.fat, root.startSector, base.sectorLimit);
|
|
826
|
+
return readChainBytes(base.bytes, chain, base.header.sectorSize, root.size);
|
|
782
827
|
}
|
|
783
|
-
function buildMiniFat(
|
|
784
|
-
|
|
785
|
-
return toU32Array(readChainBytes(bytes, chain, header.sectorSize, chain.length * header.sectorSize));
|
|
828
|
+
function buildMiniFat(base) {
|
|
829
|
+
return toU32Array(wholeChainBytes(base, base.header.firstMiniFatSector));
|
|
786
830
|
}
|
|
787
831
|
function readMiniStream(file, entry) {
|
|
788
832
|
const { miniStream, miniFat, header } = file;
|
|
789
|
-
const
|
|
790
|
-
const
|
|
791
|
-
chain.
|
|
792
|
-
const start = id * header.miniSectorSize;
|
|
793
|
-
out.set(miniStream.subarray(start, start + header.miniSectorSize), index * header.miniSectorSize);
|
|
794
|
-
});
|
|
795
|
-
return out.subarray(0, entry.size);
|
|
833
|
+
const miniSize = header.miniSectorSize;
|
|
834
|
+
const chain = followChain(miniFat, entry.startSector, Math.ceil(miniStream.length / miniSize));
|
|
835
|
+
return readChainBytes(miniStream, chain, miniSize, entry.size, 0);
|
|
796
836
|
}
|
|
797
837
|
function toU32Array(raw) {
|
|
798
838
|
const reader = new ByteReader(raw);
|
|
@@ -802,8 +842,7 @@ function toU32Array(raw) {
|
|
|
802
842
|
return values;
|
|
803
843
|
}
|
|
804
844
|
|
|
805
|
-
|
|
806
|
-
//#region src/reader.ts
|
|
845
|
+
// src/reader.ts
|
|
807
846
|
function readXls(data) {
|
|
808
847
|
const cfb = openCompoundFile(toBytes(data));
|
|
809
848
|
const stream = cfb.readStream("Workbook") ?? cfb.readStream("Book");
|
|
@@ -817,8 +856,7 @@ function toBytes(data) {
|
|
|
817
856
|
return data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
818
857
|
}
|
|
819
858
|
|
|
820
|
-
|
|
821
|
-
//#region src/sheet-to-objects.ts
|
|
859
|
+
// src/sheet-to-objects.ts
|
|
822
860
|
function sheetToObjects(sheet, options = {}) {
|
|
823
861
|
const headerRow = options.headerRow ?? 0;
|
|
824
862
|
assertHeaderRow(headerRow);
|
|
@@ -848,8 +886,7 @@ function assertHeaderRow(headerRow) {
|
|
|
848
886
|
throw new XlsError(`headerRow must be a non-negative integer, got ${JSON.stringify(headerRow)}`);
|
|
849
887
|
}
|
|
850
888
|
|
|
851
|
-
|
|
852
|
-
//#region src/sheet-to-csv.ts
|
|
889
|
+
// src/sheet-to-csv.ts
|
|
853
890
|
function sheetToCsv(sheet, options = {}) {
|
|
854
891
|
const delimiter = options.delimiter ?? ",";
|
|
855
892
|
const eol = options.eol ?? "\n";
|
|
@@ -874,7 +911,6 @@ function assertDelimiter(delimiter) {
|
|
|
874
911
|
throw new XlsError(`delimiter must be a single character other than '"', CR, or LF, got ${JSON.stringify(delimiter)}`);
|
|
875
912
|
}
|
|
876
913
|
|
|
877
|
-
//#endregion
|
|
878
914
|
exports.CellError = CellError;
|
|
879
915
|
exports.XlsError = XlsError;
|
|
880
916
|
exports.excelSerialToDate = excelSerialToDate;
|