LTP GCOV extension - code coverage report
Current view: directory - ext/standard - iptc.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 132
Code covered: 0.0 % Executed lines: 0
Legend: not executed executed

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

Generated by: LTP GCOV extension version 1.5