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