Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

libstrfunc.c

Go to the documentation of this file.
00001 
00002 /* Taken from LibStrfunc v7.3 */
00003 
00004 #include "define.h"
00005 
00006 
00007 static char base64_code_chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/==";
00008 
00009 static void base64_append(char **ou, int *line_count, char data);
00010 static void base64_append(char **ou, int *line_count, char data)
00011 {
00012     if (*line_count == 76) {
00013         *(*ou)++ = '\n';
00014         *line_count = 0;
00015     }
00016     *(*ou)++ = data;
00017     (*line_count)++;
00018 }
00019 
00020 
00021 char *pst_base64_encode(void *data, size_t size)
00022 {
00023     int line_count = 0;
00024     return pst_base64_encode_multiple(data, size, &line_count);
00025 }
00026 
00027 
00028 char *pst_base64_encode_multiple(void *data, size_t size, int *line_count)
00029 {
00030     char *output;
00031     char *ou;
00032     unsigned char *p   = (unsigned char *)data;
00033     unsigned char *dte = p + size;
00034 
00035     if (data == NULL || size == 0) return NULL;
00036 
00037     ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 6);
00038     if (!output) return NULL;
00039 
00040     while((dte-p) >= 3) {
00041         unsigned char x = p[0];
00042         unsigned char y = p[1];
00043         unsigned char z = p[2];
00044         base64_append(&ou, line_count, base64_code_chars[ x >> 2 ]);
00045         base64_append(&ou, line_count, base64_code_chars[ ((x & 0x03) << 4) | (y >> 4) ]);
00046         base64_append(&ou, line_count, base64_code_chars[ ((y & 0x0F) << 2) | (z >> 6) ]);
00047         base64_append(&ou, line_count, base64_code_chars[ z & 0x3F ]);
00048         p+=3;
00049     };
00050     if ((dte-p) == 2) {
00051         base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]);
00052         base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) | (p[1] >> 4) ]);
00053         base64_append(&ou, line_count, base64_code_chars[ ((p[1] & 0x0F) << 2) ]);
00054         base64_append(&ou, line_count, '=');
00055     } else if ((dte-p) == 1) {
00056         base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]);
00057         base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) ]);
00058         base64_append(&ou, line_count, '=');
00059         base64_append(&ou, line_count, '=');
00060     };
00061 
00062     *ou=0;
00063     return output;
00064 };
00065 
00066 

Generated on Fri Dec 11 08:46:43 2009 for 'LibPst' by  doxygen 1.3.9.1