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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
package leb128
import (
"errors"
"fmt"
"io"
)
const (
maxVarintLen32 = 5
maxVarintLen33 = maxVarintLen32
maxVarintLen64 = 10
int33Mask int64 = 1 << 7
int33Mask2 = ^int33Mask
int33Mask3 = 1 << 6
int33Mask4 = 8589934591 // 2^33-1
int33Mask5 = 1 << 32
int33Mask6 = int33Mask4 + 1 // 2^33
int64Mask3 = 1 << 6
int64Mask4 = ^0
)
var (
errOverflow32 = errors.New("overflows a 32-bit integer")
errOverflow33 = errors.New("overflows a 33-bit integer")
errOverflow64 = errors.New("overflows a 64-bit integer")
)
// EncodeInt32 encodes the signed value into a buffer in LEB128 format
//
// See https://en.wikipedia.org/wiki/LEB128#Encode_signed_integer
func EncodeInt32(value int32) []byte {
return EncodeInt64(int64(value))
}
// EncodeInt64 encodes the signed value into a buffer in LEB128 format
//
// See https://en.wikipedia.org/wiki/LEB128#Encode_signed_integer
func EncodeInt64(value int64) (buf []byte) {
for {
// Take 7 remaining low-order bits from the value into b.
b := uint8(value & 0x7f)
// Extract the sign bit.
s := uint8(value & 0x40)
value >>= 7
// The encoding unsigned numbers is simpler as it only needs to check if the value is non-zero to tell if there
// are more bits to encode. Signed is a little more complicated as you have to double-check the sign bit.
// If either case, set the high-order bit to tell the reader there are more bytes in this int.
if (value != -1 || s == 0) && (value != 0 || s != 0) {
b |= 0x80
}
// Append b into the buffer
buf = append(buf, b)
if b&0x80 == 0 {
break
}
}
return buf
}
// EncodeUint32 encodes the value into a buffer in LEB128 format
//
// See https://en.wikipedia.org/wiki/LEB128#Encode_unsigned_integer
func EncodeUint32(value uint32) []byte {
return EncodeUint64(uint64(value))
}
// EncodeUint64 encodes the value into a buffer in LEB128 format
//
// See https://en.wikipedia.org/wiki/LEB128#Encode_unsigned_integer
func EncodeUint64(value uint64) (buf []byte) {
// This is effectively a do/while loop where we take 7 bits of the value and encode them until it is zero.
for {
// Take 7 remaining low-order bits from the value into b.
b := uint8(value & 0x7f)
value = value >> 7
// If there are remaining bits, the value won't be zero: Set the high-
// order bit to tell the reader there are more bytes in this uint.
if value != 0 {
b |= 0x80
}
// Append b into the buffer
buf = append(buf, b)
if b&0x80 == 0 {
return buf
}
}
}
type nextByte func(i int) (byte, error)
func DecodeUint32(r io.ByteReader) (ret uint32, bytesRead uint64, err error) {
return decodeUint32(func(_ int) (byte, error) { return r.ReadByte() })
}
func LoadUint32(buf []byte) (ret uint32, bytesRead uint64, err error) {
return decodeUint32(func(i int) (byte, error) {
if i >= len(buf) {
return 0, io.EOF
}
return buf[i], nil
})
}
func decodeUint32(next nextByte) (ret uint32, bytesRead uint64, err error) {
// Derived from https://github.com/golang/go/blob/go1.20/src/encoding/binary/varint.go
// with the modification on the overflow handling tailored for 32-bits.
var s uint32
for i := 0; i < maxVarintLen32; i++ {
b, err := next(i)
if err != nil {
return 0, 0, err
}
if b < 0x80 {
// Unused bits must be all zero.
if i == maxVarintLen32-1 && (b&0xf0) > 0 {
return 0, 0, errOverflow32
}
return ret | uint32(b)<<s, uint64(i) + 1, nil
}
ret |= (uint32(b) & 0x7f) << s
s += 7
}
return 0, 0, errOverflow32
}
func LoadUint64(buf []byte) (ret uint64, bytesRead uint64, err error) {
bufLen := len(buf)
if bufLen == 0 {
return 0, 0, io.EOF
}
// Derived from https://github.com/golang/go/blob/go1.20/src/encoding/binary/varint.go
var s uint64
for i := 0; i < maxVarintLen64; i++ {
if i >= bufLen {
return 0, 0, io.EOF
}
b := buf[i]
if b < 0x80 {
// Unused bits (non first bit) must all be zero.
if i == maxVarintLen64-1 && b > 1 {
return 0, 0, errOverflow64
}
return ret | uint64(b)<<s, uint64(i) + 1, nil
}
ret |= (uint64(b) & 0x7f) << s
s += 7
}
return 0, 0, errOverflow64
}
func DecodeInt32(r io.ByteReader) (ret int32, bytesRead uint64, err error) {
return decodeInt32(func(_ int) (byte, error) { return r.ReadByte() })
}
func LoadInt32(buf []byte) (ret int32, bytesRead uint64, err error) {
return decodeInt32(func(i int) (byte, error) {
if i >= len(buf) {
return 0, io.EOF
}
return buf[i], nil
})
}
func decodeInt32(next nextByte) (ret int32, bytesRead uint64, err error) {
var shift int
var b byte
for {
b, err = next(int(bytesRead))
if err != nil {
return 0, 0, fmt.Errorf("readByte failed: %w", err)
}
ret |= (int32(b) & 0x7f) << shift
shift += 7
bytesRead++
if b&0x80 == 0 {
if shift < 32 && (b&0x40) != 0 {
ret |= ^0 << shift
}
// Over flow checks.
// fixme: can be optimized.
if bytesRead > maxVarintLen32 {
return 0, 0, errOverflow32
} else if unused := b & 0b00110000; bytesRead == maxVarintLen32 && ret < 0 && unused != 0b00110000 {
return 0, 0, errOverflow32
} else if bytesRead == maxVarintLen32 && ret >= 0 && unused != 0x00 {
return 0, 0, errOverflow32
}
return
}
}
}
// DecodeInt33AsInt64 is a special cased decoder for wasm.BlockType which is encoded as a positive signed integer, yet
// still needs to fit the 32-bit range of allowed indices. Hence, this is 33, not 32-bit!
//
// See https://webassembly.github.io/spec/core/binary/instructions.html#control-instructions
func DecodeInt33AsInt64(r io.ByteReader) (ret int64, bytesRead uint64, err error) {
var shift int
var b int64
var rb byte
for shift < 35 {
rb, err = r.ReadByte()
if err != nil {
return 0, 0, fmt.Errorf("readByte failed: %w", err)
}
b = int64(rb)
ret |= (b & int33Mask2) << shift
shift += 7
bytesRead++
if b&int33Mask == 0 {
break
}
}
// fixme: can be optimized
if shift < 33 && (b&int33Mask3) == int33Mask3 {
ret |= int33Mask4 << shift
}
ret = ret & int33Mask4
// if 33rd bit == 1, we translate it as a corresponding signed-33bit minus value
if ret&int33Mask5 > 0 {
ret = ret - int33Mask6
}
// Over flow checks.
// fixme: can be optimized.
if bytesRead > maxVarintLen33 {
return 0, 0, errOverflow33
} else if unused := b & 0b00100000; bytesRead == maxVarintLen33 && ret < 0 && unused != 0b00100000 {
return 0, 0, errOverflow33
} else if bytesRead == maxVarintLen33 && ret >= 0 && unused != 0x00 {
return 0, 0, errOverflow33
}
return ret, bytesRead, nil
}
func DecodeInt64(r io.ByteReader) (ret int64, bytesRead uint64, err error) {
return decodeInt64(func(_ int) (byte, error) { return r.ReadByte() })
}
func LoadInt64(buf []byte) (ret int64, bytesRead uint64, err error) {
return decodeInt64(func(i int) (byte, error) {
if i >= len(buf) {
return 0, io.EOF
}
return buf[i], nil
})
}
func decodeInt64(next nextByte) (ret int64, bytesRead uint64, err error) {
var shift int
var b byte
for {
b, err = next(int(bytesRead))
if err != nil {
return 0, 0, fmt.Errorf("readByte failed: %w", err)
}
ret |= (int64(b) & 0x7f) << shift
shift += 7
bytesRead++
if b&0x80 == 0 {
if shift < 64 && (b&int64Mask3) == int64Mask3 {
ret |= int64Mask4 << shift
}
// Over flow checks.
// fixme: can be optimized.
if bytesRead > maxVarintLen64 {
return 0, 0, errOverflow64
} else if unused := b & 0b00111110; bytesRead == maxVarintLen64 && ret < 0 && unused != 0b00111110 {
return 0, 0, errOverflow64
} else if bytesRead == maxVarintLen64 && ret >= 0 && unused != 0x00 {
return 0, 0, errOverflow64
}
return
}
}
}
|