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 #ifndef PHP_HTTP_BUFFER_H
14 #define PHP_HTTP_BUFFER_H
15 
16 #ifndef PHP_HTTP_BUFFER_DEFAULT_SIZE
17 #	define PHP_HTTP_BUFFER_DEFAULT_SIZE 256
18 #endif
19 
20 #define PHP_HTTP_BUFFER_ERROR ((size_t) -1)
21 #define PHP_HTTP_BUFFER_NOMEM PHP_HTTP_BUFFER_ERROR
22 #define PHP_HTTP_BUFFER_PASS0 PHP_HTTP_BUFFER_ERROR
23 
24 #ifndef PTR_FREE
25 #	define PTR_FREE(PTR) \
26 	{ \
27 		if (EXPECTED(PTR)) { \
28 			efree(PTR); \
29 		} \
30 	}
31 #endif
32 #ifndef PTR_SET
33 #	define PTR_SET(PTR, SET) \
34 	{ \
35 		PTR_FREE(PTR); \
36 		PTR = SET; \
37 	}
38 #endif
39 #ifdef PHP_ATTRIBUTE_FORMAT
40 #	define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b) PHP_ATTRIBUTE_FORMAT(f, a, b)
41 #else
42 #	define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b)
43 #endif
44 #ifndef pemalloc
45 #	define pemalloc(s,p)	malloc(s)
46 #	define pefree(x,p)		free(x)
47 #	define perealloc(x,s,p)	realloc(x,s)
48 #	define perealloc_recoverable perealloc
49 #	define ecalloc calloc
estrndup(void * p,size_t s)50 static inline void *estrndup(void *p, size_t s)
51 {
52 	char *r = (char *) malloc(s+1);
53 	if (r) memcpy((void *) r, p, s), r[s] = '\0';
54 	return (void *) r;
55 }
56 #endif
57 
58 #if defined(PHP_WIN32)
59 #	if defined(PHP_HTTP_BUFFER_EXPORTS)
60 #		define PHP_HTTP_BUFFER_API __declspec(dllexport)
61 #	elif defined(COMPILE_DL_PHP_HTTP_BUFFER)
62 #		define PHP_HTTP_BUFFER_API __declspec(dllimport)
63 #	else
64 #		define PHP_HTTP_BUFFER_API
65 #	endif
66 #else
67 #	define PHP_HTTP_BUFFER_API
68 #endif
69 
70 #define PHP_HTTP_BUFFER(p) ((php_http_buffer_t *) (p))
71 
72 #define FREE_PHP_HTTP_BUFFER_PTR(STR) pefree(STR, STR->pmem)
73 #define FREE_PHP_HTTP_BUFFER_VAL(STR) php_http_buffer_dtor(STR)
74 #define FREE_PHP_HTTP_BUFFER_ALL(STR) php_http_buffer_free(&(STR))
75 #define FREE_PHP_HTTP_BUFFER(free, STR) \
76 	switch (free) \
77 	{ \
78 	case PHP_HTTP_BUFFER_FREE_NOT: \
79 		break; \
80 	case PHP_HTTP_BUFFER_FREE_PTR: \
81 		pefree(STR, STR->pmem); \
82 		break; \
83 	case PHP_HTTP_BUFFER_FREE_VAL: \
84 		php_http_buffer_dtor(STR); \
85 		break; \
86 	case PHP_HTTP_BUFFER_FREE_ALL: { \
87 		php_http_buffer_t *PTR = (STR); \
88 		php_http_buffer_free(&PTR); \
89 		break; \
90 	} \
91 	default:\
92 		break; \
93 	}
94 
95 #define RETURN_PHP_HTTP_BUFFER_PTR(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
96 #define RETURN_PHP_HTTP_BUFFER_VAL(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
97 #define RETURN_PHP_HTTP_BUFFER_DUP(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
98 #define RETVAL_PHP_HTTP_BUFFER_PTR(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
99 #define RETVAL_PHP_HTTP_BUFFER_VAL(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
100 #define RETVAL_PHP_HTTP_BUFFER_DUP(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
101 /* RETURN_PHP_HTTP_BUFFER(buf, PHP_HTTP_BUFFER_FREE_PTR, 0) */
102 #define RETURN_PHP_HTTP_BUFFER(STR, free, dup) \
103 	RETVAL_PHP_HTTP_BUFFER((STR), (free), (dup)); \
104 	return;
105 
106 #define RETVAL_PHP_HTTP_BUFFER(STR, free, dup) \
107 	php_http_buffer_fix(STR); \
108 	RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
109 	FREE_PHP_HTTP_BUFFER((free), (STR));
110 
111 typedef struct php_http_buffer {
112 	char  *data;
113 	size_t used;
114 	size_t free;
115 	size_t size;
116 	unsigned pmem:1;
117 	unsigned reserved:31;
118 } php_http_buffer_t;
119 
120 typedef enum php_http_buffer_free {
121 	PHP_HTTP_BUFFER_FREE_NOT = 0,
122 	PHP_HTTP_BUFFER_FREE_PTR,	/* pefree() */
123 	PHP_HTTP_BUFFER_FREE_VAL,	/* php_http_buffer_dtor() */
124 	PHP_HTTP_BUFFER_FREE_ALL	/* php_http_buffer_free() */
125 } php_http_buffer_free_t;
126 
127 #define PHP_HTTP_BUFFER_ALL_FREE(STR) PHP_HTTP_BUFFER_FREE_ALL,(STR)
128 #define PHP_HTTP_BUFFER_PTR_FREE(STR) PHP_HTTP_BUFFER_FREE_PTR,(STR)
129 #define PHP_HTTP_BUFFER_VAL_FREE(STR) PHP_HTTP_BUFFER_FREE_VAL,(STR)
130 #define PHP_HTTP_BUFFER_NOT_FREE(STR) PHP_HTTP_BUFFER_FREE_NOT,(STR)
131 
132 #define PHP_HTTP_BUFFER_INIT_PREALLOC	0x01
133 #define PHP_HTTP_BUFFER_INIT_PERSISTENT	0x02
134 
135 /* create a new php_http_buffer_t */
136 #define php_http_buffer_new() php_http_buffer_init(NULL)
137 #define php_http_buffer_init(b) php_http_buffer_init_ex(b, PHP_HTTP_BUFFER_DEFAULT_SIZE, 0)
138 #define php_http_buffer_clone(from, to) php_http_buffer_init_ex((to), (from)->size, (from)->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0)
139 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, unsigned flags);
140 
141 /* create a php_http_buffer_t from a zval or c-string */
142 #define php_http_buffer_from_zval(z) php_http_buffer_from_string(Z_STRVAL(z), Z_STRLEN(z))
143 #define php_http_buffer_from_zval_ex(b, z) php_http_buffer_from_string_ex(b, Z_STRVAL(z), Z_STRLEN(z))
144 #define php_http_buffer_from_string(s, l) php_http_buffer_from_string_ex(NULL, (s), (l))
145 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);
146 
147 /* usually only called from within the internal functions */
148 #define php_http_buffer_resize(b, s) php_http_buffer_resize_ex((b), (s), 0, 0)
149 PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(php_http_buffer_t *buf, size_t len, size_t override_size, zend_bool allow_error);
150 
151 PHP_HTTP_BUFFER_API char *php_http_buffer_account(php_http_buffer_t *buf, size_t to_account);
152 
153 /* shrink memory chunk to actually used size (+1) */
154 PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf);
155 
156 /* append data to the php_http_buffer_t */
157 #define php_http_buffer_appends(b, a) php_http_buffer_append((b), (a), sizeof(a)-1)
158 #define php_http_buffer_appendl(b, a) php_http_buffer_append((b), (a), strlen(a))
159 #define php_http_buffer_appendz(b, z) php_http_buffer_append((b), (z)->val, (z)->len)
160 PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len);
161 PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
162 
163 /* get a zero-terminated string */
164 PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len);
165 
166 /* remove a substring */
167 PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length);
168 
169 /* sets a trailing NUL byte */
170 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf);
171 
172 /* reset php_http_buffer_t object */
173 PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf);
174 
175 /* free a php_http_buffer_t objects contents */
176 PHP_HTTP_BUFFER_API void php_http_buffer_dtor(php_http_buffer_t *buf);
177 
178 /* free a php_http_buffer_t object completely */
179 PHP_HTTP_BUFFER_API void php_http_buffer_free(php_http_buffer_t **buf);
180 
181 /* stores data in a php_http_buffer_t until it reaches chunk_size */
182 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);
183 
184 typedef size_t (*php_http_buffer_pass_func_t)(void *opaque, char *, size_t);
185 
186 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);
187 
188 /* wrapper around php_http_buffer_chunk_buffer, which passes available chunks to passthru() */
189 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_size, php_http_buffer_pass_func_t passout, void *opaque);
190 
191 /* write chunks directly into php_http_buffer_t buffer */
192 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);
193 
194 
195 #	ifdef PHP_HTTP_BUFFER_EXTENDED
196 
197 /* memcmp for php_http_buffer_t objects */
198 PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right);
199 
200 /* get a complete php_http_buffer_t duplicate */
201 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to);
202 
203 /* merge several php_http_buffer_t objects
204    use like:
205 
206 	php_http_buffer_t *final = php_http_buffer_merge(3,
207 		PHP_HTTP_BUFFER_NOT_FREE(&keep),
208 		PHP_HTTP_BUFFER_ALL_FREE(middle_ptr),
209 		PHP_HTTP_BUFFER_VAL_FREE(&local);
210 */
211 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...);
212 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...);
213 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv);
214 
215 /* insert data at a specific position of the php_http_buffer_t */
216 #define php_http_buffer_inserts(b, i, o) php_http_buffer_insert((b), (i), sizeof(i)-1, (o))
217 #define php_http_buffer_insertl(b, i, o) php_http_buffer_insert((b), (i), strlen(i), (o))
218 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);
219 PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 3, 4);
220 
221 /* prepend data */
222 #define php_http_buffer_prepends(b, p) php_http_buffer_prepend((b), (p), sizeof(p)-1)
223 #define php_http_buffer_prependl(b, p) php_http_buffer_prepend((b), (p), strlen(p))
224 PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len);
225 PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
226 
227 /* get a part of the php_http_buffer_t */
228 #define php_http_buffer_mid(b, o, l) php_http_buffer_sub((b), (o), (l))
229 #define php_http_buffer_left(b, l) php_http_buffer_sub((b), 0, (l))
230 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length);
231 PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t len);
232 
233 #	endif /* PHP_HTTP_BUFFER_EXTENDED */
234 
235 #endif /* PHP_HTTP_BUFFER_H */
236 
237 
238 /*
239  * Local variables:
240  * tab-width: 4
241  * c-basic-offset: 4
242  * End:
243  * vim600: sw=4 ts=4 fdm=marker
244  * vim<600: sw=4 ts=4
245  */
246