forked from apache/dubbo-go-hessian2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
207 lines (169 loc) · 5.01 KB
/
decode.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
// Copyright 2016-2019 Alex Stocks
//
// 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 hessian
import (
"bufio"
"bytes"
"io"
"reflect"
)
import (
perrors "github.com/pkg/errors"
)
// Decoder struct
type Decoder struct {
reader *bufio.Reader
refs []interface{}
classInfoList []classInfo
}
// Error part
var (
ErrNotEnoughBuf = perrors.Errorf("not enough buf")
ErrIllegalRefIndex = perrors.Errorf("illegal ref index")
)
// NewDecoder generate a decoder instance
func NewDecoder(b []byte) *Decoder {
return &Decoder{reader: bufio.NewReader(bytes.NewReader(b))}
}
/////////////////////////////////////////
// utilities
/////////////////////////////////////////
// peek a byte
func (d *Decoder) peekByte() byte {
return d.peek(1)[0]
}
// get the buffer length
func (d *Decoder) len() int {
d.peek(1) // peek one byte to get the buffer length
return d.reader.Buffered()
}
// read a byte from Decoder, advance the ptr
func (d *Decoder) readByte() (byte, error) {
return d.reader.ReadByte()
}
// unread a byte
func (d *Decoder) unreadByte() error {
return d.reader.UnreadByte()
}
// read byte arr, and return the length of b
func (d *Decoder) next(b []byte) (int, error) {
return d.reader.Read(b)
}
// peek n bytes, will not advance the read ptr
func (d *Decoder) peek(n int) []byte {
b, _ := d.reader.Peek(n)
return b
}
// read utf8 len(s) of array
func (d *Decoder) nextRune(s []rune) []rune {
var (
n int
i int
r rune
ri int
err error
)
n = len(s)
s = s[:0]
for i = 0; i < n; i++ {
if r, ri, err = d.reader.ReadRune(); err == nil && ri > 0 {
s = append(s, r)
}
}
return s
}
// read the type of data, used to decode list or map
func (d *Decoder) decType() (string, error) {
var (
err error
arr [1]byte
buf []byte
tag byte
idx int32
typ reflect.Type
)
buf = arr[:1]
if _, err = io.ReadFull(d.reader, buf); err != nil {
return "", perrors.WithStack(err)
}
tag = buf[0]
if (tag >= BC_STRING_DIRECT && tag <= STRING_DIRECT_MAX) ||
(tag >= 0x30 && tag <= 0x33) || (tag == BC_STRING) || (tag == BC_STRING_CHUNK) {
return d.decString(int32(tag))
}
if idx, err = d.decInt32(TAG_READ); err != nil {
return "", perrors.WithStack(err)
}
typ, _, err = d.getStructDefByIndex(int(idx))
if err == nil {
return typ.String(), nil
}
return "", err
}
// Decode parses hessian data
func (d *Decoder) Decode() (interface{}, error) {
var (
err error
tag byte
)
tag, err = d.readByte()
if err == io.EOF {
return nil, err
}
switch {
case tag == BC_END:
// return EOF error for end flag 'Z'
return nil, io.EOF
case tag == BC_NULL: // 'N': //null
return nil, nil
case tag == BC_TRUE: // 'T': //true
return true, nil
case tag == BC_FALSE: //'F': //false
return false, nil
case tag == BC_REF: // 'R': //ref, a int which represents the previous list or map
return d.decRef(int32(tag))
case (0x80 <= tag && tag <= 0xbf) || (0xc0 <= tag && tag <= 0xcf) ||
(0xd0 <= tag && tag <= 0xd7) || tag == BC_INT: //'I': //int
return d.decInt32(int32(tag))
case (tag >= 0xd8 && tag <= 0xef) || (tag >= 0xf0 && tag <= 0xff) ||
(tag >= 0x38 && tag <= 0x3f) || (tag == BC_LONG_INT) || (tag == BC_LONG): //'L': //long
return d.decInt64(int32(tag))
case (tag == BC_DATE_MINUTE) || (tag == BC_DATE): //'d': //date
return d.decDate(int32(tag))
case (tag == BC_DOUBLE_ZERO) || (tag == BC_DOUBLE_ONE) || (tag == BC_DOUBLE_BYTE) ||
(tag == BC_DOUBLE_SHORT) || (tag == BC_DOUBLE_MILL) || (tag == BC_DOUBLE): //'D': //double
return d.decDouble(int32(tag))
// case 'S', 's', 'X', 'x': //string,xml
case (tag == BC_STRING_CHUNK || tag == BC_STRING) ||
(tag >= BC_STRING_DIRECT && tag <= STRING_DIRECT_MAX) ||
(tag >= 0x30 && tag <= 0x33):
return d.decString(int32(tag))
// case 'B', 'b': //binary
case (tag == BC_BINARY) || (tag == BC_BINARY_CHUNK) || (tag >= 0x20 && tag <= 0x2f) ||
(tag >= BC_BINARY_SHORT && tag <= 0x3f):
return d.decBinary(int32(tag))
// case 'V': //list
case (tag >= BC_LIST_DIRECT && tag <= 0x77) || (tag == BC_LIST_FIXED || tag == BC_LIST_VARIABLE) ||
(tag >= BC_LIST_DIRECT_UNTYPED && tag <= 0x7f) ||
(tag == BC_LIST_FIXED_UNTYPED || tag == BC_LIST_VARIABLE_UNTYPED):
return d.decList(int32(tag))
case (tag == BC_MAP) || (tag == BC_MAP_UNTYPED):
return d.decMap(int32(tag))
case (tag == BC_OBJECT_DEF) || (tag == BC_OBJECT) ||
(BC_OBJECT_DIRECT <= tag && tag <= (BC_OBJECT_DIRECT+OBJECT_DIRECT_MAX)):
return d.decObject(int32(tag))
default:
return nil, perrors.Errorf("Invalid type: %v,>>%v<<<", string(tag), d.peek(d.len()))
}
}