Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / exchange / lib / e2k-encoding-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2001-2004 Novell, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "e2k-encoding-utils.h"
25
26 #include <string.h>
27
28 static const char *b64_alphabet =
29         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30
31 /**
32  * e2k_base64_encode:
33  * @data: binary data
34  * @len: the length of @data
35  *
36  * Base64-encodes @len bytes of data at @data.
37  *
38  * Return value: the base64-encoded representation of @data, which
39  * the caller must free.
40  **/
41 char *
42 e2k_base64_encode (const guint8 *data, int len)
43 {
44         char *buf, *p;
45
46         p = buf = g_malloc (((len + 2) / 3) * 4 + 1);
47         while (len >= 3) {
48                 p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
49                 p[1] = b64_alphabet[((data[0] << 4) & 0x30) +
50                                     ((data[1] >> 4) & 0x0f)];
51                 p[2] = b64_alphabet[((data[1] << 2) & 0x3c) +
52                                     ((data[2] >> 6) & 0x03)];
53                 p[3] = b64_alphabet[  data[2]       & 0x3f];
54
55                 data += 3;
56                 p += 4;
57                 len -= 3;
58         }
59
60         switch (len) {
61         case 2:
62                 p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
63                 p[1] = b64_alphabet[((data[0] << 4) & 0x30) +
64                                     ((data[1] >> 4) & 0xf)];
65                 p[2] = b64_alphabet[ (data[1] << 2) & 0x3c];
66                 p[3] = '=';
67                 p += 4;
68                 break;
69         case 1:
70                 p[0] = b64_alphabet[ (data[0] >> 2) & 0x3f];
71                 p[1] = b64_alphabet[ (data[0] << 4) & 0x30];
72                 p[2] = '=';
73                 p[3] = '=';
74                 p += 4;
75                 break;
76         }
77
78         *p = '\0';
79         return buf;
80 }
81
82 static guint8 base64_unphabet[] = {
83         -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -1, -1,
84         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85         -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
86         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
87         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
88         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
89         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
90         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
91 };
92 #define E2K_B64_SPACE ((guint8)-2)
93 #define E2K_B64_BAD   ((guint8)-1)
94
95 /**
96  * e2k_base64_decode:
97  * @string: base64-encoded data
98  *
99  * Decodes the base64-encoded data in @string.
100  *
101  * Return value: the binary data encoded in @string
102  **/
103 GByteArray *
104 e2k_base64_decode (const char *string)
105 {
106         GByteArray *rc;
107         int bits, length, qw = 0;
108         guint8 *data;
109
110         rc = g_byte_array_new ();
111
112         length = strlen (string);
113         if (length == 0)
114                 return rc;
115
116         g_byte_array_set_size (rc, ((length / 4) + 1) * 3);
117
118         data = rc->data;
119         for (; *string; string++) {
120                 if ((unsigned char)*string > 127)
121                         break;
122                 bits = base64_unphabet[(unsigned char)*string];
123                 if (bits == E2K_B64_BAD)
124                         break;
125                 else if (bits == E2K_B64_SPACE)
126                         continue;
127
128                 switch (qw++) {
129                 case 0:
130                         data[0]  = (bits << 2) & 0xfc;
131                         break;
132                 case 1:
133                         data[0] |= (bits >> 4) & 0x03;
134                         data[1]  = (bits << 4) & 0xf0;
135                         break;
136                 case 2:
137                         data[1] |= (bits >> 2) & 0x0f;
138                         data[2]  = (bits << 6) & 0xc0;
139                         break;
140                 case 3:
141                         data[2] |=  bits       & 0x3f;
142                         data += 3;
143                         qw = 0;
144                         break;
145                 }
146         }
147
148         rc->len = data - rc->data;
149         if (qw > 1)
150                 rc->len += qw - 1;
151         return rc;
152 }