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, #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.


237
238
239
240
241
242
243
# File 'lib/origami/string.rb', line 237

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

    super(str)
end

Class Method Details

.parse(stream, _parser = nil) ⇒ Object

:nodoc:


245
246
247
248
249
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 245

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

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

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

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

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

    unless stream.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 = self.new(result)
    bytestr.file_offset = offset

    bytestr
end

Instance Method Details

#expandObject

:nodoc:


317
318
319
320
321
322
323
324
325
# File 'lib/origami/string.rb', line 317

def expand #:nodoc:
    extended = self.gsub("\\", "\\\\\\\\")
    extended.gsub!(/\)/, "\\)")
    extended.gsub!("\n", "\\n")
    extended.gsub!("\r", "\\r")
    extended.gsub!(/\(/, "\\(")

    extended
end

#to_hexObject

Converts self to HexaString


334
335
336
# File 'lib/origami/string.rb', line 334

def to_hex
    HexaString.new(self.value)
end

#to_sObject Also known as: to_obfuscated_str

:nodoc:


327
328
329
# File 'lib/origami/string.rb', line 327

def to_s #:nodoc:
    super(TOKENS.first + self.expand + TOKENS.last)
end

#valueObject

Returns a standard String representation.


341
342
343
344
345
# File 'lib/origami/string.rb', line 341

def value
    self.decrypt! if self.is_a?(Encryption::EncryptedString) and not @decrypted

    to_str
end