/src/freeradius-server/src/lib/util/base32.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 (base32 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(base32_h, "$Id: 397806e0b4e950d2e4e45f75f4ec9e971fa03406 $") |
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 | | extern char const fr_base32_alphabet_encode[SBUFF_CHAR_CLASS]; |
37 | | extern uint8_t const fr_base32_alphabet_decode[SBUFF_CHAR_CLASS]; |
38 | | extern char const fr_base32_hex_alphabet_encode[SBUFF_CHAR_CLASS]; |
39 | | extern uint8_t const fr_base32_hex_alphabet_decode[SBUFF_CHAR_CLASS]; |
40 | | |
41 | | /** Check if char is in base32 alphabet |
42 | | * |
43 | | * Note that '=' is padding and not considered to be part of the alphabet. |
44 | | * |
45 | | * @param[in] c char to check. |
46 | | * @param[in] alphabet to use. |
47 | | * @return |
48 | | * - true if c is a character from the base32 alphabet. |
49 | | * - false if character is not in the base32 alphabet. |
50 | | */ |
51 | | static inline bool fr_is_base32_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS]) |
52 | 0 | { |
53 | 0 | return alphabet[(uint8_t)c] < 32; |
54 | 0 | } |
55 | | |
56 | | ssize_t fr_base32_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, |
57 | | bool add_padding, char const alphabet[static SBUFF_CHAR_CLASS]); |
58 | | |
59 | | #define fr_base32_encode(_out, _in, _add_padding) \ |
60 | | fr_base32_encode_nstd(_out, _in, _add_padding, fr_base32_alphabet_encode) |
61 | | |
62 | | ssize_t fr_base32_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, |
63 | | bool expect_padding, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]) |
64 | | CC_HINT(nonnull(2,3,6)); |
65 | | |
66 | | #define fr_base32_decode(_out, _in, _expect_padding, _no_trailing) \ |
67 | | fr_base32_decode_nstd(NULL, _out, _in, _expect_padding, _no_trailing, fr_base32_alphabet_decode) |
68 | | |
69 | | #ifdef __cplusplus |
70 | | } |
71 | | #endif |
72 | | |