summaryrefslogtreecommitdiffabout
path: root/libetpan/src/data-types/base64.c
Unidiff
Diffstat (limited to 'libetpan/src/data-types/base64.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/data-types/base64.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/libetpan/src/data-types/base64.c b/libetpan/src/data-types/base64.c
new file mode 100644
index 0000000..58a2c04
--- a/dev/null
+++ b/libetpan/src/data-types/base64.c
@@ -0,0 +1,143 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2005 - Juergen Graf
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the libEtPan! project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * $Id$
34 */
35
36#include "base64.h"
37
38#include <stdlib.h>
39
40#define OUTPUT_SIZE 513
41#define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
42
43static char index_64[128] = {
44 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
45 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
46 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
47 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
48 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
49 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
50 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
51 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
52};
53
54static char basis_64[] =
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56
57char * encode_base64(const char * in, int len)
58{
59 char * output, * tmp;
60 unsigned char oval;
61 int out_len;
62
63 out_len = ((len + 2) / 3 * 4) + 1;
64
65 if ((len > 0) && (in == NULL))
66 return NULL;
67
68 output = malloc(out_len);
69 if (!output)
70 return NULL;
71
72 tmp = output;
73 while (len >= 3) {
74 *tmp++ = basis_64[in[0] >> 2];
75 *tmp++ = basis_64[((in[0] << 4) & 0x30) | (in[1] >> 4)];
76 *tmp++ = basis_64[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
77 *tmp++ = basis_64[in[2] & 0x3f];
78 in += 3;
79 len -= 3;
80 }
81 if (len > 0) {
82 *tmp++ = basis_64[in[0] >> 2];
83 oval = (in[0] << 4) & 0x30;
84 if (len > 1) oval |= in[1] >> 4;
85 *tmp++ = basis_64[oval];
86 *tmp++ = (len < 2) ? '=' : basis_64[(in[1] << 2) & 0x3c];
87 *tmp++ = '=';
88 }
89
90 *tmp = '\0';
91
92 return output;
93}
94
95char * decode_base64(const char * in, int len)
96{
97 char * output, * out;
98 int i, c1, c2, c3, c4, out_len;
99
100 out_len = 0;
101
102 output = malloc(OUTPUT_SIZE);
103 if (output == NULL)
104 return NULL;
105 out = output;
106
107 if (in[0] == '+' && in[1] == ' ')
108 in += 2;
109
110 for (i = 0; i < (len / 4); i++) {
111 c1 = in[0];
112 c2 = in[1];
113 c3 = in[2];
114 c4 = in[3];
115 if (CHAR64(c1) == -1 || CHAR64(c2) == -1 ||
116 (c3 != '=' && CHAR64(c3) == -1) ||
117 (c4 != '=' && CHAR64(c4) == -1))
118 return NULL;
119
120 in += 4;
121 *output++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
122 if (++out_len >= OUTPUT_SIZE)
123 return NULL;
124
125 if (c3 != '=') {
126 *output++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
127 if (++out_len >= OUTPUT_SIZE)
128 return NULL;
129
130 if (c4 != '=') {
131 *output++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
132 if (++out_len >= OUTPUT_SIZE)
133 return NULL;
134 }
135 }
136 }
137
138 *output = 0;
139
140 return out;
141}
142
143