1 /* 2 +----------------------------------------------------------------------+ 3 | PHP Version 7 | 4 +----------------------------------------------------------------------+ 5 | Copyright (c) 1997-2018 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 | With suggestions from: | 17 | Moriyoshi Koizumi <moriyoshi@at.wakwak.com> | 18 | Sara Golemon <pollita@php.net> | 19 +----------------------------------------------------------------------+ 20 */ 21 22 /* The filter API works on the principle of "Bucket-Brigades". This is 23 * partially inspired by the Apache 2 method of doing things, although 24 * it is intentially a light-weight implementation. 25 * 26 * Each stream can have a chain of filters for reading and another for writing. 27 * 28 * When data is written to the stream, it is placed into a bucket and placed at 29 * the start of the input brigade. 30 * 31 * The first filter in the chain is invoked on the brigade and (depending on 32 * it's return value), the next filter is invoked and so on. 33 * */ 34 35 #define PHP_STREAM_FILTER_READ 0x0001 36 #define PHP_STREAM_FILTER_WRITE 0x0002 37 #define PHP_STREAM_FILTER_ALL (PHP_STREAM_FILTER_READ | PHP_STREAM_FILTER_WRITE) 38 39 typedef struct _php_stream_bucket php_stream_bucket; 40 typedef struct _php_stream_bucket_brigade php_stream_bucket_brigade; 41 42 struct _php_stream_bucket { 43 php_stream_bucket *next, *prev; 44 php_stream_bucket_brigade *brigade; 45 46 char *buf; 47 size_t buflen; 48 /* if non-zero, buf should be pefreed when the bucket is destroyed */ 49 uint8_t own_buf; 50 uint8_t is_persistent; 51 52 /* destroy this struct when refcount falls to zero */ 53 int refcount; 54 }; 55 56 struct _php_stream_bucket_brigade { 57 php_stream_bucket *head, *tail; 58 }; 59 60 typedef enum { 61 PSFS_ERR_FATAL, /* error in data stream */ 62 PSFS_FEED_ME, /* filter needs more data; stop processing chain until more is available */ 63 PSFS_PASS_ON /* filter generated output buckets; pass them on to next in chain */ 64 } php_stream_filter_status_t; 65 66 /* Buckets API. */ 67 BEGIN_EXTERN_C() 68 PHPAPI php_stream_bucket *php_stream_bucket_new(php_stream *stream, char *buf, size_t buflen, uint8_t own_buf, uint8_t buf_persistent); 69 PHPAPI int php_stream_bucket_split(php_stream_bucket *in, php_stream_bucket **left, php_stream_bucket **right, size_t length); 70 PHPAPI void php_stream_bucket_delref(php_stream_bucket *bucket); 71 #define php_stream_bucket_addref(bucket) (bucket)->refcount++ 72 PHPAPI void php_stream_bucket_prepend(php_stream_bucket_brigade *brigade, php_stream_bucket *bucket); 73 PHPAPI void php_stream_bucket_append(php_stream_bucket_brigade *brigade, php_stream_bucket *bucket); 74 PHPAPI void php_stream_bucket_unlink(php_stream_bucket *bucket); 75 PHPAPI php_stream_bucket *php_stream_bucket_make_writeable(php_stream_bucket *bucket); 76 END_EXTERN_C() 77 78 #define PSFS_FLAG_NORMAL 0 /* regular read/write */ 79 #define PSFS_FLAG_FLUSH_INC 1 /* an incremental flush */ 80 #define PSFS_FLAG_FLUSH_CLOSE 2 /* final flush prior to closing */ 81 82 typedef struct _php_stream_filter_ops { 83 84 php_stream_filter_status_t (*filter)( 85 php_stream *stream, 86 php_stream_filter *thisfilter, 87 php_stream_bucket_brigade *buckets_in, 88 php_stream_bucket_brigade *buckets_out, 89 size_t *bytes_consumed, 90 int flags 91 ); 92 93 void (*dtor)(php_stream_filter *thisfilter); 94 95 const char *label; 96 97 } php_stream_filter_ops; 98 99 typedef struct _php_stream_filter_chain { 100 php_stream_filter *head, *tail; 101 102 /* Owning stream */ 103 php_stream *stream; 104 } php_stream_filter_chain; 105 106 struct _php_stream_filter { 107 const php_stream_filter_ops *fops; 108 zval abstract; /* for use by filter implementation */ 109 php_stream_filter *next; 110 php_stream_filter *prev; 111 int is_persistent; 112 113 /* link into stream and chain */ 114 php_stream_filter_chain *chain; 115 116 /* buffered buckets */ 117 php_stream_bucket_brigade buffer; 118 119 /* filters are auto_registered when they're applied */ 120 zend_resource *res; 121 }; 122 123 /* stack filter onto a stream */ 124 BEGIN_EXTERN_C() 125 PHPAPI void _php_stream_filter_prepend(php_stream_filter_chain *chain, php_stream_filter *filter); 126 PHPAPI int php_stream_filter_prepend_ex(php_stream_filter_chain *chain, php_stream_filter *filter); 127 PHPAPI void _php_stream_filter_append(php_stream_filter_chain *chain, php_stream_filter *filter); 128 PHPAPI int php_stream_filter_append_ex(php_stream_filter_chain *chain, php_stream_filter *filter); 129 PHPAPI int _php_stream_filter_flush(php_stream_filter *filter, int finish); 130 PHPAPI php_stream_filter *php_stream_filter_remove(php_stream_filter *filter, int call_dtor); 131 PHPAPI void php_stream_filter_free(php_stream_filter *filter); 132 PHPAPI php_stream_filter *_php_stream_filter_alloc(const php_stream_filter_ops *fops, void *abstract, uint8_t persistent STREAMS_DC); 133 END_EXTERN_C() 134 #define php_stream_filter_alloc(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_CC) 135 #define php_stream_filter_alloc_rel(fops, thisptr, persistent) _php_stream_filter_alloc((fops), (thisptr), (persistent) STREAMS_REL_CC) 136 #define php_stream_filter_prepend(chain, filter) _php_stream_filter_prepend((chain), (filter)) 137 #define php_stream_filter_append(chain, filter) _php_stream_filter_append((chain), (filter)) 138 #define php_stream_filter_flush(filter, finish) _php_stream_filter_flush((filter), (finish)) 139 140 #define php_stream_is_filtered(stream) ((stream)->readfilters.head || (stream)->writefilters.head) 141 142 typedef struct _php_stream_filter_factory { 143 php_stream_filter *(*create_filter)(const char *filtername, zval *filterparams, uint8_t persistent); 144 } php_stream_filter_factory; 145 146 BEGIN_EXTERN_C() 147 PHPAPI int php_stream_filter_register_factory(const char *filterpattern, const php_stream_filter_factory *factory); 148 PHPAPI int php_stream_filter_unregister_factory(const char *filterpattern); 149 PHPAPI int php_stream_filter_register_factory_volatile(zend_string *filterpattern, const php_stream_filter_factory *factory); 150 PHPAPI php_stream_filter *php_stream_filter_create(const char *filtername, zval *filterparams, uint8_t persistent); 151 END_EXTERN_C() 152 153 /* 154 * Local variables: 155 * tab-width: 4 156 * c-basic-offset: 4 157 * End: 158 * vim600: sw=4 ts=4 fdm=marker 159 * vim<600: sw=4 ts=4 160 */ 161