xref: /PHP-7.4/ext/standard/iptc.c (revision 5f9c82d5)
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    | Author: Thies C. Arntzen <thies@thieso.net>                          |
16    +----------------------------------------------------------------------+
17  */
18 
19 /*
20  * Functions to parse & compse IPTC data.
21  * PhotoShop >= 3.0 can read and write textual data to JPEG files.
22  * ... more to come .....
23  *
24  * i know, parts of this is now duplicated in image.c
25  * but in this case i think it's okay!
26  */
27 
28 /*
29  * TODO:
30  *  - add IPTC translation table
31  */
32 
33 #include "php.h"
34 #include "php_iptc.h"
35 #include "ext/standard/head.h"
36 
37 #include <sys/stat.h>
38 
39 #ifdef PHP_WIN32
40 # include "win32/php_stdint.h"
41 #else
42 # if HAVE_INTTYPES_H
43 #  include <inttypes.h>
44 # elif HAVE_STDINT_H
45 #  include <stdint.h>
46 # endif
47 #endif
48 
49 /* some defines for the different JPEG block types */
50 #define M_SOF0  0xC0            /* Start Of Frame N */
51 #define M_SOF1  0xC1            /* N indicates which compression process */
52 #define M_SOF2  0xC2            /* Only SOF0-SOF2 are now in common use */
53 #define M_SOF3  0xC3
54 #define M_SOF5  0xC5            /* NB: codes C4 and CC are NOT SOF markers */
55 #define M_SOF6  0xC6
56 #define M_SOF7  0xC7
57 #define M_SOF9  0xC9
58 #define M_SOF10 0xCA
59 #define M_SOF11 0xCB
60 #define M_SOF13 0xCD
61 #define M_SOF14 0xCE
62 #define M_SOF15 0xCF
63 #define M_SOI   0xD8
64 #define M_EOI   0xD9            /* End Of Image (end of datastream) */
65 #define M_SOS   0xDA            /* Start Of Scan (begins compressed data) */
66 #define M_APP0  0xe0
67 #define M_APP1  0xe1
68 #define M_APP2  0xe2
69 #define M_APP3  0xe3
70 #define M_APP4  0xe4
71 #define M_APP5  0xe5
72 #define M_APP6  0xe6
73 #define M_APP7  0xe7
74 #define M_APP8  0xe8
75 #define M_APP9  0xe9
76 #define M_APP10 0xea
77 #define M_APP11 0xeb
78 #define M_APP12 0xec
79 #define M_APP13 0xed
80 #define M_APP14 0xee
81 #define M_APP15 0xef
82 
83 /* {{{ php_iptc_put1
84  */
php_iptc_put1(FILE * fp,int spool,unsigned char c,unsigned char ** spoolbuf)85 static int php_iptc_put1(FILE *fp, int spool, unsigned char c, unsigned char **spoolbuf)
86 {
87 	if (spool > 0)
88 		PUTC(c);
89 
90 	if (spoolbuf) *(*spoolbuf)++ = c;
91 
92   	return c;
93 }
94 /* }}} */
95 
96 /* {{{ php_iptc_get1
97  */
php_iptc_get1(FILE * fp,int spool,unsigned char ** spoolbuf)98 static int php_iptc_get1(FILE *fp, int spool, unsigned char **spoolbuf)
99 {
100 	int c;
101 	char cc;
102 
103 	c = getc(fp);
104 
105 	if (c == EOF) return EOF;
106 
107 	if (spool > 0) {
108 		cc = c;
109 		PUTC(cc);
110 	}
111 
112 	if (spoolbuf) *(*spoolbuf)++ = c;
113 
114 	return c;
115 }
116 /* }}} */
117 
118 /* {{{ php_iptc_read_remaining
119  */
php_iptc_read_remaining(FILE * fp,int spool,unsigned char ** spoolbuf)120 static int php_iptc_read_remaining(FILE *fp, int spool, unsigned char **spoolbuf)
121 {
122   	while (php_iptc_get1(fp, spool, spoolbuf) != EOF) continue;
123 
124 	return M_EOI;
125 }
126 /* }}} */
127 
128 /* {{{ php_iptc_skip_variable
129  */
php_iptc_skip_variable(FILE * fp,int spool,unsigned char ** spoolbuf)130 static int php_iptc_skip_variable(FILE *fp, int spool, unsigned char **spoolbuf)
131 {
132 	unsigned int  length;
133 	int c1, c2;
134 
135     if ((c1 = php_iptc_get1(fp, spool, spoolbuf)) == EOF) return M_EOI;
136 
137     if ((c2 = php_iptc_get1(fp, spool, spoolbuf)) == EOF) return M_EOI;
138 
139 	length = (((unsigned char) c1) << 8) + ((unsigned char) c2);
140 
141 	length -= 2;
142 
143 	while (length--)
144 		if (php_iptc_get1(fp, spool, spoolbuf) == EOF) return M_EOI;
145 
146 	return 0;
147 }
148 /* }}} */
149 
150 /* {{{ php_iptc_next_marker
151  */
php_iptc_next_marker(FILE * fp,int spool,unsigned char ** spoolbuf)152 static int php_iptc_next_marker(FILE *fp, int spool, unsigned char **spoolbuf)
153 {
154     int c;
155 
156     /* skip unimportant stuff */
157 
158     c = php_iptc_get1(fp, spool, spoolbuf);
159 
160 	if (c == EOF) return M_EOI;
161 
162     while (c != 0xff) {
163         if ((c = php_iptc_get1(fp, spool, spoolbuf)) == EOF)
164             return M_EOI; /* we hit EOF */
165     }
166 
167     /* get marker byte, swallowing possible padding */
168     do {
169         c = php_iptc_get1(fp, 0, 0);
170 		if (c == EOF)
171             return M_EOI;       /* we hit EOF */
172 		else
173 		if (c == 0xff)
174 			php_iptc_put1(fp, spool, (unsigned char)c, spoolbuf);
175     } while (c == 0xff);
176 
177     return (unsigned int) c;
178 }
179 /* }}} */
180 
181 static char psheader[] = "\xFF\xED\0\0Photoshop 3.0\08BIM\x04\x04\0\0\0\0";
182 
183 /* {{{ proto array iptcembed(string iptcdata, string jpeg_file_name [, int spool])
184    Embed binary IPTC data into a JPEG image. */
PHP_FUNCTION(iptcembed)185 PHP_FUNCTION(iptcembed)
186 {
187 	char *iptcdata, *jpeg_file;
188 	size_t iptcdata_len, jpeg_file_len;
189 	zend_long spool = 0;
190 	FILE *fp;
191 	unsigned int marker, done = 0;
192 	size_t inx;
193 	zend_string *spoolbuf = NULL;
194 	unsigned char *poi = NULL;
195 	zend_stat_t sb;
196 	zend_bool written = 0;
197 
198 	ZEND_PARSE_PARAMETERS_START(2, 3)
199 		Z_PARAM_STRING(iptcdata, iptcdata_len)
200 		Z_PARAM_PATH(jpeg_file, jpeg_file_len)
201 		Z_PARAM_OPTIONAL
202 		Z_PARAM_LONG(spool)
203 	ZEND_PARSE_PARAMETERS_END();
204 
205 	if (php_check_open_basedir(jpeg_file)) {
206 		RETURN_FALSE;
207 	}
208 
209 	if (iptcdata_len >= SIZE_MAX - sizeof(psheader) - 1025) {
210 		php_error_docref(NULL, E_WARNING, "IPTC data too large");
211 		RETURN_FALSE;
212 	}
213 
214 	if ((fp = VCWD_FOPEN(jpeg_file, "rb")) == 0) {
215 		php_error_docref(NULL, E_WARNING, "Unable to open %s", jpeg_file);
216 		RETURN_FALSE;
217 	}
218 
219 	if (spool < 2) {
220 		if (zend_fstat(fileno(fp), &sb) != 0) {
221 			RETURN_FALSE;
222 		}
223 
224 		spoolbuf = zend_string_safe_alloc(1, iptcdata_len + sizeof(psheader) + 1024 + 1, sb.st_size, 0);
225 		poi = (unsigned char*)ZSTR_VAL(spoolbuf);
226 		memset(poi, 0, iptcdata_len + sizeof(psheader) + sb.st_size + 1024 + 1);
227 	}
228 
229 	if (php_iptc_get1(fp, spool, poi?&poi:0) != 0xFF) {
230 		fclose(fp);
231 		if (spoolbuf) {
232 			zend_string_efree(spoolbuf);
233 		}
234 		RETURN_FALSE;
235 	}
236 
237 	if (php_iptc_get1(fp, spool, poi?&poi:0) != 0xD8) {
238 		fclose(fp);
239 		if (spoolbuf) {
240 			zend_string_efree(spoolbuf);
241 		}
242 		RETURN_FALSE;
243 	}
244 
245 	while (!done) {
246 		marker = php_iptc_next_marker(fp, spool, poi?&poi:0);
247 
248 		if (marker == M_EOI) { /* EOF */
249 			break;
250 		} else if (marker != M_APP13) {
251 			php_iptc_put1(fp, spool, (unsigned char)marker, poi?&poi:0);
252 		}
253 
254 		switch (marker) {
255 			case M_APP13:
256 				/* we are going to write a new APP13 marker, so don't output the old one */
257 				php_iptc_skip_variable(fp, 0, 0);
258 				fgetc(fp); /* skip already copied 0xFF byte */
259 				php_iptc_read_remaining(fp, spool, poi?&poi:0);
260 				done = 1;
261 				break;
262 
263 			case M_APP0:
264 				/* APP0 is in each and every JPEG, so when we hit APP0 we insert our new APP13! */
265 			case M_APP1:
266 				if (written) {
267 					/* don't try to write the data twice */
268 					break;
269 				}
270 				written = 1;
271 
272 				php_iptc_skip_variable(fp, spool, poi?&poi:0);
273 
274 				if (iptcdata_len & 1) {
275 					iptcdata_len++; /* make the length even */
276 				}
277 
278 				psheader[ 2 ] = (char) ((iptcdata_len+28)>>8);
279 				psheader[ 3 ] = (iptcdata_len+28)&0xff;
280 
281 				for (inx = 0; inx < 28; inx++) {
282 					php_iptc_put1(fp, spool, psheader[inx], poi?&poi:0);
283 				}
284 
285 				php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len>>8), poi?&poi:0);
286 				php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len&0xff), poi?&poi:0);
287 
288 				for (inx = 0; inx < iptcdata_len; inx++) {
289 					php_iptc_put1(fp, spool, iptcdata[inx], poi?&poi:0);
290 				}
291 				break;
292 
293 			case M_SOS:
294 				/* we hit data, no more marker-inserting can be done! */
295 				php_iptc_read_remaining(fp, spool, poi?&poi:0);
296 				done = 1;
297 				break;
298 
299 			default:
300 				php_iptc_skip_variable(fp, spool, poi?&poi:0);
301 				break;
302 		}
303 	}
304 
305 	fclose(fp);
306 
307 	if (spool < 2) {
308 		spoolbuf = zend_string_truncate(spoolbuf, poi - (unsigned char*)ZSTR_VAL(spoolbuf), 0);
309 		RETURN_NEW_STR(spoolbuf);
310 	} else {
311 		RETURN_TRUE;
312 	}
313 }
314 /* }}} */
315 
316 /* {{{ proto array iptcparse(string iptcdata)
317    Parse binary IPTC-data into associative array */
PHP_FUNCTION(iptcparse)318 PHP_FUNCTION(iptcparse)
319 {
320 	size_t inx = 0, len;
321 	unsigned int tagsfound = 0;
322 	unsigned char *buffer, recnum, dataset;
323 	char *str, key[16];
324 	size_t str_len;
325 	zval values, *element;
326 
327 	ZEND_PARSE_PARAMETERS_START(1, 1)
328 		Z_PARAM_STRING(str, str_len)
329 	ZEND_PARSE_PARAMETERS_END();
330 
331 	buffer = (unsigned char *)str;
332 
333 	while (inx < str_len) { /* find 1st tag */
334 		if ((buffer[inx] == 0x1c) && ((buffer[inx+1] == 0x01) || (buffer[inx+1] == 0x02))){
335 			break;
336 		} else {
337 			inx++;
338 		}
339 	}
340 
341 	while (inx < str_len) {
342 		if (buffer[ inx++ ] != 0x1c) {
343 			break;   /* we ran against some data which does not conform to IPTC - stop parsing! */
344 		}
345 
346 		if ((inx + 4) >= str_len)
347 			break;
348 
349 		dataset = buffer[ inx++ ];
350 		recnum = buffer[ inx++ ];
351 
352 		if (buffer[ inx ] & (unsigned char) 0x80) { /* long tag */
353 			if((inx+6) >= str_len) {
354 				break;
355 			}
356 			len = (((zend_long) buffer[ inx + 2 ]) << 24) + (((zend_long) buffer[ inx + 3 ]) << 16) +
357 				  (((zend_long) buffer[ inx + 4 ]) <<  8) + (((zend_long) buffer[ inx + 5 ]));
358 			inx += 6;
359 		} else { /* short tag */
360 			len = (((unsigned short) buffer[ inx ])<<8) | (unsigned short)buffer[ inx+1 ];
361 			inx += 2;
362 		}
363 
364 		if ((len > str_len) || (inx + len) > str_len) {
365 			break;
366 		}
367 
368 		snprintf(key, sizeof(key), "%d#%03d", (unsigned int) dataset, (unsigned int) recnum);
369 
370 		if (tagsfound == 0) { /* found the 1st tag - initialize the return array */
371 			array_init(return_value);
372 		}
373 
374 		if ((element = zend_hash_str_find(Z_ARRVAL_P(return_value), key, strlen(key))) == NULL) {
375 			array_init(&values);
376 
377 			element = zend_hash_str_update(Z_ARRVAL_P(return_value), key, strlen(key), &values);
378 		}
379 
380 		add_next_index_stringl(element, (char *) buffer+inx, len);
381 		inx += len;
382 		tagsfound++;
383 	}
384 
385 	if (! tagsfound) {
386 		RETURN_FALSE;
387 	}
388 }
389 /* }}} */
390