From 93f94361ed8bbdda5c84865e57ed05fdd54c6277 Mon Sep 17 00:00:00 2001 From: moculus Date: Fri, 11 Apr 2008 16:56:45 +0000 Subject: [PATCH] endianness --HG-- branch : pmacs2 --- mode/hex.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/mode/hex.py b/mode/hex.py index 24eac4d..dac85c8 100644 --- a/mode/hex.py +++ b/mode/hex.py @@ -13,6 +13,12 @@ class Hex(mode.Fundamental): lmargin = 12 rmargin = 18 _ctrans = ['.'] * 256 + byteorder = 'native' + byteorders = { + 'native': '=', + 'little': '<', + 'big': '>', + } cgreen = color.build('green', 'default', 'bold') ccyan = color.build('cyan', 'default', 'bold') ccursor = color.build('default', 'default', 'bold', 'reverse') @@ -103,6 +109,8 @@ class Hex(mode.Fundamental): self.add_action_and_bindings(ShowX86Instruction(), ('C-c x',)) self.add_action_and_bindings(GotoAddress(), ('C-c M-g',)) + self.add_action(HexSetByteOrder()) + # create all the insert actions for the basic text input for c in string.letters + string.digits + string.punctuation: if c in string.hexdigits: @@ -152,8 +160,20 @@ class Hex(mode.Fundamental): return s def read_struct(self, cy, ix, fmt, size): s = self.read_data(cy, ix, size) + fmt = '%s%s' % (self.byteorders[self.byteorder], fmt) return struct.unpack(fmt, s)[0] +class HexSetByteOrder(Method): + '''Sets the byte-order to use to 'little', 'big', or 'native' order''' + args = [Argument("byteorder", type=type(''), prompt="Byte order: ")] + def _execute(self, w, **vargs): + order = vargs['byteorder'] + if order in w.mode.byteorders: + w.mode.byteorder = order + w.set_error("byte-order set to %r" % w.mode.byteorder) + else: + w.set_error("invalid ordering %r (use 'little', 'big', or 'native'") + class HexForward(Method): def _execute(self, w, **vargs): w.forward() @@ -212,7 +232,8 @@ class HexRead(Method): if v is None: w.set_error("not enough data to read %s" % self.type_) else: - w.set_error("%s at 0x%08x: %r" % (self.type_, addr, v)) + end = '%s-endian' % w.mode.byteorder + w.set_error("%s %s at 0x%08x: %r" % (end, self.type_, addr, v)) except Exception, e: w.set_error("%s could not be read at 0x%08x" % (self.type_, addr)) class HexReadAligned(HexRead):