-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_reader.go
More file actions
516 lines (471 loc) · 11.9 KB
/
stream_reader.go
File metadata and controls
516 lines (471 loc) · 11.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package nbt
import (
"fmt"
"io"
)
// StreamReader provides a streaming interface for reading NBT data without fully
// materializing the tree into memory. It iterates over tags token-by-token,
// allowing callers to read specific fields or skip entire subtrees.
//
// Basic usage:
//
// reader := nbt.NewStreamReader(r)
// for reader.Next() {
// if reader.TagName() == "BlockData" && reader.TagType() == nbt.TagByteArray {
// data, _ := reader.ByteArray()
// // process data...
// } else {
// reader.Skip()
// }
// }
// if err := reader.Err(); err != nil {
// // handle error
// }
type StreamReader struct {
r *offsetReader
encoding Encoding
err error
// Current tag state
tagType tagType
tagName string
depth int
// Stack for tracking compound/list nesting
stack []stackEntry
}
type stackEntry struct {
isCompound bool
listType tagType
remaining int64
}
// NewScanner creates a new streaming NBT scanner with NetworkLittleEndian encoding.
func NewStreamReader(r io.Reader) *StreamReader {
return NewStreamReaderWithEncoding(r, NetworkLittleEndian)
}
// NewStreamReaderWithEncoding creates a new streaming NBT scanner with a specific encoding.
func NewStreamReaderWithEncoding(r io.Reader, encoding Encoding) *StreamReader {
return &StreamReader{
r: newOffsetReader(r),
encoding: encoding,
stack: make([]stackEntry, 0, 16),
}
}
// Next advances the scanner to the next tag.
// Returns true if a tag was found, false if EOF or error.
// After Next returns false, call Err() to check for errors.
func (s *StreamReader) Next() bool {
if s.err != nil {
return false
}
// Handle list item iteration
if len(s.stack) > 0 {
top := &s.stack[len(s.stack)-1]
if !top.isCompound {
// Inside a list
if top.remaining > 0 {
top.remaining--
s.tagType = top.listType
s.tagName = ""
return true
}
// List exhausted, pop and continue
s.stack = s.stack[:len(s.stack)-1]
}
}
// Read next tag
tagTypeByte, err := s.r.ReadByte()
if err != nil {
if err == io.EOF {
s.err = nil // Normal EOF
} else {
s.err = err
}
return false
}
s.tagType = tagType(tagTypeByte)
// TAG_End exits current compound
if s.tagType == tagEnd {
if len(s.stack) > 0 && s.stack[len(s.stack)-1].isCompound {
s.stack = s.stack[:len(s.stack)-1]
}
// Return false to signal this compound has ended
// The caller's for loop will exit, and the outer caller will call Next() again
return false
}
// Read tag name
s.tagName, s.err = s.encoding.String(s.r)
if s.err != nil {
return false
}
return true
}
// Err returns any error encountered during scanning.
func (s *StreamReader) Err() error {
return s.err
}
// TagType returns the type of the current tag.
func (s *StreamReader) TagType() tagType {
return s.tagType
}
// TagName returns the name of the current tag.
func (s *StreamReader) TagName() string {
return s.tagName
}
// Depth returns the current nesting depth.
func (s *StreamReader) Depth() int {
return len(s.stack)
}
// Skip skips the current tag's value without reading it.
// For compounds and lists, this skips the entire subtree.
func (s *StreamReader) Skip() error {
if s.err != nil {
return s.err
}
s.err = s.skipValue(s.tagType)
return s.err
}
// Byte reads a TAG_Byte value.
func (s *StreamReader) Byte() (byte, error) {
if s.tagType != tagByte {
return 0, fmt.Errorf("expected TAG_Byte, got %v", s.tagType)
}
return s.r.ReadByte()
}
// Int16 reads a TAG_Short value.
func (s *StreamReader) Int16() (int16, error) {
if s.tagType != tagInt16 {
return 0, fmt.Errorf("expected TAG_Short, got %v", s.tagType)
}
return s.encoding.Int16(s.r)
}
// Int32 reads a TAG_Int value.
func (s *StreamReader) Int32() (int32, error) {
if s.tagType != tagInt32 {
return 0, fmt.Errorf("expected TAG_Int, got %v", s.tagType)
}
return s.encoding.Int32(s.r)
}
// Int64 reads a TAG_Long value.
func (s *StreamReader) Int64() (int64, error) {
if s.tagType != tagInt64 {
return 0, fmt.Errorf("expected TAG_Long, got %v", s.tagType)
}
return s.encoding.Int64(s.r)
}
// Float32 reads a TAG_Float value.
func (s *StreamReader) Float32() (float32, error) {
if s.tagType != tagFloat32 {
return 0, fmt.Errorf("expected TAG_Float, got %v", s.tagType)
}
return s.encoding.Float32(s.r)
}
// Float64 reads a TAG_Double value.
func (s *StreamReader) Float64() (float64, error) {
if s.tagType != tagFloat64 {
return 0, fmt.Errorf("expected TAG_Double, got %v", s.tagType)
}
return s.encoding.Float64(s.r)
}
// String reads a TAG_String value.
func (s *StreamReader) String() (string, error) {
if s.tagType != tagString {
return "", fmt.Errorf("expected TAG_String, got %v", s.tagType)
}
return s.encoding.String(s.r)
}
// ByteArray reads a TAG_ByteArray value.
func (s *StreamReader) ByteArray() ([]byte, error) {
if s.tagType != tagByteArray {
return nil, fmt.Errorf("expected TAG_ByteArray, got %v", s.tagType)
}
length, err := s.encoding.Int32(s.r)
if err != nil {
return nil, err
}
if length < 0 {
return nil, BufferOverrunError{Op: "ByteArray"}
}
data := make([]byte, length)
if _, err := io.ReadFull(s.r, data); err != nil {
return nil, BufferOverrunError{Op: "ByteArray"}
}
return data, nil
}
// Int32Array reads a TAG_IntArray value.
func (s *StreamReader) Int32Array() ([]int32, error) {
if s.tagType != tagInt32Array {
return nil, fmt.Errorf("expected TAG_IntArray, got %v", s.tagType)
}
return s.encoding.Int32Slice(s.r)
}
// Int64Array reads a TAG_LongArray value.
func (s *StreamReader) Int64Array() ([]int64, error) {
if s.tagType != tagInt64Array {
return nil, fmt.Errorf("expected TAG_LongArray, got %v", s.tagType)
}
return s.encoding.Int64Slice(s.r)
}
// EnterCompound enters a TAG_Compound to iterate its children.
// After calling this, use Next() to iterate through child tags.
// The compound is automatically exited when TAG_End is encountered.
func (s *StreamReader) EnterCompound() error {
if s.tagType != tagStruct {
return fmt.Errorf("expected TAG_Compound, got %v", s.tagType)
}
s.stack = append(s.stack, stackEntry{isCompound: true})
return nil
}
// ListInfo reads the type and length of a TAG_List without entering it.
// Use EnterList() to actually iterate the list items.
func (s *StreamReader) ListInfo() (itemType tagType, length int32, err error) {
if s.tagType != tagSlice {
return 0, 0, fmt.Errorf("expected TAG_List, got %v", s.tagType)
}
typeByte, err := s.r.ReadByte()
if err != nil {
return 0, 0, err
}
length, err = s.encoding.Int32(s.r)
if err != nil {
return 0, 0, err
}
return tagType(typeByte), length, nil
}
// EnterList reads list metadata and enters a TAG_List to iterate its items.
// Returns the item type and count. After calling this, use Next() to get each item.
func (s *StreamReader) EnterList() (itemType tagType, count int32, err error) {
itemType, count, err = s.ListInfo()
if err != nil {
return 0, 0, err
}
if count > 0 {
s.stack = append(s.stack, stackEntry{
isCompound: false,
listType: itemType,
remaining: int64(count),
})
}
return itemType, count, nil
}
// Value reads and returns the value of the current primitive tag.
// For compounds/lists, use EnterCompound/EnterList instead.
func (s *StreamReader) Value() (any, error) {
switch s.tagType {
case tagByte:
return s.Byte()
case tagInt16:
return s.Int16()
case tagInt32:
return s.Int32()
case tagInt64:
return s.Int64()
case tagFloat32:
return s.Float32()
case tagFloat64:
return s.Float64()
case tagString:
return s.String()
case tagByteArray:
return s.ByteArray()
case tagInt32Array:
return s.Int32Array()
case tagInt64Array:
return s.Int64Array()
default:
return nil, fmt.Errorf("cannot read value of %v", s.tagType)
}
}
// ReadCompound reads an entire compound tag into a map.
// This is a convenience method for when you need the whole compound in memory.
func (s *StreamReader) ReadCompound() (map[string]any, error) {
if s.tagType != tagStruct {
return nil, fmt.Errorf("expected TAG_Compound, got %v", s.tagType)
}
result := make(map[string]any)
if err := s.EnterCompound(); err != nil {
return nil, err
}
for s.Next() {
if s.Depth() == 0 {
break // Exited the compound
}
name := s.tagName
switch s.tagType {
case tagStruct:
val, err := s.ReadCompound()
if err != nil {
return nil, err
}
result[name] = val
case tagSlice:
val, err := s.ReadList()
if err != nil {
return nil, err
}
result[name] = val
default:
val, err := s.Value()
if err != nil {
return nil, err
}
result[name] = val
}
}
return result, s.err
}
// ReadList reads an entire list tag into a slice.
// This is a convenience method for when you need the whole list in memory.
func (s *StreamReader) ReadList() ([]any, error) {
if s.tagType != tagSlice {
return nil, fmt.Errorf("expected TAG_List, got %v", s.tagType)
}
_, count, err := s.EnterList()
if err != nil {
return nil, err
}
result := make([]any, 0, count)
for i := int32(0); i < count && s.Next(); i++ {
switch s.tagType {
case tagStruct:
val, err := s.ReadCompound()
if err != nil {
return nil, err
}
result = append(result, val)
case tagSlice:
val, err := s.ReadList()
if err != nil {
return nil, err
}
result = append(result, val)
default:
val, err := s.Value()
if err != nil {
return nil, err
}
result = append(result, val)
}
}
return result, s.err
}
// skipValue skips a value of the given type
func (s *StreamReader) skipValue(t tagType) error {
switch t {
case tagEnd:
return nil
case tagByte:
_, err := s.r.ReadByte()
return err
case tagInt16:
_, err := s.encoding.Int16(s.r)
return err
case tagInt32:
_, err := s.encoding.Int32(s.r)
return err
case tagInt64:
_, err := s.encoding.Int64(s.r)
return err
case tagFloat32:
_, err := s.encoding.Float32(s.r)
return err
case tagFloat64:
_, err := s.encoding.Float64(s.r)
return err
case tagString:
_, err := s.encoding.String(s.r)
return err
case tagByteArray:
length, err := s.encoding.Int32(s.r)
if err != nil {
return err
}
return s.skipBytes(int64(length))
case tagInt32Array:
length, err := s.encoding.Int32(s.r)
if err != nil {
return err
}
return s.skipBytes(int64(length) * 4)
case tagInt64Array:
length, err := s.encoding.Int32(s.r)
if err != nil {
return err
}
return s.skipBytes(int64(length) * 8)
case tagSlice:
listTypeByte, err := s.r.ReadByte()
if err != nil {
return err
}
listType := tagType(listTypeByte)
length, err := s.encoding.Int32(s.r)
if err != nil {
return err
}
for range length {
if err := s.skipValue(listType); err != nil {
return err
}
}
return nil
case tagStruct:
for {
childTypeByte, err := s.r.ReadByte()
if err != nil {
return err
}
childType := tagType(childTypeByte)
if childType == tagEnd {
return nil
}
// Skip name
if _, err := s.encoding.String(s.r); err != nil {
return err
}
if err := s.skipValue(childType); err != nil {
return err
}
}
default:
return UnknownTagError{Off: s.r.off, TagType: t, Op: "Skip"}
}
}
// skipBytes skips n bytes from the reader
func (s *StreamReader) skipBytes(n int64) error {
if seeker, ok := s.r.Reader.(io.Seeker); ok {
_, err := seeker.Seek(n, io.SeekCurrent)
if err == nil {
s.r.off += n
return nil
}
}
buf := make([]byte, min(n, 32*1024))
for n > 0 {
toRead := min(int64(len(buf)), n)
read, err := s.r.Read(buf[:toRead])
if err != nil {
return err
}
n -= int64(read)
}
return nil
}
// Offset returns the current byte offset in the stream.
func (s *StreamReader) Offset() int64 {
return s.r.off
}
// Tag type constants for external use
const (
TagEnd = tagEnd
TagByte = tagByte
TagInt16 = tagInt16
TagInt32 = tagInt32
TagInt64 = tagInt64
TagFloat32 = tagFloat32
TagFloat64 = tagFloat64
TagByteArray = tagByteArray
TagString = tagString
TagList = tagSlice
TagCompound = tagStruct
TagIntArray = tagInt32Array
TagLongArray = tagInt64Array
)