Class: Origami::Reference

Inherits:
Object
  • Object
show all
Includes:
Comparable, Object
Defined in:
lib/origami/reference.rb,
lib/origami/obfuscation.rb

Overview

Class representing a Reference Object. Reference are like symbolic links pointing to a particular object into the file.

Constant Summary collapse

TOKENS =

:nodoc:

[ "(?<no>\\d+)" + WHITESPACES +  "(?<gen>\\d+)" + WHITESPACES + "R" ]
REGEXP_TOKEN =
Regexp.new(TOKENS.first, Regexp::MULTILINE)
@@regexp =
Regexp.new(WHITESPACES + TOKENS.first + WHITESPACES)

Instance Attribute Summary collapse

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Object

#cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, #numbered?, #post_build, #pre_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #to_o, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(refno, refgen) ⇒ Reference

Returns a new instance of Reference.



40
41
42
43
44
# File 'lib/origami/reference.rb', line 40

def initialize(refno, refgen)
    super()

    @refno, @refgen = refno, refgen
end

Instance Attribute Details

#refgenObject

Returns the value of attribute refgen.



38
39
40
# File 'lib/origami/reference.rb', line 38

def refgen
  @refgen
end

#refnoObject

Returns the value of attribute refno.



38
39
40
# File 'lib/origami/reference.rb', line 38

def refno
  @refno
end

Class Method Details

.parse(stream, _parser = nil) ⇒ Object

:nodoc:



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

def self.parse(stream, _parser = nil) #:nodoc:
    scanner = Parser.init_scanner(stream)
    offset = scanner.pos

    if scanner.scan(@@regexp).nil?
        raise InvalidReferenceError, "Bad reference to indirect objet format"
    end

    no = scanner['no'].to_i
    gen = scanner['gen'].to_i

    ref = Reference.new(no, gen)
    ref.file_offset = offset

    ref
end

Instance Method Details

#<=>(ref) ⇒ Object

:nodoc



101
102
103
# File 'lib/origami/reference.rb', line 101

def <=>(ref) #:nodoc
    self.to_a <=> ref.to_a
end

#==(ref) ⇒ Object Also known as: eql?

Compares to Reference object.



108
109
110
111
112
# File 'lib/origami/reference.rb', line 108

def ==(ref)
    return false unless ref.is_a?(Reference)

    self.to_a == ref.to_a
end

#followObject Also known as: solve

Returns the object pointed to by the reference. The reference must be part of a document. Raises an InvalidReferenceError if the object cannot be found.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/origami/reference.rb', line 68

def follow
    doc = self.document

    if doc.nil?
        raise InvalidReferenceError, "Not attached to any document"
    end

    target = doc.get_object(self)

    if target.nil? and not Origami::OPTIONS[:ignore_bad_references]
        raise InvalidReferenceError, "Cannot resolve reference : #{self}"
    end

    target or Null.new
end

#hashObject

:nodoc:



97
98
99
# File 'lib/origami/reference.rb', line 97

def hash #:nodoc:
    self.to_a.hash
end

#to_aObject

Returns a Ruby array with the object number and the generation this reference is pointing to.



118
119
120
# File 'lib/origami/reference.rb', line 118

def to_a
    [@refno, @refgen]
end

#to_obfuscated_strObject



183
184
185
186
187
# File 'lib/origami/obfuscation.rb', line 183

def to_obfuscated_str
    refstr = refno.to_s + Obfuscator.junk_spaces + refgen.to_s + Obfuscator.junk_spaces + "R"

    super(refstr)
end

#to_s(eol: $/) ⇒ Object

:nodoc:



122
123
124
# File 'lib/origami/reference.rb', line 122

def to_s(eol: $/) #:nodoc:
    super("#{@refno} #{@refgen} R", eol: eol)
end

#valid?Boolean

Returns true if the reference points to an object.

Returns:



88
89
90
91
92
93
94
95
# File 'lib/origami/reference.rb', line 88

def valid?
    begin
        self.solve
        true
    rescue InvalidReferenceError
        false
    end
end

#valueObject

Returns the referenced object value.



129
130
131
# File 'lib/origami/reference.rb', line 129

def value
    self.solve.value
end