masqmail

view src/base64/base64.c @ 281:ea5f86e0a81c

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