Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 0.81 KB | None | 0 0
  1. proc ::bpacket::encode::varint {value} {
  2.   if {$value < 128} {  ; # 2**7
  3.     append x [binary format c $value]
  4.     return $x
  5.   } elseif {$value < 16384} {  ; # 2**14
  6.     append x [binary format c [expr {($value & 127) | 128}]]
  7.     append x [binary format c [expr {$value >> 7}]]
  8.     return $x
  9.   } elseif {$value < 2097152} {  ;# 2**21
  10.     append x [binary format c [expr {($value & 127) | 128}]]
  11.     append x [binary format c [expr {(($value >> 7) & 127) | 128}]]
  12.     append x [binary format c [expr {$value >> 14}]]
  13.     return $x
  14.   }
  15.   set n 0; set sh 0
  16.   while 1 {
  17.     set b [expr {($value >> $sh) & 127}]
  18.     incr sh 7
  19.     incr n
  20.     if {$value >> $sh} {
  21.       append x [binary format c [expr {$b | 128}]]
  22.     } else {
  23.       append x [binary format c $b]
  24.       return $x
  25.     }
  26.   }
  27.   return $x
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement