-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecoder.go
232 lines (215 loc) · 6.97 KB
/
decoder.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
package xivnet
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"time"
)
type OodleImpl interface {
Decompress(input []byte, outputLength int64) ([]byte, error)
}
// Decoder implements an FFXIV frame decoder. It is not thread-safe, so consumers
// need to be sure not to use it concurrently.
type Decoder struct {
frameReader *bufio.Reader
blockReader *bufio.Reader
oodleImpl OodleImpl
}
// NewDecoder creates a new instance of a frame decoder.
// bufSize controls the total size of the buffer used to store a single
// frame. It's recommended to keep this value at least 8192, since this value
// generally works for all frames (as far as I can tell).
func NewDecoder(r io.Reader, bufSize int) *Decoder {
return &Decoder{
frameReader: bufio.NewReaderSize(r, bufSize),
blockReader: bufio.NewReaderSize(r, 4096),
}
}
func NewDecoderWithOodle(r io.Reader, bufSize int, oodleImpl OodleImpl) *Decoder {
return &Decoder{
frameReader: bufio.NewReaderSize(r, bufSize),
blockReader: bufio.NewReaderSize(r, 4096),
oodleImpl: oodleImpl,
}
}
// CheckHeader checks to see whether or not the data in the buffer has a
// valid header
func (d *Decoder) CheckHeader() ([]byte, error) {
if 28 > d.frameReader.Size() {
return nil, InvalidFrameLengthError{length: 28, maxLength: d.frameReader.Size()}
}
// Validation that the frame at least has
header, err := d.frameReader.Peek(28)
if err != nil {
return nil, EOFError{
operation: "peeking header",
attemptedLength: 28,
wrapped: err,
}
}
if !isValidHeader(header) {
return nil, InvalidHeaderError{
header: hex.EncodeToString(header),
}
}
return header, nil
}
// NextFrame reads data from the underlying buffer and returns a single decoded
// FFXIV frame from the provided buffer.
// If there is some sort of decoding error, NextFrame will call
// DiscardDataUntilValid in order to recover from corrupt data in the byte
// stream.
func (d *Decoder) NextFrame() (*Frame, error) {
header, err := d.CheckHeader()
if err != nil {
return nil, err
}
length := binary.LittleEndian.Uint32(header[24:])
if length > uint32(d.frameReader.Size()) {
return nil, InvalidFrameLengthError{length: length, maxLength: d.frameReader.Size()}
}
intLength := int(length)
frameBytes, err := d.frameReader.Peek(intLength)
if err != nil {
return nil, EOFError{
operation: "peeking data",
attemptedLength: intLength,
wrapped: err,
}
}
f, err := d.decodeFrame(frameBytes, d.blockReader, length)
if err != nil {
// If there is some sort of decoding error, let's
// start discarding data until it's valid
debugData := hex.EncodeToString(frameBytes)
_, _ = d.frameReader.Discard(1)
d.DiscardDataUntilValid()
return nil, DecodingError{
wrapped: err,
debugData: debugData,
}
}
// We "read" intLength amount of data
_, _ = d.frameReader.Discard(intLength)
return f, nil
}
func (d *Decoder) decodeFrame(frameBytes []byte, blockReader *bufio.Reader, length uint32) (*Frame, error) {
// Build the frame
frame := &Frame{}
copy(frame.Preamble[:], frameBytes[0:16])
msecSinceEpoch := time.Duration(binary.LittleEndian.Uint64(frameBytes[16:24])) * time.Millisecond
frame.Time = time.Unix(0, 0).Add(msecSinceEpoch)
frame.Length = length
frame.ConnectionType = binary.LittleEndian.Uint16(frameBytes[28:30])
frame.Count = binary.LittleEndian.Uint16(frameBytes[30:32])
frame.Reserved1 = frameBytes[32]
frame.Compression = frameBytes[33]
frame.Reserved2 = binary.LittleEndian.Uint16(frameBytes[34:36])
frame.DecompressedLength = binary.LittleEndian.Uint32(frameBytes[36:40])
blockData := frameBytes[40:length]
if frame.Compression == 1 {
r, err := zlib.NewReader(bytes.NewReader(blockData))
if err != nil {
return nil, fmt.Errorf("error decompressing data: %s", err.Error())
}
blockReader.Reset(r)
} else if frame.Compression == 2 {
if d.oodleImpl == nil {
return nil, fmt.Errorf("error decompressing oodle data: oodle implementation is missing")
}
rawBytes, err := d.oodleImpl.Decompress(blockData, int64(frame.DecompressedLength))
if err != nil {
return nil, fmt.Errorf("error decompressing oodle data: %s", err.Error())
}
blockReader.Reset(bytes.NewReader(rawBytes))
} else {
blockReader.Reset(bytes.NewReader(blockData))
}
if len(blockData) == 0 {
return frame, nil
}
for {
block, err := decodeBlock(blockReader)
if err != nil {
return nil, fmt.Errorf("error decoding blocks: %s", err.Error())
}
if block == nil {
break
}
frame.Blocks = append(frame.Blocks, block)
}
return frame, nil
}
func decodeBlock(reader *bufio.Reader) (*Block, error) {
lengthBytes, err := reader.Peek(4)
if err == io.EOF {
return nil, nil
} else if err != nil {
return nil, err
}
length := binary.LittleEndian.Uint32(lengthBytes)
blockBytes, err := reader.Peek(int(length))
actualLen := len(blockBytes)
defer func() {
// No matter what, we "read" actualLen amount of data
_, _ = reader.Discard(actualLen)
}()
if err != nil {
return nil, fmt.Errorf("not enough data: expected %d bytes, got %d", length, actualLen)
}
if actualLen < 16 {
return nil, fmt.Errorf("not enough data: expected at least 16 bytes, got %d", actualLen)
}
block := &Block{}
block.Length = length
block.SubjectID = binary.LittleEndian.Uint32(blockBytes[4:8])
block.CurrentID = binary.LittleEndian.Uint32(blockBytes[8:12])
block.Type = binary.LittleEndian.Uint16(blockBytes[12:14])
block.Pad1 = binary.LittleEndian.Uint16(blockBytes[14:16])
var blockData GenericBlockData
if block.Type == BlockTypeIPC {
if actualLen < 32 {
return nil, fmt.Errorf("not enough data: expected at least 32 bytes, got %d", actualLen)
}
block.Reserved = binary.LittleEndian.Uint16(blockBytes[16:18])
block.Opcode = binary.LittleEndian.Uint16(blockBytes[18:20])
block.Pad2 = binary.LittleEndian.Uint16(blockBytes[20:22])
block.ServerID = binary.LittleEndian.Uint16(blockBytes[22:24])
block.Time = time.Unix(int64(binary.LittleEndian.Uint32(blockBytes[24:28])), 0)
block.Pad3 = binary.LittleEndian.Uint32(blockBytes[28:32])
blockData = make([]byte, length-32)
blockData.UnmarshalBytes(blockBytes[32:length])
} else {
blockData = make([]byte, length-16)
blockData.UnmarshalBytes(blockBytes[16:length])
}
block.Data = &blockData
return block, nil
}
func isValidHeader(header []byte) bool {
preamble := binary.LittleEndian.Uint64(header[0:8])
return (preamble == 0) || (preamble == 0xe2465dff41a05252)
}
func strictIsValidHeader(header []byte) bool {
preamble := binary.LittleEndian.Uint64(header[0:8])
return preamble == 0xe2465dff41a05252
}
// DiscardDataUntilValid will trim off invalid data on the buffered input
// until it reaches a header that is valid or the buffer has insufficient data.
// This is useful for when the input stream has been corrupted with some invalid bytes.
func (d *Decoder) DiscardDataUntilValid() {
for {
header, err := d.frameReader.Peek(28)
if err != nil {
return
}
if strictIsValidHeader(header) {
return
}
_, _ = d.frameReader.Discard(1)
}
}