@@ -94,7 +94,7 @@ def load_stream(self, stream):
9494 return self .readObject ()
9595
9696 def _readStreamHeader (self ):
97- (magic , version ) = self ._readStruct (">HH" , 4 )
97+ (magic , version ) = self ._readStruct (">HH" )
9898 if magic != self .STREAM_MAGIC or version != self .STREAM_VERSION :
9999 raise IOError ("The stream is not java serialized object. Invalid stream header: %04X%04X" % (magic , version ))
100100
@@ -111,18 +111,19 @@ def readObject(self):
111111 return res
112112
113113 def read_and_exec_opcode (self , ident = 0 , expect = None ):
114- (opid , ) = self ._readStruct (">B" , 1 )
114+ (opid , ) = self ._readStruct (">B" )
115115 self .print_ident ("OpCode: 0x%X" % opid , ident )
116116 if expect and opid not in expect :
117117 raise IOError ("Unexpected opcode 0x%X" % opid )
118118 return self .opmap .get (opid , self .do_default_stuff )(ident = ident )
119119
120- def _readStruct (self , unpack , length ):
120+ def _readStruct (self , unpack ):
121+ length = struct .calcsize (unpack )
121122 ba = self .object_stream .read (length )
122123 return struct .unpack (unpack , ba )
123124
124125 def _readString (self ):
125- (length , ) = self ._readStruct (">H" , 2 )
126+ (length , ) = self ._readStruct (">H" )
126127 ba = self .object_stream .read (length )
127128 return ba
128129
@@ -147,17 +148,17 @@ def do_classdesc(self, parent=None, ident=0):
147148 ba = self ._readString ()
148149 clazz .name = ba
149150 self .print_ident ("Class name: %s" % ba , ident )
150- (serialVersionUID , newHandle , classDescFlags ) = self ._readStruct (">LLB" , 4 + 4 + 1 )
151+ (serialVersionUID , newHandle , classDescFlags ) = self ._readStruct (">LLB" )
151152 clazz .serialVersionUID = serialVersionUID
152153 clazz .flags = classDescFlags
153154 self .print_ident ("Serial: 0x%X newHanle: 0x%X. classDescFlags: 0x%X" % (serialVersionUID , newHandle , classDescFlags ), ident )
154- (length , ) = self ._readStruct (">H" , 2 )
155+ (length , ) = self ._readStruct (">H" )
155156 self .print_ident ("Fields num: 0x%X" % length , ident )
156157
157158 clazz .fields_names = []
158159 clazz .fields_types = []
159160 for fieldId in range (length ):
160- (type , ) = self ._readStruct (">B" , 1 )
161+ (type , ) = self ._readStruct (">B" )
161162 field_name = self ._readString ()
162163 field_type = None
163164 if type == 0x44 : # 'D': Double
@@ -166,6 +167,8 @@ def do_classdesc(self, parent=None, ident=0):
166167 field_type = "integer"
167168 elif type == 0x4A : # 'J': Long
168169 field_type = "long"
170+ elif type == 0x53 : # 'S': Short
171+ field_type = "short"
169172 elif type == 0x5A : # 'Z': Boolean
170173 field_type = "boolean"
171174 elif type == 0x5B : # '[': Array
@@ -189,7 +192,7 @@ def do_classdesc(self, parent=None, ident=0):
189192 parent .__fields = clazz .fields_names
190193 parent .__types = clazz .fields_types
191194 # classAnnotation
192- (opid , ) = self ._readStruct (">B" , 1 )
195+ (opid , ) = self ._readStruct (">B" )
193196 if opid != self .TC_ENDBLOCKDATA :
194197 raise NotImplementedError ("classAnnotation isn't implemented yet" )
195198 self .print_ident ("OpCode: 0x%X" % opid , ident )
@@ -203,7 +206,7 @@ def do_classdesc(self, parent=None, ident=0):
203206 def do_blockdata (self , parent = None , ident = 0 ):
204207 # TC_BLOCKDATA (unsigned byte)<size> (byte)[size]
205208 self .print_ident ("[blockdata]" , ident )
206- (length , ) = self ._readStruct (">B" , 1 )
209+ (length , ) = self ._readStruct (">B" )
207210 ba = self .object_stream .read (length )
208211 return ba
209212
@@ -256,18 +259,20 @@ def do_object(self, parent=None, ident=0):
256259
257260 for field_name , field_type in zip (megalist , megatypes ):
258261 if field_type == "boolean" :
259- (val , ) = self ._readStruct (">B" , 1 )
262+ (val , ) = self ._readStruct (">B" )
260263 res = bool (val )
261264 elif field_type == "byte" :
262- (res , ) = self ._readStruct (">b" , 1 )
265+ (res , ) = self ._readStruct (">b" )
266+ elif field_type == "short" :
267+ (res , ) = self ._readStruct (">h" )
263268 elif field_type == "integer" :
264- (res , ) = self ._readStruct (">i" , 4 )
269+ (res , ) = self ._readStruct (">i" )
265270 elif field_type == "long" :
266- (res , ) = self ._readStruct (">q" , 8 )
271+ (res , ) = self ._readStruct (">q" )
267272 elif field_type == "float" :
268- (res , ) = self ._readStruct (">f" , 4 )
273+ (res , ) = self ._readStruct (">f" )
269274 elif field_type == "double" :
270- (res , ) = self ._readStruct (">d" , 8 )
275+ (res , ) = self ._readStruct (">d" )
271276 else :
272277 res = self .read_and_exec_opcode (ident = ident + 1 )
273278 java_object .__setattr__ (field_name , res )
@@ -285,7 +290,7 @@ def do_string(self, parent=None, ident=0):
285290
286291 def do_reference (self , parent = None , ident = 0 ):
287292 # TODO: Reference isn't supported yed
288- (handle , reference ) = self ._readStruct (">HH" , 4 )
293+ (handle , reference ) = self ._readStruct (">HH" )
289294 print "## Reference:" , handle , reference
290295# raise NotImplementedError("Reference isn't supported yed.")
291296
@@ -329,7 +334,7 @@ def writeObject(self, obj):
329334 elif type (obj ) is str :
330335 print "This is string."
331336 self .write_blockdata (obj )
332- # (opid, ) = self._readStruct(">B", 1 )
337+ # (opid, ) = self._readStruct(">B")
333338# print "OpCode: 0x%X" % opid
334339# res = self.opmap.get(opid, self.do_default_stuff)()
335340# return res
@@ -367,7 +372,7 @@ def write_object(self, obj, parent=None):
367372# self.current_object.classdesc = classdesc
368373#
369374# for field_name in self.current_object.__fields:
370- # (opid, ) = self._readStruct(">B", 1 )
375+ # (opid, ) = self._readStruct(">B")
371376# print "OpCode: 0x%X" % opid
372377# res = self.opmap.get(opid, self.do_default_stuff)(self.current_object)
373378# self.current_object.__setattr__(field_name, res)
0 commit comments