forked from vmihailenco/msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.go
More file actions
157 lines (141 loc) · 2.78 KB
/
ext.go
File metadata and controls
157 lines (141 loc) · 2.78 KB
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
package msgpack
import (
"bytes"
"fmt"
"reflect"
"sync"
"gopkg.in/vmihailenco/msgpack.v2/codes"
)
var (
extTypes [128]reflect.Type
)
var bufferPool = &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
func RegisterExt(id int8, value interface{}) {
if extTypes[id] != nil {
panic(fmt.Errorf("ext with id %d is already registered", id))
}
extTypes[id] = reflect.TypeOf(value)
}
func extTypeId(typ reflect.Type) int8 {
for id, t := range extTypes {
if t == typ {
return int8(id)
}
}
return -1
}
func makeExtEncoder(id int8, enc encoderFunc) encoderFunc {
return func(e *Encoder, v reflect.Value) error {
buf := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(buf)
buf.Reset()
oldw := e.w
e.w = buf
err := enc(e, v)
e.w = oldw
if err != nil {
return err
}
if err := e.encodeExtLen(buf.Len()); err != nil {
return err
}
if err := e.w.WriteByte(byte(id)); err != nil {
return err
}
return e.write(buf.Bytes())
}
}
func (e *Encoder) encodeExtLen(l int) error {
if l == 1 {
return e.w.WriteByte(codes.FixExt1)
}
if l == 2 {
return e.w.WriteByte(codes.FixExt2)
}
if l == 4 {
return e.w.WriteByte(codes.FixExt4)
}
if l == 8 {
return e.w.WriteByte(codes.FixExt8)
}
if l == 16 {
return e.w.WriteByte(codes.FixExt16)
}
if l < 256 {
return e.write1(codes.Ext8, uint64(l))
}
if l < 65536 {
return e.write2(codes.Ext16, uint64(l))
}
return e.write4(codes.Ext32, uint64(l))
}
func (d *Decoder) decodeExtLen() (int, error) {
c, err := d.r.ReadByte()
if err != nil {
return 0, err
}
return d.extLen(c)
}
func (d *Decoder) extLen(c byte) (int, error) {
switch c {
case codes.FixExt1:
return 1, nil
case codes.FixExt2:
return 2, nil
case codes.FixExt4:
return 4, nil
case codes.FixExt8:
return 8, nil
case codes.FixExt16:
return 16, nil
case codes.Ext8:
n, err := d.uint8()
return int(n), err
case codes.Ext16:
n, err := d.uint16()
return int(n), err
case codes.Ext32:
n, err := d.uint32()
return int(n), err
default:
return 0, fmt.Errorf("msgpack: invalid code %x decoding ext length", c)
}
}
func (d *Decoder) decodeExt() (interface{}, error) {
c, err := d.r.ReadByte()
if err != nil {
return 0, err
}
return d.ext(c)
}
func (d *Decoder) ext(c byte) (interface{}, error) {
// TODO: use decoded length.
_, err := d.extLen(c)
if err != nil {
return nil, err
}
extId, err := d.r.ReadByte()
if err != nil {
return nil, err
}
typ := extTypes[extId]
if typ == nil {
return nil, fmt.Errorf("msgpack: unregistered ext id %d", extId)
}
v := reflect.New(typ).Elem()
if err := d.DecodeValue(v); err != nil {
return nil, err
}
return v.Interface(), nil
}
func (d *Decoder) skipExt(c byte) error {
n, err := d.extLen(c)
if err != nil {
return err
}
return d.skipN(n)
}