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