Class: Pdfrb::Source::ObjectReader

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/source/object_reader.rb

Overview

Resolves Model::Reference values to Model::Object instances by consulting the document's XrefSection. In-use entries seek and parse the indirect object at the recorded offset. Compressed entries defer to ObjectStreamReader.

Caches resolved objects per document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, xref) ⇒ ObjectReader

Returns a new instance of ObjectReader.



14
15
16
17
18
19
# File 'lib/pdfrb/source/object_reader.rb', line 14

def initialize(document, xref)
  @document = document
  @xref = xref
  @cache = {}
  @objstm_cache = {}
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



12
13
14
# File 'lib/pdfrb/source/object_reader.rb', line 12

def document
  @document
end

#xrefObject (readonly)

Returns the value of attribute xref.



12
13
14
# File 'lib/pdfrb/source/object_reader.rb', line 12

def xref
  @xref
end

Instance Method Details

#clear_cacheObject



63
64
65
66
# File 'lib/pdfrb/source/object_reader.rb', line 63

def clear_cache
  @cache.clear
  @objstm_cache.clear
end

#decrypt_loaded(obj, gen) ⇒ Object

The single funnel for read-side decryption (s7.6.1): strings in the object's dictionaries and the stream payload decrypt with the per-object key as the object enters the Model. Objects inside object streams need no separate treatment — only the containing ObjStm stream is encrypted — and the /Encrypt dictionary is exempt (s7.6.3).



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdfrb/source/object_reader.rb', line 45

def decrypt_loaded(obj, gen)
  return obj if obj.nil?
  # The exemption check must precede handler construction:
  # building the handler resolves the /Encrypt dict through
  # this same funnel, which would recurse before the memo is
  # set.
  return obj if Pdfrb::Encryption.exempt_object?(document, obj)

  handler = document.encryption.reader_handler
  return obj if handler.nil?

  Pdfrb::Encryption::ValueStrings.decrypt!(obj.value, obj.oid, gen, handler)
  if obj.is_a?(Pdfrb::Model::Cos::Stream) && obj.stream
    obj.stream = handler.decrypt_stream(obj.stream, obj.oid, gen)
  end
  obj
end

#load(reference) ⇒ Object



21
22
23
# File 'lib/pdfrb/source/object_reader.rb', line 21

def load(reference)
  load_oid(reference.oid)
end

#load_oid(oid) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdfrb/source/object_reader.rb', line 25

def load_oid(oid)
  return @cache[oid] if @cache.key?(oid)

  entry = xref[oid]
  return nil if entry.nil? || entry.free?

  obj = case entry.type
        when :in_use then decrypt_loaded(parse_at(entry.offset, oid, entry.gen), entry.gen)
        when :compressed then load_from_objstm(entry.obj_stm_oid, entry.index, oid)
        end
  @cache[oid] = obj
  obj
end