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

#infer_encoding, #real_type, #to_pdfdoc, #to_utf16be, #to_utf8

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

Methods inherited from String

#is_binary_data?, #to_o

Constructor Details

#initialize(str = "") ⇒ ByteString

Creates a new PDF String.

str

The string value.



253
254
255
256
257
258
259
260
# File 'lib/origami/string.rb', line 253

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) ⇒ Object

:nodoc:



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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/origami/string.rb', line 262

def self.parse(stream) #: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:



331
332
333
334
335
336
337
338
339
340
# File 'lib/origami/string.rb', line 331

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



349
350
351
# File 'lib/origami/string.rb', line 349

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:



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

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

#valueObject



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

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

  to_str
end