masqmail

annotate src/base64/base64.c @ 56:f6a6f55b7b9e

added old manual from the old website it is dated May/July 2000
author meillo@marmaro.de
date Sat, 29 May 2010 21:51:13 +0200
parents 08114f7dcc23
children 589c365d90b1
rev   line source
meillo@0 1 /* base64.c, Copyright 2000 (C) Oliver Kurth,
meillo@0 2 *
meillo@0 3 * This program is free software; you can redistribute it and/or modify
meillo@0 4 * it under the terms of the GNU General Public License as published by
meillo@0 5 * the Free Software Foundation; either version 2 of the License, or
meillo@0 6 * (at your option) any later version.
meillo@10 7 *
meillo@0 8 * This program is distributed in the hope that it will be useful,
meillo@0 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 11 * GNU General Public License for more details.
meillo@0 12 *
meillo@0 13 * You should have received a copy of the GNU General Public License
meillo@0 14 * along with this program; if not, write to the Free Software
meillo@0 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
meillo@0 16 */
meillo@0 17
meillo@0 18 /*
meillo@0 19 send bugs to: kurth@innominate.de
meillo@0 20 */
meillo@0 21
meillo@0 22 /* see also RFC 1341 */
meillo@0 23
meillo@0 24 #include <glib.h>
meillo@0 25 #include <string.h>
meillo@0 26 #include "base64.h"
meillo@0 27
meillo@10 28 gchar*
meillo@10 29 base64_encode(guchar * buf, gint len)
meillo@0 30 {
meillo@10 31 guchar *outbuf, *q;
meillo@10 32 gchar enc[64];
meillo@10 33 gint i = 0, j = 0;
meillo@10 34 guint in0, in1, in2;
meillo@0 35
meillo@10 36 for (; i < 26; i++)
meillo@10 37 enc[i] = (gchar) ('A' + j++);
meillo@10 38 j = 0;
meillo@10 39 for (; i < 52; i++)
meillo@10 40 enc[i] = (gchar) ('a' + j++);
meillo@10 41 j = 0;
meillo@10 42 for (; i < 62; i++)
meillo@10 43 enc[i] = (gchar) ('0' + j++);
meillo@10 44 enc[i++] = '+';
meillo@10 45 enc[i++] = '/';
meillo@0 46
meillo@10 47 outbuf = g_malloc(((len + 3) * 8) / 6);
meillo@10 48 q = outbuf;
meillo@0 49
meillo@10 50 i = 0;
meillo@10 51 while (i < len - 2) {
meillo@10 52 in0 = buf[i++];
meillo@10 53 in1 = buf[i++];
meillo@10 54 in2 = buf[i++];
meillo@0 55
meillo@10 56 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 57 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 58 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@10 59 *(q++) = enc[in2 & 0x3f];
meillo@10 60 }
meillo@10 61 if ((len - i) == 1) {
meillo@10 62 in0 = buf[i++];
meillo@10 63 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 64 *(q++) = enc[(in0 << 4) & 0x3f];
meillo@10 65 *(q++) = '=';
meillo@10 66 *(q++) = '=';
meillo@10 67 } else if ((len - i) == 2) {
meillo@10 68 in0 = buf[i++];
meillo@10 69 in1 = buf[i++];
meillo@10 70 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 71 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 72 *(q++) = enc[(in1 << 2) & 0x3f];
meillo@10 73 *(q++) = '=';
meillo@10 74 }
meillo@10 75 *q = 0;
meillo@0 76
meillo@10 77 return outbuf;
meillo@0 78 }
meillo@0 79
meillo@10 80 gchar *base64_decode(gchar * buf, gint * size)
meillo@10 81 {
meillo@10 82 guchar *p = buf, *q;
meillo@10 83 guint in[4];
meillo@10 84 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
meillo@10 85 gchar *out = g_malloc((strlen(buf) + 3) + 1);
meillo@10 86
meillo@10 87 q = out;
meillo@10 88 *size = 0;
meillo@10 89
meillo@10 90 *q = 0;
meillo@10 91
meillo@10 92 while (*p) {
meillo@10 93 int i = 0;
meillo@10 94 while (i < 4) {
meillo@10 95 if (!*p)
meillo@10 96 break;
meillo@10 97 if ((*p >= 'A') && (*p <= 'Z'))
meillo@10 98 in[i++] = *p - 'A';
meillo@10 99 else if ((*p >= 'a') && (*p <= 'z'))
meillo@10 100 in[i++] = (*p - 'a') + 26;
meillo@10 101 else if ((*p >= '0') && (*p <= '9'))
meillo@10 102 in[i++] = (*p - '0') + 52;
meillo@10 103 else if (*p == '+')
meillo@10 104 in[i++] = 62;
meillo@10 105 else if (*p == '/')
meillo@10 106 in[i++] = 63;
meillo@10 107 else if (*p == '=') {
meillo@10 108 in[i++] = 0;
meillo@10 109 p++;
meillo@10 110 break;
meillo@10 111 } else if ((*p != '\r') && (*p != '\n')) {
meillo@10 112 p++;
meillo@10 113 break;
meillo@10 114 }
meillo@10 115 p++;
meillo@10 116 }
meillo@10 117 if ((i == 4) || (p[-1] == '=')) {
meillo@10 118 *(q++) = ((in[0] << 2) | (in[1] >> 4));
meillo@10 119 *(q++) = ((in[1] << 4) | (in[2] >> 2));
meillo@10 120 *(q++) = ((in[2] << 6) | in[3]);
meillo@10 121 if (p[-1] == '=') {
meillo@10 122 if (i == 3) {
meillo@10 123 (*size)++;
meillo@10 124 } else if (i == 4) {
meillo@10 125 (*size) += 2;
meillo@10 126 }
meillo@10 127 } else {
meillo@10 128 *size += 3;
meillo@10 129 }
meillo@10 130 }
meillo@10 131 }
meillo@10 132 return out;
meillo@10 133 }