chiark / gitweb /
client.c make-cookie support
[disorder] / lib / base64.c
CommitLineData
fce810c2
RK
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
35static const char mime_base64_table[] =
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
37
38/** @brief Convert MIME base64
39 * @param s base64 data
40 * @return Decoded data
41 */
42char *mime_base64(const char *s, size_t *nsp) {
43 struct dynstr d;
44 const char *t;
45 int b[4], n, c;
46
47 dynstr_init(&d);
48 n = 0;
49 while((c = (unsigned char)*s++)) {
50 if((t = strchr(mime_base64_table, c))) {
51 b[n++] = t - mime_base64_table;
52 if(n == 4) {
53 dynstr_append(&d, (b[0] << 2) + (b[1] >> 4));
54 dynstr_append(&d, (b[1] << 4) + (b[2] >> 2));
55 dynstr_append(&d, (b[2] << 6) + b[3]);
56 n = 0;
57 }
58 } else if(c == '=') {
59 if(n >= 2) {
60 dynstr_append(&d, (b[0] << 2) + (b[1] >> 4));
61 if(n == 3)
62 dynstr_append(&d, (b[1] << 4) + (b[2] >> 2));
63 }
64 break;
65 }
66 }
67 if(nsp)
68 *nsp = d.nvec;
69 dynstr_terminate(&d);
70 return d.vec;
71}
72
73/** @brief Convert a binary string to base64
74 * @param s Bytes to convert
75 * @param ns Number of bytes to convert
76 * @return Encoded data
77 *
78 * This function does not attempt to split up lines.
79 */
80char *mime_to_base64(const uint8_t *s, size_t ns) {
81 struct dynstr d[1];
82
83 dynstr_init(d);
84 while(ns >= 3) {
85 /* Input bytes with output bits: AAAAAABB BBBBCCCC CCDDDDDD */
86 /* Output bytes with input bits: 000000 001111 111122 222222 */
87 dynstr_append(d, mime_base64_table[s[0] >> 2]);
88 dynstr_append(d, mime_base64_table[((s[0] & 3) << 4)
89 + (s[1] >> 4)]);
90 dynstr_append(d, mime_base64_table[((s[1] & 15) << 2)
91 + (s[2] >> 6)]);
92 dynstr_append(d, mime_base64_table[s[2] & 63]);
93 ns -= 3;
94 s += 3;
95 }
96 if(ns > 0) {
97 dynstr_append(d, mime_base64_table[s[0] >> 2]);
98 switch(ns) {
99 case 1:
100 dynstr_append(d, mime_base64_table[(s[0] & 3) << 4]);
101 dynstr_append(d, '=');
102 dynstr_append(d, '=');
103 break;
104 case 2:
105 dynstr_append(d, mime_base64_table[((s[0] & 3) << 4)
106 + (s[1] >> 4)]);
107 dynstr_append(d, mime_base64_table[(s[1] & 15) << 2]);
108 dynstr_append(d, '=');
109 break;
110 }
111 }
112 dynstr_terminate(d);
113 return d->vec;
114}
115
116/*
117Local Variables:
118c-basic-offset:2
119comment-column:40
120fill-column:79
121End:
122*/