-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgstream.h
148 lines (122 loc) · 4.66 KB
/
tgstream.h
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
#ifndef TELEGRAMSTREAM_H
#define TELEGRAMSTREAM_H
#include <QObject>
#include <QDataStream>
#include <QVariantMap>
#include <QByteArray>
#include <QVariantMap>
#include <QVariantList>
#include <QByteArray>
//TODO: inline methods
class TelegramStream : public QObject
{
Q_OBJECT
private:
QByteArray array;
public:
QDataStream stream;
explicit TelegramStream(QByteArray input = QByteArray());
void skipRawBytes(qint32 i);
void readRawBytes(QByteArray &i, qint32 count);
void writeRawBytes(QByteArray i);
QByteArray toByteArray();
void setByteOrder(QDataStream::ByteOrder order);
};
typedef TelegramStream TgStream;
typedef TelegramStream TgPacket;
typedef QVariantMap TelegramObject;
typedef QVariant TgVariant;
typedef QVariantMap TgObject;
typedef QVariantMap TgMap;
typedef QVariantList TgVector;
typedef QVariantList TgList;
typedef QVariantList TgArray;
typedef QByteArray TgInt128;
typedef QByteArray TgInt256;
typedef qint32 TgInt;
typedef qint32 TgInteger;
typedef qint64 TgLong;
typedef qint64 TgLongLong;
typedef TgVariant TgLongVariant;
typedef long double TgDouble;
typedef QString TgString;
typedef bool TgBool;
typedef QByteArray TgByteArray;
//TODO: replace void* with READ_METHOD and WRITE_METHOD respectively
typedef void (*WRITE_METHOD)(TelegramStream&, QVariant, void*);
typedef void (*READ_METHOD)(TelegramStream&, QVariant&, void*);
void readUInt8(TelegramStream& stream, QVariant &i, void* callback = 0);
void readUInt32(TelegramStream& stream, QVariant &i, void* callback = 0);
void readUInt64(TelegramStream& stream, QVariant &i, void* callback = 0);
void readInt32(TelegramStream& stream, QVariant &i, void* callback = 0);
void readInt64(TelegramStream& stream, QVariant &i, void* callback = 0);
void readDouble(TelegramStream& stream, QVariant &i, void* callback = 0);
void readBool(TelegramStream& stream, QVariant &i, void* callback = 0);
void readString(TelegramStream& stream, QVariant &i, void* callback = 0);
void readByteArray(TelegramStream& stream, QVariant &i, void* callback = 0);
void readInt128(TelegramStream& stream, QVariant &i, void* callback = 0);
void readInt256(TelegramStream& stream, QVariant &i, void* callback = 0);
void readVector(TelegramStream& stream, QVariant &i, void* callback);
void readList(TelegramStream& stream, QVariant &i, void* callback);
void writeUInt8(TelegramStream& stream, QVariant i, void* callback = 0);
void writeUInt32(TelegramStream& stream, QVariant i, void* callback = 0);
void writeUInt64(TelegramStream& stream, QVariant i, void* callback = 0);
void writeInt32(TelegramStream& stream, QVariant i, void* callback = 0);
void writeInt64(TelegramStream& stream, QVariant i, void* callback = 0);
void writeDouble(TelegramStream& stream, QVariant i, void* callback = 0);
void writeBool(TelegramStream& stream, QVariant i, void* callback = 0);
void writeString(TelegramStream& stream, QVariant i, void* callback = 0);
void writeByteArray(TelegramStream& stream, QVariant i, void* callback = 0);
void writeInt128(TelegramStream& stream, QVariant i, void* callback = 0);
void writeInt256(TelegramStream& stream, QVariant i, void* callback = 0);
void writeVector(TelegramStream& stream, QVariant i, void* callback);
void writeList(TelegramStream& stream, QVariant i, void* callback);
void readRawBytes(TelegramStream& stream, QByteArray &i, qint32 count);
void skipRawBytes(TelegramStream& stream, qint32 count);
void writeRawBytes(TelegramStream& stream, QByteArray i);
#define ID_PROPERTY(name) \
name["_"]
#define TGOBJECT(id, name) \
TgObject name; \
ID_PROPERTY(name) = id;
#define ID(name) \
ID_PROPERTY(name).toInt()
#define GETID(name) \
ID(name)
#define EMPTY(name) \
(ID(name) == 0)
#define EXISTS(name) \
(ID(name) != 0)
#define INT32_BYTES 4
#define INT64_BYTES 8
#define INT128_BYTES 16
#define INT256_BYTES 32
//TODO: use TLType
#define VECTOR_ID 481674261
#define BOOL_TRUE -1720552011
#define BOOL_FALSE -1132882121
template <WRITE_METHOD W> QByteArray tlSerialize(QVariant obj, void* callback = 0)
{
TgPacket packet;
(*W)(packet, obj, callback);
return packet.toByteArray();
}
template <READ_METHOD R> QVariant tlDeserialize(QByteArray array, void* callback = 0)
{
TgVariant obj;
TgPacket packet(array);
(*R)(packet, obj, callback);
return obj;
}
template <WRITE_METHOD W> QByteArray tlVSerialize(TgVector obj)
{
return tlSerialize<&writeVector>(obj, (void*) W);
}
template <READ_METHOD R> TgVector tlVDeserialize(QByteArray array)
{
return tlDeserialize<&readVector>(array, (void*) R).toList();
}
QByteArray readFully(QIODevice &socket, qint32 length);
QByteArray qSerialize(QVariant obj);
QVariant qDeserialize(QByteArray array);
#endif // TELEGRAMSTREAM_H