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: a1a04855297b393ddc1d99454eb521920dbdd4e2 $")
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, UNUSED 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 if (p > in->end) return false; /* paranoia */
574
575 /*
576 * "p" may be ahead of "in->p", as the caller can scan forward without advancing "in->p`". So we
577 * need to measure bytes available from "p". Othwrwise using fr_sbuff_remaining(in) would
578 * over-state the available bytes by (p - in->p) and read past in->end.
579 */
580 remaining = (size_t)(in->end - p);
581
582 /*
583 * Special case for EOFlike states
584 */
585 if (remaining == 0) {
586 if (!fr_sbuff_is_extendable(in) && (idx['\0'] != 0)) return true;
587 return false;
588 }
589
590 mid = term_idx - 1; /* Inform the mid point from the index */
591
592 while (start <= end) {
593 fr_sbuff_term_elem_t const *elem;
594 size_t tlen;
595 int ret;
596
597 elem = &term->elem[mid];
598 tlen = elem->len;
599
600 ret = memcmp(p, elem->str, tlen < (size_t)remaining ? tlen : (size_t)remaining);
601 if (ret == 0) {
602 /*
603 * If we have more text than the table element, that's fine
604 */
605 if (remaining >= tlen) return true;
606
607 /*
608 * If input was shorter than the table element we need to
609 * keep searching.
610 */
611 ret = -1;
612 }
613
614 if (ret < 0) {
615 end = mid - 1;
616 } else {
617 start = mid + 1;
618 }
619
620 mid = start + ((end - start) / 2); /* Avoid overflow */
621 }
622
623 return false;
624}
625
626/** Compare two terminal elements for ordering purposes
627 *
628 * @param[in] a first terminal to compare.
629 * @param[in] b second terminal to compare.
630 * @return CMP(a,b)
631 */
632static inline int8_t terminal_cmp(fr_sbuff_term_elem_t const *a, fr_sbuff_term_elem_t const *b)
633{
634 return MEMCMP_FIELDS(a, b, str, len);
635}
636
637#if 0
638static void fr_sbuff_terminal_debug_tmp(fr_sbuff_term_elem_t const *elem[], size_t len)
639{
640 size_t i;
641
642 FR_FAULT_LOG("Terminal count %zu", len);
643
644 for (i = 0; i < len; i++) FR_FAULT_LOG("\t\"%s\" (%zu)", elem[i] ? elem[i]->str : "NULL", elem[i] ? elem[i]->len : 0);
645}
646#endif
647
648/** Merge two sets of terminal strings
649 *
650 * @param[in] ctx to allocate the new terminal array in.
651 * @param[in] a first set of terminals to merge.
652 * @param[in] b second set of terminals to merge.
653 * @return A new set of de-duplicated and sorted terminals.
654 */
656{
657 size_t i, j, num;
660
661 /*
662 * Check all inputs are pre-sorted. It doesn't break this
663 * function, but it's useful in case the terminal arrays
664 * are defined elsewhere without merging.
665 */
666#if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
667 if (a->len) for (i = 0; i < a->len - 1; i++) fr_assert(terminal_cmp(&a->elem[i], &a->elem[i + 1]) < 0);
668 if (b->len) for (i = 0; i < b->len - 1; i++) fr_assert(terminal_cmp(&b->elem[i], &b->elem[i + 1]) < 0);
669#endif
670
671 /*
672 * Since the inputs are sorted, we can just do an O(n+m)
673 * walk through the arrays, comparing entries across the
674 * two arrays.
675 *
676 * If there are duplicates, we prefer "a", for no particular reason.
677 */
678 num = i = j = 0;
679 while ((i < a->len) && (j < b->len)) {
680 int8_t cmp;
681
682 cmp = terminal_cmp(&a->elem[i], &b->elem[j]);
683 if (cmp == 0) {
684 j++;
685 tmp[num++] = &a->elem[i++];
686
687 } else if (cmp < 0) {
688 tmp[num++] = &a->elem[i++];
689
690 } else if (cmp > 0) {
691 tmp[num++] = &b->elem[j++];
692 }
693
695 }
696
697 /*
698 * Only one of these will be hit, and it's simpler than nested "if" statements.
699 */
700 while (i < a->len) tmp[num++] = &a->elem[i++];
701 while (j < b->len) tmp[num++] = &b->elem[j++];
702
704 if (unlikely(!out)) return NULL;
705
706 out->elem = talloc_array(out, fr_sbuff_term_elem_t, num);
707 if (unlikely(!out->elem)) {
709 return NULL;
710 }
711 out->len = num;
712
713 for (i = 0; i < num; i++) out->elem[i] = *tmp[i]; /* copy merged results back */
714
715#if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
716 for (i = 0; i < num - 1; i++) fr_assert(terminal_cmp(&out->elem[i], &out->elem[i + 1]) < 0);
717#endif
718
719 return out;
720}
721
722/** Copy as many bytes as possible from a sbuff to a sbuff
723 *
724 * Copy size is limited by available data in sbuff and space in output sbuff.
725 *
726 * @param[out] out Where to copy to.
727 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
728 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
729 * @return
730 * - 0 no bytes copied.
731 * - >0 the number of bytes copied.
732 */
734{
736 size_t remaining;
737
739
740 while (fr_sbuff_used_total(&our_in) < len) {
741 size_t chunk_len;
742
743 remaining = (len - fr_sbuff_used_total(&our_in));
744
745 if (!fr_sbuff_extend(&our_in)) break;
746
747 chunk_len = fr_sbuff_remaining(&our_in);
748 if (chunk_len > remaining) chunk_len = remaining;
749
750 FILL_OR_GOTO_DONE(out, &our_in, chunk_len);
751 }
752
753done:
754 *out->p = '\0';
755 return fr_sbuff_used_total(&our_in);
756}
757
758/** Copy exactly len bytes from a sbuff to a sbuff or fail
759 *
760 * Copy size is limited by available data in sbuff, space in output sbuff, and length.
761 *
762 * @param[out] out Where to copy to.
763 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
764 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
765 * @return
766 * - 0 no bytes copied, no token found of sufficient length in input buffer.
767 * - >0 the number of bytes copied.
768 * - <0 the number of additional output bytes we would have needed to
769 * complete the copy.
770 */
772{
773 fr_sbuff_t our_in = FR_SBUFF(in);
774 size_t remaining;
776
778
779 fr_sbuff_marker(&m, out);
780
781 do {
782 size_t chunk_len;
783 ssize_t copied;
784
785 remaining = (len - fr_sbuff_used_total(&our_in));
786 if (remaining && !fr_sbuff_extend(&our_in)) {
787 fr_sbuff_marker_release(&m);
788 return 0;
789 }
790
791 chunk_len = fr_sbuff_remaining(&our_in);
792 if (chunk_len > remaining) chunk_len = remaining;
793
794 copied = fr_sbuff_in_bstrncpy(out, our_in.p, chunk_len);
795 if (copied < 0) {
796 fr_sbuff_set(out, &m); /* Reset out */
797 *m.p = '\0'; /* Re-terminate */
798
799 /* Amount remaining in input buffer minus the amount we could have copied */
800 if (len == SIZE_MAX) {
801 fr_sbuff_marker_release(&m);
802 return -(fr_sbuff_remaining(in) - (chunk_len + copied));
803 }
804 /* Amount remaining to copy minus the amount we could have copied */
805 fr_sbuff_marker_release(&m);
806 return -(remaining - (chunk_len + copied));
807 }
808 fr_sbuff_advance(&our_in, copied);
809 } while (fr_sbuff_used_total(&our_in) < len);
810
811 fr_sbuff_marker_release(&m);
812
813 FR_SBUFF_SET_RETURN(in, &our_in); /* in was pinned, so this works */
814}
815
816/** Copy as many allowed characters as possible from a sbuff to a sbuff
817 *
818 * Copy size is limited by available data in sbuff and output buffer length.
819 *
820 * As soon as a disallowed character is found the copy is stopped.
821 * The input sbuff will be left pointing at the first disallowed character.
822 *
823 * @param[out] out Where to copy to.
824 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
825 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
826 * @param[in] allowed Characters to include the copy.
827 * @return
828 * - 0 no bytes copied.
829 * - >0 the number of bytes copied.
830 */
832 bool const allowed[static SBUFF_CHAR_CLASS])
833{
835
837
838 while (fr_sbuff_used_total(&our_in) < len) {
839 char *p;
840 char *end;
841
842 if (!fr_sbuff_extend(&our_in)) break;
843
844 p = fr_sbuff_current(&our_in);
845 end = CONSTRAINED_END(&our_in, len, fr_sbuff_used_total(&our_in));
846
847 while ((p < end) && allowed[(uint8_t)*p]) p++;
848
849 FILL_OR_GOTO_DONE(out, &our_in, p - our_in.p);
850
851 if (p != end) break; /* stopped early, break */
852 }
853
854done:
855 *out->p = '\0';
856 return fr_sbuff_used_total(&our_in);
857}
858
859/** Copy as many allowed characters as possible from a sbuff to a sbuff
860 *
861 * Copy size is limited by available data in sbuff and output buffer length.
862 *
863 * As soon as a disallowed character is found the copy is stopped.
864 * The input sbuff will be left pointing at the first disallowed character.
865 *
866 * @param[out] out Where to copy to.
867 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
868 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
869 * @param[in] tt Token terminals in the encompassing grammar.
870 * @param[in] u_rules If not NULL, ignore characters in the until set when
871 * prefixed with u_rules->chr. FIXME - Should actually evaluate
872 * u_rules fully.
873 * @return
874 * - 0 no bytes copied.
875 * - >0 the number of bytes copied.
876 */
878 fr_sbuff_term_t const *tt,
879 fr_sbuff_unescape_rules_t const *u_rules)
880{
882 bool do_escape = false; /* Track state across extensions */
883
884 uint8_t idx[SBUFF_CHAR_CLASS]; /* Fast path index */
885 size_t needle_len = 1;
886 char escape_chr = u_rules ? u_rules->chr : '\0';
887
889
890 /*
891 * Initialise the fastpath index and
892 * figure out the longest needle.
893 */
894 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
895
896 while (fr_sbuff_used_total(&our_in) < len) {
897 char *p;
898 char *end;
899
900 if (fr_sbuff_extend_lowat(NULL, &our_in, needle_len) == 0) break;
901
902 p = fr_sbuff_current(&our_in);
903 end = CONSTRAINED_END(&our_in, len, fr_sbuff_used_total(&our_in));
904
905 if (p == end) break;
906
907 if (escape_chr == '\0') {
908 while ((p < end) && !fr_sbuff_terminal_search(in, p, idx, tt, needle_len)) p++;
909 } else {
910 while (p < end) {
911 if (do_escape) {
912 do_escape = false;
913 } else if (*p == escape_chr) {
914 do_escape = true;
915 } else if (fr_sbuff_terminal_search(in, p, idx, tt, needle_len)) {
916 break;
917 }
918 p++;
919 }
920 }
921
922 FILL_OR_GOTO_DONE(out, &our_in, p - our_in.p);
923
924 if (p != end) break; /* stopped early, break */
925 }
926
927done:
928 *out->p = '\0';
929 return fr_sbuff_used_total(&our_in);
930}
931
932/** Copy as many allowed characters as possible from a sbuff to a sbuff
933 *
934 * Copy size is limited by available data in sbuff and output buffer length.
935 *
936 * As soon as a disallowed character is found the copy is stopped.
937 * The input sbuff will be left pointing at the first disallowed character.
938 *
939 * This de-escapes characters as they're copied out of the sbuff.
940 *
941 * @param[out] out Where to copy to.
942 * @param[in] in Where to copy from. Will copy len bytes from current position in buffer.
943 * @param[in] len How many bytes to copy. If SIZE_MAX the entire buffer will be copied.
944 * @param[in] tt Token terminal strings in the encompassing grammar.
945 * @param[in] u_rules for processing unescape sequences.
946 * @return
947 * - 0 no bytes copied.
948 * - >0 the number of bytes written to out.
949 */
951 fr_sbuff_term_t const *tt,
952 fr_sbuff_unescape_rules_t const *u_rules)
953{
954 fr_sbuff_t our_in;
955 bool do_escape = false; /* Track state across extensions */
959
960 uint8_t idx[SBUFF_CHAR_CLASS]; /* Fast path index */
961 size_t needle_len = 1;
962 fr_sbuff_extend_status_t status = 0;
963
964 /*
965 * If we don't need to do unescaping
966 * call a more suitable function.
967 */
968 if (!u_rules || (u_rules->chr == '\0')) return fr_sbuff_out_bstrncpy_until(out, in, len, tt, u_rules);
969
971
972 our_in = FR_SBUFF(in);
973
974 /*
975 * Chunk tracking...
976 */
977 fr_sbuff_marker(&c_s, &our_in);
978 fr_sbuff_marker(&end, &our_in);
979 fr_sbuff_marker_update_end(&end, len);
980
981 fr_sbuff_marker(&o_s, out);
982
983 /*
984 * Initialise the fastpath index and
985 * figure out the longest needle.
986 */
987 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
988
989 /*
990 * ...while we have remaining data
991 */
992 while (fr_sbuff_extend_lowat(&status, &our_in, needle_len) > 0) {
993 if (fr_sbuff_was_extended(status)) fr_sbuff_marker_update_end(&end, len);
994 if (!fr_sbuff_diff(&our_in, &end)) break; /* Reached the end */
995
996 if (do_escape) {
997 do_escape = false;
998
999 /*
1000 * Check for \x<hex><hex>
1001 */
1002 if (u_rules->do_hex && fr_sbuff_is_char(&our_in, 'x')) {
1003 uint8_t escape;
1005
1006 fr_sbuff_marker(&m, &our_in); /* allow for backtrack */
1007 fr_sbuff_advance(&our_in, 1); /* skip over the 'x' */
1008
1009 if (fr_sbuff_out_uint8_hex(NULL, &escape, &our_in, false) != 2) {
1010 fr_sbuff_set(&our_in, &m); /* backtrack */
1011 fr_sbuff_marker_release(&m);
1012 goto check_subs; /* allow sub for \x */
1013 }
1014
1015 if (fr_sbuff_in_char(out, escape) <= 0) {
1016 fr_sbuff_set(&our_in, &m); /* backtrack */
1017 fr_sbuff_marker_release(&m);
1018 break;
1019 }
1020 fr_sbuff_marker_release(&m);
1021 fr_sbuff_set(&c_s, &our_in);
1022 continue;
1023 }
1024
1025 /*
1026 * Check for <oct><oct><oct>
1027 */
1028 if (u_rules->do_oct && fr_sbuff_is_digit(&our_in)) {
1029 uint8_t escape;
1031
1032 fr_sbuff_marker(&m, &our_in); /* allow for backtrack */
1033
1034 if (fr_sbuff_out_uint8_oct(NULL, &escape, &our_in, false) != 3) {
1035 fr_sbuff_set(&our_in, &m); /* backtrack */
1036 fr_sbuff_marker_release(&m);
1037 goto check_subs; /* allow sub for <oct> */
1038 }
1039
1040 if (fr_sbuff_in_char(out, escape) <= 0) {
1041 fr_sbuff_set(&our_in, &m); /* backtrack */
1042 fr_sbuff_marker_release(&m);
1043 break;
1044 }
1045 fr_sbuff_marker_release(&m);
1046 fr_sbuff_set(&c_s, &our_in);
1047 continue;
1048 }
1049
1050 check_subs:
1051 /*
1052 * Not a recognised hex or octal escape sequence
1053 * may be a substitution or a sequence that
1054 * should be copied to the output buffer.
1055 */
1056 {
1057 uint8_t c = *fr_sbuff_current(&our_in);
1058
1059 if (u_rules->subs[c] == '\0') {
1060 if (u_rules->skip[c] == true) goto next;
1061 goto next_esc;
1062 }
1063
1064 /*
1065 * We already copied everything up
1066 * to this point, so we can now
1067 * write the substituted char to
1068 * the output buffer.
1069 */
1070 if (fr_sbuff_in_char(out, u_rules->subs[c]) <= 0) break;
1071
1072 /*
1073 * ...and advance past the entire
1074 * escape seq in the input buffer.
1075 */
1076 fr_sbuff_advance(&our_in, 1);
1077 fr_sbuff_set(&c_s, &our_in);
1078 continue;
1079 }
1080 }
1081
1082 next_esc:
1083 if (*fr_sbuff_current(&our_in) == u_rules->chr) {
1084 /*
1085 * Copy out any data we got before
1086 * we hit the escape char.
1087 *
1088 * We need to do this before we
1089 * can write the escape char to
1090 * the output sbuff.
1091 */
1093
1094 do_escape = true;
1095 fr_sbuff_advance(&our_in, 1);
1096 continue;
1097 }
1098
1099 next:
1100 if (tt && fr_sbuff_terminal_search(&our_in, fr_sbuff_current(&our_in), idx, tt, needle_len)) break;
1101 fr_sbuff_advance(&our_in, 1);
1102 }
1103
1104 /*
1105 * Copy any remaining data over
1106 */
1108
1109done:
1110 fr_sbuff_set(in, &c_s); /* Only advance by as much as we copied */
1111 *out->p = '\0';
1112
1113 return fr_sbuff_marker_release_behind(&o_s);
1114}
1115
1116/** See if the string contains a truth value
1117 *
1118 * @param[out] out Where to write boolean value.
1119 * @param[in] in Where to search for a truth value.
1120 * @return
1121 * - >0 the number of bytes consumed.
1122 * - -1 no bytes copied, was not a truth value.
1123 */
1125{
1126 fr_sbuff_t our_in = FR_SBUFF(in);
1127
1128 static bool const bool_prefix[SBUFF_CHAR_CLASS] = {
1129 ['t'] = true, ['T'] = true, /* true */
1130 ['f'] = true, ['F'] = true, /* false */
1131 ['y'] = true, ['Y'] = true, /* yes */
1132 ['n'] = true, ['N'] = true, /* no */
1133 };
1134
1135 if (fr_sbuff_is_in_charset(&our_in, bool_prefix)) {
1136 switch (tolower(fr_sbuff_uint8(&our_in, '\0'))) {
1137 default:
1138 break;
1139
1140 case 't':
1141 if (fr_sbuff_adv_past_strcase_literal(&our_in, "true")) {
1142 *out = true;
1143 FR_SBUFF_SET_RETURN(in, &our_in);
1144 }
1145 break;
1146
1147 case 'f':
1148 if (fr_sbuff_adv_past_strcase_literal(&our_in, "false")) {
1149 *out = false;
1150 FR_SBUFF_SET_RETURN(in, &our_in);
1151 }
1152 break;
1153
1154 case 'y':
1155 if (fr_sbuff_adv_past_strcase_literal(&our_in, "yes")) {
1156 *out = true;
1157 FR_SBUFF_SET_RETURN(in, &our_in);
1158 }
1159 break;
1160
1161 case 'n':
1162 if (fr_sbuff_adv_past_strcase_literal(&our_in, "no")) {
1163 *out = false;
1164 FR_SBUFF_SET_RETURN(in, &our_in);
1165 }
1166 break;
1167 }
1168 }
1169
1170 *out = false; /* Always initialise out */
1171
1172 fr_strerror_const("Not a valid boolean value. Accepted values are 'yes', 'no', 'true', 'false'");
1173
1174 return -1;
1175}
1176
1177/** Used to define a number parsing functions for signed integers
1178 *
1179 * @param[in] _name Function suffix.
1180 * @param[in] _type Output type.
1181 * @param[in] _min value.
1182 * @param[in] _max value.
1183 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1184 * Can't use stringify because of width modifiers like 'u'
1185 * used in <stdint.h>.
1186 * @param[in] _base to use.
1187 */
1188#define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base) \
1189fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1190{ \
1191 char buff[_max_char + 1]; \
1192 char *end, *a_end; \
1193 size_t len; \
1194 long long num; \
1195 _type cast_num; \
1196 fr_sbuff_t our_in = FR_SBUFF(in); \
1197 buff[0] = '\0'; /* clang scan */ \
1198 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1199 if (len == 0) { \
1200 if (err) *err = (fr_sbuff_remaining(in) == 0) ? FR_SBUFF_PARSE_ERROR_INPUT_EMPTY : FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1201 return -1; \
1202 } \
1203 errno = 0; /* this is needed as strtoll doesn't reset errno */ \
1204 num = strtoll(buff, &end, _base); \
1205 cast_num = (_type)(num); \
1206 if (end == buff) { \
1207 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1208 return -1; \
1209 } \
1210 if (num > cast_num) { \
1211 overflow: \
1212 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1213 *out = (_type)(_max); \
1214 return -1; \
1215 } \
1216 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == LLONG_MAX))) goto overflow; \
1217 if (num < cast_num) { \
1218 underflow: \
1219 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1220 *out = (_type)(_min); \
1221 return -1; \
1222 } \
1223 if ((errno == ERANGE) && (num == LLONG_MIN)) goto underflow; \
1224 if (no_trailing && ((a_end = in->p + (end - buff)) < in->end)) { \
1225 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1226 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1227 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1228 *out = (_type)(_max); \
1229 FR_SBUFF_ERROR_RETURN(&our_in); \
1230 } \
1231 *out = cast_num; \
1232 } else { \
1233 if (err) *err = FR_SBUFF_PARSE_OK; \
1234 *out = cast_num; \
1235 } \
1236 return fr_sbuff_advance(in, end - buff); /* Advance by the length strtoll gives us */ \
1237}
1238
1239SBUFF_PARSE_INT_DEF(int8, int8_t, INT8_MIN, INT8_MAX, 4, 0)
1240SBUFF_PARSE_INT_DEF(int16, int16_t, INT16_MIN, INT16_MAX, 6, 0)
1241SBUFF_PARSE_INT_DEF(int32, int32_t, INT32_MIN, INT32_MAX, 11, 0)
1242SBUFF_PARSE_INT_DEF(int64, int64_t, INT64_MIN, INT64_MAX, 20, 0)
1243SBUFF_PARSE_INT_DEF(ssize, ssize_t, SSIZE_MIN, SSIZE_MAX, 20, 0)
1244
1245/** Used to define a number parsing functions for signed integers
1246 *
1247 * @param[in] _name Function suffix.
1248 * @param[in] _type Output type.
1249 * @param[in] _max value.
1250 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1251 * Can't use stringify because of width modifiers like 'u'
1252 * used in <stdint.h>.
1253 * @param[in] _base of the number being parsed, 8, 10, 16 etc...
1254 */
1255#define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base) \
1256fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1257{ \
1258 char buff[_max_char + 1]; \
1259 char *end, *a_end; \
1260 size_t len; \
1261 unsigned long long num; \
1262 _type cast_num; \
1263 fr_sbuff_t our_in = FR_SBUFF(in); \
1264 buff[0] = '\0'; /* clang scan */ \
1265 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1266 if (len == 0) { \
1267 if (err) *err = (fr_sbuff_remaining(in) == 0) ? FR_SBUFF_PARSE_ERROR_INPUT_EMPTY : FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1268 return -1; \
1269 } \
1270 if (buff[0] == '-') { \
1271 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1272 return -1; \
1273 } \
1274 errno = 0; /* this is needed as strtoull doesn't reset errno */ \
1275 num = strtoull(buff, &end, _base); \
1276 cast_num = (_type)(num); \
1277 if (end == buff) { \
1278 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1279 return -1; \
1280 } \
1281 if (num > cast_num) { \
1282 overflow: \
1283 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1284 *out = (_type)(_max); \
1285 return -1; \
1286 } \
1287 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == ULLONG_MAX))) goto overflow; \
1288 if (no_trailing && ((a_end = in->p + (end - buff)) < in->end)) { \
1289 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1290 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1291 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1292 *out = (_type)(_max); \
1293 FR_SBUFF_ERROR_RETURN(&our_in); \
1294 } \
1295 if (err) *err = FR_SBUFF_PARSE_OK; \
1296 *out = cast_num; \
1297 } else { \
1298 if (err) *err = FR_SBUFF_PARSE_OK; \
1299 *out = cast_num; \
1300 } \
1301 return fr_sbuff_advance(in, end - buff); /* Advance by the length strtoull gives us */ \
1302}
1303
1304/* max chars here is the octal string value with prefix */
1306SBUFF_PARSE_UINT_DEF(uint16, uint16_t, UINT16_MAX, 7, 0)
1307SBUFF_PARSE_UINT_DEF(uint32, uint32_t, UINT32_MAX, 12, 0)
1308SBUFF_PARSE_UINT_DEF(uint64, uint64_t, UINT64_MAX, 23, 0)
1309SBUFF_PARSE_UINT_DEF(size, size_t, SIZE_MAX, 23, 0)
1310
1311SBUFF_PARSE_UINT_DEF(uint8_dec, uint8_t, UINT8_MAX, 3, 0)
1312SBUFF_PARSE_UINT_DEF(uint16_dec, uint16_t, UINT16_MAX, 4, 0)
1313SBUFF_PARSE_UINT_DEF(uint32_dec, uint32_t, UINT32_MAX, 10, 0)
1314SBUFF_PARSE_UINT_DEF(uint64_dec, uint64_t, UINT64_MAX, 19, 0)
1315SBUFF_PARSE_UINT_DEF(size_dec, size_t, SIZE_MAX, 19, 0)
1316
1317
1318SBUFF_PARSE_UINT_DEF(uint8_oct, uint8_t, UINT8_MAX, 3, 8)
1319SBUFF_PARSE_UINT_DEF(uint16_oct, uint16_t, UINT16_MAX, 6, 8)
1320SBUFF_PARSE_UINT_DEF(uint32_oct, uint32_t, UINT32_MAX, 11, 8)
1321SBUFF_PARSE_UINT_DEF(uint64_oct, uint64_t, UINT64_MAX, 22, 8)
1322SBUFF_PARSE_UINT_DEF(size_oct, size_t, SIZE_MAX, 22, 8)
1323
1324SBUFF_PARSE_UINT_DEF(uint8_hex, uint8_t, UINT8_MAX, 2, 16)
1325SBUFF_PARSE_UINT_DEF(uint16_hex, uint16_t, UINT16_MAX, 4, 16)
1326SBUFF_PARSE_UINT_DEF(uint32_hex, uint32_t, UINT32_MAX, 8, 16)
1327SBUFF_PARSE_UINT_DEF(uint64_hex, uint64_t, UINT64_MAX, 16, 16)
1328SBUFF_PARSE_UINT_DEF(size_hex, size_t, SIZE_MAX, 22, 16)
1329
1330/** Used to define a number parsing functions for floats
1331 *
1332 * @param[in] _name Function suffix.
1333 * @param[in] _type Output type.
1334 * @param[in] _func Parsing function to use.
1335 * @param[in] _max_char Maximum digits that can be used to represent an integer.
1336 * Can't use stringify because of width modifiers like 'u'
1337 * used in <stdint.h>.
1338 */
1339#define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char) \
1340fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1341{ \
1342 char buff[_max_char + 1] = ""; \
1343 char *end; \
1344 fr_sbuff_t our_in = FR_SBUFF(in); \
1345 size_t len; \
1346 _type res; \
1347 len = fr_sbuff_out_bstrncpy_allowed(&FR_SBUFF_OUT(buff, sizeof(buff)), &our_in, SIZE_MAX, sbuff_char_class_float); \
1348 if (len == sizeof(buff)) { \
1349 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1350 return -1; \
1351 } else if (len == 0) { \
1352 if (err) *err = (fr_sbuff_remaining(in) == 0) ? FR_SBUFF_PARSE_ERROR_INPUT_EMPTY : FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1353 return -1; \
1354 } \
1355 errno = 0; /* this is needed as parsing functions don't reset errno */ \
1356 res = _func(buff, &end); \
1357 if (errno == ERANGE) { \
1358 if (res > 0) { \
1359 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1360 } else { \
1361 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1362 } \
1363 return -1; \
1364 } \
1365 if (no_trailing && (*end != '\0')) { \
1366 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1367 FR_SBUFF_ERROR_RETURN(&our_in); \
1368 } \
1369 *out = res; \
1370 return fr_sbuff_advance(in, end - buff); \
1371}
1372
1373SBUFF_PARSE_FLOAT_DEF(float32, float, strtof, 100)
1374SBUFF_PARSE_FLOAT_DEF(float64, double, strtod, 100)
1375
1376/** Move data from one sbuff to another
1377 *
1378 * @note Do not call this function directly use #fr_sbuff_move
1379 *
1380 * Both in and out will be advanced by len, with len set to the shortest
1381 * value between the user specified value, the number of bytes remaining
1382 * in the input buffer (after extension), and the number of bytes remaining
1383 * in the output buffer (after extension).
1384 *
1385 * @param[in] out sbuff to copy data to.
1386 * @param[in] in sbuff to copy data from.
1387 * @param[in] len Maximum length of string to copy.
1388 * @return The amount of data copied.
1389 */
1391{
1392 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1393 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1394 size_t to_copy = len;
1395 if (to_copy > o_remaining) to_copy = o_remaining;
1396 if (to_copy > i_remaining) to_copy = i_remaining;
1398 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1399}
1400
1401/** Move data from a marker to an sbuff
1402 *
1403 * @note Do not call this function directly use #fr_sbuff_move
1404 *
1405 * @param[in] out sbuff to copy data to.
1406 * @param[in] in marker to copy data from.
1407 * @param[in] len Maximum length of string to copy.
1408 * @return The amount of data copied.
1409 */
1411{
1412 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1413 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1414 size_t to_copy = len;
1415 if (to_copy > o_remaining) to_copy = o_remaining;
1416 if (to_copy > i_remaining) to_copy = i_remaining;
1418 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1419}
1420
1421/** Move data from one marker to another
1422 *
1423 * @note Do not call this function directly use #fr_sbuff_move
1424 *
1425 * @param[in] out marker to copy data to.
1426 * @param[in] in marker to copy data from.
1427 * @param[in] len Maximum length of string to copy.
1428 * @return The amount of data copied.
1429 */
1431{
1432 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1433 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1434 size_t to_copy = len;
1435 if (to_copy > o_remaining) to_copy = o_remaining;
1436 if (to_copy > i_remaining) to_copy = i_remaining;
1438 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1439}
1440
1441/** Move data from an sbuff to a marker
1442 *
1443 * @note Do not call this function directly use #fr_sbuff_move
1444 *
1445 * @param[in] out marker to copy data to.
1446 * @param[in] in sbuff to copy data from.
1447 * @param[in] len Maximum length of string to copy.
1448 * @return The amount of data copied.
1449 */
1451{
1452 size_t o_remaining = fr_sbuff_extend_lowat(NULL, out, len);
1453 size_t i_remaining = fr_sbuff_extend_lowat(NULL, in, len);
1454 size_t to_copy = len;
1455 if (to_copy > o_remaining) to_copy = o_remaining;
1456 if (to_copy > i_remaining) to_copy = i_remaining;
1458 return fr_sbuff_advance(out, fr_sbuff_advance(in, to_copy));
1459}
1460
1461/** Copy bytes into the sbuff up to the first \0
1462 *
1463 * @param[in] sbuff to copy into.
1464 * @param[in] str to copy into buffer.
1465 * @return
1466 * - >= 0 the number of bytes copied into the sbuff.
1467 * - <0 the number of bytes required to complete the copy operation.
1468 */
1469ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
1470{
1471 size_t len;
1472
1473 CHECK_SBUFF_WRITEABLE(sbuff);
1474
1475 len = strlen(str);
1477
1478 safecpy(sbuff->p, sbuff->end, str, str + len);
1479 sbuff->p[len] = '\0';
1480
1481 return fr_sbuff_advance(sbuff, len);
1482}
1483
1484/** Copy bytes into the sbuff up to the first \0
1485 *
1486 * @param[in] sbuff to copy into.
1487 * @param[in] str to copy into buffer.
1488 * @param[in] len number of bytes to copy.
1489 * @return
1490 * - >= 0 the number of bytes copied into the sbuff.
1491 * - <0 the number of bytes required to complete the copy operation.
1492 */
1493ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
1494{
1495 CHECK_SBUFF_WRITEABLE(sbuff);
1496
1498
1499 safecpy(sbuff->p, sbuff->end, str, str + len);
1500 sbuff->p[len] = '\0';
1501
1502 return fr_sbuff_advance(sbuff, len);
1503}
1504
1505/** Copy bytes into the sbuff up to the first \0
1506 *
1507 * @param[in] sbuff to copy into.
1508 * @param[in] str talloced buffer to copy into sbuff.
1509 * @return
1510 * - >= 0 the number of bytes copied into the sbuff.
1511 * - <0 the number of bytes required to complete the copy operation.
1512 */
1514{
1515 size_t len;
1516
1517 CHECK_SBUFF_WRITEABLE(sbuff);
1518
1519 len = talloc_strlen(str);
1520
1522
1523 safecpy(sbuff->p, sbuff->end, str, str + len);
1524 sbuff->p[len] = '\0';
1525
1526 return fr_sbuff_advance(sbuff, len);
1527}
1528
1529/** Free the scratch buffer used for printf
1530 *
1531 */
1532static int _sbuff_scratch_free(void *arg)
1533{
1534 sbuff_scratch_freed = true;
1535 return talloc_free(arg);
1536}
1537
1538static inline CC_HINT(always_inline) int sbuff_scratch_init(TALLOC_CTX **out)
1539{
1540 TALLOC_CTX *scratch;
1541
1542 /*
1543 * Once main has signalled shutdown the TLS slot may be a
1544 * dangling pointer on threads we don't own; skip the scratch
1545 * cache and let callers allocate at top level instead. The
1546 * TLS-local `sbuff_scratch_freed` is left in place for the
1547 * per-thread teardown path on FR-managed threads.
1548 */
1550 *out = NULL;
1551 return 0;
1552 }
1553
1554 scratch = sbuff_scratch;
1555 if (!scratch) {
1556 scratch = talloc_pool(NULL, 4096);
1557 if (unlikely(!scratch)) {
1558 fr_strerror_const("Out of Memory");
1559 return -1;
1560 }
1562 }
1563
1564 *out = scratch;
1565
1566 return 0;
1567}
1568
1569/** Print using a fmt string to an sbuff
1570 *
1571 * @param[in] sbuff to print into.
1572 * @param[in] fmt string.
1573 * @param[in] ap arguments for format string.
1574< * @return
1575 * - >= 0 the number of bytes printed into the sbuff.
1576 * - <0 the number of bytes required to complete the print operation.
1577 */
1578ssize_t fr_sbuff_in_vsprintf(fr_sbuff_t *sbuff, char const *fmt, va_list ap)
1579{
1580 TALLOC_CTX *scratch;
1581 va_list ap_p;
1582 char *tmp;
1583 ssize_t slen;
1584
1585 CHECK_SBUFF_WRITEABLE(sbuff);
1586
1587 if (sbuff_scratch_init(&scratch) < 0) return 0;
1588
1589 va_copy(ap_p, ap);
1590 tmp = fr_vasprintf(scratch, fmt, ap_p);
1591 va_end(ap_p);
1592 if (!tmp) return 0;
1593
1594 slen = fr_sbuff_in_bstrcpy_buffer(sbuff, tmp);
1595 talloc_free(tmp); /* Free the temporary buffer */
1596
1597 return slen;
1598}
1599
1600/** Print using a fmt string to an sbuff
1601 *
1602 * @param[in] sbuff to print into.
1603 * @param[in] fmt string.
1604 * @param[in] ... arguments for format string.
1605 * @return
1606 * - >= 0 the number of bytes printed into the sbuff.
1607 * - <0 the number of bytes required to complete the print operation.
1608 */
1610{
1611 va_list ap;
1612 ssize_t slen;
1613
1614 CHECK_SBUFF_WRITEABLE(sbuff);
1615
1616 va_start(ap, fmt);
1617 slen = fr_sbuff_in_vsprintf(sbuff, fmt, ap);
1618 va_end(ap);
1619
1620 return slen;
1621}
1622
1623/** Print an escaped string to an sbuff
1624 *
1625 * @param[in] sbuff to print into.
1626 * @param[in] in to escape.
1627 * @param[in] inlen of string to escape.
1628 * @param[in] e_rules Escaping rules. Used to escape special characters
1629 * as data is written to the sbuff. May be NULL.
1630 * @return
1631 * - >= 0 the number of bytes printed into the sbuff.
1632 * - <0 the number of bytes required to complete the print operation.
1633 */
1634ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules)
1635{
1636 char const *end = in + inlen;
1637 char const *p = in;
1638 fr_sbuff_t our_sbuff;
1639
1640 /* Significantly quicker if there are no rules */
1641 if (!e_rules || (e_rules->chr == '\0')) return fr_sbuff_in_bstrncpy(sbuff, in, inlen);
1642
1643 CHECK_SBUFF_WRITEABLE(sbuff);
1644
1645 our_sbuff = FR_SBUFF(sbuff);
1646 while (p < end) {
1647 size_t clen;
1648 uint8_t c = (uint8_t)*p;
1649 char sub;
1650
1651 /*
1652 * We don't support escaping UTF8 sequences
1653 * as they're not used anywhere in our
1654 * grammar.
1655 */
1656 if (e_rules->do_utf8 && ((clen = fr_utf8_char((uint8_t const *)p, end - p)) > 1)) {
1657 FR_SBUFF_IN_BSTRNCPY_RETURN(&our_sbuff, p, clen);
1658 p += clen;
1659 continue;
1660 }
1661
1662 /*
1663 * Check if there's a special substitution
1664 * like 0x0a -> \n.
1665 */
1666 sub = e_rules->subs[c];
1667 if (sub != '\0') {
1668 FR_SBUFF_IN_CHAR_RETURN(&our_sbuff, e_rules->chr, sub);
1669 p++;
1670 continue;
1671 }
1672
1673 /*
1674 * Check if the character is in the range
1675 * we escape.
1676 */
1677 if (e_rules->esc[c]) {
1678 /*
1679 * For legacy reasons we prefer
1680 * octal escape sequences.
1681 */
1682 if (e_rules->do_oct) {
1683 FR_SBUFF_IN_SPRINTF_RETURN(&our_sbuff, "%c%03o", e_rules->chr, (uint8_t)*p++);
1684 continue;
1685 } else if (e_rules->do_hex) {
1686 FR_SBUFF_IN_SPRINTF_RETURN(&our_sbuff, "%cx%02x", e_rules->chr, (uint8_t)*p++);
1687 continue;
1688 }
1689 }
1690
1691 FR_SBUFF_IN_CHAR_RETURN(&our_sbuff, *p++);
1692 }
1693
1694 FR_SBUFF_SET_RETURN(sbuff, &our_sbuff);
1695}
1696
1697/** Print an escaped string to an sbuff taking a talloced buffer as input
1698 *
1699 * @param[in] sbuff to print into.
1700 * @param[in] in to escape.
1701 * @param[in] e_rules Escaping rules. Used to escape special characters
1702 * as data is written to the sbuff. May be NULL.
1703 * @return
1704 * - >= 0 the number of bytes printed into the sbuff.
1705 * - <0 the number of bytes required to complete the print operation.
1706 */
1708{
1709 if (unlikely(!in)) return 0;
1710
1711 CHECK_SBUFF_WRITEABLE(sbuff);
1712
1713 return fr_sbuff_in_escape(sbuff, in, talloc_strlen(in), e_rules);
1714}
1715
1716/** Concat an array of strings (NULL terminated), with a string separator
1717 *
1718 * @param[out] out Where to write the resulting string.
1719 * @param[in] array of strings to concat.
1720 * @param[in] sep to insert between elements. May be NULL.
1721 * @return
1722 * - >= 0 on success - length of the string created.
1723 * - <0 on failure. How many bytes we would need.
1724 */
1725fr_slen_t fr_sbuff_in_array(fr_sbuff_t *out, char const * const *array, char const *sep)
1726{
1727 fr_sbuff_t our_out = FR_SBUFF(out);
1728 char const * const * p;
1729 fr_sbuff_escape_rules_t e_rules = {
1730 .name = __FUNCTION__,
1731 .chr = '\\'
1732 };
1733
1734 if (sep) e_rules.subs[(uint8_t)*sep] = *sep;
1735
1737
1738 for (p = array; *p; p++) {
1739 if (*p) FR_SBUFF_RETURN(fr_sbuff_in_escape, &our_out, *p, strlen(*p), &e_rules);
1740
1741 if (sep && p[1]) {
1742 FR_SBUFF_RETURN(fr_sbuff_in_strcpy, &our_out, sep);
1743 }
1744 }
1745
1746 FR_SBUFF_SET_RETURN(out, &our_out);
1747}
1748
1749/** Return true and advance past the end of the needle if needle occurs next in the sbuff
1750 *
1751 * @param[in] sbuff to search in.
1752 * @param[in] needle to search for.
1753 * @param[in] needle_len of needle. If SIZE_MAX strlen is used
1754 * to determine length of the needle.
1755 * @return how many bytes we advanced
1756 */
1757size_t fr_sbuff_adv_past_str(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
1758{
1759 char const *found;
1760
1761 CHECK_SBUFF_INIT(sbuff);
1762
1763 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1764
1765 /*
1766 * If there's insufficient bytes in the
1767 * buffer currently, try to extend it,
1768 * returning if we can't.
1769 */
1770 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) < needle_len) return 0;
1771
1772 found = memmem(sbuff->p, needle_len, needle, needle_len); /* sbuff needle_len and needle needle_len ensures match must be next */
1773 if (!found) return 0;
1774
1775 return fr_sbuff_advance(sbuff, needle_len);
1776}
1777
1778/** Return true and advance past the end of the needle if needle occurs next in the sbuff
1779 *
1780 * This function is similar to fr_sbuff_adv_past_str but is case insensitive.
1781 *
1782 * @param[in] sbuff to search in.
1783 * @param[in] needle to search for.
1784 * @param[in] needle_len of needle. If SIZE_MAX strlen is used
1785 * to determine length of the needle.
1786 * @return how many bytes we advanced
1787 */
1788size_t fr_sbuff_adv_past_strcase(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
1789{
1790 char const *p, *n_p;
1791 char const *end;
1792
1793 CHECK_SBUFF_INIT(sbuff);
1794
1795 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1796
1797 /*
1798 * If there's insufficient bytes in the
1799 * buffer currently, try to extend it,
1800 * returning if we can't.
1801 */
1802 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) < needle_len) return 0;
1803
1804 p = sbuff->p;
1805 end = p + needle_len;
1806
1807 for (p = sbuff->p, n_p = needle; p < end; p++, n_p++) {
1808 if (tolower((uint8_t) *p) != tolower((uint8_t) *n_p)) return 0;
1809 }
1810
1811 return fr_sbuff_advance(sbuff, needle_len);
1812}
1813
1814/** Wind position past characters in the allowed set
1815 *
1816 * @param[in] sbuff sbuff to search in.
1817 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1818 * @param[in] allowed character set.
1819 * @param[in] tt If not NULL, stop if we find a terminal sequence.
1820 * @return how many bytes we advanced.
1821 */
1822size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool
1823 const allowed[static SBUFF_CHAR_CLASS], fr_sbuff_term_t const *tt)
1824{
1825 size_t total = 0;
1826 char const *p;
1827 uint8_t idx[SBUFF_CHAR_CLASS]; /* Fast path index */
1828 size_t needle_len = 0;
1829
1830 CHECK_SBUFF_INIT(sbuff);
1831
1832 if (tt) fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
1833
1834 while (total < len) {
1835 char *end;
1836
1837 if (!fr_sbuff_extend(sbuff)) break;
1838
1839 end = CONSTRAINED_END(sbuff, len, total);
1840 p = sbuff->p;
1841 while ((p < end) && allowed[(uint8_t)*p]) {
1842 if (needle_len == 0) {
1843 p++;
1844 continue;
1845 }
1846
1847 /*
1848 * If this character is allowed, BUT is also listed as a one-character terminal,
1849 * then we still allow it. This decision implements "greedy" parsing.
1850 */
1851 if (fr_sbuff_terminal_search(sbuff, p, idx, tt, 1)) {
1852 p++;
1853 continue;
1854 }
1855
1856 /*
1857 * Otherwise if the next *set* of characters) is not in the terminals, then
1858 * allow the current character.
1859 */
1860 if (!fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) {
1861 p++;
1862 continue;
1863 }
1864
1865 /*
1866 * The character is allowed, and is NOT listed as a terminal character by itself.
1867 * However, it is part of a multi-character terminal sequence. We therefore
1868 * stop.
1869 *
1870 * This decision allows us to parse things like "Framed-User", where we might
1871 * normally stop at the "-". However, we will still stop at "Framed-=User", as
1872 * "-=" may be a terminal sequence.
1873 *
1874 * There is no perfect solution here, other than to fix the input grammar so that
1875 * it has no ambiguity. Since we can't do that, we choose to err on the side of
1876 * allowing the existing grammar, where it makes sense
1877 */
1878 break;
1879 }
1880
1881 total += fr_sbuff_set(sbuff, p);
1882 if (p != end) break; /* stopped early, break */
1883 }
1884
1885 return total;
1886}
1887
1888/** Wind position until we hit a character in the terminal set
1889 *
1890 * @param[in] sbuff sbuff to search in.
1891 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1892 * @param[in] tt Token terminals in the encompassing grammar.
1893 * @param[in] escape_chr If not '\0', ignore characters in the tt set when
1894 * prefixed with this escape character.
1895 * @return how many bytes we advanced.
1896 */
1897size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
1898{
1899 size_t total = 0;
1900 char const *p;
1901 bool do_escape = false; /* Track state across extensions */
1902
1903 uint8_t idx[SBUFF_CHAR_CLASS]; /* Fast path index */
1904 size_t needle_len = 1;
1905
1906 CHECK_SBUFF_INIT(sbuff);
1907
1908 /*
1909 * Initialise the fastpath index and
1910 * figure out the longest needle.
1911 */
1912 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
1913
1914 while (total < len) {
1915 char *end;
1916
1917 if (fr_sbuff_extend_lowat(NULL, sbuff, needle_len) == 0) break;
1918
1919 end = CONSTRAINED_END(sbuff, len, total);
1920 p = sbuff->p;
1921
1922 if (escape_chr == '\0') {
1923 while ((p < end) && !fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) p++;
1924 } else {
1925 while (p < end) {
1926 if (do_escape) {
1927 do_escape = false;
1928 } else if (*p == escape_chr) {
1929 do_escape = true;
1930 } else if (fr_sbuff_terminal_search(sbuff, p, idx, tt, needle_len)) {
1931 break;
1932 }
1933 p++;
1934 }
1935 }
1936
1937 total += fr_sbuff_set(sbuff, p);
1938 if (p != end) break; /* stopped early, break */
1939 }
1940
1941 return total;
1942}
1943
1944/** Wind position to first instance of specified multibyte utf8 char
1945 *
1946 * Only use this function if the search char could be multibyte,
1947 * as there's a large performance penalty.
1948 *
1949 * @param[in,out] sbuff to search in.
1950 * @param[in] len the maximum number of characters to search in sbuff.
1951 * @param[in] chr to search for.
1952 * @return
1953 * - NULL, no instances found.
1954 * - The position of the first character.
1955 */
1956char *fr_sbuff_adv_to_chr_utf8(fr_sbuff_t *sbuff, size_t len, char const *chr)
1957{
1958 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
1959 size_t total = 0;
1960 size_t clen = strlen(chr);
1961
1962 CHECK_SBUFF_INIT(sbuff);
1963
1964 /*
1965 * Needle bigger than haystack
1966 */
1967 if (len < clen) return NULL;
1968
1969 while (total <= (len - clen)) {
1970 char const *found;
1971 char *end;
1972
1973 /*
1974 * Ensure we have enough chars to match
1975 * the needle.
1976 */
1977 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, clen) < clen) break;
1978
1979 end = CONSTRAINED_END(&our_sbuff, len, total);
1980
1981 found = fr_utf8_strchr(NULL, our_sbuff.p, end - our_sbuff.p, chr);
1982 if (found) {
1983 (void)fr_sbuff_set(sbuff, found);
1984 return sbuff->p;
1985 }
1986 total += fr_sbuff_set(&our_sbuff, (end - clen) + 1);
1987 }
1988
1989 return NULL;
1990}
1991
1992/** Wind position to first instance of specified char
1993 *
1994 * @param[in,out] sbuff to search in.
1995 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
1996 * @param[in] c to search for.
1997 * @return
1998 * - NULL, no instances found.
1999 * - The position of the first character.
2000 */
2001char *fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
2002{
2003 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
2004 size_t total = 0;
2005
2006 CHECK_SBUFF_INIT(sbuff);
2007
2008 while (total < len) {
2009 char const *found;
2010 char *end;
2011
2012 if (!fr_sbuff_extend(&our_sbuff)) break;
2013
2014 end = CONSTRAINED_END(&our_sbuff, len, total);
2015 found = memchr(our_sbuff.p, c, end - our_sbuff.p);
2016 if (found) {
2017 (void)fr_sbuff_set(sbuff, found);
2018 return sbuff->p;
2019 }
2020
2021 total += fr_sbuff_set(&our_sbuff, end);
2022 }
2023
2024 return NULL;
2025}
2026
2027/** Wind position to the first instance of the specified needle
2028 *
2029 * @param[in,out] sbuff sbuff to search in.
2030 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
2031 * @param[in] needle to search for.
2032 * @param[in] needle_len Length of the needle. SIZE_MAX to used strlen.
2033 * @return
2034 * - NULL, no instances found.
2035 * - The position of the first character.
2036 */
2037char *fr_sbuff_adv_to_str(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
2038{
2039 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
2040 size_t total = 0;
2041
2042 CHECK_SBUFF_INIT(sbuff);
2043
2044 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2045 if (!needle_len) return NULL;
2046
2047 /*
2048 * Needle bigger than haystack
2049 */
2050 if (len < needle_len) return NULL;
2051
2052 while (total <= (len - needle_len)) {
2053 char const *found;
2054 char *end;
2055
2056 /*
2057 * If the needle is longer than
2058 * the remaining buffer, return.
2059 */
2060 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, needle_len) < needle_len) break;
2061
2062 end = CONSTRAINED_END(&our_sbuff, len, total);
2063 found = memmem(our_sbuff.p, end - our_sbuff.p, needle, needle_len);
2064 if (found) {
2065 (void)fr_sbuff_set(sbuff, found);
2066 return sbuff->p;
2067 }
2068
2069 /*
2070 * Partial needle may be in
2071 * the end of the buffer so
2072 * don't advance too far.
2073 */
2074 total += fr_sbuff_set(&our_sbuff, (end - needle_len) + 1);
2075 }
2076
2077 return NULL;
2078}
2079
2080/** Wind position to the first instance of the specified needle
2081 *
2082 * @param[in,out] sbuff sbuff to search in.
2083 * @param[in] len Maximum amount to advance by. Unconstrained if SIZE_MAX.
2084 * @param[in] needle to search for.
2085 * @param[in] needle_len Length of the needle. SIZE_MAX to used strlen.
2086 * @return
2087 * - NULL, no instances found.
2088 * - The position of the first character.
2089 */
2090char *fr_sbuff_adv_to_strcase(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
2091{
2092 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
2093 size_t total = 0;
2094
2095 CHECK_SBUFF_INIT(sbuff);
2096
2097 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2098 if (!needle_len) return NULL;
2099
2100 /*
2101 * Needle bigger than haystack
2102 */
2103 if (len < needle_len) return NULL;
2104
2105 while (total <= (len - needle_len)) {
2106 char *p, *end;
2107 char const *n_p;
2108
2109 if (fr_sbuff_extend_lowat(NULL, &our_sbuff, needle_len) < needle_len) break;
2110
2111 for (p = our_sbuff.p, n_p = needle, end = our_sbuff.p + needle_len;
2112 (p < end) && (tolower((uint8_t) *p) == tolower((uint8_t) *n_p));
2113 p++, n_p++);
2114 if (p == end) {
2115 (void)fr_sbuff_set(sbuff, our_sbuff.p);
2116 return sbuff->p;
2117 }
2118
2119 total += fr_sbuff_advance(&our_sbuff, 1);
2120 }
2121
2122 return NULL;
2123}
2124
2125/** Return true if the current char matches, and if it does, advance
2126 *
2127 * @param[in] sbuff to search for char in.
2128 * @param[in] c char to search for.
2129 * @return
2130 * - true and advance if the next character matches.
2131 * - false and don't advance if the next character doesn't match.
2132 */
2134{
2135 CHECK_SBUFF_INIT(sbuff);
2136
2137 if (!fr_sbuff_extend(sbuff)) return false;
2138
2139 if (*sbuff->p != c) return false;
2140
2141 fr_sbuff_advance(sbuff, 1);
2142
2143 return true;
2144}
2145
2146/** Return true and advance if the next char does not match
2147 *
2148 * @param[in] sbuff to search for char in.
2149 * @param[in] c char to search for.
2150 * @return
2151 * - true and advance unless the character matches.
2152 * - false and don't advance if the next character matches.
2153 */
2155{
2156 CHECK_SBUFF_INIT(sbuff);
2157
2158 if (!fr_sbuff_extend(sbuff)) return false;
2159
2160 if (*sbuff->p == c) return false;
2161
2162 fr_sbuff_advance(sbuff, 1);
2163
2164 return true;
2165}
2166
2167/** Trim trailing characters from a string we're composing
2168 *
2169 * @param[in] sbuff to trim trailing characters from.
2170 * @param[in] to_trim Charset to trim.
2171 * @return how many chars we removed.
2172 */
2173size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static SBUFF_CHAR_CLASS])
2174{
2175 char *p = sbuff->p - 1;
2176 ssize_t slen;
2177
2178 while ((p >= sbuff->start) && to_trim[(uint8_t)*p]) p--;
2179
2180 slen = fr_sbuff_set(sbuff, p + 1);
2181 if (slen != 0) fr_sbuff_terminate(sbuff);
2182
2183 return slen;
2184}
2185
2186/** Efficient terminal string search
2187 *
2188 * Caller should ensure that a buffer extension of needle_len bytes has been requested
2189 * before calling this function.
2190 *
2191 * @param[in] in Sbuff to search in.
2192 * @param[in] tt Token terminals in the encompassing grammar.
2193 * @return
2194 * - true if found.
2195 * - false if not.
2196 */
2198{
2199 uint8_t idx[SBUFF_CHAR_CLASS]; /* Fast path index */
2200 size_t needle_len = 1;
2201
2202 /*
2203 * No terminal, check for EOF.
2204 */
2205 if (!tt) {
2206 fr_sbuff_extend_status_t status = 0;
2207
2208 if ((fr_sbuff_extend_lowat(&status, in, 1) == 0) &&
2209 (status & FR_SBUFF_FLAG_EXTEND_ERROR) == 0) {
2210 return true;
2211 }
2212
2213 return false;
2214 }
2215
2216 /*
2217 * Initialise the fastpath index and
2218 * figure out the longest needle.
2219 */
2220 fr_sbuff_terminal_idx_init(&needle_len, idx, tt);
2221
2222 fr_sbuff_extend_lowat(NULL, in, needle_len);
2223
2224 return fr_sbuff_terminal_search(in, in->p, idx, tt, needle_len);
2225}
2226
2227/** Print a char in a friendly format
2228 *
2229 */
2230static char const *sbuff_print_char(char c)
2231{
2232 static bool const unprintables[SBUFF_CHAR_CLASS] = {
2235 };
2236
2237 static _Thread_local char str[10][5];
2238 static _Thread_local size_t i = 0;
2239
2240 switch (c) {
2241 case '\a':
2242 return "\a";
2243
2244 case '\b':
2245 return "\b";
2246
2247 case '\n':
2248 return "\n";
2249
2250 case '\r':
2251 return "\r";
2252
2253 case '\t':
2254 return "\t";
2255
2256 case '\f':
2257 return "\f";
2258
2259 case '\v':
2260 return "\v";
2261
2262 default:
2263 if (i >= NUM_ELEMENTS(str)) i = 0;
2264
2265 if (unprintables[(uint8_t)c]) {
2266 snprintf(str[i], sizeof(str[i]), "\\x%02x", (uint8_t) c);
2267 return str[i++];
2268 }
2269
2270 str[i][0] = c;
2271 str[i][1] = '\0';
2272 return str[i++];
2273 }
2274}
2275
2277{
2278 int i;
2279
2280 fprintf(fp, "Escape rules %s (%p)\n", escapes->name, escapes);
2281 fprintf(fp, "chr : %c\n", escapes->chr ? escapes->chr : ' ');
2282 fprintf(fp, "do_hex : %s\n", escapes->do_hex ? "yes" : "no");
2283 fprintf(fp, "do_oct : %s\n", escapes->do_oct ? "yes" : "no");
2284
2285 fprintf(fp, "substitutions:\n");
2286 for (i = 0; i < SBUFF_CHAR_CLASS; i++) {
2287 if (escapes->subs[i]) FR_FAULT_LOG("\t%s -> %s\n",
2288 sbuff_print_char((char)i),
2289 sbuff_print_char((char)escapes->subs[i]));
2290 }
2291 fprintf(fp, "skips:\n");
2292 for (i = 0; i < SBUFF_CHAR_CLASS; i++) {
2293 if (escapes->skip[i]) fprintf(fp, "\t%s\n", sbuff_print_char((char)i));
2294 }
2295}
2296
2298{
2299 size_t i;
2300
2301 fprintf(fp, "Terminal count %zu\n", tt->len);
2302
2303 for (i = 0; i < tt->len; i++) fprintf(fp, "\t\"%s\" (%zu)\n", tt->elem[i].str, tt->elem[i].len);
2304}
2305
2306void fr_sbuff_parse_rules_debug(FILE *fp, fr_sbuff_parse_rules_t const *p_rules)
2307{
2308 fprintf(fp, "Parse rules %p\n", p_rules);
2309
2310 FR_FAULT_LOG("Escapes - ");
2311 if (p_rules->escapes) {
2312 fr_sbuff_unescape_debug(fp, p_rules->escapes);
2313 } else {
2314 fprintf(fp, "<none>\n");
2315 }
2316
2317 FR_FAULT_LOG("Terminals - ");
2318 if (p_rules->terminals) {
2319 fr_sbuff_terminal_debug(fp, p_rules->terminals);
2320 } else {
2321 fprintf(fp, "<none>\n");
2322 }
2323}
2324
2325/** Concat an array of strings (not NULL terminated), with a string separator
2326 *
2327 * @param[out] out Where to write the resulting string.
2328 * @param[in] array of strings to concat.
2329 * @param[in] sep to insert between elements. May be NULL.
2330 * @return
2331 * - >= 0 on success - length of the string created.
2332 * - <0 on failure. How many bytes we would need.
2333 */
2334fr_slen_t fr_sbuff_array_concat(fr_sbuff_t *out, char const * const *array, char const *sep)
2335{
2336 fr_sbuff_t our_out = FR_SBUFF(out);
2337 size_t len = talloc_array_length(array);
2338 char const * const * p;
2339 char const * const * end;
2340 fr_sbuff_escape_rules_t e_rules = {
2341 .name = __FUNCTION__,
2342 .chr = '\\'
2343 };
2344
2345 if (sep) e_rules.subs[(uint8_t)*sep] = *sep;
2346
2347 for (p = array, end = array + len;
2348 (p < end);
2349 p++) {
2350 if (*p) FR_SBUFF_RETURN(fr_sbuff_in_escape, &our_out, *p, strlen(*p), &e_rules);
2351
2352 if (sep && ((p + 1) < end)) {
2353 FR_SBUFF_RETURN(fr_sbuff_in_strcpy, &our_out, sep);
2354 }
2355 }
2356
2357 FR_SBUFF_SET_RETURN(out, &our_out);
2358}
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:445
#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 UNUSED
Definition build.h:336
#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_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:1822
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:1469
#define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char)
Used to define a number parsing functions for floats.
Definition sbuff.c:1339
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:1634
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:2173
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:1956
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:1255
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:950
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:1538
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:2037
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:1410
bool const sbuff_char_class_uint[SBUFF_CHAR_CLASS]
Definition sbuff.c:64
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:1707
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:2090
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:2276
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:771
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:1757
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:877
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:2001
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:831
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:2297
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:2230
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, UNUSED size_t needle_len)
Efficient terminal string search.
Definition sbuff.c:554
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2197
fr_slen_t fr_sbuff_out_bool(bool *out, fr_sbuff_t *in)
See if the string contains a truth value.
Definition sbuff.c:1124
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:1430
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:1450
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:1493
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:1788
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:2154
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:1897
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:2334
#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:632
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:1513
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:733
#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:655
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:1390
#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:1188
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:1609
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:2133
void fr_sbuff_parse_rules_debug(FILE *fp, fr_sbuff_parse_rules_t const *p_rules)
Definition sbuff.c:2306
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:1725
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:1578
static int _sbuff_scratch_free(void *arg)
Free the scratch buffer used for printf.
Definition sbuff.c:1532
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)
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(...)
#define fr_sbuff_uint8(_sbuff_or_marker, _eob)
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:37
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