Skip to content

Commit cd14e8e

Browse files
committed
Better hex dump. Show stream offests in logs
1 parent e4562ad commit cd14e8e

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

javaobj.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Copyright 2010-2013 Volodymyr Buell
2-
#
31
# Licensed under the Apache License, Version 2.0 (the "License");
42
# you may not use this file except in compliance with the License.
53
# You may obtain a copy of the License at
@@ -54,6 +52,7 @@ def load(file_object, *args):
5452
marshaller.add_transformer(DefaultObjectTransformer())
5553
return marshaller.readObject()
5654

55+
5756
def load_all(file_object):
5857
marshaller = JavaObjectUnmarshaller(file_object)
5958
marshaller.add_transformer(DefaultObjectTransformer())
@@ -64,7 +63,6 @@ def load_all(file_object):
6463
return res
6564

6665

67-
6866
def loads(string, *args):
6967
"""
7068
Deserializes Java objects and primitive data serialized by ObjectOutputStream
@@ -150,6 +148,7 @@ def copy(self, new_object):
150148
for name in self.classdesc.fields_names:
151149
new_object.__setattr__(name, getattr(self, name))
152150

151+
153152
class JavaString(str):
154153
def __init__(self, *args, **kwargs):
155154
str.__init__(self, *args, **kwargs)
@@ -159,17 +158,20 @@ def __eq__(self, other):
159158
return False
160159
return str.__eq__(self, other)
161160

161+
162162
class JavaEnum(JavaObject):
163163
def __init__(self, constant=None):
164164
super(JavaEnum, self).__init__()
165165
self.constant = constant
166166

167+
167168
class JavaArray(list, JavaObject):
168169
def __init__(self, classdesc=None):
169170
list.__init__(self)
170171
JavaObject.__init__(self)
171172
self.classdesc = classdesc
172173

174+
173175
class JavaObjectConstants:
174176

175177
STREAM_MAGIC = 0xaced
@@ -262,15 +264,15 @@ def readObject(self):
262264
the_rest = self.object_stream.read()
263265
if len(the_rest):
264266
log_error("Warning!!!!: Stream still has %s bytes left. Enable debug mode of logging to see the hexdump." % len(the_rest))
265-
log_debug(self._create_hexdump(the_rest))
267+
log_debug(self._create_hexdump(the_rest, position_bak))
266268
self.data_left = True
267269
else:
268270
log_debug("Java Object unmarshalled succesfully!")
269271
self.data_left = False
270272
self.object_stream.seek(position_bak)
271273

272274
return res
273-
except Exception, e:
275+
except Exception as e:
274276
self._oops_dump_state()
275277
raise
276278

@@ -283,8 +285,9 @@ def _readStreamHeader(self):
283285
raise IOError("The stream is not java serialized object. Invalid stream header: %04X%04X" % (magic, version))
284286

285287
def _read_and_exec_opcode(self, ident=0, expect=None):
288+
position = self.object_stream.tell()
286289
(opid, ) = self._readStruct(">B")
287-
log_debug("OpCode: 0x%X" % opid, ident)
290+
log_debug("OpCode: 0x%X (at offset: 0x%X)" % (opid, position), ident)
288291
if expect and opid not in expect:
289292
raise IOError("Unexpected opcode 0x%X" % opid)
290293
handler = self.opmap.get(opid)
@@ -390,7 +393,6 @@ def do_blockdata_long(self, parent=None, ident=0):
390393
ba = self.object_stream.read(length)
391394
return ba
392395

393-
394396
def do_class(self, parent=None, ident=0):
395397
# TC_CLASS classDesc newHandle
396398
log_debug("[class]", ident)
@@ -527,14 +529,14 @@ def do_enum(self, parent=None, ident=0):
527529
enum.constant = enumConstantName
528530
return enum
529531

530-
def _create_hexdump(self, src, length=16):
531-
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
532+
def _create_hexdump(self, src, start_offset=0, length=16):
533+
FILTER = ''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
532534
result = []
533535
for i in xrange(0, len(src), length):
534536
s = src[i:i+length]
535-
hexa = ' '.join(["%02X"%ord(x) for x in s])
537+
hexa = ' '.join(["%02X" % ord(x) for x in s])
536538
printable = s.translate(FILTER)
537-
result.append("%04X %-*s %s\n" % (i, length*3, hexa, printable))
539+
result.append("%04X %-*s %s\n" % (i+start_offset, length*3, hexa, printable))
538540
return ''.join(result)
539541

540542
def _read_value(self, field_type, ident, name = ""):
@@ -589,10 +591,10 @@ def _oops_dump_state(self):
589591
log_error("References: %s" % str(self.references))
590592
log_error("Stream seeking back at -16 byte (2nd line is an actual position!):")
591593
self.object_stream.seek(-16, 1)
594+
position = self.object_stream.tell()
592595
the_rest = self.object_stream.read()
593596
if len(the_rest):
594-
log_error("Warning!!!!: Stream still has %s bytes left." % len(the_rest))
595-
log_error(self._create_hexdump(the_rest))
597+
log_error(self._create_hexdump(the_rest, position))
596598
log_error("=" * 30)
597599

598600

@@ -693,7 +695,7 @@ def write_object(self, obj, parent=None):
693695
for name, type in zip(all_names, all_types):
694696
try:
695697
self._write_value(type, getattr(obj, name))
696-
except AttributeError, e:
698+
except AttributeError as e:
697699
log_error("%s e, %s %s" % (str(e), repr(obj), repr(dir(obj))))
698700
raise
699701

0 commit comments

Comments
 (0)