-
Notifications
You must be signed in to change notification settings - Fork 25
/
amf3.go
478 lines (448 loc) · 9.84 KB
/
amf3.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
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
// Copyright 2013, zhangpeihao All rights reserved.
package amf
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"sort"
)
//-----------------------------------------------------------------------
// AMF3 Write functions
func AMF3_WriteU29(w Writer, n uint32) (num int, err error) {
if n <= 0x0000007F {
err = w.WriteByte(byte(n))
if err != nil {
return 0, err
} else {
return 1, nil
}
} else if n <= 0x00003FFF {
return w.Write([]byte{byte(n>>7 | 0x80), byte(n & 0x7F)})
} else if n <= 0x001FFFFF {
return w.Write([]byte{byte(n>>14 | 0x80), byte(n>>7&0x7F | 0x80), byte(n & 0x7F)})
} else if n <= 0x1FFFFFFF {
return w.Write([]byte{byte(n>>22 | 0x80), byte(n>>15&0x7F | 0x80), byte(n>>8&0x7F | 0x80), byte(n)})
}
return 0, errors.New("out of range")
}
func AMF3_WriteString(w Writer, str string) (n int, err error) {
err = w.WriteByte(AMF3_STRING_MARKER)
if err != nil {
return 0, err
}
n, err = AMF3_WriteUTF8(w, str)
if err != nil {
return 1, err
}
return 1 + n, nil
}
func AMF3_WriteUTF8(w Writer, str string) (num int, err error) {
length := len(str)
if length == 0 {
err = w.WriteByte(0x01)
if err != nil {
return 0, err
} else {
return 1, nil
}
}
u := uint32((length << 1) | 0x01) // Todo: reference
n, err := AMF3_WriteU29(w, u)
if err != nil {
return 0, err
}
m, err := w.Write([]byte(str))
if err != nil {
return n, err
}
return m + n, nil
}
func AMF3_WriteDouble(w Writer, num float64) (n int, err error) {
err = w.WriteByte(AMF3_DOUBLE_MARKER)
if err != nil {
return 0, err
}
err = binary.Write(w, binary.BigEndian, num)
if err != nil {
return 1, err
}
return 9, nil
}
func AMF3_WriteBoolean(w Writer, b bool) (n int, err error) {
if b {
err = w.WriteByte(AMF3_TRUE_MARKER)
} else {
err = w.WriteByte(AMF3_FALSE_MARKER)
}
if err != nil {
return 0, err
}
return 1, nil
}
func AMF3_WriteNull(w Writer) (n int, err error) {
err = w.WriteByte(AMF3_NULL_MARKER)
if err != nil {
return 0, err
}
return 1, nil
}
func AMF3_WriteUndefined(w Writer) (n int, err error) {
err = w.WriteByte(AMF3_UNDEFINED_MARKER)
if err != nil {
return 0, err
}
return 1, nil
}
func AMF3_WriteObjectMarker(w Writer) (n int, err error) {
return WriteMarker(w, AMF3_OBJECT_MARKER)
}
func AMF3_WriteObjectEndMarker(w Writer) (n int, err error) {
err = w.WriteByte(0x01) // Empty string
if err != nil {
return 0, err
}
return 1, nil
}
func AMF3_WriteObjectName(w Writer, name string) (n int, err error) {
return AMF3_WriteUTF8(w, name)
}
// Object's item order is uncertainty.
func AMF3_WriteObject(w Writer, obj Object) (n int, err error) {
n, err = AMF3_WriteObjectMarker(w)
if err != nil {
return
}
m := 0
// Write traits flag, Todo: traits class support
err = w.WriteByte(0x0b)
if err != nil {
return
}
n += 1
// Write empty class name
m, err = AMF3_WriteUTF8(w, "")
if err != nil {
return
}
n += m
for key, value := range obj {
m, err = AMF3_WriteObjectName(w, key)
if err != nil {
return
}
n += m
m, err = AMF3_WriteValue(w, value)
if err != nil {
return
}
n += m
}
m, err = AMF3_WriteObjectEndMarker(w)
return n + m, err
}
func AMF3_WriteValue(w Writer, value interface{}) (n int, err error) {
if value == nil {
return AMF3_WriteNull(w)
}
v := reflect.ValueOf(value)
if !v.IsValid() {
return AMF3_WriteNull(w)
}
switch v.Kind() {
case reflect.String:
return AMF3_WriteString(w, value.(string))
case reflect.Bool:
return AMF3_WriteBoolean(w, v.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return AMF3_WriteDouble(w, float64(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return AMF3_WriteDouble(w, float64(v.Uint()))
case reflect.Float32, reflect.Float64:
return AMF3_WriteDouble(w, v.Float())
case reflect.Array:
fallthrough
case reflect.Slice:
if v.Type().Elem().Kind() == reflect.Uint8 {
// Byte array
n, err = WriteMarker(w, AMF3_BYTEARRAY_MARKER)
if err != nil {
return
}
b := v.Bytes()
length := len(b)
u := uint32((length << 1) | 0x01) // Todo: reference
var m int
m, err = AMF3_WriteU29(w, u)
if err != nil {
return
}
n += m
m, err = w.Write(b)
if err != nil {
return
}
n += m
return
} else {
// Array
// Dense array only
n, err = WriteMarker(w, AMF3_ARRAY_MARKER)
if err != nil {
return
}
// Write dense array length
length := v.Len()
u := uint32((length << 1) | 0x01) // Todo: reference
var m int
m, err = AMF3_WriteU29(w, u)
if err != nil {
return
}
n += m
// Todo: Associative array
// Empty string to end associative array
err = w.WriteByte(0x01)
if err != nil {
return
}
n += 1
for i := 0; i < length; i++ {
m, err = AMF3_WriteValue(w, v.Index(i).Interface())
if err != nil {
return
}
n += m
}
return
}
case reflect.Map:
if v.Type().Key().Kind() != reflect.String {
return 0, errors.New("Unsupported type")
}
n, err = AMF3_WriteObjectMarker(w)
if err != nil {
return
}
m := 0
// Write traits flag, Todo: traits class support
err = w.WriteByte(0x0b)
if err != nil {
return
}
n += 1
// Write empty class name
m, err = AMF3_WriteUTF8(w, "")
if err != nil {
return
}
n += m
var sv stringValues = v.MapKeys()
sort.Sort(sv)
for _, k := range sv {
m, err = AMF3_WriteObjectName(w, k.String())
if err != nil {
return
}
n += m
m, err = AMF3_WriteValue(w, v.MapIndex(k).Interface())
if err != nil {
return
}
n += m
}
m, err = AMF3_WriteObjectEndMarker(w)
return n + m, err
}
if _, ok := value.(Undefined); ok {
return AMF3_WriteUndefined(w)
} else if vt, ok := value.(Object); ok {
return AMF3_WriteObject(w, vt)
} else if vt, ok := value.([]interface{}); ok {
fmt.Printf("Todo: WriteValue: %+v\n", vt)
}
return 0, errors.New("Unsupported type")
}
//-----------------------------------------------------------------------
// AMF3 Read functions
func AMF3_ReadU29(r Reader) (n uint32, err error) {
var b byte
for i := 0; i < 3; i++ {
b, err = r.ReadByte()
if err != nil {
return
}
n = (n << 7) + uint32(b&0x7F)
if (b & 0x80) == 0 {
return
}
}
b, err = r.ReadByte()
if err != nil {
return
}
return ((n << 8) + uint32(b)), nil
}
func AMF3_ReadUTF8(r Reader) (string, error) {
var length uint32
var err error
length, err = AMF3_ReadU29(r)
if err != nil {
return "", err
}
if length&uint32(0x01) != uint32(1) {
// Todo: reference
return "", errors.New("AMF3 reference unsupported")
}
length = length >> 1
if length == 0 {
return "", nil
}
data := make([]byte, length)
_, err = r.Read(data)
if err != nil {
return "", err
}
return string(data), nil
}
func AMF3_ReadString(r Reader) (str string, err error) {
marker, err := ReadMarker(r)
if err != nil {
return "", err
}
if marker != AMF3_STRING_MARKER {
return "", errors.New("Type error")
}
return AMF3_ReadUTF8(r)
}
func AMF3_ReadInteger(r Reader) (num uint32, err error) {
marker, err := ReadMarker(r)
if err != nil {
return 0, err
}
if marker != AMF3_INTEGER_MARKER {
return 0, errors.New("Type error")
}
return AMF3_ReadU29(r)
}
func AMF3_ReadDouble(r Reader) (num float64, err error) {
marker, err := ReadMarker(r)
if err != nil {
return 0, err
}
if marker != AMF3_DOUBLE_MARKER {
return 0, errors.New("Type error")
}
err = binary.Read(r, binary.BigEndian, &num)
return
}
func AMF3_ReadObjectName(r Reader) (name string, err error) {
return AMF3_ReadUTF8(r)
}
func AMF3_ReadObject(r Reader) (obj Object, err error) {
marker, err := ReadMarker(r)
if err != nil {
return nil, err
}
if marker != AMF3_OBJECT_MARKER {
return nil, errors.New("Type error")
}
return AMF3_ReadObjectProperty(r)
}
func AMF3_ReadObjectProperty(r Reader) (Object, error) {
obj := make(Object)
// Read traits flag
b, err := r.ReadByte()
if err != nil {
return nil, err
}
if b != 0x0b {
return nil, errors.New("Unsupported type: traits object")
}
// Read empty string
b, err = r.ReadByte()
if err != nil {
return nil, err
}
if b != 0x01 {
return nil, errors.New("Unsupported type: traits object")
}
for {
name, err := AMF3_ReadObjectName(r)
if err != nil {
return nil, err
}
if name == "" {
break
}
if _, ok := obj[name]; ok {
return nil, errors.New("object-property exists")
}
value, err := AMF3_ReadValue(r)
if err != nil {
return nil, err
}
obj[name] = value
}
return obj, nil
}
func AMF3_ReadByteArray(r Reader) ([]byte, error) {
marker, err := ReadMarker(r)
if err != nil {
return nil, err
}
if marker != AMF3_BYTEARRAY_MARKER {
return nil, errors.New("Type error")
}
return AMF3_readByteArray(r)
}
func AMF3_readByteArray(r Reader) ([]byte, error) {
length, err := AMF3_ReadU29(r)
if err != nil {
return nil, err
}
if length&uint32(0x01) != uint32(0x01) {
return nil, errors.New("Unsupported type: reference")
}
length = (length >> 1)
buf := make([]byte, length)
n, err := r.Read(buf)
if err != nil {
return nil, err
}
if n != int(length) {
return nil, errors.New("Read buffer size error")
}
return buf, nil
}
func AMF3_ReadValue(r Reader) (value interface{}, err error) {
marker, err := ReadMarker(r)
if err != nil {
return 0, err
}
switch marker {
case AMF3_UNDEFINED_MARKER:
return Undefined{}, nil
case AMF3_NULL_MARKER:
return nil, nil
case AMF3_FALSE_MARKER:
return false, nil
case AMF3_TRUE_MARKER:
return true, nil
case AMF3_INTEGER_MARKER:
return AMF3_ReadU29(r)
case AMF3_DOUBLE_MARKER:
var num float64
err = binary.Read(r, binary.BigEndian, &num)
return num, err
case AMF3_STRING_MARKER:
return AMF3_ReadUTF8(r)
case AMF3_ARRAY_MARKER:
// Todo: read array
case AMF3_OBJECT_MARKER:
return AMF3_ReadObjectProperty(r)
case AMF3_BYTEARRAY_MARKER:
return AMF3_readByteArray(r)
}
return nil, errors.New(fmt.Sprintf("Unknown marker type: %d", marker))
}