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