-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpack.go
198 lines (169 loc) · 4.28 KB
/
unpack.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
// Copyright 2017~2022 The Bottos Authors
// This file is part of the Bottos Chain library.
// Created by Rocket Core Team of Bottos.
//This program is free software: you can distribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
// along with bottos. If not, see <http://www.gnu.org/licenses/>.
/*
* file description: msgpack go
* @Author: Gong Zibin
* @Date: 2017-12-20
* @Last Modified by:
* @Last Modified time:
*/
package msgpack
import (
"fmt"
"io"
)
type (
//Bytes1 is first
Bytes1 [1]byte
//Bytes2 is second
Bytes2 [2]byte
//Bytes4 is third
Bytes4 [4]byte
//Bytes8 is forth
Bytes8 [8]byte
)
const (
//NEGFIXNUM is negfix maxnum
NEGFIXNUM = 0xe0
//FIXMAPMAX is fixmap maxnum
FIXMAPMAX = 0x8f
//FIXARRAYMAX is fixarray maxnum
FIXARRAYMAX = 0x9f
//FIXRAWMAX is fix raw max
FIXRAWMAX = 0xbf
//FIRSTBYTEMASK is first byte mask
FIRSTBYTEMASK = 0xf
)
func readByte(reader io.Reader) (v uint8, err error) {
var data Bytes1
_, e := reader.Read(data[0:])
if e != nil {
return 0, e
}
return data[0], nil
}
//UnpackUint8 is to unpack message
func UnpackUint8(reader io.Reader) (v uint8, err error) {
c, e := readByte(reader)
if e == nil && c == UINT8 {
v, err = readByte(reader)
if err == nil {
return v, nil
}
}
return 0, err
}
func readUint16(reader io.Reader) (v uint16, n int, err error) {
var data Bytes2
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint16(data[0]) << 8) | uint16(data[1]), n, nil
}
//UnpackUint16 is to unpack message
func UnpackUint16(reader io.Reader) (v uint16, err error) {
c, e := readByte(reader)
if e == nil && c == UINT16 {
v, _, err = readUint16(reader)
if err == nil {
return v, nil
}
}
return 0, err
}
func readUint32(reader io.Reader) (v uint32, n int, err error) {
var data Bytes4
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint32(data[0]) << 24) | (uint32(data[1]) << 16) | (uint32(data[2]) << 8) | uint32(data[3]), n, nil
}
//UnpackUint32 is to unpack message
func UnpackUint32(reader io.Reader) (v uint32, err error) {
c, e := readByte(reader)
if e == nil && c == UINT32 {
v, _, err = readUint32(reader)
if err == nil {
return v, nil
}
}
return 0, err
}
func readUint64(reader io.Reader) (v uint64, n int, err error) {
var data Bytes8
n, e := reader.Read(data[0:])
if e != nil {
return 0, n, e
}
return (uint64(data[0]) << 56) | (uint64(data[1]) << 48) | (uint64(data[2]) << 40) | (uint64(data[3]) << 32) | (uint64(data[4]) << 24) | (uint64(data[5]) << 16) | (uint64(data[6]) << 8) | uint64(data[7]), n, nil
}
//UnpackUint64 is to unpack message
func UnpackUint64(reader io.Reader) (v uint64, err error) {
c, e := readByte(reader)
if e == nil && c == UINT64 {
v, _, err = readUint64(reader)
if err == nil {
return v, nil
}
}
return 0, err
}
//UnpackArraySize is to unpack message
func UnpackArraySize(reader io.Reader) (size uint16, err error) {
c, e := readByte(reader)
if e != nil {
return 0, e
}
header := uint16(c)
if header != ARRAY16 {
return 0, fmt.Errorf("Not Array 16")
}
size, _, e = readUint16(reader)
if e != nil {
return 0, e
}
return size, nil
}
//UnpackStr16 is to unpack message
func UnpackStr16(reader io.Reader) (string, error) {
c, e := readByte(reader)
if e == nil && c == STR16 {
size, _, e := readUint16(reader)
if e == nil {
value := make([]byte, size)
n, e := reader.Read(value)
if e == nil && uint16(n) == size {
return string(value), nil
}
}
}
return "", e
}
//UnpackBin16 is to unpack message
func UnpackBin16(reader io.Reader) ([]byte, error) {
c, e := readByte(reader)
if e == nil && c == BIN16 {
size, _, e := readUint16(reader)
if e == nil {
value := make([]byte, size)
n, e := reader.Read(value)
if e == nil && uint16(n) == size {
return value, nil
}
}
}
return []byte{}, e
}