lib/base64.c

Go to the documentation of this file.
00001 #include "common.h"
00002 #include "base64.h"
00003 
00004 char *base64Encode(char *input, size_t inplen)
00005 /* Use base64 to encode a string.  Returns one long encoded
00006  * string which need to be freeMem'd. Note: big-endian algorithm.
00007  * For some applications you may need to break the base64 output
00008  * of this function into lines no longer than 76 chars.
00009  */
00010 {
00011 char b64[] = B64CHARS;
00012 int words = (inplen+2)/3;
00013 int remains = inplen % 3;
00014 char *result = (char *)needMem(4*words+1);
00015 size_t i=0, j=0;
00016 int word = 0;
00017 unsigned char *p = (unsigned char*) input;  
00018 /* p must be unsigned char*,  because without "unsigned",
00019 sign extend messes up last group outputted
00020 when the value of the chars following last in input
00021 happens to be char 0x80 or higher */
00022 for(i=1; i<=words; i++)
00023     {
00024     word = 0;
00025     word |= *p++;
00026     word <<= 8;
00027     word |= *p++;
00028     word <<= 8;
00029     word |= *p++;
00030     if (i==words && remains>0)
00031         {
00032         word &= 0x00FFFF00;
00033         if (remains==1)
00034             word &= 0x00FF0000;
00035         }
00036     result[j++]=b64[word >> 18 & 0x3F];
00037     result[j++]=b64[word >> 12 & 0x3F];
00038     result[j++]=b64[word >> 6 & 0x3F];
00039     result[j++]=b64[word & 0x3F];
00040     }
00041 result[j] = 0;
00042 if (remains >0) result[j-1] = '=';    
00043 if (remains==1) result[j-2] = '=';    
00044 return result;
00045 }
00046 
00047 
00048 boolean base64Validate(char *input)
00049 /* Return true if input is valid base64.
00050  * Note that the input string is changed by 
00051  * eraseWhiteSpace(). */
00052 {
00053 size_t i = 0, l = 0;
00054 char *p = input;
00055 boolean validB64 = TRUE;
00056 
00057 /* remove whitespace which is unnecessary and  */
00058 eraseWhiteSpace(input);  
00059 
00060 l = strlen(p);
00061 for(i=0;i<l;i++)
00062     {
00063     char c = ' ';
00064     if (!strchr(B64CHARS,c=*p++))
00065         {
00066         if (c != '=')
00067             {
00068             validB64 = FALSE;
00069             break;
00070             }
00071         }
00072     }
00073 if (l%4)
00074     validB64 = FALSE;
00075 return validB64;
00076 }
00077 
00078 char *base64Decode(char *input, size_t *returnSize)
00079 /* Use base64 to decode a string.  Return decoded
00080  * string which will be freeMem'd. Note: big-endian algorithm.
00081  * Call eraseWhiteSpace() and check for invalid input 
00082  * before passing in input if needed.  
00083  * Optionally set return size for use with binary data.
00084  */
00085 {
00086 static int *map=NULL;
00087 char b64[] = B64CHARS;
00088 size_t inplen = strlen(input);
00089 int words = (inplen+3)/4;
00090 char *result = (char *)needMem(3*words+1);
00091 size_t i=0, j=0;
00092 int word = 0;
00093 char *p = input;
00094 
00095 if (!map)
00096     {
00097     int i = 0;
00098     map = needMem(256*sizeof(int));
00099     for (i = 0; i < 256; ++i)
00100         {
00101         map[i]=0;
00102         }
00103     for (i = 0; i < 64; ++i)
00104         {
00105         map[(int)b64[i]]=i;
00106         }
00107     }
00108 for(i=0; i<words; i++)
00109     {
00110     word = 0;
00111     word |= map[(int)*p++];
00112     word <<= 6;
00113     word |= map[(int)*p++];
00114     word <<= 6;
00115     word |= map[(int)*p++];
00116     word <<= 6;
00117     word |= map[(int)*p++];
00118     result[j++]=word >> 16 & 0xFF;
00119     result[j++]=word >> 8 & 0xFF;
00120     result[j++]=word & 0xFF;
00121     }
00122 result[j] = 0;
00123 if (returnSize)
00124     *returnSize = j;
00125      
00126 return result;
00127 }
00128 

Generated on Tue Dec 25 18:39:30 2007 for blat by  doxygen 1.5.2