-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathamf0.go
579 lines (544 loc) · 12.8 KB
/
amf0.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright 2013, zhangpeihao All rights reserved.
package amf
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"sort"
"strings"
"time"
)
//-----------------------------------------------------------------------
// AMF0 Write functions
func WriteMarker(w Writer, mark byte) (n int, err error) {
err = w.WriteByte(mark)
if err != nil {
return 0, err
}
return 1, nil
}
func WriteString(w Writer, str string) (n int, err error) {
length := uint32(len(str))
if length > 0xFFFF {
err = w.WriteByte(AMF0_LONG_STRING_MARKER)
if err != nil {
return 0, err
}
err = WriteUTF8Long(w, str, length)
length += 5
} else {
err = w.WriteByte(AMF0_STRING_MARKER)
if err != nil {
return 0, err
}
err = WriteUTF8(w, str, uint16(length))
length += 3
}
if err != nil {
return 1, err
}
return int(length), nil
}
func WriteUTF8(w Writer, s string, length uint16) error {
err := binary.Write(w, binary.BigEndian, &length)
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}
func WriteUTF8Long(w Writer, s string, length uint32) error {
err := binary.Write(w, binary.BigEndian, &length)
if err != nil {
return err
}
_, err = w.Write([]byte(s))
return err
}
func WriteDouble(w Writer, num float64) (n int, err error) {
err = w.WriteByte(AMF0_NUMBER_MARKER)
if err != nil {
return 0, err
}
err = binary.Write(w, binary.BigEndian, num)
if err != nil {
return 1, err
}
return 9, nil
}
func WriteBoolean(w Writer, b bool) (n int, err error) {
err = w.WriteByte(AMF0_BOOLEAN_MARKER)
if err != nil {
return 0, err
}
if b {
err = w.WriteByte(1)
} else {
err = w.WriteByte(0)
}
if err != nil {
return 1, err
}
return 2, nil
}
func WriteNull(w Writer) (n int, err error) {
err = w.WriteByte(AMF0_NULL_MARKER)
if err != nil {
return 0, err
}
return 1, nil
}
func WriteUndefined(w Writer) (n int, err error) {
err = w.WriteByte(AMF0_UNDEFINED_MARKER)
if err != nil {
return 0, err
}
return 1, nil
}
func WriteEcmaArray(w Writer, arr []interface{}) (n int, err error) {
n, err = WriteMarker(w, AMF0_ECMA_ARRAY_MARKER)
if err != nil {
return
}
length := int32(len(arr))
err = binary.Write(w, binary.BigEndian, &length)
if err != nil {
return
}
n += 4
m := 0
for index, value := range arr {
m, err = WriteObjectName(w, fmt.Sprintf("%d", index))
if err != nil {
return
}
n += m
m, err = WriteValue(w, value)
if err != nil {
return
}
n += m
}
m, err = WriteObjectEndMarker(w)
return n + m, err
}
func WriteObjectMarker(w Writer) (n int, err error) {
return WriteMarker(w, AMF0_OBJECT_MARKER)
}
func WriteObjectEndMarker(w Writer) (n int, err error) {
return w.Write([]byte{0x00, 0x00, AMF0_OBJECT_END_MARKER})
}
func WriteObjectName(w Writer, name string) (n int, err error) {
length := uint16(len(name))
err = WriteUTF8(w, name, length)
return int(length + 2), err
}
// Object's item order is uncertainty.
func WriteObject(w Writer, obj Object) (n int, err error) {
n, err = WriteObjectMarker(w)
if err != nil {
return
}
m := 0
for key, value := range obj {
m, err = WriteObjectName(w, key)
if err != nil {
return
}
n += m
m, err = WriteValue(w, value)
if err != nil {
return
}
n += m
}
m, err = WriteObjectEndMarker(w)
return n + m, err
}
func WriteStruct(w Writer, value reflect.Value) (n int, err error) {
var m int
FOR_LOOP:
for i := 0; i < value.NumField(); i++ {
structField := value.Type().Field(i)
if structField.Anonymous {
m, err = WriteStruct(w, value.Field(i))
if err != nil {
return
}
n += m
} else {
name := structField.Tag.Get("amf")
switch name {
case "":
name = structField.Name
case "-":
continue FOR_LOOP
default:
if strings.HasSuffix(name, ",omitempty") {
if value.IsNil() {
continue FOR_LOOP
}
name = strings.Split(name, ",")[0]
if len(name) == 0 {
name = structField.Name
}
}
}
m, err = WriteObjectName(w, name)
if err != nil {
return
}
n += m
field := value.Field(i)
m, err = writeValue(w, field)
if err != nil {
return
}
n += m
}
}
return n, nil
}
func WriteValue(w Writer, value interface{}) (n int, err error) {
if value == nil {
return WriteNull(w)
}
v := reflect.ValueOf(value)
if !v.IsValid() {
return WriteNull(w)
}
return writeValue(w, v)
}
func writeValue(w Writer, v reflect.Value) (n int, err error) {
switch v.Kind() {
case reflect.String:
return WriteString(w, v.String())
case reflect.Bool:
return WriteBoolean(w, v.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return WriteDouble(w, float64(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return WriteDouble(w, float64(v.Uint()))
case reflect.Float32, reflect.Float64:
return WriteDouble(w, v.Float())
case reflect.Array:
fallthrough
case reflect.Slice:
// Copy from WriteEcmaArray
n, err = WriteMarker(w, AMF0_ECMA_ARRAY_MARKER)
if err != nil {
return
}
length := int32(v.Len())
err = binary.Write(w, binary.BigEndian, &length)
if err != nil {
return
}
n += 4
m := 0
for index := int32(0); index < length; index++ {
m, err = WriteObjectName(w, fmt.Sprintf("%d", index))
if err != nil {
return
}
n += m
m, err = WriteValue(w, v.Index(int(index)).Interface())
if err != nil {
return
}
n += m
}
m, err = WriteObjectEndMarker(w)
return n + m, err
case reflect.Map:
if v.Type().Key().Kind() != reflect.String {
return 0, errors.New("Unsupported type")
}
if v.IsNil() {
return WriteNull(w)
}
n, err = WriteObjectMarker(w)
if err != nil {
return
}
m := 0
var sv stringValues = v.MapKeys()
sort.Sort(sv)
for _, k := range sv {
m, err = WriteObjectName(w, k.String())
if err != nil {
return
}
n += m
m, err = WriteValue(w, v.MapIndex(k).Interface())
if err != nil {
return
}
n += m
}
m, err = WriteObjectEndMarker(w)
return n + m, err
case reflect.Ptr:
if v.IsNil() || !v.IsValid() {
return WriteNull(w)
}
return WriteValue(w, v.Elem().Interface())
case reflect.Struct:
n, err = WriteObjectMarker(w)
if err != nil {
return
}
m := 0
m, err = WriteStruct(w, v)
if err != nil {
return
}
n += m
m, err = WriteObjectEndMarker(w)
return n + m, err
}
value := v.Interface()
if value != nil {
if _, ok := value.(Undefined); ok {
return WriteUndefined(w)
} else if vt, ok := value.(Object); ok {
return WriteObject(w, vt)
} else if vt, ok := value.([]interface{}); ok {
return WriteEcmaArray(w, vt)
}
}
return 0, errors.New("Unsupported type")
}
//-----------------------------------------------------------------------
// AMF0 Read functions
func ReadMarker(r Reader) (mark byte, err error) {
return r.ReadByte()
}
func ReadString(r Reader) (str string, err error) {
marker, err := ReadMarker(r)
if err != nil {
return "", err
}
switch marker {
case AMF0_STRING_MARKER:
return ReadUTF8(r)
case AMF0_LONG_STRING_MARKER:
return ReadUTF8Long(r)
}
return "", errors.New("Type error")
}
func ReadUTF8(r Reader) (string, error) {
var stringLength uint16
err := binary.Read(r, binary.BigEndian, &stringLength)
if err != nil {
return "", err
}
if stringLength == 0 {
return "", nil
}
data := make([]byte, stringLength)
_, err = r.Read(data)
if err != nil {
return "", err
}
return string(data), nil
}
func ReadUTF8Long(r Reader) (string, error) {
var stringLength uint32
err := binary.Read(r, binary.BigEndian, &stringLength)
if err != nil {
return "", err
}
if stringLength == 0 {
return "", nil
}
data := make([]byte, stringLength)
_, err = r.Read(data)
if err != nil {
return "", err
}
return string(data), nil
}
func ReadDouble(r Reader) (num float64, err error) {
marker, err := ReadMarker(r)
if err != nil {
return 0, err
}
if marker != AMF0_NUMBER_MARKER {
return 0, errors.New("Type error")
}
err = binary.Read(r, binary.BigEndian, &num)
return
}
func ReadBoolean(r Reader) (b bool, err error) {
marker, err := ReadMarker(r)
if err != nil {
return false, err
}
if marker != AMF0_BOOLEAN_MARKER {
return false, errors.New("Type error")
}
value, err := r.ReadByte()
return bool(value != 0), nil
}
func ReadObjectName(r Reader) (name string, err error) {
return ReadUTF8(r)
}
func ReadObject(r Reader) (obj Object, err error) {
marker, err := ReadMarker(r)
if err != nil {
return nil, err
}
if marker != AMF0_OBJECT_MARKER {
return nil, errors.New("Type error")
}
return ReadObjectProperty(r)
}
func ReadObjectProperty(r Reader) (Object, error) {
obj := make(Object)
for {
name, err := ReadUTF8(r)
if err != nil {
return nil, err
}
if name == "" {
b, err := r.ReadByte()
if err != nil {
return nil, err
}
if b == AMF0_OBJECT_END_MARKER {
break
} else {
return nil, errors.New("expect ObjectEndMarker here")
}
}
if _, ok := obj[name]; ok {
return nil, errors.New("object-property exists")
}
value, err := ReadValue(r)
if err != nil {
return nil, err
}
obj[name] = value
}
return obj, nil
}
// A strict Array contains only ordinal indices; however, in AMF 0 the indices can be dense
// or sparse. Undefined entries in the sparse regions between indices are serialized as
// undefined.
//
// array-count = U32
// strict-array-type = array-count *(value-type)
//
// A 32-bit array-count implies a theoretical maximum of 4,294,967,295 array entries.
func ReadStrictArray(r Reader) (arr []interface{}, err error) {
var arrayCount uint32
err = binary.Read(r, binary.BigEndian, &arrayCount)
if err != nil {
return nil, err
}
if arrayCount == 0 {
return
}
arr = make([]interface{}, arrayCount)
for i := uint32(0); i < arrayCount; i++ {
arr[i], err = ReadValue(r)
if err != nil {
return nil, err
}
}
return
}
// An ActionScript Date is serialized as the number of milliseconds elapsed since the epoch
// of midnight on 1st Jan 1970 in the UTC time zone. While the design of this type reserves
// room for time zone offset information, it should not be filled in, nor used, as it is
// unconventional to change time zones when serializing dates on a network. It is suggested
// that the time zone be queried independently as needed.
// time-zone = S16 ; reserved,
// ; not supported
// ; should be set
// Keng-die: time-zone = int16 * -60 (seconds)
// ; to 0x0000
// date-type = date-marker DOUBLE time-zone
func ReadDate(r Reader) (t time.Time, err error) {
var d float64
var timeZone int16
if err = binary.Read(r, binary.BigEndian, &d); err == nil {
// time-zone
err = binary.Read(r, binary.BigEndian, &timeZone)
if err != nil {
fmt.Printf("ReadDate() Read time zone err: %s\n", err)
return
}
d /= 1000.0
// d += float64(timeZone) * -60.0
sec := int64(d)
nsec := int64((d - float64(sec)) * 1000000000.0)
t = time.Unix(sec, nsec)
} else {
fmt.Printf("ReadDate() ReadDouble err: %s\n", err)
}
return
}
func ReadValue(r Reader) (value interface{}, err error) {
marker, err := ReadMarker(r)
if err != nil {
return 0, err
}
switch marker {
case AMF0_NUMBER_MARKER:
var num float64
err = binary.Read(r, binary.BigEndian, &num)
return num, err
case AMF0_BOOLEAN_MARKER:
b, err := r.ReadByte()
if err != nil {
return nil, err
}
return bool(b != 0), nil
case AMF0_STRING_MARKER:
return ReadUTF8(r)
case AMF0_OBJECT_MARKER:
return ReadObjectProperty(r)
case AMF0_MOVIECLIP_MARKER:
return nil, errors.New("Unsupported type: movie clip")
case AMF0_NULL_MARKER:
return nil, nil
case AMF0_UNDEFINED_MARKER:
return Undefined{}, nil
case AMF0_REFERENCE_MARKER:
return nil, errors.New("Unsupported type: reference")
case AMF0_ECMA_ARRAY_MARKER:
// Decode ECMA Array to object
arrLen := make([]byte, 4)
_, err = r.Read(arrLen)
if err != nil {
return nil, err
}
obj, err := ReadObjectProperty(r)
if err != nil {
return nil, err
}
return obj, nil
case AMF0_OBJECT_END_MARKER:
return nil, errors.New("Marker error, Object end")
case AMF0_STRICT_ARRAY_MARKER:
return ReadStrictArray(r)
case AMF0_DATE_MARKER:
return ReadDate(r)
case AMF0_LONG_STRING_MARKER:
return ReadUTF8Long(r)
case AMF0_UNSUPPORTED_MARKER:
return nil, errors.New("Unsupported type: unsupported")
case AMF0_RECORDSET_MARKER:
return nil, errors.New("Unsupported type: recordset")
case AMF0_XML_DOCUMENT_MARKER:
return nil, errors.New("Unsupported type: XML document")
case AMF0_TYPED_OBJECT_MARKER:
return nil, errors.New("Unsupported type: typed object")
case AMF0_ACMPLUS_OBJECT_MARKER:
return AMF3_ReadValue(r)
}
return nil, errors.New(fmt.Sprintf("Unknown marker type: %d", marker))
}