iptc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. * Functions to parse & compse IPTC data.
  20. * PhotoShop >= 3.0 can read and write textual data to JPEG files.
  21. * ... more to come .....
  22. *
  23. * i know, parts of this is now duplicated in image.c
  24. * but in this case i think it's okay!
  25. */
  26. /*
  27. * TODO:
  28. * - add IPTC translation table
  29. */
  30. #include "php.h"
  31. #include "php_iptc.h"
  32. #include "ext/standard/head.h"
  33. #include <sys/stat.h>
  34. #ifdef PHP_WIN32
  35. # include "win32/php_stdint.h"
  36. #else
  37. # if HAVE_INTTYPES_H
  38. # include <inttypes.h>
  39. # elif HAVE_STDINT_H
  40. # include <stdint.h>
  41. # endif
  42. #endif
  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. /* {{{ php_iptc_put1
  77. */
  78. static int php_iptc_put1(FILE *fp, int spool, unsigned char c, unsigned char **spoolbuf)
  79. {
  80. if (spool > 0)
  81. PUTC(c);
  82. if (spoolbuf) *(*spoolbuf)++ = c;
  83. return c;
  84. }
  85. /* }}} */
  86. /* {{{ php_iptc_get1
  87. */
  88. static int php_iptc_get1(FILE *fp, int spool, unsigned char **spoolbuf)
  89. {
  90. int c;
  91. char cc;
  92. c = getc(fp);
  93. if (c == EOF) return EOF;
  94. if (spool > 0) {
  95. cc = c;
  96. PUTC(cc);
  97. }
  98. if (spoolbuf) *(*spoolbuf)++ = c;
  99. return c;
  100. }
  101. /* }}} */
  102. /* {{{ php_iptc_read_remaining
  103. */
  104. static int php_iptc_read_remaining(FILE *fp, int spool, unsigned char **spoolbuf)
  105. {
  106. while (php_iptc_get1(fp, spool, spoolbuf) != EOF) continue;
  107. return M_EOI;
  108. }
  109. /* }}} */
  110. /* {{{ php_iptc_skip_variable
  111. */
  112. static int php_iptc_skip_variable(FILE *fp, int spool, unsigned char **spoolbuf)
  113. {
  114. unsigned int length;
  115. int c1, c2;
  116. if ((c1 = php_iptc_get1(fp, spool, spoolbuf)) == EOF) return M_EOI;
  117. if ((c2 = php_iptc_get1(fp, spool, spoolbuf)) == EOF) return M_EOI;
  118. length = (((unsigned char) c1) << 8) + ((unsigned char) c2);
  119. length -= 2;
  120. while (length--)
  121. if (php_iptc_get1(fp, spool, spoolbuf) == EOF) return M_EOI;
  122. return 0;
  123. }
  124. /* }}} */
  125. /* {{{ php_iptc_next_marker
  126. */
  127. static int php_iptc_next_marker(FILE *fp, int spool, unsigned char **spoolbuf)
  128. {
  129. int c;
  130. /* skip unimportant stuff */
  131. c = php_iptc_get1(fp, spool, spoolbuf);
  132. if (c == EOF) return M_EOI;
  133. while (c != 0xff) {
  134. if ((c = php_iptc_get1(fp, spool, spoolbuf)) == EOF)
  135. return M_EOI; /* we hit EOF */
  136. }
  137. /* get marker byte, swallowing possible padding */
  138. do {
  139. c = php_iptc_get1(fp, 0, 0);
  140. if (c == EOF)
  141. return M_EOI; /* we hit EOF */
  142. else
  143. if (c == 0xff)
  144. php_iptc_put1(fp, spool, (unsigned char)c, spoolbuf);
  145. } while (c == 0xff);
  146. return (unsigned int) c;
  147. }
  148. /* }}} */
  149. static char psheader[] = "\xFF\xED\0\0Photoshop 3.0\08BIM\x04\x04\0\0\0\0";
  150. /* {{{ proto array iptcembed(string iptcdata, string jpeg_file_name [, int spool])
  151. Embed binary IPTC data into a JPEG image. */
  152. PHP_FUNCTION(iptcembed)
  153. {
  154. char *iptcdata, *jpeg_file;
  155. size_t iptcdata_len, jpeg_file_len;
  156. zend_long spool = 0;
  157. FILE *fp;
  158. unsigned int marker, done = 0;
  159. size_t inx;
  160. zend_string *spoolbuf = NULL;
  161. unsigned char *poi = NULL;
  162. zend_stat_t sb;
  163. zend_bool written = 0;
  164. ZEND_PARSE_PARAMETERS_START(2, 3)
  165. Z_PARAM_STRING(iptcdata, iptcdata_len)
  166. Z_PARAM_PATH(jpeg_file, jpeg_file_len)
  167. Z_PARAM_OPTIONAL
  168. Z_PARAM_LONG(spool)
  169. ZEND_PARSE_PARAMETERS_END();
  170. if (php_check_open_basedir(jpeg_file)) {
  171. RETURN_FALSE;
  172. }
  173. if (iptcdata_len >= SIZE_MAX - sizeof(psheader) - 1025) {
  174. php_error_docref(NULL, E_WARNING, "IPTC data too large");
  175. RETURN_FALSE;
  176. }
  177. if ((fp = VCWD_FOPEN(jpeg_file, "rb")) == 0) {
  178. php_error_docref(NULL, E_WARNING, "Unable to open %s", jpeg_file);
  179. RETURN_FALSE;
  180. }
  181. if (spool < 2) {
  182. zend_fstat(fileno(fp), &sb);
  183. spoolbuf = zend_string_safe_alloc(1, iptcdata_len + sizeof(psheader) + 1024 + 1, sb.st_size, 0);
  184. poi = (unsigned char*)ZSTR_VAL(spoolbuf);
  185. memset(poi, 0, iptcdata_len + sizeof(psheader) + sb.st_size + 1024 + 1);
  186. }
  187. if (php_iptc_get1(fp, spool, poi?&poi:0) != 0xFF) {
  188. fclose(fp);
  189. if (spoolbuf) {
  190. zend_string_efree(spoolbuf);
  191. }
  192. RETURN_FALSE;
  193. }
  194. if (php_iptc_get1(fp, spool, poi?&poi:0) != 0xD8) {
  195. fclose(fp);
  196. if (spoolbuf) {
  197. zend_string_efree(spoolbuf);
  198. }
  199. RETURN_FALSE;
  200. }
  201. while (!done) {
  202. marker = php_iptc_next_marker(fp, spool, poi?&poi:0);
  203. if (marker == M_EOI) { /* EOF */
  204. break;
  205. } else if (marker != M_APP13) {
  206. php_iptc_put1(fp, spool, (unsigned char)marker, poi?&poi:0);
  207. }
  208. switch (marker) {
  209. case M_APP13:
  210. /* we are going to write a new APP13 marker, so don't output the old one */
  211. php_iptc_skip_variable(fp, 0, 0);
  212. fgetc(fp); /* skip already copied 0xFF byte */
  213. php_iptc_read_remaining(fp, spool, poi?&poi:0);
  214. done = 1;
  215. break;
  216. case M_APP0:
  217. /* APP0 is in each and every JPEG, so when we hit APP0 we insert our new APP13! */
  218. case M_APP1:
  219. if (written) {
  220. /* don't try to write the data twice */
  221. break;
  222. }
  223. written = 1;
  224. php_iptc_skip_variable(fp, spool, poi?&poi:0);
  225. if (iptcdata_len & 1) {
  226. iptcdata_len++; /* make the length even */
  227. }
  228. psheader[ 2 ] = (char) ((iptcdata_len+28)>>8);
  229. psheader[ 3 ] = (iptcdata_len+28)&0xff;
  230. for (inx = 0; inx < 28; inx++) {
  231. php_iptc_put1(fp, spool, psheader[inx], poi?&poi:0);
  232. }
  233. php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len>>8), poi?&poi:0);
  234. php_iptc_put1(fp, spool, (unsigned char)(iptcdata_len&0xff), poi?&poi:0);
  235. for (inx = 0; inx < iptcdata_len; inx++) {
  236. php_iptc_put1(fp, spool, iptcdata[inx], poi?&poi:0);
  237. }
  238. break;
  239. case M_SOS:
  240. /* we hit data, no more marker-inserting can be done! */
  241. php_iptc_read_remaining(fp, spool, poi?&poi:0);
  242. done = 1;
  243. break;
  244. default:
  245. php_iptc_skip_variable(fp, spool, poi?&poi:0);
  246. break;
  247. }
  248. }
  249. fclose(fp);
  250. if (spool < 2) {
  251. spoolbuf = zend_string_truncate(spoolbuf, poi - (unsigned char*)ZSTR_VAL(spoolbuf), 0);
  252. RETURN_NEW_STR(spoolbuf);
  253. } else {
  254. RETURN_TRUE;
  255. }
  256. }
  257. /* }}} */
  258. /* {{{ proto array iptcparse(string iptcdata)
  259. Parse binary IPTC-data into associative array */
  260. PHP_FUNCTION(iptcparse)
  261. {
  262. size_t inx = 0, len;
  263. unsigned int tagsfound = 0;
  264. unsigned char *buffer, recnum, dataset;
  265. char *str, key[16];
  266. size_t str_len;
  267. zval values, *element;
  268. ZEND_PARSE_PARAMETERS_START(1, 1)
  269. Z_PARAM_STRING(str, str_len)
  270. ZEND_PARSE_PARAMETERS_END();
  271. buffer = (unsigned char *)str;
  272. while (inx < str_len) { /* find 1st tag */
  273. if ((buffer[inx] == 0x1c) && ((buffer[inx+1] == 0x01) || (buffer[inx+1] == 0x02))){
  274. break;
  275. } else {
  276. inx++;
  277. }
  278. }
  279. while (inx < str_len) {
  280. if (buffer[ inx++ ] != 0x1c) {
  281. break; /* we ran against some data which does not conform to IPTC - stop parsing! */
  282. }
  283. if ((inx + 4) >= str_len)
  284. break;
  285. dataset = buffer[ inx++ ];
  286. recnum = buffer[ inx++ ];
  287. if (buffer[ inx ] & (unsigned char) 0x80) { /* long tag */
  288. if((inx+6) >= str_len) {
  289. break;
  290. }
  291. len = (((zend_long) buffer[ inx + 2 ]) << 24) + (((zend_long) buffer[ inx + 3 ]) << 16) +
  292. (((zend_long) buffer[ inx + 4 ]) << 8) + (((zend_long) buffer[ inx + 5 ]));
  293. inx += 6;
  294. } else { /* short tag */
  295. len = (((unsigned short) buffer[ inx ])<<8) | (unsigned short)buffer[ inx+1 ];
  296. inx += 2;
  297. }
  298. if ((len > str_len) || (inx + len) > str_len) {
  299. break;
  300. }
  301. snprintf(key, sizeof(key), "%d#%03d", (unsigned int) dataset, (unsigned int) recnum);
  302. if (tagsfound == 0) { /* found the 1st tag - initialize the return array */
  303. array_init(return_value);
  304. }
  305. if ((element = zend_hash_str_find(Z_ARRVAL_P(return_value), key, strlen(key))) == NULL) {
  306. array_init(&values);
  307. element = zend_hash_str_update(Z_ARRVAL_P(return_value), key, strlen(key), &values);
  308. }
  309. add_next_index_stringl(element, (char *) buffer+inx, len);
  310. inx += len;
  311. tagsfound++;
  312. }
  313. if (! tagsfound) {
  314. RETURN_FALSE;
  315. }
  316. }
  317. /* }}} */
  318. /*
  319. * Local variables:
  320. * tab-width: 4
  321. * c-basic-offset: 4
  322. * End:
  323. * vim600: sw=4 ts=4 fdm=marker
  324. * vim<600: sw=4 ts=4
  325. */