summaryrefslogtreecommitdiff
path: root/protocols/WhatsAppWeb/src/utils.h
blob: c05dbd541023f76718db9b3262cb7d638f162d3a (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
/*

WhatsAppWeb plugin for Miranda NG
Copyright © 2019-22 George Hazan

*/

#define DICT_VERSION 2

#define LIST_EMPTY   0
#define STREAM_END   2
#define DICTIONARY_0 236
#define DICTIONARY_1 237
#define DICTIONARY_2 238
#define DICTIONARY_3 239
#define LIST_8       248
#define LIST_16      249
#define JID_PAIR     250
#define HEX_8        251
#define BINARY_8     252
#define BINARY_20    253
#define BINARY_32    254
#define NIBBLE_8     255

class WANode // kinda XML
{
	friend class WAWriter;

	struct Attr
	{
		Attr(const char *pszName, const char *pszValue) :
			name(pszName),
			value(pszValue)
		{}

		CMStringA name, value;
	};

	std::list<Attr*> attrs;

public:
	WANode();
	~WANode();

	void addAttr(const char *pszName, const char *pszValue);
	CMStringA getAttr(const char *pszFieldValue) const;

	void print(CMStringA &dest, int level = 0) const;

	CMStringA title;
	MBinBuffer content;
	std::list<WANode*> children;
};

class WAReader
{
	const BYTE *m_buf, *m_limit;

	uint32_t readIntN(int i);
	CMStringA readStringFromChars(int size);

	__forceinline uint32_t readInt8() { return readIntN(1); }
	__forceinline uint32_t readInt16() { return readIntN(2); }
	__forceinline uint32_t readInt32() { return readIntN(4); }

	bool      readAttributes(WANode *node, int count);
	uint32_t  readInt20();
	bool      readList(WANode *pParent, int tag);
	int       readListSize(int tag);
	CMStringA readPacked(int tag);
	CMStringA readString(int tag);

public:
	WAReader(const void *buf, size_t cbLen) :
		m_buf((BYTE*)buf),
		m_limit((BYTE*)buf + cbLen)
	{}

	WANode* readNode();
};

class WAWriter
{
	__forceinline void writeInt8(int value) { writeIntN(value, 1); }
	__forceinline void writeInt16(int value) { writeIntN(value, 2); }
	__forceinline void writeInt32(int value) { writeIntN(value, 4); }

	void writeByte(uint8_t b);
	void writeIntN(int value, int i);
	void writeInt20(int value);
	void writeLength(int value);
	void writeListSize(int tag);
	void writePacked(const CMStringA &str);
	void writeString(const char *str, bool bRaw = false);
	bool writeToken(const char *str);

public:
	void writeNode(const WANode *pNode);

	MBinBuffer body;
};

/////////////////////////////////////////////////////////////////////////////////////////

std::string encodeBigEndian(uint32_t num, size_t len = sizeof(uint32_t));
void generateIV(uint8_t *iv, int &pVar);