- Timestamp:
- 2006-02-12T07:26:20Z (19 years ago)
- Branches:
- master
- Children:
- f665dab
- Parents:
- a323a22 (diff), 58bc4e6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
util.c
ra323a22 r5ebe625 6 6 7 7 /* 8 * nogaim 9 * 10 * Gaim without gaim - for BitlBee 8 * Various utility functions. Some are copied from Gaim to support the 9 * IM-modules, most are from BitlBee. 11 10 * 12 11 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> 13 12 * (and possibly other members of the Gaim team) 14 * Copyright 2002-200 4 Wilmer van der Gaast <lintux@lintux.cx>13 * Copyright 2002-2005 Wilmer van der Gaast <wilmer@gaast.net> 15 14 */ 16 15 … … 32 31 */ 33 32 34 /* Parts from util.c from gaim needed by nogaim */35 33 #define BITLBEE_CORE 36 34 #include "nogaim.h" … … 38 36 #include <stdlib.h> 39 37 #include <string.h> 38 #include <ctype.h> 40 39 #include <glib.h> 41 40 #include <time.h> 42 43 char *utf8_to_str(const char *in)44 {45 int n = 0, i = 0;46 int inlen;47 char *result;48 49 if (!in)50 return NULL;51 52 inlen = strlen(in);53 54 result = g_malloc(inlen + 1);55 56 while (n <= inlen - 1) {57 long c = (long)in[n];58 if (c < 0x80)59 result[i++] = (char)c;60 else {61 if ((c & 0xC0) == 0xC0)62 result[i++] =63 (char)(((c & 0x03) << 6) | (((unsigned char)in[++n]) & 0x3F));64 else if ((c & 0xE0) == 0xE0) {65 if (n + 2 <= inlen) {66 result[i] =67 (char)(((c & 0xF) << 4) | (((unsigned char)in[++n]) & 0x3F));68 result[i] =69 (char)(((unsigned char)result[i]) |70 (((unsigned char)in[++n]) & 0x3F));71 i++;72 } else73 n += 2;74 } else if ((c & 0xF0) == 0xF0)75 n += 3;76 else if ((c & 0xF8) == 0xF8)77 n += 4;78 else if ((c & 0xFC) == 0xFC)79 n += 5;80 }81 n++;82 }83 result[i] = '\0';84 85 return result;86 }87 88 char *str_to_utf8(const char *in)89 {90 int n = 0, i = 0;91 int inlen;92 char *result = NULL;93 94 if (!in)95 return NULL;96 97 inlen = strlen(in);98 99 result = g_malloc(inlen * 2 + 1);100 101 while (n < inlen) {102 long c = (long)in[n];103 if (c == 27) {104 n += 2;105 if (in[n] == 'x')106 n++;107 if (in[n] == '3')108 n++;109 n += 2;110 continue;111 }112 /* why are we removing newlines and carriage returns?113 if ((c == 0x0D) || (c == 0x0A)) {114 n++;115 continue;116 }117 */118 if (c < 128)119 result[i++] = (char)c;120 else {121 result[i++] = (char)((c >> 6) | 192);122 result[i++] = (char)((c & 63) | 128);123 }124 n++;125 }126 result[i] = '\0';127 128 return result;129 }130 41 131 42 void strip_linefeed(gchar *text) … … 271 182 { 272 183 char code[8]; 273 char is ;184 char is[4]; 274 185 } htmlentity_t; 275 186 276 187 /* FIXME: This is ISO8859-1(5) centric, so might cause problems with other charsets. */ 277 188 278 static htmlentity_t ent[] = 279 { 280 { "lt", '<' }, 281 { "gt", '>' }, 282 { "amp", '&' }, 283 { "quot", '"' }, 284 { "aacute", 'á' }, 285 { "eacute", 'é' }, 286 { "iacute", 'é' }, 287 { "oacute", 'ó' }, 288 { "uacute", 'ú' }, 289 { "agrave", 'à' }, 290 { "egrave", 'è' }, 291 { "igrave", 'ì' }, 292 { "ograve", 'ò' }, 293 { "ugrave", 'ù' }, 294 { "acirc", 'â' }, 295 { "ecirc", 'ê' }, 296 { "icirc", 'î' }, 297 { "ocirc", 'ô' }, 298 { "ucirc", 'û' }, 299 { "nbsp", ' ' }, 300 { "", 0 } 189 static const htmlentity_t ent[] = 190 { 191 { "lt", "<" }, 192 { "gt", ">" }, 193 { "amp", "&" }, 194 { "quot", "\"" }, 195 { "aacute", "á" }, 196 { "eacute", "é" }, 197 { "iacute", "é" }, 198 { "oacute", "ó" }, 199 { "uacute", "ú" }, 200 { "agrave", "à" }, 201 { "egrave", "è" }, 202 { "igrave", "ì" }, 203 { "ograve", "ò" }, 204 { "ugrave", "ù" }, 205 { "acirc", "â" }, 206 { "ecirc", "ê" }, 207 { "icirc", "î" }, 208 { "ocirc", "ô" }, 209 { "ucirc", "û" }, 210 { "auml", "ä" }, 211 { "euml", "ë" }, 212 { "iuml", "ï" }, 213 { "ouml", "ö" }, 214 { "uuml", "ü" }, 215 { "nbsp", " " }, 216 { "", "" } 301 217 }; 302 218 … … 347 263 if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 ) 348 264 { 349 *(s++) = ent[i].is; 265 int j; 266 267 for( j = 0; ent[i].is[j]; j ++ ) 268 *(s++) = ent[i].is[j]; 269 350 270 matched = 1; 351 271 break; … … 412 332 g_string_sprintfa( str, "%s%s: %s", newline, name, value ); 413 333 } 334 335 /* Decode%20a%20file%20name */ 336 void http_decode( char *s ) 337 { 338 char *t; 339 int i, j, k; 340 341 t = g_new( char, strlen( s ) + 1 ); 342 343 for( i = j = 0; s[i]; i ++, j ++ ) 344 { 345 if( s[i] == '%' ) 346 { 347 if( sscanf( s + i + 1, "%2x", &k ) ) 348 { 349 t[j] = k; 350 i += 2; 351 } 352 else 353 { 354 *t = 0; 355 break; 356 } 357 } 358 else 359 { 360 t[j] = s[i]; 361 } 362 } 363 t[j] = 0; 364 365 strcpy( s, t ); 366 g_free( t ); 367 } 368 369 /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ 370 /* This fuction is safe, but make sure you call it safely as well! */ 371 void http_encode( char *s ) 372 { 373 char *t; 374 int i, j; 375 376 t = g_strdup( s ); 377 378 for( i = j = 0; t[i]; i ++, j ++ ) 379 { 380 /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ 381 if( !isalnum( t[i] ) ) 382 { 383 sprintf( s + j, "%%%02X", ((unsigned char*)t)[i] ); 384 j += 2; 385 } 386 else 387 { 388 s[j] = t[i]; 389 } 390 } 391 s[j] = 0; 392 393 g_free( t ); 394 } 395 396 /* Strip newlines from a string. Modifies the string passed to it. */ 397 char *strip_newlines( char *source ) 398 { 399 int i; 400 401 for( i = 0; source[i] != '\0'; i ++ ) 402 if( source[i] == '\n' || source[i] == '\r' ) 403 source[i] = ' '; 404 405 return source; 406 } 407 408 #ifdef IPV6 409 /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ 410 char *ipv6_wrap( char *src ) 411 { 412 static char dst[64]; 413 int i; 414 415 for( i = 0; src[i]; i ++ ) 416 if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) 417 break; 418 419 /* Hmm, it's not even an IP... */ 420 if( src[i] ) 421 return src; 422 423 g_snprintf( dst, sizeof( dst ), "::ffff:%s", src ); 424 425 return dst; 426 } 427 428 /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ 429 char *ipv6_unwrap( char *src ) 430 { 431 int i; 432 433 if( g_strncasecmp( src, "::ffff:", 7 ) != 0 ) 434 return src; 435 436 for( i = 7; src[i]; i ++ ) 437 if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) 438 break; 439 440 /* Hmm, it's not even an IP... */ 441 if( src[i] ) 442 return src; 443 444 return ( src + 7 ); 445 } 446 #endif
Note: See TracChangeset
for help on using the changeset viewer.