masqmail-0.2
view src/base64/base64.c @ 0:08114f7dcc23
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
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 /*
19 send bugs to: kurth@innominate.de
20 */
22 /* see also RFC 1341 */
24 #include <glib.h>
25 #include <string.h>
26 #include "base64.h"
28 gchar *base64_encode(guchar *buf, gint len)
29 {
30 guchar *outbuf, *q;
31 gchar enc[64];
32 gint i = 0, j = 0;
33 guint in0, in1, in2;
35 for(; i < 26; i++) enc[i] = (gchar)('A' + j++); j = 0;
36 for(; i < 52; i++) enc[i] = (gchar)('a' + j++); j = 0;
37 for(; i < 62; i++) enc[i] = (gchar)('0' + j++);
38 enc[i++] = '+';
39 enc[i++] = '/';
41 outbuf = g_malloc(((len+3) * 8)/6);
42 q = outbuf;
44 i = 0;
45 while(i < len-2){
46 in0 = buf[i++];
47 in1 = buf[i++];
48 in2 = buf[i++];
50 *(q++) = enc[(in0 >> 2) & 0x3f];
51 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
52 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
53 *(q++) = enc[in2 & 0x3f];
54 }
55 if((len - i) == 1){
56 in0 = buf[i++];
57 *(q++) = enc[(in0 >> 2) & 0x3f];
58 *(q++) = enc[(in0 << 4) & 0x3f];
59 *(q++) = '=';
60 *(q++) = '=';
61 }else if((len - i) == 2){
62 in0 = buf[i++];
63 in1 = buf[i++];
64 *(q++) = enc[(in0 >> 2) & 0x3f];
65 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
66 *(q++) = enc[(in1 << 2) & 0x3f];
67 *(q++) = '=';
68 }
69 *q = 0;
71 return outbuf;
72 }
74 gchar *base64_decode(gchar *buf, gint *size)
75 {
76 guchar *p = buf, *q;
77 guint in[4];
78 // gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1);
79 gchar *out = g_malloc((strlen(buf)+3) + 1);
81 q = out;
82 *size = 0;
84 *q = 0;
86 while(*p){
87 int i = 0;
88 while(i < 4){
89 if(!*p) break;
90 if((*p >= 'A') && (*p <= 'Z'))
91 in[i++] = *p - 'A';
92 else if((*p >= 'a') && (*p <= 'z'))
93 in[i++] = (*p - 'a') + 26;
94 else if((*p >= '0') && (*p <= '9'))
95 in[i++] = (*p - '0') + 52;
96 else if(*p == '+')
97 in[i++] = 62;
98 else if(*p == '/')
99 in[i++] = 63;
100 else if(*p == '='){
101 in[i++] = 0;
102 p++;
103 break;
104 }else if((*p != '\r') && (*p != '\n')){
105 p++;
106 break;
107 }
108 p++;
109 }
110 if((i == 4) || (p[-1] == '=')){
111 *(q++) = ((in[0] << 2) | (in[1] >> 4));
112 *(q++) = ((in[1] << 4) | (in[2] >> 2));
113 *(q++) = ((in[2] << 6) | in[3]);
114 if(p[-1] == '='){
115 if(i == 3){
116 (*size)++;
117 }
118 else if(i == 4){
119 (*size) += 2;
120 }
121 }else{
122 *size += 3;
123 }
124 }
125 }
126 return out;
127 }