-
Notifications
You must be signed in to change notification settings - Fork 211
/
databuffer.cc
420 lines (370 loc) · 8.3 KB
/
databuffer.cc
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <string.h>
#include <assert.h>
#include "databuffer.h"
#include "keygen.h"
#include "wadict.h"
#include "tree.h"
#include "wa_connection.h"
extern "C" {
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
}
// This changed to a huffman-like encoding. Therefore we can use one or two bytes...
static std::string getDecoded(int n)
{
return std::string(main_dict[n]);
}
static std::string getDecodedExtended(int n, int n2)
{
unsigned index = n * 256 + n2;
if (index < sizeof(sec_dict)/sizeof(sec_dict[0]))
return std::string(sec_dict[index]);
return "";
}
// Return a token (or extended token) for a given value
static unsigned short lookupDecoded(std::string value)
{
for (unsigned int i = 3; i < sizeof(main_dict)/sizeof(main_dict[0]); i++) {
if (strcmp(main_dict[i], value.c_str()) == 0)
return i;
}
// Not found in the main dict, search the secondary
for (unsigned int i = 0; i < sizeof(sec_dict)/sizeof(sec_dict[0]); i++) {
if (strcmp(sec_dict[i], value.c_str()) == 0)
return i + 0x0100;
}
return 0;
}
DataBuffer::DataBuffer(const void *ptr, int size)
{
if (ptr != NULL and size > 0) {
buffer = (unsigned char *)malloc(size + 1);
memcpy(buffer, ptr, size);
blen = size;
} else {
blen = 0;
buffer = (unsigned char *)malloc(1024);
}
}
DataBuffer::~DataBuffer()
{
free(buffer);
}
DataBuffer::DataBuffer(const DataBuffer & other)
{
blen = other.blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, other.buffer, blen);
}
DataBuffer DataBuffer::operator+(const DataBuffer & other) const
{
DataBuffer result = *this;
result.addData(other.buffer, other.blen);
return result;
}
DataBuffer & DataBuffer::operator =(const DataBuffer & other)
{
if (this != &other) {
free(buffer);
this->blen = other.blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, other.buffer, blen);
}
return *this;
}
DataBuffer::DataBuffer(const DataBuffer * d)
{
blen = d->blen;
buffer = (unsigned char *)malloc(blen + 1024);
memcpy(buffer, d->buffer, blen);
}
DataBuffer *DataBuffer::decodedBuffer(RC4Decoder * decoder, int clength, bool dout)
{
DataBuffer *deco = new DataBuffer(this->buffer, clength);
decoder->cipher(&deco->buffer[0], clength - 4);
return deco;
}
DataBuffer *DataBuffer::decompressedBuffer()
{
int ressize = this->size()*2;
char dbuf[ressize];
size_t outsize = tinfl_decompress_mem_to_mem(dbuf, ressize, this->buffer, this->size(), 1);
if (outsize >= 0)
return new DataBuffer(dbuf, outsize);
return NULL;
}
DataBuffer DataBuffer::encodedBuffer(RC4Decoder * decoder, unsigned char *key, bool dout, unsigned int seq)
{
DataBuffer deco = *this;
decoder->cipher(&deco.buffer[0], blen);
unsigned char hmacint[4];
DataBuffer hmac;
KeyGenerator::calc_hmac(deco.buffer, blen, key, (unsigned char *)&hmacint, seq);
hmac.addData(hmacint, 4);
if (dout)
deco = deco + hmac;
else
deco = hmac + deco;
return deco;
}
void DataBuffer::clear()
{
blen = 0;
free(buffer);
buffer = (unsigned char *)malloc(1);
}
void *DataBuffer::getPtr()
{
return buffer;
}
void DataBuffer::addData(const void *ptr, int size)
{
if (ptr != NULL and size > 0) {
buffer = (unsigned char *)realloc(buffer, blen + size);
memcpy(&buffer[blen], ptr, size);
blen += size;
}
}
void DataBuffer::popData(int size)
{
if (size > blen) {
throw 0;
} else {
memmove(&buffer[0], &buffer[size], blen - size);
blen -= size;
if (blen + size > blen * 2 and blen > 8 * 1024)
buffer = (unsigned char *)realloc(buffer, blen + 1);
}
}
void DataBuffer::crunchData(int size)
{
if (size > blen) {
throw 0;
} else {
blen -= size;
}
}
int DataBuffer::getInt(int nbytes, int offset)
{
if (nbytes > blen)
throw 0;
int ret = 0;
for (int i = 0; i < nbytes; i++) {
ret <<= 8;
ret |= buffer[i + offset];
}
return ret;
}
void DataBuffer::putInt(int value, int nbytes)
{
assert(nbytes > 0);
unsigned char out[nbytes];
for (int i = 0; i < nbytes; i++) {
out[nbytes - i - 1] = (value >> (i << 3)) & 0xFF;
}
this->addData(out, nbytes);
}
int DataBuffer::readInt(int nbytes)
{
if (nbytes > blen)
throw 0;
int ret = getInt(nbytes);
popData(nbytes);
return ret;
}
int DataBuffer::readListSize()
{
if (blen == 0)
throw 0;
int ret;
if (buffer[0] == 0) {
popData(1);
ret = 0;
} else if (buffer[0] == 0xf8) {
ret = buffer[1];
popData(2);
} else if (buffer[0] == 0xf9) {
ret = getInt(2, 1);
popData(3);
} else {
/* FIXME throw 0 error */
ret = -1;
throw 0;
}
return ret;
}
void DataBuffer::writeListSize(int size)
{
if (size == 0) {
putInt(0, 1);
} else if (size < 256) {
putInt(0xf8, 1);
putInt(size, 1);
} else {
putInt(0xf9, 1);
putInt(size, 2);
}
}
std::string DataBuffer::readRawString(int size)
{
if (size < 0 or size > blen)
throw 0;
std::string st(size, ' ');
memcpy(&st[0], buffer, size);
popData(size);
return st;
}
std::string DataBuffer::readNibbleHex(char bchar) {
// read packed nibble or packed hex!
int nbyte = readInt(1);
int size = nbyte & 0x7f;
int numnibbles = size*2 - ((nbyte&0x80) ? 1 : 0);
std::string rawd = readRawString(size);
std::string s;
for (int i = 0; i < numnibbles; i++) {
char c = (rawd[i/2] >> (4-((i&1)<<2))) & 0xF;
if (c < 10) s += (c+'0');
else s += (c-10+bchar);
}
return s;
}
std::string DataBuffer::readString()
{
if (blen == 0)
throw 0;
int type = readInt(1);
switch (type) {
case 236:
case 237:
case 238:
case 239:
return getDecodedExtended(type - 236, readInt(1));
case 250: {
std::string u = readString();
std::string s = readString();
if (u.size() > 0 and s.size() > 0)
return u + "@" + s;
else if (s.size() > 0)
return s;
return "";
};
case 252: {
int slen = readInt(1);
return readRawString(slen);
};
case 253: {
// 20 bits length
int slen = readInt(3) & 0xFFFFF;
return readRawString(slen);
};
case 254: {
// 31 bits length
int slen = readInt(4) & 0x7FFFFFFF;
return readRawString(slen);
};
case 251:
case 255:
return readNibbleHex(type == 255 ? '-' : 'A');
default:
if (type < 236)
return getDecoded(type);
};
return "";
}
void DataBuffer::putRawString(std::string s)
{
if (s.size() < 256) {
putInt(0xfc, 1);
putInt(s.size(), 1);
addData(s.c_str(), s.size());
} else {
putInt(0xfd, 1);
putInt(s.size(), 3);
addData(s.c_str(), s.size());
}
}
bool DataBuffer::canbeNibbled(const std::string & s) const {
for (unsigned i = 0; i < s.size(); i++) {
if (!(
(s[i] >= '0' && s[i] <= '9') ||
(s[i] == '-') ||
(s[i] == '.')
))
return false;
}
return true;
}
bool DataBuffer::canbeHexed(const std::string & s) const {
for (unsigned i = 0; i < s.size(); i++) {
if (!(
(s[i] >= '0' && s[i] <= '9') ||
(s[i] >= 'A' && s[i] <= 'F')
))
return false;
}
return true;
}
void DataBuffer::putString(std::string s)
{
unsigned short lu = lookupDecoded(s);
int sub_dict = (lu >> 8);
if (sub_dict != 0)
putInt(sub_dict + 236 - 1, 1); // Put dict byte first!
if (lu != 0) {
putInt(lu & 0xFF, 1); // Now put second byte
} else if (s.find('@') != std::string::npos) {
std::string p1 = s.substr(0, s.find('@'));
std::string p2 = s.substr(s.find('@') + 1);
putInt(250, 1);
putString(p1);
putString(p2);
} else if (canbeNibbled(s) || canbeHexed(s)) {
// Encode it in nibbles
int numn = (s.size()+1)/2;
std::string out(numn, 0);
for (unsigned i = 0; i < s.size(); i++) {
unsigned char c;
if (s[i] >= '0' && s[i] <= '9') c = s[i]-'0';
else if (s[i] >= 'A' && s[i] <= 'F') c = s[i]-'A'+10;
else c = s[i]-'-'+10;
out[i/2] |= c << (4-4*(i&1));
}
if (s.size() % 2 != 0) {
numn |= 0x80;
out[out.size()-1] |= 0xf;
}
putInt(canbeHexed(s) ? 251 : 255,1);
putInt(numn,1);
addData(out.c_str(), out.size());
} else if (s.size() < 256) {
putInt(252, 1);
putInt(s.size(), 1);
addData(s.c_str(), s.size());
} else {
putInt(253, 1);
putInt(s.size(), 3);
addData(s.c_str(), s.size());
}
}
bool DataBuffer::isList()
{
if (blen == 0)
throw 0;
return (buffer[0] == 248 or buffer[0] == 0 or buffer[0] == 249);
}
std::vector < Tree > DataBuffer::readList(WhatsappConnection * c)
{
std::vector < Tree > l;
int size = readListSize();
while (size--) {
Tree t;
if (c->read_tree(this, t))
l.push_back(t);
}
return l;
}
std::string DataBuffer::toString()
{
std::string r(blen, ' ');
memcpy(&r[0], buffer, blen);
return r;
}