-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.c
322 lines (303 loc) · 9.07 KB
/
package.c
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
/*
* This file is part of mrim-prpl.
*
* mrim-prpl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* mrim-prpl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mrim-prpl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mrim.h"
#include "package.h"
#include <stdarg.h>
MrimPackage *mrim_package_new(guint32 seq, guint32 type) {
MrimPackage *pack = g_new0(MrimPackage, 1);
pack->header = g_new0(mrim_packet_header_t, 1);
pack->header->magic = CS_MAGIC;
pack->header->proto = PROTO_VERSION;
pack->header->seq = seq;
pack->header->msg = type;
return pack;
}
void mrim_package_free(MrimPackage *pack) {
if (pack){
g_free(pack->header);
g_free(pack->data);
g_free(pack);
}
else
purple_debug_error("mrim-prpl", "[%s] pack == NULL\n", __func__);
}
gboolean mrim_package_send(MrimPackage *pack, MrimData *mrim) {
pack->header->dlen = pack->data_size;
gsize ret, size;
if (pack->data) {
size = sizeof(mrim_packet_header_t) + pack->data_size;
gchar *buffer = g_new(gchar, size);
g_memmove(buffer, pack->header, sizeof(mrim_packet_header_t));
g_memmove(buffer + sizeof(mrim_packet_header_t), pack->data, pack->data_size);
ret = send(mrim->fd, buffer, size, 0);
g_free(buffer);
} else {
size = sizeof(mrim_packet_header_t);
ret = send(mrim->fd, pack->header, size, 0);
}
purple_debug_info("mrim-prpl", "[%s] Package sent: type is 0x%x, seq is %i\n", __func__,
pack->header->msg, pack->header->seq);
mrim_package_free(pack);
return (ret >= size);
}
static mrim_packet_header_t *read_header(MrimData *mrim)
{
mrim_packet_header_t *header = g_new0(mrim_packet_header_t, 1);
gsize ret = recv(mrim->fd, header, sizeof(mrim_packet_header_t), 0);
if (ret < sizeof(mrim_packet_header_t)) {
g_free(header);
purple_debug_info("mrim-prpl", "[%s] Package header len is %d instead of %d\n", __func__, ret, sizeof(mrim_packet_header_t));
return NULL;
}
if (header->magic == CS_MAGIC) {
return header;
} else {
purple_debug_info("mrim-prpl", "[%s] Package header MAGIC is 0x%x instead of 0x%x\n", __func__, header->magic, CS_MAGIC);
g_free(header);
return NULL;
}
}
MrimPackage *mrim_package_read(MrimData *mrim) {
ssize_t ret = 0;
if (mrim->inp_package) {
MrimPackage *pack = mrim->inp_package;
gsize size = pack->data_size - pack->cur;
ret = recv(mrim->fd, pack->data + pack->cur, size, 0);
if (ret > 0) {
if (ret < size) {
pack->cur += ret;
return NULL;
} else {
pack->cur = 0;
mrim->inp_package = NULL;
return pack;
}
}
} else {
MrimPackage *pack = g_new0(MrimPackage, 1);
pack->header = read_header(mrim);
if (pack->header == NULL) {
g_free(pack);
return NULL;
}
purple_debug_info("mrim-prpl", "[%s] seq = %u, type = 0x%x len = %u\n", __func__, pack->header->seq, pack->header->msg, pack->header->dlen);
pack->data_size = pack->header->dlen;
pack->data = g_new0(char, pack->data_size);
pack->cur = 0;
if (pack->data_size)
{
ret = recv(mrim->fd, pack->data, pack->data_size, 0);
if ((ret < (pack->data_size)) && (ret > 0)) {
pack->cur += ret;
mrim->inp_package = pack;
return NULL;
}
if (ret == pack->data_size) {
return pack;
}
}
else
return pack;
}
if (ret < 0) {
if (mrim->gc)
purple_connection_error(mrim->gc, _("Read Error!") );
return NULL;
}
if (ret == 0) {
if (mrim->gc)
purple_connection_error(mrim->gc, _("Peer closed connection"));
return NULL;
}
return NULL;
}
/* Package parsing */
gboolean mrim_package_read_raw(MrimPackage *pack, gpointer buffer, gsize size) {
if (pack && pack->data) {
if (pack->cur + size <= pack->data_size) {
g_memmove(buffer, pack->data + pack->cur, size);
pack->cur += size;
return TRUE;
} else {
purple_debug_error("mrim-prpl", "[%s] Insufficient data in the buffer\n", __func__);
return FALSE;
}
} else {
purple_debug_info("mrim-prpl", "[%s] Null buffer!\n", __func__);
return FALSE;
}
}
guint32 mrim_package_read_UL(MrimPackage *pack) {
guint32 result;
if (mrim_package_read_raw(pack, &result, sizeof(result))) {
return result;
} else {
return 0;
}
}
gchar *mrim_package_read_LPSA(MrimPackage *pack) {
gsize str_len = mrim_package_read_UL(pack);
if (str_len) {
gchar *str = g_new(gchar, str_len + 1);
mrim_package_read_raw(pack, str, str_len);
str[str_len] = '\0';
gchar *string = g_convert(str, -1, "UTF8" , "CP1251", NULL, NULL, NULL);
g_free(str);
return string;
} else {
return NULL;
}
}
gchar *mrim_package_read_LPSW(MrimPackage *pack) {
gsize str_len = mrim_package_read_UL(pack);
if (str_len) {
gunichar2 *str = g_new(gunichar2, str_len / sizeof(gunichar2) + 1);
mrim_package_read_raw(pack, str, str_len);
str[str_len / sizeof(gunichar2)] = '\0';
gchar *string = g_utf16_to_utf8(str, -1, NULL, NULL, NULL);
g_free(str);
return string;
} else {
return NULL;
}
}
gchar *mrim_package_read_UIDL(MrimPackage *pack) {
gchar *uidl = g_new(gchar, 8);
mrim_package_read_raw(pack, uidl, 8);
return uidl;
}
gchar *mrim_package_read_LPS(MrimPackage *pack) {
gsize str_len = mrim_package_read_UL(pack);
if (str_len) {
gpointer str = g_new(gchar, str_len);
mrim_package_read_raw(pack, str, str_len);
gboolean valid_utf16 = TRUE;
{
gunichar2 *string = str;
glong i;
for (i = 0; i < (str_len / 2); i++) {
gunichar ch = string[i];
purple_debug_info("mrim-prpl", "[%s] Is char 0x%x defined??\n", __func__, ch);
if ((!g_unichar_isdefined(ch)) || (ch >= 0xE000) && (ch < 0xF900)) {
valid_utf16 = FALSE;
break;
}
}
}
gchar *string;
if (valid_utf16) {
string = g_utf16_to_utf8(str, str_len / 2, NULL, NULL, NULL);
} else {
string = g_convert(str, str_len, "UTF8" , "CP1251", NULL, NULL, NULL);
}
g_free(str);
return string;
} else {
return NULL;
}
}
/* Package creating */
void mrim_package_add_raw(MrimPackage *pack, gchar *data, gsize data_size) {
if (pack) {
if (pack->data) {
pack->data = g_realloc(pack->data, pack->data_size + data_size);
g_memmove(pack->data + pack->data_size, data, data_size);
pack->data_size += data_size;
} else {
pack->data = g_memdup(data, data_size);
pack->data_size = data_size;
}
}
}
void mrim_package_add_UL(MrimPackage *pack, guint32 value) {
mrim_package_add_raw(pack, (gchar*)&value, sizeof(value));
}
void mrim_package_add_LPSA(MrimPackage *pack, gchar *string) {
gsize str_len;
gchar *str = g_convert_with_fallback(string, -1, "CP1251" , "UTF8", NULL, NULL, &str_len, NULL);
if (str) {
mrim_package_add_UL(pack, str_len);
mrim_package_add_raw(pack, str, str_len);
g_free(str);
} else {
mrim_package_add_UL(pack, 0);
}
}
void mrim_package_add_LPSW(MrimPackage *pack, gchar *string) {
glong str_len;
gunichar2 *str = g_utf8_to_utf16(string, -1, NULL, &str_len, NULL);
if (str) {
mrim_package_add_UL(pack, str_len * sizeof(gunichar2));
mrim_package_add_raw(pack, (gchar*)str, str_len * sizeof(gunichar2));
g_free(str);
} else {
mrim_package_add_UL(pack, 0);
}
}
void mrim_package_add_UIDL(MrimPackage *pack, gchar *uidl) {
mrim_package_add_raw(pack, uidl, 8);
}
void mrim_package_add_base64(MrimPackage *pack, gchar *fmt, ...) {
gchar *buffer = NULL;
gsize buffer_size = 0;
va_list ap;
va_start(ap, fmt);
while (*fmt) {
switch (*fmt) {
case 'u':
{
guint32 value = va_arg(ap, guint32);
buffer = g_realloc(buffer, buffer_size + sizeof(guint32));
g_memmove(buffer + buffer_size, &value, sizeof(guint32));
buffer_size += sizeof(guint32);
}
break;
case 's': //CP1251
{
gchar *string = va_arg(ap, gchar*);
gsize str_len = g_utf8_strlen(string, -1);
buffer = g_realloc(buffer, buffer_size + sizeof(guint32) + str_len);
g_memmove(buffer + buffer_size, &str_len, sizeof(guint32));
gchar *str = g_convert_with_fallback(string, -1, "CP1251" , "UTF8", NULL, NULL, NULL, NULL);
g_memmove(buffer + buffer_size + sizeof(guint32), str, str_len);
g_free(str);
buffer_size += sizeof(guint32) + str_len;
}
break;
case 'w': //UTF16
{
gchar *string = va_arg(ap, gchar*);
gsize str_len = g_utf8_strlen(string, -1) * sizeof(gunichar2);
buffer = g_realloc(buffer, buffer_size + sizeof(guint32) + str_len);
g_memmove(buffer + buffer_size, &str_len, sizeof(guint32));
gunichar2 *str = g_utf8_to_utf16(string, -1, NULL, NULL, NULL);
g_memmove(buffer + buffer_size + sizeof(guint32), str, str_len);
g_free(str);
buffer_size += sizeof(guint32) + str_len;
}
break;
}
fmt++;
}
va_end(ap);
gchar *encoded = purple_base64_encode((gchar*)buffer, buffer_size);
guint32 encoded_len = strlen(encoded);
mrim_package_add_UL(pack, encoded_len);
mrim_package_add_raw(pack, encoded, encoded_len);
g_free(encoded);
g_free(buffer);
}