Posted to tcl by schelte at Thu Jul 10 21:18:02 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. set rebinary {[^\x20-\x7e]}
  5.  
  6. proc decode {var handle {indent 0}} {
  7. upvar 1 $var raw
  8. global group rebinary
  9. while {$handle >= 8} {
  10.  
  11. # read word data type and length
  12. binary scan $raw a4I ptype plen
  13. set raw [string range $raw 8 end]
  14. incr handle -8
  15.  
  16. # recurse into groups
  17. if {$ptype in $group} {
  18. puts "[string repeat \t $indent]$ptype --+"
  19. decode raw $plen [expr {$indent + 1}]
  20. incr handle -$plen
  21. continue
  22. }
  23.  
  24. # read and parse data
  25. if {$plen == 1} {
  26. binary scan $raw H2X1cu hex dec
  27. }
  28. if {$plen == 4} {
  29. binary scan $raw H8X4Iu hex dec
  30. }
  31. if {$plen == 8} {
  32. binary scan $raw H16X8Wu hex dec
  33. }
  34. set nice "$hex == $dec"
  35.  
  36. binary scan $raw a$plen pdata
  37. if {![regexp $rebinary $pdata]} {
  38. set nice $pdata
  39. }
  40.  
  41. set raw [string range $raw $plen end]
  42. incr handle -$plen
  43.  
  44. puts [format %s%-6s%-6s%s \
  45. [string repeat \t $indent] $ptype $plen $nice]
  46.  
  47. }
  48. }
  49.  
  50. fconfigure stdin -translation binary
  51. set raw [read stdin]
  52.  
  53. decode raw [string length $raw]
  54.