xref: /lighttpd1.4/src/buffer.h (revision 81ef66ea)
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 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_returns_nonnull__
54 char* buffer_string_prepare_copy(buffer *b, size_t size);
55 
56 /* allocate buffer large enough to be able to append a string of given size
57  * if b was empty (used == 0) it will contain an empty string (used == 1)
58  * afterwards
59  * "used" data is preserved; if not empty buffer must contain a
60  * zero terminated string.
61  */
62 __attribute_returns_nonnull__
63 char* buffer_string_prepare_append(buffer *b, size_t size);
64 
65 /* use after prepare_(copy,append) when you have written data to the buffer
66  * to increase the buffer length by size. also sets the terminating zero.
67  * requires enough space is present for the terminating zero (prepare with the
68  * same size to be sure).
69  */
70 void buffer_commit(buffer *b, size_t size);
71 
72 /* sets string length:
73  * - always stores a terminating zero to terminate the "new" string
74  * - does not modify the string data apart from terminating zero
75  * - reallocates the buffer iff needed
76  */
77 void buffer_string_set_length(buffer *b, uint32_t len);
78 
79 /* clear buffer
80  * - invalidate buffer contents
81  * - unsets used chars but does not modify existing ptr contents
82  *   (b->ptr *is not* set to an empty, '\0'-terminated string "")
83  */
84 static inline void buffer_clear(buffer *b);
85 
86 /* reset buffer
87  * - invalidate buffer contents
88  * - unsets used chars
89  * - keeps smaller buffer (unmodified) for reuse
90  *   (b->ptr *is not* set to an empty, '\0'-terminated string "")
91  * - frees larger buffer (b->size > BUFFER_MAX_REUSE_SIZE)
92  */
93 static inline void buffer_reset(buffer *b);
94 
95 /* free buffer ptr
96  * - invalidate buffer contents; free ptr; reset ptr, used, size to 0
97  */
98 __attribute_cold__
99 void buffer_free_ptr(buffer *b);
100 
101 void buffer_copy_string(buffer * restrict b, const char * restrict s);
102 void buffer_copy_string_len(buffer * restrict b, const char * restrict s, size_t s_len);
103 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src);
104 
105 void buffer_append_string(buffer * restrict b, const char * restrict s);
106 void buffer_append_string_len(buffer * restrict b, const char * restrict s, size_t s_len);
107 static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src);
108 
109 #define buffer_append_uint_hex(b,len) buffer_append_uint_hex_lc((b),(len))
110 void buffer_append_uint_hex_lc(buffer *b, uintmax_t len);
111 void buffer_append_int(buffer *b, intmax_t val);
112 
113 void buffer_append_strftime(buffer * restrict b, const char * restrict format, const struct tm * restrict tm);
114 
115 /* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */
116 #define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100)
117 
118 size_t li_itostrn(char *buf, size_t buf_len, intmax_t val);
119 size_t li_utostrn(char *buf, size_t buf_len, uintmax_t val);
120 
121 /* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */
122 #define li_tohex(buf,buf_len,s,s_len) li_tohex_lc((buf),(buf_len),(s),(s_len))
123 void li_tohex_lc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
124 void li_tohex_uc(char * restrict buf, size_t buf_len, const char * restrict s, size_t s_len);
125 
126 /* NULL buffer or empty buffer (used == 0);
127  * unset "string" (buffer) config options are initialized to used == 0,
128  * while setting an empty string leads to used == 1
129  */
130 __attribute_pure__
131 static inline int buffer_is_empty(const buffer *b);
132 /* NULL buffer, empty buffer (used == 0) or empty string (used == 1) */
133 __attribute_pure__
134 static inline int buffer_string_is_empty(const buffer *b);
135 
136 __attribute_pure__
137 int buffer_eq_icase_ssn(const char * const a, const char * const b, const size_t len);
138 
139 __attribute_pure__
140 int buffer_eq_icase_ss(const char * const a, const size_t alen, const char * const b, const size_t blen);
141 
142 __attribute_pure__
143 int buffer_eq_icase_slen(const buffer * const b, const char * const s, const size_t slen);
144 #define buffer_is_equal_caseless_string buffer_eq_icase_slen
145 
146 __attribute_pure__
147 int buffer_eq_slen(const buffer * const b, const char * const s, const size_t slen);
148 
149 __attribute_pure__
150 int buffer_is_equal(const buffer *a, const buffer *b);
151 
152 __attribute_pure__
153 int buffer_is_equal_right_len(const buffer *a, const buffer *b, size_t len);
154 
155 __attribute_pure__
156 int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len);
157 
158 void buffer_substr_replace (buffer * restrict b, size_t offset, size_t len, const buffer * restrict replace);
159 
160 void buffer_append_string_encoded_hex_lc(buffer * restrict b, const char * restrict s, size_t len);
161 void buffer_append_string_encoded_hex_uc(buffer * restrict b, const char * restrict s, size_t len);
162 
163 typedef enum {
164 	ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */
165 	ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */
166 	ENCODING_HTML,         /* & becomes &amp; and so on */
167 	ENCODING_MINIMAL_XML   /* minimal encoding for xml */
168 } buffer_encoding_t;
169 
170 void buffer_append_string_encoded(buffer * restrict b, const char * restrict s, size_t s_len, buffer_encoding_t encoding);
171 
172 /* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */
173 void buffer_append_string_c_escaped(buffer * restrict b, const char * restrict s, size_t s_len);
174 
175 void buffer_urldecode_path(buffer *b);
176 
177 __attribute_pure__
178 int buffer_is_valid_UTF8(const buffer *b);
179 
180 void buffer_path_simplify(buffer *dest, buffer *src);
181 
182 void buffer_to_lower(buffer *b);
183 void buffer_to_upper(buffer *b);
184 
185 
186 /** deprecated */
187 __attribute_const__
188 char hex2int(unsigned char c);
189 
190 __attribute_pure__
191 static inline int light_isdigit(int c);
192 static inline int light_isdigit(int c) {
193 	return ((uint32_t)c-'0' <= '9'-'0');
194 }
195 
196 __attribute_pure__
197 static inline int light_isxdigit(int c);
198 static inline int light_isxdigit(int c) {
199 	return light_isdigit(c) || (((uint32_t)c | 0x20)-'a' <= 'f'-'a');
200 }
201 
202 __attribute_pure__
203 static inline int light_isalpha(int c);
204 static inline int light_isalpha(int c) {
205 	return (((uint32_t)c | 0x20)-'a' <= 'z'-'a');
206 }
207 
208 __attribute_pure__
209 static inline int light_isalnum(int c);
210 static inline int light_isalnum(int c) {
211 	return light_isdigit(c) || light_isalpha(c);
212 }
213 
214 #define light_isupper(c) ((uint32_t)(c)-'A' <= 'Z'-'A')
215 #define light_islower(c) ((uint32_t)(c)-'a' <= 'z'-'a')
216 
217 #define light_bshift(b)           ((uint64_t)1uL << (b))
218 #define light_btst(a,b)  ((a) &   ((uint64_t)1uL << (b)))
219 #define light_bclr(a,b)  ((a) &= ~((uint64_t)1uL << (b)))
220 #define light_bset(a,b)  ((a) |=  ((uint64_t)1uL << (b)))
221 
222 
223 __attribute_pure__
224 static inline uint32_t buffer_string_length(const buffer *b); /* buffer string length without terminating 0 */
225 
226 __attribute_pure__
227 static inline uint32_t buffer_string_space(const buffer *b); /* maximum length of string that can be stored without reallocating */
228 
229 static inline void buffer_append_slash(buffer *b); /* append '/' no non-empty strings not ending in '/' */
230 void buffer_append_path_len(buffer * restrict b, const char * restrict a, size_t alen); /* join strings with '/', if '/' not present */
231 
232 __attribute_pure__
233 static inline int buffer_has_slash_suffix (const buffer * const b);
234 
235 __attribute_pure__
236 static inline int buffer_has_pathsep_suffix (const buffer * const b);
237 
238 #define BUFFER_APPEND_STRING_CONST(x, y) \
239 	buffer_append_string_len(x, y, sizeof(y) - 1)
240 
241 #define BUFFER_COPY_STRING_CONST(x, y) \
242 	buffer_copy_string_len(x, y, sizeof(y) - 1)
243 
244 #define BUFFER_INTLEN_PTR(x) (x)->used ? (int)((x)->used - 1) : 0, (x)->ptr
245 
246 #define CONST_LEN_STR(x) (uint32_t)sizeof(x)-1, x
247 #define CONST_STR_LEN(x) x, (uint32_t)sizeof(x) - 1
248 #define CONST_BUF_LEN(x) ((x) ? (x)->ptr : NULL), buffer_string_length(x)
249 
250 
251 #define LI_NORETURN __attribute_noreturn__
252 
253 __attribute_cold__
254 void log_failed_assert(const char *filename, unsigned int line, const char *msg) LI_NORETURN;
255 #define force_assert(x) do { if (!(x)) log_failed_assert(__FILE__, __LINE__, "assertion failed: " #x); } while(0)
256 #define SEGFAULT() log_failed_assert(__FILE__, __LINE__, "aborted");
257 
258 /* inline implementations */
259 
260 static inline int buffer_is_empty(const buffer *b) {
261 	return NULL == b || 0 == b->used;
262 }
263 static inline int buffer_string_is_empty(const buffer *b) {
264 	return NULL == b || b->used < 2;
265 }
266 
267 static inline uint32_t buffer_string_length(const buffer *b) {
268 	return NULL != b && 0 != b->used ? b->used - 1 : 0;
269 }
270 
271 static inline uint32_t buffer_string_space(const buffer *b) {
272 	return NULL != b && b->size ? b->size - (b->used | (0 == b->used)) : 0;
273 }
274 
275 static inline void buffer_copy_buffer(buffer * restrict b, const buffer * restrict src) {
276 	buffer_copy_string_len(b, CONST_BUF_LEN(src));
277 }
278 
279 static inline void buffer_append_string_buffer(buffer * restrict b, const buffer * restrict src) {
280 	buffer_append_string_len(b, CONST_BUF_LEN(src));
281 }
282 
283 static inline void buffer_append_slash(buffer *b) {
284 	uint32_t len = buffer_string_length(b);
285 	if (len > 0 && '/' != b->ptr[len-1]) BUFFER_APPEND_STRING_CONST(b, "/");
286 }
287 
288 static inline void buffer_clear(buffer *b) {
289 	b->used = 0;
290 }
291 
292 static inline void buffer_reset(buffer *b) {
293 	b->used = 0;
294 	/* release buffer larger than BUFFER_MAX_REUSE_SIZE bytes */
295 	if (b->size > BUFFER_MAX_REUSE_SIZE) buffer_free_ptr(b);
296 }
297 
298 static inline int buffer_has_slash_suffix (const buffer * const b) {
299     return (b->used > 1 && b->ptr[b->used-2] == '/');
300 }
301 
302 static inline int buffer_has_pathsep_suffix (const buffer * const b) {
303     return (b->used > 1 && b->ptr[b->used-2] == '/');
304 }
305 
306 #endif
307