1 #ifndef _BUFFER_H_ 2 #define _BUFFER_H_ 3 #include "first.h" 4 5 struct tm; /* declaration */ 6 7 /* generic string + binary data container; contains a terminating 0 in both 8 * cases 9 * 10 * used == 0 indicates a special "empty" state (unset config values); ptr 11 * might be NULL too then. otherwise an empty string has used == 1 (and ptr[0] 12 * == 0); 13 * 14 * copy/append functions will ensure used >= 1 (i.e. never leave it in the 15 * special empty state); only buffer_copy_buffer will copy the special empty 16 * state. 17 */ 18 typedef struct { 19 char *ptr; 20 21 /* "used" includes a terminating 0 */ 22 uint32_t used; 23 /* size of allocated buffer at *ptr */ 24 uint32_t size; 25 } buffer; 26 27 /* create new buffer; either empty or copy given data */ 28 __attribute_returns_nonnull__ 29 buffer* buffer_init(void); 30 31 __attribute_returns_nonnull__ 32 buffer* buffer_init_buffer(const buffer *src); /* src can be NULL */ 33 34 __attribute_returns_nonnull__ 35 buffer* buffer_init_string(const char *str); /* str can be NULL */ 36 37 void buffer_free(buffer *b); /* b can be NULL */ 38 /* truncates to used == 0; frees large buffers, might keep smaller ones for reuse */ 39 void buffer_reset(buffer *b); /* b can be NULL */ 40 41 /* reset b. if NULL != b && NULL != src, move src content to b. reset src. */ 42 void buffer_move(buffer * restrict b, buffer * restrict src); 43 44 /* make sure buffer is large enough to store a string of given size 45 * and a terminating zero. 46 * sets b to an empty string, and may drop old content. 47 * @return b->ptr 48 */ 49 __attribute_returns_nonnull__ 50 char* buffer_string_prepare_copy(buffer *b, size_t size); 51 52 /* allocate buffer large enough to be able to append a string of given size 53 * if b was empty (used == 0) it will contain an empty string (used == 1) 54 * afterwards 55 * "used" data is preserved; if not empty buffer must contain a 56 * zero terminated string. 57 */ 58 __attribute_returns_nonnull__ 59 char* buffer_string_prepare_append(buffer *b, size_t size); 60 61 /* use after prepare_(copy,append) when you have written data to the buffer 62 * to increase the buffer length by size. also sets the terminating zero. 63 * requires enough space is present for the terminating zero (prepare with the 64 * same size to be sure). 65 */ 66 void buffer_commit(buffer *b, size_t size); 67 68 /* sets string length: 69 * - always stores a terminating zero to terminate the "new" string 70 * - does not modify the string data apart from terminating zero 71 * - reallocates the buffer iff needed 72 */ 73 void buffer_string_set_length(buffer *b, uint32_t len); 74 75 /* clear buffer 76 * - invalidate buffer contents 77 * - unsets used chars but does not modify existing ptr contents 78 * (b->ptr *is not* set to an empty, '\0'-terminated string "") 79 */ 80 static inline void buffer_clear(buffer *b); 81 82 void buffer_copy_string(buffer * restrict b, const char * restrict s); 83 void buffer_copy_string_len(buffer * restrict b, const char * restrict s, size_t s_len); 84 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src); 85 86 void buffer_append_string(buffer * restrict b, const char * restrict s); 87 void buffer_append_string_len(buffer * restrict b, const char * restrict s, size_t s_len); 88 static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src); 89 90 #define buffer_append_uint_hex(b,len) buffer_append_uint_hex_lc((b),(len)) 91 void buffer_append_uint_hex_lc(buffer *b, uintmax_t len); 92 void buffer_append_int(buffer *b, intmax_t val); 93 94 void buffer_append_strftime(buffer * restrict b, const char * restrict format, const struct tm * restrict tm); 95 96 /* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */ 97 #define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100) 98 99 size_t li_itostrn(char *buf, size_t buf_len, intmax_t val); 100 size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val); 101 102 /* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */ 103 #define li_tohex(buf,buf_len,s,s_len) li_tohex_lc((buf),(buf_len),(s),(s_len)) 104 void li_tohex_lc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len); 105 void li_tohex_uc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len); 106 107 /* NULL buffer or empty buffer (used == 0); 108 * unset "string" (buffer) config options are initialized to used == 0, 109 * while setting an empty string leads to used == 1 110 */ 111 __attribute_pure__ 112 static inline int buffer_is_empty(const buffer *b); 113 /* NULL buffer, empty buffer (used == 0) or empty string (used == 1) */ 114 __attribute_pure__ 115 static inline int buffer_string_is_empty(const buffer *b); 116 117 __attribute_pure__ 118 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len); 119 120 __attribute_pure__ 121 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen); 122 123 __attribute_pure__ 124 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen); 125 126 __attribute_pure__ 127 int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen); 128 129 __attribute_pure__ 130 int buffer_is_equal(const buffer *a, const buffer *b); 131 132 __attribute_pure__ 133 int buffer_is_equal_right_len(const buffer *a, const buffer *b, size_t len); 134 135 __attribute_pure__ 136 int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len); 137 138 __attribute_pure__ 139 int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len); 140 141 void buffer_substr_replace (buffer * restrict b, size_t offset, size_t len, const buffer * restrict replace); 142 143 void buffer_append_string_encoded_hex_lc(buffer * restrict b, const char * restrict s, size_t len); 144 void buffer_append_string_encoded_hex_uc(buffer * restrict b, const char * restrict s, size_t len); 145 146 typedef enum { 147 ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */ 148 ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */ 149 ENCODING_HTML, /* & becomes & and so on */ 150 ENCODING_MINIMAL_XML /* minimal encoding for xml */ 151 } buffer_encoding_t; 152 153 void buffer_append_string_encoded(buffer * restrict b, const char * restrict s, size_t s_len, buffer_encoding_t encoding); 154 155 /* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */ 156 void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len); 157 158 /* to upper case, replace non alpha-numerics with '_'; if is_http_header prefix with "HTTP_" unless s is "content-type" */ 159 void buffer_copy_string_encoded_cgi_varnames(buffer * restrict b, const char * restrict s, size_t s_len, int is_http_header); 160 161 void buffer_urldecode_path(buffer *url); 162 void buffer_urldecode_query(buffer *url); 163 int buffer_is_valid_UTF8(const buffer *b); 164 void buffer_path_simplify(buffer *dest, buffer *src); 165 166 void buffer_to_lower(buffer *b); 167 void buffer_to_upper(buffer *b); 168 169 170 /** deprecated */ 171 char hex2int(unsigned char c); 172 char int2hex(char i); 173 174 __attribute_pure__ 175 static inline int light_isdigit(int c); 176 static inline int light_isdigit(int c) { 177 return (c >= '0' && c <= '9'); 178 } 179 180 __attribute_pure__ 181 static inline int light_isxdigit(int c); 182 static inline int light_isxdigit(int c) { 183 return light_isdigit(c) || (c |= 32, c >= 'a' && c <= 'f'); 184 } 185 186 __attribute_pure__ 187 static inline int light_isalpha(int c); 188 static inline int light_isalpha(int c) { 189 return (c |= 32, c >= 'a' && c <= 'z'); 190 } 191 192 __attribute_pure__ 193 static inline int light_isalnum(int c); 194 static inline int light_isalnum(int c) { 195 return light_isdigit(c) || light_isalpha(c); 196 } 197 198 199 __attribute_pure__ 200 static inline uint32_t buffer_string_length(const buffer *b); /* buffer string length without terminating 0 */ 201 202 __attribute_pure__ 203 static inline uint32_t buffer_string_space(const buffer *b); /* maximum length of string that can be stored without reallocating */ 204 205 static inline void buffer_append_slash(buffer *b); /* append '/' no non-empty strings not ending in '/' */ 206 void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen); /* join strings with '/', if '/' not present */ 207 208 #define BUFFER_APPEND_STRING_CONST(x, y) \ 209 buffer_append_string_len(x, y, sizeof(y) - 1) 210 211 #define BUFFER_COPY_STRING_CONST(x, y) \ 212 buffer_copy_string_len(x, y, sizeof(y) - 1) 213 214 #define BUFFER_INTLEN_PTR(x) (x)->used ? (int)((x)->used - 1) : 0, (x)->ptr 215 216 #define CONST_LEN_STR(x) (uint32_t)sizeof(x)-1, x 217 #define CONST_STR_LEN(x) x, (uint32_t)sizeof(x) - 1 218 #define CONST_BUF_LEN(x) ((x) ? (x)->ptr : NULL), buffer_string_length(x) 219 220 221 #define LI_NORETURN __attribute_noreturn__ 222 223 __attribute_cold__ 224 void log_failed_assert(const char *filename, unsigned int line, const char *msg) LI_NORETURN; 225 #define force_assert(x) do { if (!(x)) log_failed_assert(__FILE__, __LINE__, "assertion failed: " #x); } while(0) 226 #define SEGFAULT() log_failed_assert(__FILE__, __LINE__, "aborted"); 227 228 /* inline implementations */ 229 230 static inline int buffer_is_empty(const buffer *b) { 231 return NULL == b || 0 == b->used; 232 } 233 static inline int buffer_string_is_empty(const buffer *b) { 234 return NULL == b || b->used < 2; 235 } 236 237 static inline uint32_t buffer_string_length(const buffer *b) { 238 return NULL != b && 0 != b->used ? b->used - 1 : 0; 239 } 240 241 static inline uint32_t buffer_string_space(const buffer *b) { 242 return NULL != b && b->size ? b->size - (b->used | (0 == b->used)) : 0; 243 } 244 245 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src) { 246 buffer_copy_string_len(b, CONST_BUF_LEN(src)); 247 } 248 249 static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src) { 250 buffer_append_string_len(b, CONST_BUF_LEN(src)); 251 } 252 253 static inline void buffer_append_slash(buffer *b) { 254 uint32_t len = buffer_string_length(b); 255 if (len > 0 && '/' != b->ptr[len-1]) BUFFER_APPEND_STRING_CONST(b, "/"); 256 } 257 258 static inline void buffer_clear(buffer *b) { 259 b->used = 0; 260 } 261 262 #endif 263