-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutput.go
213 lines (192 loc) · 4.81 KB
/
output.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package msgpackzip
import (
"bytes"
"encoding/binary"
"errors"
)
type outputter struct {
buf bytes.Buffer
}
func (o *outputter) Bytes() []byte {
return o.buf.Bytes()
}
func (o *outputter) outputRawUint(i uint) error {
return o.outputInt(msgpackIntFromUint(i))
}
func (o *outputter) outputMapPrefix(i msgpackInt) (err error) {
return o.outputContainerPrefix(i, 0x80, 0x0f, 0x00, 0xde, 0xdf)
}
func (o *outputter) outputArrayPrefix(i msgpackInt) (err error) {
return o.outputContainerPrefix(i, 0x90, 0x0f, 0x00, 0xdc, 0xdd)
}
func (o *outputter) outputByte(b byte) error {
buf := []byte{b}
_, err := o.buf.Write(buf)
return err
}
func (o *outputter) outputBinaryInt(i interface{}) error {
return binary.Write(&o.buf, binary.BigEndian, i)
}
func (o *outputter) outputPrefixAndBinaryInt(b byte, i interface{}) error {
err := o.outputByte(b)
if err != nil {
return err
}
return binary.Write(&o.buf, binary.BigEndian, i)
}
func (o *outputter) outputContainerPrefix(i msgpackInt, fixed byte, numFixed byte, u8 byte, u16 byte, u32 byte) (err error) {
switch i.typ {
case intTypeFixedUint:
if fixed != 0x0 && byte(i.val) <= numFixed {
return o.outputByte(fixed | byte(i.val))
}
fallthrough
case intTypeUint8:
if u8 != 0x0 {
var b [2]byte
b[0] = u8
b[1] = byte(i.val)
_, err = o.buf.Write(b[:])
return err
}
fallthrough
case intTypeUint16:
err = o.outputByte(u16)
if err != nil {
return err
}
err = o.outputBinaryInt(uint16(i.val))
return err
case intTypeUint32:
err = o.outputByte(u32)
if err != nil {
return err
}
err = o.outputBinaryInt(uint32(i.val))
return err
default:
return errors.New("bad container length")
}
}
func (o *outputter) outputString(i msgpackInt, s string) (err error) {
err = o.outputContainerPrefix(i, 0xa0, 0x1f, 0xd9, 0xda, 0xdb)
if err != nil {
return err
}
_, err = o.buf.Write([]byte(s))
return err
}
func (o *outputter) outputBinary(i msgpackInt, b []byte) (err error) {
err = o.outputContainerPrefix(i, 0x00, 0x00, 0xc4, 0xc5, 0xc6)
if err != nil {
return err
}
_, err = o.buf.Write(b)
return err
}
func (o *outputter) outputNil() error {
return o.outputByte(0xc0)
}
func (o *outputter) outputBool(b bool) error {
var val byte
if b {
val = 0xc3
} else {
val = 0xc2
}
return o.outputByte(val)
}
func (o *outputter) outputInt(i msgpackInt) error {
switch i.typ {
case intTypeFixedUint:
return o.outputByte(byte(i.val))
case intTypeFixedInt:
return o.outputByte(byte(0x100 + i.val))
case intTypeUint8:
return o.outputPrefixAndBinaryInt(0xcc, uint8(i.val))
case intTypeUint16:
return o.outputPrefixAndBinaryInt(0xcd, uint16(i.val))
case intTypeUint32:
return o.outputPrefixAndBinaryInt(0xce, uint32(i.val))
case intTypeUint64:
return o.outputPrefixAndBinaryInt(0xcf, i.uval)
case intTypeInt8:
return o.outputPrefixAndBinaryInt(0xd0, int8(i.val))
case intTypeInt16:
return o.outputPrefixAndBinaryInt(0xd1, int16(i.val))
case intTypeInt32:
return o.outputPrefixAndBinaryInt(0xd2, int32(i.val))
case intTypeInt64:
return o.outputPrefixAndBinaryInt(0xd3, i.val)
default:
return errors.New("unhandled int output case")
}
}
func (o *outputter) outputFloat32(b []byte) error {
err := o.outputByte(0xca)
if err != nil {
return err
}
_, err = o.buf.Write(b)
return err
}
func (o *outputter) outputFloat64(b []byte) error {
err := o.outputByte(0xcb)
if err != nil {
return err
}
_, err = o.buf.Write(b)
return err
}
func (o *outputter) decoderHooks() msgpackDecoderHooks {
return msgpackDecoderHooks{
mapStartHook: func(d decodeStack, i msgpackInt) (decodeStack, error) {
err := o.outputMapPrefix(i)
return d, err
},
arrayStartHook: func(d decodeStack, i msgpackInt) (decodeStack, error) {
err := o.outputArrayPrefix(i)
return d, err
},
stringHook: o.outputString,
binaryHook: o.outputBinary,
nilHook: o.outputNil,
boolHook: o.outputBool,
intHook: o.outputInt,
float32Hook: o.outputFloat32,
float64Hook: o.outputFloat64,
}
}
func (o *outputter) outputStringOrUintOrBinary(i interface{}) error {
switch t := i.(type) {
case BinaryMapKey:
return o.outputBinary(msgpackIntFromUint(uint(len(t))), []byte(t))
case string:
return o.outputString(msgpackIntFromUint(uint(len(t))), t)
case int64:
return o.outputInt(msgpackIntFromUint(uint(t)))
default:
return errors.New("Unhandled map key interface type in output path")
}
}
// we we substitute in a binary or string for something in our dictionary,
// we output it as a big endian integer, prefixed by the "external" byte.
func (o *outputter) outputExtUint(u uint) error {
var i interface{}
var b byte
switch {
case u <= 0xff:
b = 0xd4
i = uint8(u)
case u <= 0xffff:
b = 0xd5
i = uint16(u)
case u <= 0xffffffff:
b = 0xd6
i = uint32(u)
default:
b = 0xd7
i = uint64(u)
}
return o.outputPrefixAndBinaryInt(b, i)
}