Changeset 812a413 for util.c


Ignore:
Timestamp:
2006-06-23T18:15:28Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
df1694b
Parents:
00ab350
Message:

Added saner base64 encoding function (actually, moved the one from libyahoo2.c
to core, with some changes), which I need for the XML format password garbling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r00ab350 r812a413  
    8484}
    8585
    86 static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/";
    87 
    88 /* XXX Find bug */
    8986char *tobase64(const char *text)
    9087{
    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         }
     88        char *out;
     89        int len;
     90       
     91        len = strlen(text);
     92        out = g_malloc((len + 2)    /* the == padding */
     93                            / 3     /* every 3-byte block */
     94                            * 4     /* becomes a 4-byte one */
     95                            + 1);   /* and of course, ASCIIZ! */
     96       
     97        base64_encode_real(text, len, out, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
     98       
    14099        return out;
     100}
     101
     102void base64_encode_real(const unsigned char *in, int inlen, unsigned char *out, char *b64digits)
     103{
     104        for (; inlen >= 3; inlen -= 3)
     105        {
     106                *out++ = b64digits[in[0] >> 2];
     107                *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)];
     108                *out++ = b64digits[((in[1]<<2) & 0x3c) | (in[2]>>6)];
     109                *out++ = b64digits[in[2] & 0x3f];
     110                in += 3;
     111        }
     112        if (inlen > 0)
     113        {
     114                *out++ = b64digits[in[0] >> 2];
     115                if (inlen > 1)
     116                {
     117                        *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)];
     118                        *out++ = b64digits[((in[1]<<2) & 0x3c)];
     119                }
     120                else
     121                {
     122                        *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)];
     123                        *out++ = b64digits[64];
     124                }
     125                *out++ = b64digits[64];
     126        }
     127        *out = '\0';
    141128}
    142129
Note: See TracChangeset for help on using the changeset viewer.