Changeset 69cb623 for lib/misc.c


Ignore:
Timestamp:
2006-10-15T09:41:12Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
2529faf
Parents:
695e392 (diff), e97827b (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:

Merging with storage-xml. It seems to be working pretty well, so maybe
this way more people will test it. :-)

File:
1 moved

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    r695e392 r69cb623  
    22  * BitlBee -- An IRC to other IM-networks gateway                     *
    33  *                                                                    *
    4   * Copyright 2002-2004 Wilmer van der Gaast and others                *
     4  * Copyright 2002-2006 Wilmer van der Gaast and others                *
    55  \********************************************************************/
    66
     
    1111 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
    1212 *                          (and possibly other members of the Gaim team)
    13  * Copyright 2002-2005 Wilmer van der Gaast <wilmer@gaast.net>
     13 * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net>
    1414 */
    1515
     
    5454}
    5555
    56 char *add_cr(char *text)
    57 {
    58         char *ret = NULL;
    59         int count = 0, j;
    60         unsigned int i;
    61 
    62         if (text[0] == '\n')
    63                 count++;
    64         for (i = 1; i < strlen(text); i++)
    65                 if (text[i] == '\n' && text[i - 1] != '\r')
    66                         count++;
    67 
    68         if (count == 0)
    69                 return g_strdup(text);
    70 
    71         ret = g_malloc0(strlen(text) + count + 1);
    72 
    73         i = 0; j = 0;
    74         if (text[i] == '\n')
    75                 ret[j++] = '\r';
    76         ret[j++] = text[i++];
    77         for (; i < strlen(text); i++) {
    78                 if (text[i] == '\n' && text[i - 1] != '\r')
    79                         ret[j++] = '\r';
    80                 ret[j++] = text[i];
    81         }
    82 
    83         return ret;
    84 }
    85 
    86 static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/";
    87 
    88 /* XXX Find bug */
    89 char *tobase64(const char *text)
    90 {
    91         char *out = NULL;
    92         const char *c;
    93         unsigned int tmp = 0;
    94         int len = 0, n = 0;
    95 
    96         c = text;
    97 
    98         while (*c) {
    99                 tmp = tmp << 8;
    100                 tmp += *c;
    101                 n++;
    102 
    103                 if (n == 3) {
    104                         out = g_realloc(out, len + 4);
    105                         out[len] = alphabet[(tmp >> 18) & 0x3f];
    106                         out[len + 1] = alphabet[(tmp >> 12) & 0x3f];
    107                         out[len + 2] = alphabet[(tmp >> 6) & 0x3f];
    108                         out[len + 3] = alphabet[tmp & 0x3f];
    109                         len += 4;
    110                         tmp = 0;
    111                         n = 0;
    112                 }
    113                 c++;
    114         }
    115         switch (n) {
    116 
    117         case 2:
    118                 tmp <<= 8;
    119                 out = g_realloc(out, len + 5);
    120                 out[len] = alphabet[(tmp >> 18) & 0x3f];
    121                 out[len + 1] = alphabet[(tmp >> 12) & 0x3f];
    122                 out[len + 2] = alphabet[(tmp >> 6) & 0x3f];
    123                 out[len + 3] = '=';
    124                 out[len + 4] = 0;
    125                 break;
    126         case 1:
    127                 tmp <<= 16;
    128                 out = g_realloc(out, len + 5);
    129                 out[len] = alphabet[(tmp >> 18) & 0x3f];
    130                 out[len + 1] = alphabet[(tmp >> 12) & 0x3f];
    131                 out[len + 2] = '=';
    132                 out[len + 3] = '=';
    133                 out[len + 4] = 0;
    134                 break;
    135         case 0:
    136                 out = g_realloc(out, len + 1);
    137                 out[len] = 0;
    138                 break;
    139         }
    140         return out;
    141 }
    142 
    14356char *normalize(const char *s)
    14457{
     
    18194typedef struct htmlentity
    18295{
    183         char code[8];
    184         char is[4];
     96        char code[7];
     97        char is[3];
    18598} htmlentity_t;
    186 
    187 /* FIXME: This is ISO8859-1(5) centric, so might cause problems with other charsets. */
    18899
    189100static const htmlentity_t ent[] =
     
    479390}
    480391
    481 char *set_eval_charset( irc_t *irc, set_t *set, char *value )
    482 {
    483         GIConv cd;
    484 
    485         if ( g_strncasecmp( value, "none", 4 ) == 0 )
    486                 return( value );
    487 
    488         cd = g_iconv_open( "UTF-8", value );
    489         if( cd == (GIConv) -1 )
    490                 return( NULL );
    491 
    492         g_iconv_close( cd );
    493         return( value );
    494 }
     392/* A pretty reliable random number generator. Tries to use the /dev/random
     393   devices first, and falls back to the random number generator from libc
     394   when it fails. Opens randomizer devices with O_NONBLOCK to make sure a
     395   lack of entropy won't halt BitlBee. */
     396void random_bytes( unsigned char *buf, int count )
     397{
     398        static int use_dev = -1;
     399       
     400        /* Actually this probing code isn't really necessary, is it? */
     401        if( use_dev == -1 )
     402        {
     403                if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 )
     404                        use_dev = 1;
     405                else
     406                {
     407                        use_dev = 0;
     408                        srand( ( getpid() << 16 ) ^ time( NULL ) );
     409                }
     410        }
     411       
     412        if( use_dev )
     413        {
     414                int fd;
     415               
     416                /* At least on Linux, /dev/random can block if there's not
     417                   enough entropy. We really don't want that, so if it can't
     418                   give anything, use /dev/urandom instead. */
     419                if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 )
     420                        if( read( fd, buf, count ) == count )
     421                        {
     422                                close( fd );
     423                                return;
     424                        }
     425                close( fd );
     426               
     427                /* urandom isn't supposed to block at all, but just to be
     428                   sure. If it blocks, we'll disable use_dev and use the libc
     429                   randomizer instead. */
     430                if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 )
     431                        if( read( fd, buf, count ) == count )
     432                        {
     433                                close( fd );
     434                                return;
     435                        }
     436                close( fd );
     437               
     438                /* If /dev/random blocks once, we'll still try to use it
     439                   again next time. If /dev/urandom also fails for some
     440                   reason, stick with libc during this session. */
     441               
     442                use_dev = 0;
     443                srand( ( getpid() << 16 ) ^ time( NULL ) );
     444        }
     445       
     446        if( !use_dev )
     447        {
     448                int i;
     449               
     450                /* Possibly the LSB of rand() isn't very random on some
     451                   platforms. Seems okay on at least Linux and OSX though. */
     452                for( i = 0; i < count; i ++ )
     453                        buf[i] = rand() & 0xff;
     454        }
     455}
     456
     457int is_bool( char *value )
     458{
     459        if( *value == 0 )
     460                return 0;
     461       
     462        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
     463                return 1;
     464        if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
     465                return 1;
     466       
     467        while( *value )
     468                if( !isdigit( *value ) )
     469                        return 0;
     470                else
     471                        value ++;
     472       
     473        return 1;
     474}
     475
     476int bool2int( char *value )
     477{
     478        int i;
     479       
     480        if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) )
     481                return 1;
     482        if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) )
     483                return 0;
     484       
     485        if( sscanf( value, "%d", &i ) == 1 )
     486                return i;
     487       
     488        return 0;
     489}
Note: See TracChangeset for help on using the changeset viewer.