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