ext.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package msgpack
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "github.com/vmihailenco/msgpack/v5/msgpcode"
  7. )
  8. type extInfo struct {
  9. Type reflect.Type
  10. Decoder func(d *Decoder, v reflect.Value, extLen int) error
  11. }
  12. var extTypes = make(map[int8]*extInfo)
  13. type MarshalerUnmarshaler interface {
  14. Marshaler
  15. Unmarshaler
  16. }
  17. func RegisterExt(extID int8, value MarshalerUnmarshaler) {
  18. RegisterExtEncoder(extID, value, func(e *Encoder, v reflect.Value) ([]byte, error) {
  19. marshaler := v.Interface().(Marshaler)
  20. return marshaler.MarshalMsgpack()
  21. })
  22. RegisterExtDecoder(extID, value, func(d *Decoder, v reflect.Value, extLen int) error {
  23. b, err := d.readN(extLen)
  24. if err != nil {
  25. return err
  26. }
  27. return v.Interface().(Unmarshaler).UnmarshalMsgpack(b)
  28. })
  29. }
  30. func UnregisterExt(extID int8) {
  31. unregisterExtEncoder(extID)
  32. unregisterExtDecoder(extID)
  33. }
  34. func RegisterExtEncoder(
  35. extID int8,
  36. value interface{},
  37. encoder func(enc *Encoder, v reflect.Value) ([]byte, error),
  38. ) {
  39. unregisterExtEncoder(extID)
  40. typ := reflect.TypeOf(value)
  41. extEncoder := makeExtEncoder(extID, typ, encoder)
  42. typeEncMap.Store(extID, typ)
  43. typeEncMap.Store(typ, extEncoder)
  44. if typ.Kind() == reflect.Ptr {
  45. typeEncMap.Store(typ.Elem(), makeExtEncoderAddr(extEncoder))
  46. }
  47. }
  48. func unregisterExtEncoder(extID int8) {
  49. t, ok := typeEncMap.Load(extID)
  50. if !ok {
  51. return
  52. }
  53. typeEncMap.Delete(extID)
  54. typ := t.(reflect.Type)
  55. typeEncMap.Delete(typ)
  56. if typ.Kind() == reflect.Ptr {
  57. typeEncMap.Delete(typ.Elem())
  58. }
  59. }
  60. func makeExtEncoder(
  61. extID int8,
  62. typ reflect.Type,
  63. encoder func(enc *Encoder, v reflect.Value) ([]byte, error),
  64. ) encoderFunc {
  65. nilable := typ.Kind() == reflect.Ptr
  66. return func(e *Encoder, v reflect.Value) error {
  67. if nilable && v.IsNil() {
  68. return e.EncodeNil()
  69. }
  70. b, err := encoder(e, v)
  71. if err != nil {
  72. return err
  73. }
  74. if err := e.EncodeExtHeader(extID, len(b)); err != nil {
  75. return err
  76. }
  77. return e.write(b)
  78. }
  79. }
  80. func makeExtEncoderAddr(extEncoder encoderFunc) encoderFunc {
  81. return func(e *Encoder, v reflect.Value) error {
  82. if !v.CanAddr() {
  83. return fmt.Errorf("msgpack: Decode(nonaddressable %T)", v.Interface())
  84. }
  85. return extEncoder(e, v.Addr())
  86. }
  87. }
  88. func RegisterExtDecoder(
  89. extID int8,
  90. value interface{},
  91. decoder func(dec *Decoder, v reflect.Value, extLen int) error,
  92. ) {
  93. unregisterExtDecoder(extID)
  94. typ := reflect.TypeOf(value)
  95. extDecoder := makeExtDecoder(extID, typ, decoder)
  96. extTypes[extID] = &extInfo{
  97. Type: typ,
  98. Decoder: decoder,
  99. }
  100. typeDecMap.Store(extID, typ)
  101. typeDecMap.Store(typ, extDecoder)
  102. if typ.Kind() == reflect.Ptr {
  103. typeDecMap.Store(typ.Elem(), makeExtDecoderAddr(extDecoder))
  104. }
  105. }
  106. func unregisterExtDecoder(extID int8) {
  107. t, ok := typeDecMap.Load(extID)
  108. if !ok {
  109. return
  110. }
  111. typeDecMap.Delete(extID)
  112. delete(extTypes, extID)
  113. typ := t.(reflect.Type)
  114. typeDecMap.Delete(typ)
  115. if typ.Kind() == reflect.Ptr {
  116. typeDecMap.Delete(typ.Elem())
  117. }
  118. }
  119. func makeExtDecoder(
  120. wantedExtID int8,
  121. typ reflect.Type,
  122. decoder func(d *Decoder, v reflect.Value, extLen int) error,
  123. ) decoderFunc {
  124. return nilAwareDecoder(typ, func(d *Decoder, v reflect.Value) error {
  125. extID, extLen, err := d.DecodeExtHeader()
  126. if err != nil {
  127. return err
  128. }
  129. if extID != wantedExtID {
  130. return fmt.Errorf("msgpack: got ext type=%d, wanted %d", extID, wantedExtID)
  131. }
  132. return decoder(d, v, extLen)
  133. })
  134. }
  135. func makeExtDecoderAddr(extDecoder decoderFunc) decoderFunc {
  136. return func(d *Decoder, v reflect.Value) error {
  137. if !v.CanAddr() {
  138. return fmt.Errorf("msgpack: Decode(nonaddressable %T)", v.Interface())
  139. }
  140. return extDecoder(d, v.Addr())
  141. }
  142. }
  143. func (e *Encoder) EncodeExtHeader(extID int8, extLen int) error {
  144. if err := e.encodeExtLen(extLen); err != nil {
  145. return err
  146. }
  147. if err := e.w.WriteByte(byte(extID)); err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. func (e *Encoder) encodeExtLen(l int) error {
  153. switch l {
  154. case 1:
  155. return e.writeCode(msgpcode.FixExt1)
  156. case 2:
  157. return e.writeCode(msgpcode.FixExt2)
  158. case 4:
  159. return e.writeCode(msgpcode.FixExt4)
  160. case 8:
  161. return e.writeCode(msgpcode.FixExt8)
  162. case 16:
  163. return e.writeCode(msgpcode.FixExt16)
  164. }
  165. if l <= math.MaxUint8 {
  166. return e.write1(msgpcode.Ext8, uint8(l))
  167. }
  168. if l <= math.MaxUint16 {
  169. return e.write2(msgpcode.Ext16, uint16(l))
  170. }
  171. return e.write4(msgpcode.Ext32, uint32(l))
  172. }
  173. func (d *Decoder) DecodeExtHeader() (extID int8, extLen int, err error) {
  174. c, err := d.readCode()
  175. if err != nil {
  176. return
  177. }
  178. return d.extHeader(c)
  179. }
  180. func (d *Decoder) extHeader(c byte) (int8, int, error) {
  181. extLen, err := d.parseExtLen(c)
  182. if err != nil {
  183. return 0, 0, err
  184. }
  185. extID, err := d.readCode()
  186. if err != nil {
  187. return 0, 0, err
  188. }
  189. return int8(extID), extLen, nil
  190. }
  191. func (d *Decoder) parseExtLen(c byte) (int, error) {
  192. switch c {
  193. case msgpcode.FixExt1:
  194. return 1, nil
  195. case msgpcode.FixExt2:
  196. return 2, nil
  197. case msgpcode.FixExt4:
  198. return 4, nil
  199. case msgpcode.FixExt8:
  200. return 8, nil
  201. case msgpcode.FixExt16:
  202. return 16, nil
  203. case msgpcode.Ext8:
  204. n, err := d.uint8()
  205. return int(n), err
  206. case msgpcode.Ext16:
  207. n, err := d.uint16()
  208. return int(n), err
  209. case msgpcode.Ext32:
  210. n, err := d.uint32()
  211. return int(n), err
  212. default:
  213. return 0, fmt.Errorf("msgpack: invalid code=%x decoding ext len", c)
  214. }
  215. }
  216. func (d *Decoder) decodeInterfaceExt(c byte) (interface{}, error) {
  217. extID, extLen, err := d.extHeader(c)
  218. if err != nil {
  219. return nil, err
  220. }
  221. info, ok := extTypes[extID]
  222. if !ok {
  223. return nil, fmt.Errorf("msgpack: unknown ext id=%d", extID)
  224. }
  225. v := reflect.New(info.Type).Elem()
  226. if nilable(v.Kind()) && v.IsNil() {
  227. v.Set(reflect.New(info.Type.Elem()))
  228. }
  229. if err := info.Decoder(d, v, extLen); err != nil {
  230. return nil, err
  231. }
  232. return v.Interface(), nil
  233. }
  234. func (d *Decoder) skipExt(c byte) error {
  235. n, err := d.parseExtLen(c)
  236. if err != nil {
  237. return err
  238. }
  239. return d.skipN(n + 1)
  240. }
  241. func (d *Decoder) skipExtHeader(c byte) error {
  242. // Read ext type.
  243. _, err := d.readCode()
  244. if err != nil {
  245. return err
  246. }
  247. // Read ext body len.
  248. for i := 0; i < extHeaderLen(c); i++ {
  249. _, err := d.readCode()
  250. if err != nil {
  251. return err
  252. }
  253. }
  254. return nil
  255. }
  256. func extHeaderLen(c byte) int {
  257. switch c {
  258. case msgpcode.Ext8:
  259. return 1
  260. case msgpcode.Ext16:
  261. return 2
  262. case msgpcode.Ext32:
  263. return 4
  264. }
  265. return 0
  266. }