@patrickdk77/xfa-flatten 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +14 -0
- package/README.md +112 -2
- package/dist/barcode-paint.d.ts +11 -0
- package/dist/barcode-paint.js +135 -0
- package/dist/barcode.d.ts +45 -0
- package/dist/barcode.js +750 -0
- package/dist/canvas.d.ts +59 -0
- package/dist/canvas.js +179 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +34 -0
- package/dist/css.d.ts +48 -0
- package/dist/css.js +288 -0
- package/dist/deadline.d.ts +6 -0
- package/dist/deadline.js +18 -0
- package/dist/filters.d.ts +21 -0
- package/dist/filters.js +296 -0
- package/dist/flatten.d.ts +25 -0
- package/dist/flatten.js +319 -0
- package/dist/font-metrics.d.ts +9 -0
- package/dist/font-metrics.js +215 -0
- package/dist/fonts.d.ts +36 -0
- package/dist/fonts.js +144 -0
- package/dist/images.d.ts +23 -0
- package/dist/images.js +519 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/index.mjs +3 -0
- package/dist/layout.d.ts +64 -0
- package/dist/layout.js +779 -0
- package/dist/limits.d.ts +2 -0
- package/dist/limits.js +6 -0
- package/dist/locale.d.ts +29 -0
- package/dist/locale.js +97 -0
- package/dist/paint.d.ts +15 -0
- package/dist/paint.js +533 -0
- package/dist/pdf-crypt.d.ts +35 -0
- package/dist/pdf-crypt.js +261 -0
- package/dist/pdf-read.d.ts +140 -0
- package/dist/pdf-read.js +1026 -0
- package/dist/pdf417-table.d.ts +2 -0
- package/dist/pdf417-table.js +146 -0
- package/dist/pdfjs-repair.d.ts +13 -0
- package/dist/pdfjs-repair.js +257 -0
- package/dist/pdfjs.d.ts +16 -0
- package/dist/pdfjs.js +62 -0
- package/dist/picture.d.ts +40 -0
- package/dist/picture.js +678 -0
- package/dist/saslprep.d.ts +11 -0
- package/dist/saslprep.js +178 -0
- package/dist/style.d.ts +29 -0
- package/dist/style.js +178 -0
- package/dist/text.d.ts +44 -0
- package/dist/text.js +199 -0
- package/dist/tiff.d.ts +3 -0
- package/dist/tiff.js +270 -0
- package/dist/tree.d.ts +23 -0
- package/dist/tree.js +54 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +2 -0
- package/dist/writer.d.ts +38 -0
- package/dist/writer.js +145 -0
- package/dist/xfa-apply.d.ts +49 -0
- package/dist/xfa-apply.js +617 -0
- package/dist/xfa-model.d.ts +42 -0
- package/dist/xfa-model.js +227 -0
- package/dist/xfa-paint.d.ts +24 -0
- package/dist/xfa-paint.js +275 -0
- package/dist/xfa-spec.d.ts +47 -0
- package/dist/xfa-spec.js +3 -0
- package/dist/xml.d.ts +30 -0
- package/dist/xml.js +211 -0
- package/package.json +83 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare function rc4(key: Uint8Array, data: Uint8Array): Uint8Array;
|
|
2
|
+
export type CryptMethod = 'none' | 'rc4' | 'aes128' | 'aes256';
|
|
3
|
+
export interface EncryptInfo {
|
|
4
|
+
v: number;
|
|
5
|
+
r: number;
|
|
6
|
+
lengthBits: number;
|
|
7
|
+
o: Uint8Array;
|
|
8
|
+
u: Uint8Array;
|
|
9
|
+
oe: Uint8Array | null;
|
|
10
|
+
ue: Uint8Array | null;
|
|
11
|
+
p: number;
|
|
12
|
+
id0: Uint8Array;
|
|
13
|
+
encryptMetadata: boolean;
|
|
14
|
+
/** Methods for strings and streams, from /StrF and /StmF for V4 and V5. */
|
|
15
|
+
strMethod: CryptMethod;
|
|
16
|
+
stmMethod: CryptMethod;
|
|
17
|
+
}
|
|
18
|
+
/** Decrypts strings and streams of one document once a password has opened it. */
|
|
19
|
+
export declare class Decryptor {
|
|
20
|
+
private readonly info;
|
|
21
|
+
private readonly key;
|
|
22
|
+
/** The password that opened the file, in the prepared form that worked. */
|
|
23
|
+
readonly password: string;
|
|
24
|
+
private constructor();
|
|
25
|
+
/** Tries each password, as the user password and then the owner password. Null when none opens the file. */
|
|
26
|
+
static open(info: EncryptInfo, passwords: readonly string[]): Decryptor | null;
|
|
27
|
+
private objectKey;
|
|
28
|
+
private run;
|
|
29
|
+
string(num: number, gen: number, data: Uint8Array): Uint8Array;
|
|
30
|
+
/** Encrypts with the method and key the file uses, for an object written into an incremental update. */
|
|
31
|
+
private seal;
|
|
32
|
+
sealString(num: number, gen: number, data: Uint8Array): Uint8Array;
|
|
33
|
+
sealStream(num: number, gen: number, data: Uint8Array): Uint8Array;
|
|
34
|
+
streamBytes(num: number, gen: number, data: Uint8Array): Uint8Array;
|
|
35
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The PDF standard security handler, enough to read encrypted XFA packets: RC4 and AES-128 (revisions 2 to 4)
|
|
3
|
+
// and AES-256 (revisions 5 and 6). Node's crypto supplies MD5, SHA-2 and AES; RC4 is ours, as OpenSSL 3's default
|
|
4
|
+
// provider has none.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Decryptor = void 0;
|
|
7
|
+
exports.rc4 = rc4;
|
|
8
|
+
const node_crypto_1 = require("node:crypto");
|
|
9
|
+
const saslprep_1 = require("./saslprep");
|
|
10
|
+
const PAD = Uint8Array.from([
|
|
11
|
+
0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a,
|
|
12
|
+
]);
|
|
13
|
+
function rc4(key, data) {
|
|
14
|
+
const s = new Uint8Array(256);
|
|
15
|
+
for (let i = 0; i < 256; i++)
|
|
16
|
+
s[i] = i;
|
|
17
|
+
let j = 0;
|
|
18
|
+
for (let i = 0; i < 256; i++) {
|
|
19
|
+
j = (j + s[i] + key[i % key.length]) & 255;
|
|
20
|
+
const t = s[i];
|
|
21
|
+
s[i] = s[j];
|
|
22
|
+
s[j] = t;
|
|
23
|
+
}
|
|
24
|
+
const out = new Uint8Array(data.length);
|
|
25
|
+
let a = 0;
|
|
26
|
+
let b = 0;
|
|
27
|
+
for (let k = 0; k < data.length; k++) {
|
|
28
|
+
a = (a + 1) & 255;
|
|
29
|
+
b = (b + s[a]) & 255;
|
|
30
|
+
const t = s[a];
|
|
31
|
+
s[a] = s[b];
|
|
32
|
+
s[b] = t;
|
|
33
|
+
out[k] = data[k] ^ s[(s[a] + s[b]) & 255];
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function md5(...parts) {
|
|
38
|
+
const h = (0, node_crypto_1.createHash)('md5');
|
|
39
|
+
for (const p of parts)
|
|
40
|
+
h.update(p);
|
|
41
|
+
return new Uint8Array(h.digest());
|
|
42
|
+
}
|
|
43
|
+
function sha(alg, ...parts) {
|
|
44
|
+
const h = (0, node_crypto_1.createHash)(alg);
|
|
45
|
+
for (const p of parts)
|
|
46
|
+
h.update(p);
|
|
47
|
+
return new Uint8Array(h.digest());
|
|
48
|
+
}
|
|
49
|
+
function aesCbc(bits, key, iv, data, padding) {
|
|
50
|
+
const d = (0, node_crypto_1.createDecipheriv)(`aes-${bits}-cbc`, key, iv);
|
|
51
|
+
d.setAutoPadding(padding);
|
|
52
|
+
return new Uint8Array(Buffer.concat([d.update(data), d.final()]));
|
|
53
|
+
}
|
|
54
|
+
function aesCbcEncrypt128(key, iv, data) {
|
|
55
|
+
const c = (0, node_crypto_1.createCipheriv)('aes-128-cbc', key, iv);
|
|
56
|
+
c.setAutoPadding(false);
|
|
57
|
+
return new Uint8Array(Buffer.concat([c.update(data), c.final()]));
|
|
58
|
+
}
|
|
59
|
+
/** Encrypts with AES-CBC and PKCS#7 padding, and puts a random IV in front, as PDF wants. */
|
|
60
|
+
function aesSeal(bits, key, data) {
|
|
61
|
+
const iv = (0, node_crypto_1.randomBytes)(16);
|
|
62
|
+
const c = (0, node_crypto_1.createCipheriv)(`aes-${bits}-cbc`, key, iv);
|
|
63
|
+
return new Uint8Array(Buffer.concat([iv, c.update(data), c.final()]));
|
|
64
|
+
}
|
|
65
|
+
/** Decrypts AES-CBC data that starts with its 16-byte IV, tolerating bad padding. */
|
|
66
|
+
function aesWithIv(bits, key, data) {
|
|
67
|
+
if (data.length < 32 || data.length % 16 !== 0) {
|
|
68
|
+
if (data.length < 16)
|
|
69
|
+
return new Uint8Array(0);
|
|
70
|
+
const whole = data.subarray(0, 16 + Math.floor((data.length - 16) / 16) * 16);
|
|
71
|
+
return aesCbc(bits, key, whole.subarray(0, 16), whole.subarray(16), false);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
return aesCbc(bits, key, data.subarray(0, 16), data.subarray(16), true);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return aesCbc(bits, key, data.subarray(0, 16), data.subarray(16), false);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function latin1(pw) {
|
|
81
|
+
const out = new Uint8Array(Math.min(pw.length, 32));
|
|
82
|
+
for (let i = 0; i < out.length; i++) {
|
|
83
|
+
const c = pw.charCodeAt(i);
|
|
84
|
+
out[i] = c < 256 ? c : 0x3f;
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
function padded(pw) {
|
|
89
|
+
const out = new Uint8Array(32);
|
|
90
|
+
out.set(pw.subarray(0, 32));
|
|
91
|
+
out.set(PAD.subarray(0, 32 - Math.min(32, pw.length)), Math.min(32, pw.length));
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
function equalPrefix(a, b, n) {
|
|
95
|
+
if (a.length < n || b.length < n)
|
|
96
|
+
return false;
|
|
97
|
+
for (let i = 0; i < n; i++)
|
|
98
|
+
if (a[i] !== b[i])
|
|
99
|
+
return false;
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/** Algorithm 2: the file key from a user password, revisions 2 to 4. */
|
|
103
|
+
function fileKeyR4(info, pw) {
|
|
104
|
+
const n = info.r === 2 ? 5 : Math.max(5, Math.min(16, Math.floor(info.lengthBits / 8)));
|
|
105
|
+
const p = new Uint8Array(4);
|
|
106
|
+
new DataView(p.buffer).setInt32(0, info.p, true);
|
|
107
|
+
const parts = [padded(pw), info.o.subarray(0, 32), p, info.id0];
|
|
108
|
+
if (info.r >= 4 && !info.encryptMetadata)
|
|
109
|
+
parts.push(Uint8Array.from([255, 255, 255, 255]));
|
|
110
|
+
let key = md5(...parts).subarray(0, n);
|
|
111
|
+
if (info.r >= 3)
|
|
112
|
+
for (let i = 0; i < 50; i++)
|
|
113
|
+
key = md5(key).subarray(0, n);
|
|
114
|
+
return key;
|
|
115
|
+
}
|
|
116
|
+
function checkUserR4(info, key) {
|
|
117
|
+
if (info.r === 2)
|
|
118
|
+
return equalPrefix(rc4(key, PAD), info.u, 32);
|
|
119
|
+
let x = rc4(key, md5(PAD, info.id0));
|
|
120
|
+
for (let i = 1; i <= 19; i++)
|
|
121
|
+
x = rc4(key.map(b => b ^ i), x);
|
|
122
|
+
return equalPrefix(x, info.u, 16);
|
|
123
|
+
}
|
|
124
|
+
function userFromOwnerR4(info, pw) {
|
|
125
|
+
const n = info.r === 2 ? 5 : Math.max(5, Math.min(16, Math.floor(info.lengthBits / 8)));
|
|
126
|
+
let k = md5(padded(pw));
|
|
127
|
+
if (info.r >= 3)
|
|
128
|
+
for (let i = 0; i < 50; i++)
|
|
129
|
+
k = md5(k);
|
|
130
|
+
const key = k.subarray(0, n);
|
|
131
|
+
let x = info.o.subarray(0, 32);
|
|
132
|
+
if (info.r === 2)
|
|
133
|
+
x = rc4(key, x);
|
|
134
|
+
else
|
|
135
|
+
for (let i = 19; i >= 0; i--)
|
|
136
|
+
x = rc4(key.map(b => b ^ i), x);
|
|
137
|
+
return x;
|
|
138
|
+
}
|
|
139
|
+
/** Algorithm 2.B (revision 6) or plain SHA-256 (revision 5). */
|
|
140
|
+
function hashR6(info, pw, salt, udata) {
|
|
141
|
+
if (info.r === 5)
|
|
142
|
+
return sha('sha256', pw, salt, udata);
|
|
143
|
+
let k = sha('sha256', pw, salt, udata);
|
|
144
|
+
for (let i = 0;; i++) {
|
|
145
|
+
const unit = new Uint8Array(pw.length + k.length + udata.length);
|
|
146
|
+
unit.set(pw);
|
|
147
|
+
unit.set(k, pw.length);
|
|
148
|
+
unit.set(udata, pw.length + k.length);
|
|
149
|
+
const k1 = new Uint8Array(unit.length * 64);
|
|
150
|
+
for (let j = 0; j < 64; j++)
|
|
151
|
+
k1.set(unit, j * unit.length);
|
|
152
|
+
const e = aesCbcEncrypt128(k.subarray(0, 16), k.subarray(16, 32), k1);
|
|
153
|
+
let sum = 0;
|
|
154
|
+
for (let j = 0; j < 16; j++)
|
|
155
|
+
sum += e[j];
|
|
156
|
+
const mod = sum % 3;
|
|
157
|
+
k = sha(mod === 0 ? 'sha256' : mod === 1 ? 'sha384' : 'sha512', e);
|
|
158
|
+
if (i >= 63 && e[e.length - 1] <= i - 31)
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
return k.subarray(0, 32);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The file key for revision 5 or 6. ISO 32000-2 prepares a revision 6 password with SASLprep; pdf.js prepares it its
|
|
165
|
+
* own way and then tries it as typed, so every form a reader tries is tried here too, as pdf-defuse does. Revision 5
|
|
166
|
+
* takes it as typed. Each form is tried as the user password, then as the owner password.
|
|
167
|
+
*/
|
|
168
|
+
function fileKeyR6(info, password) {
|
|
169
|
+
const forms = info.r === 6 ? [(0, saslprep_1.saslprep)(password), (0, saslprep_1.pdfjsPrep)(password), password] : [password];
|
|
170
|
+
const u48 = info.u.subarray(0, 48);
|
|
171
|
+
for (const form of new Set(forms)) {
|
|
172
|
+
if (form === undefined)
|
|
173
|
+
continue;
|
|
174
|
+
const pw = new Uint8Array(Buffer.from(form, 'utf8')).subarray(0, 127);
|
|
175
|
+
if (equalPrefix(hashR6(info, pw, info.u.subarray(32, 40), new Uint8Array(0)), info.u, 32) && info.ue) {
|
|
176
|
+
const k = hashR6(info, pw, info.u.subarray(40, 48), new Uint8Array(0));
|
|
177
|
+
return { key: aesCbc(256, k, new Uint8Array(16), info.ue.subarray(0, 32), false), form };
|
|
178
|
+
}
|
|
179
|
+
if (equalPrefix(hashR6(info, pw, info.o.subarray(32, 40), u48), info.o, 32) && info.oe) {
|
|
180
|
+
const k = hashR6(info, pw, info.o.subarray(40, 48), u48);
|
|
181
|
+
return { key: aesCbc(256, k, new Uint8Array(16), info.oe.subarray(0, 32), false), form };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
/** Decrypts strings and streams of one document once a password has opened it. */
|
|
187
|
+
class Decryptor {
|
|
188
|
+
info;
|
|
189
|
+
key;
|
|
190
|
+
password;
|
|
191
|
+
constructor(info, key,
|
|
192
|
+
/** The password that opened the file, in the prepared form that worked. */
|
|
193
|
+
password) {
|
|
194
|
+
this.info = info;
|
|
195
|
+
this.key = key;
|
|
196
|
+
this.password = password;
|
|
197
|
+
}
|
|
198
|
+
/** Tries each password, as the user password and then the owner password. Null when none opens the file. */
|
|
199
|
+
static open(info, passwords) {
|
|
200
|
+
for (const password of passwords) {
|
|
201
|
+
try {
|
|
202
|
+
if (info.r >= 5) {
|
|
203
|
+
const found = fileKeyR6(info, password);
|
|
204
|
+
if (found)
|
|
205
|
+
return new Decryptor(info, found.key, found.form);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const pw = latin1(password);
|
|
209
|
+
let key = fileKeyR4(info, pw);
|
|
210
|
+
if (checkUserR4(info, key))
|
|
211
|
+
return new Decryptor(info, key, password);
|
|
212
|
+
key = fileKeyR4(info, userFromOwnerR4(info, pw));
|
|
213
|
+
if (checkUserR4(info, key))
|
|
214
|
+
return new Decryptor(info, key, password);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// A malformed encryption dictionary opens with no password.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
objectKey(num, gen, aes) {
|
|
223
|
+
const extra = Uint8Array.from([num & 255, (num >> 8) & 255, (num >> 16) & 255, gen & 255, (gen >> 8) & 255]);
|
|
224
|
+
const parts = [this.key, extra];
|
|
225
|
+
if (aes)
|
|
226
|
+
parts.push(Uint8Array.from([0x73, 0x41, 0x6c, 0x54]));
|
|
227
|
+
return md5(...parts).subarray(0, Math.min(this.key.length + 5, 16));
|
|
228
|
+
}
|
|
229
|
+
run(method, num, gen, data) {
|
|
230
|
+
if (method === 'none')
|
|
231
|
+
return data;
|
|
232
|
+
if (method === 'aes256')
|
|
233
|
+
return aesWithIv(256, this.key, data);
|
|
234
|
+
if (method === 'aes128')
|
|
235
|
+
return aesWithIv(128, this.objectKey(num, gen, true), data);
|
|
236
|
+
return rc4(this.objectKey(num, gen, false), data);
|
|
237
|
+
}
|
|
238
|
+
string(num, gen, data) {
|
|
239
|
+
return this.run(this.info.strMethod, num, gen, data);
|
|
240
|
+
}
|
|
241
|
+
/** Encrypts with the method and key the file uses, for an object written into an incremental update. */
|
|
242
|
+
seal(method, num, gen, data) {
|
|
243
|
+
if (method === 'none')
|
|
244
|
+
return data;
|
|
245
|
+
if (method === 'aes256')
|
|
246
|
+
return aesSeal(256, this.key, data);
|
|
247
|
+
if (method === 'aes128')
|
|
248
|
+
return aesSeal(128, this.objectKey(num, gen, true), data);
|
|
249
|
+
return rc4(this.objectKey(num, gen, false), data);
|
|
250
|
+
}
|
|
251
|
+
sealString(num, gen, data) {
|
|
252
|
+
return this.seal(this.info.strMethod, num, gen, data);
|
|
253
|
+
}
|
|
254
|
+
sealStream(num, gen, data) {
|
|
255
|
+
return this.seal(this.info.stmMethod, num, gen, data);
|
|
256
|
+
}
|
|
257
|
+
streamBytes(num, gen, data) {
|
|
258
|
+
return this.run(this.info.stmMethod, num, gen, data);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
exports.Decryptor = Decryptor;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Decryptor } from './pdf-crypt';
|
|
2
|
+
/**
|
|
3
|
+
* The most bytes the stream filters may produce for one reader, over all the streams it decodes: 64 MiB. The XFA
|
|
4
|
+
* packets of the largest form in the test corpus decode to 2.4 MB in all. A stream past the limit reads as
|
|
5
|
+
* undecodable.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DECODE_LIMIT: number;
|
|
8
|
+
/**
|
|
9
|
+
* The most the XFA packets may decode to, all together: 16 MiB, about seven times the largest form in the test
|
|
10
|
+
* corpus. pdf.js holds several copies of the XFA while it lays a form out, so this also bounds its memory; a larger
|
|
11
|
+
* form is not handed to pdf.js at all.
|
|
12
|
+
*/
|
|
13
|
+
export declare const XFA_LIMIT: number;
|
|
14
|
+
export declare class PName {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
constructor(name: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class PRef {
|
|
19
|
+
readonly num: number;
|
|
20
|
+
readonly gen: number;
|
|
21
|
+
constructor(num: number, gen: number);
|
|
22
|
+
}
|
|
23
|
+
export declare class PString {
|
|
24
|
+
readonly bytes: Uint8Array;
|
|
25
|
+
constructor(bytes: Uint8Array);
|
|
26
|
+
}
|
|
27
|
+
export type PDict = Map<string, PObj>;
|
|
28
|
+
export declare class PStream {
|
|
29
|
+
readonly dict: PDict;
|
|
30
|
+
readonly raw: Uint8Array;
|
|
31
|
+
constructor(dict: PDict, raw: Uint8Array);
|
|
32
|
+
}
|
|
33
|
+
export type PObj = number | boolean | null | PName | PRef | PString | PObj[] | PDict | PStream | string;
|
|
34
|
+
export declare function isDict(v: PObj | undefined): v is PDict;
|
|
35
|
+
/**
|
|
36
|
+
* Counts the bytes a reader's lexers walk. A hostile file can make each of many parses run to the end of the file (an
|
|
37
|
+
* unclosed string after every 'trailer', say); the budget turns that quadratic walk into a failure after a bounded
|
|
38
|
+
* amount of work. A well-formed file walks about its own length.
|
|
39
|
+
*/
|
|
40
|
+
export declare class Meter {
|
|
41
|
+
limit: number;
|
|
42
|
+
private readonly deadline?;
|
|
43
|
+
used: number;
|
|
44
|
+
/** Set once the budget has run out. */
|
|
45
|
+
exhausted: boolean;
|
|
46
|
+
constructor(limit: number, deadline?: number | undefined);
|
|
47
|
+
spend(n: number): void;
|
|
48
|
+
check(): void;
|
|
49
|
+
}
|
|
50
|
+
export declare class PdfReader {
|
|
51
|
+
private readonly b;
|
|
52
|
+
private readonly xref;
|
|
53
|
+
private trailer;
|
|
54
|
+
private decryptor;
|
|
55
|
+
private encryptRef;
|
|
56
|
+
private readonly cache;
|
|
57
|
+
private readonly objStmCache;
|
|
58
|
+
private readonly resolving;
|
|
59
|
+
/** The offset the final startxref gives, whether or not a cross-reference section is there; null without one. */
|
|
60
|
+
private lastXref;
|
|
61
|
+
private wasRebuilt;
|
|
62
|
+
private readonly meter;
|
|
63
|
+
/** Bytes the stream filters may still produce, over all streams this reader decodes. */
|
|
64
|
+
private decodeBudget;
|
|
65
|
+
/** Set when a stream or the XFA as a whole was refused for its decoded size. */
|
|
66
|
+
oversize: boolean;
|
|
67
|
+
/** True once reading this file has spent its parsing budget: a damaged or crafted structure. */
|
|
68
|
+
get overBudget(): boolean;
|
|
69
|
+
private constructor();
|
|
70
|
+
/** A trailer entry as written, not decrypted. */
|
|
71
|
+
trailerEntry(key: string): PObj | undefined;
|
|
72
|
+
/** The offset a new cross-reference section's /Prev points at. */
|
|
73
|
+
get previousXref(): number | null;
|
|
74
|
+
/** True when the cross-references could not be read and the object table was rebuilt by scanning. */
|
|
75
|
+
get rebuilt(): boolean;
|
|
76
|
+
/** Every object written at an offset: number, offset and generation. Null when some object is in an object stream. */
|
|
77
|
+
offsetEntries(): [number, number, number][] | null;
|
|
78
|
+
/** The highest object number in use plus one. */
|
|
79
|
+
get size(): number;
|
|
80
|
+
/** The generation of an object written at an offset; 0 for one in an object stream or not in the table. */
|
|
81
|
+
generation(n: number): number;
|
|
82
|
+
/** The decryptor once a password has opened the file; null when the file is not encrypted. */
|
|
83
|
+
get security(): Decryptor | null;
|
|
84
|
+
/** Opens the file; null when it cannot be parsed or no password decrypts it. */
|
|
85
|
+
static open(bytes: Uint8Array, passwords: readonly string[], deadline?: number): PdfReader | null;
|
|
86
|
+
/** open, also telling whether the file spent the parsing budget (whether or not it opened). */
|
|
87
|
+
static attempt(bytes: Uint8Array, passwords: readonly string[], deadline?: number): {
|
|
88
|
+
reader: PdfReader | null;
|
|
89
|
+
overBudget: boolean;
|
|
90
|
+
};
|
|
91
|
+
private init;
|
|
92
|
+
private encryptInfo;
|
|
93
|
+
private readXrefChain;
|
|
94
|
+
private setEntry;
|
|
95
|
+
private readXrefAt;
|
|
96
|
+
/** Rebuilds the object table by scanning for "N G obj", later definitions winning, and finds a trailer. */
|
|
97
|
+
private rebuild;
|
|
98
|
+
private objectOrNull;
|
|
99
|
+
private parseObjectAt;
|
|
100
|
+
/** indexOf on the file, charged to the parsing budget for the bytes it passes over. */
|
|
101
|
+
private find;
|
|
102
|
+
private isXrefStream;
|
|
103
|
+
private decryptValue;
|
|
104
|
+
private objStm;
|
|
105
|
+
object(n: number): PObj;
|
|
106
|
+
resolve(v: PObj | undefined): PObj;
|
|
107
|
+
get root(): PObj;
|
|
108
|
+
/** Decodes a stream through its filters; null when a filter is not supported. */
|
|
109
|
+
decodeStream(s: PStream, _objNum: number | null): Uint8Array | null;
|
|
110
|
+
private runFilters;
|
|
111
|
+
}
|
|
112
|
+
/** The XFA packets pdf.js would read, by its names: xdp:xdp, template, datasets, config, localeSet and so on. */
|
|
113
|
+
export type XfaPackets = Map<string, string>;
|
|
114
|
+
export interface XfaSource {
|
|
115
|
+
packets: XfaPackets;
|
|
116
|
+
/** Names in the catalog's /Names /XFAImages tree, where pdf.js finds images a template names by href. */
|
|
117
|
+
imageNames: Set<string>;
|
|
118
|
+
}
|
|
119
|
+
/** Reads the XFA packets and the XFAImages names. Null when there is no XFA or it cannot be read. */
|
|
120
|
+
export declare function readXfa(bytes: Uint8Array, passwords: readonly string[], deadline?: number): XfaSource | null;
|
|
121
|
+
/**
|
|
122
|
+
* The password that opens the file, in the prepared form that worked (a revision 6 password after SASLprep, say);
|
|
123
|
+
* null when the file is not encrypted, cannot be read, or no password opens it.
|
|
124
|
+
*/
|
|
125
|
+
export declare function openingPassword(bytes: Uint8Array, passwords: readonly string[], deadline?: number): string | null;
|
|
126
|
+
/**
|
|
127
|
+
* What flattenXfa checks before pdf.js sees the file: the password that opens it (see openingPassword), and whether
|
|
128
|
+
* its XFA decodes past XFA_LIMIT (or a stream it needs past DECODE_LIMIT).
|
|
129
|
+
*/
|
|
130
|
+
export declare function inspectPdf(bytes: Uint8Array, passwords: readonly string[], deadline?: number): {
|
|
131
|
+
password: string | null;
|
|
132
|
+
oversize: boolean;
|
|
133
|
+
overBudget: boolean;
|
|
134
|
+
};
|
|
135
|
+
/** Reads the /AcroForm /XFA streams the way pdf.js picks them. Null when there is no XFA or it cannot be read. */
|
|
136
|
+
export declare function readXfaPackets(bytes: Uint8Array, passwords: readonly string[]): XfaPackets | null;
|
|
137
|
+
/** The packets of an open reader; see readXfaPackets. */
|
|
138
|
+
export declare function readPackets(r: PdfReader): XfaPackets | null;
|
|
139
|
+
/** All packets joined in pdf.js's order into one XDP document. */
|
|
140
|
+
export declare function joinPackets(p: XfaPackets): string;
|