-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.go
327 lines (264 loc) · 5.72 KB
/
fix.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
package scale
import (
"bytes"
"errors"
"math"
"math/big"
)
type FixU8 struct {
Val uint8
}
func (f *FixU8) Encode() ([]byte, error) {
return []byte{f.Val}, nil
}
func (f *FixU8) Decode(val []byte) (int, error) {
f.Val = val[0]
return 1, nil
}
func (f *FixU8) GetVal() interface{} {
return f.Val
}
func (f *FixU8) GetType() PrimitiveType {
return Uint8
}
func (f *FixU8) CloneNew() Compact {
return &FixU8{Val: f.Val}
}
type FixI8 struct {
Val int8
}
func (f *FixI8) Encode() ([]byte, error) {
return []byte{byte(f.Val)}, nil
}
func (f *FixI8) Decode(val []byte) (int, error) {
f.Val = int8(val[0])
return 1, nil
}
func (f *FixI8) GetVal() interface{} {
return f.Val
}
func (f *FixI8) GetType() PrimitiveType {
return Int8
}
func (f *FixI8) CloneNew() Compact {
return &FixI8{Val: f.Val}
}
type FixU16 struct {
Val uint16
}
func (f *FixU16) Encode() ([]byte, error) {
return []byte{byte(f.Val), byte(f.Val >> 8)}, nil
}
func (f *FixU16) Decode(val []byte) (int, error) {
f.Val = uint16(val[0]) | uint16(val[1])<<8
return 2, nil
}
func (f *FixU16) GetVal() interface{} {
return f.Val
}
func (f *FixU16) GetType() PrimitiveType {
return Uint16
}
func (f *FixU16) CloneNew() Compact {
return &FixU16{Val: f.Val}
}
type FixI16 struct {
Val int16
}
func (f *FixI16) Encode() ([]byte, error) {
return []byte{byte(f.Val), byte(f.Val >> 8)}, nil
}
func (f *FixI16) Decode(val []byte) (int, error) {
f.Val = int16(val[0]) | int16(val[1])<<8
return 2, nil
}
func (f *FixI16) GetVal() interface{} {
return f.Val
}
func (f *FixI16) GetType() PrimitiveType {
return Int16
}
func (f *FixI16) CloneNew() Compact {
return &FixI16{Val: f.Val}
}
type FixU32 struct {
Val uint32
}
func (f *FixU32) Encode() ([]byte, error) {
return []byte{byte(f.Val), byte(f.Val >> 8), byte(f.Val >> 16), byte(f.Val >> 24)}, nil
}
func (f *FixU32) Decode(val []byte) (int, error) {
f.Val = uint32(val[0]) | uint32(val[1])<<8 | uint32(val[2])<<16 | uint32(val[3])<<24
return 4, nil
}
func (f *FixU32) GetVal() interface{} {
return f.Val
}
func (f *FixU32) GetType() PrimitiveType {
return Uint32
}
func (f *FixU32) CloneNew() Compact {
return &FixU32{Val: f.Val}
}
type FixI32 struct {
Val int32
}
func (f *FixI32) Encode() ([]byte, error) {
return []byte{byte(f.Val), byte(f.Val >> 8), byte(f.Val >> 16), byte(f.Val >> 24)}, nil
}
func (f *FixI32) Decode(val []byte) (int, error) {
f.Val = int32(val[0]) | int32(val[1])<<8 | int32(val[2])<<16 | int32(val[3])<<24
return 4, nil
}
func (f *FixI32) GetVal() interface{} {
return f.Val
}
func (f *FixI32) GetType() PrimitiveType {
return Int32
}
func (f *FixI32) CloneNew() Compact {
return &FixI32{Val: f.Val}
}
type FixU64 struct {
Val uint64
}
func (f *FixU64) Encode() ([]byte, error) {
return []byte{
byte(f.Val),
byte(f.Val >> 8),
byte(f.Val >> 16),
byte(f.Val >> 24),
byte(f.Val >> 32),
byte(f.Val >> 40),
byte(f.Val >> 48),
byte(f.Val >> 56),
}, nil
}
func (f *FixU64) Decode(val []byte) (int, error) {
f.Val = uint64(val[0]) | uint64(val[1])<<8 | uint64(val[2])<<16 | uint64(val[3])<<24 | uint64(val[4])<<32 | uint64(val[5])<<40 | uint64(val[6])<<48 | uint64(val[7])<<56
return 8, nil
}
func (f *FixU64) GetVal() interface{} {
return f.Val
}
func (f *FixU64) GetType() PrimitiveType {
return Uint64
}
func (f *FixU64) CloneNew() Compact {
return &FixU64{Val: f.Val}
}
type FixI64 struct {
Val int64
}
func (f *FixI64) Encode() ([]byte, error) {
return []byte{
byte(f.Val),
byte(f.Val >> 8),
byte(f.Val >> 16),
byte(f.Val >> 24),
byte(f.Val >> 32),
byte(f.Val >> 40),
byte(f.Val >> 48),
byte(f.Val >> 56),
}, nil
}
func (f *FixI64) Decode(val []byte) (int, error) {
f.Val = int64(val[0]) | int64(val[1])<<8 | int64(val[2])<<16 | int64(val[3])<<24 | int64(val[4])<<32 | int64(val[5])<<40 | int64(val[6])<<48 | int64(val[7])<<56
return 8, nil
}
func (f *FixI64) GetVal() interface{} {
return f.Val
}
func (f *FixI64) GetType() PrimitiveType {
return Int64
}
func (f *FixI64) CloneNew() Compact {
return &FixI64{Val: f.Val}
}
type FixU128 struct {
Val *big.Int
}
func (f *FixU128) Encode() ([]byte, error) {
var buf bytes.Buffer
origin := f.Val.Bytes()
if len(origin) > 16 {
return nil, errors.New("out of u128 range")
}
for i := len(origin) - 1; i >= 0; i-- {
buf.WriteByte(origin[i])
}
if len(origin) < 16 {
ap := make([]byte, 16-len(origin))
buf.Write(ap)
}
return buf.Bytes(), nil
}
func (f *FixU128) Decode(val []byte) (int, error) {
var v big.Int
if len(val) != 16 {
return 0, errors.New("out of u128 range")
}
v.SetBytes(reverse(val))
f.Val = &v
return 16, nil
}
func (f *FixU128) GetVal() interface{} {
return f.Val
}
func (f *FixU128) GetType() PrimitiveType {
return BigUint
}
func (f *FixU128) CloneNew() Compact {
return &FixU128{Val: new(big.Int).SetBytes(f.Val.Bytes())}
}
type FixI128 struct {
Val *big.Int
}
func (f *FixI128) Encode() ([]byte, error) {
var buf bytes.Buffer
val := f.Val.Bytes()
if len(val) > 16 {
return nil, errors.New("out of u128 range")
}
sign := f.Val.Sign()
if sign < 0 {
negchange(val)
}
val = reverse(val)
buf.Write(val)
if len(val) < 16 {
for i := 0; i < 16-len(val); i++ {
if sign < 0 {
buf.WriteByte(255)
} else {
buf.WriteByte(0)
}
}
}
return buf.Bytes(), nil
}
func (f *FixI128) Decode(val []byte) (int, error) {
v := new(big.Int)
if len(val) != 16 {
return 0, errors.New("out of range i128")
}
res := reverse(val)
if res[0] > math.MaxInt8 {
negchange(res)
v.SetBytes(res)
v.Neg(v)
} else {
v.SetBytes(res)
}
f.Val = v
return 16, nil
}
func (f *FixI128) GetVal() interface{} {
return f.Val
}
func (f *FixI128) GetType() PrimitiveType {
return BigInt
}
func (f *FixI128) CloneNew() Compact {
return &FixI128{Val: new(big.Int).SetBytes(f.Val.Bytes())}
}