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 + "(" + 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

#<=>, #copy, #indirect_parent, #is_indirect?, #pdf, #pdf_version_required, #post_build, #pre_build, #reference, #set_indirect, #set_pdf, #size, skip_until_next_obj, #solve, #to_o, #type, typeof, #xrefs

Constructor Details

#initialize(name = "") ⇒ Name

Creates a new Name.

name

A symbol representing the new Name value.



74
75
76
77
78
79
80
81
82
# File 'lib/origami/name.rb', line 74

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:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/origami/name.rb', line 128

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

  while i < name.length

    if name[i,1] == "#"
      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:



157
158
159
160
161
162
163
164
# File 'lib/origami/name.rb', line 157

def self.expand(name) #:nodoc:
  
  forbiddenchars = /[ #\t\r\n\0\[\]<>()%\/]/
  
  name.gsub(forbiddenchars) do |c|
    "#" + c[0].ord.to_s(16).rjust(2,"0")
  end
end

.parse(stream) ⇒ Object

:nodoc:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/origami/name.rb', line 110

def self.parse(stream) #:nodoc:

  offset = stream.pos
  
  name = 
    if stream.scan(@@regexp).nil?
      raise InvalidNameObjectError, "Bad name format"
    else
      value = stream[2]
      
      Name.new(value.include?('#') ? contract(value) : value)
    end

  name.file_offset = offset

  name
end

Instance Method Details

#==(object) ⇒ Object

:nodoc:



94
95
96
# File 'lib/origami/name.rb', line 94

def ==(object) #:nodoc:
  @value.to_sym == object
end

#eql?(object) ⇒ Boolean

:nodoc:

Returns:



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

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

#hashObject

:nodoc:



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

def hash #:nodoc:
  @value.hash
end

#real_typeObject



166
# File 'lib/origami/name.rb', line 166

def real_type ; Name end

#to_obfuscated_str(prop = 2) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/origami/obfuscation.rb', line 185

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[0].to_s(base=16)
      hexchar = "0" + hexchar if hexchar.length < 2
      
      '#' + hexchar
    else
      c
    end
  end

  super(TOKENS.first + name)
end

#to_sObject

:nodoc:



106
107
108
# File 'lib/origami/name.rb', line 106

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

#valueObject



85
86
87
# File 'lib/origami/name.rb', line 85

def value   
  ( @value.empty? ) ? EmptySymbol.new : @value.to_sym
end