1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Wez Furlong (wez@thebrainroom.com) |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: php_streams.h 288605 2009-09-23 13:51:50Z dmitry $ */
20 :
21 : #ifndef PHP_STREAMS_H
22 : #define PHP_STREAMS_H
23 :
24 : #include "php_ini.h"
25 :
26 : #ifdef HAVE_SYS_TIME_H
27 : #include <sys/time.h>
28 : #endif
29 : #include <sys/types.h>
30 : #include <sys/stat.h>
31 :
32 : BEGIN_EXTERN_C()
33 : PHPAPI int php_file_le_stream(void);
34 : PHPAPI int php_file_le_pstream(void);
35 : PHPAPI int php_file_le_stream_filter(void);
36 : END_EXTERN_C()
37 :
38 : /* {{{ Streams memory debugging stuff */
39 :
40 : #if ZEND_DEBUG
41 : /* these have more of a dependency on the definitions of the zend macros than
42 : * I would prefer, but doing it this way saves loads of idefs :-/ */
43 : # define STREAMS_D int __php_stream_call_depth ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC
44 : # define STREAMS_C 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC
45 : # define STREAMS_REL_C __php_stream_call_depth + 1 ZEND_FILE_LINE_CC, \
46 : __php_stream_call_depth ? __zend_orig_filename : __zend_filename, \
47 : __php_stream_call_depth ? __zend_orig_lineno : __zend_lineno
48 :
49 : # define STREAMS_DC , STREAMS_D
50 : # define STREAMS_CC , STREAMS_C
51 : # define STREAMS_REL_CC , STREAMS_REL_C
52 :
53 : #else
54 : # define STREAMS_D
55 : # define STREAMS_C
56 : # define STREAMS_REL_C
57 : # define STREAMS_DC
58 : # define STREAMS_CC
59 : # define STREAMS_REL_CC
60 : #endif
61 :
62 : /* these functions relay the file/line number information. They are depth aware, so they will pass
63 : * the ultimate ancestor, which is useful, because there can be several layers of calls */
64 : #define php_stream_alloc_rel(ops, thisptr, persistent, mode) _php_stream_alloc((ops), (thisptr), (persistent), (mode) STREAMS_REL_CC TSRMLS_CC)
65 :
66 : #define php_stream_copy_to_mem_rel(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_REL_CC TSRMLS_CC)
67 :
68 : #define php_stream_fopen_rel(filename, mode, opened, options) _php_stream_fopen((filename), (mode), (opened), (options) STREAMS_REL_CC TSRMLS_CC)
69 :
70 : #define php_stream_fopen_with_path_rel(filename, mode, path, opened, options) _php_stream_fopen_with_path((filename), (mode), (path), (opened), (options) STREAMS_REL_CC TSRMLS_CC)
71 :
72 : #define php_stream_fopen_from_fd_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_REL_CC TSRMLS_CC)
73 : #define php_stream_fopen_from_file_rel(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_REL_CC TSRMLS_CC)
74 :
75 : #define php_stream_fopen_from_pipe_rel(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC TSRMLS_CC)
76 :
77 : #define php_stream_fopen_tmpfile_rel() _php_stream_fopen_tmpfile(0 STREAMS_REL_CC TSRMLS_CC)
78 :
79 : #define php_stream_fopen_temporary_file_rel(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_REL_CC TSRMLS_CC)
80 :
81 : #define php_stream_open_wrapper_rel(path, mode, options, opened) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_REL_CC TSRMLS_CC)
82 : #define php_stream_open_wrapper_ex_rel(path, mode, options, opened, context) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), (context) STREAMS_REL_CC TSRMLS_CC)
83 :
84 : #define php_stream_make_seekable_rel(origstream, newstream, flags) _php_stream_make_seekable((origstream), (newstream), (flags) STREAMS_REL_CC TSRMLS_CC)
85 :
86 : /* }}} */
87 :
88 : /* The contents of the php_stream_ops and php_stream should only be accessed
89 : * using the functions/macros in this header.
90 : * If you need to get at something that doesn't have an API,
91 : * drop me a line <wez@thebrainroom.com> and we can sort out a way to do
92 : * it properly.
93 : *
94 : * The only exceptions to this rule are that stream implementations can use
95 : * the php_stream->abstract pointer to hold their context, and streams
96 : * opened via stream_open_wrappers can use the zval ptr in
97 : * php_stream->wrapperdata to hold meta data for php scripts to
98 : * retrieve using file_get_wrapper_data(). */
99 :
100 : typedef struct _php_stream php_stream;
101 : typedef struct _php_stream_wrapper php_stream_wrapper;
102 : typedef struct _php_stream_context php_stream_context;
103 : typedef struct _php_stream_filter php_stream_filter;
104 :
105 : #include "streams/php_stream_context.h"
106 : #include "streams/php_stream_filter_api.h"
107 :
108 : typedef struct _php_stream_statbuf {
109 : struct stat sb; /* regular info */
110 : /* extended info to go here some day: content-type etc. etc. */
111 : } php_stream_statbuf;
112 :
113 : typedef struct _php_stream_dirent {
114 : char d_name[MAXPATHLEN];
115 : } php_stream_dirent;
116 :
117 : /* operations on streams that are file-handles */
118 : typedef struct _php_stream_ops {
119 : /* stdio like functions - these are mandatory! */
120 : size_t (*write)(php_stream *stream, const char *buf, size_t count TSRMLS_DC);
121 : size_t (*read)(php_stream *stream, char *buf, size_t count TSRMLS_DC);
122 : int (*close)(php_stream *stream, int close_handle TSRMLS_DC);
123 : int (*flush)(php_stream *stream TSRMLS_DC);
124 :
125 : const char *label; /* label for this ops structure */
126 :
127 : /* these are optional */
128 : int (*seek)(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC);
129 : int (*cast)(php_stream *stream, int castas, void **ret TSRMLS_DC);
130 : int (*stat)(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
131 : int (*set_option)(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
132 : } php_stream_ops;
133 :
134 : typedef struct _php_stream_wrapper_ops {
135 : /* open/create a wrapped stream */
136 : php_stream *(*stream_opener)(php_stream_wrapper *wrapper, char *filename, char *mode,
137 : int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
138 : /* close/destroy a wrapped stream */
139 : int (*stream_closer)(php_stream_wrapper *wrapper, php_stream *stream TSRMLS_DC);
140 : /* stat a wrapped stream */
141 : int (*stream_stat)(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
142 : /* stat a URL */
143 : int (*url_stat)(php_stream_wrapper *wrapper, char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC);
144 : /* open a "directory" stream */
145 : php_stream *(*dir_opener)(php_stream_wrapper *wrapper, char *filename, char *mode,
146 : int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
147 :
148 : const char *label;
149 :
150 : /* delete a file */
151 : int (*unlink)(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC);
152 :
153 : /* rename a file */
154 : int (*rename)(php_stream_wrapper *wrapper, char *url_from, char *url_to, int options, php_stream_context *context TSRMLS_DC);
155 :
156 : /* Create/Remove directory */
157 : int (*stream_mkdir)(php_stream_wrapper *wrapper, char *url, int mode, int options, php_stream_context *context TSRMLS_DC);
158 : int (*stream_rmdir)(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC);
159 :
160 : /* Unicode path manipulation -- Leave NULL to use UG(filesystem_encoding_conv) for conversion */
161 : int (*path_encode)(php_stream_wrapper *wrapper, char **encpath, int *encpath_len, const UChar *path, int path_len,
162 : int options, php_stream_context *context TSRMLS_DC);
163 : int (*path_decode)(php_stream_wrapper *wrapper, UChar **decpath, int *decpath_len, const char *path, int path_len,
164 : int options, php_stream_context *context TSRMLS_DC);
165 : } php_stream_wrapper_ops;
166 :
167 : struct _php_stream_wrapper {
168 : php_stream_wrapper_ops *wops; /* operations the wrapper can perform */
169 : void *abstract; /* context for the wrapper */
170 : int is_url; /* so that PG(allow_url_fopen_list)/PG(allow_url_include_list) can be respected */
171 :
172 : /* support for wrappers to return (multiple) error messages to the stream opener */
173 : int err_count;
174 : char **err_stack;
175 : };
176 :
177 : #define PHP_STREAM_FLAG_NO_SEEK 1
178 : #define PHP_STREAM_FLAG_NO_BUFFER 2
179 :
180 : #define PHP_STREAM_FLAG_EOL_UNIX 0 /* also includes DOS */
181 : #define PHP_STREAM_FLAG_DETECT_EOL 4
182 : #define PHP_STREAM_FLAG_EOL_MAC 8
183 :
184 : /* set this when the stream might represent "interactive" data.
185 : * When set, the read buffer will avoid certain operations that
186 : * might otherwise cause the read to block for much longer than
187 : * is strictly required. */
188 : #define PHP_STREAM_FLAG_AVOID_BLOCKING 16
189 :
190 : #define PHP_STREAM_FLAG_NO_CLOSE 32
191 :
192 : #define PHP_STREAM_FLAG_IS_DIR 64
193 :
194 : #define PHP_STREAM_FLAG_NO_FCLOSE 128
195 :
196 : struct _php_stream {
197 : php_stream_ops *ops;
198 : void *abstract; /* convenience pointer for abstraction */
199 :
200 : php_stream_filter_chain readfilters, writefilters;
201 :
202 : php_stream_wrapper *wrapper; /* which wrapper was used to open the stream */
203 : void *wrapperthis; /* convenience pointer for a instance of a wrapper */
204 : zval *wrapperdata; /* fgetwrapperdata retrieves this */
205 :
206 : int fgetss_state; /* for fgetss to handle multiline tags */
207 : int is_persistent;
208 : char mode[16]; /* "rwb" etc. ala stdio */
209 : int rsrc_id; /* used for auto-cleanup */
210 : int in_free; /* to prevent recursion during free */
211 : /* so we know how to clean it up correctly. This should be set to
212 : * PHP_STREAM_FCLOSE_XXX as appropriate */
213 : int fclose_stdiocast;
214 : FILE *stdiocast; /* cache this, otherwise we might leak! */
215 : #if ZEND_DEBUG
216 : int __exposed; /* non-zero if exposed as a zval somewhere */
217 : #endif
218 : char *orig_path;
219 :
220 : php_stream_context *context;
221 : int flags; /* PHP_STREAM_FLAG_XXX */
222 :
223 : /* buffer */
224 : off_t position; /* of underlying stream */
225 : zend_uchar readbuf_type;
226 : zstr readbuf; /* readbuf.s or readbuf.u */
227 : size_t readbuflen; /* Length in units (char or UChar) */
228 : off_t readpos; /* Position in units (char or UChar) */
229 : off_t writepos;
230 :
231 : /* how much data to read when filling buffer */
232 : size_t chunk_size;
233 :
234 : int eof;
235 :
236 : #if ZEND_DEBUG
237 : char *open_filename;
238 : uint open_lineno;
239 : #endif
240 : }; /* php_stream */
241 :
242 : /* state definitions when closing down; these are private to streams.c */
243 : #define PHP_STREAM_FCLOSE_NONE 0
244 : #define PHP_STREAM_FCLOSE_FDOPEN 1
245 : #define PHP_STREAM_FCLOSE_FOPENCOOKIE 2
246 :
247 : /* allocate a new stream for a particular ops */
248 : BEGIN_EXTERN_C()
249 : PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract,
250 : const char *persistent_id, const char *mode STREAMS_DC TSRMLS_DC);
251 : END_EXTERN_C()
252 : #define php_stream_alloc(ops, thisptr, persistent_id, mode) _php_stream_alloc((ops), (thisptr), (persistent_id), (mode) STREAMS_CC TSRMLS_CC)
253 :
254 : #define php_stream_get_resource_id(stream) (stream)->rsrc_id
255 : #if ZEND_DEBUG
256 : /* use this to tell the stream that it is OK if we don't explicitly close it */
257 : # define php_stream_auto_cleanup(stream) { (stream)->__exposed++; }
258 : /* use this to assign the stream to a zval and tell the stream that is
259 : * has been exported to the engine; it will expect to be closed automatically
260 : * when the resources are auto-destructed */
261 : # define php_stream_to_zval(stream, zval) { ZVAL_RESOURCE(zval, (stream)->rsrc_id); (stream)->__exposed++; }
262 : #else
263 : # define php_stream_auto_cleanup(stream) /* nothing */
264 : # define php_stream_to_zval(stream, zval) { ZVAL_RESOURCE(zval, (stream)->rsrc_id); }
265 : #endif
266 :
267 : #define php_stream_from_zval(xstr, ppzval) ZEND_FETCH_RESOURCE2((xstr), php_stream *, (ppzval), -1, "stream", php_file_le_stream(), php_file_le_pstream())
268 : #define php_stream_from_zval_no_verify(xstr, ppzval) (xstr) = (php_stream*)zend_fetch_resource((ppzval) TSRMLS_CC, -1, "stream", NULL, 2, php_file_le_stream(), php_file_le_pstream())
269 :
270 : #define PS_ULEN(is_unicode, len) ((is_unicode) ? UBYTES(len) : (len))
271 :
272 : BEGIN_EXTERN_C()
273 : PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream TSRMLS_DC);
274 : #define PHP_STREAM_PERSISTENT_SUCCESS 0 /* id exists */
275 : #define PHP_STREAM_PERSISTENT_FAILURE 1 /* id exists but is not a stream! */
276 : #define PHP_STREAM_PERSISTENT_NOT_EXIST 2 /* id does not exist */
277 :
278 : #define PHP_STREAM_FREE_CALL_DTOR 1 /* call ops->close */
279 : #define PHP_STREAM_FREE_RELEASE_STREAM 2 /* pefree(stream) */
280 : #define PHP_STREAM_FREE_PRESERVE_HANDLE 4 /* tell ops->close to not close it's underlying handle */
281 : #define PHP_STREAM_FREE_RSRC_DTOR 8 /* called from the resource list dtor */
282 : #define PHP_STREAM_FREE_PERSISTENT 16 /* manually freeing a persistent connection */
283 : #define PHP_STREAM_FREE_CLOSE (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)
284 : #define PHP_STREAM_FREE_CLOSE_CASTED (PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_PRESERVE_HANDLE)
285 : #define PHP_STREAM_FREE_CLOSE_PERSISTENT (PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_PERSISTENT)
286 :
287 : PHPAPI int _php_stream_free(php_stream *stream, int close_options TSRMLS_DC);
288 : #define php_stream_free(stream, close_options) _php_stream_free((stream), (close_options) TSRMLS_CC)
289 : #define php_stream_close(stream) _php_stream_free((stream), PHP_STREAM_FREE_CLOSE TSRMLS_CC)
290 : #define php_stream_pclose(stream) _php_stream_free((stream), PHP_STREAM_FREE_CLOSE_PERSISTENT TSRMLS_CC)
291 :
292 : PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC);
293 : #define php_stream_rewind(stream) _php_stream_seek((stream), 0L, SEEK_SET TSRMLS_CC)
294 : #define php_stream_seek(stream, offset, whence) _php_stream_seek((stream), (offset), (whence) TSRMLS_CC)
295 :
296 : PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC);
297 : #define php_stream_tell(stream) _php_stream_tell((stream) TSRMLS_CC)
298 :
299 : /* Convert using runtime_encoding if necessary -- return string */
300 : PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC);
301 : #define php_stream_read(stream, buf, count) _php_stream_read((stream), (buf), (count) TSRMLS_CC)
302 :
303 : /* Convert using runtime_encoding if necessary -- return unicode */
304 : PHPAPI size_t _php_stream_read_unicode(php_stream *stream, UChar *buf, int maxlen, int maxchars TSRMLS_DC);
305 : #define php_stream_read_unicode(stream, buf, maxlen) _php_stream_read_unicode((stream), (buf), (maxlen), -1 TSRMLS_CC)
306 : #define php_stream_read_unicode_ex(stream, buf, maxlen, maxchars) _php_stream_read_unicode((stream), (buf), (maxlen), (maxchars) TSRMLS_CC)
307 :
308 : #define php_stream_u_read_ex(stream, type, zbuf, maxlen, maxchars) ((type == IS_UNICODE) ? \
309 : php_stream_read_unicode_ex(stream, zbuf.u, maxlen, maxchars) : \
310 : php_stream_read(stream, zbuf.s, maxlen))
311 :
312 : #define php_stream_u_read(stream, type, zbuf, maxlen) php_stream_u_read_ex(stream, type, zbuf, maxlen, -1)
313 :
314 : PHPAPI UChar *_php_stream_read_unicode_chars(php_stream *stream, int *pchars TSRMLS_DC);
315 : #define php_stream_read_unicode_chars(stream, pchars) _php_stream_read_unicode_chars((stream), (pchars) TSRMLS_CC)
316 :
317 : PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC);
318 : #define php_stream_write_string(stream, str) _php_stream_write(stream, str, strlen(str) TSRMLS_CC)
319 : #define php_stream_write(stream, buf, count) _php_stream_write(stream, (buf), (count) TSRMLS_CC)
320 :
321 : PHPAPI size_t _php_stream_write_unicode(php_stream *stream, const UChar *buf, int count TSRMLS_DC);
322 : #define php_stream_write_unicode(stream, buf, count) _php_stream_write_unicode((stream), (buf), (count) TSRMLS_CC)
323 :
324 : #define php_stream_u_write(stream, type, zbuf, buflen) ((type == IS_UNICODE) ? \
325 : php_stream_write_unicode((stream), (zbuf).u, (buflen)) : \
326 : php_stream_write((stream), (zbuf).s, (buflen)))
327 :
328 : PHPAPI size_t _php_stream_printf(php_stream *stream TSRMLS_DC, const char *fmt, ...);
329 : /* php_stream_printf macro & function require TSRMLS_CC */
330 : #define php_stream_printf _php_stream_printf
331 :
332 : PHPAPI int _php_stream_eof(php_stream *stream TSRMLS_DC);
333 : #define php_stream_eof(stream) _php_stream_eof((stream) TSRMLS_CC)
334 :
335 : PHPAPI int _php_stream_getc(php_stream *stream TSRMLS_DC);
336 : #define php_stream_getc(stream) _php_stream_getc((stream) TSRMLS_CC)
337 :
338 : PHPAPI int _php_stream_putc(php_stream *stream, int c TSRMLS_DC);
339 : #define php_stream_putc(stream, c) _php_stream_putc((stream), (c) TSRMLS_CC)
340 :
341 : PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC);
342 : #define php_stream_flush(stream) _php_stream_flush((stream), 0 TSRMLS_CC)
343 :
344 : PHPAPI void *_php_stream_get_line(php_stream *stream, int buf_type, zstr buf, size_t maxlen, size_t maxchars, size_t *returned_len TSRMLS_DC);
345 : #define php_stream_get_line(stream, buf, maxlen, retlen) _php_stream_get_line((stream), IS_STRING, buf, (maxlen), 0, (retlen) TSRMLS_CC)
346 : #define php_stream_get_line_ex(stream, buf_type, buf, maxlen, maxchars, retlen) \
347 : _php_stream_get_line((stream), (buf_type), buf, (maxlen), (maxchars), (retlen) TSRMLS_CC)
348 : #define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), IS_STRING, buf, (maxlen), 0, NULL TSRMLS_CC)
349 : #define php_stream_gets_ex(stream, buf_type, buf, maxlen, maxchars) \
350 : _php_stream_get_line((stream), (buf_type), buf, (maxlen), (maxchars), NULL TSRMLS_CC)
351 :
352 : PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *returned_len, char *delim, size_t delim_len TSRMLS_DC);
353 : PHPAPI UChar *php_stream_get_record_unicode(php_stream *stream, size_t maxlen, size_t maxchars, size_t *returned_len, UChar *delim, size_t delim_len TSRMLS_DC);
354 :
355 :
356 : PHPAPI UChar *_php_stream_u_get_line(php_stream *stream, UChar *buf, int32_t *pmax_bytes, int32_t *pmax_chars, int *pis_unicode TSRMLS_DC);
357 : #define php_stream_u_get_line(stream, buf, maxlen_buf, maxlen_chars, retlen) _php_stream_get_line((stream), IS_UNICODE, (buf), (maxlen_buf), (maxlen_chars), (retlen) TSRMLS_CC)
358 :
359 : /* CAREFUL! this is equivalent to puts NOT fputs! */
360 : PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC);
361 : #define php_stream_puts(stream, buf) _php_stream_puts((stream), (buf) TSRMLS_CC)
362 :
363 : PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
364 : #define php_stream_stat(stream, ssb) _php_stream_stat((stream), (ssb) TSRMLS_CC)
365 :
366 : PHPAPI int _php_stream_stat_path(char *path, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC);
367 : #define php_stream_stat_path(path, ssb) _php_stream_stat_path((path), 0, (ssb), NULL TSRMLS_CC)
368 : #define php_stream_stat_path_ex(path, flags, ssb, context) _php_stream_stat_path((path), (flags), (ssb), (context) TSRMLS_CC)
369 :
370 : PHPAPI int _php_stream_mkdir(char *path, int mode, int options, php_stream_context *context TSRMLS_DC);
371 : #define php_stream_mkdir(path, mode, options, context) _php_stream_mkdir(path, mode, options, context TSRMLS_CC)
372 :
373 : PHPAPI int _php_stream_rmdir(char *path, int options, php_stream_context *context TSRMLS_DC);
374 : #define php_stream_rmdir(path, options, context) _php_stream_rmdir(path, options, context TSRMLS_CC)
375 :
376 : PHPAPI php_stream *_php_stream_opendir(char *path, int options, php_stream_context *context STREAMS_DC TSRMLS_DC);
377 : PHPAPI php_stream *_php_stream_u_opendir(zend_uchar type, zstr path, int path_len, int options, php_stream_context *context STREAMS_DC TSRMLS_DC);
378 : #define php_stream_opendir(path, options, context) _php_stream_opendir((path), (options), (context) STREAMS_CC TSRMLS_CC)
379 : #define php_stream_u_opendir(path_type, path, path_len, options, context) _php_stream_u_opendir((path_type), (path), (path_len), (options), (context) STREAMS_CC TSRMLS_CC)
380 : PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent TSRMLS_DC);
381 : #define php_stream_readdir(dirstream, dirent) _php_stream_readdir((dirstream), (dirent) TSRMLS_CC)
382 : #define php_stream_closedir(dirstream) php_stream_close((dirstream))
383 : #define php_stream_rewinddir(dirstream) php_stream_rewind((dirstream))
384 :
385 : PHPAPI int php_stream_dirent_alphasort(const char **a, const char **b);
386 : PHPAPI int php_stream_dirent_alphasortr(const char **a, const char **b);
387 :
388 : PHPAPI int _php_stream_scandir(char *dirname, char **namelist[], int flags, php_stream_context *context,
389 : int (*compare) (const char **a, const char **b) TSRMLS_DC);
390 : #define php_stream_scandir(dirname, namelist, context, compare) _php_stream_scandir((dirname), (namelist), 0, (context), (compare) TSRMLS_CC)
391 :
392 : PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
393 : #define php_stream_set_option(stream, option, value, ptrvalue) _php_stream_set_option((stream), (option), (value), (ptrvalue) TSRMLS_CC)
394 :
395 : #define php_stream_set_chunk_size(stream, size) _php_stream_set_option((stream), PHP_STREAM_OPTION_SET_CHUNK_SIZE, (size), NULL TSRMLS_CC)
396 :
397 : PHPAPI int _php_stream_path_encode(php_stream_wrapper *wrapper,
398 : char **pathenc, int *pathenc_len, const UChar *path, int path_len,
399 : int options, php_stream_context *context TSRMLS_DC);
400 : #define php_stream_path_encode(wrapper, pathenc, pathenc_len, path, path_len, options, context) \
401 : _php_stream_path_encode((wrapper), (pathenc), (pathenc_len), (path), (path_len), (options), (context) TSRMLS_CC)
402 :
403 : PHPAPI int _php_stream_path_decode(php_stream_wrapper *wrapper,
404 : UChar **pathdec, int *pathdec_len, const char *path, int path_len,
405 : int options, php_stream_context *context TSRMLS_DC);
406 : #define php_stream_path_decode(wrapper, pathdec, pathdec_len, path, path_len, options, context) \
407 : _php_stream_path_decode((wrapper), (pathdec), (pathdec_len), (path), (path_len), (options), (context) TSRMLS_CC)
408 :
409 : END_EXTERN_C()
410 :
411 :
412 : #define php_stream_path_param_encode(ppzval, ppath, ppath_len, options, context) \
413 : _php_stream_path_param_encode((ppzval), (ppath), (ppath_len), (options), (context) TSRMLS_CC)
414 : static inline int _php_stream_path_param_encode(zval **ppzval, char **ppath, int *ppath_len, int options, php_stream_context *context TSRMLS_DC)
415 0 : {
416 0 : if (Z_TYPE_PP(ppzval) == IS_UNICODE) {
417 : char *path;
418 : int path_len;
419 :
420 : /* Convert the path and put it into a fresh new zval */
421 0 : if (FAILURE == php_stream_path_encode(NULL, &path, &path_len, Z_USTRVAL_PP(ppzval), Z_USTRLEN_PP(ppzval), options, context)) {
422 0 : return FAILURE;
423 : }
424 0 : if (Z_ISREF_PP(ppzval) || Z_REFCOUNT_PP(ppzval) == 1) {
425 0 : zval_dtor(*ppzval);
426 0 : ZVAL_STRINGL(*ppzval, path, path_len, 0);
427 : } else {
428 0 : zval_ptr_dtor(ppzval);
429 0 : MAKE_STD_ZVAL(*ppzval);
430 0 : ZVAL_STRINGL(*ppzval, path, path_len, 0);
431 : }
432 0 : } else if (Z_TYPE_PP(ppzval) != IS_STRING) {
433 0 : convert_to_string_ex(ppzval);
434 : }
435 :
436 : /* Populate convenience params if requested */
437 0 : if (ppath) {
438 0 : *ppath = Z_STRVAL_PP(ppzval);
439 : }
440 0 : if (ppath_len) {
441 0 : *ppath_len = Z_STRLEN_PP(ppzval);
442 : }
443 :
444 0 : return SUCCESS;
445 : }
446 :
447 :
448 : /* Flags for mkdir method in wrapper ops */
449 : #define PHP_STREAM_MKDIR_RECURSIVE 1
450 : /* define REPORT ERRORS 8 (below) */
451 :
452 : /* Flags for rmdir method in wrapper ops */
453 : /* define REPORT_ERRORS 8 (below) */
454 :
455 : /* Flags for url_stat method in wrapper ops */
456 : #define PHP_STREAM_URL_STAT_LINK 1
457 : #define PHP_STREAM_URL_STAT_QUIET 2
458 :
459 : /* change the blocking mode of stream: value == 1 => blocking, value == 0 => non-blocking. */
460 : #define PHP_STREAM_OPTION_BLOCKING 1
461 :
462 : /* change the buffering mode of stream. value is a PHP_STREAM_BUFFER_XXXX value, ptrparam is a ptr to a size_t holding
463 : * the required buffer size */
464 : #define PHP_STREAM_OPTION_READ_BUFFER 2
465 : #define PHP_STREAM_OPTION_WRITE_BUFFER 3
466 :
467 : #define PHP_STREAM_BUFFER_NONE 0 /* unbuffered */
468 : #define PHP_STREAM_BUFFER_LINE 1 /* line buffered */
469 : #define PHP_STREAM_BUFFER_FULL 2 /* fully buffered */
470 :
471 : /* set the timeout duration for reads on the stream. ptrparam is a pointer to a struct timeval * */
472 : #define PHP_STREAM_OPTION_READ_TIMEOUT 4
473 : #define PHP_STREAM_OPTION_SET_CHUNK_SIZE 5
474 :
475 : /* set or release lock on a stream */
476 : #define PHP_STREAM_OPTION_LOCKING 6
477 :
478 : /* whether or not locking is supported */
479 : #define PHP_STREAM_LOCK_SUPPORTED 1
480 :
481 : #define php_stream_supports_lock(stream) _php_stream_set_option((stream), PHP_STREAM_OPTION_LOCKING, 0, (void *) PHP_STREAM_LOCK_SUPPORTED TSRMLS_CC) == 0 ? 1 : 0
482 : #define php_stream_lock(stream, mode) _php_stream_set_option((stream), PHP_STREAM_OPTION_LOCKING, (mode), (void *) NULL TSRMLS_CC)
483 :
484 : /* option code used by the php_stream_xport_XXX api */
485 : #define PHP_STREAM_OPTION_XPORT_API 7 /* see php_stream_transport.h */
486 : #define PHP_STREAM_OPTION_CRYPTO_API 8 /* see php_stream_transport.h */
487 : #define PHP_STREAM_OPTION_MMAP_API 9 /* see php_stream_mmap.h */
488 : #define PHP_STREAM_OPTION_TRUNCATE_API 10
489 :
490 : #define PHP_STREAM_TRUNCATE_SUPPORTED 0
491 : #define PHP_STREAM_TRUNCATE_SET_SIZE 1 /* ptrparam is a pointer to a size_t */
492 :
493 : #define php_stream_truncate_supported(stream) (_php_stream_set_option((stream), PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SUPPORTED, NULL TSRMLS_CC) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0)
494 :
495 : BEGIN_EXTERN_C()
496 : PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize TSRMLS_DC);
497 : #define php_stream_truncate_set_size(stream, size) _php_stream_truncate_set_size((stream), (size) TSRMLS_CC)
498 : END_EXTERN_C()
499 :
500 : #define PHP_STREAM_OPTION_META_DATA_API 11 /* ptrparam is a zval* to which to add meta data information */
501 : #define php_stream_populate_meta_data(stream, zv) (_php_stream_set_option((stream), PHP_STREAM_OPTION_META_DATA_API, 0, zv TSRMLS_CC) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0)
502 :
503 : /* Check if the stream is still "live"; for sockets/pipes this means the socket
504 : * is still connected; for files, this does not really have meaning */
505 : #define PHP_STREAM_OPTION_CHECK_LIVENESS 12 /* no parameters */
506 :
507 : #define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
508 : #define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
509 : #define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */
510 :
511 : /* copy up to maxlen bytes from src to dest. If maxlen is PHP_STREAM_COPY_ALL, copy until eof(src).
512 : * Uses mmap if the src is a plain file and at offset 0 */
513 : #define PHP_STREAM_COPY_ALL ((size_t)-1)
514 :
515 : BEGIN_EXTERN_C()
516 : ZEND_ATTRIBUTE_DEPRECATED
517 : PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC);
518 : ZEND_ATTRIBUTE_DEPRECATED
519 : PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC);
520 : /* Preserve "characters" semantics by having maxlen refer to maxchars in a unicode context */
521 : #define php_stream_copy_to_stream(src, dest, maxlen) ( ((src)->readbuf_type == IS_STRING) \
522 : ? _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC) \
523 : : _php_stream_ucopy_to_stream((src), (dest), -1, (maxlen) STREAMS_CC TSRMLS_CC) )
524 :
525 : PHPAPI size_t _php_stream_ucopy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars, size_t *len STREAMS_DC TSRMLS_DC);
526 : PHPAPI size_t _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC TSRMLS_DC);
527 : /* Preserve "characters" semantics by having maxlen refer to maxchars in a unicode context */
528 : #define php_stream_copy_to_stream_ex(src, dest, maxlen, len) ( ((src)->readbuf_type == IS_STRING) \
529 : ? _php_stream_copy_to_stream_ex((src), (dest), (maxlen), (len) STREAMS_CC TSRMLS_CC) \
530 : : _php_stream_ucopy_to_stream_ex((src), (dest), -1, (maxlen), (len) STREAMS_CC TSRMLS_CC) )
531 :
532 : /* read all data from stream and put into a buffer. Caller must free buffer when done.
533 : * The copy will use mmap if available. */
534 : PHPAPI size_t _php_stream_copy_to_mem_ex(php_stream *src, zend_uchar rettype, void **buf, size_t maxlen, size_t maxchars,
535 : int persistent STREAMS_DC TSRMLS_DC);
536 : #define php_stream_copy_to_mem(src, buf, maxlen, persistent) \
537 : _php_stream_copy_to_mem_ex((src), IS_STRING, (buf), (maxlen), -1, (persistent) STREAMS_CC TSRMLS_CC)
538 : #define php_stream_copy_to_mem_ex(src, rettype, buf, maxlen, maxchars, persistent) \
539 : _php_stream_copy_to_mem_ex((src), (rettype), (buf), (maxlen), (maxchars), (persistent) STREAMS_CC TSRMLS_CC)
540 :
541 : /* output all data from a stream */
542 : PHPAPI size_t _php_stream_passthru(php_stream * src STREAMS_DC TSRMLS_DC);
543 : #define php_stream_passthru(stream) _php_stream_passthru((stream) STREAMS_CC TSRMLS_CC)
544 : END_EXTERN_C()
545 :
546 : #include "streams/php_stream_transport.h"
547 : #include "streams/php_stream_plain_wrapper.h"
548 : #include "streams/php_stream_glob_wrapper.h"
549 : #include "streams/php_stream_userspace.h"
550 : #include "streams/php_stream_mmap.h"
551 :
552 : /* coerce the stream into some other form */
553 : /* cast as a stdio FILE * */
554 : #define PHP_STREAM_AS_STDIO 0
555 : /* cast as a POSIX fd or socketd */
556 : #define PHP_STREAM_AS_FD 1
557 : /* cast as a socketd */
558 : #define PHP_STREAM_AS_SOCKETD 2
559 : /* cast as fd/socket for select purposes */
560 : #define PHP_STREAM_AS_FD_FOR_SELECT 3
561 :
562 : /* try really, really hard to make sure the cast happens (avoid using this flag if possible) */
563 : #define PHP_STREAM_CAST_TRY_HARD 0x80000000
564 : #define PHP_STREAM_CAST_RELEASE 0x40000000 /* stream becomes invalid on success */
565 : #define PHP_STREAM_CAST_INTERNAL 0x20000000 /* stream cast for internal use */
566 : #define PHP_STREAM_CAST_MASK (PHP_STREAM_CAST_TRY_HARD | PHP_STREAM_CAST_RELEASE | PHP_STREAM_CAST_INTERNAL)
567 : BEGIN_EXTERN_C()
568 : PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err TSRMLS_DC);
569 : END_EXTERN_C()
570 : /* use this to check if a stream can be cast into another form */
571 : #define php_stream_can_cast(stream, as) _php_stream_cast((stream), (as), NULL, 0 TSRMLS_CC)
572 : #define php_stream_cast(stream, as, ret, show_err) _php_stream_cast((stream), (as), (ret), (show_err) TSRMLS_CC)
573 :
574 : /* use this to check if a stream is of a particular type:
575 : * PHPAPI int php_stream_is(php_stream *stream, php_stream_ops *ops); */
576 : #define php_stream_is(stream, anops) ((stream)->ops == anops)
577 : #define PHP_STREAM_IS_STDIO &php_stream_stdio_ops
578 :
579 : #define php_stream_is_persistent(stream) (stream)->is_persistent
580 :
581 : /* Wrappers support */
582 :
583 : #define IGNORE_PATH 0x00000000
584 : #define USE_PATH 0x00000001
585 : #define IGNORE_URL 0x00000002
586 : #define ENFORCE_SAFE_MODE 0x00000004
587 : #define REPORT_ERRORS 0x00000008
588 :
589 : /* If you don't need to write to the stream, but really need to
590 : * be able to seek, use this flag in your options. */
591 : #define STREAM_MUST_SEEK 0x00000010
592 : /* If you are going to end up casting the stream into a FILE* or
593 : * a socket, pass this flag and the streams/wrappers will not use
594 : * buffering mechanisms while reading the headers, so that HTTP
595 : * wrapped streams will work consistently.
596 : * If you omit this flag, streams will use buffering and should end
597 : * up working more optimally.
598 : * */
599 : #define STREAM_WILL_CAST 0x00000020
600 :
601 : /* this flag applies to php_stream_locate_url_wrapper */
602 : #define STREAM_LOCATE_WRAPPERS_ONLY 0x00000040
603 :
604 : /* this flag is only used by include/require functions */
605 : #define STREAM_OPEN_FOR_INCLUDE 0x00000080
606 :
607 : /* this flag tells streams to ONLY open urls */
608 : #define STREAM_USE_URL 0x00000100
609 :
610 : /* this flag is used when only the headers from HTTP request are to be fetched */
611 : #define STREAM_ONLY_GET_HEADERS 0x00000200
612 :
613 : /* don't apply open_basedir checks */
614 : #define STREAM_DISABLE_OPEN_BASEDIR 0x00000400
615 :
616 : /* get (or create) a persistent version of the stream */
617 : #define STREAM_OPEN_PERSISTENT 0x00000800
618 :
619 : /* use glob stream for directory open in plain files stream */
620 : #define STREAM_USE_GLOB_DIR_OPEN 0x00001000
621 :
622 : /* don't check allow_url_fopen and allow_url_include */
623 : #define STREAM_DISABLE_URL_PROTECTION 0x00002000
624 :
625 : /* assume the path passed in exists and is fully expanded, avoiding syscalls */
626 : #define STREAM_ASSUME_REALPATH 0x00004000
627 :
628 : /* Antique - no longer has meaning */
629 : #define IGNORE_URL_WIN 0
630 :
631 : int php_init_stream_wrappers(int module_number TSRMLS_DC);
632 : int php_shutdown_stream_wrappers(int module_number TSRMLS_DC);
633 : void php_shutdown_stream_hashes(TSRMLS_D);
634 : PHP_RSHUTDOWN_FUNCTION(streams);
635 :
636 : BEGIN_EXTERN_C()
637 : PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
638 : PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC);
639 : PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
640 : PHPAPI int php_unregister_url_stream_wrapper_volatile(char *protocol TSRMLS_DC);
641 : PHPAPI void php_stream_fix_encoding(php_stream *stream, const char *mode, php_stream_context *context TSRMLS_DC);
642 : PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
643 : PHPAPI php_stream *_php_stream_u_open_wrapper(zend_uchar type, zstr path, int path_len, char *mode, int options, zstr *opened_path, int *opened_path_len, php_stream_context *context STREAMS_DC TSRMLS_DC);
644 : PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC);
645 : PHPAPI void *php_stream_locate_eol(php_stream *stream, zstr zbuf, int buf_len TSRMLS_DC);
646 :
647 : #define php_stream_open_wrapper(path, mode, options, opened) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_CC TSRMLS_CC)
648 : #define php_stream_open_wrapper_ex(path, mode, options, opened, context) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), (context) STREAMS_CC TSRMLS_CC)
649 :
650 : #define php_stream_u_open_wrapper(type, path, path_len, mode, options, opened, context) \
651 : _php_stream_u_open_wrapper((type), (path), (path_len), (mode), (options), (opened), NULL, (context) STREAMS_CC TSRMLS_CC)
652 : #define php_stream_u_open_wrapper_ex(type, path, path_len, mode, options, opened, opened_len, context) \
653 : _php_stream_u_open_wrapper((type), (path), (path_len), (mode), (options), (opened), (opened_len), (context) STREAMS_CC TSRMLS_CC)
654 :
655 : #define php_stream_get_from_zval(stream, zstream, mode, options, opened, context) \
656 : if (Z_TYPE_PP((zstream)) == IS_RESOURCE) { \
657 : php_stream_from_zval((stream), (zstream)); \
658 : } else (stream) = Z_TYPE_PP((zstream)) == IS_STRING ? \
659 : php_stream_open_wrapper_ex(Z_STRVAL_PP((zstream)), (mode), (options), (opened), (context)) : NULL
660 :
661 : /* pushes an error message onto the stack for a wrapper instance */
662 : PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options TSRMLS_DC, const char *fmt, ...);
663 :
664 :
665 : #define PHP_STREAM_UNCHANGED 0 /* orig stream was seekable anyway */
666 : #define PHP_STREAM_RELEASED 1 /* newstream should be used; origstream is no longer valid */
667 : #define PHP_STREAM_FAILED 2 /* an error occurred while attempting conversion */
668 : #define PHP_STREAM_CRITICAL 3 /* an error occurred; origstream is in an unknown state; you should close origstream */
669 : #define PHP_STREAM_NO_PREFERENCE 0
670 : #define PHP_STREAM_PREFER_STDIO 1
671 : #define PHP_STREAM_FORCE_CONVERSION 2
672 : /* DO NOT call this on streams that are referenced by resources! */
673 : PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC);
674 : #define php_stream_make_seekable(origstream, newstream, flags) _php_stream_make_seekable((origstream), (newstream), (flags) STREAMS_CC TSRMLS_CC)
675 :
676 : PHP_INI_MH(OnUpdateAllowUrl);
677 : PHPAPI int php_stream_wrapper_is_allowed(const char *wrapper, int wrapper_len, const char *setting TSRMLS_DC);
678 : #define php_stream_allow_url_fopen(wrapper, wrapper_len) php_stream_wrapper_is_allowed((wrapper), (wrapper_len), PG(in_user_include)?PG(allow_url_include_list):PG(allow_url_fopen_list) TSRMLS_CC)
679 : #define php_stream_allow_url_include(wrapper, wrapper_len) php_stream_wrapper_is_allowed((wrapper), (wrapper_len), PG(allow_url_include_list) TSRMLS_CC)
680 :
681 : /* Give other modules access to the url_stream_wrappers_hash and stream_filters_hash */
682 : PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D);
683 : #define php_stream_get_url_stream_wrappers_hash() _php_stream_get_url_stream_wrappers_hash(TSRMLS_C)
684 : PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void);
685 : PHPAPI HashTable *_php_get_stream_filters_hash(TSRMLS_D);
686 : #define php_get_stream_filters_hash() _php_get_stream_filters_hash(TSRMLS_C)
687 : PHPAPI HashTable *php_get_stream_filters_hash_global(void);
688 : extern php_stream_wrapper_ops *php_stream_user_wrapper_ops;
689 : END_EXTERN_C()
690 : #endif
691 :
692 : /* Definitions for user streams */
693 : #define PHP_STREAM_IS_URL 1
694 : /*
695 : * Local variables:
696 : * tab-width: 4
697 : * c-basic-offset: 4
698 : * End:
699 : * vim600: sw=4 ts=4 fdm=marker
700 : * vim<600: sw=4 ts=4
701 : */
|