/
fill.go
184 lines (163 loc) · 4.46 KB
/
fill.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
// Copyright 2020 Marius van der Wijden
// This file is part of the fuzzy-vm library.
//
// The fuzzy-vm library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The fuzzy-vm library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the fuzzy-vm library. If not, see <http://www.gnu.org/licenses/>.
// Package filler can fill objects based on a provided data source.
package filler
import (
"encoding/binary"
"math/big"
)
// Filler can be used to fill objects from a data source.
type Filler struct {
data []byte
pointer int
usedUp bool
}
// NewFiller creates a new Filler.
func NewFiller(data []byte) *Filler {
if len(data) == 0 {
data = make([]byte, 1)
}
return &Filler{
data: data,
pointer: 0,
usedUp: false,
}
}
// incPointer increments the internal pointer
// to the next position to be read.
func (f *Filler) incPointer(i int) {
if f.pointer+i >= len(f.data) {
f.usedUp = true
}
f.pointer = (f.pointer + i) % len(f.data)
}
// Bool returns a new bool.
func (f *Filler) Bool() bool {
b := f.Byte()
return b > 127
}
// Byte returns a new byte.
func (f *Filler) Byte() byte {
b := f.data[f.pointer]
f.incPointer(1)
return b
}
// Read implements the io.Reader interface.
func (f *Filler) Read(b []byte) (n int, err error) {
// TODO (MariusVanDerWijden) this can be done more efficiently
tmp := f.ByteSlice(len(b))
for i := 0; i < len(b); i++ {
b[i] = tmp[i]
}
return len(b), nil
}
// BigInt16 returns a new big int in [0, 2^16).
func (f *Filler) BigInt16() *big.Int {
i := f.Uint16()
return big.NewInt(int64(i))
}
// BigInt32 returns a new big int in [0, 2^32).
func (f *Filler) BigInt32() *big.Int {
i := f.Uint32()
return big.NewInt(int64(i))
}
// BigInt64 returns a new big int in [0, 2^64).
func (f *Filler) BigInt64() *big.Int {
i := f.ByteSlice(8)
return new(big.Int).SetBytes(i)
}
// BigInt256 returns a new big int in [0, 2^256).
func (f *Filler) BigInt256() *big.Int {
i := f.ByteSlice(32)
return new(big.Int).SetBytes(i)
}
// GasInt returns a new big int to be used as a gas value.
// With probability 254/255 its in [0, 20.000.000].
// With probability 1/255 its in [0, 2^32].
func (f *Filler) GasInt() *big.Int {
b := f.Byte()
if b == 253 {
return f.BigInt32()
} else if b == 254 {
return f.BigInt64()
} else if b == 255 {
return f.BigInt256()
}
i := f.BigInt32()
return i.Mod(i, big.NewInt(20000000))
}
// MemInt returns a new big int to be used as a memory or offset value.
// With probability 252/255 its in [0, 256].
// With probability 1/255 its in [0, 2^32].
// With probability 1/255 its in [0, 2^64].
// With probability 1/255 its in [0, 2^256].
func (f *Filler) MemInt() *big.Int {
b := f.Byte()
if b == 253 {
return f.BigInt32()
} else if b == 254 {
return f.BigInt64()
} else if b == 255 {
return f.BigInt256()
}
return big.NewInt(int64(f.Byte()))
}
// ByteSlice returns a byteslice with `items` values.
func (f *Filler) ByteSlice(items int) []byte {
// TODO (MariusVanDerWijden) this can be done way more efficiently
b := make([]byte, items)
if f.pointer+items < len(f.data) {
copy(b, f.data[f.pointer:])
} else {
// Not enough data available
for i := 0; i < items; {
it := copy(b[i:], f.data[f.pointer:])
if it == 0 {
panic("should not happen, infinite loop")
}
i += it
f.pointer = 0
}
f.usedUp = true
}
f.incPointer(items)
return b
}
// ByteSlice256 returns a byteslice with 0..255 values.
func (f *Filler) ByteSlice256() []byte {
return f.ByteSlice(int(f.Byte()))
}
// Uint16 returns a new uint16.
func (f *Filler) Uint16() uint16 {
return binary.BigEndian.Uint16(f.ByteSlice(2))
}
// Uint32 returns a new uint32.
func (f *Filler) Uint32() uint32 {
return binary.BigEndian.Uint32(f.ByteSlice(4))
}
// Uint64 returns a new uint64.
func (f *Filler) Uint64() uint64 {
return binary.BigEndian.Uint64(f.ByteSlice(8))
}
// Reset resets a filler.
func (f *Filler) Reset() {
f.pointer = 0
f.usedUp = false
}
// UsedUp returns wether all bytes from the source have been used.
func (f *Filler) UsedUp() bool {
return f.usedUp
}