summaryrefslogtreecommitdiff
path: root/binary.tcl
blob: edc6eb158de7e5228588fac9b2edda7dd19a481c (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# 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 {}
	# This RE is too unreliable...
	foreach {conv t u n} [regexp -all -inline {([^[:space:]])(u)?([*0-9]*)} $formatString] {
		switch -exact -- $t {
			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]
				}
			}
			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]
				}
			}
			@ {
				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]
					append result [string repeat \x00 $($n - $max)]
					set bitoffset $(8 * $n)
				}
			}
			X {
				if {$n eq "*"} {
					set bitoffset 0
				} elseif {$n eq ""} {
					incr bitoffset -8
				} else {
					incr bitoffset $($n * -8)
				}
				if {$bitoffset < 0} {
					set bitoffset 0
				}
			}
			default {
				if {![info exists ::binary::scalarinfo($t)]} {
					return -code error "bad field specifier \"$t\""
				}

				# A scalar (integer or float) type
				lassign $::binary::scalarinfo($t) type convtype size prefix
				set value [binary::nextarg args]

				if {$type in {bin hex}} {
					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 in {bin hex}} {
						# Need to pad the list with zeros
						lappend value {*}[lrepeat $($n - $vn) 0]
					} else {
						return -code error "number of elements in list does not match count"
					}
				} elseif {$vn > $n} {
					# Need to truncate the list
					set value [lrange $value 0 $n-1]
				}

				set convtype -$::binary::convtype($convtype)

				foreach v $value {
					set bitoffset [pack result $prefix$v $convtype $size $bitoffset]
				}
				# Now pad out with zeros to the end of the current byte
				if {$bitoffset % 8} {
					set bitoffset [pack result 0 $convtype $(8 - $bitoffset % 8) $bitoffset]
				}
			}
		}
	}
	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
	# This RE is too unreliable...
	foreach {conv t u n} [regexp -all -inline {([^[:space:]])(u)?([*0-9]*)} $formatString] {
		set rembytes $([string bytelength $value] - $bitoffset / 8)
		switch -exact -- $t {
			a -
			A {
				if {$n eq "*"} {
					set n $rembytes
				} elseif {$n eq ""} {
					set n 1
				}
				if {$n > $rembytes} {
					break
				}

				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]
				}
			}
			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
			}
			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
			}
			@ {
				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
			}
			default {
				if {![info exists ::binary::scalarinfo($t)]} {
					return -code error "bad field specifier \"$t\""
				}
				# A scalar (integer or float) type
				lassign $::binary::scalarinfo($t) type convtype 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} {
					break
				}

				if {$type in {hex bin}} {
					set u u
				}
				set convtype -$u$::binary::convtype($convtype)

				set result {}
				loop i 0 $n {
					set v [unpack $value $convtype $bitoffset $size]
					if {$type in {bin hex}} {
						append result [lindex {0 1 2 3 4 5 6 7 8 9 a b c d e f} $v]
					} else {
						lappend result $v
					}
					incr bitoffset $size
				}
				# Now skip to the end of the current byte
				if {$bitoffset % 8} {
					incr bitoffset $(8 - ($bitoffset % 8))
				}
			}
		}
		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
}

set binary::scalarinfo {
	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}
	r {float fle 32}
	R {float fbe 32}
	f {float fhost 32}
	q {float fle 64}
	Q {float fbe 64}
	d {float fhost 64}
}
set binary::convtype {
	be intbe
	le intle
	fbe floatbe
	fle floatle
}
if {$::tcl_platform(byteOrder) eq "bigEndian"} {
	array set binary::convtype {host intbe fhost floatbe}
} else {
	array set binary::convtype {host intle fhost floatle}
}