The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base64.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 (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 */
24RCSIDH(base64_h, "$Id: 9e913b39feb72bfd4eb57c67f0f852918acaf691 $")
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
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
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 */
58static inline bool fr_is_base64_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
59{
60 return alphabet[(uint8_t)c] < 64;
61}
62
63size_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
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
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
char const fr_base64_url_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base64.c:173
char const fr_base64_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base64.c:32
ssize_t fr_base64_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 base64.c:401
#define fr_base64_encode(_out, _in, _add_padding)
Definition base64.h:71
uint8_t const fr_base64_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base64.c:99
static bool fr_is_base64_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Check if char is in Base64 alphabet.
Definition base64.h:58
uint8_t const fr_base64_url_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base64.c:240
ssize_t fr_base64_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 base64.c:326
#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
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030