Changeset 9fae35c for util.c


Ignore:
Timestamp:
2006-01-23T23:28:13Z (18 years ago)
Author:
Jelmer Vernooij <jelmer@…>
Branches:
master
Children:
ec3e411
Parents:
7308b63 (diff), 68c7c14 (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.
Message:

Merge from Wilmer

File:
1 moved

Legend:

Unmodified
Added
Removed
  • util.c

    r7308b63 r9fae35c  
    66
    77/*
    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.
    1110 *
    1211 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
    1312 *                          (and possibly other members of the Gaim team)
    14  * Copyright 2002-2004 Wilmer van der Gaast <lintux@lintux.cx>
     13 * Copyright 2002-2005 Wilmer van der Gaast <wilmer@gaast.net>
    1514 */
    1615
     
    3231*/
    3332
    34 /* Parts from util.c from gaim needed by nogaim */
    3533#define BITLBEE_CORE
    3634#include "nogaim.h"
     
    3836#include <stdlib.h>
    3937#include <string.h>
     38#include <ctype.h>
    4039#include <glib.h>
    4140#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                                 } else
    73                                         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 }
    13041
    13142void strip_linefeed(gchar *text)
     
    271182{
    272183        char code[8];
    273         char is;
     184        char is[4];
    274185} htmlentity_t;
    275186
    276187/* FIXME: This is ISO8859-1(5) centric, so might cause problems with other charsets. */
    277188
    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  }
     189static 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        { "",        ""  }
    301217};
    302218
     
    347263                                if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 )
    348264                                {
    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                                       
    350270                                        matched = 1;
    351271                                        break;
     
    412332                g_string_sprintfa( str, "%s%s: %s", newline, name, value );
    413333}
     334
     335/* Decode%20a%20file%20name                                             */
     336void 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! */
     371void 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. */
     397char *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... */
     410char *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. :-) */
     429char *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.