summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeReader.cpp
blob: 492dde1027a08bdd9bcb91d8dbeec614e4b0372e (plain)
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
/*
 * BinTreeNodeReader.cpp
 *
 *  Created on: 26/06/2012
 *      Author: Antonio
 */

#include "../common.h" // #TODO Remove Miranda-dependency

#include "BinTreeNodeReader.h"
#include "WAException.h"
#include "ProtocolTreeNode.h"
#include "utilities.h"

BinTreeNodeReader::BinTreeNodeReader(WAConnection *conn, ISocketConnection *connection) :
	buf(BUFFER_SIZE)
{
	this->conn = conn;
	this->rawIn = connection;
	this->readSize = 1;
	this->in = NULL;
}

BinTreeNodeReader::~BinTreeNodeReader()
{
	if (this->in != NULL)
		delete this->in;
}

ProtocolTreeNode* BinTreeNodeReader::nextTreeInternal()
{
	int b = this->in->read();
	int size = readListSize(b);
	b = this->in->read();
	if (b == 2)
		return NULL;

	std::string* tag = this->readStringAsString(b);

	if ((size == 0) || (tag == NULL))
		throw WAException("nextTree sees 0 list or null tag", WAException::CORRUPT_STREAM_EX, -1);
	int attribCount = (size - 2 + size % 2) / 2;
	std::map<string, string>* attribs = readAttributes(attribCount);
	if (size % 2 == 1) {
		ProtocolTreeNode* ret = new ProtocolTreeNode(*tag); ret->attributes = attribs;
		delete tag;
		return ret;
	}
	b = this->in->read();
	if (isListTag(b)) {
		ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, NULL, readList(b)); ret->attributes = attribs;
		delete tag;
		return ret;
	}

	ReadData* obj = this->readString(b);
	std::vector<unsigned char>* data;
	if (obj->type == STRING) {
		std::string* s = (std::string*) obj->data;
		data = new std::vector<unsigned char>(s->begin(), s->end());
		delete s;
	}
	else data = (std::vector<unsigned char>*) obj->data;

	ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, data); ret->attributes = attribs;
	delete obj;
	delete tag;
	return ret;
}

bool BinTreeNodeReader::isListTag(int b)
{
	return (b == 248) || (b == 0) || (b == 249);
}

void BinTreeNodeReader::decodeStream(int flags, int offset, int length)
{
	unsigned char *pData = (unsigned char*)&buf[0];

	if ((flags & 8) != 0) {
		if (length < 4)
			throw WAException("invalid length" + length, WAException::CORRUPT_STREAM_EX, 0);

		length -= 4;

		this->conn->inputKey.decodeMessage(pData, offset + length, 0, length);
	}
	this->rawIn->dump(pData + offset, length);

	if (this->in != NULL)
		delete this->in;
	this->in = new ByteArrayInputStream(&this->buf, offset, length);
}

std::map<string, string>* BinTreeNodeReader::readAttributes(int attribCount)
{
	std::map<string, string>* attribs = new std::map<string, string>();
	for (int i = 0; i < attribCount; i++) {
		std::string* key = readStringAsString();
		std::string* value = readStringAsString();
		(*attribs)[*key] = *value;
		delete key;
		delete value;
	}
	return attribs;
}

std::vector<ProtocolTreeNode*>* BinTreeNodeReader::readList(int token)
{
	int size = readListSize(token);
	std::vector<ProtocolTreeNode*>* list = new std::vector<ProtocolTreeNode*>(size);
	for (int i = 0; i < size; i++)
		(*list)[i] = nextTreeInternal();

	return list;
}

int BinTreeNodeReader::readListSize(int token)
{
	switch (token) {
	case 0:   return 0;
	case 248: return readInt8(this->in);
	case 249: return readInt16(this->in);
	default:
		throw new WAException("invalid list size in readListSize: token " + token, WAException::CORRUPT_STREAM_EX, 0);
	}
	return 0;
}

std::vector<ProtocolTreeNode*>* BinTreeNodeReader::readList()
{
	return readList(this->in->read());
}

ReadData* BinTreeNodeReader::readString()
{
	return readString(this->in->read());
}

ReadData* BinTreeNodeReader::readString(int token)
{
	if (token == -1)
		throw WAException("-1 token in readString", WAException::CORRUPT_STREAM_EX, -1);

	int bSize;
	ReadData *ret = new ReadData();

	if (token > 2 && token <= WAConnection::DICTIONARY_LEN) {
		if (token != WAConnection::DICTIONARY_LEN)
			ret->data = new std::string(WAConnection::dictionary[token]);
		else {
			token = readInt8(this->in);
			if (token >= WAConnection::EXTDICTIONARY_LEN)
				throw WAException("invalid token/length in getToken", WAException::CORRUPT_STREAM_EX, 0);
			
			ret->data = new std::string(WAConnection::extended_dict[token]);
		}

		ret->type = STRING;
		return ret;
	}

	switch (token) {
	case 0:
		return NULL;

	case 252:
		bSize = readInt8(this->in);
		{
			std::vector<unsigned char>* buf8 = new std::vector<unsigned char>(bSize);
			fillArray(*buf8, bSize, this->in);
			ret->type = ARRAY;
			ret->data = buf8;
		}
		return ret;

	case 253:
		bSize = readInt24(this->in);
		{
			std::vector<unsigned char>* buf24 = new std::vector<unsigned char>(bSize);
			fillArray(*buf24, bSize, this->in);
			ret->type = ARRAY;
			ret->data = buf24;
		}
		return ret;

	case 255:
		bSize = readInt8(this->in);
		{
			int size = bSize & 0x7f;
			int numnibbles = size * 2 - ((bSize & 0x80) ? 1 : 0);

			std::vector<unsigned char> tmp(size);
			fillArray(tmp, size, this->in);
			std::string s;
			for (int i = 0; i < numnibbles; i++) {
				char c = (tmp[i / 2] >> (4 - ((i & 1) << 2))) & 0xF;
				if (c < 10) s += (c + '0');
				else s += (c - 10 + '-');
			}

			ret->type = STRING;
			ret->data = new std::string(s);
		}
		return ret;

	case 250:
		std::string* user = readStringAsString();
		std::string* server = readStringAsString();
		if ((user != NULL) && (server != NULL)) {
			std::string* result = new std::string(*user + "@" + *server);
			delete user;
			delete server;
			ret->type = STRING;
			ret->data = result;
			return ret;
		}
		if (server != NULL) {
			ret->type = STRING;
			ret->data = server;
			return ret;
		}
		throw WAException("readString couldn't reconstruct jid", WAException::CORRUPT_STREAM_EX, -1);
	}
	throw WAException("readString couldn't match token" + (int)token, WAException::CORRUPT_STREAM_EX, -1);
}

std::string* BinTreeNodeReader::objectAsString(ReadData* o)
{
	if (o->type == STRING)
		return (std::string*) o->data;

	if (o->type == ARRAY) {
		std::vector<unsigned char>* v = (std::vector<unsigned char>*) o->data;
		std::string* ret = new std::string(v->begin(), v->end());
		delete v;
		return ret;
	}

	return NULL;
}

std::string* BinTreeNodeReader::readStringAsString()
{
	ReadData* o = this->readString();
	std::string* ret = this->objectAsString(o);
	delete o;
	return ret;
}

std::string* BinTreeNodeReader::readStringAsString(int token)
{
	ReadData* o = this->readString(token);
	std::string* ret = this->objectAsString(o);
	delete o;
	return ret;
}

void BinTreeNodeReader::fillArray(std::vector<unsigned char>& buff, int len, ByteArrayInputStream* in)
{
	int count = 0;
	while (count < len)
		count += in->read(buff, count, len - count);
}

void BinTreeNodeReader::fillArray(std::vector<unsigned char>& buff, int len, ISocketConnection* in)
{
	int count = 0;
	while (count < len)
		count += in->read(buff, count, len - count);
}

void BinTreeNodeReader::getTopLevelStream()
{
	int stanzaSize = readInt24(this->rawIn);
	int flags = (stanzaSize >> 20);
	stanzaSize &= 0x0FFFFF;	

	if (this->buf.size() < (size_t)stanzaSize) {
		int newsize = max((int)(this->buf.size() * 3 / 2), stanzaSize);
		this->buf.resize(newsize);
	}
	fillArray(this->buf, stanzaSize, this->rawIn);

	this->decodeStream(flags, 0, stanzaSize);
}

int BinTreeNodeReader::readInt8(ByteArrayInputStream* in)
{
	return in->read();
}

int BinTreeNodeReader::readInt16(ByteArrayInputStream* in)
{
	int intTop = in->read();
	int intBot = in->read();
	int value = (intTop << 8) + intBot;
	return value;
}

int BinTreeNodeReader::readInt24(ByteArrayInputStream* in)
{
	int int1 = in->read();
	int int2 = in->read();
	int int3 = in->read();
	int value = (int1 << 16) + (int2 << 8) + int3;

	return value;
}

ProtocolTreeNode* BinTreeNodeReader::nextTree()
{
	this->getTopLevelStream();
	return nextTreeInternal();
}

void BinTreeNodeReader::streamStart()
{
	this->getTopLevelStream();

	int tag = this->in->read();
	int size = readListSize(tag);
	tag = this->in->read();
	if (tag != 1)
		throw WAException("expecting STREAM_START in streamStart", WAException::CORRUPT_STREAM_EX, 0);

	int attribCount = (size - 2 + size % 2) / 2;
	std::map<string, string>* attributes = readAttributes(attribCount);
	delete attributes;
}

int BinTreeNodeReader::readInt8(ISocketConnection* in)
{
	return in->read();
}

int BinTreeNodeReader::readInt16(ISocketConnection* in)
{
	unsigned char data[2];
	in->read(data, 2);
	return (int(data[0]) << 8) + int(data[1]);
}

int BinTreeNodeReader::readInt24(ISocketConnection* in)
{
	unsigned char data[3];
	in->read(data, 3);
	return (int(data[0]) << 16) + (int(data[1]) << 8) + int(data[2]);
}