1 #ifndef _BUFFER_H_ 2 #define _BUFFER_H_ 3 #include "first.h" 4 5 struct tm; /* declaration */ 6 7 /** 8 * max size of a buffer which will just be reset 9 * to ->used = 0 instead of really freeing the buffer 10 */ 11 #define BUFFER_MAX_REUSE_SIZE 4096 12 13 /* generic string + binary data container; contains a terminating 0 in both 14 * cases 15 * 16 * used == 0 indicates a special "empty" state (unset config values); 17 * ptr might be NULL, too. 18 * 19 * copy/append functions will ensure used >= 1 20 * (i.e. never leave it in the special empty state) 21 */ 22 typedef struct { 23 char *ptr; 24 25 /* "used" includes a terminating 0 */ 26 uint32_t used; 27 /* size of allocated buffer at *ptr */ 28 uint32_t size; 29 } buffer; 30 31 /* create new buffer; either empty or copy given data */ 32 __attribute_returns_nonnull__ 33 buffer* buffer_init(void); 34 35 __attribute_nonnull__ 36 __attribute_returns_nonnull__ 37 buffer* buffer_init_buffer(const buffer *src); 38 39 __attribute_returns_nonnull__ 40 buffer* buffer_init_string(const char *str); /* str can be NULL */ 41 42 void buffer_free(buffer *b); /* b can be NULL */ 43 44 /* reset b. if NULL != b && NULL != src, move src content to b. reset src. */ 45 __attribute_nonnull__ 46 void buffer_move(buffer * restrict b, buffer * restrict src); 47 48 /* make sure buffer is large enough to store a string of given size 49 * and a terminating zero. 50 * sets b to an empty string, and may drop old content. 51 * @return b->ptr 52 */ 53 __attribute_nonnull__ 54 __attribute_returns_nonnull__ 55 char* buffer_string_prepare_copy(buffer *b, size_t size); 56 57 /* allocate buffer large enough to be able to append a string of given size 58 * if b was empty (used == 0) it will contain an empty string (used == 1) 59 * afterwards 60 * "used" data is preserved; if not empty buffer must contain a 61 * zero terminated string. 62 */ 63 __attribute_nonnull__ 64 __attribute_returns_nonnull__ 65 char* buffer_string_prepare_append(buffer *b, size_t size); 66 67 /* extend and modify buffer for immediate addition of x bytes (differs from 68 * buffer_string_prepare_append() which only ensures space is available) 69 * returns pointer to which callers should immediately write x bytes 70 */ 71 __attribute_nonnull__ 72 __attribute_returns_nonnull__ 73 char* buffer_extend(buffer * const restrict b, size_t x); 74 75 /* use after prepare_(copy,append) when you have written data to the buffer 76 * to increase the buffer length by size. also sets the terminating zero. 77 * requires enough space is present for the terminating zero (prepare with the 78 * same size to be sure). 79 */ 80 __attribute_nonnull__ 81 void buffer_commit(buffer *b, size_t size); 82 83 /* clear buffer 84 * - invalidate buffer contents 85 * - unsets used chars but does not modify existing ptr contents 86 * (b->ptr *is not* set to an empty, '\0'-terminated string "") 87 */ 88 __attribute_nonnull__ 89 static inline void buffer_clear(buffer *b); 90 91 /* reset buffer 92 * - invalidate buffer contents 93 * - unsets used chars 94 * - keeps smaller buffer (unmodified) for reuse 95 * (b->ptr *is not* set to an empty, '\0'-terminated string "") 96 * - frees larger buffer (b->size > BUFFER_MAX_REUSE_SIZE) 97 */ 98 __attribute_nonnull__ 99 static inline void buffer_reset(buffer *b); 100 101 /* free buffer ptr 102 * - invalidate buffer contents; free ptr; reset ptr, used, size to 0 103 */ 104 __attribute_cold__ 105 __attribute_nonnull__ 106 void buffer_free_ptr(buffer *b); 107 108 void buffer_copy_string(buffer * restrict b, const char * restrict s); 109 void buffer_copy_string_len(buffer * restrict b, const char * restrict s, size_t len); 110 111 void buffer_append_string(buffer * restrict b, const char * restrict s); 112 void buffer_append_string_len(buffer * restrict b, const char * restrict s, size_t len); 113 void buffer_append_str2(buffer * restrict b, const char *s1, size_t len1, const char *s2, size_t len2); 114 void buffer_append_str3(buffer * restrict b, const char *s1, size_t len1, const char *s2, size_t len2, const char *s3, size_t len3); 115 116 struct const_iovec { 117 const void *iov_base; 118 size_t iov_len; 119 }; 120 121 __attribute_nonnull__ 122 void buffer_append_iovec(buffer * restrict b, const struct const_iovec *iov, size_t n); 123 124 #define buffer_append_uint_hex(b,len) buffer_append_uint_hex_lc((b),(len)) 125 __attribute_nonnull__ 126 void buffer_append_uint_hex_lc(buffer *b, uintmax_t len); 127 __attribute_nonnull__ 128 void buffer_append_int(buffer *b, intmax_t val); 129 130 void buffer_append_strftime(buffer * restrict b, const char * restrict format, const struct tm * restrict tm); 131 132 /* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */ 133 #define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100) 134 135 __attribute_nonnull__ 136 size_t li_itostrn(char *buf, size_t buf_len, intmax_t val); 137 __attribute_nonnull__ 138 size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val); 139 140 /* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */ 141 #define li_tohex(buf,buf_len,s,s_len) li_tohex_lc((buf),(buf_len),(s),(s_len)) 142 __attribute_nonnull__ 143 void li_tohex_lc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len); 144 __attribute_nonnull__ 145 void li_tohex_uc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len); 146 147 __attribute_nonnull__ 148 __attribute_pure__ 149 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len); 150 151 __attribute_nonnull__ 152 __attribute_pure__ 153 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen); 154 155 __attribute_nonnull__ 156 __attribute_pure__ 157 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen); 158 159 __attribute_nonnull__ 160 __attribute_pure__ 161 int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen); 162 163 __attribute_nonnull__ 164 __attribute_pure__ 165 int buffer_is_equal(const buffer *a, const buffer *b); 166 167 __attribute_nonnull__ 168 void buffer_substr_replace (buffer * restrict b, size_t offset, size_t len, const buffer * restrict replace); 169 170 __attribute_nonnull__ 171 void buffer_append_string_encoded_hex_lc(buffer * restrict b, const char * restrict s, size_t len); 172 __attribute_nonnull__ 173 void buffer_append_string_encoded_hex_uc(buffer * restrict b, const char * restrict s, size_t len); 174 175 typedef enum { 176 ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */ 177 ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */ 178 ENCODING_HTML, /* & becomes & and so on */ 179 ENCODING_MINIMAL_XML /* minimal encoding for xml */ 180 } buffer_encoding_t; 181 182 void buffer_append_string_encoded(buffer * restrict b, const char * restrict s, size_t s_len, buffer_encoding_t encoding); 183 184 /* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */ 185 __attribute_nonnull__ 186 void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len); 187 188 __attribute_nonnull__ 189 void buffer_urldecode_path(buffer *b); 190 191 __attribute_nonnull__ 192 __attribute_pure__ 193 int buffer_is_valid_UTF8(const buffer *b); 194 195 __attribute_nonnull__ 196 void buffer_path_simplify(buffer *b); 197 198 __attribute_nonnull__ 199 void buffer_to_lower(buffer *b); 200 __attribute_nonnull__ 201 void buffer_to_upper(buffer *b); 202 203 204 /** deprecated */ 205 __attribute_const__ 206 char hex2int(unsigned char c); 207 208 int li_hex2bin (unsigned char *bin, size_t binlen, const char *hexstr, size_t len); 209 210 __attribute_pure__ 211 static inline int light_isdigit(int c); 212 static inline int light_isdigit(int c) { 213 return ((uint32_t)c-'0' <= '9'-'0'); 214 } 215 216 __attribute_pure__ 217 static inline int light_isxdigit(int c); 218 static inline int light_isxdigit(int c) { 219 return light_isdigit(c) || (((uint32_t)c | 0x20)-'a' <= 'f'-'a'); 220 } 221 222 __attribute_pure__ 223 static inline int light_isalpha(int c); 224 static inline int light_isalpha(int c) { 225 return (((uint32_t)c | 0x20)-'a' <= 'z'-'a'); 226 } 227 228 __attribute_pure__ 229 static inline int light_isalnum(int c); 230 static inline int light_isalnum(int c) { 231 return light_isdigit(c) || light_isalpha(c); 232 } 233 234 #define light_isupper(c) ((uint32_t)(c)-'A' <= 'Z'-'A') 235 #define light_islower(c) ((uint32_t)(c)-'a' <= 'z'-'a') 236 237 #define light_bshift(b) ((uint64_t)1uL << (b)) 238 #define light_btst(a,b) ((a) & ((uint64_t)1uL << (b))) 239 #define light_bclr(a,b) ((a) &= ~((uint64_t)1uL << (b))) 240 #define light_bset(a,b) ((a) |= ((uint64_t)1uL << (b))) 241 242 243 void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen); /* join strings with '/', if '/' not present */ 244 void buffer_copy_path_len2(buffer * restrict b, const char * restrict s1, size_t len1, const char * restrict s2, size_t len2); 245 246 __attribute_nonnull__ 247 __attribute_pure__ 248 static inline int buffer_has_slash_suffix (const buffer * const b); 249 250 __attribute_nonnull__ 251 __attribute_pure__ 252 static inline int buffer_has_pathsep_suffix (const buffer * const b); 253 254 #define BUFFER_INTLEN_PTR(x) (int)buffer_clen(x), (x)->ptr 255 #define BUF_PTR_LEN(x) (x)->ptr, buffer_clen(x) 256 257 #define CONST_LEN_STR(x) (uint32_t)sizeof(x)-1, x 258 #define CONST_STR_LEN(x) x, (uint32_t)sizeof(x) - 1 259 260 261 #define LI_NORETURN __attribute_noreturn__ 262 263 __attribute_cold__ 264 void log_failed_assert(const char *filename, unsigned int line, const char *msg) LI_NORETURN; 265 #define force_assert(x) do { if (!(x)) log_failed_assert(__FILE__, __LINE__, "assertion failed: " #x); } while(0) 266 #define SEGFAULT() log_failed_assert(__FILE__, __LINE__, "aborted"); 267 268 269 /* inline implementations */ 270 271 __attribute_nonnull__ 272 __attribute_pure__ 273 static inline int buffer_is_unset(const buffer *b); 274 static inline int buffer_is_unset(const buffer *b) { 275 return 0 == b->used; 276 } 277 278 __attribute_nonnull__ 279 __attribute_pure__ 280 static inline int buffer_is_blank(const buffer *b); 281 static inline int buffer_is_blank(const buffer *b) { 282 return b->used < 2; /* buffer_is_blank() || buffer_is_unset() */ 283 } 284 285 /* buffer "C" len (bytes) */ 286 __attribute_nonnull__ 287 __attribute_pure__ 288 static inline uint32_t buffer_clen (const buffer *b); 289 static inline uint32_t buffer_clen (const buffer *b) { 290 return b->used - (0 != b->used); 291 } 292 293 /* buffer space remaining to append string without reallocating */ 294 __attribute_nonnull__ 295 __attribute_pure__ 296 static inline uint32_t buffer_string_space(const buffer *b); 297 static inline uint32_t buffer_string_space(const buffer *b) { 298 return b->size ? b->size - (b->used | (0 == b->used)) : 0; 299 } 300 301 __attribute_nonnull__ 302 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src); 303 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src) { 304 buffer_copy_string_len(b, BUF_PTR_LEN(src)); 305 } 306 307 __attribute_nonnull__ 308 static inline void buffer_append_buffer(buffer * restrict b, const buffer * restrict src); 309 static inline void buffer_append_buffer(buffer * restrict b, const buffer * restrict src) { 310 buffer_append_string_len(b, BUF_PTR_LEN(src)); 311 } 312 313 __attribute_nonnull__ 314 static inline void buffer_truncate(buffer *b, uint32_t len); 315 static inline void buffer_truncate(buffer *b, uint32_t len) { 316 b->ptr[len] = '\0'; /* b->ptr must exist; use buffer_blank() for trunc 0 */ 317 b->used = len + 1; 318 } 319 320 __attribute_nonnull__ 321 static inline void buffer_blank(buffer *b); 322 static inline void buffer_blank(buffer *b) { 323 b->ptr ? buffer_truncate(b, 0) : (void)buffer_extend(b, 0); 324 } 325 326 /* append '/' to non-empty strings not ending in '/' */ 327 __attribute_nonnull__ 328 static inline void buffer_append_slash(buffer *b); 329 static inline void buffer_append_slash(buffer *b) { 330 const uint32_t len = buffer_clen(b); 331 if (len > 0 && '/' != b->ptr[len-1]) 332 buffer_append_string_len(b, CONST_STR_LEN("/")); 333 } 334 335 static inline void buffer_clear(buffer *b) { 336 b->used = 0; 337 } 338 339 static inline void buffer_reset(buffer *b) { 340 b->used = 0; 341 /* release buffer larger than BUFFER_MAX_REUSE_SIZE bytes */ 342 if (b->size > BUFFER_MAX_REUSE_SIZE) buffer_free_ptr(b); 343 } 344 345 static inline int buffer_has_slash_suffix (const buffer * const b) { 346 return (b->used > 1 && b->ptr[b->used-2] == '/'); 347 } 348 349 static inline int buffer_has_pathsep_suffix (const buffer * const b) { 350 return (b->used > 1 && b->ptr[b->used-2] == '/'); 351 } 352 353 354 /* backwards compat (deprecated; older interfaces) */ 355 356 #define buffer_append_string_buffer buffer_append_buffer 357 #define buffer_is_equal_caseless_string buffer_eq_icase_slen 358 #define buffer_is_equal_string buffer_eq_slen 359 360 #define BUFFER_APPEND_STRING_CONST(x, y) \ 361 buffer_append_string_len(x, y, sizeof(y) - 1) 362 363 #define BUFFER_COPY_STRING_CONST(x, y) \ 364 buffer_copy_string_len(x, y, sizeof(y) - 1) 365 366 #define CONST_BUF_LEN(x) ((x) ? (x)->ptr : NULL), buffer_string_length(x) 367 368 /* NULL buffer or empty buffer (used == 0); 369 * unset "string" (buffer) config options are initialized to used == 0, 370 * while setting an empty string leads to used == 1 371 */ 372 __attribute_pure__ 373 static inline int buffer_is_empty(const buffer *b); 374 static inline int buffer_is_empty(const buffer *b) { 375 return NULL == b || buffer_is_unset(b); 376 } 377 /* NULL buffer, empty buffer (used == 0) or empty string (used == 1) */ 378 __attribute_pure__ 379 static inline int buffer_string_is_empty(const buffer *b); 380 static inline int buffer_string_is_empty(const buffer *b) { 381 return NULL == b || buffer_is_blank(b); 382 } 383 384 /* buffer string length without terminating 0 */ 385 __attribute_pure__ 386 static inline uint32_t buffer_string_length(const buffer *b); 387 static inline uint32_t buffer_string_length(const buffer *b) { 388 return NULL != b ? buffer_clen(b) : 0; 389 } 390 391 /* sets string length: 392 * - deprecated; use buffer_truncate() or buffer_extend() instead 393 * - always stores a terminating zero to terminate the "new" string 394 * - does not modify the string data apart from terminating zero 395 * - reallocates the buffer iff needed 396 */ 397 __attribute_nonnull__ 398 static inline void buffer_string_set_length(buffer *b, uint32_t len); 399 static inline void buffer_string_set_length(buffer *b, uint32_t len) { 400 if (len < b->size) 401 buffer_truncate(b, len); 402 else 403 buffer_extend(b, len - buffer_clen(b)); 404 } 405 406 407 #endif 408