Mercurial > pidgin.yaz
comparison src/protocols/qq/utils.c @ 14049:8294485b79db
[gaim-migrate @ 16662]
Enhanced protocol testing tools, primarily by designing a more useful interface for sending customized packets.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Huetsch <markhuetsch> |
---|---|
date | Mon, 07 Aug 2006 06:17:13 +0000 |
parents | ef8490f9e823 |
children | 51f71ad82141 |
comparison
equal
deleted
inserted
replaced
14048:9c4bec886220 | 14049:8294485b79db |
---|---|
197 { | 197 { |
198 GString *stripped; | 198 GString *stripped; |
199 gchar *ret; | 199 gchar *ret; |
200 int i; | 200 int i; |
201 | 201 |
202 g_return_val_if_fail(buffer != NULL, NULL); | |
203 | |
202 stripped = g_string_new(""); | 204 stripped = g_string_new(""); |
203 for (i=0; i<strlen(buffer); i++) { | 205 for (i=0; i<strlen(buffer); i++) { |
204 if ((int) buffer[i] != 32) { | 206 if ((int) buffer[i] != 32) { |
205 g_string_append_c(stripped, buffer[i]); | 207 g_string_append_c(stripped, buffer[i]); |
206 } | 208 } |
212 } | 214 } |
213 | 215 |
214 /* Dumps an ASCII hex string to a string of bytes. The return should be freed later. | 216 /* Dumps an ASCII hex string to a string of bytes. The return should be freed later. |
215 * Returns NULL if a string with an odd number of nibbles is passed in or if buffer | 217 * Returns NULL if a string with an odd number of nibbles is passed in or if buffer |
216 * isn't a valid hex string */ | 218 * isn't a valid hex string */ |
217 guint8 *hex_str_to_bytes(const gchar *buffer) | 219 guint8 *hex_str_to_bytes(const gchar *buffer, gint *out_len) |
218 { | 220 { |
219 gchar *hex_str, *hex_buffer, *cursor, tmp; | 221 gchar *hex_str, *hex_buffer, *cursor, tmp; |
220 guint8 *bytes, nibble1, nibble2; | 222 guint8 *bytes, nibble1, nibble2; |
221 gint index, len; | 223 gint index; |
222 | 224 |
225 g_return_val_if_fail(buffer != NULL, NULL); | |
226 | |
223 hex_buffer = strstrip(buffer); | 227 hex_buffer = strstrip(buffer); |
224 | 228 |
225 if (strlen(hex_buffer) % 2 != 0) { | 229 if (strlen(hex_buffer) % 2 != 0) { |
226 gaim_debug(GAIM_DEBUG_WARNING, "QQ", | 230 gaim_debug(GAIM_DEBUG_WARNING, "QQ", |
227 "Unable to convert an odd number of nibbles to a string of bytes!\n"); | 231 "Unable to convert an odd number of nibbles to a string of bytes!\n"); |
231 bytes = g_newa(guint8, strlen(hex_buffer) / 2); | 235 bytes = g_newa(guint8, strlen(hex_buffer) / 2); |
232 hex_str = g_ascii_strdown(hex_buffer, -1); | 236 hex_str = g_ascii_strdown(hex_buffer, -1); |
233 g_free(hex_buffer); | 237 g_free(hex_buffer); |
234 index = 0; | 238 index = 0; |
235 for (cursor = hex_str; cursor < hex_str + sizeof(gchar) * (strlen(hex_str)) - 1; cursor++) { | 239 for (cursor = hex_str; cursor < hex_str + sizeof(gchar) * (strlen(hex_str)) - 1; cursor++) { |
236 if (g_ascii_isdigit(*cursor)) {tmp = *cursor; nibble1 = atoi(&tmp); } | 240 if (g_ascii_isdigit(*cursor)) { |
237 else if (g_ascii_isalpha(*cursor) && (gint) *cursor - 87 < 16) | 241 tmp = *cursor; nibble1 = atoi(&tmp); |
242 } else if (g_ascii_isalpha(*cursor) && (gint) *cursor - 87 < 16) { | |
238 nibble1 = (gint) *cursor - 87; | 243 nibble1 = (gint) *cursor - 87; |
239 else { | 244 } else { |
240 gaim_debug(GAIM_DEBUG_WARNING, "QQ", | 245 gaim_debug(GAIM_DEBUG_WARNING, "QQ", |
241 "Invalid char found in hex string!\n"); | 246 "Invalid char found in hex string!\n"); |
242 g_free(hex_str); | 247 g_free(hex_str); |
243 return NULL; | 248 return NULL; |
244 } | 249 } |
245 nibble1 = nibble1 << 4; | 250 nibble1 = nibble1 << 4; |
246 cursor++; | 251 cursor++; |
247 if (g_ascii_isdigit(*cursor)) {tmp = *cursor; nibble2 = atoi(&tmp); } | 252 if (g_ascii_isdigit(*cursor)) { |
248 else if (g_ascii_isalpha(*cursor) && (gint) (*cursor - 87) < 16) | 253 tmp = *cursor; nibble2 = atoi(&tmp); |
254 } else if (g_ascii_isalpha(*cursor) && (gint) (*cursor - 87) < 16) { | |
249 nibble2 = (gint) *cursor - 87; | 255 nibble2 = (gint) *cursor - 87; |
250 else { | 256 } else { |
257 gaim_debug(GAIM_DEBUG_WARNING, "QQ", | |
258 "Invalid char found in hex string!\n"); | |
251 g_free(hex_str); | 259 g_free(hex_str); |
252 return NULL; | 260 return NULL; |
253 } | 261 } |
254 bytes[index++] = nibble1 + nibble2; | 262 bytes[index++] = nibble1 + nibble2; |
255 } | 263 } |
256 len = strlen(hex_str) / 2; | 264 *out_len = strlen(hex_str) / 2; |
257 g_free(hex_str); | 265 g_free(hex_str); |
258 return g_memdup(bytes, len); | 266 return g_memdup(bytes, *out_len); |
259 } | 267 } |
260 | 268 |
261 /* Dumps a chunk of raw data into an ASCII hex string. The return should be freed later. */ | 269 /* Dumps a chunk of raw data into an ASCII hex string. The return should be freed later. */ |
262 gchar *hex_dump_to_str(const guint8 *buffer, gint bytes) | 270 gchar *hex_dump_to_str(const guint8 *buffer, gint bytes) |
263 { | 271 { |