-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamf0serializer.go
193 lines (173 loc) · 3.99 KB
/
amf0serializer.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
package nxamf
import "time"
import "reflect"
type AMF0_Serializer struct {
Stream *OutputStream
}
type MixedArray map[string]interface{}
func (a *AMF0_Serializer) WriteAMFData(data interface{}, forcetype int) {
types := AMF0Const()
// Recognize the type if it's not forced
var typ int = -1
if forcetype==-1 {
switch data.(type) {
case nil:
typ = types["DT_NULL"]
break
case bool:
typ = types["DT_BOOL"]
break
case float64,int:
typ = types["DT_NUMBER"]
break
case string:
if len(data.(string))>65536 {
typ = types["DT_LONGSTRING"]
} else {
typ = types["DT_STRING"]
}
break
case map[string]interface{}:
typ = types["DT_OBJECT"]
break
case []interface{}:
typ = types["DT_ARRAY"]
break
case MixedArray:
typ = types["DT_MIXEDARRAY"]
break
case *AMF3_Wrapper:
typ = types["DT_AMF3"]
break
case time.Time:
typ = types["DT_DATE"]
break
default:
if reflect.ValueOf(data).Kind() == reflect.Struct {
//if a.GetRemoteClassName(data.(string)) != "" { // temporary
if false == true { // typed object not supported alright
typ = types["DT_TYPEDOBJECT"]
break
}
}
}
} else {
typ = forcetype
}
a.Stream.WriteByte(typ)
switch typ {
case types["DT_NUMBER"]:
switch i := data.(type) {
case float64:
a.Stream.WriteDouble(i)
case float32:
a.Stream.WriteDouble(float64(i))
case int:
a.Stream.WriteDouble(float64(i))
}
break
case types["DT_BOOL"]:
c := 0
if data.(bool) {
c = 1
}
a.Stream.WriteByte(c)
break
case types["DT_STRING"]:
a.WriteString(data.(string))
break
case types["DT_OBJECT"]:
a.WriteObject(data.(map[string]interface{}))
break
case types["DT_NULL"]:
break
case types["DT_MIXEDARRAY"]:
a.WriteMixedArray(data.(MixedArray))
break
case types["DT_ARRAY"]:
a.WriteArray(data.([]interface{}))
break
case types["DT_DATE"]:
a.WriteDate(data.(time.Time))
break
case types["DT_LONGSTRING"]:
a.WriteLongString(data.(string))
break
case types["DT_TYPEDOBJECT"]:
a.WriteTypedObject(data)
break
case types["DT_AMF3"]:
a.WriteAMF3Data(data.(*AMF3_Wrapper))
break
}
}
func (a *AMF0_Serializer) WriteMixedArray(data MixedArray) {
a.Stream.WriteLong(0)
for k,v := range data {
a.WriteString(string(k))
a.WriteAMFData(v,-1)
}
a.WriteString("");
a.Stream.WriteByte(AMF0Const()["DT_OBJECTTERM"])
}
func (a *AMF0_Serializer) WriteArray(data []interface{}) {
if len(data)<1 {
a.Stream.WriteLong(0)
} else {
a.Stream.WriteLong(uint64(len(data)))
for i := range data {
if data[i] != nil {
a.WriteAMFData(data[i],-1)
} else {
a.Stream.WriteByte(AMF0Const()["DT_UNDEFINED"])
}
}
}
}
func (a *AMF0_Serializer) WriteObject(data map[string]interface{}) {
for i,v := range data {
a.WriteString(i)
a.WriteAMFData(v,-1)
}
a.WriteString("")
a.Stream.WriteByte(AMF0Const()["DT_OBJECTTERM"])
}
func (a *AMF0_Serializer) WriteString(data string) {
a.Stream.WriteInt(uint16(len(data)))
a.Stream.WriteBuffer(data)
}
func (a *AMF0_Serializer) WriteLongString(data string) {
a.Stream.WriteLong(uint64(len(data)))
a.Stream.WriteBuffer(data)
}
func (a *AMF0_Serializer) WriteTypedObject(data interface{}) {
// im lost
// this isnt supported alright
}
func (a *AMF0_Serializer) WriteAMF3Data(data *AMF3_Wrapper) {
NewAMF3_Serializer(a.Stream).WriteAMFData(data.GetData(),-1)
}
func (a *AMF0_Serializer) WriteDate(data time.Time) {
a.Stream.WriteDouble(float64(data.Unix()*1000))
a.Stream.WriteInt(0)
}
// Checks whether array (or in this case map[int]interface{}) is sparse or not, for some reason doesn't work even if indexes are in a good order..
// Doesn't work :trollface:
func (a *AMF0_Serializer) IsPureArray(array map[int]interface{}) bool {
var i int = 0
for k,_ := range array {
if k!=i {
return false
}
i+=1
}
return true
}
func (a *AMF0_Serializer) GetStream() *OutputStream {
return a.Stream
}
func NewAMF0_Serializer(str *OutputStream) *AMF0_Serializer {
a := new(AMF0_Serializer)
a.Stream = str
return a
}