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