chiark / gitweb /
Typo fix.
[disorder] / lib / base64.c
CommitLineData
fce810c2
RK
1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
fce810c2 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
fce810c2 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
fce810c2 8 * (at your option) any later version.
e7eb3a27
RK
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 *
fce810c2 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fce810c2
RK
17 */
18/** @file lib/base64.c
19 * @brief Support for MIME base64
20 */
21
05b75f8d 22#include "common.h"
fce810c2
RK
23
24#include "mem.h"
25#include "base64.h"
26#include "vector.h"
27
28static const char mime_base64_table[] =
86e3aea7 29 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
fce810c2
RK
30
31/** @brief Convert MIME base64
32 * @param s base64 data
158d0961 33 * @param nsp Where to store length of converted data
fce810c2 34 * @return Decoded data
78d8e29d
RK
35 *
36 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.8">RFC
37 * 2045 s6.8</a>.
fce810c2
RK
38 */
39char *mime_base64(const char *s, size_t *nsp) {
86e3aea7 40 return generic_base64(s, nsp, mime_base64_table);
41}
42
43/** @brief Convert base64
44 * @param s base64 data
45 * @param nsp Where to store length of converted data
0590cedc 46 * @param table Table of characters to use
86e3aea7 47 * @return Decoded data
48 *
49 * @p table should consist of 65 characters. The first 64 will be used to
50 * represents the 64 digits and the 65th will be used as padding at the end
51 * (i.e. the role of '=' in RFC2045 base64).
52 */
53char *generic_base64(const char *s, size_t *nsp, const char *table) {
fce810c2
RK
54 struct dynstr d;
55 const char *t;
56 int b[4], n, c;
57
58 dynstr_init(&d);
59 n = 0;
60 while((c = (unsigned char)*s++)) {
4d06b900 61 if(c == table[64]) {
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 } else if((t = strchr(table, c))) {
86e3aea7 69 b[n++] = t - table;
fce810c2
RK
70 if(n == 4) {
71 dynstr_append(&d, (b[0] << 2) + (b[1] >> 4));
72 dynstr_append(&d, (b[1] << 4) + (b[2] >> 2));
73 dynstr_append(&d, (b[2] << 6) + b[3]);
74 n = 0;
75 }
fce810c2
RK
76 }
77 }
78 if(nsp)
79 *nsp = d.nvec;
80 dynstr_terminate(&d);
81 return d.vec;
82}
83
86e3aea7 84/** @brief Convert a binary string to MIME base64
fce810c2
RK
85 * @param s Bytes to convert
86 * @param ns Number of bytes to convert
87 * @return Encoded data
88 *
89 * This function does not attempt to split up lines.
78d8e29d
RK
90 *
91 * See <a href="http://tools.ietf.org/html/rfc2045#section-6.8">RFC
92 * 2045 s6.8</a>.
fce810c2
RK
93 */
94char *mime_to_base64(const uint8_t *s, size_t ns) {
86e3aea7 95 return generic_to_base64(s, ns, mime_base64_table);
96}
97
98/** @brief Convert a binary string to base64
99 * @param s Bytes to convert
100 * @param ns Number of bytes to convert
101 * @param table Table of characters to use
102 * @return Encoded data
103 *
104 * This function does not attempt to split up lines.
105 *
106 * @p table should consist of 65 characters. The first 64 will be used to
107 * represents the 64 digits and the 65th will be used as padding at the end
108 * (i.e. the role of '=' in RFC2045 base64).
109 */
110char *generic_to_base64(const uint8_t *s, size_t ns, const char *table) {
fce810c2
RK
111 struct dynstr d[1];
112
113 dynstr_init(d);
114 while(ns >= 3) {
115 /* Input bytes with output bits: AAAAAABB BBBBCCCC CCDDDDDD */
116 /* Output bytes with input bits: 000000 001111 111122 222222 */
86e3aea7 117 dynstr_append(d, table[s[0] >> 2]);
118 dynstr_append(d, table[((s[0] & 3) << 4)
fce810c2 119 + (s[1] >> 4)]);
86e3aea7 120 dynstr_append(d, table[((s[1] & 15) << 2)
fce810c2 121 + (s[2] >> 6)]);
86e3aea7 122 dynstr_append(d, table[s[2] & 63]);
fce810c2
RK
123 ns -= 3;
124 s += 3;
125 }
126 if(ns > 0) {
86e3aea7 127 dynstr_append(d, table[s[0] >> 2]);
fce810c2
RK
128 switch(ns) {
129 case 1:
86e3aea7 130 dynstr_append(d, table[(s[0] & 3) << 4]);
131 dynstr_append(d, table[64]);
132 dynstr_append(d, table[64]);
fce810c2
RK
133 break;
134 case 2:
86e3aea7 135 dynstr_append(d, table[((s[0] & 3) << 4)
fce810c2 136 + (s[1] >> 4)]);
86e3aea7 137 dynstr_append(d, table[(s[1] & 15) << 2]);
138 dynstr_append(d, table[64]);
fce810c2
RK
139 break;
140 }
141 }
142 dynstr_terminate(d);
143 return d->vec;
144}
145
146/*
147Local Variables:
148c-basic-offset:2
149comment-column:40
150fill-column:79
151End:
152*/