/* librock/comm/person/email/base64.c librock_CHISEL _summary MIME Base 64 encoding/decoding Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************/ #ifdef librock_NOTLIBROCK /**************************************************************/ /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED int librock_base64enc(char *wptr,long *paccum,int ch); int librock_base64dec(const char *wptr,char *optr); #endif /**************************************************************/ #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_base64 #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_base64 /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_base64[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_base64enc librock_base64enc #define librock_body_base64encN librock_base64encN #define librock_body_base64dec librock_base64dec #define librock_body_base64decN librock_base64decN /**************************************************************/ #endif #ifndef librock_NOIMPL_base64enc /**************************************************************/ librock_PRIVATE char *vector = { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}; #define librock_FNTYPE_base64enc fni_S_I_i int librock_body_base64enc(char *wptr, long *paccum, int ch) {/* Copyright 1998-2001, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_base64enc /* librock_base64enc - Encode binary data as MIME base 64 (deprecated. See librock_base64encN) */ /**/ #include int librock_base64enc(char *wptr, /* Output. Must have room to store 4 bytes */ long *paccum, /* Accumulator. NOTE: Initialize to 0 or 1*/ int ch) /* Byte to encode in range 0-0xff, or -1 to pad at end */ /* This function performs MIME Base64 encoding. An accumulator is used to collect 3 bytes on successive calls, which are then encoded and written to wptr. To end the encoding, call with ch == -1, which will pad if necessary. Inserting NUL termination and newlines in the output is up to the caller. This function returns the number of characters written at pointer, either 0, or 4. To encode more than one byte, see @librock_base64encN instead. Typical use is */ #ifdef librock_TYPICAL_USE_base64enc #include const char *ptr = "Some string to encode."; long accum = 0; /* Must initialize */ char *out = malloc(strlen(ptr) * 4/3+9); /* at least 4 bytes for every 3, plus 4 bytes extra plus '\0' end*/ char *wptr = out; if (!wptr) { /* Error handling */ } while(*ptr) { wptr += librock_base64enc(wptr,&accum,*ptr & 0xff); ptr++; } wptr += librock_base64enc(wptr,&accum,-1); *wptr = '\0'; printf("%s\n",out); #endif /* // No external calls */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ if (!*paccum) { *paccum = 1; } if (ch == -1) { if (*paccum <= 1) { /* No need to pad */ return 0; } if (!(*paccum & 0x10000)) { librock_base64enc(wptr,paccum,0); librock_base64enc(wptr,paccum,0); wptr[2] = '='; } else { librock_base64enc(wptr,paccum,0); } wptr[3] = '='; return 4; } *paccum <<= 8; *paccum |= ch & 0xff; if (*paccum & 0x01000000) { wptr[3] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[2] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[1] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[0] = vector[*paccum&0x3f]; *paccum >>= 6; return 4; } return 0; } /**************************************************************/ #endif /* NOIMP section */ #ifndef librock_NOIMPL_base64encN /**************************************************************/ int librock_body_base64encN(char *wptr, long *paccum, const char *ptr, int amt) {/* Copyright 1998-2001, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_base64encN /* librock_base64encN - Encode binary data as MIME base 64 */ /**/ #include int librock_base64encN(char *wptr, /* Output. Must have room to store 4 bytes */ long *paccum, /* Accumulator. NOTE: Initialize to 0 or 1*/ const char *ptr, /* Pointer to bytes to encode */ int amt); /* Number of bytes to encode or -1 */ /* This function performs MIME Base64 encoding. An accumulator is used to collect 3 bytes on successive calls, which are then encoded and written to wptr. To end the encoding, call with amt == -1, which will pad if necessary. Inserting NUL termination and newlines in the output is up to the caller. This function returns the number of characters written at pointer, either 0, or 4. Typical use is */ #ifdef librock_TYPICAL_USE_base64encN #include const char *ptr = "Some string to encode."; long accum = 0; /* Must initialize */ char *out = malloc(strlen(ptr) * 4/3+9); /* at least 4 bytes for every 3, plus 4 bytes extra plus '\0' end*/ char *wptr = out; if (!wptr) { /* Error handling */ } wptr += librock_base64encN(wptr,&accum,ptr,strlen(ptr)); wptr += librock_base64encN(wptr,&accum,0,-1); *wptr = '\0'; printf("%s\n",out); #endif /* // No external calls */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ int ret =0; if (!*paccum) { *paccum = 1; } if (amt == -1) { if (*paccum <= 1) { /* No need to pad */ return 0; } if (!(*paccum & 0x10000)) { librock_base64enc(wptr,paccum,0); librock_base64enc(wptr,paccum,0); wptr[2] = '='; } else { librock_base64enc(wptr,paccum,0); } wptr[3] = '='; return 4; } while(amt > 0) { char ch = *ptr; *paccum <<= 8; *paccum |= ch & 0xff; if (*paccum & 0x01000000) { wptr[3] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[2] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[1] = vector[*paccum&0x3f]; *paccum >>= 6; wptr[0] = vector[*paccum&0x3f]; *paccum >>= 6; ret += 4; wptr += 4; } amt--; ptr++; } return ret; } /**************************************************************/ #endif /* NOIMP section */ #ifndef librock_NOIMPL_base64dec /**************************************************************/ #define librock_FNTYPE_base64dec fni_s_S int librock_body_base64dec(const char *wptr,char *optr) {/* Copyright 1998-2001, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_base64dec /* librock_base64dec - Decodes 4 bytes of MIME Base64 data. (Deprecated) */ /**/ #include int librock_base64dec( const char *wptr; /* Input buffer */ char *optr; /* Output buffer */ ); /* Decodes the 4 bytes at wptr, encoded as MIME Base 64. The input and output buffer pointers can be equal. Returns the number of characters stored at optr, which is normally 3, but may be less than 3 at the end of the encoded block if there was padding. To decode more than 4 bytes, see @librock_base64decN instead. Typical use is */ #ifdef librock_TYPICAL_USE_base64dec #include char *wptr = "U29tZSBlbmNvZGVkIHN0cmluZw=="; char *optr = wptr; if (strlen(wptr) % 4) { fprintf(stderr,"%s","bad input. base64 encodings are always a multiple of 4\n"); } else { char *result = wptr; /* In place */ while(*wptr) { optr += librock_base64dec(wptr,optr); wptr += 4; } *optr = '\0'; printf("%s\n",result); } #endif /* // No external calls */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ static char decodetable[128]; if (!decodetable[vector[63]]) { int ind; /* Because this is write only, and the last position is checked, this is multi-thread safe */ decodetable['='] = 0; /* Create decode table */ for(ind = 0;vector[ind];ind++) { decodetable[vector[ind]] = ind; } } /* AAAAAABB BBBBCCCC CCDDDDDD */ optr[0] = (decodetable[wptr[0] & 0x7f]<<2)|(decodetable[wptr[1] & 0x7f]>>4); if (wptr[2] == '=') { return 1; } optr[1] = (decodetable[wptr[1] & 0x7f]<<4)|(decodetable[wptr[2] & 0x7f]>>2); if (wptr[3] == '=') { return 2; } optr[2] = (decodetable[wptr[2] & 0x7f]<<6)|(decodetable[wptr[3] & 0x7f]); return 3; } /**************************************************************/ #endif /* NOIMP section */ #ifndef librock_NOIMPL_base64decN /**************************************************************/ #define librock_FNTYPE_base64decN fni_s_S int librock_body_base64decN(const char *wptr,int amt,char *optr) {/* Copyright 1998-2001, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_base64decN /* librock_base64decN - Decodes N bytes of MIME Base64 data. */ /**/ #include int librock_base64decN( const char *wptr. /* Input buffer */ int amt, /* Bytes at input buffer. Must be a multiple of 4 */ char *optr; /* Output buffer */ ); /* Decodes the @amt bytes at @wptr, encoded as MIME Base 64. The input and output buffer pointers can be equal. The amt must be a multiple of 4. Returns the number of characters stored at optr, which is normally 3, but may be less than 3 at the end of the encoded block if there was padding. Typical use is */ #ifdef librock_TYPICAL_USE_base64decN #include char *wptr = "U29tZSBlbmNvZGVkIHN0cmluZw=="; char *optr = wptr; if (strlen(wptr) % 4) { fprintf(stderr,"%s","bad input. base64 encodings are always a multiple of 4\n"); } else { char *result = wptr; /* In place */ optr += librock_base64decN(wptr,strlen(wptr),optr); *optr = '\0'; printf("%s\n",result); } #endif /* // No external calls */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ static char decodetable[128]; int ret = 0; if (!decodetable[vector[63]]) { int ind; /* Because this is write only, and the last position is checked, this is multi-thread safe */ decodetable['='] = 0; /* Create decode table */ for(ind = 0;vector[ind];ind++) { decodetable[vector[ind]] = ind; } } /* AAAAAABB BBBBCCCC CCDDDDDD */ while(amt > 0) { optr[0] = (decodetable[wptr[0] & 0x7f]<<2)|(decodetable[wptr[1] & 0x7f]>>4); if (wptr[2] == '=') { return ret+1; } optr[1] = (decodetable[wptr[1] & 0x7f]<<4)|(decodetable[wptr[2] & 0x7f]>>2); if (wptr[3] == '=') { return ret+2; } optr[2] = (decodetable[wptr[2] & 0x7f]<<6)|(decodetable[wptr[3] & 0x7f]); ret += 3; wptr += 4; optr += 3; amt -= 4; } return ret; } /**************************************************************/ #endif /* NOIMP section */ /* $Log: base64.c,v $ Revision 1.8 2002/08/01 21:13:23 forrest@mibsoftware.com rights=#1 Needed librock_PRIVATE, so #include types.c Revision 1.7 2002/08/01 20:53:13 forrest@mibsoftware.com rights=#1 Added base64decN and base64encN functions. Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.6 2002/04/19 14:58:41 forrest@mibsoftware.com rights=#1 Manual page example correction. Revision 1.5 2002/04/09 03:37:54 forrest@mibsoftware.com rights=#1 Added FNTYPEs decls. Updated LICENSE in manual. Revision 1.4 2002/02/09 16:39:39 forrest@mibsoftware.com rights=#1 Fixed LIDESC. Standardized chg log Revision 1.3 2002/01/30 16:07:45 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.2 2002/01/29 04:41:54 forrest@mibsoftware.com rights=#1 Prep for publish. Clean up spaces at eol, tabs Revision 1.1 2001/10/15 12:54:13 forrest@mibsoftware.com rights=#1 Initial commit rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */