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