myhexdump.py 行号导览
📜 按行号区间逐段拆解
myhexdump.py(562 行)。
行号区间索引
| 行号区间 | 内容 | 教学要点 |
|---|---|---|
| 1-60 | 模块 docstring | 在 r0capture 里的角色 |
| 60-126 | history | 版本演进 |
| 128-136 | import + PY3K 开关 | Py2/3 兼容 |
| 139-155 | normalize_py | Windows stdout 二进制 |
| 158-204 | 分块 helper | chunks/chunkread/genchunks |
| 210-222 | dehex | hex→二进制 |
| 225-245 | dump | 二进制→hex 字符串 |
| 247-300 | dumpgen | 逐行生成 hex dump |
| 300-420 | hexdump | 主入口 |
| 420-560 | restore + main | 还原 + 命令行 |
1-60:模块 docstring
详细说明了:
- 在 r0capture 里干什么(把明文字节渲染成 hexdump)
- 教学价值(hexdump 格式算法、Py2/3 兼容、生成器、鸭子类型)
- 来源(fork 自 techtonik 的 hexdump 库)
60-126:history
版本演进记录(0.1 → 3.3),记录了 dump/restore/result 参数等功能的引入过程。
128-136:import 与 PY3K
python
import binascii # hexlify/unhexlify
import sys
PY3K = sys.version_info >= (3, 0) # Py2/3 分流开关139-155:normalize_py
Windows 下把 stdout 切二进制模式,避免 \n→\r\n。Linux 无需。详见 Py2/3 兼容。
158-204:分块 helper
| 函数 | 行号 | 作用 |
|---|---|---|
chunks(seq, size) | 159-177 | 序列定长分块 |
chunkread(f, size) | 180-190 | 文件流分块 |
genchunks(mixed, size) | 193-204 | 鸭子分派 |
210-222:dehex
hex 字符串 → 二进制。详见 dehex。
关键行 218-222:Py3 用 bytes.fromhex,Py2 用 .decode('hex')。
225-245:dump
二进制 → hex 字符串(00 DE AD)。详见 dumpgen。
关键行 237-238:binascii.hexlify + Py3 decode。
247-300:dumpgen
逐行生成 hex dump 的生成器。每行三栏:偏移量 + hex + ASCII。
300-420:hexdump 主入口
python
def hexdump(binary, size=2, sep=' ', result='print', only_str=False)r0capture 调用:hexdump(data, result="generator", only_str=True)。详见 hexdump 主函数。
420-560:restore 与 main
restore(hexdump_text):从 hex dump 还原二进制main():命令行模式(python myhexdump.py file)
r0capture 不直接用这两部分,但作为完整工具链保留。