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