class Rex::Proto::Rmi::Model::Element

Public Class Methods

attr_accessor(*vars) click to toggle source
Calls superclass method
# File lib/rex/proto/rmi/model/element.rb, line 10
def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end
attributes() click to toggle source

Retrieves the element class fields

@return [Array]

# File lib/rex/proto/rmi/model/element.rb, line 19
def self.attributes
  @attributes
end
decode(io) click to toggle source

Creates a Rex::Proto::Rmi::Model::Element with data from the IO.

@param io [IO] the IO to read data from @return [Rex::Proto::Rmi::Model::Element]

# File lib/rex/proto/rmi/model/element.rb, line 27
def self.decode(io)
  elem = self.new
  elem.decode(io)

  elem
end
new(options = {}) click to toggle source
# File lib/rex/proto/rmi/model/element.rb, line 34
def initialize(options = {})
  self.class.attributes.each do |attr|
    if options.has_key?(attr)
      m = (attr.to_s + '=').to_sym
      self.send(m, options[attr])
    end
  end
end

Public Instance Methods

attributes() click to toggle source

Retrieves the element instance fields

@return [Array]

# File lib/rex/proto/rmi/model/element.rb, line 46
def attributes
  self.class.attributes
end
decode(io) click to toggle source

Decodes the Rex::Proto::Rmi::Model::Element from the input.

@raise [NoMethodError] @return [Rex::Proto::Rmi::Model::Element]

# File lib/rex/proto/rmi/model/element.rb, line 54
def decode(io)
  self.class.attributes.each do |attr|
    dec_method = ("decode_#{attr}").to_sym
    decoded = self.send(dec_method, io)
    assign_method = (attr.to_s + '=').to_sym
    self.send(assign_method, decoded)
  end

  self
end
encode() click to toggle source

Encodes the Rex::Proto::Rmi::Model::Element into an String.

@raise [NoMethodError] @return [String]

# File lib/rex/proto/rmi/model/element.rb, line 69
def encode
  encoded = ''
  self.class.attributes.each do |attr|
    m = ("encode_#{attr}").to_sym
    encoded << self.send(m) if self.send(attr)
  end

  encoded
end

Private Instance Methods

read_byte(io) click to toggle source

Reads a byte from an IO

@param io [IO] the IO to read from @return [Fixnum] @raise [Rex::Proto::Rmi::DecodeError] if the byte can't be read from io

# File lib/rex/proto/rmi/model/element.rb, line 86
def read_byte(io)
  raw = io.read(1)
  raise Rex::Proto::Rmi::DecodeError, 'Failed to read byte' unless raw

  raw.unpack('c')[0]
end
read_int(io) click to toggle source

Reads a four bytes int from an IO

@param io [IO] the IO to read from @return [Fixnum] @raise [Rex::Proto::Rmi::DecodeError] if the int can't be read from io

# File lib/rex/proto/rmi/model/element.rb, line 113
def read_int(io)
  raw = io.read(4)

  unless raw && raw.length == 4
    raise Rex::Proto::Rmi::DecodeError, 'Failed to read int'
  end

  raw.unpack('l>')[0]
end
read_long(io) click to toggle source

Reads a 8 bytes long from an IO

@param io [IO] the IO to read from @return [Fixnum] @raise [Rex::Proto::Rmi::DecodeError] if the long can't be read from io

# File lib/rex/proto/rmi/model/element.rb, line 128
def read_long(io)
  raw = io.read(8)

  unless raw && raw.length == 8
    raise Rex::Proto::Rmi::DecodeError, 'Failed to read long'
  end

  raw.unpack('q>')[0]
end
read_short(io) click to toggle source

Reads a two bytes short from an IO

@param io [IO] the IO to read from @return [Fixnum] @raise [Rex::Proto::Rmi::DecodeError] if the short can't be read from io

# File lib/rex/proto/rmi/model/element.rb, line 98
def read_short(io)
  raw = io.read(2)

  unless raw && raw.length == 2
    raise Rex::Proto::Rmi::DecodeError, 'Failed to read short'
  end

  raw.unpack('s>')[0]
end
read_string(io, length) click to toggle source

Reads an string from an IO

@param io [IO] the IO to read from @param length [Fixnum] the string length @return [String] @raise [Rex::Proto::Rmi::DecodeError] if the string can't be read from io

# File lib/rex/proto/rmi/model/element.rb, line 144
def read_string(io, length)
  raw = io.read(length)

  unless raw && raw.length == length
    raise Rex::Proto::Rmi::DecodeError, 'Failed to read string'
  end

  raw
end