chiark / gitweb /
client.c doxygen; kill some redundant code
[disorder] / lib / base64.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2007 Richard Kettlewell
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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/base64.c
21  * @brief Support for MIME base64
22  */
23
24 #include <config.h>
25 #include "types.h"
26
27 #include <string.h>
28
29 #include <stdio.h>
30
31 #include "mem.h"
32 #include "base64.h"
33 #include "vector.h"
34
35 static const char mime_base64_table[] =
36   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37
38 /** @brief Convert MIME base64
39  * @param s base64 data
40  * @return Decoded data
41  *
42  * See <a href="http://tools.ietf.org/html/rfc2045#section-6.8">RFC
43  * 2045 s6.8</a>.
44  */
45 char *mime_base64(const char *s, size_t *nsp) {
46   struct dynstr d;
47   const char *t;
48   int b[4], n, c;
49
50   dynstr_init(&d);
51   n = 0;
52   while((c = (unsigned char)*s++)) {
53     if((t = strchr(mime_base64_table, c))) {
54       b[n++] = t - mime_base64_table;
55       if(n == 4) {
56         dynstr_append(&d, (b[0] << 2) + (b[1] >> 4));
57         dynstr_append(&d, (b[1] << 4) + (b[2] >> 2));
58         dynstr_append(&d, (b[2] << 6) + b[3]);
59         n = 0;
60       }
61     } else if(c == '=') {
62       if(n >= 2) {
63         dynstr_append(&d, (b[0] << 2) + (b[1] >> 4));
64         if(n == 3)
65           dynstr_append(&d, (b[1] << 4) + (b[2] >> 2));
66       }
67       break;
68     }
69   }
70   if(nsp)
71     *nsp = d.nvec;
72   dynstr_terminate(&d);
73   return d.vec;
74 }
75
76 /** @brief Convert a binary string to base64
77  * @param s Bytes to convert
78  * @param ns Number of bytes to convert
79  * @return Encoded data
80  *
81  * This function does not attempt to split up lines.
82  *
83  * See <a href="http://tools.ietf.org/html/rfc2045#section-6.8">RFC
84  * 2045 s6.8</a>.
85  */
86 char *mime_to_base64(const uint8_t *s, size_t ns) {
87   struct dynstr d[1];
88
89   dynstr_init(d);
90   while(ns >= 3) {
91     /* Input bytes with output bits: AAAAAABB BBBBCCCC CCDDDDDD */
92     /* Output bytes with input bits: 000000 001111 111122 222222 */
93     dynstr_append(d, mime_base64_table[s[0] >> 2]);
94     dynstr_append(d, mime_base64_table[((s[0] & 3) << 4)
95                                        + (s[1] >> 4)]);
96     dynstr_append(d, mime_base64_table[((s[1] & 15) << 2)
97                                        + (s[2] >> 6)]);
98     dynstr_append(d, mime_base64_table[s[2] & 63]);
99     ns -= 3;
100     s += 3;
101   }
102   if(ns > 0) {
103     dynstr_append(d, mime_base64_table[s[0] >> 2]);
104     switch(ns) {
105     case 1:
106       dynstr_append(d, mime_base64_table[(s[0] & 3) << 4]);
107       dynstr_append(d, '=');
108       dynstr_append(d, '=');
109       break;
110     case 2:
111       dynstr_append(d, mime_base64_table[((s[0] & 3) << 4)
112                                          + (s[1] >> 4)]);
113       dynstr_append(d, mime_base64_table[(s[1] & 15) << 2]);
114       dynstr_append(d, '=');
115       break;
116     }
117   }
118   dynstr_terminate(d);
119   return d->vec;
120 }
121
122 /*
123 Local Variables:
124 c-basic-offset:2
125 comment-column:40
126 fill-column:79
127 End:
128 */