You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
3.7 KiB

  1. package msgpack
  2. import (
  3. "bytes"
  4. "fmt"
  5. "reflect"
  6. "sync"
  7. "github.com/vmihailenco/msgpack/codes"
  8. )
  9. var extTypes = make(map[int8]reflect.Type)
  10. var bufferPool = &sync.Pool{
  11. New: func() interface{} {
  12. return new(bytes.Buffer)
  13. },
  14. }
  15. // RegisterExt records a type, identified by a value for that type,
  16. // under the provided id. That id will identify the concrete type of a value
  17. // sent or received as an interface variable. Only types that will be
  18. // transferred as implementations of interface values need to be registered.
  19. // Expecting to be used only during initialization, it panics if the mapping
  20. // between types and ids is not a bijection.
  21. func RegisterExt(id int8, value interface{}) {
  22. typ := reflect.TypeOf(value)
  23. if typ.Kind() == reflect.Ptr {
  24. typ = typ.Elem()
  25. }
  26. ptr := reflect.PtrTo(typ)
  27. if _, ok := extTypes[id]; ok {
  28. panic(fmt.Errorf("msgpack: ext with id=%d is already registered", id))
  29. }
  30. registerExt(id, ptr, getEncoder(ptr), nil)
  31. registerExt(id, typ, getEncoder(typ), getDecoder(typ))
  32. }
  33. func registerExt(id int8, typ reflect.Type, enc encoderFunc, dec decoderFunc) {
  34. if dec != nil {
  35. extTypes[id] = typ
  36. }
  37. if enc != nil {
  38. typEncMap[typ] = makeExtEncoder(id, enc)
  39. }
  40. if dec != nil {
  41. typDecMap[typ] = dec
  42. }
  43. }
  44. func makeExtEncoder(typeId int8, enc encoderFunc) encoderFunc {
  45. return func(e *Encoder, v reflect.Value) error {
  46. buf := bufferPool.Get().(*bytes.Buffer)
  47. defer bufferPool.Put(buf)
  48. buf.Reset()
  49. oldw := e.w
  50. e.w = buf
  51. err := enc(e, v)
  52. e.w = oldw
  53. if err != nil {
  54. return err
  55. }
  56. if err := e.encodeExtLen(buf.Len()); err != nil {
  57. return err
  58. }
  59. if err := e.w.WriteByte(byte(typeId)); err != nil {
  60. return err
  61. }
  62. return e.write(buf.Bytes())
  63. }
  64. }
  65. func (e *Encoder) encodeExtLen(l int) error {
  66. switch l {
  67. case 1:
  68. return e.writeCode(codes.FixExt1)
  69. case 2:
  70. return e.writeCode(codes.FixExt2)
  71. case 4:
  72. return e.writeCode(codes.FixExt4)
  73. case 8:
  74. return e.writeCode(codes.FixExt8)
  75. case 16:
  76. return e.writeCode(codes.FixExt16)
  77. }
  78. if l < 256 {
  79. return e.write1(codes.Ext8, uint64(l))
  80. }
  81. if l < 65536 {
  82. return e.write2(codes.Ext16, uint64(l))
  83. }
  84. return e.write4(codes.Ext32, uint32(l))
  85. }
  86. func (d *Decoder) parseExtLen(c codes.Code) (int, error) {
  87. switch c {
  88. case codes.FixExt1:
  89. return 1, nil
  90. case codes.FixExt2:
  91. return 2, nil
  92. case codes.FixExt4:
  93. return 4, nil
  94. case codes.FixExt8:
  95. return 8, nil
  96. case codes.FixExt16:
  97. return 16, nil
  98. case codes.Ext8:
  99. n, err := d.uint8()
  100. return int(n), err
  101. case codes.Ext16:
  102. n, err := d.uint16()
  103. return int(n), err
  104. case codes.Ext32:
  105. n, err := d.uint32()
  106. return int(n), err
  107. default:
  108. return 0, fmt.Errorf("msgpack: invalid code=%x decoding ext length", c)
  109. }
  110. }
  111. func (d *Decoder) extInterface(c codes.Code) (interface{}, error) {
  112. extLen, err := d.parseExtLen(c)
  113. if err != nil {
  114. return nil, err
  115. }
  116. extId, err := d.readCode()
  117. if err != nil {
  118. return nil, err
  119. }
  120. typ, ok := extTypes[int8(extId)]
  121. if !ok {
  122. return nil, fmt.Errorf("msgpack: unregistered ext id=%d", int8(extId))
  123. }
  124. v := reflect.New(typ)
  125. d.extLen = extLen
  126. err = d.DecodeValue(v.Elem())
  127. d.extLen = 0
  128. if err != nil {
  129. return nil, err
  130. }
  131. return v.Interface(), nil
  132. }
  133. func (d *Decoder) skipExt(c codes.Code) error {
  134. n, err := d.parseExtLen(c)
  135. if err != nil {
  136. return err
  137. }
  138. return d.skipN(n + 1)
  139. }
  140. func (d *Decoder) skipExtHeader(c codes.Code) error {
  141. // Read ext type.
  142. _, err := d.readCode()
  143. if err != nil {
  144. return err
  145. }
  146. // Read ext body len.
  147. for i := 0; i < extHeaderLen(c); i++ {
  148. _, err := d.readCode()
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. return nil
  154. }
  155. func extHeaderLen(c codes.Code) int {
  156. switch c {
  157. case codes.Ext8:
  158. return 1
  159. case codes.Ext16:
  160. return 2
  161. case codes.Ext32:
  162. return 4
  163. }
  164. return 0
  165. }