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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// Package nbt provides functions for reading and writing NBT encoded data.
package nbt
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"github.com/kch42/kagus"
"io"
"strconv"
)
// Tag holds the data of an NBT tag. Type is a TAG_* value.
//
// The type of Payload depends on Type:
//
// TAG_Byte -- byte
// TAG_Short -- int16
// TAG_Int -- int32
// TAG_Long -- int64
// TAG_Float -- float32
// TAG_Double -- float64
// TAG_Byte_Array -- []byte
// TAG_String -- string
// TAG_List -- TagList
// TAG_Compound -- TagCompound
// TAG_Int_Array -- []int32
type Tag struct {
Type TagType
Payload interface{}
}
// TagList is the payload of a TAG_List Tag. The type of the Elems depends on Type (see docu of Tag).
type TagList struct {
Type TagType
Elems []interface{}
}
func (t Tag) String() string {
s := t.Type.String()
switch t.Type {
case TAG_Byte:
s += fmt.Sprintf(": 0x%02x", t.Payload.(byte))
case TAG_Short:
s += fmt.Sprintf(": %d", t.Payload.(int16))
case TAG_Int:
s += fmt.Sprintf(": %d", t.Payload.(int32))
case TAG_Long:
s += fmt.Sprintf(": %d", t.Payload.(int64))
case TAG_Float:
s += fmt.Sprintf(": %f", t.Payload.(float32))
case TAG_Double:
s += fmt.Sprintf(": %f", t.Payload.(float64))
case TAG_Byte_Array:
s += "\n" + kagus.Indent(hex.Dump(t.Payload.([]byte)), " ")
case TAG_String:
s += ": " + strconv.Quote(t.Payload.(string))
case TAG_List:
l := t.Payload.(TagList)
s += " of " + l.Type.String() + ":"
for _, elem := range l.Elems {
s += "\n" + kagus.Indent(Tag{l.Type, elem}.String(), " ")
}
case TAG_Compound:
s += ":"
comp := t.Payload.(TagCompound)
for k, v := range comp {
s += "\n" + kagus.Indent(strconv.Quote(k)+" ->"+kagus.Indent(v.String(), " "), " ")
}
return s
case TAG_Int_Array:
l := t.Payload.([]int32)
s += ": "
sep := ""
for _, elem := range l {
s += sep + fmt.Sprintf("%d", elem)
sep = ", "
}
}
return s
}
// TagCompund is the payload of a TAG_Compound. Initialize with make.
type TagCompound map[string]Tag
func readTagData(r io.Reader, tt TagType) (interface{}, error) {
switch tt {
case TAG_End:
case TAG_Byte:
var v uint8
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Short:
var v int16
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Int:
var v int32
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Long:
var v int64
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Float:
var v float32
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Double:
var v float64
err := binary.Read(r, binary.BigEndian, &v)
return v, err
case TAG_Byte_Array:
var l int32
if err := binary.Read(r, binary.BigEndian, &l); err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("Byte array has negative length?")
}
data := make([]byte, l)
_, err := io.ReadFull(r, data)
return data, err
case TAG_String:
var l int16
if err := binary.Read(r, binary.BigEndian, &l); err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("String has negative length?")
}
data := make([]byte, l)
_, err := io.ReadFull(r, data)
return string(data), err
case TAG_List:
_ltt, err := kagus.ReadByte(r)
if err != nil {
return nil, err
}
ltt := TagType(_ltt)
var l int32
if err := binary.Read(r, binary.BigEndian, &l); err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("List has negative length?")
}
tl := TagList{Type: ltt, Elems: make([]interface{}, l)}
for i := 0; i < int(l); i++ {
if tl.Elems[i], err = readTagData(r, ltt); err != nil {
return nil, err
}
}
return tl, nil
case TAG_Compound:
comp := make(TagCompound)
for {
tag, name, err := ReadNamedTag(r)
if err != nil {
return nil, err
}
if tag.Type == TAG_End {
break
}
comp[name] = tag
}
return comp, nil
case TAG_Int_Array:
var l int32
if err := binary.Read(r, binary.BigEndian, &l); err != nil {
return nil, err
}
if l < 0 {
return nil, errors.New("Int Array has negative length?")
}
data := make([]int32, l)
for i := 0; i < int(l); i++ {
var e int32
if err := binary.Read(r, binary.BigEndian, &e); err != nil {
return nil, err
}
data[i] = e
}
return data, nil
}
return nil, errors.New("Unknown tag type")
}
// ReadNamedTag reads a named Tag from an io.Reader. It returns the Tag, the tags Name and an error.
func ReadNamedTag(r io.Reader) (Tag, string, error) {
_tt, err := kagus.ReadByte(r)
if err != nil {
return Tag{}, "", err
}
tt := TagType(_tt)
if tt == TAG_End {
return Tag{Type: tt}, "", nil
}
name, err := readTagData(r, TAG_String)
if err != nil {
return Tag{}, "", err
}
td, err := readTagData(r, tt)
return Tag{Type: tt, Payload: td}, name.(string), err
}
func writeByte(w io.Writer, b byte) error {
_, err := w.Write([]byte{b})
return err
}
func writeTagData(w io.Writer, tt TagType, data interface{}) error {
switch tt {
case TAG_End:
return nil
case TAG_Byte:
return writeByte(w, data.(byte))
case TAG_Short:
return binary.Write(w, binary.BigEndian, data.(int16))
case TAG_Int:
return binary.Write(w, binary.BigEndian, data.(int32))
case TAG_Long:
return binary.Write(w, binary.BigEndian, data.(int64))
case TAG_Float:
return binary.Write(w, binary.BigEndian, data.(float32))
case TAG_Double:
return binary.Write(w, binary.BigEndian, data.(float64))
case TAG_Byte_Array:
slice := data.([]byte)
if err := binary.Write(w, binary.BigEndian, int32(len(slice))); err != nil {
return err
}
_, err := w.Write(slice)
return err
case TAG_String:
strEnc := []byte(data.(string))
if err := binary.Write(w, binary.BigEndian, int16(len(strEnc))); err != nil {
return err
}
_, err := w.Write(strEnc)
return err
case TAG_List:
list := data.(TagList)
if err := writeByte(w, byte(list.Type)); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, int32(len(list.Elems))); err != nil {
return err
}
for _, el := range list.Elems {
if err := writeTagData(w, list.Type, el); err != nil {
return err
}
}
return nil
case TAG_Compound:
comp := data.(TagCompound)
for name, tag := range comp {
if err := WriteNamedTag(w, name, tag); err != nil {
return err
}
}
return writeByte(w, TAG_End)
case TAG_Int_Array:
slice := data.([]int32)
if err := binary.Write(w, binary.BigEndian, int32(len(slice))); err != nil {
return err
}
for _, el := range slice {
if err := binary.Write(w, binary.BigEndian, el); err != nil {
return err
}
}
return nil
}
return errors.New("Unknown tage type")
}
// WriteNamedTag writes a named Tag to an io.Writer.
func WriteNamedTag(w io.Writer, name string, tag Tag) error {
if err := writeByte(w, byte(tag.Type)); err != nil {
return err
}
if err := writeTagData(w, TAG_String, name); err != nil {
return err
}
return writeTagData(w, tag.Type, tag.Payload)
}
|