-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_input.go
212 lines (180 loc) · 5.06 KB
/
binary_input.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
package osg
import (
"bufio"
"encoding/binary"
"github.com/flywave/go-osg/model"
)
const OSG_HEADER_LOW = 0x6C910EA1
const OSG_HEADER_HIGH = 0x1AFB4545
type OsgInputIterator interface {
IsBinary() bool
ReadBool(b *bool)
ReadChar(c *int8)
ReadUChar(c *uint8)
ReadShort(s *int16)
ReadUShort(us *uint16)
ReadInt(i *int32)
ReadUInt(i *uint32)
ReadLong(l *int64)
ReadULong(ul *uint64)
ReadFloat(f *float32)
ReadDouble(d *float64)
ReadString() string
ReadGlenum(value *model.ObjectGlenum)
ReadProperty(prop *model.ObjectProperty)
ReadMark(mark *model.ObjectMark)
ReadCharArray(size int) []byte
ReadWrappedString(str *string)
MatchString(str string) bool
AdvanceToCurrentEndBracket()
SetInputSteam(is *OsgIstream)
GetIterator() *bufio.Reader
SetIterator(*bufio.Reader)
SetSupportBinaryBrackets(sbb bool)
}
type InputIterator struct {
In *bufio.Reader
InputStream *OsgIstream
ByteSwap int
SupportBinaryBrackets bool
Failed bool
}
func NewInputIterator(in *bufio.Reader, bysp int) *InputIterator {
return &InputIterator{In: in, ByteSwap: bysp, SupportBinaryBrackets: false, Failed: false}
}
func (it *InputIterator) SetSupportBinaryBrackets(sbb bool) {
it.SupportBinaryBrackets = sbb
}
func (it *InputIterator) SetInputSteam(is *OsgIstream) {
it.InputStream = is
}
func (iter *InputIterator) MatchString(str string) bool {
return false
}
func (iter *InputIterator) GetIterator() *bufio.Reader {
return iter.In
}
func (iter *InputIterator) SetIterator(bf *bufio.Reader) {
iter.In = bf
}
type BinaryInputIterator struct {
InputIterator
Offset int64
BeginPositions []int64
BlockSizes []int64
}
func NewBinaryInputIterator(in *bufio.Reader) *BinaryInputIterator {
var low int32
var high int32
bysp := 1
binary.Read(in, binary.LittleEndian, &low)
binary.Read(in, binary.LittleEndian, &high)
if low == OSG_HEADER_LOW && high == OSG_HEADER_HIGH {
bysp = 0
}
it := NewInputIterator(in, bysp)
return &BinaryInputIterator{InputIterator: *it}
}
func (iter *BinaryInputIterator) IsBinary() bool {
return true
}
func (iter *BinaryInputIterator) readData(val interface{}, size int) {
if iter.ByteSwap == 0 {
binary.Read(iter.In, binary.LittleEndian, val)
} else {
binary.Read(iter.In, binary.BigEndian, val)
}
iter.Offset += int64(size)
}
func (iter *BinaryInputIterator) ReadCharArray(s int) []byte {
arry := make([]byte, s)
iter.readData(arry, s)
return arry
}
func (iter *BinaryInputIterator) ReadBool(b *bool) {
iter.readData(b, model.BOOLSIZE)
}
func (iter *BinaryInputIterator) ReadChar(b *int8) {
iter.readData(b, model.CHARSIZE)
}
func (iter *BinaryInputIterator) ReadUChar(b *uint8) {
iter.readData(b, model.CHARSIZE)
}
func (iter *BinaryInputIterator) ReadShort(val *int16) {
iter.readData(val, model.SHORTSIZE)
}
func (iter *BinaryInputIterator) ReadUShort(val *uint16) {
iter.readData(val, model.SHORTSIZE)
}
func (iter *BinaryInputIterator) ReadInt(val *int32) {
iter.readData(val, model.INTSIZE)
}
func (iter *BinaryInputIterator) ReadUInt(val *uint32) {
iter.readData(val, model.INTSIZE)
}
func (iter *BinaryInputIterator) ReadLong(val *int64) {
iter.readData(val, model.LONGSIZE)
}
func (iter *BinaryInputIterator) ReadULong(val *uint64) {
iter.readData(val, model.LONGSIZE)
}
func (iter *BinaryInputIterator) ReadFloat(val *float32) {
iter.readData(val, model.FLOATSIZE)
}
func (iter *BinaryInputIterator) ReadDouble(val *float64) {
iter.readData(val, model.DOUBLESIZE)
}
func (iter *BinaryInputIterator) ReadString() string {
var size int32
iter.ReadInt(&size)
return string(iter.ReadCharArray(int(size)))
}
func (iter *BinaryInputIterator) ReadGlenum(val *model.ObjectGlenum) {
var c int32
iter.ReadInt(&c)
val.Value = c
}
func (iter *BinaryInputIterator) ReadProperty(val *model.ObjectProperty) {
if val.MapProperty {
var c int32
iter.ReadInt(&c)
val.Value = c
} else {
val.Value = 0
}
}
func (iter *BinaryInputIterator) ReadMark(mark *model.ObjectMark) {
if iter.SupportBinaryBrackets {
if mark.Name == "{" {
iter.BeginPositions = append(iter.BeginPositions, iter.Offset)
if iter.InputStream.FileVersion > 148 {
var size int64
iter.ReadLong(&size)
iter.BlockSizes = append(iter.BlockSizes, size)
} else {
var size int32
iter.ReadInt(&size)
iter.BlockSizes = append(iter.BlockSizes, int64(size))
}
} else if mark.Name == "}" && len(iter.BeginPositions) > 0 {
iter.BeginPositions = iter.BeginPositions[:len(iter.BeginPositions)-1]
iter.BlockSizes = iter.BlockSizes[:len(iter.BlockSizes)-1]
}
}
}
func (iter *BinaryInputIterator) ReadWrappedString(str *string) {
*str = iter.ReadString()
}
func (iter *BinaryInputIterator) AdvanceToCurrentEndBracket() {
l := len(iter.BeginPositions)
if iter.SupportBinaryBrackets && l > 0 {
pos := iter.BeginPositions[l-1]
bs := len(iter.BlockSizes)
pos += iter.BlockSizes[bs-1]
skip := pos - iter.Offset
iter.Offset = pos
iter.In.Discard(int(skip))
iter.BeginPositions = iter.BeginPositions[:len(iter.BeginPositions)-1]
iter.BlockSizes = iter.BlockSizes[:len(iter.BlockSizes)-1]
}
}