The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
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: bbc1e3863c818b427823ad3bebf6260e29f62146 $")
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
53/** Encode a single byte as two hex digits
54 *
55 * @param[out] out Where to write the two digits.
56 * @param[in] byte to encode.
57 * @param[in] alphabet to use.
58 * @return
59 * - 2 on success.
60 * - < 0 the number of additional bytes out needed.
61 */
63 char const alphabet[static SBUFF_CHAR_CLASS])
64{
65 return fr_sbuff_in_char(out, alphabet[(uint8_t)(byte >> 4)], alphabet[(uint8_t)(byte & 0x0f)]);
66}
67#define fr_base16_encode_byte(_out, _byte) \
68 fr_base16_encode_byte_nstd(_out, _byte, fr_base16_alphabet_encode_lc)
69
71#define fr_base16_encode(_out, _in) \
72 fr_base16_encode_nstd(_out, _in, fr_base16_alphabet_encode_lc)
73
74/** Convert binary data to a hex string, allocating the output buffer
75 *
76 * Ascii encoded hex string will not be prefixed with '0x'
77 *
78 * @param[in] ctx to allocate the buffer in.
79 * @param[out] out where to write the new buffer.
80 * @param[in] in input.
81 * @return
82 * - >=0 the number of bytes written to out.
83 * - <0 number of bytes we would have needed to print the next hexit.
84 */
85static inline fr_slen_t fr_base16_aencode(TALLOC_CTX *ctx, char **out, fr_dbuff_t *in)
86{
87 fr_sbuff_t sbuff;
89 ssize_t slen;
90
91 fr_sbuff_init_talloc(ctx, &sbuff, &tctx,
92 (fr_dbuff_remaining(in) << 1),
93 SIZE_MAX);
94
95 slen = fr_base16_encode(&sbuff, in);
96 if (slen < 0) {
97 (void)fr_sbuff_trim_talloc(&sbuff, 0);
98 *out = sbuff.buff;
99 return slen;
100 }
101
102 *out = sbuff.buff;
103
104 return (size_t)slen;
105}
106
108 bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]);
109#define fr_base16_decode(_err, _out, _in, _no_trailing) \
110 fr_base16_decode_nstd(_err, _out, _in, _no_trailing, fr_base16_alphabet_decode_mc)
111
112#ifdef __cplusplus
113}
114#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:85
#define fr_base16_encode(_out, _in)
Definition base16.h:71
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
static fr_slen_t fr_base16_encode_byte_nstd(fr_sbuff_t *out, uint8_t byte, char const alphabet[static SBUFF_CHAR_CLASS])
Encode a single byte as two hex digits.
Definition base16.h:62
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:561
#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:906
static fr_slen_t in
Definition dict.h:906
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:433
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
#define fr_sbuff_in_char(_sbuff,...)
Talloc sbuff extension structure.
Definition sbuff.h:137
static size_t char ** out
Definition value.h:1062