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