Repository navigation
Expand file tree
/
Copy paththrift.go
More file actions
394 lines (352 loc) · 10.6 KB
/
Copy paththrift.go
File metadata and controls
394 lines (352 loc) · 10.6 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package dbimp
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"math"
)
// The code in this file reads and writes the binary protocol of Apache
// Thrift, with the strict form of a message, for a driver whose server speaks
// it, such as Hive and Impala (D232). It reads one item at a time, so a driver
// never holds a whole result in memory (D25). The meaning of a field belongs
// to the driver, so this code reads and writes fields and gives them no
// meaning. A reader that finds a field that it does not know calls Skip.
// ThriftType is the type of a Thrift value on the wire.
type ThriftType byte
// The types of the binary protocol. ThriftStop ends a struct.
const (
ThriftStop ThriftType = 0
ThriftBool ThriftType = 2
ThriftI8 ThriftType = 3
ThriftDouble ThriftType = 4
ThriftI16 ThriftType = 6
ThriftI32 ThriftType = 8
ThriftI64 ThriftType = 10
ThriftString ThriftType = 11
ThriftStruct ThriftType = 12
ThriftMap ThriftType = 13
ThriftSet ThriftType = 14
ThriftList ThriftType = 15
)
// ThriftMessageType is the type of a message.
type ThriftMessageType byte
// The types of a message.
const (
// ThriftCall is a request that expects a reply.
ThriftCall ThriftMessageType = 1
// ThriftReply is the answer to a call.
ThriftReply ThriftMessageType = 2
// ThriftException is the answer to a call that the server could not
// run. Its body is a struct with a message and a type.
ThriftException ThriftMessageType = 3
// ThriftOneway is a request that expects no reply.
ThriftOneway ThriftMessageType = 4
)
// thriftVersion is the high 16 bits of the first word of a strict message.
const thriftVersion = 0x80010000
// maxThriftDepth is the deepest nesting of structs, lists and maps that the
// decoder skips. A deeper value is ErrInvalidValue, so a hostile response
// cannot exhaust the stack.
const maxThriftDepth = 64
// maxThriftCount is the largest count of elements in a list, a set or a map
// that the decoder accepts. A count over it is ErrInvalidValue.
const maxThriftCount = 1 << 28
// thriftChunk is the most that the decoder allocates at once for a string. A
// string that claims a larger length grows as its bytes arrive, so a length
// that lies costs no memory.
const thriftChunk = 64 << 10
// ThriftDecoder reads Thrift values from a reader, one at a time.
type ThriftDecoder struct {
r *bufio.Reader
buf [8]byte
}
// NewThriftDecoder returns a ThriftDecoder that reads from r.
func NewThriftDecoder(r io.Reader) *ThriftDecoder {
return &ThriftDecoder{r: bufio.NewReader(r)}
}
// ReadMessage reads the head of a strict message: its name, its type and its
// sequence number.
func (d *ThriftDecoder) ReadMessage() (string, ThriftMessageType, int32, error) {
v, err := d.ReadI32()
if err != nil {
return "", 0, 0, err
}
if uint32(v)&0xffff0000 != thriftVersion { //nolint:gosec // G115: the bits are the version.
return "", 0, 0, fmt.Errorf("reading the version %#x of a message: %w", uint32(v), ErrInvalidValue) //nolint:gosec // G115: the bits are the version.
}
name, err := d.ReadString()
if err != nil {
return "", 0, 0, err
}
seq, err := d.ReadI32()
if err != nil {
return "", 0, 0, err
}
return name, ThriftMessageType(v & 0xff), seq, nil
}
// ReadField reads the head of a field of a struct. It returns ThriftStop at
// the end of the struct, and then the id is 0.
func (d *ThriftDecoder) ReadField() (ThriftType, int16, error) {
b, err := d.r.ReadByte()
if err != nil {
return 0, 0, unexpected(err)
}
if ThriftType(b) == ThriftStop {
return ThriftStop, 0, nil
}
id, err := d.ReadI16()
if err != nil {
return 0, 0, err
}
return ThriftType(b), id, nil
}
// ReadBool reads a bool.
func (d *ThriftDecoder) ReadBool() (bool, error) {
b, err := d.r.ReadByte()
if err != nil {
return false, unexpected(err)
}
return b != 0, nil
}
// ReadI8 reads a byte as an int8.
func (d *ThriftDecoder) ReadI8() (int8, error) {
b, err := d.r.ReadByte()
if err != nil {
return 0, unexpected(err)
}
return int8(b), nil //nolint:gosec // G115: Thrift names the byte as signed.
}
// ReadI16 reads an i16.
func (d *ThriftDecoder) ReadI16() (int16, error) {
if _, err := io.ReadFull(d.r, d.buf[:2]); err != nil {
return 0, unexpected(err)
}
return int16(binary.BigEndian.Uint16(d.buf[:2])), nil //nolint:gosec // G115: the bits are the value.
}
// ReadI32 reads an i32.
func (d *ThriftDecoder) ReadI32() (int32, error) {
if _, err := io.ReadFull(d.r, d.buf[:4]); err != nil {
return 0, unexpected(err)
}
return int32(binary.BigEndian.Uint32(d.buf[:4])), nil //nolint:gosec // G115: the bits are the value.
}
// ReadI64 reads an i64.
func (d *ThriftDecoder) ReadI64() (int64, error) {
if _, err := io.ReadFull(d.r, d.buf[:8]); err != nil {
return 0, unexpected(err)
}
return int64(binary.BigEndian.Uint64(d.buf[:8])), nil //nolint:gosec // G115: the bits are the value.
}
// ReadDouble reads a double.
func (d *ThriftDecoder) ReadDouble() (float64, error) {
if _, err := io.ReadFull(d.r, d.buf[:8]); err != nil {
return 0, unexpected(err)
}
return math.Float64frombits(binary.BigEndian.Uint64(d.buf[:8])), nil
}
// ReadBinary reads a string of bytes.
func (d *ThriftDecoder) ReadBinary() ([]byte, error) {
n, err := d.ReadI32()
if err != nil {
return nil, err
}
if n < 0 {
return nil, fmt.Errorf("reading a string of length %d: %w", n, ErrInvalidValue)
}
return d.readN(nil, uint64(n))
}
// ReadString reads a string of text.
func (d *ThriftDecoder) ReadString() (string, error) {
b, err := d.ReadBinary()
if err != nil {
return "", err
}
return string(b), nil
}
// ReadList reads the head of a list or a set. It returns the type of the
// elements and their count.
func (d *ThriftDecoder) ReadList() (ThriftType, int, error) {
b, err := d.r.ReadByte()
if err != nil {
return 0, 0, unexpected(err)
}
n, err := d.ReadI32()
if err != nil {
return 0, 0, err
}
if n < 0 || n > maxThriftCount {
return 0, 0, fmt.Errorf("reading a list of %d elements: %w", n, ErrInvalidValue)
}
return ThriftType(b), int(n), nil
}
// ReadMap reads the head of a map. It returns the types of the keys and of the
// values, and the count of pairs.
func (d *ThriftDecoder) ReadMap() (ThriftType, ThriftType, int, error) {
k, err := d.r.ReadByte()
if err != nil {
return 0, 0, 0, unexpected(err)
}
v, err := d.r.ReadByte()
if err != nil {
return 0, 0, 0, unexpected(err)
}
n, err := d.ReadI32()
if err != nil {
return 0, 0, 0, err
}
if n < 0 || n > maxThriftCount {
return 0, 0, 0, fmt.Errorf("reading a map of %d pairs: %w", n, ErrInvalidValue)
}
return ThriftType(k), ThriftType(v), int(n), nil
}
// Skip reads and drops one value of type t.
func (d *ThriftDecoder) Skip(t ThriftType) error {
return d.skip(t, 0)
}
func (d *ThriftDecoder) skip(t ThriftType, depth int) error {
if depth > maxThriftDepth {
return fmt.Errorf("skipping a value nested deeper than %d: %w", maxThriftDepth, ErrInvalidValue)
}
var err error
switch t {
case ThriftBool, ThriftI8:
_, err = d.r.ReadByte()
err = unexpected(err)
case ThriftI16:
_, err = d.ReadI16()
case ThriftI32:
_, err = d.ReadI32()
case ThriftI64, ThriftDouble:
_, err = d.ReadI64()
case ThriftString:
_, err = d.ReadBinary()
case ThriftStruct:
for {
ft, _, err := d.ReadField()
if err != nil {
return err
}
if ft == ThriftStop {
return nil
}
if err := d.skip(ft, depth+1); err != nil {
return err
}
}
case ThriftMap:
k, v, n, err := d.ReadMap()
if err != nil {
return err
}
for range n {
if err := d.skip(k, depth+1); err != nil {
return err
}
if err := d.skip(v, depth+1); err != nil {
return err
}
}
case ThriftList, ThriftSet:
et, n, err := d.ReadList()
if err != nil {
return err
}
for range n {
if err := d.skip(et, depth+1); err != nil {
return err
}
}
default:
return fmt.Errorf("skipping a value of type %d: %w", t, ErrInvalidValue)
}
return err
}
// readN reads n bytes. A length that is large grows the result a chunk at a
// time, so that a length that lies costs no memory.
func (d *ThriftDecoder) readN(dst []byte, n uint64) ([]byte, error) {
for n > 0 {
c := min(n, thriftChunk)
start := len(dst)
dst = append(dst, make([]byte, c)...)
if _, err := io.ReadFull(d.r, dst[start:]); err != nil {
return nil, unexpected(err)
}
n -= c
}
if dst == nil {
dst = []byte{}
}
return dst, nil
}
// ThriftEncoder writes Thrift values into a buffer.
type ThriftEncoder struct {
b []byte
}
// Bytes returns what the encoder wrote.
func (e *ThriftEncoder) Bytes() []byte {
return e.b
}
// Message writes the head of a strict message.
func (e *ThriftEncoder) Message(name string, t ThriftMessageType, seq int32) {
e.I32(int32(thriftVersion | uint32(t))) //nolint:gosec // G115: the bits are the version.
e.String(name)
e.I32(seq)
}
// Field writes the head of a field of a struct.
func (e *ThriftEncoder) Field(t ThriftType, id int16) {
e.b = append(e.b, byte(t))
e.I16(id)
}
// Stop ends a struct.
func (e *ThriftEncoder) Stop() {
e.b = append(e.b, byte(ThriftStop))
}
// Bool writes a bool.
func (e *ThriftEncoder) Bool(v bool) {
if v {
e.b = append(e.b, 1)
return
}
e.b = append(e.b, 0)
}
// I8 writes an int8.
func (e *ThriftEncoder) I8(v int8) {
e.b = append(e.b, byte(v)) //nolint:gosec // G115: the bits are the value.
}
// I16 writes an i16.
func (e *ThriftEncoder) I16(v int16) {
e.b = binary.BigEndian.AppendUint16(e.b, uint16(v)) //nolint:gosec // G115: the bits are the value.
}
// I32 writes an i32.
func (e *ThriftEncoder) I32(v int32) {
e.b = binary.BigEndian.AppendUint32(e.b, uint32(v)) //nolint:gosec // G115: the bits are the value.
}
// I64 writes an i64.
func (e *ThriftEncoder) I64(v int64) {
e.b = binary.BigEndian.AppendUint64(e.b, uint64(v)) //nolint:gosec // G115: the bits are the value.
}
// Double writes a double.
func (e *ThriftEncoder) Double(v float64) {
e.b = binary.BigEndian.AppendUint64(e.b, math.Float64bits(v))
}
// Binary writes a string of bytes.
func (e *ThriftEncoder) Binary(v []byte) {
e.I32(int32(len(v))) //nolint:gosec // G115: a request is never 2 GiB.
e.b = append(e.b, v...)
}
// String writes a string of text.
func (e *ThriftEncoder) String(v string) {
e.I32(int32(len(v))) //nolint:gosec // G115: a request is never 2 GiB.
e.b = append(e.b, v...)
}
// List writes the head of a list of n elements of type t.
func (e *ThriftEncoder) List(t ThriftType, n int) {
e.b = append(e.b, byte(t))
e.I32(int32(n)) //nolint:gosec // G115: a request is never 2 GiB.
}
// Map writes the head of a map of n pairs.
func (e *ThriftEncoder) Map(k, v ThriftType, n int) {
e.b = append(e.b, byte(k), byte(v))
e.I32(int32(n)) //nolint:gosec // G115: a request is never 2 GiB.
}