-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslice_reader.go
197 lines (182 loc) · 5.02 KB
/
slice_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
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package velocypack
import (
"bufio"
"io"
)
const (
maxByteSizeBytes = 16
)
// SliceFromReader reads a slice from the given reader.
func SliceFromReader(r io.Reader) (Slice, error) {
if r, ok := r.(*bufio.Reader); ok {
// Buffered reader can use faster path.
return sliceFromBufReader(r)
}
hdr := make(Slice, 1, maxByteSizeBytes)
// Read first byte
if err := readBytes(hdr, r); err != nil {
if Cause(err) == io.EOF {
// Empty slice
return nil, nil
}
return nil, WithStack(err)
}
// Lookup first size
// check if the type has a fixed length first
l := fixedTypeLengths[hdr[0]]
if l != 0 {
// Found fixed length, read it (minus byte already read)
s := make(Slice, l)
s[0] = hdr[0]
if err := readBytes(s[1:], r); err != nil {
return nil, WithStack(err)
}
return s, nil
}
readRemaining := func(prefix Slice, l ValueLength) (Slice, error) {
s := make(Slice, l)
copy(s, prefix)
if err := readBytes(s[len(prefix):], r); err != nil {
return nil, WithStack(err)
}
return s, nil
}
// types with dynamic lengths need special treatment:
h := hdr[0]
switch hdr.Type() {
case Array, Object:
if h == 0x13 || h == 0x14 {
// compact Array or Object
l, bytes, err := readVariableValueLengthFromReader(r, false)
if err != nil {
return nil, WithStack(err)
}
return readRemaining(append(hdr, bytes...), l)
}
vpackAssert(h > 0x00 && h <= 0x0e)
l, bytes, err := readIntegerNonEmptyFromReader(r, widthMap[h])
if err != nil {
return nil, WithStack(err)
}
return readRemaining(append(hdr, bytes...), ValueLength(l))
case String:
vpackAssert(h == 0xbf)
// long UTF-8 String
l, bytes, err := readIntegerFixedFromReader(r, 8)
if err != nil {
return nil, WithStack(err)
}
return readRemaining(append(hdr, bytes...), ValueLength(l+1+8))
case Binary:
vpackAssert(h >= 0xc0 && h <= 0xc7)
x, bytes, err := readIntegerNonEmptyFromReader(r, uint(h)-0xbf)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(1 + ValueLength(h) - 0xbf + ValueLength(x))
return readRemaining(append(hdr, bytes...), l)
case BCD:
if h <= 0xcf {
// positive BCD
vpackAssert(h >= 0xc8 && h < 0xcf)
x, bytes, err := readIntegerNonEmptyFromReader(r, uint(h)-0xc7)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(1 + ValueLength(h) - 0xc7 + ValueLength(x))
return readRemaining(append(hdr, bytes...), l)
}
// negative BCD
vpackAssert(h >= 0xd0 && h < 0xd7)
x, bytes, err := readIntegerNonEmptyFromReader(r, uint(h)-0xcf)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(1 + ValueLength(h) - 0xcf + ValueLength(x))
return readRemaining(append(hdr, bytes...), l)
case Custom:
vpackAssert(h >= 0xf4)
switch h {
case 0xf4, 0xf5, 0xf6:
x, bytes, err := readIntegerFixedFromReader(r, 1)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(2 + x)
return readRemaining(append(hdr, bytes...), l)
case 0xf7, 0xf8, 0xf9:
x, bytes, err := readIntegerFixedFromReader(r, 2)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(3 + x)
return readRemaining(append(hdr, bytes...), l)
case 0xfa, 0xfb, 0xfc:
x, bytes, err := readIntegerFixedFromReader(r, 4)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(5 + x)
return readRemaining(append(hdr, bytes...), l)
case 0xfd, 0xfe, 0xff:
x, bytes, err := readIntegerFixedFromReader(r, 8)
if err != nil {
return nil, WithStack(err)
}
l := ValueLength(9 + x)
return readRemaining(append(hdr, bytes...), l)
}
}
return nil, WithStack(InternalError)
}
// sliceFromBufReader reads a slice from the given buffered reader.
func sliceFromBufReader(r *bufio.Reader) (Slice, error) {
// ByteSize is always found within first 16 bytes
hdr, err := r.Peek(maxByteSizeBytes)
if len(hdr) == 0 && err != nil {
if Cause(err) == io.EOF {
// Empty slice
return nil, nil
}
return nil, WithStack(err)
}
s := Slice(hdr)
size, err := s.ByteSize()
if err != nil {
return nil, WithStack(err)
}
// Now that we know the size, read the entire slice
buf := make(Slice, size)
offset := 0
bytesRead := 0
for ValueLength(bytesRead) < size {
n, err := r.Read(buf[offset:])
bytesRead += n
offset += n
if err != nil && ValueLength(bytesRead) < size {
return nil, WithStack(err)
}
}
return buf, nil
}