-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathamf3.go
308 lines (289 loc) · 5.95 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
package util
import (
"errors"
"reflect"
"strconv"
"unicode"
)
const (
AMF3_UNDEFINED = iota
AMF3_NULL
AMF3_FALSE
AMF3_TRUE
AMF3_INTEGER
AMF3_DOUBLE
AMF3_STRING
AMF3_XML_DOC
AMF3_DATE
AMF3_ARRAY
AMF3_OBJECT
AMF3_XML
AMF3_BYTE_ARRAY
AMF3_VECTOR_INT
AMF3_VECTOR_UINT
AMF3_VECTOR_DOUBLE
AMF3_VECTOR_OBJECT
AMF3_DICTIONARY
)
type AMF3 struct {
AMF
scEnc map[string]int
scDec []string
ocEnc map[uintptr]int
ocDec []any
reservStruct bool
}
func (amf *AMF3) readString() (string, error) {
index, err := amf.readU29()
if err != nil {
return "", err
}
ret := ""
if (index & 0x01) == 0 {
ret = amf.scDec[int(index>>1)]
} else {
index >>= 1
ret = string(amf.ReadN(int(index)))
}
if ret != "" {
amf.scDec = append(amf.scDec, ret)
}
return ret, nil
}
func (amf *AMF3) Unmarshal() (obj any, err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New("amf3 unmarshal error")
}
}()
switch amf.ReadByte() {
case AMF3_NULL:
return nil, nil
case AMF3_FALSE:
return false, nil
case AMF3_TRUE:
return true, nil
case AMF3_INTEGER:
return amf.readU29()
case AMF3_DOUBLE:
return amf.ReadFloat64(), nil
case AMF3_STRING:
return amf.readString()
case AMF3_OBJECT:
index, err := amf.readU29()
if err != nil {
return nil, err
}
if (index & 0x01) == 0 {
return amf.ocDec[int(index>>1)], nil
}
if index != 0x0b {
return nil, errors.New("invalid object type")
}
if amf.ReadByte() != 0x01 {
return nil, errors.New("type object not allowed")
}
ret := make(map[string]any)
amf.ocDec = append(amf.ocDec, ret)
for {
key, err := amf.readString()
if err != nil {
return nil, err
}
if key == "" {
break
}
ret[key], err = amf.Unmarshal()
if err != nil {
return nil, err
}
}
return ret, nil
}
return nil, errors.New("amf3 unmarshal error")
}
func (amf *AMF3) writeString(s string) error {
index, ok := amf.scEnc[s]
if ok {
amf.writeU29(uint32(index << 1))
return nil
}
err := amf.writeU29(uint32((len(s) << 1) | 0x01))
if err != nil {
return err
}
if s != "" {
amf.scEnc[s] = len(amf.scEnc)
}
amf.WriteString(s)
return nil
}
func (amf *AMF3) readU29() (uint32, error) {
var ret uint32 = 0
for i := 0; i < 4; i++ {
b := amf.ReadByte()
if i != 3 {
ret = (ret << 7) | uint32(b&0x7f)
if (b & 0x80) == 0 {
break
}
} else {
ret = (ret << 8) | uint32(b)
}
}
return ret, nil
}
func (amf *AMF3) writeU29(value uint32) error {
switch {
case value < 0x80:
amf.WriteByte(byte(value))
case value < 0x4000:
amf.Write([]byte{byte((value >> 7) | 0x80), byte(value & 0x7f)})
case value < 0x200000:
amf.Write([]byte{byte((value >> 14) | 0x80), byte((value >> 7) | 0x80), byte(value & 0x7f)})
case value < 0x20000000:
amf.Write([]byte{byte((value >> 22) | 0x80), byte((value >> 15) | 0x80), byte((value >> 7) | 0x80), byte(value & 0xff)})
default:
return errors.New("u29 over flow")
}
return nil
}
func (amf *AMF3) Marshals(v ...any) []byte {
for _, vv := range v {
amf.Marshal(vv)
}
return amf.Buffer
}
func MarshalAMF3s(v ...any) []byte {
var amf AMF3
amf.ocEnc = make(map[uintptr]int)
amf.scEnc = make(map[string]int)
return amf.Marshals(v...)
}
func (amf *AMF3) Marshal(v any) []byte {
if v == nil {
amf.WriteByte(AMF3_NULL)
return amf.Buffer
}
switch vv := v.(type) {
case string:
amf.WriteByte(AMF3_STRING)
amf.writeString(vv)
case bool:
if vv {
amf.WriteByte(AMF3_TRUE)
} else {
amf.WriteByte(AMF3_FALSE)
}
case int, int8, int16, int32, int64:
var value int64
reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
if value < -0xfffffff {
if value > -0x7fffffff {
return amf.Marshal(float64(value))
}
return amf.Marshal(strconv.FormatInt(value, 10))
}
amf.WriteByte(AMF3_INTEGER)
amf.writeU29(uint32(value))
case uint, uint8, uint16, uint32, uint64:
var value uint64
reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
if value >= 0x20000000 {
if value <= 0xffffffff {
return amf.Marshal(float64(value))
}
return amf.Marshal(strconv.FormatUint(value, 10))
}
amf.WriteByte(AMF3_INTEGER)
amf.writeU29(uint32(value))
case float32:
amf.Marshal(float64(vv))
case float64:
amf.WriteByte(AMF3_DOUBLE)
amf.WriteFloat64(vv)
case map[string]any:
amf.WriteByte(AMF3_OBJECT)
index, ok := amf.ocEnc[reflect.ValueOf(vv).Pointer()]
if ok {
index <<= 1
amf.writeU29(uint32(index << 1))
return nil
}
amf.WriteByte(0x0b)
err := amf.writeString("")
if err != nil {
return nil
}
for k, v := range vv {
err = amf.writeString(k)
if err != nil {
return nil
}
amf.Marshal(v)
}
amf.writeString("")
default:
v := reflect.ValueOf(vv)
if !v.IsValid() {
amf.WriteByte(AMF3_NULL)
return amf.Buffer
}
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
amf.WriteByte(AMF3_NULL)
return amf.Buffer
}
vv := reflect.Indirect(v)
if vv.Kind() == reflect.Struct {
amf.WriteByte(AMF3_OBJECT)
index, ok := amf.ocEnc[v.Pointer()]
if ok {
index <<= 1
amf.writeU29(uint32(index << 1))
return nil
}
amf.WriteByte(0x0b)
err := amf.writeString("")
if err != nil {
return nil
}
t := vv.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
key := amf.getFieldName(f)
if key == "" {
continue
}
err = amf.writeString(key)
if err != nil {
return nil
}
fv := v.FieldByName(f.Name)
if fv.Kind() == reflect.Struct {
fv = fv.Addr()
}
amf.Marshal(fv.Interface())
}
amf.writeString("")
}
}
}
return amf.Buffer
}
func (amf *AMF3) getFieldName(f reflect.StructField) string {
chars := []rune(f.Name)
if unicode.IsLower(chars[0]) {
return ""
}
name := f.Tag.Get("amf.name")
if name != "" {
return name
}
if !amf.reservStruct {
chars[0] = unicode.ToLower(chars[0])
return string(chars)
}
return f.Name
}