-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.go
387 lines (353 loc) · 8.96 KB
/
compact.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package scale
import (
"bytes"
"errors"
"math"
"math/big"
)
// Compact base interface for encoding and decoding. We redefine
// the way to encode and decode all the types that supported by us.
// For more details see [SCALE Codec](https://docs.substrate.io/reference/scale-codec/)
type Compact interface {
Encode() ([]byte, error)
Decode(val []byte) (int, error)
GetVal() interface{}
GetType() PrimitiveType
CloneNew() Compact
}
// CompactU8 one byte uint in compact mode
type CompactU8 struct {
Val uint8
}
func (c *CompactU8) Encode() ([]byte, error) {
if c.Val > 255 {
return nil, U8OutOfRange
}
if c.Val > 63 && int(c.Val) <= MaxDouble {
temp := uint16(c.Val)
temp = temp<<MoveBit + DoubleMode
return []byte{byte(temp), byte(temp >> 8)}, nil
}
return []byte{c.Val<<MoveBit + SingleMode}, nil
}
func (c *CompactU8) Decode(val []byte) (int, error) {
switch val[0] % 4 {
case SingleMode:
c.Val = val[0] >> MoveBit
return 1, nil
case DoubleMode:
temp := uint16(val[0]>>MoveBit) | (uint16(val[1])<<8)>>MoveBit
if temp > 63 && temp <= 255 {
c.Val = uint8(temp)
} else {
return 0, U8OutOfRange
}
return 2, nil
default:
return 0, errors.New("unexpected prefix decoding Compact<u8>")
}
}
func (c *CompactU8) GetVal() interface{} {
return c.Val
}
func (c *CompactU8) GetType() PrimitiveType {
return CompactUInt8
}
func (c *CompactU8) CloneNew() Compact {
return &CompactU8{Val: c.Val}
}
// CompactU16 two bytes uint in compact mode
type CompactU16 struct {
Val uint16
}
func (u *CompactU16) GetVal() interface{} {
return u.Val
}
func (u *CompactU16) Encode() ([]byte, error) {
if u.Val > math.MaxUint16 {
return nil, U16OutOfRange
}
if u.Val <= MaxSingle {
s := uint8(u.Val)
return []byte{s<<MoveBit + SingleMode}, nil
} else if u.Val <= MaxDouble {
u.Val = u.Val<<MoveBit + DoubleMode
return []byte{byte(u.Val), byte(u.Val >> 8)}, nil
} else {
s := uint32(u.Val)
s = s<<MoveBit + FourByteMode
return []byte{byte(s), byte(s >> 8), byte(s >> 16), byte(s >> 24)}, nil
}
}
func (u *CompactU16) Decode(val []byte) (int, error) {
switch val[0] % 4 {
case SingleMode:
u.Val = uint16(val[0]) >> MoveBit
return 1, nil
case DoubleMode:
u.Val = uint16(val[0]>>MoveBit) | (uint16(val[1])<<8)>>MoveBit
return 2, nil
case FourByteMode:
temp := uint32(val[0]>>MoveBit) | (uint32(val[1])<<8|uint32(val[2])<<16|(uint32(val[3])<<24))>>MoveBit
if temp > MaxDouble && temp <= math.MaxUint16 {
u.Val = uint16(temp)
return 4, nil
} else {
return 0, U16OutOfRange
}
default:
return 0, errors.New("unexpected prefix decoding Compact<u16>")
}
}
func (u *CompactU16) GetType() PrimitiveType {
return CompactUint16
}
func (u *CompactU16) CloneNew() Compact {
return &CompactU16{Val: u.Val}
}
// CompactU32 four bytes uint
type CompactU32 struct {
Val uint32
}
func (u *CompactU32) GetVal() interface{} {
return u.Val
}
func (u *CompactU32) Encode() ([]byte, error) {
if u.Val > math.MaxUint32 {
return nil, U32OutOfRange
}
if u.Val <= MaxSingle {
s := uint8(u.Val)
return []byte{s<<MoveBit + SingleMode}, nil
} else if u.Val <= MaxDouble {
s := uint16(u.Val)
s = s<<MoveBit + DoubleMode
return []byte{byte(s), byte(s >> 8)}, nil
} else if u.Val <= MaxFour {
u.Val = u.Val<<MoveBit + FourByteMode
return []byte{byte(u.Val), byte(u.Val >> 8), byte(u.Val >> 16), byte(u.Val >> 24)}, nil
} else {
return []byte{BigIntMode, byte(u.Val), byte(u.Val >> 8), byte(u.Val >> 16), byte(u.Val >> 24)}, nil
}
}
func (u *CompactU32) Decode(val []byte) (int, error) {
switch val[0] % 4 {
case SingleMode:
u.Val = uint32(val[0] >> MoveBit)
return 1, nil
case DoubleMode:
temp := uint16(val[0]>>MoveBit) | (uint16(val[1])<<8)>>MoveBit
if temp > 63 && temp < MaxDouble {
u.Val = uint32(temp)
return 2, nil
} else {
return 0, U32OutOfRange
}
case FourByteMode:
u.Val = uint32(val[0]>>MoveBit) | (uint32(val[1])<<8|uint32(val[2])<<16|uint32(val[3])<<24)>>MoveBit
return 4, nil
case BigIntMode:
if val[0]>>MoveBit == 0 {
temp := uint32(val[1]) | uint32(val[2])<<8 | uint32(val[3])<<16 | uint32(val[4])<<24
if temp > math.MaxUint32>>2 {
u.Val = temp
return 5, nil
} else {
return 0, U32OutOfRange
}
}
}
return 0, nil
}
func (u *CompactU32) GetType() PrimitiveType {
return CompactUint32
}
func (u *CompactU32) CloneNew() Compact {
return &CompactU32{Val: u.Val}
}
type CompactU64 struct {
Val uint64
}
func (c *CompactU64) GetVal() interface{} {
return c.Val
}
func (c *CompactU64) Encode() ([]byte, error) {
if c.Val > uint64(math.MaxUint64) {
return nil, U64OutOfRange
}
if c.Val <= MaxSingle {
s := uint8(c.Val)
return []byte{s<<MoveBit + SingleMode}, nil
} else if c.Val <= MaxDouble {
s := uint16(c.Val)
s = s<<MoveBit + DoubleMode
return []byte{byte(s), byte(s >> 8)}, nil
} else if c.Val <= MaxFour {
s := uint32(c.Val)
s = s<<MoveBit + FourByteMode
return []byte{byte(s), byte(s >> 8), byte(s >> 16), byte(s >> 24)}, nil
} else {
res := BaseIntEncode.EncodeUInt64(c.Val)
var buf bytes.Buffer
end := len(res)
for i := len(res) - 1; i >= 0; i-- {
if res[i] != 0 {
break
}
end--
}
buf.WriteByte(byte((end-4)<<MoveBit + BigIntMode))
buf.Write(res[:end])
return buf.Bytes(), nil
}
}
func (c *CompactU64) Decode(val []byte) (int, error) {
switch val[0] % 4 {
case SingleMode:
c.Val = uint64(val[0] >> MoveBit)
return 1, nil
case DoubleMode:
temp := uint16(val[0]>>MoveBit) | (uint16(val[1])<<8)>>MoveBit
if temp > 63 && temp < MaxDouble {
c.Val = uint64(temp)
return 2, nil
} else {
return 0, U32OutOfRange
}
case FourByteMode:
temp := uint32(val[0]>>MoveBit) | (uint32(val[1])<<8|uint32(val[2])<<16|uint32(val[3])<<24)>>MoveBit
if temp > MaxDouble && temp < math.MaxUint32>>2 {
c.Val = uint64(temp)
return 4, nil
} else {
return 0, U64OutOfRange
}
case BigIntMode:
ll := int(val[0]>>MoveBit) + 4
if ll == 4 {
temp := BaseIntEncode.UInt32(val[1:])
if temp > math.MaxUint32>>2 {
c.Val = uint64(temp)
} else {
return 0, U64OutOfRange
}
} else if ll == 8 {
temp := BaseIntEncode.UInt64(val[1:])
if temp > math.MaxUint64>>2 {
c.Val = temp
} else {
return 0, U64OutOfRange
}
} else if ll > 8 {
return 0, errors.New("unexpected prefix decoding Compact<u64>")
} else {
var buf bytes.Buffer
buf.Write(val[1:])
if ll < 8 {
before := make([]byte, 8-ll)
buf.Write(before)
}
c.Val = BaseIntEncode.UInt64(buf.Bytes())
}
return ll + 1, nil
}
return 0, nil
}
func (c *CompactU64) GetType() PrimitiveType {
return CompactUint64
}
func (c *CompactU64) CloneNew() Compact {
return &CompactU64{Val: c.Val}
}
type CompactU128 struct {
Val *big.Int
}
func (c *CompactU128) GetVal() interface{} {
return c.Val
}
func (c *CompactU128) GetType() PrimitiveType {
return CompactBigInt
}
func (c *CompactU128) Encode() ([]byte, error) {
temp := c.Val.Uint64()
if temp <= MaxSingle {
s := uint8(temp)
return []byte{s<<MoveBit + SingleMode}, nil
} else if temp <= MaxDouble {
s := uint16(temp)
s = s<<MoveBit + DoubleMode
return []byte{byte(s), byte(s >> 8)}, nil
} else if temp <= MaxFour {
s := uint32(temp)
s = s<<MoveBit + FourByteMode
return []byte{byte(s), byte(s >> 8), byte(s >> 16), byte(s >> 24)}, nil
} else {
res := BaseIntEncode.EncodeU128(c.Val)
var buf bytes.Buffer
end := len(res)
for i := len(res) - 1; i >= 0; i-- {
if res[i] != 0 {
break
}
end--
}
buf.WriteByte(byte((end-4)<<MoveBit + BigIntMode))
buf.Write(res[:end])
return buf.Bytes(), nil
}
}
func (c *CompactU128) Decode(val []byte) (int, error) {
switch val[0] % 4 {
case SingleMode:
c.Val = new(big.Int).SetBytes([]byte{val[0] >> MoveBit})
return 1, nil
case DoubleMode:
temp := uint16(val[0]>>MoveBit) | (uint16(val[1])<<8)>>MoveBit
if temp > 63 && temp < MaxDouble {
c.Val = new(big.Int).SetUint64(uint64(temp))
return 2, nil
} else {
return 0, U128OutOfRange
}
case FourByteMode:
temp := uint32(val[0]>>MoveBit) | (uint32(val[1])<<8|uint32(val[2])<<16|uint32(val[3])<<24)>>MoveBit
if temp > MaxDouble && temp < math.MaxUint32>>2 {
c.Val = new(big.Int).SetUint64(uint64(temp))
return 4, nil
} else {
return 0, U128OutOfRange
}
case BigIntMode:
ll := int(val[0]>>MoveBit) + 4
if ll == 4 {
temp := BaseIntEncode.UInt32(val[1:])
if temp > math.MaxUint32>>2 {
c.Val = new(big.Int).SetUint64(uint64(temp))
} else {
return 0, U128OutOfRange
}
} else if ll == 8 {
temp := BaseIntEncode.UInt64(val[1:])
if temp > math.MaxUint64>>2 {
c.Val = new(big.Int).SetUint64(temp)
} else {
return 0, U128OutOfRange
}
} else if ll > 16 {
return 0, errors.New("unexpected prefix decoding Compact<u128>")
} else {
var buf bytes.Buffer
buf.Write(val[1:])
if ll < 16 {
before := make([]byte, 16-ll)
buf.Write(before)
}
c.Val = BaseIntEncode.U128(buf.Bytes())
}
return ll + 1, nil
}
return 0, nil
}
func (c *CompactU128) CloneNew() Compact {
return &CompactU128{Val: new(big.Int).SetBytes(c.Val.Bytes())}
}