The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base16.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 (base16 format - hex)
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(base16_h, "$Id: 74e7f594ed008d3914596e8a671cdedbede569bd $")
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
38extern uint8_t const fr_base16_alphabet_decode_mc[SBUFF_CHAR_CLASS]; /* mixed case */
39
40/** Check if char is in base16 alphabet
41 *
42 * @param[in] c char to check.
43 * @param[in] alphabet to use.
44 * @return
45 * - true if c is a character from the base32 alphabet.
46 * - false if character is not in the base32 alphabet.
47 */
48static inline bool fr_is_base16_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
49{
50 return alphabet[(uint8_t)c] < 16;
51}
52
54#define fr_base16_encode(_out, _in) \
55 fr_base16_encode_nstd(_out, _in, fr_base16_alphabet_encode_lc)
56
57/** Convert binary data to a hex string, allocating the output buffer
58 *
59 * Ascii encoded hex string will not be prefixed with '0x'
60 *
61 * @param[in] ctx to allocate the buffer in.
62 * @param[out] out where to write the new buffer.
63 * @param[in] in input.
64 * @return
65 * - >=0 the number of bytes written to out.
66 * - <0 number of bytes we would have needed to print the next hexit.
67 */
68static inline fr_slen_t fr_base16_aencode(TALLOC_CTX *ctx, char **out, fr_dbuff_t *in)
69{
70 fr_sbuff_t sbuff;
72 ssize_t slen;
73
74 fr_sbuff_init_talloc(ctx, &sbuff, &tctx,
75 (fr_dbuff_remaining(in) << 1),
76 SIZE_MAX);
77
78 slen = fr_base16_encode(&sbuff, in);
79 if (slen < 0) {
80 fr_sbuff_trim_talloc(&sbuff, 0);
81 *out = sbuff.buff;
82 return slen;
83 }
84
85 *out = sbuff.buff;
86
87 return (size_t)slen;
88}
89
91 bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]);
92#define fr_base16_decode(_err, _out, _in, _no_trailing) \
93 fr_base16_decode_nstd(_err, _out, _in, _no_trailing, fr_base16_alphabet_decode_mc)
94
95#ifdef __cplusplus
96}
97#endif
fr_slen_t fr_base16_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Decode base16 encoded input.
static fr_slen_t fr_base16_aencode(TALLOC_CTX *ctx, char **out, fr_dbuff_t *in)
Convert binary data to a hex string, allocating the output buffer.
Definition base16.h:68
#define fr_base16_encode(_out, _in)
Definition base16.h:54
char const fr_base16_alphabet_encode_uc[SBUFF_CHAR_CLASS]
lower case encode alphabet for base16
Definition base16.c:53
char const fr_base16_alphabet_encode_lc[SBUFF_CHAR_CLASS]
Encode/decode binary data using printable characters (base16 format - hex)
Definition base16.c:31
uint8_t const fr_base16_alphabet_decode_mc[SBUFF_CHAR_CLASS]
Mixed case decode alphabet for base16.
Definition base16.c:75
static bool fr_is_base16_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Check if char is in base16 alphabet.
Definition base16.h:48
fr_slen_t fr_base16_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, char const alphabet[static SBUFF_CHAR_CLASS])
Convert binary data to a hex string.
#define RCSIDH(h, id)
Definition build.h:489
#define fr_dbuff_remaining(_dbuff_or_marker)
Return the number of bytes remaining between the dbuff or marker and the end of the buffer.
Definition dbuff.h:751
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
ssize_t fr_slen_t
fr_sbuff_parse_error_t
int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len)
Trim a talloced sbuff to the minimum length required to represent the contained string.
Definition sbuff.c:432
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
Talloc sbuff extension structure.
Definition sbuff.h:137
static size_t char ** out
Definition value.h:1030