1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
# Implements the 'binary scan' and 'binary format' commands.
#
# (c) 2010 Steve Bennett <steveb@workware.net.au>
#
# See LICENCE in this directory for licensing.
package require pack
package require regexp
proc binary {cmd args} {
tailcall "binary $cmd" {*}$args
}
proc "binary format" {formatString args} {
set bitoffset 0
set result {}
foreach {conv t u n} [regexp -all -inline {([a-zA-Z@])(u)?([*0-9]*)} $formatString] {
if {$t in {a A}} {
set value [binary.nextarg args]
set sn [string bytelength $value]
if {$n ne "*"} {
if {$n eq ""} {
set n 1
}
if {$n > $sn} {
# Need to pad the string with spaces or nulls
append value [string repeat [dict get {A " " a \x00} $t] $($n - $sn)]
}
} else {
set n $sn
}
if {$n} {
set bitoffset [pack result $value -str $(8 * $n) $bitoffset]
}
} elseif {[binary.intinfo $t] ne ""} {
# An integer type
lassign [binary.intinfo $t] type endian size prefix
set value [binary.nextarg args]
if {$type ne "int"} {
set value [split $value {}]
}
set vn [llength $value]
if {$n eq "*"} {
set n $vn
} elseif {$n eq ""} {
set n 1
set value [list $value]
} elseif {$vn < $n} {
if {$type eq "int"} {
return -code error "number of elements in list does not match count"
} else {
# Need to pad the list with zeros
lappend value {*}[lrepeat $($n - $vn) 0]
}
} elseif {$vn > $n} {
# Need to truncate the list
set value [lrange $value 0 $n-1]
}
if {$endian eq "host"} {
set endian $($::tcl_platform(byteOrder) eq "bigEndian" ? "be" : "le")
}
foreach v $value {
set bitoffset [pack result $prefix$v -int$endian $size $bitoffset]
}
# Now pad out with zeros to the end of the current byte
if {$bitoffset % 8} {
set bitoffset [pack result 0 -int$endian $(8 - $bitoffset % 8) $bitoffset]
}
} elseif {$t eq "x"} {
if {$n eq "*"} {
return -code error {cannot use "*" in format string with "x"}
}
if {$n eq ""} {
set n 1
}
loop i 0 $n {
set bitoffset [pack result 0 -intbe 8 $bitoffset]
}
} elseif {$t eq "@"} {
if {$n eq ""} {
return -code error {missing count for "@" field specifier}
}
if {$n eq "*"} {
set bitoffset $(8 * [string bytelength $result])
} else {
# May need to pad it out
set max [string bytelength $result]
while {$n > $max} {
append result \x00
incr max
}
set bitoffset $(8 * $n)
}
} elseif {$t eq "X"} {
if {$n eq "*"} {
set bitoffset 0
} elseif {$n eq ""} {
incr bitoffset -8
} else {
incr bitoffset $($n * -8)
}
if {$bitoffset < 0} {
set bitoffset 0
}
} else {
return -code error "bad field specifier \"$t\""
}
}
return $result
}
proc "binary scan" {value formatString {args varName}} {
# Pops the next arg from the front of the list and returns it.
# Throws an error if no more args
set bitoffset 0
set count 0
foreach {conv t u n} [regexp -all -inline {([a-zA-Z@])(u)?([*0-9]*)} $formatString] {
set rembytes $([string bytelength $value] - $bitoffset / 8)
if {$t in {a A}} {
if {$n eq "*"} {
set n $rembytes
} elseif {$n eq ""} {
set n 1
}
if {$n > $rembytes} {
continue
}
set var [binary.nextarg varName]
set result [unpack $value -str $bitoffset $($n * 8)]
incr bitoffset $([string bytelength $result] * 8)
if {$t eq "A"} {
set result [string trimright $result]
}
} elseif {[binary.intinfo $t] ne ""} {
# An integer type
lassign [binary.intinfo $t] type endian size prefix
set var [binary.nextarg varName]
if {$n eq "*"} {
set n $($rembytes * 8 / $size)
} else {
if {$n eq ""} {
set n 1
}
}
if {$n * $size > $rembytes * 8} {
continue
}
if {$type ne "int"} {
set u u
}
if {$endian eq "host"} {
set endian $($::tcl_platform(byteOrder) eq "bigEndian" ? "be" : "le")
}
set result {}
loop i 0 $n {
set v [unpack $value -${u}int$endian $bitoffset $size]
if {$type eq "int"} {
lappend result $v
} else {
append result [lindex {0 1 2 3 4 5 6 7 8 9 a b c d e f} $v]
}
incr bitoffset $size
}
# Now skip to the end of the current byte
if {$bitoffset % 8} {
incr bitoffset $(8 - ($bitoffset % 8))
}
} elseif {$t eq "x"} {
# Skip bytes
if {$n eq "*"} {
set n $rembytes
} elseif {$n eq ""} {
set n 1
}
if {$n > $rembytes} {
set n $rembytes
}
incr bitoffset $($n * 8)
continue
} elseif {$t eq "X"} {
# Back up bytes
if {$n eq "*"} {
set bitoffset 0
continue
}
if {$n eq ""} {
set n 1
}
if {$n * 8 > $bitoffset} {
set bitoffset 0
continue
}
incr bitoffset -$($n * 8)
continue
} elseif {$t eq "@"} {
if {$n eq ""} {
return -code error {missing count for "@" field specifier}
}
if {$n eq "*" || $n > $rembytes + $bitoffset / 8} {
incr bitoffset $($rembytes * 8)
} elseif {$n < 0} {
set bitoffset 0
} else {
set bitoffset $($n * 8)
}
continue
} else {
return -code error "bad field specifier \"$t\""
}
uplevel 1 [list set $var $result]
incr count
}
return $count
}
# Pops the next arg from the front of the list and returns it.
# Throws an error if no more args
proc binary.nextarg {&arglist} {
if {[llength $arglist] == 0} {
return -level 2 -code error "not enough arguments for all format specifiers"
}
set arglist [lassign $arglist arg]
return $arg
}
proc binary.intinfo {type} {
set info {
c {int be 8}
s {int le 16}
t {int host 16}
S {int be 16}
i {int le 32}
I {int be 32}
n {int host 32}
w {int le 64}
W {int be 64}
m {int host 64}
h {hex le 4 0x}
H {hex be 4 0x}
b {bin le 1}
B {bin be 1}
}
if {[exists info($type)]} {
return $info($type)
}
return ""
}
|