@inkvero/pdf-tools 0.1.0 → 0.2.0
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/README.md +121 -8
- package/dist/index.d.mts +78 -2
- package/dist/index.mjs +3115 -534
- package/package.json +2 -1
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createCipheriv, createDecipheriv, createHash } from "node:crypto";
|
|
2
1
|
import { constants, inflateSync } from "node:zlib";
|
|
2
|
+
import { createCipheriv, createDecipheriv, createHash } from "node:crypto";
|
|
3
3
|
// src/values.ts
|
|
4
4
|
var PdfName = class {
|
|
5
5
|
/** The name with every `#xx` escape decoded, as a string of byte values (Latin-1). */
|
|
@@ -11,12 +11,21 @@ var PdfName = class {
|
|
|
11
11
|
/** A literal or hex string; its bytes are decoded only when they are needed (metadata). */
|
|
12
12
|
var PdfString = class {
|
|
13
13
|
decode;
|
|
14
|
-
|
|
14
|
+
check;
|
|
15
|
+
/** How many bytes it is written in, between its delimiters: at least as many as it decodes to. */
|
|
16
|
+
written;
|
|
17
|
+
constructor(decode, irregular, written = 0) {
|
|
15
18
|
this.decode = decode;
|
|
19
|
+
this.check = irregular;
|
|
20
|
+
this.written = written;
|
|
16
21
|
}
|
|
17
22
|
bytes() {
|
|
18
23
|
return this.decode();
|
|
19
24
|
}
|
|
25
|
+
/** Whether it is written outside its written form, which readers may decode differently (see lexer.ts). */
|
|
26
|
+
irregular() {
|
|
27
|
+
return this.check?.() ?? false;
|
|
28
|
+
}
|
|
20
29
|
};
|
|
21
30
|
/** A word that is not a name, number or keyword of the syntax (`foo`, `{`): pdf.js keeps it as a value. */
|
|
22
31
|
var PdfKeyword = class {
|
|
@@ -158,9 +167,12 @@ const emptyCounts = () => ({
|
|
|
158
167
|
unresolved: 0
|
|
159
168
|
});
|
|
160
169
|
/** Nodes visited between two yields. */
|
|
161
|
-
const STEP = 256;
|
|
162
|
-
/**
|
|
163
|
-
|
|
170
|
+
const STEP$2 = 256;
|
|
171
|
+
/**
|
|
172
|
+
* Counts the active content of every definition the reader holds. `files` receives the keys of the embedded
|
|
173
|
+
* files counted: `n<num>` for a stream object, anything else for one that is not an object (see addFiles).
|
|
174
|
+
*/
|
|
175
|
+
function* countActive(reader, files = /* @__PURE__ */ new Set(), visit) {
|
|
164
176
|
const { work } = reader;
|
|
165
177
|
const maxDepth = work.limits.maxDepth;
|
|
166
178
|
const counts = emptyCounts();
|
|
@@ -258,7 +270,6 @@ function* countActive(reader) {
|
|
|
258
270
|
}
|
|
259
271
|
return false;
|
|
260
272
|
}, true);
|
|
261
|
-
const files = /* @__PURE__ */ new Set();
|
|
262
273
|
let anonymous = 0;
|
|
263
274
|
const followed = /* @__PURE__ */ new Set();
|
|
264
275
|
const addFiles = (v, depth, key) => {
|
|
@@ -327,8 +338,9 @@ function* countActive(reader) {
|
|
|
327
338
|
const stack = [def.value];
|
|
328
339
|
while (stack.length > 0) {
|
|
329
340
|
nodes += 1;
|
|
330
|
-
if (nodes % STEP === 0) yield;
|
|
341
|
+
if (nodes % STEP$2 === 0) yield;
|
|
331
342
|
const v = stack.pop();
|
|
343
|
+
visit?.(v, def);
|
|
332
344
|
if (Array.isArray(v)) {
|
|
333
345
|
for (const x of v) if (x !== null && typeof x === "object") stack.push(x);
|
|
334
346
|
} else if (v instanceof PdfDict) {
|
|
@@ -343,6 +355,206 @@ function* countActive(reader) {
|
|
|
343
355
|
counts.embeddedFile = files.size;
|
|
344
356
|
return counts;
|
|
345
357
|
}
|
|
358
|
+
// src/errors.ts
|
|
359
|
+
/** The error thrown (as a rejection) for every problem this library reports. */
|
|
360
|
+
var PdfToolsError = class extends Error {
|
|
361
|
+
code;
|
|
362
|
+
details;
|
|
363
|
+
constructor(code, message, details = {}) {
|
|
364
|
+
super(message);
|
|
365
|
+
this.name = "PdfToolsError";
|
|
366
|
+
this.code = code;
|
|
367
|
+
this.details = details;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
/** A `LIMIT_EXCEEDED` error for `limit`. */
|
|
371
|
+
const limitError = (limit, value, what) => new PdfToolsError("LIMIT_EXCEEDED", `${what} (limit ${limit} = ${value})`, {
|
|
372
|
+
limit,
|
|
373
|
+
value
|
|
374
|
+
});
|
|
375
|
+
/** A `DAMAGED` error with a reason from a fixed list. */
|
|
376
|
+
const damaged = (reason, offset) => new PdfToolsError("DAMAGED", `the file is damaged: ${reason}${offset === void 0 ? "" : ` at byte ${offset}`}`, offset === void 0 ? { reason } : {
|
|
377
|
+
reason,
|
|
378
|
+
offset
|
|
379
|
+
});
|
|
380
|
+
const isSpace$2 = (c) => c === 32 || c === 10 || c === 13;
|
|
381
|
+
const isDigit$1 = (c) => c !== void 0 && c >= 48 && c <= 57;
|
|
382
|
+
/** A delimiter that ends a regular token. */
|
|
383
|
+
const isDelimiter$1 = (c) => c === 40 || c === 41 || c === 60 || c === 62 || c === 91 || c === 93 || c === 123 || c === 125 || c === 47 || c === 37;
|
|
384
|
+
const isRegular = (c) => c !== void 0 && !isDelimiter$1(c) && !isSpace$2(c) && c !== 9 && c !== 12 && c !== 0;
|
|
385
|
+
/** Whether `b[from..to)` is an unsigned whole number in digits without leading zeros, of at most `max` digits. */
|
|
386
|
+
function plainDigits(b, from, to, max) {
|
|
387
|
+
if (to <= from || to - from > max) return false;
|
|
388
|
+
for (let i = from; i < to; i += 1) if (!isDigit$1(b[i])) return false;
|
|
389
|
+
return to - from === 1 || b[from] !== 48;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Whether a number token is written as producers write it: an optional minus, digits without a leading zero (or one
|
|
393
|
+
* zero), and optionally a point and digits (`0.5`, `.5`, `-3.25`); at most MAX_NUMBER characters, no plus sign.
|
|
394
|
+
*/
|
|
395
|
+
function plainNumber(b, from, to) {
|
|
396
|
+
if (to - from > 20) return false;
|
|
397
|
+
let i = from;
|
|
398
|
+
if (b[i] === 45) i += 1;
|
|
399
|
+
const intStart = i;
|
|
400
|
+
let c = b[i];
|
|
401
|
+
while (i < to && c >= 48 && c <= 57) c = b[++i];
|
|
402
|
+
const intEnd = i;
|
|
403
|
+
if (intEnd - intStart > 1 && b[intStart] === 48) return false;
|
|
404
|
+
if (i === to) return intEnd > intStart;
|
|
405
|
+
if (c !== 46) return false;
|
|
406
|
+
const fracStart = ++i;
|
|
407
|
+
c = b[i];
|
|
408
|
+
while (i < to && c >= 48 && c <= 57) c = b[++i];
|
|
409
|
+
return i === to && i > fracStart;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Whether the header of numbers of an object stream, the data before `first`, is written as producers write it:
|
|
413
|
+
* whole numbers in plain digits (at most 10), each followed by one space or one end of line, and nothing but up to
|
|
414
|
+
* MAX_SPACE bytes of white space after the last; no comment (pypdf reads no offset after one).
|
|
415
|
+
*/
|
|
416
|
+
function plainObjectStreamHeader(b, first) {
|
|
417
|
+
let i = 0;
|
|
418
|
+
while (i < first) {
|
|
419
|
+
const start = i;
|
|
420
|
+
while (i < first && isDigit$1(b[i])) i += 1;
|
|
421
|
+
if (i === start) {
|
|
422
|
+
if (first - i > 8) return false;
|
|
423
|
+
for (; i < first; i += 1) if (!isSpace$2(b[i])) return false;
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
if (!plainDigits(b, start, i, 10)) return false;
|
|
427
|
+
if (b[i] === 13 && b[i + 1] === 10) i += 2;
|
|
428
|
+
else if (isSpace$2(b[i])) i += 1;
|
|
429
|
+
else return false;
|
|
430
|
+
}
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* The white space and comments from `from` to `to` (between a header's `obj` and the value): -1 when they are not
|
|
435
|
+
* in the canonical form, else how many comments they hold.
|
|
436
|
+
*/
|
|
437
|
+
function gapForm(b, from, to) {
|
|
438
|
+
let comments = 0;
|
|
439
|
+
let run = 0;
|
|
440
|
+
for (let i = from; i < to;) {
|
|
441
|
+
const c = b[i];
|
|
442
|
+
if (isSpace$2(c)) {
|
|
443
|
+
if (++run > 8) return -1;
|
|
444
|
+
i += 1;
|
|
445
|
+
} else if (c === 37) {
|
|
446
|
+
const start = i;
|
|
447
|
+
while (i < to && b[i] !== 10 && b[i] !== 13) i += 1;
|
|
448
|
+
if (i - start > 128) return -1;
|
|
449
|
+
comments += 1;
|
|
450
|
+
run = 0;
|
|
451
|
+
} else return -1;
|
|
452
|
+
}
|
|
453
|
+
return comments;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Whether the object header at `at` is `num gen obj` with the numbers in digits without leading zeros (the header
|
|
457
|
+
* being at the start of a line with single spaces is checked with the rest of the one form).
|
|
458
|
+
*/
|
|
459
|
+
function canonicalHeader(b, at) {
|
|
460
|
+
let i = at;
|
|
461
|
+
while (isDigit$1(b[i])) i += 1;
|
|
462
|
+
if (!plainDigits(b, at, i, 7) || b[i] !== 32) return false;
|
|
463
|
+
const g = i + 1;
|
|
464
|
+
i = g;
|
|
465
|
+
while (isDigit$1(b[i])) i += 1;
|
|
466
|
+
return plainDigits(b, g, i, 5);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Whether the text from `from` to `to` (the header of numbers of an object stream, or a trailer after its keyword,
|
|
470
|
+
* up to the end of its outermost dictionary) is in the canonical form: white space only
|
|
471
|
+
* space, LF and CR, in runs of at most MAX_SPACE bytes; at most MAX_COMMENTS short comments; every number in its plain form; every reference
|
|
472
|
+
* `num gen R` with one space between its parts and the numbers in plain digits.
|
|
473
|
+
*/
|
|
474
|
+
function canonicalText(b, from, to, trailer = false) {
|
|
475
|
+
let i = from;
|
|
476
|
+
let depth = 0;
|
|
477
|
+
let comments = 0;
|
|
478
|
+
let n1s = -1;
|
|
479
|
+
let n1e = -1;
|
|
480
|
+
let n2s = -1;
|
|
481
|
+
let n2e = -1;
|
|
482
|
+
while (i < to) {
|
|
483
|
+
const c = b[i];
|
|
484
|
+
if (isSpace$2(c)) {
|
|
485
|
+
const start = i;
|
|
486
|
+
while (i < to && isSpace$2(b[i])) i += 1;
|
|
487
|
+
if (i - start > 8) return false;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (c === 37) {
|
|
491
|
+
const start = i;
|
|
492
|
+
while (i < to && b[i] !== 10 && b[i] !== 13) i += 1;
|
|
493
|
+
comments += 1;
|
|
494
|
+
if (comments > 2 || i - start > 128) return false;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (c === 9 || c === 12 || c === 0) return false;
|
|
498
|
+
if (c === 40) {
|
|
499
|
+
let level = 0;
|
|
500
|
+
for (; i < to; i += 1) {
|
|
501
|
+
if (b[i] === 92) {
|
|
502
|
+
i += 1;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (b[i] === 40) level += 1;
|
|
506
|
+
else if (b[i] === 41 && --level === 0) break;
|
|
507
|
+
}
|
|
508
|
+
i += 1;
|
|
509
|
+
n1s = n2s = -1;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
if (c === 60 && b[i + 1] !== 60) {
|
|
513
|
+
while (i < to && b[i] !== 62) i += 1;
|
|
514
|
+
i += 1;
|
|
515
|
+
n1s = n2s = -1;
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (c === 60 && b[i + 1] === 60 || c === 62 && b[i + 1] === 62) {
|
|
519
|
+
depth += c === 60 ? 1 : -1;
|
|
520
|
+
i += 2;
|
|
521
|
+
n1s = n2s = -1;
|
|
522
|
+
if (trailer && depth === 0) return true;
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
if (c === 91 || c === 93) {
|
|
526
|
+
i += 1;
|
|
527
|
+
n1s = n2s = -1;
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
if (c === 47) {
|
|
531
|
+
i += 1;
|
|
532
|
+
while (i < to && isRegular(b[i])) i += 1;
|
|
533
|
+
n1s = n2s = -1;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
if (!isRegular(c)) return false;
|
|
537
|
+
const start = i;
|
|
538
|
+
while (i < to && isRegular(b[i])) i += 1;
|
|
539
|
+
const first = b[start];
|
|
540
|
+
if (isDigit$1(first) || first === 45 || first === 46 || first === 43) {
|
|
541
|
+
if (!plainNumber(b, start, i)) return false;
|
|
542
|
+
n1s = n2s;
|
|
543
|
+
n1e = n2e;
|
|
544
|
+
n2s = start;
|
|
545
|
+
n2e = i;
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
if (i - start === 1 && first === 82) {
|
|
549
|
+
if (n1s < 0 || n2s < 0 || n2s !== n1e + 1 || start !== n2e + 1 || b[n1e] !== 32 || b[n2e] !== 32) return false;
|
|
550
|
+
if (!plainDigits(b, n1s, n1e, 7) || !plainDigits(b, n2s, n2e, 5)) return false;
|
|
551
|
+
n1s = n2s = -1;
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
n1s = n2s = -1;
|
|
555
|
+
}
|
|
556
|
+
return !trailer;
|
|
557
|
+
}
|
|
346
558
|
// src/bytes.ts
|
|
347
559
|
/** NUL, TAB, LF, FF, CR, SPACE, and VT, which some readers also take as whitespace. */
|
|
348
560
|
const isWhite = (c) => c === 32 || c === 10 || c === 13 || c === 9 || c === 12 || c === 0 || c === 11;
|
|
@@ -432,73 +644,368 @@ function firstAtLeast(sorted, at) {
|
|
|
432
644
|
}
|
|
433
645
|
/** The text of a byte range as Latin-1. */
|
|
434
646
|
const latin1 = (bytes) => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("latin1");
|
|
435
|
-
// src/
|
|
436
|
-
/** The
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
647
|
+
// src/filters.ts
|
|
648
|
+
/** The most filters one stream may chain. */
|
|
649
|
+
const MAX_FILTERS = 8;
|
|
650
|
+
/** Output that stops at the room the budget leaves. */
|
|
651
|
+
var Out = class {
|
|
652
|
+
buf;
|
|
653
|
+
length = 0;
|
|
654
|
+
room;
|
|
655
|
+
limit;
|
|
656
|
+
work;
|
|
657
|
+
constructor(work, guess) {
|
|
658
|
+
const { room, limit } = work.inflateRoom();
|
|
659
|
+
this.room = room;
|
|
660
|
+
this.limit = limit;
|
|
661
|
+
this.work = work;
|
|
662
|
+
this.buf = new Uint8Array(Math.max(16, Math.min(guess, room)));
|
|
663
|
+
}
|
|
664
|
+
grow(need) {
|
|
665
|
+
if (need > this.room) throw limitError(this.limit, this.work.limits[this.limit], "a stream decompresses to too many bytes");
|
|
666
|
+
let size = this.buf.length;
|
|
667
|
+
while (size < need) size *= 2;
|
|
668
|
+
const next = new Uint8Array(Math.min(size, Math.max(need, this.room)));
|
|
669
|
+
next.set(this.buf.subarray(0, this.length));
|
|
670
|
+
this.buf = next;
|
|
671
|
+
}
|
|
672
|
+
push(byte) {
|
|
673
|
+
if (this.length >= this.buf.length) this.grow(this.length + 1);
|
|
674
|
+
this.buf[this.length] = byte;
|
|
675
|
+
this.length += 1;
|
|
676
|
+
}
|
|
677
|
+
/** Repeats `count` bytes starting at `from` of the output so far (LZW). */
|
|
678
|
+
copyWithin(from, count) {
|
|
679
|
+
if (this.length + count > this.buf.length) this.grow(this.length + count);
|
|
680
|
+
this.buf.copyWithin(this.length, from, from + count);
|
|
681
|
+
this.length += count;
|
|
682
|
+
}
|
|
683
|
+
byteAt(i) {
|
|
684
|
+
return this.buf[i];
|
|
685
|
+
}
|
|
686
|
+
done() {
|
|
687
|
+
this.work.inflate(this.length);
|
|
688
|
+
return this.buf.length === this.length ? this.buf : this.buf.slice(0, this.length);
|
|
445
689
|
}
|
|
446
690
|
};
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
limit
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
const equal = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
460
|
-
const md5 = (data) => new Uint8Array(createHash("md5").update(data).digest());
|
|
461
|
-
const sha = (alg, ...parts) => {
|
|
462
|
-
const h = createHash(alg);
|
|
463
|
-
for (const p of parts) h.update(p);
|
|
464
|
-
return new Uint8Array(h.digest());
|
|
465
|
-
};
|
|
466
|
-
/** RC4 (encryption and decryption are the same). OpenSSL 3 no longer offers it by default. */
|
|
467
|
-
function rc4(key, data) {
|
|
468
|
-
const s = /* @__PURE__ */ new Uint8Array(256);
|
|
469
|
-
for (let i = 0; i < 256; i += 1) s[i] = i;
|
|
470
|
-
for (let i = 0, j = 0; i < 256; i += 1) {
|
|
471
|
-
j = j + s[i] + key[i % key.length] & 255;
|
|
472
|
-
const t = s[i];
|
|
473
|
-
s[i] = s[j];
|
|
474
|
-
s[j] = t;
|
|
691
|
+
function inflate(data, work) {
|
|
692
|
+
if (data.length === 0) return data;
|
|
693
|
+
const { room, limit } = work.inflateRoom();
|
|
694
|
+
let out;
|
|
695
|
+
try {
|
|
696
|
+
out = inflateSync(data, {
|
|
697
|
+
maxOutputLength: Math.max(1, room),
|
|
698
|
+
finishFlush: constants.Z_SYNC_FLUSH
|
|
699
|
+
});
|
|
700
|
+
} catch (error) {
|
|
701
|
+
if (error.code === "ERR_BUFFER_TOO_LARGE" || error instanceof RangeError) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
702
|
+
throw damaged("compressed data");
|
|
475
703
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
704
|
+
if (out.length > room) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
705
|
+
work.inflate(out.length);
|
|
706
|
+
return out.byteOffset === 0 && out.buffer.byteLength === out.length ? new Uint8Array(out.buffer) : new Uint8Array(out);
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Flate data read as one whole zlib stream: header, final block and Adler-32 checksum, as every reader reads
|
|
710
|
+
* it alike (bytes after it are ignored by all). Throws `DAMAGED` for data that ends early, a wrong checksum, a
|
|
711
|
+
* preset dictionary or a header that is not zlib, where readers extract different bytes; the limits as
|
|
712
|
+
* `inflate` does. Used only to check embedded files; the streams the library reads use `inflate`.
|
|
713
|
+
*/
|
|
714
|
+
function inflateWhole(data, work) {
|
|
715
|
+
const { room, limit } = work.inflateRoom();
|
|
716
|
+
let out;
|
|
717
|
+
try {
|
|
718
|
+
out = inflateSync(data, { maxOutputLength: Math.max(1, room) });
|
|
719
|
+
} catch (error) {
|
|
720
|
+
if (error.code === "ERR_BUFFER_TOO_LARGE" || error instanceof RangeError) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
721
|
+
throw damaged("compressed data");
|
|
484
722
|
}
|
|
485
|
-
|
|
723
|
+
if (out.length > room) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
724
|
+
work.inflate(out.length);
|
|
725
|
+
return out.byteOffset === 0 && out.buffer.byteLength === out.length ? new Uint8Array(out.buffer) : new Uint8Array(out);
|
|
486
726
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
727
|
+
/**
|
|
728
|
+
* Whether Flate data is one whole zlib stream (as `inflateWhole` requires), inflating at most `bound` bytes: for a
|
|
729
|
+
* stream of the file structure already decoded within the budget, so not counted against it again. qpdf warns
|
|
730
|
+
* about data that ends before its checksum; other readers read it as far as it goes.
|
|
731
|
+
*/
|
|
732
|
+
function flateIsWhole(data, bound) {
|
|
733
|
+
try {
|
|
734
|
+
inflateSync(data, { maxOutputLength: Math.max(1, bound) });
|
|
735
|
+
return true;
|
|
736
|
+
} catch {
|
|
737
|
+
return false;
|
|
738
|
+
}
|
|
492
739
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
|
|
740
|
+
function lzw(data, earlyChange, work) {
|
|
741
|
+
const out = new Out(work, data.length * 3);
|
|
742
|
+
const start = /* @__PURE__ */ new Int32Array(4096);
|
|
743
|
+
const size = /* @__PURE__ */ new Int32Array(4096);
|
|
744
|
+
let next = 258;
|
|
745
|
+
let width = 9;
|
|
746
|
+
let previous = -1;
|
|
747
|
+
let acc = 0;
|
|
748
|
+
let bits = 0;
|
|
749
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
750
|
+
acc = (acc << 8 | data[i]) >>> 0;
|
|
751
|
+
bits += 8;
|
|
752
|
+
while (bits >= width) {
|
|
753
|
+
const code = acc >>> bits - width & (1 << width) - 1;
|
|
754
|
+
bits -= width;
|
|
755
|
+
acc &= (1 << bits) - 1;
|
|
756
|
+
if (code === 256) {
|
|
757
|
+
next = 258;
|
|
758
|
+
width = 9;
|
|
759
|
+
previous = -1;
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
if (code === 257) return out.done();
|
|
763
|
+
const at = out.length;
|
|
764
|
+
if (code < 256) out.push(code);
|
|
765
|
+
else if (code < next) out.copyWithin(start[code], size[code]);
|
|
766
|
+
else if (code === next && previous >= 0) {
|
|
767
|
+
if (previous < 256) out.push(previous);
|
|
768
|
+
else out.copyWithin(start[previous], size[previous]);
|
|
769
|
+
out.push(out.byteAt(at));
|
|
770
|
+
} else throw damaged("LZW data");
|
|
771
|
+
if (previous >= 0 && next < 4096) {
|
|
772
|
+
const prevLen = previous < 256 ? 1 : size[previous];
|
|
773
|
+
start[next] = at - prevLen;
|
|
774
|
+
size[next] = prevLen + 1;
|
|
775
|
+
next += 1;
|
|
776
|
+
}
|
|
777
|
+
if (next + earlyChange >= 1 << width && width < 12) width += 1;
|
|
778
|
+
previous = code;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return out.done();
|
|
499
782
|
}
|
|
500
|
-
|
|
501
|
-
const
|
|
783
|
+
function asciiHex(data, work) {
|
|
784
|
+
const out = new Out(work, data.length / 2 + 1);
|
|
785
|
+
let high = -1;
|
|
786
|
+
for (const c of data) {
|
|
787
|
+
if (c === 62) break;
|
|
788
|
+
if (isWhite(c)) continue;
|
|
789
|
+
const v = c >= 48 && c <= 57 ? c - 48 : c >= 65 && c <= 70 ? c - 55 : c >= 97 && c <= 102 ? c - 87 : -1;
|
|
790
|
+
if (v < 0) throw damaged("ASCIIHex data");
|
|
791
|
+
if (high < 0) high = v;
|
|
792
|
+
else {
|
|
793
|
+
out.push(high * 16 + v);
|
|
794
|
+
high = -1;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
if (high >= 0) out.push(high * 16);
|
|
798
|
+
return out.done();
|
|
799
|
+
}
|
|
800
|
+
function ascii85(data, work) {
|
|
801
|
+
const out = new Out(work, data.length);
|
|
802
|
+
const group = [];
|
|
803
|
+
const flush = (count) => {
|
|
804
|
+
let n = 0;
|
|
805
|
+
for (let k = 0; k < 5; k += 1) n = n * 85 + (k < group.length ? group[k] : 84);
|
|
806
|
+
for (let k = 0; k < count; k += 1) out.push(n >>> 24 - 8 * k & 255);
|
|
807
|
+
};
|
|
808
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
809
|
+
const c = data[i];
|
|
810
|
+
if (isWhite(c)) continue;
|
|
811
|
+
if (c === 126) break;
|
|
812
|
+
if (c === 122 && group.length === 0) {
|
|
813
|
+
for (let k = 0; k < 4; k += 1) out.push(0);
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
if (c < 33 || c > 117) throw damaged("ASCII85 data");
|
|
817
|
+
group.push(c - 33);
|
|
818
|
+
if (group.length === 5) {
|
|
819
|
+
flush(4);
|
|
820
|
+
group.length = 0;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (group.length === 1) throw damaged("ASCII85 data");
|
|
824
|
+
if (group.length > 1) flush(group.length - 1);
|
|
825
|
+
return out.done();
|
|
826
|
+
}
|
|
827
|
+
function runLength(data, work) {
|
|
828
|
+
const out = new Out(work, data.length * 2);
|
|
829
|
+
for (let i = 0; i < data.length;) {
|
|
830
|
+
const n = data[i];
|
|
831
|
+
i += 1;
|
|
832
|
+
if (n === 128) break;
|
|
833
|
+
if (n < 128) for (let k = 0; k <= n && i < data.length; k += 1, i += 1) out.push(data[i]);
|
|
834
|
+
else {
|
|
835
|
+
if (i >= data.length) break;
|
|
836
|
+
const b = data[i];
|
|
837
|
+
i += 1;
|
|
838
|
+
for (let k = 0; k < 257 - n; k += 1) out.push(b);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
return out.done();
|
|
842
|
+
}
|
|
843
|
+
const num = (v, fallback) => typeof v === "number" && Number.isSafeInteger(v) ? v : fallback;
|
|
844
|
+
function predict(data, parms, work) {
|
|
845
|
+
if (!parms) return data;
|
|
846
|
+
const predictor = num(parms.get("Predictor"), 1);
|
|
847
|
+
if (predictor <= 1) return data;
|
|
848
|
+
const colors = num(parms.get("Colors"), 1);
|
|
849
|
+
const bpc = num(parms.get("BitsPerComponent"), 8);
|
|
850
|
+
const columns = num(parms.get("Columns"), 1);
|
|
851
|
+
if (colors < 1 || colors > 32 || ![
|
|
852
|
+
1,
|
|
853
|
+
2,
|
|
854
|
+
4,
|
|
855
|
+
8,
|
|
856
|
+
16
|
|
857
|
+
].includes(bpc) || columns < 1 || columns > 1 << 24) throw damaged("predictor");
|
|
858
|
+
const rowBytes = Math.ceil(columns * colors * bpc / 8);
|
|
859
|
+
const bpp = Math.max(1, Math.ceil(colors * bpc / 8));
|
|
860
|
+
if (predictor === 2) {
|
|
861
|
+
if (bpc !== 8) throw damaged("predictor");
|
|
862
|
+
const out = data.slice();
|
|
863
|
+
for (let r = 0; r + rowBytes <= out.length; r += rowBytes) for (let i = colors; i < rowBytes; i += 1) out[r + i] = out[r + i] + out[r + i - colors] & 255;
|
|
864
|
+
work.checkpoint();
|
|
865
|
+
return out;
|
|
866
|
+
}
|
|
867
|
+
if (predictor < 10 || predictor > 15) throw damaged("predictor");
|
|
868
|
+
const rows = Math.floor(data.length / (rowBytes + 1));
|
|
869
|
+
const out = new Uint8Array(rows * rowBytes);
|
|
870
|
+
for (let r = 0; r < rows; r += 1) {
|
|
871
|
+
const type = data[r * (rowBytes + 1)];
|
|
872
|
+
const src = r * (rowBytes + 1) + 1;
|
|
873
|
+
const dst = r * rowBytes;
|
|
874
|
+
for (let i = 0; i < rowBytes; i += 1) {
|
|
875
|
+
const raw = data[src + i];
|
|
876
|
+
const left = i >= bpp ? out[dst + i - bpp] : 0;
|
|
877
|
+
const up = r > 0 ? out[dst - rowBytes + i] : 0;
|
|
878
|
+
const upLeft = r > 0 && i >= bpp ? out[dst - rowBytes + i - bpp] : 0;
|
|
879
|
+
let v;
|
|
880
|
+
switch (type) {
|
|
881
|
+
case 0:
|
|
882
|
+
v = raw;
|
|
883
|
+
break;
|
|
884
|
+
case 1:
|
|
885
|
+
v = raw + left;
|
|
886
|
+
break;
|
|
887
|
+
case 2:
|
|
888
|
+
v = raw + up;
|
|
889
|
+
break;
|
|
890
|
+
case 3:
|
|
891
|
+
v = raw + (left + up >> 1);
|
|
892
|
+
break;
|
|
893
|
+
case 4: {
|
|
894
|
+
const p = left + up - upLeft;
|
|
895
|
+
const pa = Math.abs(p - left);
|
|
896
|
+
const pb = Math.abs(p - up);
|
|
897
|
+
const pc = Math.abs(p - upLeft);
|
|
898
|
+
v = raw + (pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft);
|
|
899
|
+
break;
|
|
900
|
+
}
|
|
901
|
+
default: throw damaged("predictor");
|
|
902
|
+
}
|
|
903
|
+
out[dst + i] = v & 255;
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
work.checkpoint();
|
|
907
|
+
return out;
|
|
908
|
+
}
|
|
909
|
+
/** The filter names and parameters of a stream, resolved. */
|
|
910
|
+
function filtersOf(dict, resolve) {
|
|
911
|
+
const f = resolve(dict.get("Filter"));
|
|
912
|
+
const p = resolve(dict.get("DecodeParms") ?? dict.get("DP"));
|
|
913
|
+
const list = f === null || f === void 0 ? [] : Array.isArray(f) ? f.map((x) => resolve(x)) : [f];
|
|
914
|
+
if (list.length > MAX_FILTERS) throw damaged("too many filters");
|
|
915
|
+
const names = list.map((x) => {
|
|
916
|
+
if (!(x instanceof PdfName)) throw damaged("filter");
|
|
917
|
+
return x.name;
|
|
918
|
+
});
|
|
919
|
+
const plist = Array.isArray(p) ? p.map((x) => resolve(x)) : [p];
|
|
920
|
+
return {
|
|
921
|
+
names,
|
|
922
|
+
parms: names.map((_, i) => {
|
|
923
|
+
const v = plist[i];
|
|
924
|
+
return v instanceof PdfDict ? v : void 0;
|
|
925
|
+
})
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
/** Decodes `data` through `names` (in order), within the budget. */
|
|
929
|
+
function decode(data, names, parms, work) {
|
|
930
|
+
let out = data;
|
|
931
|
+
names.forEach((name, i) => {
|
|
932
|
+
const parm = parms[i];
|
|
933
|
+
switch (name) {
|
|
934
|
+
case "FlateDecode":
|
|
935
|
+
case "Fl":
|
|
936
|
+
out = predict(inflate(out, work), parm, work);
|
|
937
|
+
break;
|
|
938
|
+
case "LZWDecode":
|
|
939
|
+
case "LZW":
|
|
940
|
+
out = predict(lzw(out, num(parm?.get("EarlyChange"), 1) === 0 ? 0 : 1, work), parm, work);
|
|
941
|
+
break;
|
|
942
|
+
case "ASCIIHexDecode":
|
|
943
|
+
case "AHx":
|
|
944
|
+
out = asciiHex(out, work);
|
|
945
|
+
break;
|
|
946
|
+
case "ASCII85Decode":
|
|
947
|
+
case "A85":
|
|
948
|
+
out = ascii85(out, work);
|
|
949
|
+
break;
|
|
950
|
+
case "RunLengthDecode":
|
|
951
|
+
case "RL":
|
|
952
|
+
out = runLength(out, work);
|
|
953
|
+
break;
|
|
954
|
+
case "Crypt": {
|
|
955
|
+
const crypt = parm?.get("Name");
|
|
956
|
+
if (crypt instanceof PdfName && crypt.name !== "Identity") throw damaged("filter");
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
default: throw damaged("filter");
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
return out;
|
|
963
|
+
}
|
|
964
|
+
// src/crypt.ts
|
|
965
|
+
const concat = (...parts) => new Uint8Array(Buffer.concat(parts));
|
|
966
|
+
const equal = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
967
|
+
const md5 = (data) => new Uint8Array(createHash("md5").update(data).digest());
|
|
968
|
+
const sha = (alg, ...parts) => {
|
|
969
|
+
const h = createHash(alg);
|
|
970
|
+
for (const p of parts) h.update(p);
|
|
971
|
+
return new Uint8Array(h.digest());
|
|
972
|
+
};
|
|
973
|
+
/** RC4 (encryption and decryption are the same). OpenSSL 3 no longer offers it by default. */
|
|
974
|
+
function rc4(key, data) {
|
|
975
|
+
const s = /* @__PURE__ */ new Uint8Array(256);
|
|
976
|
+
for (let i = 0; i < 256; i += 1) s[i] = i;
|
|
977
|
+
for (let i = 0, j = 0; i < 256; i += 1) {
|
|
978
|
+
j = j + s[i] + key[i % key.length] & 255;
|
|
979
|
+
const t = s[i];
|
|
980
|
+
s[i] = s[j];
|
|
981
|
+
s[j] = t;
|
|
982
|
+
}
|
|
983
|
+
const out = new Uint8Array(data.length);
|
|
984
|
+
for (let k = 0, i = 0, j = 0; k < data.length; k += 1) {
|
|
985
|
+
i = i + 1 & 255;
|
|
986
|
+
j = j + s[i] & 255;
|
|
987
|
+
const t = s[i];
|
|
988
|
+
s[i] = s[j];
|
|
989
|
+
s[j] = t;
|
|
990
|
+
out[k] = data[k] ^ s[s[i] + s[j] & 255];
|
|
991
|
+
}
|
|
992
|
+
return out;
|
|
993
|
+
}
|
|
994
|
+
function aesNoPad(encrypt, key, iv, data) {
|
|
995
|
+
const alg = key.length === 32 ? "aes-256-cbc" : "aes-128-cbc";
|
|
996
|
+
const c = encrypt ? createCipheriv(alg, key, iv) : createDecipheriv(alg, key, iv);
|
|
997
|
+
c.setAutoPadding(false);
|
|
998
|
+
return new Uint8Array(Buffer.concat([c.update(data), c.final()]));
|
|
999
|
+
}
|
|
1000
|
+
/** PDF AES: a 16-byte IV, then CBC with PKCS#5 padding. Tolerates truncated or badly padded data. */
|
|
1001
|
+
function aesDecrypt(key, data) {
|
|
1002
|
+
if (data.length < 32) return /* @__PURE__ */ new Uint8Array(0);
|
|
1003
|
+
const raw = aesNoPad(false, key, data.subarray(0, 16), data.subarray(16, 16 + Math.floor((data.length - 16) / 16) * 16));
|
|
1004
|
+
const p = raw[raw.length - 1];
|
|
1005
|
+
return p >= 1 && p <= 16 && raw.subarray(raw.length - p).every((x) => x === p) ? raw.subarray(0, raw.length - p) : raw;
|
|
1006
|
+
}
|
|
1007
|
+
/** Padding string of Algorithm 2 (7.6.4.3.2). */
|
|
1008
|
+
const PASSWORD_PAD = new Uint8Array([
|
|
502
1009
|
40,
|
|
503
1010
|
191,
|
|
504
1011
|
78,
|
|
@@ -765,291 +1272,6 @@ function decryptStream(h, num, gen, dict, data, names, parms, resolve) {
|
|
|
765
1272
|
parms
|
|
766
1273
|
};
|
|
767
1274
|
}
|
|
768
|
-
// src/filters.ts
|
|
769
|
-
/** The most filters one stream may chain. */
|
|
770
|
-
const MAX_FILTERS = 8;
|
|
771
|
-
/** Output that stops at the room the budget leaves. */
|
|
772
|
-
var Out = class {
|
|
773
|
-
buf;
|
|
774
|
-
length = 0;
|
|
775
|
-
room;
|
|
776
|
-
limit;
|
|
777
|
-
work;
|
|
778
|
-
constructor(work, guess) {
|
|
779
|
-
const { room, limit } = work.inflateRoom();
|
|
780
|
-
this.room = room;
|
|
781
|
-
this.limit = limit;
|
|
782
|
-
this.work = work;
|
|
783
|
-
this.buf = new Uint8Array(Math.max(16, Math.min(guess, room)));
|
|
784
|
-
}
|
|
785
|
-
grow(need) {
|
|
786
|
-
if (need > this.room) throw limitError(this.limit, this.work.limits[this.limit], "a stream decompresses to too many bytes");
|
|
787
|
-
let size = this.buf.length;
|
|
788
|
-
while (size < need) size *= 2;
|
|
789
|
-
const next = new Uint8Array(Math.min(size, Math.max(need, this.room)));
|
|
790
|
-
next.set(this.buf.subarray(0, this.length));
|
|
791
|
-
this.buf = next;
|
|
792
|
-
}
|
|
793
|
-
push(byte) {
|
|
794
|
-
if (this.length >= this.buf.length) this.grow(this.length + 1);
|
|
795
|
-
this.buf[this.length] = byte;
|
|
796
|
-
this.length += 1;
|
|
797
|
-
}
|
|
798
|
-
/** Repeats `count` bytes starting at `from` of the output so far (LZW). */
|
|
799
|
-
copyWithin(from, count) {
|
|
800
|
-
if (this.length + count > this.buf.length) this.grow(this.length + count);
|
|
801
|
-
this.buf.copyWithin(this.length, from, from + count);
|
|
802
|
-
this.length += count;
|
|
803
|
-
}
|
|
804
|
-
byteAt(i) {
|
|
805
|
-
return this.buf[i];
|
|
806
|
-
}
|
|
807
|
-
done() {
|
|
808
|
-
this.work.inflate(this.length);
|
|
809
|
-
return this.buf.length === this.length ? this.buf : this.buf.slice(0, this.length);
|
|
810
|
-
}
|
|
811
|
-
};
|
|
812
|
-
function inflate(data, work) {
|
|
813
|
-
if (data.length === 0) return data;
|
|
814
|
-
const { room, limit } = work.inflateRoom();
|
|
815
|
-
let out;
|
|
816
|
-
try {
|
|
817
|
-
out = inflateSync(data, {
|
|
818
|
-
maxOutputLength: Math.max(1, room),
|
|
819
|
-
finishFlush: constants.Z_SYNC_FLUSH
|
|
820
|
-
});
|
|
821
|
-
} catch (error) {
|
|
822
|
-
if (error.code === "ERR_BUFFER_TOO_LARGE" || error instanceof RangeError) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
823
|
-
throw damaged("compressed data");
|
|
824
|
-
}
|
|
825
|
-
if (out.length > room) throw limitError(limit, work.limits[limit], "a stream decompresses to too many bytes");
|
|
826
|
-
work.inflate(out.length);
|
|
827
|
-
return out.byteOffset === 0 && out.buffer.byteLength === out.length ? new Uint8Array(out.buffer) : new Uint8Array(out);
|
|
828
|
-
}
|
|
829
|
-
function lzw(data, earlyChange, work) {
|
|
830
|
-
const out = new Out(work, data.length * 3);
|
|
831
|
-
const start = /* @__PURE__ */ new Int32Array(4096);
|
|
832
|
-
const size = /* @__PURE__ */ new Int32Array(4096);
|
|
833
|
-
let next = 258;
|
|
834
|
-
let width = 9;
|
|
835
|
-
let previous = -1;
|
|
836
|
-
let acc = 0;
|
|
837
|
-
let bits = 0;
|
|
838
|
-
for (let i = 0; i < data.length; i += 1) {
|
|
839
|
-
acc = (acc << 8 | data[i]) >>> 0;
|
|
840
|
-
bits += 8;
|
|
841
|
-
while (bits >= width) {
|
|
842
|
-
const code = acc >>> bits - width & (1 << width) - 1;
|
|
843
|
-
bits -= width;
|
|
844
|
-
acc &= (1 << bits) - 1;
|
|
845
|
-
if (code === 256) {
|
|
846
|
-
next = 258;
|
|
847
|
-
width = 9;
|
|
848
|
-
previous = -1;
|
|
849
|
-
continue;
|
|
850
|
-
}
|
|
851
|
-
if (code === 257) return out.done();
|
|
852
|
-
const at = out.length;
|
|
853
|
-
if (code < 256) out.push(code);
|
|
854
|
-
else if (code < next) out.copyWithin(start[code], size[code]);
|
|
855
|
-
else if (code === next && previous >= 0) {
|
|
856
|
-
if (previous < 256) out.push(previous);
|
|
857
|
-
else out.copyWithin(start[previous], size[previous]);
|
|
858
|
-
out.push(out.byteAt(at));
|
|
859
|
-
} else throw damaged("LZW data");
|
|
860
|
-
if (previous >= 0 && next < 4096) {
|
|
861
|
-
const prevLen = previous < 256 ? 1 : size[previous];
|
|
862
|
-
start[next] = at - prevLen;
|
|
863
|
-
size[next] = prevLen + 1;
|
|
864
|
-
next += 1;
|
|
865
|
-
}
|
|
866
|
-
if (next + earlyChange >= 1 << width && width < 12) width += 1;
|
|
867
|
-
previous = code;
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
return out.done();
|
|
871
|
-
}
|
|
872
|
-
function asciiHex(data, work) {
|
|
873
|
-
const out = new Out(work, data.length / 2 + 1);
|
|
874
|
-
let high = -1;
|
|
875
|
-
for (const c of data) {
|
|
876
|
-
if (c === 62) break;
|
|
877
|
-
if (isWhite(c)) continue;
|
|
878
|
-
const v = c >= 48 && c <= 57 ? c - 48 : c >= 65 && c <= 70 ? c - 55 : c >= 97 && c <= 102 ? c - 87 : -1;
|
|
879
|
-
if (v < 0) throw damaged("ASCIIHex data");
|
|
880
|
-
if (high < 0) high = v;
|
|
881
|
-
else {
|
|
882
|
-
out.push(high * 16 + v);
|
|
883
|
-
high = -1;
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
if (high >= 0) out.push(high * 16);
|
|
887
|
-
return out.done();
|
|
888
|
-
}
|
|
889
|
-
function ascii85(data, work) {
|
|
890
|
-
const out = new Out(work, data.length);
|
|
891
|
-
const group = [];
|
|
892
|
-
const flush = (count) => {
|
|
893
|
-
let n = 0;
|
|
894
|
-
for (let k = 0; k < 5; k += 1) n = n * 85 + (k < group.length ? group[k] : 84);
|
|
895
|
-
for (let k = 0; k < count; k += 1) out.push(n >>> 24 - 8 * k & 255);
|
|
896
|
-
};
|
|
897
|
-
for (let i = 0; i < data.length; i += 1) {
|
|
898
|
-
const c = data[i];
|
|
899
|
-
if (isWhite(c)) continue;
|
|
900
|
-
if (c === 126) break;
|
|
901
|
-
if (c === 122 && group.length === 0) {
|
|
902
|
-
for (let k = 0; k < 4; k += 1) out.push(0);
|
|
903
|
-
continue;
|
|
904
|
-
}
|
|
905
|
-
if (c < 33 || c > 117) throw damaged("ASCII85 data");
|
|
906
|
-
group.push(c - 33);
|
|
907
|
-
if (group.length === 5) {
|
|
908
|
-
flush(4);
|
|
909
|
-
group.length = 0;
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
if (group.length === 1) throw damaged("ASCII85 data");
|
|
913
|
-
if (group.length > 1) flush(group.length - 1);
|
|
914
|
-
return out.done();
|
|
915
|
-
}
|
|
916
|
-
function runLength(data, work) {
|
|
917
|
-
const out = new Out(work, data.length * 2);
|
|
918
|
-
for (let i = 0; i < data.length;) {
|
|
919
|
-
const n = data[i];
|
|
920
|
-
i += 1;
|
|
921
|
-
if (n === 128) break;
|
|
922
|
-
if (n < 128) for (let k = 0; k <= n && i < data.length; k += 1, i += 1) out.push(data[i]);
|
|
923
|
-
else {
|
|
924
|
-
if (i >= data.length) break;
|
|
925
|
-
const b = data[i];
|
|
926
|
-
i += 1;
|
|
927
|
-
for (let k = 0; k < 257 - n; k += 1) out.push(b);
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
return out.done();
|
|
931
|
-
}
|
|
932
|
-
const num = (v, fallback) => typeof v === "number" && Number.isSafeInteger(v) ? v : fallback;
|
|
933
|
-
function predict(data, parms, work) {
|
|
934
|
-
if (!parms) return data;
|
|
935
|
-
const predictor = num(parms.get("Predictor"), 1);
|
|
936
|
-
if (predictor <= 1) return data;
|
|
937
|
-
const colors = num(parms.get("Colors"), 1);
|
|
938
|
-
const bpc = num(parms.get("BitsPerComponent"), 8);
|
|
939
|
-
const columns = num(parms.get("Columns"), 1);
|
|
940
|
-
if (colors < 1 || colors > 32 || ![
|
|
941
|
-
1,
|
|
942
|
-
2,
|
|
943
|
-
4,
|
|
944
|
-
8,
|
|
945
|
-
16
|
|
946
|
-
].includes(bpc) || columns < 1 || columns > 1 << 24) throw damaged("predictor");
|
|
947
|
-
const rowBytes = Math.ceil(columns * colors * bpc / 8);
|
|
948
|
-
const bpp = Math.max(1, Math.ceil(colors * bpc / 8));
|
|
949
|
-
if (predictor === 2) {
|
|
950
|
-
if (bpc !== 8) throw damaged("predictor");
|
|
951
|
-
const out = data.slice();
|
|
952
|
-
for (let r = 0; r + rowBytes <= out.length; r += rowBytes) for (let i = colors; i < rowBytes; i += 1) out[r + i] = out[r + i] + out[r + i - colors] & 255;
|
|
953
|
-
work.checkpoint();
|
|
954
|
-
return out;
|
|
955
|
-
}
|
|
956
|
-
if (predictor < 10 || predictor > 15) throw damaged("predictor");
|
|
957
|
-
const rows = Math.floor(data.length / (rowBytes + 1));
|
|
958
|
-
const out = new Uint8Array(rows * rowBytes);
|
|
959
|
-
for (let r = 0; r < rows; r += 1) {
|
|
960
|
-
const type = data[r * (rowBytes + 1)];
|
|
961
|
-
const src = r * (rowBytes + 1) + 1;
|
|
962
|
-
const dst = r * rowBytes;
|
|
963
|
-
for (let i = 0; i < rowBytes; i += 1) {
|
|
964
|
-
const raw = data[src + i];
|
|
965
|
-
const left = i >= bpp ? out[dst + i - bpp] : 0;
|
|
966
|
-
const up = r > 0 ? out[dst - rowBytes + i] : 0;
|
|
967
|
-
const upLeft = r > 0 && i >= bpp ? out[dst - rowBytes + i - bpp] : 0;
|
|
968
|
-
let v;
|
|
969
|
-
switch (type) {
|
|
970
|
-
case 0:
|
|
971
|
-
v = raw;
|
|
972
|
-
break;
|
|
973
|
-
case 1:
|
|
974
|
-
v = raw + left;
|
|
975
|
-
break;
|
|
976
|
-
case 2:
|
|
977
|
-
v = raw + up;
|
|
978
|
-
break;
|
|
979
|
-
case 3:
|
|
980
|
-
v = raw + (left + up >> 1);
|
|
981
|
-
break;
|
|
982
|
-
case 4: {
|
|
983
|
-
const p = left + up - upLeft;
|
|
984
|
-
const pa = Math.abs(p - left);
|
|
985
|
-
const pb = Math.abs(p - up);
|
|
986
|
-
const pc = Math.abs(p - upLeft);
|
|
987
|
-
v = raw + (pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft);
|
|
988
|
-
break;
|
|
989
|
-
}
|
|
990
|
-
default: throw damaged("predictor");
|
|
991
|
-
}
|
|
992
|
-
out[dst + i] = v & 255;
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
work.checkpoint();
|
|
996
|
-
return out;
|
|
997
|
-
}
|
|
998
|
-
/** The filter names and parameters of a stream, resolved. */
|
|
999
|
-
function filtersOf(dict, resolve) {
|
|
1000
|
-
const f = resolve(dict.get("Filter"));
|
|
1001
|
-
const p = resolve(dict.get("DecodeParms") ?? dict.get("DP"));
|
|
1002
|
-
const list = f === null || f === void 0 ? [] : Array.isArray(f) ? f.map((x) => resolve(x)) : [f];
|
|
1003
|
-
if (list.length > MAX_FILTERS) throw damaged("too many filters");
|
|
1004
|
-
const names = list.map((x) => {
|
|
1005
|
-
if (!(x instanceof PdfName)) throw damaged("filter");
|
|
1006
|
-
return x.name;
|
|
1007
|
-
});
|
|
1008
|
-
const plist = Array.isArray(p) ? p.map((x) => resolve(x)) : [p];
|
|
1009
|
-
return {
|
|
1010
|
-
names,
|
|
1011
|
-
parms: names.map((_, i) => {
|
|
1012
|
-
const v = plist[i];
|
|
1013
|
-
return v instanceof PdfDict ? v : void 0;
|
|
1014
|
-
})
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
/** Decodes `data` through `names` (in order), within the budget. */
|
|
1018
|
-
function decode(data, names, parms, work) {
|
|
1019
|
-
let out = data;
|
|
1020
|
-
names.forEach((name, i) => {
|
|
1021
|
-
const parm = parms[i];
|
|
1022
|
-
switch (name) {
|
|
1023
|
-
case "FlateDecode":
|
|
1024
|
-
case "Fl":
|
|
1025
|
-
out = predict(inflate(out, work), parm, work);
|
|
1026
|
-
break;
|
|
1027
|
-
case "LZWDecode":
|
|
1028
|
-
case "LZW":
|
|
1029
|
-
out = predict(lzw(out, num(parm?.get("EarlyChange"), 1) === 0 ? 0 : 1, work), parm, work);
|
|
1030
|
-
break;
|
|
1031
|
-
case "ASCIIHexDecode":
|
|
1032
|
-
case "AHx":
|
|
1033
|
-
out = asciiHex(out, work);
|
|
1034
|
-
break;
|
|
1035
|
-
case "ASCII85Decode":
|
|
1036
|
-
case "A85":
|
|
1037
|
-
out = ascii85(out, work);
|
|
1038
|
-
break;
|
|
1039
|
-
case "RunLengthDecode":
|
|
1040
|
-
case "RL":
|
|
1041
|
-
out = runLength(out, work);
|
|
1042
|
-
break;
|
|
1043
|
-
case "Crypt": {
|
|
1044
|
-
const crypt = parm?.get("Name");
|
|
1045
|
-
if (crypt instanceof PdfName && crypt.name !== "Identity") throw damaged("filter");
|
|
1046
|
-
break;
|
|
1047
|
-
}
|
|
1048
|
-
default: throw damaged("filter");
|
|
1049
|
-
}
|
|
1050
|
-
});
|
|
1051
|
-
return out;
|
|
1052
|
-
}
|
|
1053
1275
|
// src/lexer.ts
|
|
1054
1276
|
/** The longest number token read in an object header. */
|
|
1055
1277
|
/** The longest keyword pdf.js reads; a longer one is an error there. */
|
|
@@ -1135,6 +1357,7 @@ function pdfNumber(bytes, at) {
|
|
|
1135
1357
|
const isNumberStart = (c) => c !== void 0 && (c >= 48 && c <= 57 || c === 43 || c === 45 || c === 46);
|
|
1136
1358
|
/** The bytes of a literal string, decoded as pdf.js decodes them (Lexer.getString). */
|
|
1137
1359
|
function decodeString(raw) {
|
|
1360
|
+
if (raw.indexOf(92) < 0) return raw.slice();
|
|
1138
1361
|
const out = [];
|
|
1139
1362
|
for (let i = 0; i < raw.length; i += 1) {
|
|
1140
1363
|
const c = raw[i];
|
|
@@ -1176,7 +1399,94 @@ function decodeString(raw) {
|
|
|
1176
1399
|
}
|
|
1177
1400
|
return new Uint8Array(out);
|
|
1178
1401
|
}
|
|
1179
|
-
/**
|
|
1402
|
+
/** Whether a hex string holds a character that is neither a hex digit nor white space (pdf.js skips it, qpdf does not). */
|
|
1403
|
+
function hexIrregular(raw) {
|
|
1404
|
+
for (const c of raw) if (hexDigit(c) < 0) return true;
|
|
1405
|
+
return raw.length % 2 === 1;
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Whether a literal string is outside its written form: a raw CR or LF in it (MuPDF and qpdf read a CR or CR LF
|
|
1409
|
+
* as LF, pdf.js and pypdf keep it), a backslash before a line end (pypdf takes two line ends after it as one), or
|
|
1410
|
+
* an escape readers read differently.
|
|
1411
|
+
*/
|
|
1412
|
+
function literalIrregular(raw) {
|
|
1413
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
1414
|
+
const c = raw[i];
|
|
1415
|
+
if (c === 13 || c === 10) return true;
|
|
1416
|
+
if (c === 92) {
|
|
1417
|
+
if (escapeIrregular(raw, i) || raw[i + 1] === 13 || raw[i + 1] === 10) return true;
|
|
1418
|
+
i += 1;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return false;
|
|
1422
|
+
}
|
|
1423
|
+
/**
|
|
1424
|
+
* Whether the escape at `i` (a backslash) is one readers read differently: an escape that is not defined (`\\q`),
|
|
1425
|
+
* an octal code above 377, or a backslash at the end of the data.
|
|
1426
|
+
*/
|
|
1427
|
+
function escapeIrregular(b, i) {
|
|
1428
|
+
const e = b[i + 1];
|
|
1429
|
+
if (e === void 0) return true;
|
|
1430
|
+
if (e >= 48 && e <= 55) {
|
|
1431
|
+
let x = e - 48;
|
|
1432
|
+
for (let n = 2; n <= 3 && b[i + n] >= 48 && b[i + n] <= 55; n += 1) x = x * 8 + b[i + n] - 48;
|
|
1433
|
+
return x > 255;
|
|
1434
|
+
}
|
|
1435
|
+
return !(e === 110 || e === 114 || e === 116 || e === 98 || e === 102 || e === 40 || e === 41 || e === 92 || e === 13 || e === 10);
|
|
1436
|
+
}
|
|
1437
|
+
/** The index of the first value in sorted `list` that is at least `x` (list.length when none). */
|
|
1438
|
+
function lowerBound(list, x) {
|
|
1439
|
+
let lo = 0;
|
|
1440
|
+
let hi = list.length;
|
|
1441
|
+
while (lo < hi) {
|
|
1442
|
+
const mid = lo + hi >>> 1;
|
|
1443
|
+
if (list[mid] < x) lo = mid + 1;
|
|
1444
|
+
else hi = mid;
|
|
1445
|
+
}
|
|
1446
|
+
return lo;
|
|
1447
|
+
}
|
|
1448
|
+
/** Whether a number token is written as an optional sign, digits and at most one point (`1.2.3`, `--1` are not). */
|
|
1449
|
+
function numberIrregular(b, start, end) {
|
|
1450
|
+
let points = 0;
|
|
1451
|
+
let digits = 0;
|
|
1452
|
+
for (let i = start; i < end; i += 1) {
|
|
1453
|
+
const c = b[i];
|
|
1454
|
+
if (c === 46) points += 1;
|
|
1455
|
+
else if (c === 43 || c === 45) {
|
|
1456
|
+
if (i !== start) return true;
|
|
1457
|
+
} else if (c >= 48 && c <= 57) digits += 1;
|
|
1458
|
+
else return true;
|
|
1459
|
+
}
|
|
1460
|
+
return points > 1 || digits === 0;
|
|
1461
|
+
}
|
|
1462
|
+
/** A control byte that is not white space (C0 other than NUL, HT, LF, FF, CR; and DEL): not part of PDF syntax. */
|
|
1463
|
+
const isControl = (c) => c < 32 && c !== 0 && c !== 9 && c !== 10 && c !== 12 && c !== 13 || c === 127;
|
|
1464
|
+
/** Whether the white space from `from` to `to` holds no NUL, form feed or vertical tab (read differently by some readers). */
|
|
1465
|
+
function plainGap(b, from, to) {
|
|
1466
|
+
for (let i = from; i < to; i += 1) if (b[i] === 0 || b[i] === 11 || b[i] === 12) return false;
|
|
1467
|
+
return true;
|
|
1468
|
+
}
|
|
1469
|
+
/** Space, tab, CR or LF: the white space every reader skips alike. */
|
|
1470
|
+
const isPlainSpace = (c) => c === 32 || c === 9 || c === 10 || c === 13;
|
|
1471
|
+
/** Whether a key of `entries` is written twice. */
|
|
1472
|
+
function repeatsKey(entries) {
|
|
1473
|
+
if (entries.length <= 12) {
|
|
1474
|
+
for (let i = 1; i < entries.length; i += 1) for (let j = 0; j < i; j += 1) if (entries[i][0] === entries[j][0]) return true;
|
|
1475
|
+
return false;
|
|
1476
|
+
}
|
|
1477
|
+
return new Set(entries.map((e) => e[0])).size !== entries.length;
|
|
1478
|
+
}
|
|
1479
|
+
/** Whether `b` from `start` to `end` is space, tab, CR and LF only. */
|
|
1480
|
+
function spaceOnly(b, start, end) {
|
|
1481
|
+
for (let i = start; i < end; i += 1) if (!isPlainSpace(b[i])) return false;
|
|
1482
|
+
return true;
|
|
1483
|
+
}
|
|
1484
|
+
/** Whether `b` from `start` to `end` is digits only. */
|
|
1485
|
+
function digitsOnly(b, start, end) {
|
|
1486
|
+
for (let i = start; i < end; i += 1) if (!isDigit(b[i])) return false;
|
|
1487
|
+
return end > start;
|
|
1488
|
+
}
|
|
1489
|
+
/** The bytes of a hex string, decoded as pdf.js decodes them: other characters ignored, an odd digit padded. */
|
|
1180
1490
|
function decodeHex(raw) {
|
|
1181
1491
|
const out = [];
|
|
1182
1492
|
let first = -1;
|
|
@@ -1222,12 +1532,92 @@ const STR = 3;
|
|
|
1222
1532
|
const HEX = 4;
|
|
1223
1533
|
const CMD = 5;
|
|
1224
1534
|
const LIT = 6;
|
|
1535
|
+
const FORM_GAP = 1;
|
|
1536
|
+
const FORM_COMMENT = 2;
|
|
1537
|
+
const FORM_COMMENTS = 6;
|
|
1538
|
+
const FORM_TOKEN = 8;
|
|
1225
1539
|
const isCmd = (t, v) => t.k === CMD && t.v === v;
|
|
1540
|
+
/** With how each encoding writes U+FFFD (as Latin-1 characters): a decoded U+FFFD without it marks invalid bytes. */
|
|
1541
|
+
const DECODERS = [["utf-8", "�"], ["gbk", "1¤7"]].map(([label, replacement]) => ({
|
|
1542
|
+
lenient: new TextDecoder(label),
|
|
1543
|
+
strict: new TextDecoder(label, { fatal: true }),
|
|
1544
|
+
replacement
|
|
1545
|
+
}));
|
|
1546
|
+
/** `bytes` decoded with `d`, or null when they are not valid in its encoding (an exception only in the rare case of a U+FFFD written in them). */
|
|
1547
|
+
function decodeOrNull(d, bytes) {
|
|
1548
|
+
const text = d.lenient.decode(bytes);
|
|
1549
|
+
if (!text.includes("�")) return text;
|
|
1550
|
+
try {
|
|
1551
|
+
return d.strict.decode(bytes);
|
|
1552
|
+
} catch {
|
|
1553
|
+
return null;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
/** Whether two keys are one: written twice, or two that pypdf decodes to one text. */
|
|
1557
|
+
function pypdfRepeats(entries) {
|
|
1558
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1559
|
+
const wide = [];
|
|
1560
|
+
for (const [k] of entries) {
|
|
1561
|
+
if (seen.has(k)) return true;
|
|
1562
|
+
seen.add(k);
|
|
1563
|
+
if (WIDE.test(k)) wide.push(k);
|
|
1564
|
+
}
|
|
1565
|
+
const decoded = /* @__PURE__ */ new Map();
|
|
1566
|
+
pypdfNames(wide).forEach((text, k) => {
|
|
1567
|
+
if (text !== wide[k]) decoded.set(wide[k], text);
|
|
1568
|
+
});
|
|
1569
|
+
const texts = /* @__PURE__ */ new Set();
|
|
1570
|
+
for (const text of decoded.values()) {
|
|
1571
|
+
if (texts.has(text) || seen.has(text) && !decoded.has(text)) return true;
|
|
1572
|
+
texts.add(text);
|
|
1573
|
+
}
|
|
1574
|
+
return false;
|
|
1575
|
+
}
|
|
1576
|
+
const WIDE = /[\x80-\xff]/;
|
|
1577
|
+
/**
|
|
1578
|
+
* Names (their bytes as Latin-1 characters) as pypdf decodes them: UTF-8, else GBK, else Latin-1. Names above ASCII
|
|
1579
|
+
* are decoded together, one call per encoding, joined by an ASCII byte none of them holds (an ASCII byte ends any
|
|
1580
|
+
* byte sequence in both encodings); only when every ASCII byte occurs in them is each name decoded on its own.
|
|
1581
|
+
*/
|
|
1582
|
+
function pypdfNames(names) {
|
|
1583
|
+
const out = [...names];
|
|
1584
|
+
let pending = names.map((_, i) => i);
|
|
1585
|
+
let free = 10;
|
|
1586
|
+
while (free < 128 && pending.some((i) => names[i].includes(String.fromCharCode(free)))) free = free === 10 ? 0 : free + 1 === 10 ? 11 : free + 1;
|
|
1587
|
+
if (free >= 128) free = -1;
|
|
1588
|
+
if (free < 0) {
|
|
1589
|
+
const one = (d, name) => {
|
|
1590
|
+
const t = d.lenient.decode(Buffer.from(name, "latin1"));
|
|
1591
|
+
return !t.includes("�") ? t : name.includes(d.replacement) ? decodeOrNull(d, Buffer.from(name, "latin1")) : null;
|
|
1592
|
+
};
|
|
1593
|
+
for (const i of pending) out[i] = one(DECODERS[0], names[i]) ?? one(DECODERS[1], names[i]) ?? names[i];
|
|
1594
|
+
return out;
|
|
1595
|
+
}
|
|
1596
|
+
const separator = String.fromCharCode(free);
|
|
1597
|
+
for (const d of DECODERS) {
|
|
1598
|
+
if (pending.length === 0) break;
|
|
1599
|
+
const texts = d.lenient.decode(Buffer.from(pending.map((i) => names[i]).join(separator), "latin1")).split(separator);
|
|
1600
|
+
const left = [];
|
|
1601
|
+
pending.forEach((i, k) => {
|
|
1602
|
+
const t = !texts[k].includes("�") ? texts[k] : names[i].includes(d.replacement) ? decodeOrNull(d, Buffer.from(names[i], "latin1")) : null;
|
|
1603
|
+
if (t === null) left.push(i);
|
|
1604
|
+
else out[i] = t;
|
|
1605
|
+
});
|
|
1606
|
+
pending = left;
|
|
1607
|
+
}
|
|
1608
|
+
return out;
|
|
1609
|
+
}
|
|
1226
1610
|
/** A parser over one buffer: the file, or the decompressed data of an object stream. */
|
|
1227
1611
|
var Lexer = class {
|
|
1612
|
+
/** Whether a reference with a generation other than 0 was read (the description compares generations only then). */
|
|
1613
|
+
generations = false;
|
|
1228
1614
|
bytes;
|
|
1229
1615
|
work;
|
|
1230
1616
|
pos = 0;
|
|
1617
|
+
/** The canonical form of the value being read: whether it is still being followed, broken, and its comments. */
|
|
1618
|
+
formOpen = false;
|
|
1619
|
+
formBad = false;
|
|
1620
|
+
formComments = 0;
|
|
1231
1621
|
buf1;
|
|
1232
1622
|
buf2;
|
|
1233
1623
|
depth = 0;
|
|
@@ -1240,6 +1630,7 @@ var Lexer = class {
|
|
|
1240
1630
|
/** How streams are read in this reading (see ReadOptions). */
|
|
1241
1631
|
options = {};
|
|
1242
1632
|
nestedStream = false;
|
|
1633
|
+
irregular = false;
|
|
1243
1634
|
indirectLength = false;
|
|
1244
1635
|
lengthCandidates = 0;
|
|
1245
1636
|
/** Where the last token used ends. */
|
|
@@ -1248,11 +1639,16 @@ var Lexer = class {
|
|
|
1248
1639
|
endstreams;
|
|
1249
1640
|
/** Where the string opened by the parenthesis at each offset closes, plus one (0: not known). */
|
|
1250
1641
|
closes;
|
|
1251
|
-
/** The last hex string scanned: no `>` lies between its start and its end. */
|
|
1642
|
+
/** The last hex string scanned: no `>` lies between its start and its end; `bad`, its last character that is neither a hex digit nor white space (-1: none). */
|
|
1252
1643
|
lastHex = {
|
|
1253
1644
|
start: -1,
|
|
1254
|
-
end: -1
|
|
1645
|
+
end: -1,
|
|
1646
|
+
bad: -1
|
|
1255
1647
|
};
|
|
1648
|
+
/** Where the string scans found an escape readers read differently, in order once sorted. */
|
|
1649
|
+
escapes = [];
|
|
1650
|
+
/** How far string scans have noted escapes: each escape is noted once, in order, so the list stays sorted. */
|
|
1651
|
+
escapesTo = 0;
|
|
1256
1652
|
constructor(bytes, work) {
|
|
1257
1653
|
this.bytes = bytes;
|
|
1258
1654
|
this.work = work;
|
|
@@ -1264,26 +1660,61 @@ var Lexer = class {
|
|
|
1264
1660
|
this.closes = void 0;
|
|
1265
1661
|
this.lastHex = {
|
|
1266
1662
|
start: -1,
|
|
1267
|
-
end: -1
|
|
1663
|
+
end: -1,
|
|
1664
|
+
bad: -1
|
|
1268
1665
|
};
|
|
1666
|
+
this.escapes = [];
|
|
1667
|
+
this.escapesTo = 0;
|
|
1269
1668
|
}
|
|
1270
1669
|
token() {
|
|
1271
|
-
const odd = {
|
|
1670
|
+
const odd = {
|
|
1671
|
+
flag: false,
|
|
1672
|
+
space: false,
|
|
1673
|
+
form: 0
|
|
1674
|
+
};
|
|
1272
1675
|
const t = this.readToken(odd);
|
|
1273
1676
|
if (odd.flag) t.odd = true;
|
|
1677
|
+
if (odd.space) t.irregular = true;
|
|
1678
|
+
let form = odd.form;
|
|
1679
|
+
if (t.k === NUM) {
|
|
1680
|
+
const plain = t.end - t.start <= 20 && plainNumber(this.bytes, t.start, t.end);
|
|
1681
|
+
if (!plain) form |= FORM_TOKEN;
|
|
1682
|
+
if (!plain && numberIrregular(this.bytes, t.start, t.end) || isNumberStart(this.bytes[t.end]) || t.end < this.bytes.length && this.cls[this.bytes[t.end]] === 0) t.irregular = true;
|
|
1683
|
+
} else if (t.k === CMD) {
|
|
1684
|
+
if (this.hasControl(t.start, t.end)) t.irregular = true;
|
|
1685
|
+
if (t.v === "{" || t.v === "}" || t.v === ")" || t.v === ">") form |= FORM_TOKEN;
|
|
1686
|
+
}
|
|
1687
|
+
if (form !== 0) t.form = form;
|
|
1274
1688
|
return t;
|
|
1275
1689
|
}
|
|
1690
|
+
hasControl(start, end) {
|
|
1691
|
+
for (let i = start; i < end; i += 1) if (isControl(this.bytes[i])) return true;
|
|
1692
|
+
return false;
|
|
1693
|
+
}
|
|
1276
1694
|
readToken(odd) {
|
|
1277
1695
|
const b = this.bytes;
|
|
1278
1696
|
const CLASS = this.cls;
|
|
1279
1697
|
let i = this.pos;
|
|
1698
|
+
let form = 0;
|
|
1280
1699
|
for (;;) {
|
|
1281
|
-
|
|
1700
|
+
const run = i;
|
|
1701
|
+
while (i < b.length && CLASS[b[i]] === 1) {
|
|
1702
|
+
const c = b[i];
|
|
1703
|
+
if (c !== 32 && c !== 10 && c !== 13) {
|
|
1704
|
+
form |= FORM_GAP;
|
|
1705
|
+
if (c === 0 || c === 12) odd.space = true;
|
|
1706
|
+
}
|
|
1707
|
+
i += 1;
|
|
1708
|
+
}
|
|
1709
|
+
if (i - run > 8) form |= FORM_GAP;
|
|
1282
1710
|
if (b[i] !== 37) break;
|
|
1283
1711
|
const end = commentEnd(b, i);
|
|
1284
1712
|
if (end - i >= SCAN_CHECK) this.work.checkpoint();
|
|
1713
|
+
if (end - i > 128) form |= FORM_GAP;
|
|
1714
|
+
if ((form & FORM_COMMENTS) !== FORM_COMMENTS) form += FORM_COMMENT;
|
|
1285
1715
|
i = end;
|
|
1286
1716
|
}
|
|
1717
|
+
odd.form = form;
|
|
1287
1718
|
const start = i;
|
|
1288
1719
|
if (i >= b.length) {
|
|
1289
1720
|
this.pos = i;
|
|
@@ -1434,9 +1865,13 @@ var Lexer = class {
|
|
|
1434
1865
|
const special = (c) => c === void 0 || CLASS[c] !== 0;
|
|
1435
1866
|
let i = at + 1;
|
|
1436
1867
|
let out = "";
|
|
1868
|
+
let irregular = false;
|
|
1869
|
+
let wide = false;
|
|
1437
1870
|
while (i < b.length && !special(b[i])) {
|
|
1438
1871
|
const c = b[i];
|
|
1439
1872
|
i += 1;
|
|
1873
|
+
if (c > 126) wide = true;
|
|
1874
|
+
if (isControl(c)) irregular = true;
|
|
1440
1875
|
if (c !== 35) {
|
|
1441
1876
|
out += String.fromCharCode(c);
|
|
1442
1877
|
continue;
|
|
@@ -1444,11 +1879,13 @@ var Lexer = class {
|
|
|
1444
1879
|
const c1 = b[i];
|
|
1445
1880
|
if (special(c1)) {
|
|
1446
1881
|
out += "#";
|
|
1882
|
+
irregular = true;
|
|
1447
1883
|
break;
|
|
1448
1884
|
}
|
|
1449
1885
|
const x = hexDigit(c1);
|
|
1450
1886
|
if (x < 0) {
|
|
1451
1887
|
out += "#" + String.fromCharCode(c1);
|
|
1888
|
+
irregular = true;
|
|
1452
1889
|
i += 1;
|
|
1453
1890
|
continue;
|
|
1454
1891
|
}
|
|
@@ -1456,22 +1893,29 @@ var Lexer = class {
|
|
|
1456
1893
|
const x2 = hexDigit(c2);
|
|
1457
1894
|
if (x2 < 0) {
|
|
1458
1895
|
out += "#" + String.fromCharCode(c1);
|
|
1896
|
+
irregular = true;
|
|
1459
1897
|
i += 1;
|
|
1460
1898
|
if (special(c2)) break;
|
|
1461
1899
|
out += String.fromCharCode(c2);
|
|
1462
1900
|
i += 1;
|
|
1463
1901
|
continue;
|
|
1464
1902
|
}
|
|
1465
|
-
|
|
1903
|
+
if (x === 0 && x2 === 0) irregular = true;
|
|
1904
|
+
const v = x << 4 | x2;
|
|
1905
|
+
if (v > 126) wide = true;
|
|
1906
|
+
out += String.fromCharCode(v);
|
|
1466
1907
|
i += 2;
|
|
1467
1908
|
}
|
|
1909
|
+
if (out.length > 127) irregular = true;
|
|
1468
1910
|
if (!this.qpdf && out.includes("\v")) this.ambiguous = true;
|
|
1469
1911
|
this.pos = i;
|
|
1470
1912
|
return {
|
|
1471
1913
|
k: NAME,
|
|
1472
1914
|
v: out,
|
|
1473
1915
|
start: at,
|
|
1474
|
-
end: i
|
|
1916
|
+
end: i,
|
|
1917
|
+
...irregular ? { irregular } : {},
|
|
1918
|
+
...wide ? { wide } : {}
|
|
1475
1919
|
};
|
|
1476
1920
|
}
|
|
1477
1921
|
/**
|
|
@@ -1487,8 +1931,10 @@ var Lexer = class {
|
|
|
1487
1931
|
let checked = at;
|
|
1488
1932
|
for (let i = at; i < b.length; i += 1) {
|
|
1489
1933
|
const c = b[i];
|
|
1490
|
-
if (c === 92)
|
|
1491
|
-
|
|
1934
|
+
if (c === 92) {
|
|
1935
|
+
if (i >= this.escapesTo && escapeIrregular(b, i)) this.escapes.push(i);
|
|
1936
|
+
i += 1;
|
|
1937
|
+
} else if (c === 40) open.push(i);
|
|
1492
1938
|
else if (c === 41) {
|
|
1493
1939
|
const o = open.pop();
|
|
1494
1940
|
if (open.length === 0) {
|
|
@@ -1507,8 +1953,14 @@ var Lexer = class {
|
|
|
1507
1953
|
this.closes ??= new Int32Array(b.length + 1);
|
|
1508
1954
|
for (const o of open) this.closes[o] = b.length + 1;
|
|
1509
1955
|
end = b.length;
|
|
1956
|
+
} else {
|
|
1957
|
+
this.closes ??= new Int32Array(b.length + 1);
|
|
1958
|
+
this.closes[at] = end + 1;
|
|
1510
1959
|
}
|
|
1960
|
+
this.escapesTo = Math.max(this.escapesTo, end);
|
|
1511
1961
|
}
|
|
1962
|
+
const k = lowerBound(this.escapes, at);
|
|
1963
|
+
const irregular = k < this.escapes.length && this.escapes[k] < Math.min(end, b.length);
|
|
1512
1964
|
if (end >= b.length) {
|
|
1513
1965
|
this.pos = b.length;
|
|
1514
1966
|
return {
|
|
@@ -1516,7 +1968,8 @@ var Lexer = class {
|
|
|
1516
1968
|
a: at + 1,
|
|
1517
1969
|
b: b.length,
|
|
1518
1970
|
start: at,
|
|
1519
|
-
end: b.length
|
|
1971
|
+
end: b.length,
|
|
1972
|
+
...irregular ? { irregular } : {}
|
|
1520
1973
|
};
|
|
1521
1974
|
}
|
|
1522
1975
|
this.pos = end + 1;
|
|
@@ -1525,31 +1978,51 @@ var Lexer = class {
|
|
|
1525
1978
|
a: at + 1,
|
|
1526
1979
|
b: end,
|
|
1527
1980
|
start: at,
|
|
1528
|
-
end: end + 1
|
|
1981
|
+
end: end + 1,
|
|
1982
|
+
...irregular ? { irregular } : {}
|
|
1529
1983
|
};
|
|
1530
1984
|
}
|
|
1531
1985
|
/** A hex string, up to `>` or the end of data. */
|
|
1532
1986
|
hex(at) {
|
|
1533
1987
|
const b = this.bytes;
|
|
1534
|
-
|
|
1988
|
+
const inner = at > this.lastHex.start && at < this.lastHex.end;
|
|
1989
|
+
let end = inner ? this.lastHex.end : b.indexOf(62, at + 1);
|
|
1535
1990
|
if (end < 0) end = b.length;
|
|
1536
1991
|
if (end - at >= SCAN_CHECK) this.work.checkpoint();
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
end
|
|
1540
|
-
|
|
1992
|
+
if (!inner) {
|
|
1993
|
+
let bad = -1;
|
|
1994
|
+
for (let i = end - 1; i > at; i -= 1) if (hexDigit(b[i]) < 0 && !isPlainSpace(b[i])) {
|
|
1995
|
+
bad = i;
|
|
1996
|
+
break;
|
|
1997
|
+
}
|
|
1998
|
+
this.lastHex = {
|
|
1999
|
+
start: at,
|
|
2000
|
+
end,
|
|
2001
|
+
bad
|
|
2002
|
+
};
|
|
2003
|
+
}
|
|
1541
2004
|
this.pos = Math.min(b.length, end + 1);
|
|
2005
|
+
const irregular = this.lastHex.bad > at;
|
|
1542
2006
|
return {
|
|
1543
2007
|
k: HEX,
|
|
1544
2008
|
a: at + 1,
|
|
1545
2009
|
b: end,
|
|
1546
2010
|
start: at,
|
|
1547
|
-
end: this.pos
|
|
2011
|
+
end: this.pos,
|
|
2012
|
+
...irregular ? { irregular } : {}
|
|
1548
2013
|
};
|
|
1549
2014
|
}
|
|
2015
|
+
/** Adds a token's form (or, with `gapOnly`, that of the white space and comments before it) to the value's. */
|
|
2016
|
+
addForm(form, gapOnly) {
|
|
2017
|
+
if (form === void 0 || !this.formOpen) return;
|
|
2018
|
+
if (form & FORM_GAP || !gapOnly && form & FORM_TOKEN) this.formBad = true;
|
|
2019
|
+
this.formComments += (form & FORM_COMMENTS) >> 1;
|
|
2020
|
+
}
|
|
1550
2021
|
/** Uses up `buf1`. */
|
|
1551
2022
|
shift() {
|
|
2023
|
+
if (this.buf1.form !== void 0) this.addForm(this.buf1.form, false);
|
|
1552
2024
|
if (this.buf1.odd) this.odd = true;
|
|
2025
|
+
if (this.buf1.irregular) this.irregular = true;
|
|
1553
2026
|
this.consumedEnd = this.buf1.end;
|
|
1554
2027
|
this.buf1 = this.buf2;
|
|
1555
2028
|
this.buf2 = this.token();
|
|
@@ -1577,30 +2050,42 @@ var Lexer = class {
|
|
|
1577
2050
|
this.odd = true;
|
|
1578
2051
|
return null;
|
|
1579
2052
|
case NUM:
|
|
2053
|
+
if (Number.isInteger(t.v) && !Number.isSafeInteger(t.v)) this.irregular = true;
|
|
1580
2054
|
if (Number.isInteger(t.v) && this.buf1.k === NUM && Number.isInteger(this.buf1.v) && isCmd(this.buf2, "R")) {
|
|
1581
2055
|
const gen = this.buf1.v;
|
|
2056
|
+
if (this.formOpen) {
|
|
2057
|
+
const b = this.bytes;
|
|
2058
|
+
const g = this.buf1;
|
|
2059
|
+
if (g.start !== t.end + 1 || b[t.end] !== 32 || this.buf2.start !== g.end + 1 || b[g.end] !== 32 || !plainDigits(b, t.start, t.end, 7) || !plainDigits(b, g.start, g.end, 5)) this.formBad = true;
|
|
2060
|
+
}
|
|
2061
|
+
if (!digitsOnly(this.bytes, t.start, t.end) || !digitsOnly(this.bytes, this.buf1.start, this.buf1.end) || this.buf1.start === t.end || this.buf2.start === this.buf1.end || !spaceOnly(this.bytes, t.end, this.buf1.start) || !spaceOnly(this.bytes, this.buf1.end, this.buf2.start)) this.irregular = true;
|
|
1582
2062
|
this.shift();
|
|
1583
2063
|
this.shift();
|
|
2064
|
+
if (t.v === 0 || gen >= 65535) this.irregular = true;
|
|
2065
|
+
if (t.v > 8388607) this.irregular = true;
|
|
1584
2066
|
if (t.v < 0 || gen < 0 || !Number.isSafeInteger(t.v) || !Number.isSafeInteger(gen)) {
|
|
1585
2067
|
this.odd = true;
|
|
1586
2068
|
return null;
|
|
1587
2069
|
}
|
|
2070
|
+
if (gen !== 0) this.generations = true;
|
|
1588
2071
|
return new PdfRef(t.v, gen);
|
|
1589
2072
|
}
|
|
1590
2073
|
return t.v;
|
|
1591
2074
|
case NAME: return new PdfName(t.v);
|
|
1592
2075
|
case STR: {
|
|
1593
2076
|
const raw = this.bytes.subarray(t.a, t.b);
|
|
1594
|
-
return new PdfString(() => decodeString(raw));
|
|
2077
|
+
return new PdfString(() => decodeString(raw), () => literalIrregular(raw), raw.length);
|
|
1595
2078
|
}
|
|
1596
2079
|
case HEX: {
|
|
1597
2080
|
const raw = this.bytes.subarray(t.a, t.b);
|
|
1598
|
-
return new PdfString(() => decodeHex(raw));
|
|
2081
|
+
return new PdfString(() => decodeHex(raw), () => hexIrregular(raw), raw.length);
|
|
1599
2082
|
}
|
|
1600
2083
|
case LIT: return t.v;
|
|
1601
2084
|
case CMD:
|
|
1602
2085
|
if (t.v === "[") return this.array();
|
|
1603
2086
|
if (t.v === "<<") return this.dict();
|
|
2087
|
+
this.irregular = true;
|
|
2088
|
+
if (t.v === "R") this.formBad = true;
|
|
1604
2089
|
return new PdfKeyword(t.v);
|
|
1605
2090
|
}
|
|
1606
2091
|
}
|
|
@@ -1623,6 +2108,7 @@ var Lexer = class {
|
|
|
1623
2108
|
this.enter();
|
|
1624
2109
|
try {
|
|
1625
2110
|
const entries = [];
|
|
2111
|
+
let wideKeys = false;
|
|
1626
2112
|
if (this.qpdf) {
|
|
1627
2113
|
const items = [];
|
|
1628
2114
|
while (!isCmd(this.buf1, ">>") && this.buf1.k !== EOF) items.push(this.getObj());
|
|
@@ -1634,22 +2120,28 @@ var Lexer = class {
|
|
|
1634
2120
|
while (!this.qpdf && !isCmd(this.buf1, ">>") && this.buf1.k !== EOF) {
|
|
1635
2121
|
if (this.buf1.k !== NAME) {
|
|
1636
2122
|
this.ambiguous = true;
|
|
2123
|
+
this.irregular = true;
|
|
1637
2124
|
this.countValue();
|
|
1638
2125
|
this.shift();
|
|
1639
2126
|
continue;
|
|
1640
2127
|
}
|
|
1641
2128
|
const key = this.buf1.v;
|
|
2129
|
+
if (this.buf1.wide) wideKeys = true;
|
|
1642
2130
|
this.shift();
|
|
1643
2131
|
if (this.buf1.k === EOF) break;
|
|
1644
2132
|
if (isCmd(this.buf1, ">>")) this.ambiguous = true;
|
|
1645
2133
|
entries.push([key, this.getObj()]);
|
|
1646
2134
|
}
|
|
2135
|
+
if (entries.length > 1 && (wideKeys ? pypdfRepeats(entries) : repeatsKey(entries))) this.irregular = true;
|
|
1647
2136
|
const d = new PdfDict(entries);
|
|
1648
2137
|
if (this.buf1.k === EOF) {
|
|
1649
2138
|
this.odd = true;
|
|
1650
2139
|
return d;
|
|
1651
2140
|
}
|
|
1652
2141
|
if (isCmd(this.buf2, "stream")) {
|
|
2142
|
+
this.addForm(this.buf1.form, false);
|
|
2143
|
+
this.addForm(this.buf2.form, true);
|
|
2144
|
+
this.formOpen = false;
|
|
1653
2145
|
const nested = this.depth > 1;
|
|
1654
2146
|
if (nested) this.nestedStream = true;
|
|
1655
2147
|
if (!(nested && this.options.mode === "noNested")) {
|
|
@@ -1666,6 +2158,8 @@ var Lexer = class {
|
|
|
1666
2158
|
/** The data of a stream whose dictionary just ended: pdf.js Parser.makeStream. */
|
|
1667
2159
|
stream(d) {
|
|
1668
2160
|
const b = this.bytes;
|
|
2161
|
+
const kw = this.buf2.end;
|
|
2162
|
+
if (!(b[kw] === 10 || b[kw] === 13 && b[kw + 1] === 10) || b.subarray(this.buf1.end, this.buf2.start).includes(37)) this.irregular = true;
|
|
1669
2163
|
let start = commentEnd(b, this.buf2.end);
|
|
1670
2164
|
if (b[start] === 13) start += b[start + 1] === 10 ? 2 : 1;
|
|
1671
2165
|
else if (b[start] === 10) start += 1;
|
|
@@ -1686,6 +2180,9 @@ var Lexer = class {
|
|
|
1686
2180
|
if (isCmd(next, "endstream")) {
|
|
1687
2181
|
length = wanted;
|
|
1688
2182
|
this.pos = next.end;
|
|
2183
|
+
const gap = next.start - (start + wanted);
|
|
2184
|
+
const at = start + wanted;
|
|
2185
|
+
if (!(gap === 0 || gap === 1 && (b[at] === 10 || b[at] === 13) || gap === 2 && b[at] === 13 && b[at + 1] === 10)) this.irregular = true;
|
|
1689
2186
|
}
|
|
1690
2187
|
}
|
|
1691
2188
|
if (length < 0) {
|
|
@@ -1703,6 +2200,7 @@ var Lexer = class {
|
|
|
1703
2200
|
return;
|
|
1704
2201
|
}
|
|
1705
2202
|
if (at.offset >= b.length - 9) this.odd = true;
|
|
2203
|
+
if (this.options.mode !== "first" && !(declared instanceof PdfRef && wanted === void 0)) this.irregular = true;
|
|
1706
2204
|
length = at.offset - start;
|
|
1707
2205
|
this.pos = at.offset;
|
|
1708
2206
|
this.pos = this.token().end;
|
|
@@ -1757,15 +2255,23 @@ var Lexer = class {
|
|
|
1757
2255
|
this.odd = false;
|
|
1758
2256
|
this.ambiguous = false;
|
|
1759
2257
|
this.nestedStream = false;
|
|
2258
|
+
this.irregular = false;
|
|
1760
2259
|
this.indirectLength = false;
|
|
1761
2260
|
this.lengthCandidates = 0;
|
|
1762
2261
|
this.qpdf = qpdf;
|
|
1763
2262
|
this.cls = qpdf ? CLASS_QPDF : CLASS;
|
|
1764
2263
|
this.options = options;
|
|
2264
|
+
this.formOpen = true;
|
|
2265
|
+
this.formBad = false;
|
|
2266
|
+
this.formComments = 0;
|
|
1765
2267
|
try {
|
|
1766
2268
|
this.refill(offset);
|
|
2269
|
+
const value = this.getObj();
|
|
2270
|
+
this.addForm(this.buf1.form, true);
|
|
2271
|
+
this.formOpen = false;
|
|
1767
2272
|
return {
|
|
1768
|
-
|
|
2273
|
+
form: this.formBad ? -1 : this.formComments,
|
|
2274
|
+
value,
|
|
1769
2275
|
end: this.buf1.start,
|
|
1770
2276
|
valueEnd: this.consumedEnd,
|
|
1771
2277
|
odd: this.odd,
|
|
@@ -1773,7 +2279,8 @@ var Lexer = class {
|
|
|
1773
2279
|
nestedStream: this.nestedStream,
|
|
1774
2280
|
indirectLength: this.indirectLength,
|
|
1775
2281
|
lengthCandidates: this.lengthCandidates,
|
|
1776
|
-
values: this.objectValues
|
|
2282
|
+
values: this.objectValues,
|
|
2283
|
+
...this.irregular ? { irregular: true } : {}
|
|
1777
2284
|
};
|
|
1778
2285
|
} finally {
|
|
1779
2286
|
this.qpdf = false;
|
|
@@ -1827,6 +2334,7 @@ var Lexer = class {
|
|
|
1827
2334
|
values: 0
|
|
1828
2335
|
};
|
|
1829
2336
|
const objAt = header.after - 3;
|
|
2337
|
+
const gap = gapForm(b, header.after, start);
|
|
1830
2338
|
if (keywordAt(b, start, "endobj")) return {
|
|
1831
2339
|
num: header.num,
|
|
1832
2340
|
gen: header.gen,
|
|
@@ -1835,7 +2343,9 @@ var Lexer = class {
|
|
|
1835
2343
|
endobj: true,
|
|
1836
2344
|
odd: false,
|
|
1837
2345
|
objAt,
|
|
1838
|
-
...none
|
|
2346
|
+
...none,
|
|
2347
|
+
irregular: true,
|
|
2348
|
+
plain: gap >= 0 && gap <= 2
|
|
1839
2349
|
};
|
|
1840
2350
|
if (start >= b.length) return {
|
|
1841
2351
|
num: header.num,
|
|
@@ -1845,11 +2355,13 @@ var Lexer = class {
|
|
|
1845
2355
|
endobj: false,
|
|
1846
2356
|
odd: false,
|
|
1847
2357
|
objAt,
|
|
1848
|
-
...none
|
|
2358
|
+
...none,
|
|
2359
|
+
plain: gap >= 0 && gap <= 2
|
|
1849
2360
|
};
|
|
1850
2361
|
const r = this.valueAt(start, options);
|
|
1851
2362
|
const after = skipSpace(b, r.end);
|
|
1852
2363
|
const endobj = keywordAt(b, after, "endobj");
|
|
2364
|
+
const gaps = !plainGap(b, header.after, start) || !plainGap(b, r.valueEnd, after);
|
|
1853
2365
|
return {
|
|
1854
2366
|
num: header.num,
|
|
1855
2367
|
gen: header.gen,
|
|
@@ -1862,11 +2374,75 @@ var Lexer = class {
|
|
|
1862
2374
|
lengthCandidates: r.lengthCandidates,
|
|
1863
2375
|
values: r.values,
|
|
1864
2376
|
objAt,
|
|
1865
|
-
|
|
2377
|
+
plain: gap >= 0 && r.form >= 0 && gap + r.form <= 2,
|
|
2378
|
+
...r.alternate !== void 0 ? { alternate: r.alternate } : {},
|
|
2379
|
+
...r.irregular || gaps || r.value instanceof PdfRef ? { irregular: true } : {}
|
|
1866
2380
|
};
|
|
1867
2381
|
}
|
|
1868
2382
|
};
|
|
1869
2383
|
// src/reader.ts
|
|
2384
|
+
/** White space or a delimiter: where a name or number token ends. */
|
|
2385
|
+
const tokenEnds = (c) => c === void 0 || c === 0 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32 || c === 47 || c === 60 || c === 62 || c === 91 || c === 93 || c === 40 || c === 41 || c === 37;
|
|
2386
|
+
const isSpace$1 = (c) => c === 0 || c === 9 || c === 10 || c === 12 || c === 13 || c === 32;
|
|
2387
|
+
/**
|
|
2388
|
+
* Whether the integers of dictionary `dict`, written as `text`, are written as every reader reads them: each of
|
|
2389
|
+
* `keys` present at most once, as a whole number in digits, directly (not a reference, not `6.0` or `+6`), and
|
|
2390
|
+
* each of `arrays` an array of such numbers. qpdf refuses a trailer whose /Size is a reference or a real
|
|
2391
|
+
* number; other readers read an offset or a count written otherwise differently, or not at all.
|
|
2392
|
+
*/
|
|
2393
|
+
function plainIntegers(dict, text, keys, arrays = []) {
|
|
2394
|
+
const buf = Buffer.from(text.buffer, text.byteOffset, text.length);
|
|
2395
|
+
/** Each place `/key` is written, as a whole name: the offset after it and its white space. */
|
|
2396
|
+
const places = (key) => {
|
|
2397
|
+
const out = [];
|
|
2398
|
+
for (let i = buf.indexOf(`/${key}`, 0, "latin1"); i >= 0; i = buf.indexOf(`/${key}`, i + 1, "latin1")) {
|
|
2399
|
+
let j = i + 1 + key.length;
|
|
2400
|
+
if (!tokenEnds(buf[j])) continue;
|
|
2401
|
+
while (isSpace$1(buf[j])) j += 1;
|
|
2402
|
+
out.push(j);
|
|
2403
|
+
}
|
|
2404
|
+
return out;
|
|
2405
|
+
};
|
|
2406
|
+
/** Whether the token at `at` is digits only, and where it ends. */
|
|
2407
|
+
const digitsAt = (at) => {
|
|
2408
|
+
let j = at;
|
|
2409
|
+
while (j < buf.length && buf[j] >= 48 && buf[j] <= 57) j += 1;
|
|
2410
|
+
return j > at && tokenEnds(buf[j]) ? j : -1;
|
|
2411
|
+
};
|
|
2412
|
+
for (const key of keys) {
|
|
2413
|
+
const all = dict.all(key);
|
|
2414
|
+
if (all.length === 0) continue;
|
|
2415
|
+
if (all.length !== 1 || typeof all[0] !== "number") return false;
|
|
2416
|
+
const at = places(key);
|
|
2417
|
+
if (at.length === 0 || at.some((x) => digitsAt(x) < 0)) return false;
|
|
2418
|
+
}
|
|
2419
|
+
for (const key of arrays) {
|
|
2420
|
+
const all = dict.all(key);
|
|
2421
|
+
if (all.length === 0) continue;
|
|
2422
|
+
if (all.length !== 1 || !Array.isArray(all[0]) || !all[0].every((x) => typeof x === "number")) return false;
|
|
2423
|
+
const at = places(key);
|
|
2424
|
+
if (at.length === 0) return false;
|
|
2425
|
+
for (const x of at) {
|
|
2426
|
+
if (buf[x] !== 91) return false;
|
|
2427
|
+
let j = x + 1;
|
|
2428
|
+
for (;;) {
|
|
2429
|
+
while (isSpace$1(buf[j])) j += 1;
|
|
2430
|
+
if (buf[j] === 93) break;
|
|
2431
|
+
const e = digitsAt(j);
|
|
2432
|
+
if (e < 0) return false;
|
|
2433
|
+
j = e;
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
return true;
|
|
2438
|
+
}
|
|
2439
|
+
/** After spaces at `at`, one end of line (LF, CR LF or CR): the offset after it, or -1. */
|
|
2440
|
+
function lineEndAt(b, at) {
|
|
2441
|
+
let i = at;
|
|
2442
|
+
while (b[i] === 32) i += 1;
|
|
2443
|
+
if (b[i] === 13) return b[i + 1] === 10 ? i + 2 : i + 1;
|
|
2444
|
+
return b[i] === 10 ? i + 1 : -1;
|
|
2445
|
+
}
|
|
1870
2446
|
/** Whether `c` can be part of a number token. */
|
|
1871
2447
|
const isNumberChar = (c) => isDigit(c) || c === 43 || c === 45 || c === 46;
|
|
1872
2448
|
/** The most readings of one `obj` that become definitions; an `obj` with more counts as unresolved. */
|
|
@@ -1939,9 +2515,65 @@ var Reader = class {
|
|
|
1939
2515
|
chainLength = 0;
|
|
1940
2516
|
chainBroken = false;
|
|
1941
2517
|
inForceCache = /* @__PURE__ */ new Map();
|
|
2518
|
+
/** Objects whose lookup finds no definition where the cross-reference chain points (the file needs repair). */
|
|
2519
|
+
repairing = /* @__PURE__ */ new Set();
|
|
2520
|
+
/**
|
|
2521
|
+
* While set, lookups only note what they find: the description of the embedded files looks up objects 0.1.0
|
|
2522
|
+
* never looked up, and must leave what the inspection reports as it was.
|
|
2523
|
+
*/
|
|
2524
|
+
quiet = false;
|
|
2525
|
+
/** Whether any lookup, quiet or not, found an object the file needs repair to reach. */
|
|
2526
|
+
repairNoted() {
|
|
2527
|
+
return this.repaired || this.repairing.size > 0;
|
|
2528
|
+
}
|
|
1942
2529
|
/** Object numbers that have a definition which could not be parsed. */
|
|
1943
2530
|
undecodable = /* @__PURE__ */ new Set();
|
|
1944
2531
|
repaired = false;
|
|
2532
|
+
/**
|
|
2533
|
+
* Set when the page tree has a kid that is not a dictionary: the inspection reports the file repaired, as it
|
|
2534
|
+
* did, but this is about pages, not about the file's structure.
|
|
2535
|
+
*/
|
|
2536
|
+
pagesRepaired = false;
|
|
2537
|
+
/** Whether a cross-reference stream's data is not exactly as long as its entries declare. */
|
|
2538
|
+
xrefStreamSizes = false;
|
|
2539
|
+
/** Whether a trailer's syntax is read differently by pdf.js and qpdf. */
|
|
2540
|
+
trailerSyntax = false;
|
|
2541
|
+
/** Whether a cross-reference table entry is not written as 10 digits, 5 digits and n or f, then an end of line. */
|
|
2542
|
+
xrefTableForm = false;
|
|
2543
|
+
/** Whether a cross-reference stream has an entry of a type other than 0, 1 or 2 (readers drop it or refuse the file). */
|
|
2544
|
+
xrefEntryType = false;
|
|
2545
|
+
/**
|
|
2546
|
+
* Whether a trailer's /Size is missing, or an integer of a trailer, a cross-reference stream or an object
|
|
2547
|
+
* stream (/Size, /Prev, /XRefStm, /W, /Index, /N, /First) is not a whole number written in digits directly.
|
|
2548
|
+
*/
|
|
2549
|
+
trailerSize = false;
|
|
2550
|
+
/** The trailer of the section the chain starts from (for a cross-reference stream, its dictionary). */
|
|
2551
|
+
newestTrailer;
|
|
2552
|
+
/** The sections of the chain, with the cross-reference streams of hybrid files. */
|
|
2553
|
+
chainSections = [];
|
|
2554
|
+
/**
|
|
2555
|
+
* Whether every cross-reference stream of the chain is listed, as in use at its own offset, by the entries in
|
|
2556
|
+
* force (qpdf warns when the entry for a cross-reference stream itself is missing).
|
|
2557
|
+
*/
|
|
2558
|
+
xrefStreamsListed() {
|
|
2559
|
+
for (const sec of this.chainSections) {
|
|
2560
|
+
if (sec.kind !== "stream") continue;
|
|
2561
|
+
const d = this.defsAt.get(sec.offset);
|
|
2562
|
+
const e = d ? this.entries.get(d.num) : void 0;
|
|
2563
|
+
if (!d || !e || e.type !== 1 || e.offset !== sec.offset) return false;
|
|
2564
|
+
}
|
|
2565
|
+
return true;
|
|
2566
|
+
}
|
|
2567
|
+
/** Whether startxref, a /Prev or an /XRefStm gives an offset that is not exactly that of its section. */
|
|
2568
|
+
chainOffsets = false;
|
|
2569
|
+
/**
|
|
2570
|
+
* Whether an object stream holds an object of its own number, an entry gives an object as held by itself, an
|
|
2571
|
+
* object stream lacks `/Type /ObjStm` (pypdf does not read it then), or its header gives another number than the
|
|
2572
|
+
* entry for an index.
|
|
2573
|
+
*/
|
|
2574
|
+
objectStreamForm = false;
|
|
2575
|
+
/** The most bytes an object stream or a cross-reference stream decoded to. */
|
|
2576
|
+
largestDecoded = 0;
|
|
1945
2577
|
objectStreams = false;
|
|
1946
2578
|
xrefStreams = false;
|
|
1947
2579
|
trailer = {};
|
|
@@ -1956,6 +2588,7 @@ var Reader = class {
|
|
|
1956
2588
|
addDefinition(d) {
|
|
1957
2589
|
this.defs.push(d);
|
|
1958
2590
|
this.inForceCache.delete(d.num);
|
|
2591
|
+
this.repairing.delete(d.num);
|
|
1959
2592
|
let list = this.byNum.get(d.num);
|
|
1960
2593
|
if (!list) this.byNum.set(d.num, list = []);
|
|
1961
2594
|
list.push(d);
|
|
@@ -1969,7 +2602,9 @@ var Reader = class {
|
|
|
1969
2602
|
value: parsed.value,
|
|
1970
2603
|
offset,
|
|
1971
2604
|
order: offset,
|
|
1972
|
-
hidden
|
|
2605
|
+
hidden,
|
|
2606
|
+
...parsed.irregular ? { irregular: true } : {},
|
|
2607
|
+
...parsed.plain ? { plain: true } : {}
|
|
1973
2608
|
};
|
|
1974
2609
|
if (parsed.odd) {
|
|
1975
2610
|
d.undecodable = true;
|
|
@@ -1988,7 +2623,8 @@ var Reader = class {
|
|
|
1988
2623
|
value: parsed.alternate,
|
|
1989
2624
|
hidden: true,
|
|
1990
2625
|
undecodable: false,
|
|
1991
|
-
alternateOf: d
|
|
2626
|
+
alternateOf: d,
|
|
2627
|
+
syntax: true
|
|
1992
2628
|
});
|
|
1993
2629
|
const s = parsed.value instanceof PdfDict ? parsed.value.stream : void 0;
|
|
1994
2630
|
if (s && s.declaredLength !== void 0 && s.declaredLength !== s.data.length) this.repaired = true;
|
|
@@ -2313,18 +2949,27 @@ var Reader = class {
|
|
|
2313
2949
|
const { bytes } = this;
|
|
2314
2950
|
const entries = /* @__PURE__ */ new Map();
|
|
2315
2951
|
let i = skipSpace(bytes, at + 4);
|
|
2952
|
+
if (lineEndAt(bytes, at + 4) !== i) this.xrefTableForm = true;
|
|
2953
|
+
let subsections = 0;
|
|
2954
|
+
let empty = false;
|
|
2316
2955
|
let count = 0;
|
|
2317
2956
|
for (;;) {
|
|
2318
2957
|
const first = intAt(bytes, i, 10);
|
|
2319
2958
|
if (!first) break;
|
|
2320
2959
|
const n = intAt(bytes, skipSpace(bytes, first.end), 10);
|
|
2321
2960
|
if (!n) break;
|
|
2961
|
+
subsections += 1;
|
|
2962
|
+
if (n.value === 0) empty = true;
|
|
2963
|
+
if (n.value === 0 && first.value !== 0) this.xrefTableForm = true;
|
|
2964
|
+
const eol = bytes[n.end] === 13 ? bytes[n.end + 1] === 10 ? n.end + 2 : n.end + 1 : bytes[n.end] === 10 ? n.end + 1 : -1;
|
|
2965
|
+
if (skipSpace(bytes, first.end) !== first.end + 1 || bytes[first.end] !== 32 || eol !== skipSpace(bytes, n.end)) this.xrefTableForm = true;
|
|
2322
2966
|
let j = skipSpace(bytes, n.end);
|
|
2323
2967
|
let k = 0;
|
|
2324
2968
|
for (; k < n.value; k += 1) {
|
|
2325
2969
|
const offset = intAt(bytes, j, 10);
|
|
2326
2970
|
if (!offset) break;
|
|
2327
|
-
const
|
|
2971
|
+
const g = skipSpace(bytes, offset.end);
|
|
2972
|
+
const gen = intAt(bytes, g, 5);
|
|
2328
2973
|
if (!gen) break;
|
|
2329
2974
|
const t = skipSpace(bytes, gen.end);
|
|
2330
2975
|
const type = bytes[t];
|
|
@@ -2332,9 +2977,19 @@ var Reader = class {
|
|
|
2332
2977
|
count += 1;
|
|
2333
2978
|
if ((count & 4095) === 0) this.work.xrefEntries(4096);
|
|
2334
2979
|
const num = first.value + k;
|
|
2980
|
+
const odd = offset.end - j !== 10 || gen.end - g !== 5;
|
|
2981
|
+
const s0 = t + 1;
|
|
2982
|
+
const length = skipSpace(bytes, s0) - s0;
|
|
2983
|
+
const pair = bytes[s0] === 32 && (bytes[s0 + 1] === 10 || bytes[s0 + 1] === 13) || bytes[s0] === 13 && bytes[s0 + 1] === 10;
|
|
2984
|
+
const blank = length === 3 ? bytes[s0 + 2] === 10 && bytes[s0 + 1] !== 13 : length === 4 && bytes[s0 + 2] === 13 && bytes[s0 + 3] === 10;
|
|
2985
|
+
const eol = length === 2 && pair || blank && pair && keywordAt(bytes, s0 + length, "trailer");
|
|
2986
|
+
if (odd || g !== offset.end + 1 || t !== gen.end + 1 || bytes[offset.end] !== 32 || bytes[gen.end] !== 32 || !eol) this.xrefTableForm = true;
|
|
2987
|
+
if (num === 0 && type === 110) this.xrefTableForm = true;
|
|
2335
2988
|
if (!entries.has(num)) entries.set(num, type === 110 ? {
|
|
2336
2989
|
type: 1,
|
|
2337
|
-
offset: offset.value
|
|
2990
|
+
offset: offset.value,
|
|
2991
|
+
gen: gen.value,
|
|
2992
|
+
...odd ? { odd } : {}
|
|
2338
2993
|
} : { type: 0 });
|
|
2339
2994
|
j = skipSpace(bytes, t + 1);
|
|
2340
2995
|
}
|
|
@@ -2345,6 +3000,7 @@ var Reader = class {
|
|
|
2345
3000
|
}
|
|
2346
3001
|
i = j;
|
|
2347
3002
|
}
|
|
3003
|
+
if (!keywordAt(bytes, i, "trailer") || bytes.subarray(at, i).includes(37) || subsections === 0 || empty && subsections > 1) this.xrefTableForm = true;
|
|
2348
3004
|
this.work.xrefEntries(count & 4095);
|
|
2349
3005
|
this.work.section();
|
|
2350
3006
|
const section = {
|
|
@@ -2361,11 +3017,20 @@ var Reader = class {
|
|
|
2361
3017
|
readTrailer(at, standalone = false) {
|
|
2362
3018
|
this.trailersRead.add(at);
|
|
2363
3019
|
try {
|
|
2364
|
-
const
|
|
3020
|
+
const start = skipSpace(this.bytes, at + 7);
|
|
3021
|
+
const { value, end, alternate, odd, irregular, valueEnd } = this.lexer.valueAt(start);
|
|
3022
|
+
if (alternate !== void 0 || odd || irregular || !plainGap(this.bytes, at + 7, start)) this.trailerSyntax = true;
|
|
3023
|
+
if (value instanceof PdfDict && (!value.has("Size") || !plainIntegers(value, this.bytes.subarray(at, valueEnd), [
|
|
3024
|
+
"Size",
|
|
3025
|
+
"Prev",
|
|
3026
|
+
"XRefStm"
|
|
3027
|
+
]))) this.trailerSize = true;
|
|
2365
3028
|
if (value instanceof PdfDict) {
|
|
2366
3029
|
const table = this.sections.at(-1);
|
|
2367
|
-
if (!standalone && table && table.kind === "table" && !table.trailer && table.offset < at)
|
|
2368
|
-
|
|
3030
|
+
if (!standalone && table && table.kind === "table" && !table.trailer && table.offset < at) {
|
|
3031
|
+
table.trailer = value;
|
|
3032
|
+
table.trailerAt = at;
|
|
3033
|
+
} else this.standaloneTrailers.push({
|
|
2369
3034
|
offset: at,
|
|
2370
3035
|
dict: value
|
|
2371
3036
|
});
|
|
@@ -2536,14 +3201,16 @@ var Reader = class {
|
|
|
2536
3201
|
value: parsed.value,
|
|
2537
3202
|
hidden: true,
|
|
2538
3203
|
undecodable: false,
|
|
2539
|
-
alternateOf: d
|
|
3204
|
+
alternateOf: d,
|
|
3205
|
+
...parsed.irregular ? { irregular: true } : {}
|
|
2540
3206
|
});
|
|
2541
3207
|
if (parsed.alternate !== void 0) this.addDefinition({
|
|
2542
3208
|
...d,
|
|
2543
3209
|
value: parsed.alternate,
|
|
2544
3210
|
hidden: true,
|
|
2545
3211
|
undecodable: false,
|
|
2546
|
-
alternateOf: d
|
|
3212
|
+
alternateOf: d,
|
|
3213
|
+
syntax: true
|
|
2547
3214
|
});
|
|
2548
3215
|
if (parsed.nestedStream && !d.undecodable) {
|
|
2549
3216
|
d.undecodable = true;
|
|
@@ -2678,6 +3345,13 @@ var Reader = class {
|
|
|
2678
3345
|
if (this.readXrefStreamDefs.has(d)) return;
|
|
2679
3346
|
this.readXrefStreamDefs.add(d);
|
|
2680
3347
|
this.xrefStreams = true;
|
|
3348
|
+
if (d.container || !dict.stream || !dict.has("Size") || !plainIntegers(dict, this.bytes.subarray(d.offset, dict.stream.start), [
|
|
3349
|
+
"Size",
|
|
3350
|
+
"Prev",
|
|
3351
|
+
"Length"
|
|
3352
|
+
], ["W", "Index"])) this.trailerSize = true;
|
|
3353
|
+
const index = dict.get("Index");
|
|
3354
|
+
if (Array.isArray(index) && index.length % 2 !== 0) this.trailerSize = true;
|
|
2681
3355
|
for (const v of this.variants(d, dict)) try {
|
|
2682
3356
|
this.readXrefStream(d, dict, v.data);
|
|
2683
3357
|
} catch (error) {
|
|
@@ -2789,14 +3463,18 @@ var Reader = class {
|
|
|
2789
3463
|
const a = int(index[i]);
|
|
2790
3464
|
const b = int(index[i + 1]);
|
|
2791
3465
|
if (a === void 0 || b === void 0) throw damaged("a cross-reference stream has a wrong index", d.offset);
|
|
3466
|
+
if (b === 0) this.xrefStreamSizes = true;
|
|
2792
3467
|
ranges.push([a, b]);
|
|
2793
3468
|
}
|
|
2794
3469
|
else ranges.push([0, size]);
|
|
2795
3470
|
const total = ranges.reduce((n, [, c]) => n + c, 0);
|
|
2796
3471
|
const { names, parms } = filtersOf(dict, (v) => this.resolve(v));
|
|
2797
3472
|
const data = decode(raw, names, parms, this.work);
|
|
3473
|
+
this.largestDecoded = Math.max(this.largestDecoded, data.length);
|
|
3474
|
+
if ((names[0] === "FlateDecode" || names[0] === "Fl") && !flateIsWhole(raw, 2 * data.length + 1024)) this.xrefStreamSizes = true;
|
|
2798
3475
|
const rows = Math.floor(data.length / rowWidth);
|
|
2799
3476
|
if (rows < total) this.repaired = true;
|
|
3477
|
+
if (data.length !== total * rowWidth) this.xrefStreamSizes = true;
|
|
2800
3478
|
this.work.xrefEntries(Math.min(rows, total));
|
|
2801
3479
|
const field = (at, width) => {
|
|
2802
3480
|
let v = 0;
|
|
@@ -2813,16 +3491,21 @@ var Reader = class {
|
|
|
2813
3491
|
const num = first + k;
|
|
2814
3492
|
if ((row & 65535) === 65535) this.work.checkpoint();
|
|
2815
3493
|
if (entries.has(num)) continue;
|
|
3494
|
+
if (num === 0 && type !== 0) this.xrefEntryType = true;
|
|
2816
3495
|
if (type === 0) entries.set(num, { type: 0 });
|
|
2817
3496
|
else if (type === 1) entries.set(num, {
|
|
2818
3497
|
type: 1,
|
|
2819
|
-
offset: second
|
|
2820
|
-
|
|
2821
|
-
else if (type === 2) entries.set(num, {
|
|
2822
|
-
type: 2,
|
|
2823
|
-
stream: second,
|
|
2824
|
-
index: third
|
|
3498
|
+
offset: second,
|
|
3499
|
+
gen: third
|
|
2825
3500
|
});
|
|
3501
|
+
else if (type === 2) {
|
|
3502
|
+
entries.set(num, {
|
|
3503
|
+
type: 2,
|
|
3504
|
+
stream: second,
|
|
3505
|
+
index: third
|
|
3506
|
+
});
|
|
3507
|
+
if (second === num) this.objectStreamForm = true;
|
|
3508
|
+
} else this.xrefEntryType = true;
|
|
2826
3509
|
}
|
|
2827
3510
|
const section = {
|
|
2828
3511
|
kind: "stream",
|
|
@@ -2842,6 +3525,79 @@ var Reader = class {
|
|
|
2842
3525
|
if (h && this.sectionAt.has(h.start)) return this.sectionAt.get(h.start);
|
|
2843
3526
|
}
|
|
2844
3527
|
}
|
|
3528
|
+
/**
|
|
3529
|
+
* Whether every number a cross-reference section lists is below its own trailer's /Size and below `size` (the
|
|
3530
|
+
* newest /Size), and `size` is one more than the highest of them: MuPDF rebuilds a file otherwise.
|
|
3531
|
+
*/
|
|
3532
|
+
entriesWithin(size) {
|
|
3533
|
+
let highest = -1;
|
|
3534
|
+
for (const section of this.sections) {
|
|
3535
|
+
const own = section.trailer?.get("Size");
|
|
3536
|
+
const limit = typeof own === "number" ? Math.min(own, size) : size;
|
|
3537
|
+
for (const num of section.entries.keys()) {
|
|
3538
|
+
if (num >= limit) return false;
|
|
3539
|
+
if (num > highest) highest = num;
|
|
3540
|
+
}
|
|
3541
|
+
}
|
|
3542
|
+
return size === highest + 1;
|
|
3543
|
+
}
|
|
3544
|
+
/**
|
|
3545
|
+
* Whether every in-use entry of every cross-reference section gives the exact offset of a header of its object,
|
|
3546
|
+
* under its generation (pypdf drops an object whose entry points at white space or a comment before it; qpdf
|
|
3547
|
+
* rebuilds a file whose entry gives another generation, in any section).
|
|
3548
|
+
*/
|
|
3549
|
+
entriesExact() {
|
|
3550
|
+
for (const section of this.sections) for (const [num, e] of section.entries) {
|
|
3551
|
+
if (e.type !== 1) continue;
|
|
3552
|
+
const d = this.defsAt.get(e.offset);
|
|
3553
|
+
if (!d || d.num !== num || d.gen !== e.gen) return false;
|
|
3554
|
+
}
|
|
3555
|
+
return true;
|
|
3556
|
+
}
|
|
3557
|
+
/**
|
|
3558
|
+
* Whether every object the cross-reference chain gives as held by an object stream is read from the definition
|
|
3559
|
+
* of that stream in force, and that definition is a stream (an update that redefines or frees the stream leaves
|
|
3560
|
+
* readers without the objects it held, though an earlier definition of the stream holds them).
|
|
3561
|
+
*/
|
|
3562
|
+
objectStreamsInForce() {
|
|
3563
|
+
for (const [num, e] of this.entries) {
|
|
3564
|
+
if (e.type !== 2) continue;
|
|
3565
|
+
const container = this.inForce(e.stream);
|
|
3566
|
+
if (!container || !(container.value instanceof PdfDict) || !container.value.stream) return false;
|
|
3567
|
+
const d = this.inForce(num);
|
|
3568
|
+
if (d && d.container !== container) return false;
|
|
3569
|
+
}
|
|
3570
|
+
return true;
|
|
3571
|
+
}
|
|
3572
|
+
/**
|
|
3573
|
+
* The texts of the cross-reference chain the description reads: where each table's trailer starts (after its
|
|
3574
|
+
* keyword), and each cross-reference stream's definition.
|
|
3575
|
+
*/
|
|
3576
|
+
chainTexts() {
|
|
3577
|
+
const trailers = [];
|
|
3578
|
+
const streams = [];
|
|
3579
|
+
for (const sec of this.chainSections) {
|
|
3580
|
+
if (sec.kind === "table" && sec.trailerAt !== void 0) trailers.push(sec.trailerAt + 7);
|
|
3581
|
+
const d = sec.kind === "stream" ? this.defsAt.get(sec.offset) : void 0;
|
|
3582
|
+
if (d) streams.push(d);
|
|
3583
|
+
}
|
|
3584
|
+
return {
|
|
3585
|
+
trailers,
|
|
3586
|
+
streams
|
|
3587
|
+
};
|
|
3588
|
+
}
|
|
3589
|
+
/** Whether any reference read has a generation other than 0. */
|
|
3590
|
+
referencesWithGenerations() {
|
|
3591
|
+
return this.lexer.generations || this.streamLexer?.generations === true;
|
|
3592
|
+
}
|
|
3593
|
+
/** The definition read with its header at `offset`, if any. */
|
|
3594
|
+
definitionAt(offset) {
|
|
3595
|
+
return this.defsAt.get(offset);
|
|
3596
|
+
}
|
|
3597
|
+
/** The offset the last `startxref` gives: where the chain of cross-reference sections starts. */
|
|
3598
|
+
chainStart() {
|
|
3599
|
+
return this.startxrefs.at(-1);
|
|
3600
|
+
}
|
|
2845
3601
|
/** Follows the last `startxref` and every /Prev, and settles the trailer and the entries in force. */
|
|
2846
3602
|
buildChain() {
|
|
2847
3603
|
const chain = [];
|
|
@@ -2855,9 +3611,11 @@ var Reader = class {
|
|
|
2855
3611
|
}
|
|
2856
3612
|
visited.add(s);
|
|
2857
3613
|
chain.push(s);
|
|
3614
|
+
if (this.sectionAt.get(at) !== s || at > 0 && this.bytes[at - 1] !== 10 && this.bytes[at - 1] !== 13) this.chainOffsets = true;
|
|
2858
3615
|
if (s.kind === "table") {
|
|
2859
3616
|
const x = this.resolve(s.trailer?.get("XRefStm"));
|
|
2860
3617
|
const hybrid = typeof x === "number" ? this.findSection(x) : void 0;
|
|
3618
|
+
if (hybrid && (this.sectionAt.get(x) !== hybrid || this.bytes[x - 1] !== 10 && this.bytes[x - 1] !== 13)) this.chainOffsets = true;
|
|
2861
3619
|
if (hybrid && hybrid.kind === "stream" && !visited.has(hybrid)) {
|
|
2862
3620
|
s.hybrid = hybrid;
|
|
2863
3621
|
visited.add(hybrid);
|
|
@@ -2869,8 +3627,11 @@ var Reader = class {
|
|
|
2869
3627
|
if (chain.length === 0) this.chainBroken = true;
|
|
2870
3628
|
if (this.chainBroken) this.repaired = true;
|
|
2871
3629
|
this.inForceCache.clear();
|
|
3630
|
+
this.repairing.clear();
|
|
2872
3631
|
for (const s of chain) for (const part of s.hybrid ? [s, s.hybrid] : [s]) for (const [num, e] of part.entries) if (!this.entries.has(num)) this.entries.set(num, e);
|
|
2873
3632
|
this.chainLength = chain.length;
|
|
3633
|
+
this.newestTrailer = chain[0]?.trailer;
|
|
3634
|
+
this.chainSections = chain.flatMap((sec) => sec.hybrid ? [sec, sec.hybrid] : [sec]);
|
|
2874
3635
|
const dicts = chain.length > 0 && !this.chainBroken ? chain.map((s) => s.trailer).filter((t) => t !== void 0) : [...this.sections.filter((s) => s.trailer).map((s) => ({
|
|
2875
3636
|
offset: s.offset,
|
|
2876
3637
|
dict: s.trailer
|
|
@@ -2879,7 +3640,8 @@ var Reader = class {
|
|
|
2879
3640
|
"Root",
|
|
2880
3641
|
"Info",
|
|
2881
3642
|
"Encrypt",
|
|
2882
|
-
"ID"
|
|
3643
|
+
"ID",
|
|
3644
|
+
"Size"
|
|
2883
3645
|
]) for (const t of dicts) {
|
|
2884
3646
|
const v = t.get(key);
|
|
2885
3647
|
if (v !== void 0 && v !== null) {
|
|
@@ -2969,6 +3731,8 @@ var Reader = class {
|
|
|
2969
3731
|
if (combos.length === 0) throw damaged("an object stream has a wrong header", d.offset);
|
|
2970
3732
|
yield;
|
|
2971
3733
|
this.objectStreams = true;
|
|
3734
|
+
if (!isName(dict.get("Type"), "ObjStm") || dict.all("Type").length !== 1) this.objectStreamForm = true;
|
|
3735
|
+
if (d.container || !plainIntegers(dict, this.bytes.subarray(d.offset, dict.stream.start), ["N", "First"])) this.trailerSize = true;
|
|
2972
3736
|
const variants = this.variants(d, dict);
|
|
2973
3737
|
for (let c = 0; c < combos.length; c += 1) for (let v = 0; v < variants.length; v += 1) {
|
|
2974
3738
|
const required = variants[v].required && c === 0 && !d.alternateOf;
|
|
@@ -2985,18 +3749,26 @@ var Reader = class {
|
|
|
2985
3749
|
}
|
|
2986
3750
|
for (const section of this.sections) for (const [num, e] of section.entries) {
|
|
2987
3751
|
if (e.type !== 2) continue;
|
|
2988
|
-
for (const member of held.get(`${e.stream} ${e.index}`) ?? [])
|
|
2989
|
-
|
|
2990
|
-
num
|
|
2991
|
-
|
|
3752
|
+
for (const member of held.get(`${e.stream} ${e.index}`) ?? []) {
|
|
3753
|
+
if (member.num !== num) this.objectStreamForm = true;
|
|
3754
|
+
if (member.num !== num && !this.definitionsOf(num).some((x) => x.container === member.container && x.index === member.index)) this.addDefinition({
|
|
3755
|
+
...member,
|
|
3756
|
+
num
|
|
3757
|
+
});
|
|
3758
|
+
}
|
|
2992
3759
|
}
|
|
2993
3760
|
for (const section of this.sections) for (const [, e] of section.entries) if (e.type === 2 && !held.has(`${e.stream} ${e.index}`)) throw damaged("a compressed-object entry leads to no object", section.offset);
|
|
2994
3761
|
this.inForceCache.clear();
|
|
3762
|
+
this.repairing.clear();
|
|
2995
3763
|
}
|
|
2996
3764
|
*readObjectStream(d, dict, raw, alternate, n, first) {
|
|
2997
3765
|
if (!Number.isSafeInteger(n) || n < 0 || !Number.isSafeInteger(first) || first < 0) throw damaged("an object stream has a wrong header", d.offset);
|
|
2998
3766
|
this.work.announce(n);
|
|
2999
3767
|
const data = this.streamData(d, dict, raw);
|
|
3768
|
+
this.largestDecoded = Math.max(this.largestDecoded, data.length);
|
|
3769
|
+
const firstFilter = filtersOf(dict, (v) => this.resolve(v)).names[0];
|
|
3770
|
+
const flated = firstFilter === "FlateDecode" || firstFilter === "Fl";
|
|
3771
|
+
if (!this.security && flated && !flateIsWhole(raw, 2 * data.length + 1024)) this.objectStreamForm = true;
|
|
3000
3772
|
const pairs = [];
|
|
3001
3773
|
let at = 0;
|
|
3002
3774
|
for (let i = 0; i < n; i += 1) {
|
|
@@ -3007,22 +3779,31 @@ var Reader = class {
|
|
|
3007
3779
|
at = off.end;
|
|
3008
3780
|
if ((i & 4095) === 4095) this.work.checkpoint();
|
|
3009
3781
|
}
|
|
3782
|
+
if (!alternate) d.membersPlain = plainObjectStreamHeader(data, first);
|
|
3010
3783
|
const lexer = this.streamLexer ??= new Lexer(data, this.work);
|
|
3011
3784
|
lexer.reset(data);
|
|
3012
3785
|
for (let i = 0; i < pairs.length; i += 1) {
|
|
3013
3786
|
yield;
|
|
3014
3787
|
this.work.object();
|
|
3015
3788
|
const [num, off] = pairs[i];
|
|
3789
|
+
if (num === d.num) this.objectStreamForm = true;
|
|
3016
3790
|
const offset = first + off;
|
|
3017
3791
|
if (offset >= data.length) throw damaged("an object stream has a wrong offset", d.offset);
|
|
3018
3792
|
let value;
|
|
3019
3793
|
let odd;
|
|
3020
3794
|
let alternateValue;
|
|
3795
|
+
let irregular = false;
|
|
3796
|
+
let digits = false;
|
|
3797
|
+
let plain = false;
|
|
3021
3798
|
try {
|
|
3022
3799
|
const r = lexer.valueAt(offset, { lengths: this.lengthsOf });
|
|
3023
3800
|
({value, alternate: alternateValue} = r);
|
|
3801
|
+
irregular = r.irregular === true || value instanceof PdfRef;
|
|
3024
3802
|
const slotEnd = i + 1 < pairs.length ? first + pairs[i + 1][1] : data.length;
|
|
3025
3803
|
odd = r.odd || r.nestedStream || r.valueEnd > slotEnd;
|
|
3804
|
+
if (r.valueEnd <= slotEnd && !spaceOnly(data, r.valueEnd, slotEnd)) irregular = true;
|
|
3805
|
+
digits = typeof value === "number" && digitsOnly(data, skipSpace(data, offset), r.valueEnd);
|
|
3806
|
+
plain = r.form >= 0 && r.form <= 2;
|
|
3026
3807
|
} catch (error) {
|
|
3027
3808
|
if (error instanceof PdfToolsError) throw error;
|
|
3028
3809
|
throw damaged("an object in an object stream cannot be read", d.offset);
|
|
@@ -3039,7 +3820,10 @@ var Reader = class {
|
|
|
3039
3820
|
container: d,
|
|
3040
3821
|
index: i,
|
|
3041
3822
|
...odd ? { undecodable: true } : {},
|
|
3042
|
-
...earlier ? { alternateOf: earlier } : {}
|
|
3823
|
+
...earlier ? { alternateOf: earlier } : {},
|
|
3824
|
+
...irregular ? { irregular: true } : {},
|
|
3825
|
+
...digits ? { digits } : {},
|
|
3826
|
+
...plain ? { plain } : {}
|
|
3043
3827
|
};
|
|
3044
3828
|
this.addDefinition(member);
|
|
3045
3829
|
if (!alternate) this.firstReading.set(`${d.num} ${d.offset} ${i}`, member);
|
|
@@ -3048,70 +3832,1883 @@ var Reader = class {
|
|
|
3048
3832
|
value: alternateValue,
|
|
3049
3833
|
hidden: true,
|
|
3050
3834
|
undecodable: false,
|
|
3051
|
-
alternateOf: member
|
|
3835
|
+
alternateOf: member,
|
|
3836
|
+
syntax: true
|
|
3837
|
+
});
|
|
3838
|
+
if (odd) {
|
|
3839
|
+
this.undecodable.add(num);
|
|
3840
|
+
this.repaired = true;
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
}
|
|
3844
|
+
/** The definition of object `num` in force: the one the cross-reference chain gives, else the latest in file order. */
|
|
3845
|
+
inForce(num) {
|
|
3846
|
+
let found = this.inForceCache.get(num);
|
|
3847
|
+
if (found === void 0) {
|
|
3848
|
+
found = this.findInForce(num) ?? null;
|
|
3849
|
+
this.inForceCache.set(num, found);
|
|
3850
|
+
}
|
|
3851
|
+
if (!this.quiet && this.repairing.has(num)) this.repaired = true;
|
|
3852
|
+
return found ?? void 0;
|
|
3853
|
+
}
|
|
3854
|
+
findInForce(num) {
|
|
3855
|
+
const list = this.byNum.get(num) ?? [];
|
|
3856
|
+
const e = this.entries.get(num);
|
|
3857
|
+
if (e) {
|
|
3858
|
+
if (e.type === 0) return void 0;
|
|
3859
|
+
if (e.type === 1) {
|
|
3860
|
+
const d = this.defsAt.get(e.offset) ?? (this.headerOffset > 0 ? this.defsAt.get(e.offset + this.headerOffset) : void 0);
|
|
3861
|
+
if (d && d.num === num) return d;
|
|
3862
|
+
} else {
|
|
3863
|
+
const members = list.filter((d) => d.container && d.container.num === e.stream && d.index === e.index);
|
|
3864
|
+
if (members.length > 0) return members.reduce((a, b) => b.container.order > a.container.order ? b : a);
|
|
3865
|
+
}
|
|
3866
|
+
if (list.length > 0) this.repairing.add(num);
|
|
3867
|
+
}
|
|
3868
|
+
const visible = list.filter((d) => !d.hidden);
|
|
3869
|
+
const pool = visible.length > 0 ? visible : list;
|
|
3870
|
+
return pool.length > 0 ? pool.reduce((a, b) => b.order >= a.order ? b : a) : void 0;
|
|
3871
|
+
}
|
|
3872
|
+
/**
|
|
3873
|
+
* Whether the definition of object `num` in force is the one an in-use entry of the cross-reference chain
|
|
3874
|
+
* leads to (not one chosen by file order, for an object the chain does not list or lists elsewhere).
|
|
3875
|
+
*/
|
|
3876
|
+
listedInForce(num) {
|
|
3877
|
+
const e = this.entries.get(num);
|
|
3878
|
+
const d = this.inForce(num);
|
|
3879
|
+
if (!e || !d || e.type === 0) return false;
|
|
3880
|
+
if (e.type === 1) return d.num === num && (this.defsAt.get(e.offset) === d || this.headerOffset > 0 && this.defsAt.get(e.offset + this.headerOffset) === d);
|
|
3881
|
+
return d.container !== void 0 && d.container.num === e.stream && d.index === e.index;
|
|
3882
|
+
}
|
|
3883
|
+
/**
|
|
3884
|
+
* The generation the in-use cross-reference entry of `num` gives: 0 for an object in an object stream;
|
|
3885
|
+
* undefined for no entry, a free one, or one whose fields are not written in 10 and 5 digits.
|
|
3886
|
+
*/
|
|
3887
|
+
entryGeneration(num) {
|
|
3888
|
+
const e = this.entries.get(num);
|
|
3889
|
+
if (!e || e.type === 0) return void 0;
|
|
3890
|
+
if (e.type === 2) return 0;
|
|
3891
|
+
return e.odd ? void 0 : e.gen;
|
|
3892
|
+
}
|
|
3893
|
+
/** `v` with references followed to the definitions in force; null for a missing object, a loop or a chain longer than maxDepth. */
|
|
3894
|
+
resolve(v) {
|
|
3895
|
+
return this.resolveWithDefinition(v).value;
|
|
3896
|
+
}
|
|
3897
|
+
resolveWithDefinition(v) {
|
|
3898
|
+
let definition;
|
|
3899
|
+
let value = v ?? null;
|
|
3900
|
+
for (let hops = 0; value instanceof PdfRef; hops += 1) {
|
|
3901
|
+
if (hops >= this.work.limits.maxDepth) return {
|
|
3902
|
+
value: null,
|
|
3903
|
+
definition: void 0
|
|
3904
|
+
};
|
|
3905
|
+
definition = this.inForce(value.num);
|
|
3906
|
+
value = definition ? definition.value : null;
|
|
3907
|
+
}
|
|
3908
|
+
return {
|
|
3909
|
+
value,
|
|
3910
|
+
definition
|
|
3911
|
+
};
|
|
3912
|
+
}
|
|
3913
|
+
/** Definitions that a later definition of the same object replaces (hidden ones are not counted). */
|
|
3914
|
+
shadowedCount() {
|
|
3915
|
+
let n = 0;
|
|
3916
|
+
for (const list of this.byNum.values()) {
|
|
3917
|
+
const visible = list.filter((d) => !d.hidden).length;
|
|
3918
|
+
if (visible > 1) n += visible - 1;
|
|
3919
|
+
}
|
|
3920
|
+
return n;
|
|
3921
|
+
}
|
|
3922
|
+
};
|
|
3923
|
+
// src/text.ts
|
|
3924
|
+
/** PDFDocEncoding bytes 0x18 to 0x1f and 0x80 to 0xad that differ from Latin-1. */
|
|
3925
|
+
const PDFDOC = {
|
|
3926
|
+
24: "˘",
|
|
3927
|
+
25: "ˇ",
|
|
3928
|
+
26: "ˆ",
|
|
3929
|
+
27: "˙",
|
|
3930
|
+
28: "˝",
|
|
3931
|
+
29: "˛",
|
|
3932
|
+
30: "˚",
|
|
3933
|
+
31: "˜",
|
|
3934
|
+
128: "•",
|
|
3935
|
+
129: "†",
|
|
3936
|
+
130: "‡",
|
|
3937
|
+
131: "…",
|
|
3938
|
+
132: "—",
|
|
3939
|
+
133: "–",
|
|
3940
|
+
134: "ƒ",
|
|
3941
|
+
135: "⁄",
|
|
3942
|
+
136: "‹",
|
|
3943
|
+
137: "›",
|
|
3944
|
+
138: "−",
|
|
3945
|
+
139: "‰",
|
|
3946
|
+
140: "„",
|
|
3947
|
+
141: "“",
|
|
3948
|
+
142: "”",
|
|
3949
|
+
143: "‘",
|
|
3950
|
+
144: "’",
|
|
3951
|
+
145: "‚",
|
|
3952
|
+
146: "™",
|
|
3953
|
+
147: "fi",
|
|
3954
|
+
148: "fl",
|
|
3955
|
+
149: "Ł",
|
|
3956
|
+
150: "Œ",
|
|
3957
|
+
151: "Š",
|
|
3958
|
+
152: "Ÿ",
|
|
3959
|
+
153: "Ž",
|
|
3960
|
+
154: "ı",
|
|
3961
|
+
155: "ł",
|
|
3962
|
+
156: "œ",
|
|
3963
|
+
157: "š",
|
|
3964
|
+
158: "ž",
|
|
3965
|
+
160: "€"
|
|
3966
|
+
};
|
|
3967
|
+
/** A text string: UTF-16BE or UTF-8 with a byte order mark, else PDFDocEncoding. */
|
|
3968
|
+
function textOf(b) {
|
|
3969
|
+
if (b[0] === 254 && b[1] === 255) {
|
|
3970
|
+
let s = "";
|
|
3971
|
+
for (let i = 2; i + 1 < b.length; i += 2) s += String.fromCharCode(b[i] << 8 | b[i + 1]);
|
|
3972
|
+
return s;
|
|
3973
|
+
}
|
|
3974
|
+
if (b[0] === 239 && b[1] === 187 && b[2] === 191) return new TextDecoder("utf-8").decode(b.subarray(3));
|
|
3975
|
+
let plain = true;
|
|
3976
|
+
for (let i = 0; i < b.length && plain; i += 1) {
|
|
3977
|
+
const c = b[i];
|
|
3978
|
+
if (c >= 24 && c <= 31 || c >= 128 && c <= 173) plain = false;
|
|
3979
|
+
}
|
|
3980
|
+
if (plain) return Buffer.from(b.buffer, b.byteOffset, b.length).toString("latin1");
|
|
3981
|
+
let s = "";
|
|
3982
|
+
for (const c of b) s += PDFDOC[c] ?? String.fromCharCode(c);
|
|
3983
|
+
return s;
|
|
3984
|
+
}
|
|
3985
|
+
/** The bytes of string `s` held by definition `holder`, decrypted when the file is encrypted. */
|
|
3986
|
+
function stringBytes(reader, s, holder) {
|
|
3987
|
+
const bytes = s.bytes();
|
|
3988
|
+
return holder && reader.security && reader.encrypted(holder) ? decryptString(reader.security, holder.num, holder.gen, bytes) : bytes;
|
|
3989
|
+
}
|
|
3990
|
+
/**
|
|
3991
|
+
* Whether every reader is bound to show text string `b` as `textOf(b)`. Not when readers decode it
|
|
3992
|
+
* differently: a UTF-16LE byte order mark (some readers take it, others read PDFDocEncoding), UTF-16BE of an
|
|
3993
|
+
* odd length or with an unpaired surrogate, a UTF-8 byte order mark before bytes that are not UTF-8, or a
|
|
3994
|
+
* PDFDocEncoding code that is not defined (0x7F, 0x9F, 0xAD); nor when the text holds a control character
|
|
3995
|
+
* (C0, DEL or C1: language escapes and NULs are dropped or cut at by some readers), a backslash (some
|
|
3996
|
+
* readers show it as a path separator), a slash (pdf.js shows only what follows it), or a byte order mark after
|
|
3997
|
+
* the first (some readers drop it).
|
|
3998
|
+
*/
|
|
3999
|
+
/**
|
|
4000
|
+
* The text of `b` when it is printable ASCII without a backslash or a slash, which every reader shows as it is (then
|
|
4001
|
+
* `plainText(b)` is true and `textOf(b)` is this text); otherwise null. One pass, for long keys.
|
|
4002
|
+
*/
|
|
4003
|
+
function printableAscii(b) {
|
|
4004
|
+
for (let i = 0; i < b.length; i += 1) {
|
|
4005
|
+
const c = b[i];
|
|
4006
|
+
if (c < 32 || c > 126 || c === 92 || c === 47) return null;
|
|
4007
|
+
}
|
|
4008
|
+
return Buffer.from(b.buffer, b.byteOffset, b.length).toString("latin1");
|
|
4009
|
+
}
|
|
4010
|
+
/** (`known` is `textOf(b)` when the caller has it already.) */
|
|
4011
|
+
function plainText(b, known) {
|
|
4012
|
+
if (b[0] === 255 && b[1] === 254) return false;
|
|
4013
|
+
let text;
|
|
4014
|
+
if (b[0] === 254 && b[1] === 255) {
|
|
4015
|
+
if (b.length % 2 !== 0) return false;
|
|
4016
|
+
try {
|
|
4017
|
+
text = new TextDecoder("utf-16be", {
|
|
4018
|
+
fatal: true,
|
|
4019
|
+
ignoreBOM: true
|
|
4020
|
+
}).decode(b.subarray(2));
|
|
4021
|
+
} catch {
|
|
4022
|
+
return false;
|
|
4023
|
+
}
|
|
4024
|
+
} else if (b[0] === 239 && b[1] === 187 && b[2] === 191) try {
|
|
4025
|
+
text = new TextDecoder("utf-8", {
|
|
4026
|
+
fatal: true,
|
|
4027
|
+
ignoreBOM: true
|
|
4028
|
+
}).decode(b.subarray(3));
|
|
4029
|
+
} catch {
|
|
4030
|
+
return false;
|
|
4031
|
+
}
|
|
4032
|
+
else {
|
|
4033
|
+
for (let i = 0; i < b.length; i += 1) if (b[i] === 127 || b[i] === 159 || b[i] === 173) return false;
|
|
4034
|
+
text = known ?? textOf(b);
|
|
4035
|
+
}
|
|
4036
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
4037
|
+
const c = text.charCodeAt(i);
|
|
4038
|
+
if (c < 32 || c >= 127 && c <= 159 || c === 92 || c === 47 || c === 65279) return false;
|
|
4039
|
+
}
|
|
4040
|
+
return true;
|
|
4041
|
+
}
|
|
4042
|
+
const WORK_BASE = 65536;
|
|
4043
|
+
/** Units between two checkpoints. */
|
|
4044
|
+
const STEP$1 = 16384;
|
|
4045
|
+
/** Thrown when the work passes the budget: the reading ends, failing closed. */
|
|
4046
|
+
var OverBudget = class extends Error {};
|
|
4047
|
+
var Scan = class {
|
|
4048
|
+
/** The work charged so far. */
|
|
4049
|
+
count = 0;
|
|
4050
|
+
text;
|
|
4051
|
+
/** The work allowed: past it every method throws OverBudget. */
|
|
4052
|
+
budget;
|
|
4053
|
+
checkpoint;
|
|
4054
|
+
next = STEP$1;
|
|
4055
|
+
constructor(text, checkpoint) {
|
|
4056
|
+
this.text = text;
|
|
4057
|
+
this.checkpoint = checkpoint;
|
|
4058
|
+
this.budget = 32 * text.length + WORK_BASE;
|
|
4059
|
+
}
|
|
4060
|
+
/** Charges `units` of work; past the budget the reading ends. */
|
|
4061
|
+
charge(units) {
|
|
4062
|
+
this.count += units;
|
|
4063
|
+
if (this.count > this.budget) throw new OverBudget();
|
|
4064
|
+
}
|
|
4065
|
+
/** True, after the checkpoint, once STEP units have been charged since the last one: the caller then yields. */
|
|
4066
|
+
due() {
|
|
4067
|
+
if (this.count < this.next) return false;
|
|
4068
|
+
this.next = this.count + STEP$1;
|
|
4069
|
+
this.checkpoint();
|
|
4070
|
+
return true;
|
|
4071
|
+
}
|
|
4072
|
+
/** The character code at `i`. */
|
|
4073
|
+
code(i) {
|
|
4074
|
+
if ((this.count += 1) > this.budget) throw new OverBudget();
|
|
4075
|
+
return this.text.charCodeAt(i);
|
|
4076
|
+
}
|
|
4077
|
+
/** Whether `s` stands at `at`. */
|
|
4078
|
+
at(s, at) {
|
|
4079
|
+
this.charge(s.length);
|
|
4080
|
+
return this.text.startsWith(s, at);
|
|
4081
|
+
}
|
|
4082
|
+
/** Where `needle` first starts at or after `from` and ends by `to`; -1 when nowhere there. */
|
|
4083
|
+
find(needle, from, to) {
|
|
4084
|
+
const end = Math.min(to, this.text.length);
|
|
4085
|
+
if (end - from < needle.length) return -1;
|
|
4086
|
+
const k = this.text.slice(from, end).indexOf(needle);
|
|
4087
|
+
this.charge(k < 0 ? end - from : k + needle.length);
|
|
4088
|
+
return k < 0 ? -1 : from + k;
|
|
4089
|
+
}
|
|
4090
|
+
/** The text from `from` to `to`. */
|
|
4091
|
+
take(from, to) {
|
|
4092
|
+
this.charge(Math.max(0, to - from));
|
|
4093
|
+
return this.text.slice(from, to);
|
|
4094
|
+
}
|
|
4095
|
+
/** The end of the run of characters from `at` for which `ok(code)` holds, reading at most `max` + 1 of them. */
|
|
4096
|
+
run(at, max, ok) {
|
|
4097
|
+
const end = Math.min(this.text.length, at + max + 1);
|
|
4098
|
+
let i = at;
|
|
4099
|
+
while (i < end && ok(this.text.charCodeAt(i))) i += 1;
|
|
4100
|
+
this.charge(i - at + 1);
|
|
4101
|
+
return i;
|
|
4102
|
+
}
|
|
4103
|
+
/** The end of the run of XML white space from `at` (at most `max` + 1 characters read). */
|
|
4104
|
+
spaces(at, max) {
|
|
4105
|
+
const t = this.text;
|
|
4106
|
+
const end = Math.min(t.length, at + max + 1);
|
|
4107
|
+
let i = at;
|
|
4108
|
+
for (; i < end; i += 1) {
|
|
4109
|
+
const c = t.charCodeAt(i);
|
|
4110
|
+
if (c !== 32 && c !== 10 && c !== 9 && c !== 13) break;
|
|
4111
|
+
}
|
|
4112
|
+
this.charge(i - at + 1);
|
|
4113
|
+
return i;
|
|
4114
|
+
}
|
|
4115
|
+
/** The end of the run of ASCII name characters (letters, digits, `_`, `.`, `-`, `:`) from `at` (at most `max` + 1 read). */
|
|
4116
|
+
nameEnd(at, max) {
|
|
4117
|
+
const t = this.text;
|
|
4118
|
+
const end = Math.min(t.length, at + max + 1);
|
|
4119
|
+
let i = at;
|
|
4120
|
+
for (; i < end; i += 1) {
|
|
4121
|
+
const c = t.charCodeAt(i);
|
|
4122
|
+
if (!(c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 58 || c === 95 || c === 46 || c === 45)) break;
|
|
4123
|
+
}
|
|
4124
|
+
this.charge(i - at + 1);
|
|
4125
|
+
return i;
|
|
4126
|
+
}
|
|
4127
|
+
/** The end of the run of characters content needs no look at (not `&`, `<`, `>`, `]`) from `at` (at most `max` + 1 read). */
|
|
4128
|
+
plainEnd(at, max) {
|
|
4129
|
+
const t = this.text;
|
|
4130
|
+
const end = Math.min(t.length, at + max + 1);
|
|
4131
|
+
let i = at;
|
|
4132
|
+
for (; i < end; i += 1) {
|
|
4133
|
+
const c = t.charCodeAt(i);
|
|
4134
|
+
if (c === 38 || c === 60 || c === 62 || c === 93) break;
|
|
4135
|
+
}
|
|
4136
|
+
this.charge(i - at + 1);
|
|
4137
|
+
return i;
|
|
4138
|
+
}
|
|
4139
|
+
/** Where `needle` first stands in `s`; -1 when nowhere. */
|
|
4140
|
+
index(s, needle) {
|
|
4141
|
+
this.charge(s.length);
|
|
4142
|
+
return s.indexOf(needle);
|
|
4143
|
+
}
|
|
4144
|
+
/** A part of `s`. */
|
|
4145
|
+
part(s, from, to = s.length) {
|
|
4146
|
+
this.charge(Math.max(0, to - from));
|
|
4147
|
+
return s.slice(from, to);
|
|
4148
|
+
}
|
|
4149
|
+
/** The match of `re` on `s`, which must be at most `cap` characters (null when longer: no pattern runs on it). */
|
|
4150
|
+
match(re, s, cap) {
|
|
4151
|
+
if (s.length > cap) return null;
|
|
4152
|
+
this.charge(s.length);
|
|
4153
|
+
re.lastIndex = 0;
|
|
4154
|
+
return re.exec(s);
|
|
4155
|
+
}
|
|
4156
|
+
/** Whether `re` matches `s`, which must be at most `cap` characters (false when longer: no pattern runs on it). */
|
|
4157
|
+
fits(re, s, cap) {
|
|
4158
|
+
if (s.length > cap) return false;
|
|
4159
|
+
this.charge(s.length);
|
|
4160
|
+
re.lastIndex = 0;
|
|
4161
|
+
return re.test(s);
|
|
4162
|
+
}
|
|
4163
|
+
};
|
|
4164
|
+
// src/xml.ts
|
|
4165
|
+
const NOT = {
|
|
4166
|
+
wellFormed: false,
|
|
4167
|
+
doctype: false,
|
|
4168
|
+
root: null
|
|
4169
|
+
};
|
|
4170
|
+
const notWellFormed = () => ({ ...NOT });
|
|
4171
|
+
/** A qualified name in the subset read: one or two parts, each starting with a letter or `_`. */
|
|
4172
|
+
const QNAME = /^[A-Za-z_][A-Za-z0-9._-]*(?::[A-Za-z_][A-Za-z0-9._-]*)?$/;
|
|
4173
|
+
const XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
|
|
4174
|
+
/**
|
|
4175
|
+
* A character not allowed in a namespace name of the subset read: outside printable ASCII, or one RFC 3986 allows
|
|
4176
|
+
* nowhere in a URI (space, `<`, `>`, `"`, `{`, `}`, `|`, `\`, `^`, a backquote, and the brackets, which this check
|
|
4177
|
+
* does not tell apart from their use in an address). One character class: no backtracking, whatever the length.
|
|
4178
|
+
*/
|
|
4179
|
+
const NOT_URI = /[^\x21-\x7e]|[<>"{}|\\^`[\]]/;
|
|
4180
|
+
/** A `%` not followed by two hex digits. */
|
|
4181
|
+
const BAD_ESCAPE = /%(?![0-9A-Fa-f]{2})/;
|
|
4182
|
+
/** Limits of the subset read, well above what producers write: bytes of a namespace name as written, of a name, levels of nesting, attributes of one element. */
|
|
4183
|
+
const MAX_NAMESPACE = 2048;
|
|
4184
|
+
const MAX_NAME = 1024;
|
|
4185
|
+
const MAX_DEPTH = 200;
|
|
4186
|
+
const MAX_ATTRIBUTES = 1024;
|
|
4187
|
+
/** An xml:id: a name without a colon. */
|
|
4188
|
+
const ID = /^[A-Za-z_][A-Za-z0-9._-]*$/;
|
|
4189
|
+
/** A character that is not a Char. */
|
|
4190
|
+
const ILLEGAL = /[^\t\n\r\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u;
|
|
4191
|
+
/** The XML declaration: version 1.0, then optional encoding and standalone, in this order. */
|
|
4192
|
+
const DECLARATION = /<\?xml[ \t\r\n]+version[ \t\r\n]*=[ \t\r\n]*(?:"1\.0"|'1\.0')(?:[ \t\r\n]+encoding[ \t\r\n]*=[ \t\r\n]*(?:"([A-Za-z][A-Za-z0-9._-]*)"|'([A-Za-z][A-Za-z0-9._-]*)'))?(?:[ \t\r\n]+standalone[ \t\r\n]*=[ \t\r\n]*(?:"(?:yes|no)"|'(?:yes|no)'))?[ \t\r\n]*\?>/y;
|
|
4193
|
+
const PREDEFINED = /* @__PURE__ */ new Map([
|
|
4194
|
+
["lt", "<"],
|
|
4195
|
+
["gt", ">"],
|
|
4196
|
+
["amp", "&"],
|
|
4197
|
+
["apos", "'"],
|
|
4198
|
+
["quot", "\""]
|
|
4199
|
+
]);
|
|
4200
|
+
const XML_NAMESPACE$1 = "http://www.w3.org/XML/1998/namespace";
|
|
4201
|
+
const isSpace = (c) => c === 32 || c === 9 || c === 10 || c === 13;
|
|
4202
|
+
const isChar = (n) => n === 9 || n === 10 || n === 13 || n >= 32 && n <= 55295 || n >= 57344 && n <= 65533 || n >= 65536 && n <= 1114111;
|
|
4203
|
+
/**
|
|
4204
|
+
* The text of the document: valid UTF-8, with or without one byte order mark at the start; null when the bytes are
|
|
4205
|
+
* not that (UTF-16 and UTF-32 are outside the subset read). A second byte order mark is text before the root.
|
|
4206
|
+
*/
|
|
4207
|
+
function decodeText(b) {
|
|
4208
|
+
try {
|
|
4209
|
+
const start = b[0] === 239 && b[1] === 187 && b[2] === 191 ? 3 : 0;
|
|
4210
|
+
return new TextDecoder("utf-8", {
|
|
4211
|
+
fatal: true,
|
|
4212
|
+
ignoreBOM: true
|
|
4213
|
+
}).decode(b.subarray(start));
|
|
4214
|
+
} catch {
|
|
4215
|
+
return null;
|
|
4216
|
+
}
|
|
4217
|
+
}
|
|
4218
|
+
/** The longest reference read (`&#x` and its digits, leading zeros included): a longer one is not read. */
|
|
4219
|
+
const MAX_REFERENCE = 64;
|
|
4220
|
+
/** The character a reference names (`lt`, `#65`, `#x41`), or null for one that is not a predefined entity or a legal character. */
|
|
4221
|
+
function referenced(ref, scan) {
|
|
4222
|
+
if (ref.startsWith("#")) {
|
|
4223
|
+
const hex = ref.startsWith("#x");
|
|
4224
|
+
const digits = scan.part(ref, hex ? 2 : 1);
|
|
4225
|
+
if (!scan.fits(hex ? HEX_DIGITS : DIGITS, digits, MAX_REFERENCE)) return null;
|
|
4226
|
+
let first = 0;
|
|
4227
|
+
while (first < digits.length - 1 && digits.charCodeAt(first) === 48) first += 1;
|
|
4228
|
+
const significant = scan.part(digits, first);
|
|
4229
|
+
const n = Number.parseInt(significant, hex ? 16 : 10);
|
|
4230
|
+
return significant.length <= 8 && isChar(n) ? String.fromCodePoint(n) : null;
|
|
4231
|
+
}
|
|
4232
|
+
return PREDEFINED.get(ref) ?? null;
|
|
4233
|
+
}
|
|
4234
|
+
const HEX_DIGITS = /^[0-9A-Fa-f]+$/;
|
|
4235
|
+
const DIGITS = /^[0-9]+$/;
|
|
4236
|
+
/**
|
|
4237
|
+
* Whether the text from `from` to `to` is well-formed character data (`attribute` false: no `]]>`) or an
|
|
4238
|
+
* attribute value (`attribute` true: no `<`), with only references to predefined entities or legal
|
|
4239
|
+
* characters. One forward pass: a reference ends at the first `;` within MAX_REFERENCE characters.
|
|
4240
|
+
*/
|
|
4241
|
+
function* contentOk(scan, from, to, attribute) {
|
|
4242
|
+
let brackets = 0;
|
|
4243
|
+
for (let k = from; k < to; k += 1) {
|
|
4244
|
+
if (scan.due()) yield;
|
|
4245
|
+
const plain = scan.plainEnd(k, Math.min(to - k, PLAIN_RUN) - 1);
|
|
4246
|
+
if (plain > k) {
|
|
4247
|
+
brackets = 0;
|
|
4248
|
+
k = plain - 1;
|
|
4249
|
+
continue;
|
|
4250
|
+
}
|
|
4251
|
+
const c = scan.code(k);
|
|
4252
|
+
if (c === 93) {
|
|
4253
|
+
brackets += 1;
|
|
4254
|
+
continue;
|
|
4255
|
+
}
|
|
4256
|
+
if (c === 62 && brackets >= 2 && !attribute) return false;
|
|
4257
|
+
brackets = 0;
|
|
4258
|
+
if (c === 60 && attribute) return false;
|
|
4259
|
+
if (c !== 38) continue;
|
|
4260
|
+
const semi = scan.find(";", k + 1, Math.min(to, k + 2 + MAX_REFERENCE));
|
|
4261
|
+
if (semi < 0 || referenced(scan.take(k + 1, semi), scan) === null) return false;
|
|
4262
|
+
k = semi;
|
|
4263
|
+
}
|
|
4264
|
+
return true;
|
|
4265
|
+
}
|
|
4266
|
+
/**
|
|
4267
|
+
* A value as an XML parser reports it: references to the five predefined entities and to characters
|
|
4268
|
+
* decoded; in an attribute, white space normalised (XML 1.0, 3.3.3). Null for a reference that is neither.
|
|
4269
|
+
* Charged to `scan`, and yields when it is due.
|
|
4270
|
+
*/
|
|
4271
|
+
function* xmlValue(raw, attribute, scan) {
|
|
4272
|
+
if (!scan.fits(attribute ? TO_NORMALISE : TO_DECODE, raw, raw.length)) return raw;
|
|
4273
|
+
const parts = [];
|
|
4274
|
+
let run = 0;
|
|
4275
|
+
for (let k = 0; k < raw.length; k += 1) {
|
|
4276
|
+
scan.charge(1);
|
|
4277
|
+
if (scan.due()) yield;
|
|
4278
|
+
const c = raw.charCodeAt(k);
|
|
4279
|
+
let replaced;
|
|
4280
|
+
const from = k;
|
|
4281
|
+
if (c === 38) {
|
|
4282
|
+
const semi = scan.index(scan.part(raw, k + 1, Math.min(raw.length, k + 2 + MAX_REFERENCE)), ";");
|
|
4283
|
+
replaced = semi < 0 ? null : referenced(scan.part(raw, k + 1, k + 1 + semi), scan);
|
|
4284
|
+
if (replaced === null) return null;
|
|
4285
|
+
k += 1 + semi;
|
|
4286
|
+
} else if (c === 13) {
|
|
4287
|
+
if (raw.charCodeAt(k + 1) === 10) k += 1;
|
|
4288
|
+
replaced = attribute ? " " : "\n";
|
|
4289
|
+
} else if (attribute && (c === 10 || c === 9)) replaced = " ";
|
|
4290
|
+
else continue;
|
|
4291
|
+
if (from > run) parts.push(scan.part(raw, run, from));
|
|
4292
|
+
parts.push(replaced);
|
|
4293
|
+
run = k + 1;
|
|
4294
|
+
}
|
|
4295
|
+
if (run < raw.length) parts.push(scan.part(raw, run));
|
|
4296
|
+
return parts.join("");
|
|
4297
|
+
}
|
|
4298
|
+
/** The longest run of plain content read at once (the checkpoint comes between runs). */
|
|
4299
|
+
const PLAIN_RUN = 16384;
|
|
4300
|
+
const TO_NORMALISE = /[&\r\n\t]/;
|
|
4301
|
+
const TO_DECODE = /[&\r]/;
|
|
4302
|
+
/** Characters tested for legality in one step. */
|
|
4303
|
+
const CHUNK = 65536;
|
|
4304
|
+
/**
|
|
4305
|
+
* The well-formedness of `bytes` as an XML document. All work on the text goes through one Scan (see scan.ts): it
|
|
4306
|
+
* yields and calls `checkpoint` as the work is charged, and a document whose check would pass the work budget is
|
|
4307
|
+
* not well-formed. `scan` receives the Scan, for tests that read the work done.
|
|
4308
|
+
*/
|
|
4309
|
+
function* checkXml(bytes, checkpoint, seen) {
|
|
4310
|
+
const text = decodeText(bytes);
|
|
4311
|
+
if (text === null) return notWellFormed();
|
|
4312
|
+
return yield* checkText(text, checkpoint, seen);
|
|
4313
|
+
}
|
|
4314
|
+
/** The well-formedness of a document already decoded by decodeText (see checkXml). */
|
|
4315
|
+
function* checkText(text, checkpoint, seen) {
|
|
4316
|
+
const scan = new Scan(text, checkpoint);
|
|
4317
|
+
seen?.(scan);
|
|
4318
|
+
try {
|
|
4319
|
+
return yield* check(scan);
|
|
4320
|
+
} catch (error) {
|
|
4321
|
+
if (error instanceof OverBudget) return notWellFormed();
|
|
4322
|
+
throw error;
|
|
4323
|
+
}
|
|
4324
|
+
}
|
|
4325
|
+
function* check(scan) {
|
|
4326
|
+
const n = scan.text.length;
|
|
4327
|
+
for (let k = 0; k < n;) {
|
|
4328
|
+
yield;
|
|
4329
|
+
scan.due();
|
|
4330
|
+
let end = Math.min(n, k + CHUNK);
|
|
4331
|
+
const last = scan.code(end - 1);
|
|
4332
|
+
if (end < n && last >= 55296 && last <= 56319) end += 1;
|
|
4333
|
+
if (scan.fits(ILLEGAL, scan.take(k, end), 65537)) return notWellFormed();
|
|
4334
|
+
k = end;
|
|
4335
|
+
}
|
|
4336
|
+
/** The name at `at` (at most MAX_NAME + 1 characters are read), or undefined when none starts there. */
|
|
4337
|
+
const name = (at) => {
|
|
4338
|
+
const c = scan.code(at);
|
|
4339
|
+
if (!(isNameStart(c) || c === 58)) return void 0;
|
|
4340
|
+
return scan.take(at, scan.nameEnd(at, MAX_NAME));
|
|
4341
|
+
};
|
|
4342
|
+
const skipSpace = (at) => scan.spaces(at, n);
|
|
4343
|
+
/** A name read by `name` is a qualified name when it has no colon (it starts with a letter or `_`), else when it matches QNAME. */
|
|
4344
|
+
const qualified = (s) => s.charCodeAt(0) !== 58 && scan.index(s, ":") < 0 || scan.fits(QNAME, s, MAX_NAME);
|
|
4345
|
+
let i = 0;
|
|
4346
|
+
if (scan.at("<?xml", 0) && isSpace(scan.code(5))) {
|
|
4347
|
+
const end = scan.find("?>", 0, MAX_DECLARATION);
|
|
4348
|
+
const m = end < 0 ? null : scan.match(DECLARATION, scan.take(0, end + 2), MAX_DECLARATION);
|
|
4349
|
+
if (!m || m[0].length !== end + 2) return notWellFormed();
|
|
4350
|
+
const encoding = (m[1] ?? m[2])?.toLowerCase();
|
|
4351
|
+
if (encoding !== void 0 && encoding !== "utf-8") return notWellFormed();
|
|
4352
|
+
i = end + 2;
|
|
4353
|
+
}
|
|
4354
|
+
const open = [];
|
|
4355
|
+
const bound = [];
|
|
4356
|
+
const scope = /* @__PURE__ */ new Map([["xml", [XML_NAMESPACE$1]]]);
|
|
4357
|
+
const lookup = (prefix) => scope.get(prefix)?.at(-1);
|
|
4358
|
+
const unbind = (prefixes) => {
|
|
4359
|
+
for (const p of prefixes ?? []) scope.get(p).pop();
|
|
4360
|
+
};
|
|
4361
|
+
let rooted = false;
|
|
4362
|
+
let root = null;
|
|
4363
|
+
const prefixOf = (qname) => {
|
|
4364
|
+
const colon = scan.index(qname, ":");
|
|
4365
|
+
return colon < 0 ? null : scan.part(qname, 0, colon);
|
|
4366
|
+
};
|
|
4367
|
+
const bad = (qname, attribute) => {
|
|
4368
|
+
const prefix = prefixOf(qname);
|
|
4369
|
+
if (prefix === null) return false;
|
|
4370
|
+
return attribute && prefix === "xmlns" ? false : lookup(prefix) === void 0;
|
|
4371
|
+
};
|
|
4372
|
+
while (i < n) {
|
|
4373
|
+
if (scan.due()) yield;
|
|
4374
|
+
if (scan.code(i) !== 60) {
|
|
4375
|
+
const next = scan.find("<", i, n);
|
|
4376
|
+
const end = next < 0 ? n : next;
|
|
4377
|
+
if (open.length === 0) {
|
|
4378
|
+
if (skipSpace(i) < end) return notWellFormed();
|
|
4379
|
+
} else if (!(yield* contentOk(scan, i, end, false))) return notWellFormed();
|
|
4380
|
+
i = end;
|
|
4381
|
+
continue;
|
|
4382
|
+
}
|
|
4383
|
+
const c1 = scan.code(i + 1);
|
|
4384
|
+
if (c1 === 33) {
|
|
4385
|
+
if (scan.at("<!--", i)) {
|
|
4386
|
+
const dashes = scan.find("--", i + 4, n);
|
|
4387
|
+
if (dashes < 0 || scan.code(dashes + 2) !== 62) return notWellFormed();
|
|
4388
|
+
i = dashes + 3;
|
|
4389
|
+
continue;
|
|
4390
|
+
}
|
|
4391
|
+
if (scan.at("<![CDATA[", i)) {
|
|
4392
|
+
const end = scan.find("]]>", i + 9, n);
|
|
4393
|
+
if (open.length === 0 || end < 0) return notWellFormed();
|
|
4394
|
+
i = end + 3;
|
|
4395
|
+
continue;
|
|
4396
|
+
}
|
|
4397
|
+
if (scan.at("<!DOCTYPE", i)) return {
|
|
4398
|
+
wellFormed: false,
|
|
4399
|
+
doctype: true,
|
|
4400
|
+
root: null
|
|
4401
|
+
};
|
|
4402
|
+
return notWellFormed();
|
|
4403
|
+
}
|
|
4404
|
+
if (c1 === 63) {
|
|
4405
|
+
const target = name(i + 2);
|
|
4406
|
+
if (!target || target.length > MAX_NAME || target.toLowerCase() === "xml" || scan.index(target, ":") >= 0) return notWellFormed();
|
|
4407
|
+
const after = i + 2 + target.length;
|
|
4408
|
+
const end = scan.find("?>", after, n);
|
|
4409
|
+
if (end < 0 || end > after && !isSpace(scan.code(after))) return notWellFormed();
|
|
4410
|
+
i = end + 2;
|
|
4411
|
+
continue;
|
|
4412
|
+
}
|
|
4413
|
+
if (c1 === 47) {
|
|
4414
|
+
const tag = name(i + 2);
|
|
4415
|
+
if (!tag || open.at(-1) !== tag) return notWellFormed();
|
|
4416
|
+
const j = skipSpace(i + 2 + tag.length);
|
|
4417
|
+
if (scan.code(j) !== 62) return notWellFormed();
|
|
4418
|
+
open.pop();
|
|
4419
|
+
unbind(bound.pop());
|
|
4420
|
+
i = j + 1;
|
|
4421
|
+
continue;
|
|
4422
|
+
}
|
|
4423
|
+
if (rooted && open.length === 0) return notWellFormed();
|
|
4424
|
+
const tag = name(i + 1);
|
|
4425
|
+
if (!tag || tag.length > MAX_NAME || !qualified(tag) || open.length >= MAX_DEPTH) return notWellFormed();
|
|
4426
|
+
let j = i + 1 + tag.length;
|
|
4427
|
+
/** The namespace declarations of this element. */
|
|
4428
|
+
let declared = null;
|
|
4429
|
+
const names = /* @__PURE__ */ new Set();
|
|
4430
|
+
const values = rooted ? null : /* @__PURE__ */ new Map();
|
|
4431
|
+
let empty = false;
|
|
4432
|
+
for (;;) {
|
|
4433
|
+
if (scan.due()) yield;
|
|
4434
|
+
const before = j;
|
|
4435
|
+
j = skipSpace(j);
|
|
4436
|
+
if (scan.code(j) === 62) break;
|
|
4437
|
+
if (scan.code(j) === 47 && scan.code(j + 1) === 62) {
|
|
4438
|
+
empty = true;
|
|
4439
|
+
j += 1;
|
|
4440
|
+
break;
|
|
4441
|
+
}
|
|
4442
|
+
if (j === before) return notWellFormed();
|
|
4443
|
+
const key = name(j);
|
|
4444
|
+
if (!key || key.length > MAX_NAME || !qualified(key) || names.has(key) || names.size >= MAX_ATTRIBUTES) return notWellFormed();
|
|
4445
|
+
names.add(key);
|
|
4446
|
+
j = skipSpace(j + key.length);
|
|
4447
|
+
if (scan.code(j) !== 61) return notWellFormed();
|
|
4448
|
+
j = skipSpace(j + 1);
|
|
4449
|
+
const quote = scan.code(j);
|
|
4450
|
+
if (quote !== 34 && quote !== 39) return notWellFormed();
|
|
4451
|
+
const close = scan.find(quote === 34 ? "\"" : "'", j + 1, n);
|
|
4452
|
+
if (close < 0) return notWellFormed();
|
|
4453
|
+
if (key === "xmlns" || key.startsWith("xmlns:")) {
|
|
4454
|
+
if (close - j - 1 > MAX_NAMESPACE || scan.find("&", j + 1, close) >= 0) return notWellFormed();
|
|
4455
|
+
(declared ??= []).push([key === "xmlns" ? "" : scan.part(key, 6), scan.take(j + 1, close)]);
|
|
4456
|
+
}
|
|
4457
|
+
if (!(yield* contentOk(scan, j + 1, close, true))) return notWellFormed();
|
|
4458
|
+
if (values) values.set(key, close - j - 1 > MAX_NAMESPACE ? "" : scan.take(j + 1, close));
|
|
4459
|
+
if (key === "xml:id" && (close - j - 1 > MAX_NAME || !scan.fits(ID, scan.take(j + 1, close), MAX_NAME))) return notWellFormed();
|
|
4460
|
+
j = close + 1;
|
|
4461
|
+
}
|
|
4462
|
+
let prefixes = null;
|
|
4463
|
+
for (const [prefix, uri] of declared ?? []) {
|
|
4464
|
+
if (scan.fits(NOT_URI, uri, MAX_NAMESPACE) || scan.fits(BAD_ESCAPE, uri, MAX_NAMESPACE) || uri.startsWith(":") || prefix === "xmlns" || prefix !== "" && uri === "" || uri === XMLNS_NAMESPACE || uri === XML_NAMESPACE$1 !== (prefix === "xml")) {
|
|
4465
|
+
unbind(prefixes);
|
|
4466
|
+
return notWellFormed();
|
|
4467
|
+
}
|
|
4468
|
+
let list = scope.get(prefix);
|
|
4469
|
+
if (!list) scope.set(prefix, list = []);
|
|
4470
|
+
list.push(uri);
|
|
4471
|
+
(prefixes ??= []).push(prefix);
|
|
4472
|
+
}
|
|
4473
|
+
let expanded = null;
|
|
4474
|
+
let wrong = bad(tag, false) || tag.startsWith("xmlns:");
|
|
4475
|
+
for (const key of names) {
|
|
4476
|
+
if (wrong) break;
|
|
4477
|
+
if (key === "xmlns" || key.startsWith("xmlns:")) continue;
|
|
4478
|
+
if (bad(key, true)) wrong = true;
|
|
4479
|
+
else {
|
|
4480
|
+
const prefix = prefixOf(key);
|
|
4481
|
+
if (prefix !== null) {
|
|
4482
|
+
const id = `${lookup(prefix)} ${scan.part(key, prefix.length + 1)}`;
|
|
4483
|
+
if ((expanded ??= /* @__PURE__ */ new Set()).has(id)) wrong = true;
|
|
4484
|
+
expanded.add(id);
|
|
4485
|
+
}
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4488
|
+
if (wrong) {
|
|
4489
|
+
unbind(prefixes);
|
|
4490
|
+
return notWellFormed();
|
|
4491
|
+
}
|
|
4492
|
+
if (!rooted) {
|
|
4493
|
+
rooted = true;
|
|
4494
|
+
root = yield* rootName(tag, values, scan);
|
|
4495
|
+
if (!root) return notWellFormed();
|
|
4496
|
+
}
|
|
4497
|
+
if (!empty) {
|
|
4498
|
+
open.push(tag);
|
|
4499
|
+
bound.push(prefixes);
|
|
4500
|
+
} else unbind(prefixes);
|
|
4501
|
+
i = j + 1;
|
|
4502
|
+
}
|
|
4503
|
+
if (!rooted || open.length > 0) return notWellFormed();
|
|
4504
|
+
return {
|
|
4505
|
+
wellFormed: true,
|
|
4506
|
+
doctype: false,
|
|
4507
|
+
root
|
|
4508
|
+
};
|
|
4509
|
+
}
|
|
4510
|
+
/** The longest XML declaration read. */
|
|
4511
|
+
const MAX_DECLARATION = 256;
|
|
4512
|
+
const isNameStart = (c) => c >= 65 && c <= 90 || c >= 97 && c <= 122 || c === 95;
|
|
4513
|
+
/** The root element's local name and namespace, through the root's own namespace declarations; null when its prefix is not bound. */
|
|
4514
|
+
function* rootName(tag, attributes, scan) {
|
|
4515
|
+
const colon = scan.index(tag, ":");
|
|
4516
|
+
const prefix = colon < 0 ? null : scan.part(tag, 0, colon);
|
|
4517
|
+
const local = colon < 0 ? tag : scan.part(tag, colon + 1);
|
|
4518
|
+
if (prefix === "xmlns") return null;
|
|
4519
|
+
if (prefix === "xml") return {
|
|
4520
|
+
name: local,
|
|
4521
|
+
namespace: XML_NAMESPACE$1
|
|
4522
|
+
};
|
|
4523
|
+
const raw = attributes.get(prefix === null ? "xmlns" : `xmlns:${prefix}`);
|
|
4524
|
+
const v = raw === void 0 ? null : yield* xmlValue(raw, true, scan);
|
|
4525
|
+
if (v !== null && v !== v.trim()) return null;
|
|
4526
|
+
if (prefix === null) return {
|
|
4527
|
+
name: tag,
|
|
4528
|
+
namespace: v || null
|
|
4529
|
+
};
|
|
4530
|
+
return v ? {
|
|
4531
|
+
name: local,
|
|
4532
|
+
namespace: v
|
|
4533
|
+
} : null;
|
|
4534
|
+
}
|
|
4535
|
+
// src/embedded.ts
|
|
4536
|
+
/** Nodes visited between two yields. */
|
|
4537
|
+
const STEP = 256;
|
|
4538
|
+
/** Keys of a file specification that name the file for one platform, or relate it to other files or systems. */
|
|
4539
|
+
const SPEC_OTHER = [
|
|
4540
|
+
"DOS",
|
|
4541
|
+
"Mac",
|
|
4542
|
+
"Unix",
|
|
4543
|
+
"RF",
|
|
4544
|
+
"FS"
|
|
4545
|
+
];
|
|
4546
|
+
/** Keys of a stream dictionary that put its data in another file. */
|
|
4547
|
+
const EXTERNAL_DATA = [
|
|
4548
|
+
"F",
|
|
4549
|
+
"FFilter",
|
|
4550
|
+
"FDecodeParms"
|
|
4551
|
+
];
|
|
4552
|
+
/**
|
|
4553
|
+
* The longest name, name-tree key, `/Limits` string or date read, in bytes as written. No file name comes near it;
|
|
4554
|
+
* a longer string is not complete, and is compared with nothing.
|
|
4555
|
+
*/
|
|
4556
|
+
const MAX_TEXT = 4096;
|
|
4557
|
+
/** The most entries of a file specification (MuPDF loses the name and the data of one of more than 101). */
|
|
4558
|
+
const MAX_SPEC_ENTRIES = 100;
|
|
4559
|
+
/**
|
|
4560
|
+
* pypdf's limits on what it reads: the entries of an array or a dictionary (it refuses the object), the bytes a
|
|
4561
|
+
* stream is written in or decodes to (one embedded file, an object stream), and the bytes all embedded files
|
|
4562
|
+
* decode to together (its attachments dictionary).
|
|
4563
|
+
*/
|
|
4564
|
+
const PYPDF_ENTRIES = 1e5;
|
|
4565
|
+
const PYPDF_FILE = 75e6;
|
|
4566
|
+
const PYPDF_FILES = 25e7;
|
|
4567
|
+
/** The most entries a name tree lists: pypdf lists no file from a longer one. */
|
|
4568
|
+
const MAX_ENTRIES = 1e3;
|
|
4569
|
+
/** The text a report may carry however small the file, in characters (see describeSpecs). */
|
|
4570
|
+
const MIN_REPORT_TEXT = 1 << 20;
|
|
4571
|
+
/** Whether a media type is XML: `text/xml`, `application/xml`, or a type with the `+xml` suffix. */
|
|
4572
|
+
const isXmlType = (type) => {
|
|
4573
|
+
if (type === null) return false;
|
|
4574
|
+
const t = type.split(";")[0].trim().toLowerCase();
|
|
4575
|
+
return t === "text/xml" || t === "application/xml" || /^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*\+xml$/.test(t);
|
|
4576
|
+
};
|
|
4577
|
+
/**
|
|
4578
|
+
* Whether the bytes at `at` are `num gen obj` at the start of a line: the numbers in digits, one space between
|
|
4579
|
+
* the three. (What follows `obj` needs no check: the definition was read with `obj` as a keyword there, and white
|
|
4580
|
+
* space or a byte readers read otherwise after it is refused as syntax.)
|
|
4581
|
+
*/
|
|
4582
|
+
function plainHeader(b, at, num, gen) {
|
|
4583
|
+
if (b[at - 1] !== 10 && b[at - 1] !== 13) return false;
|
|
4584
|
+
const digits = (from) => {
|
|
4585
|
+
let i = from;
|
|
4586
|
+
let value = 0;
|
|
4587
|
+
while (b[i] >= 48 && b[i] <= 57) value = value * 10 + b[i++] - 48;
|
|
4588
|
+
return i === from ? null : {
|
|
4589
|
+
value,
|
|
4590
|
+
end: i
|
|
4591
|
+
};
|
|
4592
|
+
};
|
|
4593
|
+
const n = digits(at);
|
|
4594
|
+
if (!n || n.value !== num || b[n.end] !== 32) return false;
|
|
4595
|
+
const g = digits(n.end + 1);
|
|
4596
|
+
return g !== null && g.value === gen && b[g.end] === 32 && b[g.end + 1] === 111;
|
|
4597
|
+
}
|
|
4598
|
+
/**
|
|
4599
|
+
* Whether the file ends with `startxref` at the start of a line, an end of line, the offset `at` in digits, an end
|
|
4600
|
+
* of line and `%%EOF`, then at most one end of line. Readers look for `startxref` from the end, each over its own distance and each
|
|
4601
|
+
* skipping other bytes differently (a comment, a second `startxref`, more bytes than qpdf looks at).
|
|
4602
|
+
*/
|
|
4603
|
+
function plainEnding(b, at) {
|
|
4604
|
+
const tail = Buffer.from(b.buffer, b.byteOffset + Math.max(0, b.length - 64), Math.min(b.length, 64)).toString("latin1");
|
|
4605
|
+
const m = /(?:^|[\r\n])startxref(?:\r\n|\r|\n)(\d+)(?:\r\n|\r|\n)%%EOF(?:\r\n|\r|\n)?$/.exec(tail);
|
|
4606
|
+
return m !== null && at !== void 0 && Number(m[1]) === at;
|
|
4607
|
+
}
|
|
4608
|
+
/** Byte order of two strings, as name-tree keys are sorted. */
|
|
4609
|
+
const compareBytes = (a, b) => Buffer.compare(a, b);
|
|
4610
|
+
/** A date as the format writes it: D:YYYY, then optionally MM, DD, HH, mm, SS, and Z or an offset HH'mm. */
|
|
4611
|
+
const PDF_DATE = /^D:(\d{4})(?:(\d{2})(?:(\d{2})(?:(\d{2})(?:(\d{2})(?:(\d{2})(?:Z|[+-](\d{2})(?:'(\d{2})'?)?)?)?)?)?)?)?$/;
|
|
4612
|
+
/**
|
|
4613
|
+
* Whether `text` is a date as the format writes it with every field in range: the year from 1970 (MuPDF warns
|
|
4614
|
+
* about an earlier one), the month 01 to 12, the day 01 to 31, the hour 00 to 23, minutes and seconds 00 to 59, an
|
|
4615
|
+
* offset of at most 14 hours and 59 minutes (MuPDF warns about any field out of range).
|
|
4616
|
+
*/
|
|
4617
|
+
function plainDate(text) {
|
|
4618
|
+
const m = PDF_DATE.exec(text);
|
|
4619
|
+
if (m === null) return false;
|
|
4620
|
+
const [, year, month, day, hour, minute, second, offsetHours, offsetMinutes] = m.map((x) => x === void 0 ? void 0 : Number(x));
|
|
4621
|
+
const within = (v, lo, hi) => v === void 0 || v >= lo && v <= hi;
|
|
4622
|
+
return year >= 1970 && within(month, 1, 12) && within(day, 1, 31) && within(hour, 0, 23) && within(minute, 0, 59) && within(second, 0, 59) && within(offsetHours, 0, 14) && within(offsetMinutes, 0, 59);
|
|
4623
|
+
}
|
|
4624
|
+
/**
|
|
4625
|
+
* Follows references through the definitions in force (at most 8), without counting the objects as read. A
|
|
4626
|
+
* reference to an object the chain does not list as in use, or under another generation, is null, as readers
|
|
4627
|
+
* that check it read it.
|
|
4628
|
+
*/
|
|
4629
|
+
const peek = (reader, v) => {
|
|
4630
|
+
let x = v ?? null;
|
|
4631
|
+
for (let hops = 0; x instanceof PdfRef && hops < 8; hops += 1) {
|
|
4632
|
+
const d = reader.inForce(x.num);
|
|
4633
|
+
x = d && d.gen === x.gen && reader.listedInForce(x.num) ? d.value : null;
|
|
4634
|
+
}
|
|
4635
|
+
return x instanceof PdfRef ? null : x;
|
|
4636
|
+
};
|
|
4637
|
+
/**
|
|
4638
|
+
* Whether pdf.js opens the file without a warning about its pages. When it opens a file it takes the number of
|
|
4639
|
+
* pages from the top /Pages node's /Count (or from the linearization dictionary, when it accepts one) and, for
|
|
4640
|
+
* more than one page, loads the last page by walking the tree; any failure there is a warning, and a top node
|
|
4641
|
+
* that is no dictionary, or a /Count that is no integer, fails the opening. So: /Pages a dictionary with an
|
|
4642
|
+
* integer /Count; for more than one page, a tree whose nodes are reached once by reference, each node with
|
|
4643
|
+
* /Kids an array and a /Count that is the number of pages below it; for an accepted linearization dictionary,
|
|
4644
|
+
* /N that /Count and /O a page. Nothing else about pages is checked: pages are not part of what `complete` says.
|
|
4645
|
+
*/
|
|
4646
|
+
function* pagesOpen(reader, catalog, first, tick) {
|
|
4647
|
+
const value = (v) => peek(reader, v);
|
|
4648
|
+
const root = value(catalog.get("Pages"));
|
|
4649
|
+
if (!(root instanceof PdfDict) || root.stream) return false;
|
|
4650
|
+
const total = value(root.get("Count"));
|
|
4651
|
+
if (typeof total !== "number" || !Number.isInteger(total)) return false;
|
|
4652
|
+
if (first?.has("Linearized") && first.get("L") === reader.bytes.length) {
|
|
4653
|
+
if (first.get("N") !== total) return false;
|
|
4654
|
+
const o = first.get("O");
|
|
4655
|
+
const page = typeof o === "number" ? value(new PdfRef(o, 0)) : null;
|
|
4656
|
+
if (!(page instanceof PdfDict) || !(isName(value(page.get("Type")), "Page") || !page.has("Type") && !page.has("Kids") && page.has("Contents"))) return false;
|
|
4657
|
+
}
|
|
4658
|
+
if (total <= 1) return true;
|
|
4659
|
+
const leaf = (n) => isName(value(n.get("Type")), "Page") || !n.has("Kids");
|
|
4660
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4661
|
+
let pages = 0;
|
|
4662
|
+
const stack = [root];
|
|
4663
|
+
while (stack.length > 0) {
|
|
4664
|
+
if (tick()) yield;
|
|
4665
|
+
const item = stack.pop();
|
|
4666
|
+
if (!(item instanceof PdfDict)) {
|
|
4667
|
+
if (value(item.done.get("Count")) !== pages - item.before) return false;
|
|
4668
|
+
continue;
|
|
4669
|
+
}
|
|
4670
|
+
if (item.stream || seen.has(item) || seen.size > reader.work.limits.maxObjects) return false;
|
|
4671
|
+
seen.add(item);
|
|
4672
|
+
if (leaf(item)) {
|
|
4673
|
+
pages += 1;
|
|
4674
|
+
continue;
|
|
4675
|
+
}
|
|
4676
|
+
const kids = value(item.get("Kids"));
|
|
4677
|
+
if (!Array.isArray(kids)) return false;
|
|
4678
|
+
stack.push({
|
|
4679
|
+
done: item,
|
|
4680
|
+
before: pages
|
|
4681
|
+
});
|
|
4682
|
+
for (let i = kids.length - 1; i >= 0; i -= 1) {
|
|
4683
|
+
const kid = value(kids[i]);
|
|
4684
|
+
if (!(kids[i] instanceof PdfRef) || !(kid instanceof PdfDict)) return false;
|
|
4685
|
+
stack.push(kid);
|
|
4686
|
+
}
|
|
4687
|
+
}
|
|
4688
|
+
return pages === total;
|
|
4689
|
+
}
|
|
4690
|
+
var StringText = class {
|
|
4691
|
+
holder;
|
|
4692
|
+
long;
|
|
4693
|
+
irregular;
|
|
4694
|
+
#decode;
|
|
4695
|
+
#bytes;
|
|
4696
|
+
#ascii;
|
|
4697
|
+
#text;
|
|
4698
|
+
#plain;
|
|
4699
|
+
#key;
|
|
4700
|
+
constructor(holder, decode, long, irregular) {
|
|
4701
|
+
this.holder = holder;
|
|
4702
|
+
this.#decode = decode;
|
|
4703
|
+
this.long = long;
|
|
4704
|
+
this.irregular = irregular;
|
|
4705
|
+
}
|
|
4706
|
+
/** Decoded when first asked for: a string too long to be compared is never decoded for a comparison. */
|
|
4707
|
+
get bytes() {
|
|
4708
|
+
this.#bytes ??= this.#decode();
|
|
4709
|
+
return this.#bytes;
|
|
4710
|
+
}
|
|
4711
|
+
get ascii() {
|
|
4712
|
+
if (this.#ascii === void 0) this.#ascii = printableAscii(this.bytes);
|
|
4713
|
+
return this.#ascii;
|
|
4714
|
+
}
|
|
4715
|
+
get text() {
|
|
4716
|
+
this.#text ??= this.ascii ?? textOf(this.bytes);
|
|
4717
|
+
return this.#text;
|
|
4718
|
+
}
|
|
4719
|
+
get plain() {
|
|
4720
|
+
this.#plain ??= this.ascii !== null || plainText(this.bytes, this.text);
|
|
4721
|
+
return this.#plain;
|
|
4722
|
+
}
|
|
4723
|
+
get key() {
|
|
4724
|
+
this.#key ??= {
|
|
4725
|
+
bytes: this.bytes,
|
|
4726
|
+
text: Buffer.from(this.text, "utf8")
|
|
4727
|
+
};
|
|
4728
|
+
return this.#key;
|
|
4729
|
+
}
|
|
4730
|
+
};
|
|
4731
|
+
/** The newest trailer's /Size, or -1. */
|
|
4732
|
+
const newestSizeOf = (reader) => typeof reader.trailer.Size === "number" ? reader.trailer.Size : -1;
|
|
4733
|
+
/**
|
|
4734
|
+
* Whether a definition is written in the canonical form (see canonical.ts): its header and its text, or for a
|
|
4735
|
+
* member of an object stream its slot and the header of the stream.
|
|
4736
|
+
*/
|
|
4737
|
+
function canonicalDefinition(reader, d) {
|
|
4738
|
+
if (d.container) return d.plain === true && d.container.membersPlain === true && canonicalDefinition(reader, d.container);
|
|
4739
|
+
return d.plain === true && canonicalHeader(reader.bytes, d.offset);
|
|
4740
|
+
}
|
|
4741
|
+
/** The container: the profile, the cross-reference chain, the trailers, and object numbers. */
|
|
4742
|
+
function checkContainer(w, unresolved) {
|
|
4743
|
+
const { reader, incomplete } = w;
|
|
4744
|
+
if (reader.trailerDicts().some((t) => t.has("Encrypt"))) incomplete("encrypted");
|
|
4745
|
+
if (reader.headerOffset !== 0 || !/^%PDF-(?:1\.[0-7]|2\.0)(?:\r\n|\n|\r)/.test(Buffer.from(reader.bytes.subarray(0, 16)).toString("latin1"))) incomplete("header-offset");
|
|
4746
|
+
if (!plainEnding(reader.bytes, reader.chainStart())) incomplete("ending");
|
|
4747
|
+
if (unresolved !== 0) incomplete("unresolved");
|
|
4748
|
+
if (reader.repaired) incomplete("repaired");
|
|
4749
|
+
if (reader.xrefStreamSizes) incomplete("xref-stream");
|
|
4750
|
+
if (reader.trailerSyntax) incomplete("trailer-syntax");
|
|
4751
|
+
if (reader.xrefTableForm) incomplete("xref-table");
|
|
4752
|
+
if (reader.xrefEntryType) incomplete("xref-stream");
|
|
4753
|
+
if (reader.trailerSize) incomplete("size");
|
|
4754
|
+
if (reader.objectStreamForm) incomplete("object-stream");
|
|
4755
|
+
if (reader.largestDecoded > PYPDF_FILE) incomplete("pypdf-size");
|
|
4756
|
+
if (!reader.objectStreamsInForce()) incomplete("object-stream-in-force");
|
|
4757
|
+
if (reader.chainOffsets) incomplete("xref-offset");
|
|
4758
|
+
for (const t of reader.trailerDicts()) {
|
|
4759
|
+
if (!t.stream) {
|
|
4760
|
+
const stack = [t];
|
|
4761
|
+
while (stack.length > 0) {
|
|
4762
|
+
const v = stack.pop();
|
|
4763
|
+
const items = Array.isArray(v) ? v : v instanceof PdfDict ? v.entries.map(([, x]) => x) : [];
|
|
4764
|
+
if (items.length > PYPDF_ENTRIES) incomplete("pypdf-entries");
|
|
4765
|
+
for (const x of items) if (x !== null && typeof x === "object") stack.push(x);
|
|
4766
|
+
}
|
|
4767
|
+
continue;
|
|
4768
|
+
}
|
|
4769
|
+
if (EXTERNAL_DATA.some((k) => t.has(k)) || t.all("Type").length !== 1 || !isName(t.get("Type"), "XRef")) incomplete("xref-stream");
|
|
4770
|
+
}
|
|
4771
|
+
const newestSize = newestSizeOf(reader);
|
|
4772
|
+
if (!reader.entriesWithin(Math.max(0, newestSize))) incomplete("size");
|
|
4773
|
+
if (newestSize - 1 >= Math.min(Math.floor(reader.bytes.length / 3), 8388607)) incomplete("object-number");
|
|
4774
|
+
if (!reader.entriesExact()) incomplete("xref-offset");
|
|
4775
|
+
if (!reader.xrefStreamsListed()) incomplete("xref-stream");
|
|
4776
|
+
const chain = reader.chainTexts();
|
|
4777
|
+
if (chain.trailers.some((at) => !canonicalText(reader.bytes, at, reader.bytes.length, true)) || chain.streams.some((d) => !canonicalDefinition(reader, d))) incomplete("form");
|
|
4778
|
+
}
|
|
4779
|
+
/** The catalog the trailers name, and the pages as pdf.js reads them when it opens the file. Returns the catalog's holder. */
|
|
4780
|
+
function* checkCatalog(w, catalog) {
|
|
4781
|
+
const { reader, incomplete, tick, follow, once } = w;
|
|
4782
|
+
const root = follow(reader.trailer.Root, void 0);
|
|
4783
|
+
const rootNum = reader.trailer.Root instanceof PdfRef ? reader.trailer.Root.num : void 0;
|
|
4784
|
+
for (const t of reader.trailerDicts()) {
|
|
4785
|
+
const roots = t.all("Root");
|
|
4786
|
+
if (roots.length > 1 || roots.some((r) => !(r instanceof PdfRef) || r.num !== rootNum)) incomplete("trailer-roots");
|
|
4787
|
+
}
|
|
4788
|
+
if (reader.newestTrailer?.all("Root").length !== 1) incomplete("trailer-roots");
|
|
4789
|
+
const catalogHolder = root.holder;
|
|
4790
|
+
let first;
|
|
4791
|
+
for (const d of reader.defs) if (!d.container && !d.hidden && !d.alternateOf && (!first || d.offset < first.offset)) first = d;
|
|
4792
|
+
if (!(yield* pagesOpen(reader, catalog, first?.value instanceof PdfDict ? first.value : void 0, tick))) incomplete("pages-open");
|
|
4793
|
+
if (!isName(follow(once(catalog, "Type"), catalogHolder).value, "Catalog")) incomplete("catalog-type");
|
|
4794
|
+
return catalogHolder;
|
|
4795
|
+
}
|
|
4796
|
+
/** The /EmbeddedFiles name tree: each file specification it lists, with the keys it is listed under. */
|
|
4797
|
+
function* readNameTree(w, catalog, catalogHolder) {
|
|
4798
|
+
const { maxDepth, incomplete, tick, follow, once, text, cmp } = w;
|
|
4799
|
+
const listed = /* @__PURE__ */ new Map();
|
|
4800
|
+
let entries = 0;
|
|
4801
|
+
const names = follow(once(catalog, "Names"), catalogHolder);
|
|
4802
|
+
if (names.value instanceof PdfDict && names.value.stream) incomplete("names-not-dict");
|
|
4803
|
+
if (names.value instanceof PdfDict) {
|
|
4804
|
+
const tree = follow(once(names.value, "EmbeddedFiles"), names.holder);
|
|
4805
|
+
if (tree.value !== null) {
|
|
4806
|
+
const stack = [{
|
|
4807
|
+
node: tree,
|
|
4808
|
+
depth: 0
|
|
4809
|
+
}];
|
|
4810
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4811
|
+
/** The /Kids and /Names arrays walked: one met again (shared by two nodes) is not walked again. */
|
|
4812
|
+
const arrays = /* @__PURE__ */ new Set();
|
|
4813
|
+
const fresh = (a) => {
|
|
4814
|
+
if (arrays.has(a)) {
|
|
4815
|
+
incomplete("tree-node");
|
|
4816
|
+
return false;
|
|
4817
|
+
}
|
|
4818
|
+
arrays.add(a);
|
|
4819
|
+
return true;
|
|
4820
|
+
};
|
|
4821
|
+
const keys = [];
|
|
4822
|
+
/** High limits of nodes done, each checked against the next key. */
|
|
4823
|
+
const pendingHigh = [];
|
|
4824
|
+
while (stack.length > 0) {
|
|
4825
|
+
if (tick()) yield;
|
|
4826
|
+
const frame = stack.pop();
|
|
4827
|
+
if ("limits" in frame) {
|
|
4828
|
+
const [lo, hi] = frame.limits;
|
|
4829
|
+
if (frame.from < keys.length && (cmp(lo, keys[frame.from]) > 0 || cmp(keys[keys.length - 1], hi) > 0)) incomplete("tree-limits");
|
|
4830
|
+
if (frame.from > 0 && cmp(keys[frame.from - 1], lo) >= 0) incomplete("tree-limits");
|
|
4831
|
+
pendingHigh.push(hi);
|
|
4832
|
+
continue;
|
|
4833
|
+
}
|
|
4834
|
+
const { node, depth } = frame;
|
|
4835
|
+
if (node.value instanceof PdfDict && seen.has(node.value)) {
|
|
4836
|
+
incomplete("tree-node");
|
|
4837
|
+
continue;
|
|
4838
|
+
}
|
|
4839
|
+
if (!(node.value instanceof PdfDict) || node.value.stream) {
|
|
4840
|
+
incomplete("tree-node");
|
|
4841
|
+
continue;
|
|
4842
|
+
}
|
|
4843
|
+
if (depth > maxDepth) continue;
|
|
4844
|
+
seen.add(node.value);
|
|
4845
|
+
const limits = once(node.value, "Limits");
|
|
4846
|
+
if (limits !== void 0) {
|
|
4847
|
+
const l = follow(limits, node.holder);
|
|
4848
|
+
const limit = (v) => {
|
|
4849
|
+
const t = text(follow(v, l.holder));
|
|
4850
|
+
return t !== null && !t.long && t.plain && !t.irregular ? t.key : null;
|
|
4851
|
+
};
|
|
4852
|
+
const pair = Array.isArray(l.value) && l.value.length === 2 ? l.value : null;
|
|
4853
|
+
const lo = pair === null ? null : limit(pair[0]);
|
|
4854
|
+
const hi = pair === null ? null : limit(pair[1]);
|
|
4855
|
+
if (lo === null || hi === null || cmp(lo, hi) > 0) incomplete("tree-limits");
|
|
4856
|
+
else stack.push({
|
|
4857
|
+
limits: [lo, hi],
|
|
4858
|
+
from: keys.length
|
|
4859
|
+
});
|
|
4860
|
+
} else if (depth > 0) incomplete("tree-limits");
|
|
4861
|
+
const kids = once(node.value, "Kids");
|
|
4862
|
+
const items = once(node.value, "Names");
|
|
4863
|
+
if (kids !== void 0 && items !== void 0) incomplete("kids-and-names");
|
|
4864
|
+
const filled = (x) => {
|
|
4865
|
+
const v = follow(x, node.holder).value;
|
|
4866
|
+
return Array.isArray(v) && v.length > 0;
|
|
4867
|
+
};
|
|
4868
|
+
if (!filled(kids) && !filled(items)) incomplete("name-tree-empty");
|
|
4869
|
+
if (kids !== void 0) {
|
|
4870
|
+
const list = follow(kids, node.holder);
|
|
4871
|
+
if (Array.isArray(list.value) && fresh(list.value)) {
|
|
4872
|
+
if (list.value.some((k) => !(k instanceof PdfRef))) incomplete("tree-node");
|
|
4873
|
+
for (let i = list.value.length - 1; i >= 0; i -= 1) stack.push({
|
|
4874
|
+
node: follow(list.value[i], list.holder),
|
|
4875
|
+
depth: depth + 1
|
|
4876
|
+
});
|
|
4877
|
+
}
|
|
4878
|
+
}
|
|
4879
|
+
if (items !== void 0) {
|
|
4880
|
+
const list = follow(items, node.holder);
|
|
4881
|
+
if (!Array.isArray(list.value) || list.value.length % 2 !== 0) {
|
|
4882
|
+
incomplete("names-not-pairs");
|
|
4883
|
+
continue;
|
|
4884
|
+
}
|
|
4885
|
+
if (!fresh(list.value)) continue;
|
|
4886
|
+
for (let i = 0; i + 1 < list.value.length; i += 2) {
|
|
4887
|
+
if (tick()) yield;
|
|
4888
|
+
const spec = follow(list.value[i + 1], list.holder).value;
|
|
4889
|
+
const t = list.value[i] instanceof PdfString ? text(follow(list.value[i], list.holder)) : null;
|
|
4890
|
+
if (t === null || !(spec instanceof PdfDict)) {
|
|
4891
|
+
incomplete("tree-entry");
|
|
4892
|
+
continue;
|
|
4893
|
+
}
|
|
4894
|
+
entries += 1;
|
|
4895
|
+
if (entries > MAX_ENTRIES) incomplete("tree-count");
|
|
4896
|
+
const listings = listed.get(spec) ?? [];
|
|
4897
|
+
listed.set(spec, listings);
|
|
4898
|
+
if (t.long) {
|
|
4899
|
+
incomplete("key-encoding");
|
|
4900
|
+
listings.push("");
|
|
4901
|
+
continue;
|
|
4902
|
+
}
|
|
4903
|
+
const key = t.text;
|
|
4904
|
+
if (!t.plain || t.irregular) incomplete("key-encoding");
|
|
4905
|
+
const k = t.key;
|
|
4906
|
+
if (keys.length > 0 && compareBytes(keys[keys.length - 1].bytes, k.bytes) >= 0) incomplete("tree-order");
|
|
4907
|
+
if (keys.length > 0 && Buffer.compare(keys[keys.length - 1].text, k.text) >= 0) incomplete("tree-order");
|
|
4908
|
+
for (const hi of pendingHigh) if (cmp(hi, k) >= 0) incomplete("tree-limits");
|
|
4909
|
+
pendingHigh.length = 0;
|
|
4910
|
+
keys.push(k);
|
|
4911
|
+
listings.push(key);
|
|
4912
|
+
}
|
|
4913
|
+
}
|
|
4914
|
+
}
|
|
4915
|
+
}
|
|
4916
|
+
} else if (names.value !== null) incomplete("names-not-dict");
|
|
4917
|
+
return listed;
|
|
4918
|
+
}
|
|
4919
|
+
/** The catalog's /AF: each file specification it lists, with how many times. */
|
|
4920
|
+
function* readAssociated(w, catalog, catalogHolder) {
|
|
4921
|
+
const { incomplete, tick, follow, once } = w;
|
|
4922
|
+
const associated = /* @__PURE__ */ new Map();
|
|
4923
|
+
const af = follow(once(catalog, "AF"), catalogHolder);
|
|
4924
|
+
if (Array.isArray(af.value)) for (const item of af.value) {
|
|
4925
|
+
if (tick()) yield;
|
|
4926
|
+
const spec = follow(item, af.holder).value;
|
|
4927
|
+
if (spec instanceof PdfDict) associated.set(spec, (associated.get(spec) ?? 0) + 1);
|
|
4928
|
+
else incomplete("af-item");
|
|
4929
|
+
}
|
|
4930
|
+
else if (af.value !== null) incomplete("af-not-array");
|
|
4931
|
+
return associated;
|
|
4932
|
+
}
|
|
4933
|
+
/**
|
|
4934
|
+
* Whether the /Length of stream definition `d` is written as a whole number in digits, directly or in the
|
|
4935
|
+
* object a reference leads to: qpdf ignores another (13.0, +13) and looks for `endstream`.
|
|
4936
|
+
*/
|
|
4937
|
+
function integerLength(reader, d) {
|
|
4938
|
+
const dict = d.value;
|
|
4939
|
+
if (!(dict instanceof PdfDict) || !dict.stream || d.container) return true;
|
|
4940
|
+
const v = dict.get("Length");
|
|
4941
|
+
const text = (from, to) => Buffer.from(reader.bytes.subarray(from, to)).toString("latin1");
|
|
4942
|
+
if (v instanceof PdfRef) {
|
|
4943
|
+
const target = reader.inForce(v.num);
|
|
4944
|
+
if (!target) return false;
|
|
4945
|
+
if (target.container) return target.digits === true;
|
|
4946
|
+
const body = /^\d+[ \t\r\n]+\d+[ \t\r\n]+obj([\s\S]*?)endobj/.exec(text(target.offset, target.offset + 64));
|
|
4947
|
+
return body !== null && /^\s*\d+\s*$/.test(body[1]);
|
|
4948
|
+
}
|
|
4949
|
+
return dict.has("Length") && plainIntegers(dict, reader.bytes.subarray(d.offset, dict.stream.start), ["Length"]);
|
|
4950
|
+
}
|
|
4951
|
+
/**
|
|
4952
|
+
* What the walk over every value of every definition finds for the description (detection walks them all once, and
|
|
4953
|
+
* hands each value here): in the definitions in force, every dictionary with /EF (a file specification) and every
|
|
4954
|
+
* file attachment annotation; and the rules a value breaks.
|
|
4955
|
+
*/
|
|
4956
|
+
var Values = class {
|
|
4957
|
+
specs = [];
|
|
4958
|
+
specSeen = /* @__PURE__ */ new Set();
|
|
4959
|
+
/** The /FS of every file attachment annotation in the definitions in force, with its holder. */
|
|
4960
|
+
attached = [];
|
|
4961
|
+
/** The rules broken by a value. */
|
|
4962
|
+
broken = /* @__PURE__ */ new Set();
|
|
4963
|
+
reader;
|
|
4964
|
+
def;
|
|
4965
|
+
inForce = false;
|
|
4966
|
+
/** References are compared with the generation of their objects only when one of either is not 0. */
|
|
4967
|
+
generations;
|
|
4968
|
+
constructor(reader) {
|
|
4969
|
+
this.reader = reader;
|
|
4970
|
+
this.generations = reader.referencesWithGenerations() || reader.defs.some((d) => d.gen !== 0);
|
|
4971
|
+
}
|
|
4972
|
+
/** One value of definition `def`, in the order detection walks them. */
|
|
4973
|
+
visit(v, def) {
|
|
4974
|
+
if (def.alternateOf) return;
|
|
4975
|
+
const { reader } = this;
|
|
4976
|
+
if (def !== this.def) {
|
|
4977
|
+
this.def = def;
|
|
4978
|
+
this.inForce = reader.inForce(def.num) === def;
|
|
4979
|
+
}
|
|
4980
|
+
const inForce = this.inForce;
|
|
4981
|
+
if (v instanceof PdfRef) {
|
|
4982
|
+
if (inForce && this.generations) {
|
|
4983
|
+
const target = reader.inForce(v.num);
|
|
4984
|
+
if (target && target.gen !== v.gen) this.broken.add("reference-generation");
|
|
4985
|
+
}
|
|
4986
|
+
} else if (Array.isArray(v)) {
|
|
4987
|
+
if (v.length > PYPDF_ENTRIES) this.broken.add("pypdf-entries");
|
|
4988
|
+
} else if (v instanceof PdfDict) {
|
|
4989
|
+
if (v.entries.length > PYPDF_ENTRIES) this.broken.add("pypdf-entries");
|
|
4990
|
+
let subtype;
|
|
4991
|
+
let ef = false;
|
|
4992
|
+
for (const [k, x] of v.entries) if (k === "Subtype") subtype = x;
|
|
4993
|
+
else if (k === "EF") ef = true;
|
|
4994
|
+
if (inForce && subtype !== void 0 && isName(peek(reader, subtype), "FileAttachment")) this.attached.push({
|
|
4995
|
+
value: v.get("FS") ?? null,
|
|
4996
|
+
holder: def
|
|
3052
4997
|
});
|
|
3053
|
-
if (
|
|
3054
|
-
this.
|
|
3055
|
-
|
|
4998
|
+
if (ef) {
|
|
4999
|
+
if (!inForce || this.specSeen.has(v)) this.broken.add("ef-elsewhere");
|
|
5000
|
+
else {
|
|
5001
|
+
this.specSeen.add(v);
|
|
5002
|
+
this.specs.push({
|
|
5003
|
+
dict: v,
|
|
5004
|
+
holder: def
|
|
5005
|
+
});
|
|
5006
|
+
}
|
|
3056
5007
|
}
|
|
3057
5008
|
}
|
|
3058
5009
|
}
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
5010
|
+
};
|
|
5011
|
+
/** Every definition in the file, read or not: the one form for each. */
|
|
5012
|
+
function* scanDefinitions(w, values) {
|
|
5013
|
+
const { reader, incomplete, tick } = w;
|
|
5014
|
+
const newestSize = newestSizeOf(reader);
|
|
5015
|
+
const { specs, specSeen, attached } = values;
|
|
5016
|
+
for (const rule of values.broken) incomplete(rule);
|
|
5017
|
+
for (const def of reader.defs) {
|
|
5018
|
+
if (tick()) yield;
|
|
5019
|
+
if (def.syntax || def.irregular) incomplete("syntax");
|
|
5020
|
+
if (def.alternateOf) continue;
|
|
5021
|
+
if (def.hidden) incomplete("hidden");
|
|
5022
|
+
if (def.num >= newestSize) incomplete("size");
|
|
5023
|
+
if (def.gen >= 65535) incomplete("generation");
|
|
5024
|
+
if (!integerLength(reader, def)) incomplete("extent");
|
|
5025
|
+
if (def.value instanceof PdfDict && def.value.stream && Math.max(def.value.stream.data.length, def.value.stream.declaredLength ?? 0) > PYPDF_FILE) incomplete("pypdf-size");
|
|
5026
|
+
if (!def.container && !plainHeader(reader.bytes, def.offset, def.num, def.gen)) incomplete("header");
|
|
5027
|
+
if (!canonicalDefinition(reader, def)) incomplete("form");
|
|
3066
5028
|
}
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
5029
|
+
return {
|
|
5030
|
+
specs,
|
|
5031
|
+
specSeen,
|
|
5032
|
+
attached
|
|
5033
|
+
};
|
|
5034
|
+
}
|
|
5035
|
+
/** Each file specification, and each stream its /EF gives: the files described. */
|
|
5036
|
+
function* describeSpecs(w, specs, listed, associated) {
|
|
5037
|
+
const { reader, work, incomplete, used, follow, once, has, text } = w;
|
|
5038
|
+
const entries = [];
|
|
5039
|
+
const owners = /* @__PURE__ */ new Map();
|
|
5040
|
+
let decoding = true;
|
|
5041
|
+
/**
|
|
5042
|
+
* The size of a stream's data once decoded, and the XML check of it. In a file that may be complete, Flate
|
|
5043
|
+
* data is read as one whole zlib stream; data readers read differently is measured as this library reads it.
|
|
5044
|
+
* A decoder that fails has done work the budget does not count: after one, nothing more is decoded, so many
|
|
5045
|
+
* such files cannot add up.
|
|
5046
|
+
*/
|
|
5047
|
+
const measure = function* (def, s, read, flate, mediaType) {
|
|
5048
|
+
const result = {
|
|
5049
|
+
size: null,
|
|
5050
|
+
xml: null,
|
|
5051
|
+
flateDiffers: false
|
|
5052
|
+
};
|
|
5053
|
+
if (!decoding) return result;
|
|
5054
|
+
try {
|
|
5055
|
+
let decoded;
|
|
5056
|
+
if (flate && !reader.security) try {
|
|
5057
|
+
decoded = inflateWhole(read, work);
|
|
5058
|
+
} catch (error) {
|
|
5059
|
+
if (!(error instanceof PdfToolsError) || error.code !== "DAMAGED") throw error;
|
|
5060
|
+
result.flateDiffers = true;
|
|
3078
5061
|
}
|
|
3079
|
-
|
|
5062
|
+
decoded ??= reader.streamData(def, s, read);
|
|
5063
|
+
result.size = decoded.length;
|
|
5064
|
+
if (isXmlType(mediaType)) result.xml = yield* checkXml(decoded, () => work.checkpoint());
|
|
5065
|
+
} catch (error) {
|
|
5066
|
+
const budget = error instanceof PdfToolsError && error.code === "LIMIT_EXCEEDED" && (error.details.limit === "maxStreamBytes" || error.details.limit === "maxInflateBytes");
|
|
5067
|
+
if (!(error instanceof PdfToolsError) || error.code !== "DAMAGED" && !budget) throw error;
|
|
5068
|
+
decoding = false;
|
|
5069
|
+
}
|
|
5070
|
+
return result;
|
|
5071
|
+
};
|
|
5072
|
+
const described = /* @__PURE__ */ new Map();
|
|
5073
|
+
/** The /Params dictionaries whose dates were read. */
|
|
5074
|
+
const paramsRead = /* @__PURE__ */ new Set();
|
|
5075
|
+
/** The bytes of every stream described, decoded. */
|
|
5076
|
+
let total = 0;
|
|
5077
|
+
const describe = function* (def, s) {
|
|
5078
|
+
if (EXTERNAL_DATA.some((k) => has(s, k))) incomplete("external-data");
|
|
5079
|
+
if (has(s, "DecodeParms") && has(s, "DP")) incomplete("dp-and-decodeparms");
|
|
5080
|
+
const sType = once(s, "Type");
|
|
5081
|
+
if (sType !== void 0) {
|
|
5082
|
+
const t = follow(sType, def).value;
|
|
5083
|
+
if (!(t instanceof PdfName) || t.name !== "EmbeddedFile") incomplete("stream-type");
|
|
5084
|
+
}
|
|
5085
|
+
const params = follow(once(s, "Params"), def);
|
|
5086
|
+
if (params.value instanceof PdfDict && !paramsRead.has(params.value)) {
|
|
5087
|
+
paramsRead.add(params.value);
|
|
5088
|
+
for (const key of ["CreationDate", "ModDate"]) {
|
|
5089
|
+
const date = once(params.value, key);
|
|
5090
|
+
if (date === void 0) continue;
|
|
5091
|
+
const t = text(follow(date, params.holder));
|
|
5092
|
+
if (t === null || t.long || !plainDate(t.text)) incomplete("params-date");
|
|
5093
|
+
}
|
|
5094
|
+
}
|
|
5095
|
+
let mediaType = null;
|
|
5096
|
+
const sub = once(s, "Subtype");
|
|
5097
|
+
if (sub !== void 0) {
|
|
5098
|
+
const m = follow(sub, def).value;
|
|
5099
|
+
if (m instanceof PdfName) mediaType = m.name;
|
|
5100
|
+
else if (m !== null) incomplete("subtype-not-name");
|
|
5101
|
+
}
|
|
5102
|
+
const length = follow(once(s, "Length"), def).value;
|
|
5103
|
+
const data = s.stream.data;
|
|
5104
|
+
const extent = typeof length === "number" && Number.isSafeInteger(length) && length >= 0 && length < data.length;
|
|
5105
|
+
const filter = once(s, "Filter");
|
|
5106
|
+
const flate = isName(filter, "FlateDecode") || Array.isArray(filter) && filter.length === 1 && isName(filter[0], "FlateDecode");
|
|
5107
|
+
if (filter !== void 0 && !flate) incomplete("filter");
|
|
5108
|
+
for (const key of ["DecodeParms", "DP"]) {
|
|
5109
|
+
const parms = once(s, key);
|
|
5110
|
+
if (parms !== void 0 && parms !== null && !(Array.isArray(parms) && parms.every((x) => x === null))) incomplete("decode-parms");
|
|
5111
|
+
}
|
|
5112
|
+
const measured = yield* measure(def, s, extent ? data.subarray(0, length) : data, flate, mediaType);
|
|
5113
|
+
if (measured.flateDiffers) incomplete("flate");
|
|
5114
|
+
if (measured.size === null) incomplete("not-decoded");
|
|
5115
|
+
else if (measured.size > PYPDF_FILE) incomplete("pypdf-size");
|
|
5116
|
+
else total += measured.size;
|
|
5117
|
+
if (total > PYPDF_FILES) incomplete("pypdf-size");
|
|
5118
|
+
return {
|
|
5119
|
+
...measured,
|
|
5120
|
+
mediaType
|
|
5121
|
+
};
|
|
5122
|
+
};
|
|
5123
|
+
/** The /EF dictionaries read: whether each has keys other than /F and /UF. */
|
|
5124
|
+
const efRead = /* @__PURE__ */ new Map();
|
|
5125
|
+
let room = Math.max(reader.bytes.length, MIN_REPORT_TEXT);
|
|
5126
|
+
for (const spec of specs) {
|
|
5127
|
+
yield;
|
|
5128
|
+
const d = spec.dict;
|
|
5129
|
+
if (!used.has(spec.holder.num)) used.set(spec.holder.num, /* @__PURE__ */ new Set([spec.holder.gen]));
|
|
5130
|
+
if (SPEC_OTHER.some((k) => has(d, k))) incomplete("spec-other-keys");
|
|
5131
|
+
if (d.entries.length > MAX_SPEC_ENTRIES) incomplete("spec-size");
|
|
5132
|
+
if (d.stream) incomplete("spec-stream");
|
|
5133
|
+
const type = once(d, "Type");
|
|
5134
|
+
const t = type === void 0 ? void 0 : follow(type, spec.holder).value;
|
|
5135
|
+
if (!(t instanceof PdfName) || t.name !== "Filespec") incomplete("spec-type");
|
|
5136
|
+
const desc = once(d, "Desc");
|
|
5137
|
+
if (desc !== void 0 && !(follow(desc, spec.holder).value instanceof PdfString)) incomplete("spec-desc");
|
|
5138
|
+
const read = (key) => {
|
|
5139
|
+
const raw = once(d, key);
|
|
5140
|
+
if (raw === void 0) return null;
|
|
5141
|
+
const found = follow(raw, spec.holder);
|
|
5142
|
+
const name = text(found);
|
|
5143
|
+
if (name === null) {
|
|
5144
|
+
incomplete(found.value === null ? "empty-name" : "name-not-string");
|
|
5145
|
+
return null;
|
|
5146
|
+
}
|
|
5147
|
+
if (name.long) {
|
|
5148
|
+
incomplete("name-encoding");
|
|
5149
|
+
return null;
|
|
5150
|
+
}
|
|
5151
|
+
if (!name.plain || name.irregular) incomplete("name-encoding");
|
|
5152
|
+
if (name.text === "") incomplete("empty-name");
|
|
5153
|
+
return name.text === "" ? null : name.text;
|
|
5154
|
+
};
|
|
5155
|
+
const uf = read("UF");
|
|
5156
|
+
const f = read("F");
|
|
5157
|
+
if (uf !== null && f !== null && uf !== f) incomplete("f-uf-names");
|
|
5158
|
+
const name = uf ?? f;
|
|
5159
|
+
if (name === null) incomplete("no-name");
|
|
5160
|
+
let relationship = null;
|
|
5161
|
+
const rel = once(d, "AFRelationship");
|
|
5162
|
+
if (rel !== void 0) {
|
|
5163
|
+
const r = follow(rel, spec.holder).value;
|
|
5164
|
+
if (r instanceof PdfName) relationship = r.name;
|
|
5165
|
+
else if (r !== null) incomplete("rel-not-name");
|
|
5166
|
+
}
|
|
5167
|
+
const keys = listed.get(d);
|
|
5168
|
+
if (keys && (keys.length > 1 || keys[0] !== name)) incomplete("tree-key");
|
|
5169
|
+
const timesAssociated = associated.get(d) ?? 0;
|
|
5170
|
+
const ef = follow(once(d, "EF"), spec.holder);
|
|
5171
|
+
if (!(ef.value instanceof PdfDict)) {
|
|
5172
|
+
incomplete("ef-not-dict");
|
|
5173
|
+
continue;
|
|
5174
|
+
}
|
|
5175
|
+
let otherKeys = efRead.get(ef.value);
|
|
5176
|
+
if (otherKeys === void 0) {
|
|
5177
|
+
otherKeys = ef.value.entries.some(([k]) => k !== "F" && k !== "UF");
|
|
5178
|
+
efRead.set(ef.value, otherKeys);
|
|
5179
|
+
}
|
|
5180
|
+
if (otherKeys) incomplete("ef-other-keys");
|
|
5181
|
+
if (has(ef.value, "UF") && !has(d, "UF")) incomplete("ef-uf");
|
|
5182
|
+
const nums = [];
|
|
5183
|
+
for (const key of ["UF", "F"]) {
|
|
5184
|
+
const v = once(ef.value, key);
|
|
5185
|
+
if (v === void 0) continue;
|
|
5186
|
+
if (v === null) {
|
|
5187
|
+
incomplete("ef-null");
|
|
5188
|
+
continue;
|
|
5189
|
+
}
|
|
5190
|
+
if (!(v instanceof PdfRef)) continue;
|
|
5191
|
+
const target = follow(v, ef.holder);
|
|
5192
|
+
if (!(target.value instanceof PdfDict) || !target.value.stream || !target.holder) continue;
|
|
5193
|
+
if (nums.includes(target.holder.num)) continue;
|
|
5194
|
+
if (nums.length > 0) incomplete("f-uf-streams");
|
|
5195
|
+
nums.push(target.holder.num);
|
|
5196
|
+
const def = target.holder;
|
|
5197
|
+
owners.set(def.num, (owners.get(def.num) ?? 0) + 1);
|
|
5198
|
+
let stream = described.get(def);
|
|
5199
|
+
if (!stream) {
|
|
5200
|
+
stream = yield* describe(def, target.value);
|
|
5201
|
+
described.set(def, stream);
|
|
5202
|
+
}
|
|
5203
|
+
const { size, xml, mediaType } = stream;
|
|
5204
|
+
const file = {
|
|
5205
|
+
name,
|
|
5206
|
+
mediaType,
|
|
5207
|
+
relationship,
|
|
5208
|
+
size,
|
|
5209
|
+
inNameTree: keys !== void 0,
|
|
5210
|
+
inAssociatedFiles: timesAssociated > 0,
|
|
5211
|
+
xml
|
|
5212
|
+
};
|
|
5213
|
+
room -= (name?.length ?? 0) + (mediaType?.length ?? 0) + (relationship?.length ?? 0) + (xml?.root ? xml.root.name.length + (xml.root.namespace?.length ?? 0) : 0);
|
|
5214
|
+
if (room < 0) {
|
|
5215
|
+
incomplete("report-text");
|
|
5216
|
+
file.name = file.mediaType = file.relationship = file.xml = null;
|
|
5217
|
+
}
|
|
5218
|
+
entries.push({
|
|
5219
|
+
spec,
|
|
5220
|
+
num: def.num,
|
|
5221
|
+
file
|
|
5222
|
+
});
|
|
3080
5223
|
}
|
|
3081
|
-
const visible = list.filter((d) => !d.hidden);
|
|
3082
|
-
const pool = visible.length > 0 ? visible : list;
|
|
3083
|
-
return pool.length > 0 ? pool.reduce((a, b) => b.order >= a.order ? b : a) : void 0;
|
|
3084
5224
|
}
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
5225
|
+
return {
|
|
5226
|
+
entries,
|
|
5227
|
+
owners
|
|
5228
|
+
};
|
|
5229
|
+
}
|
|
5230
|
+
/**
|
|
5231
|
+
* What the readers list against what is described: annotations, listings, streams, the files counted, and one
|
|
5232
|
+
* reading of every object read.
|
|
5233
|
+
*/
|
|
5234
|
+
function* checkAgreement(w, r) {
|
|
5235
|
+
const { reader, incomplete, tick, used, follow, has } = w;
|
|
5236
|
+
const { entries, owners, listed, associated, specSeen, attached, counted } = r;
|
|
5237
|
+
for (const fs of attached) {
|
|
5238
|
+
const spec = follow(fs.value, fs.holder).value;
|
|
5239
|
+
if (!(spec instanceof PdfDict) || !listed.has(spec)) incomplete("annotation-file");
|
|
5240
|
+
}
|
|
5241
|
+
const describedSpecs = new Set(entries.map((e) => e.spec.dict));
|
|
5242
|
+
for (const spec of listed.keys()) if (!describedSpecs.has(spec)) incomplete("tree-not-described");
|
|
5243
|
+
for (const spec of associated.keys()) if (!specSeen.has(spec)) incomplete("af-not-described");
|
|
5244
|
+
for (const n of owners.values()) if (n > 1) incomplete("two-specs");
|
|
5245
|
+
const described = new Set(entries.map((e) => `n${e.num}`));
|
|
5246
|
+
if ([...counted].some((k) => !described.has(k))) incomplete("counted");
|
|
5247
|
+
for (const [num, gens] of used) {
|
|
5248
|
+
if (tick()) yield;
|
|
5249
|
+
const primary = reader.definitionsOf(num).filter((x) => !x.alternateOf);
|
|
5250
|
+
const gen = [...gens][0];
|
|
5251
|
+
if (reader.inForce(num)?.gen !== gen || reader.entryGeneration(num) !== gen) incomplete("generation");
|
|
5252
|
+
if (primary.length !== 1) {
|
|
5253
|
+
incomplete("one-definition");
|
|
5254
|
+
continue;
|
|
5255
|
+
}
|
|
5256
|
+
if (!canonicalDefinition(reader, primary[0])) incomplete("form");
|
|
5257
|
+
const container = primary[0].container?.value;
|
|
5258
|
+
if (container instanceof PdfDict && EXTERNAL_DATA.some((k) => has(container, k))) incomplete("object-stream");
|
|
3088
5259
|
}
|
|
3089
|
-
|
|
3090
|
-
|
|
5260
|
+
}
|
|
5261
|
+
/**
|
|
5262
|
+
* Describes the embedded files. Runs after the inspection is made: it reads `reader.repaired` as the
|
|
5263
|
+
* inspection reports it. `counted` holds the keys of the files detection counted (see countActive),
|
|
5264
|
+
* `unresolved` its unresolved count. Decoding counts against `maxStreamBytes` and `maxInflateBytes`; a file
|
|
5265
|
+
* that cannot be decoded within them, or is damaged, has the size null, and no further file is decoded.
|
|
5266
|
+
*/
|
|
5267
|
+
function* describeEmbeddedFiles(reader, catalog, counted, unresolved, values) {
|
|
5268
|
+
const { work } = reader;
|
|
5269
|
+
const maxDepth = work.limits.maxDepth;
|
|
5270
|
+
let complete = true;
|
|
5271
|
+
const incomplete = (_rule) => {
|
|
5272
|
+
complete = false;
|
|
5273
|
+
};
|
|
5274
|
+
let nodes = 0;
|
|
5275
|
+
const tick = () => {
|
|
5276
|
+
nodes += 1;
|
|
5277
|
+
if ((nodes & 1023) === 0) work.checkpoint();
|
|
5278
|
+
return nodes % STEP === 0;
|
|
5279
|
+
};
|
|
5280
|
+
/** Object numbers read, with the generation each was referred to by: each must have one reading only. */
|
|
5281
|
+
const used = /* @__PURE__ */ new Map();
|
|
5282
|
+
const follow = (v, holder) => {
|
|
3091
5283
|
let value = v ?? null;
|
|
5284
|
+
let h = holder;
|
|
3092
5285
|
for (let hops = 0; value instanceof PdfRef; hops += 1) {
|
|
3093
|
-
|
|
5286
|
+
let gens = used.get(value.num);
|
|
5287
|
+
if (!gens) used.set(value.num, gens = /* @__PURE__ */ new Set());
|
|
5288
|
+
gens.add(value.gen);
|
|
5289
|
+
const d = hops < maxDepth ? reader.inForce(value.num) : void 0;
|
|
5290
|
+
if (!d) return {
|
|
3094
5291
|
value: null,
|
|
3095
|
-
|
|
5292
|
+
holder: void 0
|
|
3096
5293
|
};
|
|
3097
|
-
|
|
3098
|
-
value =
|
|
5294
|
+
h = d;
|
|
5295
|
+
value = d.value;
|
|
3099
5296
|
}
|
|
3100
5297
|
return {
|
|
3101
5298
|
value,
|
|
3102
|
-
|
|
5299
|
+
holder: h
|
|
5300
|
+
};
|
|
5301
|
+
};
|
|
5302
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
5303
|
+
const indexOf = (d) => {
|
|
5304
|
+
let index = indexes.get(d);
|
|
5305
|
+
if (!index) {
|
|
5306
|
+
index = /* @__PURE__ */ new Map();
|
|
5307
|
+
for (const [k, v] of d.entries) index.set(k, v);
|
|
5308
|
+
indexes.set(d, index);
|
|
5309
|
+
}
|
|
5310
|
+
return index;
|
|
5311
|
+
};
|
|
5312
|
+
/** The value of `key`, which must not be repeated (readers keep the first or the last). */
|
|
5313
|
+
const once = (d, key) => indexOf(d).get(key);
|
|
5314
|
+
const has = (d, key) => indexOf(d).has(key);
|
|
5315
|
+
const texts = /* @__PURE__ */ new Map();
|
|
5316
|
+
const text = (f) => {
|
|
5317
|
+
if (!(f.value instanceof PdfString)) return null;
|
|
5318
|
+
let t = texts.get(f.value);
|
|
5319
|
+
if (!t || t.holder !== f.holder) {
|
|
5320
|
+
const s = f.value;
|
|
5321
|
+
const holder = f.holder;
|
|
5322
|
+
t = new StringText(holder, () => stringBytes(reader, s, holder), s.written > MAX_TEXT, s.written <= MAX_TEXT && s.irregular());
|
|
5323
|
+
texts.set(f.value, t);
|
|
5324
|
+
}
|
|
5325
|
+
return t;
|
|
5326
|
+
};
|
|
5327
|
+
const orders = /* @__PURE__ */ new Map();
|
|
5328
|
+
const cmp = (a, b) => {
|
|
5329
|
+
let known = orders.get(a);
|
|
5330
|
+
if (!known) orders.set(a, known = /* @__PURE__ */ new Map());
|
|
5331
|
+
let order = known.get(b);
|
|
5332
|
+
if (order === void 0) {
|
|
5333
|
+
order = Math.max(compareBytes(a.bytes, b.bytes), Buffer.compare(a.text, b.text));
|
|
5334
|
+
known.set(b, order);
|
|
5335
|
+
}
|
|
5336
|
+
return order;
|
|
5337
|
+
};
|
|
5338
|
+
const w = {
|
|
5339
|
+
reader,
|
|
5340
|
+
work,
|
|
5341
|
+
maxDepth,
|
|
5342
|
+
incomplete,
|
|
5343
|
+
tick,
|
|
5344
|
+
used,
|
|
5345
|
+
follow,
|
|
5346
|
+
once,
|
|
5347
|
+
has,
|
|
5348
|
+
text,
|
|
5349
|
+
cmp
|
|
5350
|
+
};
|
|
5351
|
+
checkContainer(w, unresolved);
|
|
5352
|
+
const catalogHolder = yield* checkCatalog(w, catalog);
|
|
5353
|
+
const listed = yield* readNameTree(w, catalog, catalogHolder);
|
|
5354
|
+
const associated = yield* readAssociated(w, catalog, catalogHolder);
|
|
5355
|
+
const { specs, specSeen, attached } = yield* scanDefinitions(w, values);
|
|
5356
|
+
const { entries, owners } = yield* describeSpecs(w, specs, listed, associated);
|
|
5357
|
+
yield* checkAgreement(w, {
|
|
5358
|
+
entries,
|
|
5359
|
+
owners,
|
|
5360
|
+
listed,
|
|
5361
|
+
associated,
|
|
5362
|
+
specSeen,
|
|
5363
|
+
attached,
|
|
5364
|
+
counted
|
|
5365
|
+
});
|
|
5366
|
+
if (reader.repairNoted()) incomplete("repaired");
|
|
5367
|
+
entries.sort((a, b) => a.num - b.num);
|
|
5368
|
+
return {
|
|
5369
|
+
files: entries.map((e) => e.file),
|
|
5370
|
+
complete
|
|
5371
|
+
};
|
|
5372
|
+
}
|
|
5373
|
+
// src/xmp.ts
|
|
5374
|
+
/** The namespaces of the invoice properties, compared exactly: Factur-X 1.0 and ZUGFeRD 2.1 and later share one. */
|
|
5375
|
+
const SCHEMAS = /* @__PURE__ */ new Map([
|
|
5376
|
+
["urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#", "factur-x"],
|
|
5377
|
+
["urn:zugferd:pdfa:CrossIndustryDocument:invoice:2p0#", "zugferd-2.0"],
|
|
5378
|
+
["urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#", "zugferd-1.0"]
|
|
5379
|
+
]);
|
|
5380
|
+
const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
5381
|
+
/** A language tag of the plain form: letters, then subtags of letters and digits, each of 1 to 8. */
|
|
5382
|
+
const LANGUAGE = /^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$/;
|
|
5383
|
+
/** A character outside printable ASCII, or one RFC 3986 allows nowhere in an address. One class: no backtracking. */
|
|
5384
|
+
/** Patterns tested through the Scan, each on a value already taken and charged. */
|
|
5385
|
+
const BLANK = /^[ \t\r\n]*$/;
|
|
5386
|
+
const SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
5387
|
+
const RAW_SPACE = /[\t\n\r]/;
|
|
5388
|
+
const NOT_ADDRESS = /[^\x21-\x7e]|[<>"{}|\\^`]/;
|
|
5389
|
+
const XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace";
|
|
5390
|
+
const PROPERTIES = [
|
|
5391
|
+
"DocumentType",
|
|
5392
|
+
"DocumentFileName",
|
|
5393
|
+
"Version",
|
|
5394
|
+
"ConformanceLevel"
|
|
5395
|
+
];
|
|
5396
|
+
/**
|
|
5397
|
+
* The longest value, as written, the reading decodes (a namespace name, rdf:about, rdf:resource, rdf:datatype, a
|
|
5398
|
+
* property of the declaration): a longer one gives no declaration, decided before it is decoded.
|
|
5399
|
+
*/
|
|
5400
|
+
const MAX_VALUE = 4096;
|
|
5401
|
+
const PROPERTY_NAMES = new Set(PROPERTIES);
|
|
5402
|
+
/** Where comments and processing instructions were (one for a run of them). */
|
|
5403
|
+
const MARK = {
|
|
5404
|
+
name: "!",
|
|
5405
|
+
attributes: [],
|
|
5406
|
+
selfClosing: true,
|
|
5407
|
+
end: 0
|
|
5408
|
+
};
|
|
5409
|
+
/** The start tag at `at` (just after `<`), or null when it does not read as one. */
|
|
5410
|
+
function startTag(scan, at) {
|
|
5411
|
+
const n = scan.text.length;
|
|
5412
|
+
const space = (c) => c === 32 || c === 9 || c === 10 || c === 13;
|
|
5413
|
+
const nameEnd = (from, stop) => scan.run(from, n, (c) => !space(c) && !stop(c));
|
|
5414
|
+
let i = nameEnd(at, (c) => c === 62 || c === 47);
|
|
5415
|
+
const name = scan.take(at, i);
|
|
5416
|
+
if (name === "") return null;
|
|
5417
|
+
const attributes = [];
|
|
5418
|
+
for (;;) {
|
|
5419
|
+
i = scan.run(i, n, space);
|
|
5420
|
+
const c = scan.code(i);
|
|
5421
|
+
if (c === 62) return {
|
|
5422
|
+
name,
|
|
5423
|
+
attributes,
|
|
5424
|
+
selfClosing: false,
|
|
5425
|
+
end: i + 1
|
|
5426
|
+
};
|
|
5427
|
+
if (c === 47 && scan.code(i + 1) === 62) return {
|
|
5428
|
+
name,
|
|
5429
|
+
attributes,
|
|
5430
|
+
selfClosing: true,
|
|
5431
|
+
end: i + 2
|
|
3103
5432
|
};
|
|
5433
|
+
const from = i;
|
|
5434
|
+
i = nameEnd(i, (d) => d === 61 || d === 62 || d === 47);
|
|
5435
|
+
const key = scan.take(from, i);
|
|
5436
|
+
i = scan.run(i, n, space);
|
|
5437
|
+
if (key === "" || scan.code(i) !== 61) return null;
|
|
5438
|
+
i = scan.run(i + 1, n, space);
|
|
5439
|
+
const quote = scan.code(i);
|
|
5440
|
+
if (quote !== 34 && quote !== 39) return null;
|
|
5441
|
+
const close = scan.find(quote === 34 ? "\"" : "'", i + 1, n);
|
|
5442
|
+
if (close < 0) return null;
|
|
5443
|
+
attributes.push([key, scan.take(i + 1, close)]);
|
|
5444
|
+
i = close + 1;
|
|
3104
5445
|
}
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
5446
|
+
}
|
|
5447
|
+
/**
|
|
5448
|
+
* The e-invoice declaration of an XMP packet, or null.
|
|
5449
|
+
*
|
|
5450
|
+
* No declaration (null) when it is not certain what the packet declares: properties in more than one of the
|
|
5451
|
+
* namespaces, one property with two different values, a packet that is not well-formed or holds a CDATA
|
|
5452
|
+
* section or an unbound prefix, a property element that is not a plain value (child elements, a comment,
|
|
5453
|
+
* attributes other than xml:lang, a reference to anything but a predefined entity or a character), or more
|
|
5454
|
+
* than one rdf:RDF. Values are exactly the text, references decoded and white space kept.
|
|
5455
|
+
*/
|
|
5456
|
+
function* eInvoiceClaim(packet, checkpoint, seen) {
|
|
5457
|
+
if (packet === null) return null;
|
|
5458
|
+
const xmp = decodeText(packet);
|
|
5459
|
+
if (xmp === null) return null;
|
|
5460
|
+
const scan = new Scan(xmp, checkpoint);
|
|
5461
|
+
seen?.(scan);
|
|
5462
|
+
try {
|
|
5463
|
+
const n = xmp.length;
|
|
5464
|
+
if (![...SCHEMAS.keys()].some((uri) => scan.find(uri, 0, n) >= 0)) return null;
|
|
5465
|
+
if (!(yield* checkText(xmp, checkpoint)).wellFormed) return null;
|
|
5466
|
+
return yield* claim(scan);
|
|
5467
|
+
} catch (error) {
|
|
5468
|
+
if (error instanceof OverBudget) return null;
|
|
5469
|
+
throw error;
|
|
5470
|
+
}
|
|
5471
|
+
}
|
|
5472
|
+
function* claim(scan) {
|
|
5473
|
+
const n = scan.text.length;
|
|
5474
|
+
let i = 0;
|
|
5475
|
+
/** The next tag and the text after it; MARK for a run of comments and processing instructions; null at the end, or for something not read. */
|
|
5476
|
+
const nextTag = function* () {
|
|
5477
|
+
let marked = false;
|
|
5478
|
+
while (i < n) {
|
|
5479
|
+
if (scan.due()) yield;
|
|
5480
|
+
const lt = scan.find("<", i, n);
|
|
5481
|
+
if (lt < 0) break;
|
|
5482
|
+
if (scan.at("<!--", lt) || scan.at("<?", lt)) {
|
|
5483
|
+
i = scan.at("<!--", lt) ? scan.find("-->", lt + 4, n) + 3 : scan.find("?>", lt + 2, n) + 2;
|
|
5484
|
+
marked = true;
|
|
5485
|
+
continue;
|
|
5486
|
+
}
|
|
5487
|
+
if (marked) {
|
|
5488
|
+
i = lt;
|
|
5489
|
+
return {
|
|
5490
|
+
tag: MARK,
|
|
5491
|
+
text: ""
|
|
5492
|
+
};
|
|
5493
|
+
}
|
|
5494
|
+
const c1 = scan.code(lt + 1);
|
|
5495
|
+
if (c1 === 33) return void 0;
|
|
5496
|
+
const following = (from) => {
|
|
5497
|
+
const next = scan.find("<", from, n);
|
|
5498
|
+
return scan.take(from, next < 0 ? n : next);
|
|
5499
|
+
};
|
|
5500
|
+
if (c1 === 47) {
|
|
5501
|
+
const end = scan.find(">", lt, n);
|
|
5502
|
+
i = end + 1;
|
|
5503
|
+
return {
|
|
5504
|
+
tag: {
|
|
5505
|
+
name: `/${scan.take(lt + 2, end).trim()}`,
|
|
5506
|
+
attributes: [],
|
|
5507
|
+
selfClosing: false,
|
|
5508
|
+
end: end + 1
|
|
5509
|
+
},
|
|
5510
|
+
text: following(i)
|
|
5511
|
+
};
|
|
5512
|
+
}
|
|
5513
|
+
const tag = startTag(scan, lt + 1);
|
|
5514
|
+
if (!tag) return void 0;
|
|
5515
|
+
i = tag.end;
|
|
5516
|
+
return {
|
|
5517
|
+
tag,
|
|
5518
|
+
text: following(tag.end)
|
|
5519
|
+
};
|
|
5520
|
+
}
|
|
5521
|
+
return marked ? {
|
|
5522
|
+
tag: MARK,
|
|
5523
|
+
text: ""
|
|
5524
|
+
} : null;
|
|
5525
|
+
};
|
|
5526
|
+
const scope = /* @__PURE__ */ new Map([["xml", [XML_NAMESPACE]]]);
|
|
5527
|
+
const lookup = (prefix) => scope.get(prefix)?.at(-1);
|
|
5528
|
+
const unbind = (bound) => {
|
|
5529
|
+
for (const p of bound) scope.get(p).pop();
|
|
5530
|
+
};
|
|
5531
|
+
const stack = [];
|
|
5532
|
+
const values = /* @__PURE__ */ new Map();
|
|
5533
|
+
const add = (schema, prop, value, lang) => {
|
|
5534
|
+
if (!PROPERTY_NAMES.has(prop)) return true;
|
|
5535
|
+
if (value === null) return false;
|
|
5536
|
+
let props = values.get(schema);
|
|
5537
|
+
if (!props) values.set(schema, props = /* @__PURE__ */ new Map());
|
|
5538
|
+
let set = props.get(prop);
|
|
5539
|
+
if (!set) props.set(prop, set = /* @__PURE__ */ new Map());
|
|
5540
|
+
if (set.has(value) && set.get(value) !== lang) return false;
|
|
5541
|
+
set.set(value, lang);
|
|
5542
|
+
return true;
|
|
5543
|
+
};
|
|
5544
|
+
let rdfRoots = 0;
|
|
5545
|
+
const blank = (t) => scan.fits(BLANK, t, t.length);
|
|
5546
|
+
/** Text in the element open at the top of the stack: white space only in RDF structure; noted in a property. */
|
|
5547
|
+
const content = (t) => {
|
|
5548
|
+
const owner = stack.at(-1);
|
|
5549
|
+
if (!owner || blank(t)) return true;
|
|
5550
|
+
if (owner.role === "rdf" || owner.role === "node" || owner.role === "resource") return false;
|
|
5551
|
+
if (owner.role === "property") owner.text = true;
|
|
5552
|
+
return true;
|
|
5553
|
+
};
|
|
5554
|
+
/** How many rdf:RDF elements are open: no comment or processing instruction may stand inside one. */
|
|
5555
|
+
let inRdf = 0;
|
|
5556
|
+
let current = yield* nextTag();
|
|
5557
|
+
for (;;) {
|
|
5558
|
+
if (current === void 0) return null;
|
|
5559
|
+
if (current === null) break;
|
|
5560
|
+
const following = yield* nextTag();
|
|
5561
|
+
if (following === void 0) return null;
|
|
5562
|
+
const { tag, text } = current;
|
|
5563
|
+
current = following;
|
|
5564
|
+
if (tag === MARK) {
|
|
5565
|
+
if (inRdf > 0) return null;
|
|
5566
|
+
continue;
|
|
5567
|
+
}
|
|
5568
|
+
if (tag.name.startsWith("/")) {
|
|
5569
|
+
const closed = stack.pop();
|
|
5570
|
+
if (closed) {
|
|
5571
|
+
unbind(closed.bound);
|
|
5572
|
+
if (closed.role === "rdf") inRdf -= 1;
|
|
5573
|
+
if (closed.role === "property" && ((closed.resource || closed.attributes) && (closed.children > 0 || closed.text) || closed.children > 0 && closed.text)) return null;
|
|
5574
|
+
}
|
|
5575
|
+
if (!content(text)) return null;
|
|
5576
|
+
continue;
|
|
5577
|
+
}
|
|
5578
|
+
const parent = stack.at(-1);
|
|
5579
|
+
const bound = [];
|
|
5580
|
+
for (const [k, v] of tag.attributes) if (k === "xmlns" || k.startsWith("xmlns:")) {
|
|
5581
|
+
const uri = v.length > MAX_VALUE ? null : yield* xmlValue(v, true, scan);
|
|
5582
|
+
if (uri === null) return null;
|
|
5583
|
+
const prefix = k === "xmlns" ? "" : scan.part(k, 6);
|
|
5584
|
+
let list = scope.get(prefix);
|
|
5585
|
+
if (!list) scope.set(prefix, list = []);
|
|
5586
|
+
list.push(uri);
|
|
5587
|
+
bound.push(prefix);
|
|
5588
|
+
}
|
|
5589
|
+
const resolve = (qname, attribute) => {
|
|
5590
|
+
const colon = scan.index(qname, ":");
|
|
5591
|
+
if (colon < 0) return {
|
|
5592
|
+
uri: attribute ? null : lookup("") || null,
|
|
5593
|
+
local: qname
|
|
5594
|
+
};
|
|
5595
|
+
if (colon === 0) return null;
|
|
5596
|
+
const uri = lookup(scan.part(qname, 0, colon));
|
|
5597
|
+
return uri ? {
|
|
5598
|
+
uri,
|
|
5599
|
+
local: scan.part(qname, colon + 1)
|
|
5600
|
+
} : null;
|
|
5601
|
+
};
|
|
5602
|
+
const el = resolve(tag.name, false);
|
|
5603
|
+
if (!el) return null;
|
|
5604
|
+
const attrs = [];
|
|
5605
|
+
for (const [k, v] of tag.attributes) {
|
|
5606
|
+
if (k === "xmlns" || k.startsWith("xmlns:")) continue;
|
|
5607
|
+
const a = resolve(k, true);
|
|
5608
|
+
if (!a) return null;
|
|
5609
|
+
attrs.push({
|
|
5610
|
+
...a,
|
|
5611
|
+
value: v
|
|
5612
|
+
});
|
|
5613
|
+
}
|
|
5614
|
+
const isRdf = (x, local) => x.uri === RDF && x.local === local;
|
|
5615
|
+
if (isRdf(el, "RDF")) rdfRoots += 1;
|
|
5616
|
+
if (attrs.some((a) => a.uri === XML_NAMESPACE && a.local === "base")) return null;
|
|
5617
|
+
const lang = attrs.find((a) => a.uri === XML_NAMESPACE && a.local === "lang")?.value ?? parent?.lang ?? "";
|
|
5618
|
+
const parseType = attrs.find((a) => a.uri === RDF && a.local === "parseType")?.value;
|
|
5619
|
+
let propertyAttributes = false;
|
|
5620
|
+
if (parseType !== void 0 && parseType !== "Resource" && (parent?.role === "node" || parent?.role === "resource")) return null;
|
|
5621
|
+
const role = isRdf(el, "RDF") ? "rdf" : parent === void 0 || parent.role === "other" ? "other" : parent.role === "rdf" || parent.role === "property" ? "node" : parseType === "Resource" ? "resource" : parseType === void 0 ? "property" : "other";
|
|
5622
|
+
for (const a of attrs) if (a.uri === XML_NAMESPACE && a.local === "lang" && !scan.fits(LANGUAGE, a.value, 64)) return null;
|
|
5623
|
+
if (role !== "other") {
|
|
5624
|
+
for (const a of attrs) {
|
|
5625
|
+
if (a.uri === RDF && (a.local === "about" || a.local === "resource" || a.local === "datatype")) {
|
|
5626
|
+
const v = a.value.length > MAX_VALUE ? null : yield* xmlValue(a.value, true, scan);
|
|
5627
|
+
if (v === null || scan.fits(NOT_ADDRESS, v, v.length)) return null;
|
|
5628
|
+
}
|
|
5629
|
+
if (role === "rdf" && a.uri === null) return null;
|
|
5630
|
+
}
|
|
5631
|
+
if (role !== "rdf" && el.uri === null) return null;
|
|
5632
|
+
}
|
|
5633
|
+
if (role !== "rdf" && role !== "other") {
|
|
5634
|
+
const container = parent !== void 0 && parent.uri === RDF && (parent.local === "Bag" || parent.local === "Seq" || parent.local === "Alt");
|
|
5635
|
+
if (el.uri === RDF) {
|
|
5636
|
+
if (!(role === "node" ? el.local === "Description" || el.local === "Bag" || el.local === "Seq" || el.local === "Alt" : el.local === "li" && container)) return null;
|
|
5637
|
+
} else if (role !== "node" && container) return null;
|
|
5638
|
+
for (const a of attrs) if (a.uri === RDF) {
|
|
5639
|
+
if (!(role === "node" ? a.local === "about" : a.local === "resource" || a.local === "datatype" || a.local === "parseType" && a.value === "Resource")) return null;
|
|
5640
|
+
} else if (a.uri === null) return null;
|
|
5641
|
+
if (role === "node" && parent?.role === "property" && ++parent.nodes > 1) return null;
|
|
5642
|
+
if (role === "node" && parent?.role === "property" && parent.resource) return null;
|
|
5643
|
+
if (parseType === "Resource" && attrs.some((a) => a.uri === RDF && (a.local === "resource" || a.local === "datatype"))) return null;
|
|
5644
|
+
if ((role === "property" || role === "resource") && attrs.some((a) => a.uri !== RDF && a.uri !== XML_NAMESPACE && a.uri !== null)) {
|
|
5645
|
+
if (parseType !== void 0 || attrs.some((a) => a.uri === RDF && a.local === "datatype")) return null;
|
|
5646
|
+
propertyAttributes = true;
|
|
5647
|
+
}
|
|
5648
|
+
}
|
|
5649
|
+
if (role === "node") {
|
|
5650
|
+
for (const a of attrs) if (a.local === "about" && (a.uri === RDF || a.uri === null) && a.value !== "" && !scan.fits(SCHEME, a.value, a.value.length)) return null;
|
|
5651
|
+
}
|
|
5652
|
+
const about = role === "node" && attrs.every((a) => !((a.uri === RDF || a.uri === null && (a.local === "about" || a.local === "ID")) && (a.local === "ID" || a.local === "nodeID" || a.local === "about" && a.value !== ""))) && attrs.some((a) => (a.uri === RDF || a.uri === null) && a.local === "about");
|
|
5653
|
+
if (about) for (const a of attrs) {
|
|
5654
|
+
const schema = a.uri === null ? void 0 : SCHEMAS.get(a.uri);
|
|
5655
|
+
if (!schema) continue;
|
|
5656
|
+
if (PROPERTY_NAMES.has(a.local) && scan.fits(RAW_SPACE, a.value, a.value.length)) return null;
|
|
5657
|
+
if (a.value.length > MAX_VALUE || !add(schema, a.local, yield* xmlValue(a.value, true, scan), lang)) return null;
|
|
5658
|
+
}
|
|
5659
|
+
if (parent?.about) {
|
|
5660
|
+
const schema = el.uri === null ? void 0 : SCHEMAS.get(el.uri);
|
|
5661
|
+
if (schema && PROPERTY_NAMES.has(el.local)) {
|
|
5662
|
+
if (!(!tag.selfClosing && following?.tag.name === `/${tag.name}` && attrs.every((a) => a.uri === XML_NAMESPACE && a.local === "lang")) || text.length > MAX_VALUE || scan.index(text, "\r") >= 0 || !add(schema, el.local, yield* xmlValue(text, false, scan), lang)) return null;
|
|
5663
|
+
}
|
|
5664
|
+
}
|
|
5665
|
+
if (parent?.role === "property") parent.children += 1;
|
|
5666
|
+
if (tag.selfClosing) {
|
|
5667
|
+
unbind(bound);
|
|
5668
|
+
if (!content(text)) return null;
|
|
5669
|
+
} else {
|
|
5670
|
+
stack.push({
|
|
5671
|
+
...el,
|
|
5672
|
+
bound,
|
|
5673
|
+
about,
|
|
5674
|
+
role,
|
|
5675
|
+
lang,
|
|
5676
|
+
nodes: 0,
|
|
5677
|
+
resource: attrs.some((a) => a.uri === RDF && a.local === "resource"),
|
|
5678
|
+
attributes: propertyAttributes,
|
|
5679
|
+
text: false,
|
|
5680
|
+
children: 0
|
|
5681
|
+
});
|
|
5682
|
+
if (role === "rdf") inRdf += 1;
|
|
5683
|
+
if (!content(text)) return null;
|
|
3111
5684
|
}
|
|
3112
|
-
return n;
|
|
3113
5685
|
}
|
|
3114
|
-
|
|
5686
|
+
if (rdfRoots > 1 || values.size !== 1) return null;
|
|
5687
|
+
const [[schema, props]] = values;
|
|
5688
|
+
const one = (prop) => {
|
|
5689
|
+
const set = props.get(prop);
|
|
5690
|
+
if (!set) return null;
|
|
5691
|
+
return set.size === 1 ? [...set.keys()][0] : void 0;
|
|
5692
|
+
};
|
|
5693
|
+
const claim = {
|
|
5694
|
+
schema,
|
|
5695
|
+
documentType: null,
|
|
5696
|
+
documentFileName: null,
|
|
5697
|
+
version: null,
|
|
5698
|
+
conformanceLevel: null
|
|
5699
|
+
};
|
|
5700
|
+
for (const [prop, field] of [
|
|
5701
|
+
["DocumentType", "documentType"],
|
|
5702
|
+
["DocumentFileName", "documentFileName"],
|
|
5703
|
+
["Version", "version"],
|
|
5704
|
+
["ConformanceLevel", "conformanceLevel"]
|
|
5705
|
+
]) {
|
|
5706
|
+
const v = one(prop);
|
|
5707
|
+
if (v === void 0) return null;
|
|
5708
|
+
claim[field] = v;
|
|
5709
|
+
}
|
|
5710
|
+
return claim;
|
|
5711
|
+
}
|
|
3115
5712
|
// src/document.ts
|
|
3116
5713
|
const INFO_FIELDS = [
|
|
3117
5714
|
"Title",
|
|
@@ -3182,7 +5779,7 @@ function* readPages(reader, catalog) {
|
|
|
3182
5779
|
box,
|
|
3183
5780
|
rotate
|
|
3184
5781
|
});
|
|
3185
|
-
else reader.
|
|
5782
|
+
else reader.pagesRepaired = true;
|
|
3186
5783
|
}
|
|
3187
5784
|
continue;
|
|
3188
5785
|
}
|
|
@@ -3202,61 +5799,6 @@ function* readPages(reader, catalog) {
|
|
|
3202
5799
|
}
|
|
3203
5800
|
return sizes;
|
|
3204
5801
|
}
|
|
3205
|
-
/** PDFDocEncoding bytes 0x18 to 0x1f and 0x80 to 0xad that differ from Latin-1. */
|
|
3206
|
-
const PDFDOC = {
|
|
3207
|
-
24: "˘",
|
|
3208
|
-
25: "ˇ",
|
|
3209
|
-
26: "ˆ",
|
|
3210
|
-
27: "˙",
|
|
3211
|
-
28: "˝",
|
|
3212
|
-
29: "˛",
|
|
3213
|
-
30: "˚",
|
|
3214
|
-
31: "˜",
|
|
3215
|
-
128: "•",
|
|
3216
|
-
129: "†",
|
|
3217
|
-
130: "‡",
|
|
3218
|
-
131: "…",
|
|
3219
|
-
132: "—",
|
|
3220
|
-
133: "–",
|
|
3221
|
-
134: "ƒ",
|
|
3222
|
-
135: "⁄",
|
|
3223
|
-
136: "‹",
|
|
3224
|
-
137: "›",
|
|
3225
|
-
138: "−",
|
|
3226
|
-
139: "‰",
|
|
3227
|
-
140: "„",
|
|
3228
|
-
141: "“",
|
|
3229
|
-
142: "”",
|
|
3230
|
-
143: "‘",
|
|
3231
|
-
144: "’",
|
|
3232
|
-
145: "‚",
|
|
3233
|
-
146: "™",
|
|
3234
|
-
147: "fi",
|
|
3235
|
-
148: "fl",
|
|
3236
|
-
149: "Ł",
|
|
3237
|
-
150: "Œ",
|
|
3238
|
-
151: "Š",
|
|
3239
|
-
152: "Ÿ",
|
|
3240
|
-
153: "Ž",
|
|
3241
|
-
154: "ı",
|
|
3242
|
-
155: "ł",
|
|
3243
|
-
156: "œ",
|
|
3244
|
-
157: "š",
|
|
3245
|
-
158: "ž",
|
|
3246
|
-
160: "€"
|
|
3247
|
-
};
|
|
3248
|
-
/** A text string: UTF-16BE or UTF-8 with a byte order mark, else PDFDocEncoding. */
|
|
3249
|
-
function textOf(b) {
|
|
3250
|
-
if (b[0] === 254 && b[1] === 255) {
|
|
3251
|
-
let s = "";
|
|
3252
|
-
for (let i = 2; i + 1 < b.length; i += 2) s += String.fromCharCode(b[i] << 8 | b[i + 1]);
|
|
3253
|
-
return s;
|
|
3254
|
-
}
|
|
3255
|
-
if (b[0] === 239 && b[1] === 187 && b[2] === 191) return new TextDecoder("utf-8").decode(b.subarray(3));
|
|
3256
|
-
let s = "";
|
|
3257
|
-
for (const c of b) s += PDFDOC[c] ?? String.fromCharCode(c);
|
|
3258
|
-
return s;
|
|
3259
|
-
}
|
|
3260
5802
|
/** The PDF/A part and conformance an XMP packet declares (any prefix bound to the pdfaid namespace). */
|
|
3261
5803
|
function pdfaClaim(xmp) {
|
|
3262
5804
|
const prefixes = /* @__PURE__ */ new Set();
|
|
@@ -3300,16 +5842,12 @@ function readInfo(reader) {
|
|
|
3300
5842
|
if (value === null) continue;
|
|
3301
5843
|
out.fields.push(field);
|
|
3302
5844
|
if (value instanceof PdfName) out.values[field] = value.name;
|
|
3303
|
-
else if (value instanceof PdfString)
|
|
3304
|
-
const holder = own ?? definition;
|
|
3305
|
-
let bytes = value.bytes();
|
|
3306
|
-
if (holder && reader.security && reader.encrypted(holder)) bytes = decryptString(reader.security, holder.num, holder.gen, bytes);
|
|
3307
|
-
out.values[field] = textOf(bytes);
|
|
3308
|
-
}
|
|
5845
|
+
else if (value instanceof PdfString) out.values[field] = textOf(stringBytes(reader, value, own ?? definition));
|
|
3309
5846
|
}
|
|
3310
5847
|
for (const k of keys) if (!INFO_FIELDS.includes(k)) out.custom += 1;
|
|
3311
5848
|
return out;
|
|
3312
5849
|
}
|
|
5850
|
+
/** The XMP packet as text (decoded as 0.1.0 decoded it), with its bytes. */
|
|
3313
5851
|
function readXmp(reader, catalog) {
|
|
3314
5852
|
const { value, definition } = reader.resolveWithDefinition(catalog.get("Metadata"));
|
|
3315
5853
|
if (!(value instanceof PdfDict) || !value.stream) return null;
|
|
@@ -3321,9 +5859,18 @@ function readXmp(reader, catalog) {
|
|
|
3321
5859
|
reader.repaired = true;
|
|
3322
5860
|
return null;
|
|
3323
5861
|
}
|
|
3324
|
-
if (bytes[0] === 254 && bytes[1] === 255) return
|
|
3325
|
-
|
|
3326
|
-
|
|
5862
|
+
if (bytes[0] === 254 && bytes[1] === 255) return {
|
|
5863
|
+
text: textOf(bytes),
|
|
5864
|
+
bytes
|
|
5865
|
+
};
|
|
5866
|
+
if (bytes[0] === 255 && bytes[1] === 254) return {
|
|
5867
|
+
text: new TextDecoder("utf-16le").decode(bytes.subarray(2)),
|
|
5868
|
+
bytes
|
|
5869
|
+
};
|
|
5870
|
+
return {
|
|
5871
|
+
text: new TextDecoder("utf-8").decode(bytes[0] === 239 && bytes[1] === 187 && bytes[2] === 191 ? bytes.subarray(3) : bytes),
|
|
5872
|
+
bytes
|
|
5873
|
+
};
|
|
3327
5874
|
}
|
|
3328
5875
|
/** The catalog: the trailer's /Root, or else the latest catalog in force. */
|
|
3329
5876
|
function findCatalog(reader) {
|
|
@@ -3361,40 +5908,61 @@ function* analyse(snapshot, work) {
|
|
|
3361
5908
|
const catalog = findCatalog(reader);
|
|
3362
5909
|
const pageSizes = yield* readPages(reader, catalog);
|
|
3363
5910
|
const info = readInfo(reader);
|
|
3364
|
-
const
|
|
3365
|
-
const
|
|
5911
|
+
const packet = readXmp(reader, catalog);
|
|
5912
|
+
const xmp = packet?.text ?? null;
|
|
5913
|
+
const counted = /* @__PURE__ */ new Set();
|
|
5914
|
+
const values = new Values(reader);
|
|
5915
|
+
const active = yield* countActive(reader, counted, (v, def) => {
|
|
5916
|
+
reader.quiet = true;
|
|
5917
|
+
try {
|
|
5918
|
+
values.visit(v, def);
|
|
5919
|
+
} finally {
|
|
5920
|
+
reader.quiet = false;
|
|
5921
|
+
}
|
|
5922
|
+
});
|
|
3366
5923
|
let version = header.version;
|
|
3367
5924
|
const catalogVersion = reader.resolve(catalog.get("Version"));
|
|
3368
5925
|
if (catalogVersion instanceof PdfName && /^\d\.\d$/.test(catalogVersion.name) && versionValue(catalogVersion.name) > versionValue(version)) version = catalogVersion.name;
|
|
3369
5926
|
const first = reader.defs.filter((d) => !d.container && !d.hidden).reduce((a, d) => !a || d.offset < a.offset ? d : a, void 0);
|
|
3370
5927
|
const linearized = first !== void 0 && first.value instanceof PdfDict && first.value.has("Linearized");
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
xmp: xmp !== null
|
|
3391
|
-
},
|
|
3392
|
-
active
|
|
5928
|
+
const inspection = {
|
|
5929
|
+
version,
|
|
5930
|
+
pages: pageSizes.length,
|
|
5931
|
+
pageSizes,
|
|
5932
|
+
encrypted,
|
|
5933
|
+
pdfa: xmp === null ? null : pdfaClaim(xmp),
|
|
5934
|
+
structure: {
|
|
5935
|
+
revisions: reader.revisions(linearized),
|
|
5936
|
+
xrefStreams: reader.xrefStreams,
|
|
5937
|
+
objectStreams: reader.objectStreams,
|
|
5938
|
+
linearized,
|
|
5939
|
+
shadowedObjects: reader.shadowedCount(),
|
|
5940
|
+
hiddenObjects: reader.defs.filter((d) => d.hidden && !d.container && !d.alternateOf).length,
|
|
5941
|
+
repaired: reader.repaired || reader.pagesRepaired
|
|
5942
|
+
},
|
|
5943
|
+
metadata: {
|
|
5944
|
+
infoFields: info.fields,
|
|
5945
|
+
customInfoFields: info.custom,
|
|
5946
|
+
xmp: xmp !== null
|
|
3393
5947
|
},
|
|
5948
|
+
active
|
|
5949
|
+
};
|
|
5950
|
+
reader.quiet = true;
|
|
5951
|
+
let embeddedFiles;
|
|
5952
|
+
try {
|
|
5953
|
+
embeddedFiles = yield* describeEmbeddedFiles(reader, catalog, counted, active.unresolved, values);
|
|
5954
|
+
} finally {
|
|
5955
|
+
reader.quiet = false;
|
|
5956
|
+
}
|
|
5957
|
+
const eInvoice = yield* eInvoiceClaim(packet?.bytes ?? null, () => work.checkpoint());
|
|
5958
|
+
return {
|
|
5959
|
+
inspection,
|
|
3394
5960
|
metadata: {
|
|
3395
5961
|
info: info.values,
|
|
3396
|
-
xmp
|
|
3397
|
-
|
|
5962
|
+
xmp,
|
|
5963
|
+
eInvoice
|
|
5964
|
+
},
|
|
5965
|
+
embeddedFiles
|
|
3398
5966
|
};
|
|
3399
5967
|
}
|
|
3400
5968
|
// src/options.ts
|
|
@@ -3641,9 +6209,22 @@ var OpenedDoc = class {
|
|
|
3641
6209
|
readMetadata() {
|
|
3642
6210
|
return structuredClone(this.#analysis.metadata);
|
|
3643
6211
|
}
|
|
6212
|
+
readEmbeddedFiles() {
|
|
6213
|
+
const { files, complete } = this.#analysis.embeddedFiles;
|
|
6214
|
+
return {
|
|
6215
|
+
files: files.map((f) => ({
|
|
6216
|
+
...f,
|
|
6217
|
+
xml: f.xml && {
|
|
6218
|
+
...f.xml,
|
|
6219
|
+
root: f.xml.root && { ...f.xml.root }
|
|
6220
|
+
}
|
|
6221
|
+
})),
|
|
6222
|
+
complete
|
|
6223
|
+
};
|
|
6224
|
+
}
|
|
3644
6225
|
};
|
|
3645
6226
|
/**
|
|
3646
|
-
* Opens a PDF under the limits and reads everything `inspect` and `
|
|
6227
|
+
* Opens a PDF under the limits and reads everything `inspect`, `readMetadata` and `readEmbeddedFiles` report. Rejects with
|
|
3647
6228
|
* `PdfToolsError` for every problem; anything that cannot be read within the limits is an error, never an
|
|
3648
6229
|
* empty report.
|
|
3649
6230
|
*/
|