xref: /PHP-7.4/ext/gd/gd_ctx.c (revision 345a75f5)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #include "php_gd.h"
20 
21 #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
22 
_php_image_output_putc(struct gdIOCtx * ctx,int c)23 static void _php_image_output_putc(struct gdIOCtx *ctx, int c) /* {{{ */
24 {
25 	/* without the following downcast, the write will fail
26 	 * (i.e., will write a zero byte) for all
27 	 * big endian architectures:
28 	 */
29 	unsigned char ch = (unsigned char) c;
30 	php_write(&ch, 1);
31 } /* }}} */
32 
_php_image_output_putbuf(struct gdIOCtx * ctx,const void * buf,int l)33 static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
34 {
35 	return php_write((void *)buf, l);
36 } /* }}} */
37 
_php_image_output_ctxfree(struct gdIOCtx * ctx)38 static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
39 {
40 	if(ctx) {
41 		efree(ctx);
42 	}
43 } /* }}} */
44 
_php_image_stream_putc(struct gdIOCtx * ctx,int c)45 static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
46 	char ch = (char) c;
47 	php_stream * stream = (php_stream *)ctx->data;
48 	php_stream_write(stream, &ch, 1);
49 } /* }}} */
50 
_php_image_stream_putbuf(struct gdIOCtx * ctx,const void * buf,int l)51 static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
52 {
53 	php_stream * stream = (php_stream *)ctx->data;
54 	return php_stream_write(stream, (void *)buf, l);
55 } /* }}} */
56 
_php_image_stream_ctxfree(struct gdIOCtx * ctx)57 static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
58 {
59 	if(ctx->data) {
60 		ctx->data = NULL;
61 	}
62 	if(ctx) {
63 		efree(ctx);
64 	}
65 } /* }}} */
66 
_php_image_stream_ctxfreeandclose(struct gdIOCtx * ctx)67 static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
68 {
69 
70 	if(ctx->data) {
71 		php_stream_close((php_stream *) ctx->data);
72 		ctx->data = NULL;
73 	}
74 	if(ctx) {
75 		efree(ctx);
76 	}
77 } /* }}} */
78 
79 /* {{{ _php_image_output_ctx */
_php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS,int image_type,char * tn,void (* func_p)())80 static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
81 {
82 	zval *imgind;
83 	char *file = NULL;
84 	size_t file_len = 0;
85 	zend_long quality, basefilter;
86 	zend_bool compressed = 1;
87 	gdImagePtr im;
88 	int argc = ZEND_NUM_ARGS();
89 	int q = -1, i;
90 	int f = -1;
91 	gdIOCtx *ctx = NULL;
92 	zval *to_zval = NULL;
93 	php_stream *stream;
94 	int close_stream = 1;
95 
96 	/* The third (quality) parameter for Wbmp and Xbm stands for the foreground color index when called
97 	 * from image<type>().
98 	 */
99 	switch (image_type) {
100 		case PHP_GDIMG_TYPE_XBM:
101 			if (zend_parse_parameters(argc, "rp!|ll", &imgind, &file, &file_len, &quality, &basefilter) == FAILURE) {
102 				return;
103 			}
104 			break;
105 		case PHP_GDIMG_TYPE_BMP:
106 			if (zend_parse_parameters(argc, "r|z!b", &imgind, &to_zval, &compressed) == FAILURE) {
107 				return;
108 			}
109 			break;
110 		default:
111 			/* PHP_GDIMG_TYPE_GIF
112 			 * PHP_GDIMG_TYPE_PNG
113 			 * PHP_GDIMG_TYPE_JPG
114 			 * PHP_GDIMG_TYPE_WBM
115 			 * PHP_GDIMG_TYPE_WEBP
116 			 * */
117 			if (zend_parse_parameters(argc, "r|z!ll", &imgind, &to_zval, &quality, &basefilter) == FAILURE) {
118 				return;
119 			}
120 	}
121 
122 	if ((im = (gdImagePtr)zend_fetch_resource(Z_RES_P(imgind), "Image", phpi_get_le_gd())) == NULL) {
123 		RETURN_FALSE;
124 	}
125 
126 	if (image_type != PHP_GDIMG_TYPE_BMP && argc >= 3) {
127 		q = quality; /* or colorindex for foreground of BW images (defaults to black) */
128 		if (argc == 4) {
129 			f = basefilter;
130 		}
131 	}
132 
133 	if (argc > 1 && to_zval != NULL) {
134 		if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
135 			php_stream_from_zval_no_verify(stream, to_zval);
136 			if (stream == NULL) {
137 				RETURN_FALSE;
138 			}
139 			close_stream = 0;
140 		} else if (Z_TYPE_P(to_zval) == IS_STRING) {
141 			if (CHECK_ZVAL_NULL_PATH(to_zval)) {
142 				php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, filename must not contain null bytes");
143 				RETURN_FALSE;
144 			}
145 
146 			stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
147 			if (stream == NULL) {
148 				RETURN_FALSE;
149 			}
150 		} else {
151 			php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
152 			RETURN_FALSE;
153 		}
154 	} else if (argc > 1 && file != NULL) {
155 		stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
156 		if (stream == NULL) {
157 			RETURN_FALSE;
158 		}
159 	} else {
160 		ctx = ecalloc(1, sizeof(gdIOCtx));
161 		ctx->putC = _php_image_output_putc;
162 		ctx->putBuf = _php_image_output_putbuf;
163 		ctx->gd_free = _php_image_output_ctxfree;
164 	}
165 
166 	if (!ctx)	{
167 		ctx = ecalloc(1, sizeof(gdIOCtx));
168 		ctx->putC = _php_image_stream_putc;
169 		ctx->putBuf = _php_image_stream_putbuf;
170 		if (close_stream) {
171 			ctx->gd_free = _php_image_stream_ctxfreeandclose;
172 		} else {
173 			ctx->gd_free = _php_image_stream_ctxfree;
174 		}
175 		ctx->data = (void *)stream;
176 	}
177 
178 	switch(image_type) {
179 		case PHP_GDIMG_TYPE_JPG:
180 			(*func_p)(im, ctx, q);
181 			break;
182 		case PHP_GDIMG_TYPE_WEBP:
183 			if (q == -1) {
184 				q = 80;
185 			}
186 			(*func_p)(im, ctx, q);
187 			break;
188 		case PHP_GDIMG_TYPE_PNG:
189 			(*func_p)(im, ctx, q, f);
190 			break;
191 		case PHP_GDIMG_TYPE_XBM:
192 		case PHP_GDIMG_TYPE_WBM:
193 			if (argc < 3) {
194 				for(i=0; i < gdImageColorsTotal(im); i++) {
195 					if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
196 				}
197 				q = i;
198 			}
199 			if (image_type == PHP_GDIMG_TYPE_XBM) {
200 				(*func_p)(im, file ? file : "", q, ctx);
201 			} else {
202 				(*func_p)(im, q, ctx);
203 			}
204 			break;
205 		case PHP_GDIMG_TYPE_BMP:
206 			(*func_p)(im, ctx, (int) compressed);
207 			break;
208 		default:
209 			(*func_p)(im, ctx);
210 			break;
211 	}
212 
213 	ctx->gd_free(ctx);
214 
215 	RETURN_TRUE;
216 }
217 /* }}} */
218