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
)
38 if ((buf
= php_http_buffer_init(buf
))) {
39 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(buf
, str
, len
)) {
40 pefree(buf
, buf
->pmem
);
47 PHP_HTTP_BUFFER_API
size_t php_http_buffer_resize_ex(
48 php_http_buffer_t
*buf
, size_t len
, size_t override_size
,
49 zend_bool allow_error
)
53 fprintf(stderr
, "RESIZE: len=%lu, size=%lu, used=%lu, free=%lu\n", len
, buf
->size
, buf
->used
, buf
->free
);
55 if (buf
->free
< len
) {
56 size_t size
= override_size
? override_size
: buf
->size
;
58 while ((size
+ buf
->free
) < len
) {
63 ptr
= perealloc_recoverable(buf
->data
,
64 buf
->used
+ buf
->free
+ size
, buf
->pmem
);
66 ptr
= perealloc(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
72 return PHP_HTTP_BUFFER_NOMEM
;
81 PHP_HTTP_BUFFER_API
char *php_http_buffer_account(
82 php_http_buffer_t
*buf
, size_t to_account
)
84 /* it's probably already too late but check anyway */
85 if (to_account
> buf
->free
) {
89 buf
->free
-= to_account
;
90 buf
->used
+= to_account
;
92 return buf
->data
+ buf
->used
;
95 PHP_HTTP_BUFFER_API
size_t php_http_buffer_shrink(php_http_buffer_t
*buf
)
97 /* avoid another realloc on fixation */
99 char *ptr
= perealloc(buf
->data
, buf
->used
+ 1, buf
->pmem
);
104 return PHP_HTTP_BUFFER_NOMEM
;
111 PHP_HTTP_BUFFER_API
size_t php_http_buffer_append(php_http_buffer_t
*buf
,
112 const char *append
, size_t append_len
)
114 if ( buf
->free
< append_len
&&
115 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, append_len
)
117 return PHP_HTTP_BUFFER_NOMEM
;
119 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
120 buf
->used
+= append_len
;
121 buf
->free
-= append_len
;
125 PHP_HTTP_BUFFER_API
size_t php_http_buffer_appendf(php_http_buffer_t
*buf
,
126 const char *format
, ...)
130 size_t append_len
, alloc
;
132 va_start(argv
, format
);
133 append_len
= vspprintf(&append
, 0, format
, argv
);
136 alloc
= php_http_buffer_append(buf
, append
, append_len
);
139 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
140 return PHP_HTTP_BUFFER_NOMEM
;
145 PHP_HTTP_BUFFER_API
char *php_http_buffer_data(const php_http_buffer_t
*buf
,
146 char **into
, size_t *len
)
148 char *copy
= ecalloc(1, buf
->used
+ 1);
149 memcpy(copy
, buf
->data
, buf
->used
);
159 PHP_HTTP_BUFFER_API
size_t php_http_buffer_cut(php_http_buffer_t
*buf
,
160 size_t offset
, size_t length
)
162 if (offset
> buf
->used
) {
165 if (offset
+ length
> buf
->used
) {
166 length
= buf
->used
- offset
;
168 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
,
169 buf
->used
- length
- offset
);
175 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_fix(
176 php_http_buffer_t
*buf
)
178 if ( buf
->free
< 1 &&
179 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(buf
, 1, 1, 0)
183 buf
->data
[buf
->used
] = '\0';
187 PHP_HTTP_BUFFER_API
void php_http_buffer_reset(php_http_buffer_t
*buf
)
189 buf
->free
+= buf
->used
;
193 PHP_HTTP_BUFFER_API
void php_http_buffer_dtor(php_http_buffer_t
*buf
)
196 pefree(buf
->data
, buf
->pmem
);
203 PHP_HTTP_BUFFER_API
void php_http_buffer_free(php_http_buffer_t
**buf
)
206 php_http_buffer_dtor(*buf
);
207 pefree(*buf
, (*buf
)->pmem
);
212 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunk_buffer(php_http_buffer_t
**s
,
213 const char *data
, size_t data_len
, char **chunk
, size_t chunk_size
)
215 php_http_buffer_t
*storage
;
220 *s
= php_http_buffer_init_ex(NULL
, chunk_size
<< 1,
221 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
226 php_http_buffer_append(storage
, data
, data_len
);
230 php_http_buffer_data(storage
, chunk
, &chunk_size
);
231 php_http_buffer_free(s
);
235 if (storage
->used
>= chunk_size
) {
236 *chunk
= estrndup(storage
->data
, chunk_size
);
237 php_http_buffer_cut(storage
, 0, chunk_size
);
244 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunked_output(php_http_buffer_t
**s
,
245 const char *data
, size_t data_len
, size_t chunk_len
,
246 php_http_buffer_pass_func_t passout
, void *opaque
)
249 size_t passed
= 0, got
= 0;
251 while ((got
= php_http_buffer_chunk_buffer(s
, data
, data_len
, &chunk
, chunk_len
))) {
252 if (PHP_HTTP_BUFFER_PASS0
== passout(opaque
, chunk
, got
)) {
253 PTR_SET(chunk
, NULL
);
254 return PHP_HTTP_BUFFER_PASS0
;
258 /* we already got the last chunk,
259 and freed all resources */
264 PTR_SET(chunk
, NULL
);
270 PHP_HTTP_BUFFER_API ssize_t
php_http_buffer_passthru(php_http_buffer_t
**s
, size_t chunk_size
,
271 php_http_buffer_pass_func_t passin
, void *passin_arg
,
272 php_http_buffer_pass_func_t passon
, void *passon_arg
)
274 size_t passed_on
= 0, passed_in
;
276 passed_in
= php_http_buffer_chunked_input(s
, chunk_size
, passin
, passin_arg
);
278 if (passed_in
== PHP_HTTP_BUFFER_PASS0
) {
281 if (passed_in
|| (*s
)->used
) {
282 passed_on
= passon(passon_arg
, (*s
)->data
, (*s
)->used
);
284 if (passed_on
== PHP_HTTP_BUFFER_PASS0
) {
289 php_http_buffer_cut(*s
, 0, passed_on
);
293 return passed_on
- passed_in
;
296 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunked_input(php_http_buffer_t
**s
,
297 size_t chunk_size
, php_http_buffer_pass_func_t passin
, void *opaque
)
299 php_http_buffer_t
*str
;
303 *s
= php_http_buffer_init_ex(NULL
, chunk_size
,
304 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
308 php_http_buffer_resize(str
, chunk_size
);
309 passed
= passin(opaque
, str
->data
+ str
->used
, chunk_size
);
311 if (passed
!= PHP_HTTP_BUFFER_PASS0
) {
316 php_http_buffer_fix(str
);
321 #ifdef PHP_HTTP_BUFFER_EXTENDED
323 PHP_HTTP_BUFFER_API
int php_http_buffer_cmp(php_http_buffer_t
*left
,
324 php_http_buffer_t
*right
)
326 if (left
->used
> right
->used
) {
328 } else if (right
->used
> left
->used
) {
331 return memcmp(left
->data
, right
->data
, left
->used
);
335 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_copy(
336 const php_http_buffer_t
*from
, php_http_buffer_t
*to
)
340 to
= php_http_buffer_clone(from
, to
);
342 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(to
, from
->data
, from
->used
)) {
344 php_http_buffer_free(&to
);
346 php_http_buffer_dtor(to
);
352 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insert(php_http_buffer_t
*buf
,
353 const char *insert
, size_t insert_len
, size_t offset
)
355 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, insert_len
)) {
356 return PHP_HTTP_BUFFER_NOMEM
;
358 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
359 memcpy(buf
->data
+ offset
, insert
, insert_len
);
360 buf
->used
+= insert_len
;
361 buf
->free
-= insert_len
;
365 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insertf(php_http_buffer_t
*buf
,
366 size_t offset
, const char *format
, ...)
370 size_t insert_len
, alloc
;
372 va_start(argv
, format
);
373 insert_len
= vspprintf(&insert
, 0, format
, argv
);
376 alloc
= php_http_buffer_insert(buf
, insert
, insert_len
, offset
);
379 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
380 return PHP_HTTP_BUFFER_NOMEM
;
385 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prepend(php_http_buffer_t
*buf
,
386 const char *prepend
, size_t prepend_len
)
388 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, prepend_len
)) {
389 return PHP_HTTP_BUFFER_NOMEM
;
391 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
392 memcpy(buf
->data
, prepend
, prepend_len
);
393 buf
->used
+= prepend_len
;
394 buf
->free
-= prepend_len
;
398 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prependf(php_http_buffer_t
*buf
,
399 const char *format
, ...)
403 size_t prepend_len
, alloc
;
405 va_start(argv
, format
);
406 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
409 alloc
= php_http_buffer_prepend(buf
, prepend
, prepend_len
);
412 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
413 return PHP_HTTP_BUFFER_NOMEM
;
418 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_sub(
419 const php_http_buffer_t
*buf
, size_t offset
, size_t length
)
421 if (offset
>= buf
->used
) {
424 php_http_buffer_t
*sub
;
425 size_t need
= 1 + ((length
+ offset
) > buf
->used
?
426 (buf
->used
- offset
) : (length
- offset
));
427 unsigned flags
= buf
->pmem
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0;
429 sub
= php_http_buffer_init_ex(NULL
, need
,
430 PHP_HTTP_BUFFER_INIT_PREALLOC
| flags
);
433 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(sub
, buf
->data
+ offset
, need
)) {
434 php_http_buffer_free(&sub
);
436 sub
->size
= buf
->size
;
443 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_right(
444 const php_http_buffer_t
*buf
, size_t length
)
446 if (length
< buf
->used
) {
447 return php_http_buffer_sub(buf
, buf
->used
- length
, length
);
449 return php_http_buffer_sub(buf
, 0, buf
->used
);
454 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_va(
455 php_http_buffer_t
*buf
, unsigned argc
, va_list argv
)
458 buf
= php_http_buffer_init(buf
);
462 php_http_buffer_free_t f
= va_arg(argv
, php_http_buffer_free_t
);
463 php_http_buffer_t
*current
= va_arg(argv
, php_http_buffer_t
*);
464 php_http_buffer_append(buf
, current
->data
, current
->used
);
465 FREE_PHP_HTTP_BUFFER(f
, current
);
472 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_ex(
473 php_http_buffer_t
*buf
, unsigned argc
, ...)
476 php_http_buffer_t
*ret
;
478 va_start(argv
, argc
);
479 ret
= php_http_buffer_merge_va(buf
, argc
, argv
);
484 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge(unsigned argc
, ...)
487 php_http_buffer_t
*ret
;
489 va_start(argv
, argc
);
490 ret
= php_http_buffer_merge_va(NULL
, argc
, argv
);
502 * vim600: sw=4 ts=4 fdm=marker