-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreader_list_binary.go
84 lines (67 loc) · 2.03 KB
/
reader_list_binary.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
package ply
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
)
type listBinaryPropertyReader struct {
property ListProperty
endian binary.ByteOrder
buf []byte
lastReadListSize int32
}
func (lpr listBinaryPropertyReader) Count(in io.Reader) (int32, error) {
switch lpr.property.CountType {
case UChar:
_, err := io.ReadFull(in, lpr.buf[:1])
return int32(lpr.buf[0]), err
case UInt, Int:
_, err := io.ReadFull(in, lpr.buf[:4])
return int32(lpr.endian.Uint32(lpr.buf)), err
}
return -1, fmt.Errorf("unimplemented list property count type: %s", lpr.property.CountType)
}
func (lpr *listBinaryPropertyReader) Read(in io.Reader) (err error) {
lpr.lastReadListSize, err = lpr.Count(in)
if err != nil {
return err
}
payloadSize := int(lpr.lastReadListSize) * lpr.property.ListType.Size()
if len(lpr.buf) < payloadSize {
lpr.buf = make([]byte, payloadSize)
}
_, err = io.ReadFull(in, lpr.buf[:payloadSize])
return err
}
func (lpr listBinaryPropertyReader) Int(out []int) error {
if int32(len(out)) < lpr.lastReadListSize {
return errors.New("can't fit property reader data in provided out slice")
}
for i := 0; i < int(lpr.lastReadListSize); i++ {
switch lpr.property.ListType {
case UInt, Int:
out[i] = int(int32(lpr.endian.Uint32(lpr.buf[i*4:])))
default:
return fmt.Errorf("unimplemented list property list type: '%s' for int deserialization", lpr.property.ListType)
}
}
return nil
}
func (lpr listBinaryPropertyReader) Float64(out []float64) error {
if int32(len(out)) < lpr.lastReadListSize {
return errors.New("can't fit property reader data in provided out slice")
}
for i := 0; i < int(lpr.lastReadListSize); i++ {
switch lpr.property.ListType {
case Float:
out[i] = float64(math.Float32frombits(lpr.endian.Uint32(lpr.buf[i*4:])))
case Double:
out[i] = math.Float64frombits(lpr.endian.Uint64(lpr.buf[i*8:]))
default:
return fmt.Errorf("unimplemented list property list type: '%s' for float deserialization", lpr.property.ListType)
}
}
return nil
}