1 /* 2 * Copyright (c) 1995 - 1999 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 RCSID("$Id: base64.c,v 1.4 1999/12/02 16:58:45 joda Exp $"); 37 #endif 38 #include <stdlib.h> 39 #include <string.h> 40 #include "base64.h" 41 42 static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 43 44 static int pos(char c) 45 { 46 char *p; 47 for(p = base64; *p; p++) 48 if(*p == c) 49 return p - base64; 50 return -1; 51 } 52 53 int base64_encode(const void *data, int size, char **str) 54 { 55 char *s, *p; 56 int i; 57 int c; 58 const unsigned char *q; 59 60 p = s = (char*)malloc(size*4/3+4); 61 if (p == NULL) 62 return -1; 63 q = (const unsigned char*)data; 64 i=0; 65 for(i = 0; i < size;){ 66 c=q[i++]; 67 c*=256; 68 if(i < size) 69 c+=q[i]; 70 i++; 71 c*=256; 72 if(i < size) 73 c+=q[i]; 74 i++; 75 p[0]=base64[(c&0x00fc0000) >> 18]; 76 p[1]=base64[(c&0x0003f000) >> 12]; 77 p[2]=base64[(c&0x00000fc0) >> 6]; 78 p[3]=base64[(c&0x0000003f) >> 0]; 79 if(i > size) 80 p[3]='='; 81 if(i > size+1) 82 p[2]='='; 83 p+=4; 84 } 85 *p=0; 86 *str = s; 87 return strlen(s); 88 } 89 90 int base64_decode(const char *str, void *data) 91 { 92 const char *p; 93 unsigned char *q; 94 int c; 95 int x; 96 int done = 0; 97 q=(unsigned char*)data; 98 for(p=str; *p && !done; p+=4){ 99 x = pos(p[0]); 100 if(x >= 0) 101 c = x; 102 else{ 103 done = 3; 104 break; 105 } 106 c*=64; 107 108 x = pos(p[1]); 109 if(x >= 0) 110 c += x; 111 else 112 return -1; 113 c*=64; 114 115 if(p[2] == '=') 116 done++; 117 else{ 118 x = pos(p[2]); 119 if(x >= 0) 120 c += x; 121 else 122 return -1; 123 } 124 c*=64; 125 126 if(p[3] == '=') 127 done++; 128 else{ 129 if(done) 130 return -1; 131 x = pos(p[3]); 132 if(x >= 0) 133 c += x; 134 else 135 return -1; 136 } 137 if(done < 3) 138 *q++=(c&0x00ff0000)>>16; 139 140 if(done < 2) 141 *q++=(c&0x0000ff00)>>8; 142 if(done < 1) 143 *q++=(c&0x000000ff)>>0; 144 } 145 return q - (unsigned char*)data; 146 } 147