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