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