Skip to content

Commit d1d20ee

Browse files
committed
Add #to_s
2 parents ff99669 + 564da44 commit d1d20ee

28 files changed

+419
-4
lines changed

lib/rex/java/serialization/model/annotation.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ def encode
4949
encoded
5050
end
5151

52+
# Creates a print-friendly string representation
53+
#
54+
# @return [String]
55+
def to_s
56+
str = '[ '
57+
contents_data = contents.collect {|content| "#{print_content(content)}"}
58+
str << contents_data.join(', ')
59+
str << ' ]'
60+
str
61+
end
62+
5263
end
5364
end
5465
end

lib/rex/java/serialization/model/block_data.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ def decode(io)
4242
self
4343
end
4444

45+
# Creates a print-friendly string representation
46+
#
47+
# @return [String]
48+
def to_s
49+
contents_hex = []
50+
contents.each_byte {|byte| contents_hex << "0x#{byte.to_s(16)}" }
51+
52+
"[ #{contents_hex.join(', ')} ]"
53+
end
54+
4555
# Serializes the Java::Serialization::Model::BlockData
4656
#
4757
# @return [String]

lib/rex/java/serialization/model/block_data_long.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def encode
5353

5454
encoded
5555
end
56+
57+
# Creates a print-friendly string representation
58+
#
59+
# @return [String]
60+
def to_s
61+
contents_hex = []
62+
contents.each_byte {|byte| contents_hex << "0x#{byte.to_s(16)}" }
63+
64+
"[ #{contents_hex.join(', ')} ]"
65+
end
5666
end
5767
end
5868
end

lib/rex/java/serialization/model/class_desc.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def encode
4848

4949
encoded
5050
end
51+
52+
# Creates a print-friendly string representation
53+
#
54+
# @return [String]
55+
def to_s
56+
print_content(description)
57+
end
5158
end
5259
end
5360
end

lib/rex/java/serialization/model/contents.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,55 @@ def encode_content(content)
9898
encoded << content.encode
9999
encoded
100100
end
101+
102+
# Creates a print-friendly string representation
103+
#
104+
# @param content [Rex::Java::Serialization::Model::Element] the content to print
105+
# @return [String]
106+
def print_content(content)
107+
str = ''
108+
109+
case content
110+
when BlockData
111+
str << "#{print_class(content)} { #{content.to_s} }"
112+
when BlockDataLong
113+
str << "#{print_class(content)} { #{content.to_s} }"
114+
when EndBlockData
115+
str << "#{print_class(content)}"
116+
when NewObject
117+
str << "#{print_class(content)} { #{content.to_s} }"
118+
when ClassDesc
119+
str << "#{print_class(content)} { #{content.to_s} }"
120+
when NewArray
121+
str << "#{print_class(content)} { #{content.to_s} }"
122+
when Utf
123+
str << "#{print_class(content)} { #{content.to_s} }"
124+
when LongUtf
125+
str << "#{print_class(content)} { #{content.to_s} } "
126+
when NewEnum
127+
str << "#{print_class(content)} { #{content.to_s} }"
128+
when NewClassDesc
129+
str << "#{print_class(content)} { #{content.to_s} }"
130+
when NullReference
131+
str << "#{print_class(content)}"
132+
when Reset
133+
str << "#{print_class(content)}"
134+
when Reference
135+
str << "#{print_class(content)} { #{content.to_s} }"
136+
else
137+
raise ::RuntimeError, 'Failed to serialize content'
138+
end
139+
140+
str
141+
end
142+
143+
# Creates a print-friendly string representation of the content class
144+
#
145+
# @param content [Rex::Java::Serialization::Model::Element] the content
146+
# @return [String]
147+
def print_class(content)
148+
content.class.name.split('::').last
149+
end
101150
end
102151
end
103152
end

lib/rex/java/serialization/model/element.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def decode(io)
2828
def encode
2929
''
3030
end
31+
32+
# Creates a print-friendly string representation
33+
#
34+
# @return [String]
35+
def to_s
36+
self.class.name.split('::').last
37+
end
3138
end
3239
end
3340
end

lib/rex/java/serialization/model/field.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ def is_object?
105105
false
106106
end
107107

108+
# Creates a print-friendly string representation
109+
#
110+
# @return [String]
111+
def to_s
112+
str = "#{name} "
113+
if is_primitive?
114+
str << "(#{type})"
115+
else
116+
str << "(#{field_type})"
117+
end
118+
119+
str
120+
end
121+
108122
private
109123

110124
# Whether the type opcode is a valid one.

lib/rex/java/serialization/model/long_utf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def encode
3838

3939
encoded
4040
end
41+
4142
end
4243
end
4344
end

lib/rex/java/serialization/model/new_array.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NewArray < Element
88
include Rex::Java::Serialization::Model::Contents
99

1010
# @!attribute array_description
11-
# @return [Java::Serialization::Model::ClassDescription] The description of the array
11+
# @return [Java::Serialization::Model::ClassDesc] The description of the array
1212
attr_accessor :array_description
1313
# @!attribute type
1414
# @return [String] The type of the array values
@@ -66,6 +66,15 @@ def encode
6666
encoded
6767
end
6868

69+
# Creates a print-friendly string representation
70+
#
71+
# @return [String]
72+
def to_s
73+
str = "#{type}, "
74+
values_data = values.collect {|v| "#{v}"}
75+
str << "#{values_data}"
76+
end
77+
6978
private
7079

7180
# Deserializes the NewArray length

lib/rex/java/serialization/model/new_class_desc.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ def encode
8484
encoded
8585
end
8686

87+
# Creates a print-friendly string representation
88+
#
89+
# @return [String]
90+
def to_s
91+
str = "#{class_name}, [ "
92+
fields_str = []
93+
fields.each do |field|
94+
fields_str << field.to_s
95+
end
96+
str << "#{fields_str.join(', ')} ]"
97+
98+
case super_class.description
99+
when NewClassDesc
100+
str << ", @super_class: #{super_class.description.class_name.to_s}"
101+
when Reference
102+
str << ", @super_class: #{super_class.description.to_s}"
103+
end
104+
105+
str
106+
end
107+
87108
private
88109

89110
# Deserializes a class serial version

0 commit comments

Comments
 (0)