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