Module: Pdfrb::Source::Recovery

Defined in:
lib/pdfrb/source/recovery.rb

Overview

Last-resort recovery: scan the entire file for \d+ \d+ obj patterns and synthesise an XrefSection from the offsets found. Triggered when the xref table is missing or points to a wrong offset.

Class Method Summary collapse

Class Method Details

.rebuild_xref(io, recover: true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdfrb/source/recovery.rb', line 15

def rebuild_xref(io, recover: true)
  return nil unless recover

  io.seek(0, IO::SEEK_SET)
  section = XrefSection.new
  data = io.read
  data.force_encoding(Encoding::BINARY)
  offset = 0
  while (m = data.match(OBJ_PATTERN, offset))
    oid = m[1].to_i
    gen = m[2].to_i
    entry_offset = m.begin(0)
    # Take the last-seen gen per oid (PDF spec: highest gen wins).
    section.add_in_use(oid, gen, entry_offset)
    offset = m.end(0)
  end
  section
end