summaryrefslogtreecommitdiff
path: root/main/streams.c
blob: 0e1cd399a68aaf289042a22d07f354671a3f1202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors:                                                             |
   | Wez Furlong (wez@thebrainroom.com)                                   |
   +----------------------------------------------------------------------+
 */

#define _GNU_SOURCE
#include "php.h"

#if HAVE_PHP_STREAM

#ifdef PHP_WIN32
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include "build-defs.h"
#endif

#define MAX_CHUNK_SIZE	8192

#define TOREAD(stream) ((stream)->readbuf.writepos - (stream)->readbuf.readpos)
#define TOWRITE(stream)	((stream)->readbuf.writepos - (stream)->readbuf.readpos)

#define READPTR(stream) ((stream)->readbuf.buffer + (stream)->readbuf.readpos)
#define WRITEPTR(stream) ((stream)->readbuf.buffer + (stream)->readbuf.writepos)

#define READ_MAX(stream, max) if (stream->is_blocked) stream_read_total(sock, max); else stream_readahead(sock)

/* allocate a new stream for a particular ops */
PHPAPI php_stream * php_stream_alloc(php_stream_ops * ops, void * abstract, int persistent, const char * mode)
{
	php_stream * ret;

	ret = (php_stream*)pemalloc(sizeof(php_stream), persistent);

	memset(ret, 0, sizeof(php_stream));

	ret->ops = ops;
	ret->abstract = abstract;
	ret->is_persistent = persistent;

	strncpy(ret->mode, mode, sizeof(ret->mode));

	return ret;
}

PHPAPI int php_stream_free(php_stream * stream, int call_dtor)
{
	int ret = 1;

	if (call_dtor)	{

		if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE)	{
			/* calling fclose on an fopencookied stream will ultimately
				call this very same function.  If we were called via fclose,
				the cookie_closer unsets the fclose_stdiocast flags, so
				we can be sure that we only reach here when PHP code calls
				php_stream_free.
				Lets let the cookie code clean it all up.
			 */
			return fclose(stream->stdiocast);
		}
		
		php_stream_flush(stream);
		ret = stream->ops->close(stream);
	
		/* tidy up any FILE* that might have been fdopened */
		if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast)
		{
			fclose(stream->stdiocast);
			stream->stdiocast = NULL;
		}
	}
	pefree(stream, stream->is_persistent);

	return ret;
}

PHPAPI size_t php_stream_read(php_stream * stream, char * buf, size_t size)
{
	return stream->ops->read(stream, buf, size);
}

PHPAPI int php_stream_eof(php_stream * stream)
{
	/* we define our stream reading function so that it
	   must return EOF when an EOF condition occurs, when
	   working in unbuffered mode and called with these args */
	return stream->ops->read(stream, NULL, 0) == EOF ? 1 : 0;
}

PHPAPI int php_stream_getc(php_stream * stream)
{
	char buf;

	if (php_stream_read(stream, &buf, 1) > 0)
		return buf;
	return EOF;
}

PHPAPI char *php_stream_gets(php_stream * stream, char *buf, size_t maxlen)
{

	if (maxlen == 0)	{
		buf[0] = 0;
		return buf;
	}

	if (stream->ops->gets)	{
		return stream->ops->gets(stream, buf, maxlen);
	}
	else	{
		/* unbuffered fgets - poor performance ! */
		size_t n = 0;
		char * c = buf;

		/* TODO: look at error returns? */

		while(n < maxlen && stream->ops->read(stream, c, 1) > 0)	{
			n++;
			if (*c == '\n')	{
				c++;
				break;
			}
			c++;
		}
		*c = 0;
		return buf;
	}
}

PHPAPI int php_stream_flush(php_stream * stream)
{
	return stream->ops->flush(stream);
}

PHPAPI size_t php_stream_write(php_stream * stream, const char * buf, size_t count)
{
	if (strchr(stream->mode, 'w') == NULL)	{
		TSRMLS_FETCH();

		zend_error(E_WARNING, "%s(): stream was not opened for writing", get_active_function_name(TSRMLS_C));
		return 0;
	}
	
	return stream->ops->write(stream, buf, count);
}

PHPAPI off_t php_stream_tell(php_stream * stream)
{
	off_t ret = -1;
	if (stream->ops->seek)	{
		ret = stream->ops->seek(stream, 0, SEEK_CUR);
	}
	return ret;
}

PHPAPI int php_stream_seek(php_stream * stream, off_t offset, int whence)
{
	if (stream->ops->seek)	
		return stream->ops->seek(stream, offset, whence);

	zend_error(E_WARNING, "streams of type %s do not support seeking", stream->ops->label);
	return -1;
}

/*------- STDIO stream implementation -------*/

static size_t php_stdiop_write(php_stream * stream, const char * buf, size_t count)
{
	return fwrite(buf, 1, count, (FILE*)stream->abstract);
}

static size_t php_stdiop_read(php_stream * stream, char * buf, size_t count)
{
	if (buf == NULL && count == 0)	{
		/* check for EOF condition */
		if (feof((FILE*)stream->abstract))	{
			return EOF;
		}
		return 0;
	}
	return fread(buf, 1, count, (FILE*)stream->abstract);
}

static int php_stdiop_close(php_stream * stream)
{
	return fclose((FILE*)stream->abstract);
}

static int php_stdiop_flush(php_stream * stream)
{
	return fflush((FILE*)stream->abstract);
}

static int php_stdiop_seek(php_stream * stream, off_t offset, int whence)
{
	return fseek((FILE*)stream->abstract, offset, whence);
}

static char * php_stdiop_gets(php_stream * stream, char * buf, size_t size)
{
	return fgets(buf, size, (FILE*)stream->abstract);
}
static int php_stdiop_cast(php_stream * stream, int castas, void ** ret)
{
	int fd;

	switch (castas)	{
		case PHP_STREAM_AS_STDIO:
			if (ret)
				*ret = stream->abstract;
			return SUCCESS;

		case PHP_STREAM_AS_FD:
			fd = fileno((FILE*)stream->abstract);
			if (fd < 0)
				return FAILURE;
			if (ret)
				*ret = (void*)fd;
			return SUCCESS;
		default:
			return FAILURE;
	}
}

php_stream_ops	php_stream_stdio_ops = {
	php_stdiop_write, php_stdiop_read,
	php_stdiop_close, php_stdiop_flush, php_stdiop_seek,
	php_stdiop_gets, php_stdiop_cast,
	"STDIO"
};

PHPAPI php_stream * php_stream_fopen(const char * filename, const char * mode)
{
	FILE * fp = fopen(filename, mode);

	if (fp)	{
		php_stream * ret = php_stream_alloc(&php_stream_stdio_ops, fp, 0, mode);

		if (ret)
			return ret;

		fclose(fp);
	}
	return NULL;
}

#if HAVE_FOPENCOOKIE
static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
{
   return php_stream_read(((php_stream *)cookie), buffer, size);
}

static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size) {
   return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
}

static int stream_cookie_seeker(void *cookie, off_t position, int whence) {
   return php_stream_seek(((php_stream *)cookie), position, whence);
}

static int stream_cookie_closer(void *cookie) {
	php_stream * stream = (php_stream*)cookie;
	/* prevent recursion */
	stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
	return php_stream_close(stream);
}

static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
{
	stream_cookie_reader, stream_cookie_writer,
	stream_cookie_seeker, stream_cookie_closer
};
#else
/* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
#endif

PHPAPI int php_stream_cast(php_stream * stream, int castas, void ** ret, int show_err)
{
	if (castas == PHP_STREAM_AS_STDIO)	{
		if (stream->stdiocast)	{
			if (ret)
				*ret = stream->stdiocast;
			return SUCCESS;
		}

		if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS)
			goto exit_success;


#if HAVE_FOPENCOOKIE
		/* if just checking, say yes we can be a FILE*, but don't actually create it yet */
		if (ret == NULL)
			goto exit_success;

		*ret = fopencookie(stream, stream->mode, stream_cookie_functions);

		if (*ret != NULL)	{
			stream->fclose_stdiocast = 1;
			goto exit_success;
		}

		{
			TSRMLS_FETCH();

			/* must be either:
		  		a) programmer error
		  		b) no memory
		  		-> lets bail
			 */
			zend_error(E_ERROR, "%s(): fopencookie failed", get_active_function_name(TSRMLS_C));
			return FAILURE;
		}
#endif

		goto exit_fail;
	}
	if (stream->ops->cast && stream->ops->cast(stream, castas, ret) == SUCCESS)
		goto exit_success;


exit_fail:
	if (show_err)	{
		/* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
		static const char * cast_names[3] = {
			"STDIO FILE*", "File Descriptor", "Socket Descriptor"
		};
		TSRMLS_FETCH();

		zend_error(E_WARNING, "%s(): cannot represent a stream of type %s as a %s",
			get_active_function_name(TSRMLS_C),
			stream->ops->label,
			cast_names[castas]
			);
	}

	return FAILURE;

exit_success:
	if (castas == PHP_STREAM_AS_STDIO && ret)
		stream->stdiocast = *ret;

	return SUCCESS;

}

#endif
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */