This source file includes following definitions.
- php_gziop_read
- php_gziop_write
- php_gziop_seek
- php_gziop_close
- php_gziop_flush
- php_stream_gzopen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #define _GNU_SOURCE
23
24 #include "php.h"
25 #include "php_zlib.h"
26 #include "fopen_wrappers.h"
27
28 #include "main/php_network.h"
29
30 struct php_gz_stream_data_t {
31 gzFile gz_file;
32 php_stream *stream;
33 };
34
35 static size_t php_gziop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
36 {
37 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
38 int read;
39
40 read = gzread(self->gz_file, buf, count);
41
42 if (gzeof(self->gz_file)) {
43 stream->eof = 1;
44 }
45
46 return (read < 0) ? 0 : read;
47 }
48
49 static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
50 {
51 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
52 int wrote;
53
54 wrote = gzwrite(self->gz_file, (char *) buf, count);
55
56 return (wrote < 0) ? 0 : wrote;
57 }
58
59 static int php_gziop_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
60 {
61 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
62
63 assert(self != NULL);
64
65 if (whence == SEEK_END) {
66 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SEEK_END is not supported");
67 return -1;
68 }
69 *newoffs = gzseek(self->gz_file, offset, whence);
70
71 return (*newoffs < 0) ? -1 : 0;
72 }
73
74 static int php_gziop_close(php_stream *stream, int close_handle TSRMLS_DC)
75 {
76 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
77 int ret = EOF;
78
79 if (close_handle) {
80 if (self->gz_file) {
81 ret = gzclose(self->gz_file);
82 self->gz_file = NULL;
83 }
84 if (self->stream) {
85 php_stream_close(self->stream);
86 self->stream = NULL;
87 }
88 }
89 efree(self);
90
91 return ret;
92 }
93
94 static int php_gziop_flush(php_stream *stream TSRMLS_DC)
95 {
96 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
97
98 return gzflush(self->gz_file, Z_SYNC_FLUSH);
99 }
100
101 php_stream_ops php_stream_gzio_ops = {
102 php_gziop_write, php_gziop_read,
103 php_gziop_close, php_gziop_flush,
104 "ZLIB",
105 php_gziop_seek,
106 NULL,
107 NULL,
108 NULL
109 };
110
111 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
112 char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
113 {
114 struct php_gz_stream_data_t *self;
115 php_stream *stream = NULL, *innerstream = NULL;
116
117
118 if (strchr(mode, '+')) {
119 if (options & REPORT_ERRORS) {
120 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot open a zlib stream for reading and writing at the same time!");
121 }
122 return NULL;
123 }
124
125 if (strncasecmp("compress.zlib://", path, 16) == 0) {
126 path += 16;
127 } else if (strncasecmp("zlib:", path, 5) == 0) {
128 path += 5;
129 }
130
131 innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
132
133 if (innerstream) {
134 php_socket_t fd;
135
136 if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
137 self = emalloc(sizeof(*self));
138 self->stream = innerstream;
139 self->gz_file = gzdopen(dup(fd), mode);
140
141 if (self->gz_file) {
142 stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
143 if (stream) {
144 stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
145 return stream;
146 }
147
148 gzclose(self->gz_file);
149 }
150
151 efree(self);
152 if (options & REPORT_ERRORS) {
153 php_error_docref(NULL TSRMLS_CC, E_WARNING, "gzopen failed");
154 }
155 }
156
157 php_stream_close(innerstream);
158 }
159
160 return NULL;
161 }
162
163 static php_stream_wrapper_ops gzip_stream_wops = {
164 php_stream_gzopen,
165 NULL,
166 NULL,
167 NULL,
168 NULL,
169 "ZLIB",
170 NULL,
171 NULL,
172 NULL,
173 NULL
174 };
175
176 php_stream_wrapper php_stream_gzip_wrapper = {
177 &gzip_stream_wops,
178 NULL,
179 0,
180 };
181
182
183
184
185
186
187
188
189