Class: Origami::Reference

Inherits:
Object
  • Object
show all
Includes:
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, #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.


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

def initialize(refno, refgen)
    super()

    @refno, @refgen = refno, refgen
end

Instance Attribute Details

#refgenObject

Returns the value of attribute refgen


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

def refgen
  @refgen
end

#refnoObject

Returns the value of attribute refno


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

def refno
  @refno
end

Class Method Details

.parse(stream, _parser = nil) ⇒ Object

:nodoc:


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

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

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

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

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

    ref
end

Instance Method Details

#<=>(ref) ⇒ Object

:nodoc


81
82
83
# File 'lib/origami/reference.rb', line 81

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

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

Compares to Reference object.


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

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

    self.to_a == ref.to_a
end

#hashObject

:nodoc:


77
78
79
# File 'lib/origami/reference.rb', line 77

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

#solveObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/origami/reference.rb', line 61

def solve
    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

#to_aObject

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


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

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_sObject

:nodoc:


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

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

#valueObject

Returns the referenced object value.


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

def value
    self.solve.value
end