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(php_http_buffer_t
*buf
, size_t chunk_size
, int flags
)
19 buf
= pemalloc(sizeof(*buf
), flags
& PHP_HTTP_BUFFER_INIT_PERSISTENT
);
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;
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
)
37 if ((buf
= php_http_buffer_init(buf
))) {
38 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(buf
, string
, length
)) {
40 pefree(buf
, buf
->pmem
);
48 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
)
52 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
);
54 if (buf
->free
< len
) {
55 size_t size
= override_size
? override_size
: buf
->size
;
57 while ((size
+ buf
->free
) < len
) {
62 ptr
= perealloc_recoverable(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
64 ptr
= perealloc(buf
->data
, buf
->used
+ buf
->free
+ size
, buf
->pmem
);
70 return PHP_HTTP_BUFFER_NOMEM
;
79 PHP_HTTP_BUFFER_API
char *php_http_buffer_account(php_http_buffer_t
*buf
, size_t to_account
)
81 assert(to_account
<= buf
->free
);
83 buf
->free
-= to_account
;
84 buf
->used
+= to_account
;
86 return buf
->data
+ buf
->used
;
89 PHP_HTTP_BUFFER_API
size_t php_http_buffer_shrink(php_http_buffer_t
*buf
)
91 /* avoid another realloc on fixation */
93 char *ptr
= perealloc(buf
->data
, buf
->used
+ 1, buf
->pmem
);
98 return PHP_HTTP_BUFFER_NOMEM
;
105 PHP_HTTP_BUFFER_API
size_t php_http_buffer_append(php_http_buffer_t
*buf
, const char *append
, size_t append_len
)
107 if (buf
->free
< append_len
&& PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, append_len
)) {
108 return PHP_HTTP_BUFFER_NOMEM
;
110 memcpy(buf
->data
+ buf
->used
, append
, append_len
);
111 buf
->used
+= append_len
;
112 buf
->free
-= append_len
;
116 PHP_HTTP_BUFFER_API
size_t php_http_buffer_appendf(php_http_buffer_t
*buf
, const char *format
, ...)
120 size_t append_len
, alloc
;
122 va_start(argv
, format
);
123 append_len
= vspprintf(&append
, 0, format
, argv
);
126 alloc
= php_http_buffer_append(buf
, append
, append_len
);
129 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
130 return PHP_HTTP_BUFFER_NOMEM
;
135 PHP_HTTP_BUFFER_API
char *php_http_buffer_data(const php_http_buffer_t
*buf
, char **into
, size_t *len
)
137 char *copy
= ecalloc(1, buf
->used
+ 1);
138 memcpy(copy
, buf
->data
, buf
->used
);
148 PHP_HTTP_BUFFER_API
size_t php_http_buffer_cut(php_http_buffer_t
*buf
, size_t offset
, size_t length
)
150 if (offset
> buf
->used
) {
153 if (offset
+ length
> buf
->used
) {
154 length
= buf
->used
- offset
;
156 memmove(buf
->data
+ offset
, buf
->data
+ offset
+ length
, buf
->used
- length
- offset
);
162 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_fix(php_http_buffer_t
*buf
)
164 if (buf
->free
< 1 && PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(buf
, 1, 1, 0)) {
167 buf
->data
[buf
->used
] = '\0';
171 PHP_HTTP_BUFFER_API
void php_http_buffer_reset(php_http_buffer_t
*buf
)
173 buf
->free
+= buf
->used
;
177 PHP_HTTP_BUFFER_API
void php_http_buffer_dtor(php_http_buffer_t
*buf
)
180 pefree(buf
->data
, buf
->pmem
);
187 PHP_HTTP_BUFFER_API
void php_http_buffer_free(php_http_buffer_t
**buf
)
190 php_http_buffer_dtor(*buf
);
191 pefree(*buf
, (*buf
)->pmem
);
196 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
)
198 php_http_buffer_t
*storage
;
203 *s
= php_http_buffer_init_ex(NULL
, chunk_size
<< 1, chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
208 php_http_buffer_append(storage
, data
, data_len
);
212 php_http_buffer_data(storage
, chunk
, &chunk_size
);
213 php_http_buffer_free(s
);
217 if (storage
->used
>= chunk_size
) {
218 *chunk
= estrndup(storage
->data
, chunk_size
);
219 php_http_buffer_cut(storage
, 0, chunk_size
);
226 PHP_HTTP_BUFFER_API
size_t 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
)
229 size_t passed
= 0, got
= 0;
231 while ((got
= php_http_buffer_chunk_buffer(s
, data
, data_len
, &chunk
, chunk_len
))) {
232 if (PHP_HTTP_BUFFER_PASS0
== passout(opaque
, chunk
, got TSRMLS_CC
)) {
233 PTR_SET(chunk
, NULL
);
234 return PHP_HTTP_BUFFER_PASS0
;
238 /* we already got the last chunk,
239 and freed all resources */
244 PTR_SET(chunk
, NULL
);
250 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
)
252 size_t passed_on
= 0, passed_in
= php_http_buffer_chunked_input(s
, chunk_size
, passin
, passin_arg TSRMLS_CC
);
254 if (passed_in
== PHP_HTTP_BUFFER_PASS0
) {
257 if (passed_in
|| (*s
)->used
) {
258 passed_on
= passon(passon_arg
, (*s
)->data
, (*s
)->used TSRMLS_CC
);
260 if (passed_on
== PHP_HTTP_BUFFER_PASS0
) {
265 php_http_buffer_cut(*s
, 0, passed_on
);
269 return passed_on
- passed_in
;
272 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
)
274 php_http_buffer_t
*str
;
278 *s
= php_http_buffer_init_ex(NULL
, chunk_size
, chunk_size
? PHP_HTTP_BUFFER_INIT_PREALLOC
: 0);
282 php_http_buffer_resize(str
, chunk_size
);
283 passed
= passin(opaque
, str
->data
+ str
->used
, chunk_size TSRMLS_CC
);
285 if (passed
!= PHP_HTTP_BUFFER_PASS0
) {
290 php_http_buffer_fix(str
);
295 #ifdef PHP_HTTP_BUFFER_EXTENDED
297 PHP_HTTP_BUFFER_API
int php_http_buffer_cmp(php_http_buffer_t
*left
, php_http_buffer_t
*right
)
299 if (left
->used
> right
->used
) {
301 } else if (right
->used
> left
->used
) {
304 return memcmp(left
->data
, right
->data
, left
->used
);
308 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_copy(const php_http_buffer_t
*from
, php_http_buffer_t
*to
)
312 to
= php_http_buffer_clone(from
, to
);
314 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(to
, from
->data
, from
->used
)) {
316 php_http_buffer_free(&to
);
318 php_http_buffer_dtor(to
);
324 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
)
326 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, insert_len
)) {
327 return PHP_HTTP_BUFFER_NOMEM
;
329 memmove(buf
->data
+ offset
+ insert_len
, buf
->data
+ offset
, insert_len
);
330 memcpy(buf
->data
+ offset
, insert
, insert_len
);
331 buf
->used
+= insert_len
;
332 buf
->free
-= insert_len
;
336 PHP_HTTP_BUFFER_API
size_t php_http_buffer_insertf(php_http_buffer_t
*buf
, size_t offset
, const char *format
, ...)
340 size_t insert_len
, alloc
;
342 va_start(argv
, format
);
343 insert_len
= vspprintf(&insert
, 0, format
, argv
);
346 alloc
= php_http_buffer_insert(buf
, insert
, insert_len
, offset
);
349 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
350 return PHP_HTTP_BUFFER_NOMEM
;
355 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prepend(php_http_buffer_t
*buf
, const char *prepend
, size_t prepend_len
)
357 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize(buf
, prepend_len
)) {
358 return PHP_HTTP_BUFFER_NOMEM
;
360 memmove(buf
->data
+ prepend_len
, buf
->data
, buf
->used
);
361 memcpy(buf
->data
, prepend
, prepend_len
);
362 buf
->used
+= prepend_len
;
363 buf
->free
-= prepend_len
;
367 PHP_HTTP_BUFFER_API
size_t php_http_buffer_prependf(php_http_buffer_t
*buf
, const char *format
, ...)
371 size_t prepend_len
, alloc
;
373 va_start(argv
, format
);
374 prepend_len
= vspprintf(&prepend
, 0, format
, argv
);
377 alloc
= php_http_buffer_prepend(buf
, prepend
, prepend_len
);
380 if (PHP_HTTP_BUFFER_NOMEM
== alloc
) {
381 return PHP_HTTP_BUFFER_NOMEM
;
386 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_sub(const php_http_buffer_t
*buf
, size_t offset
, size_t length
)
388 if (offset
>= buf
->used
) {
391 size_t need
= 1 + ((length
+ offset
) > buf
->used
? (buf
->used
- offset
) : (length
- offset
));
392 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));
394 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(sub
, buf
->data
+ offset
, need
)) {
395 php_http_buffer_free(&sub
);
397 sub
->size
= buf
->size
;
404 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_right(const php_http_buffer_t
*buf
, size_t length
)
406 if (length
< buf
->used
) {
407 return php_http_buffer_sub(buf
, buf
->used
- length
, length
);
409 return php_http_buffer_sub(buf
, 0, buf
->used
);
414 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_va(php_http_buffer_t
*buf
, unsigned argc
, va_list argv
)
417 buf
= php_http_buffer_init(buf
);
421 php_http_buffer_free_t f
= va_arg(argv
, php_http_buffer_free_t
);
422 php_http_buffer_t
*current
= va_arg(argv
, php_http_buffer_t
*);
423 php_http_buffer_append(buf
, current
->data
, current
->used
);
424 FREE_PHP_HTTP_BUFFER(f
, current
);
431 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge_ex(php_http_buffer_t
*buf
, unsigned argc
, ...)
434 php_http_buffer_t
*ret
;
436 va_start(argv
, argc
);
437 ret
= php_http_buffer_merge_va(buf
, argc
, argv
);
442 PHP_HTTP_BUFFER_API php_http_buffer_t
*php_http_buffer_merge(unsigned argc
, ...)
445 php_http_buffer_t
*ret
;
447 va_start(argv
, argc
);
448 ret
= php_http_buffer_merge_va(NULL
, argc
, argv
);
460 * vim600: sw=4 ts=4 fdm=marker