xref: /lighttpd1.4/src/buffer.h (revision 0c1aed55)
1 #ifndef _BUFFER_H_
2 #define _BUFFER_H_
3 #include "first.h"
4 
5 #include "settings.h"
6 
7 #include <sys/types.h>
8 #include <time.h>
9 
10 #if defined HAVE_STDINT_H
11 # include <stdint.h>
12 #elif defined HAVE_INTTYPES_H
13 # include <inttypes.h>
14 #endif
15 
16 /* generic string + binary data container; contains a terminating 0 in both
17  * cases
18  *
19  * used == 0 indicates a special "empty" state (unset config values); ptr
20  * might be NULL too then. otherwise an empty string has used == 1 (and ptr[0]
21  * == 0);
22  *
23  * copy/append functions will ensure used >= 1 (i.e. never leave it in the
24  * special empty state); only buffer_copy_buffer will copy the special empty
25  * state.
26  */
27 typedef struct {
28 	char *ptr;
29 
30 	/* "used" includes a terminating 0 */
31 	size_t used;
32 	/* size of allocated buffer at *ptr */
33 	size_t size;
34 } buffer;
35 
36 /* create new buffer; either empty or copy given data */
37 buffer* buffer_init(void);
38 buffer* buffer_init_buffer(const buffer *src); /* src can  be NULL */
39 buffer* buffer_init_string(const char *str); /* str can  be NULL */
40 
41 void buffer_free(buffer *b); /* b can be NULL */
42 /* truncates to used == 0; frees large buffers, might keep smaller ones for reuse */
43 void buffer_reset(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 *b, buffer *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 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 char* buffer_string_prepare_append(buffer *b, size_t size);
62 
63 /* use after prepare_(copy,append) when you have written data to the buffer
64  * to increase the buffer length by size. also sets the terminating zero.
65  * requires enough space is present for the terminating zero (prepare with the
66  * same size to be sure).
67  */
68 void buffer_commit(buffer *b, size_t size);
69 
70 /* sets string length:
71  * - always stores a terminating zero to terminate the "new" string
72  * - does not modify the string data apart from terminating zero
73  * - reallocates the buffer iff needed
74  */
75 void buffer_string_set_length(buffer *b, size_t len);
76 
77 void buffer_copy_string(buffer *b, const char *s);
78 void buffer_copy_string_len(buffer *b, const char *s, size_t s_len);
79 void buffer_copy_buffer(buffer *b, const buffer *src);
80 /* convert input to hex and store in buffer */
81 void buffer_copy_string_hex(buffer *b, const char *in, size_t in_len);
82 
83 void buffer_append_string(buffer *b, const char *s);
84 void buffer_append_string_len(buffer *b, const char *s, size_t s_len);
85 void buffer_append_string_buffer(buffer *b, const buffer *src);
86 
87 void buffer_append_uint_hex(buffer *b, uintmax_t len);
88 void buffer_append_int(buffer *b, intmax_t val);
89 void buffer_copy_int(buffer *b, intmax_t val);
90 
91 void buffer_append_strftime(buffer *b, const char *format, const struct tm *tm);
92 
93 /* '-', log_10 (2^bits) = bits * log 2 / log 10 < bits * 0.31, terminating 0 */
94 #define LI_ITOSTRING_LENGTH (2 + (8 * sizeof(intmax_t) * 31 + 99) / 100)
95 
96 void li_itostrn(char *buf, size_t buf_len, intmax_t val);
97 void li_utostrn(char *buf, size_t buf_len, uintmax_t val);
98 
99 /* buf must be (at least) 2*s_len + 1 big. uses lower-case hex letters. */
100 void li_tohex(char *buf, size_t buf_len, const char *s, size_t s_len);
101 
102 /* NULL buffer or empty buffer (used == 0);
103  * unset "string" (buffer) config options are initialized to used == 0,
104  * while setting an empty string leads to used == 1
105  */
106 static inline int buffer_is_empty(const buffer *b);
107 /* NULL buffer, empty buffer (used == 0) or empty string (used == 1) */
108 static inline int buffer_string_is_empty(const buffer *b);
109 
110 int buffer_is_equal(const buffer *a, const buffer *b);
111 int buffer_is_equal_right_len(const buffer *a, const buffer *b, size_t len);
112 int buffer_is_equal_string(const buffer *a, const char *s, size_t b_len);
113 int buffer_is_equal_caseless_string(const buffer *a, const char *s, size_t b_len);
114 int buffer_caseless_compare(const char *a, size_t a_len, const char *b, size_t b_len);
115 
116 void buffer_substr_replace (buffer *b, size_t offset, size_t len, const buffer *replace);
117 
118 typedef enum {
119 	ENCODING_REL_URI, /* for coding a rel-uri (/with space/and%percent) nicely as part of a href */
120 	ENCODING_REL_URI_PART, /* same as ENC_REL_URL plus coding / too as %2F */
121 	ENCODING_HTML,         /* & becomes &amp; and so on */
122 	ENCODING_MINIMAL_XML,  /* minimal encoding for xml */
123 	ENCODING_HEX,          /* encode string as hex */
124 	ENCODING_HTTP_HEADER   /* encode \n with \t\n */
125 } buffer_encoding_t;
126 
127 void buffer_append_string_encoded(buffer *b, const char *s, size_t s_len, buffer_encoding_t encoding);
128 
129 /* escape non-printable characters; simple escapes for \t, \r, \n; fallback to \xCC */
130 void buffer_append_string_c_escaped(buffer *b, const char *s, size_t s_len);
131 
132 /* to upper case, replace non alpha-numerics with '_'; if is_http_header prefix with "HTTP_" unless s is "content-type" */
133 void buffer_copy_string_encoded_cgi_varnames(buffer *b, const char *s, size_t s_len, int is_http_header);
134 
135 void buffer_urldecode_path(buffer *url);
136 void buffer_urldecode_query(buffer *url);
137 void buffer_path_simplify(buffer *dest, buffer *src);
138 
139 void buffer_to_lower(buffer *b);
140 void buffer_to_upper(buffer *b);
141 
142 
143 /** deprecated */
144 char hex2int(unsigned char c);
145 char int2hex(char i);
146 
147 int light_isdigit(int c);
148 int light_isxdigit(int c);
149 int light_isalpha(int c);
150 int light_isalnum(int c);
151 
152 static inline size_t buffer_string_length(const buffer *b); /* buffer string length without terminating 0 */
153 static inline size_t buffer_string_space(const buffer *b); /* maximum length of string that can be stored without reallocating */
154 static inline void buffer_append_slash(buffer *b); /* append '/' no non-empty strings not ending in '/' */
155 
156 #define BUFFER_APPEND_STRING_CONST(x, y) \
157 	buffer_append_string_len(x, y, sizeof(y) - 1)
158 
159 #define BUFFER_COPY_STRING_CONST(x, y) \
160 	buffer_copy_string_len(x, y, sizeof(y) - 1)
161 
162 #define CONST_STR_LEN(x) x, (x) ? sizeof(x) - 1 : 0
163 #define CONST_BUF_LEN(x) ((x) ? (x)->ptr : NULL), buffer_string_length(x)
164 
165 
166 void log_failed_assert(const char *filename, unsigned int line, const char *msg) LI_NORETURN;
167 #define force_assert(x) do { if (!(x)) log_failed_assert(__FILE__, __LINE__, "assertion failed: " #x); } while(0)
168 #define SEGFAULT() log_failed_assert(__FILE__, __LINE__, "aborted");
169 
170 /* inline implementations */
171 
172 static inline int buffer_is_empty(const buffer *b) {
173 	return NULL == b || 0 == b->used;
174 }
175 static inline int buffer_string_is_empty(const buffer *b) {
176 	return NULL == b || b->used < 2;
177 }
178 
179 static inline size_t buffer_string_length(const buffer *b) {
180 	return NULL != b && 0 != b->used ? b->used - 1 : 0;
181 }
182 
183 static inline size_t buffer_string_space(const buffer *b) {
184 	if (NULL == b || b->size == 0) return 0;
185 	if (0 == b->used) return b->size - 1;
186 	return b->size - b->used;
187 }
188 
189 static inline void buffer_append_slash(buffer *b) {
190 	size_t len = buffer_string_length(b);
191 	if (len > 0 && '/' != b->ptr[len-1]) BUFFER_APPEND_STRING_CONST(b, "/");
192 }
193 
194 #endif
195