-
Notifications
You must be signed in to change notification settings - Fork 4
/
raw_safe.go
183 lines (174 loc) · 4.79 KB
/
raw_safe.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
//+build !unsafe
package flexbuffers
import (
"fmt"
"math"
)
func (b Raw) ReadUInt64(offset int, byteWidth uint8) (uint64, error) {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return 0, ErrOutOfRange
}
if byteWidth < 4 {
if byteWidth < 2 {
return uint64(b[offset]), nil
} else {
return uint64(b[offset+1])<<8 | uint64(b[offset]), nil
}
} else {
if byteWidth < 8 {
return uint64(b[offset+3])<<24 | uint64(b[offset+2])<<16 | uint64(b[offset+1])<<8 | uint64(b[offset]), nil
} else {
return uint64(b[offset+7])<<56 | uint64(b[offset+6])<<48 | uint64(b[offset+5])<<40 | uint64(b[offset+4])<<32 |
uint64(b[offset+3])<<24 | uint64(b[offset+2])<<16 | uint64(b[offset+1])<<8 | uint64(b[offset]), nil
}
}
}
func (b Raw) ReadInt64(offset int, byteWidth uint8) (int64, error) {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return 0, ErrOutOfRange
}
if byteWidth < 4 {
if byteWidth < 2 {
return int64(int8(b[offset])), nil
} else {
return int64(int16(b[offset+1])<<8 | int16(b[offset])), nil
}
} else {
if byteWidth < 8 {
return int64(int32(b[offset+3])<<24 | int32(b[offset+2])<<16 | int32(b[offset+1])<<8 | int32(b[offset])), nil
} else {
return int64(b[offset+7])<<56 | int64(b[offset+6])<<48 | int64(b[offset+5])<<40 | int64(b[offset+4])<<32 |
int64(b[offset+3])<<24 | int64(b[offset+2])<<16 | int64(b[offset+1])<<8 | int64(b[offset]), nil
}
}
}
func (b Raw) ReadDouble(offset int, byteWidth uint8) (float64, error) {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return 0.0, ErrOutOfRange
}
if byteWidth < 4 {
if byteWidth < 2 {
return 0.0, fmt.Errorf("float8 is not supported")
} else {
return 0.0, fmt.Errorf("float16 is not supported")
}
} else {
ui, err := b.ReadUInt64(offset, byteWidth)
if err != nil {
return 0.0, err
}
if byteWidth < 8 {
return float64(math.Float32frombits(uint32(ui))), nil
} else {
return math.Float64frombits(ui), nil
}
}
}
func (b Raw) WriteInt64(offset int, byteWidth uint8, value int64) error {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return ErrOutOfRange
}
valueWidth := WidthI(value)
fits := (1 << valueWidth) <= byteWidth
if !fits {
return ErrUpdateDoesntFit
}
if valueWidth <= BitWidth8 {
b[offset] = byte(value)
}
if valueWidth <= BitWidth16 {
b[offset+1] = byte(value >> 8)
}
if valueWidth <= BitWidth32 {
b[offset+2] = byte(value >> 16)
b[offset+3] = byte(value >> 24)
}
if valueWidth <= BitWidth64 {
b[offset+4] = byte(value >> 32)
b[offset+5] = byte(value >> 40)
b[offset+6] = byte(value >> 48)
b[offset+7] = byte(value >> 56)
}
return nil
}
func (b Raw) WriteUInt64(offset int, byteWidth uint8, value uint64) error {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return ErrOutOfRange
}
valueWidth := WidthU(value)
fits := (1 << valueWidth) <= byteWidth
if !fits {
return ErrUpdateDoesntFit
}
if valueWidth <= BitWidth8 {
b[offset] = byte(value)
}
if valueWidth <= BitWidth16 {
b[offset+1] = byte(value >> 8)
}
if valueWidth <= BitWidth32 {
b[offset+2] = byte(value >> 16)
b[offset+3] = byte(value >> 24)
}
if valueWidth <= BitWidth64 {
b[offset+4] = byte(value >> 32)
b[offset+5] = byte(value >> 40)
b[offset+6] = byte(value >> 48)
b[offset+7] = byte(value >> 56)
}
return nil
}
func (b Raw) WriteFloat(offset int, byteWidth uint8, value float64) error {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return ErrOutOfRange
}
valueWidth := WidthF(value)
fits := (1 << valueWidth) <= byteWidth
if !fits {
return ErrUpdateDoesntFit
}
if byteWidth == 4 {
bits := math.Float32bits(float32(value))
b[offset] = byte(bits)
b[offset+1] = byte(bits >> 8)
b[offset+2] = byte(bits >> 16)
b[offset+3] = byte(bits >> 24)
} else if byteWidth == 8 {
bits := math.Float64bits(value)
b[offset] = byte(bits)
b[offset+1] = byte(bits >> 8)
b[offset+2] = byte(bits >> 16)
b[offset+3] = byte(bits >> 24)
b[offset+4] = byte(bits >> 32)
b[offset+5] = byte(bits >> 40)
b[offset+6] = byte(bits >> 48)
b[offset+7] = byte(bits >> 56)
}
return nil
}
func (b Raw) Indirect(offset int, byteWidth uint8) (int, error) {
if len(b) <= offset+int(byteWidth) || offset < 0 {
return 0, ErrOutOfRange
}
var ind int
if byteWidth < 4 {
if byteWidth < 2 {
ind = offset - int(b[offset])
} else {
ind = offset - int(uint64(b[offset+1])<<8|uint64(b[offset]))
}
} else {
if byteWidth < 8 {
ind = offset - int(uint64(b[offset+3])<<24|uint64(b[offset+2])<<16|uint64(b[offset+1])<<8|uint64(b[offset]))
} else {
ind = offset - int(
uint64(b[offset+7])<<56|uint64(b[offset+6])<<48|uint64(b[offset+5])<<40|uint64(b[offset+4])<<32|
uint64(b[offset+3])<<24|uint64(b[offset+2])<<16|uint64(b[offset+1])<<8|uint64(b[offset]),
)
}
}
if ind < 0 || len(b) <= ind {
return 0, ErrOutOfRange
}
return ind, nil
}