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, total=%lu\n", len
, buf
->size
, buf
->used
, buf
->free
, buf
->free
+buf
->used
);
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 assert(to_account
<= buf
->free
);
86 buf
->free
-= to_account
;
87 buf
->used
+= to_account
;
89 return buf
->data
+ buf
->used
;
92 PHP_HTTP_BUFFER_API
size_t php_http_buffer_shrink(php_http_buffer_t
*buf
)
94 /* avoid another realloc on fixation */
96 char *ptr
= perealloc(buf
->data
, buf
->used
+ 1, buf
->pmem
);
101 return PHP_HTTP_BUFFER_NOMEM
;
108 PHP_HTTP_BUFFER_API
size_t php_http_buffer_append(php_http_buffer_t
*buf
,
109 const char *append
, size_t append_len
)
111 if ( buf
->free
< append_len
&&
112 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, append_len
)
114 return PHP_HTTP_BUFFER_NOMEM
;
116 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
117 buf
->used
+= append_len
;
118 buf
->free
-= append_len
;
122 PHP_HTTP_BUFFER_API
size_t php_http_buffer_appendf(php_http_buffer_t
*buf
,
123 const char *format
, ...)
127 size_t append_len
, alloc
;
129 va_start(argv
, format
);
130 append_len
= vspprintf(&append
, 0, format
, argv
);
133 alloc
= php_http_buffer_append(buf
, append
, append_len
);
136 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
137 return PHP_HTTP_BUFFER_NOMEM
;
142 PHP_HTTP_BUFFER_API
char *php_http_buffer_data(const php_http_buffer_t
*buf
,
143 char **into
, size_t *len
)
145 char *copy
= ecalloc(1, buf
->used
+ 1);
146 memcpy(copy
, buf
->data
, buf
->used
);
156 PHP_HTTP_BUFFER_API
size_t php_http_buffer_cut(php_http_buffer_t
*buf
,
157 size_t offset
, size_t length
)
159 if (offset
> buf
->used
) {
162 if (offset
+ length
> buf
->used
) {
163 length
= buf
->used
- offset
;
165 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
,
166 buf
->used
- length
- offset
);
172 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_fix(
173 php_http_buffer_t
*buf
)
175 if ( buf
->free
< 1 &&
176 PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(buf
, 1, 1, 0)
180 buf
->data
[buf
->used
] = '\0';
184 PHP_HTTP_BUFFER_API
void php_http_buffer_reset(php_http_buffer_t
*buf
)
186 buf
->free
+= buf
->used
;
190 PHP_HTTP_BUFFER_API
void php_http_buffer_dtor(php_http_buffer_t
*buf
)
193 pefree(buf
->data
, buf
->pmem
);
200 PHP_HTTP_BUFFER_API
void php_http_buffer_free(php_http_buffer_t
**buf
)
203 php_http_buffer_dtor(*buf
);
204 pefree(*buf
, (*buf
)->pmem
);
209 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunk_buffer(php_http_buffer_t
**s
,
210 const char *data
, size_t data_len
, char **chunk
, size_t chunk_size
)
212 php_http_buffer_t
*storage
;
217 *s
= php_http_buffer_init_ex(NULL
, chunk_size
<< 1,
218 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
223 php_http_buffer_append(storage
, data
, data_len
);
227 php_http_buffer_data(storage
, chunk
, &chunk_size
);
228 php_http_buffer_free(s
);
232 if (storage
->used
>= chunk_size
) {
233 *chunk
= estrndup(storage
->data
, chunk_size
);
234 php_http_buffer_cut(storage
, 0, chunk_size
);
241 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunked_output(php_http_buffer_t
**s
,
242 const char *data
, size_t data_len
, size_t chunk_len
,
243 php_http_buffer_pass_func_t passout
, void *opaque
)
246 size_t passed
= 0, got
= 0;
248 while ((got
= php_http_buffer_chunk_buffer(s
, data
, data_len
, &chunk
, chunk_len
))) {
249 if (PHP_HTTP_BUFFER_PASS0
== passout(opaque
, chunk
, got
)) {
250 PTR_SET(chunk
, NULL
);
251 return PHP_HTTP_BUFFER_PASS0
;
255 /* we already got the last chunk,
256 and freed all resources */
261 PTR_SET(chunk
, NULL
);
267 PHP_HTTP_BUFFER_API ssize_t
php_http_buffer_passthru(php_http_buffer_t
**s
, size_t chunk_size
,
268 php_http_buffer_pass_func_t passin
, void *passin_arg
,
269 php_http_buffer_pass_func_t passon
, void *passon_arg
)
271 size_t passed_on
= 0, passed_in
;
273 passed_in
= php_http_buffer_chunked_input(s
, chunk_size
, passin
, passin_arg
);
275 if (passed_in
== PHP_HTTP_BUFFER_PASS0
) {
278 if (passed_in
|| (*s
)->used
) {
279 passed_on
= passon(passon_arg
, (*s
)->data
, (*s
)->used
);
281 if (passed_on
== PHP_HTTP_BUFFER_PASS0
) {
286 php_http_buffer_cut(*s
, 0, passed_on
);
290 return passed_on
- passed_in
;
293 PHP_HTTP_BUFFER_API
size_t php_http_buffer_chunked_input(php_http_buffer_t
**s
,
294 size_t chunk_size
, php_http_buffer_pass_func_t passin
, void *opaque
)
296 php_http_buffer_t
*str
;
300 *s
= php_http_buffer_init_ex(NULL
, chunk_size
,
301 chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
305 php_http_buffer_resize(str
, chunk_size
);
306 passed
= passin(opaque
, str
->data
+ str
->used
, chunk_size
);
308 if (passed
!= PHP_HTTP_BUFFER_PASS0
) {
313 php_http_buffer_fix(str
);
318 #ifdef PHP_HTTP_BUFFER_EXTENDED
320 PHP_HTTP_BUFFER_API
int php_http_buffer_cmp(php_http_buffer_t
*left
,
321 php_http_buffer_t
*right
)
323 if (left
->used
> right
->used
) {
325 } else if (right
->used
> left
->used
) {
328 return memcmp(left
->data
, right
->data
, left
->used
);
332 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_copy(
333 const php_http_buffer_t
*from
, php_http_buffer_t
*to
)
337 to
= php_http_buffer_clone(from
, to
);
339 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(to
, from
->data
, from
->used
)) {
341 php_http_buffer_free(&to
);
343 php_http_buffer_dtor(to
);
349 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insert(php_http_buffer_t
*buf
,
350 const char *insert
, size_t insert_len
, size_t offset
)
352 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, insert_len
)) {
353 return PHP_HTTP_BUFFER_NOMEM
;
355 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
356 memcpy(buf
->data
+ offset
, insert
, insert_len
);
357 buf
->used
+= insert_len
;
358 buf
->free
-= insert_len
;
362 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insertf(php_http_buffer_t
*buf
,
363 size_t offset
, const char *format
, ...)
367 size_t insert_len
, alloc
;
369 va_start(argv
, format
);
370 insert_len
= vspprintf(&insert
, 0, format
, argv
);
373 alloc
= php_http_buffer_insert(buf
, insert
, insert_len
, offset
);
376 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
377 return PHP_HTTP_BUFFER_NOMEM
;
382 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prepend(php_http_buffer_t
*buf
,
383 const char *prepend
, size_t prepend_len
)
385 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, prepend_len
)) {
386 return PHP_HTTP_BUFFER_NOMEM
;
388 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
389 memcpy(buf
->data
, prepend
, prepend_len
);
390 buf
->used
+= prepend_len
;
391 buf
->free
-= prepend_len
;
395 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prependf(php_http_buffer_t
*buf
,
396 const char *format
, ...)
400 size_t prepend_len
, alloc
;
402 va_start(argv
, format
);
403 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
406 alloc
= php_http_buffer_prepend(buf
, prepend
, prepend_len
);
409 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
410 return PHP_HTTP_BUFFER_NOMEM
;
415 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_sub(
416 const php_http_buffer_t
*buf
, size_t offset
, size_t length
)
418 if (offset
>= buf
->used
) {
421 php_http_buffer_t
*sub
;
422 size_t need
= 1 + ((length
+ offset
) > buf
->used
?
423 (buf
->used
- offset
) : (length
- offset
));
424 unsigned flags
= buf
->pmem
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0;
426 sub
= php_http_buffer_init_ex(NULL
, need
,
427 PHP_HTTP_BUFFER_INIT_PREALLOC
| flags
);
430 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(sub
, buf
->data
+ offset
, need
)) {
431 php_http_buffer_free(&sub
);
433 sub
->size
= buf
->size
;
440 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_right(
441 const php_http_buffer_t
*buf
, size_t length
)
443 if (length
< buf
->used
) {
444 return php_http_buffer_sub(buf
, buf
->used
- length
, length
);
446 return php_http_buffer_sub(buf
, 0, buf
->used
);
451 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_va(
452 php_http_buffer_t
*buf
, unsigned argc
, va_list argv
)
455 buf
= php_http_buffer_init(buf
);
459 php_http_buffer_free_t f
= va_arg(argv
, php_http_buffer_free_t
);
460 php_http_buffer_t
*current
= va_arg(argv
, php_http_buffer_t
*);
461 php_http_buffer_append(buf
, current
->data
, current
->used
);
462 FREE_PHP_HTTP_BUFFER(f
, current
);
469 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_ex(
470 php_http_buffer_t
*buf
, unsigned argc
, ...)
473 php_http_buffer_t
*ret
;
475 va_start(argv
, argc
);
476 ret
= php_http_buffer_merge_va(buf
, argc
, argv
);
481 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge(unsigned argc
, ...)
484 php_http_buffer_t
*ret
;
486 va_start(argv
, argc
);
487 ret
= php_http_buffer_merge_va(NULL
, argc
, argv
);
499 * vim600: sw=4 ts=4 fdm=marker