-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
251 lines (225 loc) · 4.69 KB
/
reader.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
package swfx
import (
"bufio"
"compress/zlib"
"encoding/binary"
"errors"
"io"
"b7c.io/swfx/tagcode"
)
type SwfReader interface {
io.Reader
io.ByteReader
WrapInnerStream(wrap func(inner io.Reader) io.Reader)
Position() int
// Reads into the specified byte slice.
// Panics if the slice is unable to be filled.
MustRead([]byte)
// Reads a single bit and advances the bit position.
ReadBit() byte
// Reads a single bit as a boolean.
ReadBool() bool
// Reads n bits as an unsigned integer.
ReadUbits(n int) int
// Reads n bits as a signed integer.
ReadSbits(n int) int
// Reads an unsigned 8-bit integer.
ReadUint8() uint8
// Reads an unsigned 16-bit integer.
ReadUint16() uint16
// Reads an unsigned 32-bit integer.
ReadUint32() uint32
// Reads a string.
ReadString(limit int) string
// Reads a rectangle.
ReadRect() Rect
// Reads a tag code and length.
ReadTagCodeAndLength() (code tagcode.TagCode, length uint32)
// Reads a tag.
ReadTag(code tagcode.TagCode, length uint32) SwfTag
}
type swfReader struct {
r io.Reader
buf [8]byte
pos int
bitPos int
}
func (r *swfReader) WrapInnerStream(wrap func(inner io.Reader) io.Reader) {
r.r = wrap(r.r)
}
func (r *swfReader) Position() int {
return r.pos
}
func (r *swfReader) ReadByte() (byte, error) {
r.align()
if byteReader, ok := r.r.(io.ByteReader); ok {
b, err := byteReader.ReadByte()
if err != nil {
r.pos++
}
return b, err
}
n, err := r.r.Read(r.buf[:1])
r.pos += n
return r.buf[0], err
}
func (r *swfReader) Read(p []byte) (int, error) {
r.align()
n, err := r.r.Read(p)
r.pos += n
return n, err
}
func (r *swfReader) MustRead(p []byte) {
read := 0
var n int
var err error
for read < len(p) {
if err != nil {
if errors.Is(err, io.EOF) {
err = io.ErrUnexpectedEOF
}
panic(err)
}
n, err = r.Read(p[read:])
read += n
}
}
func (r *swfReader) ReadBool() bool {
return r.ReadBit() == 1
}
func (r *swfReader) ReadUbits(n int) int {
var bit byte
var value int
for i := 0; i < n; i++ {
bit = r.ReadBit()
value = (value << 1) | int(bit)
}
return value
}
func (r *swfReader) ReadSbits(n int) int {
// sign-extend
shift := 32 - n
return int(int32(r.ReadUbits(n)) << shift >> shift)
}
func (r *swfReader) ReadFbits(n int) int {
panic("not implemented")
}
func (r *swfReader) ReadRect() Rect {
r.align()
nbits := r.ReadUbits(5)
xmin := r.ReadSbits(nbits)
xmax := r.ReadSbits(nbits)
ymin := r.ReadSbits(nbits)
ymax := r.ReadSbits(nbits)
return Rect{
Xmin: xmin,
Xmax: xmax,
Ymin: ymin,
Ymax: ymax,
}
}
func (r *swfReader) fill(count int) {
r.MustRead(r.buf[:count])
}
func (r *swfReader) ReadUint8() uint8 {
r.fill(1)
return r.buf[0]
}
func (r *swfReader) ReadUint16() uint16 {
r.fill(2)
return binary.LittleEndian.Uint16(r.buf[:2])
}
func (r *swfReader) ReadUint32() uint32 {
r.fill(4)
return binary.LittleEndian.Uint32(r.buf[:4])
}
func (r *swfReader) ReadString(limit int) string {
buf := make([]byte, 0, limit)
read := 0
for read < limit {
b, err := r.ReadByte()
if err != nil {
panic(err)
}
if b == 0 {
break
}
buf = append(buf, b)
read++
}
return string(buf)
}
func (r *swfReader) ReadBit() byte {
var bit byte
var err error
if r.bitPos == 0 {
bit, err = r.ReadByte()
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
r.buf[0] = bit
} else {
bit = r.buf[0]
}
r.bitPos++
bit = (bit >> (8 - r.bitPos)) & 1
r.bitPos %= 8
return bit
}
func (r *swfReader) ReadTagCodeAndLength() (code tagcode.TagCode, length uint32) {
tagCodeAndLength := r.ReadUint16()
code = tagcode.TagCode(tagCodeAndLength >> 6)
length = uint32(tagCodeAndLength & 0x3f)
if length == 0x3f {
length = r.ReadUint32()
}
return
}
func (r *swfReader) ReadTag(tagCode tagcode.TagCode, length uint32) SwfTag {
t := MakeTag(tagCode)
t.readData(r, int(length))
return t
}
func (r *swfReader) align() {
r.bitPos = 0
}
func NewReader(r io.Reader) SwfReader {
return &swfReader{
r: r,
}
}
// Reads the SWF header and applies compression if necessary as specified by the file signature.
func ReadHeader(r SwfReader) (header SwfHeader, err error) {
buf := make([]byte, 8)
_, err = r.Read(buf)
if err != nil {
return
}
switch buf[0] {
case 'F':
r.WrapInnerStream(func(inner io.Reader) io.Reader {
if _, ok := inner.(*bufio.Reader); !ok {
inner = bufio.NewReader(inner)
}
return inner
})
case 'C':
r.WrapInnerStream(func(inner io.Reader) io.Reader {
inner, err = zlib.NewReader(inner)
if err != nil {
panic(err)
}
return inner
})
default:
err = errors.ErrUnsupported
return
}
header = SwfHeader{
Version: int(buf[3]),
FrameSize: r.ReadRect(),
FrameRate: r.ReadUint16(),
FrameCount: r.ReadUint16(),
}
return
}