masqmail

view src/base64/base64.c @ 382:7fe36ba87598

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