MCPcopy
hub / github.com/knownsec/pocsuite3 / Field

Class Field

pocsuite3/lib/helper/java/serialization.py:179–250  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

177
178
179class Field(Element):
180 def __init__(self, stream=''):
181 Element.__init__(self, stream)
182 self.type = ''
183 self.name = None
184 self.field_type = None
185
186 def decode(self, io):
187 code = io.read(1)
188 if not code or not self.is_valid(code):
189 raise Exception('Failed to unserialize Field')
190 self.type = Constants.TYPE_CODES[code]
191 utf = Utf(self.stream)
192 self.name = utf.decode(io)
193 if self.is_object():
194 self.field_type = self.decode_field_type(io)
195 return self
196
197 def encode(self):
198 if self.name.__class__ is not Utf:
199 raise Exception('Failed to serialize Field')
200 if not self.is_type_valid():
201 raise Exception('Failed to serialize Field')
202 encoded = ''
203 encoded += get_key_by_value(Constants.TYPE_CODES, self.type)
204 encoded += self.name.encode()
205
206 if self.is_object():
207 encoded += self.encode_field_type()
208 return encoded
209
210 def is_type_valid(self):
211 if self.type in Constants.TYPE_CODES.values():
212 return True
213 return False
214
215 def is_primitive(self):
216 if self.type in Constants.PRIMITIVE_TYPE_CODES.values():
217 return True
218 return False
219
220 def is_object(self):
221 if self.type in Constants.OBJECT_TYPE_CODES.values():
222 return True
223 return False
224
225 def is_valid(self, code):
226 if code in Constants.TYPE_CODES.keys():
227 return True
228 return False
229
230 def encode_field_type(self):
231 allowed_contents = [Utf, Reference]
232 if self.field_type.__class__ not in allowed_contents:
233 raise Exception('Failed to serialize Field')
234 encoded = encode_content(self.field_type)
235 return encoded
236

Callers 1

decodeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected