Back to index

php5  5.3.10
iptc.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Thies C. Arntzen <thies@thieso.net>                          |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: iptc.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 /*
00022  * Functions to parse & compse IPTC data.
00023  * PhotoShop >= 3.0 can read and write textual data to JPEG files.
00024  * ... more to come .....
00025  * 
00026  * i know, parts of this is now duplicated in image.c 
00027  * but in this case i think it's okay!
00028  */
00029 
00030 /*
00031  * TODO:
00032  *  - add IPTC translation table
00033  */
00034  
00035 #include "php.h"
00036 #include "php_iptc.h"
00037 #include "ext/standard/head.h"
00038 
00039 #include <sys/stat.h>
00040 
00041 
00042 /* some defines for the different JPEG block types */
00043 #define M_SOF0  0xC0            /* Start Of Frame N */
00044 #define M_SOF1  0xC1            /* N indicates which compression process */
00045 #define M_SOF2  0xC2            /* Only SOF0-SOF2 are now in common use */
00046 #define M_SOF3  0xC3
00047 #define M_SOF5  0xC5            /* NB: codes C4 and CC are NOT SOF markers */
00048 #define M_SOF6  0xC6
00049 #define M_SOF7  0xC7
00050 #define M_SOF9  0xC9
00051 #define M_SOF10 0xCA
00052 #define M_SOF11 0xCB
00053 #define M_SOF13 0xCD
00054 #define M_SOF14 0xCE
00055 #define M_SOF15 0xCF
00056 #define M_SOI   0xD8
00057 #define M_EOI   0xD9            /* End Of Image (end of datastream) */
00058 #define M_SOS   0xDA            /* Start Of Scan (begins compressed data) */
00059 #define M_APP0  0xe0
00060 #define M_APP1  0xe1
00061 #define M_APP2  0xe2
00062 #define M_APP3  0xe3
00063 #define M_APP4  0xe4
00064 #define M_APP5  0xe5
00065 #define M_APP6  0xe6
00066 #define M_APP7  0xe7
00067 #define M_APP8  0xe8
00068 #define M_APP9  0xe9
00069 #define M_APP10 0xea
00070 #define M_APP11 0xeb
00071 #define M_APP12 0xec
00072 #define M_APP13 0xed
00073 #define M_APP14 0xee
00074 #define M_APP15 0xef
00075 
00076 /* {{{ php_iptc_put1
00077  */
00078 static int php_iptc_put1(FILE *fp, int spool, unsigned char c, unsigned char **spoolbuf TSRMLS_DC)
00079 { 
00080        if (spool > 0)
00081               PUTC(c);
00082 
00083        if (spoolbuf) *(*spoolbuf)++ = c;
00084 
00085        return c;
00086 }
00087 /* }}} */
00088 
00089 /* {{{ php_iptc_get1
00090  */
00091 static int php_iptc_get1(FILE *fp, int spool, unsigned char **spoolbuf TSRMLS_DC)
00092 {      
00093        int c;
00094        char cc;
00095 
00096        c = getc(fp);
00097 
00098        if (c == EOF) return EOF;
00099 
00100        if (spool > 0) {
00101               cc = c;
00102               PUTC(cc);
00103        }
00104 
00105        if (spoolbuf) *(*spoolbuf)++ = c;
00106 
00107        return c;
00108 }
00109 /* }}} */
00110 
00111 /* {{{ php_iptc_read_remaining
00112  */
00113 static int php_iptc_read_remaining(FILE *fp, int spool, unsigned char **spoolbuf TSRMLS_DC)
00114 {
00115        while (php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC) != EOF) continue;
00116 
00117        return M_EOI;
00118 }
00119 /* }}} */
00120 
00121 /* {{{ php_iptc_skip_variable
00122  */
00123 static int php_iptc_skip_variable(FILE *fp, int spool, unsigned char **spoolbuf TSRMLS_DC)
00124 { 
00125        unsigned int  length;
00126        int c1, c2;
00127 
00128     if ((c1 = php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC)) == EOF) return M_EOI;
00129 
00130     if ((c2 = php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC)) == EOF) return M_EOI;
00131 
00132        length = (((unsigned char) c1) << 8) + ((unsigned char) c2);
00133 
00134        length -= 2;
00135 
00136        while (length--)
00137               if (php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC) == EOF) return M_EOI;
00138 
00139        return 0;
00140 }
00141 /* }}} */
00142 
00143 /* {{{ php_iptc_next_marker
00144  */
00145 static int php_iptc_next_marker(FILE *fp, int spool, unsigned char **spoolbuf TSRMLS_DC)
00146 {
00147     int c;
00148 
00149     /* skip unimportant stuff */
00150 
00151     c = php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC);
00152 
00153        if (c == EOF) return M_EOI;
00154 
00155     while (c != 0xff) {
00156         if ((c = php_iptc_get1(fp, spool, spoolbuf TSRMLS_CC)) == EOF)
00157             return M_EOI; /* we hit EOF */
00158     }
00159 
00160     /* get marker byte, swallowing possible padding */
00161     do {
00162         c = php_iptc_get1(fp, 0, 0 TSRMLS_CC);
00163               if (c == EOF)
00164             return M_EOI;       /* we hit EOF */
00165               else
00166               if (c == 0xff)
00167                      php_iptc_put1(fp, spool, (unsigned char)c, spoolbuf TSRMLS_CC);
00168     } while (c == 0xff);
00169 
00170     return (unsigned int) c;
00171 }
00172 /* }}} */
00173 
00174 static char psheader[] = "\xFF\xED\0\0Photoshop 3.0\08BIM\x04\x04\0\0\0\0";
00175 
00176 /* {{{ proto array iptcembed(string iptcdata, string jpeg_file_name [, int spool])
00177    Embed binary IPTC data into a JPEG image. */
00178 PHP_FUNCTION(iptcembed)
00179 {
00180        char *iptcdata, *jpeg_file;
00181        int iptcdata_len, jpeg_file_len;
00182        long spool = 0;
00183        FILE *fp;
00184        unsigned int marker, done = 0, inx;
00185        unsigned char *spoolbuf = NULL, *poi = NULL;
00186        struct stat sb;
00187        zend_bool written = 0;
00188 
00189        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &iptcdata, &iptcdata_len, &jpeg_file, &jpeg_file_len, &spool) != SUCCESS) {
00190               return;
00191        }
00192 
00193        if (strlen(jpeg_file) != jpeg_file_len) {
00194               RETURN_FALSE;
00195        }
00196 
00197        if (PG(safe_mode) && (!php_checkuid(jpeg_file, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
00198               RETURN_FALSE;
00199        }
00200 
00201        if (php_check_open_basedir(jpeg_file TSRMLS_CC)) {
00202               RETURN_FALSE;
00203        }
00204 
00205        if ((fp = VCWD_FOPEN(jpeg_file, "rb")) == 0) {
00206               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open %s", jpeg_file);
00207               RETURN_FALSE;
00208        }
00209 
00210        if (spool < 2) {
00211               fstat(fileno(fp), &sb);
00212 
00213               poi = spoolbuf = safe_emalloc(1, iptcdata_len + sizeof(psheader) + sb.st_size + 1024, 1);
00214               memset(poi, 0, iptcdata_len + sizeof(psheader) + sb.st_size + 1024 + 1);
00215        } 
00216 
00217        if (php_iptc_get1(fp, spool, poi?&poi:0 TSRMLS_CC) != 0xFF) {
00218               fclose(fp);
00219               if (spoolbuf) {
00220                      efree(spoolbuf);
00221               }
00222               RETURN_FALSE;
00223        }
00224 
00225        if (php_iptc_get1(fp, spool, poi?&poi:0 TSRMLS_CC) != 0xD8) {
00226               fclose(fp);
00227               if (spoolbuf) {
00228                      efree(spoolbuf);
00229               }
00230               RETURN_FALSE;
00231        }
00232 
00233        while (!done) {
00234               marker = php_iptc_next_marker(fp, spool, poi?&poi:0 TSRMLS_CC);
00235 
00236               if (marker == M_EOI) { /* EOF */
00237                      break;
00238               } else if (marker != M_APP13) { 
00239                      php_iptc_put1(fp, spool, (unsigned char)marker, poi?&poi:0 TSRMLS_CC);
00240               }
00241 
00242               switch (marker) {
00243                      case M_APP13:
00244                             /* we are going to write a new APP13 marker, so don't output the old one */
00245                             php_iptc_skip_variable(fp, 0, 0 TSRMLS_CC);    
00246                             php_iptc_read_remaining(fp, spool, poi?&poi:0 TSRMLS_CC);
00247                             done = 1;
00248                             break;
00249 
00250                      case M_APP0:
00251                             /* APP0 is in each and every JPEG, so when we hit APP0 we insert our new APP13! */
00252                      case M_APP1:
00253                             if (written) {
00254                                    /* don't try to write the data twice */
00255                                    break;
00256                             }
00257                             written = 1;
00258 
00259                             php_iptc_skip_variable(fp, spool, poi?&poi:0 TSRMLS_CC);
00260 
00261                             if (iptcdata_len & 1) {
00262                                    iptcdata_len++; /* make the length even */
00263                             }
00264 
00265                             psheader[ 2 ] = (iptcdata_len+28)>>8;
00266                             psheader[ 3 ] = (iptcdata_len+28)&0xff;
00267 
00268                             for (inx = 0; inx < 28; inx++) {
00269                                    php_iptc_put1(fp, spool, psheader[inx], poi?&poi:0 TSRMLS_CC);
00270                             }
00271 
00272                             php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len>>8), poi?&poi:0 TSRMLS_CC);
00273                             php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len&0xff), poi?&poi:0 TSRMLS_CC);
00274 
00275                             for (inx = 0; inx < iptcdata_len; inx++) {
00276                                    php_iptc_put1(fp, spool, iptcdata[inx], poi?&poi:0 TSRMLS_CC);
00277                             }
00278                             break;
00279 
00280                      case M_SOS:                                                    
00281                             /* we hit data, no more marker-inserting can be done! */
00282                             php_iptc_read_remaining(fp, spool, poi?&poi:0 TSRMLS_CC);
00283                             done = 1;
00284                             break;
00285 
00286                      default:
00287                             php_iptc_skip_variable(fp, spool, poi?&poi:0 TSRMLS_CC);
00288                             break;
00289               }
00290        }
00291 
00292        fclose(fp);
00293 
00294        if (spool < 2) {
00295               RETVAL_STRINGL(spoolbuf, poi - spoolbuf, 0);
00296        } else {
00297               RETURN_TRUE;
00298        }
00299 }
00300 /* }}} */
00301 
00302 /* {{{ proto array iptcparse(string iptcdata)
00303    Parse binary IPTC-data into associative array */
00304 PHP_FUNCTION(iptcparse)
00305 {
00306        unsigned int inx = 0, len, tagsfound = 0;
00307        unsigned char *buffer, recnum, dataset, key[ 16 ];
00308        char *str;
00309        int str_len;
00310        zval *values, **element;
00311 
00312        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) != SUCCESS) {
00313               return;
00314        }
00315 
00316        buffer = (unsigned char *)str;
00317 
00318        while (inx < str_len) { /* find 1st tag */
00319               if ((buffer[inx] == 0x1c) && ((buffer[inx+1] == 0x01) || (buffer[inx+1] == 0x02))){ 
00320                      break;
00321               } else {
00322                      inx++;
00323               }
00324        }
00325 
00326        while (inx < str_len) {
00327               if (buffer[ inx++ ] != 0x1c) {
00328                      break;   /* we ran against some data which does not conform to IPTC - stop parsing! */
00329               } 
00330               
00331               if ((inx + 4) >= str_len)
00332                      break;
00333 
00334               dataset = buffer[ inx++ ];
00335               recnum = buffer[ inx++ ];
00336 
00337               if (buffer[ inx ] & (unsigned char) 0x80) { /* long tag */
00338                      len = (((long) buffer[ inx + 2 ]) << 24) + (((long) buffer[ inx + 3 ]) << 16) + 
00339                               (((long) buffer[ inx + 4 ]) <<  8) + (((long) buffer[ inx + 5 ]));
00340                      inx += 6;
00341               } else { /* short tag */
00342                      len = (((unsigned short) buffer[ inx ])<<8) | (unsigned short)buffer[ inx+1 ];
00343                      inx += 2;
00344               }
00345 
00346               snprintf(key, sizeof(key), "%d#%03d", (unsigned int) dataset, (unsigned int) recnum);
00347 
00348               if ((len > str_len) || (inx + len) > str_len) {
00349                      break;
00350               }
00351 
00352               if (tagsfound == 0) { /* found the 1st tag - initialize the return array */
00353                      array_init(return_value);
00354               }
00355 
00356               if (zend_hash_find(Z_ARRVAL_P(return_value), key, strlen(key) + 1, (void **) &element) == FAILURE) {
00357                      MAKE_STD_ZVAL(values);
00358                      array_init(values);
00359                      
00360                      zend_hash_update(Z_ARRVAL_P(return_value), key, strlen(key) + 1, (void *) &values, sizeof(zval*), (void **) &element);
00361               } 
00362                      
00363               add_next_index_stringl(*element, buffer+inx, len, 1);
00364               inx += len;
00365               tagsfound++;
00366        }
00367 
00368        if (! tagsfound) {
00369               RETURN_FALSE;
00370        }
00371 }
00372 /* }}} */
00373 
00374 /*
00375  * Local variables:
00376  * tab-width: 4
00377  * c-basic-offset: 4
00378  * End:
00379  * vim600: sw=4 ts=4 fdm=marker
00380  * vim<600: sw=4 ts=4
00381  */