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
|
/* Copyright (C) 2005-2007, Thorvald Natvig <thorvald@natvig.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of the Mumble Developers nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _PACKETDATASTREAM_H
#define _PACKETDATASTREAM_H
/*
* GCC doesn't yet do inter-object-file inlining, so unfortunately, this all has to be defined here.
*/
class PacketDataStream {
protected:
unsigned char *data;
quint32 maxsize;
quint32 offset;
bool ok;
public:
quint32 size() const {
return offset;
}
bool isValid() const {
return ok;
}
quint32 left() const {
return maxsize - offset;
}
void append(const quint32 v) {
if (offset < maxsize)
data[offset++] = v;
else
ok = false;
};
void append(const char *d, quint32 len) {
if (left() >= len) {
memcpy(& data[offset], d, len);
offset += len;
} else {
ok = false;
}
}
void skip(quint32 len) {
if (left() >= len)
offset += len;
else
ok = false;
}
quint64 next() {
if (offset < maxsize)
return data[offset++];
else {
ok = false;
return 0;
}
};
void rewind() {
offset = 0;
}
void truncate() {
maxsize = offset;
}
const unsigned char *dataPtr() const {
return reinterpret_cast<const unsigned char *>(& data[offset]);
}
const char *charPtr() const {
return reinterpret_cast<const char *>(& data[offset]);
}
QByteArray dataBlock(quint32 len) {
if (len <= left()) {
QByteArray a(charPtr(), len);
offset +=len;
return a;
} else {
ok = false;
return QByteArray();
}
}
protected:
void setup(unsigned char *d, int msize) {
data = d;
offset = 0;
maxsize = msize;
ok = true;
}
public:
PacketDataStream(const char *d, int msize) {
setup(const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(d)), msize);
};
PacketDataStream(char *d, int msize) {
setup(reinterpret_cast<unsigned char *>(d), msize);
};
PacketDataStream(unsigned char *d, int msize) {
setup(d, msize);
};
PacketDataStream &operator <<(const quint64 value) {
quint64 i = value;
if ((i & 0x8000000000000000LL) && (~i < 0x100000000LL)) {
// Signed number.
i = ~i;
if (i <= 0x3) {
// Shortcase for -1 to -4
append(0xFC | i);
return *this;
} else {
append(0xF8);
}
}
if (i < 0x80) {
// Need top bit clear
append(i);
} else if (i < 0x4000) {
// Need top two bits clear
append((i >> 8) | 0x80);
append(i & 0xFF);
} else if (i < 0x200000) {
// Need top three bits clear
append((i >> 16) | 0xC0);
append((i >> 8) & 0xFF);
append(i & 0xFF);
} else if (i < 0x10000000) {
// Need top four bits clear
append((i >> 24) | 0xE0);
append((i >> 16) & 0xFF);
append((i >> 8) & 0xFF);
append(i & 0xFF);
} else if (i < 0x100000000LL) {
// It's a full 32-bit integer.
append(0xF0);
append((i >> 24) & 0xFF);
append((i >> 16) & 0xFF);
append((i >> 8) & 0xFF);
append(i & 0xFF);
} else {
// It's a 64-bit value.
append(0xF4);
append((i >> 56) & 0xFF);
append((i >> 48) & 0xFF);
append((i >> 40) & 0xFF);
append((i >> 32) & 0xFF);
append((i >> 24) & 0xFF);
append((i >> 16) & 0xFF);
append((i >> 8) & 0xFF);
append(i & 0xFF);
}
return *this;
}
PacketDataStream &operator >>(quint64 &i) {
quint32 v = next();
if ((v & 0xF0) == 0xF0) {
switch (v & 0xFC) {
case 0xF0:
i=next() << 24 | next() << 16 | next() << 8 | next();
return *this;
case 0xF4:
i=next() << 56 | next() << 48 | next() << 40 | next() << 32 | next() << 24 | next() << 16 | next() << 8 | next();
return *this;
case 0xF8:
*this >> i;
i = ~i;
return *this;
case 0xFC:
i=v & 0x03;
i = ~i;
return *this;
default:
ok = false;
i = 0;
return *this;
}
}
if ((v & 0xF0) == 0xE0) {
i=(v & 0x0F) << 24 | next() << 16 | next() << 8 | next();
} else if ((v & 0xE0) == 0xC0) {
i=(v & 0x1F) << 16 | next() << 8 | next();
} else if ((v & 0xC0) == 0x80) {
i=(v & 0x3F) << 8 | next();
} else {
i=(v & 0x7F);
}
return *this;
}
PacketDataStream &operator <<(const QByteArray &a) {
*this << a.size();
append(a.constData(), a.size());
return *this;
}
PacketDataStream &operator >>(QByteArray &a) {
quint32 len;
*this >> len;
if (len > left()) {
len = left();
ok = false;
}
a = QByteArray(reinterpret_cast<const char *>(& data[offset]), len);
offset+=len;
return *this;
}
PacketDataStream &operator <<(const QString &s) {
return *this << s.toUtf8();
}
// Using the data directly instead of through qbuff avoids a copy.
PacketDataStream &operator >>(QString &s) {
quint32 len;
*this >> len;
if (len > left()) {
len = left();
ok = false;
}
s = QString::fromUtf8(reinterpret_cast<const char *>(& data[offset]), len);
offset+=len;
return *this;
}
PacketDataStream &operator <<(const bool b) {
quint32 v = b ? 1 : 0;
return *this << v;
}
PacketDataStream &operator >>(bool &b) {
quint32 v;
*this >> v;
b = v ? true : false;
return *this;
}
#define INTMAPOPERATOR(type) \
PacketDataStream &operator <<(const type v) { \
return *this << static_cast<quint64>(v); \
} \
PacketDataStream &operator >>(type &v) { \
quint64 vv; \
*this >> vv; \
v = static_cast<type>(vv); \
return *this; \
}
INTMAPOPERATOR(int);
INTMAPOPERATOR(unsigned int);
INTMAPOPERATOR(short);
INTMAPOPERATOR(unsigned short);
INTMAPOPERATOR(char);
INTMAPOPERATOR(unsigned char);
template <typename T>
PacketDataStream &operator <<(const QList<T> &l) {
*this << l.size();
for (int i=0;i < l.size();i++)
*this << l.at(i);
return *this;
}
template <typename T>
PacketDataStream &operator >>(QList<T> &l) {
l.clear();
quint32 len;
*this >> len;
if (len > left()) {
len = left();
ok = false;
}
for (quint32 i=0;i<len;i++) {
if (left() == 0) {
ok = false;
break;
}
T t;
*this >> t;
l.append(t);
}
return *this;
}
template <typename T>
PacketDataStream &operator <<(const QSet<T> &s) {
*this << s.size();
for (typename QSet<T>::const_iterator i=s.constBegin();i!=s.constEnd();++i)
*this << *i;
return *this;
}
template <typename T>
PacketDataStream &operator >>(QSet<T> &s) {
s.clear();
quint32 len;
*this >> len;
if (len > left()) {
len = left();
ok = false;
}
for (quint32 i=0;i<len;i++) {
if (left() == 0) {
ok = false;
break;
}
T t;
*this >> t;
s.insert(t);
}
return *this;
}
template <typename T,typename U>
PacketDataStream &operator <<(const QPair<T,U> &p) {
return *this << p.first << p.second;
}
template <typename T,typename U>
PacketDataStream &operator >>(QPair<T,U> &p) {
return *this >> p.first >> p.second;
}
};
#else
class PacketDataStream;
#endif
|