2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
14 #include "php_http_buffer.h"
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
)
20 buf
= pemalloc(sizeof(*buf
), flags
& PHP_HTTP_BUFFER_INIT_PERSISTENT
);
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;
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
)
40 if ((buf
= php_http_buffer_init(buf
))) {
41 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(buf
, str
, len
)) {
43 pefree(buf
, buf
->pmem
);
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
)
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
);
59 if (buf
->free
< len
) {
60 size_t size
= override_size
? override_size
: buf
->size
;
62 while ((size
+ buf
->free
) < len
) {
67 ptr
= perealloc_recoverable(buf
->data
,
68 buf
->used
+ buf
->free
+ size
, buf
->pmem
);
70 ptr
= perealloc(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
76 return PHP_HTTP_BUFFER_NOMEM
;
85 PHP_HTTP_BUFFER_API
char *php_http_buffer_account(
86 php_http_buffer_t
*buf
, size_t to_account
)
88 assert(to_account
<= buf
->free
);
90 buf
->free
-= to_account
;
91 buf
->used
+= to_account
;
93 return buf
->data
+ buf
->used
;
96 PHP_HTTP_BUFFER_API
size_t php_http_buffer_shrink(php_http_buffer_t
*buf
)
98 /* avoid another realloc on fixation */
100 char *ptr
= perealloc(buf
->data
, buf
->used
+ 1, buf
->pmem
);
105 return PHP_HTTP_BUFFER_NOMEM
;
112 PHP_HTTP_BUFFER_API
size_t php_http_buffer_append(php_http_buffer_t
*buf
,
113 const char *append
, size_t append_len
)
115 if ( buf
->free
< append_len
&&
116 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, append_len
)
118 return PHP_HTTP_BUFFER_NOMEM
;
120 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
121 buf
->used
+= append_len
;
122 buf
->free
-= append_len
;
126 PHP_HTTP_BUFFER_API
size_t php_http_buffer_appendf(php_http_buffer_t
*buf
,
127 const char *format
, ...)
131 size_t append_len
, alloc
;
133 va_start(argv
, format
);
134 append_len
= vspprintf(&append
, 0, format
, argv
);
137 alloc
= php_http_buffer_append(buf
, append
, append_len
);
140 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
141 return PHP_HTTP_BUFFER_NOMEM
;
146 PHP_HTTP_BUFFER_API
char *php_http_buffer_data(const php_http_buffer_t
*buf
,
147 char **into
, size_t *len
)
149 char *copy
= ecalloc(1, buf
->used
+ 1);
150 memcpy(copy
, buf
->data
, buf
->used
);
160 PHP_HTTP_BUFFER_API
size_t php_http_buffer_cut(php_http_buffer_t
*buf
,
161 size_t offset
, size_t length
)
163 if (offset
> buf
->used
) {
166 if (offset
+ length
> buf
->used
) {
167 length
= buf
->used
- offset
;
169 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
,
170 buf
->used
- length
- offset
);
176 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_fix(
177 php_http_buffer_t
*buf
)
179 if ( buf
->free
< 1 &&
180 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(buf
, 1, 1, 0)
184 buf
->data
[buf
->used
] = '\0';
188 PHP_HTTP_BUFFER_API
void php_http_buffer_reset(php_http_buffer_t
*buf
)
190 buf
->free
+= buf
->used
;
194 PHP_HTTP_BUFFER_API
void php_http_buffer_dtor(php_http_buffer_t
*buf
)
197 pefree(buf
->data
, buf
->pmem
);
204 PHP_HTTP_BUFFER_API
void php_http_buffer_free(php_http_buffer_t
**buf
)
207 php_http_buffer_dtor(*buf
);
208 pefree(*buf
, (*buf
)->pmem
);
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
)
216 php_http_buffer_t
*storage
;
221 *s
= php_http_buffer_init_ex(NULL
, chunk_size
<< 1,
222 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
227 php_http_buffer_append(storage
, data
, data_len
);
231 php_http_buffer_data(storage
, chunk
, &chunk_size
);
232 php_http_buffer_free(s
);
236 if (storage
->used
>= chunk_size
) {
237 *chunk
= estrndup(storage
->data
, chunk_size
);
238 php_http_buffer_cut(storage
, 0, chunk_size
);
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
)
250 size_t passed
= 0, got
= 0;
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
;
259 /* we already got the last chunk,
260 and freed all resources */
265 PTR_SET(chunk
, NULL
);
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
)
275 size_t passed_on
= 0, passed_in
;
277 passed_in
= php_http_buffer_chunked_input(s
, chunk_size
, passin
, passin_arg
);
279 if (passed_in
== PHP_HTTP_BUFFER_PASS0
) {
282 if (passed_in
|| (*s
)->used
) {
283 passed_on
= passon(passon_arg
, (*s
)->data
, (*s
)->used
);
285 if (passed_on
== PHP_HTTP_BUFFER_PASS0
) {
290 php_http_buffer_cut(*s
, 0, passed_on
);
294 return passed_on
- passed_in
;
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
)
300 php_http_buffer_t
*str
;
304 *s
= php_http_buffer_init_ex(NULL
, chunk_size
,
305 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
309 php_http_buffer_resize(str
, chunk_size
);
310 passed
= passin(opaque
, str
->data
+ str
->used
, chunk_size
);
312 if (passed
!= PHP_HTTP_BUFFER_PASS0
) {
317 php_http_buffer_fix(str
);
322 #ifdef PHP_HTTP_BUFFER_EXTENDED
324 PHP_HTTP_BUFFER_API
int php_http_buffer_cmp(php_http_buffer_t
*left
,
325 php_http_buffer_t
*right
)
327 if (left
->used
> right
->used
) {
329 } else if (right
->used
> left
->used
) {
332 return memcmp(left
->data
, right
->data
, left
->used
);
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
)
341 to
= php_http_buffer_clone(from
, to
);
343 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(to
, from
->data
, from
->used
)) {
345 php_http_buffer_free(&to
);
347 php_http_buffer_dtor(to
);
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
)
356 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, insert_len
)) {
357 return PHP_HTTP_BUFFER_NOMEM
;
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
;
366 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insertf(php_http_buffer_t
*buf
,
367 size_t offset
, const char *format
, ...)
371 size_t insert_len
, alloc
;
373 va_start(argv
, format
);
374 insert_len
= vspprintf(&insert
, 0, format
, argv
);
377 alloc
= php_http_buffer_insert(buf
, insert
, insert_len
, offset
);
380 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
381 return PHP_HTTP_BUFFER_NOMEM
;
386 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prepend(php_http_buffer_t
*buf
,
387 const char *prepend
, size_t prepend_len
)
389 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, prepend_len
)) {
390 return PHP_HTTP_BUFFER_NOMEM
;
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
;
399 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prependf(php_http_buffer_t
*buf
,
400 const char *format
, ...)
404 size_t prepend_len
, alloc
;
406 va_start(argv
, format
);
407 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
410 alloc
= php_http_buffer_prepend(buf
, prepend
, prepend_len
);
413 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
414 return PHP_HTTP_BUFFER_NOMEM
;
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
)
422 if (offset
>= buf
->used
) {
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;
430 sub
= php_http_buffer_init_ex(NULL
, need
,
431 PHP_HTTP_BUFFER_INIT_PREALLOC
| flags
);
434 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(sub
, buf
->data
+ offset
, need
)) {
435 php_http_buffer_free(&sub
);
437 sub
->size
= buf
->size
;
444 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_right(
445 const php_http_buffer_t
*buf
, size_t length
)
447 if (length
< buf
->used
) {
448 return php_http_buffer_sub(buf
, buf
->used
- length
, length
);
450 return php_http_buffer_sub(buf
, 0, buf
->used
);
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
)
459 buf
= php_http_buffer_init(buf
);
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
);
473 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_ex(
474 php_http_buffer_t
*buf
, unsigned argc
, ...)
477 php_http_buffer_t
*ret
;
479 va_start(argv
, argc
);
480 ret
= php_http_buffer_merge_va(buf
, argc
, argv
);
485 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge(unsigned argc
, ...)
488 php_http_buffer_t
*ret
;
490 va_start(argv
, argc
);
491 ret
= php_http_buffer_merge_va(NULL
, argc
, argv
);
503 * vim600: sw=4 ts=4 fdm=marker