endianness

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-04-11 16:56:45 +00:00
parent a27edc8224
commit 93f94361ed
1 changed files with 22 additions and 1 deletions

View File

@ -13,6 +13,12 @@ class Hex(mode.Fundamental):
lmargin = 12 lmargin = 12
rmargin = 18 rmargin = 18
_ctrans = ['.'] * 256 _ctrans = ['.'] * 256
byteorder = 'native'
byteorders = {
'native': '=',
'little': '<',
'big': '>',
}
cgreen = color.build('green', 'default', 'bold') cgreen = color.build('green', 'default', 'bold')
ccyan = color.build('cyan', 'default', 'bold') ccyan = color.build('cyan', 'default', 'bold')
ccursor = color.build('default', 'default', 'bold', 'reverse') 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(ShowX86Instruction(), ('C-c x',))
self.add_action_and_bindings(GotoAddress(), ('C-c M-g',)) self.add_action_and_bindings(GotoAddress(), ('C-c M-g',))
self.add_action(HexSetByteOrder())
# create all the insert actions for the basic text input # create all the insert actions for the basic text input
for c in string.letters + string.digits + string.punctuation: for c in string.letters + string.digits + string.punctuation:
if c in string.hexdigits: if c in string.hexdigits:
@ -152,8 +160,20 @@ class Hex(mode.Fundamental):
return s return s
def read_struct(self, cy, ix, fmt, size): def read_struct(self, cy, ix, fmt, size):
s = self.read_data(cy, ix, size) s = self.read_data(cy, ix, size)
fmt = '%s%s' % (self.byteorders[self.byteorder], fmt)
return struct.unpack(fmt, s)[0] 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): class HexForward(Method):
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
w.forward() w.forward()
@ -212,7 +232,8 @@ class HexRead(Method):
if v is None: if v is None:
w.set_error("not enough data to read %s" % self.type_) w.set_error("not enough data to read %s" % self.type_)
else: 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: except Exception, e:
w.set_error("%s could not be read at 0x%08x" % (self.type_, addr)) w.set_error("%s could not be read at 0x%08x" % (self.type_, addr))
class HexReadAligned(HexRead): class HexReadAligned(HexRead):