forked from danielpaulus/go-ios
/
decoder.go
265 lines (229 loc) · 8.45 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
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
package dtx
import (
"bytes"
"encoding/binary"
"fmt"
"io"
log "github.com/sirupsen/logrus"
"github.com/greficsmurf/go-ios/ios/nskeyedarchiver"
)
// ReadMessage uses the reader to fully read a Message from it in blocking mode.
func ReadMessage(reader io.Reader) (Message, error) {
header := make([]byte, 32)
_, err := io.ReadFull(reader, header)
if err != nil {
return Message{}, err
}
if binary.BigEndian.Uint32(header) != DtxMessageMagic {
return Message{}, NewOutOfSync(fmt.Sprintf("Wrong Magic: %x", header[0:4]))
}
result := readHeader(header)
if result.IsFragment() {
// the first part of a fragmented message is only a header indicating the total length of
// the defragmented message
if result.IsFirstFragment() {
// put in the header as bytes here
result.fragmentBytes = header
return result, nil
}
// 32 offset is correct, the binary starts with a payload header
messageBytes := make([]byte, result.MessageLength)
_, err := io.ReadFull(reader, messageBytes)
if err != nil {
return Message{}, err
}
result.fragmentBytes = messageBytes
return result, nil
}
payloadHeaderBytes := make([]byte, 16)
_, err = io.ReadFull(reader, payloadHeaderBytes)
if err != nil {
return Message{}, err
}
ph, err := parsePayloadHeader(payloadHeaderBytes)
if err != nil {
return Message{}, err
}
result.PayloadHeader = ph
if result.HasAuxiliary() {
auxHeaderBytes := make([]byte, 16)
_, err = io.ReadFull(reader, auxHeaderBytes)
if err != nil {
return Message{}, err
}
header, err := parseAuxiliaryHeader(auxHeaderBytes)
if err != nil {
return Message{}, err
}
result.AuxiliaryHeader = header
auxBytes := make([]byte, result.AuxiliaryHeader.AuxiliarySize)
_, err = io.ReadFull(reader, auxBytes)
if err != nil {
return Message{}, err
}
result.Auxiliary = DecodeAuxiliary(auxBytes)
}
result.RawBytes = make([]byte, 0)
if result.HasPayload() {
payloadBytes := make([]byte, result.PayloadLength())
_, err := io.ReadFull(reader, payloadBytes)
if err != nil {
return Message{}, err
}
payload, err := nskeyedarchiver.Unarchive(payloadBytes)
if err != nil {
return Message{}, err
}
result.Payload = payload
}
return result, nil
}
// DecodeNonBlocking should only be used for the debug proxy to on the fly decode DtxMessages.
// It is used because if the Decoder encounters an error, we can still keep reading and forwarding the raw bytes.
// This ensures that the debug proxy keeps working and the byte dump can be used to fix the DtxDecoder
func DecodeNonBlocking(messageBytes []byte) (Message, []byte, error) {
if len(messageBytes) < 4 {
return Message{}, make([]byte, 0), NewIncomplete("Less than 4 bytes")
}
if binary.BigEndian.Uint32(messageBytes) != DtxMessageMagic {
return Message{}, make([]byte, 0), NewOutOfSync(fmt.Sprintf("Wrong Magic: %x", messageBytes[0:4]))
}
if len(messageBytes) < 32 {
return Message{}, make([]byte, 0), NewIncomplete("Less than 32 bytes")
}
if binary.LittleEndian.Uint32(messageBytes[4:]) != DtxMessageHeaderLength {
return Message{}, make([]byte, 0), fmt.Errorf("Incorrect Header length, should be 32: %x", messageBytes[4:8])
}
result := readHeader(messageBytes)
if result.IsFirstFragment() {
result.fragmentBytes = messageBytes[:32]
return result, messageBytes[32:], nil
}
if result.IsFragment() {
// 32 offset is correct, the binary starts with a payload header
if len(messageBytes) < result.MessageLength+32 {
return Message{}, make([]byte, 0), NewIncomplete("Fragment lacks bytes")
}
result.fragmentBytes = messageBytes[32 : result.MessageLength+32]
return result, messageBytes[result.MessageLength+32:], nil
}
if len(messageBytes) < 48 {
return Message{}, make([]byte, 0), NewIncomplete("Payload Header missing")
}
ph, err := parsePayloadHeader(messageBytes[32:48])
if err != nil {
return Message{}, make([]byte, 0), err
}
result.PayloadHeader = ph
if result.HasAuxiliary() {
if len(messageBytes) < 64 {
return Message{}, make([]byte, 0), NewIncomplete("Aux Header missing")
}
header, err := parseAuxiliaryHeader(messageBytes[48:64])
if err != nil {
return Message{}, make([]byte, 0), err
}
result.AuxiliaryHeader = header
if len(messageBytes) < int(48+result.PayloadHeader.AuxiliaryLength) {
return Message{}, make([]byte, 0), NewIncomplete("Aux Payload missing")
}
auxBytes := messageBytes[64 : 48+result.PayloadHeader.AuxiliaryLength]
result.Auxiliary = DecodeAuxiliary(auxBytes)
}
totalMessageLength := result.MessageLength + int(DtxMessageHeaderLength)
if len(messageBytes) < totalMessageLength {
return Message{}, make([]byte, 0), NewIncomplete("Payload missing")
}
result.RawBytes = messageBytes[:totalMessageLength]
if result.HasPayload() {
payload, err := result.parsePayloadBytes(result.RawBytes)
if err != nil {
return Message{}, make([]byte, 0), err
}
result.Payload = payload
}
remainingBytes := messageBytes[totalMessageLength:]
return result, remainingBytes, nil
}
func readHeader(messageBytes []byte) Message {
result := Message{}
result.FragmentIndex = binary.LittleEndian.Uint16(messageBytes[8:])
result.Fragments = binary.LittleEndian.Uint16(messageBytes[10:])
result.MessageLength = int(binary.LittleEndian.Uint32(messageBytes[12:]))
result.Identifier = int(binary.LittleEndian.Uint32(messageBytes[16:]))
result.ConversationIndex = int(binary.LittleEndian.Uint32(messageBytes[20:]))
result.ChannelCode = int(binary.LittleEndian.Uint32(messageBytes[24:]))
result.ExpectsReply = binary.LittleEndian.Uint32(messageBytes[28:]) == uint32(1)
return result
}
func parseAuxiliaryHeader(headerBytes []byte) (AuxiliaryHeader, error) {
r := bytes.NewReader(headerBytes)
var result AuxiliaryHeader
err := binary.Read(r, binary.LittleEndian, &result)
if err != nil {
return result, err
}
return result, nil
}
func parsePayloadHeader(messageBytes []byte) (PayloadHeader, error) {
result := PayloadHeader{}
result.MessageType = MessageType(binary.LittleEndian.Uint32(messageBytes))
result.AuxiliaryLength = binary.LittleEndian.Uint32(messageBytes[4:])
result.TotalPayloadLength = binary.LittleEndian.Uint32(messageBytes[8:])
result.Flags = binary.LittleEndian.Uint32(messageBytes[12:])
return result, nil
}
func (d Message) parsePayloadBytes(messageBytes []byte) ([]interface{}, error) {
offset := uint32(0)
if d.HasAuxiliary() && d.HasPayload() {
offset = 48 + d.PayloadHeader.AuxiliaryLength
}
if !d.HasAuxiliary() && d.HasPayload() {
offset = 48
}
if d.PayloadHeader.MessageType == UnknownTypeOne {
return []interface{}{messageBytes[offset:]}, nil
}
if d.PayloadHeader.MessageType == LZ4CompressedMessage {
uncompressed, err := Decompress(messageBytes[offset:])
if err == nil {
log.Infof("lz4 compressed %d bytes/ %d uncompressed ", len(messageBytes[offset:]), len(uncompressed))
} else {
log.Infof("skipping lz4 compressed msg with %d bytes, decompression error %v", len(messageBytes[offset:]), err)
}
return []interface{}{messageBytes[offset:]}, nil
}
return nskeyedarchiver.Unarchive(messageBytes[offset:])
}
// PayloadLength equals PayloadHeader.TotalPayloadLength - d.PayloadHeader.AuxiliaryLength so it is the Payload without the Auxiliary
func (d Message) PayloadLength() uint32 {
return d.PayloadHeader.TotalPayloadLength - d.PayloadHeader.AuxiliaryLength
}
// HasAuxiliary returns PayloadHeader.AuxiliaryLength > 0
func (d Message) HasAuxiliary() bool {
return d.PayloadHeader.AuxiliaryLength > 0
}
// HasPayload returns PayloadLength() > 0, it is true if the Message has payload bytes
func (d Message) HasPayload() bool {
return d.PayloadLength() > 0
}
// IsFirstFragment returns true if the message is the first of a series of fragments.IsFirstFragment
// The first fragment message is only 32 bytes long
func (d Message) IsFirstFragment() bool {
return d.Fragments > 1 && d.FragmentIndex == 0
}
// IsLastFragment returns true if this message is the last fragment
func (d Message) IsLastFragment() bool {
return d.Fragments > 1 && d.Fragments-d.FragmentIndex == 1
}
// IsFragment returns true if the Message is a fragment
func (d Message) IsFragment() bool {
return d.Fragments > 1
}
// MessageIsFirstFragmentFor indicates whether the message you call this on, is the first part of a fragmented message, and if otherMessage is a subsequent fragment
func (d Message) MessageIsFirstFragmentFor(otherMessage Message) bool {
if !d.IsFirstFragment() {
return false
}
return d.Identifier == otherMessage.Identifier && d.Fragments == otherMessage.Fragments && otherMessage.FragmentIndex > 0
}