PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main/streams - cast.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 72 110 65.5 %
Date: 2014-10-24 Functions: 5 8 62.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.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             :    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #define _GNU_SOURCE
      22             : #include "php.h"
      23             : #include "php_globals.h"
      24             : #include "php_network.h"
      25             : #include "php_open_temporary_file.h"
      26             : #include "ext/standard/file.h"
      27             : #include <stddef.h>
      28             : #include <fcntl.h>
      29             : 
      30             : #include "php_streams_int.h"
      31             : 
      32             : /* Under BSD, emulate fopencookie using funopen */
      33             : #if defined(HAVE_FUNOPEN) && !defined(HAVE_FOPENCOOKIE)
      34             : 
      35             : /* NetBSD 6.0+ uses off_t instead of fpos_t in funopen */
      36             : # if defined(__NetBSD__) && (__NetBSD_Version__ >= 600000000)
      37             : #  define PHP_FPOS_T off_t
      38             : # else
      39             : #  define PHP_FPOS_T fpos_t
      40             : # endif
      41             : 
      42             : typedef struct {
      43             :         int (*reader)(void *, char *, int);
      44             :         int (*writer)(void *, const char *, int);
      45             :         PHP_FPOS_T (*seeker)(void *, PHP_FPOS_T, int);
      46             :         int (*closer)(void *);
      47             : } COOKIE_IO_FUNCTIONS_T;
      48             : 
      49             : FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
      50             : {
      51             :         return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
      52             : }
      53             : # define HAVE_FOPENCOOKIE 1
      54             : # define PHP_EMULATE_FOPENCOOKIE 1
      55             : # define PHP_STREAM_COOKIE_FUNCTIONS    &stream_cookie_functions
      56             : #elif defined(HAVE_FOPENCOOKIE)
      57             : # define PHP_STREAM_COOKIE_FUNCTIONS    stream_cookie_functions
      58             : #endif
      59             : 
      60             : /* {{{ STDIO with fopencookie */
      61             : #if defined(PHP_EMULATE_FOPENCOOKIE)
      62             : /* use our fopencookie emulation */
      63             : static int stream_cookie_reader(void *cookie, char *buffer, int size)
      64             : {
      65             :         int ret;
      66             :         TSRMLS_FETCH();
      67             : 
      68             :         ret = php_stream_read((php_stream*)cookie, buffer, size);
      69             :         return ret;
      70             : }
      71             : 
      72             : static int stream_cookie_writer(void *cookie, const char *buffer, int size)
      73             : {
      74             :         TSRMLS_FETCH();
      75             : 
      76             :         return php_stream_write((php_stream *)cookie, (char *)buffer, size);
      77             : }
      78             : 
      79             : static PHP_FPOS_T stream_cookie_seeker(void *cookie, off_t position, int whence)
      80             : {
      81             :         TSRMLS_FETCH();
      82             : 
      83             :         return (PHP_FPOS_T)php_stream_seek((php_stream *)cookie, position, whence);
      84             : }
      85             : 
      86             : static int stream_cookie_closer(void *cookie)
      87             : {
      88             :         php_stream *stream = (php_stream*)cookie;
      89             :         TSRMLS_FETCH();
      90             : 
      91             :         /* prevent recursion */
      92             :         stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
      93             :         return php_stream_close(stream);
      94             : }
      95             : #elif defined(HAVE_FOPENCOOKIE)
      96           0 : static ssize_t stream_cookie_reader(void *cookie, char *buffer, size_t size)
      97             : {
      98             :         ssize_t ret;
      99             :         TSRMLS_FETCH();
     100             : 
     101           0 :         ret = php_stream_read(((php_stream *)cookie), buffer, size);
     102           0 :         return ret;
     103             : }
     104             : 
     105           0 : static ssize_t stream_cookie_writer(void *cookie, const char *buffer, size_t size)
     106             : {
     107             :         TSRMLS_FETCH();
     108             : 
     109           0 :         return php_stream_write(((php_stream *)cookie), (char *)buffer, size);
     110             : }
     111             : 
     112             : # ifdef COOKIE_SEEKER_USES_OFF64_T
     113           1 : static int stream_cookie_seeker(void *cookie, __off64_t *position, int whence)
     114             : {
     115             :         TSRMLS_FETCH();
     116             : 
     117           1 :         *position = php_stream_seek((php_stream *)cookie, (off_t)*position, whence);
     118             : 
     119           1 :         if (*position == -1) {
     120           0 :                 return -1;
     121             :         }
     122           1 :         return 0;
     123             : }
     124             : # else
     125             : static int stream_cookie_seeker(void *cookie, off_t position, int whence)
     126             : {
     127             :         TSRMLS_FETCH();
     128             : 
     129             :         return php_stream_seek((php_stream *)cookie, position, whence);
     130             : }
     131             : # endif
     132             : 
     133           1 : static int stream_cookie_closer(void *cookie)
     134             : {
     135           1 :         php_stream *stream = (php_stream*)cookie;
     136             :         TSRMLS_FETCH();
     137             : 
     138             :         /* prevent recursion */
     139           1 :         stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
     140           1 :         return php_stream_close(stream);
     141             : }
     142             : #endif /* elif defined(HAVE_FOPENCOOKIE) */
     143             : 
     144             : #if HAVE_FOPENCOOKIE
     145             : static COOKIE_IO_FUNCTIONS_T stream_cookie_functions =
     146             : {
     147             :         stream_cookie_reader, stream_cookie_writer,
     148             :         stream_cookie_seeker, stream_cookie_closer
     149             : };
     150             : #else
     151             : /* TODO: use socketpair() to emulate fopencookie, as suggested by Hartmut ? */
     152             : #endif
     153             : /* }}} */
     154             : 
     155             : /* {{{ php_stream_mode_sanitize_fdopen_fopencookie
     156             :  * Result should have at least size 5, e.g. to write wbx+\0 */
     157          85 : void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *result)
     158             : {
     159             :         /* replace modes not supported by fdopen and fopencookie, but supported 
     160             :          * by PHP's fread(), so that their calls won't fail */
     161          85 :         const char *cur_mode = stream->mode;
     162          85 :         int         has_plus = 0,
     163          85 :                         has_bin  = 0,
     164             :                                 i,
     165          85 :                                 res_curs = 0;
     166             : 
     167         169 :         if (cur_mode[0] == 'r' || cur_mode[0] == 'w' || cur_mode[0] == 'a') {
     168          84 :                 result[res_curs++] = cur_mode[0];
     169             :         } else {
     170             :                 /* assume cur_mode[0] is 'c' or 'x'; substitute by 'w', which should not
     171             :                  * truncate anything in fdopen/fopencookie */
     172           1 :                 result[res_curs++] = 'w';
     173             : 
     174             :                 /* x is allowed (at least by glibc & compat), but not as the 1st mode
     175             :                  * as in PHP and in any case is (at best) ignored by fdopen and fopencookie */
     176             :         }
     177             :         
     178             :         /* assume current mode has at most length 4 (e.g. wbn+) */
     179         160 :         for (i = 1; i < 4 && cur_mode[i] != '\0'; i++) {
     180          75 :                 if (cur_mode[i] == 'b') {
     181          68 :                         has_bin = 1;
     182           7 :                 } else if (cur_mode[i] == '+') {
     183           7 :                         has_plus = 1;
     184             :                 }
     185             :                 /* ignore 'n', 't' or other stuff */
     186             :         }
     187             : 
     188          85 :         if (has_bin) {
     189          68 :                 result[res_curs++] = 'b';
     190             :         }
     191          85 :         if (has_plus) {
     192           7 :                 result[res_curs++] = '+';
     193             :         }
     194             : 
     195          85 :         result[res_curs] = '\0';
     196          85 : }
     197             : /* }}} */
     198             : 
     199             : /* {{{ php_stream_cast */
     200     3370189 : PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show_err TSRMLS_DC)
     201             : {
     202     3370189 :         int flags = castas & PHP_STREAM_CAST_MASK;
     203     3370189 :         castas &= ~PHP_STREAM_CAST_MASK;
     204             : 
     205             :         /* synchronize our buffer (if possible) */
     206     3370189 :         if (ret && castas != PHP_STREAM_AS_FD_FOR_SELECT) {
     207        1491 :                 php_stream_flush(stream);
     208        1491 :                 if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
     209             :                         off_t dummy;
     210             : 
     211        1398 :                         stream->ops->seek(stream, stream->position, SEEK_SET, &dummy TSRMLS_CC);
     212        1398 :                         stream->readpos = stream->writepos = 0;
     213             :                 }
     214             :         }
     215             : 
     216             :         /* filtered streams can only be cast as stdio, and only when fopencookie is present */
     217             : 
     218     3370189 :         if (castas == PHP_STREAM_AS_STDIO) {
     219        1113 :                 if (stream->stdiocast) {
     220        1027 :                         if (ret) {
     221        1027 :                                 *(FILE**)ret = stream->stdiocast;
     222             :                         }
     223        1027 :                         goto exit_success;
     224             :                 }
     225             : 
     226             :                 /* if the stream is a stdio stream let's give it a chance to respond
     227             :                  * first, to avoid doubling up the layers of stdio with an fopencookie */
     228         256 :                 if (php_stream_is(stream, PHP_STREAM_IS_STDIO) &&
     229          85 :                         stream->ops->cast &&
     230             :                         !php_stream_is_filtered(stream) &&
     231          85 :                         stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS
     232             :                 ) {
     233          85 :                         goto exit_success;
     234             :                 }
     235             : 
     236             : #if HAVE_FOPENCOOKIE
     237             :                 /* if just checking, say yes we can be a FILE*, but don't actually create it yet */
     238           1 :                 if (ret == NULL) {
     239           0 :                         goto exit_success;
     240             :                 }
     241             : 
     242             :                 {
     243             :                         char fixed_mode[5];
     244           1 :                         php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode);
     245           1 :                         *(FILE**)ret = fopencookie(stream, fixed_mode, PHP_STREAM_COOKIE_FUNCTIONS);
     246             :                 }
     247             : 
     248           1 :                 if (*ret != NULL) {
     249             :                         off_t pos;
     250             : 
     251           1 :                         stream->fclose_stdiocast = PHP_STREAM_FCLOSE_FOPENCOOKIE;
     252             : 
     253             :                         /* If the stream position is not at the start, we need to force
     254             :                          * the stdio layer to believe it's real location. */
     255           1 :                         pos = php_stream_tell(stream);
     256           1 :                         if (pos > 0) {
     257           1 :                                 fseek(*ret, pos, SEEK_SET);
     258             :                         }
     259             : 
     260           1 :                         goto exit_success;
     261             :                 }
     262             : 
     263             :                 /* must be either:
     264             :                         a) programmer error
     265             :                         b) no memory
     266             :                         -> lets bail
     267             :                 */
     268           0 :                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "fopencookie failed");
     269           0 :                 return FAILURE;
     270             : #endif
     271             : 
     272             :                 if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) {
     273             :                         if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) {
     274             :                                 return FAILURE;
     275             :                         }
     276             :                         goto exit_success;
     277             :                 } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
     278             :                         php_stream *newstream;
     279             : 
     280             :                         newstream = php_stream_fopen_tmpfile();
     281             :                         if (newstream) {
     282             :                                 int retcopy = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL, NULL);
     283             : 
     284             :                                 if (retcopy != SUCCESS) {
     285             :                                         php_stream_close(newstream);
     286             :                                 } else {
     287             :                                         int retcast = php_stream_cast(newstream, castas | flags, (void **)ret, show_err);
     288             : 
     289             :                                         if (retcast == SUCCESS) {
     290             :                                                 rewind(*(FILE**)ret);
     291             :                                         }
     292             : 
     293             :                                         /* do some specialized cleanup */
     294             :                                         if ((flags & PHP_STREAM_CAST_RELEASE)) {
     295             :                                                 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
     296             :                                         }
     297             : 
     298             :                                         /* TODO: we probably should be setting .stdiocast and .fclose_stdiocast or
     299             :                                          * we may be leaking the FILE*. Needs investigation, though. */
     300             :                                         return retcast;
     301             :                                 }
     302             :                         }
     303             :                 }
     304             :         }
     305             : 
     306     3369076 :         if (php_stream_is_filtered(stream)) {
     307           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot cast a filtered stream on this system");
     308           0 :                 return FAILURE;
     309     3369076 :         } else if (stream->ops->cast && stream->ops->cast(stream, castas, ret TSRMLS_CC) == SUCCESS) {
     310     3369068 :                 goto exit_success;
     311             :         }
     312             : 
     313           8 :         if (show_err) {
     314             :                 /* these names depend on the values of the PHP_STREAM_AS_XXX defines in php_streams.h */
     315             :                 static const char *cast_names[4] = {
     316             :                         "STDIO FILE*",
     317             :                         "File Descriptor",
     318             :                         "Socket Descriptor",
     319             :                         "select()able descriptor"
     320             :                 };
     321             : 
     322           7 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot represent a stream of type %s as a %s", stream->ops->label, cast_names[castas]);
     323             :         }
     324             : 
     325           8 :         return FAILURE;
     326             : 
     327             : exit_success:
     328             : 
     329     3370183 :         if ((stream->writepos - stream->readpos) > 0 &&
     330           1 :                 stream->fclose_stdiocast != PHP_STREAM_FCLOSE_FOPENCOOKIE &&
     331           1 :                 (flags & PHP_STREAM_CAST_INTERNAL) == 0
     332             :         ) {
     333             :                 /* the data we have buffered will be lost to the third party library that
     334             :                  * will be accessing the stream.  Emit a warning so that the end-user will
     335             :                  * know that they should try something else */
     336             : 
     337           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld bytes of buffered data lost during stream conversion!", (long)(stream->writepos - stream->readpos));
     338             :         }
     339             : 
     340     3370181 :         if (castas == PHP_STREAM_AS_STDIO && ret) {
     341        1113 :                 stream->stdiocast = *(FILE**)ret;
     342             :         }
     343             : 
     344     3370181 :         if (flags & PHP_STREAM_CAST_RELEASE) {
     345           0 :                 php_stream_free(stream, PHP_STREAM_FREE_CLOSE_CASTED);
     346             :         }
     347             : 
     348     3370181 :         return SUCCESS;
     349             : 
     350             : }
     351             : /* }}} */
     352             : 
     353             : /* {{{ php_stream_open_wrapper_as_file */
     354           0 : PHPAPI FILE * _php_stream_open_wrapper_as_file(char *path, char *mode, int options, char **opened_path STREAMS_DC TSRMLS_DC)
     355             : {
     356           0 :         FILE *fp = NULL;
     357           0 :         php_stream *stream = NULL;
     358             : 
     359           0 :         stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);
     360             : 
     361           0 :         if (stream == NULL) {
     362           0 :                 return NULL;
     363             :         }
     364             : 
     365           0 :         if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
     366           0 :                 php_stream_close(stream);
     367           0 :                 if (opened_path && *opened_path) {
     368           0 :                         efree(*opened_path);
     369             :                 }
     370           0 :                 return NULL;
     371             :         }
     372           0 :         return fp;
     373             : }
     374             : /* }}} */
     375             : 
     376             : /* {{{ php_stream_make_seekable */
     377        9132 : PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstream, int flags STREAMS_DC TSRMLS_DC)
     378             : {
     379        9132 :         if (newstream == NULL) {
     380           0 :                 return PHP_STREAM_FAILED;
     381             :         }
     382        9132 :         *newstream = NULL;
     383             : 
     384        9132 :         if (((flags & PHP_STREAM_FORCE_CONVERSION) == 0) && origstream->ops->seek != NULL) {
     385        9132 :                 *newstream = origstream;
     386        9132 :                 return PHP_STREAM_UNCHANGED;
     387             :         }
     388             : 
     389             :         /* Use a tmpfile and copy the old streams contents into it */
     390             : 
     391           0 :         if (flags & PHP_STREAM_PREFER_STDIO) {
     392           0 :                 *newstream = php_stream_fopen_tmpfile();
     393             :         } else {
     394           0 :                 *newstream = php_stream_temp_new();
     395             :         }
     396             : 
     397           0 :         if (*newstream == NULL) {
     398           0 :                 return PHP_STREAM_FAILED;
     399             :         }
     400             : 
     401             : #if ZEND_DEBUG
     402             :         (*newstream)->open_filename = origstream->open_filename;
     403             :         (*newstream)->open_lineno = origstream->open_lineno;
     404             : #endif
     405             : 
     406           0 :         if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL, NULL) != SUCCESS) {
     407           0 :                 php_stream_close(*newstream);
     408           0 :                 *newstream = NULL;
     409           0 :                 return PHP_STREAM_CRITICAL;
     410             :         }
     411             : 
     412           0 :         php_stream_close(origstream);
     413           0 :         php_stream_seek(*newstream, 0, SEEK_SET);
     414             : 
     415           0 :         return PHP_STREAM_RELEASED;
     416             : }
     417             : /* }}} */
     418             : 
     419             : /*
     420             :  * Local variables:
     421             :  * tab-width: 4
     422             :  * c-basic-offset: 4
     423             :  * End:
     424             :  * vim600: noet sw=4 ts=4 fdm=marker
     425             :  * vim<600: noet sw=4 ts=4
     426             :  */

Generated by: LCOV version 1.10

Generated at Fri, 24 Oct 2014 05:22:01 +0000 (39 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.