Version 1 of nedbrek

Updated 2012-08-27 23:28:11 by LkpPo

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]
}