Class: Origami::Name

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

Overview

Class representing a Name Object. Name objects are strings which identify some PDF file inner structures.

Constant Summary collapse

TOKENS =

:nodoc:

%w{ / }
@@regexp =

:nodoc

Regexp.new(WHITESPACES + TOKENS.first + "(?<name>#{REGULARCHARS})" + WHITESPACES)

Instance Attribute Summary

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, #solve, #to_o, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(name = "") ⇒ Name

Creates a new Name.

name

A symbol representing the new Name value.


43
44
45
46
47
48
49
50
51
# File 'lib/origami/name.rb', line 43

def initialize(name = "")
    unless name.is_a?(Symbol) or name.is_a?(::String)
        raise TypeError, "Expected type Symbol or String, received #{name.class}."
    end

    @value = name.to_s

    super()
end

Class Method Details

.contract(name) ⇒ Object

:nodoc:


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
# File 'lib/origami/name.rb', line 97

def self.contract(name) #:nodoc:
    i = 0
    name = name.dup

    while i < name.length
        if name[i] == "#"
            digits = name[i+1, 2]

            unless /^[A-Za-z0-9]{2}$/ === digits
                raise InvalidNameObjectError, "Irregular use of # token"
            end

            char = digits.hex.chr

            if char == "\0"
                raise InvalidNameObjectError, "Null byte forbidden inside name definition"
            end

            name[i, 3] = char
        end

        i = i + 1
    end

    name
end

.expand(name) ⇒ Object

:nodoc:


124
125
126
127
128
129
130
# File 'lib/origami/name.rb', line 124

def self.expand(name) #:nodoc:
    forbiddenchars = /[ #\t\r\n\0\[\]<>()%\/]/

    name.gsub(forbiddenchars) do |c|
        "#" + c.ord.to_s(16).rjust(2,"0")
    end
end

.parse(stream, _parser = nil) ⇒ Object

:nodoc:


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/origami/name.rb', line 80

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

    name =
        if stream.scan(@@regexp).nil?
            raise InvalidNameObjectError, "Bad name format"
        else
            value = stream['name']

            Name.new(value.include?('#') ? contract(value) : value)
        end

    name.file_offset = offset

    name
end

Instance Method Details

#<=>(name) ⇒ Object


58
59
60
61
62
# File 'lib/origami/name.rb', line 58

def <=>(name)
    return unless name.is_a?(Name)

    self.value <=> name.value
end

#==(object) ⇒ Object

:nodoc:


64
65
66
# File 'lib/origami/name.rb', line 64

def ==(object) #:nodoc:
    self.eql?(object) or @value.to_sym == object
end

#eql?(object) ⇒ Boolean

:nodoc:

Returns:


68
69
70
# File 'lib/origami/name.rb', line 68

def eql?(object) #:nodoc:
    object.is_a?(Name) and self.value.eql?(object.value)
end

#hashObject

:nodoc:


72
73
74
# File 'lib/origami/name.rb', line 72

def hash #:nodoc:
    @value.hash
end

#to_obfuscated_str(prop = 2) ⇒ Object


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/origami/obfuscation.rb', line 199

def to_obfuscated_str(prop = 2)
    name = @value.dup

    forbiddenchars = [ " ","#","\t","\r","\n","\0","[","]","<",">","(",")","%","/","\\" ]

    name.gsub!(/./) do |c|
        if rand(prop) == 0 or forbiddenchars.include?(c)
            hexchar = c.ord.to_s(16)
            hexchar = "0" + hexchar if hexchar.length < 2

            '#' + hexchar
        else
            c
        end
    end

    super(TOKENS.first + name)
end

#to_sObject

:nodoc:


76
77
78
# File 'lib/origami/name.rb', line 76

def to_s #:nodoc:
    super(TOKENS.first + Name.expand(@value))
end

#valueObject Also known as: to_sym


53
54
55
# File 'lib/origami/name.rb', line 53

def value
    @value.to_sym
end