-
Notifications
You must be signed in to change notification settings - Fork 9
/
decoder.go
325 lines (258 loc) · 7.99 KB
/
decoder.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
/*
Copyright 2021 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package structex
import (
"bytes"
"fmt"
"io"
"math"
"math/bits"
"reflect"
)
type decoder struct {
reader io.ByteReader
currentByte uint8
byteOffset uint64
bitOffset uint64
transcoder *transcoder
}
func (d *decoder) read(nbits uint64) (uint64, error) {
if nbits == 0 {
return 0, fmt.Errorf("unsupported zero bit operation")
}
if nbits > 64 {
return 0, fmt.Errorf("bitfield exceeds 64-bit limitation")
}
var value uint64 = 0
var offset uint64 = 0
// Check for carry-over bits from previous bitfields
if d.bitOffset != 0 {
mask := uint8(math.Pow(2, float64(nbits)) - 1)
value = uint64((d.currentByte >> d.bitOffset) & mask)
if d.bitOffset+nbits < 8 {
d.bitOffset += nbits
return value, nil
} else {
offset = 8 - d.bitOffset
nbits = nbits - (8 - d.bitOffset)
d.bitOffset = 0
}
}
for nbits != 0 {
b, err := d.reader.ReadByte()
if err != nil {
return 0, err
}
d.currentByte = b
d.byteOffset += 1
if nbits < 8 {
mask := uint8(math.Pow(2, float64(nbits)) - 1)
value |= uint64(d.currentByte&mask) << offset
d.bitOffset += nbits
return value, nil
} else {
value |= uint64(d.currentByte) << offset
offset += 8
nbits -= 8
}
}
return value, nil
}
func (d *decoder) readValue(value reflect.Value, tags *tags) (uint64, error) {
if !value.CanSet() {
return 0, fmt.Errorf("Field of type %s cannot be set. Make sure it is exported.",
value.Type().Kind().String())
}
nbits := uint64(0)
kind := value.Kind()
if kind == reflect.Bool {
nbits = 1
} else {
nbits = uint64(value.Type().Bits())
}
if tags != nil {
if nbits < tags.bitfield.nbits {
return 0, fmt.Errorf("Field value of type %s has bitfield definition with %d bits, exceeding field size of %d bits.",
value.Type().Kind().String(),
tags.bitfield.nbits,
nbits)
}
if tags.bitfield.nbits > 0 {
nbits = tags.bitfield.nbits
}
}
v, err := d.read(nbits)
if err != nil {
return 0, err
}
if (tags != nil && tags.endian == big) || (d.transcoder.defaultEndianness == big && (tags != nil && tags.endian != little)) {
switch kind {
case reflect.Uint16, reflect.Int16:
v = uint64(bits.ReverseBytes16(uint16(v)))
case reflect.Uint32, reflect.Int32, reflect.Uint, reflect.Int:
v = uint64(bits.ReverseBytes32(uint32(v)))
case reflect.Uint64, reflect.Int64:
v = bits.ReverseBytes64(v)
}
}
switch value.Kind() {
case reflect.Bool:
value.SetBool(v == 1)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
value.SetUint(v)
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
if v>>(nbits-1) == 1 {
value.SetInt(int64(v) - int64((1<<nbits)-1) - 1)
} else {
value.SetInt(int64(v))
}
default:
return 0, fmt.Errorf("Unsupported read type %s", value.Kind().String())
}
return v, nil
}
func (d *decoder) align(val alignment) error {
if d.bitOffset != 0 {
if _, err := d.read(8 - d.bitOffset); err != nil {
return err
}
}
for d.byteOffset%uint64(val) != 0 {
if _, err := d.read(8); err != nil {
return err
}
}
return nil
}
func (d *decoder) field(val reflect.Value, tags *tags) error {
_, err := d.readValue(val, tags)
return err
}
func (d *decoder) layout(val reflect.Value, ref *tagReference) error {
value, err := d.readValue(ref.value, ref.tags)
ref.tags.layout.value = value
return err
}
func (d *decoder) array(t *transcoder, arr reflect.Value, tags *tags, ref *tagReference) error {
isStruct := arr.Type().Elem().Kind() == reflect.Struct
for j := 0; j < arr.Len(); j++ {
if isStruct { // Recurse down into the struct
if err := t.transcode(arr.Index(j), tags); err != nil {
return err
}
} else {
if _, err := d.readValue(arr.Index(j), tags); err != nil {
if err == io.EOF && tags != nil && tags.truncate {
return nil
}
return err
}
}
}
return nil
}
func (d *decoder) slice(t *transcoder, arr reflect.Value, tags *tags, ref *tagReference) error {
length := uint64(arr.Len())
if ref != nil {
switch ref.tags.layout.format {
case sizeOf:
sz, err := typeSize(arr.Type().Elem())
if err != nil {
return err
}
if ref.tags.layout.value%sz != 0 {
return fmt.Errorf("Slice with size %d of slice is a non-multiple of structure size %d",
ref.tags.layout.value,
sz)
}
length = ref.tags.layout.value / sz
case countOf:
length = ref.tags.layout.value
default:
return fmt.Errorf("Slice size cannot be determined. Did you miss a field tag?")
}
arr.Set(reflect.MakeSlice(arr.Type(), int(length), int(length)))
}
for j := 0; j < arr.Len(); j++ {
if err := t.transcode(arr.Index(j), tags); err != nil {
if err == io.EOF && tags != nil && tags.truncate {
return nil
}
return err
}
}
return nil
}
/*
Decode reads data from a ByteReader into provided annotated structure.
Deserialization occurs according to the annotations in the structure which
take several options:
Bitfields:
Bitfields define a structure field with an explicit size in bits. They are
analogous to bit fields in the C specification. Un
`bitfield:[size][,reserved]`
size Specifies the size, in bits, of the field.
reserved Optional modifier that specifies the field contains reserved
bits and should be encoded as zeros.
Dynamic Layouts:
Many industry standards support dynamically sized return fields where the
data layout is self described by other fields. To support such formats
two annotations are provided.
`sizeOf:"[Field][,relative]"`
Field Specifies that the field describes the size of Field within the
structure.
During decoding, if field is non-zero, the field's value is
used to limit the number elements in the array or slice of
name Field.
relative Optional modifier that specifies the field value describing
the size of Field is relative to the field offset within
the structure. This is often used in T10.org documentation
`countOf:"[Field]"`
Field Specifies that the field describes the count of elements in
Field.
During decoding, if field is non-zero, the field's value is
used to limit the number elements in the array or slice of
name Field.
Alignment:
Annotations can specified the byte-alignment requirement for structure
fields. Analogous to the alignas specifier in C. Can only be applied
to non-bitfield structure fields.
`align:"[value]"`
value An integer value specifying the byte alignment of the field.
Invalid non-zero alignments panic.
*/
func Decode(reader io.ByteReader, s interface{}) error {
d := decoder{
reader: reader,
currentByte: 0,
byteOffset: 0,
bitOffset: 0,
}
t := newTranscoder(&d)
d.transcoder = t
return t.transcode(reflect.ValueOf(s), nil)
}
// DecodeByteBuffer takes a raw byte buffer and unpacks the buffer into
// the provided structure. Unused bytes do not cause an error.
func DecodeByteBuffer(b *bytes.Buffer, s interface{}) error {
reader := byteBufferReader{
buffer: b,
}
return Decode(&reader, s)
}