amf3.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package util
  2. import (
  3. "errors"
  4. "reflect"
  5. "strconv"
  6. "unicode"
  7. )
  8. const (
  9. AMF3_UNDEFINED = iota
  10. AMF3_NULL
  11. AMF3_FALSE
  12. AMF3_TRUE
  13. AMF3_INTEGER
  14. AMF3_DOUBLE
  15. AMF3_STRING
  16. AMF3_XML_DOC
  17. AMF3_DATE
  18. AMF3_ARRAY
  19. AMF3_OBJECT
  20. AMF3_XML
  21. AMF3_BYTE_ARRAY
  22. AMF3_VECTOR_INT
  23. AMF3_VECTOR_UINT
  24. AMF3_VECTOR_DOUBLE
  25. AMF3_VECTOR_OBJECT
  26. AMF3_DICTIONARY
  27. )
  28. type AMF3 struct {
  29. AMF
  30. scEnc map[string]int
  31. scDec []string
  32. ocEnc map[uintptr]int
  33. ocDec []any
  34. reservStruct bool
  35. }
  36. func (amf *AMF3) readString() (string, error) {
  37. index, err := amf.readU29()
  38. if err != nil {
  39. return "", err
  40. }
  41. ret := ""
  42. if (index & 0x01) == 0 {
  43. ret = amf.scDec[int(index>>1)]
  44. } else {
  45. index >>= 1
  46. ret = string(amf.ReadN(int(index)))
  47. }
  48. if ret != "" {
  49. amf.scDec = append(amf.scDec, ret)
  50. }
  51. return ret, nil
  52. }
  53. func (amf *AMF3) Unmarshal() (obj any, err error) {
  54. defer func() {
  55. if e := recover(); e != nil {
  56. err = errors.New("amf3 unmarshal error")
  57. }
  58. }()
  59. switch amf.ReadByte() {
  60. case AMF3_NULL:
  61. return nil, nil
  62. case AMF3_FALSE:
  63. return false, nil
  64. case AMF3_TRUE:
  65. return true, nil
  66. case AMF3_INTEGER:
  67. return amf.readU29()
  68. case AMF3_DOUBLE:
  69. return amf.ReadFloat64(), nil
  70. case AMF3_STRING:
  71. return amf.readString()
  72. case AMF3_OBJECT:
  73. index, err := amf.readU29()
  74. if err != nil {
  75. return nil, err
  76. }
  77. if (index & 0x01) == 0 {
  78. return amf.ocDec[int(index>>1)], nil
  79. }
  80. if index != 0x0b {
  81. return nil, errors.New("invalid object type")
  82. }
  83. if amf.ReadByte() != 0x01 {
  84. return nil, errors.New("type object not allowed")
  85. }
  86. ret := make(map[string]any)
  87. amf.ocDec = append(amf.ocDec, ret)
  88. for {
  89. key, err := amf.readString()
  90. if err != nil {
  91. return nil, err
  92. }
  93. if key == "" {
  94. break
  95. }
  96. ret[key], err = amf.Unmarshal()
  97. if err != nil {
  98. return nil, err
  99. }
  100. }
  101. return ret, nil
  102. }
  103. return nil, errors.New("amf3 unmarshal error")
  104. }
  105. func (amf *AMF3) writeString(s string) error {
  106. index, ok := amf.scEnc[s]
  107. if ok {
  108. amf.writeU29(uint32(index << 1))
  109. return nil
  110. }
  111. err := amf.writeU29(uint32((len(s) << 1) | 0x01))
  112. if err != nil {
  113. return err
  114. }
  115. if s != "" {
  116. amf.scEnc[s] = len(amf.scEnc)
  117. }
  118. amf.WriteString(s)
  119. return nil
  120. }
  121. func (amf *AMF3) readU29() (uint32, error) {
  122. var ret uint32 = 0
  123. for i := 0; i < 4; i++ {
  124. b := amf.ReadByte()
  125. if i != 3 {
  126. ret = (ret << 7) | uint32(b&0x7f)
  127. if (b & 0x80) == 0 {
  128. break
  129. }
  130. } else {
  131. ret = (ret << 8) | uint32(b)
  132. }
  133. }
  134. return ret, nil
  135. }
  136. func (amf *AMF3) writeU29(value uint32) error {
  137. switch {
  138. case value < 0x80:
  139. amf.WriteByte(byte(value))
  140. case value < 0x4000:
  141. amf.Write([]byte{byte((value >> 7) | 0x80), byte(value & 0x7f)})
  142. case value < 0x200000:
  143. amf.Write([]byte{byte((value >> 14) | 0x80), byte((value >> 7) | 0x80), byte(value & 0x7f)})
  144. case value < 0x20000000:
  145. amf.Write([]byte{byte((value >> 22) | 0x80), byte((value >> 15) | 0x80), byte((value >> 7) | 0x80), byte(value & 0xff)})
  146. default:
  147. return errors.New("u29 over flow")
  148. }
  149. return nil
  150. }
  151. func (amf *AMF3) Marshals(v ...any) []byte {
  152. for _, vv := range v {
  153. amf.Marshal(vv)
  154. }
  155. return amf.Buffer
  156. }
  157. func MarshalAMF3s(v ...any) []byte {
  158. var amf AMF3
  159. amf.ocEnc = make(map[uintptr]int)
  160. amf.scEnc = make(map[string]int)
  161. return amf.Marshals(v...)
  162. }
  163. func (amf *AMF3) Marshal(v any) []byte {
  164. if v == nil {
  165. amf.WriteByte(AMF3_NULL)
  166. return amf.Buffer
  167. }
  168. switch vv := v.(type) {
  169. case string:
  170. amf.WriteByte(AMF3_STRING)
  171. amf.writeString(vv)
  172. case bool:
  173. if vv {
  174. amf.WriteByte(AMF3_TRUE)
  175. } else {
  176. amf.WriteByte(AMF3_FALSE)
  177. }
  178. case int, int8, int16, int32, int64:
  179. var value int64
  180. reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
  181. if value < -0xfffffff {
  182. if value > -0x7fffffff {
  183. return amf.Marshal(float64(value))
  184. }
  185. return amf.Marshal(strconv.FormatInt(value, 10))
  186. }
  187. amf.WriteByte(AMF3_INTEGER)
  188. amf.writeU29(uint32(value))
  189. case uint, uint8, uint16, uint32, uint64:
  190. var value uint64
  191. reflect.ValueOf(&value).Elem().Set(reflect.ValueOf(vv).Convert(reflect.TypeOf(value)))
  192. if value >= 0x20000000 {
  193. if value <= 0xffffffff {
  194. return amf.Marshal(float64(value))
  195. }
  196. return amf.Marshal(strconv.FormatUint(value, 10))
  197. }
  198. amf.WriteByte(AMF3_INTEGER)
  199. amf.writeU29(uint32(value))
  200. case float32:
  201. amf.Marshal(float64(vv))
  202. case float64:
  203. amf.WriteByte(AMF3_DOUBLE)
  204. amf.WriteFloat64(vv)
  205. case map[string]any:
  206. amf.WriteByte(AMF3_OBJECT)
  207. index, ok := amf.ocEnc[reflect.ValueOf(vv).Pointer()]
  208. if ok {
  209. index <<= 1
  210. amf.writeU29(uint32(index << 1))
  211. return nil
  212. }
  213. amf.WriteByte(0x0b)
  214. err := amf.writeString("")
  215. if err != nil {
  216. return nil
  217. }
  218. for k, v := range vv {
  219. err = amf.writeString(k)
  220. if err != nil {
  221. return nil
  222. }
  223. amf.Marshal(v)
  224. }
  225. amf.writeString("")
  226. default:
  227. v := reflect.ValueOf(vv)
  228. if !v.IsValid() {
  229. amf.WriteByte(AMF3_NULL)
  230. return amf.Buffer
  231. }
  232. switch v.Kind() {
  233. case reflect.Ptr:
  234. if v.IsNil() {
  235. amf.WriteByte(AMF3_NULL)
  236. return amf.Buffer
  237. }
  238. vv := reflect.Indirect(v)
  239. if vv.Kind() == reflect.Struct {
  240. amf.WriteByte(AMF3_OBJECT)
  241. index, ok := amf.ocEnc[v.Pointer()]
  242. if ok {
  243. index <<= 1
  244. amf.writeU29(uint32(index << 1))
  245. return nil
  246. }
  247. amf.WriteByte(0x0b)
  248. err := amf.writeString("")
  249. if err != nil {
  250. return nil
  251. }
  252. t := vv.Type()
  253. for i := 0; i < t.NumField(); i++ {
  254. f := t.Field(i)
  255. key := amf.getFieldName(f)
  256. if key == "" {
  257. continue
  258. }
  259. err = amf.writeString(key)
  260. if err != nil {
  261. return nil
  262. }
  263. fv := v.FieldByName(f.Name)
  264. if fv.Kind() == reflect.Struct {
  265. fv = fv.Addr()
  266. }
  267. amf.Marshal(fv.Interface())
  268. }
  269. amf.writeString("")
  270. }
  271. }
  272. }
  273. return amf.Buffer
  274. }
  275. func (amf *AMF3) getFieldName(f reflect.StructField) string {
  276. chars := []rune(f.Name)
  277. if unicode.IsLower(chars[0]) {
  278. return ""
  279. }
  280. name := f.Tag.Get("amf.name")
  281. if name != "" {
  282. return name
  283. }
  284. if !amf.reservStruct {
  285. chars[0] = unicode.ToLower(chars[0])
  286. return string(chars)
  287. }
  288. return f.Name
  289. }