The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
sbuff.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/** A generic buffer structure for string printing and parsing strings
19 *
20 * Because doing manual length checks is error prone and a waste of everyones time.
21 *
22 * @file src/lib/util/sbuff.h
23 *
24 * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25 */
26RCSIDH(sbuff_h, "$Id: 3b8307fa2e57a108bf228b9a68b54dd69cfeb0b7 $")
27
28# ifdef __cplusplus
29extern "C" {
30# endif
31
32#include <ctype.h>
33#include <errno.h>
34#include <limits.h>
35#include <stdbool.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <string.h>
39#include <sys/types.h>
40
41/** Represents number of bytes parsed or location of parse error
42 *
43 * Number of bytes parsed will be >= 0.
44 *
45 * If a parse error occurs the value will be the negative offset
46 * of the error -1. i.e. offset 0 will be -1.
47 *
48 * This is to disambiguate between 0 bytes parsed and error at
49 * offset 0.
50 */
52typedef struct fr_sbuff_s fr_sbuff_t;
54
55#include <freeradius-devel/util/atexit.h>
56#include <freeradius-devel/util/strerror.h>
57#include <freeradius-devel/util/table.h>
58#include <freeradius-devel/util/talloc.h>
59
60/** Whether the buffer is currently extendable and whether it was extended
61 *
62 */
63DIAG_OFF(attributes)
64typedef enum CC_HINT(flag_enum) {
65 FR_SBUFF_FLAG_EXTENDED = 0x01, //!< The last call to extend function actually extended the buffer.
66 FR_SBUFF_FLAG_EXTEND_ERROR = 0x02 //!< The last call to an extend function resulted in an error.
67 ///< Error should be provided using fr_strerror_const/fr_strerror_printf
68 ///< by the extension function.
70DIAG_OFF(attributes)
71
72/** Extension callback
73 *
74 * Retrieves additional data from a source and adds it to a buffer.
75 */
76typedef size_t(*fr_sbuff_extend_t)(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t req_extension);
77
78/** For a given extension function, returns whether it is at EOF
79 *
80 */
81typedef bool(*fr_sbuff_eof_t)(fr_sbuff_t *sbuff);
82
83
85 union {
86 char const *p_i; //!< Immutable position pointer.
87 char *p; //!< Mutable position pointer.
88 };
89 fr_sbuff_marker_t *next; //!< Next m in the list.
90 fr_sbuff_t *parent; //!< Owner of the marker
91};
92
93struct fr_sbuff_s {
94 union {
95 char const *buff_i; //!< Immutable buffer pointer.
96 char *buff; //!< Mutable buffer pointer.
97 };
98
99 union {
100 char const *start_i; //!< Immutable start pointer.
101 char *start; //!< Mutable start pointer.
102 };
103
104 union {
105 char const *end_i; //!< Immutable end pointer.
106 char *end; //!< Mutable end pointer.
107 };
108
109 union {
110 char const *p_i; //!< Immutable position pointer.
111 char *p; //!< Mutable position pointer.
112 };
113
114 char const *err; //!< Where the last error occurred.
115
116 uint8_t is_const:1; //!< Can't be modified.
117 uint8_t adv_parent:1; //!< If true, advance the parent.
118 size_t shifted; //!< How many bytes this sbuff has been
119 ///< shifted since its creation.
120
121 fr_sbuff_extend_t extend; //!< Function to re-populate or extend
122 ///< the buffer.
123
124 fr_sbuff_eof_t eof; //!< Function to determine if the buffer is at EOF.
125
126 void *uctx; //!< Extend uctx data.
127
128 fr_sbuff_t *parent; //!< sbuff this sbuff was copied from.
129
130 fr_sbuff_marker_t *m; //!< Pointers to update if the underlying
131 ///< buffer changes.
132};
133
134/** Talloc sbuff extension structure
135 *
136 * Holds the data necessary for creating dynamically
137 * extensible buffers.
138 */
139typedef struct {
140 TALLOC_CTX *ctx; //!< Context to alloc new buffers in.
141 size_t init; //!< How much to allocate initially.
142 size_t max; //!< Maximum size of the buffer.
144
145/** File sbuff extension structure
146 *
147 * Holds the data necessary for creating dynamically
148 * extensible file buffers.
149 */
150typedef struct {
151 FILE *file; //!< FILE * we're reading from.
152 char *buff_end; //!< The true end of the buffer.
153 size_t max; //!< Maximum number of bytes to read.
154 size_t shifted; //!< How much we've read from this file.
155 bool eof; //!< are we at EOF?
157
158/** Terminal element with pre-calculated lengths
159 *
160 */
161typedef struct {
162 char const *str; //!< Terminal string
163 size_t len; //!< Length of string
165
166/** Set of terminal elements
167 *
168 * The elements MUST be listed in sorted order. If the inputs are
169 * not sorted, then all kinds of things will break.
170 */
171typedef struct {
172 size_t len; //!< Length of the list.
173 fr_sbuff_term_elem_t *elem; //!< A sorted list of terminal strings.
175
176/** Initialise a terminal structure with a single string
177 *
178 * @param[in] _str terminal string.
179 */
180#define FR_SBUFF_TERM(_str) \
181(fr_sbuff_term_t){ \
182 .len = 1, \
183 .elem = (fr_sbuff_term_elem_t[]){ L(_str) }, \
184}
185
186/** Initialise a terminal structure with a list of sorted strings
187 *
188 * Strings must be lexicographically sorted.
189 *
190 * @param[in] ... Lexicographically sorted list of terminal strings.
191 */
192#define FR_SBUFF_TERMS(...) \
193(fr_sbuff_term_t){ \
194 .len = (sizeof((fr_sbuff_term_elem_t[]){ __VA_ARGS__ }) / sizeof(fr_sbuff_term_elem_t)), \
195 .elem = (fr_sbuff_term_elem_t[]){ __VA_ARGS__ }, \
196}
197
198/** Set of parsing rules for *unescape_until functions
199 *
200 */
201typedef struct {
202 char const *name; //!< Name for rule set to aid we debugging.
203
204 char chr; //!< Character at the start of an escape sequence.
205 char subs[UINT8_MAX + 1]; //!< Special characters and their substitutions.
206 ///< Indexed by the printable representation i.e.
207 ///< 'n' for \n.
208 bool skip[UINT8_MAX + 1]; //!< Characters that are escaped, but left in the
209 ///< output along with the escape character.
210 ///< This is useful where we need to interpret escape
211 ///< sequences for parsing, but where the string will
212 ///< be passed off to a 3rd party library which will
213 ///< need to interpret the same sequences.
214
215 bool do_hex; //!< Process hex sequences i.e. @verbatim\x<hex><hex>.@endverbatim
216 bool do_oct; //!< Process oct sequences i.e. @verbatim<oct><oct><oct>.@endverbatim
218
219/** Set of parsing rules for *unescape_until functions
220 *
221 */
222typedef struct {
223 char const *name; //!< Name for rule set to aid we debugging.
224
225 char chr; //!< Character at the start of an escape sequence.
226
227 char subs[UINT8_MAX + 1]; //!< Special characters and their substitutions.
228 ///< Indexed by the binary representation i.e.
229 ///< 0x0a for \n.
230 bool esc[UINT8_MAX + 1]; //!< Characters that should be translated to hex or
231 ///< octal escape sequences.
232 bool do_utf8; //!< If true Don't apply escaping rules to valid UTF-8 sequences.
233
234 bool do_hex; //!< Represent escaped chars as hex sequences i.e.
235 ///< @verbatim\x<hex><hex>@endverbatim
236 bool do_oct; //!< Represent escapes chars as octal sequences i.e.
237 ///< @verbatim<oct><oct><oct>@endvertbatim
239
240/** A set of terminal sequences, and escape rules
241 *
242 */
243typedef struct {
244 fr_sbuff_unescape_rules_t const *escapes; //!< Escape characters
245
246 fr_sbuff_term_t const *terminals; //!< Terminal characters used as a hint
247 ///< that a token is not complete.
248} fr_sbuff_parse_rules_t;
249
250/** Standard parsing errors to be used by sbuff functions and other sbuff based parsing functions
251 *
252 */
253typedef enum {
254 FR_SBUFF_PARSE_OK = 0, //!< No error.
255 FR_SBUFF_PARSE_ERROR_NOT_FOUND = -1, //!< String does not contain a token
256 ///< matching the output type.
257 FR_SBUFF_PARSE_ERROR_TRAILING = -2, //!< Trailing characters found.
258 FR_SBUFF_PARSE_ERROR_FORMAT = -3, //!< Format of data was invalid.
259 FR_SBUFF_PARSE_ERROR_OUT_OF_SPACE = -4, //!< No space available in output buffer.
260 FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW = -5, //!< Integer type would overflow.
261 FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW = -6 //!< Integer type would underflow.
263
265extern size_t sbuff_parse_error_table_len;
266
267/** Replace the contents of the thread local error stack with the string representation of a parse error
268 */
269static inline void fr_sbuff_parse_error_to_strerror(fr_sbuff_parse_error_t err)
270{
272}
273
274/** Return whether the sbuff is extendable
275 */
276static inline bool fr_sbuff_is_extendable(fr_sbuff_t *sbuff)
277{
278 return sbuff->extend && (!sbuff->eof || (sbuff->eof(sbuff) == false));
279}
280
281#define fr_sbuff_was_extended(_status) (_status & FR_SBUFF_FLAG_EXTENDED)
282
283extern bool const sbuff_char_class_uint[UINT8_MAX + 1];
284extern bool const sbuff_char_class_int[UINT8_MAX + 1];
285extern bool const sbuff_char_class_float[UINT8_MAX + 1];
286extern bool const sbuff_char_class_zero[UINT8_MAX + 1];
287extern bool const sbuff_char_class_hex[UINT8_MAX + 1];
288extern bool const sbuff_char_alpha_num[UINT8_MAX + 1];
289extern bool const sbuff_char_word[UINT8_MAX + 1];
290extern bool const sbuff_char_whitespace[UINT8_MAX + 1];
291extern bool const sbuff_char_line_endings[UINT8_MAX + 1];
292extern bool const sbuff_char_blank[UINT8_MAX + 1];
293extern bool const sbuff_char_class_hostname[UINT8_MAX + 1];
294
295/** Matches a-z,A-Z
296 */
297#define SBUFF_CHAR_CLASS_ALPHA \
298 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, \
299 ['f'] = true, ['g'] = true, ['h'] = true, ['i'] = true, ['j'] = true, \
300 ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true, \
301 ['p'] = true, ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true, \
302 ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true, ['y'] = true, \
303 ['z'] = true, \
304 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true, \
305 ['F'] = true, ['G'] = true, ['H'] = true, ['I'] = true, ['J'] = true, \
306 ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true, \
307 ['P'] = true, ['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true, \
308 ['U'] = true, ['V'] = true, ['W'] = true, ['X'] = true, ['Y'] = true, \
309 ['Z'] = true
310
311/** Matches 0-9
312 */
313#define SBUFF_CHAR_CLASS_NUM \
314 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true, \
315 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true
316
317/** Matches 0-9,a-z,A-Z
318 */
319#define SBUFF_CHAR_CLASS_ALPHA_NUM \
320 SBUFF_CHAR_CLASS_ALPHA, \
321 SBUFF_CHAR_CLASS_NUM
322
323/** Matches 0-9,a-f,A-F
324 */
325#define SBUFF_CHAR_CLASS_HEX \
326 SBUFF_CHAR_CLASS_NUM, \
327 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, \
328 ['f'] = true, \
329 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true, \
330 ['F'] = true
331
332#define SBUFF_CHAR_CLASS_SYMBOLS \
333 ['!'] = true, ['"'] = true, ['#'] = true, ['$'] = true, ['%'] = true, \
334 ['&'] = true, ['/'] = true, ['('] = true, [')'] = true, ['*'] = true, \
335 ['+'] = true, ['`'] = true, ['-'] = true, ['.'] = true, ['/'] = true, \
336 [':'] = true, [';'] = true, ['<'] = true, ['='] = true, ['>'] = true, \
337 ['?'] = true, ['@'] = true, ['['] = true, ['\''] = true, [']'] = true, \
338 ['^'] = true, ['_'] = true, ['`'] = true, ['{'] = true, ['|'] = true, \
339 ['}'] = true, ['~'] = true \
340
341/*
342 * If the additional tables need to be generated feel free to use this
343 * code snippet.
344 *
345 * @verbatim
346 #include <stdio.h>
347 #include <stdlib.h>
348
349 int main(int argc, char **argv)
350 {
351 int start, end, i;
352 start = atoi(argv[1]);
353 end = atoi(argv[2]);
354 for (i = start; i <= end; i++) {
355 printf("[0x%02x] = true, ", i);
356 if (!(i % 8)) printf("\\\n");
357 }
358 return 0;
359 }
360 * @endverbatim
361 */
362
363/** Unprintables (ascii range)
364 *
365 * We don't include characters in the extended range (128-255) as they're
366 * likely part of a multi-byte sequence and we don't want to break UTF8 strings.
367 */
368#define SBUFF_CHAR_UNPRINTABLES_LOW \
369 [0x00] = true, \
370 [0x01] = true, [0x02] = true, [0x03] = true, [0x04] = true, [0x05] = true, [0x06] = true, [0x07] = true, [0x08] = true, \
371 [0x09] = true, [0x0a] = true, [0x0b] = true, [0x0c] = true, [0x0d] = true, [0x0e] = true, [0x0f] = true, [0x10] = true, \
372 [0x11] = true, [0x12] = true, [0x13] = true, [0x14] = true, [0x15] = true, [0x16] = true, [0x17] = true, [0x18] = true, \
373 [0x19] = true, [0x1a] = true, [0x1b] = true, [0x1c] = true, [0x1d] = true, [0x1e] = true, [0x1f] = true, \
374 [0x7f] = true
375
376/** Unprintables (extended range)
377 *
378 * If these characters are being escaped, the function should also be passed
379 * the 'do_utf8' flag.
380 */
381#define SBUFF_CHAR_UNPRINTABLES_EXTENDED \
382 [0x80] = true, \
383 [0x81] = true, [0x82] = true, [0x83] = true, [0x84] = true, [0x85] = true, [0x86] = true, [0x87] = true, [0x88] = true, \
384 [0x89] = true, [0x8a] = true, [0x8b] = true, [0x8c] = true, [0x8d] = true, [0x8e] = true, [0x8f] = true, [0x90] = true, \
385 [0x91] = true, [0x92] = true, [0x93] = true, [0x94] = true, [0x95] = true, [0x96] = true, [0x97] = true, [0x98] = true, \
386 [0x99] = true, [0x9a] = true, [0x9b] = true, [0x9c] = true, [0x9d] = true, [0x9e] = true, [0x9f] = true, [0xa0] = true, \
387 [0xa1] = true, [0xa2] = true, [0xa3] = true, [0xa4] = true, [0xa5] = true, [0xa6] = true, [0xa7] = true, [0xa8] = true, \
388 [0xa9] = true, [0xaa] = true, [0xab] = true, [0xac] = true, [0xad] = true, [0xae] = true, [0xaf] = true, [0xb0] = true, \
389 [0xb1] = true, [0xb2] = true, [0xb3] = true, [0xb4] = true, [0xb5] = true, [0xb6] = true, [0xb7] = true, [0xb8] = true, \
390 [0xb9] = true, [0xba] = true, [0xbb] = true, [0xbc] = true, [0xbd] = true, [0xbe] = true, [0xbf] = true, [0xc0] = true, \
391 [0xc1] = true, [0xc2] = true, [0xc3] = true, [0xc4] = true, [0xc5] = true, [0xc6] = true, [0xc7] = true, [0xc8] = true, \
392 [0xc9] = true, [0xca] = true, [0xcb] = true, [0xcc] = true, [0xcd] = true, [0xce] = true, [0xcf] = true, [0xd0] = true, \
393 [0xd1] = true, [0xd2] = true, [0xd3] = true, [0xd4] = true, [0xd5] = true, [0xd6] = true, [0xd7] = true, [0xd8] = true, \
394 [0xd9] = true, [0xda] = true, [0xdb] = true, [0xdc] = true, [0xdd] = true, [0xde] = true, [0xdf] = true, [0xe0] = true, \
395 [0xe1] = true, [0xe2] = true, [0xe3] = true, [0xe4] = true, [0xe5] = true, [0xe6] = true, [0xe7] = true, [0xe8] = true, \
396 [0xe9] = true, [0xea] = true, [0xeb] = true, [0xec] = true, [0xed] = true, [0xee] = true, [0xef] = true, [0xf0] = true, \
397 [0xf1] = true, [0xf2] = true, [0xf3] = true, [0xf4] = true, [0xf5] = true, [0xf6] = true, [0xf7] = true, [0xf8] = true, \
398 [0xf9] = true, [0xfa] = true, [0xfb] = true, [0xfc] = true, [0xfd] = true, [0xfe] = true, [0xff] = true
399
400/** Generic wrapper macro to return if there's insufficient memory to satisfy the request on the sbuff
401 *
402 */
403#define FR_SBUFF_RETURN(_func, _sbuff, ...) \
404do { \
405 ssize_t _slen; \
406 _slen = _func(_sbuff, ## __VA_ARGS__ ); \
407 if (_slen < 0) return _slen; \
408} while (0)
409
410/** @name Ephemeral copying macros
411 * @{
412 */
413
414/** @cond */
415
416/** Copy another fr_sbuff_t, modifying it.
417 *
418 * @private
419 */
420#define _FR_SBUFF(_sbuff_or_marker, _start, _current, _end, _extend, _eof, _adv_parent) \
421((fr_sbuff_t){ \
422 .buff = fr_sbuff_buff(_sbuff_or_marker), \
423 .start = (_start), \
424 .end = (_end), \
425 .p = (_current), \
426 .is_const = fr_sbuff_ptr(_sbuff_or_marker)->is_const, \
427 .adv_parent = (_adv_parent), \
428 .shifted = 0, \
429 .extend = (_extend), \
430 .eof = (_eof), \
431 .uctx = fr_sbuff_ptr(_sbuff_or_marker)->uctx, \
432 .parent = fr_sbuff_ptr(_sbuff_or_marker) \
433})
434/* @endcond */
435
436/** Create a new sbuff pointing to the same underlying buffer
437 *
438 * - Parent will _NOT_ be advanced by operations on its child.
439 * - Child will have its `start` pointer set to the `p` pointer of the parent.
440 *
441 * @param[in] _sbuff_or_marker to make an ephemeral copy of.
442 */
443#define FR_SBUFF(_sbuff_or_marker) _FR_SBUFF(_sbuff_or_marker, \
444 fr_sbuff_current(_sbuff_or_marker), \
445 fr_sbuff_current(_sbuff_or_marker), \
446 fr_sbuff_end(_sbuff_or_marker), \
447 fr_sbuff_ptr(_sbuff_or_marker)->extend, \
448 fr_sbuff_ptr(_sbuff_or_marker)->eof, \
449 0x00)
450
451/** Create a new sbuff pointing to the same underlying buffer
452 *
453 * - Parent will _NOT_ be advanced by operations on its child.
454 * - Child will have its `start` pointer set to the `start` pointer of the parent.
455 *
456 * @param[in] _sbuff_or_marker to make an ephemeral copy of.
457 */
458#define FR_SBUFF_ABS(_sbuff_or_marker) _FR_SBUFF(_sbuff_or_marker, \
459 fr_sbuff_start(_sbuff_or_marker), \
460 fr_sbuff_current(_sbuff_or_marker), \
461 fr_sbuff_end(_sbuff_or_marker), \
462 fr_sbuff_ptr(_sbuff_or_marker)->extend, \
463 0x00)
464
465/** Create a new sbuff pointing to the same underlying buffer
466 *
467 * The intent of this sbuff type is to allow parsing operations to be performed
468 * on a subset of the buffer data.
469 *
470 * - Parent will _NOT_ be advanced by operations on its child.
471 * - Child will have its `start` pointer set to the `start` pointer of the parent.
472 * - Child will have its `end` pointer set to the `p` pointer of the parent.
473 * - Child will not extend parent.
474 *
475 * @param[in] _sbuff_or_marker to make an ephemeral copy of.
476 */
477#define FR_SBUFF_REPARSE(_sbuff_or_marker) _FR_SBUFF(_sbuff_or_marker, \
478 fr_sbuff_start(_sbuff_or_marker), \
479 fr_sbuff_start(_sbuff_or_marker), \
480 fr_sbuff_current(_sbuff_or_marker), \
481 NULL, \
482 NULL, \
483 0x00)
484
485/** Create a new sbuff pointing to the same underlying buffer
486 *
487 * - Parent `p` pointer will be advanced with child's `p` pointer.
488 * - Child will have its `start` pointer set to the `p` pointer of the parent.
489 *
490 * @param[in] _sbuff_or_marker to make an ephemeral copy of.
491 */
492#define FR_SBUFF_BIND_CURRENT(_sbuff_or_marker) _FR_SBUFF(_sbuff_or_marker, \
493 fr_sbuff_current(_sbuff_or_marker), \
494 fr_sbuff_current(_sbuff_or_marker), \
495 fr_sbuff_end(_sbuff_or_marker), \
496 fr_sbuff_ptr(_sbuff_or_marker)->extend, \
497 fr_sbuff_ptr(_sbuff_or_marker)->eof, \
498 0x01)
499
500/** Create a new sbuff pointing to the same underlying buffer
501 *
502 * - Parent `p` pointer will be advanced with child's `p` pointer.
503 * - Child will have its `start` pointer set to the `start` pointer of the parent.
504 *
505 * @param[in] _sbuff_or_marker to make an ephemeral copy of.
506 */
507#define FR_SBUFF_BIND_CURRENT_ABS(_sbuff_or_marker) FR_SBUFF_ABS(_sbuff_or_marker, \
508 fr_sbuff_start(_sbuff_or_marker), \
509 fr_sbuff_current(_sbuff_or_marker), \
510 fr_sbuff_end(_sbuff_or_marker), \
511 fr_sbuff_ptr(_sbuff_or_marker)->extend, \
512 fr_sbuff_ptr(_sbuff_or_marker)->eof, \
513 0x01)
514
515/** Creates a compound literal to pass into functions which accept a sbuff
516 *
517 * @note The return value of the function should be used to determine how much
518 * data was written to the buffer.
519 *
520 * @param[in] _start of the buffer.
521 * @param[in] _len_or_end Length of the buffer or the end pointer.
522 */
523#define FR_SBUFF_OUT(_start, _len_or_end) \
524((fr_sbuff_t){ \
525 .buff_i = _start, \
526 .start_i = _start, \
527 .end_i = _Generic((_len_or_end), \
528 size_t : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
529 long : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
530 int : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
531 unsigned int : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
532 char * : (char const *)(_len_or_end), \
533 char const * : (char const *)(_len_or_end) \
534 ), \
535 .p_i = _start, \
536 .is_const = IS_CONST(char *, _start) \
537})
538
539/** Creates a compound literal to pass into functions which accept a sbuff
540 *
541 * @note The return value of the function should be used to determine how much
542 * data was written to the buffer.
543 *
544 * @param[in] _start of the buffer.
545 * @param[in] _len_or_end Length of the buffer or the end pointer.
546 */
547#define FR_SBUFF_IN(_start, _len_or_end) \
548((fr_sbuff_t){ \
549 .buff_i = _start, \
550 .start_i = _start, \
551 .end_i = _Generic((_len_or_end), \
552 size_t : (char const *)(_start) + (size_t)(_len_or_end), \
553 long : (char const *)(_start) + (size_t)(_len_or_end), \
554 int : (char const *)(_start) + (size_t)(_len_or_end), \
555 unsigned int : (char const *)(_start) + (size_t)(_len_or_end), \
556 char * : (char const *)(_len_or_end), \
557 char const * : (char const *)(_len_or_end) \
558 ), \
559 .p_i = _start, \
560 .is_const = IS_CONST(char *, _start) \
561})
562
563/** Structure to encapsulate a thread local sbuff information
564 *
565 */
566typedef struct {
567 fr_sbuff_t sbuff; //!< Thread local sbuff.
568 fr_sbuff_uctx_talloc_t tctx; //!< Thread local tctx.
569} fr_sbuff_thread_local_t;
570
571static inline int _sbuff_thread_local_free(void *sbtl)
572{
573 return talloc_free(sbtl);
574}
575
576/** Create a function local and thread local extensible sbuff
577 *
578 * @param[out] _sbuff_out Where to write a pointer to the thread local sbuff
579 * @param[in] _init Initial size for the sbuff buffer.
580 * @param[in] _max Maximum size of the sbuff buffer.
581 */
582#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max) \
583do { \
584 static _Thread_local fr_sbuff_thread_local_t *_sbuff_t_local; \
585 if (!_sbuff_t_local) { \
586 fr_sbuff_thread_local_t *sbtl = talloc_zero(NULL, fr_sbuff_thread_local_t); \
587 fr_sbuff_init_talloc(sbtl, &sbtl->sbuff, &sbtl->tctx, _init, _max); \
588 fr_atexit_thread_local(_sbuff_t_local, _sbuff_thread_local_free, sbtl); \
589 *(_out) = &_sbuff_t_local->sbuff; \
590 } else { \
591 fr_sbuff_reset_talloc(&_sbuff_t_local->sbuff); \
592 *(_out) = &_sbuff_t_local->sbuff; \
593 } \
594} while (0)
595
596void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len);
597
598size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift);
599
600size_t fr_sbuff_extend_file(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension);
601
602bool fr_sbuff_eof_file(fr_sbuff_t *sbuff);
603
604size_t fr_sbuff_extend_talloc(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension);
605
606int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len);
607
608int fr_sbuff_reset_talloc(fr_sbuff_t *sbuff);
609
610static inline void fr_sbuff_terminate(fr_sbuff_t *sbuff)
611{
612 *sbuff->p = '\0';
613}
614
615static inline void _fr_sbuff_init(fr_sbuff_t *out, char const *start, char const *end, bool is_const, bool nul_term)
616{
617 if (unlikely(end < start)) end = start; /* Could be an assert? */
618
619 *out = (fr_sbuff_t){
620 .buff_i = start,
621 .start_i = start,
622 .p_i = start,
623 .end_i = end,
624 .is_const = is_const
625 };
626
627 if (nul_term) *out->start = '\0';
628}
629
630/*
631 * GCC is stupid and will warn about output variables
632 * being unnitialised, even if they're not dereferenced.
633 */
634#if defined(__GNUC__) && __GNUC__ >= 11
635DIAG_OFF(maybe-uninitialized)
636#endif
637
638/** Initialise an sbuff around a stack allocated buffer for printing
639 *
640 * Will \0 terminate the output buffer.
641 *
642 * @param[out] _out Pointer to buffer.
643 * @param[in] _start Start of the buffer.
644 * @param[in] _len_or_end Either an end pointer or the length
645 * of the buffer.
646 */
647#define fr_sbuff_init_out(_out, _start, _len_or_end) \
648_fr_sbuff_init(_out, _start, \
649_Generic((_len_or_end), \
650 size_t : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
651 long : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
652 int : (char const *)(_start) + ((size_t)(_len_or_end) - 1), \
653 char * : (char const *)(_len_or_end), \
654 char const * : (char const *)(_len_or_end) \
655), \
656IS_CONST(char *, _start), true)
657
658#if defined(__GNUC__) && __GNUC__ >= 11
659DIAG_ON(maybe-uninitialized)
660#endif
661
662/** Initialise an sbuff around a stack allocated buffer for parsing
663 *
664 * @param[out] _out Pointer to buffer.
665 * @param[in] _start Start of the buffer.
666 * @param[in] _len_or_end Either an end pointer or the length
667 * of the buffer.
668 */
669#define fr_sbuff_init_in(_out, _start, _len_or_end) \
670_fr_sbuff_init(_out, _start, \
671_Generic((_len_or_end), \
672 size_t : (char const *)(_start) + (size_t)(_len_or_end), \
673 long : (char const *)(_start) + (size_t)(_len_or_end), \
674 int : (char const *)(_start) + (size_t)(_len_or_end), \
675 char * : (char const *)(_len_or_end), \
676 char const * : (char const *)(_len_or_end) \
677), \
678IS_CONST(char *, _start), false)
679
680/** Initialise a special sbuff which automatically reads in more data as the buffer is exhausted
681 *
682 * @param[out] sbuff to initialise.
683 * @param[out] fctx to initialise. Must have a lifetime >= to the sbuff.
684 * @param[in] buff Temporary buffer to use for storing file contents.
685 * @param[in] len Length of the temporary buffer.
686 * @param[in] file to read from.
687 * @param[in] max The maximum length of data to read from the file.
688 * @return
689 * - The passed sbuff on success.
690 * - NULL on failure.
691 */
692static inline fr_sbuff_t *fr_sbuff_init_file(fr_sbuff_t *sbuff, fr_sbuff_uctx_file_t *fctx,
693 char *buff, size_t len, FILE *file, size_t max)
694{
695 *fctx = (fr_sbuff_uctx_file_t){
696 .file = file,
697 .max = max,
698 .buff_end = buff + len //!< Store the real end
699 };
700
701 *sbuff = (fr_sbuff_t){
702 .buff = buff,
703 .start = buff,
704 .p = buff,
705 .end = buff, //!< Starts with 0 bytes available
706 .extend = fr_sbuff_extend_file,
707 .eof = fr_sbuff_eof_file,
708 .uctx = fctx
709 };
710
711 return sbuff;
712}
713
714/** Initialise a special sbuff which automatically extends as additional data is written
715 *
716 * @param[in] ctx to allocate buffer in.
717 * @param[out] sbuff to initialise.
718 * @param[out] tctx to initialise. Must have a lifetime >= to the sbuff.
719 * @param[in] init The length of the initial buffer, excluding \0 byte.
720 * @param[in] max The maximum length of the buffer.
721 * @return
722 * - The passed sbuff on success.
723 * - NULL on failure.
724 */
725static inline fr_sbuff_t *fr_sbuff_init_talloc(TALLOC_CTX *ctx,
726 fr_sbuff_t *sbuff, fr_sbuff_uctx_talloc_t *tctx,
727 size_t init, size_t max)
728{
729 char *buff;
730
731 *tctx = (fr_sbuff_uctx_talloc_t){
732 .ctx = ctx,
733 .init = init,
734 .max = max
735 };
736
737 /*
738 * Allocate the initial buffer
739 *
740 * We always allocate a buffer so we don't
741 * trigger ubsan errors by performing
742 * arithmetic on NULL pointers.
743 */
744 buff = talloc_zero_array(ctx, char, init + 1);
745 if (!buff) {
746 fr_strerror_printf("Failed allocating buffer of %zu bytes", init + 1);
747 memset(sbuff, 0, sizeof(*sbuff)); /* clang scan */
748 return NULL;
749 }
750
751 *sbuff = (fr_sbuff_t){
752 .buff = buff,
753 .start = buff,
754 .p = buff,
755 .end = buff + init,
756 .extend = fr_sbuff_extend_talloc,
757 .uctx = tctx
758 };
759
760 return sbuff;
761}
762/** @} */
763
764/** @name Accessors
765 *
766 * Caching the values of these pointers or the pointer values from the sbuff
767 * directly is strongly discouraged as they can become invalidated during
768 * stream parsing or when printing to an auto-expanding buffer.
769 *
770 * These functions should only be used to pass sbuff pointers into 3rd party
771 * APIs.
772 *
773 * @{
774 */
775
776/** Return a pointer to the sbuff
777 *
778 * @param[in] _sbuff_or_marker to return a pointer to.
779 * @return A pointer to the sbuff.
780 */
781#define fr_sbuff_ptr(_sbuff_or_marker) \
782 _Generic((_sbuff_or_marker), \
783 fr_sbuff_t * : ((fr_sbuff_t *)(_sbuff_or_marker)), \
784 fr_sbuff_marker_t * : ((fr_sbuff_marker_t *)(_sbuff_or_marker))->parent \
785 )
786
787/** Return a pointer to the start of the underlying buffer in an sbuff or one of its markers
788 *
789 * @param[in] _sbuff_or_marker to return the buffer for.
790 * @return A pointer to the start of the buffer.
791 */
792#define fr_sbuff_buff(_sbuff_or_marker) \
793 _Generic((_sbuff_or_marker), \
794 fr_sbuff_t * : ((fr_sbuff_t const *)(_sbuff_or_marker))->buff, \
795 fr_sbuff_t const * : ((fr_sbuff_t const *)(_sbuff_or_marker))->buff, \
796 fr_sbuff_marker_t * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->buff, \
797 fr_sbuff_marker_t const * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->buff \
798 )
799
800/** Return a pointer to the 'start' position of an sbuff or one of its markers
801 *
802 * The start position is not necessarily the start of the buffer, and is
803 * advanced every time an sbuff is copied.
804 *
805 * @param[in] _sbuff_or_marker to return the start position of.
806 * @return A pointer to the start position of the buffer.
807 */
808#define fr_sbuff_start(_sbuff_or_marker) \
809 (_Generic((_sbuff_or_marker), \
810 fr_sbuff_t * : ((fr_sbuff_t const *)(_sbuff_or_marker))->start, \
811 fr_sbuff_t const * : ((fr_sbuff_t const *)(_sbuff_or_marker))->start, \
812 fr_sbuff_marker_t * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->start, \
813 fr_sbuff_marker_t const * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->start \
814 ))
815
816/** Return a pointer to the 'current' position of an sbuff or one of its markers
817 *
818 * @note Should not be derferenced as it may point past the end of the buffer.
819 * Use #fr_sbuff_char to get the current char.
820 *
821 * @param[in] _sbuff_or_marker to return the current position of.
822 * @return A pointer to the current position of the buffer or marker.
823 */
824#define fr_sbuff_current(_sbuff_or_marker) \
825 (_Generic((_sbuff_or_marker), \
826 fr_sbuff_t * : ((fr_sbuff_t const *)(_sbuff_or_marker))->p, \
827 fr_sbuff_t const * : ((fr_sbuff_t const *)(_sbuff_or_marker))->p, \
828 fr_sbuff_marker_t * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->p, \
829 fr_sbuff_marker_t const * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->p \
830 ))
831
832/** Return a pointer to the 'end' position of an sbuff or one of its markers
833 *
834 * @param[in] _sbuff_or_marker to return the end position of.
835 * @return A pointer to the end position of the buffer or marker.
836 */
837#define fr_sbuff_end(_sbuff_or_marker) \
838 (_Generic((_sbuff_or_marker), \
839 fr_sbuff_t * : ((fr_sbuff_t const *)(_sbuff_or_marker))->end, \
840 fr_sbuff_t const * : ((fr_sbuff_t const *)(_sbuff_or_marker))->end, \
841 fr_sbuff_marker_t * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->end, \
842 fr_sbuff_marker_t const * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->end \
843 ))
844
845/** Return the value of the shifted field
846 *
847 * @param[in] _sbuff_or_marker to return the position of.
848 * @return the number of bytes the buffer has been shifted.
849 */
850#define fr_sbuff_shifted(_sbuff_or_marker) \
851 (_Generic((_sbuff_or_marker), \
852 fr_sbuff_t * : ((fr_sbuff_t const *)(_sbuff_or_marker))->shifted, \
853 fr_sbuff_t const * : ((fr_sbuff_t const *)(_sbuff_or_marker))->shifted, \
854 fr_sbuff_marker_t * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->shifted, \
855 fr_sbuff_marker_t const * : ((fr_sbuff_marker_t const *)(_sbuff_or_marker))->parent->shifted \
856 ))
857
858/** Return the current char pointed to by the sbuff or '\0' if no more chars remain
859 *
860 * @note Should be used in place of #fr_sbuff_current if switching over the current char.
861 *
862 * @param[in] _sbuff_or_marker to return the current char from.
863 * @param[in] _eob char used to indicate End of Buffer, usually '\0'.
864 * @return The current char pointed to be the sbuff.
865 */
866#define fr_sbuff_char(_sbuff_or_marker, _eob) \
867 (fr_sbuff_current(_sbuff_or_marker) >= fr_sbuff_end(_sbuff_or_marker) ? _eob : *fr_sbuff_current(_sbuff_or_marker))
868
869/** Start a switch block over the current sbuff char
870 *
871 * @note '\0' is used to indicate EOB.
872 *
873 * @param[in] _sbuff_or_marker to return the current char from.
874 * @param[in] _eob char used to indicate End of Buffer, usually '\0'.
875 */
876#define fr_sbuff_switch(_sbuff_or_marker, _eob) \
877 switch (fr_sbuff_char(_sbuff_or_marker, _eob))
878/** @} */
879
880/** @name Length calculations
881 * @{
882 */
883
884/** Return the difference in position between the two sbuffs or markers
885 *
886 * @param[in] _a The first sbuff or marker.
887 * @param[in] _b The second sbuff or marker.
888 * @return
889 * - >0 the number of bytes _a is ahead of _b.
890 * - 0 _a and _b are the same position.
891 * - <0 the number of bytes _a is behind of _b.
892 */
893#define fr_sbuff_diff(_a, _b) \
894 ((ssize_t)(fr_sbuff_current(_a) - fr_sbuff_current(_b)))
895
896/** Return the number of bytes remaining between the sbuff or marker and the end of the buffer
897 *
898 * @note Do not use this in functions that may be used for stream parsing
899 * unless you're sure you know what you're doing.
900 * The value return does not reflect the number of bytes that may
901 * be potentially read from the stream, only the number of bytes
902 * until the end of the current chunk.
903 *
904 * @param[in] _sbuff_or_marker to return the number of bytes remaining for.
905 * @return
906 * - >0 the number of bytes remaining before we reach the end of the buffer.
907 * - -0 we're at the end of the buffer.
908 */
909#define fr_sbuff_remaining(_sbuff_or_marker) \
910 ((size_t)(fr_sbuff_end(_sbuff_or_marker) < fr_sbuff_current(_sbuff_or_marker) ? \
911 0 : (fr_sbuff_end(_sbuff_or_marker) - fr_sbuff_current(_sbuff_or_marker))))
912
913/** Return the number of bytes remaining between the start of the sbuff or marker and the current position
914 *
915 * @param[in] _sbuff_or_marker to return the number of bytes used for.
916 * @return
917 * - >0 the number of bytes the current position has advanced past the start.
918 * - -0 the current position is at the start of the buffer.
919 */
920#define fr_sbuff_used(_sbuff_or_marker) \
921 ((size_t)(fr_sbuff_start(_sbuff_or_marker) > fr_sbuff_current(_sbuff_or_marker) ? \
922 0 : (fr_sbuff_current(_sbuff_or_marker) - fr_sbuff_start(_sbuff_or_marker))))
923
924/** Sets an error marker in the parent
925 *
926 * If an error already exists at this level it will be used instead of the provided error.
927 *
928 * @param[in] sbuff who's parent we'll set the error marker in.
929 * @param[in] err marker to set.
930 * @return <0 the negative offset of the error.
931 */
932static inline fr_slen_t _fr_sbuff_error(fr_sbuff_t *sbuff, char const *err)
933{
934 fr_sbuff_t *parent = sbuff->parent;
935 fr_slen_t slen;
936
937 if (sbuff->err) err = sbuff->err;
938 if (parent) parent->err = err;
939
940 slen = -((err - fr_sbuff_start(sbuff)) + 1);
941
942#ifdef __clang_analyzer__
943 /*
944 * Convince clang that the return value
945 * is always negative. It never can be
946 * else the sbuff code is very broken.
947 */
948 if (slen >= 0) return -1;
949#endif
950
951 return slen;
952}
953
954/** Return the current position as an error marker
955 *
956 * @param[in] _sbuff_or_marker Error marker will be set from the current position of this sbuff.
957 *
958 * +1 is added to the position to disambiguate with 0 meaning "parsed no data".
959 *
960 * An error at offset 0 will be returned as -1.
961 */
962#define fr_sbuff_error(_sbuff_or_marker) \
963 _fr_sbuff_error(fr_sbuff_ptr(_sbuff_or_marker), fr_sbuff_current(_sbuff_or_marker))
964
965/** Like fr_sbuff_used, but adjusts for the value returned for the amount shifted
966 *
967 * @param[in] _sbuff_or_marker to return the number of bytes used for.
968 * @return
969 * - >0 the number of bytes the current position has advanced past the start +
970 * the amount the buffer has shifted.
971 * - -0 the current position is at the start of the buffer (and hasn't been shifted).
972 */
973#define fr_sbuff_used_total(_sbuff_or_marker) \
974 ((size_t)((fr_sbuff_current(_sbuff_or_marker) + fr_sbuff_shifted(_sbuff_or_marker)) - fr_sbuff_start(_sbuff_or_marker)))
975
976/** The length of the underlying buffer
977 *
978 * @param[in] _sbuff_or_marker to return the length of.
979 * @return The length of the underlying buffer (minus 1 byte for \0).
980 */
981#define fr_sbuff_len(_sbuff_or_marker) \
982 ((size_t)(fr_sbuff_end(_sbuff_or_marker) - fr_sbuff_buff(_sbuff_or_marker)))
983
984/** How many bytes the sbuff or marker is behind its parent
985 *
986 * @param[in] _sbuff_or_marker
987 * @return
988 * - 0 the sbuff or marker is ahead of its parent.
989 * - >0 the number of bytes the marker is behind its parent.
990 */
991#define fr_sbuff_behind(_sbuff_or_marker) \
992 (fr_sbuff_current(_sbuff_or_marker) > fr_sbuff_current((_sbuff_or_marker)->parent) ? \
993 0 : fr_sbuff_current((_sbuff_or_marker)->parent) - fr_sbuff_current(_sbuff_or_marker))
994
995/** How many bytes the sbuff or marker is ahead of its parent
996 *
997 * @return
998 * - 0 the sbuff or marker is behind its parent.
999 * - >0 the number of bytes the marker is ahead of its parent.
1000 */
1001#define fr_sbuff_ahead(_sbuff_or_marker) \
1002 (fr_sbuff_current((_sbuff_or_marker)->parent) > fr_sbuff_current(_sbuff_or_marker) ? \
1003 0 : fr_sbuff_current(_sbuff_or_marker) - fr_sbuff_current((_sbuff_or_marker)->parent))
1004
1005/** Return the current position in the sbuff as a negative offset
1006 *
1007 */
1008#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker) return fr_sbuff_error(_sbuff_or_marker)
1009
1010/** Check if _len bytes are available in the sbuff, and if not return the number of bytes we'd need
1011 *
1012 */
1013#define FR_SBUFF_CHECK_REMAINING_RETURN(_sbuff, _len) \
1014 if ((_len) > fr_sbuff_remaining(_sbuff)) return -((_len) - fr_sbuff_remaining(_sbuff))
1015
1016static inline size_t _fr_sbuff_extend_lowat(fr_sbuff_extend_status_t *status, fr_sbuff_t *in, size_t remaining, size_t lowat)
1017{
1018 size_t extended;
1019 fr_sbuff_extend_status_t our_status = 0;
1020
1021 if (!fr_sbuff_is_extendable(in)) {
1022 no_extend:
1023 if (status) *status = our_status;
1024 return remaining;
1025 }
1026
1027 /* Still have data remaining, no need to try and extend */
1028 if (remaining >= lowat) goto no_extend;
1029
1030 if (!in->extend || ((extended = in->extend(&our_status, in, lowat - remaining)) == 0)) {
1031 goto no_extend;
1032 }
1033
1034 our_status |= FR_SBUFF_FLAG_EXTENDED;
1035
1036 if (status) *status = our_status;
1037 return remaining + extended;
1038}
1039
1040/** Extend a buffer if we're below the low water mark
1041 *
1042 * @param[out] _status Should be initialised to FR_SBUFF_EXTENDABLE
1043 * for the first call to this function if used
1044 * as a loop condition.
1045 * Will be filled with the result of the previous
1046 * call, and can be used to determine if the buffer
1047 * was extended.
1048 * @param[in] _sbuff_or_marker to extend.
1049 * @param[in] _lowat If bytes remaining are below the amount, extend.
1050 * @return
1051 * - 0 if there are no bytes left in the buffer and we couldn't extend.
1052 * - >0 the number of bytes in the buffer after extending.
1053 */
1054#define fr_sbuff_extend_lowat(_status, _sbuff_or_marker, _lowat) \
1055 _fr_sbuff_extend_lowat(_status, \
1056 fr_sbuff_ptr(_sbuff_or_marker), \
1057 fr_sbuff_remaining(_sbuff_or_marker), _lowat)
1058
1059/** Check if _len bytes are available in the sbuff and extend the buffer if possible
1060 *
1061 * If we do not have _len bytes in the sbuff after extending, then return.
1062 *
1063 * @param[in] _sbuff to extend.
1064 * @param[in] _len The minimum amount the sbuff should be extended by.
1065 * @return The number of bytes we would need to satisfy _len as a negative integer.
1066 */
1067#define FR_SBUFF_EXTEND_LOWAT_OR_RETURN(_sbuff, _len) \
1068do { \
1069 size_t _remaining = fr_sbuff_extend_lowat(NULL, _sbuff, _len); \
1070 if (_remaining < _len) return -(_len - _remaining); \
1071} while (0)
1072
1073/** Extend a buffer if no space remains
1074 *
1075 * @param[in] _sbuff_or_marker to extend.
1076 * @return
1077 * - 0 if there are no bytes left in the buffer and we couldn't extend.
1078 * - >0 the number of bytes in the buffer after extending.
1079 */
1080#define fr_sbuff_extend(_sbuff_or_marker) fr_sbuff_extend_lowat(NULL, _sbuff_or_marker, 1)
1081
1082/** @} */
1083
1084/** @name Position modification (recursive)
1085 *
1086 * Change the current position of pointers in the sbuff and their children.
1087 * @{
1088 */
1089
1090/** Update the position of p in a list of sbuffs
1091 *
1092 * @note Do not call directly.
1093 */
1094static inline void _fr_sbuff_set_recurse(fr_sbuff_t *sbuff, char const *p)
1095{
1096 sbuff->p_i = p;
1097 sbuff->err = NULL; /* Modifying the position of the sbuff clears the error */
1098
1099 if (sbuff->adv_parent && sbuff->parent) _fr_sbuff_set_recurse(sbuff->parent, p);
1100}
1101
1102static inline ssize_t _fr_sbuff_marker_set(fr_sbuff_marker_t *m, char const *p)
1103{
1104 fr_sbuff_t *sbuff = m->parent;
1105 char *current = m->p;
1106
1107 if (unlikely(p > sbuff->end)) return -(p - sbuff->end);
1108 if (unlikely(p < sbuff->start)) return 0;
1109
1110 sbuff->err = NULL; /* Modifying the position of any markers clears the error, unsure if this is correct? */
1111 m->p_i = p;
1112
1113 return p - current;
1114}
1115
1116static inline ssize_t _fr_sbuff_set(fr_sbuff_t *sbuff, char const *p)
1117{
1118 char const *c;
1119
1120 if (unlikely(p > sbuff->end)) return -(p - sbuff->end);
1121 if (unlikely(p < sbuff->start)) return 0;
1122
1123 c = sbuff->p;
1124 _fr_sbuff_set_recurse(sbuff, p);
1125
1126 return p - c;
1127}
1128
1129/** Set the position in a sbuff using another sbuff, a char pointer, or a length
1130 *
1131 * @param[in] _dst sbuff or marker to set the position for.
1132 * @param[in] _src Variable to glean new position from. Behaviour here
1133 * depends on the type of the variable.
1134 * - sbuff, the current position of the sbuff.
1135 * - marker, the current position of the marker.
1136 * - pointer, the position of the pointer.
1137 * - size_t, _dst->start + _src.
1138 * @return
1139 * - 0 not advanced.
1140 * - >0 the number of bytes the sbuff was advanced by.
1141 * - <0 the number of bytes required to complete the advancement
1142 */
1143#define fr_sbuff_set(_dst, _src) \
1144_Generic((_dst), \
1145 fr_sbuff_t * : _fr_sbuff_set, \
1146 fr_sbuff_marker_t * : _fr_sbuff_marker_set \
1147)(_dst, \
1148_Generic((_src), \
1149 fr_sbuff_t * : fr_sbuff_current((fr_sbuff_t const *)(_src)), \
1150 fr_sbuff_t const * : fr_sbuff_current((fr_sbuff_t const *)(_src)), \
1151 fr_sbuff_marker_t * : fr_sbuff_current((fr_sbuff_marker_t const *)(_src)), \
1152 fr_sbuff_marker_t const * : fr_sbuff_current((fr_sbuff_marker_t const *)(_src)), \
1153 char const * : (char const *)(_src), \
1154 char * : (char const *)(_src), \
1155 size_t : (fr_sbuff_start(_dst) + (uintptr_t)(_src)) \
1156))
1157
1158/** Set an sbuff to a given position, and return how much it was advanced
1159 *
1160 * @param[in] _dst to modify.
1161 * @param[in] _src position to set it too.
1162 * @return The number of bytes _dst was advanced.
1163 */
1164#define FR_SBUFF_SET_RETURN(_dst, _src) return fr_sbuff_set(_dst, _src)
1165
1166/** Advance position in sbuff by N bytes
1167 *
1168 * @param[in] _sbuff_or_marker to advance.
1169 * @param[in] _len How much to advance sbuff by.
1170 * @return
1171 * - 0 not advanced.
1172 * - >0 the number of bytes the sbuff was advanced by.
1173 * - <0 the number of bytes required to complete the advancement
1174 */
1175#define fr_sbuff_advance(_sbuff_or_marker, _len) fr_sbuff_set(_sbuff_or_marker, (fr_sbuff_current(_sbuff_or_marker) + (_len)))
1176#define FR_SBUFF_ADVANCE_RETURN(_sbuff, _len) FR_SBUFF_RETURN(fr_sbuff_advance, _sbuff, _len)
1177
1178/** Reset the current position of the sbuff to the start of the string
1179 *
1180 */
1181static inline void fr_sbuff_set_to_start(fr_sbuff_t *sbuff)
1182{
1183 _fr_sbuff_set_recurse(sbuff, sbuff->start);
1184}
1185
1186/** Reset the current position of the sbuff to the end of the string
1187 *
1188 */
1189static inline void fr_sbuff_set_to_end(fr_sbuff_t *sbuff)
1190{
1191 _fr_sbuff_set_recurse(sbuff, sbuff->end);
1192}
1193/** @} */
1194
1195/** @name Add a marker to an sbuff
1196 *
1197 * Markers are used to indicate an area of the code is working at a particular
1198 * point in a string buffer.
1199 *
1200 * If the sbuff is performing stream parsing, then markers are used to update
1201 * any pointers to the buffer, as the data in the buffer is shifted to make
1202 * room for new data from the stream.
1203 *
1204 * If the sbuff is being used to create strings, then the markers are updated
1205 * if the buffer is re-allocated.
1206 * @{
1207 */
1208
1209/** Adds a new pointer to the beginning of the list of pointers to update
1210 *
1211 * @param[out] m to initialise.
1212 * @param[in] sbuff to associate marker with.
1213 * @return The position the marker was set to.
1214 */
1215static inline char *fr_sbuff_marker(fr_sbuff_marker_t *m, fr_sbuff_t *sbuff)
1216{
1217 *m = (fr_sbuff_marker_t){
1218 .next = sbuff->m, /* Link into the head */
1219 .p = sbuff->p, /* Set the current position in the sbuff */
1220 .parent = sbuff /* Record which sbuff this marker was associated with */
1221 };
1222 sbuff->m = m;
1223
1224 return sbuff->p;
1225}
1226
1227/** Used to update the position of an 'end' position marker
1228 *
1229 * Updates a marker so that it represents a 'constrained' end.
1230 * If max > fr_sbuff_remaining() + fr_sbuff_used_total(), then the marker will
1231 * be set to the end of the sbuff.
1232 *
1233 * Otherwise the marker will be set to the position indicated by
1234 * start + (max - fr_sbuff_used_total()).
1235 *
1236 * This is used to add a constraint on the amount data that can be copied from
1237 * an extendable buffer.
1238 *
1239 * @param[in] m Marker created with #fr_sbuff_marker.
1240 * @param[in] max amount of data we allow to be read from
1241 * the sbuff. May be SIZE_MAX in which
1242 * case there is no constraint imposed
1243 * and the marker is set to the end of the buffer.
1244 */
1245static inline char *fr_sbuff_marker_update_end(fr_sbuff_marker_t *m, size_t max)
1246{
1247 fr_sbuff_t *sbuff = m->parent;
1248 size_t used = fr_sbuff_used_total(sbuff);
1249
1250 m->p = (((max) - (used)) > fr_sbuff_remaining(sbuff) ?
1251 fr_sbuff_end(sbuff) :
1252 fr_sbuff_current(sbuff) + ((max) - (used)));
1253
1254 return m->p;
1255}
1256
1257/** Trims the linked list back to the specified pointer
1258 *
1259 * Pointers should be released in the inverse order to allocation.
1260 *
1261 * Alternatively the oldest pointer can be released, resulting in any newer pointer
1262 * also being removed from the list.
1263 *
1264 * @param[in] m to release.
1265 */
1266static inline void fr_sbuff_marker_release(fr_sbuff_marker_t *m)
1267{
1268 m->parent->m = m->next;
1269
1270#ifndef NDEBUF
1271 memset(m, 0, sizeof(*m)); /* Use after release */
1272#endif
1273}
1274
1275/** Trims the linked list back to the specified pointer and return how many bytes marker was behind p
1276 *
1277 * Pointers should be released in the inverse order to allocation.
1278 *
1279 * Alternatively the oldest pointer can be released, resulting in any newer pointer
1280 * also being removed from the list.
1281 *
1282 * @param[in] m to release.
1283 * @return
1284 * - 0 marker is ahead of p.
1285 * - >0 the number of bytes the marker is behind p.
1286 */
1287static inline size_t fr_sbuff_marker_release_behind(fr_sbuff_marker_t *m)
1288{
1289 size_t len = fr_sbuff_behind(m);
1290 fr_sbuff_marker_release(m);
1291 return len;
1292}
1293
1294/** Trims the linked list back to the specified pointer and return how many bytes marker was ahead of p
1295 *
1296 * Pointers should be released in the inverse order to allocation.
1297 *
1298 * Alternatively the oldest pointer can be released, resulting in any newer pointer
1299 * also being removed from the list.
1300 *
1301 * @param[in] m to release.
1302 * @return
1303 * - 0 marker is ahead of p.
1304 * - >0 the number of bytes the marker is behind p.
1305 */
1306static inline size_t fr_sbuff_marker_release_ahead(fr_sbuff_marker_t *m)
1307{
1308 size_t len = fr_sbuff_ahead(m);
1309 fr_sbuff_marker_release(m);
1310 return len;
1311}
1312
1313/** Trims the linked list back to the specified pointer and return how many bytes marker was behind p
1314 *
1315 * Pointers should be released in the inverse order to allocation.
1316 *
1317 * Alternatively the oldest pointer can be released, resulting in any newer pointer
1318 * also being removed from the list.
1319 *
1320 * @param[in] m to release.
1321 * @return
1322 * - 0 marker is ahead of p.
1323 * - >0 the number of bytes the marker is behind p.
1324 */
1325static inline size_t fr_sbuff_marker_release_reset_behind(fr_sbuff_marker_t *m)
1326{
1327 size_t len = fr_sbuff_behind(m);
1328 fr_sbuff_set(m->parent, m);
1329 fr_sbuff_marker_release(m);
1330 return len;
1331}
1332
1333/** Trims the linked list back to the specified pointer and return how many bytes marker was ahead of p
1334 *
1335 * Pointers should be released in the inverse order to allocation.
1336 *
1337 * Alternatively the oldest pointer can be released, resulting in any newer pointer
1338 * also being removed from the list.
1339 *
1340 * @param[in] m to release.
1341 * @return
1342 * - 0 marker is ahead of p.
1343 * - >0 the number of bytes the marker is behind p.
1344 */
1345static inline size_t fr_sbuff_marker_release_reset_ahead(fr_sbuff_marker_t *m)
1346{
1347 size_t len = fr_sbuff_ahead(m);
1348 fr_sbuff_set(m->parent, m);
1349 fr_sbuff_marker_release(m);
1350 return len;
1351}
1352/** @} */
1353
1354/** @name Copy data between an sbuff/marker
1355 *
1356 * These functions are typically used for moving data between sbuffs
1357 *
1358 * @{
1359 */
1360size_t _fr_sbuff_move_sbuff_to_sbuff(fr_sbuff_t *out, fr_sbuff_t *in, size_t len);
1361
1362size_t _fr_sbuff_move_marker_to_sbuff(fr_sbuff_t *out, fr_sbuff_marker_t *in, size_t len);
1363
1364size_t _fr_sbuff_move_marker_to_marker(fr_sbuff_marker_t *out, fr_sbuff_marker_t *in, size_t len);
1365
1366size_t _fr_sbuff_move_sbuff_to_marker(fr_sbuff_marker_t *out, fr_sbuff_t *in, size_t len);
1367
1368/** Copy in as many bytes as possible from one sbuff or marker to another
1369 *
1370 * @param[in] _out to copy into.
1371 * @param[in] _in to copy from.
1372 * @param[in] _len The maximum length to copy.
1373 * @return Number of bytes copied.
1374 */
1375#define fr_sbuff_move(_out, _in, _len) \
1376 _Generic((_out), \
1377 fr_sbuff_t * : \
1378 _Generic((_in), \
1379 fr_sbuff_t * : _fr_sbuff_move_sbuff_to_sbuff((fr_sbuff_t *)_out, (fr_sbuff_t *)_in, _len), \
1380 fr_sbuff_marker_t * : _fr_sbuff_move_marker_to_sbuff((fr_sbuff_t *)_out, (fr_sbuff_marker_t *)_in, _len) \
1381 ), \
1382 fr_sbuff_marker_t * : \
1383 _Generic((_in), \
1384 fr_sbuff_t * : _fr_sbuff_move_sbuff_to_marker((fr_sbuff_marker_t *)_out, (fr_sbuff_t *)_in, _len), \
1385 fr_sbuff_marker_t * : _fr_sbuff_move_marker_to_marker((fr_sbuff_marker_t *)_out, (fr_sbuff_marker_t *)_in, _len) \
1386 ) \
1387 )
1388/** @} */
1389
1390/** @name Copy/print complete input data to an sbuff
1391 *
1392 * These functions are typically used for printing.
1393 *
1394 * @{
1395 */
1396#define fr_sbuff_in_char(_sbuff, ...) fr_sbuff_in_bstrncpy(_sbuff, ((char []){ __VA_ARGS__ }), sizeof((char []){ __VA_ARGS__ }))
1397#define FR_SBUFF_IN_CHAR_RETURN(_sbuff, ...) FR_SBUFF_RETURN(fr_sbuff_in_bstrncpy, _sbuff, ((char []){ __VA_ARGS__ }), sizeof((char []){ __VA_ARGS__ }))
1398
1399ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str);
1400#define FR_SBUFF_IN_STRCPY_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_strcpy, ##__VA_ARGS__)
1401
1402ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len);
1403#define FR_SBUFF_IN_BSTRNCPY_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_bstrncpy, ##__VA_ARGS__)
1404
1405#define fr_sbuff_in_strcpy_literal(_sbuff, _str) fr_sbuff_in_bstrncpy(_sbuff, _str, sizeof(_str) - 1)
1406#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str) FR_SBUFF_RETURN(fr_sbuff_in_bstrncpy, _sbuff, _str, sizeof(_str) - 1)
1407
1408ssize_t fr_sbuff_in_bstrcpy_buffer(fr_sbuff_t *sbuff, char const *str);
1409#define FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_bstrcpy_buffer, ##__VA_ARGS__)
1410
1411ssize_t fr_sbuff_in_vsprintf(fr_sbuff_t *sbuff, char const *fmt, va_list ap);
1412#define FR_SBUFF_IN_VSPRINTF_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_vsprintf, ##__VA_ARGS__)
1413
1414ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt, ...);
1415#define FR_SBUFF_IN_SPRINTF_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_sprintf, ##__VA_ARGS__)
1416
1417ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules);
1418#define FR_SBUFF_IN_ESCAPE_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_escape, ##__VA_ARGS__)
1419
1420ssize_t fr_sbuff_in_escape_buffer(fr_sbuff_t *sbuff, char const *in, fr_sbuff_escape_rules_t const *e_rules);
1421#define FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(...) FR_SBUFF_RETURN(fr_sbuff_in_escape_buffer, ##__VA_ARGS__)
1422
1423/** Lookup a string in a table using an integer value, and copy it to the sbuff
1424 *
1425 * @param[out] _slen Where to write the return value.
1426 * @param[in] _sbuff to search in.
1427 * @param[in] _table to search for number in.
1428 * @param[in] _number to search for.
1429 * @param[in] _def Default string value.
1430 */
1431#define fr_sbuff_in_table_str(_slen, _sbuff, _table, _number, _def) \
1432 _slen = fr_sbuff_in_strcpy(_sbuff, fr_table_str_by_value(_table, _number, _def))
1433#define FR_SBUFF_IN_TABLE_STR_RETURN(_sbuff, _table, _number, _def) \
1434do { \
1435 ssize_t _slen; \
1436 fr_sbuff_in_table_str(_slen, _sbuff, _table, _number, _def); \
1437 if (_slen < 0) return _slen; \
1438} while (0)
1439/** @} */
1440
1441/** @name Copy data out of an sbuff
1442 *
1443 * These functions are typically used for parsing.
1444 *
1445 * @{
1446 */
1447
1448/** Toggle any chars to 'true' in out, that were present in, out or in
1449 *
1450 */
1451static inline void fr_sbuff_allowed_merge(bool out[static UINT8_MAX + 1], bool const in[static UINT8_MAX + 1])
1452{
1453 for (size_t i = 0; i <= UINT8_MAX; i++) out[i] = out[i] || in[i];
1454}
1455
1456fr_sbuff_term_t *fr_sbuff_terminals_amerge(TALLOC_CTX *ctx,
1457 fr_sbuff_term_t const *a, fr_sbuff_term_t const *b);
1458
1459size_t fr_sbuff_out_bstrncpy(fr_sbuff_t *out, fr_sbuff_t *in, size_t len);
1460
1461ssize_t fr_sbuff_out_bstrncpy_exact(fr_sbuff_t *out, fr_sbuff_t *in, size_t len);
1462
1463size_t fr_sbuff_out_bstrncpy_allowed(fr_sbuff_t *out, fr_sbuff_t *in, size_t len,
1464 bool const allowed[static UINT8_MAX + 1]);
1465
1466size_t fr_sbuff_out_bstrncpy_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len,
1467 fr_sbuff_term_t const *tt,
1468 fr_sbuff_unescape_rules_t const *u_rules);
1469
1470size_t fr_sbuff_out_unescape_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len,
1471 fr_sbuff_term_t const *tt,
1472 fr_sbuff_unescape_rules_t const *u_rules);
1473
1474/** Find the longest prefix in an sbuff
1475 *
1476 * @param[out] _match_len The length of the matched string.
1477 * May be NULL.
1478 * @param[out] _out The value resolved in the table.
1479 * @param[in] _table to find longest match in.
1480 * @param[in] _sbuff containing the needle.
1481 * @param[in] _def Default value if no match is found.
1482 */
1483#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def) \
1484do { \
1485 size_t _match_len_tmp; \
1486 fr_sbuff_extend_lowat(NULL, _sbuff, fr_table_max_needle_len(_table)); \
1487 *(_out) = fr_table_value_by_longest_prefix(&_match_len_tmp, _table, \
1488 fr_sbuff_current(_sbuff), fr_sbuff_remaining(_sbuff), \
1489 _def); \
1490 (void) fr_sbuff_advance(_sbuff, _match_len_tmp); /* can't fail */ \
1491 *(_match_len) = _match_len_tmp; \
1492} while (0)
1493
1494/** Build a talloc wrapper function for a fr_sbuff_out_* function
1495 *
1496 * @param[in] _func to call.
1497 * @param[in] _in input sbuff arg.
1498 * @param[in] _len expected output len.
1499 * @param[in] ... additional arguments to pass to _func.
1500 */
1501#define SBUFF_OUT_TALLOC_ERR_FUNC_DEF(_func, _in, _len, ...) \
1502{ \
1503 fr_sbuff_t sbuff; \
1504 fr_sbuff_uctx_talloc_t tctx; \
1505 fr_sbuff_parse_error_t err; \
1506 fr_slen_t slen = -1; \
1507 if (unlikely(fr_sbuff_init_talloc(ctx, &sbuff, &tctx, \
1508 ((_len) != SIZE_MAX) ? (_len) : 1024, \
1509 ((_len) != SIZE_MAX) ? (_len) : SIZE_MAX) == NULL)) { \
1510 error: \
1511 TALLOC_FREE(sbuff.buff); \
1512 *out = NULL; \
1513 return slen; \
1514 } \
1515 slen = _func(&err, &sbuff, _in, _len, ##__VA_ARGS__); \
1516 if (slen < 0) goto error; \
1517 if (unlikely(fr_sbuff_trim_talloc(&sbuff, SIZE_MAX) < 0)) { \
1518 slen = -1; \
1519 goto error; \
1520 } \
1521 *out = sbuff.buff; \
1522 return slen; \
1523}
1524
1525/** Build a talloc wrapper function for a fr_sbuff_out_* function
1526 *
1527 * @param[in] _func to call.
1528 * @param[in] _in input sbuff arg.
1529 * @param[in] _len expected output len.
1530 * @param[in] ... additional arguments to pass to _func.
1531 */
1532#define SBUFF_OUT_TALLOC_FUNC_DEF(_func, _in, _len, ...) \
1533{ \
1534 fr_sbuff_t sbuff; \
1535 fr_sbuff_uctx_talloc_t tctx; \
1536 fr_slen_t slen = -1; \
1537 if (unlikely(fr_sbuff_init_talloc(ctx, &sbuff, &tctx, \
1538 ((_len) != SIZE_MAX) ? (_len) : 1024, \
1539 ((_len) != SIZE_MAX) ? (_len) : SIZE_MAX) == NULL)) { \
1540 error: \
1541 TALLOC_FREE(sbuff.buff); \
1542 *out = NULL; \
1543 return slen; \
1544 } \
1545 slen = _func(&sbuff, _in, _len, ##__VA_ARGS__); \
1546 if (slen < 0) goto error; \
1547 if (unlikely(fr_sbuff_trim_talloc(&sbuff, SIZE_MAX) < 0)) { \
1548 slen = -1; \
1549 goto error; \
1550 } \
1551 *out = sbuff.buff; \
1552 return slen; \
1553}
1554
1555/** Build a talloc wrapper function for a fr_sbuff_out_* function
1556 *
1557 * @param[in] _func to call.
1558 * @param[in] ... additional arguments to pass to _func.
1559 */
1560#define SBUFF_OUT_TALLOC_FUNC_NO_LEN_DEF(_func, ...) \
1561{ \
1562 fr_sbuff_t sbuff; \
1563 fr_sbuff_uctx_talloc_t tctx; \
1564 fr_slen_t slen = -1; \
1565 if (unlikely(fr_sbuff_init_talloc(ctx, &sbuff, &tctx, 0, SIZE_MAX) == NULL)) { \
1566 error: \
1567 TALLOC_FREE(sbuff.buff); \
1568 *out = NULL; \
1569 return slen; \
1570 } \
1571 slen = _func(&sbuff, ##__VA_ARGS__); \
1572 if (slen < 0) goto error; \
1573 if (unlikely(fr_sbuff_trim_talloc(&sbuff, SIZE_MAX) < 0)) { \
1574 slen = -1; \
1575 goto error; \
1576 } \
1577 *out = sbuff.buff; \
1578 return slen; \
1579}
1580
1581static inline fr_slen_t fr_sbuff_out_abstrncpy(TALLOC_CTX *ctx, char **out, fr_sbuff_t *in, size_t len)
1582SBUFF_OUT_TALLOC_FUNC_DEF(fr_sbuff_out_bstrncpy, in, len)
1583
1584static inline fr_slen_t fr_sbuff_out_abstrncpy_exact(TALLOC_CTX *ctx, char **out, fr_sbuff_t *in, size_t len)
1585SBUFF_OUT_TALLOC_FUNC_DEF(fr_sbuff_out_bstrncpy_exact, in, len)
1586
1587static inline fr_slen_t fr_sbuff_out_abstrncpy_allowed(TALLOC_CTX *ctx, char **out, fr_sbuff_t *in, size_t len,
1588 bool const allowed[static UINT8_MAX + 1])
1589SBUFF_OUT_TALLOC_FUNC_DEF(fr_sbuff_out_bstrncpy_allowed, in, len, allowed)
1590
1591static inline fr_slen_t fr_sbuff_out_abstrncpy_until(TALLOC_CTX *ctx, char **out, fr_sbuff_t *in, size_t len,
1592 fr_sbuff_term_t const *tt,
1593 fr_sbuff_unescape_rules_t const *u_rules)
1594SBUFF_OUT_TALLOC_FUNC_DEF(fr_sbuff_out_bstrncpy_until, in, len, tt, u_rules)
1595
1596static inline fr_slen_t fr_sbuff_out_aunescape_until(TALLOC_CTX *ctx, char **out, fr_sbuff_t *in, size_t len,
1597 fr_sbuff_term_t const *tt,
1598 fr_sbuff_unescape_rules_t const *u_rules)
1599SBUFF_OUT_TALLOC_FUNC_DEF(fr_sbuff_out_unescape_until, in, len, tt, u_rules)
1600/** @} */
1601
1602/** @name Look for a token in a particular format, parse it, and write it to the output pointer
1603 *
1604 * These functions should not be called directly. #fr_sbuff_out should be used instead
1605 * so that if the output variable type changes, the parse rules are automatically changed.
1606 * @{
1607 */
1608fr_slen_t fr_sbuff_out_bool(bool *out, fr_sbuff_t *in);
1609
1610fr_slen_t fr_sbuff_out_int8(fr_sbuff_parse_error_t *err, int8_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1611fr_slen_t fr_sbuff_out_int16(fr_sbuff_parse_error_t *err, int16_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1612fr_slen_t fr_sbuff_out_int32(fr_sbuff_parse_error_t *err, int32_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1613fr_slen_t fr_sbuff_out_int64(fr_sbuff_parse_error_t *err, int64_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1614fr_slen_t fr_sbuff_out_ssize(fr_sbuff_parse_error_t *err, ssize_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1615fr_slen_t fr_sbuff_out_uint8(fr_sbuff_parse_error_t *err, uint8_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1616fr_slen_t fr_sbuff_out_uint16(fr_sbuff_parse_error_t *err, uint16_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1617fr_slen_t fr_sbuff_out_uint32(fr_sbuff_parse_error_t *err, uint32_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1618fr_slen_t fr_sbuff_out_uint64(fr_sbuff_parse_error_t *err, uint64_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1619fr_slen_t fr_sbuff_out_size(fr_sbuff_parse_error_t *err, size_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1620
1621fr_slen_t fr_sbuff_out_uint8_dec(fr_sbuff_parse_error_t *err, uint8_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1622fr_slen_t fr_sbuff_out_uint16_dec(fr_sbuff_parse_error_t *err, uint16_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1623fr_slen_t fr_sbuff_out_uint32_dec(fr_sbuff_parse_error_t *err, uint32_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1624fr_slen_t fr_sbuff_out_uint64_dec(fr_sbuff_parse_error_t *err, uint64_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1625fr_slen_t fr_sbuff_out_size_dec(fr_sbuff_parse_error_t *err, size_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1626
1627fr_slen_t fr_sbuff_out_uint8_oct(fr_sbuff_parse_error_t *err, uint8_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1628fr_slen_t fr_sbuff_out_uint16_oct(fr_sbuff_parse_error_t *err, uint16_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1629fr_slen_t fr_sbuff_out_uint32_oct(fr_sbuff_parse_error_t *err, uint32_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1630fr_slen_t fr_sbuff_out_uint64_oct(fr_sbuff_parse_error_t *err, uint64_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1631fr_slen_t fr_sbuff_out_size_oct(fr_sbuff_parse_error_t *err, size_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1632
1633fr_slen_t fr_sbuff_out_uint8_hex(fr_sbuff_parse_error_t *err, uint8_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1634fr_slen_t fr_sbuff_out_uint16_hex(fr_sbuff_parse_error_t *err, uint16_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1635fr_slen_t fr_sbuff_out_uint32_hex(fr_sbuff_parse_error_t *err, uint32_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1636fr_slen_t fr_sbuff_out_uint64_hex(fr_sbuff_parse_error_t *err, uint64_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1637fr_slen_t fr_sbuff_out_size_hex(fr_sbuff_parse_error_t *err, size_t *out, fr_sbuff_t *sbuff, bool no_trailing);
1638
1639fr_slen_t fr_sbuff_out_float32(fr_sbuff_parse_error_t *err, float *out, fr_sbuff_t *sbuff, bool no_trailing);
1640fr_slen_t fr_sbuff_out_float64(fr_sbuff_parse_error_t *err, double *out, fr_sbuff_t *sbuff, bool no_trailing);
1641
1642#ifndef SIZE_SAME_AS_UINT64
1643# define _fr_sbuff_out_size(_err, _out, _in) size_t * : fr_sbuff_out_size(_err, (size_t *)_out, _in, true),
1644#else
1645# define _fr_sbuff_out_size(_err, _out, _in)
1646#endif
1647
1648#ifndef SSIZE_SAME_AS_INT64
1649# define _fr_sbuff_out_ssize(_err, _out, _in) ssize_t * : fr_sbuff_out_ssize(_err, (ssize_t *)_out, _in, true),
1650#else
1651# define _fr_sbuff_out_ssize(_err, _out, _in)
1652#endif
1653
1654/** Parse a value based on the output type
1655 *
1656 * @param[out] _err If not NULL a value describing the parse error
1657 * will be written to err.
1658 * @param[out] _out Pointer to an integer type.
1659 * @param[in] _in Sbuff to parse integer from.
1660 * @return The number of bytes parsed (even on error).
1661 */
1662#define fr_sbuff_out(_err, _out, _in) \
1663 _Generic((_out), \
1664 bool * : fr_sbuff_out_bool((bool *)_out, _in), \
1665 int8_t * : fr_sbuff_out_int8(_err, (int8_t *)_out, _in, true), \
1666 int16_t * : fr_sbuff_out_int16(_err, (int16_t *)_out, _in, true), \
1667 int32_t * : fr_sbuff_out_int32(_err, (int32_t *)_out, _in, true), \
1668 int64_t * : fr_sbuff_out_int64(_err, (int64_t *)_out, _in, true), \
1669 _fr_sbuff_out_ssize(_err, _out, _in) \
1670 uint8_t * : fr_sbuff_out_uint8(_err, (uint8_t *)_out, _in, true), \
1671 uint16_t * : fr_sbuff_out_uint16(_err, (uint16_t *)_out, _in, true), \
1672 uint32_t * : fr_sbuff_out_uint32(_err, (uint32_t *)_out, _in, true), \
1673 uint64_t * : fr_sbuff_out_uint64(_err, (uint64_t *)_out, _in, true), \
1674 _fr_sbuff_out_size(_err, _out, _in) \
1675 float * : fr_sbuff_out_float32(_err, (float *)_out, _in, true), \
1676 double * : fr_sbuff_out_float64(_err, (double *)_out, _in, true) \
1677 )
1678/** @} */
1679
1680
1681/** @name Conditional advancement
1682 *
1683 * These functions are typically used for parsing when trying to locate
1684 * a sequence of characters in the sbuff.
1685 *
1686 * @{
1687 */
1688size_t fr_sbuff_adv_past_str(fr_sbuff_t *sbuff, char const *needle, size_t need_len);
1689
1690#define fr_sbuff_adv_past_str_literal(_sbuff, _needle) fr_sbuff_adv_past_str(_sbuff, _needle, sizeof(_needle) - 1)
1691
1692size_t fr_sbuff_adv_past_strcase(fr_sbuff_t *sbuff, char const *needle, size_t need_len);
1693
1694#define fr_sbuff_adv_past_strcase_literal(_sbuff, _needle) fr_sbuff_adv_past_strcase(_sbuff, _needle, sizeof(_needle) - 1)
1695
1696size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len,
1697 bool const allowed[static UINT8_MAX + 1], fr_sbuff_term_t const *tt);
1698
1699#define fr_sbuff_adv_past_zeros(_sbuff, _len, _tt) fr_sbuff_adv_past_allowed(_sbuff, _len, sbuff_char_class_zero, _tt)
1700
1701#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt) fr_sbuff_adv_past_allowed(_sbuff, _len, sbuff_char_whitespace, _tt)
1702
1703#define fr_sbuff_adv_past_blank(_sbuff, _len, _tt) fr_sbuff_adv_past_allowed(_sbuff, _len, sbuff_char_blank, _tt)
1704
1705size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr);
1706
1707char *fr_sbuff_adv_to_chr_utf8(fr_sbuff_t *in, size_t len, char const *chr);
1708
1709char *fr_sbuff_adv_to_chr(fr_sbuff_t *in, size_t len, char c);
1710
1711char *fr_sbuff_adv_to_str(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len);
1712
1713#define fr_sbuff_adv_to_str_literal(_sbuff, _len, _needle) fr_sbuff_adv_to_str(_sbuff, _len, _needle, sizeof(_needle) - 1)
1714
1715char *fr_sbuff_adv_to_strcase(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len);
1716
1717#define fr_sbuff_adv_to_strcase_literal(_sbuff, _len, _needle) fr_sbuff_adv_to_strcase(_sbuff, _len, _needle, sizeof(_needle) - 1)
1718
1719bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c);
1720
1721bool fr_sbuff_next_unless_char(fr_sbuff_t *sbuff, char c);
1722
1723/** Advance the sbuff by one char
1724 *
1725 */
1726static inline char fr_sbuff_next(fr_sbuff_t *sbuff)
1727{
1728 if (!fr_sbuff_extend(sbuff)) return '\0';
1729 return fr_sbuff_advance(sbuff, 1);
1730}
1731/** @} */
1732
1733/** @name Remove chars from a buffer and re-terminate
1734 *
1735 * @{
1736 */
1737size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static UINT8_MAX + 1]);
1738/** @} */
1739
1740/** @name Conditions
1741 *
1742 * These functions are typically used in recursive decent parsing for
1743 * look ahead.
1744 * @{
1745 */
1746bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt);
1747
1748static inline bool fr_sbuff_is_in_charset(fr_sbuff_t *sbuff, bool const chars[static UINT8_MAX + 1])
1749{
1750 if (!fr_sbuff_extend(sbuff)) return false;
1751 return chars[(uint8_t)*sbuff->p];
1752}
1753
1754static inline bool fr_sbuff_is_str(fr_sbuff_t *sbuff, char const *str, size_t len)
1755{
1756 if (len == SIZE_MAX) len = strlen(str);
1757 if (!fr_sbuff_extend_lowat(NULL, sbuff, len)) return false;
1758 return memcmp(sbuff->p, str, len) == 0;
1759}
1760#define fr_sbuff_is_str_literal(_sbuff, _str) fr_sbuff_is_str(_sbuff, _str, sizeof(_str) - 1)
1761
1762static inline bool _fr_sbuff_is_char(fr_sbuff_t *sbuff, char *p, char c)
1763{
1764 if (!fr_sbuff_extend(sbuff)) return false;
1765 return *p == c;
1766}
1767static inline bool _fr_marker_is_char(fr_sbuff_marker_t *marker, char *p, char c)
1768{
1769 if (!fr_sbuff_extend(marker)) return false;
1770 return *p == c;
1771}
1772#define fr_sbuff_is_char(_sbuff_or_marker, _c) \
1773 _Generic((_sbuff_or_marker), \
1774 fr_sbuff_t * : _fr_sbuff_is_char((fr_sbuff_t *)(_sbuff_or_marker), fr_sbuff_current(_sbuff_or_marker), _c), \
1775 fr_sbuff_marker_t * : _fr_marker_is_char((fr_sbuff_marker_t *)(_sbuff_or_marker), fr_sbuff_current(_sbuff_or_marker), _c) \
1776 )
1777
1778#define SBUFF_IS_FUNC(_name, _test) \
1779 static inline bool _fr_sbuff_is_ ## _name(fr_sbuff_t *sbuff, char *p) \
1780 { \
1781 if (!fr_sbuff_extend(sbuff)) return false; \
1782 return _test; \
1783 }\
1784 static inline bool _fr_marker_is_ ## _name(fr_sbuff_marker_t *marker, char *p) \
1785 { \
1786 if (!fr_sbuff_extend(marker)) return false; \
1787 return _test; \
1788 }
1789
1790#define SBUFF_IS_GENERIC(_sbuff_or_marker, _name) \
1791 _Generic((_sbuff_or_marker), \
1792 fr_sbuff_t * : _fr_sbuff_is_ ## _name((fr_sbuff_t *)(_sbuff_or_marker), fr_sbuff_current(_sbuff_or_marker)), \
1793 fr_sbuff_marker_t * : _fr_marker_is_ ## _name((fr_sbuff_marker_t *)(_sbuff_or_marker), fr_sbuff_current(_sbuff_or_marker)) \
1794 )
1795
1796SBUFF_IS_FUNC(digit, isdigit((uint8_t) *p))
1797#define fr_sbuff_is_digit(_sbuff_or_marker) \
1798 SBUFF_IS_GENERIC(_sbuff_or_marker, digit)
1799
1800SBUFF_IS_FUNC(upper, isupper((uint8_t) *p))
1801#define fr_sbuff_is_upper(_sbuff_or_marker) \
1802 SBUFF_IS_GENERIC(_sbuff_or_marker, upper)
1803
1804SBUFF_IS_FUNC(lower, islower((uint8_t) *p))
1805#define fr_sbuff_is_lower(_sbuff_or_marker) \
1806 SBUFF_IS_GENERIC(_sbuff_or_marker, lower)
1807
1808SBUFF_IS_FUNC(alpha, isalpha((uint8_t) *p))
1809#define fr_sbuff_is_alpha(_sbuff_or_marker) \
1810 SBUFF_IS_GENERIC(_sbuff_or_marker, alpha)
1811
1812SBUFF_IS_FUNC(alnum, isalnum((uint8_t) *p))
1813#define fr_sbuff_is_alnum(_sbuff_or_marker) \
1814 SBUFF_IS_GENERIC(_sbuff_or_marker, alnum)
1815
1816SBUFF_IS_FUNC(space, isspace((uint8_t) *p))
1817#define fr_sbuff_is_space(_sbuff_or_marker) \
1818 SBUFF_IS_GENERIC(_sbuff_or_marker, space)
1819
1820SBUFF_IS_FUNC(hex, isxdigit((uint8_t) *p))
1821#define fr_sbuff_is_hex(_sbuff_or_marker) \
1822 SBUFF_IS_GENERIC(_sbuff_or_marker, hex)
1823
1824/** @} */
1825
1826void fr_sbuff_unescape_debug(fr_sbuff_unescape_rules_t const *escapes);
1827
1828void fr_sbuff_terminal_debug(fr_sbuff_term_t const *tt);
1829
1830void fr_sbuff_parse_rules_debug(fr_sbuff_parse_rules_t const *p_rules);
1831
1832/*
1833 * ...printf("foo %.*s", fr_sbuff_as_percent_s(&sbuff));
1834 */
1835#define fr_sbuff_as_percent_s(_sbuff) (int) fr_sbuff_remaining(_sbuff), fr_sbuff_current(_sbuff)
1836
1837#ifdef __cplusplus
1838}
1839#endif
#define RCSIDH(h, id)
Definition build.h:484
#define DIAG_OFF(_x)
Definition build.h:457
static fr_slen_t err
Definition dict.h:824
static const bool escapes[UINT8_MAX+1]
Definition util.c:38
long int ssize_t
unsigned char uint8_t
#define UINT8_MAX
fr_sbuff_parse_error_t
@ FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW
Integer type would overflow.
@ FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW
Integer type would underflow.
@ FR_SBUFF_PARSE_ERROR_NOT_FOUND
String does not contain a token matching the output type.
@ FR_SBUFF_PARSE_ERROR_FORMAT
Format of data was invalid.
@ FR_SBUFF_PARSE_OK
No error.
@ FR_SBUFF_PARSE_ERROR_OUT_OF_SPACE
No space available in output buffer.
@ FR_SBUFF_PARSE_ERROR_TRAILING
Trailing characters found.
static char const * name
bool const sbuff_char_class_float[UINT8_MAX+1]
Definition sbuff.c:70
bool const sbuff_char_line_endings[UINT8_MAX+1]
Definition sbuff.c:104
bool const sbuff_char_class_hex[UINT8_MAX+1]
Definition sbuff.c:94
bool const sbuff_char_class_uint[UINT8_MAX+1]
Definition sbuff.c:60
size_t sbuff_parse_error_table_len
Definition sbuff.c:52
bool const sbuff_char_class_zero[UINT8_MAX+1]
Definition sbuff.c:75
bool const sbuff_char_class_hostname[UINT8_MAX+1]
Definition sbuff.c:82
bool const sbuff_char_whitespace[UINT8_MAX+1]
Definition sbuff.c:100
bool const sbuff_char_blank[UINT8_MAX+1]
Definition sbuff.c:108
bool const sbuff_char_class_int[UINT8_MAX+1]
Definition sbuff.c:65
fr_table_num_ordered_t const sbuff_parse_error_table[]
Definition sbuff.c:43
bool const sbuff_char_word[UINT8_MAX+1]
Definition sbuff.c:96
bool const sbuff_char_alpha_num[UINT8_MAX+1]
Definition sbuff.c:95
TALLOC_CTX * ctx
Context to alloc new buffers in.
Definition sbuff.h:140
bool(* fr_sbuff_eof_t)(fr_sbuff_t *sbuff)
For a given extension function, returns whether it is at EOF.
Definition sbuff.h:81
size_t shifted
How much we've read from this file.
Definition sbuff.h:154
size_t shifted
How many bytes this sbuff has been shifted since its creation.
Definition sbuff.h:118
fr_sbuff_extend_t extend
Function to re-populate or extend the buffer.
Definition sbuff.h:121
char const * str
Terminal string.
Definition sbuff.h:162
void * uctx
Extend uctx data.
Definition sbuff.h:126
size_t init
How much to allocate initially.
Definition sbuff.h:141
char chr
Character at the start of an escape sequence.
Definition sbuff.h:204
char const * name
Name for rule set to aid we debugging.
Definition sbuff.h:202
bool do_oct
Process oct sequences i.e.
Definition sbuff.h:216
fr_sbuff_marker_t * m
Pointers to update if the underlying buffer changes.
Definition sbuff.h:130
size_t len
Length of the list.
Definition sbuff.h:172
char const * err
Where the last error occurred.
Definition sbuff.h:114
bool do_hex
Process hex sequences i.e.
Definition sbuff.h:215
size_t max
Maximum size of the buffer.
Definition sbuff.h:142
size_t len
Length of string.
Definition sbuff.h:163
char * buff_end
The true end of the buffer.
Definition sbuff.h:152
fr_sbuff_t * parent
sbuff this sbuff was copied from.
Definition sbuff.h:128
uint8_t is_const
Can't be modified.
Definition sbuff.h:116
fr_sbuff_marker_t * next
Next m in the list.
Definition sbuff.h:89
bool eof
are we at EOF?
Definition sbuff.h:155
uint8_t adv_parent
If true, advance the parent.
Definition sbuff.h:117
fr_sbuff_term_elem_t * elem
A sorted list of terminal strings.
Definition sbuff.h:173
ssize_t fr_slen_t
Represents number of bytes parsed or location of parse error.
Definition sbuff.h:51
fr_sbuff_t * parent
Owner of the marker.
Definition sbuff.h:90
fr_sbuff_eof_t eof
Function to determine if the buffer is at EOF.
Definition sbuff.h:124
size_t(* fr_sbuff_extend_t)(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t req_extension)
Extension callback.
Definition sbuff.h:76
FILE * file
FILE * we're reading from.
Definition sbuff.h:151
size_t max
Maximum number of bytes to read.
Definition sbuff.h:153
fr_sbuff_extend_status_t
Whether the buffer is currently extendable and whether it was extended.
Definition sbuff.h:64
@ FR_SBUFF_FLAG_EXTEND_ERROR
The last call to an extend function resulted in an error.
Definition sbuff.h:66
@ FR_SBUFF_FLAG_EXTENDED
The last call to extend function actually extended the buffer.
Definition sbuff.h:65
Terminal element with pre-calculated lengths.
Definition sbuff.h:161
Set of terminal elements.
File sbuff extension structure.
Definition sbuff.h:150
Talloc sbuff extension structure.
Definition sbuff.h:139
Set of parsing rules for *unescape_until functions.
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
#define fr_strerror_const(_msg)
Definition strerror.h:223