1+ # -*- coding: binary -*-
2+
3+ module Rex
4+ module Java
5+ module Serialization
6+ module Model
7+ # This class provides a block data (long) representation
8+ class BlockDataLong < Element
9+
10+ # @!attribute length
11+ # @return [Integer] the length of the block
12+ attr_accessor :length
13+ # @!attribute contents
14+ # @return [String] the contents of the block
15+ attr_accessor :contents
16+
17+ # @param stream [Rex::Java::Serialization::Model::Stream] the stream where it belongs to
18+ # @param contents [String] the contents of the block
19+ def initialize ( stream = nil , contents = '' )
20+ super ( stream )
21+ self . contents = contents
22+ self . length = contents . length
23+ end
24+
25+ # Deserializes a Rex::Java::Serialization::Model::BlockDataLong
26+ #
27+ # @param io [IO] the io to read from
28+ # @return [self] if deserialization succeeds
29+ # @raise [RuntimeError] if deserialization doesn't succeed
30+ def decode ( io )
31+ raw_length = io . read ( 4 )
32+ if raw_length . nil? || raw_length . length != 4
33+ raise ::RuntimeError , 'Failed to unserialize BlockDataLong'
34+ end
35+ self . length = raw_length . unpack ( 'N' ) [ 0 ]
36+
37+ if length == 0
38+ self . contents = ''
39+ else
40+ self . contents = io . read ( length )
41+ if contents . nil? || contents . length != length
42+ raise ::RuntimeError , 'Failed to unserialize BlockData'
43+ end
44+ end
45+
46+ self
47+ end
48+
49+ # Serializes the Rex::Java::Serialization::Model::BlockDataLong
50+ #
51+ # @return [String]
52+ def encode
53+ encoded = [ length ] . pack ( 'N' )
54+ encoded << contents
55+
56+ encoded
57+ end
58+
59+ # Creates a print-friendly string representation
60+ #
61+ # @return [String]
62+ def to_s
63+ contents_hex = [ ]
64+ contents . each_byte { |byte | contents_hex << "0x#{ byte . to_s ( 16 ) } " }
65+
66+ "[ #{ contents_hex . join ( ', ' ) } ]"
67+ end
68+ end
69+ end
70+ end
71+ end
72+ end
0 commit comments