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 - ext/zlib - zlib_fopen_wrapper.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 57 59 96.6 %
Date: 2014-08-04 Functions: 6 6 100.0 %
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             :    | Author: Wez Furlong <wez@thebrainroom.com>, based on work by:        |
      16             :    |         Hartmut Holzgraefe <hholzgra@php.net>                        |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #define _GNU_SOURCE
      23             : 
      24             : #include "php.h"
      25             : #include "php_zlib.h"
      26             : #include "fopen_wrappers.h"
      27             : 
      28             : #include "main/php_network.h"
      29             : 
      30             : struct php_gz_stream_data_t     {
      31             :         gzFile gz_file;
      32             :         php_stream *stream;
      33             : };
      34             : 
      35         405 : static size_t php_gziop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
      36             : {
      37         405 :         struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
      38             :         int read;
      39             :         
      40         405 :         read = gzread(self->gz_file, buf, count);
      41             :         
      42         405 :         if (gzeof(self->gz_file)) {
      43         170 :                 stream->eof = 1;
      44             :         }
      45             :                 
      46         405 :         return (read < 0) ? 0 : read;
      47             : }
      48             : 
      49          80 : static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
      50             : {
      51          80 :         struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
      52             :         int wrote;
      53             : 
      54          80 :         wrote = gzwrite(self->gz_file, (char *) buf, count);
      55             : 
      56          80 :         return (wrote < 0) ? 0 : wrote;
      57             : }
      58             : 
      59          22 : static int php_gziop_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
      60             : {
      61          22 :         struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
      62             : 
      63             :         assert(self != NULL);
      64             : 
      65          22 :         if (whence == SEEK_END) {
      66           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SEEK_END is not supported");
      67           2 :                 return -1;
      68             :         }
      69          20 :         *newoffs = gzseek(self->gz_file, offset, whence);
      70             : 
      71          20 :         return (*newoffs < 0) ? -1 : 0;
      72             : }
      73             : 
      74         198 : static int php_gziop_close(php_stream *stream, int close_handle TSRMLS_DC)
      75             : {
      76         198 :         struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
      77         198 :         int ret = EOF;
      78             :         
      79         198 :         if (close_handle) {
      80         198 :                 if (self->gz_file) {
      81         198 :                         ret = gzclose(self->gz_file);
      82         198 :                         self->gz_file = NULL;
      83             :                 }
      84         198 :                 if (self->stream) {
      85         198 :                         php_stream_close(self->stream);
      86         198 :                         self->stream = NULL;
      87             :                 }
      88             :         }
      89         198 :         efree(self);
      90             : 
      91         198 :         return ret;
      92             : }
      93             : 
      94         200 : static int php_gziop_flush(php_stream *stream TSRMLS_DC)
      95             : {
      96         200 :         struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
      97             : 
      98         200 :         return gzflush(self->gz_file, Z_SYNC_FLUSH);
      99             : }
     100             : 
     101             : php_stream_ops php_stream_gzio_ops = {
     102             :         php_gziop_write, php_gziop_read,
     103             :         php_gziop_close, php_gziop_flush,
     104             :         "ZLIB",
     105             :         php_gziop_seek, 
     106             :         NULL, /* cast */
     107             :         NULL, /* stat */
     108             :         NULL  /* set_option */
     109             : };
     110             : 
     111         291 : php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, 
     112             :                                                           char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
     113             : {
     114             :         struct php_gz_stream_data_t *self;
     115         291 :         php_stream *stream = NULL, *innerstream = NULL;
     116             : 
     117             :         /* sanity check the stream: it can be either read-only or write-only */
     118         291 :         if (strchr(mode, '+')) {
     119           8 :                 if (options & REPORT_ERRORS) {
     120           8 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot open a zlib stream for reading and writing at the same time!");
     121             :                 }
     122           8 :                 return NULL;
     123             :         }
     124             :         
     125         283 :         if (strncasecmp("compress.zlib://", path, 16) == 0) {
     126          17 :                 path += 16;
     127         266 :         } else if (strncasecmp("zlib:", path, 5) == 0) {
     128           0 :                 path += 5;
     129             :         }
     130             :         
     131         283 :         innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
     132             :         
     133         283 :         if (innerstream) {
     134             :                 php_socket_t fd;
     135             : 
     136         199 :                 if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
     137         199 :                         self = emalloc(sizeof(*self));
     138         199 :                         self->stream = innerstream;
     139         199 :                         self->gz_file = gzdopen(dup(fd), mode);
     140             : 
     141         199 :                         if (self->gz_file) {
     142         198 :                                 stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
     143         198 :                                 if (stream) {
     144         198 :                                         stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
     145         198 :                                         return stream;
     146             :                                 }
     147             : 
     148           0 :                                 gzclose(self->gz_file);
     149             :                         }
     150             : 
     151           1 :                         efree(self);
     152           1 :                         if (options & REPORT_ERRORS) {
     153           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "gzopen failed");
     154             :                         }
     155             :                 }
     156             : 
     157           1 :                 php_stream_close(innerstream);
     158             :         }
     159             : 
     160          85 :         return NULL;
     161             : }
     162             : 
     163             : static php_stream_wrapper_ops gzip_stream_wops = {
     164             :         php_stream_gzopen,
     165             :         NULL, /* close */
     166             :         NULL, /* stat */
     167             :         NULL, /* stat_url */
     168             :         NULL, /* opendir */
     169             :         "ZLIB",
     170             :         NULL, /* unlink */
     171             :         NULL, /* rename */
     172             :         NULL, /* mkdir */
     173             :         NULL  /* rmdir */
     174             : };
     175             : 
     176             : php_stream_wrapper php_stream_gzip_wrapper =    {
     177             :         &gzip_stream_wops,
     178             :         NULL,
     179             :         0, /* is_url */
     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             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:16 +0000 (16 days ago)

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