The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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  */
24 RCSIDH(base32_h, "$Id: 7252ea2075dfae504500b340ca5e53e33d79c6a3 $")
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 <stdbool.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <sys/types.h>
38 
39 extern char const fr_base32_alphabet_encode[UINT8_MAX];
41 extern char const fr_base32_hex_alphabet_encode[UINT8_MAX];
43 
44 /** Check if char is in base32 alphabet
45  *
46  * Note that '=' is padding and not considered to be part of the alphabet.
47  *
48  * @param[in] c char to check.
49  * @param[in] alphabet to use.
50  * @return
51  * - true if c is a character from the base32 alphabet.
52  * - false if character is not in the base32 alphabet.
53  */
54 static inline bool fr_is_base32_nstd(char c, uint8_t const alphabet[static UINT8_MAX])
55 {
56  return alphabet[(uint8_t)c] < 32;
57 }
58 
60  bool add_padding, char const alphabet[static UINT8_MAX]);
61 
62 #define fr_base32_encode(_out, _in, _add_padding) \
63  fr_base32_encode_nstd(_out, _in, _add_padding, fr_base32_alphabet_encode)
64 
66  bool expect_padding, bool no_trailing, uint8_t const alphabet[static UINT8_MAX])
67  CC_HINT(nonnull(2,3,6));
68 
69 #define fr_base32_decode(_out, _in, _expect_padding, _no_trailing) \
70  fr_base32_decode_nstd(NULL, _out, _in, _expect_padding, _no_trailing, fr_base32_alphabet_decode)
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
char const fr_base32_alphabet_encode[UINT8_MAX]
Encode/decode binary data using printable characters (base32 format)
Definition: base32.c:32
ssize_t fr_base32_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static UINT8_MAX])
Base 64 encode binary data.
Definition: base32.c:190
uint8_t const fr_base32_alphabet_decode[UINT8_MAX]
Definition: base32.c:67
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 UINT8_MAX]))
Definition: base32.c:315
static bool fr_is_base32_nstd(char c, uint8_t const alphabet[static UINT8_MAX])
Check if char is in base32 alphabet.
Definition: base32.h:54
uint8_t const fr_base32_hex_alphabet_decode[UINT8_MAX]
Definition: base32.c:140
char const fr_base32_hex_alphabet_encode[UINT8_MAX]
Definition: base32.c:105
#define RCSIDH(h, id)
Definition: build.h:445
static fr_slen_t err
Definition: dict.h:645
static fr_slen_t in
Definition: dict.h:645
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
#define UINT8_MAX
Definition: merged_model.c:32
fr_sbuff_parse_error_t
Definition: merged_model.c:45
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:984