consitency with type names
[m6w6/ext-http] / php_http_buffer.h
1
2 /* $Id: php_http_buffer_t.h 229282 2007-02-07 15:31:50Z mike $ */
3
4 #ifndef _PHP_HTTP_BUFFER_H
5 #define _PHP_HTTP_BUFFER_H
6
7 #ifndef PHP_HTTP_BUFFER_DEFAULT_SIZE
8 # define PHP_HTTP_BUFFER_DEFAULT_SIZE 256
9 #endif
10
11 #define PHP_HTTP_BUFFER_ERROR ((size_t) -1)
12 #define PHP_HTTP_BUFFER_NOMEM PHP_HTTP_BUFFER_ERROR
13 #define PHP_HTTP_BUFFER_PASS0 PHP_HTTP_BUFFER_ERROR
14
15 #ifndef STR_FREE
16 # define STR_FREE(STR) \
17 { \
18 if (STR) { \
19 efree(STR); \
20 } \
21 }
22 #endif
23 #ifndef STR_SET
24 # define STR_SET(STR, SET) \
25 { \
26 STR_FREE(STR); \
27 STR = SET; \
28 }
29 #endif
30 #ifndef TSRMLS_D
31 # define TSRMLS_D
32 # define TSRMLS_DC
33 # define TSRMLS_CC
34 # define TSRMLS_C
35 #endif
36 #ifdef PHP_ATTRIBUTE_FORMAT
37 # define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b) PHP_ATTRIBUTE_FORMAT(f, a, b)
38 #else
39 # define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b)
40 #endif
41 #ifndef pemalloc
42 # define pemalloc(s,p) malloc(s)
43 # define pefree(x,p) free(x)
44 # define perealloc(x,s,p) realloc(x,s)
45 # define perealloc_recoverable perealloc
46 # define ecalloc calloc
47 static inline void *estrndup(void *p, size_t s)
48 {
49 char *r = (char *) malloc(s+1);
50 if (r) memcpy((void *) r, p, s), r[s] = '\0';
51 return (void *) r;
52 }
53 #endif
54
55 #if defined(PHP_WIN32)
56 # if defined(PHP_HTTP_BUFFER_EXPORTS)
57 # define PHP_HTTP_BUFFER_API __declspec(dllexport)
58 # elif defined(COMPILE_DL_PHP_HTTP_BUFFER)
59 # define PHP_HTTP_BUFFER_API __declspec(dllimport)
60 # else
61 # define PHP_HTTP_BUFFER_API
62 # endif
63 #else
64 # define PHP_HTTP_BUFFER_API
65 #endif
66
67 #define PHP_HTTP_BUFFER(p) ((php_http_buffer_t *) (p))
68 #define PHP_HTTP_BUFFER_VAL(p) (PHP_HTTP_BUFFER(p))->data
69 #define PHP_HTTP_BUFFER_LEN(p) (PHP_HTTP_BUFFER(p))->used
70
71 #define FREE_PHP_HTTP_BUFFER_PTR(STR) pefree(STR, STR->pmem)
72 #define FREE_PHP_HTTP_BUFFER_VAL(STR) php_http_buffer_dtor(STR)
73 #define FREE_PHP_HTTP_BUFFER_ALL(STR) php_http_buffer_free(&(STR))
74 #define FREE_PHP_HTTP_BUFFER(free, STR) \
75 switch (free) \
76 { \
77 case PHP_HTTP_BUFFER_FREE_NOT: break; \
78 case PHP_HTTP_BUFFER_FREE_PTR: pefree(STR, STR->pmem); break; \
79 case PHP_HTTP_BUFFER_FREE_VAL: php_http_buffer_dtor(STR); break; \
80 case PHP_HTTP_BUFFER_FREE_ALL: \
81 { \
82 php_http_buffer_t *PTR = (STR); \
83 php_http_buffer_free(&PTR); \
84 } \
85 break; \
86 default: break; \
87 }
88
89 #define RETURN_PHP_HTTP_BUFFER_PTR(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
90 #define RETURN_PHP_HTTP_BUFFER_VAL(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
91 #define RETURN_PHP_HTTP_BUFFER_DUP(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
92 #define RETVAL_PHP_HTTP_BUFFER_PTR(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
93 #define RETVAL_PHP_HTTP_BUFFER_VAL(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
94 #define RETVAL_PHP_HTTP_BUFFER_DUP(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
95 /* RETURN_PHP_HTTP_BUFFER(buf, PHP_HTTP_BUFFER_FREE_PTR, 0) */
96 #define RETURN_PHP_HTTP_BUFFER(STR, free, dup) \
97 RETVAL_PHP_HTTP_BUFFER((STR), (free), (dup)); \
98 return;
99
100 #define RETVAL_PHP_HTTP_BUFFER(STR, free, dup) \
101 php_http_buffer_fix(STR); \
102 RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
103 FREE_PHP_HTTP_BUFFER((free), (STR));
104
105 typedef struct php_http_buffer {
106 char *data;
107 size_t used;
108 size_t free;
109 size_t size;
110 unsigned pmem:1;
111 unsigned reserved:31;
112 } php_http_buffer_t;
113
114 typedef enum php_http_buffer_free {
115 PHP_HTTP_BUFFER_FREE_NOT = 0,
116 PHP_HTTP_BUFFER_FREE_PTR, /* pefree() */
117 PHP_HTTP_BUFFER_FREE_VAL, /* php_http_buffer_dtor() */
118 PHP_HTTP_BUFFER_FREE_ALL /* php_http_buffer_free() */
119 } php_http_buffer_free_t;
120
121 #define PHP_HTTP_BUFFER_ALL_FREE(STR) PHP_HTTP_BUFFER_FREE_ALL,(STR)
122 #define PHP_HTTP_BUFFER_PTR_FREE(STR) PHP_HTTP_BUFFER_FREE_PTR,(STR)
123 #define PHP_HTTP_BUFFER_VAL_FREE(STR) PHP_HTTP_BUFFER_FREE_VAL,(STR)
124 #define PHP_HTTP_BUFFER_NOT_FREE(STR) PHP_HTTP_BUFFER_FREE_NOT,(STR)
125
126 #define PHP_HTTP_BUFFER_INIT_PREALLOC 0x01
127 #define PHP_HTTP_BUFFER_INIT_PERSISTENT 0x02
128
129 /* create a new php_http_buffer_t */
130 #define php_http_buffer_new() php_http_buffer_init(NULL)
131 #define php_http_buffer_init(b) php_http_buffer_init_ex(b, PHP_HTTP_BUFFER_DEFAULT_SIZE, 0)
132 #define php_http_buffer_clone(from, to) php_http_buffer_init_ex((to), (from)->size, (from)->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0)
133 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, int flags);
134
135 /* create a php_http_buffer_t from a zval or c-string */
136 #define php_http_buffer_from_zval(z) php_http_buffer_from_string(Z_STRVAL(z), Z_STRLEN(z))
137 #define php_http_buffer_from_zval_ex(b, z) php_http_buffer_from_string_ex(b, Z_STRVAL(z), Z_STRLEN(z))
138 #define php_http_buffer_from_string(s, l) php_http_buffer_from_string_ex(NULL, (s), (l))
139 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_from_string_ex(php_http_buffer_t *buf, const char *string, size_t length);
140
141 /* usually only called from within the internal functions */
142 #define php_http_buffer_resize(b, s) php_http_buffer_resize_ex((b), (s), 0, 0)
143 PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(php_http_buffer_t *buf, size_t len, size_t override_size, int allow_error);
144
145 /* shrink memory chunk to actually used size (+1) */
146 PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf);
147
148 /* append data to the php_http_buffer_t */
149 #define php_http_buffer_appends(b, a) php_http_buffer_append((b), (a), sizeof(a)-1)
150 #define php_http_buffer_appendl(b, a) php_http_buffer_append((b), (a), strlen(a))
151 PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len);
152 PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
153
154 /* insert data at a specific position of the php_http_buffer_t */
155 #define php_http_buffer_inserts(b, i, o) php_http_buffer_insert((b), (i), sizeof(i)-1, (o))
156 #define php_http_buffer_insertl(b, i, o) php_http_buffer_insert((b), (i), strlen(i), (o))
157 PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset);
158 PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 3, 4);
159
160 /* prepend data */
161 #define php_http_buffer_prepends(b, p) php_http_buffer_prepend((b), (p), sizeof(p)-1)
162 #define php_http_buffer_prependl(b, p) php_http_buffer_prepend((b), (p), strlen(p))
163 PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len);
164 PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
165
166 /* get a zero-terminated string */
167 PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len);
168
169 /* get a part of the php_http_buffer_t */
170 #define php_http_buffer_mid(b, o, l) php_http_buffer_sub((b), (o), (l))
171 #define php_http_buffer_left(b, l) php_http_buffer_sub((b), 0, (l))
172 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length);
173 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t len);
174
175 /* remove a substring */
176 PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length);
177
178 /* get a complete php_http_buffer_t duplicate */
179 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to);
180
181 /* merge several php_http_buffer_t objects
182 use like:
183
184 php_http_buffer_t *final = php_http_buffer_merge(3,
185 PHP_HTTP_BUFFER_NOT_FREE(&keep),
186 PHP_HTTP_BUFFER_ALL_FREE(middle_ptr),
187 PHP_HTTP_BUFFER_VAL_FREE(&local);
188 */
189 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...);
190 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...);
191 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv);
192
193 /* sets a trailing NUL byte */
194 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf);
195
196 /* memcmp for php_http_buffer_t objects */
197 PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right);
198
199 /* reset php_http_buffer_t object */
200 PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf);
201
202 /* free a php_http_buffer_t objects contents */
203 PHP_HTTP_BUFFER_API void php_http_buffer_dtor(php_http_buffer_t *buf);
204
205 /* free a php_http_buffer_t object completely */
206 PHP_HTTP_BUFFER_API void php_http_buffer_free(php_http_buffer_t **buf);
207
208 /* stores data in a php_http_buffer_t until it reaches chunk_size */
209 PHP_HTTP_BUFFER_API size_t php_http_buffer_chunk_buffer(php_http_buffer_t **s, const char *data, size_t data_len, char **chunk, size_t chunk_size);
210
211 typedef size_t (*php_http_buffer_pass_func_t)(void *opaque, const char *, size_t TSRMLS_DC);
212
213 /* wrapper around php_http_buffer_chunk_buffer, which passes available chunks to passthru() */
214 PHP_HTTP_BUFFER_API void php_http_buffer_chunked_output(php_http_buffer_t **s, const char *data, size_t data_len, size_t chunk_size, php_http_buffer_pass_func_t passout, void *opaque TSRMLS_DC);
215
216 /* write chunks directly into php_http_buffer_t buffer */
217 PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque TSRMLS_DC);
218
219 #endif
220
221
222 /*
223 * Local variables:
224 * tab-width: 4
225 * c-basic-offset: 4
226 * End:
227 * vim600: sw=4 ts=4 fdm=marker
228 * vim<600: sw=4 ts=4
229 */