Changeset 7ed3199 for lib/misc.c


Ignore:
Timestamp:
2006-06-25T14:07:01Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
6e1fed7
Parents:
df1694b
Message:

Moved Base64-related functions to a separate file and added decode funtions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    rdf1694b r7ed3199  
    8282
    8383        return ret;
    84 }
    85 
    86 char *tobase64(const char *text)
    87 {
    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        
    99         return out;
    100 }
    101 
    102 void 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';
    12884}
    12985
Note: See TracChangeset for help on using the changeset viewer.