/src/freeradius-server/src/lib/util/base64.h
Line | Count | Source |
1 | | #pragma once |
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 2 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, write to the Free Software |
15 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
16 | | */ |
17 | | |
18 | | /** Encode/decode binary data using printable characters (base64 format) |
19 | | * |
20 | | * @see RFC 4648 <http://www.ietf.org/rfc/rfc4648.txt>. |
21 | | * |
22 | | * @copyright 2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org) |
23 | | */ |
24 | | RCSIDH(base64_h, "$Id: 9e913b39feb72bfd4eb57c67f0f852918acaf691 $") |
25 | | |
26 | | #ifdef __cplusplus |
27 | | extern "C" { |
28 | | #endif |
29 | | |
30 | | #include <freeradius-devel/missing.h> |
31 | | #include <freeradius-devel/util/sbuff.h> |
32 | | #include <freeradius-devel/util/dbuff.h> |
33 | | |
34 | | #include <sys/types.h> |
35 | | |
36 | | /* |
37 | | * This uses that the expression (n+(k-1))/k means the smallest |
38 | | * Integer >= n/k, i.e., the ceiling of n/k. |
39 | | */ |
40 | | #define FR_BASE64_ENC_LENGTH(_inlen) ((((_inlen) + 2) / 3) * 4) |
41 | | #define FR_BASE64_DEC_LENGTH(_inlen) ((3 * ((_inlen) / 4)) + 2) |
42 | | |
43 | | extern char const fr_base64_alphabet_encode[SBUFF_CHAR_CLASS]; |
44 | | extern uint8_t const fr_base64_alphabet_decode[SBUFF_CHAR_CLASS]; |
45 | | extern char const fr_base64_url_alphabet_encode[SBUFF_CHAR_CLASS]; |
46 | | extern uint8_t const fr_base64_url_alphabet_decode[SBUFF_CHAR_CLASS]; |
47 | | |
48 | | /** Check if char is in Base64 alphabet |
49 | | * |
50 | | * Note that '=' is padding and not considered to be part of the alphabet. |
51 | | * |
52 | | * @param[in] c char to check. |
53 | | * @param[in] alphabet to use. |
54 | | * @return |
55 | | * - true if c is a character from the Base64 alphabet. |
56 | | * - false if character is not in the Base64 alphabet. |
57 | | */ |
58 | | static inline bool fr_is_base64_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS]) |
59 | 531k | { |
60 | 531k | return alphabet[(uint8_t)c] < 64; |
61 | 531k | } |
62 | | |
63 | | size_t fr_base64_encode(char * restrict out, size_t outlen, uint8_t const * restrict in, size_t inlen); |
64 | | #define fr_is_base64(_c) fr_is_base64_nstd(_c, fr_base64_alphabet_decode) |
65 | | |
66 | | |
67 | | ssize_t fr_base64_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, |
68 | | bool add_padding, char const alphabet[static SBUFF_CHAR_CLASS]) |
69 | | CC_HINT(nonnull); |
70 | | |
71 | | #define fr_base64_encode(_out, _in, _add_padding) \ |
72 | | fr_base64_encode_nstd(_out, _in, _add_padding, fr_base64_alphabet_encode) |
73 | | |
74 | | ssize_t fr_base64_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, |
75 | | bool expect_padding, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]) |
76 | | CC_HINT(nonnull(2,3,6)); |
77 | | |
78 | | #define fr_base64_decode(_out, _in, _expect_padding, _no_trailing) \ |
79 | | fr_base64_decode_nstd(NULL, _out, _in, _expect_padding, _no_trailing, fr_base64_alphabet_decode) |
80 | | |
81 | | #ifdef __cplusplus |
82 | | } |
83 | | #endif |