20#ifndef USHER_BOUNDED_INI_H
21#define USHER_BOUNDED_INI_H
44 while (isspace((
unsigned char)*text)) ++text;
45 char *end = text + strlen(text);
46 while (end > text && isspace((
unsigned char)end[-1])) --end;
52static inline bool ini_uint(
const char *text, uint64_t *number)
54 if (!text || !*text || !number)
return false;
56 for (
const unsigned char *digit = (
const unsigned char *)text; *digit; ++digit) {
57 if (*digit <
'0' || *digit >
'9')
return false;
58 const unsigned next = *digit -
'0';
59 if (value > (UINT64_MAX - next) / 10)
return false;
60 value = value * 10 + next;
67static inline bool ini_real(
const char *text,
double *number)
69 if (!text || !*text || !number)
return false;
70 const char *cursor = text;
71 if (*cursor ==
'+' || *cursor ==
'-') ++cursor;
73 while (*cursor >=
'0' && *cursor <=
'9') { digit =
true; ++cursor; }
76 while (*cursor >=
'0' && *cursor <=
'9') { digit =
true; ++cursor; }
78 if (!digit)
return false;
79 if (*cursor ==
'e' || *cursor ==
'E') {
81 if (*cursor ==
'+' || *cursor ==
'-') ++cursor;
82 const char *
start = cursor;
83 while (*cursor >=
'0' && *cursor <=
'9') ++cursor;
84 if (cursor ==
start)
return false;
86 if (*cursor)
return false;
89 const double value = strtod(text, &end);
90 if (errno == ERANGE || *end || !isfinite(value))
return false;
100 if (!filename || !*filename || !buffer ||
capacity < 2 || !length)
return false;
101 const int descriptor =
open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
103 struct stat before, after;
106 if (fstat(
descriptor, &before) || !S_ISREG(before.st_mode) || before.st_size <= 0
107 || (uint64_t)before.st_size >=
capacity)
goto done;
110 if (
count < 0 && errno == EINTR)
continue;
111 if (
count < 0)
goto done;
113 used += (size_t)
count;
115 if (!used || used >=
capacity || used != (
size_t)before.st_size
116 || fstat(
descriptor, &after) || after.st_size != before.st_size
117 || after.st_mtim.tv_sec != before.st_mtim.tv_sec
118 || after.st_mtim.tv_nsec != before.st_mtim.tv_nsec)
goto done;
134 if (!buffer || !length || !handler || memchr(buffer,
'\0', length))
return false;
135 buffer[length] =
'\0';
137 char *limit = buffer + length;
138 while (line < limit) {
139 char *next = (
char *)memchr(line,
'\n', (
size_t)(limit - line));
140 if (next) *next++ =
'\0';
144 if (!*entry || *entry ==
'#' || *entry ==
';')
continue;
145 char *separator = strchr(entry,
'=');
146 if (!separator)
return false;
149 const char *value =
ini_trim(separator);
150 if (!*key || !*value)
return false;
151 for (
const unsigned char *character = (
const unsigned char *)key; *character; ++character) {
152 if (!isalnum(*character) && *character !=
'_' && *character !=
'.')
return false;
154 if (!handler(context, key, value))
return false;
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
static bool ini_real(const char *text, double *number)
Parse finite decimal/exponent notation, rejecting range errors and suffixes.
static bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read a nonempty regular file smaller than capacity and append NUL.
bool(* ini_field_handler)(void *context, const char *key, const char *value)
static char * ini_trim(char *text)
Trim surrounding whitespace in a mutable INI field.
static bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
Destructively parse flat fields with blank lines and #/; comment lines.
static unsigned capacity[COVERAGE_CAMERAS]
void(* close)(struct usbfs_handle *)
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.