amf.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package util
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. )
  7. // Action Message Format -- AMF 0
  8. // Action Message Format -- AMF 3
  9. // http://download.macromedia.com/pub/labs/amf/amf0_spec_121207.pdf
  10. // http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/amf/pdf/amf-file-format-spec.pdf
  11. // AMF Object == AMF Object Type(1 byte) + AMF Object Value
  12. //
  13. // AMF Object Value :
  14. // AMF0_STRING : 2 bytes(datasize,记录string的长度) + data(string)
  15. // AMF0_OBJECT : AMF0_STRING + AMF Object
  16. // AMF0_NULL : 0 byte
  17. // AMF0_NUMBER : 8 bytes
  18. // AMF0_DATE : 10 bytes
  19. // AMF0_BOOLEAN : 1 byte
  20. // AMF0_ECMA_ARRAY : 4 bytes(arraysize,记录数组的长度) + AMF0_OBJECT
  21. // AMF0_STRICT_ARRAY : 4 bytes(arraysize,记录数组的长度) + AMF Object
  22. // 实际测试时,AMF0_ECMA_ARRAY数据如下:
  23. // 8 0 0 0 13 0 8 100 117 114 97 116 105 111 110 0 0 0 0 0 0 0 0 0 0 5 119 105 100 116 104 0 64 158 0 0 0 0 0 0 0 6 104 101 105 103 104 116 0 64 144 224 0 0 0 0 0
  24. // 8 0 0 0 13 | { 0 8 100 117 114 97 116 105 111 110 --- 0 0 0 0 0 0 0 0 0 } | { 0 5 119 105 100 116 104 --- 0 64 158 0 0 0 0 0 0 } | { 0 6 104 101 105 103 104 116 --- 0 64 144 224 0 0 0 0 0 } |...
  25. // 13 | {AMF0_STRING --- AMF0_NUMBER} | {AMF0_STRING --- AMF0_NUMBER} | {AMF0_STRING --- AMF0_NUMBER} | ...
  26. // 13 | {AMF0_OBJECT} | {AMF0_OBJECT} | {AMF0_OBJECT} | ...
  27. // 13 | {duration --- 0} | {width --- 1920} | {height --- 1080} | ...
  28. const (
  29. AMF0_NUMBER = iota // 浮点数
  30. AMF0_BOOLEAN
  31. AMF0_STRING
  32. AMF0_OBJECT
  33. AMF0_MOVIECLIP
  34. AMF0_NULL
  35. AMF0_UNDEFINED
  36. AMF0_REFERENCE
  37. AMF0_ECMA_ARRAY
  38. AMF0_END_OBJECT
  39. AMF0_STRICT_ARRAY
  40. AMF0_DATE
  41. AMF0_LONG_STRING
  42. AMF0_UNSUPPORTED
  43. AMF0_RECORDSET
  44. AMF0_XML_DOCUMENT
  45. AMF0_TYPED_OBJECT
  46. AMF0_AVMPLUS_OBJECT
  47. )
  48. const (
  49. AMF3_UNDEFINED = iota
  50. AMF3_NULL
  51. AMF3_FALSE
  52. AMF3_TRUE
  53. AMF3_INTEGER
  54. AMF3_DOUBLE
  55. AMF3_STRING
  56. AMF3_XML_DOC
  57. AMF3_DATE
  58. AMF3_ARRAY
  59. AMF3_OBJECT
  60. AMF3_XML
  61. AMF3_BYTE_ARRAY
  62. AMF3_VECTOR_INT
  63. AMF3_VECTOR_UINT
  64. AMF3_VECTOR_DOUBLE
  65. AMF3_VECTOR_OBJECT
  66. AMF3_DICTIONARY
  67. )
  68. var (
  69. END_OBJ = []byte{0, 0, AMF0_END_OBJECT}
  70. ObjectEnd = &struct{}{}
  71. Undefined = &struct{}{}
  72. )
  73. type EcmaArray map[string]any
  74. type AMF struct {
  75. Buffer
  76. }
  77. func ReadAMF[T string | float64 | bool | map[string]any](amf *AMF) (result T) {
  78. value, err := amf.Unmarshal()
  79. if err != nil {
  80. return
  81. }
  82. result, _ = value.(T)
  83. return
  84. }
  85. func (amf *AMF) ReadShortString() (result string) {
  86. return ReadAMF[string](amf)
  87. }
  88. func (amf *AMF) ReadNumber() (result float64) {
  89. return ReadAMF[float64](amf)
  90. }
  91. func (amf *AMF) ReadObject() (result map[string]any) {
  92. return ReadAMF[map[string]any](amf)
  93. }
  94. func (amf *AMF) ReadBool() (result bool) {
  95. return ReadAMF[bool](amf)
  96. }
  97. func (amf *AMF) readKey() (string, error) {
  98. if !amf.CanReadN(2) {
  99. return "", io.ErrUnexpectedEOF
  100. }
  101. l := int(amf.ReadUint16())
  102. if !amf.CanReadN(l) {
  103. return "", io.ErrUnexpectedEOF
  104. }
  105. return string(amf.ReadN(l)), nil
  106. }
  107. func (amf *AMF) readProperty(m map[string]any) (obj any, err error) {
  108. var k string
  109. var v any
  110. if k, err = amf.readKey(); err == nil {
  111. if v, err = amf.Unmarshal(); k == "" && v == ObjectEnd {
  112. obj = m
  113. } else if err == nil {
  114. m[k] = v
  115. }
  116. }
  117. return
  118. }
  119. func (amf *AMF) Unmarshal() (obj any, err error) {
  120. if !amf.CanRead() {
  121. return nil, io.ErrUnexpectedEOF
  122. }
  123. defer func(b Buffer) {
  124. if err != nil {
  125. amf.Buffer = b
  126. }
  127. }(amf.Buffer)
  128. switch t := amf.ReadByte(); t {
  129. case AMF0_NUMBER:
  130. if !amf.CanReadN(8) {
  131. return 0, io.ErrUnexpectedEOF
  132. }
  133. obj = amf.ReadFloat64()
  134. case AMF0_BOOLEAN:
  135. if !amf.CanRead() {
  136. return false, io.ErrUnexpectedEOF
  137. }
  138. obj = amf.ReadByte() == 1
  139. case AMF0_STRING:
  140. obj, err = amf.readKey()
  141. case AMF0_OBJECT:
  142. m := make(map[string]any)
  143. for err == nil && obj == nil {
  144. obj, err = amf.readProperty(m)
  145. }
  146. case AMF0_NULL:
  147. return nil, nil
  148. case AMF0_UNDEFINED:
  149. return Undefined, nil
  150. case AMF0_ECMA_ARRAY:
  151. size := amf.ReadUint32()
  152. m := make(EcmaArray)
  153. for i := uint32(0); i < size && err == nil && obj == nil; i++ {
  154. obj, err = amf.readProperty(m)
  155. }
  156. case AMF0_END_OBJECT:
  157. return ObjectEnd, nil
  158. case AMF0_STRICT_ARRAY:
  159. size := amf.ReadUint32()
  160. var list []any
  161. for i := uint32(0); i < size; i++ {
  162. v, err := amf.Unmarshal()
  163. if err != nil {
  164. return nil, err
  165. }
  166. list = append(list, v)
  167. }
  168. obj = list
  169. case AMF0_DATE:
  170. if !amf.CanReadN(10) {
  171. return 0, io.ErrUnexpectedEOF
  172. }
  173. obj = amf.ReadFloat64()
  174. amf.ReadN(2)
  175. case AMF0_LONG_STRING,
  176. AMF0_XML_DOCUMENT:
  177. if !amf.CanReadN(4) {
  178. return "", io.ErrUnexpectedEOF
  179. }
  180. l := int(amf.ReadUint32())
  181. if !amf.CanReadN(l) {
  182. return "", io.ErrUnexpectedEOF
  183. }
  184. obj = string(amf.ReadN(l))
  185. default:
  186. err = fmt.Errorf("unsupported type:%d", t)
  187. }
  188. return
  189. }
  190. func (amf *AMF) writeProperty(key string, v any) {
  191. amf.WriteUint16(uint16(len(key)))
  192. amf.WriteString(key)
  193. amf.Marshal(v)
  194. }
  195. func MarshalAMFs(v ...any) []byte {
  196. var amf AMF
  197. return amf.Marshals(v...)
  198. }
  199. func (amf *AMF) Marshals(v ...any) []byte {
  200. for _, vv := range v {
  201. amf.Marshal(vv)
  202. }
  203. return amf.Buffer
  204. }
  205. func (amf *AMF) Marshal(v any) []byte {
  206. if v == nil {
  207. amf.WriteByte(AMF0_NULL)
  208. return amf.Buffer
  209. }
  210. switch vv := v.(type) {
  211. case string:
  212. if l := len(vv); l > 0xFFFF {
  213. amf.WriteByte(AMF0_LONG_STRING)
  214. amf.WriteUint32(uint32(l))
  215. } else {
  216. amf.WriteByte(AMF0_STRING)
  217. amf.WriteUint16(uint16(l))
  218. }
  219. amf.WriteString(vv)
  220. case float64, uint, float32, int, int16, int32, int64, uint16, uint32, uint64, uint8, int8:
  221. amf.WriteByte(AMF0_NUMBER)
  222. amf.WriteFloat64(ToFloat64(vv))
  223. case bool:
  224. amf.WriteByte(AMF0_BOOLEAN)
  225. if vv {
  226. amf.WriteByte(1)
  227. } else {
  228. amf.WriteByte(0)
  229. }
  230. case EcmaArray:
  231. if vv == nil {
  232. amf.WriteByte(AMF0_NULL)
  233. return amf.Buffer
  234. }
  235. amf.WriteByte(AMF0_ECMA_ARRAY)
  236. amf.WriteUint32(uint32(len(vv)))
  237. for k, v := range vv {
  238. amf.writeProperty(k, v)
  239. }
  240. amf.Write(END_OBJ)
  241. case map[string]any:
  242. if vv == nil {
  243. amf.WriteByte(AMF0_NULL)
  244. return amf.Buffer
  245. }
  246. amf.WriteByte(AMF0_OBJECT)
  247. for k, v := range vv {
  248. amf.writeProperty(k, v)
  249. }
  250. amf.Write(END_OBJ)
  251. default:
  252. v := reflect.ValueOf(vv)
  253. if !v.IsValid() {
  254. amf.WriteByte(AMF0_NULL)
  255. return amf.Buffer
  256. }
  257. switch v.Kind() {
  258. case reflect.Slice, reflect.Array:
  259. amf.WriteByte(AMF0_STRICT_ARRAY)
  260. size := v.Len()
  261. amf.WriteUint32(uint32(size))
  262. for i := 0; i < size; i++ {
  263. amf.Marshal(v.Index(i).Interface())
  264. }
  265. amf.Write(END_OBJ)
  266. default:
  267. panic("amf Marshal faild")
  268. }
  269. }
  270. return amf.Buffer
  271. }