-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
67 lines (63 loc) · 990 Bytes
/
encoding.go
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
package stackvm
// MaxVarCodeLen is the maximum size that a varcode can
// be: a 32-bit arg (encodes to at most 5 bytes) and then
// a final byte with the 7-bit code.
const MaxVarCodeLen = 1 + 5
func readVarCode(buf []byte) (n int, arg uint32, code uint8, ok bool) {
for i, v := range buf {
n++
if v&0x80 == 0 {
code = uint8(v)
if i > 0 {
code |= 0x80
}
ok = true
return
}
if i == 5 {
break
}
arg = arg<<7 | uint32(v&0x7f)
}
return
}
func putVarCode(buf []byte, arg uint32, code uint8) (n int) {
var (
tmp [6]byte
i int
)
tmp[i] = code & 0x7f
if code&0x80 != 0 {
i++
for ; i < len(tmp); i++ {
tmp[i] = byte(arg) | 0x80
arg >>= 7
if arg == 0 {
break
}
}
}
for i >= 0 && n < len(buf) {
buf[n] = tmp[i]
i--
n++
}
if i >= 0 {
return -n
}
return n
}
func varCodeLength(arg uint32, code uint8) (n int) {
n++
if code&0x80 == 0 {
return
}
for {
n++
arg >>= 7
if arg == 0 {
break
}
}
return
}