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/gd - gd_ctx.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 103 121 85.1 %
Date: 2022-01-21 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2018 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: Stanislav Malyshev <stas@php.net>                           |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include "php_gd.h"
      22             : 
      23             : #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
      24             : 
      25          27 : static void _php_image_output_putc(struct gdIOCtx *ctx, int c) /* {{{ */
      26             : {
      27             :         /* without the following downcast, the write will fail
      28             :          * (i.e., will write a zero byte) for all
      29             :          * big endian architectures:
      30             :          */
      31          27 :         unsigned char ch = (unsigned char) c;
      32          27 :         php_write(&ch, 1);
      33          27 : } /* }}} */
      34             : 
      35          81 : static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
      36             : {
      37          81 :         return php_write((void *)buf, l);
      38             : } /* }}} */
      39             : 
      40           6 : static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
      41             : {
      42           6 :         if(ctx) {
      43           6 :                 efree(ctx);
      44             :         }
      45           6 : } /* }}} */
      46             : 
      47        3616 : static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
      48        3616 :         char ch = (char) c;
      49        3616 :         php_stream * stream = (php_stream *)ctx->data;
      50        3616 :         php_stream_write(stream, &ch, 1);
      51        3616 : } /* }}} */
      52             : 
      53        1458 : static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
      54             : {
      55        1458 :         php_stream * stream = (php_stream *)ctx->data;
      56        1458 :         return php_stream_write(stream, (void *)buf, l);
      57             : } /* }}} */
      58             : 
      59           1 : static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
      60             : {
      61           1 :         if(ctx->data) {
      62           1 :                 ctx->data = NULL;
      63             :         }
      64           1 :         if(ctx) {
      65           1 :                 efree(ctx);
      66             :         }
      67           1 : } /* }}} */
      68             : 
      69          72 : static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
      70             : {
      71             : 
      72          72 :         if(ctx->data) {
      73          72 :                 php_stream_close((php_stream *) ctx->data);
      74          72 :                 ctx->data = NULL;
      75             :         }
      76          72 :         if(ctx) {
      77          72 :                 efree(ctx);
      78             :         }
      79          72 : } /* }}} */
      80             : 
      81             : /* {{{ _php_image_output_ctx */
      82          83 : static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
      83             : {
      84             :         zval *imgind;
      85          83 :         char *file = NULL;
      86          83 :         size_t file_len = 0;
      87             :         zend_long quality, basefilter;
      88          83 :         zend_bool compressed = 1;
      89             :         gdImagePtr im;
      90          83 :         int argc = ZEND_NUM_ARGS();
      91          83 :         int q = -1, i;
      92          83 :         int f = -1;
      93          83 :         gdIOCtx *ctx = NULL;
      94          83 :         zval *to_zval = NULL;
      95             :         php_stream *stream;
      96          83 :         int close_stream = 1;
      97             : 
      98             :         /* The third (quality) parameter for Wbmp stands for the threshold when called from image2wbmp().
      99             :          * The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
     100             :          * from imagey<type>().
     101             :          */
     102          83 :         switch (image_type) {
     103           5 :                 case PHP_GDIMG_TYPE_XBM:
     104           5 :                         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
     105           0 :                                 return;
     106             :                         }
     107           5 :                         break;
     108           1 :                 case PHP_GDIMG_TYPE_BMP:
     109           1 :                         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z/!b", &imgind, &to_zval, &compressed) == FAILURE) {
     110           0 :                                 return;
     111             :                         }
     112           1 :                         break;
     113          77 :                 default:
     114             :                         /* PHP_GDIMG_TYPE_GIF
     115             :                          * PHP_GDIMG_TYPE_PNG
     116             :                          * PHP_GDIMG_TYPE_JPG
     117             :                          * PHP_GDIMG_TYPE_WBM
     118             :                          * PHP_GDIMG_TYPE_WEBP
     119             :                          * */
     120          77 :                         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|z/!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
     121           0 :                                 return;
     122             :                         }
     123             :         }
     124             : 
     125          83 :         if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) {
     126           0 :                 RETURN_FALSE;
     127             :         }
     128             : 
     129          83 :         if (image_type != PHP_GDIMG_TYPE_BMP && argc >= 3) {
     130           1 :                 q = quality; /* or colorindex for foreground of BW images (defaults to black) */
     131           1 :                 if (argc == 4) {
     132           0 :                         f = basefilter;
     133             :                 }
     134             :         }
     135             : 
     136          83 :         if (argc > 1 && to_zval != NULL) {
     137         221 :                 if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
     138           1 :                         php_stream_from_zval_no_verify(stream, to_zval);
     139           1 :                         if (stream == NULL) {
     140           0 :                                 RETURN_FALSE;
     141             :                         }
     142           1 :                         close_stream = 0;
     143         148 :                 } else if (Z_TYPE_P(to_zval) == IS_STRING) {
     144          74 :                         if (CHECK_ZVAL_NULL_PATH(to_zval)) {
     145           4 :                                 php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
     146           4 :                                 RETURN_FALSE;
     147             :                         }
     148             : 
     149          70 :                         stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
     150          70 :                         if (stream == NULL) {
     151           0 :                                 RETURN_FALSE;
     152             :                         }
     153             :                 } else {
     154           0 :                         php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
     155           0 :                         RETURN_FALSE;
     156             :                 }
     157           8 :         } else if (argc > 1 && file != NULL) {
     158           2 :                 stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
     159           4 :                 if (stream == NULL) {
     160           0 :                         RETURN_FALSE;
     161             :                 }
     162             :         } else {
     163           6 :                 ctx = ecalloc(1, sizeof(gdIOCtx));
     164           6 :                 ctx->putC = _php_image_output_putc;
     165           6 :                 ctx->putBuf = _php_image_output_putbuf;
     166           6 :                 ctx->gd_free = _php_image_output_ctxfree;
     167             : 
     168             : #if APACHE && defined(CHARSET_EBCDIC)
     169             :                 /* XXX this is unlikely to work any more thies@thieso.net */
     170             :                 /* This is a binary file already: avoid EBCDIC->ASCII conversion */
     171             :                 ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
     172             : #endif
     173             :         }
     174             : 
     175          79 :         if (!ctx)       {
     176          73 :                 ctx = ecalloc(1, sizeof(gdIOCtx));
     177          73 :                 ctx->putC = _php_image_stream_putc;
     178          73 :                 ctx->putBuf = _php_image_stream_putbuf;
     179          73 :                 if (close_stream) {
     180          72 :                         ctx->gd_free = _php_image_stream_ctxfreeandclose;
     181             :                 } else {
     182           1 :                         ctx->gd_free = _php_image_stream_ctxfree;
     183             :                 }
     184          73 :                 ctx->data = (void *)stream;
     185             :         }
     186             : 
     187          79 :         switch(image_type) {
     188           0 :                 case PHP_GDIMG_CONVERT_WBM:
     189           0 :                         if(q<0||q>255) {
     190           0 :                                 php_error_docref(NULL, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
     191             :                         }
     192             :                 case PHP_GDIMG_TYPE_JPG:
     193          13 :                         (*func_p)(im, ctx, q);
     194          13 :                         break;
     195           0 :                 case PHP_GDIMG_TYPE_WEBP:
     196           0 :                         if (q == -1) {
     197           0 :                                 q = 80;
     198             :                         }
     199           0 :                         (*func_p)(im, ctx, q);
     200           0 :                         break;
     201          44 :                 case PHP_GDIMG_TYPE_PNG:
     202          44 :                         (*func_p)(im, ctx, q, f);
     203          44 :                         break;
     204           8 :                 case PHP_GDIMG_TYPE_XBM:
     205             :                 case PHP_GDIMG_TYPE_WBM:
     206           8 :                         if (argc < 3) {
     207           8 :                                 for(i=0; i < gdImageColorsTotal(im); i++) {
     208           6 :                                         if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
     209             :                                 }
     210           8 :                                 q = i;
     211             :                         }
     212           8 :                         if (image_type == PHP_GDIMG_TYPE_XBM) {
     213           5 :                                 (*func_p)(im, file ? file : "", q, ctx);
     214             :                         } else {
     215           3 :                                 (*func_p)(im, q, ctx);
     216             :                         }
     217           8 :                         break;
     218           1 :                 case PHP_GDIMG_TYPE_BMP:
     219           1 :                         (*func_p)(im, ctx, (int) compressed);
     220           1 :                         break;
     221          13 :                 default:
     222          13 :                         (*func_p)(im, ctx);
     223          13 :                         break;
     224             :         }
     225             : 
     226          79 :         ctx->gd_free(ctx);
     227             : 
     228          79 :         RETURN_TRUE;
     229             : }
     230             : /* }}} */
     231             : 
     232             : /*
     233             :  * Local variables:
     234             :  * tab-width: 4
     235             :  * c-basic-offset: 4
     236             :  * End:
     237             :  * vim600: sw=4 ts=4 fdm=marker
     238             :  * vim<600: sw=4 ts=4
     239             :  */

Generated by: LCOV version 1.10

Generated at Fri, 21 Jan 2022 14:15:10 +0000 (31 hours ago)

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