|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 4 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1997-2002 The PHP Group | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 2.02 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available at through the world-wide-web at | |
| 10 | + | http://www.php.net/license/2_02.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: | |
| 16 | + | Marcus Boerger <helly@php.net> | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | + */ |
| 19 | + |
| 20 | +#define _GNU_SOURCE |
| 21 | +#include "php.h" |
| 22 | +#include "php_streams.h" |
| 23 | + |
| 24 | +/* {{{ ------- MEMORY stream implementation -------*/ |
| 25 | + |
| 26 | +typedef struct { |
| 27 | + char *data; |
| 28 | + size_t fpos; |
| 29 | + size_t fsize; |
| 30 | +} php_memory_stream_data; |
| 31 | + |
| 32 | +static size_t php_memory_stream_write(php_stream *stream, const char *buf, size_t count) {/* {{{ */ |
| 33 | + php_memory_stream_data *ms; |
| 34 | + |
| 35 | + assert(stream != NULL); |
| 36 | + ms = stream->abstract; |
| 37 | + assert(ms != NULL); |
| 38 | + if ( ms->fpos + count > ms->fsize) { |
| 39 | + char *tmp; |
| 40 | + |
| 41 | + if ( !ms->data) { |
| 42 | + tmp = emalloc( ms->fpos + count); |
| 43 | + } else { |
| 44 | + tmp = erealloc( ms->data, ms->fpos + count); |
| 45 | + } |
| 46 | + if ( !tmp) { |
| 47 | + count = ms->fsize - ms->fpos + 1; |
| 48 | + } else { |
| 49 | + ms->data = tmp; |
| 50 | + ms->fsize = ms->fpos + count; |
| 51 | + } |
| 52 | + } |
| 53 | + if ( !ms->data) |
| 54 | + count = 0; |
| 55 | + if ( count) { |
| 56 | + assert(buf!= NULL); |
| 57 | + memcpy( ms->data+ms->fpos, (char*)buf, count); |
| 58 | + ms->fpos += count; |
| 59 | + } |
| 60 | + return count; |
| 61 | +} /* }}} */ |
| 62 | + |
| 63 | +static size_t php_memory_stream_read(php_stream *stream, char *buf, size_t count) {/* {{{ */ |
| 64 | + php_memory_stream_data *ms; |
| 65 | + |
| 66 | + assert(stream != NULL); |
| 67 | + ms = stream->abstract; |
| 68 | + assert(ms != NULL); |
| 69 | + |
| 70 | + if (buf == NULL && count == 0) { |
| 71 | + /* check for EOF condition */ |
| 72 | + if (ms->fpos >= ms->fsize) { |
| 73 | + return EOF; |
| 74 | + } |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + if ( ms->fpos + count > ms->fsize) { |
| 79 | + count = ms->fsize - ms->fpos; |
| 80 | + } |
| 81 | + if ( count) { |
| 82 | + assert(buf!= NULL); |
| 83 | + memcpy( buf, ms->data+ms->fpos, count); |
| 84 | + ms->fpos += count; |
| 85 | + } |
| 86 | + return count; |
| 87 | +} /* }}} */ |
| 88 | + |
| 89 | + |
| 90 | +static int php_memory_stream_close(php_stream *stream, int close_handle) {/* {{{ */ |
| 91 | + php_memory_stream_data *ms; |
| 92 | + |
| 93 | + assert(stream != NULL); |
| 94 | + ms = stream->abstract; |
| 95 | + assert(ms != NULL); |
| 96 | + if (ms->data) { |
| 97 | + efree(ms->data); |
| 98 | + } |
| 99 | + ms->data = NULL; |
| 100 | + ms->fsize = 0; |
| 101 | + ms->fpos = 0; |
| 102 | +} /* }}} */ |
| 103 | + |
| 104 | + |
| 105 | +static int php_memory_stream_flush(php_stream *stream) {/* {{{ */ |
| 106 | + // nothing to do here |
| 107 | + return 0; |
| 108 | +} /* }}} */ |
| 109 | + |
| 110 | + |
| 111 | +static int php_memory_stream_seek(php_stream *stream, off_t offset, int whence) {/* {{{ */ |
| 112 | + php_memory_stream_data *ms; |
| 113 | + |
| 114 | + assert(stream != NULL); |
| 115 | + ms = stream->abstract; |
| 116 | + assert(ms != NULL); |
| 117 | + if (offset == 0 && whence == SEEK_CUR) |
| 118 | + return ms->fpos; |
| 119 | + switch( whence) { |
| 120 | + case SEEK_CUR: |
| 121 | + if ( offset < 0) { |
| 122 | + if ( ms->fpos < -offset) { |
| 123 | + ms->fpos = 0; |
| 124 | + /*return EINVAL;*/ |
| 125 | + } else { |
| 126 | + ms->fpos = ms->fpos + offset; |
| 127 | + } |
| 128 | + } else { |
| 129 | + if ( ms->fpos < offset) { |
| 130 | + ms->fpos = ms->fsize; |
| 131 | + /*return EINVAL;*/ |
| 132 | + } else { |
| 133 | + ms->fpos = ms->fpos + offset; |
| 134 | + } |
| 135 | + } |
| 136 | + return 0; |
| 137 | + case SEEK_SET: |
| 138 | + if ( offset > ms->fsize) { |
| 139 | + ms->fpos = ms->fsize; |
| 140 | + /*return EINVAL;*/ |
| 141 | + } else { |
| 142 | + ms->fpos = offset; |
| 143 | + } |
| 144 | + return 0; |
| 145 | + case SEEK_END: |
| 146 | + if ( offset > 0) { |
| 147 | + ms->fpos = ms->fsize; |
| 148 | + /*return EINVAL;*/ |
| 149 | + } else if ( ms->fpos < -offset) { |
| 150 | + ms->fpos = 0; |
| 151 | + /*return EINVAL;*/ |
| 152 | + } else { |
| 153 | + ms->fpos = ms->fsize + offset; |
| 154 | + } |
| 155 | + return 0; |
| 156 | + default: |
| 157 | + return 0; |
| 158 | + return /*EINVAL*/; |
| 159 | + } |
| 160 | +} /* }}} */ |
| 161 | + |
| 162 | +php_stream_ops php_stream_memory_ops = { |
| 163 | + php_memory_stream_write, php_memory_stream_read, |
| 164 | + php_memory_stream_close, php_memory_stream_flush, |
| 165 | + php_memory_stream_seek, |
| 166 | + NULL, |
| 167 | + NULL, |
| 168 | + "MEMORY" |
| 169 | +}; |
| 170 | + |
| 171 | +PHPAPI php_stream *php_memory_stream_create() {/* {{{ */ |
| 172 | + php_memory_stream_data *self; |
| 173 | + |
| 174 | + self = emalloc(sizeof(*self)); |
| 175 | + assert( self != NULL); |
| 176 | + self->data = NULL; |
| 177 | + self->fpos = 0; |
| 178 | + self->fsize = 0; |
| 179 | + return php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb"); |
| 180 | +} /* }}} */ |
| 181 | + |
| 182 | +/* |
| 183 | + * Local variables: |
| 184 | + * tab-width: 4 |
| 185 | + * c-basic-offset: 4 |
| 186 | + * End: |
| 187 | + * vim600: noet sw=4 ts=4 fdm=marker |
| 188 | + * vim<600: noet sw=4 ts=4 |
| 189 | + */ |
0 commit comments