masqmail

annotate src/base64/base64.c @ 424:19be3b27df6f

Fixed forgotten resetting of identity.
author markus schnalke <meillo@marmaro.de>
date Wed, 30 May 2012 11:24:33 +0200
parents 41958685480d
children
rev   line source
meillo@367 1 /*
meillo@367 2 ** base64.c
meillo@367 3 ** Copyright 2000 (C) Oliver Kurth,
meillo@367 4 **
meillo@367 5 ** This program is free software; you can redistribute it and/or modify
meillo@367 6 ** it under the terms of the GNU General Public License as published by
meillo@367 7 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 8 ** (at your option) any later version.
meillo@367 9 **
meillo@367 10 ** This program is distributed in the hope that it will be useful,
meillo@367 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 13 ** GNU General Public License for more details.
meillo@367 14 **
meillo@367 15 ** You should have received a copy of the GNU General Public License
meillo@367 16 ** along with this program; if not, write to the Free Software
meillo@367 17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
meillo@367 18 */
meillo@0 19
meillo@0 20 /* see also RFC 1341 */
meillo@0 21
meillo@0 22 #include <glib.h>
meillo@0 23 #include <string.h>
meillo@0 24 #include "base64.h"
meillo@0 25
meillo@10 26 gchar*
meillo@366 27 base64_encode(guchar *buf, gint len)
meillo@0 28 {
meillo@10 29 guchar *outbuf, *q;
meillo@10 30 gchar enc[64];
meillo@10 31 gint i = 0, j = 0;
meillo@10 32 guint in0, in1, in2;
meillo@0 33
meillo@10 34 for (; i < 26; i++)
meillo@10 35 enc[i] = (gchar) ('A' + j++);
meillo@10 36 j = 0;
meillo@10 37 for (; i < 52; i++)
meillo@10 38 enc[i] = (gchar) ('a' + j++);
meillo@10 39 j = 0;
meillo@10 40 for (; i < 62; i++)
meillo@10 41 enc[i] = (gchar) ('0' + j++);
meillo@10 42 enc[i++] = '+';
meillo@10 43 enc[i++] = '/';
meillo@0 44
meillo@312 45 outbuf = g_malloc(((len + 3) * 8) / 6 +1);
meillo@312 46 memset(outbuf, 0, ((len + 3) * 8) / 6 +1);
meillo@10 47 q = outbuf;
meillo@0 48
meillo@10 49 i = 0;
meillo@10 50 while (i < len - 2) {
meillo@10 51 in0 = buf[i++];
meillo@10 52 in1 = buf[i++];
meillo@10 53 in2 = buf[i++];
meillo@0 54
meillo@10 55 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 56 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 57 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@10 58 *(q++) = enc[in2 & 0x3f];
meillo@10 59 }
meillo@10 60 if ((len - i) == 1) {
meillo@10 61 in0 = buf[i++];
meillo@10 62 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 63 *(q++) = enc[(in0 << 4) & 0x3f];
meillo@10 64 *(q++) = '=';
meillo@10 65 *(q++) = '=';
meillo@10 66 } else if ((len - i) == 2) {
meillo@10 67 in0 = buf[i++];
meillo@10 68 in1 = buf[i++];
meillo@10 69 *(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10 70 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10 71 *(q++) = enc[(in1 << 2) & 0x3f];
meillo@10 72 *(q++) = '=';
meillo@10 73 }
meillo@10 74 *q = 0;
meillo@0 75
meillo@10 76 return outbuf;
meillo@0 77 }
meillo@0 78
meillo@366 79 gchar *base64_decode(gchar *buf, gint *size)
meillo@10 80 {
meillo@10 81 guchar *p = buf, *q;
meillo@10 82 guint in[4];
meillo@10 83 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
meillo@312 84 gchar *out = g_malloc((strlen(buf) + 3) + 1 +1);
meillo@312 85 memset(out, 0, (strlen(buf) + 3) + 1 +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@312 132 out[*size] = '\0';
meillo@10 133 return out;
meillo@10 134 }