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