You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

618 lines
31 KiB

25 years ago
24 years ago
25 years ago
25 years ago
24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2003 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. /* $Id$ */
  19. #ifndef PHP_STREAMS_H
  20. #define PHP_STREAMS_H
  21. #ifdef HAVE_SYS_TIME_H
  22. #include <sys/time.h>
  23. #endif
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. PHPAPI int php_file_le_stream(void);
  27. PHPAPI int php_file_le_pstream(void);
  28. /* {{{ Streams memory debugging stuff */
  29. #if ZEND_DEBUG
  30. /* these have more of a dependency on the definitions of the zend macros than
  31. * I would prefer, but doing it this way saves loads of idefs :-/ */
  32. # define STREAMS_D int __php_stream_call_depth ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC
  33. # define STREAMS_C 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC
  34. # define STREAMS_REL_C __php_stream_call_depth + 1 ZEND_FILE_LINE_CC, \
  35. __php_stream_call_depth ? __zend_orig_filename : __zend_filename, \
  36. __php_stream_call_depth ? __zend_orig_lineno : __zend_lineno
  37. # define STREAMS_DC , STREAMS_D
  38. # define STREAMS_CC , STREAMS_C
  39. # define STREAMS_REL_CC , STREAMS_REL_C
  40. #else
  41. # define STREAMS_D
  42. # define STREAMS_C
  43. # define STREAMS_REL_C
  44. # define STREAMS_DC
  45. # define STREAMS_CC
  46. # define STREAMS_REL_CC
  47. #endif
  48. /* these functions relay the file/line number information. They are depth aware, so they will pass
  49. * the ultimate ancestor, which is useful, because there can be several layers of calls */
  50. #define php_stream_alloc_rel(ops, thisptr, persistent, mode) _php_stream_alloc((ops), (thisptr), (persistent), (mode) STREAMS_REL_CC TSRMLS_CC)
  51. #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)
  52. #define php_stream_fopen_rel(filename, mode, opened, options) _php_stream_fopen((filename), (mode), (opened), (options) STREAMS_REL_CC TSRMLS_CC)
  53. #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)
  54. #define php_stream_fopen_from_file_rel(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_REL_CC TSRMLS_CC)
  55. #define php_stream_fopen_from_pipe_rel(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_REL_CC TSRMLS_CC)
  56. #define php_stream_fopen_tmpfile_rel() _php_stream_fopen_tmpfile(0 STREAMS_REL_CC TSRMLS_CC)
  57. #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)
  58. #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)
  59. #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)
  60. #define php_stream_make_seekable_rel(origstream, newstream, flags) _php_stream_make_seekable((origstream), (newstream), (flags) STREAMS_REL_CC TSRMLS_CC)
  61. /* }}} */
  62. /* The contents of the php_stream_ops and php_stream should only be accessed
  63. * using the functions/macros in this header.
  64. * If you need to get at something that doesn't have an API,
  65. * drop me a line <wez@thebrainroom.com> and we can sort out a way to do
  66. * it properly.
  67. *
  68. * The only exceptions to this rule are that stream implementations can use
  69. * the php_stream->abstract pointer to hold their context, and streams
  70. * opened via stream_open_wrappers can use the zval ptr in
  71. * php_stream->wrapperdata to hold meta data for php scripts to
  72. * retrieve using file_get_wrapper_data(). */
  73. typedef struct _php_stream php_stream;
  74. typedef struct _php_stream_wrapper php_stream_wrapper;
  75. typedef struct _php_stream_context php_stream_context;
  76. typedef struct _php_stream_filter php_stream_filter;
  77. /* callback for status notifications */
  78. typedef void (*php_stream_notification_func)(php_stream_context *context,
  79. int notifycode, int severity,
  80. char *xmsg, int xcode,
  81. size_t bytes_sofar, size_t bytes_max,
  82. void * ptr TSRMLS_DC);
  83. typedef struct _php_stream_statbuf {
  84. #if defined(NETWARE) && defined(CLIB_STAT_PATCH)
  85. struct stat_libc sb; /* regular info */
  86. #else
  87. struct stat sb; /* regular info */
  88. #endif
  89. /* extended info to go here some day: content-type etc. etc. */
  90. } php_stream_statbuf;
  91. typedef struct _php_stream_dirent {
  92. char d_name[MAXPATHLEN];
  93. } php_stream_dirent;
  94. #define PHP_STREAM_NOTIFIER_PROGRESS 1
  95. typedef struct _php_stream_notifier {
  96. php_stream_notification_func func;
  97. void *ptr;
  98. int mask;
  99. size_t progress, progress_max; /* position for progress notification */
  100. } php_stream_notifier;
  101. struct _php_stream_context {
  102. php_stream_notifier *notifier;
  103. zval *options; /* hash keyed by wrapper family or specific wrapper */
  104. };
  105. typedef struct _php_stream_wrapper_options {
  106. int valid_options; /* PHP_STREAM_WRAPPER_OPT_XXX */
  107. php_stream_notification_func notifier;
  108. void *notifier_ptr;
  109. /* other info like user-agent, SSL cert and cookie information
  110. * to go here some day */
  111. } php_stream_wrapper_options;
  112. /* operations on streams that are file-handles */
  113. typedef struct _php_stream_ops {
  114. /* stdio like functions - these are mandatory! */
  115. size_t (*write)(php_stream *stream, const char *buf, size_t count TSRMLS_DC);
  116. size_t (*read)(php_stream *stream, char *buf, size_t count TSRMLS_DC);
  117. int (*close)(php_stream *stream, int close_handle TSRMLS_DC);
  118. int (*flush)(php_stream *stream TSRMLS_DC);
  119. const char *label; /* label for this ops structure */
  120. /* these are optional */
  121. int (*seek)(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC);
  122. int (*cast)(php_stream *stream, int castas, void **ret TSRMLS_DC);
  123. int (*stat)(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
  124. int (*set_option)(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
  125. } php_stream_ops;
  126. typedef struct _php_stream_wrapper_ops {
  127. /* open/create a wrapped stream */
  128. php_stream *(*stream_opener)(php_stream_wrapper *wrapper, char *filename, char *mode,
  129. int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
  130. /* close/destroy a wrapped stream */
  131. int (*stream_closer)(php_stream_wrapper *wrapper, php_stream *stream TSRMLS_DC);
  132. /* stat a wrapped stream */
  133. int (*stream_stat)(php_stream_wrapper *wrapper, php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
  134. /* stat a URL */
  135. int (*url_stat)(php_stream_wrapper *wrapper, char *url, php_stream_statbuf *ssb TSRMLS_DC);
  136. /* open a "directory" stream */
  137. php_stream *(*dir_opener)(php_stream_wrapper *wrapper, char *filename, char *mode,
  138. int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
  139. const char *label;
  140. } php_stream_wrapper_ops;
  141. struct _php_stream_wrapper {
  142. php_stream_wrapper_ops *wops; /* operations the wrapper can perform */
  143. void *abstract; /* context for the wrapper */
  144. int is_url; /* so that PG(allow_url_fopen) can be respected */
  145. /* support for wrappers to return (multiple) error messages to the stream opener */
  146. int err_count;
  147. char **err_stack;
  148. };
  149. typedef struct _php_stream_filter_ops {
  150. size_t (*write)(php_stream *stream, php_stream_filter *thisfilter,
  151. const char *buf, size_t count TSRMLS_DC);
  152. size_t (*read)(php_stream *stream, php_stream_filter *thisfilter,
  153. char *buf, size_t count TSRMLS_DC);
  154. int (*flush)(php_stream *stream, php_stream_filter *thisfilter, int closing TSRMLS_DC);
  155. int (*eof)(php_stream *stream, php_stream_filter *thisfilter TSRMLS_DC);
  156. void (*dtor)(php_stream_filter *thisfilter TSRMLS_DC);
  157. const char *label;
  158. } php_stream_filter_ops;
  159. struct _php_stream_filter {
  160. php_stream_filter_ops *fops;
  161. void *abstract; /* for use by filter implementation */
  162. php_stream_filter *next;
  163. php_stream_filter *prev;
  164. int is_persistent;
  165. php_stream *stream;
  166. };
  167. #define php_stream_filter_write_next(stream, thisfilter, buf, size) \
  168. (thisfilter)->next ? (thisfilter)->next->fops->write((stream), (thisfilter)->next, (buf), (size) TSRMLS_CC) \
  169. : (stream)->ops->write((stream), (buf), (size) TSRMLS_CC)
  170. #define php_stream_filter_read_next(stream, thisfilter, buf, size) \
  171. (thisfilter)->next ? (thisfilter)->next->fops->read((stream), (thisfilter)->next, (buf), (size) TSRMLS_CC) \
  172. : (stream)->ops->read((stream), (buf), (size) TSRMLS_CC)
  173. #define php_stream_filter_flush_next(stream, thisfilter, closing) \
  174. (thisfilter)->next ? (thisfilter)->next->fops->flush((stream), (thisfilter)->next, (closing) TSRMLS_CC) \
  175. : (stream)->ops->flush((stream) TSRMLS_CC)
  176. #define php_stream_filter_eof_next(stream, thisfilter) \
  177. (thisfilter)->next ? (thisfilter)->next->fops->eof((stream), (thisfilter)->next TSRMLS_CC) \
  178. : (stream)->ops->read((stream), NULL, 0 TSRMLS_CC) == EOF ? 1 : 0
  179. #define PHP_STREAM_FLAG_NO_SEEK 1
  180. #define PHP_STREAM_FLAG_NO_BUFFER 2
  181. #define PHP_STREAM_FLAG_EOL_UNIX 0 /* also includes DOS */
  182. #define PHP_STREAM_FLAG_DETECT_EOL 4
  183. #define PHP_STREAM_FLAG_EOL_MAC 8
  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. struct _php_stream {
  190. php_stream_ops *ops;
  191. void *abstract; /* convenience pointer for abstraction */
  192. php_stream_filter *filterhead;
  193. php_stream_filter *filtertail;
  194. php_stream_wrapper *wrapper; /* which wrapper was used to open the stream */
  195. void *wrapperthis; /* convenience pointer for a instance of a wrapper */
  196. zval *wrapperdata; /* fgetwrapperdata retrieves this */
  197. int fgetss_state; /* for fgetss to handle multiline tags */
  198. int is_persistent;
  199. char mode[16]; /* "rwb" etc. ala stdio */
  200. int rsrc_id; /* used for auto-cleanup */
  201. int in_free; /* to prevent recursion during free */
  202. /* so we know how to clean it up correctly. This should be set to
  203. * PHP_STREAM_FCLOSE_XXX as appropriate */
  204. int fclose_stdiocast;
  205. FILE *stdiocast; /* cache this, otherwise we might leak! */
  206. #if ZEND_DEBUG
  207. int __exposed; /* non-zero if exposed as a zval somewhere */
  208. char *__orig_path; /* it really helps when debugging "unclosed" streams */
  209. #endif
  210. php_stream_context *context;
  211. int flags; /* PHP_STREAM_FLAG_XXX */
  212. /* buffer */
  213. off_t position; /* of underlying stream */
  214. unsigned char *readbuf;
  215. size_t readbuflen;
  216. off_t readpos;
  217. off_t writepos;
  218. /* how much data to read when filling buffer */
  219. size_t chunk_size;
  220. int eof;
  221. }; /* php_stream */
  222. /* state definitions when closing down; these are private to streams.c */
  223. #define PHP_STREAM_FCLOSE_NONE 0
  224. #define PHP_STREAM_FCLOSE_FDOPEN 1
  225. #define PHP_STREAM_FCLOSE_FOPENCOOKIE 2
  226. /* allocate a new stream for a particular ops */
  227. PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract,
  228. const char *persistent_id, const char *mode STREAMS_DC TSRMLS_DC);
  229. #define php_stream_alloc(ops, thisptr, persistent_id, mode) _php_stream_alloc((ops), (thisptr), (persistent_id), (mode) STREAMS_CC TSRMLS_CC)
  230. /* stack filter onto a stream */
  231. PHPAPI void php_stream_filter_prepend(php_stream *stream, php_stream_filter *filter);
  232. PHPAPI void php_stream_filter_append(php_stream *stream, php_stream_filter *filter);
  233. PHPAPI php_stream_filter *php_stream_filter_remove(php_stream *stream, php_stream_filter *filter, int call_dtor TSRMLS_DC);
  234. PHPAPI void php_stream_filter_free(php_stream_filter *filter TSRMLS_DC);
  235. PHPAPI php_stream_filter *_php_stream_filter_alloc(php_stream_filter_ops *fops, void *abstract, int persistent STREAMS_DC TSRMLS_DC);
  236. #define php_stream_filter_alloc(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_CC TSRMLS_CC)
  237. #define php_stream_filter_alloc_rel(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_REL_CC TSRMLS_CC)
  238. #define php_stream_filter_remove_head(stream, call_dtor) php_stream_filter_remove((stream), (stream)->filterhead, (call_dtor) TSRMLS_CC)
  239. #define php_stream_filter_remove_tail(stream, call_dtor) php_stream_filter_remove((stream), (stream)->filtertail, (call_dtor) TSRMLS_CC)
  240. typedef struct _php_stream_filter_factory {
  241. php_stream_filter *(*create_filter)(const char *filtername, const char *filterparams, int filterparamslen, int persistent TSRMLS_DC);
  242. } php_stream_filter_factory;
  243. PHPAPI int php_stream_filter_register_factory(const char *filterpattern, php_stream_filter_factory *factory TSRMLS_DC);
  244. PHPAPI int php_stream_filter_unregister_factory(const char *filterpattern TSRMLS_DC);
  245. PHPAPI php_stream_filter *php_stream_filter_create(const char *filtername, const char *filterparams, int filterparamslen, int persistent TSRMLS_DC);
  246. #define php_stream_get_resource_id(stream) (stream)->rsrc_id
  247. #if ZEND_DEBUG
  248. /* use this to tell the stream that it is OK if we don't explicitly close it */
  249. # define php_stream_auto_cleanup(stream) { (stream)->__exposed++; }
  250. /* use this to assign the stream to a zval and tell the stream that is
  251. * has been exported to the engine; it will expect to be closed automatically
  252. * when the resources are auto-destructed */
  253. # define php_stream_to_zval(stream, zval) { ZVAL_RESOURCE(zval, (stream)->rsrc_id); (stream)->__exposed++; }
  254. #else
  255. # define php_stream_auto_cleanup(stream) /* nothing */
  256. # define php_stream_to_zval(stream, zval) { ZVAL_RESOURCE(zval, (stream)->rsrc_id); }
  257. #endif
  258. #define php_stream_from_zval(str, ppzval) ZEND_FETCH_RESOURCE2((str), php_stream *, (ppzval), -1, "stream", php_file_le_stream(), php_file_le_pstream())
  259. #define php_stream_from_zval_no_verify(str, ppzval) (str) = (php_stream*)zend_fetch_resource((ppzval) TSRMLS_CC, -1, "stream", NULL, 2, php_file_le_stream(), php_file_le_pstream())
  260. PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream TSRMLS_DC);
  261. #define PHP_STREAM_PERSISTENT_SUCCESS 0 /* id exists */
  262. #define PHP_STREAM_PERSISTENT_FAILURE 1 /* id exists but is not a stream! */
  263. #define PHP_STREAM_PERSISTENT_NOT_EXIST 2 /* id does not exist */
  264. #define PHP_STREAM_FREE_CALL_DTOR 1 /* call ops->close */
  265. #define PHP_STREAM_FREE_RELEASE_STREAM 2 /* pefree(stream) */
  266. #define PHP_STREAM_FREE_PRESERVE_HANDLE 4 /* tell ops->close to not close it's underlying handle */
  267. #define PHP_STREAM_FREE_RSRC_DTOR 8 /* called from the resource list dtor */
  268. #define PHP_STREAM_FREE_CLOSE (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)
  269. #define PHP_STREAM_FREE_CLOSE_CASTED (PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_PRESERVE_HANDLE)
  270. PHPAPI int _php_stream_free(php_stream *stream, int close_options TSRMLS_DC);
  271. #define php_stream_free(stream, close_options) _php_stream_free((stream), (close_options) TSRMLS_CC)
  272. #define php_stream_close(stream) _php_stream_free((stream), PHP_STREAM_FREE_CLOSE TSRMLS_CC)
  273. PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC);
  274. #define php_stream_rewind(stream) _php_stream_seek((stream), 0L, SEEK_SET TSRMLS_CC)
  275. #define php_stream_seek(stream, offset, whence) _php_stream_seek((stream), (offset), (whence) TSRMLS_CC)
  276. PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC);
  277. #define php_stream_tell(stream) _php_stream_tell((stream) TSRMLS_CC)
  278. PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC);
  279. #define php_stream_read(stream, buf, count) _php_stream_read((stream), (buf), (count) TSRMLS_CC)
  280. PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC);
  281. #define php_stream_write_string(stream, str) _php_stream_write(stream, str, strlen(str) TSRMLS_CC)
  282. #define php_stream_write(stream, buf, count) _php_stream_write(stream, (buf), (count) TSRMLS_CC)
  283. PHPAPI size_t _php_stream_printf(php_stream *stream TSRMLS_DC, const char *fmt, ...);
  284. /* php_stream_printf macro & function require TSRMLS_CC */
  285. #define php_stream_printf _php_stream_printf
  286. PHPAPI int _php_stream_eof(php_stream *stream TSRMLS_DC);
  287. #define php_stream_eof(stream) _php_stream_eof((stream) TSRMLS_CC)
  288. PHPAPI int _php_stream_getc(php_stream *stream TSRMLS_DC);
  289. #define php_stream_getc(stream) _php_stream_getc((stream) TSRMLS_CC)
  290. PHPAPI int _php_stream_putc(php_stream *stream, int c TSRMLS_DC);
  291. #define php_stream_putc(stream, c) _php_stream_putc((stream), (c) TSRMLS_CC)
  292. PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC);
  293. #define php_stream_flush(stream) _php_stream_flush((stream), 0 TSRMLS_CC)
  294. PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen, size_t *returned_len TSRMLS_DC);
  295. #define php_stream_gets(stream, buf, maxlen) _php_stream_get_line((stream), (buf), (maxlen), NULL TSRMLS_CC)
  296. #define php_stream_get_line(stream, buf, maxlen, retlen) _php_stream_get_line((stream), (buf), (maxlen), (retlen) TSRMLS_CC)
  297. /* CAREFUL! this is equivalent to puts NOT fputs! */
  298. PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC);
  299. #define php_stream_puts(stream, buf) _php_stream_puts((stream), (buf) TSRMLS_CC)
  300. PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC);
  301. #define php_stream_stat(stream, ssb) _php_stream_stat((stream), (ssb) TSRMLS_CC)
  302. PHPAPI int _php_stream_stat_path(char *path, php_stream_statbuf *ssb TSRMLS_DC);
  303. #define php_stream_stat_path(path, ssb) _php_stream_stat_path((path), (ssb) TSRMLS_CC)
  304. PHPAPI php_stream *_php_stream_opendir(char *path, int options, php_stream_context *context STREAMS_DC TSRMLS_DC);
  305. #define php_stream_opendir(path, options, context) _php_stream_opendir((path), (options), (context) STREAMS_CC TSRMLS_CC)
  306. PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent TSRMLS_DC);
  307. #define php_stream_readdir(dirstream, dirent) _php_stream_readdir((dirstream), (dirent) TSRMLS_CC)
  308. #define php_stream_closedir(dirstream) php_stream_close((dirstream))
  309. #define php_stream_rewinddir(dirstream) php_stream_rewind((dirstream))
  310. PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC);
  311. #define php_stream_set_option(stream, option, value, ptrvalue) _php_stream_set_option((stream), (option), (value), (ptrvalue) TSRMLS_CC)
  312. #define php_stream_set_chunk_size(stream, size) _php_stream_set_option((stream), PHP_STREAM_OPTION_SET_CHUNK_SIZE, (size), NULL TSRMLS_CC)
  313. /* change the blocking mode of stream: value == 1 => blocking, value == 0 => non-blocking. */
  314. #define PHP_STREAM_OPTION_BLOCKING 1
  315. /* change the buffering mode of stream. value is a PHP_STREAM_BUFFER_XXXX value, ptrparam is a ptr to a size_t holding
  316. * the required buffer size */
  317. #define PHP_STREAM_OPTION_READ_BUFFER 2
  318. #define PHP_STREAM_OPTION_WRITE_BUFFER 3
  319. #define PHP_STREAM_BUFFER_NONE 0 /* unbuffered */
  320. #define PHP_STREAM_BUFFER_LINE 1 /* line buffered */
  321. #define PHP_STREAM_BUFFER_FULL 2 /* fully buffered */
  322. /* set the timeout duration for reads on the stream. ptrparam is a pointer to a struct timeval * */
  323. #define PHP_STREAM_OPTION_READ_TIMEOUT 4
  324. #define PHP_STREAM_OPTION_SET_CHUNK_SIZE 5
  325. #define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
  326. #define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
  327. #define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */
  328. /* copy up to maxlen bytes from src to dest. If maxlen is PHP_STREAM_COPY_ALL, copy until eof(src).
  329. * Uses mmap if the src is a plain file and at offset 0 */
  330. #define PHP_STREAM_COPY_ALL -1
  331. PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC);
  332. #define php_stream_copy_to_stream(src, dest, maxlen) _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC)
  333. /* read all data from stream and put into a buffer. Caller must free buffer when done.
  334. * The copy will use mmap if available. */
  335. PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen,
  336. int persistent STREAMS_DC TSRMLS_DC);
  337. #define php_stream_copy_to_mem(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_CC TSRMLS_CC)
  338. /* output all data from a stream */
  339. PHPAPI size_t _php_stream_passthru(php_stream * src STREAMS_DC TSRMLS_DC);
  340. #define php_stream_passthru(stream) _php_stream_passthru((stream) STREAMS_CC TSRMLS_CC)
  341. /* operations for a stdio FILE; use the php_stream_fopen_XXX funcs below */
  342. PHPAPI extern php_stream_ops php_stream_stdio_ops;
  343. /* like fopen, but returns a stream */
  344. PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC TSRMLS_DC);
  345. #define php_stream_fopen(filename, mode, opened) _php_stream_fopen((filename), (mode), (opened), 0 STREAMS_CC TSRMLS_CC)
  346. PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path, int options STREAMS_DC TSRMLS_DC);
  347. #define php_stream_fopen_with_path(filename, mode, path, opened) _php_stream_fopen_with_path((filename), (mode), (path), (opened) STREAMS_CC TSRMLS_CC)
  348. PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC TSRMLS_DC);
  349. #define php_stream_fopen_from_file(file, mode) _php_stream_fopen_from_file((file), (mode) STREAMS_CC TSRMLS_CC)
  350. PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC TSRMLS_DC);
  351. #define php_stream_fopen_from_pipe(file, mode) _php_stream_fopen_from_pipe((file), (mode) STREAMS_CC TSRMLS_CC)
  352. PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC);
  353. #define php_stream_fopen_tmpfile() _php_stream_fopen_tmpfile(0 STREAMS_CC TSRMLS_CC)
  354. PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC);
  355. #define php_stream_fopen_temporary_file(dir, pfx, opened_path) _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC TSRMLS_CC)
  356. /* coerce the stream into some other form */
  357. /* cast as a stdio FILE * */
  358. #define PHP_STREAM_AS_STDIO 0
  359. /* cast as a POSIX fd or socketd */
  360. #define PHP_STREAM_AS_FD 1
  361. /* cast as a socketd */
  362. #define PHP_STREAM_AS_SOCKETD 2
  363. /* try really, really hard to make sure the cast happens (avoid using this flag if possible) */
  364. #define PHP_STREAM_CAST_TRY_HARD 0x80000000
  365. #define PHP_STREAM_CAST_RELEASE 0x40000000 /* stream becomes invalid on success */
  366. #define PHP_STREAM_CAST_INTERNAL 0x20000000 /* stream cast for internal use */
  367. #define PHP_STREAM_CAST_MASK (PHP_STREAM_CAST_TRY_HARD | PHP_STREAM_CAST_RELEASE | PHP_STREAM_CAST_INTERNAL)
  368. PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err TSRMLS_DC);
  369. /* use this to check if a stream can be cast into another form */
  370. #define php_stream_can_cast(stream, as) _php_stream_cast((stream), (as), NULL, 0 TSRMLS_CC)
  371. #define php_stream_cast(stream, as, ret, show_err) _php_stream_cast((stream), (as), (ret), (show_err) TSRMLS_CC)
  372. /* use this to check if a stream is of a particular type:
  373. * PHPAPI int php_stream_is(php_stream *stream, php_stream_ops *ops); */
  374. #define php_stream_is(stream, anops) ((stream)->ops == anops)
  375. #define PHP_STREAM_IS_STDIO &php_stream_stdio_ops
  376. #define php_stream_is_persistent(stream) (stream)->is_persistent
  377. /* Wrappers support */
  378. #define IGNORE_PATH 0
  379. #define USE_PATH 1
  380. #define IGNORE_URL 2
  381. /* There's no USE_URL. */
  382. #define ENFORCE_SAFE_MODE 4
  383. #define REPORT_ERRORS 8
  384. /* If you don't need to write to the stream, but really need to
  385. * be able to seek, use this flag in your options. */
  386. #define STREAM_MUST_SEEK 16
  387. /* If you are going to end up casting the stream into a FILE* or
  388. * a socket, pass this flag and the streams/wrappers will not use
  389. * buffering mechanisms while reading the headers, so that HTTP
  390. * wrapped streams will work consistently.
  391. * If you omit this flag, streams will use buffering and should end
  392. * up working more optimally.
  393. * */
  394. #define STREAM_WILL_CAST 32
  395. /* this flag applies to php_stream_locate_url_wrapper */
  396. #define STREAM_LOCATE_WRAPPERS_ONLY 64
  397. /* this flag is only used by include/require functions */
  398. #define STREAM_OPEN_FOR_INCLUDE 128
  399. /* Antique - no longer has meaning */
  400. #define IGNORE_URL_WIN 0
  401. int php_init_stream_wrappers(int module_number TSRMLS_DC);
  402. int php_shutdown_stream_wrappers(int module_number TSRMLS_DC);
  403. PHP_RSHUTDOWN_FUNCTION(streams);
  404. PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
  405. PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC);
  406. 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);
  407. PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC);
  408. PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC);
  409. #define php_stream_open_wrapper(path, mode, options, opened) _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_CC TSRMLS_CC)
  410. #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)
  411. /* pushes an error message onto the stack for a wrapper instance */
  412. PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options TSRMLS_DC, const char *fmt, ...);
  413. #define PHP_STREAM_UNCHANGED 0 /* orig stream was seekable anyway */
  414. #define PHP_STREAM_RELEASED 1 /* newstream should be used; origstream is no longer valid */
  415. #define PHP_STREAM_FAILED 2 /* an error occurred while attempting conversion */
  416. #define PHP_STREAM_CRITICAL 3 /* an error occurred; origstream is in an unknown state; you should close origstream */
  417. #define PHP_STREAM_NO_PREFERENCE 0
  418. #define PHP_STREAM_PREFER_STDIO 1
  419. #define PHP_STREAM_FORCE_CONVERSION 2
  420. /* DO NOT call this on streams that are referenced by resources! */
  421. PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC);
  422. #define php_stream_make_seekable(origstream, newstream, flags) _php_stream_make_seekable((origstream), (newstream), (flags) STREAMS_CC TSRMLS_CC)
  423. /* This is a utility API for extensions that are opening a stream, converting it
  424. * to a FILE* and then closing it again. Be warned that fileno() on the result
  425. * will most likely fail on systems with fopencookie. */
  426. PHPAPI FILE * _php_stream_open_wrapper_as_file(char * path, char * mode, int options, char **opened_path STREAMS_DC TSRMLS_DC);
  427. #define php_stream_open_wrapper_as_file(path, mode, options, opened_path) _php_stream_open_wrapper_as_file((path), (mode), (options), (opened_path) STREAMS_CC TSRMLS_CC)
  428. /* for user-space streams */
  429. PHPAPI extern php_stream_ops php_stream_userspace_ops;
  430. PHPAPI extern php_stream_ops php_stream_userspace_dir_ops;
  431. #define PHP_STREAM_IS_USERSPACE &php_stream_userspace_ops
  432. #define PHP_STREAM_IS_USERSPACE_DIR &php_stream_userspace_dir_ops
  433. PHPAPI void php_stream_context_free(php_stream_context *context);
  434. PHPAPI php_stream_context *php_stream_context_alloc(void);
  435. PHPAPI int php_stream_context_get_option(php_stream_context *context,
  436. const char *wrappername, const char *optionname, zval ***optionvalue);
  437. PHPAPI int php_stream_context_set_option(php_stream_context *context,
  438. const char *wrappername, const char *optionname, zval *optionvalue);
  439. PHPAPI php_stream_notifier *php_stream_notification_alloc(void);
  440. PHPAPI void php_stream_notification_free(php_stream_notifier *notifier);
  441. /* not all notification codes are implemented */
  442. #define PHP_STREAM_NOTIFY_RESOLVE 1
  443. #define PHP_STREAM_NOTIFY_CONNECT 2
  444. #define PHP_STREAM_NOTIFY_AUTH_REQUIRED 3
  445. #define PHP_STREAM_NOTIFY_MIME_TYPE_IS 4
  446. #define PHP_STREAM_NOTIFY_FILE_SIZE_IS 5
  447. #define PHP_STREAM_NOTIFY_REDIRECTED 6
  448. #define PHP_STREAM_NOTIFY_PROGRESS 7
  449. #define PHP_STREAM_NOTIFY_COMPLETED 8
  450. #define PHP_STREAM_NOTIFY_FAILURE 9
  451. #define PHP_STREAM_NOTIFY_AUTH_RESULT 10
  452. #define PHP_STREAM_NOTIFY_SEVERITY_INFO 0
  453. #define PHP_STREAM_NOTIFY_SEVERITY_WARN 1
  454. #define PHP_STREAM_NOTIFY_SEVERITY_ERR 2
  455. PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
  456. char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC);
  457. PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context);
  458. #define php_stream_notify_info(context, code, xmsg, xcode) do { if ((context) && (context)->notifier) { \
  459. php_stream_notification_notify((context), (code), PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  460. (xmsg), (xcode), 0, 0, NULL TSRMLS_CC); } } while (0)
  461. #define php_stream_notify_progress(context, bsofar, bmax) do { if ((context) && (context)->notifier) { \
  462. php_stream_notification_notify((context), PHP_STREAM_NOTIFY_PROGRESS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  463. NULL, 0, (bsofar), (bmax), NULL TSRMLS_CC); } } while(0)
  464. #define php_stream_notify_progress_init(context, sofar, bmax) do { if ((context) && (context)->notifier) { \
  465. (context)->notifier->progress = (sofar); \
  466. (context)->notifier->progress_max = (bmax); \
  467. (context)->notifier->mask |= PHP_STREAM_NOTIFIER_PROGRESS; \
  468. php_stream_notify_progress((context), (sofar), (bmax)); } } while (0)
  469. #define php_stream_notify_progress_increment(context, dsofar, dmax) do { if ((context) && (context)->notifier && (context)->notifier->mask & PHP_STREAM_NOTIFIER_PROGRESS) { \
  470. (context)->notifier->progress += (dsofar); \
  471. (context)->notifier->progress_max += (dmax); \
  472. php_stream_notify_progress((context), (context)->notifier->progress, (context)->notifier->progress_max); } } while (0)
  473. #define php_stream_notify_file_size(context, file_size, xmsg, xcode) do { if ((context) && (context)->notifier) { \
  474. php_stream_notification_notify((context), PHP_STREAM_NOTIFY_FILE_SIZE_IS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  475. (xmsg), (xcode), 0, (file_size), NULL TSRMLS_CC); } } while(0)
  476. #define php_stream_notify_error(context, code, xmsg, xcode) do { if ((context) && (context)->notifier) {\
  477. php_stream_notification_notify((context), (code), PHP_STREAM_NOTIFY_SEVERITY_ERR, \
  478. (xmsg), (xcode), 0, 0, NULL TSRMLS_CC); } } while(0)
  479. /* Give other modules access to the url_stream_wrappers_hash and stream_filters_hash */
  480. PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash();
  481. PHPAPI HashTable *php_get_stream_filters_hash();
  482. #endif
  483. /*
  484. * Local variables:
  485. * tab-width: 4
  486. * c-basic-offset: 4
  487. * End:
  488. * vim600: sw=4 ts=4 fdm=marker
  489. * vim<600: sw=4 ts=4
  490. */