image.c 45 KB

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