# configuration variables SPACE_BYTES = True ESCAPE_WHITE = True # end configuration variables from binascii import hexlify, unhexlify chunkify = lambda L, n=2: [L[i:i+n] for i in range(0, len(L), n)] bin2hex = lambda data: '\n'.join(chunkify(' '.join(chunkify(hexlify(data))), 48)) hex2bin = lambda txt: unhexlify(flauxtext.re.sub('[^\da-fA-F]', '', txt)) def bin2hex_quote(data): r = '' in_quote = False in_line = 0 for i in data: if i in safe_chars: if in_quote: r += i else: in_quote = True in_line = 0 r += '\nr' + i else: if in_quote: in_quote = False r += '\n' + hexlify(i) + ' ' else: r += hexlify(i) + ' ' if (in_line > 48) and not in_quote: r += '\n' in_line = 0 return r def hex2bin_quote(txt): r = '' in_quote = False byt = '' escaped for i in txt: if in_quote: if i r += i def new_save_buffer(self, action=None): if hasattr(self.current_buffer(), 'hex'): f = open(self.current_filename(), 'wb') f.write(hex2bin(self.current_text())) f.close() else: self.save_buffer_orig(action) def hex_add_to_notebk(self, filename): try: fl = open(filename) text = bin2hex(fl.read()) fl.close() textbuffer = self.new_buffer() textbuffer.hex = True textbuffer.set_text(text) curpagen = self.notebk.get_current_page() self.notebk.set_tab_label_text(self.notebk.get_nth_page(curpagen), flauxtext.os.path.basename(filename)) textbuffer.place_cursor(textbuffer.get_start_iter()) self.current_textview().scroll_to_iter(textbuffer.get_start_iter(),0.0) self.openfiles[curpagen] = filename self.undostack[curpagen] = [0] except Exception, e: self.flash_info('minor error opening ' + filename + ': ' + str(e), 5000) print 'lineno', flauxtext.sys.exc_info()[2].tb_lineno def hex_open_file(self): d = flauxtext.gtk.FileChooserDialog('you can pick your nose, your friends, and your files', None, flauxtext.gtk.FILE_CHOOSER_ACTION_OPEN, (flauxtext.gtk.STOCK_CANCEL, 0, flauxtext.gtk.STOCK_OPEN, 1)) flauxtext.respond_dialog_on_escape(d) d.set_property("show-hidden", flauxtext.openshowhidden) d.set_default_response(1) d.set_property("select-multiple", True) d.set_current_folder(flauxtext.os.path.dirname(flauxtext.os.path.abspath(self.current_filename()))) while 1: # event loops rock r = d.run() if r == 0: break elif r == 1: fnyms = d.get_filenames() if False in map(flauxtext.os.path.isfile, fnyms): if (len(fnyms) == 1) and flauxtext.os.path.isdir(fnyms[0]): d.set_current_folder(fnyms[0]) else: map(hex_add_to_notebk, fnyms) break d.destroy() ################################################################################ def hexed(*a, **kw): ''' open a binary file in hexadecimal. hook the save mechanism to appropriately save the binary file. ''' if self.save != new_save_buffer: self.save_buffer_orig = self.save self.save = new_save_buffer hex_open_file(self)