Class: Origami::Name

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



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

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:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/origami/name.rb', line 99

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

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

            unless digits =~ /^[A-Za-z0-9]{2}$/
                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:



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

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:



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

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

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

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

    name.file_offset = offset

    name
end

Instance Method Details

#<=>(name) ⇒ Object



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

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

    self.value <=> name.value
end

#==(object) ⇒ Object

:nodoc:



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

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

#eql?(object) ⇒ Boolean

:nodoc:

Returns:



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

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

#hashObject

:nodoc:



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

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_s(eol: $/) ⇒ Object

:nodoc:



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

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

#valueObject Also known as: to_sym



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

def value
    @value.to_sym
end