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@10: * 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@10: gchar* meillo@10: base64_encode(guchar * buf, gint len) meillo@0: { meillo@10: guchar *outbuf, *q; meillo@10: gchar enc[64]; meillo@10: gint i = 0, j = 0; meillo@10: guint in0, in1, in2; meillo@0: meillo@10: for (; i < 26; i++) meillo@10: enc[i] = (gchar) ('A' + j++); meillo@10: j = 0; meillo@10: for (; i < 52; i++) meillo@10: enc[i] = (gchar) ('a' + j++); meillo@10: j = 0; meillo@10: for (; i < 62; i++) meillo@10: enc[i] = (gchar) ('0' + j++); meillo@10: enc[i++] = '+'; meillo@10: enc[i++] = '/'; meillo@0: meillo@10: outbuf = g_malloc(((len + 3) * 8) / 6); meillo@10: q = outbuf; meillo@0: meillo@10: i = 0; meillo@10: while (i < len - 2) { meillo@10: in0 = buf[i++]; meillo@10: in1 = buf[i++]; meillo@10: in2 = buf[i++]; meillo@0: meillo@10: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@10: *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; meillo@10: *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f]; meillo@10: *(q++) = enc[in2 & 0x3f]; meillo@10: } meillo@10: if ((len - i) == 1) { meillo@10: in0 = buf[i++]; meillo@10: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@10: *(q++) = enc[(in0 << 4) & 0x3f]; meillo@10: *(q++) = '='; meillo@10: *(q++) = '='; meillo@10: } else if ((len - i) == 2) { meillo@10: in0 = buf[i++]; meillo@10: in1 = buf[i++]; meillo@10: *(q++) = enc[(in0 >> 2) & 0x3f]; meillo@10: *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f]; meillo@10: *(q++) = enc[(in1 << 2) & 0x3f]; meillo@10: *(q++) = '='; meillo@10: } meillo@10: *q = 0; meillo@0: meillo@10: return outbuf; meillo@0: } meillo@0: meillo@10: gchar *base64_decode(gchar * buf, gint * size) meillo@10: { meillo@10: guchar *p = buf, *q; meillo@10: guint in[4]; meillo@10: /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */ meillo@10: gchar *out = g_malloc((strlen(buf) + 3) + 1); meillo@10: meillo@10: q = out; meillo@10: *size = 0; meillo@10: meillo@10: *q = 0; meillo@10: meillo@10: while (*p) { meillo@10: int i = 0; meillo@10: while (i < 4) { meillo@10: if (!*p) meillo@10: break; meillo@10: if ((*p >= 'A') && (*p <= 'Z')) meillo@10: in[i++] = *p - 'A'; meillo@10: else if ((*p >= 'a') && (*p <= 'z')) meillo@10: in[i++] = (*p - 'a') + 26; meillo@10: else if ((*p >= '0') && (*p <= '9')) meillo@10: in[i++] = (*p - '0') + 52; meillo@10: else if (*p == '+') meillo@10: in[i++] = 62; meillo@10: else if (*p == '/') meillo@10: in[i++] = 63; meillo@10: else if (*p == '=') { meillo@10: in[i++] = 0; meillo@10: p++; meillo@10: break; meillo@10: } else if ((*p != '\r') && (*p != '\n')) { meillo@10: p++; meillo@10: break; meillo@10: } meillo@10: p++; meillo@10: } meillo@10: if ((i == 4) || (p[-1] == '=')) { meillo@10: *(q++) = ((in[0] << 2) | (in[1] >> 4)); meillo@10: *(q++) = ((in[1] << 4) | (in[2] >> 2)); meillo@10: *(q++) = ((in[2] << 6) | in[3]); meillo@10: if (p[-1] == '=') { meillo@10: if (i == 3) { meillo@10: (*size)++; meillo@10: } else if (i == 4) { meillo@10: (*size) += 2; meillo@10: } meillo@10: } else { meillo@10: *size += 3; meillo@10: } meillo@10: } meillo@10: } meillo@10: return out; meillo@10: }