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