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