Class: Rex::Java::Serialization::Model::BlockData

Inherits:
Element
  • Object
show all
Defined in:
lib/rex/java/serialization/model/block_data.rb

Overview

This class provides a block data representation

Instance Attribute Summary collapse

Attributes inherited from Element

#stream

Instance Method Summary collapse

Methods inherited from Element

decode

Constructor Details

#initialize(stream = nil, contents = '') ⇒ BlockData

Returns a new instance of BlockData.

Parameters:



19
20
21
22
23
# File 'lib/rex/java/serialization/model/block_data.rb', line 19

def initialize(stream = nil, contents = '')
  super(stream)
  self.contents = contents
  self.length = contents.length
end

Instance Attribute Details

#contentsString

Returns the contents of the block.

Returns:

  • (String)

    the contents of the block



15
16
17
# File 'lib/rex/java/serialization/model/block_data.rb', line 15

def contents
  @contents
end

#lengthInteger

Returns the length of the block.

Returns:

  • (Integer)

    the length of the block



12
13
14
# File 'lib/rex/java/serialization/model/block_data.rb', line 12

def length
  @length
end

Instance Method Details

#decode(io) ⇒ self

Deserializes a Rex::Java::Serialization::Model::BlockData

Parameters:

  • io (IO)

    the io to read from

Returns:

  • (self)

    if deserialization succeeds

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rex/java/serialization/model/block_data.rb', line 30

def decode(io)
  raw_length = io.read(1)
  raise RuntimeError, 'Failed to unserialize BlockData' if raw_length.nil?
  self.length = raw_length.unpack('C')[0]

  if length == 0
    self.contents = ''
  else
    self.contents = io.read(length)
    if contents.nil? || contents.length != length
      raise RuntimeError, 'Failed to unserialize BlockData'
    end
  end

  self
end

#encodeString

Serializes the Rex::Java::Serialization::Model::BlockData

Returns:

  • (String)


60
61
62
63
64
65
# File 'lib/rex/java/serialization/model/block_data.rb', line 60

def encode
  encoded = [length].pack('C')
  encoded << contents

  encoded
end

#to_sString

Creates a print-friendly string representation

Returns:

  • (String)


50
51
52
53
54
55
# File 'lib/rex/java/serialization/model/block_data.rb', line 50

def to_s
  contents_hex = []
  contents.each_byte {|byte| contents_hex << "0x#{byte.to_s(16)}" }

  "[ #{contents_hex.join(', ')} ]"
end