-
Notifications
You must be signed in to change notification settings - Fork 4
/
surge.go
311 lines (274 loc) · 9.96 KB
/
surge.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
package surge
import (
"reflect"
"unsafe"
)
// MaxBytes is set to 64 MB by default.
const MaxBytes = int(64 * 1024 * 1024)
// A SizeHinter can hint at the number of bytes required to represented it in
// binary.
type SizeHinter interface {
// SizeHint returns the upper bound for the number of bytes required to
// represent this value in binary.
SizeHint() int
}
// A Marshaler can marshal itself into bytes.
type Marshaler interface {
SizeHinter
// Marshal this value into bytes.
Marshal(buf []byte, rem int) ([]byte, int, error)
}
// An Unmarshaler can unmarshal itself from bytes.
type Unmarshaler interface {
// Unmarshal this value from bytes.
Unmarshal(buf []byte, rem int) ([]byte, int, error)
}
// A MarshalUnmarshaler is a marshaler and an unmarshaler.
type MarshalUnmarshaler interface {
Marshaler
Unmarshaler
}
// ToBinary returns the byte representation of a value. In uses the maximum
// memory quota to restrict the number of bytes that will be allocated during
// marshaling.
func ToBinary(v interface{}) ([]byte, error) {
buf := make([]byte, SizeHint(v))
_, _, err := Marshal(v, buf, MaxBytes)
return buf, err
}
// FromBinary unmarshals a byte representation of a value to a pointer to that
// value. In uses the maximum memory quota to restrict the number of bytes that
// will be allocated during unmarshaling.
func FromBinary(v interface{}, buf []byte) error {
_, _, err := Unmarshal(v, buf, MaxBytes)
return err
}
// SizeHint returns the number of bytes required to store a value in its binary
// representation. This is the number of bytes "on the wire", not the number of
// bytes that need to be allocated during marshaling/unmarshaling (which can be
// different, depending on the representation of the value). SizeHint supports
// all scalars, strings, arrays, slices, maps, structs, and custom
// implementations (for types that implement the SizeHinter interface). If the
// type is not supported, then zero is returned. If the value is a pointer, then
// the size of the underlying value being pointed to will be returned.
//
// x := int64(0)
// sizeHint := surge.SizeHint(x)
// if sizeHint != 8 {
// panic("assertion failed: size of int64 must be 8 bytes")
// }
//
func SizeHint(v interface{}) int {
return sizeHintReflected(reflect.ValueOf(v))
}
// Marshal a value into its binary representation, and store the value in a byte
// slice. The "remaining memory quota" defines the maximum amount of bytes that
// can be allocated on the heap when marshaling the value. In this way, the
// remaining memory quota can be used to avoid allocating too much memory during
// marshaling. Marshaling supports all scalars, strings, arrays, slices, maps,
// structs, and custom implementations (for types that implement the Marshaler
// interface). After marshaling, the unconsumed tail of the byte slice, and the
// remaining memory quota, are returned. If the byte slice is too small, then an
// error is returned. Similarly, if the remaining memory quote is too small,
// then an error is returned. If the type is not supported, then an error is
// returned. An error does not imply that nothing from the byte slice, or
// remaining memory quota, was consumed. If the value is a pointer, then the
// underlying value being pointed to will be marshaled.
//
// x := int64(0)
// buf := make([]byte, 8)
// tail, rem, err := surge.Marshal(x, buf, 8)
// if len(tail) != 0 {
// panic("assertion failed: int64 must consume 8 bytes")
// }
// if rem != 0 {
// panic("assertion failed: int64 must consume 8 bytes of the memory quota")
// }
// if err != nil {
// panic(fmt.Errorf("assertion failed: %v", err))
// }
//
func Marshal(v interface{}, buf []byte, rem int) ([]byte, int, error) {
return marshalReflected(reflect.ValueOf(v), buf, rem)
}
// Unmarshal a value from its binary representation by reading from a byte
// slice. The "remaining memory quota" defines the maximum amount of bytes that
// can be allocated on the heap when unmarshaling the value. In this way, the
// remaining memory quota can be used to avoid allocating too much memory during
// unmarshaling (this is particularly useful when dealing with potentially
// malicious input). Unmarshaling supports pointers to all scalars, strings,
// arrays, slices, maps, structs, and custom implementations (for types that
// implement the Unmarshaler interface). After unmarshaling, the unconsumed tail
// of the byte slice, and the remaining memory quota, are returned. If the byte
// slice is too small, then an error is returned. Similarly, if the remaining
// memory quote is too small, then an error is returned. If the type is not a
// pointer to one of the supported types, then an error is returned. An error
// does not imply that nothing from the byte slice, or remaining memory quota,
// was consumed. If the value is not a pointer, then an error is returned.
//
// x := int64(0)
// buf := make([]byte, 8)
// tail, rem, err := surge.Unmarshal(&x, buf, 8)
// if len(tail) != 0 {
// panic("assertion failed: int64 must consume 8 bytes")
// }
// if rem != 0 {
// panic("assertion failed: int64 must consume 8 bytes of the memory quota")
// }
// if err != nil {
// panic(fmt.Errorf("assertion failed: %v", err))
// }
//
func Unmarshal(v interface{}, buf []byte, rem int) ([]byte, int, error) {
valueOf := reflect.ValueOf(v)
if valueOf.Kind() != reflect.Ptr {
return buf, rem, NewErrUnsupportedUnmarshalType(v)
}
return unmarshalReflected(valueOf, buf, rem)
}
func sizeHintReflected(v reflect.Value) int {
if v.Type().Implements(sizeHinter) {
return v.Interface().(SizeHinter).SizeHint()
}
switch v.Kind() {
case reflect.Bool:
return SizeHintBool
case reflect.Uint8:
return SizeHintU8
case reflect.Uint16:
return SizeHintU16
case reflect.Uint32:
return SizeHintU32
case reflect.Uint, reflect.Uint64:
return SizeHintU64
case reflect.Int8:
return SizeHintI8
case reflect.Int16:
return SizeHintI16
case reflect.Int32:
return SizeHintI32
case reflect.Int64:
return SizeHintI64
case reflect.Float32:
return SizeHintF32
case reflect.Float64:
return SizeHintF64
case reflect.String:
return SizeHintString(v.String())
case reflect.Array:
return sizeHintReflectedArray(v)
case reflect.Slice:
if v, ok := v.Interface().([]byte); ok {
return SizeHintBytes(v)
}
return sizeHintReflectedSlice(v)
case reflect.Map:
return sizeHintReflectedMap(v)
case reflect.Struct:
return sizeHintReflectedStruct(v)
case reflect.Ptr:
v = reflect.Indirect(v)
if v.IsValid() {
return sizeHintReflected(v)
}
return 0
}
return 0
}
func marshalReflected(v reflect.Value, buf []byte, rem int) ([]byte, int, error) {
if v.Type().Implements(marshaler) {
return v.Interface().(Marshaler).Marshal(buf, rem)
}
switch v.Kind() {
case reflect.Bool:
return MarshalBool(v.Bool(), buf, rem)
case reflect.Uint8:
return MarshalU8(uint8(v.Uint()), buf, rem)
case reflect.Uint16:
return MarshalU16(uint16(v.Uint()), buf, rem)
case reflect.Uint32:
return MarshalU32(uint32(v.Uint()), buf, rem)
case reflect.Uint, reflect.Uint64:
return MarshalU64(uint64(v.Uint()), buf, rem)
case reflect.Int8:
return MarshalI8(int8(v.Int()), buf, rem)
case reflect.Int16:
return MarshalI16(int16(v.Int()), buf, rem)
case reflect.Int32:
return MarshalI32(int32(v.Int()), buf, rem)
case reflect.Int64:
return MarshalI64(int64(v.Int()), buf, rem)
case reflect.Float32:
return MarshalF32(float32(v.Float()), buf, rem)
case reflect.Float64:
return MarshalF64(float64(v.Float()), buf, rem)
case reflect.String:
return MarshalString(v.String(), buf, rem)
case reflect.Array:
return marshalReflectedArray(v, buf, rem)
case reflect.Slice:
if v, ok := v.Interface().([]byte); ok {
return MarshalBytes(v, buf, rem)
}
return marshalReflectedSlice(v, buf, rem)
case reflect.Map:
return marshalReflectedMap(v, buf, rem)
case reflect.Struct:
return marshalReflectedStruct(v, buf, rem)
case reflect.Ptr:
v = reflect.Indirect(v)
if v.IsValid() {
return marshalReflected(v, buf, rem)
}
return buf, rem, nil
}
return buf, rem, NewErrUnsupportedMarshalType(v.Interface())
}
func unmarshalReflected(v reflect.Value, buf []byte, rem int) ([]byte, int, error) {
if v.Type().Implements(unmarshaler) {
return v.Interface().(Unmarshaler).Unmarshal(buf, rem)
}
switch v.Type().Elem().Kind() {
case reflect.Bool:
return UnmarshalBool((*bool)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Uint8:
return UnmarshalU8((*uint8)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Uint16:
return UnmarshalU16((*uint16)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Uint32:
return UnmarshalU32((*uint32)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Uint64:
return UnmarshalU64((*uint64)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Int8:
return UnmarshalI8((*int8)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Int16:
return UnmarshalI16((*int16)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Int32:
return UnmarshalI32((*int32)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Int64:
return UnmarshalI64((*int64)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Float32:
return UnmarshalF32((*float32)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Float64:
return UnmarshalF64((*float64)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.String:
return UnmarshalString((*string)(unsafe.Pointer(v.Pointer())), buf, rem)
case reflect.Array:
return unmarshalReflectedArray(v, buf, rem)
case reflect.Slice:
if v, ok := v.Interface().(*[]byte); ok {
return UnmarshalBytes(v, buf, rem)
}
return unmarshalReflectedSlice(v, buf, rem)
case reflect.Map:
return unmarshalReflectedMap(v, buf, rem)
case reflect.Struct:
return unmarshalReflectedStruct(v, buf, rem)
}
return buf, rem, NewErrUnsupportedUnmarshalType(v.Interface())
}
var (
sizeHinter = reflect.ValueOf((*SizeHinter)(nil)).Type().Elem()
marshaler = reflect.ValueOf((*Marshaler)(nil)).Type().Elem()
unmarshaler = reflect.ValueOf((*Unmarshaler)(nil)).Type().Elem()
)