forked from patrickxb/mpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack_reader.go
247 lines (209 loc) · 5.53 KB
/
pack_reader.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package mpack
import (
"encoding/binary"
"errors"
"fmt"
"github.com/okcupid/jsonw"
"io"
"log"
)
type PackReader struct {
reader io.Reader
framed bool
offset int
first bool
}
func (pr *PackReader) expectFraming() bool {
return pr.framed && pr.first
}
func NewPackReader(r io.Reader, f bool) *PackReader {
result := new(PackReader)
result.reader = r
result.framed = f
result.offset = 0
result.first = true
return result
}
func (pr *PackReader) incOffset(i int) {
pr.offset += i
}
func (pr *PackReader) ReadBinary(result interface{}) error {
e := binary.Read(pr.reader, binary.BigEndian, result)
if e == nil {
pr.incOffset(binary.Size(result))
}
return e
}
func (pr *PackReader) ReadUint8() (tmp uint8, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadUint32() (tmp uint32, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadUint16() (tmp uint16, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadUint64() (tmp uint64, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadInt16() (tmp int16, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadInt32() (tmp int32, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadInt64() (tmp uint64, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadFloat32() (tmp float32, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) ReadFloat64() (tmp float64, e error) { e = pr.ReadBinary(&tmp); return }
func (pr *PackReader) unpackRaw(length uint32) (res string, e error) {
e = nil
var n int
if length > 0 {
bytes := make([]byte, length)
// NB: We need to be worried about a short read here,
// so we keep reading until we've filled the expected
// number of bytes!
n, e = io.ReadFull(pr.reader, bytes)
if e == nil {
pr.incOffset(n)
res = string(bytes)
} else {
log.Printf("error in unpack raw (len=%d): %s\n", length, e)
}
}
return
}
func (pr *PackReader) unpackArray(length uint32) (res []interface{}, e error) {
res = nil
e = nil
res = make([]interface{}, length)
for i := uint32(0); e == nil && i < length; i++ {
var elt interface{}
elt, _, e = pr.unpack()
if e == nil {
res[i] = elt
}
}
return
}
func (pr *PackReader) unpackMap(length uint32) (res map[string]interface{}, err error) {
res = make(map[string]interface{})
err = nil
for i := uint32(0); err == nil && i < length; i++ {
var key, val interface{}
key, _, err = pr.unpack()
if err == nil {
val, _, err = pr.unpack()
}
if err != nil {
/* noop */
} else if s, ok := key.(string); ok {
res[s] = val
} else {
msg := fmt.Sprintf("non-string keys for maps not allowed (%s)", key)
err = errors.New(msg)
}
}
return res, err
}
func (pr *PackReader) unpack() (interface{}, int, error) {
frame := 0
frame_len := 0
var b uint8
var err error = nil
var iRes interface{} = nil
var framed bool = pr.expectFraming()
// Threaded implementation won't need to worry, so we can
// just throw the framing away....
if framed {
pr.first = false
var tmp interface{}
tmp, frame_len, err = pr.unpack()
if err == nil {
w := jsonw.NewWrapper(tmp)
frame, err = w.GetInt()
if err == nil {
// Reset the offset so that we start counting from the
// inside of the frame
pr.offset = 0
}
}
}
if err == nil {
b, err = pr.ReadUint8()
}
doswitch := false
if err == nil {
if b <= positive_fix_max {
iRes = b
} else if b >= negative_fix_min && b <= negative_fix_max {
iRes = int8(b)
} else if b >= type_fix_raw && b <= type_fix_raw_max {
iRes, err = pr.unpackRaw(uint32(b & fix_raw_count_mask))
} else if b >= type_fix_array_min && b <= type_fix_array_max {
iRes, err = pr.unpackArray(uint32(b & fix_array_count_mask))
} else if b >= type_fix_map_min && b <= type_fix_map_max {
iRes, err = pr.unpackMap(uint32(b & fix_map_count_mask))
} else {
doswitch = true
}
}
if doswitch && err == nil {
switch b {
case type_nil:
case type_false:
iRes = false
case type_true:
iRes = true
case type_uint8:
iRes, err = pr.ReadUint8()
case type_uint16:
iRes, err = pr.ReadUint16()
case type_uint32:
iRes, err = pr.ReadUint32()
case type_uint64:
iRes, err = pr.ReadUint64()
case type_int16:
iRes, err = pr.ReadInt16()
case type_int32:
iRes, err = pr.ReadInt32()
case type_int64:
iRes, err = pr.ReadInt64()
case type_float:
iRes, err = pr.ReadFloat32()
case type_double:
iRes, err = pr.ReadFloat64()
case type_raw16:
var length uint16
length, err = pr.ReadUint16()
if err == nil {
iRes, err = pr.unpackRaw(uint32(length))
}
case type_raw32:
var length uint32
length, err = pr.ReadUint32()
if err == nil {
iRes, err = pr.unpackRaw(length)
}
case type_array16:
var length uint16
length, err := pr.ReadUint16()
if err == nil {
iRes, err = pr.unpackArray(uint32(length))
}
case type_array32:
var length uint32
length, err = pr.ReadUint32()
if err == nil {
iRes, err = pr.unpackArray(length)
}
case type_map16:
var length uint16
length, err = pr.ReadUint16()
if err == nil {
iRes, err = pr.unpackMap(uint32(length))
}
case type_map32:
var length uint32
length, err = pr.ReadUint32()
if err == nil {
iRes, err = pr.unpackMap(length)
}
default:
log.Printf("unhandled type prefix: %x\n", b)
}
}
numRead := pr.offset
if framed && err == nil && numRead != frame {
log.Printf("bad frame value: %d v %d\n", numRead, frame)
}
return iRes, numRead + frame_len, err
}