-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbvj.go
257 lines (205 loc) · 4.03 KB
/
sbvj.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
package sbvj
import (
"bufio"
"bytes"
"encoding/binary"
"io"
"os"
)
type SBVJType byte
const (
NIL SBVJType = 0x01
DOUBLE SBVJType = 0x02
BOOLEAN SBVJType = 0x03
VARINT SBVJType = 0x04
STRING SBVJType = 0x05
LIST SBVJType = 0x06
MAP SBVJType = 0x07
)
type SBVJ struct {
Name string
Versioned bool
Version int32
Content SBVJObject
}
func ReadBytes(buf []byte) (*SBVJ, error) {
buffer := bytes.NewBuffer(buf)
return Read(buffer)
}
func ReadFile(path string) (*SBVJ, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return Read(file)
}
func Read(r io.Reader) (*SBVJ, error) {
reader := bufio.NewReader(r)
magic := make([]byte, 6)
if _, err := io.ReadFull(reader, magic); err != nil {
return nil, err
}
if string(magic) != "SBVJ01" {
return nil, &ErrMagic{magic}
}
sbvj := SBVJ{}
name, err := readString(reader)
if err != nil {
return nil, err
}
sbvj.Name = name
versioned, err := readBoolean(reader)
if err != nil {
return nil, err
}
sbvj.Versioned = versioned
if versioned {
if err := binary.Read(reader, binary.BigEndian, &sbvj.Version); err != nil {
return nil, err
}
}
object, err := readObject(reader)
if err != nil {
return nil, err
}
sbvj.Content = object
return &sbvj, nil
}
func readByte(r *bufio.Reader) (byte, error) {
b, err := r.ReadByte()
if err != nil {
return 0, err
}
return b, nil
}
func readBytes(r *bufio.Reader) ([]byte, error) {
size, err := readVarint(r)
if err != nil {
return nil, err
}
bytes := make([]byte, size)
if _, err = io.ReadFull(r, bytes); err != nil {
return nil, err
}
return bytes, nil
}
func readString(r *bufio.Reader) (string, error) {
bytes, err := readBytes(r)
if err != nil {
return "", err
}
return string(bytes), nil
}
func readVarint(r *bufio.Reader) (int64, error) {
var value int64
for {
b, err := r.ReadByte()
if err != nil {
return 0, err
}
if b&0b10000000 == 0 {
value = value<<7 | int64(b)
return value, nil
}
value = value<<7 | (int64(b) & 0b01111111)
}
}
func readUVarint(r *bufio.Reader) (int64, error) {
v, err := readVarint(r)
if err != nil {
return 0, err
}
if v&1 != 0 {
return -(v >> 1) - 1, nil
}
return v >> 1, nil
}
func readDouble(r *bufio.Reader) (float64, error) {
var val float64
if err := binary.Read(r, binary.LittleEndian, &val); err != nil {
return 0, nil
}
return val, nil
}
func readBoolean(r *bufio.Reader) (bool, error) {
b, err := readByte(r)
if err != nil {
return false, nil
}
if b == 0 {
return false, nil
} else {
return true, nil
}
}
func readObject(r *bufio.Reader) (SBVJObject, error) {
object := SBVJObject{}
tp, err := readByte(r)
if err != nil {
return SBVJObject{}, err
}
object.Type = SBVJType(tp)
var value any
switch object.Type {
case NIL:
object.Value = nil
case DOUBLE:
value, err = readDouble(r)
case BOOLEAN:
value, err = readBoolean(r)
case VARINT:
value, err = readUVarint(r)
case STRING:
value, err = readString(r)
case LIST:
value, err = readList(r)
case MAP:
value, err = readMap(r)
default:
return SBVJObject{}, &ErrObjectType{object.Type}
}
if err != nil {
return SBVJObject{}, err
}
object.Value = value
return object, nil
}
func readList(r *bufio.Reader) (SBVJList, error) {
sbvjList := SBVJList{}
size, err := readVarint(r)
if err != nil {
return SBVJList{}, err
}
sbvjList.Items = make([]SBVJObject, size)
var i int64
for i = 0; i < size; i++ {
token, err := readObject(r)
if err != nil {
return SBVJList{}, err
}
sbvjList.Items[i] = token
}
return sbvjList, nil
}
func readMap(r *bufio.Reader) (SBVJMap, error) {
sbvjmap := SBVJMap{}
size, err := readVarint(r)
if err != nil {
return SBVJMap{}, err
}
sbvjmap.Items = make([]SBVJPair, size)
var i int64
for i = 0; i < size; i++ {
key, err := readString(r)
if err != nil {
return SBVJMap{}, err
}
value, err := readObject(r)
if err != nil {
return SBVJMap{}, err
}
sbvjmap.Items[i] = SBVJPair{key, value}
}
return sbvjmap, nil
}