The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base32.h
Go to the documentation of this file.
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 */
24RCSIDH(base32_h, "$Id: 397806e0b4e950d2e4e45f75f4ec9e971fa03406 $")
25
26#ifdef __cplusplus
27extern "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
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 */
51static inline bool fr_is_base32_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
52{
53 return alphabet[(uint8_t)c] < 32;
54}
55
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
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
char const fr_base32_alphabet_encode[SBUFF_CHAR_CLASS]
Encode/decode binary data using printable characters (base32 format)
Definition base32.c:32
char const fr_base32_hex_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base32.c:105
ssize_t fr_base32_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static SBUFF_CHAR_CLASS])
Base 64 encode binary data.
Definition base32.c:190
uint8_t const fr_base32_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base32.c:67
static bool fr_is_base32_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Check if char is in base32 alphabet.
Definition base32.h:51
ssize_t fr_base32_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool expect_padding, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]))
Definition base32.c:315
uint8_t const fr_base32_hex_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base32.c:140
#define RCSIDH(h, id)
Definition build.h:507
static fr_slen_t err
Definition dict.h:882
static fr_slen_t in
Definition dict.h:882
long int ssize_t
unsigned char uint8_t
fr_sbuff_parse_error_t
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030