nedbrek

I am working on code to parse Google Protocol Buffers

# find the var int starting at offset i
# return tuple with new i and value
proc getVarInt {i tl} {
   # split the data into a list of bytes
   set tl [split $tl {}]

   # return value
   set v 0

   # place multiplier
   set mul 1

   # pull the first byte (lsb)
   binary scan [lindex $tl $i] c b

   # check for multi-byte
   while {$b < 0} {
      set v [expr $v + ($b & 0x7f) * $mul]
      set mul [expr $mul * 128]

      incr i
      binary scan [lindex $tl $i] c b
   }

   set v [expr $v + $b * $mul]
   incr i

   return [list $i $v]
}