masqmail

annotate src/base64/base64.c @ 321:412385b57dc4

refactoring
author meillo@marmaro.de
date Thu, 28 Apr 2011 16:48:23 +0200
parents 589c365d90b1
children 41958685480d
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 /* see also RFC 1341 */
meillo@0 19
meillo@0 20 #include <glib.h>
meillo@0 21 #include <string.h>
meillo@0 22 #include "base64.h"
meillo@0 23
meillo@10 24 gchar*
meillo@10 25 base64_encode(guchar * buf, gint len)
meillo@0 26 {
meillo@10 27 guchar *outbuf, *q;
meillo@10 28 gchar enc[64];
meillo@10 29 gint i = 0, j = 0;
meillo@10 30 guint in0, in1, in2;
meillo@0 31
meillo@10 32 for (; i < 26; i++)
meillo@10 33 enc[i] = (gchar) ('A' + j++);
meillo@10 34 j = 0;
meillo@10 35 for (; i < 52; i++)
meillo@10 36 enc[i] = (gchar) ('a' + j++);
meillo@10 37 j = 0;
meillo@10 38 for (; i < 62; i++)
meillo@10 39 enc[i] = (gchar) ('0' + j++);
meillo@10 40 enc[i++] = '+';
meillo@10 41 enc[i++] = '/';
meillo@0 42
meillo@312 43 outbuf = g_malloc(((len + 3) * 8) / 6 +1);
meillo@312 44 memset(outbuf, 0, ((len + 3) * 8) / 6 +1);
meillo@10 45 q = outbuf;
meillo@0 46
meillo@10 47 i = 0;
meillo@10 48 while (i < len - 2) {
meillo@10 49 in0 = buf[i++];
meillo@10 50 in1 = buf[i++];
meillo@10 51 in2 = buf[i++];
meillo@0 52
meillo@10 53 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 54 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 55 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@10 56 *(q++) = enc[in2 & 0x3f];
meillo@10 57 }
meillo@10 58 if ((len - i) == 1) {
meillo@10 59 in0 = buf[i++];
meillo@10 60 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 61 *(q++) = enc[(in0 << 4) & 0x3f];
meillo@10 62 *(q++) = '=';
meillo@10 63 *(q++) = '=';
meillo@10 64 } else if ((len - i) == 2) {
meillo@10 65 in0 = buf[i++];
meillo@10 66 in1 = buf[i++];
meillo@10 67 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 68 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 69 *(q++) = enc[(in1 << 2) & 0x3f];
meillo@10 70 *(q++) = '=';
meillo@10 71 }
meillo@10 72 *q = 0;
meillo@0 73
meillo@10 74 return outbuf;
meillo@0 75 }
meillo@0 76
meillo@10 77 gchar *base64_decode(gchar * buf, gint * size)
meillo@10 78 {
meillo@10 79 guchar *p = buf, *q;
meillo@10 80 guint in[4];
meillo@10 81 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
meillo@312 82 gchar *out = g_malloc((strlen(buf) + 3) + 1 +1);
meillo@312 83 memset(out, 0, (strlen(buf) + 3) + 1 +1);
meillo@10 84
meillo@10 85 q = out;
meillo@10 86 *size = 0;
meillo@10 87
meillo@10 88 *q = 0;
meillo@10 89
meillo@10 90 while (*p) {
meillo@10 91 int i = 0;
meillo@10 92 while (i < 4) {
meillo@10 93 if (!*p)
meillo@10 94 break;
meillo@10 95 if ((*p >= 'A') && (*p <= 'Z'))
meillo@10 96 in[i++] = *p - 'A';
meillo@10 97 else if ((*p >= 'a') && (*p <= 'z'))
meillo@10 98 in[i++] = (*p - 'a') + 26;
meillo@10 99 else if ((*p >= '0') && (*p <= '9'))
meillo@10 100 in[i++] = (*p - '0') + 52;
meillo@10 101 else if (*p == '+')
meillo@10 102 in[i++] = 62;
meillo@10 103 else if (*p == '/')
meillo@10 104 in[i++] = 63;
meillo@10 105 else if (*p == '=') {
meillo@10 106 in[i++] = 0;
meillo@10 107 p++;
meillo@10 108 break;
meillo@10 109 } else if ((*p != '\r') && (*p != '\n')) {
meillo@10 110 p++;
meillo@10 111 break;
meillo@10 112 }
meillo@10 113 p++;
meillo@10 114 }
meillo@10 115 if ((i == 4) || (p[-1] == '=')) {
meillo@10 116 *(q++) = ((in[0] << 2) | (in[1] >> 4));
meillo@10 117 *(q++) = ((in[1] << 4) | (in[2] >> 2));
meillo@10 118 *(q++) = ((in[2] << 6) | in[3]);
meillo@10 119 if (p[-1] == '=') {
meillo@10 120 if (i == 3) {
meillo@10 121 (*size)++;
meillo@10 122 } else if (i == 4) {
meillo@10 123 (*size) += 2;
meillo@10 124 }
meillo@10 125 } else {
meillo@10 126 *size += 3;
meillo@10 127 }
meillo@10 128 }
meillo@10 129 }
meillo@312 130 out[*size] = '\0';
meillo@10 131 return out;
meillo@10 132 }