willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 1 | /* |
Krzysztof Piotr Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 2 | * ASCII <-> Base64 conversion as described in RFC1421. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | c01062b | 2010-10-07 19:27:29 +0200 | [diff] [blame] | 4 | * Copyright 2006-2010 Willy Tarreau <w@1wt.eu> |
Krzysztof Piotr Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 5 | * Copyright 2009-2010 Krzysztof Piotr Oledzki <ole@ans.pl> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 6 | * |
| 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 Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/base64.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 18 | #include <common/config.h> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 19 | |
Krzysztof Piotr Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 20 | #define B64BASE '#' /* arbitrary chosen base value */ |
| 21 | #define B64CMIN '+' |
| 22 | #define B64CMAX 'z' |
| 23 | #define B64PADV 64 /* Base64 chosen special pad value */ |
| 24 | |
Willy Tarreau | 69e989c | 2008-06-29 17:17:38 +0200 | [diff] [blame] | 25 | const char base64tab[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
Krzysztof Piotr Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 26 | const char base64rev[]="b###cXYZ[\\]^_`a###d###$%&'()*+,-./0123456789:;<=######>?@ABCDEFGHIJKLMNOPQRSTUVW"; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 27 | |
| 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 | */ |
| 33 | int a2base64(char *in, int ilen, char *out, int olen) |
| 34 | { |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 35 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | 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 tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 48 | out += 4; |
| 49 | in += 3; ilen -= 3; |
| 50 | } |
| 51 | |
| 52 | if (!ilen) { |
| 53 | out[0] = '\0'; |
| 54 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | out[0] = base64tab[((unsigned char)in[0]) >> 2]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 56 | if (ilen == 1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | out[1] = base64tab[((unsigned char)in[0] & 0x03) << 4]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 58 | out[2] = '='; |
| 59 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 61 | (((unsigned char)in[1]) >> 4)]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | out[2] = base64tab[((unsigned char)in[1] & 0x0F) << 2]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 63 | } |
| 64 | out[3] = '='; |
| 65 | out[4] = '\0'; |
| 66 | } |
| 67 | |
| 68 | return convlen; |
| 69 | } |
Krzysztof Piotr Oledzki | fccbdc8 | 2010-01-29 13:36:23 +0100 | [diff] [blame] | 70 | |
| 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 | */ |
| 77 | int 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 | } |
Willy Tarreau | c01062b | 2010-10-07 19:27:29 +0200 | [diff] [blame] | 138 | |
| 139 | |
| 140 | /* Converts the lower 30 bits of an integer to a 5-char base64 string. The |
| 141 | * caller is responsible for ensuring that the output buffer can accept 6 bytes |
| 142 | * (5 + the trailing zero). The pointer to the string is returned. The |
| 143 | * conversion is performed with MSB first and in a format that can be |
| 144 | * decoded with b64tos30(). This format is not padded and thus is not |
| 145 | * compatible with usual base64 routines. |
| 146 | */ |
| 147 | const char *s30tob64(int in, char *out) |
| 148 | { |
| 149 | int i; |
| 150 | for (i = 0; i < 5; i++) { |
| 151 | out[i] = base64tab[(in >> 24) & 0x3F]; |
| 152 | in <<= 6; |
| 153 | } |
| 154 | out[5] = '\0'; |
| 155 | return out; |
| 156 | } |
| 157 | |
| 158 | /* Converts a 5-char base64 string encoded by s30tob64() into a 30-bit integer. |
| 159 | * The caller is responsible for ensuring that the input contains at least 5 |
| 160 | * chars. If any unexpected character is encountered, a negative value is |
| 161 | * returned. Otherwise the decoded value is returned. |
| 162 | */ |
| 163 | int b64tos30(const char *in) |
| 164 | { |
| 165 | int i, out; |
| 166 | signed char b; |
| 167 | |
| 168 | out = 0; |
| 169 | for (i = 0; i < 5; i++) { |
| 170 | b = (signed char)in[i] - B64CMIN; |
| 171 | if ((unsigned char)b > (B64CMAX - B64CMIN)) |
| 172 | return -1; /* input character out of range */ |
| 173 | |
| 174 | b = base64rev[b] - B64BASE - 1; |
| 175 | if (b < 0) /* invalid character */ |
| 176 | return -1; |
| 177 | |
| 178 | if (b == B64PADV) /* padding not allowed */ |
| 179 | return -1; |
| 180 | |
| 181 | out = (out << 6) + b; |
| 182 | } |
| 183 | return out; |
| 184 | } |