willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Ascii to Base64 conversion as described in RFC1421. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2006 Willy Tarreau <w@1wt.eu> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 13 | #include <common/base64.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | #include <common/config.h> |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | const char base64tab[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 17 | |
| 18 | /* Encodes <ilen> bytes from <in> to <out> for at most <olen> chars (including |
| 19 | * the trailing zero). Returns the number of bytes written. No check is made |
| 20 | * for <in> or <out> to be NULL. Returns negative value if <olen> is too short |
| 21 | * to accept <ilen>. 4 output bytes are produced for 1 to 3 input bytes. |
| 22 | */ |
| 23 | int a2base64(char *in, int ilen, char *out, int olen) |
| 24 | { |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 25 | int convlen; |
| 26 | |
| 27 | convlen = ((ilen + 2) / 3) * 4; |
| 28 | |
| 29 | if (convlen >= olen) |
| 30 | return -1; |
| 31 | |
| 32 | /* we don't need to check olen anymore */ |
| 33 | while (ilen >= 3) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | out[0] = base64tab[(((unsigned char)in[0]) >> 2)]; |
| 35 | out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) | (((unsigned char)in[1]) >> 4)]; |
| 36 | out[2] = base64tab[(((unsigned char)in[1] & 0x0F) << 2) | (((unsigned char)in[2]) >> 6)]; |
| 37 | out[3] = base64tab[(((unsigned char)in[2] & 0x3F))]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 38 | out += 4; |
| 39 | in += 3; ilen -= 3; |
| 40 | } |
| 41 | |
| 42 | if (!ilen) { |
| 43 | out[0] = '\0'; |
| 44 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | out[0] = base64tab[((unsigned char)in[0]) >> 2]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 46 | if (ilen == 1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | out[1] = base64tab[((unsigned char)in[0] & 0x03) << 4]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 48 | out[2] = '='; |
| 49 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | out[1] = base64tab[(((unsigned char)in[0] & 0x03) << 4) | |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 51 | (((unsigned char)in[1]) >> 4)]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | out[2] = base64tab[((unsigned char)in[1] & 0x0F) << 2]; |
willy tarreau | 9e13886 | 2006-05-14 23:06:28 +0200 | [diff] [blame] | 53 | } |
| 54 | out[3] = '='; |
| 55 | out[4] = '\0'; |
| 56 | } |
| 57 | |
| 58 | return convlen; |
| 59 | } |