#!/usr/bin/env python3
"""toc_dump.py —— 原样 dump READ TOC 返回的字节，用于核对字段布局"""
import os, struct, sys
sys.path.insert(0, "/home/zyw/Downloads/vcd-rip")
from sgio import sg_io, SG_DXFER_FROM_DEV

fd = os.open("/dev/sr0", os.O_RDONLY | os.O_NONBLOCK)
try:
    data, st, host, drv, sb, resid = sg_io(
        fd, [0x43, 0x00, 0x00, 0, 0, 0, 0x03, 0x40, 804 >> 8, 804 & 0xFF], 804, SG_DXFER_FROM_DEV)
    if st != 0 or not data:
        print("失败", st, sb.hex()); raise SystemExit(1)
    print("返回字节数 =", len(data), " 声明长度 =", struct.unpack(">H", data[0:2])[0],
          " 首轨 =", data[2], " 末轨 =", data[3])
    print("--- 按 8 字节分组 ---")
    for i in range(0, min(len(data), 96), 8):
        c = data[i:i + 8]
        hexs = " ".join("%02x" % b for b in c)
        decs = " ".join("%3d" % b for b in c)
        print("  [%3d] %-24s %s" % (i, hexs, decs))
    print("--- 按 4 字节分组（另一种可能布局）---")
    for i in range(0, min(len(data), 96), 4):
        c = data[i:i + 4]
        print("  [%3d] %-12s %s" % (i, " ".join("%02x" % b for b in c),
                                    " ".join("%3d" % b for b in c)))
finally:
    os.close(fd)
