/src/php-src/Zend/zend_stream.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Wez Furlong <wez@thebrainroom.com> | |
16 | | | Scott MacVicar <scottmac@php.net> | |
17 | | | Nuno Lopes <nlopess@php.net> | |
18 | | | Marcus Boerger <helly@php.net> | |
19 | | +----------------------------------------------------------------------+ |
20 | | */ |
21 | | |
22 | | #include "zend.h" |
23 | | #include "zend_compile.h" |
24 | | #include "zend_stream.h" |
25 | | |
26 | | ZEND_DLIMPORT int isatty(int fd); |
27 | | |
28 | | static ssize_t zend_stream_stdio_reader(void *handle, char *buf, size_t len) /* {{{ */ |
29 | 0 | { |
30 | 0 | return fread(buf, 1, len, (FILE*)handle); |
31 | 0 | } /* }}} */ |
32 | | |
33 | | static void zend_stream_stdio_closer(void *handle) /* {{{ */ |
34 | 0 | { |
35 | 0 | if (handle && (FILE*)handle != stdin) { |
36 | 0 | fclose((FILE*)handle); |
37 | 0 | } |
38 | 0 | } /* }}} */ |
39 | | |
40 | | static size_t zend_stream_stdio_fsizer(void *handle) /* {{{ */ |
41 | 0 | { |
42 | 0 | zend_stat_t buf; |
43 | 0 | if (handle && zend_fstat(fileno((FILE*)handle), &buf) == 0) { |
44 | 0 | #ifdef S_ISREG |
45 | 0 | if (!S_ISREG(buf.st_mode)) { |
46 | 0 | return 0; |
47 | 0 | } |
48 | 0 | #endif |
49 | 0 | return buf.st_size; |
50 | 0 | } |
51 | 0 | return -1; |
52 | 0 | } /* }}} */ |
53 | | |
54 | | static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */ |
55 | 0 | { |
56 | 0 | ZEND_ASSERT(file_handle->type == ZEND_HANDLE_STREAM); |
57 | 0 | if (file_handle->handle.stream.isatty) { |
58 | 0 | return 0; |
59 | 0 | } |
60 | 0 | return file_handle->handle.stream.fsizer(file_handle->handle.stream.handle); |
61 | 0 | } /* }}} */ |
62 | | |
63 | 0 | ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) { |
64 | 0 | memset(handle, 0, sizeof(zend_file_handle)); |
65 | 0 | handle->type = ZEND_HANDLE_FP; |
66 | 0 | handle->handle.fp = fp; |
67 | 0 | handle->filename = filename; |
68 | 0 | } |
69 | | |
70 | 0 | ZEND_API void zend_stream_init_filename(zend_file_handle *handle, const char *filename) { |
71 | 0 | memset(handle, 0, sizeof(zend_file_handle)); |
72 | 0 | handle->type = ZEND_HANDLE_FILENAME; |
73 | 0 | handle->filename = filename; |
74 | 0 | } |
75 | | |
76 | | ZEND_API int zend_stream_open(const char *filename, zend_file_handle *handle) /* {{{ */ |
77 | 0 | { |
78 | 0 | zend_string *opened_path; |
79 | 0 | if (zend_stream_open_function) { |
80 | 0 | return zend_stream_open_function(filename, handle); |
81 | 0 | } |
82 | | |
83 | 0 | zend_stream_init_fp(handle, zend_fopen(filename, &opened_path), filename); |
84 | 0 | handle->opened_path = opened_path; |
85 | 0 | return handle->handle.fp ? SUCCESS : FAILURE; |
86 | 0 | } /* }}} */ |
87 | | |
88 | | static int zend_stream_getc(zend_file_handle *file_handle) /* {{{ */ |
89 | 0 | { |
90 | 0 | char buf; |
91 | |
|
92 | 0 | if (file_handle->handle.stream.reader(file_handle->handle.stream.handle, &buf, sizeof(buf))) { |
93 | 0 | return (int)buf; |
94 | 0 | } |
95 | 0 | return EOF; |
96 | 0 | } /* }}} */ |
97 | | |
98 | | static ssize_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t len) /* {{{ */ |
99 | 0 | { |
100 | 0 | if (file_handle->handle.stream.isatty) { |
101 | 0 | int c = '*'; |
102 | 0 | size_t n; |
103 | |
|
104 | 0 | for (n = 0; n < len && (c = zend_stream_getc(file_handle)) != EOF && c != '\n'; ++n) { |
105 | 0 | buf[n] = (char)c; |
106 | 0 | } |
107 | 0 | if (c == '\n') { |
108 | 0 | buf[n++] = (char)c; |
109 | 0 | } |
110 | |
|
111 | 0 | return n; |
112 | 0 | } |
113 | 0 | return file_handle->handle.stream.reader(file_handle->handle.stream.handle, buf, len); |
114 | 0 | } /* }}} */ |
115 | | |
116 | | ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len) /* {{{ */ |
117 | 390k | { |
118 | 390k | size_t file_size; |
119 | | |
120 | 390k | if (file_handle->buf) { |
121 | 390k | *buf = file_handle->buf; |
122 | 390k | *len = file_handle->len; |
123 | 390k | return SUCCESS; |
124 | 390k | } |
125 | | |
126 | 0 | if (file_handle->type == ZEND_HANDLE_FILENAME) { |
127 | 0 | if (zend_stream_open(file_handle->filename, file_handle) == FAILURE) { |
128 | 0 | return FAILURE; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | 0 | if (file_handle->type == ZEND_HANDLE_FP) { |
133 | 0 | if (!file_handle->handle.fp) { |
134 | 0 | return FAILURE; |
135 | 0 | } |
136 | | |
137 | 0 | file_handle->type = ZEND_HANDLE_STREAM; |
138 | 0 | file_handle->handle.stream.handle = file_handle->handle.fp; |
139 | 0 | file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle)); |
140 | 0 | file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader; |
141 | 0 | file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer; |
142 | 0 | file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer; |
143 | 0 | } |
144 | |
|
145 | 0 | file_size = zend_stream_fsize(file_handle); |
146 | 0 | if (file_size == (size_t)-1) { |
147 | 0 | return FAILURE; |
148 | 0 | } |
149 | | |
150 | 0 | if (file_size) { |
151 | 0 | ssize_t read; |
152 | 0 | size_t size = 0; |
153 | 0 | *buf = safe_emalloc(1, file_size, ZEND_MMAP_AHEAD); |
154 | 0 | while ((read = zend_stream_read(file_handle, *buf + size, file_size - size)) > 0) { |
155 | 0 | size += read; |
156 | 0 | } |
157 | 0 | if (read < 0) { |
158 | 0 | efree(*buf); |
159 | 0 | return FAILURE; |
160 | 0 | } |
161 | 0 | file_handle->buf = *buf; |
162 | 0 | file_handle->len = size; |
163 | 0 | } else { |
164 | 0 | size_t size = 0, remain = 4*1024; |
165 | 0 | ssize_t read; |
166 | 0 | *buf = emalloc(remain); |
167 | |
|
168 | 0 | while ((read = zend_stream_read(file_handle, *buf + size, remain)) > 0) { |
169 | 0 | size += read; |
170 | 0 | remain -= read; |
171 | |
|
172 | 0 | if (remain == 0) { |
173 | 0 | *buf = safe_erealloc(*buf, size, 2, 0); |
174 | 0 | remain = size; |
175 | 0 | } |
176 | 0 | } |
177 | 0 | if (read < 0) { |
178 | 0 | efree(*buf); |
179 | 0 | return FAILURE; |
180 | 0 | } |
181 | | |
182 | 0 | file_handle->len = size; |
183 | 0 | if (size && remain < ZEND_MMAP_AHEAD) { |
184 | 0 | *buf = safe_erealloc(*buf, size, 1, ZEND_MMAP_AHEAD); |
185 | 0 | } |
186 | 0 | file_handle->buf = *buf; |
187 | 0 | } |
188 | |
|
189 | 0 | if (file_handle->len == 0) { |
190 | 0 | *buf = erealloc(*buf, ZEND_MMAP_AHEAD); |
191 | 0 | file_handle->buf = *buf; |
192 | 0 | } |
193 | |
|
194 | 0 | memset(file_handle->buf + file_handle->len, 0, ZEND_MMAP_AHEAD); |
195 | |
|
196 | 0 | *buf = file_handle->buf; |
197 | 0 | *len = file_handle->len; |
198 | |
|
199 | 0 | return SUCCESS; |
200 | 0 | } /* }}} */ |
201 | | |
202 | | ZEND_API void zend_file_handle_dtor(zend_file_handle *fh) /* {{{ */ |
203 | 390k | { |
204 | 390k | switch (fh->type) { |
205 | 0 | case ZEND_HANDLE_FP: |
206 | 0 | fclose(fh->handle.fp); |
207 | 0 | break; |
208 | 390k | case ZEND_HANDLE_STREAM: |
209 | 390k | if (fh->handle.stream.closer && fh->handle.stream.handle) { |
210 | 0 | fh->handle.stream.closer(fh->handle.stream.handle); |
211 | 0 | } |
212 | 390k | fh->handle.stream.handle = NULL; |
213 | 390k | break; |
214 | 0 | case ZEND_HANDLE_FILENAME: |
215 | | /* We're only supposed to get here when destructing the used_files hash, |
216 | | * which doesn't really contain open files, but references to their names/paths |
217 | | */ |
218 | 0 | break; |
219 | 390k | } |
220 | 390k | if (fh->opened_path) { |
221 | 0 | zend_string_release_ex(fh->opened_path, 0); |
222 | 0 | fh->opened_path = NULL; |
223 | 0 | } |
224 | 390k | if (fh->buf) { |
225 | 390k | efree(fh->buf); |
226 | 390k | fh->buf = NULL; |
227 | 390k | } |
228 | 390k | if (fh->free_filename && fh->filename) { |
229 | 0 | efree((char*)fh->filename); |
230 | 0 | fh->filename = NULL; |
231 | 0 | } |
232 | 390k | } |
233 | | /* }}} */ |
234 | | |
235 | | ZEND_API int zend_compare_file_handles(zend_file_handle *fh1, zend_file_handle *fh2) /* {{{ */ |
236 | | { |
237 | | if (fh1->type != fh2->type) { |
238 | | return 0; |
239 | | } |
240 | | switch (fh1->type) { |
241 | | case ZEND_HANDLE_FILENAME: |
242 | | return strcmp(fh1->filename, fh2->filename) == 0; |
243 | | case ZEND_HANDLE_FP: |
244 | | return fh1->handle.fp == fh2->handle.fp; |
245 | | case ZEND_HANDLE_STREAM: |
246 | | return fh1->handle.stream.handle == fh2->handle.stream.handle; |
247 | | default: |
248 | | return 0; |
249 | | } |
250 | | return 0; |
251 | | } /* }}} */ |