image.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include <stdio.h>
  21. #if HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #include "fopen_wrappers.h"
  25. #include "ext/standard/fsock.h"
  26. #if HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #include "php_image.h"
  30. #ifdef PHP_WIN32
  31. #include "win32/php_stdint.h"
  32. #endif
  33. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  34. #include "zlib.h"
  35. #endif
  36. /* file type markers */
  37. PHPAPI const char php_sig_gif[3] = {'G', 'I', 'F'};
  38. PHPAPI const char php_sig_psd[4] = {'8', 'B', 'P', 'S'};
  39. PHPAPI const char php_sig_bmp[2] = {'B', 'M'};
  40. PHPAPI const char php_sig_swf[3] = {'F', 'W', 'S'};
  41. PHPAPI const char php_sig_swc[3] = {'C', 'W', 'S'};
  42. PHPAPI const char php_sig_jpg[3] = {(char) 0xff, (char) 0xd8, (char) 0xff};
  43. PHPAPI const char php_sig_png[8] = {(char) 0x89, (char) 0x50, (char) 0x4e, (char) 0x47,
  44. (char) 0x0d, (char) 0x0a, (char) 0x1a, (char) 0x0a};
  45. PHPAPI const char php_sig_tif_ii[4] = {'I','I', (char)0x2A, (char)0x00};
  46. PHPAPI const char php_sig_tif_mm[4] = {'M','M', (char)0x00, (char)0x2A};
  47. PHPAPI const char php_sig_jpc[3] = {(char)0xff, (char)0x4f, (char)0xff};
  48. PHPAPI const char php_sig_jp2[12] = {(char)0x00, (char)0x00, (char)0x00, (char)0x0c,
  49. (char)0x6a, (char)0x50, (char)0x20, (char)0x20,
  50. (char)0x0d, (char)0x0a, (char)0x87, (char)0x0a};
  51. PHPAPI const char php_sig_iff[4] = {'F','O','R','M'};
  52. PHPAPI const char php_sig_ico[4] = {(char)0x00, (char)0x00, (char)0x01, (char)0x00};
  53. PHPAPI const char php_sig_riff[4] = {'R', 'I', 'F', 'F'};
  54. PHPAPI const char php_sig_webp[4] = {'W', 'E', 'B', 'P'};
  55. /* REMEMBER TO ADD MIME-TYPE TO FUNCTION php_image_type_to_mime_type */
  56. /* PCX must check first 64bytes and byte 0=0x0a and byte2 < 0x06 */
  57. /* return info as a struct, to make expansion easier */
  58. struct gfxinfo {
  59. unsigned int width;
  60. unsigned int height;
  61. unsigned int bits;
  62. unsigned int channels;
  63. };
  64. /* {{{ PHP_MINIT_FUNCTION(imagetypes)
  65. * Register IMAGETYPE_<xxx> constants used by GetImageSize(), image_type_to_mime_type, ext/exif */
  66. PHP_MINIT_FUNCTION(imagetypes)
  67. {
  68. REGISTER_LONG_CONSTANT("IMAGETYPE_GIF", IMAGE_FILETYPE_GIF, CONST_CS | CONST_PERSISTENT);
  69. REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG", IMAGE_FILETYPE_JPEG, CONST_CS | CONST_PERSISTENT);
  70. REGISTER_LONG_CONSTANT("IMAGETYPE_PNG", IMAGE_FILETYPE_PNG, CONST_CS | CONST_PERSISTENT);
  71. REGISTER_LONG_CONSTANT("IMAGETYPE_SWF", IMAGE_FILETYPE_SWF, CONST_CS | CONST_PERSISTENT);
  72. REGISTER_LONG_CONSTANT("IMAGETYPE_PSD", IMAGE_FILETYPE_PSD, CONST_CS | CONST_PERSISTENT);
  73. REGISTER_LONG_CONSTANT("IMAGETYPE_BMP", IMAGE_FILETYPE_BMP, CONST_CS | CONST_PERSISTENT);
  74. REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_II", IMAGE_FILETYPE_TIFF_II, CONST_CS | CONST_PERSISTENT);
  75. REGISTER_LONG_CONSTANT("IMAGETYPE_TIFF_MM", IMAGE_FILETYPE_TIFF_MM, CONST_CS | CONST_PERSISTENT);
  76. REGISTER_LONG_CONSTANT("IMAGETYPE_JPC", IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT);
  77. REGISTER_LONG_CONSTANT("IMAGETYPE_JP2", IMAGE_FILETYPE_JP2, CONST_CS | CONST_PERSISTENT);
  78. REGISTER_LONG_CONSTANT("IMAGETYPE_JPX", IMAGE_FILETYPE_JPX, CONST_CS | CONST_PERSISTENT);
  79. REGISTER_LONG_CONSTANT("IMAGETYPE_JB2", IMAGE_FILETYPE_JB2, CONST_CS | CONST_PERSISTENT);
  80. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  81. REGISTER_LONG_CONSTANT("IMAGETYPE_SWC", IMAGE_FILETYPE_SWC, CONST_CS | CONST_PERSISTENT);
  82. #endif
  83. REGISTER_LONG_CONSTANT("IMAGETYPE_IFF", IMAGE_FILETYPE_IFF, CONST_CS | CONST_PERSISTENT);
  84. REGISTER_LONG_CONSTANT("IMAGETYPE_WBMP", IMAGE_FILETYPE_WBMP, CONST_CS | CONST_PERSISTENT);
  85. REGISTER_LONG_CONSTANT("IMAGETYPE_JPEG2000",IMAGE_FILETYPE_JPC, CONST_CS | CONST_PERSISTENT); /* keep alias */
  86. REGISTER_LONG_CONSTANT("IMAGETYPE_XBM", IMAGE_FILETYPE_XBM, CONST_CS | CONST_PERSISTENT);
  87. REGISTER_LONG_CONSTANT("IMAGETYPE_ICO", IMAGE_FILETYPE_ICO, CONST_CS | CONST_PERSISTENT);
  88. REGISTER_LONG_CONSTANT("IMAGETYPE_WEBP", IMAGE_FILETYPE_WEBP, CONST_CS | CONST_PERSISTENT);
  89. REGISTER_LONG_CONSTANT("IMAGETYPE_UNKNOWN", IMAGE_FILETYPE_UNKNOWN, CONST_CS | CONST_PERSISTENT);
  90. REGISTER_LONG_CONSTANT("IMAGETYPE_COUNT", IMAGE_FILETYPE_COUNT, CONST_CS | CONST_PERSISTENT);
  91. return SUCCESS;
  92. }
  93. /* }}} */
  94. /* {{{ php_handle_gif
  95. * routine to handle GIF files. If only everything were that easy... ;} */
  96. static struct gfxinfo *php_handle_gif (php_stream * stream)
  97. {
  98. struct gfxinfo *result = NULL;
  99. unsigned char dim[5];
  100. if (php_stream_seek(stream, 3, SEEK_CUR))
  101. return NULL;
  102. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  103. return NULL;
  104. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  105. result->width = (unsigned int)dim[0] | (((unsigned int)dim[1])<<8);
  106. result->height = (unsigned int)dim[2] | (((unsigned int)dim[3])<<8);
  107. result->bits = dim[4]&0x80 ? ((((unsigned int)dim[4])&0x07) + 1) : 0;
  108. result->channels = 3; /* always */
  109. return result;
  110. }
  111. /* }}} */
  112. /* {{{ php_handle_psd
  113. */
  114. static struct gfxinfo *php_handle_psd (php_stream * stream)
  115. {
  116. struct gfxinfo *result = NULL;
  117. unsigned char dim[8];
  118. if (php_stream_seek(stream, 11, SEEK_CUR))
  119. return NULL;
  120. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  121. return NULL;
  122. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  123. result->height = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
  124. result->width = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
  125. return result;
  126. }
  127. /* }}} */
  128. /* {{{ php_handle_bmp
  129. */
  130. static struct gfxinfo *php_handle_bmp (php_stream * stream)
  131. {
  132. struct gfxinfo *result = NULL;
  133. unsigned char dim[16];
  134. int size;
  135. if (php_stream_seek(stream, 11, SEEK_CUR))
  136. return NULL;
  137. if (php_stream_read(stream, (char*)dim, sizeof(dim)) != sizeof(dim))
  138. return NULL;
  139. size = (((unsigned int)dim[ 3]) << 24) + (((unsigned int)dim[ 2]) << 16) + (((unsigned int)dim[ 1]) << 8) + ((unsigned int) dim[ 0]);
  140. if (size == 12) {
  141. result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
  142. result->width = (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
  143. result->height = (((unsigned int)dim[ 7]) << 8) + ((unsigned int) dim[ 6]);
  144. result->bits = ((unsigned int)dim[11]);
  145. } else if (size > 12 && (size <= 64 || size == 108 || size == 124)) {
  146. result = (struct gfxinfo *) ecalloc (1, sizeof(struct gfxinfo));
  147. result->width = (((unsigned int)dim[ 7]) << 24) + (((unsigned int)dim[ 6]) << 16) + (((unsigned int)dim[ 5]) << 8) + ((unsigned int) dim[ 4]);
  148. result->height = (((unsigned int)dim[11]) << 24) + (((unsigned int)dim[10]) << 16) + (((unsigned int)dim[ 9]) << 8) + ((unsigned int) dim[ 8]);
  149. result->height = abs((int32_t)result->height);
  150. result->bits = (((unsigned int)dim[15]) << 8) + ((unsigned int)dim[14]);
  151. } else {
  152. return NULL;
  153. }
  154. return result;
  155. }
  156. /* }}} */
  157. /* {{{ php_swf_get_bits
  158. * routines to handle SWF files. */
  159. static unsigned long int php_swf_get_bits (unsigned char* buffer, unsigned int pos, unsigned int count)
  160. {
  161. unsigned int loop;
  162. unsigned long int result = 0;
  163. for (loop = pos; loop < pos + count; loop++)
  164. {
  165. result = result +
  166. ((((buffer[loop / 8]) >> (7 - (loop % 8))) & 0x01) << (count - (loop - pos) - 1));
  167. }
  168. return result;
  169. }
  170. /* }}} */
  171. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  172. /* {{{ php_handle_swc
  173. */
  174. static struct gfxinfo *php_handle_swc(php_stream * stream)
  175. {
  176. struct gfxinfo *result = NULL;
  177. long bits;
  178. unsigned char a[64];
  179. unsigned long len=64, szlength;
  180. int factor = 1,maxfactor = 16;
  181. int status = 0;
  182. unsigned char *b, *buf = NULL;
  183. zend_string *bufz;
  184. if (php_stream_seek(stream, 5, SEEK_CUR)) {
  185. return NULL;
  186. }
  187. if (php_stream_read(stream, (char *) a, sizeof(a)) != sizeof(a)) {
  188. return NULL;
  189. }
  190. b = ecalloc(1, len + 1);
  191. if (uncompress(b, &len, a, sizeof(a)) != Z_OK) {
  192. /* failed to decompress the file, will try reading the rest of the file */
  193. if (php_stream_seek(stream, 8, SEEK_SET)) {
  194. efree(b);
  195. return NULL;
  196. }
  197. bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  198. if (!bufz) {
  199. efree(b);
  200. return NULL;
  201. }
  202. /*
  203. * zlib::uncompress() wants to know the output data length
  204. * if none was given as a parameter
  205. * we try from input length * 2 up to input length * 2^8
  206. * doubling it whenever it wasn't big enough
  207. * that should be eneugh for all real life cases
  208. */
  209. do {
  210. szlength = ZSTR_LEN(bufz) * (1<<factor++);
  211. buf = erealloc(buf, szlength);
  212. status = uncompress(buf, &szlength, (unsigned char *) ZSTR_VAL(bufz), ZSTR_LEN(bufz));
  213. } while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
  214. if (bufz) {
  215. zend_string_release_ex(bufz, 0);
  216. }
  217. if (status == Z_OK) {
  218. memcpy(b, buf, len);
  219. }
  220. if (buf) {
  221. efree(buf);
  222. }
  223. }
  224. if (!status) {
  225. result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
  226. bits = php_swf_get_bits (b, 0, 5);
  227. result->width = (php_swf_get_bits (b, 5 + bits, bits) -
  228. php_swf_get_bits (b, 5, bits)) / 20;
  229. result->height = (php_swf_get_bits (b, 5 + (3 * bits), bits) -
  230. php_swf_get_bits (b, 5 + (2 * bits), bits)) / 20;
  231. } else {
  232. result = NULL;
  233. }
  234. efree (b);
  235. return result;
  236. }
  237. /* }}} */
  238. #endif
  239. /* {{{ php_handle_swf
  240. */
  241. static struct gfxinfo *php_handle_swf (php_stream * stream)
  242. {
  243. struct gfxinfo *result = NULL;
  244. long bits;
  245. unsigned char a[32];
  246. if (php_stream_seek(stream, 5, SEEK_CUR))
  247. return NULL;
  248. if (php_stream_read(stream, (char*)a, sizeof(a)) != sizeof(a))
  249. return NULL;
  250. result = (struct gfxinfo *) ecalloc (1, sizeof (struct gfxinfo));
  251. bits = php_swf_get_bits (a, 0, 5);
  252. result->width = (php_swf_get_bits (a, 5 + bits, bits) -
  253. php_swf_get_bits (a, 5, bits)) / 20;
  254. result->height = (php_swf_get_bits (a, 5 + (3 * bits), bits) -
  255. php_swf_get_bits (a, 5 + (2 * bits), bits)) / 20;
  256. result->bits = 0;
  257. result->channels = 0;
  258. return result;
  259. }
  260. /* }}} */
  261. /* {{{ php_handle_png
  262. * routine to handle PNG files */
  263. static struct gfxinfo *php_handle_png (php_stream * stream)
  264. {
  265. struct gfxinfo *result = NULL;
  266. unsigned char dim[9];
  267. /* Width: 4 bytes
  268. * Height: 4 bytes
  269. * Bit depth: 1 byte
  270. * Color type: 1 byte
  271. * Compression method: 1 byte
  272. * Filter method: 1 byte
  273. * Interlace method: 1 byte
  274. */
  275. if (php_stream_seek(stream, 8, SEEK_CUR))
  276. return NULL;
  277. if((php_stream_read(stream, (char*)dim, sizeof(dim))) < sizeof(dim))
  278. return NULL;
  279. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  280. result->width = (((unsigned int)dim[0]) << 24) + (((unsigned int)dim[1]) << 16) + (((unsigned int)dim[2]) << 8) + ((unsigned int)dim[3]);
  281. result->height = (((unsigned int)dim[4]) << 24) + (((unsigned int)dim[5]) << 16) + (((unsigned int)dim[6]) << 8) + ((unsigned int)dim[7]);
  282. result->bits = (unsigned int)dim[8];
  283. return result;
  284. }
  285. /* }}} */
  286. /* routines to handle JPEG data */
  287. /* some defines for the different JPEG block types */
  288. #define M_SOF0 0xC0 /* Start Of Frame N */
  289. #define M_SOF1 0xC1 /* N indicates which compression process */
  290. #define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
  291. #define M_SOF3 0xC3
  292. #define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
  293. #define M_SOF6 0xC6
  294. #define M_SOF7 0xC7
  295. #define M_SOF9 0xC9
  296. #define M_SOF10 0xCA
  297. #define M_SOF11 0xCB
  298. #define M_SOF13 0xCD
  299. #define M_SOF14 0xCE
  300. #define M_SOF15 0xCF
  301. #define M_SOI 0xD8
  302. #define M_EOI 0xD9 /* End Of Image (end of datastream) */
  303. #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
  304. #define M_APP0 0xe0
  305. #define M_APP1 0xe1
  306. #define M_APP2 0xe2
  307. #define M_APP3 0xe3
  308. #define M_APP4 0xe4
  309. #define M_APP5 0xe5
  310. #define M_APP6 0xe6
  311. #define M_APP7 0xe7
  312. #define M_APP8 0xe8
  313. #define M_APP9 0xe9
  314. #define M_APP10 0xea
  315. #define M_APP11 0xeb
  316. #define M_APP12 0xec
  317. #define M_APP13 0xed
  318. #define M_APP14 0xee
  319. #define M_APP15 0xef
  320. #define M_COM 0xFE /* COMment */
  321. #define M_PSEUDO 0xFFD8 /* pseudo marker for start of image(byte 0) */
  322. /* {{{ php_read2
  323. */
  324. static unsigned short php_read2(php_stream * stream)
  325. {
  326. unsigned char a[2];
  327. /* return 0 if we couldn't read enough data */
  328. if((php_stream_read(stream, (char *) a, sizeof(a))) < sizeof(a)) return 0;
  329. return (((unsigned short)a[0]) << 8) + ((unsigned short)a[1]);
  330. }
  331. /* }}} */
  332. /* {{{ php_next_marker
  333. * get next marker byte from file */
  334. static unsigned int php_next_marker(php_stream * stream, int last_marker, int ff_read)
  335. {
  336. int a=0, marker;
  337. /* get marker byte, swallowing possible padding */
  338. if (!ff_read) {
  339. size_t extraneous = 0;
  340. while ((marker = php_stream_getc(stream)) != 0xff) {
  341. if (marker == EOF) {
  342. return M_EOI;/* we hit EOF */
  343. }
  344. extraneous++;
  345. }
  346. if (extraneous) {
  347. php_error_docref(NULL, E_WARNING, "corrupt JPEG data: %zu extraneous bytes before marker", extraneous);
  348. }
  349. }
  350. a = 1;
  351. do {
  352. if ((marker = php_stream_getc(stream)) == EOF)
  353. {
  354. return M_EOI;/* we hit EOF */
  355. }
  356. a++;
  357. } while (marker == 0xff);
  358. if (a < 2)
  359. {
  360. return M_EOI; /* at least one 0xff is needed before marker code */
  361. }
  362. return (unsigned int)marker;
  363. }
  364. /* }}} */
  365. /* {{{ php_skip_variable
  366. * skip over a variable-length block; assumes proper length marker */
  367. static int php_skip_variable(php_stream * stream)
  368. {
  369. zend_off_t length = ((unsigned int)php_read2(stream));
  370. if (length < 2) {
  371. return 0;
  372. }
  373. length = length - 2;
  374. php_stream_seek(stream, (zend_long)length, SEEK_CUR);
  375. return 1;
  376. }
  377. /* }}} */
  378. /* {{{ php_read_APP
  379. */
  380. static int php_read_APP(php_stream * stream, unsigned int marker, zval *info)
  381. {
  382. unsigned short length;
  383. char *buffer;
  384. char markername[16];
  385. zval *tmp;
  386. length = php_read2(stream);
  387. if (length < 2) {
  388. return 0;
  389. }
  390. length -= 2; /* length includes itself */
  391. buffer = emalloc(length);
  392. if (php_stream_read(stream, buffer, (zend_long) length) != length) {
  393. efree(buffer);
  394. return 0;
  395. }
  396. snprintf(markername, sizeof(markername), "APP%d", marker - M_APP0);
  397. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(info), markername, strlen(markername))) == NULL) {
  398. /* XXX we only catch the 1st tag of it's kind! */
  399. add_assoc_stringl(info, markername, buffer, length);
  400. }
  401. efree(buffer);
  402. return 1;
  403. }
  404. /* }}} */
  405. /* {{{ php_handle_jpeg
  406. main loop to parse JPEG structure */
  407. static struct gfxinfo *php_handle_jpeg (php_stream * stream, zval *info)
  408. {
  409. struct gfxinfo *result = NULL;
  410. unsigned int marker = M_PSEUDO;
  411. unsigned short length, ff_read=1;
  412. for (;;) {
  413. marker = php_next_marker(stream, marker, ff_read);
  414. ff_read = 0;
  415. switch (marker) {
  416. case M_SOF0:
  417. case M_SOF1:
  418. case M_SOF2:
  419. case M_SOF3:
  420. case M_SOF5:
  421. case M_SOF6:
  422. case M_SOF7:
  423. case M_SOF9:
  424. case M_SOF10:
  425. case M_SOF11:
  426. case M_SOF13:
  427. case M_SOF14:
  428. case M_SOF15:
  429. if (result == NULL) {
  430. /* handle SOFn block */
  431. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  432. length = php_read2(stream);
  433. result->bits = php_stream_getc(stream);
  434. result->height = php_read2(stream);
  435. result->width = php_read2(stream);
  436. result->channels = php_stream_getc(stream);
  437. if (!info || length < 8) { /* if we don't want an extanded info -> return */
  438. return result;
  439. }
  440. if (php_stream_seek(stream, length - 8, SEEK_CUR)) { /* file error after info */
  441. return result;
  442. }
  443. } else {
  444. if (!php_skip_variable(stream)) {
  445. return result;
  446. }
  447. }
  448. break;
  449. case M_APP0:
  450. case M_APP1:
  451. case M_APP2:
  452. case M_APP3:
  453. case M_APP4:
  454. case M_APP5:
  455. case M_APP6:
  456. case M_APP7:
  457. case M_APP8:
  458. case M_APP9:
  459. case M_APP10:
  460. case M_APP11:
  461. case M_APP12:
  462. case M_APP13:
  463. case M_APP14:
  464. case M_APP15:
  465. if (info) {
  466. if (!php_read_APP(stream, marker, info)) { /* read all the app marks... */
  467. return result;
  468. }
  469. } else {
  470. if (!php_skip_variable(stream)) {
  471. return result;
  472. }
  473. }
  474. break;
  475. case M_SOS:
  476. case M_EOI:
  477. return result; /* we're about to hit image data, or are at EOF. stop processing. */
  478. default:
  479. if (!php_skip_variable(stream)) { /* anything else isn't interesting */
  480. return result;
  481. }
  482. break;
  483. }
  484. }
  485. return result; /* perhaps image broken -> no info but size */
  486. }
  487. /* }}} */
  488. /* {{{ php_read4
  489. */
  490. static unsigned int php_read4(php_stream * stream)
  491. {
  492. unsigned char a[4];
  493. /* just return 0 if we hit the end-of-file */
  494. if ((php_stream_read(stream, (char*)a, sizeof(a))) != sizeof(a)) return 0;
  495. return (((unsigned int)a[0]) << 24)
  496. + (((unsigned int)a[1]) << 16)
  497. + (((unsigned int)a[2]) << 8)
  498. + (((unsigned int)a[3]));
  499. }
  500. /* }}} */
  501. /* {{{ JPEG 2000 Marker Codes */
  502. #define JPEG2000_MARKER_PREFIX 0xFF /* All marker codes start with this */
  503. #define JPEG2000_MARKER_SOC 0x4F /* Start of Codestream */
  504. #define JPEG2000_MARKER_SOT 0x90 /* Start of Tile part */
  505. #define JPEG2000_MARKER_SOD 0x93 /* Start of Data */
  506. #define JPEG2000_MARKER_EOC 0xD9 /* End of Codestream */
  507. #define JPEG2000_MARKER_SIZ 0x51 /* Image and tile size */
  508. #define JPEG2000_MARKER_COD 0x52 /* Coding style default */
  509. #define JPEG2000_MARKER_COC 0x53 /* Coding style component */
  510. #define JPEG2000_MARKER_RGN 0x5E /* Region of interest */
  511. #define JPEG2000_MARKER_QCD 0x5C /* Quantization default */
  512. #define JPEG2000_MARKER_QCC 0x5D /* Quantization component */
  513. #define JPEG2000_MARKER_POC 0x5F /* Progression order change */
  514. #define JPEG2000_MARKER_TLM 0x55 /* Tile-part lengths */
  515. #define JPEG2000_MARKER_PLM 0x57 /* Packet length, main header */
  516. #define JPEG2000_MARKER_PLT 0x58 /* Packet length, tile-part header */
  517. #define JPEG2000_MARKER_PPM 0x60 /* Packed packet headers, main header */
  518. #define JPEG2000_MARKER_PPT 0x61 /* Packed packet headers, tile part header */
  519. #define JPEG2000_MARKER_SOP 0x91 /* Start of packet */
  520. #define JPEG2000_MARKER_EPH 0x92 /* End of packet header */
  521. #define JPEG2000_MARKER_CRG 0x63 /* Component registration */
  522. #define JPEG2000_MARKER_COM 0x64 /* Comment */
  523. /* }}} */
  524. /* {{{ php_handle_jpc
  525. Main loop to parse JPEG2000 raw codestream structure */
  526. static struct gfxinfo *php_handle_jpc(php_stream * stream)
  527. {
  528. struct gfxinfo *result = NULL;
  529. int highest_bit_depth, bit_depth;
  530. unsigned char first_marker_id;
  531. unsigned int i;
  532. /* JPEG 2000 components can be vastly different from one another.
  533. Each component can be sampled at a different resolution, use
  534. a different colour space, have a separate colour depth, and
  535. be compressed totally differently! This makes giving a single
  536. "bit depth" answer somewhat problematic. For this implementation
  537. we'll use the highest depth encountered. */
  538. /* Get the single byte that remains after the file type indentification */
  539. first_marker_id = php_stream_getc(stream);
  540. /* Ensure that this marker is SIZ (as is mandated by the standard) */
  541. if (first_marker_id != JPEG2000_MARKER_SIZ) {
  542. php_error_docref(NULL, E_WARNING, "JPEG2000 codestream corrupt(Expected SIZ marker not found after SOC)");
  543. return NULL;
  544. }
  545. result = (struct gfxinfo *)ecalloc(1, sizeof(struct gfxinfo));
  546. php_read2(stream); /* Lsiz */
  547. php_read2(stream); /* Rsiz */
  548. result->width = php_read4(stream); /* Xsiz */
  549. result->height = php_read4(stream); /* Ysiz */
  550. #if MBO_0
  551. php_read4(stream); /* XOsiz */
  552. php_read4(stream); /* YOsiz */
  553. php_read4(stream); /* XTsiz */
  554. php_read4(stream); /* YTsiz */
  555. php_read4(stream); /* XTOsiz */
  556. php_read4(stream); /* YTOsiz */
  557. #else
  558. if (php_stream_seek(stream, 24, SEEK_CUR)) {
  559. efree(result);
  560. return NULL;
  561. }
  562. #endif
  563. result->channels = php_read2(stream); /* Csiz */
  564. if ((result->channels == 0 && php_stream_eof(stream)) || result->channels > 256) {
  565. efree(result);
  566. return NULL;
  567. }
  568. /* Collect bit depth info */
  569. highest_bit_depth = 0;
  570. for (i = 0; i < result->channels; i++) {
  571. bit_depth = php_stream_getc(stream); /* Ssiz[i] */
  572. bit_depth++;
  573. if (bit_depth > highest_bit_depth) {
  574. highest_bit_depth = bit_depth;
  575. }
  576. php_stream_getc(stream); /* XRsiz[i] */
  577. php_stream_getc(stream); /* YRsiz[i] */
  578. }
  579. result->bits = highest_bit_depth;
  580. return result;
  581. }
  582. /* }}} */
  583. /* {{{ php_handle_jp2
  584. main loop to parse JPEG 2000 JP2 wrapper format structure */
  585. static struct gfxinfo *php_handle_jp2(php_stream *stream)
  586. {
  587. struct gfxinfo *result = NULL;
  588. unsigned int box_length;
  589. unsigned int box_type;
  590. char jp2c_box_id[] = {(char)0x6a, (char)0x70, (char)0x32, (char)0x63};
  591. /* JP2 is a wrapper format for JPEG 2000. Data is contained within "boxes".
  592. Boxes themselves can be contained within "super-boxes". Super-Boxes can
  593. contain super-boxes which provides us with a hierarchical storage system.
  594. It is valid for a JP2 file to contain multiple individual codestreams.
  595. We'll just look for the first codestream at the root of the box structure
  596. and handle that.
  597. */
  598. for (;;)
  599. {
  600. box_length = php_read4(stream); /* LBox */
  601. /* TBox */
  602. if (php_stream_read(stream, (void *)&box_type, sizeof(box_type)) != sizeof(box_type)) {
  603. /* Use this as a general "out of stream" error */
  604. break;
  605. }
  606. if (box_length == 1) {
  607. /* We won't handle XLBoxes */
  608. return NULL;
  609. }
  610. if (!memcmp(&box_type, jp2c_box_id, 4))
  611. {
  612. /* Skip the first 3 bytes to emulate the file type examination */
  613. php_stream_seek(stream, 3, SEEK_CUR);
  614. result = php_handle_jpc(stream);
  615. break;
  616. }
  617. /* Stop if this was the last box */
  618. if ((int)box_length <= 0) {
  619. break;
  620. }
  621. /* Skip over LBox (Which includes both TBox and LBox itself */
  622. if (php_stream_seek(stream, box_length - 8, SEEK_CUR)) {
  623. break;
  624. }
  625. }
  626. if (result == NULL) {
  627. php_error_docref(NULL, E_WARNING, "JP2 file has no codestreams at root level");
  628. }
  629. return result;
  630. }
  631. /* }}} */
  632. /* {{{ tiff constants
  633. */
  634. PHPAPI const int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};
  635. /* uncompressed only */
  636. #define TAG_IMAGEWIDTH 0x0100
  637. #define TAG_IMAGEHEIGHT 0x0101
  638. /* compressed images only */
  639. #define TAG_COMP_IMAGEWIDTH 0xA002
  640. #define TAG_COMP_IMAGEHEIGHT 0xA003
  641. #define TAG_FMT_BYTE 1
  642. #define TAG_FMT_STRING 2
  643. #define TAG_FMT_USHORT 3
  644. #define TAG_FMT_ULONG 4
  645. #define TAG_FMT_URATIONAL 5
  646. #define TAG_FMT_SBYTE 6
  647. #define TAG_FMT_UNDEFINED 7
  648. #define TAG_FMT_SSHORT 8
  649. #define TAG_FMT_SLONG 9
  650. #define TAG_FMT_SRATIONAL 10
  651. #define TAG_FMT_SINGLE 11
  652. #define TAG_FMT_DOUBLE 12
  653. /* }}} */
  654. /* {{{ php_ifd_get16u
  655. * Convert a 16 bit unsigned value from file's native byte order */
  656. static int php_ifd_get16u(void *Short, int motorola_intel)
  657. {
  658. if (motorola_intel) {
  659. return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
  660. } else {
  661. return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
  662. }
  663. }
  664. /* }}} */
  665. /* {{{ php_ifd_get16s
  666. * Convert a 16 bit signed value from file's native byte order */
  667. static signed short php_ifd_get16s(void *Short, int motorola_intel)
  668. {
  669. return (signed short)php_ifd_get16u(Short, motorola_intel);
  670. }
  671. /* }}} */
  672. /* {{{ php_ifd_get32s
  673. * Convert a 32 bit signed value from file's native byte order */
  674. static int php_ifd_get32s(void *Long, int motorola_intel)
  675. {
  676. if (motorola_intel) {
  677. return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16)
  678. | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
  679. } else {
  680. return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16)
  681. | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
  682. }
  683. }
  684. /* }}} */
  685. /* {{{ php_ifd_get32u
  686. * Convert a 32 bit unsigned value from file's native byte order */
  687. static unsigned php_ifd_get32u(void *Long, int motorola_intel)
  688. {
  689. return (unsigned)php_ifd_get32s(Long, motorola_intel) & 0xffffffff;
  690. }
  691. /* }}} */
  692. /* {{{ php_handle_tiff
  693. main loop to parse TIFF structure */
  694. static struct gfxinfo *php_handle_tiff (php_stream * stream, zval *info, int motorola_intel)
  695. {
  696. struct gfxinfo *result = NULL;
  697. int i, num_entries;
  698. unsigned char *dir_entry;
  699. size_t ifd_size, dir_size, entry_value, width=0, height=0, ifd_addr;
  700. int entry_tag , entry_type;
  701. char *ifd_data, ifd_ptr[4];
  702. if (php_stream_read(stream, ifd_ptr, 4) != 4)
  703. return NULL;
  704. ifd_addr = php_ifd_get32u(ifd_ptr, motorola_intel);
  705. if (php_stream_seek(stream, ifd_addr-8, SEEK_CUR))
  706. return NULL;
  707. ifd_size = 2;
  708. ifd_data = emalloc(ifd_size);
  709. if (php_stream_read(stream, ifd_data, 2) != 2) {
  710. efree(ifd_data);
  711. return NULL;
  712. }
  713. num_entries = php_ifd_get16u(ifd_data, motorola_intel);
  714. dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
  715. ifd_size = dir_size;
  716. ifd_data = erealloc(ifd_data,ifd_size);
  717. if (php_stream_read(stream, ifd_data+2, dir_size-2) != dir_size-2) {
  718. efree(ifd_data);
  719. return NULL;
  720. }
  721. /* now we have the directory we can look how long it should be */
  722. ifd_size = dir_size;
  723. for(i=0;i<num_entries;i++) {
  724. dir_entry = (unsigned char *) ifd_data+2+i*12;
  725. entry_tag = php_ifd_get16u(dir_entry+0, motorola_intel);
  726. entry_type = php_ifd_get16u(dir_entry+2, motorola_intel);
  727. switch(entry_type) {
  728. case TAG_FMT_BYTE:
  729. case TAG_FMT_SBYTE:
  730. entry_value = (size_t)(dir_entry[8]);
  731. break;
  732. case TAG_FMT_USHORT:
  733. entry_value = php_ifd_get16u(dir_entry+8, motorola_intel);
  734. break;
  735. case TAG_FMT_SSHORT:
  736. entry_value = php_ifd_get16s(dir_entry+8, motorola_intel);
  737. break;
  738. case TAG_FMT_ULONG:
  739. entry_value = php_ifd_get32u(dir_entry+8, motorola_intel);
  740. break;
  741. case TAG_FMT_SLONG:
  742. entry_value = php_ifd_get32s(dir_entry+8, motorola_intel);
  743. break;
  744. default:
  745. continue;
  746. }
  747. switch(entry_tag) {
  748. case TAG_IMAGEWIDTH:
  749. case TAG_COMP_IMAGEWIDTH:
  750. width = entry_value;
  751. break;
  752. case TAG_IMAGEHEIGHT:
  753. case TAG_COMP_IMAGEHEIGHT:
  754. height = entry_value;
  755. break;
  756. }
  757. }
  758. efree(ifd_data);
  759. if ( width && height) {
  760. /* not the same when in for-loop */
  761. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  762. result->height = height;
  763. result->width = width;
  764. result->bits = 0;
  765. result->channels = 0;
  766. return result;
  767. }
  768. return NULL;
  769. }
  770. /* }}} */
  771. /* {{{ php_handle_psd
  772. */
  773. static struct gfxinfo *php_handle_iff(php_stream * stream)
  774. {
  775. struct gfxinfo * result;
  776. unsigned char a[10];
  777. int chunkId;
  778. int size;
  779. short width, height, bits;
  780. if (php_stream_read(stream, (char *) a, 8) != 8) {
  781. return NULL;
  782. }
  783. if (strncmp((char *) a+4, "ILBM", 4) && strncmp((char *) a+4, "PBM ", 4)) {
  784. return NULL;
  785. }
  786. /* loop chunks to find BMHD chunk */
  787. do {
  788. if (php_stream_read(stream, (char*)a, 8) != 8) {
  789. return NULL;
  790. }
  791. chunkId = php_ifd_get32s(a+0, 1);
  792. size = php_ifd_get32s(a+4, 1);
  793. if (size < 0) {
  794. return NULL;
  795. }
  796. if ((size & 1) == 1) {
  797. size++;
  798. }
  799. if (chunkId == 0x424d4844) { /* BMHD chunk */
  800. if (size < 9 || php_stream_read(stream, (char*)a, 9) != 9) {
  801. return NULL;
  802. }
  803. width = php_ifd_get16s(a+0, 1);
  804. height = php_ifd_get16s(a+2, 1);
  805. bits = a[8] & 0xff;
  806. if (width > 0 && height > 0 && bits > 0 && bits < 33) {
  807. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  808. result->width = width;
  809. result->height = height;
  810. result->bits = bits;
  811. result->channels = 0;
  812. return result;
  813. }
  814. } else {
  815. if (php_stream_seek(stream, size, SEEK_CUR)) {
  816. return NULL;
  817. }
  818. }
  819. } while (1);
  820. }
  821. /* }}} */
  822. /* {{{ php_get_wbmp
  823. * int WBMP file format type
  824. * byte Header Type
  825. * byte Extended Header
  826. * byte Header Data (type 00 = multibyte)
  827. * byte Header Data (type 11 = name/pairs)
  828. * int Number of columns
  829. * int Number of rows
  830. */
  831. static int php_get_wbmp(php_stream *stream, struct gfxinfo **result, int check)
  832. {
  833. int i, width = 0, height = 0;
  834. if (php_stream_rewind(stream)) {
  835. return 0;
  836. }
  837. /* get type */
  838. if (php_stream_getc(stream) != 0) {
  839. return 0;
  840. }
  841. /* skip header */
  842. do {
  843. i = php_stream_getc(stream);
  844. if (i < 0) {
  845. return 0;
  846. }
  847. } while (i & 0x80);
  848. /* get width */
  849. do {
  850. i = php_stream_getc(stream);
  851. if (i < 0) {
  852. return 0;
  853. }
  854. width = (width << 7) | (i & 0x7f);
  855. /* maximum valid width for wbmp (although 127 may be a more accurate one) */
  856. if (width > 2048) {
  857. return 0;
  858. }
  859. } while (i & 0x80);
  860. /* get height */
  861. do {
  862. i = php_stream_getc(stream);
  863. if (i < 0) {
  864. return 0;
  865. }
  866. height = (height << 7) | (i & 0x7f);
  867. /* maximum valid heigth for wbmp (although 127 may be a more accurate one) */
  868. if (height > 2048) {
  869. return 0;
  870. }
  871. } while (i & 0x80);
  872. if (!height || !width) {
  873. return 0;
  874. }
  875. if (!check) {
  876. (*result)->width = width;
  877. (*result)->height = height;
  878. }
  879. return IMAGE_FILETYPE_WBMP;
  880. }
  881. /* }}} */
  882. /* {{{ php_handle_wbmp
  883. */
  884. static struct gfxinfo *php_handle_wbmp(php_stream * stream)
  885. {
  886. struct gfxinfo *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  887. if (!php_get_wbmp(stream, &result, 0)) {
  888. efree(result);
  889. return NULL;
  890. }
  891. return result;
  892. }
  893. /* }}} */
  894. /* {{{ php_get_xbm
  895. */
  896. static int php_get_xbm(php_stream *stream, struct gfxinfo **result)
  897. {
  898. char *fline;
  899. char *iname;
  900. char *type;
  901. int value;
  902. unsigned int width = 0, height = 0;
  903. if (result) {
  904. *result = NULL;
  905. }
  906. if (php_stream_rewind(stream)) {
  907. return 0;
  908. }
  909. while ((fline=php_stream_gets(stream, NULL, 0)) != NULL) {
  910. iname = estrdup(fline); /* simple way to get necessary buffer of required size */
  911. if (sscanf(fline, "#define %s %d", iname, &value) == 2) {
  912. if (!(type = strrchr(iname, '_'))) {
  913. type = iname;
  914. } else {
  915. type++;
  916. }
  917. if (!strcmp("width", type)) {
  918. width = (unsigned int) value;
  919. if (height) {
  920. efree(iname);
  921. break;
  922. }
  923. }
  924. if (!strcmp("height", type)) {
  925. height = (unsigned int) value;
  926. if (width) {
  927. efree(iname);
  928. break;
  929. }
  930. }
  931. }
  932. efree(fline);
  933. efree(iname);
  934. }
  935. if (fline) {
  936. efree(fline);
  937. }
  938. if (width && height) {
  939. if (result) {
  940. *result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  941. (*result)->width = width;
  942. (*result)->height = height;
  943. }
  944. return IMAGE_FILETYPE_XBM;
  945. }
  946. return 0;
  947. }
  948. /* }}} */
  949. /* {{{ php_handle_xbm
  950. */
  951. static struct gfxinfo *php_handle_xbm(php_stream * stream)
  952. {
  953. struct gfxinfo *result;
  954. php_get_xbm(stream, &result);
  955. return result;
  956. }
  957. /* }}} */
  958. /* {{{ php_handle_ico
  959. */
  960. static struct gfxinfo *php_handle_ico(php_stream * stream)
  961. {
  962. struct gfxinfo *result = NULL;
  963. unsigned char dim[16];
  964. int num_icons = 0;
  965. if (php_stream_read(stream, (char *) dim, 2) != 2)
  966. return NULL;
  967. num_icons = (((unsigned int)dim[1]) << 8) + ((unsigned int) dim[0]);
  968. if (num_icons < 1 || num_icons > 255)
  969. return NULL;
  970. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  971. while (num_icons > 0)
  972. {
  973. if (php_stream_read(stream, (char *) dim, sizeof(dim)) != sizeof(dim))
  974. break;
  975. if ((((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]) >= result->bits)
  976. {
  977. result->width = (unsigned int)dim[0];
  978. result->height = (unsigned int)dim[1];
  979. result->bits = (((unsigned int)dim[7]) << 8) + ((unsigned int)dim[6]);
  980. }
  981. num_icons--;
  982. }
  983. return result;
  984. }
  985. /* }}} */
  986. /* {{{ php_handle_webp
  987. */
  988. static struct gfxinfo *php_handle_webp(php_stream * stream)
  989. {
  990. struct gfxinfo *result = NULL;
  991. const char sig[3] = {'V', 'P', '8'};
  992. unsigned char buf[18];
  993. char format;
  994. if (php_stream_read(stream, (char *) buf, 18) != 18)
  995. return NULL;
  996. if (memcmp(buf, sig, 3)) {
  997. return NULL;
  998. }
  999. switch (buf[3]) {
  1000. case ' ':
  1001. case 'L':
  1002. case 'X':
  1003. format = buf[3];
  1004. break;
  1005. default:
  1006. return NULL;
  1007. }
  1008. result = (struct gfxinfo *) ecalloc(1, sizeof(struct gfxinfo));
  1009. switch (format) {
  1010. case ' ':
  1011. result->width = buf[14] + ((buf[15] & 0x3F) << 8);
  1012. result->height = buf[16] + ((buf[17] & 0x3F) << 8);
  1013. break;
  1014. case 'L':
  1015. result->width = buf[9] + ((buf[10] & 0x3F) << 8) + 1;
  1016. result->height = (buf[10] >> 6) + (buf[11] << 2) + ((buf[12] & 0xF) << 10) + 1;
  1017. break;
  1018. case 'X':
  1019. result->width = buf[12] + (buf[13] << 8) + (buf[14] << 16) + 1;
  1020. result->height = buf[15] + (buf[16] << 8) + (buf[17] << 16) + 1;
  1021. break;
  1022. }
  1023. result->bits = 8; /* always 1 byte */
  1024. return result;
  1025. }
  1026. /* }}} */
  1027. /* {{{ php_image_type_to_mime_type
  1028. * Convert internal image_type to mime type */
  1029. PHPAPI char * php_image_type_to_mime_type(int image_type)
  1030. {
  1031. switch( image_type) {
  1032. case IMAGE_FILETYPE_GIF:
  1033. return "image/gif";
  1034. case IMAGE_FILETYPE_JPEG:
  1035. return "image/jpeg";
  1036. case IMAGE_FILETYPE_PNG:
  1037. return "image/png";
  1038. case IMAGE_FILETYPE_SWF:
  1039. case IMAGE_FILETYPE_SWC:
  1040. return "application/x-shockwave-flash";
  1041. case IMAGE_FILETYPE_PSD:
  1042. return "image/psd";
  1043. case IMAGE_FILETYPE_BMP:
  1044. return "image/bmp";
  1045. case IMAGE_FILETYPE_TIFF_II:
  1046. case IMAGE_FILETYPE_TIFF_MM:
  1047. return "image/tiff";
  1048. case IMAGE_FILETYPE_IFF:
  1049. return "image/iff";
  1050. case IMAGE_FILETYPE_WBMP:
  1051. return "image/vnd.wap.wbmp";
  1052. case IMAGE_FILETYPE_JPC:
  1053. return "application/octet-stream";
  1054. case IMAGE_FILETYPE_JP2:
  1055. return "image/jp2";
  1056. case IMAGE_FILETYPE_XBM:
  1057. return "image/xbm";
  1058. case IMAGE_FILETYPE_ICO:
  1059. return "image/vnd.microsoft.icon";
  1060. case IMAGE_FILETYPE_WEBP:
  1061. return "image/webp";
  1062. default:
  1063. case IMAGE_FILETYPE_UNKNOWN:
  1064. return "application/octet-stream"; /* suppose binary format */
  1065. }
  1066. }
  1067. /* }}} */
  1068. /* {{{ proto string image_type_to_mime_type(int imagetype)
  1069. Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
  1070. PHP_FUNCTION(image_type_to_mime_type)
  1071. {
  1072. zend_long p_image_type;
  1073. ZEND_PARSE_PARAMETERS_START(1, 1)
  1074. Z_PARAM_LONG(p_image_type)
  1075. ZEND_PARSE_PARAMETERS_END();
  1076. ZVAL_STRING(return_value, (char*)php_image_type_to_mime_type(p_image_type));
  1077. }
  1078. /* }}} */
  1079. /* {{{ proto string image_type_to_extension(int imagetype [, bool include_dot])
  1080. Get file extension for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype */
  1081. PHP_FUNCTION(image_type_to_extension)
  1082. {
  1083. zend_long image_type;
  1084. zend_bool inc_dot=1;
  1085. const char *imgext = NULL;
  1086. ZEND_PARSE_PARAMETERS_START(1, 2)
  1087. Z_PARAM_LONG(image_type)
  1088. Z_PARAM_OPTIONAL
  1089. Z_PARAM_BOOL(inc_dot)
  1090. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  1091. switch (image_type) {
  1092. case IMAGE_FILETYPE_GIF:
  1093. imgext = ".gif";
  1094. break;
  1095. case IMAGE_FILETYPE_JPEG:
  1096. imgext = ".jpeg";
  1097. break;
  1098. case IMAGE_FILETYPE_PNG:
  1099. imgext = ".png";
  1100. break;
  1101. case IMAGE_FILETYPE_SWF:
  1102. case IMAGE_FILETYPE_SWC:
  1103. imgext = ".swf";
  1104. break;
  1105. case IMAGE_FILETYPE_PSD:
  1106. imgext = ".psd";
  1107. break;
  1108. case IMAGE_FILETYPE_BMP:
  1109. case IMAGE_FILETYPE_WBMP:
  1110. imgext = ".bmp";
  1111. break;
  1112. case IMAGE_FILETYPE_TIFF_II:
  1113. case IMAGE_FILETYPE_TIFF_MM:
  1114. imgext = ".tiff";
  1115. break;
  1116. case IMAGE_FILETYPE_IFF:
  1117. imgext = ".iff";
  1118. break;
  1119. case IMAGE_FILETYPE_JPC:
  1120. imgext = ".jpc";
  1121. break;
  1122. case IMAGE_FILETYPE_JP2:
  1123. imgext = ".jp2";
  1124. break;
  1125. case IMAGE_FILETYPE_JPX:
  1126. imgext = ".jpx";
  1127. break;
  1128. case IMAGE_FILETYPE_JB2:
  1129. imgext = ".jb2";
  1130. break;
  1131. case IMAGE_FILETYPE_XBM:
  1132. imgext = ".xbm";
  1133. break;
  1134. case IMAGE_FILETYPE_ICO:
  1135. imgext = ".ico";
  1136. break;
  1137. case IMAGE_FILETYPE_WEBP:
  1138. imgext = ".webp";
  1139. break;
  1140. }
  1141. if (imgext) {
  1142. RETURN_STRING(&imgext[!inc_dot]);
  1143. }
  1144. RETURN_FALSE;
  1145. }
  1146. /* }}} */
  1147. /* {{{ php_imagetype
  1148. detect filetype from first bytes */
  1149. PHPAPI int php_getimagetype(php_stream * stream, char *filetype)
  1150. {
  1151. char tmp[12];
  1152. int twelve_bytes_read;
  1153. if ( !filetype) filetype = tmp;
  1154. if((php_stream_read(stream, filetype, 3)) != 3) {
  1155. php_error_docref(NULL, E_NOTICE, "Read error!");
  1156. return IMAGE_FILETYPE_UNKNOWN;
  1157. }
  1158. /* BYTES READ: 3 */
  1159. if (!memcmp(filetype, php_sig_gif, 3)) {
  1160. return IMAGE_FILETYPE_GIF;
  1161. } else if (!memcmp(filetype, php_sig_jpg, 3)) {
  1162. return IMAGE_FILETYPE_JPEG;
  1163. } else if (!memcmp(filetype, php_sig_png, 3)) {
  1164. if (php_stream_read(stream, filetype+3, 5) != 5) {
  1165. php_error_docref(NULL, E_NOTICE, "Read error!");
  1166. return IMAGE_FILETYPE_UNKNOWN;
  1167. }
  1168. if (!memcmp(filetype, php_sig_png, 8)) {
  1169. return IMAGE_FILETYPE_PNG;
  1170. } else {
  1171. php_error_docref(NULL, E_WARNING, "PNG file corrupted by ASCII conversion");
  1172. return IMAGE_FILETYPE_UNKNOWN;
  1173. }
  1174. } else if (!memcmp(filetype, php_sig_swf, 3)) {
  1175. return IMAGE_FILETYPE_SWF;
  1176. } else if (!memcmp(filetype, php_sig_swc, 3)) {
  1177. return IMAGE_FILETYPE_SWC;
  1178. } else if (!memcmp(filetype, php_sig_psd, 3)) {
  1179. return IMAGE_FILETYPE_PSD;
  1180. } else if (!memcmp(filetype, php_sig_bmp, 2)) {
  1181. return IMAGE_FILETYPE_BMP;
  1182. } else if (!memcmp(filetype, php_sig_jpc, 3)) {
  1183. return IMAGE_FILETYPE_JPC;
  1184. } else if (!memcmp(filetype, php_sig_riff, 3)) {
  1185. if (php_stream_read(stream, filetype+3, 9) != 9) {
  1186. php_error_docref(NULL, E_NOTICE, "Read error!");
  1187. return IMAGE_FILETYPE_UNKNOWN;
  1188. }
  1189. if (!memcmp(filetype+8, php_sig_webp, 4)) {
  1190. return IMAGE_FILETYPE_WEBP;
  1191. } else {
  1192. return IMAGE_FILETYPE_UNKNOWN;
  1193. }
  1194. }
  1195. if (php_stream_read(stream, filetype+3, 1) != 1) {
  1196. php_error_docref(NULL, E_NOTICE, "Read error!");
  1197. return IMAGE_FILETYPE_UNKNOWN;
  1198. }
  1199. /* BYTES READ: 4 */
  1200. if (!memcmp(filetype, php_sig_tif_ii, 4)) {
  1201. return IMAGE_FILETYPE_TIFF_II;
  1202. } else if (!memcmp(filetype, php_sig_tif_mm, 4)) {
  1203. return IMAGE_FILETYPE_TIFF_MM;
  1204. } else if (!memcmp(filetype, php_sig_iff, 4)) {
  1205. return IMAGE_FILETYPE_IFF;
  1206. } else if (!memcmp(filetype, php_sig_ico, 4)) {
  1207. return IMAGE_FILETYPE_ICO;
  1208. }
  1209. /* WBMP may be smaller than 12 bytes, so delay error */
  1210. twelve_bytes_read = (php_stream_read(stream, filetype+4, 8) == 8);
  1211. /* BYTES READ: 12 */
  1212. if (twelve_bytes_read && !memcmp(filetype, php_sig_jp2, 12)) {
  1213. return IMAGE_FILETYPE_JP2;
  1214. }
  1215. /* AFTER ALL ABOVE FAILED */
  1216. if (php_get_wbmp(stream, NULL, 1)) {
  1217. return IMAGE_FILETYPE_WBMP;
  1218. }
  1219. if (!twelve_bytes_read) {
  1220. php_error_docref(NULL, E_NOTICE, "Read error!");
  1221. return IMAGE_FILETYPE_UNKNOWN;
  1222. }
  1223. if (php_get_xbm(stream, NULL)) {
  1224. return IMAGE_FILETYPE_XBM;
  1225. }
  1226. return IMAGE_FILETYPE_UNKNOWN;
  1227. }
  1228. /* }}} */
  1229. static void php_getimagesize_from_stream(php_stream *stream, zval *info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  1230. {
  1231. int itype = 0;
  1232. struct gfxinfo *result = NULL;
  1233. if (!stream) {
  1234. RETURN_FALSE;
  1235. }
  1236. itype = php_getimagetype(stream, NULL);
  1237. switch( itype) {
  1238. case IMAGE_FILETYPE_GIF:
  1239. result = php_handle_gif(stream);
  1240. break;
  1241. case IMAGE_FILETYPE_JPEG:
  1242. if (info) {
  1243. result = php_handle_jpeg(stream, info);
  1244. } else {
  1245. result = php_handle_jpeg(stream, NULL);
  1246. }
  1247. break;
  1248. case IMAGE_FILETYPE_PNG:
  1249. result = php_handle_png(stream);
  1250. break;
  1251. case IMAGE_FILETYPE_SWF:
  1252. result = php_handle_swf(stream);
  1253. break;
  1254. case IMAGE_FILETYPE_SWC:
  1255. #if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
  1256. result = php_handle_swc(stream);
  1257. #else
  1258. php_error_docref(NULL, E_NOTICE, "The image is a compressed SWF file, but you do not have a static version of the zlib extension enabled");
  1259. #endif
  1260. break;
  1261. case IMAGE_FILETYPE_PSD:
  1262. result = php_handle_psd(stream);
  1263. break;
  1264. case IMAGE_FILETYPE_BMP:
  1265. result = php_handle_bmp(stream);
  1266. break;
  1267. case IMAGE_FILETYPE_TIFF_II:
  1268. result = php_handle_tiff(stream, NULL, 0);
  1269. break;
  1270. case IMAGE_FILETYPE_TIFF_MM:
  1271. result = php_handle_tiff(stream, NULL, 1);
  1272. break;
  1273. case IMAGE_FILETYPE_JPC:
  1274. result = php_handle_jpc(stream);
  1275. break;
  1276. case IMAGE_FILETYPE_JP2:
  1277. result = php_handle_jp2(stream);
  1278. break;
  1279. case IMAGE_FILETYPE_IFF:
  1280. result = php_handle_iff(stream);
  1281. break;
  1282. case IMAGE_FILETYPE_WBMP:
  1283. result = php_handle_wbmp(stream);
  1284. break;
  1285. case IMAGE_FILETYPE_XBM:
  1286. result = php_handle_xbm(stream);
  1287. break;
  1288. case IMAGE_FILETYPE_ICO:
  1289. result = php_handle_ico(stream);
  1290. break;
  1291. case IMAGE_FILETYPE_WEBP:
  1292. result = php_handle_webp(stream);
  1293. break;
  1294. default:
  1295. case IMAGE_FILETYPE_UNKNOWN:
  1296. break;
  1297. }
  1298. if (result) {
  1299. char temp[MAX_LENGTH_OF_LONG * 2 + sizeof("width=\"\" height=\"\"")];
  1300. array_init(return_value);
  1301. add_index_long(return_value, 0, result->width);
  1302. add_index_long(return_value, 1, result->height);
  1303. add_index_long(return_value, 2, itype);
  1304. snprintf(temp, sizeof(temp), "width=\"%d\" height=\"%d\"", result->width, result->height);
  1305. add_index_string(return_value, 3, temp);
  1306. if (result->bits != 0) {
  1307. add_assoc_long(return_value, "bits", result->bits);
  1308. }
  1309. if (result->channels != 0) {
  1310. add_assoc_long(return_value, "channels", result->channels);
  1311. }
  1312. add_assoc_string(return_value, "mime", (char*)php_image_type_to_mime_type(itype));
  1313. efree(result);
  1314. } else {
  1315. RETURN_FALSE;
  1316. }
  1317. }
  1318. /* }}} */
  1319. #define FROM_DATA 0
  1320. #define FROM_PATH 1
  1321. static void php_getimagesize_from_any(INTERNAL_FUNCTION_PARAMETERS, int mode) { /* {{{ */
  1322. zval *info = NULL;
  1323. php_stream *stream = NULL;
  1324. char *input;
  1325. size_t input_len;
  1326. const int argc = ZEND_NUM_ARGS();
  1327. ZEND_PARSE_PARAMETERS_START(1, 2)
  1328. Z_PARAM_STRING(input, input_len)
  1329. Z_PARAM_OPTIONAL
  1330. Z_PARAM_ZVAL_DEREF(info)
  1331. ZEND_PARSE_PARAMETERS_END();
  1332. if (mode == FROM_PATH && CHECK_NULL_PATH(input, input_len)) {
  1333. php_error_docref(NULL, E_WARNING, "Invalid path");
  1334. return;
  1335. }
  1336. if (argc == 2) {
  1337. zval_ptr_dtor(info);
  1338. array_init(info);
  1339. }
  1340. if (mode == FROM_PATH) {
  1341. stream = php_stream_open_wrapper(input, "rb", STREAM_MUST_SEEK|REPORT_ERRORS|IGNORE_PATH, NULL);
  1342. } else {
  1343. stream = php_stream_memory_open(TEMP_STREAM_READONLY, input, input_len);
  1344. }
  1345. if (!stream) {
  1346. RETURN_FALSE;
  1347. }
  1348. php_getimagesize_from_stream(stream, info, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1349. php_stream_close(stream);
  1350. }
  1351. /* }}} */
  1352. /* {{{ proto array getimagesize(string imagefile [, array info])
  1353. Get the size of an image as 4-element array */
  1354. PHP_FUNCTION(getimagesize)
  1355. {
  1356. php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_PATH);
  1357. }
  1358. /* }}} */
  1359. /* {{{ proto array getimagesizefromstring(string data [, array info])
  1360. Get the size of an image as 4-element array */
  1361. PHP_FUNCTION(getimagesizefromstring)
  1362. {
  1363. php_getimagesize_from_any(INTERNAL_FUNCTION_PARAM_PASSTHRU, FROM_DATA);
  1364. }
  1365. /* }}} */
  1366. /*
  1367. * Local variables:
  1368. * tab-width: 4
  1369. * c-basic-offset: 4
  1370. * End:
  1371. * vim600: sw=4 ts=4 fdm=marker
  1372. * vim<600: sw=4 ts=4
  1373. */