summaryrefslogtreecommitdiff
path: root/rrdformat/rrdbinary/decode.go
blob: c4f7760f4fb0396eb8e9260b01cc7cba9967a164 (plain)
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
package rrdbinary

import (
	"bytes"
	"fmt"
	"math"
	"reflect"
	"strconv"
	"strings"
)

type Decoder struct {
	arch Architecture
	pos  int
	data []byte
}

func NewDecoder(arch Architecture, data []byte) *Decoder {
	return &Decoder{
		arch: arch,
		pos:  0,
		data: data,
	}
}

type unmarshaler interface {
	unmarshalRRD(d *Decoder, tag string) error
}

func Unmarshal(arch Architecture, data []byte, ptr interface{}) error {
	decoder := NewDecoder(arch, data)
	if err := decoder.Decode(ptr, ""); err != nil {
		return err
	}
	if err := decoder.Decode(&EOF{}, ""); err != nil {
		return err
	}
	return nil
}

func (d *Decoder) binError(ctxLen int, msg string) error {
	return NewBinError(msg, d.data, d.pos, ctxLen)
}

func (d *Decoder) binErrorf(ctxLen int, format string, a ...interface{}) error {
	return d.binError(ctxLen, fmt.Sprintf(format, a...))
}

func (d *Decoder) Decode(ptr interface{}, tag string) error {
	ptrValue := reflect.ValueOf(ptr)
	if ptrValue.Kind() != reflect.Ptr {
		return typeErrorf("ptr is a %v, not a pointer", ptrValue.Kind())
	}
	return d.decode(ptrValue.Elem(), tag)
}

func (d *Decoder) decode(v reflect.Value, tag string) error {
	if v.Kind() != reflect.Ptr && v.CanAddr() {
		vptr := v.Addr()
		if vptr.CanInterface() {
			if u, ok := vptr.Interface().(unmarshaler); ok {
				return u.unmarshalRRD(d, tag)
			}
		}
	}
	switch v.Type().Kind() {
	case reflect.Struct:
		return d.decodeStruct(v, tag)
	case reflect.Array, reflect.Slice:
		return d.decodeList(v, tag)
	default:
		return typeErrorf("invalid type for rrdbinary.Decoder: %v", v.Type())
	}
}

func (d *Decoder) decodeStruct(v reflect.Value, tag string) error {
	panicUnless(v.Kind() == reflect.Struct)
	panicUnless(v.CanSet())

	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for struct: %q", tag)
	}

	for i := 0; i < v.NumField(); i++ {
		fieldInfo := v.Type().Field(i)
		tag := fieldInfo.Tag.Get("rrdbinary")
		if tag == "-" {
			continue
		}
		if err := d.decode(v.Field(i), tag); err != nil {
			return fmt.Errorf("field %s: %w", fieldInfo.Name, err)
		}
	}
	return nil
}

func (d *Decoder) decodeList(v reflect.Value, tag string) error {
	panicUnless(v.Kind() == reflect.Array || v.Kind() == reflect.Slice)
	panicUnless(v.CanSet())

	for i := 0; i < v.Len(); i++ {
		if err := d.decode(v.Index(i), tag); err != nil {
			return fmt.Errorf("index %d: %w", i, err)
		}
	}
	return nil
}

func (obj *String) unmarshalRRD(d *Decoder, tag string) error {
	size := 0
	switch {
	case tag == "":
		// do nothing
	case strings.HasPrefix(tag, "size="):
		var err error
		size, err = strconv.Atoi(strings.TrimPrefix(tag, "size="))
		if err != nil {
			return typeErrorf("invalid rrdbinary struct tag for string: %q", tag)
		}
	default:
		return typeErrorf("invalid rrdbinary struct tag for string: %q", tag)
	}

	data := d.data[d.pos:]
	if size > 0 {
		if len(data) < size {
			return d.binErrorf(size, "unexpected end-of-file in %d-byte string", size)
		}
		data = data[:size]
	}

	nul := bytes.IndexByte(data, 0)
	if nul < 0 {
		if size > 0 {
			return d.binErrorf(size, "missing null-terminator on %d-byte string", size)
		} else {
			return d.binErrorf(16, "unexpected end-of-file looking for null-terminator on string")
		}
	}

	if size > 0 {
		for _, byte := range data[nul:] {
			if byte != 0 {
				return d.binErrorf(size, "garbage data after null-terminator on fixed %d-byte string", size)
			}
		}
	} else {
		size = nul + 1
	}

	*obj = String(data[:nul])
	d.pos += size
	return nil
}

func (obj *Double) unmarshalRRD(d *Decoder, tag string) error {
	if d.arch.DoubleWidth != 8 {
		return archErrorf("rrdbinary does not support DoubleWidth=%d; only supports 8", d.arch.DoubleWidth)
	}
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for float: %q", tag)
	}

	data := d.data[d.pos:]

	padding := 0
	if d.pos%d.arch.DoubleAlign != 0 {
		padding = d.arch.DoubleAlign - (d.pos % d.arch.DoubleAlign)
	}
	if len(data) < padding {
		return d.binErrorf(padding+d.arch.DoubleWidth, "unexpected end-of-file in %d-byte padding-before-float", padding)
	}
	data = data[padding:]

	if len(data) < d.arch.DoubleWidth {
		return d.binErrorf(d.arch.DoubleWidth, "unexpected end-of-file in %d-byte float", d.arch.DoubleWidth)
	}

	*obj = Double(math.Float64frombits(d.arch.ByteOrder.Uint64(data)))
	d.pos += padding + d.arch.DoubleWidth
	return nil
}

func (obj *ULong) unmarshalRRD(d *Decoder, tag string) error {
	if d.arch.LongWidth != 4 && d.arch.LongWidth != 8 {
		return archErrorf("rrdbinary does not support LongWidth=%d; only supports 4 or 8", d.arch.LongWidth)
	}
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for uint: %q", tag)
	}

	data := d.data[d.pos:]

	padding := 0
	if d.pos%d.arch.LongAlign != 0 {
		padding = d.arch.LongAlign - (d.pos % d.arch.LongAlign)
	}
	if len(data) < padding {
		return d.binErrorf(padding+d.arch.LongWidth, "unexpected end-of-file in %d-byte padding-before-uint", padding)
	}
	data = data[padding:]

	if len(data) < d.arch.LongWidth {
		return d.binErrorf(d.arch.LongWidth, "unexpected end-of-file in %d-byte uint", d.arch.LongWidth)
	}

	switch d.arch.LongWidth {
	case 4:
		*obj = ULong(d.arch.ByteOrder.Uint32(data))
	case 8:
		*obj = ULong(d.arch.ByteOrder.Uint64(data))
	}
	d.pos += padding + d.arch.LongWidth
	return nil
}

func (obj *Long) unmarshalRRD(d *Decoder, tag string) error {
	if d.arch.LongWidth != 4 && d.arch.LongWidth != 8 {
		return archErrorf("rrdbinary does not support LongWidth=%d; only supports 4 or 8", d.arch.LongWidth)
	}
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for int: %q", tag)
	}

	data := d.data[d.pos:]

	padding := 0
	if d.pos%d.arch.LongAlign != 0 {
		padding = d.arch.LongAlign - (d.pos % d.arch.LongAlign)
	}
	if len(data) < padding {
		return d.binErrorf(padding+d.arch.LongWidth, "unexpected end-of-file in %d-byte padding-before-int", padding)
	}
	data = data[padding:]

	if len(data) < d.arch.LongWidth {
		return d.binErrorf(d.arch.LongWidth, "unexpected end-of-file in %d-byte int", d.arch.LongWidth)
	}

	switch d.arch.LongWidth {
	case 4:
		*obj = Long(int32(d.arch.ByteOrder.Uint32(data)))
	case 8:
		*obj = Long(d.arch.ByteOrder.Uint64(data))
	}
	d.pos += padding + d.arch.LongWidth
	return nil
}

func (obj *Unival) unmarshalRRD(d *Decoder, tag string) error {
	if d.arch.UnivalWidth != 8 {
		return archErrorf("rrdbinary does not support UnivalWidth=%d; only supports 8", d.arch.UnivalWidth)
	}
	if d.arch.ShortWidth != 2 && d.arch.ShortWidth != 4 {
		return archErrorf("rrdbinary does not support ShortWidth=%d; only supports 2 or 4", d.arch.ShortWidth)
	}
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for unival: %q", tag)
	}

	data := d.data[d.pos:]

	padding := 0
	if d.pos%d.arch.UnivalAlign != 0 {
		padding = d.arch.UnivalAlign - (d.pos % d.arch.UnivalAlign)
	}
	if len(data) < padding {
		return d.binErrorf(padding+d.arch.UnivalWidth, "unexpected end-of-file in %d-byte padding-before-unival", padding)
	}
	data = data[padding:]

	if len(data) < d.arch.UnivalWidth {
		return d.binErrorf(d.arch.UnivalWidth, "unexpected end-of-file in %d-byte unival", d.arch.UnivalWidth)
	}

	*obj = Unival{
		arch: d.arch,
		data: data,
	}
	d.pos += padding + d.arch.UnivalWidth
	return nil
}

func (obj *Time) unmarshalRRD(d *Decoder, tag string) error {
	if d.arch.TimeWidth != 4 && d.arch.TimeWidth != 8 {
		return archErrorf("rrdbinary does not support TimeWidth=%d; only supports 4 or 8", d.arch.TimeWidth)
	}
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for time: %q", tag)
	}

	data := d.data[d.pos:]

	padding := 0
	if d.pos%d.arch.TimeAlign != 0 {
		padding = d.arch.TimeAlign - (d.pos % d.arch.TimeAlign)
	}
	if len(data) < padding {
		return d.binErrorf(padding+d.arch.TimeWidth, "unexpected end-of-file in %d-byte padding-before-time", padding)
	}
	data = data[padding:]

	if len(data) < d.arch.TimeWidth {
		return d.binErrorf(d.arch.TimeWidth, "unexpected end-of-file in %d-byte time", d.arch.TimeWidth)
	}

	switch d.arch.TimeWidth {
	case 4:
		*obj = Time(int32(d.arch.ByteOrder.Uint32(data)))
	case 8:
		*obj = Time(d.arch.ByteOrder.Uint64(data))
	}
	d.pos += padding + d.arch.TimeWidth
	return nil
}

func (_ *EOF) unmarshalRRD(d *Decoder, tag string) error {
	if tag != "" {
		return typeErrorf("invalid rrdbinary struct tag for eof: %q", tag)
	}

	data := d.data[d.pos:]
	if len(data) > 0 {
		return d.binErrorf(16, "extra %d bytes of data after expected end-of-file", len(data))
	}

	return nil
}