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