The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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  */
24 RCSIDH(base16_h, "$Id: f5638f80ae3ead4db3055e61fa89c98b04797b6f $")
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_base16_alphabet_encode_lc[UINT8_MAX + 1];
40 extern char const fr_base16_alphabet_encode_uc[UINT8_MAX + 1];
41 extern uint8_t const fr_base16_alphabet_decode_mc[UINT8_MAX + 1]; /* mixed case */
42 
43 /** Check if char is in base16 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  */
51 static inline bool fr_is_base16_nstd(char c, uint8_t const alphabet[static UINT8_MAX + 1])
52 {
53  return alphabet[(uint8_t)c] < 16;
54 }
55 
56 fr_slen_t fr_base16_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, char const alphabet[static UINT8_MAX + 1]);
57 #define fr_base16_encode(_out, _in) \
58  fr_base16_encode_nstd(_out, _in, fr_base16_alphabet_encode_lc)
59 
60 /** Convert binary data to a hex string, allocating the output buffer
61  *
62  * Ascii encoded hex string will not be prefixed with '0x'
63  *
64  * @param[in] ctx to allocate the buffer in.
65  * @param[out] out where to write the new buffer.
66  * @param[in] in input.
67  * @return
68  * - >=0 the number of bytes written to out.
69  * - <0 number of bytes we would have needed to print the next hexit.
70  */
71 static inline fr_slen_t fr_base16_aencode(TALLOC_CTX *ctx, char **out, fr_dbuff_t *in)
72 {
73  fr_sbuff_t sbuff;
75  ssize_t slen;
76 
77  fr_sbuff_init_talloc(ctx, &sbuff, &tctx,
78  (fr_dbuff_remaining(in) << 1),
79  SIZE_MAX);
80 
81  slen = fr_base16_encode(&sbuff, in);
82  if (slen < 0) {
83  fr_sbuff_trim_talloc(&sbuff, 0);
84  *out = sbuff.buff;
85  return slen;
86  }
87 
88  *out = sbuff.buff;
89 
90  return (size_t)slen;
91 }
92 
94  bool no_trailing, uint8_t const alphabet[static UINT8_MAX + 1]);
95 #define fr_base16_decode(_err, _out, _in, _no_trailing) \
96  fr_base16_decode_nstd(_err, _out, _in, _no_trailing, fr_base16_alphabet_decode_mc)
97 
98 #ifdef __cplusplus
99 }
100 #endif
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:71
char const fr_base16_alphabet_encode_lc[UINT8_MAX+1]
Encode/decode binary data using printable characters (base16 format - hex)
Definition: base16.c:31
#define fr_base16_encode(_out, _in)
Definition: base16.h:57
static bool fr_is_base16_nstd(char c, uint8_t const alphabet[static UINT8_MAX+1])
Check if char is in base16 alphabet.
Definition: base16.h:51
fr_slen_t fr_base16_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, char const alphabet[static UINT8_MAX+1])
Convert binary data to a hex string.
Definition: merged_model.c:56
char const fr_base16_alphabet_encode_uc[UINT8_MAX+1]
lower case encode alphabet for base16
Definition: base16.c:53
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 UINT8_MAX+1])
Decode base16 encoded input.
Definition: merged_model.c:65
uint8_t const fr_base16_alphabet_decode_mc[UINT8_MAX+1]
Mixed case decode alphabet for base16.
Definition: base16.c:75
#define RCSIDH(h, id)
Definition: build.h:445
#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:738
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
ssize_t fr_slen_t
Definition: merged_model.c:35
#define UINT8_MAX
Definition: merged_model.c:32
fr_sbuff_parse_error_t
Definition: merged_model.c:45
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:399
Talloc sbuff extension structure.
Definition: sbuff.h:114
static size_t char ** out
Definition: value.h:984