diff --git a/buffer.py b/buffer.py index ee2441a..1d24e82 100644 --- a/buffer.py +++ b/buffer.py @@ -2,8 +2,6 @@ import datetime, grp, md5, os, pwd, re, sets, shutil, stat, string import aes, dirutil, regex, highlight, lex from point import Point -#from point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Pointfrom point import Point - # undo/redo stack constants ACT_NORM = 0 ACT_UNDO = 1 @@ -261,8 +259,6 @@ class Buffer(object): # insertion into buffer def insert_lines(self, p, lines, act=ACT_NORM, force=False): - #if lines == ['(']: - # raise Exception, "damn" llen = len(lines) assert llen > 0 if not force and self.readonly(): @@ -436,7 +432,6 @@ class FileBuffer(Buffer): # methods for dealing with the underlying resource, etc. def name(self): - #return self.path return self._name def path_exists(self): return os.path.exists(self.path) @@ -522,6 +517,7 @@ class Binary32Buffer(FileBuffer): numgroups = 2 bytepad = 1 data = None + wordsize = 4 def __init__(self, path, nl='\n', name=None): '''fb = FileBuffer(path)''' FileBuffer.__init__(self, path, nl, name) @@ -565,6 +561,7 @@ class Binary32Buffer(FileBuffer): c = chr(int(self.lines[p.y][cx:cx + 2], 16)) rawline = self.rawdata[p.y] self.rawdata[p.y] = rawline[0:ix] + c + rawline[ix + 1:] + def read_filter(self, data): bytepad = ' ' * self.bytepad grouppad = ' ' * self.grouppad diff --git a/mode/hex.py b/mode/hex.py index 734bc77..3aa1fc2 100644 --- a/mode/hex.py +++ b/mode/hex.py @@ -1,4 +1,4 @@ -import string +import string, struct import color, mode from lex import Grammar, PatternRule, RegionRule from method import Method, Argument @@ -6,9 +6,12 @@ from point import Point class Hex(mode.Fundamental): modename = 'Hex' - lmargin = 12 - rmargin = 18 - _ctrans = ['.'] * 256 + lmargin = 12 + rmargin = 18 + _ctrans = ['.'] * 256 + cgreen = color.build('green', 'default', 'bold') + ccyan = color.build('cyan', 'default', 'bold') + ccursor = color.build('default', 'default', 'bold', 'reverse') for c in string.letters + string.digits + string.punctuation + ' ': _ctrans[ord(c)] = c ctrans = ''.join(_ctrans) @@ -16,10 +19,6 @@ class Hex(mode.Fundamental): mode.Fundamental.__init__(self, w) self.bindings = {} - self.add_bindings('start-of-line', ('C-a', 'HOME',)) - self.add_bindings('end-of-line', ('C-e', 'END',)) - #self.add_bindings('backward', ('C-b', 'L_ARROW',)) - #self.add_bindings('forward', ('C-f', 'R_ARROW',)) self.add_bindings('center-view', ('C-l',)) self.add_bindings('next-line', ('C-n', 'D_ARROW',)) self.add_bindings('previous-line', ('C-p', 'U_ARROW',)) @@ -29,16 +28,6 @@ class Hex(mode.Fundamental): self.add_bindings('page-up', ('M-v', 'PG_UP',)) self.add_bindings('goto-beginning', ('M-<',)) self.add_bindings('goto-end', ('M->',)) - #self.add_bindings('delete-left', ('DELETE', 'BACKSPACE',)) - #self.add_bindings('delete-left-word', ('M-DELETE', 'M-BACKSPACE',)) - #self.add_bindings('delete-right', ('C-d',)) - #self.add_bindings('delete-right-word', ('M-d',)) - #self.add_bindings('kill-region', ('C-w',)) - #self.add_bindings('copy-region', ('M-w',)) - #self.add_bindings('kill', ('C-k',)) - #self.add_bindings('copy', ('M-k',)) - #self.add_bindings('yank', ('C-y',)) - #self.add_bindings('pop-kill', ('M-y',)) self.add_bindings('right-word', ('M-f',)) self.add_bindings('left-word', ('M-b',)) self.add_bindings('set-mark', ('C-@',)) @@ -54,34 +43,16 @@ class Hex(mode.Fundamental): self.add_bindings('regex-search', ('M-C-s',)) self.add_bindings('regex-reverse-search', ('M-C-r',)) self.add_bindings('toggle-margins', ('M-m',)) - #self.add_bindings('replace', ('M-%',)) - #self.add_bindings('regex-replace', ('M-$',)) self.add_bindings('open-file', ('C-x C-f',)) self.add_bindings('kill-buffer', ('C-x k',)) self.add_bindings('list-buffers', ('C-x C-b',)) self.add_bindings('meta-x', ('M-x',)) - #self.add_bindings('wrap-line', ('M-q',)) - #self.add_bindings('transpose-words', ('M-t',)) self.add_bindings('save-buffer', ('C-x C-s',)) self.add_bindings('save-buffer-as', ('C-x C-w',)) - #self.add_bindings('relex-buffer', ('M-r',)) self.add_bindings('exit', ('C-x C-c',)) self.add_bindings('split-window', ('C-x s', 'C-x 2',)) self.add_bindings('unsplit-window', ('C-u s', 'C-x 1',)) self.add_bindings('toggle-window', ('C-x o',)) - #self.add_bindings('delete-left-whitespace', ('C-c DELETE', 'C-c BACKSPACE',)) - #self.add_bindings('delete-right-whitespace', ('C-c d',)) - #self.add_bindings('insert-space', ('SPACE',)) - #self.add_bindings('insert-tab', ('TAB',)) - #self.add_bindings('insert-newline', ('RETURN',)) - #self.add_bindings('comment-region', ('C-c #',)) - #self.add_bindings('uncomment-region', ('C-u C-c #',)) - #self.add_bindings('justify-right', ('C-c f',)) - #self.add_bindings('justify-left', ('C-c b',)) - #self.add_bindings('indent-block', ('C-c >',)) - #self.add_bindings('unindent-block', ('C-c <',)) - #self.add_bindings('token-complete', ('M-c', 'C-c c', 'C-c TAB',)) - #self.add_bindings('open-aes-file', ('C-c a',)) self.add_bindings('open-console', ('M-e',)) self.add_bindings('show-bindings-buffer', ('C-c M-h',)) self.add_bindings('show-functions-buffer', ('C-c M-?',)) @@ -93,16 +64,21 @@ class Hex(mode.Fundamental): self.add_bindings('grep', ('C-c g',)) self.add_bindings('pipe', ('C-c p', 'C-c |')) self.add_bindings('view-buffer-parent', ('C-c .',)) - #self.add_bindings('insert-squotes', ('M-\'',)) - #self.add_bindings('insert-dquotes', ('M-"',)) self.add_bindings('get-token', ('C-c t',)) - #self.add_bindings('insert-text', ('C-c i',)) - #self.add_bindings('insert-text2', ('C-c M-i',)) - #self.add_bindings('insert-multiline-text', ('C-c m',)) self.add_action_and_bindings(HexForward(), ('C-f', 'R_ARROW',)) self.add_action_and_bindings(HexBackward(), ('C-b', 'L_ARROW',)) + self.add_action_and_bindings(HexForwardWord(), ('M-f', 'M-R_ARROW',)) + self.add_action_and_bindings(HexBackwardWord(), ('M-b', 'M-L_ARROW',)) + self.add_action_and_bindings(HexStartOfLine(), ('C-a', 'HOME',)) + self.add_action_and_bindings(HexEndOfLine(), ('C-e', 'END',)) self.add_action_and_bindings(GotoWord(), ('M-g',)) + self.add_action_and_bindings(HexReadAligned('int', 'i'), ('C-c i',)) + self.add_action_and_bindings(HexReadAligned('uint', 'I'), ('C-c I',)) + self.add_action_and_bindings(HexReadAligned('long', 'l'), ('C-c l',)) + self.add_action_and_bindings(HexReadAligned('ulong', 'L'), ('C-c L',)) + self.add_action_and_bindings(HexReadAligned('float', 'f'), ('C-c f',)) + self.add_action_and_bindings(HexReadAligned('double', 'd'), ('C-c d',)) self.add_action(FindStrings()) self.add_action(WhichWord()) @@ -110,13 +86,19 @@ class Hex(mode.Fundamental): for c in string.letters + string.digits + string.punctuation: if c in string.hexdigits: self.add_action_and_bindings(HexOverwriteChar(c), (c,)) + + def get_address(self, y, x): + return (y * 16) + x + def get_lmargin(self, y, x=0, ended=False, cont=False): lm = self.lmargin if ended: s = ' -------- ' - elif x == 0: - s = '0x%08x ' % (y * 16) - return ((0, s, color.build('cyan', 'default', 'bold')),) + else: + addr = self.get_address(y, x) + s = '0x%08x ' % addr + return ((0, s, self.ccyan),) + def get_rmargin(self, y, x=0, ended=False, cont=False): if ended: return ((0, '', 0),) @@ -127,16 +109,17 @@ class Hex(mode.Fundamental): i = self.window.buffer.cursorx_to_datax(cy, cx) if i is None: - return ((0, s, color.build('green', 'default', 'bold')),) - elif i < len(s) - 1: - return ((0, s[0:i], color.build('green', 'default', 'bold')), - (i, s[i], color.build('default', 'default', 'bold', 'reverse')), - (i + 1, s[i+1:], color.build('green', 'default', 'bold'))) + rmargins = ((0, s, self.cgreen),) + elif i < len(s): + rmargins = ((0, s[0:i], self.cgreen), + (i, s[i], self.ccursor), + (i + 1, s[i+1:], self.cgreen)) else: - return ((0, s[0:i], color.build('green', 'default', 'bold')), - (i, s[i], color.build('default', 'default', 'bold', 'reverse'))) + rmargins= ((0, s[0:i], self.cgreen), + (i, s[i], self.cgreen),) + return rmargins else: - return ((0, s, color.build('green', 'default', 'bold')),) + return ((0, s, self.cgreen),) class HexForward(Method): def _execute(self, w, **vargs): @@ -144,7 +127,6 @@ class HexForward(Method): end = w.buffer.get_buffer_end() while w.cursor_char().isspace() and w.cursor < end: w.forward() - class HexBackward(Method): def _execute(self, w, **vargs): w.backward() @@ -152,6 +134,59 @@ class HexBackward(Method): while w.cursor_char().isspace() and w.cursor > start: w.backward() +class HexForwardWord(Method): + def _execute(self, w, **vargs): + hf = w.application.methods['hex-forward'] + for i in range(0, w.buffer.wordsize * 2): + hf.execute(w, **vargs) +class HexBackwardWord(Method): + def _execute(self, w, **vargs): + hb = w.application.methods['hex-backward'] + for i in range(0, w.buffer.wordsize * 2): + hb.execute(w, **vargs) + +class HexStartOfLine(Method): + '''Move the cursor to the start of the current hex line''' + def _execute(self, w, **vargs): + w.start_of_line() +class HexEndOfLine(Method): + '''Move the cursor to the end of the current hex line''' + def _execute(self, w, **vargs): + w.end_of_line() + if w.cursor_char() == '\n': + w.backward() + +class HexReadAligned(Method): + _is_method = False + def __init__(self, type_, fmt): + self.name = 'hex-read-aligned-%s' % type_.lower() + self.type_ = type_ + self.fmt = fmt + self.size = struct.calcsize(fmt) + self.args = [] + self.help = "Read %r from word-aligned address in the current buffer." % self.type_ + def _execute(self, w, **vargs): + b = w.buffer + hb = w.application.methods['hex-backward'] + (cx, cy) = w.cursor.xy() + ix = b.cursorx_to_datax(cy, cx) + ix -= ix % b.wordsize + s = b.rawdata[cy][ix:] + if len(s) < self.size: + if cy < len(b.rawdata) - 1: + s += b.rawdata[cy + 1] + else: + w.set_error("not enough data to read %s" % self.type_) + return + s = s[:self.size] + try: + v = struct.unpack(self.fmt, s)[0] + addr = w.mode.get_address(cy, 0) + ix + w.set_error("%s at 0x%08x: %r" % (self.type_, addr, v)) + except: + raise + w.set_error("%s could not be read" % self.type_) + class HexOverwriteChar(Method): _is_method = False def __init__(self, c):