forked from norisatir/go-dbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshall.go
452 lines (394 loc) · 9.18 KB
/
marshall.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
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
package dbus
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
)
func _Align(length int, index int) int {
switch length {
case 1:
return index
case 2, 4, 8:
bit := length - 1
return ^bit & (index + bit)
}
// default
return -1
}
func _AppendAlign(length int, buff *bytes.Buffer) {
padno := _Align(length, buff.Len()) - buff.Len()
for i := 0; i < padno; i++ {
buff.WriteByte(0)
}
}
func _AppendString(buff *bytes.Buffer, str string) {
_AppendAlign(4, buff)
binary.Write(buff, binary.LittleEndian, int32(len(str)))
buff.Write([]byte(str))
buff.WriteByte(0)
}
func _AppendSignature(buff *bytes.Buffer, sig string) {
_AppendByte(buff, byte(len(sig)))
buff.Write([]byte(sig))
buff.WriteByte(0)
}
func _AppendByte(buff *bytes.Buffer, b byte) { binary.Write(buff, binary.LittleEndian, b) }
func _AppendUint32(buff *bytes.Buffer, ui uint32) {
_AppendAlign(4, buff)
binary.Write(buff, binary.LittleEndian, ui)
}
func _AppendInt32(buff *bytes.Buffer, i int32) {
_AppendAlign(4, buff)
binary.Write(buff, binary.LittleEndian, i)
}
func _AppendArray(buff *bytes.Buffer, align int, proc func(b *bytes.Buffer)) {
_AppendAlign(4, buff)
_AppendAlign(align, buff)
b := bytes.NewBuffer(buff.Bytes())
b.Write([]byte("ABCD")) // "ABCD" will be replaced with array-size.
pos1 := b.Len()
proc(b)
pos2 := b.Len()
binary.Write(buff, binary.LittleEndian, int32(pos2-pos1))
buff.Write(b.Bytes()[pos1:pos2])
}
func _AppendValue(buff *bytes.Buffer, sig string, val interface{}) (sigOffset int, e error) {
if len(sig) == 0 {
return 0, errors.New("Invalid Signature")
}
e = nil
switch sig[0] {
case 'y': // byte
_AppendByte(buff, val.(byte))
sigOffset = 1
case 's': // string
_AppendString(buff, val.(string))
sigOffset = 1
case 'u': // uint32
_AppendUint32(buff, val.(uint32))
sigOffset = 1
case 'i': // int32
_AppendInt32(buff, val.(int32))
sigOffset = 1
case 'a': // ary
sigBlock, _ := _GetSigBlock(sig, 1)
_AppendArray(buff, 1, func(b *bytes.Buffer) {
if slice, ok := val.([]interface{}); ok && slice != nil {
for _, v := range slice {
_AppendValue(b, sigBlock, v)
}
}
})
sigOffset = 1 + len(sigBlock)
case '(': // struct FIXME: nested struct not support
_AppendAlign(8, buff)
structSig, _ := _GetStructSig(sig, 0)
for i, s := range structSig {
_AppendValue(buff, string(s), val.([]interface{})[i])
}
sigOffset = 2 + len(structSig)
case '{':
_AppendAlign(8, buff)
dictSig, _ := _GetDictSig(sig, 0)
for i, s := range dictSig {
_AppendValue(buff, string(s), val.([]interface{})[i])
}
sigOffset = 2 + len(dictSig)
}
return
}
func _AppendParamsData(buff *bytes.Buffer, sig string, params []interface{}) {
sigOffset := 0
prmsOffset := 0
sigLen := len(sig)
for ; sigOffset < sigLen; prmsOffset++ {
offset, _ := _AppendValue(buff, sig[sigOffset:len(sig)], params[prmsOffset])
sigOffset += offset
}
}
func _GetByte(buff []byte, index int) (byte, error) {
if len(buff) <= index {
return 0, errors.New("index error")
}
return buff[index], nil
}
func _GetInt16(buff []byte, index int) (int16, error) {
if len(buff) <= index+2-1 {
return 0, errors.New("index error")
}
var n int16
e := binary.Read(bytes.NewBuffer(buff[index:len(buff)]), binary.LittleEndian, &n)
if e != nil {
return 0, e
}
return n, nil
}
func _GetUint16(buff []byte, index int) (uint16, error) {
if len(buff) <= index+2-1 {
return 0, errors.New("index error")
}
var q uint16
e := binary.Read(bytes.NewBuffer(buff[index:len(buff)]), binary.LittleEndian, &q)
if e != nil {
return 0, e
}
return q, nil
}
func _GetInt32(buff []byte, index int) (int32, error) {
if len(buff) <= index+4-1 {
return 0, errors.New("index error")
}
var l int32
e := binary.Read(bytes.NewBuffer(buff[index:len(buff)]), binary.LittleEndian, &l)
if e != nil {
return 0, e
}
return l, nil
}
func _GetUint32(buff []byte, index int) (uint32, error) {
if len(buff) <= index+4-1 {
return 0, errors.New("index error")
}
var u uint32
e := binary.Read(bytes.NewBuffer(buff[index:len(buff)]), binary.LittleEndian, &u)
if e != nil {
return 0, e
}
return u, nil
}
func _GetBoolean(buff []byte, index int) (bool, error) {
if len(buff) <= index+4-1 {
return false, errors.New("index error")
}
var v int32
e := binary.Read(bytes.NewBuffer(buff[index:len(buff)]), binary.LittleEndian, &v)
if e != nil {
return false, e
}
return 0 != v, nil
}
func _GetString(buff []byte, index int, size int) (string, error) {
if len(buff) <= (index + size - 1) {
return "", errors.New("index error")
}
return string(buff[index : index+size]), nil
}
func _GetStructSig(sig string, startIdx int) (string, error) {
if len(sig) <= startIdx || '(' != sig[startIdx] {
return "<nil>", errors.New("index error")
}
sigIdx := startIdx + 1
for depth := 0; sigIdx < len(sig); sigIdx++ {
switch sig[sigIdx] {
case ')':
if depth == 0 {
return sig[startIdx+1 : sigIdx], nil
}
depth--
case '(':
depth++
}
}
return "<nil>", errors.New("parse error")
}
func _GetDictSig(sig string, startIdx int) (string, error) {
if len(sig) <= startIdx || '{' != sig[startIdx] {
return "<nil>", errors.New("index error")
}
sigIdx := startIdx + 1
for depth := 0; sigIdx < len(sig); sigIdx++ {
switch sig[sigIdx] {
case '}':
if depth == 0 {
return sig[startIdx+1 : sigIdx], nil
}
depth--
case '{':
depth++
}
}
return "<nil>", errors.New("parse error")
}
func _GetSigBlock(sig string, index int) (string, error) {
switch sig[index] {
case '(':
str, e := _GetStructSig(sig, index)
if e != nil {
return "", e
}
return strings.Join([]string{"(", str, ")"}, ""), nil
case '{':
str, e := _GetDictSig(sig, index)
if e != nil {
return "", e
}
return strings.Join([]string{"{", str, "}"}, ""), nil
}
// default
return sig[index : index+1], nil
}
func _GetVariant(buff []byte, index int) (vals []interface{}, retidx int, e error) {
retidx = index
sigSize := int(buff[retidx])
retidx++
sig := string(buff[retidx : retidx+sigSize])
vals, retidx, e = Parse(buff, sig, retidx+sigSize+1)
return
}
func Parse(buff []byte, sig string, index int) (slice []interface{}, bufIdx int, err error) {
slice = make([]interface{}, 0)
bufIdx = index
for sigIdx := 0; sigIdx < len(sig); {
switch sig[sigIdx] {
case 'b': // bool
bufIdx = _Align(4, bufIdx)
b, e := _GetBoolean(buff, bufIdx)
if e != nil {
err = e
return
}
slice = append(slice, bool(b))
bufIdx += 4
sigIdx++
case 'y': // byte
v, e := _GetByte(buff, bufIdx)
if e != nil {
err = e
return
}
slice = append(slice, v)
bufIdx++
sigIdx++
case 'n': // int16
bufIdx = _Align(2, bufIdx)
n, e := _GetInt16(buff, bufIdx)
if e != nil {
err = e
return
}
slice = append(slice, n)
bufIdx += 2
sigIdx++
case 'q': // uint16
bufIdx = _Align(2, bufIdx)
q, e := _GetUint16(buff, bufIdx)
if e != nil {
err = e
return
}
slice = append(slice, q)
bufIdx += 2
sigIdx++
case 'u': // uint32
bufIdx = _Align(4, bufIdx)
u, e := _GetUint32(buff, bufIdx)
if e != nil {
err = e
return
}
slice = append(slice, u)
bufIdx += 4
sigIdx++
case 's', 'o': // string, object
bufIdx = _Align(4, bufIdx)
size, e := _GetInt32(buff, bufIdx)
if e != nil {
err = e
return
}
str, e := _GetString(buff, bufIdx+4, int(size))
if e != nil {
err = e
return
}
slice = append(slice, str)
bufIdx += (4 + int(size) + 1)
sigIdx++
case 'g': // signature
size, e := _GetByte(buff, bufIdx)
if e != nil {
err = e
return
}
str, e := _GetString(buff, bufIdx+1, int(size))
if e != nil {
err = e
return
}
slice = append(slice, str)
bufIdx += (1 + int(size) + 1)
sigIdx++
case 'a': // array
startIdx := _Align(4, bufIdx)
arySize, e := _GetInt32(buff, startIdx)
if e != nil {
err = e
return
}
sigBlock, e := _GetSigBlock(sig, sigIdx+1)
if e != nil {
err = e
return
}
aryIdx := startIdx + 4
tmpSlice := make([]interface{}, 0)
for aryIdx < (startIdx+4)+int(arySize) {
retSlice, retidx, e := Parse(buff, sigBlock, aryIdx)
if e != nil {
err = e
return
}
tmpSlice = append(tmpSlice, retSlice...)
aryIdx = retidx
}
bufIdx = aryIdx
sigIdx += (1 + len(sigBlock))
slice = append(slice, tmpSlice)
case '(': // struct
idx := _Align(8, bufIdx)
stSig, e := _GetStructSig(sig, sigIdx)
if e != nil {
err = e
return
}
retSlice, retidx, e := Parse(buff, stSig, idx)
if e != nil {
err = e
return
}
bufIdx = retidx
sigIdx += (len(stSig) + 2)
slice = append(slice, retSlice)
case '{': // dict
idx := _Align(8, bufIdx)
stSig, e := _GetDictSig(sig, sigIdx)
if e != nil {
err = e
return
}
retSlice, retidx, e := Parse(buff, stSig, idx)
if e != nil {
err = e
return
}
bufIdx = retidx
sigIdx += (len(stSig) + 2)
slice = append(slice, retSlice)
case 'v': // variant
vals, idx, e := _GetVariant(buff, bufIdx)
if e != nil {
err = e
return
}
bufIdx = idx
sigIdx++
slice = append(slice, vals...)
default:
fmt.Println(sig[sigIdx])
return nil, index, errors.New("unknown type")
}
}
return
}