Class: Origami::LiteralString

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

Overview

Class representing a literal String Object.

Direct Known Subclasses

Date

Constant Summary collapse

TOKENS =

:nodoc:

%w[( )]
@@regexp_open =
Regexp.new(WHITESPACES + Regexp.escape(TOKENS.first))
@@regexp_close =
Regexp.new(Regexp.escape(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 = "") ⇒ LiteralString

Creates a new PDF String.

str

The string value.



242
243
244
245
246
247
248
# File 'lib/origami/string.rb', line 242

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:



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/origami/string.rb', line 250

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

  unless scanner.skip(@@regexp_open)
    raise InvalidLiteralStringObjectError, "No literal string start token found"
  end

  result = +""
  depth = 0
  while (depth != 0) || (scanner.peek(1) != TOKENS.last)
    raise InvalidLiteralStringObjectError, "Non-terminated string" if scanner.eos?

    c = scanner.get_byte
    case c
    when "\\"
      if scanner.match?(/\d{1,3}/)
        oct = scanner.peek(3).oct.chr
        scanner.pos += 3
        result << oct
      elsif scanner.match?(/((\r?\n)|(\r\n?))/)
        scanner.skip(/((\r?\n)|(\r\n?))/)
        next
      else
        flag = scanner.get_byte
        result << case flag
        when "n" then "\n"
        when "r" then "\r"
        when "t" then "\t"
        when "b" then "\b"
        when "f" then "\f"
        else
          flag
        end
      end

    when "("
      depth += 1
      result << c
    when ")"
      depth -= 1
      result << c
    else
      result << c
    end
  end

  unless scanner.skip(@@regexp_close)
    raise InvalidLiteralStringObjectError, "Byte string shall be terminated with '#{TOKENS.last}'"
  end

  # Try to cast as a Date object if possible.
  if result[0, 2] == 'D:'
    begin
      date = Date.parse(result)
      date.file_offset = offset
      return date
    rescue InvalidDateError
    end
  end

  bytestr = new(result)
  bytestr.file_offset = offset

  bytestr
end

Instance Method Details

#to_hexObject

Converts self to HexaString



324
325
326
# File 'lib/origami/string.rb', line 324

def to_hex
  HexaString.new(value)
end

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

:nodoc:



317
318
319
# File 'lib/origami/string.rb', line 317

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

#valueObject

Returns a standard String representation.



331
332
333
334
335
# File 'lib/origami/string.rb', line 331

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

  to_str
end