summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Rhymen/go-whatsapp/binary/decoder.go
blob: 95da606c23b918cb347d1a934bc88568645a2a89 (plain) (blame)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package binary

import (
	"fmt"
	"github.com/Rhymen/go-whatsapp/binary/token"
	"io"
	"strconv"
)

type binaryDecoder struct {
	data  []byte
	index int
}

func NewDecoder(data []byte) *binaryDecoder {
	return &binaryDecoder{data, 0}
}

func (r *binaryDecoder) checkEOS(length int) error {
	if r.index+length > len(r.data) {
		return io.EOF
	}

	return nil
}

func (r *binaryDecoder) readByte() (byte, error) {
	if err := r.checkEOS(1); err != nil {
		return 0, err
	}

	b := r.data[r.index]
	r.index++

	return b, nil
}

func (r *binaryDecoder) readIntN(n int, littleEndian bool) (int, error) {
	if err := r.checkEOS(n); err != nil {
		return 0, err
	}

	var ret int

	for i := 0; i < n; i++ {
		var curShift int
		if littleEndian {
			curShift = i
		} else {
			curShift = n - i - 1
		}
		ret |= int(r.data[r.index+i]) << uint(curShift*8)
	}

	r.index += n
	return ret, nil
}

func (r *binaryDecoder) readInt8(littleEndian bool) (int, error) {
	return r.readIntN(1, littleEndian)
}

func (r *binaryDecoder) readInt16(littleEndian bool) (int, error) {
	return r.readIntN(2, littleEndian)
}

func (r *binaryDecoder) readInt20() (int, error) {
	if err := r.checkEOS(3); err != nil {
		return 0, err
	}

	ret := ((int(r.data[r.index]) & 15) << 16) + (int(r.data[r.index+1]) << 8) + int(r.data[r.index+2])
	r.index += 3
	return ret, nil
}

func (r *binaryDecoder) readInt32(littleEndian bool) (int, error) {
	return r.readIntN(4, littleEndian)
}

func (r *binaryDecoder) readInt64(littleEndian bool) (int, error) {
	return r.readIntN(8, littleEndian)
}

func (r *binaryDecoder) readPacked8(tag int) (string, error) {
	startByte, err := r.readByte()
	if err != nil {
		return "", err
	}

	ret := ""

	for i := 0; i < int(startByte&127); i++ {
		currByte, err := r.readByte()
		if err != nil {
			return "", err
		}

		lower, err := unpackByte(tag, currByte&0xF0>>4)
		if err != nil {
			return "", err
		}

		upper, err := unpackByte(tag, currByte&0x0F)
		if err != nil {
			return "", err
		}

		ret += lower + upper
	}

	if startByte>>7 != 0 {
		ret = ret[:len(ret)-1]
	}
	return ret, nil
}

func unpackByte(tag int, value byte) (string, error) {
	switch tag {
	case token.NIBBLE_8:
		return unpackNibble(value)
	case token.HEX_8:
		return unpackHex(value)
	default:
		return "", fmt.Errorf("unpackByte with unknown tag %d", tag)
	}
}

func unpackNibble(value byte) (string, error) {
	switch {
	case value < 0 || value > 15:
		return "", fmt.Errorf("unpackNibble with value %d", value)
	case value == 10:
		return "-", nil
	case value == 11:
		return ".", nil
	case value == 15:
		return "\x00", nil
	default:
		return strconv.Itoa(int(value)), nil
	}
}

func unpackHex(value byte) (string, error) {
	switch {
	case value < 0 || value > 15:
		return "", fmt.Errorf("unpackHex with value %d", value)
	case value < 10:
		return strconv.Itoa(int(value)), nil
	default:
		return string('A' + value - 10), nil
	}
}

func (r *binaryDecoder) readListSize(tag int) (int, error) {
	switch tag {
	case token.LIST_EMPTY:
		return 0, nil
	case token.LIST_8:
		return r.readInt8(false)
	case token.LIST_16:
		return r.readInt16(false)
	default:
		return 0, fmt.Errorf("readListSize with unknown tag %d at position %d", tag, r.index)
	}
}

func (r *binaryDecoder) readString(tag int) (string, error) {
	switch {
	case tag >= 3 && tag <= len(token.SingleByteTokens):
		tok, err := token.GetSingleToken(tag)
		if err != nil {
			return "", err
		}

		if tok == "s.whatsapp.net" {
			tok = "c.us"
		}

		return tok, nil
	case tag == token.DICTIONARY_0 || tag == token.DICTIONARY_1 || tag == token.DICTIONARY_2 || tag == token.DICTIONARY_3:
		i, err := r.readInt8(false)
		if err != nil {
			return "", err
		}

		return token.GetDoubleToken(tag-token.DICTIONARY_0, i)
	case tag == token.LIST_EMPTY:
		return "", nil
	case tag == token.BINARY_8:
		length, err := r.readInt8(false)
		if err != nil {
			return "", err
		}

		return r.readStringFromChars(length)
	case tag == token.BINARY_20:
		length, err := r.readInt20()
		if err != nil {
			return "", err
		}

		return r.readStringFromChars(length)
	case tag == token.BINARY_32:
		length, err := r.readInt32(false)
		if err != nil {
			return "", err
		}

		return r.readStringFromChars(length)
	case tag == token.JID_PAIR:
		b, err := r.readByte()
		if err != nil {
			return "", err
		}
		i, err := r.readString(int(b))
		if err != nil {
			return "", err
		}

		b, err = r.readByte()
		if err != nil {
			return "", err
		}
		j, err := r.readString(int(b))
		if err != nil {
			return "", err
		}

		if i == "" || j == "" {
			return "", fmt.Errorf("invalid jid pair: %s - %s", i, j)
		}

		return i + "@" + j, nil
	case tag == token.NIBBLE_8 || tag == token.HEX_8:
		return r.readPacked8(tag)
	default:
		return "", fmt.Errorf("invalid string with tag %d", tag)
	}
}

func (r *binaryDecoder) readStringFromChars(length int) (string, error) {
	if err := r.checkEOS(length); err != nil {
		return "", err
	}

	ret := r.data[r.index : r.index+length]
	r.index += length

	return string(ret), nil
}

func (r *binaryDecoder) readAttributes(n int) (map[string]string, error) {
	if n == 0 {
		return nil, nil
	}

	ret := make(map[string]string)
	for i := 0; i < n; i++ {
		idx, err := r.readInt8(false)
		if err != nil {
			return nil, err
		}

		index, err := r.readString(idx)
		if err != nil {
			return nil, err
		}

		idx, err = r.readInt8(false)
		if err != nil {
			return nil, err
		}

		ret[index], err = r.readString(idx)
		if err != nil {
			return nil, err
		}
	}

	return ret, nil
}

func (r *binaryDecoder) readList(tag int) ([]Node, error) {
	size, err := r.readListSize(tag)
	if err != nil {
		return nil, err
	}

	ret := make([]Node, size)
	for i := 0; i < size; i++ {
		n, err := r.ReadNode()

		if err != nil {
			return nil, err
		}

		ret[i] = *n
	}

	return ret, nil
}

func (r *binaryDecoder) ReadNode() (*Node, error) {
	ret := &Node{}

	size, err := r.readInt8(false)
	if err != nil {
		return nil, err
	}
	listSize, err := r.readListSize(size)
	if err != nil {
		return nil, err
	}

	descrTag, err := r.readInt8(false)
	if descrTag == token.STREAM_END {
		return nil, fmt.Errorf("unexpected stream end")
	}
	ret.Description, err = r.readString(descrTag)
	if err != nil {
		return nil, err
	}
	if listSize == 0 || ret.Description == "" {
		return nil, fmt.Errorf("invalid Node")
	}

	ret.Attributes, err = r.readAttributes((listSize - 1) >> 1)
	if err != nil {
		return nil, err
	}

	if listSize%2 == 1 {
		return ret, nil
	}

	tag, err := r.readInt8(false)
	if err != nil {
		return nil, err
	}

	switch tag {
	case token.LIST_EMPTY, token.LIST_8, token.LIST_16:
		ret.Content, err = r.readList(tag)
	case token.BINARY_8:
		size, err = r.readInt8(false)
		if err != nil {
			return nil, err
		}

		ret.Content, err = r.readBytes(size)
	case token.BINARY_20:
		size, err = r.readInt20()
		if err != nil {
			return nil, err
		}

		ret.Content, err = r.readBytes(size)
	case token.BINARY_32:
		size, err = r.readInt32(false)
		if err != nil {
			return nil, err
		}

		ret.Content, err = r.readBytes(size)
	default:
		ret.Content, err = r.readString(tag)
	}

	if err != nil {
		return nil, err
	}
	return ret, nil
}

func (r *binaryDecoder) readBytes(n int) ([]byte, error) {
	ret := make([]byte, n)
	var err error

	for i := range ret {
		ret[i], err = r.readByte()
		if err != nil {
			return nil, err
		}
	}

	return ret, nil
}