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