The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
nbo.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/** Structures and functions for converting integers to/from network byte order
19 *
20 * @file src/lib/util/nbo.h
21 *
22 * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 * @copyright 2022 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25RCSIDH(nbo_h, "$Id: 627eef671c7cfdd0e2cd2a68446f63a52d19df78 $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <freeradius-devel/util/misc.h>
32
33/** Write out an unsigned 16bit integer in wire format (big endian)
34 *
35 * @param[out] out Where to write the integer.
36 * @param[in] num to encode.
37 */
38static inline void fr_nbo_from_uint16(uint8_t out[static sizeof(uint16_t)], uint16_t num)
39{
40 out[0] = (num >> 8) & 0xff;
41 out[1] = num & 0xff;
42}
43
44/** Write out an unsigned 24bit integer in wire format (big endian)
45 *
46 * @param[out] out Where to write the integer.
47 * @param[in] num to encode.
48 */
49static inline void fr_nbo_from_uint24(uint8_t out[static 3], uint32_t num)
50{
51 out[0] = (num >> 16) & 0xff;
52 out[1] = (num >> 8) & 0xff;
53 out[2] = num & 0xff;
54}
55
56/** Write out an unsigned 32bit integer in wire format (big endian)
57 *
58 * @param[out] out Where to write the integer.
59 * @param[in] num to encode.
60 */
61static inline void fr_nbo_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num)
62{
63 fr_nbo_from_uint16(out, (uint16_t) (num >> 16));
64 fr_nbo_from_uint16(out + sizeof(uint16_t), (uint16_t) num);
65}
66
67/** Write out an unsigned 64bit integer in wire format (big endian)
68 *
69 * @param[out] out Where to write the integer.
70 * @param[in] num to encode.
71 */
72static inline void fr_nbo_from_uint64(uint8_t out[static sizeof(uint64_t)], uint64_t num)
73{
74 fr_nbo_from_uint32(out, (uint32_t)(num >> 32));
75 fr_nbo_from_uint32(out + sizeof(uint32_t), (uint32_t)num);
76}
77
78/** Write out an signed 16bit integer in wire format (big endian)
79 *
80 * @param[out] out Where to write the integer.
81 * @param[in] num to encode.
82 */
83static inline void fr_nbo_from_int16(uint8_t out[static sizeof(int16_t)], int16_t num)
84{
85 out[0] = (num >> 8) & 0xff;
86 out[1] = num & 0xff;
87}
88
89/** Write out an signed 32bit integer in wire format (big endian)
90 *
91 * @param[out] out Where to write the integer.
92 * @param[in] num to encode.
93 */
94static inline void fr_nbo_from_int32(uint8_t out[static sizeof(int32_t)], int32_t num)
95{
96 fr_nbo_from_uint16(out, (int16_t) (num >> 16));
97 fr_nbo_from_uint16(out + sizeof(int16_t), (int16_t) num);
98}
99
100/** Write out an signed 64bit integer in wire format (big endian)
101 *
102 * @param[out] out Where to write the integer.
103 * @param[in] num to encode.
104 */
105static inline void fr_nbo_from_int64(uint8_t out[static sizeof(uint64_t)], uint64_t num)
106{
107 fr_nbo_from_uint32(out, (int32_t)(num >> 32));
108 fr_nbo_from_uint32(out + sizeof(int32_t), (int32_t)num);
109}
110
111/** Write out an unsigned 64bit integer in wire format using the fewest bytes possible
112 *
113 * @param[out] out Where to write big endian encoding of num.
114 * Should be at least 8 bytes.
115 * @param[in] num Number to encode.
116 * @return the number of bytes written to out.
117 */
118static inline size_t fr_nbo_from_uint64v(uint8_t out[static sizeof(uint64_t)], uint64_t num)
119{
120 size_t ret;
121 uint8_t swapped[sizeof(uint64_t)];
122
123 ret = ROUND_UP_DIV((size_t)fr_high_bit_pos(num | 0x80), 8);
124#ifdef __COVERITY__
125 /*
126 * Coverity doesn't realize that the fr_high_bit_pos() call will always
127 * return a value between 1 and 8 inclusive, the former thanks to the
128 * "| 0x80". and this function doesn't specify an error return value,
129 * so we use a Coverity-only assert.
130 */
131 if (!fr_cond_assert((ret >= 1) && (ret <= 8))) return 1;
132#endif
133
134 fr_nbo_from_uint64(swapped, num);
135 memcpy(out, (swapped + (sizeof(uint64_t) - ret)), ret); /* aligned */
136
137 return ret;
138}
139
140/** Read an unsigned 16bit integer from wire format (big endian)
141 *
142 * @param[in] data To convert to a 16bit unsigned integer of native endianness.
143 * @return a 16 bit unsigned integer of native endianness.
144 */
145/* coverity[-taint_source : arg-0] */
146static inline uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
147{
148 return (((uint16_t)data[0]) << 8) | data[1];
149}
150
151/** Read an unsigned 24bit integer from wire format (big endian)
152 *
153 * @param[in] data To convert to a 24bit unsigned integer of native endianness.
154 * @return a 24 bit unsigned integer of native endianness.
155 */
156/* coverity[-taint_source : arg-0] */
157static inline uint32_t fr_nbo_to_uint24(uint8_t const data[static 3])
158{
159 return (((uint32_t)data[0]) << 16) | (((uint32_t)data[1]) << 8) | data[2];
160}
161
162/** Read an unsigned 32bit integer from wire format (big endian)
163 *
164 * @param[in] data To convert to a 32bit unsigned integer of native endianness.
165 * @return a 32 bit unsigned integer of native endianness.
166 */
167static inline uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)])
168{
169 return ((uint32_t)fr_nbo_to_uint16(data) << 16) | fr_nbo_to_uint16(data + sizeof(uint16_t));
170}
171
172/** Read an unsigned 64bit integer from wire format (big endian)
173 *
174 * @param[in] data To convert to a 64bit unsigned integer of native endianness.
175 * @return a 64 bit unsigned integer of native endianness.
176 */
177static inline uint64_t fr_nbo_to_uint64(uint8_t const data[static sizeof(uint64_t)])
178{
179 return ((uint64_t)fr_nbo_to_uint32(data) << 32) | fr_nbo_to_uint32(data + sizeof(uint32_t));
180}
181
182/*
183 * To get signed integers, simply cast.
184 */
185#define fr_nbo_to_int16(_x) ((int16_t) fr_nbo_to_uint16(_x))
186#define fr_nbo_to_int32(_x) ((int32_t) fr_nbo_to_uint32(_x))
187#define fr_nbo_to_int64(_x) ((int64_t) fr_nbo_to_uint64(_x))
188
189
190/** Read an unsigned 64bit integer from wire format (big endian) with a variable length encoding
191 *
192 * @param[in] data Buffer containing the number.
193 * @param[in] data_len Length of number.
194 * @return a 64 bit unsigned integer of native endianness.
195 */
196static inline uint64_t fr_nbo_to_uint64v(uint8_t const *data, size_t data_len)
197{
198 uint64_t num = 0;
199 uint64_t nbo;
200
201 if (unlikely(data_len > sizeof(uint64_t))) return 0;
202
203 /*
204 * Copy at an offset into memory
205 * allocated for the uin64_t
206 */
207 memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */
208 fr_nbo_from_uint64((uint8_t *)&nbo, num);
209
210 return nbo;
211}
212
213static inline uint64_t fr_nbo_to_int64v(uint8_t const *data, size_t data_len)
214{
215 int64_t num = 0;
216 uint64_t nbo;
217
218 if (unlikely(data_len > sizeof(uint64_t))) return 0;
219
220 /*
221 * Copy at an offset into memory
222 * allocated for the uin64_t
223 */
224 memcpy(((uint8_t *)&num) + (sizeof(uint64_t) - data_len), data, data_len); /* aligned */
225 if (*data & 0x80) memset(((uint8_t *)&num) + data_len, 0xff, sizeof(num) - data_len);
226
227 fr_nbo_from_uint64((uint8_t *)&nbo, num);
228
229 return nbo;
230}
231
232/** Convert bits (as in prefix length) to bytes, rounding up.
233 *
234 * @param bits number of bits in the prefix
235 * @return number of bytes taken to store the prefix
236 */
237static inline unsigned int fr_bytes_from_bits(unsigned int bits)
238{
239 return (bits + 7) >> 3;
240}
241
242#ifdef __cplusplus
243}
244#endif
#define RCSIDH(h, id)
Definition build.h:484
#define unlikely(_x)
Definition build.h:381
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define ROUND_UP_DIV(_x, _y)
Get the ceiling value of integer division.
Definition math.h:153
static uint8_t fr_high_bit_pos(uint64_t num)
Find the highest order high bit in an unsigned 64 bit integer.
Definition math.h:36
unsigned short uint16_t
unsigned int uint32_t
unsigned char uint8_t
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:237
static uint64_t fr_nbo_to_uint64(uint8_t const data[static sizeof(uint64_t)])
Read an unsigned 64bit integer from wire format (big endian)
Definition nbo.h:177
static void fr_nbo_from_uint24(uint8_t out[static 3], uint32_t num)
Write out an unsigned 24bit integer in wire format (big endian)
Definition nbo.h:49
static uint64_t fr_nbo_to_uint64v(uint8_t const *data, size_t data_len)
Read an unsigned 64bit integer from wire format (big endian) with a variable length encoding.
Definition nbo.h:196
static size_t fr_nbo_from_uint64v(uint8_t out[static sizeof(uint64_t)], uint64_t num)
Write out an unsigned 64bit integer in wire format using the fewest bytes possible.
Definition nbo.h:118
static void fr_nbo_from_uint16(uint8_t out[static sizeof(uint16_t)], uint16_t num)
Write out an unsigned 16bit integer in wire format (big endian)
Definition nbo.h:38
static uint64_t fr_nbo_to_int64v(uint8_t const *data, size_t data_len)
Definition nbo.h:213
static uint16_t fr_nbo_to_uint16(uint8_t const data[static sizeof(uint16_t)])
Read an unsigned 16bit integer from wire format (big endian)
Definition nbo.h:146
static void fr_nbo_from_int32(uint8_t out[static sizeof(int32_t)], int32_t num)
Write out an signed 32bit integer in wire format (big endian)
Definition nbo.h:94
static void fr_nbo_from_int16(uint8_t out[static sizeof(int16_t)], int16_t num)
Write out an signed 16bit integer in wire format (big endian)
Definition nbo.h:83
static uint32_t fr_nbo_to_uint24(uint8_t const data[static 3])
Read an unsigned 24bit integer from wire format (big endian)
Definition nbo.h:157
static void fr_nbo_from_int64(uint8_t out[static sizeof(uint64_t)], uint64_t num)
Write out an signed 64bit integer in wire format (big endian)
Definition nbo.h:105
static uint32_t fr_nbo_to_uint32(uint8_t const data[static sizeof(uint32_t)])
Read an unsigned 32bit integer from wire format (big endian)
Definition nbo.h:167
static void fr_nbo_from_uint64(uint8_t out[static sizeof(uint64_t)], uint64_t num)
Write out an unsigned 64bit integer in wire format (big endian)
Definition nbo.h:72
static void fr_nbo_from_uint32(uint8_t out[static sizeof(uint32_t)], uint32_t num)
Write out an unsigned 32bit integer in wire format (big endian)
Definition nbo.h:61
static fr_slen_t data
Definition value.h:1265
static size_t char ** out
Definition value.h:997