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
|
/*
* BinTreeNodeReader.cpp
*
* Created on: 26/06/2012
* Author: Antonio
*/
#include "BinTreeNodeReader.h"
#include "WAException.h"
#include "ProtocolTreeNode.h"
#include "utilities.h"
#include <string>
BinTreeNodeReader::BinTreeNodeReader(WAConnection* conn, ISocketConnection* connection, const char** dictionary, const int dictionarysize) {
this->conn = conn;
this->rawIn = connection;
this->tokenMap = dictionary;
this->tokenmapsize = dictionarysize;
this->readSize = 1;
this->in = NULL;
this->buf = new std::vector<unsigned char>(BUFFER_SIZE);
}
BinTreeNodeReader::~BinTreeNodeReader() {
if (this->buf != NULL)
delete this->buf;
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, attribs);
delete tag;
return ret;
}
b = this->in->read();
if (isListTag(b)) {
ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, attribs, NULL, readList(b));
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, attribs, data);
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) {
if ((flags & 8) != 0) {
if (length < 4) {
throw WAException("invalid length" + length, WAException::CORRUPT_STREAM_EX, 0);
}
offset += 4;
length -= 4;
this->conn->inputKey->decodeMessage(&(*this->buf)[0], offset - 4, 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) {
int size;
if (token == 0) {
size = 0;
}
else {
size = 0;
if (token == 248) {
size = readInt8(this->in);
}
else
{
size = 0;
if (token == 249)
size = readInt16(this->in);
else
throw new WAException("invalid list size in readListSize: token " + token, WAException::CORRUPT_STREAM_EX, 0);
}
}
return size;
}
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);
}
ReadData* ret = new ReadData();
if ((token > 4) && (token < 245)) {
ret->type = STRING;
ret->data = new std::string(getToken(token));
return ret;
}
switch(token) {
case 0:
return NULL;
case 252: {
int size8 = readInt8(this->in);
std::vector<unsigned char>* buf8 = new std::vector<unsigned char>(size8);
fillArray(*buf8, size8, this->in);
// std::string* ret = new std::string(buf8->begin(), buf8->end());
// delete buf8;
ret->type = ARRAY;
ret->data = buf8;
return ret;
}
case 253: {
int size24 = readInt24(this->in);
std::vector<unsigned char>* buf24 = new std::vector<unsigned char>(size24);
fillArray(*buf24, size24, this->in);
// std::string* ret = new std::string(buf24->begin(), buf24->end());
// delete buf24;
ret->type = ARRAY;
ret->data = buf24;
return ret;
}
case 254: {
token = (unsigned char) this->in->read();
ret->type = STRING;
ret->data = new std::string(getToken(245 + token));
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);
}
}
std::string BinTreeNodeReader::getToken(int token) {
std::string ret;
if ((token >= 0) && (token < this->tokenmapsize))
ret = std::string(this->tokenMap[token]);
if (ret.empty()) {
throw WAException("invalid token/length in getToken", WAException::CORRUPT_STREAM_EX, 0);
}
return ret;
}
void BinTreeNodeReader::getTopLevelStream() {
int stanzaSize;
int flags;
int byte = readInt8(this->rawIn);
flags = byte >> 4;
int size0 = byte & 15;
int size1 = readInt8(this->rawIn);
int size2 = readInt8(this->rawIn);
stanzaSize = (size0 << 16) + (size1 << 8) + size2;
if (this->buf->size() < (size_t) stanzaSize) {
int newsize = max((int) (this->buf->size() * 3 / 2), stanzaSize);
delete this->buf;
this->buf = new std::vector<unsigned char>(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) {
int intTop = in->read();
int intBot = in->read();
int value = (intTop << 8) + intBot;
return value;
}
int BinTreeNodeReader::readInt24(ISocketConnection* in) {
int int1 = in->read();
int int2 = in->read();
int int3 = in->read();
int value = (int1 << 16) + (int2 << 8) + int3;
return value;
}
|