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