blob: ea244a25f72c1b5cae2e8efb055971b6f449b5de [file] [log] [blame]
willy tarreau9e138862006-05-14 23:06:28 +02001/*
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +01002 * ASCII <-> Base64 conversion as described in RFC1421.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau69e989c2008-06-29 17:17:38 +02004 * Copyright 2006-2008 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +01005 * Copyright 2009-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
willy tarreau9e138862006-05-14 23:06:28 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010014#include <stdlib.h>
15#include <string.h>
16
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/base64.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
willy tarreau9e138862006-05-14 23:06:28 +020019
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010020#define B64BASE '#' /* arbitrary chosen base value */
21#define B64CMIN '+'
22#define B64CMAX 'z'
23#define B64PADV 64 /* Base64 chosen special pad value */
24
Willy Tarreau69e989c2008-06-29 17:17:38 +020025const char base64tab[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010026const char base64rev[]="b###cXYZ[\\]^_`a###d###$%&'()*+,-./0123456789:;<=######>?@ABCDEFGHIJKLMNOPQRSTUVW";
willy tarreau9e138862006-05-14 23:06:28 +020027
28/* Encodes <ilen> bytes from <in> to <out> for at most <olen> chars (including
29 * the trailing zero). Returns the number of bytes written. No check is made
30 * for <in> or <out> to be NULL. Returns negative value if <olen> is too short
31 * to accept <ilen>. 4 output bytes are produced for 1 to 3 input bytes.
32 */
33int a2base64(char *in, int ilen, char *out, int olen)
34{
willy tarreau9e138862006-05-14 23:06:28 +020035 int convlen;
36
37 convlen = ((ilen + 2) / 3) * 4;
38
39 if (convlen >= olen)
40 return -1;
41
42 /* we don't need to check olen anymore */
43 while (ilen >= 3) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020044 out[0] = base64tab[(((unsigned char)in[0]) >> 2)];
45 out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) | (((unsigned char)in[1]) >> 4)];
46 out[2] = base64tab[(((unsigned char)in[1] & 0x0F) << 2) | (((unsigned char)in[2]) >> 6)];
47 out[3] = base64tab[(((unsigned char)in[2] & 0x3F))];
willy tarreau9e138862006-05-14 23:06:28 +020048 out += 4;
49 in += 3; ilen -= 3;
50 }
51
52 if (!ilen) {
53 out[0] = '\0';
54 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 out[0] = base64tab[((unsigned char)in[0]) >> 2];
willy tarreau9e138862006-05-14 23:06:28 +020056 if (ilen == 1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 out[1] = base64tab[((unsigned char)in[0] & 0x03) << 4];
willy tarreau9e138862006-05-14 23:06:28 +020058 out[2] = '=';
59 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +020060 out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) |
willy tarreau9e138862006-05-14 23:06:28 +020061 (((unsigned char)in[1]) >> 4)];
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 out[2] = base64tab[((unsigned char)in[1] & 0x0F) << 2];
willy tarreau9e138862006-05-14 23:06:28 +020063 }
64 out[3] = '=';
65 out[4] = '\0';
66 }
67
68 return convlen;
69}
Krzysztof Piotr Oledzkifccbdc82010-01-29 13:36:23 +010070
71/* Decodes <ilen> bytes from <in> to <out> for at most <olen> chars.
72 * Returns the number of bytes converted. No check is made for
73 * <in> or <out> to be NULL. Returns -1 if <in> is invalid or ilen
74 * has wrong size, -2 if <olen> is too short.
75 * 1 to 3 output bytes are produced for 4 input bytes.
76 */
77int base64dec(const char *in, size_t ilen, char *out, size_t olen) {
78
79 unsigned char t[4];
80 signed char b;
81 int convlen = 0, i = 0, pad = 0;
82
83 if (ilen % 4)
84 return -1;
85
86 if (olen < ilen / 4 * 3)
87 return -2;
88
89 while (ilen) {
90
91 /* if (*p < B64CMIN || *p > B64CMAX) */
92 b = (signed char)*in - B64CMIN;
93 if ((unsigned char)b > (B64CMAX-B64CMIN))
94 return -1;
95
96 b = base64rev[b] - B64BASE - 1;
97
98 /* b == -1: invalid character */
99 if (b < 0)
100 return -1;
101
102 /* padding has to be continous */
103 if (pad && b != B64PADV)
104 return -1;
105
106 /* valid padding: "XX==" or "XXX=", but never "X===" or "====" */
107 if (pad && i < 2)
108 return -1;
109
110 if (b == B64PADV)
111 pad++;
112
113 t[i++] = b;
114
115 if (i == 4) {
116 /*
117 * WARNING: we allow to write little more data than we
118 * should, but the checks from the beginning of the
119 * functions guarantee that we can safely do that.
120 */
121
122 /* xx000000 xx001111 xx111122 xx222222 */
123 out[convlen] = ((t[0] << 2) + (t[1] >> 4));
124 out[convlen+1] = ((t[1] << 4) + (t[2] >> 2));
125 out[convlen+2] = ((t[2] << 6) + (t[3] >> 0));
126
127 convlen += 3-pad;
128
129 pad = i = 0;
130 }
131
132 in++;
133 ilen--;
134 }
135
136 return convlen;
137}