Class: Rex::Java::Serialization::Model::Field

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

Overview

This class provides a field description representation (fieldDesc). It’s used for both primitive descriptions (primitiveDesc) and object descriptions (objectDesc).

Constant Summary

Constants included from Rex::Java::Serialization

BASE_WIRE_HANDLE, OBJECT_TYPE_CODES, PRIMITIVE_TYPE_CODES, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, TC_ARRAY, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING, TYPE_CODES

Instance Attribute Summary collapse

Attributes inherited from Element

#stream

Instance Method Summary collapse

Methods included from Contents

#decode_content, #encode_content, #print_class, #print_content

Methods inherited from Element

decode

Constructor Details

#initialize(stream = nil) ⇒ Field

Returns a new instance of Field.

Parameters:



24
25
26
27
28
29
# File 'lib/rex/java/serialization/model/field.rb', line 24

def initialize(stream = nil)
  super(stream)
  self.type = ''
  self.name = nil
  self.field_type = nil
end

Instance Attribute Details

#field_typeRex::Java::Serialization::Model::Utf

Returns The type of the field on object types.

Returns:



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

def field_type
  @field_type
end

#nameRex::Java::Serialization::Model::Utf

Returns The name of the field.

Returns:



18
19
20
# File 'lib/rex/java/serialization/model/field.rb', line 18

def name
  @name
end

#typeString

Returns The type of the field.

Returns:

  • (String)

    The type of the field.



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

def type
  @type
end

Instance Method Details

#decode(io) ⇒ self

Deserializes a Rex::Java::Serialization::Model::Field

Parameters:

  • io (IO)

    the io to read from

Returns:

  • (self)

    if deserialization succeeds

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rex/java/serialization/model/field.rb', line 36

def decode(io)
  code = io.read(1)

  unless code && is_valid?(code)
    raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize Field'
  end

  self.type = TYPE_CODES[code]
  self.name = Utf.decode(io, stream)

  if is_object?
    self.field_type = decode_field_type(io)
  end

  self
end

#encodeString

Serializes the Rex::Java::Serialization::Model::Field

Returns:

  • (String)

    if serialization succeeds

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rex/java/serialization/model/field.rb', line 57

def encode
  unless name.kind_of?(Rex::Java::Serialization::Model::Utf)
    raise Rex::Java::Serialization::EncodeError, 'Failed to serialize Field'
  end

  unless is_type_valid?
    raise Rex::Java::Serialization::EncodeError, 'Failed to serialize Field'
  end

  encoded = ''
  encoded << TYPE_CODES.key(type)
  encoded << name.encode

  if is_object?
      encoded << encode_field_type
  end

  encoded
end

#is_object?Boolean

Whether the field type is an object one.

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/rex/java/serialization/model/field.rb', line 102

def is_object?
  if OBJECT_TYPE_CODES.values.include?(type)
    return true
  end

  false
end

#is_primitive?Boolean

Whether the field type is a primitive one.

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/rex/java/serialization/model/field.rb', line 91

def is_primitive?
  if PRIMITIVE_TYPE_CODES.values.include?(type)
    return true
  end

  false
end

#is_type_valid?Boolean

Whether the field type is valid.

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/rex/java/serialization/model/field.rb', line 80

def is_type_valid?
  if TYPE_CODES.values.include?(type)
    return true
  end

  false
end

#to_sString

Creates a print-friendly string representation

Returns:

  • (String)


113
114
115
116
117
118
119
120
121
122
# File 'lib/rex/java/serialization/model/field.rb', line 113

def to_s
  str = "#{name} "
  if is_primitive?
    str << "(#{type})"
  else
    str << "(#{field_type})"
  end

  str
end