Back to index

php5  5.3.10
cast.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: cast.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #define _GNU_SOURCE
00022 #include "php.h"
00023 #include "php_globals.h"
00024 #include "php_network.h"
00025 #include "php_open_temporary_file.h"
00026 #include "ext/standard/file.h"
00027 #include <stddef.h>
00028 #include <fcntl.h>
00029 
00030 #include "php_streams_int.h"
00031 
00032 /* Under BSD, emulate fopencookie using funopen */
00033 #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
00034 typedef struct {
00035        int (*reader)(void *, char *, int);
00036        int (*writer)(void *, const char *, int);
00037        fpos_t (*seeker)(void *, fpos_t, int);
00038        int (*closer)(void *);
00039 } COOKIE_IO_FUNCTIONS_T;
00040 
00041 FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
00042 {
00043        return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
00044 }
00045 # define HAVE_FOPENCOOKIE 1
00046 # define PHP_EMULATE_FOPENCOOKIE 1
00047 # define PHP_STREAM_COOKIE_FUNCTIONS      &stream_cookie_functions
00048 #elif defined(HAVE_FOPENCOOKIE)
00049 # define PHP_STREAM_COOKIE_FUNCTIONS      stream_cookie_functions
00050 #endif
00051 
00052 /* {{{ STDIO with fopencookie */
00053 #if defined(PHP_EMULATE_FOPENCOOKIE)
00054 /* use our fopencookie emulation */
00055 static int stream_cookie_reader(void *cookie, char *buffer, int size)
00056 {
00057        int ret;
00058        TSRMLS_FETCH();
00059 
00060        ret = php_stream_read((php_stream*)cookie, buffer, size);
00061        return ret;
00062 }
00063 
00064 static int stream_cookie_writer(void *cookie, const char *buffer, int size)
00065 {
00066        TSRMLS_FETCH();
00067 
00068        return php_stream_write((php_stream *)cookie, (char *)buffer, size);
00069 }
00070 
00071 static fpos_t stream_cookie_seeker(void *cookie, off_t position, int whence)
00072 {
00073        TSRMLS_FETCH();
00074 
00075        return (fpos_t)php_stream_seek((php_stream *)cookie, position, whence);
00076 }
00077 
00078 static int stream_cookie_closer(void *cookie)
00079 {
00080        php_stream *stream = (php_stream*)cookie;
00081        TSRMLS_FETCH();
00082 
00083        /* prevent recursion */
00084        stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
00085        return php_stream_close(stream);
00086 }
00087 #elif defined(HAVE_FOPENCOOKIE)
00088 static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
00089 {
00090        ssize_t ret;
00091        TSRMLS_FETCH();
00092 
00093        ret = php_stream_read(((php_stream *)cookie), buffer, size);
00094        return ret;
00095 }
00096 
00097 static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
00098 {
00099        TSRMLS_FETCH();
00100 
00101        return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
00102 }
00103 
00104 # ifdef COOKIE_SEEKER_USES_OFF64_T
00105 static int stream_cookie_seeker(void *cookie, __off64_t *position, int whence)
00106 {
00107        TSRMLS_FETCH();
00108 
00109        *position = php_stream_seek((php_stream *)cookie, (off_t)*position, whence);
00110 
00111        if (*position == -1) {
00112               return -1;
00113        }
00114        return 0;
00115 }
00116 # else
00117 static int stream_cookie_seeker(void *cookie, off_t position, int whence)
00118 {
00119        TSRMLS_FETCH();
00120 
00121        return php_stream_seek((php_stream *)cookie, position, whence);
00122 }
00123 # endif
00124 
00125 static int stream_cookie_closer(void *cookie)
00126 {
00127        php_stream *stream = (php_stream*)cookie;
00128        TSRMLS_FETCH();
00129 
00130        /* prevent recursion */
00131        stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
00132        return php_stream_close(stream);
00133 }
00134 #endif /* elif defined(HAVE_FOPENCOOKIE) */
00135 
00136 #if HAVE_FOPENCOOKIE
00137 static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
00138 {
00139        stream_cookie_reader, stream_cookie_writer,
00140        stream_cookie_seeker, stream_cookie_closer
00141 };
00142 #else
00143 /* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
00144 #endif
00145 /* }}} */
00146 
00147 /* {{{ php_stream_mode_sanitize_fdopen_fopencookie
00148  * Result should have at least size 5, e.g. to write wbx+\0 */
00149 void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
00150 {
00151        /* replace modes not supported by fdopen and fopencookie, but supported 
00152         * by PHP's fread(), so that their calls won't fail */
00153        const char *cur_mode = stream->mode;
00154        int         has_plus = 0,
00155                       has_bin  = 0,
00156                             i,
00157                             res_curs = 0;
00158 
00159        if (cur_mode[0] == 'r' || cur_mode[0] == 'w' || cur_mode[0] == 'a') {
00160               result[res_curs++] = cur_mode[0];
00161        } else {
00162               /* assume cur_mode[0] is 'c' or 'x'; substitute by 'w', which should not
00163                * truncate anything in fdopen/fopencookie */
00164               result[res_curs++] = 'w';
00165 
00166               /* x is allowed (at least by glibc & compat), but not as the 1st mode
00167                * as in PHP and in any case is (at best) ignored by fdopen and fopencookie */
00168        }
00169        
00170        /* assume current mode has at most length 4 (e.g. wbn+) */
00171        for (i = 1; i < 4 && cur_mode[i] != '\0'; i++) {
00172               if (cur_mode[i] == 'b') {
00173                      has_bin = 1;
00174               } else if (cur_mode[i] == '+') {
00175                      has_plus = 1;
00176               }
00177               /* ignore 'n', 't' or other stuff */
00178        }
00179 
00180        if (has_bin) {
00181               result[res_curs++] = 'b';
00182        }
00183        if (has_plus) {
00184               result[res_curs++] = '+';
00185        }
00186 
00187        result[res_curs] = '\0';
00188 }
00189 /* }}} */
00190 
00191 /* {{{ php_stream_cast */
00192 PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err TSRMLS_DC)
00193 {
00194        int flags = castas & PHP_STREAM_CAST_MASK;
00195        castas &= ~PHP_STREAM_CAST_MASK;
00196 
00197        /* synchronize our buffer (if possible) */
00198        if (ret && castas != PHP_STREAM_AS_FD_FOR_SELECT) {
00199               php_stream_flush(stream);
00200               if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
00201                      off_t dummy;
00202 
00203                      stream->ops->seek(stream, stream->position, SEEK_SET, &dummy TSRMLS_CC);
00204                      stream->readpos = stream->writepos = 0;
00205               }
00206        }
00207 
00208        /* filtered streams can only be cast as stdio, and only when fopencookie is present */
00209 
00210        if (castas == PHP_STREAM_AS_STDIO) {
00211               if (stream->stdiocast) {
00212                      if (ret) {
00213                             *(FILE**)ret = stream->stdiocast;
00214                      }
00215                      goto exit_success;
00216               }
00217 
00218               /* if the stream is a stdio stream let's give it a chance to respond
00219                * first, to avoid doubling up the layers of stdio with an fopencookie */
00220               if (php_stream_is(stream, PHP_STREAM_IS_STDIO) &&
00221                      stream->ops->cast &&
00222                      !php_stream_is_filtered(stream) &&
00223                      stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS
00224               ) {
00225                      goto exit_success;
00226               }
00227 
00228 #if HAVE_FOPENCOOKIE
00229               /* if just checking, say yes we can be a FILE*, but don't actually create it yet */
00230               if (ret == NULL) {
00231                      goto exit_success;
00232               }
00233 
00234               {
00235                      char fixed_mode[5];
00236                      php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
00237                      *(FILE**)ret = fopencookie(stream, fixed_mode, PHP_STREAM_COOKIE_FUNCTIONS);
00238               }
00239 
00240               if (*ret != NULL) {
00241                      off_t pos;
00242 
00243                      stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
00244 
00245                      /* If the stream position is not at the start, we need to force
00246                       * the stdio layer to believe it's real location. */
00247                      pos = php_stream_tell(stream);
00248                      if (pos > 0) {
00249                             fseek(*ret, pos, SEEK_SET);
00250                      }
00251 
00252                      goto exit_success;
00253               }
00254 
00255               /* must be either:
00256                      a) programmer error
00257                      b) no memory
00258                      -> lets bail
00259               */
00260               php_error_docref(NULL TSRMLS_CC, E_ERROR, "fopencookie failed");
00261               return FAILURE;
00262 #endif
00263 
00264               if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) {
00265                      if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) {
00266                             return FAILURE;
00267                      }
00268                      goto exit_success;
00269               } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
00270                      php_stream *newstream;
00271 
00272                      newstream = php_stream_fopen_tmpfile();
00273                      if (newstream) {
00274                             size_t retcopy = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL, NULL);
00275 
00276                             if (retcopy != SUCCESS) {
00277                                    php_stream_close(newstream);
00278                             } else {
00279                                    int retcast = php_stream_cast(newstream, castas | flags, (void **)ret, show_err);
00280 
00281                                    if (retcast == SUCCESS) {
00282                                           rewind(*(FILE**)ret);
00283                                    }
00284 
00285                                    /* do some specialized cleanup */
00286                                    if ((flags & PHP_STREAM_CAST_RELEASE)) {
00287                                           php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
00288                                    }
00289 
00290                                    /* TODO: we probably should be setting .stdiocast and .fclose_stdiocast or
00291                                     * we may be leaking the FILE*. Needs investigation, though. */
00292                                    return retcast;
00293                             }
00294                      }
00295               }
00296        }
00297 
00298        if (php_stream_is_filtered(stream)) {
00299               php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot cast a filtered stream on this system");
00300               return FAILURE;
00301        } else if (stream->ops->cast && stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS) {
00302               goto exit_success;
00303        }
00304 
00305        if (show_err) {
00306               /* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
00307               static const char *cast_names[4] = {
00308                      "STDIO FILE*",
00309                      "File Descriptor",
00310                      "Socket Descriptor",
00311                      "select()able descriptor"
00312               };
00313 
00314               php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
00315        }
00316 
00317        return FAILURE;
00318 
00319 exit_success:
00320 
00321        if ((stream->writepos - stream->readpos) > 0 &&
00322               stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
00323               (flags & PHP_STREAM_CAST_INTERNAL) == 0
00324        ) {
00325               /* the data we have buffered will be lost to the third party library that
00326                * will be accessing the stream.  Emit a warning so that the end-user will
00327                * know that they should try something else */
00328 
00329               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld bytes of buffered data lost during stream conversion!", (long)(stream->writepos - stream->readpos));
00330        }
00331 
00332        if (castas == PHP_STREAM_AS_STDIO && ret) {
00333               stream->stdiocast = *(FILE**)ret;
00334        }
00335 
00336        if (flags & PHP_STREAM_CAST_RELEASE) {
00337               php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
00338        }
00339 
00340        return SUCCESS;
00341 
00342 }
00343 /* }}} */
00344 
00345 /* {{{ php_stream_open_wrapper_as_file */
00346 PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, char **opened_path STREAMS_DC TSRMLS_DC)
00347 {
00348        FILE *fp = NULL;
00349        php_stream *stream = NULL;
00350 
00351        stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
00352 
00353        if (stream == NULL) {
00354               return NULL;
00355        }
00356 
00357        if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
00358               php_stream_close(stream);
00359               if (opened_path && *opened_path) {
00360                      efree(*opened_path);
00361               }
00362               return NULL;
00363        }
00364        return fp;
00365 }
00366 /* }}} */
00367 
00368 /* {{{ php_stream_make_seekable */
00369 PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC)
00370 {
00371        if (newstream == NULL) {
00372               return PHP_STREAM_FAILED;
00373        }
00374        *newstream = NULL;
00375 
00376        if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
00377               *newstream = origstream;
00378               return PHP_STREAM_UNCHANGED;
00379        }
00380 
00381        /* Use a tmpfile and copy the old streams contents into it */
00382 
00383        if (flags & PHP_STREAM_PREFER_STDIO) {
00384               *newstream = php_stream_fopen_tmpfile();
00385        } else {
00386               *newstream = php_stream_temp_new();
00387        }
00388 
00389        if (*newstream == NULL) {
00390               return PHP_STREAM_FAILED;
00391        }
00392 
00393 #if ZEND_DEBUG
00394        (*newstream)->open_filename = origstream->open_filename;
00395        (*newstream)->open_lineno = origstream->open_lineno;
00396 #endif
00397 
00398        if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
00399               php_stream_close(*newstream);
00400               *newstream = NULL;
00401               return PHP_STREAM_CRITICAL;
00402        }
00403 
00404        php_stream_close(origstream);
00405        php_stream_seek(*newstream, 0, SEEK_SET);
00406 
00407        return PHP_STREAM_RELEASED;
00408 }
00409 /* }}} */
00410 
00411 /*
00412  * Local variables:
00413  * tab-width: 4
00414  * c-basic-offset: 4
00415  * End:
00416  * vim600: noet sw=4 ts=4 fdm=marker
00417  * vim<600: noet sw=4 ts=4
00418  */