PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - iptc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 99 138 71.7 %
Date: 2016-06-25 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 25 Jun 2016 07:09:06 +0000 (44 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.