1 /* crypt_base64.c - NextIRCd
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "crypt_base64.h"
21 static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
22 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
23 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
24 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
25 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
26 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
27 'w', 'x', 'y', 'z', '0', '1', '2', '3',
28 '4', '5', '6', '7', '8', '9', '+', '/'};
29 static char *decoding_table = NULL;
30 static int mod_table[] = {0, 2, 1};
33 char *crypt_base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) {
34 *output_length = 4 * ((input_length + 2) / 3);
36 char *encoded_data = malloc(*output_length);
37 if (encoded_data == NULL)
40 for (int i = 0, j = 0; i < input_length;) {
41 uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
42 uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
43 uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
45 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
47 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
48 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
49 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
50 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
53 for (int i = 0; i < mod_table[input_length % 3]; i++)
54 encoded_data[*output_length - 1 - i] = '=';
60 unsigned char *crypt_base64_decode(const char *data, size_t input_length, size_t *output_length) {
61 if (decoding_table == NULL)
63 if (input_length % 4 != 0)
66 *output_length = input_length / 4 * 3;
67 if (data[input_length - 1] == '=') (*output_length)--;
68 if (data[input_length - 2] == '=') (*output_length)--;
70 unsigned char *decoded_data = malloc(*output_length);
71 if (decoded_data == NULL)
74 for (int i = 0, j = 0; i < input_length;) {
75 uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
76 uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
77 uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
78 uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
80 uint32_t triple = (sextet_a << 3 * 6)
83 + (sextet_d << 0 * 6);
85 if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
86 if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
87 if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
94 void crypt_base64_init() {
95 decoding_table = malloc(256);
96 for (int i = 0; i < 64; i++)
97 decoding_table[(unsigned char) encoding_table[i]] = i;
100 void crypt_base64_deinit() {
101 free(decoding_table);