masqmail-0.2

annotate src/base64/base64.c @ 189:bdd343cc4e19

bumped version number
author markus schnalke <meillo@marmaro.de>
date Sat, 27 Aug 2011 18:51:46 +0200
parents 26e34ae9a3e3
children
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@10 43 outbuf = g_malloc(((len + 3) * 8) / 6);
meillo@10 44 q = outbuf;
meillo@0 45
meillo@10 46 i = 0;
meillo@10 47 while (i < len - 2) {
meillo@10 48 in0 = buf[i++];
meillo@10 49 in1 = buf[i++];
meillo@10 50 in2 = buf[i++];
meillo@0 51
meillo@10 52 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 53 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 54 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@10 55 *(q++) = enc[in2 & 0x3f];
meillo@10 56 }
meillo@10 57 if ((len - i) == 1) {
meillo@10 58 in0 = buf[i++];
meillo@10 59 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 60 *(q++) = enc[(in0 << 4) & 0x3f];
meillo@10 61 *(q++) = '=';
meillo@10 62 *(q++) = '=';
meillo@10 63 } else if ((len - i) == 2) {
meillo@10 64 in0 = buf[i++];
meillo@10 65 in1 = buf[i++];
meillo@10 66 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 67 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 68 *(q++) = enc[(in1 << 2) & 0x3f];
meillo@10 69 *(q++) = '=';
meillo@10 70 }
meillo@10 71 *q = 0;
meillo@0 72
meillo@10 73 return outbuf;
meillo@0 74 }
meillo@0 75
meillo@10 76 gchar *base64_decode(gchar * buf, gint * size)
meillo@10 77 {
meillo@10 78 guchar *p = buf, *q;
meillo@10 79 guint in[4];
meillo@10 80 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
meillo@10 81 gchar *out = g_malloc((strlen(buf) + 3) + 1);
meillo@10 82
meillo@10 83 q = out;
meillo@10 84 *size = 0;
meillo@10 85
meillo@10 86 *q = 0;
meillo@10 87
meillo@10 88 while (*p) {
meillo@10 89 int i = 0;
meillo@10 90 while (i < 4) {
meillo@10 91 if (!*p)
meillo@10 92 break;
meillo@10 93 if ((*p >= 'A') && (*p <= 'Z'))
meillo@10 94 in[i++] = *p - 'A';
meillo@10 95 else if ((*p >= 'a') && (*p <= 'z'))
meillo@10 96 in[i++] = (*p - 'a') + 26;
meillo@10 97 else if ((*p >= '0') && (*p <= '9'))
meillo@10 98 in[i++] = (*p - '0') + 52;
meillo@10 99 else if (*p == '+')
meillo@10 100 in[i++] = 62;
meillo@10 101 else if (*p == '/')
meillo@10 102 in[i++] = 63;
meillo@10 103 else if (*p == '=') {
meillo@10 104 in[i++] = 0;
meillo@10 105 p++;
meillo@10 106 break;
meillo@10 107 } else if ((*p != '\r') && (*p != '\n')) {
meillo@10 108 p++;
meillo@10 109 break;
meillo@10 110 }
meillo@10 111 p++;
meillo@10 112 }
meillo@10 113 if ((i == 4) || (p[-1] == '=')) {
meillo@10 114 *(q++) = ((in[0] << 2) | (in[1] >> 4));
meillo@10 115 *(q++) = ((in[1] << 4) | (in[2] >> 2));
meillo@10 116 *(q++) = ((in[2] << 6) | in[3]);
meillo@10 117 if (p[-1] == '=') {
meillo@10 118 if (i == 3) {
meillo@10 119 (*size)++;
meillo@10 120 } else if (i == 4) {
meillo@10 121 (*size) += 2;
meillo@10 122 }
meillo@10 123 } else {
meillo@10 124 *size += 3;
meillo@10 125 }
meillo@10 126 }
meillo@10 127 }
meillo@10 128 return out;
meillo@10 129 }