-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec.go
344 lines (303 loc) · 8.71 KB
/
codec.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2018 Jsgenesis
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package noreflect
// Adapted from https://github.com/Joystream/parity-codec-go
import (
"bytes"
"encoding/binary"
"io"
"strconv"
)
// Implementation for Parity codec in Tinygo.
// Derived from https://github.com/paritytech/parity-codec/
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
func check(err error) {
if err != nil {
panic(err)
}
}
// Encoder is a wrapper around a Writer that allows encoding data items to a stream.
type Encoder struct {
Writer io.Writer
}
// Write several bytes to the encoder.
func (pe Encoder) Write(bytes []byte) {
c, err := pe.Writer.Write(bytes)
check(err)
if c < len(bytes) {
panic("Could not write " + strconv.Itoa(len(bytes)) + " bytes to writer")
}
}
// PushByte writes a single byte to an encoder.
func (pe Encoder) PushByte(b byte) {
pe.Write([]byte{b})
}
// EncodeUintCompact writes an unsigned integer to the stream using the compact encoding.
// A typical usage is storing the length of a collection.
// Definition of compact encoding:
// 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
// xx xx xx 00 (0 ... 2**6 - 1) (u8)
// yL yL yL 01 / yH yH yH yL (2**6 ... 2**14 - 1) (u8, u16) low LH high
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 ... 2**30 - 1) (u16, u32) low LMMH high
// nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
// Rust implementation: see impl<'a> Encode for CompactRef<'a, u64>
func (pe Encoder) EncodeUintCompact(v uint64) {
// TODO: handle numbers wide than 64 bits (byte slices?)
// Currently, Rust implementation only seems to support u128
buf := make([]byte, 8)
if v < 1<<30 {
if v < 1<<6 {
pe.PushByte(byte(v) << 2)
} else if v < 1<<14 {
binary.LittleEndian.PutUint16(buf, uint16(v<<2)+1)
pe.Write(buf[:2])
} else {
binary.LittleEndian.PutUint32(buf, uint32(v<<2)+2)
pe.Write(buf[:4])
}
return
}
n := byte(0)
limit := uint64(1 << 32)
for v >= limit && limit > 256 { // when overflows, limit will be < 256
n++
limit <<= 8
}
if n > 4 {
panic("Assertion error: n>4 needed to compact-encode uint64")
}
pe.PushByte((n << 2) + 3)
binary.LittleEndian.PutUint64(buf, v)
pe.Write(buf[:4+n])
}
// TODO: codegeneration
func (pe Encoder) EncodeUint(value uint64, bytes int) {
mask := uint64(255)
for i := 0; i < bytes; i++ {
pe.PushByte(byte(value & mask))
value = value >> 8
}
}
func (pe Encoder) EncodeInt(value int64, bytes int) {
mask := int64(255)
for i := 0; i < bytes; i++ {
pe.PushByte(byte(value & mask))
value = value >> 8
}
}
func (pe Encoder) EncodeBool(b bool) {
if b {
pe.PushByte(1)
} else {
pe.PushByte(0)
}
}
func (pe Encoder) EncodeByteSlice(value []byte) {
pe.EncodeUintCompact(uint64(len(value)))
pe.Write(value)
}
func (pe Encoder) EncodeString(value string) {
pe.EncodeByteSlice([]byte(value))
}
// EncodeOption stores optionally present value to the stream.
func (pe Encoder) EncodeOption(hasValue bool, value Encodeable) {
if !hasValue {
pe.PushByte(0)
} else {
pe.PushByte(1)
value.ParityEncode(pe)
}
}
// Decoder - a wraper around a Reader that allows decoding data items from a stream.
// Unlike Rust implementations, decoder methods do not return success state, but just
// panic on error. Since decoding failue is an "unexpected" error, this approach should
// be justified.
type Decoder struct {
Reader io.Reader
}
// Read reads bytes from a stream into a buffer and panics if cannot read the required
// number of bytes.
func (pd Decoder) Read(bytes []byte) {
c, err := pd.Reader.Read(bytes)
check(err)
if c < len(bytes) {
panic("Cannot read the required number of bytes " + strconv.Itoa(len(bytes)) + ", only " + strconv.Itoa(c) + " available")
}
}
// ReadOneByte reads a next byte from the stream.
// Named so to avoid a linter warning about a clash with io.ByteReader.ReadByte
func (pd Decoder) ReadOneByte() byte {
buf := []byte{0}
pd.Read(buf)
return buf[0]
}
func (pd Decoder) DecodeUint(bytes uint) uint64 {
var value uint64
for i := uint(0); i < bytes; i++ {
b := uint64(pd.ReadOneByte())
value |= b << (i * 8)
}
return value
}
func (pd Decoder) DecodeInt(bytes uint) int64 {
var value uint64
var b uint64
for i := uint(0); i < bytes; i++ {
b = uint64(pd.ReadOneByte())
value |= b << (i * 8)
}
if b >= 128 {
for i := bytes; i < 8; i++ {
value |= b << 255
}
}
return int64(value)
}
func (pd Decoder) DecodeBool() bool {
return pd.ReadOneByte() > 0
}
// DecodeUintCompact decodes a compact-encoded integer. See EncodeUintCompact method.
func (pd Decoder) DecodeUintCompact() uint64 {
b := pd.ReadOneByte()
mode := b & 3
switch mode {
case 0:
return uint64(b >> 2)
case 1:
r := uint64(pd.ReadOneByte())
r <<= 6
r += uint64(b >> 2)
return r
case 2:
buf := make([]byte, 4)
buf[0] = b
pd.Read(buf[1:4])
r := binary.LittleEndian.Uint32(buf)
r >>= 2
return uint64(r)
case 3:
n := b >> 2
if n > 4 {
panic("Not supported: n>4 encountered when decoding a compact-encoded uint")
}
buf := make([]byte, 8)
pd.Read(buf[:n+4])
return binary.LittleEndian.Uint64(buf)
default:
panic("Code should be unreachable")
}
}
func (pd Decoder) DecodeByteSlice() []byte {
value := make([]byte, pd.DecodeUintCompact())
pd.Read(value)
return value
}
func (pd Decoder) DecodeString() string {
return string(pd.DecodeByteSlice())
}
// DecodeOption decodes a optionally available value into a boolean presence field and a value.
func (pd Decoder) DecodeOption(hasValue *bool, valuePointer Decodeable) {
b := pd.ReadOneByte()
switch b {
case 0:
*hasValue = false
case 1:
*hasValue = true
valuePointer.ParityDecode(pd)
default:
panic("Unknown byte prefix for encoded Option: " + strconv.Itoa(int(b)))
}
}
// Encodeable is an interface that defines a custom encoding rules for a data type.
// Should be defined for structs (not pointers to them).
// See OptionBool for an example implementation.
type Encodeable interface {
// ParityEncode encodes and write this structure into a stream
ParityEncode(encoder Encoder)
}
// See []int16 in tests as an example
func (pe Encoder) EncodeCollection(length int, encodeElem func(int)) {
pe.EncodeUintCompact(uint64(length))
for i := 0; i < length; i++ {
encodeElem(i)
}
}
// Decodeable is an interface that defines a custom encoding rules for a data type.
// Should be defined for pointers to structs.
// See OptionBool for an example implementation.
type Decodeable interface {
// ParityDecode populates this structure from a stream (overwriting the current contents), return false on failure
ParityDecode(decoder Decoder)
}
// See []int16 in tests as an example
func (pd Decoder) DecodeCollection(setSize func(int), decodeElem func(int)) {
l := int(pd.DecodeUintCompact())
setSize(l)
for i := 0; i < l; i++ {
decodeElem(i)
}
}
// OptionBool is a structure that can store a boolean or a missing value.
// Note that encoding rules are slightly different from other "Option" fields.
type OptionBool struct {
hasValue bool
value bool
}
// NewOptionBoolEmpty creates an OptionBool without a value.
func NewOptionBoolEmpty() OptionBool {
return OptionBool{false, false}
}
// NewOptionBool creates an OptionBool with a value.
func NewOptionBool(value bool) OptionBool {
return OptionBool{true, value}
}
// ParityEncode implements encoding for OptionBool as per Rust implementation.
func (o OptionBool) ParityEncode(encoder Encoder) {
if !o.hasValue {
encoder.PushByte(0)
} else {
if o.value {
encoder.PushByte(1)
} else {
encoder.PushByte(2)
}
}
}
// ParityDecode implements decoding for OptionBool as per Rust implementation.
func (o *OptionBool) ParityDecode(decoder Decoder) {
b := decoder.ReadOneByte()
switch b {
case 0:
o.hasValue = false
o.value = false
case 1:
o.hasValue = true
o.value = true
case 2:
o.hasValue = true
o.value = false
default:
panic("Unknown byte prefix for encoded OptionBool: " + strconv.Itoa(int(b)))
}
}
func Encode(value Encodeable) []byte {
var buffer = bytes.Buffer{}
value.ParityEncode(Encoder{&buffer})
return buffer.Bytes()
}
func Decode(value Decodeable, encoded []byte) {
var buffer = bytes.NewBuffer(encoded)
value.ParityDecode(Decoder{buffer})
}