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.



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

def initialize(name = "")
  unless name.is_a?(Symbol) || 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:



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

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 += 1
  end

  name
end

.expand(name) ⇒ Object

:nodoc:



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

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

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

#<=>(other) ⇒ Object



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

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

  value <=> other.value
end

#==(other) ⇒ Object

:nodoc:



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

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:



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

def eql?(other) # :nodoc:
  other.is_a?(Name) and value.eql?(other.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



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

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

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

  name.gsub!(/./) do |c|
    if (rand(prop) == 0) || 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:



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

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

#valueObject Also known as: to_sym



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

def value
  @value.to_sym
end