The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
sbuff.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** A generic string buffer structure for string printing and parsing
18 *
19 * @file src/lib/util/sbuff.c
20 *
21 * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22 */
23RCSID("$Id: 1c4f07f6a78e932cff13e21f9fb8351f1836698a $")
24
25#include <freeradius-devel/util/misc.h>
26#include <freeradius-devel/util/syserror.h>
27#include <freeradius-devel/util/atexit.h>
28
29
30static _Thread_local char *sbuff_scratch;
31
32/** When true, prevent use of the scratch space
33 *
34 * This prevents us from initialising a pool after the thread local destructors have run.
35 *
36 * The destructors may be called manually before thread exit, and we don't want to re-initialise the pool
37 */
38static _Thread_local bool sbuff_scratch_freed;
39
40static_assert(sizeof(long long) >= sizeof(int64_t), "long long must be as wide or wider than an int64_t");
41static_assert(sizeof(unsigned long long) >= sizeof(uint64_t), "long long must be as wide or wider than an uint64_t");
42
44 { L("ok"), FR_SBUFF_PARSE_OK },
45 { L("token not found"), FR_SBUFF_PARSE_ERROR_NOT_FOUND },
46 { L("trailing data"), FR_SBUFF_PARSE_ERROR_TRAILING },
47 { L("token format invalid"), FR_SBUFF_PARSE_ERROR_FORMAT },
48 { L("out of space"), FR_SBUFF_PARSE_ERROR_OUT_OF_SPACE },
49 { L("integer overflow"), FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW },
50 { L("integer underflow"), FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW }
51};
53
54#if defined(STATIC_ANALYZER) || !defined(NDEBUG)
55# define CHECK_SBUFF_INIT(_sbuff) do { if (!(_sbuff)->extend && (unlikely(!(_sbuff)->buff) || unlikely(!(_sbuff)->start) || unlikely(!(_sbuff)->end) || unlikely(!(_sbuff)->p))) return 0; } while (0)
56#else
57# define CHECK_SBUFF_INIT(_sbuff)
58#endif
59
62 ['+'] = true
63};
64
67 ['+'] = true, ['-'] = true
68};
69
72 ['-'] = true, ['+'] = true, ['e'] = true, ['E'] = true, ['.'] = true,
73};
74
76 ['0'] = true
77};
78
79/*
80 * Anything which vaguely resembles an IP address, prefix, or host name.
81 */
84 ['.'] = true, /* only for IPv4 and host names */
85 [':'] = true, /* only for IPv6 numerical addresses */
86 ['-'] = true, /* only for host names */
87 ['/'] = true, /* only for prefixes */
88 ['['] = true, /* only for IPv6 numerical addresses */
89 [']'] = true, /* only for IPv6 numerical addresses */
90 ['_'] = true, /* only for certain host name labels */
91 ['*'] = true, /* really only for ipv4 addresses */
92};
93
96bool const sbuff_char_word[UINT8_MAX + 1] = {
98 ['-'] = true, ['_'] = true,
99};
101 ['\t'] = true, ['\n'] = true, ['\r'] = true, ['\f'] = true, ['\v'] = true, [' '] = true,
102};
103
105 ['\n'] = true, ['\r'] = true
106};
107
108bool const sbuff_char_blank[UINT8_MAX + 1] = {
109 ['\t'] = true, [' '] = true,
110};
111
112/** Copy function that allows overlapping memory ranges to be copied
113 *
114 * @param[out] o_start start of output buffer.
115 * @param[in] o_end end of the output buffer.
116 * @param[in] i_start start of the input buffer.
117 * @param[in] i_end end of data to copy.
118 * @return
119 * - >0 the number of bytes copied.
120 * - 0 invalid args.
121 * - <0 the number of bytes we'd need to complete the copy.
122 */
123static inline CC_HINT(always_inline) ssize_t safecpy(char *o_start, char *o_end,
124 char const *i_start, char const *i_end)
125{
126 ssize_t diff;
127 size_t i_len = i_end - i_start;
128
129 if (unlikely((o_end < o_start) || (i_end < i_start))) return 0; /* sanity check */
130
131 diff = (o_end - o_start) - (i_len);
132 if (diff < 0) return diff;
133
134 if ((i_start > o_end) || (i_end < o_start)) { /* no-overlap */
135 memcpy(o_start, i_start, i_len);
136 } else { /* overlap */
137 memmove(o_start, i_start, i_len);
138 }
139
140 return (i_len);
141}
142
143static inline CC_HINT(always_inline) size_t min(size_t x, size_t y)
144{
145 return x < y ? x : y;
146}
147
148/** Update all markers and pointers in the set of sbuffs to point to new_buff
149 *
150 * This function should be used if the underlying buffer is realloced.
151 *
152 * @param[in] sbuff to update.
153 * @param[in] new_buff to assign to to sbuff.
154 * @param[in] new_len Length of the new buffer.
155 */
156void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len)
157{
158 fr_sbuff_t *sbuff_i;
159 char *old_buff; /* Current buff */
160
161 old_buff = sbuff->buff;
162
163 /*
164 * Update pointers to point to positions
165 * in new buffer based on their relative
166 * offsets in the old buffer... but not
167 * past the end of the new buffer.
168 */
169 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
171
172 sbuff_i->buff = new_buff;
173 sbuff_i->start = new_buff + min(new_len, sbuff_i->start - old_buff);
174 sbuff_i->end = sbuff_i->buff + new_len;
175 *(sbuff_i->end) = '\0'; /* Re-terminate */
176
177 sbuff_i->p = new_buff + min(new_len, sbuff_i->p - old_buff);
178
179 for (m_i = sbuff_i->m; m_i; m_i = m_i->next) m_i->p = new_buff + min(new_len, m_i->p - old_buff);
180 }
181}
182
183/** Shift the contents of the sbuff, returning the number of bytes we managed to shift
184 *
185 * @param[in] sbuff to shift.
186 * @param[in] shift the contents of the buffer this many bytes
187 * towards the start of the buffer.
188 * @return
189 * - 0 the shift failed due to constraining pointers.
190 * - >0 the number of bytes we managed to shift pointers
191 * in the sbuff. memmove should be used to move the
192 * existing contents of the buffer, and fill the free
193 * space at the end of the buffer with additional data.
194 */
195size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift)
196{
197 fr_sbuff_t *sbuff_i;
198 char *buff, *end; /* Current start */
199 size_t max_shift = shift;
200 bool reterminate = false;
201
202 CHECK_SBUFF_INIT(sbuff);
203
204 buff = sbuff->buff;
205 end = sbuff->end;
206
207 /*
208 * If the sbuff is already \0 terminated
209 * and we're not working on a const buffer
210 * then assume we need to re-terminate
211 * later.
212 */
213 reterminate = (sbuff->p < sbuff->end) && (*sbuff->p == '\0') && !sbuff->is_const;
214
215 /*
216 * First pass: find the maximum shift, which is the minimum
217 * of the distances from buff to any of the current pointers
218 * or current pointers of markers of dbuff and its ancestors.
219 * (We're also constrained by the requested shift count.)
220 */
221 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
223
224 max_shift = min(max_shift, sbuff_i->p - buff);
225 if (!max_shift) return 0;
226
227 for (m_i = sbuff_i->m; m_i; m_i = m_i->next) {
228 max_shift = min(max_shift, m_i->p - buff);
229 if (!max_shift) return 0;
230 }
231 }
232
233 /*
234 * Second pass: adjust pointers.
235 * The first pass means we need only subtract shift from
236 * current pointers. Start pointers can't constrain shift,
237 * or we'd never free any space, so they require the added
238 * check.
239 */
240 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
242 char *start = sbuff_i->start;
243
244 sbuff_i->start -= min(max_shift, sbuff_i->start - buff);
245 sbuff_i->p -= max_shift;
246 sbuff_i->end -= max_shift;
247 sbuff_i->shifted += (max_shift - (start - sbuff_i->start));
248 for (m_i = sbuff_i->m; m_i; m_i = m_i->next) m_i->p -= max_shift;
249 }
250
251 /*
252 * Only memmove if the shift wasn't the
253 * entire contents of the buffer.
254 */
255 if ((buff + max_shift) < end) memmove(buff, buff + max_shift, end - (buff + max_shift));
256
257 if (reterminate) *sbuff->p = '\0';
258
259 return max_shift;
260}
261
262/** Refresh the buffer with more data from the file
263 *
264 */
265size_t fr_sbuff_extend_file(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension)
266{
267 fr_sbuff_t *sbuff_i;
268 size_t read, available, total_read, shift;
270
271 CHECK_SBUFF_INIT(sbuff);
272
273 fctx = sbuff->uctx;
274 if (fctx->eof) return 0;
275
276 if (extension == SIZE_MAX) extension = 0;
277
278 total_read = fctx->shifted + (sbuff->end - sbuff->buff);
279 if (total_read >= fctx->max) {
280 fr_strerror_const("Can't satisfy extension request, max bytes read");
281 return 0; /* There's no way we could satisfy the extension request */
282 }
283
284 /*
285 * Shift out the maximum number of bytes we can
286 * irrespective of the amount that was requested
287 * as the extension. It's more efficient to do
288 * this than lots of small shifts, and just
289 * looking and the number of bytes used in the
290 * deepest sbuff, and using that as the shift
291 * amount, might mean we don't shift anything at
292 * all!
293 *
294 * fr_sbuff_shift will cap the max shift amount,
295 * so markers and positions will remain valid for
296 * all sbuffs in the chain.
297 */
298 shift = fr_sbuff_current(sbuff) - fr_sbuff_buff(sbuff);
299 if (shift) {
300 /*
301 * Try and shift as much as we can out
302 * of the buffer to make space.
303 *
304 * Note: p and markers are constraints here.
305 */
306 fctx->shifted += fr_sbuff_shift(sbuff, shift);
307 }
308
309 available = fctx->buff_end - sbuff->end;
310 if (available > (fctx->max - total_read)) available = fctx->max - total_read;
311 if (available < extension) {
312 fr_strerror_printf("Can't satisfy extension request for %zu bytes", extension);
313 return 0; /* There's no way we could satisfy the extension request */
314 }
315
316 read = fread(sbuff->end, 1, available, fctx->file);
317 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
318 sbuff_i->end += read; /* Advance end, which increases fr_sbuff_remaining() */
319 }
320
321 /** Check for errors
322 */
323 if (read < available) {
324 if (!feof(fctx->file)) { /* It's a real error */
325 fr_strerror_printf("Error extending buffer: %s", fr_syserror(ferror(fctx->file)));
327 return 0;
328 }
329
330 fctx->eof = true;
331 }
332
333 return read;
334}
335
336/** Accessor function for the EOF state of the file extendor
337 *
338 */
340{
341 fr_sbuff_uctx_file_t *fctx = sbuff->uctx;
342 return fctx->eof;
343}
344
345/** Reallocate the current buffer
346 *
347 * @param[in] status Extend status.
348 * @param[in] sbuff to be extended.
349 * @param[in] extension How many additional bytes should be allocated
350 * in the buffer.
351 * @return
352 * - 0 the extension operation failed.
353 * - >0 the number of bytes the buffer was extended by.
354 */
355size_t fr_sbuff_extend_talloc(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension)
356{
357 fr_sbuff_uctx_talloc_t *tctx = sbuff->uctx;
358 size_t clen, nlen, elen = extension;
359 char *new_buff;
360
361 CHECK_SBUFF_INIT(sbuff);
362
363 clen = sbuff->buff ? talloc_array_length(sbuff->buff) : 0;
364 /*
365 * If the current buffer size + the extension
366 * is less than init, extend the buffer to init.
367 *
368 * This can happen if the buffer has been
369 * trimmed, and then additional data is added.
370 */
371 if ((clen + elen) < tctx->init) {
372 elen = (tctx->init - clen) + 1; /* add \0 */
373 /*
374 * Double the buffer size if it's more than the
375 * requested amount.
376 */
377 } else if (elen < clen) {
378 elen = clen - 1; /* Don't double alloc \0 */
379 }
380
381 /*
382 * Check we don't exceed the maximum buffer
383 * length.
384 */
385 if (tctx->max && ((clen + elen) > tctx->max)) {
386 elen = tctx->max - clen;
387 if (elen == 0) {
388 fr_strerror_printf("Failed extending buffer by %zu bytes to "
389 "%zu bytes, max is %zu bytes",
390 extension, clen + extension, tctx->max);
391 return 0;
392 }
393 elen += 1; /* add \0 */
394 }
395 nlen = clen + elen;
396
397 new_buff = talloc_realloc(tctx->ctx, sbuff->buff, char, nlen);
398 if (unlikely(!new_buff)) {
399 fr_strerror_printf("Failed extending buffer by %zu bytes to %zu bytes", elen, nlen);
401 return 0;
402 }
403
404 (void)fr_sbuff_update(sbuff, new_buff, nlen - 1); /* Shouldn't fail as we're extending */
405
406 return elen;
407}
408
409/** Trim a talloced sbuff to the minimum length required to represent the contained string
410 *
411 * @param[in] sbuff to trim.
412 * @param[in] len Length to trim to. Passing SIZE_MAX will
413 * result in the buffer being trimmed to the
414 * length of the content.
415 * @return
416 * - 0 on success.
417 * - -1 on failure - markers present pointing past the end of string data.
418 */
419int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len)
420{
421 size_t clen = 0, nlen = 1;
422 char *new_buff;
423 fr_sbuff_uctx_talloc_t *tctx = sbuff->uctx;
424
425 CHECK_SBUFF_INIT(sbuff);
426
427 if (sbuff->buff) clen = talloc_array_length(sbuff->buff);
428
429 if (len != SIZE_MAX) {
430 nlen += len;
431 } else if (sbuff->buff){
432 nlen += (sbuff->p - sbuff->start);
433 }
434
435 if (nlen != clen) {
436 new_buff = talloc_realloc(tctx->ctx, sbuff->buff, char, nlen);
437 if (unlikely(!new_buff)) {
438 fr_strerror_printf("Failed trimming buffer from %zu to %zu", clen, nlen);
439 return -1;
440 }
441 fr_sbuff_update(sbuff, new_buff, nlen - 1);
442 }
443
444 return 0;
445}
446
447/** Reset a talloced buffer to its initial length, clearing any data stored
448 *
449 * @param[in] sbuff to reset.
450 * @return
451 * - 0 on success.
452 * - -1 on failure - markers present pointing past the end of string data.
453 */
455{
456 fr_sbuff_uctx_talloc_t *tctx = sbuff->uctx;
457
458 CHECK_SBUFF_INIT(sbuff);
459
460 fr_sbuff_set_to_start(sbuff); /* Clear data */
461 sbuff->m = NULL; /* Remove any maker references */
462
463 if (fr_sbuff_used(sbuff) != tctx->init) {
464 char *new_buff;
465
466 new_buff = talloc_realloc(tctx->ctx, sbuff->buff, char, tctx->init);
467 if (!new_buff) {
468 fr_strerror_printf("Failed reallocing from %zu to %zu",
469 talloc_array_length(sbuff->buff), tctx->init);
470 return -1;
471 }
472 sbuff->buff = new_buff;
473 fr_sbuff_update(sbuff, new_buff, tctx->init - 1);
474 }
475
476 return 0;
477}
478
479/** Fill as much of the output buffer we can and break on partial copy
480 *
481 * @param[in] _out sbuff to write to.
482 * @param[in] _in sbuff to copy from.
483 * @param[in] _len maximum amount to copy.
484 */
485#define FILL_OR_GOTO_DONE(_out, _in, _len) if (fr_sbuff_move(_out, _in, _len) < (size_t)(_len)) goto done
486
487/** Constrain end pointer to prevent advancing more than the amount the caller specified
488 *
489 * @param[in] _sbuff to constrain.
490 * @param[in] _max maximum amount to advance.
491 * @param[in] _used how much we've advanced so far.
492 * @return a temporary end pointer.
493 */
494#define CONSTRAINED_END(_sbuff, _max, _used) \
495 (((_max) - (_used)) > fr_sbuff_remaining(_sbuff) ? (_sbuff)->end : (_sbuff)->p + ((_max) - (_used)))
496
497
498/** Populate a terminal index
499 *
500 * @param[out] needle_len the longest needle. Will not be set
501 * if the terminal array is empty.
502 * @param[out] idx to populate.
503 * @param[in] term Terminals to populate the index with.
504 */
505static inline CC_HINT(always_inline) void fr_sbuff_terminal_idx_init(size_t *needle_len,
506 uint8_t idx[static UINT8_MAX + 1],
507 fr_sbuff_term_t const *term)
508{
509 size_t i, len, max = 0;
510
511 if (!term) return;
512
513 memset(idx, 0, UINT8_MAX + 1);
514
515 for (i = 0; i < term->len; i++) {
516 len = term->elem[i].len;
517 if (len > max) max = len;
518
519 idx[(uint8_t)term->elem[i].str[0]] = i + 1;
520 }
521
522 if (i > 0) *needle_len = max;
523}
524
525/** Efficient terminal string search
526 *
527 * Caller should ensure that a buffer extension of needle_len bytes has been requested
528 * before calling this function.
529 *
530 * @param[in] in Sbuff to search in.
531 * @param[in] p Current position (may be ahead of in->p).
532 * @param[in] idx Fastpath index, populated by
533 * fr_sbuff_terminal_idx_init.
534 * @param[in] term terminals to search in.
535 * @param[in] needle_len Length of the longest needle.
536 * @return
537 * - true if found.
538 * - false if not.
539 */
540static inline bool fr_sbuff_terminal_search(fr_sbuff_t *in, char const *p,
541 uint8_t idx[static UINT8_MAX + 1],
542 fr_sbuff_term_t const *term, size_t needle_len)
543{
544 uint8_t term_idx;
545
546 ssize_t start = 0;
547 ssize_t end;
548 ssize_t mid;
549
550 size_t remaining;
551
552 if (!term) return false; /* If there's no terminals, we don't need to search */
553
554 end = term->len - 1;
555
556 term_idx = idx[(uint8_t)*p]; /* Fast path */
557 if (!term_idx) return false;
558
559 /*
560 * Special case for EOFlike states
561 */
562 remaining = fr_sbuff_remaining(in);
563 if ((remaining == 0) && !fr_sbuff_is_extendable(in)) {
564 if (idx['\0'] != 0) return true;
565 return false;
566 }
567
568 if (remaining < needle_len) {
569 fr_assert_msg(!fr_sbuff_is_extendable(in),
570 "Caller failed to extend buffer by %zu bytes before calling fr_sbuff_terminal_search",
571 needle_len);
572 /*
573 * We can't search for the needle if we don't have
574 * enough data to match it.
575 */
576 return false;
577 }
578
579 mid = term_idx - 1; /* Inform the mid point from the index */
580
581 while (start <= end) {
582 fr_sbuff_term_elem_t const *elem;
583 size_t tlen;
584 int ret;
585
586 elem = &term->elem[mid];
587 tlen = elem->len;
588
589 ret = memcmp(p, elem->str, tlen < (size_t)remaining ? tlen : (size_t)remaining);
590 if (ret == 0) {
591 /*
592 * If we have more text than the table element, that's fine
593 */
594 if (remaining >= tlen) return true;
595
596 /*
597 * If input was shorter than the table element we need to
598 * keep searching.
599 */
600 ret = -1;
601 }
602
603 if (ret < 0) {
604 end = mid - 1;
605 } else {
606 start = mid + 1;
607 }
608
609 mid = start + ((end - start) / 2); /* Avoid overflow */
610 }
611
612 return false;
613}
614
615/** Compare two terminal elements for ordering purposes
616 *
617 * @param[in] a first terminal to compare.
618 * @param[in] b second terminal to compare.
619 * @return CMP(a,b)
620 */
621static inline int8_t terminal_cmp(fr_sbuff_term_elem_t const *a, fr_sbuff_term_elem_t const *b)
622{
623 MEMCMP_RETURN(a, b, str, len);
624 return 0;
625}
626
627#if 0
628static void fr_sbuff_terminal_debug_tmp(fr_sbuff_term_elem_t const *elem[], size_t len)
629{
630 size_t i;
631
632 FR_FAULT_LOG("Terminal count %zu", len);
633
634 for (i = 0; i < len; i++) FR_FAULT_LOG("\t\"%s\" (%zu)", elem[i] ? elem[i]->str : "NULL", elem[i] ? elem[i]->len : 0);
635}
636#endif
637
638/** Merge two sets of terminal strings
639 *
640 * @param[in] ctx to allocate the new terminal array in.
641 * @param[in] a first set of terminals to merge.
642 * @param[in] b second set of terminals to merge.
643 * @return A new set of de-duplicated and sorted terminals.
644 */
646{
647 size_t i, j, num;
649 fr_sbuff_term_elem_t const *tmp[UINT8_MAX + 1];
650
651 /*
652 * Check all inputs are pre-sorted. It doesn't break this
653 * function, but it's useful in case the terminal arrays
654 * are defined elsewhere without merging.
655 */
656#if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
657 for (i = 0; i < a->len - 1; i++) fr_assert(terminal_cmp(&a->elem[i], &a->elem[i + 1]) < 0);
658 for (i = 0; i < b->len - 1; i++) fr_assert(terminal_cmp(&b->elem[i], &b->elem[i + 1]) < 0);
659#endif
660
661 /*
662 * Since the inputs are sorted, we can just do an O(n+m)
663 * walk through the arrays, comparing entries across the
664 * two arrays.
665 *
666 * If there are duplicates, we prefer "a", for no particular reason.
667 */
668 num = i = j = 0;
669 while ((i < a->len) && (j < b->len)) {
670 int8_t cmp;
671
672 cmp = terminal_cmp(&a->elem[i], &b->elem[j]);
673 if (cmp == 0) {
674 j++;
675 tmp[num++] = &a->elem[i++];
676
677 } else if (cmp < 0) {
678 tmp[num++] = &a->elem[i++];
679
680 } else if (cmp > 0) {
681 tmp[num++] = &b->elem[j++];
682 }
683
684 fr_assert(num <= UINT8_MAX);
685 }
686
687 /*
688 * Only one of these will be hit, and it's simpler than nested "if" statements.
689 */
690 while (i < a->len) tmp[num++] = &a->elem[i++];
691 while (j < b->len) tmp[num++] = &b->elem[j++];
692
694 if (unlikely(!out)) return NULL;
695
696 out->elem = talloc_array(out, fr_sbuff_term_elem_t, num);
697 if (unlikely(!out->elem)) {
699 return NULL;
700 }
701 out->len = num;
702
703 for (i = 0; i < num; i++) out->elem[i] = *tmp[i]; /* copy merged results back */
704
705#if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
706 for (i = 0; i < num - 1; i++) fr_assert(terminal_cmp(&out->elem[i], &out->elem[i + 1]) < 0);
707#endif
708
709 return out;
710}
711
712/** Copy as many bytes as possible from a sbuff to a sbuff
713 *
714 * Copy size is limited by available data in sbuff and space in output sbuff.
715 *
716 * @param[out] out Where to copy to.
717 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
718 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
719 * @return
720 * - 0 no bytes copied.
721 * - >0 the number of bytes copied.
722 */
724{
726 size_t remaining;
727
729
730 while (fr_sbuff_used_total(&our_in) < len) {
731 size_t chunk_len;
732
733 remaining = (len - fr_sbuff_used_total(&our_in));
734
735 if (!fr_sbuff_extend(&our_in)) break;
736
737 chunk_len = fr_sbuff_remaining(&our_in);
738 if (chunk_len > remaining) chunk_len = remaining;
739
740 FILL_OR_GOTO_DONE(out, &our_in, chunk_len);
741 }
742
743done:
744 *out->p = '\0';
745 return fr_sbuff_used_total(&our_in);
746}
747
748/** Copy exactly len bytes from a sbuff to a sbuff or fail
749 *
750 * Copy size is limited by available data in sbuff, space in output sbuff, and length.
751 *
752 * @param[out] out Where to copy to.
753 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
754 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
755 * @return
756 * - 0 no bytes copied, no token found of sufficient length in input buffer.
757 * - >0 the number of bytes copied.
758 * - <0 the number of additional output bytes we would have needed to
759 * complete the copy.
760 */
762{
763 fr_sbuff_t our_in = FR_SBUFF(in);
764 size_t remaining;
766
768
769 fr_sbuff_marker(&m, out);
770
771 do {
772 size_t chunk_len;
773 ssize_t copied;
774
775 remaining = (len - fr_sbuff_used_total(&our_in));
776 if (remaining && !fr_sbuff_extend(&our_in)) {
777 fr_sbuff_marker_release(&m);
778 return 0;
779 }
780
781 chunk_len = fr_sbuff_remaining(&our_in);
782 if (chunk_len > remaining) chunk_len = remaining;
783
784 copied = fr_sbuff_in_bstrncpy(out, our_in.p, chunk_len);
785 if (copied < 0) {
786 fr_sbuff_set(out, &m); /* Reset out */
787 *m.p = '\0'; /* Re-terminate */
788
789 /* Amount remaining in input buffer minus the amount we could have copied */
790 if (len == SIZE_MAX) return -(fr_sbuff_remaining(in) - (chunk_len + copied));
791 /* Amount remaining to copy minus the amount we could have copied */
792 fr_sbuff_marker_release(&m);
793 return -(remaining - (chunk_len + copied));
794 }
795 fr_sbuff_advance(&our_in, copied);
796 } while (fr_sbuff_used_total(&our_in) < len);
797
798 FR_SBUFF_SET_RETURN(in, &our_in); /* in was pinned, so this works */
799}
800
801/** Copy as many allowed characters as possible from a sbuff to a sbuff
802 *
803 * Copy size is limited by available data in sbuff and output buffer length.
804 *
805 * As soon as a disallowed character is found the copy is stopped.
806 * The input sbuff will be left pointing at the first disallowed character.
807 *
808 * @param[out] out Where to copy to.
809 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
810 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
811 * @param[in] allowed Characters to include the copy.
812 * @return
813 * - 0 no bytes copied.
814 * - >0 the number of bytes copied.
815 */
817 bool const allowed[static UINT8_MAX + 1])
818{
820
822
823 while (fr_sbuff_used_total(&our_in) < len) {
824 char *p;
825 char *end;
826
827 if (!fr_sbuff_extend(&our_in)) break;
828
829 p = fr_sbuff_current(&our_in);
830 end = CONSTRAINED_END(&our_in, len, fr_sbuff_used_total(&our_in));
831
832 while ((p < end) && allowed[(uint8_t)*p]) p++;
833
834 FILL_OR_GOTO_DONE(out, &our_in, p - our_in.p);
835
836 if (p != end) break; /* stopped early, break */
837 }
838
839done:
840 *out->p = '\0';
841 return fr_sbuff_used_total(&our_in);
842}
843
844/** Copy as many allowed characters as possible from a sbuff to a sbuff
845 *
846 * Copy size is limited by available data in sbuff and output buffer length.
847 *
848 * As soon as a disallowed character is found the copy is stopped.
849 * The input sbuff will be left pointing at the first disallowed character.
850 *
851 * @param[out] out Where to copy to.
852 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
853 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
854 * @param[in] tt Token terminals in the encompassing grammar.
855 * @param[in] u_rules If not NULL, ignore characters in the until set when
856 * prefixed with u_rules->chr. FIXME - Should actually evaluate
857 * u_rules fully.
858 * @return
859 * - 0 no bytes copied.
860 * - >0 the number of bytes copied.
861 */
863 fr_sbuff_term_t const *tt,
864 fr_sbuff_unescape_rules_t const *u_rules)
865{
867 bool do_escape = false; /* Track state across extensions */
868
869 uint8_t idx[UINT8_MAX + 1]; /* Fast path index */
870 size_t needle_len = 1;
871 char escape_chr = u_rules ? u_rules->chr : '\0';
872
874
875 /*
876 * Initialise the fastpath index and
877 * figure out the longest needle.
878 */
879 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
880
881 while (fr_sbuff_used_total(&our_in) < len) {
882 char *p;
883 char *end;
884
885 if (fr_sbuff_extend_lowat(NULL, &our_in, needle_len) == 0) break;
886
887 p = fr_sbuff_current(&our_in);
888 end = CONSTRAINED_END(&our_in, len, fr_sbuff_used_total(&our_in));
889
890 if (p == end) break;
891
892 if (escape_chr == '\0') {
893 while ((p < end) && !fr_sbuff_terminal_search(in, p, idx, tt, needle_len)) p++;
894 } else {
895 while (p < end) {
896 if (do_escape) {
897 do_escape = false;
898 } else if (*p == escape_chr) {
899 do_escape = true;
900 } else if (fr_sbuff_terminal_search(in, p, idx, tt, needle_len)) {
901 break;
902 }
903 p++;
904 }
905 }
906
907 FILL_OR_GOTO_DONE(out, &our_in, p - our_in.p);
908
909 if (p != end) break; /* stopped early, break */
910 }
911
912done:
913 *out->p = '\0';
914 return fr_sbuff_used_total(&our_in);
915}
916
917/** Copy as many allowed characters as possible from a sbuff to a sbuff
918 *
919 * Copy size is limited by available data in sbuff and output buffer length.
920 *
921 * As soon as a disallowed character is found the copy is stopped.
922 * The input sbuff will be left pointing at the first disallowed character.
923 *
924 * This de-escapes characters as they're copied out of the sbuff.
925 *
926 * @param[out] out Where to copy to.
927 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
928 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
929 * @param[in] tt Token terminal strings in the encompassing grammar.
930 * @param[in] u_rules for processing unescape sequences.
931 * @return
932 * - 0 no bytes copied.
933 * - >0 the number of bytes written to out.
934 */
936 fr_sbuff_term_t const *tt,
937 fr_sbuff_unescape_rules_t const *u_rules)
938{
939 fr_sbuff_t our_in;
940 bool do_escape = false; /* Track state across extensions */
944
945 uint8_t idx[UINT8_MAX + 1]; /* Fast path index */
946 size_t needle_len = 1;
947 fr_sbuff_extend_status_t status = 0;
948
949 /*
950 * If we don't need to do unescaping
951 * call a more suitable function.
952 */
953 if (!u_rules || (u_rules->chr == '\0')) return fr_sbuff_out_bstrncpy_until(out, in, len, tt, u_rules);
954
956
957 our_in = FR_SBUFF(in);
958
959 /*
960 * Chunk tracking...
961 */
962 fr_sbuff_marker(&c_s, &our_in);
963 fr_sbuff_marker(&end, &our_in);
964 fr_sbuff_marker_update_end(&end, len);
965
966 fr_sbuff_marker(&o_s, out);
967
968 /*
969 * Initialise the fastpath index and
970 * figure out the longest needle.
971 */
972 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
973
974 /*
975 * ...while we have remaining data
976 */
977 while (fr_sbuff_extend_lowat(&status, &our_in, needle_len) > 0) {
978 if (fr_sbuff_was_extended(status)) fr_sbuff_marker_update_end(&end, len);
979 if (!fr_sbuff_diff(&our_in, &end)) break; /* Reached the end */
980
981 if (do_escape) {
982 do_escape = false;
983
984 /*
985 * Check for \x<hex><hex>
986 */
987 if (u_rules->do_hex && fr_sbuff_is_char(&our_in, 'x')) {
988 uint8_t escape;
990
991 fr_sbuff_marker(&m, &our_in); /* allow for backtrack */
992 fr_sbuff_advance(&our_in, 1); /* skip over the 'x' */
993
994 if (fr_sbuff_out_uint8_hex(NULL, &escape, &our_in, false) != 2) {
995 fr_sbuff_set(&our_in, &m); /* backtrack */
996 fr_sbuff_marker_release(&m);
997 goto check_subs; /* allow sub for \x */
998 }
999
1000 if (fr_sbuff_in_char(out, escape) <= 0) {
1001 fr_sbuff_set(&our_in, &m); /* backtrack */
1002 fr_sbuff_marker_release(&m);
1003 break;
1004 }
1005 fr_sbuff_marker_release(&m);
1006 fr_sbuff_set(&c_s, &our_in);
1007 continue;
1008 }
1009
1010 /*
1011 * Check for <oct><oct><oct>
1012 */
1013 if (u_rules->do_oct && fr_sbuff_is_digit(&our_in)) {
1014 uint8_t escape;
1016
1017 fr_sbuff_marker(&m, &our_in); /* allow for backtrack */
1018
1019 if (fr_sbuff_out_uint8_oct(NULL, &escape, &our_in, false) != 3) {
1020 fr_sbuff_set(&our_in, &m); /* backtrack */
1021 fr_sbuff_marker_release(&m);
1022 goto check_subs; /* allow sub for <oct> */
1023 }
1024
1025 if (fr_sbuff_in_char(out, escape) <= 0) {
1026 fr_sbuff_set(&our_in, &m); /* backtrack */
1027 fr_sbuff_marker_release(&m);
1028 break;
1029 }
1030 fr_sbuff_marker_release(&m);
1031 fr_sbuff_set(&c_s, &our_in);
1032 continue;
1033 }
1034
1035 check_subs:
1036 /*
1037 * Not a recognised hex or octal escape sequence
1038 * may be a substitution or a sequence that
1039 * should be copied to the output buffer.
1040 */
1041 {
1042 uint8_t c = *fr_sbuff_current(&our_in);
1043
1044 if (u_rules->subs[c] == '\0') {
1045 if (u_rules->skip[c] == true) goto next;
1046 goto next_esc;
1047 }
1048
1049 /*
1050 * We already copied everything up
1051 * to this point, so we can now
1052 * write the substituted char to
1053 * the output buffer.
1054 */
1055 if (fr_sbuff_in_char(out, u_rules->subs[c]) <= 0) break;
1056
1057 /*
1058 * ...and advance past the entire
1059 * escape seq in the input buffer.
1060 */
1061 fr_sbuff_advance(&our_in, 1);
1062 fr_sbuff_set(&c_s, &our_in);
1063 continue;
1064 }
1065 }
1066
1067 next_esc:
1068 if (*fr_sbuff_current(&our_in) == u_rules->chr) {
1069 /*
1070 * Copy out any data we got before
1071 * we hit the escape char.
1072 *
1073 * We need to do this before we
1074 * can write the escape char to
1075 * the output sbuff.
1076 */
1078
1079 do_escape = true;
1080 fr_sbuff_advance(&our_in, 1);
1081 continue;
1082 }
1083
1084 next:
1085 if (tt && fr_sbuff_terminal_search(&our_in, fr_sbuff_current(&our_in), idx, tt, needle_len)) break;
1086 fr_sbuff_advance(&our_in, 1);
1087 }
1088
1089 /*
1090 * Copy any remaining data over
1091 */
1093
1094done:
1095 fr_sbuff_set(in, &c_s); /* Only advance by as much as we copied */
1096 *out->p = '\0';
1097
1098 return fr_sbuff_marker_release_behind(&o_s);
1099}
1100
1101/** See if the string contains a truth value
1102 *
1103 * @param[out] out Where to write boolean value.
1104 * @param[in] in Where to search for a truth value.
1105 * @return
1106 * - >0 the number of bytes consumed.
1107 * - -1 no bytes copied, was not a truth value.
1108 */
1110{
1111 fr_sbuff_t our_in = FR_SBUFF(in);
1112
1113 static bool const bool_prefix[UINT8_MAX + 1] = {
1114 ['t'] = true, ['T'] = true, /* true */
1115 ['f'] = true, ['F'] = true, /* false */
1116 ['y'] = true, ['Y'] = true, /* yes */
1117 ['n'] = true, ['N'] = true, /* no */
1118 };
1119
1120 if (fr_sbuff_is_in_charset(&our_in, bool_prefix)) {
1121 switch (tolower(fr_sbuff_char(&our_in, '\0'))) {
1122 default:
1123 break;
1124
1125 case 't':
1126 if (fr_sbuff_adv_past_strcase_literal(&our_in, "true")) {
1127 *out = true;
1128 FR_SBUFF_SET_RETURN(in, &our_in);
1129 }
1130 break;
1131
1132 case 'f':
1133 if (fr_sbuff_adv_past_strcase_literal(&our_in, "false")) {
1134 *out = false;
1135 FR_SBUFF_SET_RETURN(in, &our_in);
1136 }
1137 break;
1138
1139 case 'y':
1140 if (fr_sbuff_adv_past_strcase_literal(&our_in, "yes")) {
1141 *out = true;
1142 FR_SBUFF_SET_RETURN(in, &our_in);
1143 }
1144 break;
1145
1146 case 'n':
1147 if (fr_sbuff_adv_past_strcase_literal(&our_in, "no")) {
1148 *out = false;
1149 FR_SBUFF_SET_RETURN(in, &our_in);
1150 }
1151 break;
1152 }
1153 }
1154
1155 *out = false; /* Always initialise out */
1156
1157 fr_strerror_const("Not a valid boolean value. Accepted values are 'yes', 'no', 'true', 'false'");
1158
1159 return -1;
1160}
1161
1162/** Used to define a number parsing functions for signed integers
1163 *
1164 * @param[in] _name Function suffix.
1165 * @param[in] _type Output type.
1166 * @param[in] _min value.
1167 * @param[in] _max value.
1168 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1169 * Can't use stringify because of width modifiers like 'u'
1170 * used in <stdint.h>.
1171 * @param[in] _base to use.
1172 */
1173#define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base) \
1174fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1175{ \
1176 char buff[_max_char + 1]; \
1177 char *end, *a_end; \
1178 size_t len; \
1179 long long num; \
1180 _type cast_num; \
1181 fr_sbuff_t our_in = FR_SBUFF(in); \
1182 buff[0] = '\0'; /* clang scan */ \
1183 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1184 if (len == 0) { \
1185 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1186 return -1; \
1187 } \
1188 errno = 0; /* this is needed as strtoll doesn't reset errno */ \
1189 num = strtoll(buff, &end, _base); \
1190 cast_num = (_type)(num); \
1191 if (end == buff) { \
1192 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1193 return -1; \
1194 } \
1195 if (num > cast_num) { \
1196 overflow: \
1197 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1198 *out = (_type)(_max); \
1199 return -1; \
1200 } \
1201 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == LLONG_MAX))) goto overflow; \
1202 if (num < cast_num) { \
1203 underflow: \
1204 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1205 *out = (_type)(_min); \
1206 return -1; \
1207 } \
1208 if ((errno == ERANGE) && (num == LLONG_MIN)) goto underflow; \
1209 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1210 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1211 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1212 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1213 *out = (_type)(_max); \
1214 FR_SBUFF_ERROR_RETURN(&our_in); \
1215 } \
1216 *out = cast_num; \
1217 } else { \
1218 if (err) *err = FR_SBUFF_PARSE_OK; \
1219 *out = cast_num; \
1220 } \
1221 return fr_sbuff_advance(in, end - buff); /* Advance by the length strtoll gives us */ \
1222}
1223
1224SBUFF_PARSE_INT_DEF(int8, int8_t, INT8_MIN, INT8_MAX, 4, 0)
1225SBUFF_PARSE_INT_DEF(int16, int16_t, INT16_MIN, INT16_MAX, 6, 0)
1226SBUFF_PARSE_INT_DEF(int32, int32_t, INT32_MIN, INT32_MAX, 11, 0)
1227SBUFF_PARSE_INT_DEF(int64, int64_t, INT64_MIN, INT64_MAX, 20, 0)
1228SBUFF_PARSE_INT_DEF(ssize, ssize_t, SSIZE_MIN, SSIZE_MAX, 20, 0)
1229
1230/** Used to define a number parsing functions for signed integers
1231 *
1232 * @param[in] _name Function suffix.
1233 * @param[in] _type Output type.
1234 * @param[in] _max value.
1235 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1236 * Can't use stringify because of width modifiers like 'u'
1237 * used in <stdint.h>.
1238 * @param[in] _base of the number being parsed, 8, 10, 16 etc...
1239 */
1240#define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base) \
1241fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1242{ \
1243 char buff[_max_char + 1]; \
1244 char *end, *a_end; \
1245 size_t len; \
1246 unsigned long long num; \
1247 _type cast_num; \
1248 fr_sbuff_t our_in = FR_SBUFF(in); \
1249 buff[0] = '\0'; /* clang scan */ \
1250 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1251 if (len == 0) { \
1252 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1253 return -1; \
1254 } \
1255 if (buff[0] == '-') { \
1256 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1257 return -1; \
1258 } \
1259 errno = 0; /* this is needed as strtoull doesn't reset errno */ \
1260 num = strtoull(buff, &end, _base); \
1261 cast_num = (_type)(num); \
1262 if (end == buff) { \
1263 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1264 return -1; \
1265 } \
1266 if (num > cast_num) { \
1267 overflow: \
1268 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1269 *out = (_type)(_max); \
1270 return -1; \
1271 } \
1272 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == ULLONG_MAX))) goto overflow; \
1273 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1274 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1275 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1276 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1277 *out = (_type)(_max); \
1278 FR_SBUFF_ERROR_RETURN(&our_in); \
1279 } \
1280 if (err) *err = FR_SBUFF_PARSE_OK; \
1281 *out = cast_num; \
1282 } else { \
1283 if (err) *err = FR_SBUFF_PARSE_OK; \
1284 *out = cast_num; \
1285 } \
1286 return fr_sbuff_advance(in, end - buff); /* Advance by the length strtoull gives us */ \
1287}
1288
1289/* max chars here is the octal string value with prefix */
1291SBUFF_PARSE_UINT_DEF(uint16, uint16_t, UINT16_MAX, 7, 0)
1292SBUFF_PARSE_UINT_DEF(uint32, uint32_t, UINT32_MAX, 12, 0)
1293SBUFF_PARSE_UINT_DEF(uint64, uint64_t, UINT64_MAX, 23, 0)
1294SBUFF_PARSE_UINT_DEF(size, size_t, SIZE_MAX, 23, 0)
1295
1296SBUFF_PARSE_UINT_DEF(uint8_dec, uint8_t, UINT8_MAX, 3, 0)
1297SBUFF_PARSE_UINT_DEF(uint16_dec, uint16_t, UINT16_MAX, 4, 0)
1298SBUFF_PARSE_UINT_DEF(uint32_dec, uint32_t, UINT32_MAX, 10, 0)
1299SBUFF_PARSE_UINT_DEF(uint64_dec, uint64_t, UINT64_MAX, 19, 0)
1300SBUFF_PARSE_UINT_DEF(size_dec, size_t, SIZE_MAX, 19, 0)
1301
1302
1303SBUFF_PARSE_UINT_DEF(uint8_oct, uint8_t, UINT8_MAX, 3, 8)
1304SBUFF_PARSE_UINT_DEF(uint16_oct, uint16_t, UINT16_MAX, 6, 8)
1305SBUFF_PARSE_UINT_DEF(uint32_oct, uint32_t, UINT32_MAX, 11, 8)
1306SBUFF_PARSE_UINT_DEF(uint64_oct, uint64_t, UINT64_MAX, 22, 8)
1307SBUFF_PARSE_UINT_DEF(size_oct, size_t, SIZE_MAX, 22, 8)
1308
1309SBUFF_PARSE_UINT_DEF(uint8_hex, uint8_t, UINT8_MAX, 2, 16)
1310SBUFF_PARSE_UINT_DEF(uint16_hex, uint16_t, UINT16_MAX, 4, 16)
1311SBUFF_PARSE_UINT_DEF(uint32_hex, uint32_t, UINT32_MAX, 8, 16)
1312SBUFF_PARSE_UINT_DEF(uint64_hex, uint64_t, UINT64_MAX, 16, 16)
1313SBUFF_PARSE_UINT_DEF(size_hex, size_t, SIZE_MAX, 22, 16)
1314
1315/** Used to define a number parsing functions for floats
1316 *
1317 * @param[in] _name Function suffix.
1318 * @param[in] _type Output type.
1319 * @param[in] _func Parsing function to use.
1320 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1321 * Can't use stringify because of width modifiers like 'u'
1322 * used in <stdint.h>.
1323 */
1324#define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char) \
1325fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1326{ \
1327 char buff[_max_char + 1] = ""; \
1328 char *end; \
1329 fr_sbuff_t our_in = FR_SBUFF(in); \
1330 size_t len; \
1331 _type res; \
1332 len = fr_sbuff_out_bstrncpy_allowed(&FR_SBUFF_OUT(buff, sizeof(buff)), &our_in, SIZE_MAX, sbuff_char_class_float); \
1333 if (len == sizeof(buff)) { \
1334 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1335 return -1; \
1336 } else if (len == 0) { \
1337 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1338 return -1; \
1339 } \
1340 errno = 0; /* this is needed as parsing functions don't reset errno */ \
1341 res = _func(buff, &end); \
1342 if (errno == ERANGE) { \
1343 if (res > 0) { \
1344 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1345 } else { \
1346 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1347 } \
1348 return -1; \
1349 } \
1350 if (no_trailing && (*end != '\0')) { \
1351 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1352 FR_SBUFF_ERROR_RETURN(&our_in); \
1353 } \
1354 *out = res; \
1355 return fr_sbuff_advance(in, end - buff); \
1356}
1357
1358SBUFF_PARSE_FLOAT_DEF(float32, float, strtof, 100)
1359SBUFF_PARSE_FLOAT_DEF(float64, double, strtod, 100)
1360
1361/** Move data from one sbuff to another
1362 *
1363 * @note Do not call this function directly use #fr_sbuff_move
1364 *
1365 * Both in and out will be advanced by len, with len set to the shortest
1366 * value between the user specified value, the number of bytes remaining
1367 * in the input buffer (after extension), and the number of bytes remaining
1368 * in the output buffer (after extension).
1369 *
1370 * @param[in] out sbuff to copy data to.
1371 * @param[in] in sbuff to copy data from.
1372 * @param[in] len Maximum length of string to copy.
1373 * @return The amount of data copied.
1374 */
1376{
1377 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1378 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1379 size_t to_copy = len;
1380 if (to_copy > o_remaining) to_copy = o_remaining;
1381 if (to_copy > i_remaining) to_copy = i_remaining;
1383 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1384}
1385
1386/** Move data from a marker to an sbuff
1387 *
1388 * @note Do not call this function directly use #fr_sbuff_move
1389 *
1390 * @param[in] out sbuff to copy data to.
1391 * @param[in] in marker to copy data from.
1392 * @param[in] len Maximum length of string to copy.
1393 * @return The amount of data copied.
1394 */
1396{
1397 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1398 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1399 size_t to_copy = len;
1400 if (to_copy > o_remaining) to_copy = o_remaining;
1401 if (to_copy > i_remaining) to_copy = i_remaining;
1403 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1404}
1405
1406/** Move data from one marker to another
1407 *
1408 * @note Do not call this function directly use #fr_sbuff_move
1409 *
1410 * @param[in] out marker to copy data to.
1411 * @param[in] in marker to copy data from.
1412 * @param[in] len Maximum length of string to copy.
1413 * @return The amount of data copied.
1414 */
1416{
1417 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1418 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1419 size_t to_copy = len;
1420 if (to_copy > o_remaining) to_copy = o_remaining;
1421 if (to_copy > i_remaining) to_copy = i_remaining;
1423 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1424}
1425
1426/** Move data from an sbuff to a marker
1427 *
1428 * @note Do not call this function directly use #fr_sbuff_move
1429 *
1430 * @param[in] out marker to copy data to.
1431 * @param[in] in sbuff to copy data from.
1432 * @param[in] len Maximum length of string to copy.
1433 * @return The amount of data copied.
1434 */
1436{
1437 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1438 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1439 size_t to_copy = len;
1440 if (to_copy > o_remaining) to_copy = o_remaining;
1441 if (to_copy > i_remaining) to_copy = i_remaining;
1443 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1444}
1445
1446/** Copy bytes into the sbuff up to the first \0
1447 *
1448 * @param[in] sbuff to copy into.
1449 * @param[in] str to copy into buffer.
1450 * @return
1451 * - >= 0 the number of bytes copied into the sbuff.
1452 * - <0 the number of bytes required to complete the copy operation.
1453 */
1454ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
1455{
1456 size_t len;
1457
1458 CHECK_SBUFF_INIT(sbuff);
1459
1460 if (unlikely(sbuff->is_const)) return 0;
1461
1462 len = strlen(str);
1464
1465 safecpy(sbuff->p, sbuff->end, str, str + len);
1466 sbuff->p[len] = '\0';
1467
1468 return fr_sbuff_advance(sbuff, len);
1469}
1470
1471/** Copy bytes into the sbuff up to the first \0
1472 *
1473 * @param[in] sbuff to copy into.
1474 * @param[in] str to copy into buffer.
1475 * @param[in] len number of bytes to copy.
1476 * @return
1477 * - >= 0 the number of bytes copied into the sbuff.
1478 * - <0 the number of bytes required to complete the copy operation.
1479 */
1480ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
1481{
1482 CHECK_SBUFF_INIT(sbuff);
1483
1484 if (unlikely(sbuff->is_const)) return 0;
1485
1487
1488 safecpy(sbuff->p, sbuff->end, str, str + len);
1489 sbuff->p[len] = '\0';
1490
1491 return fr_sbuff_advance(sbuff, len);
1492}
1493
1494/** Copy bytes into the sbuff up to the first \0
1495 *
1496 * @param[in] sbuff to copy into.
1497 * @param[in] str talloced buffer to copy into sbuff.
1498 * @return
1499 * - >= 0 the number of bytes copied into the sbuff.
1500 * - <0 the number of bytes required to complete the copy operation.
1501 */
1503{
1504 size_t len;
1505
1506 CHECK_SBUFF_INIT(sbuff);
1507
1508 if (unlikely(sbuff->is_const)) return 0;
1509
1510 len = talloc_array_length(str) - 1;
1511
1513
1514 safecpy(sbuff->p, sbuff->end, str, str + len);
1515 sbuff->p[len] = '\0';
1516
1517 return fr_sbuff_advance(sbuff, len);
1518}
1519
1520/** Free the scratch buffer used for printf
1521 *
1522 */
1523static int _sbuff_scratch_free(void *arg)
1524{
1525 sbuff_scratch_freed = true;
1526 return talloc_free(arg);
1527}
1528
1529static inline CC_HINT(always_inline) int sbuff_scratch_init(TALLOC_CTX **out)
1530{
1531 TALLOC_CTX *scratch;
1532
1533 if (sbuff_scratch_freed) {
1534 *out = NULL;
1535 return 0;
1536 }
1537
1538 scratch = sbuff_scratch;
1539 if (!scratch) {
1540 scratch = talloc_pool(NULL, 4096);
1541 if (unlikely(!scratch)) {
1542 fr_strerror_const("Out of Memory");
1543 return -1;
1544 }
1546 }
1547
1548 *out = scratch;
1549
1550 return 0;
1551}
1552
1553/** Print using a fmt string to an sbuff
1554 *
1555 * @param[in] sbuff to print into.
1556 * @param[in] fmt string.
1557 * @param[in] ap arguments for format string.
1558 * @return
1559 * - >= 0 the number of bytes printed into the sbuff.
1560 * - <0 the number of bytes required to complete the print operation.
1561 */
1562ssize_t fr_sbuff_in_vsprintf(fr_sbuff_t *sbuff, char const *fmt, va_list ap)
1563{
1564 TALLOC_CTX *scratch;
1565 va_list ap_p;
1566 char *tmp;
1567 ssize_t slen;
1568
1569 CHECK_SBUFF_INIT(sbuff);
1570
1571 if (unlikely(sbuff->is_const)) return 0;
1572
1573 if (sbuff_scratch_init(&scratch) < 0) return 0;
1574
1575 va_copy(ap_p, ap);
1576 tmp = fr_vasprintf(scratch, fmt, ap_p);
1577 va_end(ap_p);
1578 if (!tmp) return 0;
1579
1580 slen = fr_sbuff_in_bstrcpy_buffer(sbuff, tmp);
1581 talloc_free(tmp); /* Free the temporary buffer */
1582
1583 return slen;
1584}
1585
1586/** Print using a fmt string to an sbuff
1587 *
1588 * @param[in] sbuff to print into.
1589 * @param[in] fmt string.
1590 * @param[in] ... arguments for format string.
1591 * @return
1592 * - >= 0 the number of bytes printed into the sbuff.
1593 * - <0 the number of bytes required to complete the print operation.
1594 */
1596{
1597 va_list ap;
1598 ssize_t slen;
1599
1600 if (unlikely(sbuff->is_const)) return 0;
1601
1602 va_start(ap, fmt);
1603 slen = fr_sbuff_in_vsprintf(sbuff, fmt, ap);
1604 va_end(ap);
1605
1606 return slen;
1607}
1608
1609/** Print an escaped string to an sbuff
1610 *
1611 * @param[in] sbuff to print into.
1612 * @param[in] in to escape.
1613 * @param[in] inlen of string to escape.
1614 * @param[in] e_rules Escaping rules. Used to escape special characters
1615 * as data is written to the sbuff. May be NULL.
1616 * @return
1617 * - >= 0 the number of bytes printed into the sbuff.
1618 * - <0 the number of bytes required to complete the print operation.
1619 */
1620ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules)
1621{
1622 char const *end = in + inlen;
1623 char const *p = in;
1624 fr_sbuff_t our_sbuff;
1625
1626 /* Significantly quicker if there are no rules */
1627 if (!e_rules || (e_rules->chr == '\0')) return fr_sbuff_in_bstrncpy(sbuff, in, inlen);
1628
1629 CHECK_SBUFF_INIT(sbuff);
1630
1631 if (unlikely(sbuff->is_const)) return 0;
1632
1633 our_sbuff = FR_SBUFF(sbuff);
1634 while (p < end) {
1635 size_t clen;
1636 uint8_t c = (uint8_t)*p;
1637 char sub;
1638
1639 /*
1640 * We don't support escaping UTF8 sequences
1641 * as they're not used anywhere in our
1642 * grammar.
1643 */
1644 if (e_rules->do_utf8 && ((clen = fr_utf8_char((uint8_t const *)p, end - p)) > 1)) {
1645 FR_SBUFF_IN_BSTRNCPY_RETURN(&our_sbuff, p, clen);
1646 p += clen;
1647 continue;
1648 }
1649
1650 /*
1651 * Check if there's a special substitution
1652 * like 0x0a -> \n.
1653 */
1654 sub = e_rules->subs[c];
1655 if (sub != '\0') {
1656 FR_SBUFF_IN_CHAR_RETURN(&our_sbuff, e_rules->chr, sub);
1657 p++;
1658 continue;
1659 }
1660
1661 /*
1662 * Check if the character is in the range
1663 * we escape.
1664 */
1665 if (e_rules->esc[c]) {
1666 /*
1667 * For legacy reasons we prefer
1668 * octal escape sequences.
1669 */
1670 if (e_rules->do_oct) {
1671 FR_SBUFF_IN_SPRINTF_RETURN(&our_sbuff, "%c%03o", e_rules->chr, (uint8_t)*p++);
1672 continue;
1673 } else if (e_rules->do_hex) {
1674 FR_SBUFF_IN_SPRINTF_RETURN(&our_sbuff, "%cx%02x", e_rules->chr, (uint8_t)*p++);
1675 continue;
1676 }
1677 }
1678
1679 FR_SBUFF_IN_CHAR_RETURN(&our_sbuff, *p++);
1680 }
1681
1682 FR_SBUFF_SET_RETURN(sbuff, &our_sbuff);
1683}
1684
1685/** Print an escaped string to an sbuff taking a talloced buffer as input
1686 *
1687 * @param[in] sbuff to print into.
1688 * @param[in] in to escape.
1689 * @param[in] e_rules Escaping rules. Used to escape special characters
1690 * as data is written to the sbuff. May be NULL.
1691 * @return
1692 * - >= 0 the number of bytes printed into the sbuff.
1693 * - <0 the number of bytes required to complete the print operation.
1694 */
1696{
1697 if (unlikely(!in)) return 0;
1698
1699 if (unlikely(sbuff->is_const)) return 0;
1700
1701 return fr_sbuff_in_escape(sbuff, in, talloc_array_length(in) - 1, e_rules);
1702}
1703
1704/** Return true and advance past the end of the needle if needle occurs next in the sbuff
1705 *
1706 * @param[in] sbuff to search in.
1707 * @param[in] needle to search for.
1708 * @param[in] needle_len of needle. If SIZE_MAX strlen is used
1709 * to determine length of the needle.
1710 * @return how many bytes we advanced
1711 */
1712size_t fr_sbuff_adv_past_str(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
1713{
1714 char const *found;
1715
1716 CHECK_SBUFF_INIT(sbuff);
1717
1718 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1719
1720 /*
1721 * If there's insufficient bytes in the
1722 * buffer currently, try to extend it,
1723 * returning if we can't.
1724 */
1725 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) < needle_len) return 0;
1726
1727 found = memmem(sbuff->p, needle_len, needle, needle_len); /* sbuff needle_len and needle needle_len ensures match must be next */
1728 if (!found) return 0;
1729
1730 return fr_sbuff_advance(sbuff, needle_len);
1731}
1732
1733/** Return true and advance past the end of the needle if needle occurs next in the sbuff
1734 *
1735 * This function is similar to fr_sbuff_adv_past_str but is case insensitive.
1736 *
1737 * @param[in] sbuff to search in.
1738 * @param[in] needle to search for.
1739 * @param[in] needle_len of needle. If SIZE_MAX strlen is used
1740 * to determine length of the needle.
1741 * @return how many bytes we advanced
1742 */
1743size_t fr_sbuff_adv_past_strcase(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
1744{
1745 char const *p, *n_p;
1746 char const *end;
1747
1748 CHECK_SBUFF_INIT(sbuff);
1749
1750 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1751
1752 /*
1753 * If there's insufficient bytes in the
1754 * buffer currently, try to extend it,
1755 * returning if we can't.
1756 */
1757 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) < needle_len) return 0;
1758
1759 p = sbuff->p;
1760 end = p + needle_len;
1761
1762 for (p = sbuff->p, n_p = needle; p < end; p++, n_p++) {
1763 if (tolower((uint8_t) *p) != tolower((uint8_t) *n_p)) return 0;
1764 }
1765
1766 return fr_sbuff_advance(sbuff, needle_len);
1767}
1768
1769/** Wind position past characters in the allowed set
1770 *
1771 * @param[in] sbuff sbuff to search in.
1772 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1773 * @param[in] allowed character set.
1774 * @param[in] tt If not NULL, stop if we find a terminal sequence.
1775 * @return how many bytes we advanced.
1776 */
1777size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool
1778 const allowed[static UINT8_MAX + 1], fr_sbuff_term_t const *tt)
1779{
1780 size_t total = 0;
1781 char const *p;
1782 uint8_t idx[UINT8_MAX + 1]; /* Fast path index */
1783 size_t needle_len = 0;
1784
1785 CHECK_SBUFF_INIT(sbuff);
1786
1787 if (tt) fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
1788
1789 while (total < len) {
1790 char *end;
1791
1792 if (!fr_sbuff_extend(sbuff)) break;
1793
1794 end = CONSTRAINED_END(sbuff, len, total);
1795 p = sbuff->p;
1796 while ((p < end) && allowed[(uint8_t)*p]) {
1797 if (needle_len == 0) {
1798 p++;
1799 continue;
1800 }
1801
1802 /*
1803 * If this character is allowed, BUT is also listed as a one-character terminal,
1804 * then we still allow it. This decision implements "greedy" parsing.
1805 */
1806 if (fr_sbuff_terminal_search(sbuff, p, idx, tt, 1)) {
1807 p++;
1808 continue;
1809 }
1810
1811 /*
1812 * Otherwise if the next *set* of characters) is not in the terminals, then
1813 * allow the current character.
1814 */
1815 if (!fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) {
1816 p++;
1817 continue;
1818 }
1819
1820 /*
1821 * The character is allowed, and is NOT listed as a terminal character by itself.
1822 * However, it is part of a multi-character terminal sequence. We therefore
1823 * stop.
1824 *
1825 * This decision allows us to parse things like "Framed-User", where we might
1826 * normally stop at the "-". However, we will still stop at "Framed-=User", as
1827 * "-=" may be a terminal sequence.
1828 *
1829 * There is no perfect solution here, other than to fix the input grammar so that
1830 * it has no ambiguity. Since we can't do that, we choose to err on the side of
1831 * allowing the existing grammar, where it makes sense
1832 */
1833 break;
1834 }
1835
1836 total += fr_sbuff_set(sbuff, p);
1837 if (p != end) break; /* stopped early, break */
1838 }
1839
1840 return total;
1841}
1842
1843/** Wind position until we hit a character in the terminal set
1844 *
1845 * @param[in] sbuff sbuff to search in.
1846 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1847 * @param[in] tt Token terminals in the encompassing grammar.
1848 * @param[in] escape_chr If not '\0', ignore characters in the tt set when
1849 * prefixed with this escape character.
1850 * @return how many bytes we advanced.
1851 */
1852size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
1853{
1854 size_t total = 0;
1855 char const *p;
1856 bool do_escape = false; /* Track state across extensions */
1857
1858 uint8_t idx[UINT8_MAX + 1]; /* Fast path index */
1859 size_t needle_len = 1;
1860
1861 CHECK_SBUFF_INIT(sbuff);
1862
1863 /*
1864 * Initialise the fastpath index and
1865 * figure out the longest needle.
1866 */
1867 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
1868
1869 while (total < len) {
1870 char *end;
1871
1872 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) == 0) break;
1873
1874 end = CONSTRAINED_END(sbuff, len, total);
1875 p = sbuff->p;
1876
1877 if (escape_chr == '\0') {
1878 while ((p < end) && !fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) p++;
1879 } else {
1880 while (p < end) {
1881 if (do_escape) {
1882 do_escape = false;
1883 } else if (*p == escape_chr) {
1884 do_escape = true;
1885 } else if (fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) {
1886 break;
1887 }
1888 p++;
1889 }
1890 }
1891
1892 total += fr_sbuff_set(sbuff, p);
1893 if (p != end) break; /* stopped early, break */
1894 }
1895
1896 return total;
1897}
1898
1899/** Wind position to first instance of specified multibyte utf8 char
1900 *
1901 * Only use this function if the search char could be multibyte,
1902 * as there's a large performance penalty.
1903 *
1904 * @param[in,out] sbuff to search in.
1905 * @param[in] len the maximum number of characters to search in sbuff.
1906 * @param[in] chr to search for.
1907 * @return
1908 * - NULL, no instances found.
1909 * - The position of the first character.
1910 */
1911char *fr_sbuff_adv_to_chr_utf8(fr_sbuff_t *sbuff, size_t len, char const *chr)
1912{
1913 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
1914 size_t total = 0;
1915 size_t clen = strlen(chr);
1916
1917 CHECK_SBUFF_INIT(sbuff);
1918
1919 /*
1920 * Needle bigger than haystack
1921 */
1922 if (len < clen) return NULL;
1923
1924 while (total <= (len - clen)) {
1925 char const *found;
1926 char *end;
1927
1928 /*
1929 * Ensure we have enough chars to match
1930 * the needle.
1931 */
1932 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, clen) < clen) break;
1933
1934 end = CONSTRAINED_END(&our_sbuff, len, total);
1935
1936 found = fr_utf8_strchr(NULL, our_sbuff.p, end - our_sbuff.p, chr);
1937 if (found) {
1938 (void)fr_sbuff_set(sbuff, found);
1939 return sbuff->p;
1940 }
1941 total += fr_sbuff_set(&our_sbuff, (end - clen) + 1);
1942 }
1943
1944 return NULL;
1945}
1946
1947/** Wind position to first instance of specified char
1948 *
1949 * @param[in,out] sbuff to search in.
1950 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1951 * @param[in] c to search for.
1952 * @return
1953 * - NULL, no instances found.
1954 * - The position of the first character.
1955 */
1956char *fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
1957{
1958 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
1959 size_t total = 0;
1960
1961 CHECK_SBUFF_INIT(sbuff);
1962
1963 while (total < len) {
1964 char const *found;
1965 char *end;
1966
1967 if (!fr_sbuff_extend(&our_sbuff)) break;
1968
1969 end = CONSTRAINED_END(sbuff, len, total);
1970 found = memchr(our_sbuff.p, c, end - our_sbuff.p);
1971 if (found) {
1972 (void)fr_sbuff_set(sbuff, found);
1973 return sbuff->p;
1974 }
1975
1976 total += fr_sbuff_set(&our_sbuff, end);
1977 }
1978
1979 return NULL;
1980}
1981
1982/** Wind position to the first instance of the specified needle
1983 *
1984 * @param[in,out] sbuff sbuff to search in.
1985 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1986 * @param[in] needle to search for.
1987 * @param[in] needle_len Length of the needle. SIZE_MAX to used strlen.
1988 * @return
1989 * - NULL, no instances found.
1990 * - The position of the first character.
1991 */
1992char *fr_sbuff_adv_to_str(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
1993{
1994 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
1995 size_t total = 0;
1996
1997 CHECK_SBUFF_INIT(sbuff);
1998
1999 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2000 if (!needle_len) return 0;
2001
2002 /*
2003 * Needle bigger than haystack
2004 */
2005 if (len < needle_len) return NULL;
2006
2007 while (total <= (len - needle_len)) {
2008 char const *found;
2009 char *end;
2010
2011 /*
2012 * If the needle is longer than
2013 * the remaining buffer, return.
2014 */
2015 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, needle_len) < needle_len) break;
2016
2017 end = CONSTRAINED_END(&our_sbuff, len, total);
2018 found = memmem(our_sbuff.p, end - our_sbuff.p, needle, needle_len);
2019 if (found) {
2020 (void)fr_sbuff_set(sbuff, found);
2021 return sbuff->p;
2022 }
2023
2024 /*
2025 * Partial needle may be in
2026 * the end of the buffer so
2027 * don't advance too far.
2028 */
2029 total += fr_sbuff_set(&our_sbuff, (end - needle_len) + 1);
2030 }
2031
2032 return NULL;
2033}
2034
2035/** Wind position to the first instance of the specified needle
2036 *
2037 * @param[in,out] sbuff sbuff to search in.
2038 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
2039 * @param[in] needle to search for.
2040 * @param[in] needle_len Length of the needle. SIZE_MAX to used strlen.
2041 * @return
2042 * - NULL, no instances found.
2043 * - The position of the first character.
2044 */
2045char *fr_sbuff_adv_to_strcase(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
2046{
2047 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
2048 size_t total = 0;
2049
2050 CHECK_SBUFF_INIT(sbuff);
2051
2052 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2053 if (!needle_len) return 0;
2054
2055 /*
2056 * Needle bigger than haystack
2057 */
2058 if (len < needle_len) return NULL;
2059
2060 while (total <= (len - needle_len)) {
2061 char *p, *end;
2062 char const *n_p;
2063
2064 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, needle_len) < needle_len) break;
2065
2066 for (p = our_sbuff.p, n_p = needle, end = our_sbuff.p + needle_len;
2067 (p < end) && (tolower((uint8_t) *p) == tolower((uint8_t) *n_p));
2068 p++, n_p++);
2069 if (p == end) {
2070 (void)fr_sbuff_set(sbuff, our_sbuff.p);
2071 return sbuff->p;
2072 }
2073
2074 total += fr_sbuff_advance(&our_sbuff, 1);
2075 }
2076
2077 return NULL;
2078}
2079
2080/** Return true if the current char matches, and if it does, advance
2081 *
2082 * @param[in] sbuff to search for char in.
2083 * @param[in] c char to search for.
2084 * @return
2085 * - true and advance if the next character matches.
2086 * - false and don't advance if the next character doesn't match.
2087 */
2089{
2090 CHECK_SBUFF_INIT(sbuff);
2091
2092 if (!fr_sbuff_extend(sbuff)) return false;
2093
2094 if (*sbuff->p != c) return false;
2095
2096 fr_sbuff_advance(sbuff, 1);
2097
2098 return true;
2099}
2100
2101/** Return true and advance if the next char does not match
2102 *
2103 * @param[in] sbuff to search for char in.
2104 * @param[in] c char to search for.
2105 * @return
2106 * - true and advance unless the character matches.
2107 * - false and don't advance if the next character matches.
2108 */
2110{
2111 CHECK_SBUFF_INIT(sbuff);
2112
2113 if (!fr_sbuff_extend(sbuff)) return false;
2114
2115 if (*sbuff->p == c) return false;
2116
2117 fr_sbuff_advance(sbuff, 1);
2118
2119 return true;
2120}
2121
2122/** Trim trailing characters from a string we're composing
2123 *
2124 * @param[in] sbuff to trim trailing characters from.
2125 * @param[in] to_trim Charset to trim.
2126 * @return how many chars we removed.
2127 */
2128size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static UINT8_MAX + 1])
2129{
2130 char *p = sbuff->p - 1;
2131 ssize_t slen;
2132
2133 while ((p >= sbuff->start) && to_trim[(uint8_t)*p]) p--;
2134
2135 slen = fr_sbuff_set(sbuff, p + 1);
2136 if (slen != 0) fr_sbuff_terminate(sbuff);
2137
2138 return slen;
2139}
2140
2141/** Efficient terminal string search
2142 *
2143 * Caller should ensure that a buffer extension of needle_len bytes has been requested
2144 * before calling this function.
2145 *
2146 * @param[in] in Sbuff to search in.
2147 * @param[in] tt Token terminals in the encompassing grammar.
2148 * @return
2149 * - true if found.
2150 * - false if not.
2151 */
2153{
2154 uint8_t idx[UINT8_MAX + 1]; /* Fast path index */
2155 size_t needle_len = 1;
2156
2157 /*
2158 * No terminal, check for EOF.
2159 */
2160 if (!tt) {
2161 fr_sbuff_extend_status_t status = 0;
2162
2163 if ((fr_sbuff_extend_lowat(&status, in, 1) == 0) &&
2164 (status & FR_SBUFF_FLAG_EXTEND_ERROR) == 0) {
2165 return true;
2166 }
2167
2168 return false;
2169 }
2170
2171 /*
2172 * Initialise the fastpath index and
2173 * figure out the longest needle.
2174 */
2175 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
2176
2177 fr_sbuff_extend_lowat(NULL, in, needle_len);
2178
2179 return fr_sbuff_terminal_search(in, in->p, idx, tt, needle_len);
2180}
2181
2182/** Print a char in a friendly format
2183 *
2184 */
2185static char const *sbuff_print_char(char c)
2186{
2187 static bool const unprintables[UINT8_MAX + 1] = {
2190 };
2191
2192 static _Thread_local char str[10][5];
2193 static _Thread_local char **p;
2194
2195 switch (c) {
2196 case '\a':
2197 return "\a";
2198
2199 case '\b':
2200 return "\b";
2201
2202 case '\n':
2203 return "\n";
2204
2205 case '\r':
2206 return "\r";
2207
2208 case '\t':
2209 return "\t";
2210
2211 case '\f':
2212 return "\f";
2213
2214 case '\v':
2215 return "\v";
2216
2217 default:
2218 if (!p || (p++ >= ((char **)str + (NUM_PTR_ELEMENTS(str) - 1)))) p = (char **)str;
2219
2220 if (unprintables[(uint8_t)c]) {
2221 snprintf(*p, sizeof(*str), "\\x%x", c);
2222 return *p;
2223 }
2224
2225 *p[0] = c;
2226 *p[1] = '\0';
2227 return *p;
2228 }
2229}
2230
2232{
2233 uint8_t i;
2234
2235 FR_FAULT_LOG("Escape rules %s (%p)", escapes->name, escapes);
2236 FR_FAULT_LOG("chr : %c", escapes->chr ? escapes->chr : ' ');
2237 FR_FAULT_LOG("do_hex : %s", escapes->do_hex ? "yes" : "no");
2238 FR_FAULT_LOG("do_oct : %s", escapes->do_oct ? "yes" : "no");
2239
2240 FR_FAULT_LOG("substitutions:");
2241 for (i = 0; i < UINT8_MAX; i++) {
2242 if (escapes->subs[i]) FR_FAULT_LOG("\t%s -> %s",
2243 sbuff_print_char((char)i),
2244 sbuff_print_char((char)escapes->subs[i]));
2245 }
2246 FR_FAULT_LOG("skipes:");
2247 for (i = 0; i < UINT8_MAX; i++) {
2248 if (escapes->skip[i]) FR_FAULT_LOG("\t%s", sbuff_print_char((char)i));
2249 }
2250}
2251
2253{
2254 size_t i;
2255
2256 FR_FAULT_LOG("Terminal count %zu", tt->len);
2257
2258 for (i = 0; i < tt->len; i++) FR_FAULT_LOG("\t\"%s\" (%zu)", tt->elem[i].str, tt->elem[i].len);
2259}
2260
2261void fr_sbuff_parse_rules_debug(fr_sbuff_parse_rules_t const *p_rules)
2262{
2263 FR_FAULT_LOG("Parse rules %p", p_rules);
2264
2265 if (p_rules->escapes) {
2266 fr_sbuff_unescape_debug(p_rules->escapes);
2267 } else {
2268 FR_FAULT_LOG("No unescapes");
2269 }
2270
2271 if (p_rules->terminals) {
2272 fr_sbuff_terminal_debug(p_rules->terminals);
2273 } else {
2274 FR_FAULT_LOG("No terminals");
2275 }
2276}
va_end(args)
static int const char * fmt
Definition acutest.h:573
va_start(args, fmt)
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:221
#define RCSID(id)
Definition build.h:483
#define MEMCMP_RETURN(_a, _b, _field, _len_field)
Return if the contents of the specified field is not identical between the specified structures.
Definition build.h:156
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define NUM_PTR_ELEMENTS(_t)
Definition build.h:344
#define unlikely(_x)
Definition build.h:381
#define NUM_ELEMENTS(_t)
Definition build.h:337
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:49
static fr_slen_t in
Definition dict.h:824
static const bool escapes[UINT8_MAX+1]
Definition util.c:38
talloc_free(reap)
unsigned short uint16_t
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
unsigned long int size_t
#define UINT8_MAX
@ 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.
char const * fr_utf8_strchr(int *out_chr_len, char const *str, ssize_t inlen, char const *chr)
Return a pointer to the first UTF8 char in a string.
Definition print.c:174
size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
Checks for utf-8, taken from http://www.w3.org/International/questions/qa-forms-utf-8.
Definition print.c:39
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:851
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:80
int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len)
Trim a talloced sbuff to the minimum length required to represent the contained string.
Definition sbuff.c:419
ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1454
#define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char)
Used to define a number parsing functions for floats.
Definition sbuff.c:1324
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1777
ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules)
Print an escaped string to an sbuff.
Definition sbuff.c:1620
bool const sbuff_char_class_float[UINT8_MAX+1]
Definition sbuff.c:70
static _Thread_local char * sbuff_scratch
Definition sbuff.c:30
#define FILL_OR_GOTO_DONE(_out, _in, _len)
Fill as much of the output buffer we can and break on partial copy.
Definition sbuff.c:485
char * fr_sbuff_adv_to_chr_utf8(fr_sbuff_t *sbuff, size_t len, char const *chr)
Wind position to first instance of specified multibyte utf8 char.
Definition sbuff.c:1911
size_t fr_sbuff_extend_talloc(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension)
Reallocate the current buffer.
Definition sbuff.c:355
bool fr_sbuff_eof_file(fr_sbuff_t *sbuff)
Accessor function for the EOF state of the file extendor.
Definition sbuff.c:339
#define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base)
Used to define a number parsing functions for signed integers.
Definition sbuff.c:1240
size_t fr_sbuff_out_unescape_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, fr_sbuff_term_t const *tt, fr_sbuff_unescape_rules_t const *u_rules)
Copy as many allowed characters as possible from a sbuff to a sbuff.
Definition sbuff.c:935
static bool fr_sbuff_terminal_search(fr_sbuff_t *in, char const *p, uint8_t idx[static UINT8_MAX+1], fr_sbuff_term_t const *term, size_t needle_len)
Efficient terminal string search.
Definition sbuff.c:540
#define CHECK_SBUFF_INIT(_sbuff)
Definition sbuff.c:55
void fr_sbuff_terminal_debug(fr_sbuff_term_t const *tt)
Definition sbuff.c:2252
void fr_sbuff_update(fr_sbuff_t *sbuff, char *new_buff, size_t new_len)
Update all markers and pointers in the set of sbuffs to point to new_buff.
Definition sbuff.c:156
static int sbuff_scratch_init(TALLOC_CTX **out)
Definition sbuff.c:1529
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
char * fr_sbuff_adv_to_str(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
Wind position to the first instance of the specified needle.
Definition sbuff.c:1992
bool const sbuff_char_class_uint[UINT8_MAX+1]
Definition sbuff.c:60
void fr_sbuff_unescape_debug(fr_sbuff_unescape_rules_t const *escapes)
Definition sbuff.c:2231
size_t _fr_sbuff_move_marker_to_sbuff(fr_sbuff_t *out, fr_sbuff_marker_t *in, size_t len)
Move data from a marker to an sbuff.
Definition sbuff.c:1395
size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift)
Shift the contents of the sbuff, returning the number of bytes we managed to shift.
Definition sbuff.c:195
size_t sbuff_parse_error_table_len
Definition sbuff.c:52
ssize_t fr_sbuff_in_escape_buffer(fr_sbuff_t *sbuff, char const *in, fr_sbuff_escape_rules_t const *e_rules)
Print an escaped string to an sbuff taking a talloced buffer as input.
Definition sbuff.c:1695
char * fr_sbuff_adv_to_strcase(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
Wind position to the first instance of the specified needle.
Definition sbuff.c:2045
static size_t min(size_t x, size_t y)
Definition sbuff.c:143
size_t fr_sbuff_extend_file(fr_sbuff_extend_status_t *status, fr_sbuff_t *sbuff, size_t extension)
Refresh the buffer with more data from the file.
Definition sbuff.c:265
void fr_sbuff_parse_rules_debug(fr_sbuff_parse_rules_t const *p_rules)
Definition sbuff.c:2261
ssize_t fr_sbuff_out_bstrncpy_exact(fr_sbuff_t *out, fr_sbuff_t *in, size_t len)
Copy exactly len bytes from a sbuff to a sbuff or fail.
Definition sbuff.c:761
size_t fr_sbuff_adv_past_str(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
Return true and advance past the end of the needle if needle occurs next in the sbuff.
Definition sbuff.c:1712
bool const sbuff_char_class_zero[UINT8_MAX+1]
Definition sbuff.c:75
size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static UINT8_MAX+1])
Trim trailing characters from a string we're composing.
Definition sbuff.c:2128
size_t fr_sbuff_out_bstrncpy_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, fr_sbuff_term_t const *tt, fr_sbuff_unescape_rules_t const *u_rules)
Copy as many allowed characters as possible from a sbuff to a sbuff.
Definition sbuff.c:862
char * fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
Wind position to first instance of specified char.
Definition sbuff.c:1956
bool const sbuff_char_class_hostname[UINT8_MAX+1]
Definition sbuff.c:82
int fr_sbuff_reset_talloc(fr_sbuff_t *sbuff)
Reset a talloced buffer to its initial length, clearing any data stored.
Definition sbuff.c:454
static char const * sbuff_print_char(char c)
Print a char in a friendly format.
Definition sbuff.c:2185
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2152
fr_slen_t fr_sbuff_out_bool(bool *out, fr_sbuff_t *in)
See if the string contains a truth value.
Definition sbuff.c:1109
size_t _fr_sbuff_move_marker_to_marker(fr_sbuff_marker_t *out, fr_sbuff_marker_t *in, size_t len)
Move data from one marker to another.
Definition sbuff.c:1415
size_t _fr_sbuff_move_sbuff_to_marker(fr_sbuff_marker_t *out, fr_sbuff_t *in, size_t len)
Move data from an sbuff to a marker.
Definition sbuff.c:1435
ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1480
size_t fr_sbuff_adv_past_strcase(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
Return true and advance past the end of the needle if needle occurs next in the sbuff.
Definition sbuff.c:1743
static ssize_t safecpy(char *o_start, char *o_end, char const *i_start, char const *i_end)
Copy function that allows overlapping memory ranges to be copied.
Definition sbuff.c:123
bool fr_sbuff_next_unless_char(fr_sbuff_t *sbuff, char c)
Return true and advance if the next char does not match.
Definition sbuff.c:2109
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1852
#define CONSTRAINED_END(_sbuff, _max, _used)
Constrain end pointer to prevent advancing more than the amount the caller specified.
Definition sbuff.c:494
static int8_t terminal_cmp(fr_sbuff_term_elem_t const *a, fr_sbuff_term_elem_t const *b)
Compare two terminal elements for ordering purposes.
Definition sbuff.c:621
static void fr_sbuff_terminal_idx_init(size_t *needle_len, uint8_t idx[static UINT8_MAX+1], fr_sbuff_term_t const *term)
Populate a terminal index.
Definition sbuff.c:505
bool const sbuff_char_whitespace[UINT8_MAX+1]
Definition sbuff.c:100
ssize_t fr_sbuff_in_bstrcpy_buffer(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1502
size_t fr_sbuff_out_bstrncpy(fr_sbuff_t *out, fr_sbuff_t *in, size_t len)
Copy as many bytes as possible from a sbuff to a sbuff.
Definition sbuff.c:723
bool const sbuff_char_blank[UINT8_MAX+1]
Definition sbuff.c:108
fr_sbuff_term_t * fr_sbuff_terminals_amerge(TALLOC_CTX *ctx, fr_sbuff_term_t const *a, fr_sbuff_term_t const *b)
Merge two sets of terminal strings.
Definition sbuff.c:645
size_t _fr_sbuff_move_sbuff_to_sbuff(fr_sbuff_t *out, fr_sbuff_t *in, size_t len)
Move data from one sbuff to another.
Definition sbuff.c:1375
#define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base)
Used to define a number parsing functions for signed integers.
Definition sbuff.c:1173
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
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1595
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2088
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
static _Thread_local bool sbuff_scratch_freed
When true, prevent use of the scratch space.
Definition sbuff.c:38
size_t fr_sbuff_out_bstrncpy_allowed(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, bool const allowed[static UINT8_MAX+1])
Copy as many allowed characters as possible from a sbuff to a sbuff.
Definition sbuff.c:816
ssize_t fr_sbuff_in_vsprintf(fr_sbuff_t *sbuff, char const *fmt, va_list ap)
Print using a fmt string to an sbuff.
Definition sbuff.c:1562
static int _sbuff_scratch_free(void *arg)
Free the scratch buffer used for printf.
Definition sbuff.c:1523
TALLOC_CTX * ctx
Context to alloc new buffers in.
Definition sbuff.h:140
#define SBUFF_CHAR_CLASS_HEX
bool skip[UINT8_MAX+1]
Characters that are escaped, but left in the output along with the escape character.
Definition sbuff.h:208
#define SBUFF_CHAR_CLASS_NUM
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_diff(_a, _b)
size_t shifted
How much we've read from this file.
Definition sbuff.h:154
#define FR_SBUFF_BIND_CURRENT(_sbuff_or_marker)
#define fr_sbuff_adv_past_strcase_literal(_sbuff, _needle)
#define fr_sbuff_was_extended(_status)
char const * str
Terminal string.
Definition sbuff.h:162
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_char(_sbuff_or_marker, _eob)
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
bool do_oct
Process oct sequences i.e.
Definition sbuff.h:216
#define fr_sbuff_extend(_sbuff_or_marker)
#define fr_sbuff_buff(_sbuff_or_marker)
#define fr_sbuff_used_total(_sbuff_or_marker)
size_t len
Length of the list.
Definition sbuff.h:172
#define SBUFF_CHAR_CLASS_ALPHA_NUM
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define fr_sbuff_is_digit(_sbuff_or_marker)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
bool do_hex
Process hex sequences i.e.
Definition sbuff.h:215
size_t max
Maximum size of the buffer.
Definition sbuff.h:142
#define fr_sbuff_end(_sbuff_or_marker)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define FR_SBUFF(_sbuff_or_marker)
size_t len
Length of string.
Definition sbuff.h:163
#define FR_SBUFF_IN_BSTRNCPY_RETURN(...)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
char * buff_end
The true end of the buffer.
Definition sbuff.h:152
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_EXTEND_LOWAT_OR_RETURN(_sbuff, _len)
char subs[UINT8_MAX+1]
Special characters and their substitutions.
Definition sbuff.h:205
#define SBUFF_CHAR_UNPRINTABLES_LOW
fr_sbuff_marker_t * next
Next m in the list.
Definition sbuff.h:89
bool eof
are we at EOF?
Definition sbuff.h:155
#define fr_sbuff_used(_sbuff_or_marker)
fr_sbuff_term_elem_t * elem
A sorted list of terminal strings.
Definition sbuff.h:173
#define fr_sbuff_behind(_sbuff_or_marker)
#define fr_sbuff_extend_lowat(_status, _sbuff_or_marker, _lowat)
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
#define fr_sbuff_in_char(_sbuff,...)
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
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
PRIVATE void float64()
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
#define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:180
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
static size_t char fr_sbuff_t size_t inlen
Definition value.h:997
static size_t char ** out
Definition value.h:997