File size: 18,624 Bytes
4c9c5e7
 
 
da2d458
 
 
4c9c5e7
 
429e275
 
 
 
 
 
 
 
 
 
 
 
 
 
4c9c5e7
 
 
 
 
 
 
 
 
 
 
 
da2d458
4c9c5e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da2d458
4c9c5e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3944cfe
 
 
 
 
 
 
 
 
 
 
 
4c9c5e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da2d458
4c9c5e7
 
 
3944cfe
 
4c9c5e7
 
 
 
 
 
 
 
 
 
 
551ecc9
da2d458
 
551ecc9
 
da2d458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3944cfe
 
 
 
 
 
 
 
 
 
da2d458
 
 
 
 
4c9c5e7
 
 
 
 
 
 
 
 
da2d458
4c9c5e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3944cfe
4c9c5e7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Bounded scheduling hints, not PDF parsing or page identities.
export const PDF_ADAPTIVE_CHUNK_BYTES = 16384;
export const PDF_DEFAULT_CHUNK_BYTES = 262144;
export const PDF_LARGE_FLAT_CHUNK_BYTES = 32768;
const PDF_LARGE_FLAT_HEADER_BYTES = 65536;
const PDF_LARGE_FLAT_DICTIONARY_BYTES = 15000;
const PDF_MIN_SPARSE_PAGES = 128;

/** @param {Uint8Array} bytes @param {number} tailBegin @param {number} chunkSize */
export function adjacentXrefChunkStart(bytes, tailBegin, chunkSize) {
  // Only the final complete trailer can suggest reading the one missing
  // adjacent chunk. PDF.js still locates and parses the cross-reference table.
  if (chunkSize !== PDF_DEFAULT_CHUNK_BYTES || tailBegin < 2 * chunkSize ||
      !(bytes instanceof Uint8Array) || bytes.length < 16) return 0;
  const eof = new TextDecoder("latin1").decode(bytes.subarray(-2048));
  const match = /(?:^|[\r\n])[\t ]*startxref[\t\r\n ]+(\d{1,15})[\t\r\n ]+%%EOF[\t\r\n ]*$/.exec(eof);
  if (!match) return 0;
  const at = Number(match[1]);
  const begin = tailBegin - chunkSize;
  return at >= begin && at < tailBegin ? begin : 0;
}

/** @param {Uint8Array} bytes @param {number} total @param {number} chunkSize */
export function firstPageSectionEnd(bytes, total, chunkSize) {
  // A bounded scheduling hint, not a PDF parser. Only a direct, internally
  // consistent first-object linearization dictionary can justify first-page
  // lookahead. PDF.js remains the authority for all content and rendering.
  // Keep this section disjoint from the already scheduled two-chunk tail.
  if (total <= chunkSize * 6) return 0;
  const header = new TextDecoder("latin1").decode(bytes.subarray(0, 4096));
  const object = /** @type {(RegExpExecArray & [string, string]) | null} */ (/\b\d+\s+\d+\s+obj\s*<<([^]*?)>>/.exec(header));
  if (!object || object.index > 1024 || !/\/Linearized\s+1(?:\.0+)?(?=[\s/>]|$)/.test(object[1])) return 0;
  /** @param {string} name */
  const integer = name => {
    const match = new RegExp("/" + name + "\\s+(\\d+)(?=[\\s/\\[\\]<>]|$)").exec(object[1]);
    if (!match || /^\s+\d+\s+R\b/.test(object[1].slice(match.index + match[0].length))) return NaN;
    return Number(match[1]);
  };
  const length = integer("L"), end = integer("E"), firstPage = /\/P\s/.test(object[1]) ? integer("P") : 0;
  if (length !== total || firstPage !== 0 || !Number.isSafeInteger(end) || end <= chunkSize ||
      end >= total || end > chunkSize * 4) return 0;
  return Math.min(total, Math.ceil(end / chunkSize) * chunkSize);
}

// A separately downloaded, bounded tail hint can overlap PDF.js's normal
// two-chunk trailer read. It is only a transport hint: no PDF object, page
// count or identity is derived from these bytes. Restrict it to the rare
// large-parent, page-first layout recognized below and to fresh page-one opens.
/** @param {unknown} bytes @param {number} total @param {number} chunkSize */
export function firstPageTailHintStart(bytes, total, chunkSize) {
  if (!(bytes instanceof Uint8Array) || total <= chunkSize * 8 ||
      !isFirstPageCandidate(bytes,total,chunkSize)) return 0;
  const head = new TextDecoder("latin1").decode(bytes.subarray(0,4096));
  const first = /** @type {(RegExpExecArray & [string, string]) | null} */ (/(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<<([^]*?)>>/.exec(head));
  const parent = first && /\/Parent\s+(\d+)\s+\d+\s+R(?=[\s/>]|$)/.exec(first[1]);
  if (!first || !/\/Type\s*\/Page(?=[\s/>]|$)/.test(first[1]) || !parent ||
      !Number.isSafeInteger(Number(parent[1])) || Number(parent[1]) < 2048) return 0;
  const tail = Math.floor((total - 1) / chunkSize) * chunkSize - chunkSize;
  return Math.max(chunkSize,tail - chunkSize * 3);
}

// A bounded read-ahead hint only: recognize the first direct image stream in
// the validated opening bytes. It must be complete as a dictionary before
// stream data begins, and its direct length must fit in three more bootstrap
// chunks. PDF.js still resolves the current catalog/page tree and consumes
// original bytes; this never supplies a page identity, total, or decoded image.
/** @param {unknown} bytes @param {number} total @param {number} chunkSize */
export function firstImageSectionEnd(bytes, total, chunkSize) {
  if (!(bytes instanceof Uint8Array) || total <= chunkSize * 6) return 0;
  const head = new TextDecoder("latin1").decode(bytes.subarray(0, Math.min(bytes.length, PDF_LARGE_FLAT_HEADER_BYTES)));
  if (!head.startsWith("%PDF-") || /\/Linearized\b/.test(head.slice(0,1024))) return 0;
  const header = /(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<</g;
  let object, scanned = 0, image = null, firstPage = null;
  while ((object = header.exec(head)) && scanned++ < 64) {
    const endObject = head.indexOf("endobj",header.lastIndex), stream = /\bstream(?:\r\n|\r|\n)/g;
    stream.lastIndex = header.lastIndex;
    const match = stream.exec(head);
    if (!match || (endObject >= 0 && endObject < match.index)) {
      if (endObject < 0) return 0;
      const body = head.slice(object.index,endObject);
      if (!firstPage && /\/Type\s*\/Page(?=[\s/>]|$)/.test(body)) firstPage = body;
      header.lastIndex = endObject + 6;
      continue;
    }
    const dictionary = head.slice(object.index,match.index);
    if (!/>>\s*$/.test(dictionary)) return 0;
    const lengths = [...dictionary.matchAll(/\/Length\s+(\d+)(?!\s+\d+\s+R)(?=[\s/>]|$)/g)];
    if (lengths.length !== 1) return 0;
    const length = Number(/** @type {RegExpMatchArray} */ (lengths[0])[1]), streamEnd = match.index + match[0].length + length;
    if (!Number.isSafeInteger(length) || length < 1 || !Number.isSafeInteger(streamEnd)) return 0;
    if (/\/Type\s*\/XObject(?=[\s/>]|$)/.test(dictionary) && /\/Subtype\s*\/Image(?=[\s/>]|$)/.test(dictionary)) {
      if (streamEnd <= chunkSize || streamEnd > chunkSize * 4 || streamEnd >= total - chunkSize * 2) return 0;
      const id = /** @type {(RegExpExecArray & [string, string, string]) | null} */ (/(?:^|[\r\n])\s*(\d+)\s+(\d+)\s+obj\s*<</.exec(object[0]));
      if (!id || id[1].length > 15 || id[2].length > 15 ||
          !Number.isSafeInteger(Number(id[1])) || !Number.isSafeInteger(Number(id[2]))) return 0;
      image = {number:id[1],generation:id[2],end:Math.ceil(streamEnd / chunkSize) * chunkSize};
      break;
    }
    // Only cross a previous stream when its direct length and terminators fit
    // completely in the bounded header; never scan binary data for objects.
    if (streamEnd >= head.length || !/^\s*endstream\b/.test(head.slice(streamEnd))) return 0;
    const afterStream = head.indexOf("endobj",streamEnd);
    if (afterStream < 0) return 0;
    header.lastIndex = afterStream + 6;
  }
  if (!image) return 0;
  // Do not speculate on an unrelated image. In the already received, bounded
  // prefix, require a page resource dictionary to name this exact image ref.
  // This is still only a hint; PDF.js resolves the real page independently.
  const safe = head.slice(0,/** @type {RegExpExecArray} */ (object).index), resources = firstPage && /** @type {(RegExpExecArray & [string, string, string]) | null} */ (/\/Resources\s+(\d+)\s+(\d+)\s+R(?=[\s/>]|$)/.exec(firstPage));
  if (!resources || resources[1].length > 15 || resources[2].length > 15 ||
      !Number.isSafeInteger(Number(resources[1])) || !Number.isSafeInteger(Number(resources[2]))) return 0;
  const resourceHeader = new RegExp(String.raw`(?:^|[\r\n])\s*${resources[1]}\s+${resources[2]}\s+obj\s*<<`,"g");
  const matches = [...safe.matchAll(resourceHeader)];
  if (matches.length !== 1) return 0;
  const resourceEnd = safe.indexOf("endobj",/** @type {RegExpMatchArray} */ (matches[0]).index);
  if (resourceEnd < 0) return 0;
  const resourceBody = safe.slice(/** @type {RegExpMatchArray} */ (matches[0]).index,resourceEnd);
  const ref = new RegExp(String.raw`/[^\s/<>\[\]()]+\s+${image.number}\s+${image.generation}\s+R(?=[\s/>\]]|$)`);
  if (!/\/XObject(?=[\s/<]|$)/.test(resourceBody) || !ref.test(resourceBody)) return 0;
  return image.end;
}

// Extract only conventional direct page references; malformed arrays are not hints.
/** @param {string} text @param {number} minRefs @param {number} maxRefs */
function pageTreeRefs(text, minRefs, maxRefs) {
  const count = /\/Count\s+(\d+)(?!\s+\d+\s+R)(?=[\s/>]|$)/.exec(text);
  const kids = /** @type {(RegExpExecArray & [string, string]) | null} */ (/\/Kids\s*\[([^\]]*)\]/.exec(text));
  if (!count || !kids || Number(count[1]) < PDF_MIN_SPARSE_PAGES) return null;
  const refs = kids[1].match(/\b\d+\s+\d+\s+R\b/g) || [];
  if (refs.length < minRefs || refs.length > maxRefs || refs.length > Number(count[1]) ||
      kids[1].replace(/\b\d+\s+\d+\s+R\b/g, '').trim() !== '') return null;
  return refs;
}

// This is a scheduling hint, not a PDF parser or a source of page identities.
// For a directly declared large flat /Pages node, PDF.js still validates the
// entire page tree, but its dispersed metadata is cheaper to read in smaller
// chunks. Ambiguous, indirect or compressed structures keep the normal path.
/** @param {unknown} bytes @param {number} total @param {number} fallback */
export function choosePdfRangeChunkSize(bytes, total, fallback = PDF_DEFAULT_CHUNK_BYTES) {
  if (!(bytes instanceof Uint8Array) || !Number.isSafeInteger(total) || total < 8 * 1024 * 1024 ||
      !Number.isSafeInteger(fallback) || fallback !== PDF_DEFAULT_CHUNK_BYTES) return fallback;
  const head = new TextDecoder('latin1').decode(bytes.subarray(0, PDF_ADAPTIVE_CHUNK_BYTES));
  if (!head.startsWith('%PDF-') || /\/Linearized\b/.test(head.slice(0, 1024))) return fallback;
  const objectHeader = /(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<</g;
  const stream = /\bstream(?:\r\n|\r|\n)/.exec(head);
  // Never match a fake object inside a preceding stream. If an object is
  // incomplete in the bounded prefix, stay on the standard range strategy.
  const stop = stream ? stream.index : head.length;
  let object;
  while ((object = objectHeader.exec(head)) && object.index < stop) {
    const end = head.indexOf('endobj', objectHeader.lastIndex);
    if (end < 0 || end > stop) break;
    const text = head.slice(object.index, end);
    objectHeader.lastIndex = end + 6;
    if (!/\/Type\s*\/Pages(?=[\s/>]|$)/.test(text)) continue;
    const refs = pageTreeRefs(text, PDF_MIN_SPARSE_PAGES, 4096);
    if (!refs) continue;
    const numbers = refs.map(ref => Number.parseInt(ref, 10));
    if (!numbers.every(Number.isSafeInteger)) continue;
    let ordered = true;
    for (let i = 1; i < numbers.length; i++)
      if (/** @type {number} */ (numbers[i]) <= /** @type {number} */ (numbers[i - 1])) { ordered = false; break; }
    if (!ordered) continue;
    // Sequential page objects are typically clustered even in flat trees;
    // leave those PDFs on the normal larger chunks. This deliberately misses
    // some edge cases rather than penalizing ordinary books.
    if (/** @type {number} */ (numbers[numbers.length - 1]) - /** @type {number} */ (numbers[0]) >= refs.length * 2) return PDF_ADAPTIVE_CHUNK_BYTES;
  }
  return chooseLargeFlatTreeChunkSize(bytes, fallback);
}

/** @param {Uint8Array} bytes @param {number} fallback */
function chooseLargeFlatTreeChunkSize(bytes, fallback) {
  const head = new TextDecoder('latin1').decode(bytes.subarray(0, Math.min(bytes.length, PDF_LARGE_FLAT_HEADER_BYTES)));
  if (!head.startsWith('%PDF-') || /\/Linearized\b/.test(head.slice(0, 1024))) return fallback;
  // Walk only complete direct-length objects. Skip stream bytes exactly; this
  // remains a scheduling hint and PDF.js validates every real page object.
  let cursor = head.indexOf('\n') + 1;
  const line = /(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<</g;
  for (let scanned = 0; scanned < 64; scanned++) {
    line.lastIndex = Math.max(0, cursor - 1);
    const object = line.exec(head);
    if (!object || line.lastIndex - object.index > 4096) return fallback;
    const gap = object.index < cursor ? '' : head.slice(cursor, object.index);
    if (gap.replace(/%[^\r\n]*/g, '').trim()) return fallback;
    let pos = line.lastIndex, depth = 1;
    while (pos < head.length && depth) {
      if (head.startsWith('<<', pos)) { depth++; pos += 2; continue; }
      if (head.startsWith('>>', pos)) { depth--; pos += 2; continue; }
      if (head[pos] === '(') {
        let nested = 1; pos++;
        while (pos < head.length && nested) {
          if (head[pos] === '\\') { pos += 2; continue; }
          if (head[pos] === '(') nested++;
          else if (head[pos] === ')') nested--;
          pos++;
        }
        if (nested) return fallback;
        continue;
      }
      if (head[pos] === '<') {
        const close = head.indexOf('>', pos + 1);
        if (close < 0) return fallback;
        pos = close + 1; continue;
      }
      if (head[pos] === '%') {
        const close = head.indexOf('\n', pos + 1);
        if (close < 0) return fallback;
        pos = close + 1; continue;
      }
      pos++;
    }
    if (depth || pos - line.lastIndex > PDF_LARGE_FLAT_DICTIONARY_BYTES) return fallback;
    const dictionary = head.slice(line.lastIndex, pos - 2);
    // Avoid interpreting strings, comments or nested dictionaries as a direct
    // Length/Pages field. The original detector handles its own old cases.
    const simpleDictionary = /^[^()%<>]*$/.test(dictionary);
    const suffix = head.slice(pos);
    const stream = /^\s*stream(?:\r\n|\r|\n)/.exec(suffix);
    if (stream) {
      // Arrays/dictionaries may contain a misleading nested /Length token.
      // Only a flat scalar stream dictionary can justify skipping bytes.
      if (!simpleDictionary || /[\[\]{}]/.test(dictionary)) return fallback;
      const lengths = [...dictionary.matchAll(/\/Length\s+(\d+)(?!\s+\d+\s+R)(?=[\s/>]|$)/g)];
      if (lengths.length !== 1 || (dictionary.match(/\/Length(?=[\s/>]|$)/g) || []).length !== 1) return fallback;
      const length = Number(/** @type {RegExpMatchArray} */ (lengths[0])[1]);
      const end = pos + stream[0].length + length;
      if (!Number.isSafeInteger(length) || !Number.isSafeInteger(end) || end > head.length) return fallback;
      const closing = /^\s*endstream\s*endobj(?=[\s]|$)/.exec(head.slice(end));
      if (!closing) return fallback;
      cursor = end + closing[0].length;
      continue;
    }
    const closing = /^\s*endobj(?=[\s]|$)/.exec(suffix);
    if (!closing) return fallback;
    // No arrays other than the single direct Kids array may contribute
    // tokens to the page-tree proof.
    if (simpleDictionary && /\/Type\s*\/Pages(?=[\s/>]|$)/.test(dictionary)) {
      if (["Type", "Count", "Kids"].some(name =>
        (dictionary.match(new RegExp("/" + name + "(?=[\\s/\\[\\]<>]|$)", "g")) || []).length !== 1)) return fallback;
      const arrays = [...dictionary.matchAll(/\[[^\[\]]*\]/g)];
      const directKidsOnly = arrays.length === 1 &&
        /\/Kids\s*\[[^\[\]]*\]/.test(dictionary) &&
        !/[\[\]{}]/.test(dictionary.replace(/\[[^\[\]]*\]/g, ''));
      if (!directKidsOnly) return fallback;
      const refs = pageTreeRefs(dictionary, PDF_MIN_SPARSE_PAGES, 4096);
      if (refs) {
        const numbers = refs.map(ref => Number.parseInt(ref,10));
        const tail = numbers.slice(1);
        const ordered = tail.every((number,index) => Number.isSafeInteger(number) &&
          (!index || number > /** @type {number} */ (tail[index-1])));
        const firstNumber = /** @type {number} */ (numbers[0]);
        if (Number.isSafeInteger(firstNumber) && ordered && !tail.includes(firstNumber)) {
          const min = Math.min(...numbers), max = Math.max(...numbers);
          if (max - min >= refs.length * 2) return PDF_LARGE_FLAT_CHUNK_BYTES;
        }
      }
    }
    cursor = pos + closing[0].length;
  }
  return fallback;
}

// A scheduling hint only: PDF.js still parses and validates the original tree.
// Recognize a bounded large two-child root (cover plus remaining subtree),
// without inspecting stream contents or scanning the entire document.
/** @param {unknown} bytes @param {number} total @param {number} rangeChunkSize */
export function isFirstPageCandidate(bytes, total, rangeChunkSize) {
  if (!(bytes instanceof Uint8Array) || !Number.isSafeInteger(total) || total < 8 * 1024 * 1024) return false;
  if (rangeChunkSize === PDF_ADAPTIVE_CHUNK_BYTES || rangeChunkSize === PDF_LARGE_FLAT_CHUNK_BYTES) return true;
  const head = new TextDecoder("latin1").decode(bytes.subarray(0, PDF_ADAPTIVE_CHUNK_BYTES));
  if (!head.startsWith("%PDF-")) return false;
  const first = /** @type {(RegExpExecArray & [string, string]) | null} */ (/(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<<([^]*?)>>/.exec(head.slice(0, 4096)));
  const stream = /\bstream(?:\r\n|\r|\n)/.exec(head);
  if (first && /\/Linearized\s+1(?:\.0+)?(?=[\s/>]|$)/.test(first[1])) {
    const length = /\/L\s+(\d+)(?!\s+\d+\s+R)(?=[\s/>]|$)/.exec(first[1]);
    return !!length && Number.isSafeInteger(Number(length[1])) && Number(length[1]) > 0 && Number(length[1]) <= total;
  }
  // Some large PDFs begin with a page dictionary but put their edited page
  // tree at the end. An indirect parent number is only a hint that the
  // tree may be expensive; never use it to infer which page this object is,
  // or to publish a count. The staged worker still asks PDF.js for page one
  // and performs the same last-page validation and recovery as every other
  // staged candidate. An ambiguous or incomplete first object stays normal.
  if (first && first.index < 1024 && (!stream || stream.index > first.index + first[0].length) &&
      /\/Type\s*\/Page(?=[\s/>]|$)/.test(first[1])) {
    const parent = /\/Parent\s+(\d+)\s+(\d+)\s+R(?=[\s/>]|$)/.exec(first[1]);
    const end = head.indexOf("endobj", first.index + first[0].length);
    if (parent && Number.isSafeInteger(Number(parent[1])) && Number(parent[1]) >= 2048 &&
        Number.isSafeInteger(Number(parent[2])) && end >= 0 && end < 4096 &&
        (!stream || end < stream.index)) {
      const pageObject = head.slice(first.index,end);
      if (/>>\s*$/.test(pageObject) && /\/Contents(?=[\s/\[>]|$)/.test(pageObject) &&
          /\/Resources(?=[\s/<>]|$)/.test(pageObject)) return true;
    }
  }
  const stop = stream ? stream.index : head.length;
  const objects = /(?:^|[\r\n])\s*\d+\s+\d+\s+obj\s*<</g;
  let object;
  while ((object = objects.exec(head)) && object.index < stop) {
    const end = head.indexOf("endobj", objects.lastIndex);
    if (end < 0 || end > stop) return false;
    const text = head.slice(object.index, end);
    objects.lastIndex = end + 6;
    if (!/\/Type\s*\/Pages(?=[\s/>]|$)/.test(text)) continue;
    if (pageTreeRefs(text, 2, 2)) return true;
  }
  return false;
}