use the params parser for query strings
[m6w6/ext-http] / php_http_buffer.c
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 #include <php.h>
14 #include "php_http_buffer.h"
15
16 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, int flags)
17 {
18 if (!buf) {
19 buf = pemalloc(sizeof(*buf), flags & PHP_HTTP_BUFFER_INIT_PERSISTENT);
20 }
21
22 if (buf) {
23 buf->size = (chunk_size) ? chunk_size : PHP_HTTP_BUFFER_DEFAULT_SIZE;
24 buf->pmem = (flags & PHP_HTTP_BUFFER_INIT_PERSISTENT) ? 1 : 0;
25 buf->data = (flags & PHP_HTTP_BUFFER_INIT_PREALLOC) ? pemalloc(buf->size, buf->pmem) : NULL;
26 buf->free = (flags & PHP_HTTP_BUFFER_INIT_PREALLOC) ? buf->size : 0;
27 buf->used = 0;
28 }
29
30 return buf;
31 }
32
33 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)
34 {
35 if ((buf = php_http_buffer_init(buf))) {
36 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(buf, string, length)) {
37 pefree(buf, buf->pmem);
38 buf = NULL;
39 }
40 }
41 return buf;
42 }
43
44 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)
45 {
46 char *ptr = NULL;
47 #if 0
48 fprintf(stderr, "RESIZE: len=%lu, size=%lu, used=%lu, free=%lu\n", len, buf->size, buf->used, buf->free);
49 #endif
50 if (buf->free < len) {
51 size_t size = override_size ? override_size : buf->size;
52
53 while ((size + buf->free) < len) {
54 size <<= 1;
55 }
56
57 if (allow_error) {
58 ptr = perealloc_recoverable(buf->data, buf->used + buf->free + size, buf->pmem);
59 } else {
60 ptr = perealloc(buf->data, buf->used + buf->free + size, buf->pmem);
61 }
62
63 if (ptr) {
64 buf->data = ptr;
65 } else {
66 return PHP_HTTP_BUFFER_NOMEM;
67 }
68
69 buf->free += size;
70 return size;
71 }
72 return 0;
73 }
74
75 PHP_HTTP_BUFFER_API char *php_http_buffer_account(php_http_buffer_t *buf, size_t to_account)
76 {
77 /* it's probably already too late but check anyway */
78 if (to_account > buf->free) {
79 return NULL;
80 }
81
82 buf->free -= to_account;
83 buf->used += to_account;
84
85 return buf->data + buf->used;
86 }
87
88 PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf)
89 {
90 /* avoid another realloc on fixation */
91 if (buf->free > 1) {
92 char *ptr = perealloc(buf->data, buf->used + 1, buf->pmem);
93
94 if (ptr) {
95 buf->data = ptr;
96 } else {
97 return PHP_HTTP_BUFFER_NOMEM;
98 }
99 buf->free = 1;
100 }
101 return buf->used;
102 }
103
104 PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len)
105 {
106 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, append_len)) {
107 return PHP_HTTP_BUFFER_NOMEM;
108 }
109 memcpy(buf->data + buf->used, append, append_len);
110 buf->used += append_len;
111 buf->free -= append_len;
112 return append_len;
113 }
114
115 PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...)
116 {
117 va_list argv;
118 char *append;
119 size_t append_len, alloc;
120
121 va_start(argv, format);
122 append_len = vspprintf(&append, 0, format, argv);
123 va_end(argv);
124
125 alloc = php_http_buffer_append(buf, append, append_len);
126 efree(append);
127
128 if (PHP_HTTP_BUFFER_NOMEM == alloc) {
129 return PHP_HTTP_BUFFER_NOMEM;
130 }
131 return append_len;
132 }
133
134 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)
135 {
136 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, insert_len)) {
137 return PHP_HTTP_BUFFER_NOMEM;
138 }
139 memmove(buf->data + offset + insert_len, buf->data + offset, insert_len);
140 memcpy(buf->data + offset, insert, insert_len);
141 buf->used += insert_len;
142 buf->free -= insert_len;
143 return insert_len;
144 }
145
146 PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...)
147 {
148 va_list argv;
149 char *insert;
150 size_t insert_len, alloc;
151
152 va_start(argv, format);
153 insert_len = vspprintf(&insert, 0, format, argv);
154 va_end(argv);
155
156 alloc = php_http_buffer_insert(buf, insert, insert_len, offset);
157 efree(insert);
158
159 if (PHP_HTTP_BUFFER_NOMEM == alloc) {
160 return PHP_HTTP_BUFFER_NOMEM;
161 }
162 return insert_len;
163 }
164
165 PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len)
166 {
167 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, prepend_len)) {
168 return PHP_HTTP_BUFFER_NOMEM;
169 }
170 memmove(buf->data + prepend_len, buf->data, buf->used);
171 memcpy(buf->data, prepend, prepend_len);
172 buf->used += prepend_len;
173 buf->free -= prepend_len;
174 return prepend_len;
175 }
176
177 PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...)
178 {
179 va_list argv;
180 char *prepend;
181 size_t prepend_len, alloc;
182
183 va_start(argv, format);
184 prepend_len = vspprintf(&prepend, 0, format, argv);
185 va_end(argv);
186
187 alloc = php_http_buffer_prepend(buf, prepend, prepend_len);
188 efree(prepend);
189
190 if (PHP_HTTP_BUFFER_NOMEM == alloc) {
191 return PHP_HTTP_BUFFER_NOMEM;
192 }
193 return prepend_len;
194 }
195
196 PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len)
197 {
198 char *copy = ecalloc(1, buf->used + 1);
199 memcpy(copy, buf->data, buf->used);
200 if (into) {
201 *into = copy;
202 }
203 if (len) {
204 *len = buf->used;
205 }
206 return copy;
207 }
208
209 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to)
210 {
211 int free_to = !to;
212
213 to = php_http_buffer_clone(from, to);
214
215 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(to, from->data, from->used)) {
216 if (free_to) {
217 php_http_buffer_free(&to);
218 } else {
219 php_http_buffer_dtor(to);
220 }
221 }
222 return to;
223 }
224
225 PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length)
226 {
227 if (offset > buf->used) {
228 return 0;
229 }
230 if (offset + length > buf->used) {
231 length = buf->used - offset;
232 }
233 memmove(buf->data + offset, buf->data + offset + length, buf->used - length - offset);
234 buf->used -= length;
235 buf->free += length;
236 return length;
237 }
238
239 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t length)
240 {
241 if (offset >= buf->used) {
242 return NULL;
243 } else {
244 size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset));
245 php_http_buffer_t *sub = php_http_buffer_init_ex(NULL, need, PHP_HTTP_BUFFER_INIT_PREALLOC | (buf->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0));
246 if (sub) {
247 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(sub, buf->data + offset, need)) {
248 php_http_buffer_free(&sub);
249 } else {
250 sub->size = buf->size;
251 }
252 }
253 return sub;
254 }
255 }
256
257 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length)
258 {
259 if (length < buf->used) {
260 return php_http_buffer_sub(buf, buf->used - length, length);
261 } else {
262 return php_http_buffer_sub(buf, 0, buf->used);
263 }
264 }
265
266
267 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv)
268 {
269 unsigned i = 0;
270 buf = php_http_buffer_init(buf);
271
272 if (buf) {
273 while (argc > i++) {
274 php_http_buffer_free_t f = va_arg(argv, php_http_buffer_free_t);
275 php_http_buffer_t *current = va_arg(argv, php_http_buffer_t *);
276 php_http_buffer_append(buf, current->data, current->used);
277 FREE_PHP_HTTP_BUFFER(f, current);
278 }
279 }
280
281 return buf;
282 }
283
284 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...)
285 {
286 va_list argv;
287 php_http_buffer_t *ret;
288
289 va_start(argv, argc);
290 ret = php_http_buffer_merge_va(buf, argc, argv);
291 va_end(argv);
292 return ret;
293 }
294
295 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...)
296 {
297 va_list argv;
298 php_http_buffer_t *ret;
299
300 va_start(argv, argc);
301 ret = php_http_buffer_merge_va(NULL, argc, argv);
302 va_end(argv);
303 return ret;
304 }
305
306 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf)
307 {
308 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(buf, 1, 1, 0)) {
309 return NULL;
310 }
311 buf->data[buf->used] = '\0';
312 return buf;
313 }
314
315 PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right)
316 {
317 if (left->used > right->used) {
318 return -1;
319 } else if (right->used > left->used) {
320 return 1;
321 } else {
322 return memcmp(left->data, right->data, left->used);
323 }
324 }
325
326 PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf)
327 {
328 buf->free += buf->used;
329 buf->used = 0;
330 }
331
332 PHP_HTTP_BUFFER_API void php_http_buffer_dtor(php_http_buffer_t *buf)
333 {
334 if (buf->data) {
335 pefree(buf->data, buf->pmem);
336 buf->data = NULL;
337 }
338 buf->used = 0;
339 buf->free = 0;
340 }
341
342 PHP_HTTP_BUFFER_API void php_http_buffer_free(php_http_buffer_t **buf)
343 {
344 if (*buf) {
345 php_http_buffer_dtor(*buf);
346 pefree(*buf, (*buf)->pmem);
347 *buf = NULL;
348 }
349 }
350
351 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)
352 {
353 php_http_buffer_t *storage;
354
355 *chunk = NULL;
356
357 if (!*s) {
358 *s = php_http_buffer_init_ex(NULL, chunk_size << 1, chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
359 }
360 storage = *s;
361
362 if (data_len) {
363 php_http_buffer_append(storage, data, data_len);
364 }
365
366 if (!chunk_size) {
367 php_http_buffer_data(storage, chunk, &chunk_size);
368 php_http_buffer_free(s);
369 return chunk_size;
370 }
371
372 if (storage->used >= chunk_size) {
373 *chunk = estrndup(storage->data, chunk_size);
374 php_http_buffer_cut(storage, 0, chunk_size);
375 return chunk_size;
376 }
377
378 return 0;
379 }
380
381 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_len, php_http_buffer_pass_func_t passout, void *opaque TSRMLS_DC)
382 {
383 char *chunk = NULL;
384 size_t got = 0;
385
386 while ((got = php_http_buffer_chunk_buffer(s, data, data_len, &chunk, chunk_len))) {
387 passout(opaque, chunk, got TSRMLS_CC);
388 if (!chunk_len) {
389 /* we already got the last chunk,
390 and freed all resources */
391 break;
392 }
393 data = NULL;
394 data_len = 0;
395 STR_SET(chunk, NULL);
396 }
397 STR_FREE(chunk);
398 }
399
400 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)
401 {
402 size_t passed_on = 0, passed_in = php_http_buffer_chunked_input(s, chunk_size, passin, passin_arg TSRMLS_CC);
403
404 if (passed_in == PHP_HTTP_BUFFER_PASS0) {
405 return passed_in;
406 }
407 if (passed_in || (*s)->used) {
408 passed_on = passon(passon_arg, (*s)->data, (*s)->used TSRMLS_CC);
409
410 if (passed_on == PHP_HTTP_BUFFER_PASS0) {
411 return passed_on;
412 }
413
414 if (passed_on) {
415 php_http_buffer_cut(*s, 0, passed_on);
416 }
417 }
418
419 return passed_on - passed_in;
420 }
421
422 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)
423 {
424 php_http_buffer_t *str;
425 size_t passed;
426
427 if (!*s) {
428 *s = php_http_buffer_init_ex(NULL, chunk_size, chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
429 }
430 str = *s;
431
432 php_http_buffer_resize(str, chunk_size);
433 passed = passin(opaque, str->data + str->used, chunk_size TSRMLS_CC);
434
435 if (passed != PHP_HTTP_BUFFER_PASS0) {
436 str->used += passed;
437 str->free -= passed;
438 }
439
440 php_http_buffer_fix(str);
441
442 return passed;
443 }
444
445 /*
446 * Local variables:
447 * tab-width: 4
448 * c-basic-offset: 4
449 * End:
450 * vim600: sw=4 ts=4 fdm=marker
451 * vim<600: sw=4 ts=4
452 */
453