Class: Origami::ByteString

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

Overview

Class representing an ASCII 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

included, #infer_encoding, native_type, #to_pdfdoc, #to_utf16be, #to_utf8

Methods included from Object

#<=>, #cast_to, #copy, #export, #indirect_parent, #is_indirect?, #logicalize, #logicalize!, native_type, #native_type, #pdf, #pdf_version_required, #post_build, #pre_build, #reference, #resolve_all_references, #set_indirect, #set_pdf, #size, skip_until_next_obj, #solve, #to_o, #type, typeof, #xrefs

Methods inherited from String

#is_binary_data?, #to_o

Constructor Details

#initialize(str = "") ⇒ ByteString

Creates a new PDF String.

str

The string value.



266
267
268
269
270
271
272
273
# File 'lib/origami/string.rb', line 266

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:



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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/origami/string.rb', line 275

def self.parse(stream, parser = nil) #:nodoc:

  offset = stream.pos
  
  if not stream.skip(@@regexp_open)
    raise InvalidByteStringObjectError, "No literal string start token found"
  end
  
  result = ""
  depth = 0
  while depth != 0 or stream.peek(1) != TOKENS.last do

    if stream.eos?
      raise InvalidByteStringObjectError, "Non-terminated string"
    end

    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"
          if str.peek(1) == "\n" then stream.pos += 1 end
        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

  if not stream.skip(@@regexp_close)
    raise InvalidByteStringObjectError, "Byte string shall be terminated with '#{TOKENS.last}'"
  end

  bytestr = ByteString.new(result)
  bytestr.file_offset

  bytestr
end

Instance Method Details

#expandObject

:nodoc:



344
345
346
347
348
349
350
351
352
353
# File 'lib/origami/string.rb', line 344

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



362
363
364
# File 'lib/origami/string.rb', line 362

def to_hex
  HexaString.new(self.value)
end

#to_obfuscated_strObject



173
174
175
# File 'lib/origami/obfuscation.rb', line 173

def to_obfuscated_str
  to_s
end

#to_sObject

:nodoc:



355
356
357
# File 'lib/origami/string.rb', line 355

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

#valueObject



366
367
368
369
370
# File 'lib/origami/string.rb', line 366

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

  to_str
end