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