23 RCSID(
"$Id: cfbe5b8bdafc83784e982c3a90658ea4148ea245 $")
25 #include <freeradius-devel/util/misc.h>
26 #include <freeradius-devel/util/syserror.h>
27 #include <freeradius-devel/util/atexit.h>
32 static_assert(
sizeof(
long long) >=
sizeof(int64_t),
"long long must be as wide or wider than an int64_t");
33 static_assert(
sizeof(
unsigned long long) >=
sizeof(uint64_t),
"long long must be as wide or wider than an uint64_t");
46 #if defined(STATIC_ANALYZER) || !defined(NDEBUG)
47 # 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)
49 # define CHECK_SBUFF_INIT(_sbuff)
59 [
'+'] =
true, [
'-'] =
true
64 [
'-'] =
true, [
'+'] =
true, [
'e'] =
true, [
'E'] =
true, [
'.'] =
true,
90 [
'-'] =
true, [
'_'] =
true,
93 [
'\t'] =
true, [
'\n'] =
true, [
'\r'] =
true, [
'\f'] =
true, [
'\v'] =
true, [
' '] =
true,
97 [
'\n'] =
true, [
'\r'] =
true
101 [
'\t'] =
true, [
' '] =
true,
116 char const *i_start,
char const *i_end)
119 size_t i_len = i_end - i_start;
121 if (
unlikely((o_end < o_start) || (i_end < i_start)))
return 0;
123 diff = (o_end - o_start) - (i_len);
124 if (diff < 0)
return diff;
126 if ((i_start > o_end) || (i_end < o_start)) {
127 memcpy(o_start, i_start, i_len);
129 memmove(o_start, i_start, i_len);
135 static inline CC_HINT(always_inline)
size_t min(
size_t x,
size_t y)
137 return x <
y ? x :
y;
153 old_buff = sbuff->buff;
161 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
164 sbuff_i->buff = new_buff;
165 sbuff_i->start = new_buff +
min(new_len, sbuff_i->start - old_buff);
166 sbuff_i->end = sbuff_i->buff + new_len;
167 *(sbuff_i->end) =
'\0';
169 sbuff_i->
p = new_buff +
min(new_len, sbuff_i->
p - old_buff);
171 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);
191 size_t max_shift = shift;
192 bool reterminate =
false;
205 reterminate = (sbuff->
p < sbuff->end) && (*sbuff->
p ==
'\0') && !sbuff->is_const;
213 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
216 max_shift =
min(max_shift, sbuff_i->
p -
buff);
217 if (!max_shift)
return 0;
219 for (m_i = sbuff_i->m; m_i; m_i = m_i->
next) {
220 max_shift =
min(max_shift, m_i->p -
buff);
221 if (!max_shift)
return 0;
232 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
234 char *start = sbuff_i->start;
236 sbuff_i->start -=
min(max_shift, sbuff_i->start -
buff);
237 sbuff_i->
p -= max_shift;
238 sbuff_i->end -= max_shift;
239 sbuff_i->shifted += (max_shift - (start - sbuff_i->start));
240 for (m_i = sbuff_i->m; m_i; m_i = m_i->
next) m_i->p -= max_shift;
247 if ((
buff + max_shift) < end) memmove(
buff,
buff + max_shift, end - (
buff + max_shift));
249 if (reterminate) *sbuff->
p =
'\0';
260 size_t read, available, total_read, shift;
266 if (fctx->
eof)
return 0;
268 if (extension == SIZE_MAX) extension = 0;
270 total_read = fctx->
shifted + (sbuff->end - sbuff->buff);
271 if (total_read >= fctx->
max) {
301 available = fctx->
buff_end - sbuff->end;
302 if (available > (fctx->
max - total_read)) available = fctx->
max - total_read;
303 if (available < extension) {
308 read = fread(sbuff->end, 1, available, fctx->
file);
309 for (sbuff_i = sbuff; sbuff_i; sbuff_i = sbuff_i->parent) {
310 sbuff_i->end += read;
315 if (read < available) {
316 if (!feof(fctx->
file)) {
339 size_t clen, nlen, elen = extension;
344 clen = sbuff->buff ? talloc_array_length(sbuff->buff) : 0;
352 if ((clen + elen) < tctx->
init) {
353 elen = (tctx->
init - clen) + 1;
358 }
else if (elen < clen) {
366 if (tctx->
max && ((clen + elen) > tctx->
max)) {
367 elen = tctx->
max - clen;
370 "%zu bytes, max is %zu bytes",
371 extension, clen + extension, tctx->
max);
378 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, nlen);
401 size_t clen = 0, nlen = 1;
407 if (sbuff->buff) clen = talloc_array_length(sbuff->buff);
409 if (len != SIZE_MAX) {
411 }
else if (sbuff->buff){
412 nlen += (sbuff->
p - sbuff->start);
416 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, nlen);
440 fr_sbuff_set_to_start(sbuff);
445 new_buff = talloc_realloc(tctx->
ctx, sbuff->buff,
char, tctx->
init);
448 talloc_array_length(sbuff->buff), tctx->
init);
451 sbuff->buff = new_buff;
464 #define FILL_OR_GOTO_DONE(_out, _in, _len) if (fr_sbuff_move(_out, _in, _len) < (size_t)(_len)) goto done
473 #define CONSTRAINED_END(_sbuff, _max, _used) \
474 (((_max) - (_used)) > fr_sbuff_remaining(_sbuff) ? (_sbuff)->end : (_sbuff)->p + ((_max) - (_used)))
488 size_t i, len, max = 0;
494 for (i = 0; i < term->
len; i++) {
496 if (len > max) max = len;
501 if (i > 0) *needle_len = max;
530 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
532 if (!term)
return false;
536 if (!term_idx)
return false;
542 if (remaining == 0) {
543 if (status & FR_SBUFF_EXTEND_ERROR)
return false;
544 return (idx[
'\0'] != 0);
549 while (start <= end) {
557 ret = strncmp(p, elem, tlen < (
size_t)remaining ? tlen : (
size_t)remaining);
562 if (remaining >= tlen)
return true;
577 mid = start + ((end - start) / 2);
602 for (i = 0; i < len; i++)
FR_FAULT_LOG(
"\t\"%s\" (%zu)", elem[i] ? elem[i]->str :
"NULL", elem[i] ? elem[i]->len : 0);
624 #if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
637 while ((i < a->len) && (j < b->len)) {
643 tmp[num++] = &a->
elem[i++];
645 }
else if (cmp < 0) {
646 tmp[num++] = &a->
elem[i++];
648 }
else if (cmp > 0) {
649 tmp[num++] = &b->
elem[j++];
658 while (i < a->len) tmp[num++] = &a->
elem[i++];
659 while (j < b->len) tmp[num++] = &b->
elem[j++];
671 for (i = 0; i < num; i++)
out->elem[i] = *tmp[i];
673 #
if !defined(NDEBUG) && defined(WITH_VERIFY_PTR)
706 if (chunk_len > remaining) chunk_len = remaining;
737 fr_sbuff_marker(&m,
out);
747 if (chunk_len > remaining) chunk_len = remaining;
757 return -(remaining - (chunk_len + copied));
781 bool const allowed[
static UINT8_MAX + 1])
796 while ((p < end) && allowed[(
uint8_t)*p]) p++;
831 bool do_escape =
false;
834 size_t needle_len = 1;
835 char escape_chr = u_rules ? u_rules->
chr :
'\0';
856 if (escape_chr ==
'\0') {
862 }
else if (*p == escape_chr) {
904 bool do_escape =
false;
910 size_t needle_len = 1;
912 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
927 fr_sbuff_marker(&c_s, &our_in);
928 fr_sbuff_marker(&end, &our_in);
929 fr_sbuff_marker_update_end(&end, len);
931 fr_sbuff_marker(&o_s,
out);
956 fr_sbuff_marker(&m, &our_in);
959 if (fr_sbuff_out_uint8_hex(NULL, &escape, &our_in,
false) != 2) {
961 fr_sbuff_marker_release(&m);
967 fr_sbuff_marker_release(&m);
970 fr_sbuff_marker_release(&m);
982 fr_sbuff_marker(&m, &our_in);
984 if (fr_sbuff_out_uint8_oct(NULL, &escape, &our_in,
false) != 3) {
986 fr_sbuff_marker_release(&m);
992 fr_sbuff_marker_release(&m);
995 fr_sbuff_marker_release(&m);
1009 if (u_rules->
subs[c] ==
'\0') {
1010 if (u_rules->
skip[c] ==
true)
goto next;
1063 return fr_sbuff_marker_release_behind(&o_s);
1078 static bool const bool_prefix[
UINT8_MAX + 1] = {
1079 [
't'] =
true, [
'T'] =
true,
1080 [
'f'] =
true, [
'F'] =
true,
1081 [
'y'] =
true, [
'Y'] =
true,
1082 [
'n'] =
true, [
'N'] =
true,
1085 if (fr_sbuff_is_in_charset(&our_in, bool_prefix)) {
1122 fr_strerror_const(
"Not a valid boolean value. Accepted values are 'yes', 'no', 'true', 'false'");
1138 #define SBUFF_PARSE_INT_DEF(_name, _type, _min, _max, _max_char, _base) \
1139 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1141 char buff[_max_char + 1]; \
1142 char *end, *a_end; \
1146 fr_sbuff_t our_in = FR_SBUFF(in); \
1148 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1150 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1154 num = strtoll(buff, &end, _base); \
1155 cast_num = (_type)(num); \
1156 if (end == buff) { \
1157 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1160 if (num > cast_num) { \
1162 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1163 *out = (_type)(_max); \
1166 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == LLONG_MAX))) goto overflow; \
1167 if (num < cast_num) { \
1169 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1170 *out = (_type)(_min); \
1173 if ((errno == ERANGE) && (num == LLONG_MIN)) goto underflow; \
1174 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1175 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1176 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1177 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1178 *out = (_type)(_max); \
1179 FR_SBUFF_ERROR_RETURN(&our_in); \
1183 if (err) *err = FR_SBUFF_PARSE_OK; \
1186 return fr_sbuff_advance(in, end - buff); \
1205 #define SBUFF_PARSE_UINT_DEF(_name, _type, _max, _max_char, _base) \
1206 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1208 char buff[_max_char + 1]; \
1209 char *end, *a_end; \
1211 unsigned long long num; \
1213 fr_sbuff_t our_in = FR_SBUFF(in); \
1215 len = fr_sbuff_out_bstrncpy(&FR_SBUFF_IN(buff, sizeof(buff)), &our_in, _max_char); \
1217 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1220 if (buff[0] == '-') { \
1221 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1225 num = strtoull(buff, &end, _base); \
1226 cast_num = (_type)(num); \
1227 if (end == buff) { \
1228 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1231 if (num > cast_num) { \
1233 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1234 *out = (_type)(_max); \
1237 if (((errno == EINVAL) && (num == 0)) || ((errno == ERANGE) && (num == ULLONG_MAX))) goto overflow; \
1238 if (no_trailing && (((a_end = in->p + (end - buff)) + 1) < in->end)) { \
1239 if (isdigit((uint8_t) *a_end) || (((_base > 10) || ((_base == 0) && (len > 2) && (buff[0] == '0') && (buff[1] == 'x'))) && \
1240 ((tolower((uint8_t) *a_end) >= 'a') && (tolower((uint8_t) *a_end) <= 'f')))) { \
1241 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1242 *out = (_type)(_max); \
1243 FR_SBUFF_ERROR_RETURN(&our_in); \
1245 if (err) *err = FR_SBUFF_PARSE_OK; \
1248 if (err) *err = FR_SBUFF_PARSE_OK; \
1251 return fr_sbuff_advance(in, end - buff); \
1289 #define SBUFF_PARSE_FLOAT_DEF(_name, _type, _func, _max_char) \
1290 fr_slen_t fr_sbuff_out_##_name(fr_sbuff_parse_error_t *err, _type *out, fr_sbuff_t *in, bool no_trailing) \
1292 char buff[_max_char + 1] = ""; \
1294 fr_sbuff_t our_in = FR_SBUFF(in); \
1297 len = fr_sbuff_out_bstrncpy_allowed(&FR_SBUFF_OUT(buff, sizeof(buff)), &our_in, SIZE_MAX, sbuff_char_class_float); \
1298 if (len == sizeof(buff)) { \
1299 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1301 } else if (len == 0) { \
1302 if (err) *err = FR_SBUFF_PARSE_ERROR_NOT_FOUND; \
1306 res = _func(buff, &end); \
1307 if (errno == ERANGE) { \
1309 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_OVERFLOW; \
1311 if (err) *err = FR_SBUFF_PARSE_ERROR_NUM_UNDERFLOW; \
1315 if (no_trailing && (*end != '\0')) { \
1316 if (err) *err = FR_SBUFF_PARSE_ERROR_TRAILING; \
1317 FR_SBUFF_ERROR_RETURN(&our_in); \
1320 return fr_sbuff_advance(in, end - buff); \
1344 size_t to_copy = len;
1345 if (to_copy > o_remaining) to_copy = o_remaining;
1346 if (to_copy > i_remaining) to_copy = i_remaining;
1364 size_t to_copy = len;
1365 if (to_copy > o_remaining) to_copy = o_remaining;
1366 if (to_copy > i_remaining) to_copy = i_remaining;
1384 size_t to_copy = len;
1385 if (to_copy > o_remaining) to_copy = o_remaining;
1386 if (to_copy > i_remaining) to_copy = i_remaining;
1404 size_t to_copy = len;
1405 if (to_copy > o_remaining) to_copy = o_remaining;
1406 if (to_copy > i_remaining) to_copy = i_remaining;
1425 if (
unlikely(sbuff->is_const))
return 0;
1430 safecpy(sbuff->
p, sbuff->end, str, str + len);
1431 sbuff->
p[len] =
'\0';
1449 if (
unlikely(sbuff->is_const))
return 0;
1453 safecpy(sbuff->
p, sbuff->end, str, str + len);
1454 sbuff->
p[len] =
'\0';
1473 if (
unlikely(sbuff->is_const))
return 0;
1475 len = talloc_array_length(str) - 1;
1479 safecpy(sbuff->
p, sbuff->end, str, str + len);
1480 sbuff->
p[len] =
'\0';
1495 TALLOC_CTX *scratch;
1499 scratch = talloc_pool(NULL, 4096);
1523 TALLOC_CTX *scratch;
1530 if (
unlikely(sbuff->is_const))
return 0;
1559 if (
unlikely(sbuff->is_const))
return 0;
1590 if (
unlikely(sbuff->is_const))
return 0;
1613 sub = e_rules->subs[c];
1624 if (e_rules->esc[c]) {
1629 if (e_rules->do_oct) {
1632 }
else if (e_rules->do_hex) {
1658 if (
unlikely(sbuff->is_const))
return 0;
1677 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1686 found = memmem(sbuff->
p, needle_len, needle, needle_len);
1687 if (!found)
return 0;
1704 char const *p, *n_p;
1709 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1719 end = p + needle_len;
1721 for (p = sbuff->
p, n_p = needle; p < end; p++, n_p++) {
1742 size_t needle_len = 0;
1748 while (total < len) {
1755 while ((p < end) && allowed[(
uint8_t)*p]) {
1756 if (needle_len == 0) {
1796 if (p != end)
break;
1815 bool do_escape =
false;
1818 size_t needle_len = 1;
1828 while (total < len) {
1836 if (escape_chr ==
'\0') {
1842 }
else if (*p == escape_chr) {
1852 if (p != end)
break;
1874 size_t clen = strlen(chr);
1881 if (len < clen)
return NULL;
1883 while (total <= (len - clen)) {
1922 while (total < len) {
1929 found = memchr(our_sbuff.
p, c, end - our_sbuff.
p);
1958 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
1959 if (!needle_len)
return 0;
1964 if (len < needle_len)
return NULL;
1966 while (total <= (len - needle_len)) {
1977 found = memmem(our_sbuff.
p, end - our_sbuff.
p, needle, needle_len);
1988 total +=
fr_sbuff_set(&our_sbuff, (end - needle_len) + 1);
2011 if (needle_len == SIZE_MAX) needle_len = strlen(needle);
2012 if (!needle_len)
return 0;
2017 if (len < needle_len)
return NULL;
2019 while (total <= (len - needle_len)) {
2025 for (p = our_sbuff.
p, n_p = needle, end = our_sbuff.
p + needle_len;
2053 if (*sbuff->
p != c)
return false;
2074 if (*sbuff->
p == c)
return false;
2089 char *p = sbuff->
p - 1;
2092 while ((p >= sbuff->start) && to_trim[(
uint8_t)*p]) p--;
2095 if (slen != 0) fr_sbuff_terminate(sbuff);
2114 size_t needle_len = 1;
2120 fr_sbuff_extend_status_t status = FR_SBUFF_EXTENDABLE;
2123 (status & FR_SBUFF_EXTEND_ERROR) == 0) {
2144 static bool const unprintables[
UINT8_MAX + 1] = {
2149 static _Thread_local
char str[10][5];
2150 static _Thread_local
char **p;
2175 if (!p || (p++ >= ((
char **)str + (
NUM_PTR_ELEMENTS(str) - 1)))) p = (
char **)str;
2177 if (unprintables[(
uint8_t)c]) {
2178 snprintf(*p,
sizeof(*str),
"\\x%x", c);
2222 if (p_rules->escapes) {
2228 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]
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