Class: Origami::HexaString

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

Overview

Class representing an hexadecimal-writen String Object.

Constant Summary collapse

TOKENS =

:nodoc:

%w[< >]
@@regexp_open =
Regexp.new(WHITESPACES + TOKENS.first)
@@regexp_close =
Regexp.new(TOKENS.last)

Instance Attribute Summary

Attributes included from String

#encoding

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from String

#detect_encoding, #to_pdfdoc, #to_utf16be, #to_utf8

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(str = "") ⇒ HexaString

Creates a new PDF hexadecimal String.

str

The string value.



172
173
174
175
176
177
178
# File 'lib/origami/string.rb', line 172

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

  super
end

Class Method Details

.parse(stream, _parser = nil) ⇒ Object

:nodoc:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/origami/string.rb', line 180

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

  if scanner.skip(@@regexp_open).nil?
    raise InvalidHexaStringObjectError, "Hexadecimal string shall start with a '#{TOKENS.first}' token"
  end

  hexa = scanner.scan_until(@@regexp_close)
  if hexa.nil?
    raise InvalidHexaStringObjectError, "Hexadecimal string shall end with a '#{TOKENS.last}' token"
  end

  begin
    hexacopy = +hexa.dup  # Create a mutable copy
    decoded = Filter::ASCIIHex.decode(hexacopy.chomp!(TOKENS.last))
  rescue Filter::InvalidASCIIHexStringError => e
    raise InvalidHexaStringObjectError, e.message
  end

  hexastr = HexaString.new(decoded)
  hexastr.file_offset = offset

  hexastr
end

Instance Method Details

#to_literalObject

Converts self to a literal String.



213
214
215
# File 'lib/origami/string.rb', line 213

def to_literal
  LiteralString.new(value)
end

#to_s(eol: $/) ⇒ Object Also known as: to_obfuscated_str

:nodoc:



206
207
208
# File 'lib/origami/string.rb', line 206

def to_s(eol: $/) # :nodoc:
  super(TOKENS.first + Filter::ASCIIHex.encode(to_str) + TOKENS.last, eol: eol)
end

#valueObject



217
218
219
220
221
# File 'lib/origami/string.rb', line 217

def value
  decrypt! if is_a?(Encryption::EncryptedString) && !@decrypted

  to_str
end