File: decoder.go

package info (click to toggle)
golang-go-dbus 1~bzr20150122-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: makefile: 13
file content (492 lines) | stat: -rw-r--r-- 12,298 bytes parent folder | download
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package dbus

import (
	"encoding/binary"
	"errors"
	"math"
	"reflect"
)

type decoder struct {
	signature Signature
	data      []byte
	order     binary.ByteOrder

	dataOffset, sigOffset int
}

var (
	bufferOverrunError    = errors.New("Buffer too small")
	signatureOverrunError = errors.New("Signature too small")
)

func newDecoder(signature Signature, data []byte, order binary.ByteOrder) *decoder {
	return &decoder{signature: signature, data: data, order: order}
}

func (self *decoder) align(alignment int) {
	inc := -self.dataOffset % alignment
	if inc < 0 {
		inc += alignment
	}
	self.dataOffset += inc
}

func (self *decoder) Decode(args ...interface{}) error {
	for _, arg := range args {
		v := reflect.ValueOf(arg)
		// We expect to be given pointers here, so the caller
		// can see the decoded values.
		if v.Kind() != reflect.Ptr {
			return errors.New("arguments to Decode should be pointers")
		}
		if err := self.decodeValue(v.Elem()); err != nil {
			return err
		}
	}
	return nil
}

func (self *decoder) HasMore() bool {
	return self.sigOffset < len(self.signature)
}

func (self *decoder) Remainder() []byte {
	return self.data[self.dataOffset:]
}

func (self *decoder) readByte() (byte, error) {
	if len(self.data) < self.dataOffset+1 {
		return 0, bufferOverrunError
	}
	value := self.data[self.dataOffset]
	self.dataOffset += 1
	return value, nil
}

func (self *decoder) readInt16() (int16, error) {
	self.align(2)
	if len(self.data) < self.dataOffset+2 {
		return 0, bufferOverrunError
	}
	value := int16(self.order.Uint16(self.data[self.dataOffset:]))
	self.dataOffset += 2
	return value, nil
}

func (self *decoder) readUint16() (uint16, error) {
	self.align(2)
	if len(self.data) < self.dataOffset+2 {
		return 0, bufferOverrunError
	}
	value := self.order.Uint16(self.data[self.dataOffset:])
	self.dataOffset += 2
	return value, nil
}

func (self *decoder) readInt32() (int32, error) {
	self.align(4)
	if len(self.data) < self.dataOffset+4 {
		return 0, bufferOverrunError
	}
	value := int32(self.order.Uint32(self.data[self.dataOffset:]))
	self.dataOffset += 4
	return value, nil
}

func (self *decoder) readUint32() (uint32, error) {
	self.align(4)
	if len(self.data) < self.dataOffset+4 {
		return 0, bufferOverrunError
	}
	value := self.order.Uint32(self.data[self.dataOffset:])
	self.dataOffset += 4
	return value, nil
}

func (self *decoder) readInt64() (int64, error) {
	self.align(8)
	if len(self.data) < self.dataOffset+8 {
		return 0, bufferOverrunError
	}
	value := int64(self.order.Uint64(self.data[self.dataOffset:]))
	self.dataOffset += 8
	return value, nil
}

func (self *decoder) readUint64() (uint64, error) {
	self.align(8)
	if len(self.data) < self.dataOffset+8 {
		return 0, bufferOverrunError
	}
	value := self.order.Uint64(self.data[self.dataOffset:])
	self.dataOffset += 8
	return value, nil
}

func (self *decoder) readFloat64() (float64, error) {
	value, err := self.readUint64()
	return math.Float64frombits(value), err
}

func (self *decoder) readString() (string, error) {
	length, err := self.readUint32()
	if err != nil {
		return "", err
	}
	// One extra byte for null termination
	if len(self.data) < self.dataOffset+int(length)+1 {
		return "", bufferOverrunError
	}
	value := string(self.data[self.dataOffset : self.dataOffset+int(length)])
	self.dataOffset += int(length) + 1
	return value, nil
}

func (self *decoder) readSignature() (Signature, error) {
	length, err := self.readByte()
	if err != nil {
		return "", err
	}
	// One extra byte for null termination
	if len(self.data) < self.dataOffset+int(length)+1 {
		return "", bufferOverrunError
	}
	value := Signature(self.data[self.dataOffset : self.dataOffset+int(length)])
	self.dataOffset += int(length) + 1
	return value, nil
}

func (self *decoder) decodeValue(v reflect.Value) error {
	if len(self.signature) <= self.sigOffset {
		return signatureOverrunError
	}
	sigCode := self.signature[self.sigOffset]
	self.sigOffset += 1
	switch sigCode {
	case 'y':
		value, err := self.readByte()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Uint8:
			v.SetUint(uint64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'b':
		value, err := self.readUint32()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Bool:
			v.SetBool(value != 0)
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value != 0))
			return nil
		}
	case 'n':
		value, err := self.readInt16()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Int16:
			v.SetInt(int64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'q':
		value, err := self.readUint16()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Uint16:
			v.SetUint(uint64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'i':
		value, err := self.readInt32()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Int32:
			v.SetInt(int64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'u':
		value, err := self.readUint32()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Uint32:
			v.SetUint(uint64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'x':
		value, err := self.readInt64()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Int64:
			v.SetInt(int64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 't':
		value, err := self.readUint64()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Uint64:
			v.SetUint(uint64(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'd':
		value, err := self.readFloat64()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.Float64:
			v.SetFloat(value)
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 's':
		value, err := self.readString()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.String:
			v.SetString(value)
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'o':
		value, err := self.readString()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.String:
			v.SetString(value)
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(ObjectPath(value)))
			return nil
		}
	case 'g':
		value, err := self.readSignature()
		if err != nil {
			return err
		}
		switch {
		case v.Kind() == reflect.String:
			v.SetString(string(value))
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			v.Set(reflect.ValueOf(value))
			return nil
		}
	case 'a':
		length, err := self.readUint32()
		if err != nil {
			return err
		}
		elemSigOffset := self.sigOffset
		afterElemOffset, err := self.signature.NextType(elemSigOffset)
		if err != nil {
			return err
		}
		// Adjust data offset so we are aligned to read array
		// elements.  Anything with an alignment of 4 or less
		// will already be aligned due to reading the length.
		switch self.signature[self.sigOffset] {
		case 'x', 't', 'd', '(', '{':
			self.align(8)
		}
		arrayEnd := self.dataOffset + int(length)
		if len(self.data) < arrayEnd {
			return bufferOverrunError
		}
		switch {
		case v.Kind() == reflect.Array:
			for i := 0; self.dataOffset < arrayEnd; i++ {
				// Reset signature offset to the array element.
				self.sigOffset = elemSigOffset
				if err := self.decodeValue(v.Index(i)); err != nil {
					return err
				}
			}
		case v.Kind() == reflect.Slice:
			if v.IsNil() {
				v.Set(reflect.MakeSlice(v.Type(), 0, 0))
			}
			v.SetLen(0)
			for self.dataOffset < arrayEnd {
				// Reset signature offset to the array element.
				self.sigOffset = elemSigOffset
				elem := reflect.New(v.Type().Elem()).Elem()
				if err := self.decodeValue(elem); err != nil {
					return err
				}
				v.Set(reflect.Append(v, elem))
			}
		case v.Kind() == reflect.Map:
			if self.signature[elemSigOffset] != '{' {
				return errors.New("Expected type code '{' but got " + string(self.signature[elemSigOffset]) + " when decoding to map")
			}
			v.Set(reflect.MakeMap(v.Type()))
			for self.dataOffset < arrayEnd {
				self.align(8)
				// Reset signature offset to first
				// item in dictionary entry:
				self.sigOffset = elemSigOffset + 1
				key := reflect.New(v.Type().Key()).Elem()
				value := reflect.New(v.Type().Elem()).Elem()
				if err := self.decodeValue(key); err != nil {
					return err
				}
				if err := self.decodeValue(value); err != nil {
					return err
				}
				v.SetMapIndex(key, value)
			}
		case typeBlankInterface.AssignableTo(v.Type()):
			if self.signature[elemSigOffset] == '{' {
				mapv := make(map[interface{}]interface{})
				for self.dataOffset < arrayEnd {
					self.align(8)
					var key, value interface{}
					self.sigOffset = elemSigOffset + 1
					if err := self.decodeValue(reflect.ValueOf(&key).Elem()); err != nil {
						return err
					}
					self.sigOffset = elemSigOffset + 2
					if err := self.decodeValue(reflect.ValueOf(&value).Elem()); err != nil {
						return err
					}
					mapv[key] = value
				}
				v.Set(reflect.ValueOf(mapv))
			} else {
				array := make([]interface{}, 0)
				for self.dataOffset < arrayEnd {
					// Reset signature offset to the array element.
					self.sigOffset = elemSigOffset
					var elem interface{}
					if err := self.decodeValue(reflect.ValueOf(&elem).Elem()); err != nil {
						return err
					}
					array = append(array, elem)
				}
				v.Set(reflect.ValueOf(array))
			}
		}
		self.sigOffset = afterElemOffset
		return nil
	case '(':
		self.align(8)
		// Do we have a pointer to a struct?
		if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct {
			if v.IsNil() {
				v.Set(reflect.New(v.Type().Elem()))
			}
			v = v.Elem()
		}
		switch {
		case v.Kind() == reflect.Struct:
			for i := 0; i < v.NumField() && self.sigOffset < len(self.signature) && self.signature[self.sigOffset] != ')'; i++ {
				if err := self.decodeValue(v.Field(i)); err != nil {
					return err
				}
			}
			if self.sigOffset >= len(self.signature) || self.signature[self.sigOffset] != ')' {
				return signatureOverrunError
			}
			// move past the closing parentheses
			self.sigOffset += 1
			return nil
		case typeBlankInterface.AssignableTo(v.Type()):
			// Decode as a slice of interface{} values.
			s := make([]interface{}, 0)
			for self.sigOffset < len(self.signature) && self.signature[self.sigOffset] != ')' {
				var field interface{}
				if err := self.decodeValue(reflect.ValueOf(&field).Elem()); err != nil {
					return err
				}
				s = append(s, field)
			}
			v.Set(reflect.ValueOf(s))
			return nil
		}
	case 'v':
		var variant *Variant
		switch {
		case v.Kind() == reflect.Ptr && v.Type().Elem() == typeVariant:
			if v.IsNil() {
				variant = &Variant{}
				v.Set(reflect.ValueOf(variant))
			} else {
				variant = v.Interface().(*Variant)
			}
		case v.Type() == typeVariant:
			variant = v.Addr().Interface().(*Variant)
		case typeBlankInterface.AssignableTo(v.Type()):
			variant = &Variant{}
			v.Set(reflect.ValueOf(variant))
		}
		if variant != nil {
			signature, err := self.readSignature()
			if err != nil {
				return err
			}
			// Decode the variant value through a sub-decoder.
			variantDec := decoder{
				signature:  signature,
				data:       self.data,
				order:      self.order,
				dataOffset: self.dataOffset,
				sigOffset:  0}
			if err := variantDec.decodeValue(reflect.ValueOf(&variant.Value).Elem()); err != nil {
				return err
			}
			// Decoding continues after the variant value.
			self.dataOffset = variantDec.dataOffset
			return nil
		}
	}
	return errors.New("Could not decode " + string(sigCode) + " to " + v.Type().String())
}