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