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