Merge remote-tracking branch 'IOMultiplexer/v2'
[NextIRCd.git] / src / crypt_base64.c
1 /* crypt_base64.c - NextIRCd
2  * 
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.
7  * 
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.
12  * 
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/>. 
15  */
16
17 #include "crypt_base64.h"
18 #include <stdint.h>
19 #include <stdlib.h>
20
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};
31
32
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);
35         
36         char *encoded_data = malloc(*output_length);
37         if (encoded_data == NULL)
38                 return NULL;
39         
40         int i, j;
41         for (i = 0, j = 0; i < input_length;) {
42                 uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
43                 uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
44                 uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
45                 
46                 uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
47                 
48                 encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
49                 encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
50                 encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
51                 encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
52         }
53         
54         for (i = 0; i < mod_table[input_length % 3]; i++)
55                 encoded_data[*output_length - 1 - i] = '=';
56         
57         return encoded_data;
58 }
59
60
61 unsigned char *crypt_base64_decode(const char *data, size_t input_length, size_t *output_length) {
62         if (decoding_table == NULL)
63                 crypt_base64_init();
64         if (input_length % 4 != 0)
65                 return NULL;
66         
67         *output_length = input_length / 4 * 3;
68         if (data[input_length - 1] == '=') (*output_length)--;
69         if (data[input_length - 2] == '=') (*output_length)--;
70         
71         unsigned char *decoded_data = malloc(*output_length);
72         if (decoded_data == NULL)
73                 return NULL;
74         
75         int i, j;
76         for (i = 0, j = 0; i < input_length;) {
77                 uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
78                 uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
79                 uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
80                 uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
81                 
82                 uint32_t triple = (sextet_a << 3 * 6)
83                 + (sextet_b << 2 * 6)
84                 + (sextet_c << 1 * 6)
85                 + (sextet_d << 0 * 6);
86                 
87                 if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
88                 if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
89                 if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
90         }
91         
92         return decoded_data;
93 }
94
95
96 void crypt_base64_init() {
97         decoding_table = malloc(256);
98         int i;
99         for (i = 0; i < 64; i++)
100                 decoding_table[(unsigned char) encoding_table[i]] = i;
101 }
102
103 void crypt_base64_deinit() {
104         free(decoding_table);
105 }