meillo@0: /* base64.c, Copyright 2000 (C) Oliver Kurth, meillo@0: * meillo@0: * This program is free software; you can redistribute it and/or modify meillo@0: * it under the terms of the GNU General Public License as published by meillo@0: * the Free Software Foundation; either version 2 of the License, or meillo@0: * (at your option) any later version. meillo@0: * meillo@0: * This program is distributed in the hope that it will be useful, meillo@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: * GNU General Public License for more details. meillo@0: * meillo@0: * You should have received a copy of the GNU General Public License meillo@0: * along with this program; if not, write to the Free Software meillo@0: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. meillo@0: */ meillo@0: meillo@0: /* meillo@0: send bugs to: kurth@innominate.de meillo@0: */ meillo@0: meillo@0: /* see also RFC 1341 */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include "base64.h" meillo@0: meillo@0: gchar *base64_encode(guchar *buf, gint len) meillo@0: { meillo@0: guchar *outbuf, *q; meillo@0: gchar enc[64]; meillo@0: gint i = 0, j = 0; meillo@0: guint in0, in1, in2; meillo@0: meillo@0: for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0; meillo@0: for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0; meillo@0: for(; i < 62; i++) enc[i] = (gchar)('0' + j++); meillo@0: enc[i++] = '+'; meillo@0: enc[i++] = '/'; meillo@0: meillo@0: outbuf = g_malloc(((len+3) * 8)/6); meillo@0: q = outbuf; meillo@0: meillo@0: i = 0; meillo@0: while(i < len-2){ meillo@0: in0 = buf[i++]; meillo@0: in1 = buf[i++]; meillo@0: in2 = buf[i++]; meillo@0: meillo@0: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@0: *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; meillo@0: *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f]; meillo@0: *(q++) = enc[in2 & 0x3f]; meillo@0: } meillo@0: if((len - i) == 1){ meillo@0: in0 = buf[i++]; meillo@0: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@0: *(q++) = enc[(in0 << 4) & 0x3f]; meillo@0: *(q++) = '='; meillo@0: *(q++) = '='; meillo@0: }else if((len - i) == 2){ meillo@0: in0 = buf[i++]; meillo@0: in1 = buf[i++]; meillo@0: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@0: *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; meillo@0: *(q++) = enc[(in1 << 2) & 0x3f]; meillo@0: *(q++) = '='; meillo@0: } meillo@0: *q = 0; meillo@0: meillo@0: return outbuf; meillo@0: } meillo@0: meillo@0: gchar *base64_decode(gchar *buf, gint *size) meillo@0: { meillo@0: guchar *p = buf, *q; meillo@0: guint in[4]; meillo@0: // gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); meillo@0: gchar *out = g_malloc((strlen(buf)+3) + 1); meillo@0: meillo@0: q = out; meillo@0: *size = 0; meillo@0: meillo@0: *q = 0; meillo@0: meillo@0: while(*p){ meillo@0: int i = 0; meillo@0: while(i < 4){ meillo@0: if(!*p) break; meillo@0: if((*p >= 'A') && (*p <= 'Z')) meillo@0: in[i++] = *p - 'A'; meillo@0: else if((*p >= 'a') && (*p <= 'z')) meillo@0: in[i++] = (*p - 'a') + 26; meillo@0: else if((*p >= '0') && (*p <= '9')) meillo@0: in[i++] = (*p - '0') + 52; meillo@0: else if(*p == '+') meillo@0: in[i++] = 62; meillo@0: else if(*p == '/') meillo@0: in[i++] = 63; meillo@0: else if(*p == '='){ meillo@0: in[i++] = 0; meillo@0: p++; meillo@0: break; meillo@0: }else if((*p != '\r') && (*p != '\n')){ meillo@0: p++; meillo@0: break; meillo@0: } meillo@0: p++; meillo@0: } meillo@0: if((i == 4) || (p[-1] == '=')){ meillo@0: *(q++) = ((in[0] << 2) | (in[1] >> 4)); meillo@0: *(q++) = ((in[1] << 4) | (in[2] >> 2)); meillo@0: *(q++) = ((in[2] << 6) | in[3]); meillo@0: if(p[-1] == '='){ meillo@0: if(i == 3){ meillo@0: (*size)++; meillo@0: } meillo@0: else if(i == 4){ meillo@0: (*size) += 2; meillo@0: } meillo@0: }else{ meillo@0: *size += 3; meillo@0: } meillo@0: } meillo@0: } meillo@0: return out; meillo@0: } meillo@0: