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

  1. 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}]
  2.  
  3. set group {cmst mlog agal mlcl mshl mlit abro abar apso caci avdb cmgt aply adbs cmpa}
  4.  
  5. proc decode {var handle {indent 0}} {
  6. upvar 1 $var raw
  7. global group
  8. while {$handle >= 8} {
  9.  
  10. # read word data type and length
  11. binary scan $raw a4I ptype plen
  12. set raw [string range $raw 8 end]
  13. incr handle -8
  14.  
  15. # recurse into groups
  16. if {$ptype in $group} {
  17. puts "[string repeat \t $indent]$ptype --+"
  18. decode raw $plen [expr {$indent + 1}]
  19. incr handle -$plen
  20. continue
  21. }
  22.  
  23. # read and parse data
  24. if {$plen == 1} {
  25. binary scan $raw H2X1cu hex dec
  26. }
  27. if {$plen == 4} {
  28. binary scan $raw H8X4Iu hex dec
  29. }
  30. if {$plen == 8} {
  31. binary scan $raw H16X8Wu hex dec
  32. }
  33. set nice "$hex == $dec"
  34. set raw [string range $raw $plen end]
  35. incr handle -$plen
  36.  
  37. # if rebinary.search(pdata) is None:
  38. # nice = pdata
  39.  
  40. puts [format %s%-6s%-6s%s \
  41. [string repeat \t $indent] $ptype $plen $nice]
  42.  
  43. }
  44. }
  45.  
  46. decode raw [string length $raw]
  47.  

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]