mirror of https://github.com/dirtbags/netarch.git
UTF-8 symbols in hexdump
This commit is contained in:
parent
887b1e2dde
commit
833edb4135
109
__init__.py
109
__init__.py
|
@ -6,7 +6,75 @@
|
||||||
import sys
|
import sys
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
printable = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}`~/=-\\?+|\',."<>: _'
|
stdch = (u'␀·········␊··␍··'
|
||||||
|
u'················'
|
||||||
|
u' !"#$%&\'()*+,-./'
|
||||||
|
u'0123456789:;<=>?'
|
||||||
|
u'@ABCDEFGHIJKLMNO'
|
||||||
|
u'PQRSTUVWXYZ[\]^_'
|
||||||
|
u'`abcdefghijklmno'
|
||||||
|
u'pqrstuvwxyz{|}~·'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················')
|
||||||
|
|
||||||
|
decch = (u'␀␁␂␃␄␅␆␇␈␉␊␋␌␍␎␏'
|
||||||
|
u'␐␑␒␓␔␕␖␗␘␙␚·····'
|
||||||
|
u'␠!"#$%&\'()*+,-./'
|
||||||
|
u'0123456789:;<=>?'
|
||||||
|
u'@ABCDEFGHIJKLMNO'
|
||||||
|
u'PQRSTUVWXYZ[\]^_'
|
||||||
|
u'`abcdefghijklmno'
|
||||||
|
u'pqrstuvwxyz{|}~␡'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················'
|
||||||
|
u'················')
|
||||||
|
|
||||||
|
cgach = (u'␀☺☻♥♦♣♠•◘○◙♂♀♪♫☼'
|
||||||
|
u'►◄↕‼¶§▬↨↑↓→←∟↔▲▼'
|
||||||
|
u'␣!"#$%&\'()*+,-./'
|
||||||
|
u'0123456789:;<=>?'
|
||||||
|
u'@ABCDEFGHIJKLMNO'
|
||||||
|
u'PQRSTUVWXYZ[\]^_'
|
||||||
|
u'`abcdefghijklmno'
|
||||||
|
u'pqrstuvwxyz{|}~⌂'
|
||||||
|
u'ÇüéâäàåçêëèïîìÄÅ'
|
||||||
|
u'ÉæÆôöòûùÿÖÜ¢£¥₧ƒ'
|
||||||
|
u'áíóúñѪº¿⌐¬½¼¡«»'
|
||||||
|
u'░▒▓│┤╡╢╖╕╣║╗╝╜╛┐'
|
||||||
|
u'└┴┬├─┼╞╟╚╔╩╦╠═╬╧'
|
||||||
|
u'╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀'
|
||||||
|
u'αßΓπΣσµτΦΘΩδ∞φε∩'
|
||||||
|
u'≡±≥≤⌠⌡÷≈°∙·√ⁿ²■¤')
|
||||||
|
|
||||||
|
shpch = (u'␀☺☻♥♦♣♠•◘○◙♂♀♪♫☼'
|
||||||
|
u'►◄↕‼¶§▬↨↑↓→←∟↔▲▼'
|
||||||
|
u'␣!"#$%&\'()*+,-./'
|
||||||
|
u'0123456789:;<=>?'
|
||||||
|
u'@ABCDEFGHIJKLMNO'
|
||||||
|
u'PQRSTUVWXYZ[\]^_'
|
||||||
|
u'`abcdefghijklmno'
|
||||||
|
u'pqrstuvwxyz{|}~⌂'
|
||||||
|
u'ÇüéâäàåçêëèïîìÄÅ'
|
||||||
|
u'ÉæÆôöòûùÿÖÜ¢£¥₧ƒ'
|
||||||
|
u'áíóúñѪº¿⌐¬½¼¡«»'
|
||||||
|
u'░▒▓│┤╡╢╖╕╣║╗╝╜╛┐'
|
||||||
|
u'└┴┬├─┼╞╟╚╔╩╦╠═╬╧'
|
||||||
|
u'╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀'
|
||||||
|
u'αßΓπΣσµτΦΘΩδ∞φε∩'
|
||||||
|
u'≡±≥≤⌠⌡÷≈°∙·√ⁿ²■¤')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def unpack(fmt, buf):
|
def unpack(fmt, buf):
|
||||||
"""Unpack buf based on fmt, return the rest as a string."""
|
"""Unpack buf based on fmt, return the rest as a string."""
|
||||||
|
@ -24,19 +92,14 @@ class HexDumper:
|
||||||
|
|
||||||
def _to_printable(self, c):
|
def _to_printable(self, c):
|
||||||
if not c:
|
if not c:
|
||||||
return '◆'
|
return u'◌'
|
||||||
elif c in printable:
|
|
||||||
return c
|
|
||||||
elif c == '\0':
|
|
||||||
return '␀'
|
|
||||||
elif c == '\r':
|
|
||||||
return '␍'
|
|
||||||
elif c == '\n':
|
|
||||||
return ''
|
|
||||||
else:
|
else:
|
||||||
return '·'
|
return cgach[ord(c)]
|
||||||
|
|
||||||
|
|
||||||
|
def write(self, what):
|
||||||
|
self.fd.write(what.encode('utf-8'))
|
||||||
|
|
||||||
def _flush(self):
|
def _flush(self):
|
||||||
if not self.buf:
|
if not self.buf:
|
||||||
return
|
return
|
||||||
|
@ -44,26 +107,26 @@ class HexDumper:
|
||||||
o = []
|
o = []
|
||||||
for c in self.buf:
|
for c in self.buf:
|
||||||
if c:
|
if c:
|
||||||
o.append('%02x' % ord(c))
|
o.append(u'%02x' % ord(c))
|
||||||
else:
|
else:
|
||||||
o.append('--')
|
o.append(u'--')
|
||||||
o += ([' '] * (16 - len(self.buf)))
|
o += ([u' '] * (16 - len(self.buf)))
|
||||||
p = [self._to_printable(c) for c in self.buf]
|
p = [self._to_printable(c) for c in self.buf]
|
||||||
|
|
||||||
self.fd.write('%08x ' % self.offset)
|
self.write(u'%08x ' % self.offset)
|
||||||
|
|
||||||
self.fd.write(' '.join(o[:8]))
|
self.write(u' '.join(o[:8]))
|
||||||
self.fd.write(' ')
|
self.write(u' ')
|
||||||
self.fd.write(' '.join(o[8:]))
|
self.write(u' '.join(o[8:]))
|
||||||
|
|
||||||
self.fd.write(' ║')
|
self.write(u' ┆')
|
||||||
|
|
||||||
self.fd.write(''.join(p))
|
self.write(u''.join(p))
|
||||||
|
|
||||||
self.fd.write('║\n')
|
self.write(u'┆\n')
|
||||||
|
|
||||||
|
self.offset += len(self.buf)
|
||||||
self.buf = []
|
self.buf = []
|
||||||
self.offset += 16
|
|
||||||
|
|
||||||
def dump_chr(self, c):
|
def dump_chr(self, c):
|
||||||
self.buf.append(c)
|
self.buf.append(c)
|
||||||
|
@ -77,7 +140,7 @@ class HexDumper:
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
self._flush()
|
self._flush()
|
||||||
self.fd.write('%08x\n' % self.offset)
|
self.write('%08x\n' % self.offset)
|
||||||
|
|
||||||
|
|
||||||
def hexdump(buf, f=sys.stdout):
|
def hexdump(buf, f=sys.stdout):
|
||||||
|
|
39
gapstr.py
39
gapstr.py
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
## 2008 Massive Blowout
|
## 2008 Massive Blowout
|
||||||
|
|
||||||
"""Functions to treat a list as a string with gaps.
|
"""Functions to treat a list as a byte array with gaps.
|
||||||
|
|
||||||
Lists should have only string and integer items.
|
Lists should have only byte and numeric items.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -56,7 +56,9 @@ class GapString:
|
||||||
|
|
||||||
def hasgaps(self):
|
def hasgaps(self):
|
||||||
for i in self.contents:
|
for i in self.contents:
|
||||||
if isinstance(i, int):
|
try:
|
||||||
|
len(i)
|
||||||
|
except TypeError:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -66,7 +68,7 @@ class GapString:
|
||||||
d = __init__.HexDumper(fd)
|
d = __init__.HexDumper(fd)
|
||||||
for i in self.contents:
|
for i in self.contents:
|
||||||
try:
|
try:
|
||||||
for j in range(i):
|
for j in xrange(i):
|
||||||
d.dump_drop()
|
d.dump_drop()
|
||||||
except TypeError:
|
except TypeError:
|
||||||
for c in i:
|
for c in i:
|
||||||
|
@ -117,8 +119,26 @@ class GapString:
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
# XXX: speed up
|
if False:
|
||||||
return str(self)[idx]
|
c = self[idx:idx+1]
|
||||||
|
if c.hasgaps():
|
||||||
|
return self.drop[0]
|
||||||
|
else:
|
||||||
|
return c.contents[0][0]
|
||||||
|
else:
|
||||||
|
l = 0
|
||||||
|
for i in self.contents:
|
||||||
|
try:
|
||||||
|
l += len(i)
|
||||||
|
except TypeError:
|
||||||
|
l += i
|
||||||
|
if l > idx:
|
||||||
|
offs = idx - l
|
||||||
|
try:
|
||||||
|
return i[offs]
|
||||||
|
except:
|
||||||
|
return self.drop[0]
|
||||||
|
raise IndexError('Out of bounds')
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
if isinstance(other, str):
|
if isinstance(other, str):
|
||||||
|
@ -133,8 +153,12 @@ class GapString:
|
||||||
try:
|
try:
|
||||||
mask = [ord(c) for c in mask]
|
mask = [ord(c) for c in mask]
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
masklen = len(mask)
|
||||||
|
except TypeError:
|
||||||
|
masklen = 1
|
||||||
mask = [mask]
|
mask = [mask]
|
||||||
masklen = len(mask)
|
|
||||||
|
|
||||||
new = self.__class__(drop=self.drop)
|
new = self.__class__(drop=self.drop)
|
||||||
for i in self.contents:
|
for i in self.contents:
|
||||||
|
@ -147,6 +171,7 @@ class GapString:
|
||||||
offset = (offset + 1) % masklen
|
offset = (offset + 1) % masklen
|
||||||
new.append(''.join(r))
|
new.append(''.join(r))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
|
print("type error!")
|
||||||
new.append(i)
|
new.append(i)
|
||||||
return new
|
return new
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue