-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryEncoder.cs
355 lines (353 loc) · 10.3 KB
/
BinaryEncoder.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using WhatsAppLib.Models;
using WhatsAppLib.Utils;
using static WhatsAppLib.Serialization.BinaryDecoder;
namespace WhatsAppLib.Serialization
{
internal class BinaryEncoder
{
private List<byte> _data = new List<byte>();
public byte[] WriteNode(Node node)
{
var numAttributes = 0;
if (node.Attributes != null && node.Attributes.Count > 0)
{
numAttributes = node.Attributes.Count;
}
var hasContent = 0;
if (node.Content != null)
{
hasContent = 1;
}
WriteListStart(2 * numAttributes + 1 + hasContent);
WriteString(node.Description);
WriteAttributes(node.Attributes);
WriteChildren(node.Content);
return _data.ToArray();
}
private void WriteChildren(object obj)
{
switch (obj)
{
case string str:
WriteString(str,true);
break;
case byte[] bs:
WriteByteLength(bs.Length);
PushBytes(bs);
break;
case List<Node> ns:
WriteListStart(ns.Count);
foreach (var item in ns)
{
WriteNode(item);
}
break;
default:
LogUtil.Warn("cannot write child of type:"+obj);
break;
}
}
private void WriteAttributes(Dictionary<string,string> attributes)
{
if (attributes == null)
{
return;
}
foreach (var item in attributes)
{
if (string.IsNullOrWhiteSpace(item.Value))
{
continue;
}
WriteString(item.Key);
WriteString(item.Value);
}
}
private void WriteString(string str, bool i=false)
{
if (!i && str == "c.us")
{
WriteToken(SingleByteTokens.IndexOf("s.whatsapp.net"));
return;
}
var tokenIndex = SingleByteTokens.IndexOf(str);
if (tokenIndex == -1)
{
var jidSepIndex = str.IndexOf("@");
if (jidSepIndex < 1)
{
WriteStringRaw(str);
}
else
{
WriteJid(str.Substring(0, jidSepIndex), str.Substring(jidSepIndex + 1));
}
}
else
{
if (tokenIndex < 256)
{
WriteToken(tokenIndex);
}
else
{
var singleByteOverflow = tokenIndex - 256;
var dictionaryIndex = singleByteOverflow >> 8;
if (dictionaryIndex < 0 || dictionaryIndex > 3)
{
LogUtil.Warn("double byte dictionary token out of range:"+str);
return;
}
WriteToken((int)ReadStringTag.DICTIONARY_0 + dictionaryIndex);
WriteToken(singleByteOverflow % 256);
}
}
}
private void WriteJid(string jidLeft, string jidRight)
{
PushByte((byte)ReadStringTag.JID_PAIR);
if (!string.IsNullOrWhiteSpace(jidLeft))
{
WritePackedBytes(jidLeft);
}
else
{
WriteToken((int)ReadStringTag.LIST_EMPTY);
}
WriteString(jidRight);
}
private void WritePackedBytes(string str)
{
if (!WritePackedBytesImpl(str, (int)ReadStringTag.NIBBLE_8))
{
if (!WritePackedBytesImpl(str, (int)ReadStringTag.HEX_8))
{
LogUtil.Warn("WritePackedBytes fail");
}
}
}
private bool WritePackedBytesImpl(string str, int dataType)
{
var numBytes = str.Length;
if (numBytes > 254)
{
LogUtil.Warn("too many bytes to pack:" + numBytes);
return false;
}
PushByte(dataType);
int x = 0;
if (numBytes % 2 != 0)
{
x = 128;
}
PushByte(x | (int)(Math.Ceiling(numBytes / 2.0)));
for (int i = 0; i < numBytes / 2; i++)
{
var b = PackBytePair(dataType, str.Substring(2 * i, 1), str.Substring(2 * i + 1, 1));
if (b < 0)
{
return false;
}
PushByte(b);
}
if (numBytes % 2 != 0)
{
var b = PackBytePair(dataType, str.Substring(numBytes - 1), "\x00");
if (b < 0)
{
return false;
}
PushByte(b);
}
return true;
}
private int PackBytePair(int packType, string part1, string part2)
{
if (packType == (int)ReadStringTag.NIBBLE_8)
{
var n1 = PackNibble(part1);
if (n1 < 0)
{
return -1;
}
var n2 = PackNibble(part2);
if (n2 < 0)
{
return -1;
}
return (n1 << 4) | n2;
}
else if (packType == (int)ReadStringTag.HEX_8)
{
var n1 = PackHex(part1);
if (n1 < 0)
{
return -1;
}
var n2 = PackHex(part2);
if (n2 < 0)
{
return -1;
}
return (n1 << 4) | n2;
}
else
{
LogUtil.Warn($"invalid pack type {packType} for byte pair:{part1} / {part2}");
return -1;
}
}
private int PackNibble(string str)
{
if (str.Length > 1)
{
LogUtil.Warn("PackNibble str length:" + str.Length);
return -1;
}
else if (str[0] >= '0' && str[0] <= '9')
{
return Convert.ToInt32(str);
}
else if (str == "-")
{
return 10;
}
else if (str == ".")
{
return 11;
}
else if (str == "\x00")
{
return 15;
}
LogUtil.Warn("invalid string to pack as nibble:" + str);
return -1;
}
private int PackHex(string str)
{
if (str.Length > 1)
{
LogUtil.Warn("PackHex str length:" + str.Length);
return -1;
}
var value = str[0];
if ((value >= '0' && value <= '9') || (value >= 'A' && value <= 'F') || (value >= 'a' && value <= 'f'))
{
var d = Convert.ToInt32(str, 16);
return d;
}
else if (str == "\x00")
{
return 15;
}
LogUtil.Warn("invalid string to pack as hex: "+ str);
return -1;
}
private void WriteStringRaw(string str)
{
WriteByteLength(str.Length);
PushString(str);
}
private void WriteByteLength(int length)
{
if (length > int.MaxValue)
{
LogUtil.Error("length is too large:" + length);
}
else if (length >= (1 << 20))
{
PushByte((byte)ReadStringTag.BINARY_32);
PushInt32(length);
}
else if (length >=256)
{
PushByte((byte)ReadStringTag.BINARY_20);
PushInt20(length);
}
else
{
PushByte((byte)ReadStringTag.BINARY_8);
PushInt8(length);
}
}
private void WriteToken(int token)
{
if (token < SingleByteTokens.Count)
{
PushByte((byte)token);
}
else if (token <= 500)
{
LogUtil.Error("invalid token: " + token);
}
}
private void WriteListStart(int listSize)
{
if (listSize == 0)
{
PushByte((byte)ReadStringTag.LIST_EMPTY);
}
else if (listSize < 256)
{
PushByte((byte)ReadStringTag.LIST_8);
PushInt8(listSize);
}
else
{
PushByte((byte)ReadStringTag.LIST_16);
PushInt16(listSize);
}
}
private void PushString(string str)
{
PushBytes(Encoding.UTF8.GetBytes(str));
}
private void PushIntN(int value, int n, bool littleEndian = false)
{
for (int i = 0; i < n; i++)
{
int curShift;
if (littleEndian)
{
curShift = i;
}
else
{
curShift = n - i - 1;
}
PushByte((byte)((value >> (curShift * 8)) & 0xFF));
}
}
private void PushInt8(int value)
{
PushIntN(value, 1);
}
private void PushInt16(int value)
{
PushIntN(value, 2);
}
private void PushInt20(int value)
{
PushIntN(value, 3);
}
private void PushInt32(int value)
{
PushIntN(value, 4);
}
private void PushByte(byte b)
{
_data.Add(b);
}
private void PushByte(int b)
{
_data.Add((byte)b);
}
private void PushBytes(IEnumerable<byte> bs)
{
_data.AddRange(bs);
}
}
}