php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zlib_fopen_wrapper.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Wez Furlong <wez@thebrainroom.com>, based on work by: |
14 | Hartmut Holzgraefe <hholzgra@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18#ifndef _GNU_SOURCE
19# define _GNU_SOURCE
20#endif
21
22#include "php.h"
23#include "php_zlib.h"
24#include "fopen_wrappers.h"
25
26#include "main/php_network.h"
27
32
33static ssize_t php_gziop_read(php_stream *stream, char *buf, size_t count)
34{
36 ssize_t total_read = 0;
37
38 /* Despite the count argument of gzread() being "unsigned int",
39 * the return value is "int". Error returns are values < 0, otherwise the count is returned.
40 * To properly distinguish error values from success value, we therefore need to cap at INT_MAX.
41 */
42 do {
43 unsigned int chunk_size = MIN(count, INT_MAX);
44 int read = gzread(self->gz_file, buf, chunk_size);
45 count -= chunk_size;
46
47 if (gzeof(self->gz_file)) {
48 stream->eof = 1;
49 }
50
51 if (UNEXPECTED(read < 0)) {
52 return read;
53 }
54
55 total_read += read;
56 buf += read;
57 } while (count > 0 && !stream->eof);
58
59 return total_read;
60}
61
62static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
63{
65 ssize_t total_written = 0;
66
67 /* Despite the count argument of gzread() being "unsigned int",
68 * the return value is "int". Error returns are values < 0, otherwise the count is returned.
69 * To properly distinguish error values from success value, we therefore need to cap at INT_MAX.
70 */
71 do {
72 unsigned int chunk_size = MIN(count, INT_MAX);
73 int written = gzwrite(self->gz_file, buf, chunk_size);
74 count -= chunk_size;
75
76 if (UNEXPECTED(written < 0)) {
77 return written;
78 }
79
80 total_written += written;
81 buf += written;
82 } while (count > 0);
83
84 return total_written;
85}
86
87static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
88{
90
91 assert(self != NULL);
92
93 if (whence == SEEK_END) {
94 php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
95 return -1;
96 }
97 *newoffs = gzseek(self->gz_file, offset, whence);
98
99 return (*newoffs < 0) ? -1 : 0;
100}
101
102static int php_gziop_close(php_stream *stream, int close_handle)
103{
104 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
105 int ret = EOF;
106
107 if (close_handle) {
108 if (self->gz_file) {
109 ret = gzclose(self->gz_file);
110 self->gz_file = NULL;
111 }
112 if (self->stream) {
114 self->stream = NULL;
115 }
116 }
117 efree(self);
118
119 return ret;
120}
121
122static int php_gziop_flush(php_stream *stream)
123{
124 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
125
126 return gzflush(self->gz_file, Z_SYNC_FLUSH);
127}
128
130 php_gziop_write, php_gziop_read,
131 php_gziop_close, php_gziop_flush,
132 "ZLIB",
133 php_gziop_seek,
134 NULL, /* cast */
135 NULL, /* stat */
136 NULL /* set_option */
137};
138
139php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
141{
142 struct php_gz_stream_data_t *self;
143 php_stream *stream = NULL, *innerstream = NULL;
144
145 /* sanity check the stream: it can be either read-only or write-only */
146 if (strchr(mode, '+')) {
147 if (options & REPORT_ERRORS) {
148 php_error_docref(NULL, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
149 }
150 return NULL;
151 }
152
153 if (strncasecmp("compress.zlib://", path, 16) == 0) {
154 path += 16;
155 } else if (strncasecmp("zlib:", path, 5) == 0) {
156 path += 5;
157 }
158
159 innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
160
161 if (innerstream) {
163
164 if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
165 self = emalloc(sizeof(*self));
166 self->stream = innerstream;
167 self->gz_file = gzdopen(dup(fd), mode);
168
169 if (self->gz_file) {
170 zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
171 if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
172 php_error(E_WARNING, "failed setting compression level");
173 }
174
176 if (stream) {
178 return stream;
179 }
180
181 gzclose(self->gz_file);
182 }
183
184 efree(self);
185 if (options & REPORT_ERRORS) {
186 php_error_docref(NULL, E_WARNING, "gzopen failed");
187 }
188 }
189
190 php_stream_close(innerstream);
191 }
192
193 return NULL;
194}
195
196static const php_stream_wrapper_ops gzip_stream_wops = {
198 NULL, /* close */
199 NULL, /* stat */
200 NULL, /* stat_url */
201 NULL, /* opendir */
202 "ZLIB",
203 NULL, /* unlink */
204 NULL, /* rename */
205 NULL, /* mkdir */
206 NULL, /* rmdir */
207 NULL
208};
209
211 &gzip_stream_wops,
212 NULL,
213 0, /* is_url */
214};
count(Countable|array $value, int $mode=COUNT_NORMAL)
assert(mixed $assertion, Throwable|string|null $description=null)
strchr(string $haystack, string $needle, bool $before_needle=false)
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
const SEEK_END
Definition file.stub.php:21
zend_long offset
char * mode
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
#define INT_MAX
Definition php.h:237
#define php_error
Definition php.h:310
PHP_JSON_API size_t int options
Definition php_json.h:102
int php_socket_t
PHPAPI zval * php_stream_context_get_option(php_stream_context *context, const char *wrappername, const char *optionname)
Definition streams.c:2407
#define php_stream_cast(stream, as, ret, show_err)
struct _php_stream_wrapper_ops php_stream_wrapper_ops
struct _php_stream php_stream
Definition php_streams.h:96
struct _php_stream_context php_stream_context
Definition php_streams.h:98
#define REPORT_ERRORS
#define STREAMS_DC
Definition php_streams.h:53
#define PHP_STREAM_FLAG_NO_BUFFER
#define php_stream_alloc_rel(ops, thisptr, persistent, mode)
Definition php_streams.h:60
#define php_stream_open_wrapper_ex(path, mode, options, opened, context)
#define php_stream_close(stream)
#define STREAM_WILL_CAST
struct _php_stream_ops php_stream_ops
struct _php_stream_wrapper php_stream_wrapper
Definition php_streams.h:97
#define PHP_STREAM_AS_FD
#define STREAM_MUST_SEEK
const php_stream_ops php_stream_gzio_ops
const php_stream_wrapper php_stream_gzip_wrapper
php_stream * php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
int fd
Definition phpdbg.h:282
uint16_t eof
uint32_t flags
void * abstract
Definition dce.c:49
#define efree(ptr)
Definition zend_alloc.h:155
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
#define strncasecmp(s1, s2, n)
#define E_WARNING
Definition zend_errors.h:24
int32_t zend_off_t
Definition zend_long.h:44
struct _zend_string zend_string
#define MIN(a, b)
#define UNEXPECTED(condition)
zval * ret
gzwrite($stream, string $data, ?int $length=null)
gzeof($stream)
gzread($stream, int $length)
gzclose($stream)
gzseek($stream, int $offset, int $whence=SEEK_SET)
php_stream * php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)