Posted to tcl by schelte at Thu Jul 10 21:03:29 GMT 2014view pretty

set raw [binary format c* {0x6d 0x6c 0x6f 0x67 0x00 0x00 0x00 0x18 0x6d 0x73 0x74 0x74 0x00 0x00 0x00 0x04 0x00 0x00 0x00 0xc8 0x6d 0x6c 0x69 0x64 0x00 0x00 0x00 0x04 0x04 0x14 0x6c 0x16}]

set group {cmst mlog agal mlcl mshl mlit abro abar apso caci avdb cmgt aply adbs cmpa}

proc decode {var handle {indent 0}} {
    upvar 1 $var raw
    global group
    while {$handle >= 8} {

        # read word data type and length
        binary scan $raw a4I ptype plen
        set raw [string range $raw 8 end]
        incr handle -8

        # recurse into groups
        if {$ptype in $group} {
            puts "[string repeat \t $indent]$ptype --+"
            decode raw $plen [expr {$indent + 1}]
            incr handle -$plen
            continue
        }

        # read and parse data
        if {$plen == 1} {
            binary scan $raw H2X1cu hex dec
        }
        if {$plen == 4} {
            binary scan $raw H8X4Iu hex dec
        }
        if {$plen == 8} {
            binary scan $raw H16X8Wu hex dec
        }
        set nice "$hex == $dec"
        set raw [string range $raw $plen end]
        incr handle -$plen

        # if rebinary.search(pdata) is None:
        # nice = pdata

        puts [format %s%-6s%-6s%s \
          [string repeat \t $indent] $ptype $plen $nice]

    }
}

decode raw [string length $raw]

Comments

Posted by schelte at Thu Jul 10 21:17:05 GMT 2014 [text] [code]

# set raw [binary format c* {0x6d 0x6c 0x6f 0x67 0x00 0x00 0x00 0x18 0x6d 0x73 0x74 0x74 0x00 0x00 0x00 0x04 0x00 0x00 0x00 0xc8 0x6d 0x6c 0x69 0x64 0x00 0x00 0x00 0x04 0x04 0x14 0x6c 0x16}] set group {cmst mlog agal mlcl mshl mlit abro abar apso caci avdb cmgt aply adbs cmpa} set rebinary {[^\x20-\x7e]} proc decode {var handle {indent 0}} { upvar 1 $var raw global group rebinary while {$handle >= 8} { # read word data type and length binary scan $raw a4I ptype plen set raw [string range $raw 8 end] incr handle -8 # recurse into groups if {$ptype in $group} { puts "[string repeat \t $indent]$ptype --+" decode raw $plen [expr {$indent + 1}] incr handle -$plen continue } # read and parse data if {$plen == 1} { binary scan $raw H2X1cu hex dec } if {$plen == 4} { binary scan $raw H8X4Iu hex dec } if {$plen == 8} { binary scan $raw H16X8Wu hex dec } set nice "$hex == $dec" binary scan $raw a$plen pdata if {![regexp $rebinary $pdata]} { set nice $pdata } set raw [string range $raw $plen end] incr handle -$plen puts [format %s%-6s%-6s%s \ [string repeat \t $indent] $ptype $plen $nice] } } fconfigure stdin -translation binary set raw [read stdin] decode raw [string length $raw]