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
|
14319
|
162 /* convert Gaim name to original QQ UID */
|
14236
|
163 guint32 gaim_name_to_uid(const gchar *const name)
|
14192
|
164 {
|
14319
|
165 guint32 ret;
|
|
166 g_return_val_if_fail(name != NULL, 0);
|
14192
|
167
|
14319
|
168 ret = strtol(name, NULL, 10);
|
|
169 if (errno == ERANGE)
|
|
170 return 0;
|
|
171 else
|
|
172 return ret;
|
14192
|
173 }
|
|
174
|
14404
|
175 /* convert a QQ UID to a unique name of Gaim
|
|
176 * the return needs to be freed */
|
|
177 gchar *uid_to_gaim_name(guint32 uid)
|
|
178 {
|
|
179 return g_strdup_printf(QQ_NAME_FORMAT, uid);
|
|
180 }
|
|
181
|
|
182 /* convert name displayed in a chat channel to original QQ UID */
|
|
183 gchar *chat_name_to_gaim_name(const gchar *const name)
|
|
184 {
|
|
185 const gchar *tmp;
|
|
186 gchar *ret;
|
|
187
|
|
188 g_return_val_if_fail(name != NULL, NULL);
|
|
189
|
|
190 tmp = (gchar *) gaim_strcasestr(name, "(qq-");
|
|
191 ret = g_strndup(tmp + 4, strlen(name) - (tmp - name) - 4 - 1);
|
|
192
|
|
193 return ret;
|
|
194 }
|
|
195
|
14192
|
196 /* try to dump the data as GBK */
|
14236
|
197 void try_dump_as_gbk(const guint8 *const data, gint len)
|
14192
|
198 {
|
|
199 gint i;
|
|
200 guint8 *incoming;
|
|
201 gchar *msg_utf8;
|
|
202
|
|
203 incoming = g_newa(guint8, len + 1);
|
|
204 g_memmove(incoming, data, len);
|
|
205 incoming[len] = 0x00;
|
|
206 /* GBK code:
|
|
207 * Single-byte ASCII: 0x21-0x7E
|
|
208 * GBK first byte range: 0x81-0xFE
|
|
209 * GBK second byte range: 0x40-0x7E and 0x80-0xFE */
|
|
210 for (i = 0; i < len; i++)
|
|
211 if (incoming[i] >= 0x81)
|
|
212 break;
|
|
213
|
|
214 msg_utf8 = i < len ? qq_to_utf8((gchar *) &incoming[i], QQ_CHARSET_DEFAULT) : NULL;
|
|
215
|
|
216 if (msg_utf8 != NULL) {
|
|
217 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Try extract GB msg: %s\n", msg_utf8);
|
|
218 g_free(msg_utf8);
|
|
219 }
|
|
220 }
|
|
221
|
|
222 /* strips whitespace */
|
14236
|
223 static gchar *strstrip(const gchar *const buffer)
|
14192
|
224 {
|
|
225 GString *stripped;
|
14237
|
226 gchar *ret, cur;
|
|
227 gint i;
|
14192
|
228
|
|
229 g_return_val_if_fail(buffer != NULL, NULL);
|
|
230
|
|
231 stripped = g_string_new("");
|
|
232 for (i=0; i<strlen(buffer); i++) {
|
14237
|
233 cur = buffer[i];
|
|
234 if (cur != ' ' && cur != '\n')
|
14192
|
235 g_string_append_c(stripped, buffer[i]);
|
|
236 }
|
|
237 ret = stripped->str;
|
|
238 g_string_free(stripped, FALSE);
|
|
239
|
|
240 return ret;
|
|
241 }
|
|
242
|
14236
|
243 /* Attempts to dump an ASCII hex string to a string of bytes.
|
|
244 * The return should be freed later. */
|
|
245 guint8 *hex_str_to_bytes(const gchar *const buffer, gint *out_len)
|
14192
|
246 {
|
|
247 gchar *hex_str, *hex_buffer, *cursor, tmp;
|
|
248 guint8 *bytes, nibble1, nibble2;
|
|
249 gint index;
|
|
250
|
|
251 g_return_val_if_fail(buffer != NULL, NULL);
|
|
252
|
|
253 hex_buffer = strstrip(buffer);
|
|
254
|
|
255 if (strlen(hex_buffer) % 2 != 0) {
|
|
256 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
257 "Unable to convert an odd number of nibbles to a string of bytes!\n");
|
|
258 g_free(hex_buffer);
|
|
259 return NULL;
|
|
260 }
|
|
261 bytes = g_newa(guint8, strlen(hex_buffer) / 2);
|
|
262 hex_str = g_ascii_strdown(hex_buffer, -1);
|
|
263 g_free(hex_buffer);
|
|
264 index = 0;
|
|
265 for (cursor = hex_str; cursor < hex_str + sizeof(gchar) * (strlen(hex_str)) - 1; cursor++) {
|
|
266 if (g_ascii_isdigit(*cursor)) {
|
|
267 tmp = *cursor; nibble1 = atoi(&tmp);
|
|
268 } else if (g_ascii_isalpha(*cursor) && (gint) *cursor - 87 < 16) {
|
|
269 nibble1 = (gint) *cursor - 87;
|
|
270 } else {
|
|
271 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
14237
|
272 "Invalid char \'%c\' found in hex string!\n", *cursor);
|
14192
|
273 g_free(hex_str);
|
|
274 return NULL;
|
|
275 }
|
|
276 nibble1 = nibble1 << 4;
|
|
277 cursor++;
|
|
278 if (g_ascii_isdigit(*cursor)) {
|
|
279 tmp = *cursor; nibble2 = atoi(&tmp);
|
|
280 } else if (g_ascii_isalpha(*cursor) && (gint) (*cursor - 87) < 16) {
|
|
281 nibble2 = (gint) *cursor - 87;
|
|
282 } else {
|
|
283 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
284 "Invalid char found in hex string!\n");
|
|
285 g_free(hex_str);
|
|
286 return NULL;
|
|
287 }
|
|
288 bytes[index++] = nibble1 + nibble2;
|
|
289 }
|
|
290 *out_len = strlen(hex_str) / 2;
|
|
291 g_free(hex_str);
|
|
292 return g_memdup(bytes, *out_len);
|
|
293 }
|
|
294
|
14236
|
295 /* Dumps a chunk of raw data into an ASCII hex string.
|
|
296 * The return should be freed later. */
|
|
297 gchar *hex_dump_to_str(const guint8 *const buffer, gint bytes)
|
14192
|
298 {
|
|
299 GString *str;
|
|
300 gchar *ret;
|
|
301 gint i, j, ch;
|
|
302
|
|
303 str = g_string_new("");
|
|
304 for (i = 0; i < bytes; i += 16) {
|
|
305 /* length label */
|
|
306 g_string_append_printf(str, "%04d: ", i);
|
|
307
|
|
308 /* dump hex value */
|
|
309 for (j = 0; j < 16; j++)
|
|
310 if ((i + j) < bytes)
|
|
311 g_string_append_printf(str, " %02X", buffer[i + j]);
|
|
312 else
|
|
313 g_string_append(str, " ");
|
|
314 g_string_append(str, " ");
|
|
315
|
|
316 /* dump ascii value */
|
|
317 for (j = 0; j < 16 && (i + j) < bytes; j++) {
|
|
318 ch = buffer[i + j] & 127;
|
|
319 if (ch < ' ' || ch == 127)
|
|
320 g_string_append_c(str, '.');
|
|
321 else
|
|
322 g_string_append_c(str, ch);
|
|
323 }
|
|
324 g_string_append_c(str, '\n');
|
|
325 }
|
|
326
|
|
327 ret = str->str;
|
|
328 /* GString can be freed without freeing it character data */
|
|
329 g_string_free(str, FALSE);
|
|
330
|
|
331 return ret;
|
|
332 }
|
15071
|
333
|
|
334 /* convert face num from packet (0-299) to local face (1-100) */
|
|
335 gchar *face_to_icon_str(gint face)
|
|
336 {
|
|
337 gchar *icon_num_str;
|
|
338 gint icon_num = face / 3 + 1;
|
|
339 icon_num_str = g_strdup_printf("%d", icon_num);
|
|
340 return icon_num_str;
|
|
341 }
|
|
342
|
|
343 /*
|
|
344 gint face_to_icon_num(const gchar *face)
|
|
345 {
|
|
346 gchar *icon_str = face_to_icon_str(face);
|
|
347 gint icon_num = strtol(icon_str, NULL, 10);
|
|
348 g_free(icon_str);
|
|
349 return icon_num;
|
|
350 }
|
|
351 */
|