14192
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22
|
14242
|
23 #include "cipher.h"
|
|
24 #include "limits.h"
|
14192
|
25 #include "stdlib.h"
|
|
26 #include "string.h"
|
|
27
|
|
28 #ifdef _WIN32
|
|
29 #include "win32dep.h"
|
|
30 #endif
|
|
31
|
|
32 #include "char_conv.h"
|
|
33 #include "debug.h"
|
|
34 #include "prefs.h"
|
14242
|
35 #include "qq.h"
|
14192
|
36 #include "util.h"
|
|
37 #include "utils.h"
|
|
38
|
|
39 #define QQ_NAME_FORMAT "qq-%d"
|
|
40
|
|
41 gchar *get_name_by_index_str(gchar **array, const gchar *index_str, gint amount)
|
|
42 {
|
|
43 gint index;
|
|
44
|
|
45 index = atoi(index_str);
|
|
46 if (index < 0 || index >= amount)
|
|
47 index = 0;
|
|
48
|
|
49 return array[index];
|
|
50 }
|
|
51
|
|
52 gchar *get_index_str_by_name(gchar **array, const gchar *name, gint amount)
|
|
53 {
|
|
54 gint index;
|
|
55
|
|
56 for (index = 0; index <= amount; index++)
|
|
57 if (g_ascii_strcasecmp(array[index], name) == 0)
|
|
58 break;
|
|
59
|
|
60 if (index >= amount)
|
|
61 index = 0; /* meaning no match */
|
|
62 return g_strdup_printf("%d", index);
|
|
63 }
|
|
64
|
|
65 gint qq_string_to_dec_value(const gchar *str)
|
|
66 {
|
|
67 g_return_val_if_fail(str != NULL, 0);
|
|
68 return strtol(str, NULL, 10);
|
|
69 }
|
|
70
|
|
71 /* split the given data(len) with delimit,
|
|
72 * check the number of field matches the expected_fields (<=0 means all)
|
|
73 * return gchar* array (needs to be freed by g_strfreev later), or NULL */
|
|
74 gchar **split_data(guint8 *data, gint len, const gchar *delimit, gint expected_fields)
|
|
75 {
|
|
76 guint8 *input;
|
|
77 gchar **segments;
|
|
78 gint i, j;
|
|
79
|
|
80 g_return_val_if_fail(data != NULL && len != 0 && delimit != 0, NULL);
|
|
81
|
|
82 /* as the last field would be string, but data is not ended with 0x00
|
|
83 * we have to duplicate the data and append a 0x00 at the end */
|
|
84 input = g_newa(guint8, len + 1);
|
|
85 g_memmove(input, data, len);
|
|
86 input[len] = 0x00;
|
|
87
|
|
88 segments = g_strsplit((gchar *) input, delimit, 0);
|
|
89 if (expected_fields <= 0)
|
|
90 return segments;
|
|
91
|
|
92 for (i = 0; segments[i] != NULL; i++) {;
|
|
93 }
|
|
94 if (i < expected_fields) { /* not enough fields */
|
|
95 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
96 "Invalid data, expect %d fields, found only %d, discard\n", expected_fields, i);
|
|
97 g_strfreev(segments);
|
|
98 return NULL;
|
|
99 } else if (i > expected_fields) { /* more fields, OK */
|
|
100 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
101 "Dangerous data, expect %d fields, found %d, return all\n", expected_fields, i);
|
|
102 /* free up those not used */
|
|
103 for (j = expected_fields; j < i; j++) {
|
|
104 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "field[%d] is %s\n", j, segments[j]);
|
|
105 g_free(segments[j]);
|
|
106 }
|
|
107
|
|
108 segments[expected_fields] = NULL;
|
|
109 }
|
|
110
|
|
111 return segments;
|
|
112 }
|
|
113
|
14242
|
114 /* generate a md5 key using uid and session_key */
|
|
115 guint8 *_gen_session_md5(gint uid, guint8 *session_key)
|
|
116 {
|
|
117 guint8 *src, md5_str[QQ_KEY_LENGTH];
|
|
118 GaimCipher *cipher;
|
|
119 GaimCipherContext *context;
|
|
120
|
|
121 src = g_newa(guint8, 20);
|
|
122 memcpy(src, &uid, 4);
|
|
123 memcpy(src, session_key, QQ_KEY_LENGTH);
|
|
124
|
|
125 cipher = gaim_ciphers_find_cipher("md5");
|
|
126 context = gaim_cipher_context_new(cipher, NULL);
|
|
127 gaim_cipher_context_append(context, src, 20);
|
|
128 gaim_cipher_context_digest(context, sizeof(md5_str), md5_str, NULL);
|
|
129 gaim_cipher_context_destroy(context);
|
|
130
|
|
131 return g_memdup(md5_str, QQ_KEY_LENGTH);
|
|
132 }
|
|
133
|
14192
|
134 /* given a four-byte ip data, convert it into a human readable ip string
|
|
135 * the return needs to be freed */
|
|
136 gchar *gen_ip_str(guint8 *ip)
|
|
137 {
|
|
138 gchar *ret;
|
|
139 if (ip == NULL || ip[0] == 0) {
|
|
140 ret = g_new(gchar, 1);
|
|
141 *ret = '\0';
|
|
142 return ret;
|
|
143 } else {
|
|
144 return g_strdup_printf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
|
145 }
|
|
146 }
|
|
147
|
|
148 guint8 *str_ip_gen(gchar *str) {
|
|
149 guint8 *ip = g_new(guint8, 4);
|
14236
|
150 gint a, b, c, d;
|
|
151
|
14192
|
152 sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
|
|
153 ip[0] = a;
|
|
154 ip[1] = b;
|
|
155 ip[2] = c;
|
|
156 ip[3] = d;
|
|
157 return ip;
|
|
158 }
|
|
159
|
|
160 /* return the QQ icon file name
|
|
161 * the return needs to be freed */
|
|
162 gchar *get_icon_name(gint set, gint suffix)
|
|
163 {
|
|
164 return g_strdup_printf("qq_%d-%d", set, suffix);
|
|
165 }
|
|
166
|
|
167 /* convert a QQ UID to a unique name of GAIM
|
|
168 * the return needs to be freed */
|
|
169 gchar *uid_to_gaim_name(guint32 uid)
|
|
170 {
|
|
171 return g_strdup_printf(QQ_NAME_FORMAT, uid);
|
|
172 }
|
|
173
|
|
174 /* convert GAIM name to original QQ UID */
|
14236
|
175 guint32 gaim_name_to_uid(const gchar *const name)
|
14192
|
176 {
|
|
177 gchar *p;
|
|
178
|
|
179 g_return_val_if_fail(gaim_str_has_prefix(name, QQ_NAME_PREFIX), 0);
|
|
180
|
|
181 p = g_strrstr(name, QQ_NAME_PREFIX);
|
|
182 return (p == NULL) ? 0 : strtol(p + strlen(QQ_NAME_PREFIX), NULL, 10);
|
|
183 }
|
|
184
|
|
185 /* try to dump the data as GBK */
|
14236
|
186 void try_dump_as_gbk(const guint8 *const data, gint len)
|
14192
|
187 {
|
|
188 gint i;
|
|
189 guint8 *incoming;
|
|
190 gchar *msg_utf8;
|
|
191
|
|
192 incoming = g_newa(guint8, len + 1);
|
|
193 g_memmove(incoming, data, len);
|
|
194 incoming[len] = 0x00;
|
|
195 /* GBK code:
|
|
196 * Single-byte ASCII: 0x21-0x7E
|
|
197 * GBK first byte range: 0x81-0xFE
|
|
198 * GBK second byte range: 0x40-0x7E and 0x80-0xFE */
|
|
199 for (i = 0; i < len; i++)
|
|
200 if (incoming[i] >= 0x81)
|
|
201 break;
|
|
202
|
|
203 msg_utf8 = i < len ? qq_to_utf8((gchar *) &incoming[i], QQ_CHARSET_DEFAULT) : NULL;
|
|
204
|
|
205 if (msg_utf8 != NULL) {
|
|
206 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Try extract GB msg: %s\n", msg_utf8);
|
|
207 g_free(msg_utf8);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 /* strips whitespace */
|
14236
|
212 static gchar *strstrip(const gchar *const buffer)
|
14192
|
213 {
|
|
214 GString *stripped;
|
14237
|
215 gchar *ret, cur;
|
|
216 gint i;
|
14192
|
217
|
|
218 g_return_val_if_fail(buffer != NULL, NULL);
|
|
219
|
|
220 stripped = g_string_new("");
|
|
221 for (i=0; i<strlen(buffer); i++) {
|
14237
|
222 cur = buffer[i];
|
|
223 if (cur != ' ' && cur != '\n')
|
14192
|
224 g_string_append_c(stripped, buffer[i]);
|
|
225 }
|
|
226 ret = stripped->str;
|
|
227 g_string_free(stripped, FALSE);
|
|
228
|
|
229 return ret;
|
|
230 }
|
|
231
|
14236
|
232 /* Attempts to dump an ASCII hex string to a string of bytes.
|
|
233 * The return should be freed later. */
|
|
234 guint8 *hex_str_to_bytes(const gchar *const buffer, gint *out_len)
|
14192
|
235 {
|
|
236 gchar *hex_str, *hex_buffer, *cursor, tmp;
|
|
237 guint8 *bytes, nibble1, nibble2;
|
|
238 gint index;
|
|
239
|
|
240 g_return_val_if_fail(buffer != NULL, NULL);
|
|
241
|
|
242 hex_buffer = strstrip(buffer);
|
|
243
|
|
244 if (strlen(hex_buffer) % 2 != 0) {
|
|
245 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
246 "Unable to convert an odd number of nibbles to a string of bytes!\n");
|
|
247 g_free(hex_buffer);
|
|
248 return NULL;
|
|
249 }
|
|
250 bytes = g_newa(guint8, strlen(hex_buffer) / 2);
|
|
251 hex_str = g_ascii_strdown(hex_buffer, -1);
|
|
252 g_free(hex_buffer);
|
|
253 index = 0;
|
|
254 for (cursor = hex_str; cursor < hex_str + sizeof(gchar) * (strlen(hex_str)) - 1; cursor++) {
|
|
255 if (g_ascii_isdigit(*cursor)) {
|
|
256 tmp = *cursor; nibble1 = atoi(&tmp);
|
|
257 } else if (g_ascii_isalpha(*cursor) && (gint) *cursor - 87 < 16) {
|
|
258 nibble1 = (gint) *cursor - 87;
|
|
259 } else {
|
|
260 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
14237
|
261 "Invalid char \'%c\' found in hex string!\n", *cursor);
|
14192
|
262 g_free(hex_str);
|
|
263 return NULL;
|
|
264 }
|
|
265 nibble1 = nibble1 << 4;
|
|
266 cursor++;
|
|
267 if (g_ascii_isdigit(*cursor)) {
|
|
268 tmp = *cursor; nibble2 = atoi(&tmp);
|
|
269 } else if (g_ascii_isalpha(*cursor) && (gint) (*cursor - 87) < 16) {
|
|
270 nibble2 = (gint) *cursor - 87;
|
|
271 } else {
|
|
272 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
273 "Invalid char found in hex string!\n");
|
|
274 g_free(hex_str);
|
|
275 return NULL;
|
|
276 }
|
|
277 bytes[index++] = nibble1 + nibble2;
|
|
278 }
|
|
279 *out_len = strlen(hex_str) / 2;
|
|
280 g_free(hex_str);
|
|
281 return g_memdup(bytes, *out_len);
|
|
282 }
|
|
283
|
14236
|
284 /* Dumps a chunk of raw data into an ASCII hex string.
|
|
285 * The return should be freed later. */
|
|
286 gchar *hex_dump_to_str(const guint8 *const buffer, gint bytes)
|
14192
|
287 {
|
|
288 GString *str;
|
|
289 gchar *ret;
|
|
290 gint i, j, ch;
|
|
291
|
|
292 str = g_string_new("");
|
|
293 for (i = 0; i < bytes; i += 16) {
|
|
294 /* length label */
|
|
295 g_string_append_printf(str, "%04d: ", i);
|
|
296
|
|
297 /* dump hex value */
|
|
298 for (j = 0; j < 16; j++)
|
|
299 if ((i + j) < bytes)
|
|
300 g_string_append_printf(str, " %02X", buffer[i + j]);
|
|
301 else
|
|
302 g_string_append(str, " ");
|
|
303 g_string_append(str, " ");
|
|
304
|
|
305 /* dump ascii value */
|
|
306 for (j = 0; j < 16 && (i + j) < bytes; j++) {
|
|
307 ch = buffer[i + j] & 127;
|
|
308 if (ch < ' ' || ch == 127)
|
|
309 g_string_append_c(str, '.');
|
|
310 else
|
|
311 g_string_append_c(str, ch);
|
|
312 }
|
|
313 g_string_append_c(str, '\n');
|
|
314 }
|
|
315
|
|
316 ret = str->str;
|
|
317 /* GString can be freed without freeing it character data */
|
|
318 g_string_free(str, FALSE);
|
|
319
|
|
320 return ret;
|
|
321 }
|