Coverage Report

Created: 2026-08-08 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/freeradius-server/src/freeradius-devel/util/base16.h
Line
Count
Source
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: 74e7f594ed008d3914596e8a671cdedbede569bd $")
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 <sys/types.h>
35
36
extern char const fr_base16_alphabet_encode_lc[SBUFF_CHAR_CLASS];
37
extern char const fr_base16_alphabet_encode_uc[SBUFF_CHAR_CLASS];
38
extern 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
 */
48
static inline bool fr_is_base16_nstd(char c, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
49
1.26M
{
50
1.26M
  return alphabet[(uint8_t)c] < 16;
51
1.26M
}
Unexecuted instantiation: fuzzer_base16_32_64.c:fr_is_base16_nstd
base16.c:fr_is_base16_nstd
Line
Count
Source
49
1.26M
{
50
1.26M
  return alphabet[(uint8_t)c] < 16;
51
1.26M
}
Unexecuted instantiation: print.c:fr_is_base16_nstd
Unexecuted instantiation: value.c:fr_is_base16_nstd
Unexecuted instantiation: ctx.c:fr_is_base16_nstd
Unexecuted instantiation: session.c:fr_is_base16_nstd
Unexecuted instantiation: abinary.c:fr_is_base16_nstd
Unexecuted instantiation: client.c:fr_is_base16_nstd
Unexecuted instantiation: map.c:fr_is_base16_nstd
Unexecuted instantiation: password.c:fr_is_base16_nstd
Unexecuted instantiation: tmpl_tokenize.c:fr_is_base16_nstd
Unexecuted instantiation: util.c:fr_is_base16_nstd
Unexecuted instantiation: xlat_builtin.c:fr_is_base16_nstd
Unexecuted instantiation: json.c:fr_is_base16_nstd
52
53
fr_slen_t fr_base16_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, char const alphabet[static SBUFF_CHAR_CLASS]);
54
#define   fr_base16_encode(_out, _in) \
55
93
    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
 */
68
static inline fr_slen_t fr_base16_aencode(TALLOC_CTX *ctx, char **out, fr_dbuff_t *in)
69
0
{
70
0
  fr_sbuff_t    sbuff;
71
0
  fr_sbuff_uctx_talloc_t  tctx;
72
0
  ssize_t     slen;
73
74
0
  fr_sbuff_init_talloc(ctx, &sbuff, &tctx,
75
0
           (fr_dbuff_remaining(in) << 1),
76
0
           SIZE_MAX);
77
78
0
  slen = fr_base16_encode(&sbuff, in);
79
0
  if (slen < 0) {
80
0
    fr_sbuff_trim_talloc(&sbuff, 0);
81
0
    *out = sbuff.buff;
82
0
    return slen;
83
0
  }
84
85
0
  *out = sbuff.buff;
86
87
0
  return (size_t)slen;
88
0
}
Unexecuted instantiation: fuzzer_base16_32_64.c:fr_base16_aencode
Unexecuted instantiation: base16.c:fr_base16_aencode
Unexecuted instantiation: print.c:fr_base16_aencode
Unexecuted instantiation: value.c:fr_base16_aencode
Unexecuted instantiation: ctx.c:fr_base16_aencode
Unexecuted instantiation: session.c:fr_base16_aencode
Unexecuted instantiation: abinary.c:fr_base16_aencode
Unexecuted instantiation: client.c:fr_base16_aencode
Unexecuted instantiation: map.c:fr_base16_aencode
Unexecuted instantiation: password.c:fr_base16_aencode
Unexecuted instantiation: tmpl_tokenize.c:fr_base16_aencode
Unexecuted instantiation: util.c:fr_base16_aencode
Unexecuted instantiation: xlat_builtin.c:fr_base16_aencode
Unexecuted instantiation: json.c:fr_base16_aencode
89
90
fr_slen_t fr_base16_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in,
91
              bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS]);
92
#define   fr_base16_decode(_err, _out, _in, _no_trailing) \
93
25.1k
    fr_base16_decode_nstd(_err, _out, _in, _no_trailing, fr_base16_alphabet_decode_mc)
94
95
#ifdef __cplusplus
96
}
97
#endif