mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
Create netlib.utils, move cleanBin and hexdump from libmproxy.utils.
This commit is contained in:
parent
3a21e28bf1
commit
b308824193
36
netlib/utils.py
Normal file
36
netlib/utils.py
Normal file
@ -0,0 +1,36 @@
|
||||
|
||||
def cleanBin(s, fixspacing=False):
|
||||
"""
|
||||
Cleans binary data to make it safe to display. If fixspacing is True,
|
||||
tabs, newlines and so forth will be maintained, if not, they will be
|
||||
replaced with a placeholder.
|
||||
"""
|
||||
parts = []
|
||||
for i in s:
|
||||
o = ord(i)
|
||||
if (o > 31 and o < 127):
|
||||
parts.append(i)
|
||||
elif i in "\n\r\t" and not fixspacing:
|
||||
parts.append(i)
|
||||
else:
|
||||
parts.append(".")
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def hexdump(s):
|
||||
"""
|
||||
Returns a set of tuples:
|
||||
(offset, hex, str)
|
||||
"""
|
||||
parts = []
|
||||
for i in range(0, len(s), 16):
|
||||
o = "%.10x"%i
|
||||
part = s[i:i+16]
|
||||
x = " ".join("%.2x"%ord(i) for i in part)
|
||||
if len(part) < 16:
|
||||
x += " "
|
||||
x += " ".join(" " for i in range(16 - len(part)))
|
||||
parts.append(
|
||||
(o, x, cleanBin(part, True))
|
||||
)
|
||||
return parts
|
13
test/test_utils.py
Normal file
13
test/test_utils.py
Normal file
@ -0,0 +1,13 @@
|
||||
from netlib import utils
|
||||
|
||||
|
||||
def test_hexdump():
|
||||
assert utils.hexdump("one\0"*10)
|
||||
|
||||
|
||||
def test_cleanBin():
|
||||
assert utils.cleanBin("one") == "one"
|
||||
assert utils.cleanBin("\00ne") == ".ne"
|
||||
assert utils.cleanBin("\nne") == "\nne"
|
||||
assert utils.cleanBin("\nne", True) == ".ne"
|
||||
|
Loading…
Reference in New Issue
Block a user