Files
metasploit-gs/lib/rex/java/serialization/model/reference.rb
T

61 lines
1.7 KiB
Ruby
Raw Normal View History

2014-12-10 09:52:23 -06:00
# -*- coding: binary -*-
2014-12-05 12:42:27 -06:00
module Rex
module Java
module Serialization
module Model
2014-12-05 20:11:45 -06:00
# This class provides a Java Reference representation.
2014-12-05 12:42:27 -06:00
class Reference < Element
2014-12-05 16:52:59 -06:00
2014-12-05 20:11:45 -06:00
# @!attribute contents
# @return [Fixnum] The stream handle being referenced
attr_accessor :handle
2014-12-05 12:42:27 -06:00
2014-12-05 20:11:45 -06:00
# @param stream [Rex::Java::Serialization::Model::Stream] the stream where it belongs to
2014-12-05 16:52:59 -06:00
def initialize(stream = nil)
super(stream)
2014-12-05 20:11:45 -06:00
self.handle = 0
2014-12-05 16:52:59 -06:00
end
2014-12-09 19:37:42 -06:00
# Deserializes a Rex::Java::Serialization::Model::Reference
2014-12-05 20:11:45 -06:00
#
# @param io [IO] the io to read from
# @return [self] if deserialization succeeds
# @raise [RuntimeError] if deserialization doesn't succeed
2014-12-05 12:42:27 -06:00
def decode(io)
2014-12-05 20:11:45 -06:00
handle_raw = io.read(4)
unless handle_raw && handle_raw.length == 4
2014-12-05 12:42:27 -06:00
raise ::RuntimeError, 'Failed to unserialize Reference'
end
2014-12-05 20:11:45 -06:00
self.handle = handle_raw.unpack('N')[0]
2014-12-05 19:12:05 -06:00
self
2014-12-05 12:42:27 -06:00
end
2014-12-05 19:55:52 -06:00
2014-12-09 19:37:42 -06:00
# Serializes the Rex::Java::Serialization::Model::Reference
2014-12-05 20:11:45 -06:00
#
# @return [String] if serialization succeeds
# @raise [RuntimeError] if serialization doesn't succeed
2014-12-05 19:55:52 -06:00
def encode
2014-12-05 20:11:45 -06:00
if handle < BASE_WIRE_HANDLE
2014-12-05 19:55:52 -06:00
raise ::RuntimeError, 'Failed to serialize Reference'
end
encoded = ''
2014-12-05 20:11:45 -06:00
encoded << [handle].pack('N')
2014-12-05 19:55:52 -06:00
encoded
end
2014-12-07 17:52:09 -06:00
# Creates a print-friendly string representation
#
# @return [String]
def to_s
"0x#{handle.to_s(16)}"
end
2014-12-05 12:42:27 -06:00
end
end
end
end
end