forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpacker.go
271 lines (221 loc) · 6.04 KB
/
unpacker.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2013-2016 Aerospike, Inc.
//
// 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.
package aerospike
import (
"fmt"
. "github.com/aerospike/aerospike-client-go/types"
ParticleType "github.com/aerospike/aerospike-client-go/types/particle_type"
Buffer "github.com/aerospike/aerospike-client-go/utils/buffer"
)
type unpacker struct {
buffer []byte
offset int
length int
}
func newUnpacker(buffer []byte, offset int, length int) *unpacker {
return &unpacker{
buffer: buffer,
offset: offset,
length: length,
}
}
func (upckr *unpacker) UnpackList() ([]interface{}, error) {
if upckr.length <= 0 {
return nil, nil
}
theType := upckr.buffer[upckr.offset] & 0xff
upckr.offset++
var count int
if (theType & 0xf0) == 0x90 {
count = int(theType & 0x0f)
} else if theType == 0xdc {
count = int(Buffer.BytesToUint16(upckr.buffer, upckr.offset))
upckr.offset += 2
} else if theType == 0xdd {
count = int(Buffer.BytesToUint32(upckr.buffer, upckr.offset))
upckr.offset += 4
} else {
return nil, nil
}
return upckr.unpackList(count)
}
func (upckr *unpacker) unpackList(count int) ([]interface{}, error) {
out := make([]interface{}, 0, count)
for i := 0; i < count; i++ {
obj, err := upckr.unpackObject()
if err != nil {
return nil, err
}
out = append(out, obj)
}
return out, nil
}
func (upckr *unpacker) UnpackMap() (map[interface{}]interface{}, error) {
if upckr.length <= 0 {
return nil, nil
}
theType := upckr.buffer[upckr.offset] & 0xff
upckr.offset++
var count int
if (theType & 0xf0) == 0x80 {
count = int(theType & 0x0f)
} else if theType == 0xde {
count = int(Buffer.BytesToUint16(upckr.buffer, upckr.offset))
upckr.offset += 2
} else if theType == 0xdf {
count = int(Buffer.BytesToUint32(upckr.buffer, upckr.offset))
upckr.offset += 4
} else {
return make(map[interface{}]interface{}), nil
}
return upckr.unpackMap(count)
}
func (upckr *unpacker) unpackMap(count int) (map[interface{}]interface{}, error) {
out := make(map[interface{}]interface{}, count)
for i := 0; i < count; i++ {
key, err := upckr.unpackObject()
if err != nil {
return nil, err
}
val, err := upckr.unpackObject()
if err != nil {
return nil, err
}
out[key] = val
}
return out, nil
}
func (upckr *unpacker) unpackObjects() (interface{}, error) {
if upckr.length <= 0 {
return nil, nil
}
return upckr.unpackObject()
}
func (upckr *unpacker) unpackBlob(count int) (interface{}, error) {
theType := upckr.buffer[upckr.offset] & 0xff
upckr.offset++
count--
var val interface{}
switch theType {
case ParticleType.STRING:
val = string(upckr.buffer[upckr.offset : upckr.offset+count])
case ParticleType.BLOB:
b := make([]byte, count)
copy(b, upckr.buffer[upckr.offset:upckr.offset+count])
val = b
default:
panic(NewAerospikeError(SERIALIZE_ERROR, fmt.Sprintf("Error while unpacking BLOB. Type-header with code `%d` not recognized.", theType)))
}
upckr.offset += count
return val, nil
}
func (upckr *unpacker) unpackObject() (interface{}, error) {
theType := upckr.buffer[upckr.offset] & 0xff
upckr.offset++
switch theType {
case 0xc0:
return nil, nil
case 0xc3:
return true, nil
case 0xc2:
return false, nil
case 0xca:
val := Buffer.BytesToFloat32(upckr.buffer, upckr.offset)
upckr.offset += 4
return val, nil
case 0xcb:
val := Buffer.BytesToFloat64(upckr.buffer, upckr.offset)
upckr.offset += 8
return val, nil
case 0xcc:
r := upckr.buffer[upckr.offset] & 0xff
upckr.offset++
return int(r), nil
case 0xcd:
val := uint16(Buffer.BytesToInt16(upckr.buffer, upckr.offset))
upckr.offset += 2
return int(val), nil
case 0xce:
val := uint32(Buffer.BytesToInt32(upckr.buffer, upckr.offset))
upckr.offset += 4
if Buffer.Arch64Bits {
return int(val), nil
}
return int64(val), nil
case 0xcf:
val := Buffer.BytesToInt64(upckr.buffer, upckr.offset)
upckr.offset += 8
return uint64(val), nil
case 0xd0:
r := int8(upckr.buffer[upckr.offset])
upckr.offset++
return int(r), nil
case 0xd1:
val := Buffer.BytesToInt16(upckr.buffer, upckr.offset)
upckr.offset += 2
return int(val), nil
case 0xd2:
val := Buffer.BytesToInt32(upckr.buffer, upckr.offset)
upckr.offset += 4
return int(val), nil
case 0xd3:
val := Buffer.BytesToInt64(upckr.buffer, upckr.offset)
upckr.offset += 8
if Buffer.Arch64Bits {
return int(val), nil
}
return int64(val), nil
case 0xda:
count := int(Buffer.BytesToUint16(upckr.buffer, upckr.offset))
upckr.offset += 2
return upckr.unpackBlob(count)
case 0xdb:
count := int(Buffer.BytesToUint32(upckr.buffer, upckr.offset))
upckr.offset += 4
return upckr.unpackBlob(count)
case 0xdc:
count := int(Buffer.BytesToUint16(upckr.buffer, upckr.offset))
upckr.offset += 2
return upckr.unpackList(count)
case 0xdd:
count := int(Buffer.BytesToUint32(upckr.buffer, upckr.offset))
upckr.offset += 4
return upckr.unpackList(count)
case 0xde:
count := int(Buffer.BytesToUint16(upckr.buffer, upckr.offset))
upckr.offset += 2
return upckr.unpackMap(count)
case 0xdf:
count := int(Buffer.BytesToUint32(upckr.buffer, upckr.offset))
upckr.offset += 4
return upckr.unpackMap(count)
default:
if (theType & 0xe0) == 0xa0 {
return upckr.unpackBlob(int(theType & 0x1f))
}
if (theType & 0xf0) == 0x80 {
return upckr.unpackMap(int(theType & 0x0f))
}
if (theType & 0xf0) == 0x90 {
return upckr.unpackList(int(theType & 0x0f))
}
if theType < 0x80 {
return int(theType), nil
}
if theType >= 0xe0 {
return int(int(theType) - 0xe0 - 32), nil
}
}
return nil, NewAerospikeError(SERIALIZE_ERROR)
}