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: def04ff57e9d08301f81e62822a4c41689fa909a $")
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 <stdbool.h>
35#include <stddef.h>
36#include <stdint.h>
37#include <sys/types.h>
38
39/*
40 * This uses that the expression (n+(k-1))/k means the smallest
41 * Integer >= n/k, i.e., the ceiling of n/k.
42 */
43#define FR_BASE64_ENC_LENGTH(_inlen) ((((_inlen) + 2) / 3) * 4)
44#define FR_BASE64_DEC_LENGTH(_inlen) ((3 * ((_inlen) / 4)) + 2)
45
46extern char const fr_base64_alphabet_encode[UINT8_MAX];
50
51/** Check if char is in Base64 alphabet
52 *
53 * Note that '=' is padding and not considered to be part of the alphabet.
54 *
55 * @param[in] c char to check.
56 * @param[in] alphabet to use.
57 * @return
58 * - true if c is a character from the Base64 alphabet.
59 * - false if character is not in the Base64 alphabet.
60 */
61static inline bool fr_is_base64_nstd(char c, uint8_t const alphabet[static UINT8_MAX])
62{
63 return alphabet[(uint8_t)c] < 64;
64}
65
66size_t fr_base64_encode(char * restrict out, size_t outlen, uint8_t const * restrict in, size_t inlen);
67#define fr_is_base64(_c) fr_is_base64_nstd(_c, fr_base64_alphabet_decode)
68
69
71 bool add_padding, char const alphabet[static UINT8_MAX])
72 CC_HINT(nonnull);
73
74#define fr_base64_encode(_out, _in, _add_padding) \
75 fr_base64_encode_nstd(_out, _in, _add_padding, fr_base64_alphabet_encode)
76
78 bool expect_padding, bool no_trailing, uint8_t const alphabet[static UINT8_MAX])
79 CC_HINT(nonnull(2,3,6));
80
81#define fr_base64_decode(_out, _in, _expect_padding, _no_trailing) \
82 fr_base64_decode_nstd(NULL, _out, _in, _expect_padding, _no_trailing, fr_base64_alphabet_decode)
83
84#ifdef __cplusplus
85}
86#endif
char const fr_base64_alphabet_encode[UINT8_MAX]
Definition base64.c:32
uint8_t const fr_base64_alphabet_decode[UINT8_MAX]
Definition base64.c:99
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 UINT8_MAX]))
Definition base64.c:401
ssize_t fr_base64_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 base64.c:326
#define fr_base64_encode(_out, _in, _add_padding)
Definition base64.h:74
static bool fr_is_base64_nstd(char c, uint8_t const alphabet[static UINT8_MAX])
Check if char is in Base64 alphabet.
Definition base64.h:61
uint8_t const fr_base64_url_alphabet_decode[UINT8_MAX]
Definition base64.c:240
char const fr_base64_url_alphabet_encode[UINT8_MAX]
Definition base64.c:173
#define RCSIDH(h, id)
Definition build.h:484
static fr_slen_t err
Definition dict.h:824
static fr_slen_t in
Definition dict.h:824
long int ssize_t
unsigned char uint8_t
#define UINT8_MAX
fr_sbuff_parse_error_t
static size_t char fr_sbuff_t size_t inlen
Definition value.h:997
int nonnull(2, 5))
static size_t char ** out
Definition value.h:997