23 RCSID(
"$Id: 06678c89fec0cf50a843349ee30141d12024bc23 $")
25 #include <freeradius-devel/util/misc.h>
26 #include <freeradius-devel/util/syserror.h>
27 #include <freeradius-devel/util/atexit.h>
40 static_assert(
sizeof(
long long) >=
sizeof(int64_t),
"long long must be as wide or wider than an int64_t");
41 static_assert(
sizeof(
unsigned long long) >=
sizeof(uint64_t),
"long long must be as wide or wider than an uint64_t");
54 #if defined(STATIC_ANALYZER) || !defined(NDEBUG)
55 # define CHECK_SBUFF_INIT(_sbuff) do { if (!(_sbuff)->extend && (unlikely(!(_sbuff)->buff) || unlikely(!(_sbuff)->start) || unlikely(!(_sbuff)->end) || unlikely(!(_sbuff)->p))) return 0; } while (0)
57 # define CHECK_SBUFF_INIT(_sbuff)
67 [
'+'] =
true, [
'-'] =
true
72 [
'-'] =
true, [
'+'] =
true, [
'e'] =
true, [
'E'] =
true, [
'.'] =
true,
98 [
'-'] =
true, [
'_'] =
true,
101 [
'\t'] =
true, [
'\n'] =
true, [
'\r'] =
true, [
'\f'] =
true, [
'\v'] =
true, [
' '] =
true,
105 [
'\n'] =
true, [
'\r'] =
true
109 [
'\t'] =
true, [
' '] =
true,
124 char const *i_start,
char const *i_end)
127 size_t i_len = i_end - i_start;
129 if (
unlikely((o_end < o_start) || (i_end < i_start)))
return 0;
131 diff = (o_end - o_start) - (i_len);
132 if (diff < 0)
return diff;
134 if ((i_start > o_end) || (i_end < o_start)) {
135 memcpy(o_start, i_start, i_len);
137 memmove(o_start, i_start, i_len);
143 static inline CC_HINT(always_inline)
size_t min(
size_t x,
size_t y)
145 return x <
y ? x :
y;
161 old_buff = sbuff->buff;
169 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
172 sbuff_i->buff = new_buff;
173 sbuff_i->start = new_buff +
min(new_len, sbuff_i->start - old_buff);
174 sbuff_i->end = sbuff_i->buff + new_len;
175 *(sbuff_i->end) =
'\0';
177 sbuff_i->
p = new_buff +
min(new_len, sbuff_i->
p - old_buff);
179 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);
199 size_t max_shift = shift;
200 bool reterminate =
false;
213 reterminate = (sbuff->
p < sbuff->end) && (*sbuff->
p ==
'\0') && !sbuff->is_const;
221 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
224 max_shift =
min(max_shift, sbuff_i->
p -
buff);
225 if (!max_shift)
return 0;
227 for (m_i = sbuff_i->m; m_i; m_i = m_i->
next) {
228 max_shift =
min(max_shift, m_i->p -
buff);
229 if (!max_shift)
return 0;
240 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
242 char *start = sbuff_i->start;
244 sbuff_i->start -=
min(max_shift, sbuff_i->start -
buff);
245 sbuff_i->
p -= max_shift;
246 sbuff_i->end -= max_shift;
247 sbuff_i->shifted += (max_shift - (start - sbuff_i->start));
248 for (m_i = sbuff_i->m; m_i; m_i = m_i->
next) m_i->p -= max_shift;
255 if ((
buff + max_shift) < end) memmove(
buff,
buff + max_shift, end - (
buff + max_shift));
257 if (reterminate) *sbuff->
p =
'\0';
268 size_t read, available, total_read, shift;
274 if (fctx->
eof)
return 0;
276 if (extension == SIZE_MAX) extension = 0;
278 total_read = fctx->
shifted + (sbuff->end - sbuff->buff);
279 if (total_read >= fctx->
max) {
309 available = fctx->
buff_end - sbuff->end;
310 if (available > (fctx->
max - total_read)) available = fctx->
max - total_read;
311 if (available < extension) {
316 read = fread(sbuff->end, 1, available, fctx->
file);
317 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
318 sbuff_i->end += read;
323 if (read < available) {
324 if (!feof(fctx->
file)) {
347 size_t clen, nlen, elen = extension;
352 clen = sbuff->buff ? talloc_array_length(sbuff->buff) : 0;
360 if ((clen + elen) < tctx->
init) {
361 elen = (tctx->
init - clen) + 1;
366 }
else if (elen < clen) {
374 if (tctx->
max && ((clen + elen) > tctx->
max)) {
375 elen = tctx->
max - clen;
378 "%zu bytes, max is %zu bytes",
379 extension, clen + extension, tctx->
max);
386 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, nlen);
409 size_t clen = 0, nlen = 1;
415 if (sbuff->buff) clen = talloc_array_length(sbuff->buff);
417 if (len != SIZE_MAX) {
419 }
else if (sbuff->buff){
420 nlen += (sbuff->
p - sbuff->start);
424 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, nlen);
448 fr_sbuff_set_to_start(sbuff);
454 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, tctx->
init);
457 talloc_array_length(sbuff->buff), tctx->
init);
460 sbuff->buff = new_buff;
473 #define FILL_OR_GOTO_DONE(_out, _in, _len) if (fr_sbuff_move(_out, _in, _len) < (size_t)(_len)) goto done
482 #define CONSTRAINED_END(_sbuff, _max, _used) \
483 (((_max) - (_used)) > fr_sbuff_remaining(_sbuff) ? (_sbuff)->end : (_sbuff)->p + ((_max) - (_used)))
497 size_t i, len, max = 0;
503 for (i = 0; i < term->
len; i++) {
505 if (len > max) max = len;
510 if (i > 0) *needle_len = max;
539 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
541 if (!term)
return false;
545 if (!term_idx)
return false;
551 if (remaining == 0) {
552 if (status & FR_SBUFF_EXTEND_ERROR)
return false;
553 return (idx[
'\0'] != 0);
558 while (start <= end) {
566 ret = strncmp(p, elem, tlen < (
size_t)remaining ? tlen : (
size_t)remaining);
571 if (remaining >= tlen)
return true;
586 mid = start + ((end - start) / 2);
611 for (i = 0; i < len; i++)
FR_FAULT_LOG(
"\t\"%s\" (%zu)", elem[i] ? elem[i]->str :
"NULL", elem[i] ? elem[i]->len : 0);
633 #if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
646 while ((i < a->len) && (j < b->len)) {
652 tmp[num++] = &a->
elem[i++];
654 }
else if (cmp < 0) {
655 tmp[num++] = &a->
elem[i++];
657 }
else if (cmp > 0) {
658 tmp[num++] = &b->
elem[j++];
667 while (i < a->len) tmp[num++] = &a->
elem[i++];
668 while (j < b->len) tmp[num++] = &b->
elem[j++];
680 for (i = 0; i < num; i++)
out->elem[i] = *tmp[i];
682 #
if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
715 if (chunk_len > remaining) chunk_len = remaining;
746 fr_sbuff_marker(&m,
out);
754 fr_sbuff_marker_release(&m);
759 if (chunk_len > remaining) chunk_len = remaining;
769 fr_sbuff_marker_release(&m);
770 return -(remaining - (chunk_len + copied));
794 bool const allowed[
static UINT8_MAX + 1])
809 while ((p < end) && allowed[(
uint8_t)*p]) p++;
844 bool do_escape =
false;
847 size_t needle_len = 1;
848 char escape_chr = u_rules ? u_rules->
chr :
'\0';
869 if (escape_chr ==
'\0') {
875 }
else if (*p == escape_chr) {
917 bool do_escape =
false;
923 size_t needle_len = 1;
925 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
940 fr_sbuff_marker(&c_s, &our_in);
941 fr_sbuff_marker(&end, &our_in);
942 fr_sbuff_marker_update_end(&end, len);
944 fr_sbuff_marker(&o_s,
out);
969 fr_sbuff_marker(&m, &our_in);
972 if (fr_sbuff_out_uint8_hex(NULL, &escape, &our_in,
false) != 2) {
974 fr_sbuff_marker_release(&m);
980 fr_sbuff_marker_release(&m);
983 fr_sbuff_marker_release(&m);
995 fr_sbuff_marker(&m, &our_in);
997 if (fr_sbuff_out_uint8_oct(NULL, &escape, &our_in,
false) != 3) {
999 fr_sbuff_marker_release(&m);
1005 fr_sbuff_marker_release(&m);
1008 fr_sbuff_marker_release(&m);
1022 if (u_rules->
subs[c] ==
'\0') {
1023 if (u_rules->
skip[c] ==
true)
goto next;
1076 return fr_sbuff_marker_release_behind(&o_s);
1091 static bool const bool_prefix[
UINT8_MAX + 1] = {
1092 [
't'] =
true, [
'T'] =
true,
1093 [
'f'] =
true, [
'F'] =
true,
1094 [
'y'] =
true, [
'Y'] =
true,
1095 [
'n'] =
true, [
'N'] =
true,
1098 if (fr_sbuff_is_in_charset(&our_in, bool_prefix)) {
1135 fr_strerror_const(
"Not a valid boolean value. Accepted values are 'yes', 'no', 'true', 'false'");
1151 #define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base) \
1152 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1154 char buff[_max_char + 1]; \
1155 char *end, *a_end; \
1159 fr_sbuff_t our_in = FR_SBUFF(in); \
1161 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1163 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1167 num = strtoll(buff, &end, _base); \
1168 cast_num = (_type)(num); \
1169 if (end == buff) { \
1170 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1173 if (num > cast_num) { \
1175 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1176 *out = (_type)(_max); \
1179 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == LLONG_MAX))) goto overflow; \
1180 if (num < cast_num) { \
1182 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1183 *out = (_type)(_min); \
1186 if ((errno == ERANGE) && (num == LLONG_MIN)) goto underflow; \
1187 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1188 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1189 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1190 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1191 *out = (_type)(_max); \
1192 FR_SBUFF_ERROR_RETURN(&our_in); \
1196 if (err) *err = FR_SBUFF_PARSE_OK; \
1199 return fr_sbuff_advance(in, end - buff); \
1218 #define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base) \
1219 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1221 char buff[_max_char + 1]; \
1222 char *end, *a_end; \
1224 unsigned long long num; \
1226 fr_sbuff_t our_in = FR_SBUFF(in); \
1228 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1230 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1233 if (buff[0] == '-') { \
1234 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1238 num = strtoull(buff, &end, _base); \
1239 cast_num = (_type)(num); \
1240 if (end == buff) { \
1241 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1244 if (num > cast_num) { \
1246 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1247 *out = (_type)(_max); \
1250 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == ULLONG_MAX))) goto overflow; \
1251 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1252 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1253 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1254 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1255 *out = (_type)(_max); \
1256 FR_SBUFF_ERROR_RETURN(&our_in); \
1258 if (err) *err = FR_SBUFF_PARSE_OK; \
1261 if (err) *err = FR_SBUFF_PARSE_OK; \
1264 return fr_sbuff_advance(in, end - buff); \
1302 #define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char) \
1303 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1305 char buff[_max_char + 1] = ""; \
1307 fr_sbuff_t our_in = FR_SBUFF(in); \
1310 len = fr_sbuff_out_bstrncpy_allowed(&FR_SBUFF_OUT(buff, sizeof(buff)), &our_in, SIZE_MAX, sbuff_char_class_float); \
1311 if (len == sizeof(buff)) { \
1312 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1314 } else if (len == 0) { \
1315 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1319 res = _func(buff, &end); \
1320 if (errno == ERANGE) { \
1322 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1324 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1328 if (no_trailing && (*end != '\0')) { \
1329 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1330 FR_SBUFF_ERROR_RETURN(&our_in); \
1333 return fr_sbuff_advance(in, end - buff); \
1357 size_t to_copy = len;
1358 if (to_copy > o_remaining) to_copy = o_remaining;
1359 if (to_copy > i_remaining) to_copy = i_remaining;
1377 size_t to_copy = len;
1378 if (to_copy > o_remaining) to_copy = o_remaining;
1379 if (to_copy > i_remaining) to_copy = i_remaining;
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;
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;
1438 if (
unlikely(sbuff->is_const))
return 0;
1443 safecpy(sbuff->
p, sbuff->end, str, str + len);
1444 sbuff->
p[len] =
'\0';
1462 if (
unlikely(sbuff->is_const))
return 0;
1466 safecpy(sbuff->
p, sbuff->end, str, str + len);
1467 sbuff->
p[len] =
'\0';
1486 if (
unlikely(sbuff->is_const))
return 0;
1488 len = talloc_array_length(str) - 1;
1492 safecpy(sbuff->
p, sbuff->end, str, str + len);
1493 sbuff->
p[len] =
'\0';
1509 TALLOC_CTX *scratch;
1518 scratch = talloc_pool(NULL, 4096);
1542 TALLOC_CTX *scratch;
1549 if (
unlikely(sbuff->is_const))
return 0;
1578 if (
unlikely(sbuff->is_const))
return 0;
1609 if (
unlikely(sbuff->is_const))
return 0;
1632 sub = e_rules->subs[c];
1643 if (e_rules->esc[c]) {
1648 if (e_rules->do_oct) {
1651 }
else if (e_rules->do_hex) {
1677 if (
unlikely(sbuff->is_const))
return 0;
1696 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1705 found = memmem(sbuff->
p, needle_len, needle, needle_len);
1706 if (!found)
return 0;
1723 char const *p, *n_p;
1728 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1738 end = p + needle_len;
1740 for (p = sbuff->
p, n_p = needle; p < end; p++, n_p++) {
1761 size_t needle_len = 0;
1767 while (total < len) {
1774 while ((p < end) && allowed[(
uint8_t)*p]) {
1775 if (needle_len == 0) {
1815 if (p != end)
break;
1834 bool do_escape =
false;
1837 size_t needle_len = 1;
1847 while (total < len) {
1855 if (escape_chr ==
'\0') {
1861 }
else if (*p == escape_chr) {
1871 if (p != end)
break;
1893 size_t clen = strlen(chr);
1900 if (len < clen)
return NULL;
1902 while (total <= (len - clen)) {
1941 while (total < len) {
1948 found = memchr(our_sbuff.
p, c, end - our_sbuff.
p);
1977 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1978 if (!needle_len)
return 0;
1983 if (len < needle_len)
return NULL;
1985 while (total <= (len - needle_len)) {
1996 found = memmem(our_sbuff.
p, end - our_sbuff.
p, needle, needle_len);
2007 total +=
fr_sbuff_set(&our_sbuff, (end - needle_len) + 1);
2030 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2031 if (!needle_len)
return 0;
2036 if (len < needle_len)
return NULL;
2038 while (total <= (len - needle_len)) {
2044 for (p = our_sbuff.
p, n_p = needle, end = our_sbuff.
p + needle_len;
2072 if (*sbuff->
p != c)
return false;
2093 if (*sbuff->
p == c)
return false;
2108 char *p = sbuff->
p - 1;
2111 while ((p >= sbuff->start) && to_trim[(
uint8_t)*p]) p--;
2114 if (slen != 0) fr_sbuff_terminate(sbuff);
2133 size_t needle_len = 1;
2139 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
2142 (status & FR_SBUFF_EXTEND_ERROR) == 0) {
2163 static bool const unprintables[
UINT8_MAX + 1] = {
2168 static _Thread_local
char str[10][5];
2169 static _Thread_local
char **p;
2194 if (!p || (p++ >= ((
char **)str + (
NUM_PTR_ELEMENTS(str) - 1)))) p = (
char **)str;
2196 if (unprintables[(
uint8_t)c]) {
2197 snprintf(*p,
sizeof(*str),
"\\x%x", c);
2241 if (p_rules->escapes) {
2247 if (p_rules->terminals) {
static int const char * fmt
#define fr_atexit_thread_local(_name, _free, _uctx)
#define MEMCMP_RETURN(_a, _b, _field, _len_field)
Return if the contents of the specified field is not identical between the specified structures.
#define L(_str)
Helper for initialising arrays of string literals.
#define NUM_PTR_ELEMENTS(_t)
#define static_assert
For systems with an old version libc, define static_assert.
#define FR_FAULT_LOG(_fmt,...)
static const bool escapes[UINT8_MAX+1]
@ 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.
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
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.
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.
ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
#define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char)
Used to define a number parsing functions for floats.
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
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.
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.
bool const sbuff_char_class_float[UINT8_MAX+1]
static _Thread_local char * sbuff_scratch
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.
#define FILL_OR_GOTO_DONE(_out, _in, _len)
Fill as much of the output buffer we can and break on partial copy.
#define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base)
Used to define a number parsing functions for signed integers.
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.
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.
size_t fr_sbuff_extend_talloc(fr_sbuff_t *sbuff, size_t extension)
Reallocate the current buffer.
static bool fr_sbuff_terminal_search(fr_sbuff_t *in, char const *p, uint8_t idx[static UINT8_MAX+1], fr_sbuff_term_t const *term, size_t needle_len)
Efficient terminal string search.
#define CHECK_SBUFF_INIT(_sbuff)
void fr_sbuff_terminal_debug(fr_sbuff_term_t const *tt)
char * fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
Wind position to first instance of specified char.
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.
static int sbuff_scratch_init(TALLOC_CTX **out)
bool const sbuff_char_line_endings[UINT8_MAX+1]
bool const sbuff_char_class_hex[UINT8_MAX+1]
size_t fr_sbuff_extend_file(fr_sbuff_t *sbuff, size_t extension)
Refresh the buffer with more data from the file.
bool const sbuff_char_class_uint[UINT8_MAX+1]
void fr_sbuff_unescape_debug(fr_sbuff_unescape_rules_t const *escapes)
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.
size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift)
Shift the contents of the sbuff, returning the number of bytes we managed to shift.
size_t sbuff_parse_error_table_len
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.
static size_t min(size_t x, size_t y)
void fr_sbuff_parse_rules_debug(fr_sbuff_parse_rules_t const *p_rules)
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.
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.
bool const sbuff_char_class_zero[UINT8_MAX+1]
size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static UINT8_MAX+1])
Trim trailing characters from a string we're composing.
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.
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.
bool const sbuff_char_class_hostname[UINT8_MAX+1]
int fr_sbuff_reset_talloc(fr_sbuff_t *sbuff)
Reset a talloced buffer to its initial length, clearing any data stored.
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
fr_slen_t fr_sbuff_out_bool(bool *out, fr_sbuff_t *in)
See if the string contains a truth value.
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.
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.
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.
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.
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.
bool fr_sbuff_next_unless_char(fr_sbuff_t *sbuff, char c)
Return true and advance if the next char does not match.
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.
#define CONSTRAINED_END(_sbuff, _max, _used)
Constrain end pointer to prevent advancing more than the amount the caller specified.
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.
static char const * sbuff_print_char(char c)
Print a char in a friendly format.
static void fr_sbuff_terminal_idx_init(size_t *needle_len, uint8_t idx[static UINT8_MAX+1], fr_sbuff_term_t const *term)
Populate a terminal index.
bool const sbuff_char_whitespace[UINT8_MAX+1]
ssize_t fr_sbuff_in_bstrcpy_buffer(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
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.
bool const sbuff_char_blank[UINT8_MAX+1]
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.
#define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base)
Used to define a number parsing functions for signed integers.
bool const sbuff_char_class_int[UINT8_MAX+1]
fr_table_num_ordered_t const sbuff_parse_error_table[]
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
bool const sbuff_char_word[UINT8_MAX+1]
bool const sbuff_char_alpha_num[UINT8_MAX+1]
static _Thread_local bool sbuff_scratch_freed
When true, prevent use of the scratch space.
size_t fr_sbuff_out_bstrncpy_allowed(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, bool const allowed[static UINT8_MAX+1])
Copy as many allowed characters as possible from a sbuff to a sbuff.
ssize_t fr_sbuff_in_vsprintf(fr_sbuff_t *sbuff, char const *fmt, va_list ap)
Print using a fmt string to an sbuff.
static int _sbuff_scratch_free(void *arg)
Free the scratch buffer used for printf.
TALLOC_CTX * ctx
Context to alloc new buffers in.
#define SBUFF_CHAR_CLASS_HEX
bool skip[UINT8_MAX+1]
Characters that are escaped, but left in the output along with the escape character.
#define SBUFF_CHAR_CLASS_NUM
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_diff(_a, _b)
size_t shifted
How much we've read from this file.
#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.
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_char(_sbuff_or_marker, _eob)
size_t init
How much to allocate initially.
char chr
Character at the start of an escape sequence.
bool do_oct
Process oct sequences i.e.
#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.
#define SBUFF_CHAR_CLASS_ALPHA_NUM
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define fr_sbuff_is_digit(_sbuff_or_marker)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
bool do_hex
Process hex sequences i.e.
size_t max
Maximum size of the buffer.
#define fr_sbuff_end(_sbuff_or_marker)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define FR_SBUFF(_sbuff_or_marker)
size_t len
Length of string.
#define FR_SBUFF_IN_BSTRNCPY_RETURN(...)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
char * buff_end
The true end of the buffer.
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_EXTEND_LOWAT_OR_RETURN(_sbuff, _len)
char subs[UINT8_MAX+1]
Special characters and their substitutions.
#define SBUFF_CHAR_UNPRINTABLES_LOW
fr_sbuff_marker_t * next
Next m in the list.
#define fr_sbuff_used(_sbuff_or_marker)
fr_sbuff_term_elem_t * elem
A sorted list of terminal strings.
#define fr_sbuff_behind(_sbuff_or_marker)
#define fr_sbuff_extend_lowat(_status, _sbuff_or_marker, _lowat)
FILE * file
FILE * we're reading from.
size_t max
Maximum number of bytes to read.
#define fr_sbuff_in_char(_sbuff,...)
Terminal element with pre-calculated lengths.
Set of terminal elements.
File sbuff extension structure.
Talloc sbuff extension structure.
Set of parsing rules for *unescape_until functions.
static char buff[sizeof("18446744073709551615")+3]
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
PRIVATE void float64(struct DATA *p, double d)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
An element in an arbitrarily ordered array of name to num mappings.
#define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
FR_SBUFF_SET_RETURN(sbuff, &our_sbuff)
static size_t char fr_sbuff_t size_t inlen
static size_t char ** out