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