-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
82 lines (73 loc) · 1.35 KB
/
reader.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package wasm_go
import (
"io"
)
type leb128Reader struct {
bytes []byte
pos int
}
func (r *leb128Reader) eatBytes(length uint32) ([]byte, error) {
end := r.pos + int(length)
if end > len(r.bytes) {
return nil, io.EOF
}
bs := r.bytes[r.pos : r.pos+int(length)]
r.pos += int(length)
return bs, nil
}
func (r *leb128Reader) eatString(length uint32) (string, error) {
b, err := r.eatBytes(length)
if err != nil {
return "", err
}
return string(b), err
}
func (r *leb128Reader) eatU8() (uint8, error) {
if r.pos >= len(r.bytes) {
return 0, io.EOF
}
r.pos += 1
return r.bytes[r.pos-1], nil
}
func (r *leb128Reader) eatU64() (uint64, error) {
v, shift := uint64(0), 0
for {
u8, err := r.eatU8()
if err != nil {
return 0, err
}
v |= (uint64(u8) & 0x7F) << shift
shift += 7
if u8&0x80>>7 == 0 {
break
}
}
return v, nil
}
func (r *leb128Reader) eatI64() (int64, error) {
v, shift := int64(0), 0
for {
u8, err := r.eatU8()
if err != nil {
return 0, err
}
v |= (int64(u8) & 0x7F) << shift
shift += 7
if u8&0x80>>7 == 0 {
if u8&0x40>>3 != 0 {
// negative number
v |= ^0 << shift
}
break
}
}
return v, nil
}
func (r *leb128Reader) eatI32() (int32, error) {
v, err := r.eatI64()
return int32(v), err
}
func (r *leb128Reader) eatU32() (uint32, error) {
v, err := r.eatU64()
return uint32(v), err
}