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 <stdio.h>
00005 #include <ctype.h>
00006 #include <stdlib.h>
00007 #include "libstrfunc.h"
00008 
00009 
00010 static unsigned char _sf_uc_ib[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/==";
00011 
00012 char *base64_encode(void *data, size_t size) {
00013     char *output;
00014     register char *ou;
00015     register unsigned char *p=(unsigned char *)data;
00016     register void * dte = (void*)((char*)data + size);
00017     register int nc=0;
00018 
00019     if ( data == NULL || size == 0 ) return NULL;
00020 
00021     ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5);
00022     if(!output) return NULL;
00023 
00024     while((char *)dte - (char *)p >= 3) {
00025         unsigned char x = p[0];
00026         unsigned char y = p[1];
00027         unsigned char z = p[2];
00028         ou[0] = _sf_uc_ib[ x >> 2 ];
00029         ou[1] = _sf_uc_ib[ ((x & 0x03) << 4) | (y >> 4) ];
00030         ou[2] = _sf_uc_ib[ ((y & 0x0F) << 2) | (z >> 6) ];
00031         ou[3] = _sf_uc_ib[ z & 0x3F ];
00032         p+=3;
00033         ou+=4;
00034         nc+=4;
00035         if(!(nc % 76)) *ou++='\n';
00036     };
00037     if ((char *)dte - (char *)p == 2) {
00038         *ou++ = _sf_uc_ib[ *p >> 2 ];
00039         *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
00040         *ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
00041         *ou++ = '=';
00042     } else if((char *)dte - (char *)p == 1) {
00043         *ou++ = _sf_uc_ib[ *p >> 2 ];
00044         *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
00045         *ou++ = '=';
00046         *ou++ = '=';
00047     };
00048 
00049     *ou=0;
00050 
00051     return output;
00052 };
00053 
00054 
00055 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */
00056 {
00057     char c;
00058     int diff,i;
00059 
00060     while (start < stop ) {
00061         diff = stop - start;
00062         if (diff > 16) diff = 16;
00063 
00064         fprintf(stderr, ":%08X  ",start);
00065 
00066         for (i = 0; i < diff; i++) {
00067             if( 8 == i ) fprintf( stderr, " " );
00068             fprintf(stderr, "%02X ",(unsigned char)*(hbuf+start+i));
00069         }
00070         if (ascii) {
00071             for (i = diff; i < 16; i++) fprintf(stderr, "   ");
00072             for (i = 0; i < diff; i++) {
00073                 c = *(hbuf+start+i);
00074                 fprintf(stderr, "%c", isprint(c) ? c : '.');
00075             }
00076         }
00077         fprintf(stderr, "\n");
00078         start += 16;
00079     }
00080 }

Generated on Tue Aug 5 12:06:14 2008 for 'LibPst' by  doxygen 1.3.9.1