aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/rlp.go
blob: 0f88933b3ea5a630d3bfe06780cc932889791bbc (plain) (blame)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package ethutil

import (
    "bytes"
    _ "encoding/binary"
    "fmt"
    _ "log"
    _ "math"
    "math/big"
    "reflect"
)

///////////////////////////////////////
type EthEncoder interface {
    EncodeData(rlpData interface{}) []byte
}
type EthDecoder interface {
    Get(idx int) *RlpValue
}

//////////////////////////////////////

type RlpEncoder struct {
    rlpData []byte
}

func NewRlpEncoder() *RlpEncoder {
    encoder := &RlpEncoder{}

    return encoder
}
func (coder *RlpEncoder) EncodeData(rlpData interface{}) []byte {
    return Encode(rlpData)
}

// Data rlpValueutes are returned by the rlp decoder. The data rlpValueutes represents
// one item within the rlp data structure. It's responsible for all the casting
// It always returns something rlpValueid
type RlpValue struct {
    Value interface{}
    kind  reflect.Value
}

func (rlpValue *RlpValue) String() string {
    return fmt.Sprintf("%q", rlpValue.Value)
}

func Conv(rlpValue interface{}) *RlpValue {
    return &RlpValue{Value: rlpValue, kind: reflect.ValueOf(rlpValue)}
}

func NewRlpValue(rlpValue interface{}) *RlpValue {
    return &RlpValue{Value: rlpValue}
}

func (rlpValue *RlpValue) Type() reflect.Kind {
    return reflect.TypeOf(rlpValue.Value).Kind()
}

func (rlpValue *RlpValue) IsNil() bool {
    return rlpValue.Value == nil
}

func (rlpValue *RlpValue) Length() int {
    //return rlpValue.kind.Len()
    if data, ok := rlpValue.Value.([]interface{}); ok {
        return len(data)
    }

    return 0
}

func (rlpValue *RlpValue) AsRaw() interface{} {
    return rlpValue.Value
}

func (rlpValue *RlpValue) AsUint() uint64 {
    if Value, ok := rlpValue.Value.(uint8); ok {
        return uint64(Value)
    } else if Value, ok := rlpValue.Value.(uint16); ok {
        return uint64(Value)
    } else if Value, ok := rlpValue.Value.(uint32); ok {
        return uint64(Value)
    } else if Value, ok := rlpValue.Value.(uint64); ok {
        return Value
    }

    return 0
}

func (rlpValue *RlpValue) AsByte() byte {
    if Value, ok := rlpValue.Value.(byte); ok {
        return Value
    }

    return 0x0
}

func (rlpValue *RlpValue) AsBigInt() *big.Int {
    if a, ok := rlpValue.Value.([]byte); ok {
        b := new(big.Int)
        b.SetBytes(a)
        return b
    }

    return big.NewInt(0)
}

func (rlpValue *RlpValue) AsString() string {
    if a, ok := rlpValue.Value.([]byte); ok {
        return string(a)
    } else if a, ok := rlpValue.Value.(string); ok {
        return a
    } else {
        //panic(fmt.Sprintf("not string %T: %v", rlpValue.Value, rlpValue.Value))
    }

    return ""
}

func (rlpValue *RlpValue) AsBytes() []byte {
    if a, ok := rlpValue.Value.([]byte); ok {
        return a
    }

    return make([]byte, 0)
}

func (rlpValue *RlpValue) AsSlice() []interface{} {
    if d, ok := rlpValue.Value.([]interface{}); ok {
        return d
    }

    return []interface{}{}
}

func (rlpValue *RlpValue) AsSliceFrom(from int) *RlpValue {
    slice := rlpValue.AsSlice()

    return NewRlpValue(slice[from:])
}

func (rlpValue *RlpValue) AsSliceTo(to int) *RlpValue {
    slice := rlpValue.AsSlice()

    return NewRlpValue(slice[:to])
}

func (rlpValue *RlpValue) AsSliceFromTo(from, to int) *RlpValue {
    slice := rlpValue.AsSlice()

    return NewRlpValue(slice[from:to])
}

// Threat the rlpValueute as a slice
func (rlpValue *RlpValue) Get(idx int) *RlpValue {
    if d, ok := rlpValue.Value.([]interface{}); ok {
        // Guard for oob
        if len(d) <= idx {
            return NewRlpValue(nil)
        }

        if idx < 0 {
            panic("negative idx for Rlp Get")
        }

        return NewRlpValue(d[idx])
    }

    // If this wasn't a slice you probably shouldn't be using this function
    return NewRlpValue(nil)
}

func (rlpValue *RlpValue) Cmp(o *RlpValue) bool {
    return reflect.DeepEqual(rlpValue.Value, o.Value)
}

func (rlpValue *RlpValue) Encode() []byte {
    return Encode(rlpValue.Value)
}

func NewRlpValueFromBytes(rlpData []byte) *RlpValue {
    if len(rlpData) != 0 {
        data, _ := Decode(rlpData, 0)
        return NewRlpValue(data)
    }

    return NewRlpValue(nil)
}

// RlpValue value setters
// An empty rlp value is always a list
func EmptyRlpValue() *RlpValue {
    return NewRlpValue([]interface{}{})
}

func (rlpValue *RlpValue) AppendList() *RlpValue {
    list := EmptyRlpValue()
    rlpValue.Value = append(rlpValue.AsSlice(), list)

    return list
}

func (rlpValue *RlpValue) Append(v interface{}) *RlpValue {
    rlpValue.Value = append(rlpValue.AsSlice(), v)

    return rlpValue
}

/*
func FromBin(data []byte) uint64 {
    if len(data) == 0 {
        return 0
    }

    return FromBin(data[:len(data)-1])*256 + uint64(data[len(data)-1])
}
*/

const (
    RlpEmptyList = 0x80
    RlpEmptyStr  = 0x40
)

func Char(c []byte) int {
    if len(c) > 0 {
        return int(c[0])
    }

    return 0
}

func DecodeWithReader(reader *bytes.Buffer) interface{} {
    var slice []interface{}

    // Read the next byte
    char := Char(reader.Next(1))
    switch {
    case char == 0:
        return nil
    case char <= 0x7c:
        return char

    case char <= 0xb7:
        return reader.Next(int(char - 0x80))

    case char <= 0xbf:
        buff := bytes.NewReader(reader.Next(int(char - 0xb8)))
        length := ReadVarint(buff)

        return reader.Next(int(length))

    case char <= 0xf7:
        length := int(char - 0xc0)
        for i := 0; i < length; i++ {
            obj := DecodeWithReader(reader)
            if obj != nil {
                slice = append(slice, obj)
            } else {
                break
            }
        }

        return slice

    }

    return slice
}

// TODO Use a bytes.Buffer instead of a raw byte slice.
// Cleaner code, and use draining instead of seeking the next bytes to read
func Decode(data []byte, pos uint64) (interface{}, uint64) {
    /*
        if pos > uint64(len(data)-1) {
            log.Println(data)
            log.Panicf("index out of range %d for data %q, l = %d", pos, data, len(data))
        }
    */

    var slice []interface{}
    char := int(data[pos])
    switch {
    case char <= 0x7f:
        return data[pos], pos + 1

    case char <= 0xb7:
        b := uint64(data[pos]) - 0x80

        return data[pos+1 : pos+1+b], pos + 1 + b

    case char <= 0xbf:
        b := uint64(data[pos]) - 0xb7

        b2 := ReadVarint(bytes.NewReader(data[pos+1 : pos+1+b]))

        return data[pos+1+b : pos+1+b+b2], pos + 1 + b + b2

    case char <= 0xf7:
        b := uint64(data[pos]) - 0xc0
        prevPos := pos
        pos++
        for i := uint64(0); i < b; {
            var obj interface{}

            // Get the next item in the data list and append it
            obj, prevPos = Decode(data, pos)
            slice = append(slice, obj)

            // Increment i by the amount bytes read in the previous
            // read
            i += (prevPos - pos)
            pos = prevPos
        }
        return slice, pos

    case char <= 0xff:
        l := uint64(data[pos]) - 0xf7
        //b := BigD(data[pos+1 : pos+1+l]).Uint64()
        b := ReadVarint(bytes.NewReader(data[pos+1 : pos+1+l]))

        pos = pos + l + 1

        prevPos := b
        for i := uint64(0); i < uint64(b); {
            var obj interface{}

            obj, prevPos = Decode(data, pos)
            slice = append(slice, obj)

            i += (prevPos - pos)
            pos = prevPos
        }
        return slice, pos

    default:
        panic(fmt.Sprintf("byte not supported: %q", char))
    }

    return slice, 0
}

var (
    directRlp = big.NewInt(0x7f)
    numberRlp = big.NewInt(0xb7)
    zeroRlp   = big.NewInt(0x0)
)

func Encode(object interface{}) []byte {
    var buff bytes.Buffer

    if object != nil {
        switch t := object.(type) {
        case *RlpValue:
            buff.Write(Encode(t.AsRaw()))
        // Code dup :-/
        case int:
            buff.Write(Encode(big.NewInt(int64(t))))
        case uint:
            buff.Write(Encode(big.NewInt(int64(t))))
        case int8:
            buff.Write(Encode(big.NewInt(int64(t))))
        case int16:
            buff.Write(Encode(big.NewInt(int64(t))))
        case int32:
            buff.Write(Encode(big.NewInt(int64(t))))
        case int64:
            buff.Write(Encode(big.NewInt(t)))
        case uint16:
            buff.Write(Encode(big.NewInt(int64(t))))
        case uint32:
            buff.Write(Encode(big.NewInt(int64(t))))
        case uint64:
            buff.Write(Encode(big.NewInt(int64(t))))
        case byte:
            buff.Write(Encode(big.NewInt(int64(t))))
        case *big.Int:
            buff.Write(Encode(t.Bytes()))
        case []byte:
            if len(t) == 1 && t[0] <= 0x7f {
                buff.Write(t)
            } else if len(t) < 56 {
                buff.WriteByte(byte(len(t) + 0x80))
                buff.Write(t)
            } else {
                b := big.NewInt(int64(len(t)))
                buff.WriteByte(byte(len(b.Bytes()) + 0xb7))
                buff.Write(b.Bytes())
                buff.Write(t)
            }
        case string:
            buff.Write(Encode([]byte(t)))
        case []interface{}:
            // Inline function for writing the slice header
            WriteSliceHeader := func(length int) {
                if length < 56 {
                    buff.WriteByte(byte(length + 0xc0))
                } else {
                    b := big.NewInt(int64(length))
                    buff.WriteByte(byte(len(b.Bytes()) + 0xf7))
                    buff.Write(b.Bytes())
                }
            }

            var b bytes.Buffer
            for _, val := range t {
                b.Write(Encode(val))
            }
            WriteSliceHeader(len(b.Bytes()))
            buff.Write(b.Bytes())
        }
    } else {
        // Empty list for nil
        buff.WriteByte(0xc0)
    }

    return buff.Bytes()
}