exif.c 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. /* ToDos
  21. *
  22. * See if example images from http://www.exif.org have illegal
  23. * thumbnail sizes or if code is corrupt.
  24. * Create/Update exif headers.
  25. * Create/Remove/Update image thumbnails.
  26. */
  27. /* Security
  28. *
  29. * At current time i do not see any security problems but a potential
  30. * attacker could generate an image with recursive ifd pointers...(Marcus)
  31. */
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include "php.h"
  36. #include "ext/standard/file.h"
  37. #if HAVE_EXIF
  38. /* When EXIF_DEBUG is defined the module generates a lot of debug messages
  39. * that help understanding what is going on. This can and should be used
  40. * while extending the module as it shows if you are at the right position.
  41. * You are always considered to have a copy of TIFF6.0 and EXIF2.10 standard.
  42. */
  43. #undef EXIF_DEBUG
  44. #ifdef EXIF_DEBUG
  45. #define EXIFERR_DC , const char *_file, size_t _line TSRMLS_DC
  46. #define EXIFERR_CC , __FILE__, __LINE__ TSRMLS_CC
  47. #else
  48. #define EXIFERR_DC TSRMLS_DC
  49. #define EXIFERR_CC TSRMLS_CC
  50. #endif
  51. #undef EXIF_JPEG2000
  52. #include "php_exif.h"
  53. #include <math.h>
  54. #include "php_ini.h"
  55. #include "ext/standard/php_string.h"
  56. #include "ext/standard/php_image.h"
  57. #include "ext/standard/info.h"
  58. /* needed for ssize_t definition */
  59. #include <sys/types.h>
  60. typedef unsigned char uchar;
  61. #ifndef safe_emalloc
  62. # define safe_emalloc(a,b,c) emalloc((a)*(b)+(c))
  63. #endif
  64. #ifndef safe_erealloc
  65. # define safe_erealloc(p,a,b,c) erealloc(p, (a)*(b)+(c))
  66. #endif
  67. #ifndef TRUE
  68. # define TRUE 1
  69. # define FALSE 0
  70. #endif
  71. #ifndef max
  72. # define max(a,b) ((a)>(b) ? (a) : (b))
  73. #endif
  74. #define EFREE_IF(ptr) if (ptr) efree(ptr)
  75. #define MAX_IFD_NESTING_LEVEL 100
  76. /* {{{ arginfo */
  77. ZEND_BEGIN_ARG_INFO(arginfo_exif_tagname, 0)
  78. ZEND_ARG_INFO(0, index)
  79. ZEND_END_ARG_INFO()
  80. ZEND_BEGIN_ARG_INFO_EX(arginfo_exif_read_data, 0, 0, 1)
  81. ZEND_ARG_INFO(0, filename)
  82. ZEND_ARG_INFO(0, sections_needed)
  83. ZEND_ARG_INFO(0, sub_arrays)
  84. ZEND_ARG_INFO(0, read_thumbnail)
  85. ZEND_END_ARG_INFO()
  86. ZEND_BEGIN_ARG_INFO_EX(arginfo_exif_thumbnail, 0, 0, 1)
  87. ZEND_ARG_INFO(0, filename)
  88. ZEND_ARG_INFO(1, width)
  89. ZEND_ARG_INFO(1, height)
  90. ZEND_ARG_INFO(1, imagetype)
  91. ZEND_END_ARG_INFO()
  92. ZEND_BEGIN_ARG_INFO(arginfo_exif_imagetype, 0)
  93. ZEND_ARG_INFO(0, imagefile)
  94. ZEND_END_ARG_INFO()
  95. /* }}} */
  96. /* {{{ exif_functions[]
  97. */
  98. const zend_function_entry exif_functions[] = {
  99. PHP_FE(exif_read_data, arginfo_exif_read_data)
  100. PHP_FALIAS(read_exif_data, exif_read_data, arginfo_exif_read_data)
  101. PHP_FE(exif_tagname, arginfo_exif_tagname)
  102. PHP_FE(exif_thumbnail, arginfo_exif_thumbnail)
  103. PHP_FE(exif_imagetype, arginfo_exif_imagetype)
  104. PHP_FE_END
  105. };
  106. /* }}} */
  107. #define EXIF_VERSION "1.4 $Id$"
  108. /* {{{ PHP_MINFO_FUNCTION
  109. */
  110. PHP_MINFO_FUNCTION(exif)
  111. {
  112. php_info_print_table_start();
  113. php_info_print_table_row(2, "EXIF Support", "enabled");
  114. php_info_print_table_row(2, "EXIF Version", EXIF_VERSION);
  115. php_info_print_table_row(2, "Supported EXIF Version", "0220");
  116. php_info_print_table_row(2, "Supported filetypes", "JPEG,TIFF");
  117. php_info_print_table_end();
  118. DISPLAY_INI_ENTRIES();
  119. }
  120. /* }}} */
  121. ZEND_BEGIN_MODULE_GLOBALS(exif)
  122. char * encode_unicode;
  123. char * decode_unicode_be;
  124. char * decode_unicode_le;
  125. char * encode_jis;
  126. char * decode_jis_be;
  127. char * decode_jis_le;
  128. ZEND_END_MODULE_GLOBALS(exif)
  129. ZEND_DECLARE_MODULE_GLOBALS(exif)
  130. #ifdef ZTS
  131. #define EXIF_G(v) TSRMG(exif_globals_id, zend_exif_globals *, v)
  132. #else
  133. #define EXIF_G(v) (exif_globals.v)
  134. #endif
  135. /* {{{ PHP_INI
  136. */
  137. ZEND_INI_MH(OnUpdateEncode)
  138. {
  139. if (new_value && new_value_length) {
  140. const zend_encoding **return_list;
  141. size_t return_size;
  142. if (FAILURE == zend_multibyte_parse_encoding_list(new_value, new_value_length,
  143. &return_list, &return_size, 0 TSRMLS_CC)) {
  144. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
  145. return FAILURE;
  146. }
  147. efree(return_list);
  148. }
  149. return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  150. }
  151. ZEND_INI_MH(OnUpdateDecode)
  152. {
  153. if (new_value) {
  154. const zend_encoding **return_list;
  155. size_t return_size;
  156. if (FAILURE == zend_multibyte_parse_encoding_list(new_value, new_value_length,
  157. &return_list, &return_size, 0 TSRMLS_CC)) {
  158. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
  159. return FAILURE;
  160. }
  161. efree(return_list);
  162. }
  163. return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  164. }
  165. PHP_INI_BEGIN()
  166. STD_PHP_INI_ENTRY("exif.encode_unicode", "ISO-8859-15", PHP_INI_ALL, OnUpdateEncode, encode_unicode, zend_exif_globals, exif_globals)
  167. STD_PHP_INI_ENTRY("exif.decode_unicode_motorola", "UCS-2BE", PHP_INI_ALL, OnUpdateDecode, decode_unicode_be, zend_exif_globals, exif_globals)
  168. STD_PHP_INI_ENTRY("exif.decode_unicode_intel", "UCS-2LE", PHP_INI_ALL, OnUpdateDecode, decode_unicode_le, zend_exif_globals, exif_globals)
  169. STD_PHP_INI_ENTRY("exif.encode_jis", "", PHP_INI_ALL, OnUpdateEncode, encode_jis, zend_exif_globals, exif_globals)
  170. STD_PHP_INI_ENTRY("exif.decode_jis_motorola", "JIS", PHP_INI_ALL, OnUpdateDecode, decode_jis_be, zend_exif_globals, exif_globals)
  171. STD_PHP_INI_ENTRY("exif.decode_jis_intel", "JIS", PHP_INI_ALL, OnUpdateDecode, decode_jis_le, zend_exif_globals, exif_globals)
  172. PHP_INI_END()
  173. /* }}} */
  174. /* {{{ PHP_GINIT_FUNCTION
  175. */
  176. static PHP_GINIT_FUNCTION(exif)
  177. {
  178. exif_globals->encode_unicode = NULL;
  179. exif_globals->decode_unicode_be = NULL;
  180. exif_globals->decode_unicode_le = NULL;
  181. exif_globals->encode_jis = NULL;
  182. exif_globals->decode_jis_be = NULL;
  183. exif_globals->decode_jis_le = NULL;
  184. }
  185. /* }}} */
  186. /* {{{ PHP_MINIT_FUNCTION(exif)
  187. Get the size of an image as 4-element array */
  188. PHP_MINIT_FUNCTION(exif)
  189. {
  190. REGISTER_INI_ENTRIES();
  191. if (zend_hash_exists(&module_registry, "mbstring", sizeof("mbstring"))) {
  192. REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 1, CONST_CS | CONST_PERSISTENT);
  193. } else {
  194. REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 0, CONST_CS | CONST_PERSISTENT);
  195. }
  196. return SUCCESS;
  197. }
  198. /* }}} */
  199. /* {{{ PHP_MSHUTDOWN_FUNCTION
  200. */
  201. PHP_MSHUTDOWN_FUNCTION(exif)
  202. {
  203. UNREGISTER_INI_ENTRIES();
  204. return SUCCESS;
  205. }
  206. /* }}} */
  207. /* {{{ exif dependencies */
  208. static const zend_module_dep exif_module_deps[] = {
  209. ZEND_MOD_REQUIRED("standard")
  210. ZEND_MOD_OPTIONAL("mbstring")
  211. ZEND_MOD_END
  212. };
  213. /* }}} */
  214. /* {{{ exif_module_entry
  215. */
  216. zend_module_entry exif_module_entry = {
  217. STANDARD_MODULE_HEADER_EX, NULL,
  218. exif_module_deps,
  219. "exif",
  220. exif_functions,
  221. PHP_MINIT(exif),
  222. PHP_MSHUTDOWN(exif),
  223. NULL, NULL,
  224. PHP_MINFO(exif),
  225. #if ZEND_MODULE_API_NO >= 20010901
  226. EXIF_VERSION,
  227. #endif
  228. #if ZEND_MODULE_API_NO >= 20060613
  229. PHP_MODULE_GLOBALS(exif),
  230. PHP_GINIT(exif),
  231. NULL,
  232. NULL,
  233. STANDARD_MODULE_PROPERTIES_EX
  234. #else
  235. STANDARD_MODULE_PROPERTIES
  236. #endif
  237. };
  238. /* }}} */
  239. #ifdef COMPILE_DL_EXIF
  240. ZEND_GET_MODULE(exif)
  241. #endif
  242. /* {{{ php_strnlen
  243. * get length of string if buffer if less than buffer size or buffer size */
  244. static size_t php_strnlen(char* str, size_t maxlen) {
  245. size_t len = 0;
  246. if (str && maxlen && *str) {
  247. do {
  248. len++;
  249. } while (--maxlen && *(++str));
  250. }
  251. return len;
  252. }
  253. /* }}} */
  254. /* {{{ error messages
  255. */
  256. static const char * EXIF_ERROR_FILEEOF = "Unexpected end of file reached";
  257. static const char * EXIF_ERROR_CORRUPT = "File structure corrupted";
  258. static const char * EXIF_ERROR_THUMBEOF = "Thumbnail goes IFD boundary or end of file reached";
  259. static const char * EXIF_ERROR_FSREALLOC = "Illegal reallocating of undefined file section";
  260. #define EXIF_ERRLOG_FILEEOF(ImageInfo) exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FILEEOF);
  261. #define EXIF_ERRLOG_CORRUPT(ImageInfo) exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_CORRUPT);
  262. #define EXIF_ERRLOG_THUMBEOF(ImageInfo) exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_THUMBEOF);
  263. #define EXIF_ERRLOG_FSREALLOC(ImageInfo) exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s", EXIF_ERROR_FSREALLOC);
  264. /* }}} */
  265. /* {{{ format description defines
  266. Describes format descriptor
  267. */
  268. static int php_tiff_bytes_per_format[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 1};
  269. #define NUM_FORMATS 13
  270. #define TAG_FMT_BYTE 1
  271. #define TAG_FMT_STRING 2
  272. #define TAG_FMT_USHORT 3
  273. #define TAG_FMT_ULONG 4
  274. #define TAG_FMT_URATIONAL 5
  275. #define TAG_FMT_SBYTE 6
  276. #define TAG_FMT_UNDEFINED 7
  277. #define TAG_FMT_SSHORT 8
  278. #define TAG_FMT_SLONG 9
  279. #define TAG_FMT_SRATIONAL 10
  280. #define TAG_FMT_SINGLE 11
  281. #define TAG_FMT_DOUBLE 12
  282. #define TAG_FMT_IFD 13
  283. #ifdef EXIF_DEBUG
  284. static char *exif_get_tagformat(int format)
  285. {
  286. switch(format) {
  287. case TAG_FMT_BYTE: return "BYTE";
  288. case TAG_FMT_STRING: return "STRING";
  289. case TAG_FMT_USHORT: return "USHORT";
  290. case TAG_FMT_ULONG: return "ULONG";
  291. case TAG_FMT_URATIONAL: return "URATIONAL";
  292. case TAG_FMT_SBYTE: return "SBYTE";
  293. case TAG_FMT_UNDEFINED: return "UNDEFINED";
  294. case TAG_FMT_SSHORT: return "SSHORT";
  295. case TAG_FMT_SLONG: return "SLONG";
  296. case TAG_FMT_SRATIONAL: return "SRATIONAL";
  297. case TAG_FMT_SINGLE: return "SINGLE";
  298. case TAG_FMT_DOUBLE: return "DOUBLE";
  299. case TAG_FMT_IFD: return "IFD";
  300. }
  301. return "*Illegal";
  302. }
  303. #endif
  304. /* Describes tag values */
  305. #define TAG_GPS_VERSION_ID 0x0000
  306. #define TAG_GPS_LATITUDE_REF 0x0001
  307. #define TAG_GPS_LATITUDE 0x0002
  308. #define TAG_GPS_LONGITUDE_REF 0x0003
  309. #define TAG_GPS_LONGITUDE 0x0004
  310. #define TAG_GPS_ALTITUDE_REF 0x0005
  311. #define TAG_GPS_ALTITUDE 0x0006
  312. #define TAG_GPS_TIME_STAMP 0x0007
  313. #define TAG_GPS_SATELLITES 0x0008
  314. #define TAG_GPS_STATUS 0x0009
  315. #define TAG_GPS_MEASURE_MODE 0x000A
  316. #define TAG_GPS_DOP 0x000B
  317. #define TAG_GPS_SPEED_REF 0x000C
  318. #define TAG_GPS_SPEED 0x000D
  319. #define TAG_GPS_TRACK_REF 0x000E
  320. #define TAG_GPS_TRACK 0x000F
  321. #define TAG_GPS_IMG_DIRECTION_REF 0x0010
  322. #define TAG_GPS_IMG_DIRECTION 0x0011
  323. #define TAG_GPS_MAP_DATUM 0x0012
  324. #define TAG_GPS_DEST_LATITUDE_REF 0x0013
  325. #define TAG_GPS_DEST_LATITUDE 0x0014
  326. #define TAG_GPS_DEST_LONGITUDE_REF 0x0015
  327. #define TAG_GPS_DEST_LONGITUDE 0x0016
  328. #define TAG_GPS_DEST_BEARING_REF 0x0017
  329. #define TAG_GPS_DEST_BEARING 0x0018
  330. #define TAG_GPS_DEST_DISTANCE_REF 0x0019
  331. #define TAG_GPS_DEST_DISTANCE 0x001A
  332. #define TAG_GPS_PROCESSING_METHOD 0x001B
  333. #define TAG_GPS_AREA_INFORMATION 0x001C
  334. #define TAG_GPS_DATE_STAMP 0x001D
  335. #define TAG_GPS_DIFFERENTIAL 0x001E
  336. #define TAG_TIFF_COMMENT 0x00FE /* SHOUDLNT HAPPEN */
  337. #define TAG_NEW_SUBFILE 0x00FE /* New version of subfile tag */
  338. #define TAG_SUBFILE_TYPE 0x00FF /* Old version of subfile tag */
  339. #define TAG_IMAGEWIDTH 0x0100
  340. #define TAG_IMAGEHEIGHT 0x0101
  341. #define TAG_BITS_PER_SAMPLE 0x0102
  342. #define TAG_COMPRESSION 0x0103
  343. #define TAG_PHOTOMETRIC_INTERPRETATION 0x0106
  344. #define TAG_TRESHHOLDING 0x0107
  345. #define TAG_CELL_WIDTH 0x0108
  346. #define TAG_CELL_HEIGHT 0x0109
  347. #define TAG_FILL_ORDER 0x010A
  348. #define TAG_DOCUMENT_NAME 0x010D
  349. #define TAG_IMAGE_DESCRIPTION 0x010E
  350. #define TAG_MAKE 0x010F
  351. #define TAG_MODEL 0x0110
  352. #define TAG_STRIP_OFFSETS 0x0111
  353. #define TAG_ORIENTATION 0x0112
  354. #define TAG_SAMPLES_PER_PIXEL 0x0115
  355. #define TAG_ROWS_PER_STRIP 0x0116
  356. #define TAG_STRIP_BYTE_COUNTS 0x0117
  357. #define TAG_MIN_SAMPPLE_VALUE 0x0118
  358. #define TAG_MAX_SAMPLE_VALUE 0x0119
  359. #define TAG_X_RESOLUTION 0x011A
  360. #define TAG_Y_RESOLUTION 0x011B
  361. #define TAG_PLANAR_CONFIGURATION 0x011C
  362. #define TAG_PAGE_NAME 0x011D
  363. #define TAG_X_POSITION 0x011E
  364. #define TAG_Y_POSITION 0x011F
  365. #define TAG_FREE_OFFSETS 0x0120
  366. #define TAG_FREE_BYTE_COUNTS 0x0121
  367. #define TAG_GRAY_RESPONSE_UNIT 0x0122
  368. #define TAG_GRAY_RESPONSE_CURVE 0x0123
  369. #define TAG_RESOLUTION_UNIT 0x0128
  370. #define TAG_PAGE_NUMBER 0x0129
  371. #define TAG_TRANSFER_FUNCTION 0x012D
  372. #define TAG_SOFTWARE 0x0131
  373. #define TAG_DATETIME 0x0132
  374. #define TAG_ARTIST 0x013B
  375. #define TAG_HOST_COMPUTER 0x013C
  376. #define TAG_PREDICTOR 0x013D
  377. #define TAG_WHITE_POINT 0x013E
  378. #define TAG_PRIMARY_CHROMATICITIES 0x013F
  379. #define TAG_COLOR_MAP 0x0140
  380. #define TAG_HALFTONE_HINTS 0x0141
  381. #define TAG_TILE_WIDTH 0x0142
  382. #define TAG_TILE_LENGTH 0x0143
  383. #define TAG_TILE_OFFSETS 0x0144
  384. #define TAG_TILE_BYTE_COUNTS 0x0145
  385. #define TAG_SUB_IFD 0x014A
  386. #define TAG_INK_SETMPUTER 0x014C
  387. #define TAG_INK_NAMES 0x014D
  388. #define TAG_NUMBER_OF_INKS 0x014E
  389. #define TAG_DOT_RANGE 0x0150
  390. #define TAG_TARGET_PRINTER 0x0151
  391. #define TAG_EXTRA_SAMPLE 0x0152
  392. #define TAG_SAMPLE_FORMAT 0x0153
  393. #define TAG_S_MIN_SAMPLE_VALUE 0x0154
  394. #define TAG_S_MAX_SAMPLE_VALUE 0x0155
  395. #define TAG_TRANSFER_RANGE 0x0156
  396. #define TAG_JPEG_TABLES 0x015B
  397. #define TAG_JPEG_PROC 0x0200
  398. #define TAG_JPEG_INTERCHANGE_FORMAT 0x0201
  399. #define TAG_JPEG_INTERCHANGE_FORMAT_LEN 0x0202
  400. #define TAG_JPEG_RESTART_INTERVAL 0x0203
  401. #define TAG_JPEG_LOSSLESS_PREDICTOR 0x0205
  402. #define TAG_JPEG_POINT_TRANSFORMS 0x0206
  403. #define TAG_JPEG_Q_TABLES 0x0207
  404. #define TAG_JPEG_DC_TABLES 0x0208
  405. #define TAG_JPEG_AC_TABLES 0x0209
  406. #define TAG_YCC_COEFFICIENTS 0x0211
  407. #define TAG_YCC_SUB_SAMPLING 0x0212
  408. #define TAG_YCC_POSITIONING 0x0213
  409. #define TAG_REFERENCE_BLACK_WHITE 0x0214
  410. /* 0x0301 - 0x0302 */
  411. /* 0x0320 */
  412. /* 0x0343 */
  413. /* 0x5001 - 0x501B */
  414. /* 0x5021 - 0x503B */
  415. /* 0x5090 - 0x5091 */
  416. /* 0x5100 - 0x5101 */
  417. /* 0x5110 - 0x5113 */
  418. /* 0x80E3 - 0x80E6 */
  419. /* 0x828d - 0x828F */
  420. #define TAG_COPYRIGHT 0x8298
  421. #define TAG_EXPOSURETIME 0x829A
  422. #define TAG_FNUMBER 0x829D
  423. #define TAG_EXIF_IFD_POINTER 0x8769
  424. #define TAG_ICC_PROFILE 0x8773
  425. #define TAG_EXPOSURE_PROGRAM 0x8822
  426. #define TAG_SPECTRAL_SENSITY 0x8824
  427. #define TAG_GPS_IFD_POINTER 0x8825
  428. #define TAG_ISOSPEED 0x8827
  429. #define TAG_OPTOELECTRIC_CONVERSION_F 0x8828
  430. /* 0x8829 - 0x882b */
  431. #define TAG_EXIFVERSION 0x9000
  432. #define TAG_DATE_TIME_ORIGINAL 0x9003
  433. #define TAG_DATE_TIME_DIGITIZED 0x9004
  434. #define TAG_COMPONENT_CONFIG 0x9101
  435. #define TAG_COMPRESSED_BITS_PER_PIXEL 0x9102
  436. #define TAG_SHUTTERSPEED 0x9201
  437. #define TAG_APERTURE 0x9202
  438. #define TAG_BRIGHTNESS_VALUE 0x9203
  439. #define TAG_EXPOSURE_BIAS_VALUE 0x9204
  440. #define TAG_MAX_APERTURE 0x9205
  441. #define TAG_SUBJECT_DISTANCE 0x9206
  442. #define TAG_METRIC_MODULE 0x9207
  443. #define TAG_LIGHT_SOURCE 0x9208
  444. #define TAG_FLASH 0x9209
  445. #define TAG_FOCAL_LENGTH 0x920A
  446. /* 0x920B - 0x920D */
  447. /* 0x9211 - 0x9216 */
  448. #define TAG_SUBJECT_AREA 0x9214
  449. #define TAG_MAKER_NOTE 0x927C
  450. #define TAG_USERCOMMENT 0x9286
  451. #define TAG_SUB_SEC_TIME 0x9290
  452. #define TAG_SUB_SEC_TIME_ORIGINAL 0x9291
  453. #define TAG_SUB_SEC_TIME_DIGITIZED 0x9292
  454. /* 0x923F */
  455. /* 0x935C */
  456. #define TAG_XP_TITLE 0x9C9B
  457. #define TAG_XP_COMMENTS 0x9C9C
  458. #define TAG_XP_AUTHOR 0x9C9D
  459. #define TAG_XP_KEYWORDS 0x9C9E
  460. #define TAG_XP_SUBJECT 0x9C9F
  461. #define TAG_FLASH_PIX_VERSION 0xA000
  462. #define TAG_COLOR_SPACE 0xA001
  463. #define TAG_COMP_IMAGE_WIDTH 0xA002 /* compressed images only */
  464. #define TAG_COMP_IMAGE_HEIGHT 0xA003
  465. #define TAG_RELATED_SOUND_FILE 0xA004
  466. #define TAG_INTEROP_IFD_POINTER 0xA005 /* IFD pointer */
  467. #define TAG_FLASH_ENERGY 0xA20B
  468. #define TAG_SPATIAL_FREQUENCY_RESPONSE 0xA20C
  469. #define TAG_FOCALPLANE_X_RES 0xA20E
  470. #define TAG_FOCALPLANE_Y_RES 0xA20F
  471. #define TAG_FOCALPLANE_RESOLUTION_UNIT 0xA210
  472. #define TAG_SUBJECT_LOCATION 0xA214
  473. #define TAG_EXPOSURE_INDEX 0xA215
  474. #define TAG_SENSING_METHOD 0xA217
  475. #define TAG_FILE_SOURCE 0xA300
  476. #define TAG_SCENE_TYPE 0xA301
  477. #define TAG_CFA_PATTERN 0xA302
  478. #define TAG_CUSTOM_RENDERED 0xA401
  479. #define TAG_EXPOSURE_MODE 0xA402
  480. #define TAG_WHITE_BALANCE 0xA403
  481. #define TAG_DIGITAL_ZOOM_RATIO 0xA404
  482. #define TAG_FOCAL_LENGTH_IN_35_MM_FILM 0xA405
  483. #define TAG_SCENE_CAPTURE_TYPE 0xA406
  484. #define TAG_GAIN_CONTROL 0xA407
  485. #define TAG_CONTRAST 0xA408
  486. #define TAG_SATURATION 0xA409
  487. #define TAG_SHARPNESS 0xA40A
  488. #define TAG_DEVICE_SETTING_DESCRIPTION 0xA40B
  489. #define TAG_SUBJECT_DISTANCE_RANGE 0xA40C
  490. #define TAG_IMAGE_UNIQUE_ID 0xA420
  491. /* Olympus specific tags */
  492. #define TAG_OLYMPUS_SPECIALMODE 0x0200
  493. #define TAG_OLYMPUS_JPEGQUAL 0x0201
  494. #define TAG_OLYMPUS_MACRO 0x0202
  495. #define TAG_OLYMPUS_DIGIZOOM 0x0204
  496. #define TAG_OLYMPUS_SOFTWARERELEASE 0x0207
  497. #define TAG_OLYMPUS_PICTINFO 0x0208
  498. #define TAG_OLYMPUS_CAMERAID 0x0209
  499. /* end Olympus specific tags */
  500. /* Internal */
  501. #define TAG_NONE -1 /* note that -1 <> 0xFFFF */
  502. #define TAG_COMPUTED_VALUE -2
  503. #define TAG_END_OF_LIST 0xFFFD
  504. /* Values for TAG_PHOTOMETRIC_INTERPRETATION */
  505. #define PMI_BLACK_IS_ZERO 0
  506. #define PMI_WHITE_IS_ZERO 1
  507. #define PMI_RGB 2
  508. #define PMI_PALETTE_COLOR 3
  509. #define PMI_TRANSPARENCY_MASK 4
  510. #define PMI_SEPARATED 5
  511. #define PMI_YCBCR 6
  512. #define PMI_CIELAB 8
  513. /* }}} */
  514. /* {{{ TabTable[]
  515. */
  516. typedef const struct {
  517. unsigned short Tag;
  518. char *Desc;
  519. } tag_info_type;
  520. typedef tag_info_type tag_info_array[];
  521. typedef tag_info_type *tag_table_type;
  522. #define TAG_TABLE_END \
  523. {TAG_NONE, "No tag value"},\
  524. {TAG_COMPUTED_VALUE, "Computed value"},\
  525. {TAG_END_OF_LIST, ""} /* Important for exif_get_tagname() IF value != "" function result is != false */
  526. static tag_info_array tag_table_IFD = {
  527. { 0x000B, "ACDComment"},
  528. { 0x00FE, "NewSubFile"}, /* better name it 'ImageType' ? */
  529. { 0x00FF, "SubFile"},
  530. { 0x0100, "ImageWidth"},
  531. { 0x0101, "ImageLength"},
  532. { 0x0102, "BitsPerSample"},
  533. { 0x0103, "Compression"},
  534. { 0x0106, "PhotometricInterpretation"},
  535. { 0x010A, "FillOrder"},
  536. { 0x010D, "DocumentName"},
  537. { 0x010E, "ImageDescription"},
  538. { 0x010F, "Make"},
  539. { 0x0110, "Model"},
  540. { 0x0111, "StripOffsets"},
  541. { 0x0112, "Orientation"},
  542. { 0x0115, "SamplesPerPixel"},
  543. { 0x0116, "RowsPerStrip"},
  544. { 0x0117, "StripByteCounts"},
  545. { 0x0118, "MinSampleValue"},
  546. { 0x0119, "MaxSampleValue"},
  547. { 0x011A, "XResolution"},
  548. { 0x011B, "YResolution"},
  549. { 0x011C, "PlanarConfiguration"},
  550. { 0x011D, "PageName"},
  551. { 0x011E, "XPosition"},
  552. { 0x011F, "YPosition"},
  553. { 0x0120, "FreeOffsets"},
  554. { 0x0121, "FreeByteCounts"},
  555. { 0x0122, "GrayResponseUnit"},
  556. { 0x0123, "GrayResponseCurve"},
  557. { 0x0124, "T4Options"},
  558. { 0x0125, "T6Options"},
  559. { 0x0128, "ResolutionUnit"},
  560. { 0x0129, "PageNumber"},
  561. { 0x012D, "TransferFunction"},
  562. { 0x0131, "Software"},
  563. { 0x0132, "DateTime"},
  564. { 0x013B, "Artist"},
  565. { 0x013C, "HostComputer"},
  566. { 0x013D, "Predictor"},
  567. { 0x013E, "WhitePoint"},
  568. { 0x013F, "PrimaryChromaticities"},
  569. { 0x0140, "ColorMap"},
  570. { 0x0141, "HalfToneHints"},
  571. { 0x0142, "TileWidth"},
  572. { 0x0143, "TileLength"},
  573. { 0x0144, "TileOffsets"},
  574. { 0x0145, "TileByteCounts"},
  575. { 0x014A, "SubIFD"},
  576. { 0x014C, "InkSet"},
  577. { 0x014D, "InkNames"},
  578. { 0x014E, "NumberOfInks"},
  579. { 0x0150, "DotRange"},
  580. { 0x0151, "TargetPrinter"},
  581. { 0x0152, "ExtraSample"},
  582. { 0x0153, "SampleFormat"},
  583. { 0x0154, "SMinSampleValue"},
  584. { 0x0155, "SMaxSampleValue"},
  585. { 0x0156, "TransferRange"},
  586. { 0x0157, "ClipPath"},
  587. { 0x0158, "XClipPathUnits"},
  588. { 0x0159, "YClipPathUnits"},
  589. { 0x015A, "Indexed"},
  590. { 0x015B, "JPEGTables"},
  591. { 0x015F, "OPIProxy"},
  592. { 0x0200, "JPEGProc"},
  593. { 0x0201, "JPEGInterchangeFormat"},
  594. { 0x0202, "JPEGInterchangeFormatLength"},
  595. { 0x0203, "JPEGRestartInterval"},
  596. { 0x0205, "JPEGLosslessPredictors"},
  597. { 0x0206, "JPEGPointTransforms"},
  598. { 0x0207, "JPEGQTables"},
  599. { 0x0208, "JPEGDCTables"},
  600. { 0x0209, "JPEGACTables"},
  601. { 0x0211, "YCbCrCoefficients"},
  602. { 0x0212, "YCbCrSubSampling"},
  603. { 0x0213, "YCbCrPositioning"},
  604. { 0x0214, "ReferenceBlackWhite"},
  605. { 0x02BC, "ExtensibleMetadataPlatform"}, /* XAP: Extensible Authoring Publishing, obsoleted by XMP: Extensible Metadata Platform */
  606. { 0x0301, "Gamma"},
  607. { 0x0302, "ICCProfileDescriptor"},
  608. { 0x0303, "SRGBRenderingIntent"},
  609. { 0x0320, "ImageTitle"},
  610. { 0x5001, "ResolutionXUnit"},
  611. { 0x5002, "ResolutionYUnit"},
  612. { 0x5003, "ResolutionXLengthUnit"},
  613. { 0x5004, "ResolutionYLengthUnit"},
  614. { 0x5005, "PrintFlags"},
  615. { 0x5006, "PrintFlagsVersion"},
  616. { 0x5007, "PrintFlagsCrop"},
  617. { 0x5008, "PrintFlagsBleedWidth"},
  618. { 0x5009, "PrintFlagsBleedWidthScale"},
  619. { 0x500A, "HalftoneLPI"},
  620. { 0x500B, "HalftoneLPIUnit"},
  621. { 0x500C, "HalftoneDegree"},
  622. { 0x500D, "HalftoneShape"},
  623. { 0x500E, "HalftoneMisc"},
  624. { 0x500F, "HalftoneScreen"},
  625. { 0x5010, "JPEGQuality"},
  626. { 0x5011, "GridSize"},
  627. { 0x5012, "ThumbnailFormat"},
  628. { 0x5013, "ThumbnailWidth"},
  629. { 0x5014, "ThumbnailHeight"},
  630. { 0x5015, "ThumbnailColorDepth"},
  631. { 0x5016, "ThumbnailPlanes"},
  632. { 0x5017, "ThumbnailRawBytes"},
  633. { 0x5018, "ThumbnailSize"},
  634. { 0x5019, "ThumbnailCompressedSize"},
  635. { 0x501A, "ColorTransferFunction"},
  636. { 0x501B, "ThumbnailData"},
  637. { 0x5020, "ThumbnailImageWidth"},
  638. { 0x5021, "ThumbnailImageHeight"},
  639. { 0x5022, "ThumbnailBitsPerSample"},
  640. { 0x5023, "ThumbnailCompression"},
  641. { 0x5024, "ThumbnailPhotometricInterp"},
  642. { 0x5025, "ThumbnailImageDescription"},
  643. { 0x5026, "ThumbnailEquipMake"},
  644. { 0x5027, "ThumbnailEquipModel"},
  645. { 0x5028, "ThumbnailStripOffsets"},
  646. { 0x5029, "ThumbnailOrientation"},
  647. { 0x502A, "ThumbnailSamplesPerPixel"},
  648. { 0x502B, "ThumbnailRowsPerStrip"},
  649. { 0x502C, "ThumbnailStripBytesCount"},
  650. { 0x502D, "ThumbnailResolutionX"},
  651. { 0x502E, "ThumbnailResolutionY"},
  652. { 0x502F, "ThumbnailPlanarConfig"},
  653. { 0x5030, "ThumbnailResolutionUnit"},
  654. { 0x5031, "ThumbnailTransferFunction"},
  655. { 0x5032, "ThumbnailSoftwareUsed"},
  656. { 0x5033, "ThumbnailDateTime"},
  657. { 0x5034, "ThumbnailArtist"},
  658. { 0x5035, "ThumbnailWhitePoint"},
  659. { 0x5036, "ThumbnailPrimaryChromaticities"},
  660. { 0x5037, "ThumbnailYCbCrCoefficients"},
  661. { 0x5038, "ThumbnailYCbCrSubsampling"},
  662. { 0x5039, "ThumbnailYCbCrPositioning"},
  663. { 0x503A, "ThumbnailRefBlackWhite"},
  664. { 0x503B, "ThumbnailCopyRight"},
  665. { 0x5090, "LuminanceTable"},
  666. { 0x5091, "ChrominanceTable"},
  667. { 0x5100, "FrameDelay"},
  668. { 0x5101, "LoopCount"},
  669. { 0x5110, "PixelUnit"},
  670. { 0x5111, "PixelPerUnitX"},
  671. { 0x5112, "PixelPerUnitY"},
  672. { 0x5113, "PaletteHistogram"},
  673. { 0x1000, "RelatedImageFileFormat"},
  674. { 0x800D, "ImageID"},
  675. { 0x80E3, "Matteing"}, /* obsoleted by ExtraSamples */
  676. { 0x80E4, "DataType"}, /* obsoleted by SampleFormat */
  677. { 0x80E5, "ImageDepth"},
  678. { 0x80E6, "TileDepth"},
  679. { 0x828D, "CFARepeatPatternDim"},
  680. { 0x828E, "CFAPattern"},
  681. { 0x828F, "BatteryLevel"},
  682. { 0x8298, "Copyright"},
  683. { 0x829A, "ExposureTime"},
  684. { 0x829D, "FNumber"},
  685. { 0x83BB, "IPTC/NAA"},
  686. { 0x84E3, "IT8RasterPadding"},
  687. { 0x84E5, "IT8ColorTable"},
  688. { 0x8649, "ImageResourceInformation"}, /* PhotoShop */
  689. { 0x8769, "Exif_IFD_Pointer"},
  690. { 0x8773, "ICC_Profile"},
  691. { 0x8822, "ExposureProgram"},
  692. { 0x8824, "SpectralSensity"},
  693. { 0x8828, "OECF"},
  694. { 0x8825, "GPS_IFD_Pointer"},
  695. { 0x8827, "ISOSpeedRatings"},
  696. { 0x8828, "OECF"},
  697. { 0x9000, "ExifVersion"},
  698. { 0x9003, "DateTimeOriginal"},
  699. { 0x9004, "DateTimeDigitized"},
  700. { 0x9101, "ComponentsConfiguration"},
  701. { 0x9102, "CompressedBitsPerPixel"},
  702. { 0x9201, "ShutterSpeedValue"},
  703. { 0x9202, "ApertureValue"},
  704. { 0x9203, "BrightnessValue"},
  705. { 0x9204, "ExposureBiasValue"},
  706. { 0x9205, "MaxApertureValue"},
  707. { 0x9206, "SubjectDistance"},
  708. { 0x9207, "MeteringMode"},
  709. { 0x9208, "LightSource"},
  710. { 0x9209, "Flash"},
  711. { 0x920A, "FocalLength"},
  712. { 0x920B, "FlashEnergy"}, /* 0xA20B in JPEG */
  713. { 0x920C, "SpatialFrequencyResponse"}, /* 0xA20C - - */
  714. { 0x920D, "Noise"},
  715. { 0x920E, "FocalPlaneXResolution"}, /* 0xA20E - - */
  716. { 0x920F, "FocalPlaneYResolution"}, /* 0xA20F - - */
  717. { 0x9210, "FocalPlaneResolutionUnit"}, /* 0xA210 - - */
  718. { 0x9211, "ImageNumber"},
  719. { 0x9212, "SecurityClassification"},
  720. { 0x9213, "ImageHistory"},
  721. { 0x9214, "SubjectLocation"}, /* 0xA214 - - */
  722. { 0x9215, "ExposureIndex"}, /* 0xA215 - - */
  723. { 0x9216, "TIFF/EPStandardID"},
  724. { 0x9217, "SensingMethod"}, /* 0xA217 - - */
  725. { 0x923F, "StoNits"},
  726. { 0x927C, "MakerNote"},
  727. { 0x9286, "UserComment"},
  728. { 0x9290, "SubSecTime"},
  729. { 0x9291, "SubSecTimeOriginal"},
  730. { 0x9292, "SubSecTimeDigitized"},
  731. { 0x935C, "ImageSourceData"}, /* "Adobe Photoshop Document Data Block": 8BIM... */
  732. { 0x9c9b, "Title" }, /* Win XP specific, Unicode */
  733. { 0x9c9c, "Comments" }, /* Win XP specific, Unicode */
  734. { 0x9c9d, "Author" }, /* Win XP specific, Unicode */
  735. { 0x9c9e, "Keywords" }, /* Win XP specific, Unicode */
  736. { 0x9c9f, "Subject" }, /* Win XP specific, Unicode, not to be confused with SubjectDistance and SubjectLocation */
  737. { 0xA000, "FlashPixVersion"},
  738. { 0xA001, "ColorSpace"},
  739. { 0xA002, "ExifImageWidth"},
  740. { 0xA003, "ExifImageLength"},
  741. { 0xA004, "RelatedSoundFile"},
  742. { 0xA005, "InteroperabilityOffset"},
  743. { 0xA20B, "FlashEnergy"}, /* 0x920B in TIFF/EP */
  744. { 0xA20C, "SpatialFrequencyResponse"}, /* 0x920C - - */
  745. { 0xA20D, "Noise"},
  746. { 0xA20E, "FocalPlaneXResolution"}, /* 0x920E - - */
  747. { 0xA20F, "FocalPlaneYResolution"}, /* 0x920F - - */
  748. { 0xA210, "FocalPlaneResolutionUnit"}, /* 0x9210 - - */
  749. { 0xA211, "ImageNumber"},
  750. { 0xA212, "SecurityClassification"},
  751. { 0xA213, "ImageHistory"},
  752. { 0xA214, "SubjectLocation"}, /* 0x9214 - - */
  753. { 0xA215, "ExposureIndex"}, /* 0x9215 - - */
  754. { 0xA216, "TIFF/EPStandardID"},
  755. { 0xA217, "SensingMethod"}, /* 0x9217 - - */
  756. { 0xA300, "FileSource"},
  757. { 0xA301, "SceneType"},
  758. { 0xA302, "CFAPattern"},
  759. { 0xA401, "CustomRendered"},
  760. { 0xA402, "ExposureMode"},
  761. { 0xA403, "WhiteBalance"},
  762. { 0xA404, "DigitalZoomRatio"},
  763. { 0xA405, "FocalLengthIn35mmFilm"},
  764. { 0xA406, "SceneCaptureType"},
  765. { 0xA407, "GainControl"},
  766. { 0xA408, "Contrast"},
  767. { 0xA409, "Saturation"},
  768. { 0xA40A, "Sharpness"},
  769. { 0xA40B, "DeviceSettingDescription"},
  770. { 0xA40C, "SubjectDistanceRange"},
  771. { 0xA420, "ImageUniqueID"},
  772. TAG_TABLE_END
  773. } ;
  774. static tag_info_array tag_table_GPS = {
  775. { 0x0000, "GPSVersion"},
  776. { 0x0001, "GPSLatitudeRef"},
  777. { 0x0002, "GPSLatitude"},
  778. { 0x0003, "GPSLongitudeRef"},
  779. { 0x0004, "GPSLongitude"},
  780. { 0x0005, "GPSAltitudeRef"},
  781. { 0x0006, "GPSAltitude"},
  782. { 0x0007, "GPSTimeStamp"},
  783. { 0x0008, "GPSSatellites"},
  784. { 0x0009, "GPSStatus"},
  785. { 0x000A, "GPSMeasureMode"},
  786. { 0x000B, "GPSDOP"},
  787. { 0x000C, "GPSSpeedRef"},
  788. { 0x000D, "GPSSpeed"},
  789. { 0x000E, "GPSTrackRef"},
  790. { 0x000F, "GPSTrack"},
  791. { 0x0010, "GPSImgDirectionRef"},
  792. { 0x0011, "GPSImgDirection"},
  793. { 0x0012, "GPSMapDatum"},
  794. { 0x0013, "GPSDestLatitudeRef"},
  795. { 0x0014, "GPSDestLatitude"},
  796. { 0x0015, "GPSDestLongitudeRef"},
  797. { 0x0016, "GPSDestLongitude"},
  798. { 0x0017, "GPSDestBearingRef"},
  799. { 0x0018, "GPSDestBearing"},
  800. { 0x0019, "GPSDestDistanceRef"},
  801. { 0x001A, "GPSDestDistance"},
  802. { 0x001B, "GPSProcessingMode"},
  803. { 0x001C, "GPSAreaInformation"},
  804. { 0x001D, "GPSDateStamp"},
  805. { 0x001E, "GPSDifferential"},
  806. TAG_TABLE_END
  807. };
  808. static tag_info_array tag_table_IOP = {
  809. { 0x0001, "InterOperabilityIndex"}, /* should be 'R98' or 'THM' */
  810. { 0x0002, "InterOperabilityVersion"},
  811. { 0x1000, "RelatedFileFormat"},
  812. { 0x1001, "RelatedImageWidth"},
  813. { 0x1002, "RelatedImageHeight"},
  814. TAG_TABLE_END
  815. };
  816. static tag_info_array tag_table_VND_CANON = {
  817. { 0x0001, "ModeArray"}, /* guess */
  818. { 0x0004, "ImageInfo"}, /* guess */
  819. { 0x0006, "ImageType"},
  820. { 0x0007, "FirmwareVersion"},
  821. { 0x0008, "ImageNumber"},
  822. { 0x0009, "OwnerName"},
  823. { 0x000C, "Camera"},
  824. { 0x000F, "CustomFunctions"},
  825. TAG_TABLE_END
  826. };
  827. static tag_info_array tag_table_VND_CASIO = {
  828. { 0x0001, "RecordingMode"},
  829. { 0x0002, "Quality"},
  830. { 0x0003, "FocusingMode"},
  831. { 0x0004, "FlashMode"},
  832. { 0x0005, "FlashIntensity"},
  833. { 0x0006, "ObjectDistance"},
  834. { 0x0007, "WhiteBalance"},
  835. { 0x000A, "DigitalZoom"},
  836. { 0x000B, "Sharpness"},
  837. { 0x000C, "Contrast"},
  838. { 0x000D, "Saturation"},
  839. { 0x0014, "CCDSensitivity"},
  840. TAG_TABLE_END
  841. };
  842. static tag_info_array tag_table_VND_FUJI = {
  843. { 0x0000, "Version"},
  844. { 0x1000, "Quality"},
  845. { 0x1001, "Sharpness"},
  846. { 0x1002, "WhiteBalance"},
  847. { 0x1003, "Color"},
  848. { 0x1004, "Tone"},
  849. { 0x1010, "FlashMode"},
  850. { 0x1011, "FlashStrength"},
  851. { 0x1020, "Macro"},
  852. { 0x1021, "FocusMode"},
  853. { 0x1030, "SlowSync"},
  854. { 0x1031, "PictureMode"},
  855. { 0x1100, "ContTake"},
  856. { 0x1300, "BlurWarning"},
  857. { 0x1301, "FocusWarning"},
  858. { 0x1302, "AEWarning "},
  859. TAG_TABLE_END
  860. };
  861. static tag_info_array tag_table_VND_NIKON = {
  862. { 0x0003, "Quality"},
  863. { 0x0004, "ColorMode"},
  864. { 0x0005, "ImageAdjustment"},
  865. { 0x0006, "CCDSensitivity"},
  866. { 0x0007, "WhiteBalance"},
  867. { 0x0008, "Focus"},
  868. { 0x000a, "DigitalZoom"},
  869. { 0x000b, "Converter"},
  870. TAG_TABLE_END
  871. };
  872. static tag_info_array tag_table_VND_NIKON_990 = {
  873. { 0x0001, "Version"},
  874. { 0x0002, "ISOSetting"},
  875. { 0x0003, "ColorMode"},
  876. { 0x0004, "Quality"},
  877. { 0x0005, "WhiteBalance"},
  878. { 0x0006, "ImageSharpening"},
  879. { 0x0007, "FocusMode"},
  880. { 0x0008, "FlashSetting"},
  881. { 0x000F, "ISOSelection"},
  882. { 0x0080, "ImageAdjustment"},
  883. { 0x0082, "AuxiliaryLens"},
  884. { 0x0085, "ManualFocusDistance"},
  885. { 0x0086, "DigitalZoom"},
  886. { 0x0088, "AFFocusPosition"},
  887. { 0x0010, "DataDump"},
  888. TAG_TABLE_END
  889. };
  890. static tag_info_array tag_table_VND_OLYMPUS = {
  891. { 0x0200, "SpecialMode"},
  892. { 0x0201, "JPEGQuality"},
  893. { 0x0202, "Macro"},
  894. { 0x0204, "DigitalZoom"},
  895. { 0x0207, "SoftwareRelease"},
  896. { 0x0208, "PictureInfo"},
  897. { 0x0209, "CameraId"},
  898. { 0x0F00, "DataDump"},
  899. TAG_TABLE_END
  900. };
  901. typedef enum mn_byte_order_t {
  902. MN_ORDER_INTEL = 0,
  903. MN_ORDER_MOTOROLA = 1,
  904. MN_ORDER_NORMAL
  905. } mn_byte_order_t;
  906. typedef enum mn_offset_mode_t {
  907. MN_OFFSET_NORMAL,
  908. MN_OFFSET_MAKER,
  909. MN_OFFSET_GUESS
  910. } mn_offset_mode_t;
  911. typedef struct {
  912. tag_table_type tag_table;
  913. char * make;
  914. char * model;
  915. char * id_string;
  916. int id_string_len;
  917. int offset;
  918. mn_byte_order_t byte_order;
  919. mn_offset_mode_t offset_mode;
  920. } maker_note_type;
  921. static const maker_note_type maker_note_array[] = {
  922. { tag_table_VND_CANON, "Canon", NULL, NULL, 0, 0, MN_ORDER_INTEL, MN_OFFSET_GUESS},
  923. /* { tag_table_VND_CANON, "Canon", NULL, NULL, 0, 0, MN_ORDER_NORMAL, MN_OFFSET_NORMAL},*/
  924. { tag_table_VND_CASIO, "CASIO", NULL, NULL, 0, 0, MN_ORDER_MOTOROLA, MN_OFFSET_NORMAL},
  925. { tag_table_VND_FUJI, "FUJIFILM", NULL, "FUJIFILM\x0C\x00\x00\x00", 12, 12, MN_ORDER_INTEL, MN_OFFSET_MAKER},
  926. { tag_table_VND_NIKON, "NIKON", NULL, "Nikon\x00\x01\x00", 8, 8, MN_ORDER_NORMAL, MN_OFFSET_NORMAL},
  927. { tag_table_VND_NIKON_990, "NIKON", NULL, NULL, 0, 0, MN_ORDER_NORMAL, MN_OFFSET_NORMAL},
  928. { tag_table_VND_OLYMPUS, "OLYMPUS OPTICAL CO.,LTD", NULL, "OLYMP\x00\x01\x00", 8, 8, MN_ORDER_NORMAL, MN_OFFSET_NORMAL},
  929. };
  930. /* }}} */
  931. /* {{{ exif_get_tagname
  932. Get headername for tag_num or NULL if not defined */
  933. static char * exif_get_tagname(int tag_num, char *ret, int len, tag_table_type tag_table TSRMLS_DC)
  934. {
  935. int i, t;
  936. char tmp[32];
  937. for (i = 0; (t = tag_table[i].Tag) != TAG_END_OF_LIST; i++) {
  938. if (t == tag_num) {
  939. if (ret && len) {
  940. strlcpy(ret, tag_table[i].Desc, abs(len));
  941. if (len < 0) {
  942. memset(ret + strlen(ret), ' ', -len - strlen(ret) - 1);
  943. ret[-len - 1] = '\0';
  944. }
  945. return ret;
  946. }
  947. return tag_table[i].Desc;
  948. }
  949. }
  950. if (ret && len) {
  951. snprintf(tmp, sizeof(tmp), "UndefinedTag:0x%04X", tag_num);
  952. strlcpy(ret, tmp, abs(len));
  953. if (len < 0) {
  954. memset(ret + strlen(ret), ' ', -len - strlen(ret) - 1);
  955. ret[-len - 1] = '\0';
  956. }
  957. return ret;
  958. }
  959. return "";
  960. }
  961. /* }}} */
  962. /* {{{ exif_char_dump
  963. * Do not use! This is a debug function... */
  964. #ifdef EXIF_DEBUG
  965. static unsigned char* exif_char_dump(unsigned char * addr, int len, int offset)
  966. {
  967. static unsigned char buf[4096+1];
  968. static unsigned char tmp[20];
  969. int c, i, p=0, n = 5+31;
  970. p += slprintf(buf+p, sizeof(buf)-p, "\nDump Len: %08X (%d)", len, len);
  971. if (len) {
  972. for(i=0; i<len+15 && p+n<=sizeof(buf); i++) {
  973. if (i%16==0) {
  974. p += slprintf(buf+p, sizeof(buf)-p, "\n%08X: ", i+offset);
  975. }
  976. if (i<len) {
  977. c = *addr++;
  978. p += slprintf(buf+p, sizeof(buf)-p, "%02X ", c);
  979. tmp[i%16] = c>=32 ? c : '.';
  980. tmp[(i%16)+1] = '\0';
  981. } else {
  982. p += slprintf(buf+p, sizeof(buf)-p, " ");
  983. }
  984. if (i%16==15) {
  985. p += slprintf(buf+p, sizeof(buf)-p, " %s", tmp);
  986. if (i>=len) {
  987. break;
  988. }
  989. }
  990. }
  991. }
  992. buf[sizeof(buf)-1] = '\0';
  993. return buf;
  994. }
  995. #endif
  996. /* }}} */
  997. /* {{{ php_jpg_get16
  998. Get 16 bits motorola order (always) for jpeg header stuff.
  999. */
  1000. static int php_jpg_get16(void *value)
  1001. {
  1002. return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
  1003. }
  1004. /* }}} */
  1005. /* {{{ php_ifd_get16u
  1006. * Convert a 16 bit unsigned value from file's native byte order */
  1007. static int php_ifd_get16u(void *value, int motorola_intel)
  1008. {
  1009. if (motorola_intel) {
  1010. return (((uchar *)value)[0] << 8) | ((uchar *)value)[1];
  1011. } else {
  1012. return (((uchar *)value)[1] << 8) | ((uchar *)value)[0];
  1013. }
  1014. }
  1015. /* }}} */
  1016. /* {{{ php_ifd_get16s
  1017. * Convert a 16 bit signed value from file's native byte order */
  1018. static signed short php_ifd_get16s(void *value, int motorola_intel)
  1019. {
  1020. return (signed short)php_ifd_get16u(value, motorola_intel);
  1021. }
  1022. /* }}} */
  1023. /* {{{ php_ifd_get32s
  1024. * Convert a 32 bit signed value from file's native byte order */
  1025. static int php_ifd_get32s(void *value, int motorola_intel)
  1026. {
  1027. if (motorola_intel) {
  1028. return (((char *)value)[0] << 24)
  1029. | (((uchar *)value)[1] << 16)
  1030. | (((uchar *)value)[2] << 8 )
  1031. | (((uchar *)value)[3] );
  1032. } else {
  1033. return (((char *)value)[3] << 24)
  1034. | (((uchar *)value)[2] << 16)
  1035. | (((uchar *)value)[1] << 8 )
  1036. | (((uchar *)value)[0] );
  1037. }
  1038. }
  1039. /* }}} */
  1040. /* {{{ php_ifd_get32u
  1041. * Write 32 bit unsigned value to data */
  1042. static unsigned php_ifd_get32u(void *value, int motorola_intel)
  1043. {
  1044. return (unsigned)php_ifd_get32s(value, motorola_intel) & 0xffffffff;
  1045. }
  1046. /* }}} */
  1047. /* {{{ php_ifd_set16u
  1048. * Write 16 bit unsigned value to data */
  1049. static void php_ifd_set16u(char *data, unsigned int value, int motorola_intel)
  1050. {
  1051. if (motorola_intel) {
  1052. data[0] = (value & 0xFF00) >> 8;
  1053. data[1] = (value & 0x00FF);
  1054. } else {
  1055. data[1] = (value & 0xFF00) >> 8;
  1056. data[0] = (value & 0x00FF);
  1057. }
  1058. }
  1059. /* }}} */
  1060. /* {{{ php_ifd_set32u
  1061. * Convert a 32 bit unsigned value from file's native byte order */
  1062. static void php_ifd_set32u(char *data, size_t value, int motorola_intel)
  1063. {
  1064. if (motorola_intel) {
  1065. data[0] = (value & 0xFF000000) >> 24;
  1066. data[1] = (value & 0x00FF0000) >> 16;
  1067. data[2] = (value & 0x0000FF00) >> 8;
  1068. data[3] = (value & 0x000000FF);
  1069. } else {
  1070. data[3] = (value & 0xFF000000) >> 24;
  1071. data[2] = (value & 0x00FF0000) >> 16;
  1072. data[1] = (value & 0x0000FF00) >> 8;
  1073. data[0] = (value & 0x000000FF);
  1074. }
  1075. }
  1076. /* }}} */
  1077. #ifdef EXIF_DEBUG
  1078. char * exif_dump_data(int *dump_free, int format, int components, int length, int motorola_intel, char *value_ptr TSRMLS_DC) /* {{{ */
  1079. {
  1080. char *dump;
  1081. int len;
  1082. *dump_free = 0;
  1083. if (format == TAG_FMT_STRING) {
  1084. return value_ptr ? value_ptr : "<no data>";
  1085. }
  1086. if (format == TAG_FMT_UNDEFINED) {
  1087. return "<undefined>\n";
  1088. }
  1089. if (format == TAG_FMT_IFD) {
  1090. return "";
  1091. }
  1092. if (format == TAG_FMT_SINGLE || format == TAG_FMT_DOUBLE) {
  1093. return "<not implemented>";
  1094. }
  1095. *dump_free = 1;
  1096. if (components > 1) {
  1097. len = spprintf(&dump, 0, "(%d,%d) {", components, length);
  1098. } else {
  1099. len = spprintf(&dump, 0, "{");
  1100. }
  1101. while(components > 0) {
  1102. switch(format) {
  1103. case TAG_FMT_BYTE:
  1104. case TAG_FMT_UNDEFINED:
  1105. case TAG_FMT_STRING:
  1106. case TAG_FMT_SBYTE:
  1107. dump = erealloc(dump, len + 4 + 1);
  1108. snprintf(dump + len, 4 + 1, "0x%02X", *value_ptr);
  1109. len += 4;
  1110. value_ptr++;
  1111. break;
  1112. case TAG_FMT_USHORT:
  1113. case TAG_FMT_SSHORT:
  1114. dump = erealloc(dump, len + 6 + 1);
  1115. snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get16s(value_ptr, motorola_intel));
  1116. len += 6;
  1117. value_ptr += 2;
  1118. break;
  1119. case TAG_FMT_ULONG:
  1120. case TAG_FMT_SLONG:
  1121. dump = erealloc(dump, len + 6 + 1);
  1122. snprintf(dump + len, 6 + 1, "0x%04X", php_ifd_get32s(value_ptr, motorola_intel));
  1123. len += 6;
  1124. value_ptr += 4;
  1125. break;
  1126. case TAG_FMT_URATIONAL:
  1127. case TAG_FMT_SRATIONAL:
  1128. dump = erealloc(dump, len + 13 + 1);
  1129. snprintf(dump + len, 13 + 1, "0x%04X/0x%04X", php_ifd_get32s(value_ptr, motorola_intel), php_ifd_get32s(value_ptr+4, motorola_intel));
  1130. len += 13;
  1131. value_ptr += 8;
  1132. break;
  1133. }
  1134. if (components > 0) {
  1135. dump = erealloc(dump, len + 2 + 1);
  1136. snprintf(dump + len, 2 + 1, ", ");
  1137. len += 2;
  1138. components--;
  1139. } else{
  1140. break;
  1141. }
  1142. }
  1143. dump = erealloc(dump, len + 1 + 1);
  1144. snprintf(dump + len, 1 + 1, "}");
  1145. return dump;
  1146. }
  1147. /* }}} */
  1148. #endif
  1149. /* {{{ exif_convert_any_format
  1150. * Evaluate number, be it int, rational, or float from directory. */
  1151. static double exif_convert_any_format(void *value, int format, int motorola_intel TSRMLS_DC)
  1152. {
  1153. int s_den;
  1154. unsigned u_den;
  1155. switch(format) {
  1156. case TAG_FMT_SBYTE: return *(signed char *)value;
  1157. case TAG_FMT_BYTE: return *(uchar *)value;
  1158. case TAG_FMT_USHORT: return php_ifd_get16u(value, motorola_intel);
  1159. case TAG_FMT_ULONG: return php_ifd_get32u(value, motorola_intel);
  1160. case TAG_FMT_URATIONAL:
  1161. u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
  1162. if (u_den == 0) {
  1163. return 0;
  1164. } else {
  1165. return (double)php_ifd_get32u(value, motorola_intel) / u_den;
  1166. }
  1167. case TAG_FMT_SRATIONAL:
  1168. s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
  1169. if (s_den == 0) {
  1170. return 0;
  1171. } else {
  1172. return (double)php_ifd_get32s(value, motorola_intel) / s_den;
  1173. }
  1174. case TAG_FMT_SSHORT: return (signed short)php_ifd_get16u(value, motorola_intel);
  1175. case TAG_FMT_SLONG: return php_ifd_get32s(value, motorola_intel);
  1176. /* Not sure if this is correct (never seen float used in Exif format) */
  1177. case TAG_FMT_SINGLE:
  1178. #ifdef EXIF_DEBUG
  1179. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type single");
  1180. #endif
  1181. return (double)*(float *)value;
  1182. case TAG_FMT_DOUBLE:
  1183. #ifdef EXIF_DEBUG
  1184. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type double");
  1185. #endif
  1186. return *(double *)value;
  1187. }
  1188. return 0;
  1189. }
  1190. /* }}} */
  1191. /* {{{ exif_convert_any_to_int
  1192. * Evaluate number, be it int, rational, or float from directory. */
  1193. static size_t exif_convert_any_to_int(void *value, int format, int motorola_intel TSRMLS_DC)
  1194. {
  1195. int s_den;
  1196. unsigned u_den;
  1197. switch(format) {
  1198. case TAG_FMT_SBYTE: return *(signed char *)value;
  1199. case TAG_FMT_BYTE: return *(uchar *)value;
  1200. case TAG_FMT_USHORT: return php_ifd_get16u(value, motorola_intel);
  1201. case TAG_FMT_ULONG: return php_ifd_get32u(value, motorola_intel);
  1202. case TAG_FMT_URATIONAL:
  1203. u_den = php_ifd_get32u(4+(char *)value, motorola_intel);
  1204. if (u_den == 0) {
  1205. return 0;
  1206. } else {
  1207. return php_ifd_get32u(value, motorola_intel) / u_den;
  1208. }
  1209. case TAG_FMT_SRATIONAL:
  1210. s_den = php_ifd_get32s(4+(char *)value, motorola_intel);
  1211. if (s_den == 0) {
  1212. return 0;
  1213. } else {
  1214. return (size_t)((double)php_ifd_get32s(value, motorola_intel) / s_den);
  1215. }
  1216. case TAG_FMT_SSHORT: return php_ifd_get16u(value, motorola_intel);
  1217. case TAG_FMT_SLONG: return php_ifd_get32s(value, motorola_intel);
  1218. /* Not sure if this is correct (never seen float used in Exif format) */
  1219. case TAG_FMT_SINGLE:
  1220. #ifdef EXIF_DEBUG
  1221. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type single");
  1222. #endif
  1223. return (size_t)*(float *)value;
  1224. case TAG_FMT_DOUBLE:
  1225. #ifdef EXIF_DEBUG
  1226. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found value of type double");
  1227. #endif
  1228. return (size_t)*(double *)value;
  1229. }
  1230. return 0;
  1231. }
  1232. /* }}} */
  1233. /* {{{ struct image_info_value, image_info_list
  1234. */
  1235. #ifndef WORD
  1236. #define WORD unsigned short
  1237. #endif
  1238. #ifndef DWORD
  1239. #define DWORD unsigned int
  1240. #endif
  1241. typedef struct {
  1242. int num;
  1243. int den;
  1244. } signed_rational;
  1245. typedef struct {
  1246. unsigned int num;
  1247. unsigned int den;
  1248. } unsigned_rational;
  1249. typedef union _image_info_value {
  1250. char *s;
  1251. unsigned u;
  1252. int i;
  1253. float f;
  1254. double d;
  1255. signed_rational sr;
  1256. unsigned_rational ur;
  1257. union _image_info_value *list;
  1258. } image_info_value;
  1259. typedef struct {
  1260. WORD tag;
  1261. WORD format;
  1262. DWORD length;
  1263. DWORD dummy; /* value ptr of tiff directory entry */
  1264. char *name;
  1265. image_info_value value;
  1266. } image_info_data;
  1267. typedef struct {
  1268. int count;
  1269. image_info_data *list;
  1270. } image_info_list;
  1271. /* }}} */
  1272. /* {{{ exif_get_sectionname
  1273. Returns the name of a section
  1274. */
  1275. #define SECTION_FILE 0
  1276. #define SECTION_COMPUTED 1
  1277. #define SECTION_ANY_TAG 2
  1278. #define SECTION_IFD0 3
  1279. #define SECTION_THUMBNAIL 4
  1280. #define SECTION_COMMENT 5
  1281. #define SECTION_APP0 6
  1282. #define SECTION_EXIF 7
  1283. #define SECTION_FPIX 8
  1284. #define SECTION_GPS 9
  1285. #define SECTION_INTEROP 10
  1286. #define SECTION_APP12 11
  1287. #define SECTION_WINXP 12
  1288. #define SECTION_MAKERNOTE 13
  1289. #define SECTION_COUNT 14
  1290. #define FOUND_FILE (1<<SECTION_FILE)
  1291. #define FOUND_COMPUTED (1<<SECTION_COMPUTED)
  1292. #define FOUND_ANY_TAG (1<<SECTION_ANY_TAG)
  1293. #define FOUND_IFD0 (1<<SECTION_IFD0)
  1294. #define FOUND_THUMBNAIL (1<<SECTION_THUMBNAIL)
  1295. #define FOUND_COMMENT (1<<SECTION_COMMENT)
  1296. #define FOUND_APP0 (1<<SECTION_APP0)
  1297. #define FOUND_EXIF (1<<SECTION_EXIF)
  1298. #define FOUND_FPIX (1<<SECTION_FPIX)
  1299. #define FOUND_GPS (1<<SECTION_GPS)
  1300. #define FOUND_INTEROP (1<<SECTION_INTEROP)
  1301. #define FOUND_APP12 (1<<SECTION_APP12)
  1302. #define FOUND_WINXP (1<<SECTION_WINXP)
  1303. #define FOUND_MAKERNOTE (1<<SECTION_MAKERNOTE)
  1304. static char *exif_get_sectionname(int section)
  1305. {
  1306. switch(section) {
  1307. case SECTION_FILE: return "FILE";
  1308. case SECTION_COMPUTED: return "COMPUTED";
  1309. case SECTION_ANY_TAG: return "ANY_TAG";
  1310. case SECTION_IFD0: return "IFD0";
  1311. case SECTION_THUMBNAIL: return "THUMBNAIL";
  1312. case SECTION_COMMENT: return "COMMENT";
  1313. case SECTION_APP0: return "APP0";
  1314. case SECTION_EXIF: return "EXIF";
  1315. case SECTION_FPIX: return "FPIX";
  1316. case SECTION_GPS: return "GPS";
  1317. case SECTION_INTEROP: return "INTEROP";
  1318. case SECTION_APP12: return "APP12";
  1319. case SECTION_WINXP: return "WINXP";
  1320. case SECTION_MAKERNOTE: return "MAKERNOTE";
  1321. }
  1322. return "";
  1323. }
  1324. static tag_table_type exif_get_tag_table(int section)
  1325. {
  1326. switch(section) {
  1327. case SECTION_FILE: return &tag_table_IFD[0];
  1328. case SECTION_COMPUTED: return &tag_table_IFD[0];
  1329. case SECTION_ANY_TAG: return &tag_table_IFD[0];
  1330. case SECTION_IFD0: return &tag_table_IFD[0];
  1331. case SECTION_THUMBNAIL: return &tag_table_IFD[0];
  1332. case SECTION_COMMENT: return &tag_table_IFD[0];
  1333. case SECTION_APP0: return &tag_table_IFD[0];
  1334. case SECTION_EXIF: return &tag_table_IFD[0];
  1335. case SECTION_FPIX: return &tag_table_IFD[0];
  1336. case SECTION_GPS: return &tag_table_GPS[0];
  1337. case SECTION_INTEROP: return &tag_table_IOP[0];
  1338. case SECTION_APP12: return &tag_table_IFD[0];
  1339. case SECTION_WINXP: return &tag_table_IFD[0];
  1340. }
  1341. return &tag_table_IFD[0];
  1342. }
  1343. /* }}} */
  1344. /* {{{ exif_get_sectionlist
  1345. Return list of sectionnames specified by sectionlist. Return value must be freed
  1346. */
  1347. static char *exif_get_sectionlist(int sectionlist TSRMLS_DC)
  1348. {
  1349. int i, len, ml = 0;
  1350. char *sections;
  1351. for(i=0; i<SECTION_COUNT; i++) {
  1352. ml += strlen(exif_get_sectionname(i))+2;
  1353. }
  1354. sections = safe_emalloc(ml, 1, 1);
  1355. sections[0] = '\0';
  1356. len = 0;
  1357. for(i=0; i<SECTION_COUNT; i++) {
  1358. if (sectionlist&(1<<i)) {
  1359. snprintf(sections+len, ml-len, "%s, ", exif_get_sectionname(i));
  1360. len = strlen(sections);
  1361. }
  1362. }
  1363. if (len>2)
  1364. sections[len-2] = '\0';
  1365. return sections;
  1366. }
  1367. /* }}} */
  1368. /* {{{ struct image_info_type
  1369. This structure stores Exif header image elements in a simple manner
  1370. Used to store camera data as extracted from the various ways that it can be
  1371. stored in a nexif header
  1372. */
  1373. typedef struct {
  1374. int type;
  1375. size_t size;
  1376. uchar *data;
  1377. } file_section;
  1378. typedef struct {
  1379. int count;
  1380. file_section *list;
  1381. } file_section_list;
  1382. typedef struct {
  1383. image_filetype filetype;
  1384. size_t width, height;
  1385. size_t size;
  1386. size_t offset;
  1387. char *data;
  1388. } thumbnail_data;
  1389. typedef struct {
  1390. char *value;
  1391. size_t size;
  1392. int tag;
  1393. } xp_field_type;
  1394. typedef struct {
  1395. int count;
  1396. xp_field_type *list;
  1397. } xp_field_list;
  1398. /* This structure is used to store a section of a Jpeg file. */
  1399. typedef struct {
  1400. php_stream *infile;
  1401. char *FileName;
  1402. time_t FileDateTime;
  1403. size_t FileSize;
  1404. image_filetype FileType;
  1405. int Height, Width;
  1406. int IsColor;
  1407. char *make;
  1408. char *model;
  1409. float ApertureFNumber;
  1410. float ExposureTime;
  1411. double FocalplaneUnits;
  1412. float CCDWidth;
  1413. double FocalplaneXRes;
  1414. size_t ExifImageWidth;
  1415. float FocalLength;
  1416. float Distance;
  1417. int motorola_intel; /* 1 Motorola; 0 Intel */
  1418. char *UserComment;
  1419. int UserCommentLength;
  1420. char *UserCommentEncoding;
  1421. char *encode_unicode;
  1422. char *decode_unicode_be;
  1423. char *decode_unicode_le;
  1424. char *encode_jis;
  1425. char *decode_jis_be;
  1426. char *decode_jis_le;
  1427. char *Copyright;/* EXIF standard defines Copyright as "<Photographer> [ '\0' <Editor> ] ['\0']" */
  1428. char *CopyrightPhotographer;
  1429. char *CopyrightEditor;
  1430. xp_field_list xp_fields;
  1431. thumbnail_data Thumbnail;
  1432. /* other */
  1433. int sections_found; /* FOUND_<marker> */
  1434. image_info_list info_list[SECTION_COUNT];
  1435. /* for parsing */
  1436. int read_thumbnail;
  1437. int read_all;
  1438. int ifd_nesting_level;
  1439. /* internal */
  1440. file_section_list file;
  1441. } image_info_type;
  1442. /* }}} */
  1443. /* {{{ exif_error_docref */
  1444. static void exif_error_docref(const char *docref EXIFERR_DC, const image_info_type *ImageInfo, int type, const char *format, ...)
  1445. {
  1446. va_list args;
  1447. va_start(args, format);
  1448. #ifdef EXIF_DEBUG
  1449. {
  1450. char *buf;
  1451. spprintf(&buf, 0, "%s(%d): %s", _file, _line, format);
  1452. php_verror(docref, ImageInfo->FileName?ImageInfo->FileName:"", type, buf, args TSRMLS_CC);
  1453. efree(buf);
  1454. }
  1455. #else
  1456. php_verror(docref, ImageInfo->FileName?ImageInfo->FileName:"", type, format, args TSRMLS_CC);
  1457. #endif
  1458. va_end(args);
  1459. }
  1460. /* }}} */
  1461. /* {{{ jpeg_sof_info
  1462. */
  1463. typedef struct {
  1464. int bits_per_sample;
  1465. size_t width;
  1466. size_t height;
  1467. int num_components;
  1468. } jpeg_sof_info;
  1469. /* }}} */
  1470. /* {{{ exif_file_sections_add
  1471. Add a file_section to image_info
  1472. returns the used block or -1. if size>0 and data == NULL buffer of size is allocated
  1473. */
  1474. static int exif_file_sections_add(image_info_type *ImageInfo, int type, size_t size, uchar *data)
  1475. {
  1476. file_section *tmp;
  1477. int count = ImageInfo->file.count;
  1478. tmp = safe_erealloc(ImageInfo->file.list, (count+1), sizeof(file_section), 0);
  1479. ImageInfo->file.list = tmp;
  1480. ImageInfo->file.list[count].type = 0xFFFF;
  1481. ImageInfo->file.list[count].data = NULL;
  1482. ImageInfo->file.list[count].size = 0;
  1483. ImageInfo->file.count = count+1;
  1484. if (!size) {
  1485. data = NULL;
  1486. } else if (data == NULL) {
  1487. data = safe_emalloc(size, 1, 0);
  1488. }
  1489. ImageInfo->file.list[count].type = type;
  1490. ImageInfo->file.list[count].data = data;
  1491. ImageInfo->file.list[count].size = size;
  1492. return count;
  1493. }
  1494. /* }}} */
  1495. /* {{{ exif_file_sections_realloc
  1496. Reallocate a file section returns 0 on success and -1 on failure
  1497. */
  1498. static int exif_file_sections_realloc(image_info_type *ImageInfo, int section_index, size_t size TSRMLS_DC)
  1499. {
  1500. void *tmp;
  1501. /* This is not a malloc/realloc check. It is a plausibility check for the
  1502. * function parameters (requirements engineering).
  1503. */
  1504. if (section_index >= ImageInfo->file.count) {
  1505. EXIF_ERRLOG_FSREALLOC(ImageInfo)
  1506. return -1;
  1507. }
  1508. tmp = safe_erealloc(ImageInfo->file.list[section_index].data, 1, size, 0);
  1509. ImageInfo->file.list[section_index].data = tmp;
  1510. ImageInfo->file.list[section_index].size = size;
  1511. return 0;
  1512. }
  1513. /* }}} */
  1514. /* {{{ exif_file_section_free
  1515. Discard all file_sections in ImageInfo
  1516. */
  1517. static int exif_file_sections_free(image_info_type *ImageInfo)
  1518. {
  1519. int i;
  1520. if (ImageInfo->file.count) {
  1521. for (i=0; i<ImageInfo->file.count; i++) {
  1522. EFREE_IF(ImageInfo->file.list[i].data);
  1523. }
  1524. }
  1525. EFREE_IF(ImageInfo->file.list);
  1526. ImageInfo->file.count = 0;
  1527. return TRUE;
  1528. }
  1529. /* }}} */
  1530. /* {{{ exif_iif_add_value
  1531. Add a value to image_info
  1532. */
  1533. static void exif_iif_add_value(image_info_type *image_info, int section_index, char *name, int tag, int format, int length, void* value, int motorola_intel TSRMLS_DC)
  1534. {
  1535. size_t idex;
  1536. void *vptr;
  1537. image_info_value *info_value;
  1538. image_info_data *info_data;
  1539. image_info_data *list;
  1540. if (length < 0) {
  1541. return;
  1542. }
  1543. list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
  1544. image_info->info_list[section_index].list = list;
  1545. info_data = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
  1546. memset(info_data, 0, sizeof(image_info_data));
  1547. info_data->tag = tag;
  1548. info_data->format = format;
  1549. info_data->length = length;
  1550. info_data->name = estrdup(name);
  1551. info_value = &info_data->value;
  1552. switch (format) {
  1553. case TAG_FMT_STRING:
  1554. if (value) {
  1555. length = php_strnlen(value, length);
  1556. info_value->s = estrndup(value, length);
  1557. info_data->length = length;
  1558. } else {
  1559. info_data->length = 0;
  1560. info_value->s = estrdup("");
  1561. }
  1562. break;
  1563. default:
  1564. /* Standard says more types possible but skip them...
  1565. * but allow users to handle data if they know how to
  1566. * So not return but use type UNDEFINED
  1567. * return;
  1568. */
  1569. info_data->tag = TAG_FMT_UNDEFINED;/* otherwise not freed from memory */
  1570. case TAG_FMT_SBYTE:
  1571. case TAG_FMT_BYTE:
  1572. /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
  1573. if (!length)
  1574. break;
  1575. case TAG_FMT_UNDEFINED:
  1576. if (value) {
  1577. if (tag == TAG_MAKER_NOTE) {
  1578. length = (int) php_strnlen(value, length);
  1579. }
  1580. /* do not recompute length here */
  1581. info_value->s = estrndup(value, length);
  1582. info_data->length = length;
  1583. } else {
  1584. info_data->length = 0;
  1585. info_value->s = estrdup("");
  1586. }
  1587. break;
  1588. case TAG_FMT_USHORT:
  1589. case TAG_FMT_ULONG:
  1590. case TAG_FMT_URATIONAL:
  1591. case TAG_FMT_SSHORT:
  1592. case TAG_FMT_SLONG:
  1593. case TAG_FMT_SRATIONAL:
  1594. case TAG_FMT_SINGLE:
  1595. case TAG_FMT_DOUBLE:
  1596. if (length==0) {
  1597. break;
  1598. } else
  1599. if (length>1) {
  1600. info_value->list = safe_emalloc(length, sizeof(image_info_value), 0);
  1601. } else {
  1602. info_value = &info_data->value;
  1603. }
  1604. for (idex=0,vptr=value; idex<(size_t)length; idex++,vptr=(char *) vptr + php_tiff_bytes_per_format[format]) {
  1605. if (length>1) {
  1606. info_value = &info_data->value.list[idex];
  1607. }
  1608. switch (format) {
  1609. case TAG_FMT_USHORT:
  1610. info_value->u = php_ifd_get16u(vptr, motorola_intel);
  1611. break;
  1612. case TAG_FMT_ULONG:
  1613. info_value->u = php_ifd_get32u(vptr, motorola_intel);
  1614. break;
  1615. case TAG_FMT_URATIONAL:
  1616. info_value->ur.num = php_ifd_get32u(vptr, motorola_intel);
  1617. info_value->ur.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
  1618. break;
  1619. case TAG_FMT_SSHORT:
  1620. info_value->i = php_ifd_get16s(vptr, motorola_intel);
  1621. break;
  1622. case TAG_FMT_SLONG:
  1623. info_value->i = php_ifd_get32s(vptr, motorola_intel);
  1624. break;
  1625. case TAG_FMT_SRATIONAL:
  1626. info_value->sr.num = php_ifd_get32u(vptr, motorola_intel);
  1627. info_value->sr.den = php_ifd_get32u(4+(char *)vptr, motorola_intel);
  1628. break;
  1629. case TAG_FMT_SINGLE:
  1630. #ifdef EXIF_DEBUG
  1631. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type single");
  1632. #endif
  1633. info_value->f = *(float *)value;
  1634. case TAG_FMT_DOUBLE:
  1635. #ifdef EXIF_DEBUG
  1636. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Found value of type double");
  1637. #endif
  1638. info_value->d = *(double *)value;
  1639. break;
  1640. }
  1641. }
  1642. }
  1643. image_info->sections_found |= 1<<section_index;
  1644. image_info->info_list[section_index].count++;
  1645. }
  1646. /* }}} */
  1647. /* {{{ exif_iif_add_tag
  1648. Add a tag from IFD to image_info
  1649. */
  1650. static void exif_iif_add_tag(image_info_type *image_info, int section_index, char *name, int tag, int format, size_t length, void* value TSRMLS_DC)
  1651. {
  1652. exif_iif_add_value(image_info, section_index, name, tag, format, (int)length, value, image_info->motorola_intel TSRMLS_CC);
  1653. }
  1654. /* }}} */
  1655. /* {{{ exif_iif_add_int
  1656. Add an int value to image_info
  1657. */
  1658. static void exif_iif_add_int(image_info_type *image_info, int section_index, char *name, int value TSRMLS_DC)
  1659. {
  1660. image_info_data *info_data;
  1661. image_info_data *list;
  1662. list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
  1663. image_info->info_list[section_index].list = list;
  1664. info_data = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
  1665. info_data->tag = TAG_NONE;
  1666. info_data->format = TAG_FMT_SLONG;
  1667. info_data->length = 1;
  1668. info_data->name = estrdup(name);
  1669. info_data->value.i = value;
  1670. image_info->sections_found |= 1<<section_index;
  1671. image_info->info_list[section_index].count++;
  1672. }
  1673. /* }}} */
  1674. /* {{{ exif_iif_add_str
  1675. Add a string value to image_info MUST BE NUL TERMINATED
  1676. */
  1677. static void exif_iif_add_str(image_info_type *image_info, int section_index, char *name, char *value TSRMLS_DC)
  1678. {
  1679. image_info_data *info_data;
  1680. image_info_data *list;
  1681. if (value) {
  1682. list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
  1683. image_info->info_list[section_index].list = list;
  1684. info_data = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
  1685. info_data->tag = TAG_NONE;
  1686. info_data->format = TAG_FMT_STRING;
  1687. info_data->length = 1;
  1688. info_data->name = estrdup(name);
  1689. info_data->value.s = estrdup(value);
  1690. image_info->sections_found |= 1<<section_index;
  1691. image_info->info_list[section_index].count++;
  1692. }
  1693. }
  1694. /* }}} */
  1695. /* {{{ exif_iif_add_fmt
  1696. Add a format string value to image_info MUST BE NUL TERMINATED
  1697. */
  1698. static void exif_iif_add_fmt(image_info_type *image_info, int section_index, char *name TSRMLS_DC, char *value, ...)
  1699. {
  1700. char *tmp;
  1701. va_list arglist;
  1702. va_start(arglist, value);
  1703. if (value) {
  1704. vspprintf(&tmp, 0, value, arglist);
  1705. exif_iif_add_str(image_info, section_index, name, tmp TSRMLS_CC);
  1706. efree(tmp);
  1707. }
  1708. va_end(arglist);
  1709. }
  1710. /* }}} */
  1711. /* {{{ exif_iif_add_str
  1712. Add a string value to image_info MUST BE NUL TERMINATED
  1713. */
  1714. static void exif_iif_add_buffer(image_info_type *image_info, int section_index, char *name, int length, char *value TSRMLS_DC)
  1715. {
  1716. image_info_data *info_data;
  1717. image_info_data *list;
  1718. if (value) {
  1719. list = safe_erealloc(image_info->info_list[section_index].list, (image_info->info_list[section_index].count+1), sizeof(image_info_data), 0);
  1720. image_info->info_list[section_index].list = list;
  1721. info_data = &image_info->info_list[section_index].list[image_info->info_list[section_index].count];
  1722. info_data->tag = TAG_NONE;
  1723. info_data->format = TAG_FMT_UNDEFINED;
  1724. info_data->length = length;
  1725. info_data->name = estrdup(name);
  1726. info_data->value.s = safe_emalloc(length, 1, 1);
  1727. memcpy(info_data->value.s, value, length);
  1728. info_data->value.s[length] = 0;
  1729. image_info->sections_found |= 1<<section_index;
  1730. image_info->info_list[section_index].count++;
  1731. }
  1732. }
  1733. /* }}} */
  1734. /* {{{ exif_iif_free
  1735. Free memory allocated for image_info
  1736. */
  1737. static void exif_iif_free(image_info_type *image_info, int section_index) {
  1738. int i;
  1739. void *f; /* faster */
  1740. if (image_info->info_list[section_index].count) {
  1741. for (i=0; i < image_info->info_list[section_index].count; i++) {
  1742. if ((f=image_info->info_list[section_index].list[i].name) != NULL) {
  1743. efree(f);
  1744. }
  1745. switch(image_info->info_list[section_index].list[i].format) {
  1746. case TAG_FMT_SBYTE:
  1747. case TAG_FMT_BYTE:
  1748. /* in contrast to strings bytes do not need to allocate buffer for NULL if length==0 */
  1749. if (image_info->info_list[section_index].list[i].length<1)
  1750. break;
  1751. default:
  1752. case TAG_FMT_UNDEFINED:
  1753. case TAG_FMT_STRING:
  1754. if ((f=image_info->info_list[section_index].list[i].value.s) != NULL) {
  1755. efree(f);
  1756. }
  1757. break;
  1758. case TAG_FMT_USHORT:
  1759. case TAG_FMT_ULONG:
  1760. case TAG_FMT_URATIONAL:
  1761. case TAG_FMT_SSHORT:
  1762. case TAG_FMT_SLONG:
  1763. case TAG_FMT_SRATIONAL:
  1764. case TAG_FMT_SINGLE:
  1765. case TAG_FMT_DOUBLE:
  1766. /* nothing to do here */
  1767. if (image_info->info_list[section_index].list[i].length > 1) {
  1768. if ((f=image_info->info_list[section_index].list[i].value.list) != NULL) {
  1769. efree(f);
  1770. }
  1771. }
  1772. break;
  1773. }
  1774. }
  1775. }
  1776. EFREE_IF(image_info->info_list[section_index].list);
  1777. }
  1778. /* }}} */
  1779. /* {{{ add_assoc_image_info
  1780. * Add image_info to associative array value. */
  1781. static void add_assoc_image_info(zval *value, int sub_array, image_info_type *image_info, int section_index TSRMLS_DC)
  1782. {
  1783. char buffer[64], *val, *name, uname[64];
  1784. int i, ap, l, b, idx=0, unknown=0;
  1785. #ifdef EXIF_DEBUG
  1786. int info_tag;
  1787. #endif
  1788. image_info_value *info_value;
  1789. image_info_data *info_data;
  1790. zval *tmpi, *array = NULL;
  1791. #ifdef EXIF_DEBUG
  1792. /* php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Adding %d infos from section %s", image_info->info_list[section_index].count, exif_get_sectionname(section_index));*/
  1793. #endif
  1794. if (image_info->info_list[section_index].count) {
  1795. if (sub_array) {
  1796. MAKE_STD_ZVAL(tmpi);
  1797. array_init(tmpi);
  1798. } else {
  1799. tmpi = value;
  1800. }
  1801. for(i=0; i<image_info->info_list[section_index].count; i++) {
  1802. info_data = &image_info->info_list[section_index].list[i];
  1803. #ifdef EXIF_DEBUG
  1804. info_tag = info_data->tag; /* conversion */
  1805. #endif
  1806. info_value = &info_data->value;
  1807. if (!(name = info_data->name)) {
  1808. snprintf(uname, sizeof(uname), "%d", unknown++);
  1809. name = uname;
  1810. }
  1811. #ifdef EXIF_DEBUG
  1812. /* php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Adding infos: tag(0x%04X,%12s,L=0x%04X): %s", info_tag, exif_get_tagname(info_tag, buffer, -12, exif_get_tag_table(section_index) TSRMLS_CC), info_data->length, info_data->format==TAG_FMT_STRING?(info_value&&info_value->s?info_value->s:"<no data>"):exif_get_tagformat(info_data->format));*/
  1813. #endif
  1814. if (info_data->length==0) {
  1815. add_assoc_null(tmpi, name);
  1816. } else {
  1817. switch (info_data->format) {
  1818. default:
  1819. /* Standard says more types possible but skip them...
  1820. * but allow users to handle data if they know how to
  1821. * So not return but use type UNDEFINED
  1822. * return;
  1823. */
  1824. case TAG_FMT_BYTE:
  1825. case TAG_FMT_SBYTE:
  1826. case TAG_FMT_UNDEFINED:
  1827. if (!info_value->s) {
  1828. add_assoc_stringl(tmpi, name, "", 0, 1);
  1829. } else {
  1830. add_assoc_stringl(tmpi, name, info_value->s, info_data->length, 1);
  1831. }
  1832. break;
  1833. case TAG_FMT_STRING:
  1834. if (!(val = info_value->s)) {
  1835. val = "";
  1836. }
  1837. if (section_index==SECTION_COMMENT) {
  1838. add_index_string(tmpi, idx++, val, 1);
  1839. } else {
  1840. add_assoc_string(tmpi, name, val, 1);
  1841. }
  1842. break;
  1843. case TAG_FMT_URATIONAL:
  1844. case TAG_FMT_SRATIONAL:
  1845. /*case TAG_FMT_BYTE:
  1846. case TAG_FMT_SBYTE:*/
  1847. case TAG_FMT_USHORT:
  1848. case TAG_FMT_SSHORT:
  1849. case TAG_FMT_SINGLE:
  1850. case TAG_FMT_DOUBLE:
  1851. case TAG_FMT_ULONG:
  1852. case TAG_FMT_SLONG:
  1853. /* now the rest, first see if it becomes an array */
  1854. if ((l = info_data->length) > 1) {
  1855. array = NULL;
  1856. MAKE_STD_ZVAL(array);
  1857. array_init(array);
  1858. }
  1859. for(ap=0; ap<l; ap++) {
  1860. if (l>1) {
  1861. info_value = &info_data->value.list[ap];
  1862. }
  1863. switch (info_data->format) {
  1864. case TAG_FMT_BYTE:
  1865. if (l>1) {
  1866. info_value = &info_data->value;
  1867. for (b=0;b<l;b++) {
  1868. add_index_long(array, b, (int)(info_value->s[b]));
  1869. }
  1870. break;
  1871. }
  1872. case TAG_FMT_USHORT:
  1873. case TAG_FMT_ULONG:
  1874. if (l==1) {
  1875. add_assoc_long(tmpi, name, (int)info_value->u);
  1876. } else {
  1877. add_index_long(array, ap, (int)info_value->u);
  1878. }
  1879. break;
  1880. case TAG_FMT_URATIONAL:
  1881. snprintf(buffer, sizeof(buffer), "%i/%i", info_value->ur.num, info_value->ur.den);
  1882. if (l==1) {
  1883. add_assoc_string(tmpi, name, buffer, 1);
  1884. } else {
  1885. add_index_string(array, ap, buffer, 1);
  1886. }
  1887. break;
  1888. case TAG_FMT_SBYTE:
  1889. if (l>1) {
  1890. info_value = &info_data->value;
  1891. for (b=0;b<l;b++) {
  1892. add_index_long(array, ap, (int)info_value->s[b]);
  1893. }
  1894. break;
  1895. }
  1896. case TAG_FMT_SSHORT:
  1897. case TAG_FMT_SLONG:
  1898. if (l==1) {
  1899. add_assoc_long(tmpi, name, info_value->i);
  1900. } else {
  1901. add_index_long(array, ap, info_value->i);
  1902. }
  1903. break;
  1904. case TAG_FMT_SRATIONAL:
  1905. snprintf(buffer, sizeof(buffer), "%i/%i", info_value->sr.num, info_value->sr.den);
  1906. if (l==1) {
  1907. add_assoc_string(tmpi, name, buffer, 1);
  1908. } else {
  1909. add_index_string(array, ap, buffer, 1);
  1910. }
  1911. break;
  1912. case TAG_FMT_SINGLE:
  1913. if (l==1) {
  1914. add_assoc_double(tmpi, name, info_value->f);
  1915. } else {
  1916. add_index_double(array, ap, info_value->f);
  1917. }
  1918. break;
  1919. case TAG_FMT_DOUBLE:
  1920. if (l==1) {
  1921. add_assoc_double(tmpi, name, info_value->d);
  1922. } else {
  1923. add_index_double(array, ap, info_value->d);
  1924. }
  1925. break;
  1926. }
  1927. info_value = &info_data->value.list[ap];
  1928. }
  1929. if (l>1) {
  1930. add_assoc_zval(tmpi, name, array);
  1931. }
  1932. break;
  1933. }
  1934. }
  1935. }
  1936. if (sub_array) {
  1937. add_assoc_zval(value, exif_get_sectionname(section_index), tmpi);
  1938. }
  1939. }
  1940. }
  1941. /* }}} */
  1942. /* {{{ Markers
  1943. JPEG markers consist of one or more 0xFF bytes, followed by a marker
  1944. code byte (which is not an FF). Here are the marker codes of interest
  1945. in this program. (See jdmarker.c for a more complete list.)
  1946. */
  1947. #define M_TEM 0x01 /* temp for arithmetic coding */
  1948. #define M_RES 0x02 /* reserved */
  1949. #define M_SOF0 0xC0 /* Start Of Frame N */
  1950. #define M_SOF1 0xC1 /* N indicates which compression process */
  1951. #define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
  1952. #define M_SOF3 0xC3
  1953. #define M_DHT 0xC4
  1954. #define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
  1955. #define M_SOF6 0xC6
  1956. #define M_SOF7 0xC7
  1957. #define M_JPEG 0x08 /* reserved for extensions */
  1958. #define M_SOF9 0xC9
  1959. #define M_SOF10 0xCA
  1960. #define M_SOF11 0xCB
  1961. #define M_DAC 0xCC /* arithmetic table */
  1962. #define M_SOF13 0xCD
  1963. #define M_SOF14 0xCE
  1964. #define M_SOF15 0xCF
  1965. #define M_RST0 0xD0 /* restart segment */
  1966. #define M_RST1 0xD1
  1967. #define M_RST2 0xD2
  1968. #define M_RST3 0xD3
  1969. #define M_RST4 0xD4
  1970. #define M_RST5 0xD5
  1971. #define M_RST6 0xD6
  1972. #define M_RST7 0xD7
  1973. #define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
  1974. #define M_EOI 0xD9 /* End Of Image (end of datastream) */
  1975. #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
  1976. #define M_DQT 0xDB
  1977. #define M_DNL 0xDC
  1978. #define M_DRI 0xDD
  1979. #define M_DHP 0xDE
  1980. #define M_EXP 0xDF
  1981. #define M_APP0 0xE0 /* JPEG: 'JFIFF' AND (additional 'JFXX') */
  1982. #define M_EXIF 0xE1 /* Exif Attribute Information */
  1983. #define M_APP2 0xE2 /* Flash Pix Extension Data? */
  1984. #define M_APP3 0xE3
  1985. #define M_APP4 0xE4
  1986. #define M_APP5 0xE5
  1987. #define M_APP6 0xE6
  1988. #define M_APP7 0xE7
  1989. #define M_APP8 0xE8
  1990. #define M_APP9 0xE9
  1991. #define M_APP10 0xEA
  1992. #define M_APP11 0xEB
  1993. #define M_APP12 0xEC
  1994. #define M_APP13 0xED /* IPTC International Press Telecommunications Council */
  1995. #define M_APP14 0xEE /* Software, Copyright? */
  1996. #define M_APP15 0xEF
  1997. #define M_JPG0 0xF0
  1998. #define M_JPG1 0xF1
  1999. #define M_JPG2 0xF2
  2000. #define M_JPG3 0xF3
  2001. #define M_JPG4 0xF4
  2002. #define M_JPG5 0xF5
  2003. #define M_JPG6 0xF6
  2004. #define M_JPG7 0xF7
  2005. #define M_JPG8 0xF8
  2006. #define M_JPG9 0xF9
  2007. #define M_JPG10 0xFA
  2008. #define M_JPG11 0xFB
  2009. #define M_JPG12 0xFC
  2010. #define M_JPG13 0xFD
  2011. #define M_COM 0xFE /* COMment */
  2012. #define M_PSEUDO 0x123 /* Extra value. */
  2013. /* }}} */
  2014. /* {{{ jpeg2000 markers
  2015. */
  2016. /* Markers x30 - x3F do not have a segment */
  2017. /* Markers x00, x01, xFE, xC0 - xDF ISO/IEC 10918-1 -> M_<xx> */
  2018. /* Markers xF0 - xF7 ISO/IEC 10918-3 */
  2019. /* Markers xF7 - xF8 ISO/IEC 14495-1 */
  2020. /* XY=Main/Tile-header:(R:required, N:not_allowed, O:optional, L:last_marker) */
  2021. #define JC_SOC 0x4F /* NN, Start of codestream */
  2022. #define JC_SIZ 0x51 /* RN, Image and tile size */
  2023. #define JC_COD 0x52 /* RO, Codeing style defaulte */
  2024. #define JC_COC 0x53 /* OO, Coding style component */
  2025. #define JC_TLM 0x55 /* ON, Tile part length main header */
  2026. #define JC_PLM 0x57 /* ON, Packet length main header */
  2027. #define JC_PLT 0x58 /* NO, Packet length tile part header */
  2028. #define JC_QCD 0x5C /* RO, Quantization default */
  2029. #define JC_QCC 0x5D /* OO, Quantization component */
  2030. #define JC_RGN 0x5E /* OO, Region of interest */
  2031. #define JC_POD 0x5F /* OO, Progression order default */
  2032. #define JC_PPM 0x60 /* ON, Packed packet headers main header */
  2033. #define JC_PPT 0x61 /* NO, Packet packet headers tile part header */
  2034. #define JC_CME 0x64 /* OO, Comment: "LL E <text>" E=0:binary, E=1:ascii */
  2035. #define JC_SOT 0x90 /* NR, Start of tile */
  2036. #define JC_SOP 0x91 /* NO, Start of packeter default */
  2037. #define JC_EPH 0x92 /* NO, End of packet header */
  2038. #define JC_SOD 0x93 /* NL, Start of data */
  2039. #define JC_EOC 0xD9 /* NN, End of codestream */
  2040. /* }}} */
  2041. /* {{{ exif_process_COM
  2042. Process a COM marker.
  2043. We want to print out the marker contents as legible text;
  2044. we must guard against random junk and varying newline representations.
  2045. */
  2046. static void exif_process_COM (image_info_type *image_info, char *value, size_t length TSRMLS_DC)
  2047. {
  2048. exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length-2, value+2 TSRMLS_CC);
  2049. }
  2050. /* }}} */
  2051. /* {{{ exif_process_CME
  2052. Process a CME marker.
  2053. We want to print out the marker contents as legible text;
  2054. we must guard against random junk and varying newline representations.
  2055. */
  2056. #ifdef EXIF_JPEG2000
  2057. static void exif_process_CME (image_info_type *image_info, char *value, size_t length TSRMLS_DC)
  2058. {
  2059. if (length>3) {
  2060. switch(value[2]) {
  2061. case 0:
  2062. exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, length, value TSRMLS_CC);
  2063. break;
  2064. case 1:
  2065. exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_STRING, length, value);
  2066. break;
  2067. default:
  2068. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined JPEG2000 comment encoding");
  2069. break;
  2070. }
  2071. } else {
  2072. exif_iif_add_tag(image_info, SECTION_COMMENT, "Comment", TAG_COMPUTED_VALUE, TAG_FMT_UNDEFINED, 0, NULL);
  2073. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "JPEG2000 comment section too small");
  2074. }
  2075. }
  2076. #endif
  2077. /* }}} */
  2078. /* {{{ exif_process_SOFn
  2079. * Process a SOFn marker. This is useful for the image dimensions */
  2080. static void exif_process_SOFn (uchar *Data, int marker, jpeg_sof_info *result)
  2081. {
  2082. /* 0xFF SOSn SectLen(2) Bits(1) Height(2) Width(2) Channels(1) 3*Channels (1) */
  2083. result->bits_per_sample = Data[2];
  2084. result->height = php_jpg_get16(Data+3);
  2085. result->width = php_jpg_get16(Data+5);
  2086. result->num_components = Data[7];
  2087. /* switch (marker) {
  2088. case M_SOF0: process = "Baseline"; break;
  2089. case M_SOF1: process = "Extended sequential"; break;
  2090. case M_SOF2: process = "Progressive"; break;
  2091. case M_SOF3: process = "Lossless"; break;
  2092. case M_SOF5: process = "Differential sequential"; break;
  2093. case M_SOF6: process = "Differential progressive"; break;
  2094. case M_SOF7: process = "Differential lossless"; break;
  2095. case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
  2096. case M_SOF10: process = "Progressive, arithmetic coding"; break;
  2097. case M_SOF11: process = "Lossless, arithmetic coding"; break;
  2098. case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
  2099. case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
  2100. case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
  2101. default: process = "Unknown"; break;
  2102. }*/
  2103. }
  2104. /* }}} */
  2105. /* forward declarations */
  2106. static int exif_process_IFD_in_JPEG(image_info_type *ImageInfo, char *dir_start, char *offset_base, size_t IFDlength, size_t displacement, int section_index TSRMLS_DC);
  2107. static int exif_process_IFD_TAG( image_info_type *ImageInfo, char *dir_entry, char *offset_base, size_t IFDlength, size_t displacement, int section_index, int ReadNextIFD, tag_table_type tag_table TSRMLS_DC);
  2108. /* {{{ exif_get_markername
  2109. Get name of marker */
  2110. #ifdef EXIF_DEBUG
  2111. static char * exif_get_markername(int marker)
  2112. {
  2113. switch(marker) {
  2114. case 0xC0: return "SOF0";
  2115. case 0xC1: return "SOF1";
  2116. case 0xC2: return "SOF2";
  2117. case 0xC3: return "SOF3";
  2118. case 0xC4: return "DHT";
  2119. case 0xC5: return "SOF5";
  2120. case 0xC6: return "SOF6";
  2121. case 0xC7: return "SOF7";
  2122. case 0xC9: return "SOF9";
  2123. case 0xCA: return "SOF10";
  2124. case 0xCB: return "SOF11";
  2125. case 0xCD: return "SOF13";
  2126. case 0xCE: return "SOF14";
  2127. case 0xCF: return "SOF15";
  2128. case 0xD8: return "SOI";
  2129. case 0xD9: return "EOI";
  2130. case 0xDA: return "SOS";
  2131. case 0xDB: return "DQT";
  2132. case 0xDC: return "DNL";
  2133. case 0xDD: return "DRI";
  2134. case 0xDE: return "DHP";
  2135. case 0xDF: return "EXP";
  2136. case 0xE0: return "APP0";
  2137. case 0xE1: return "EXIF";
  2138. case 0xE2: return "FPIX";
  2139. case 0xE3: return "APP3";
  2140. case 0xE4: return "APP4";
  2141. case 0xE5: return "APP5";
  2142. case 0xE6: return "APP6";
  2143. case 0xE7: return "APP7";
  2144. case 0xE8: return "APP8";
  2145. case 0xE9: return "APP9";
  2146. case 0xEA: return "APP10";
  2147. case 0xEB: return "APP11";
  2148. case 0xEC: return "APP12";
  2149. case 0xED: return "APP13";
  2150. case 0xEE: return "APP14";
  2151. case 0xEF: return "APP15";
  2152. case 0xF0: return "JPG0";
  2153. case 0xFD: return "JPG13";
  2154. case 0xFE: return "COM";
  2155. case 0x01: return "TEM";
  2156. }
  2157. return "Unknown";
  2158. }
  2159. #endif
  2160. /* }}} */
  2161. /* {{{ proto string exif_tagname(index)
  2162. Get headername for index or false if not defined */
  2163. PHP_FUNCTION(exif_tagname)
  2164. {
  2165. long tag;
  2166. char *szTemp;
  2167. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag) == FAILURE) {
  2168. return;
  2169. }
  2170. szTemp = exif_get_tagname(tag, NULL, 0, tag_table_IFD TSRMLS_CC);
  2171. if (tag < 0 || !szTemp || !szTemp[0]) {
  2172. RETURN_FALSE;
  2173. }
  2174. RETURN_STRING(szTemp, 1)
  2175. }
  2176. /* }}} */
  2177. /* {{{ exif_ifd_make_value
  2178. * Create a value for an ifd from an info_data pointer */
  2179. static void* exif_ifd_make_value(image_info_data *info_data, int motorola_intel TSRMLS_DC) {
  2180. size_t byte_count;
  2181. char *value_ptr, *data_ptr;
  2182. size_t i;
  2183. image_info_value *info_value;
  2184. byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
  2185. value_ptr = safe_emalloc(max(byte_count, 4), 1, 0);
  2186. memset(value_ptr, 0, 4);
  2187. if (!info_data->length) {
  2188. return value_ptr;
  2189. }
  2190. if (info_data->format == TAG_FMT_UNDEFINED || info_data->format == TAG_FMT_STRING
  2191. || (byte_count>1 && (info_data->format == TAG_FMT_BYTE || info_data->format == TAG_FMT_SBYTE))
  2192. ) {
  2193. memmove(value_ptr, info_data->value.s, byte_count);
  2194. return value_ptr;
  2195. } else if (info_data->format == TAG_FMT_BYTE) {
  2196. *value_ptr = info_data->value.u;
  2197. return value_ptr;
  2198. } else if (info_data->format == TAG_FMT_SBYTE) {
  2199. *value_ptr = info_data->value.i;
  2200. return value_ptr;
  2201. } else {
  2202. data_ptr = value_ptr;
  2203. for(i=0; i<info_data->length; i++) {
  2204. if (info_data->length==1) {
  2205. info_value = &info_data->value;
  2206. } else {
  2207. info_value = &info_data->value.list[i];
  2208. }
  2209. switch(info_data->format) {
  2210. case TAG_FMT_USHORT:
  2211. php_ifd_set16u(data_ptr, info_value->u, motorola_intel);
  2212. data_ptr += 2;
  2213. break;
  2214. case TAG_FMT_ULONG:
  2215. php_ifd_set32u(data_ptr, info_value->u, motorola_intel);
  2216. data_ptr += 4;
  2217. break;
  2218. case TAG_FMT_SSHORT:
  2219. php_ifd_set16u(data_ptr, info_value->i, motorola_intel);
  2220. data_ptr += 2;
  2221. break;
  2222. case TAG_FMT_SLONG:
  2223. php_ifd_set32u(data_ptr, info_value->i, motorola_intel);
  2224. data_ptr += 4;
  2225. break;
  2226. case TAG_FMT_URATIONAL:
  2227. php_ifd_set32u(data_ptr, info_value->sr.num, motorola_intel);
  2228. php_ifd_set32u(data_ptr+4, info_value->sr.den, motorola_intel);
  2229. data_ptr += 8;
  2230. break;
  2231. case TAG_FMT_SRATIONAL:
  2232. php_ifd_set32u(data_ptr, info_value->ur.num, motorola_intel);
  2233. php_ifd_set32u(data_ptr+4, info_value->ur.den, motorola_intel);
  2234. data_ptr += 8;
  2235. break;
  2236. case TAG_FMT_SINGLE:
  2237. memmove(data_ptr, &info_value->f, 4);
  2238. data_ptr += 4;
  2239. break;
  2240. case TAG_FMT_DOUBLE:
  2241. memmove(data_ptr, &info_value->d, 8);
  2242. data_ptr += 8;
  2243. break;
  2244. }
  2245. }
  2246. }
  2247. return value_ptr;
  2248. }
  2249. /* }}} */
  2250. /* {{{ exif_thumbnail_build
  2251. * Check and build thumbnail */
  2252. static void exif_thumbnail_build(image_info_type *ImageInfo TSRMLS_DC) {
  2253. size_t new_size, new_move, new_value;
  2254. char *new_data;
  2255. void *value_ptr;
  2256. int i, byte_count;
  2257. image_info_list *info_list;
  2258. image_info_data *info_data;
  2259. #ifdef EXIF_DEBUG
  2260. char tagname[64];
  2261. #endif
  2262. if (!ImageInfo->read_thumbnail || !ImageInfo->Thumbnail.offset || !ImageInfo->Thumbnail.size) {
  2263. return; /* ignore this call */
  2264. }
  2265. #ifdef EXIF_DEBUG
  2266. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: filetype = %d", ImageInfo->Thumbnail.filetype);
  2267. #endif
  2268. switch(ImageInfo->Thumbnail.filetype) {
  2269. default:
  2270. case IMAGE_FILETYPE_JPEG:
  2271. /* done */
  2272. break;
  2273. case IMAGE_FILETYPE_TIFF_II:
  2274. case IMAGE_FILETYPE_TIFF_MM:
  2275. info_list = &ImageInfo->info_list[SECTION_THUMBNAIL];
  2276. new_size = 8 + 2 + info_list->count * 12 + 4;
  2277. #ifdef EXIF_DEBUG
  2278. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size of signature + directory(%d): 0x%02X", info_list->count, new_size);
  2279. #endif
  2280. new_value= new_size; /* offset for ifd values outside ifd directory */
  2281. for (i=0; i<info_list->count; i++) {
  2282. info_data = &info_list->list[i];
  2283. byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
  2284. if (byte_count > 4) {
  2285. new_size += byte_count;
  2286. }
  2287. }
  2288. new_move = new_size;
  2289. new_data = safe_erealloc(ImageInfo->Thumbnail.data, 1, ImageInfo->Thumbnail.size, new_size);
  2290. ImageInfo->Thumbnail.data = new_data;
  2291. memmove(ImageInfo->Thumbnail.data + new_move, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
  2292. ImageInfo->Thumbnail.size += new_size;
  2293. /* fill in data */
  2294. if (ImageInfo->motorola_intel) {
  2295. memmove(new_data, "MM\x00\x2a\x00\x00\x00\x08", 8);
  2296. } else {
  2297. memmove(new_data, "II\x2a\x00\x08\x00\x00\x00", 8);
  2298. }
  2299. new_data += 8;
  2300. php_ifd_set16u(new_data, info_list->count, ImageInfo->motorola_intel);
  2301. new_data += 2;
  2302. for (i=0; i<info_list->count; i++) {
  2303. info_data = &info_list->list[i];
  2304. byte_count = php_tiff_bytes_per_format[info_data->format] * info_data->length;
  2305. #ifdef EXIF_DEBUG
  2306. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: process tag(x%04X=%s): %s%s (%d bytes)", info_data->tag, exif_get_tagname(info_data->tag, tagname, -12, tag_table_IFD TSRMLS_CC), (info_data->length>1)&&info_data->format!=TAG_FMT_UNDEFINED&&info_data->format!=TAG_FMT_STRING?"ARRAY OF ":"", exif_get_tagformat(info_data->format), byte_count);
  2307. #endif
  2308. if (info_data->tag==TAG_STRIP_OFFSETS || info_data->tag==TAG_JPEG_INTERCHANGE_FORMAT) {
  2309. php_ifd_set16u(new_data + 0, info_data->tag, ImageInfo->motorola_intel);
  2310. php_ifd_set16u(new_data + 2, TAG_FMT_ULONG, ImageInfo->motorola_intel);
  2311. php_ifd_set32u(new_data + 4, 1, ImageInfo->motorola_intel);
  2312. php_ifd_set32u(new_data + 8, new_move, ImageInfo->motorola_intel);
  2313. } else {
  2314. php_ifd_set16u(new_data + 0, info_data->tag, ImageInfo->motorola_intel);
  2315. php_ifd_set16u(new_data + 2, info_data->format, ImageInfo->motorola_intel);
  2316. php_ifd_set32u(new_data + 4, info_data->length, ImageInfo->motorola_intel);
  2317. value_ptr = exif_ifd_make_value(info_data, ImageInfo->motorola_intel TSRMLS_CC);
  2318. if (byte_count <= 4) {
  2319. memmove(new_data+8, value_ptr, 4);
  2320. } else {
  2321. php_ifd_set32u(new_data+8, new_value, ImageInfo->motorola_intel);
  2322. #ifdef EXIF_DEBUG
  2323. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: writing with value offset: 0x%04X + 0x%02X", new_value, byte_count);
  2324. #endif
  2325. memmove(ImageInfo->Thumbnail.data+new_value, value_ptr, byte_count);
  2326. new_value += byte_count;
  2327. }
  2328. efree(value_ptr);
  2329. }
  2330. new_data += 12;
  2331. }
  2332. memset(new_data, 0, 4); /* next ifd pointer */
  2333. #ifdef EXIF_DEBUG
  2334. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: created");
  2335. #endif
  2336. break;
  2337. }
  2338. }
  2339. /* }}} */
  2340. /* {{{ exif_thumbnail_extract
  2341. * Grab the thumbnail, corrected */
  2342. static void exif_thumbnail_extract(image_info_type *ImageInfo, char *offset, size_t length TSRMLS_DC) {
  2343. if (ImageInfo->Thumbnail.data) {
  2344. exif_error_docref("exif_read_data#error_mult_thumb" EXIFERR_CC, ImageInfo, E_WARNING, "Multiple possible thumbnails");
  2345. return; /* Should not happen */
  2346. }
  2347. if (!ImageInfo->read_thumbnail) {
  2348. return; /* ignore this call */
  2349. }
  2350. /* according to exif2.1, the thumbnail is not supposed to be greater than 64K */
  2351. if (ImageInfo->Thumbnail.size >= 65536
  2352. || ImageInfo->Thumbnail.size <= 0
  2353. || ImageInfo->Thumbnail.offset <= 0
  2354. ) {
  2355. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Illegal thumbnail size/offset");
  2356. return;
  2357. }
  2358. /* Check to make sure we are not going to go past the ExifLength */
  2359. if (ImageInfo->Thumbnail.size > length
  2360. || (ImageInfo->Thumbnail.offset + ImageInfo->Thumbnail.size) > length
  2361. || ImageInfo->Thumbnail.offset > length - ImageInfo->Thumbnail.size
  2362. ) {
  2363. EXIF_ERRLOG_THUMBEOF(ImageInfo)
  2364. return;
  2365. }
  2366. ImageInfo->Thumbnail.data = estrndup(offset + ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
  2367. exif_thumbnail_build(ImageInfo TSRMLS_CC);
  2368. }
  2369. /* }}} */
  2370. /* {{{ exif_process_undefined
  2371. * Copy a string/buffer in Exif header to a character string and return length of allocated buffer if any. */
  2372. static int exif_process_undefined(char **result, char *value, size_t byte_count TSRMLS_DC) {
  2373. /* we cannot use strlcpy - here the problem is that we have to copy NUL
  2374. * chars up to byte_count, we also have to add a single NUL character to
  2375. * force end of string.
  2376. * estrndup does not return length
  2377. */
  2378. if (byte_count) {
  2379. (*result) = estrndup(value, byte_count); /* NULL @ byte_count!!! */
  2380. return byte_count+1;
  2381. }
  2382. return 0;
  2383. }
  2384. /* }}} */
  2385. /* {{{ exif_process_string_raw
  2386. * Copy a string in Exif header to a character string returns length of allocated buffer if any. */
  2387. static int exif_process_string_raw(char **result, char *value, size_t byte_count) {
  2388. /* we cannot use strlcpy - here the problem is that we have to copy NUL
  2389. * chars up to byte_count, we also have to add a single NUL character to
  2390. * force end of string.
  2391. */
  2392. if (byte_count) {
  2393. (*result) = safe_emalloc(byte_count, 1, 1);
  2394. memcpy(*result, value, byte_count);
  2395. (*result)[byte_count] = '\0';
  2396. return byte_count+1;
  2397. }
  2398. return 0;
  2399. }
  2400. /* }}} */
  2401. /* {{{ exif_process_string
  2402. * Copy a string in Exif header to a character string and return length of allocated buffer if any.
  2403. * In contrast to exif_process_string this function does always return a string buffer */
  2404. static int exif_process_string(char **result, char *value, size_t byte_count TSRMLS_DC) {
  2405. /* we cannot use strlcpy - here the problem is that we cannot use strlen to
  2406. * determin length of string and we cannot use strlcpy with len=byte_count+1
  2407. * because then we might get into an EXCEPTION if we exceed an allocated
  2408. * memory page...so we use php_strnlen in conjunction with memcpy and add the NUL
  2409. * char.
  2410. * estrdup would sometimes allocate more memory and does not return length
  2411. */
  2412. if ((byte_count=php_strnlen(value, byte_count)) > 0) {
  2413. return exif_process_undefined(result, value, byte_count TSRMLS_CC);
  2414. }
  2415. (*result) = estrndup("", 1); /* force empty string */
  2416. return byte_count+1;
  2417. }
  2418. /* }}} */
  2419. /* {{{ exif_process_user_comment
  2420. * Process UserComment in IFD. */
  2421. static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount TSRMLS_DC)
  2422. {
  2423. int a;
  2424. char *decode;
  2425. size_t len;;
  2426. *pszEncoding = NULL;
  2427. /* Copy the comment */
  2428. if (ByteCount>=8) {
  2429. const zend_encoding *from, *to;
  2430. if (!memcmp(szValuePtr, "UNICODE\0", 8)) {
  2431. *pszEncoding = estrdup((const char*)szValuePtr);
  2432. szValuePtr = szValuePtr+8;
  2433. ByteCount -= 8;
  2434. /* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16)
  2435. * since we have no encoding support for the BOM yet we skip that.
  2436. */
  2437. if (!memcmp(szValuePtr, "\xFE\xFF", 2)) {
  2438. decode = "UCS-2BE";
  2439. szValuePtr = szValuePtr+2;
  2440. ByteCount -= 2;
  2441. } else if (!memcmp(szValuePtr, "\xFF\xFE", 2)) {
  2442. decode = "UCS-2LE";
  2443. szValuePtr = szValuePtr+2;
  2444. ByteCount -= 2;
  2445. } else if (ImageInfo->motorola_intel) {
  2446. decode = ImageInfo->decode_unicode_be;
  2447. } else {
  2448. decode = ImageInfo->decode_unicode_le;
  2449. }
  2450. to = zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC);
  2451. from = zend_multibyte_fetch_encoding(decode TSRMLS_CC);
  2452. /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX */
  2453. if (!to || !from || zend_multibyte_encoding_converter(
  2454. (unsigned char**)pszInfoPtr,
  2455. &len,
  2456. (unsigned char*)szValuePtr,
  2457. ByteCount,
  2458. to,
  2459. from
  2460. TSRMLS_CC) == (size_t)-1) {
  2461. len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
  2462. }
  2463. return len;
  2464. } else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
  2465. *pszEncoding = estrdup((const char*)szValuePtr);
  2466. szValuePtr = szValuePtr+8;
  2467. ByteCount -= 8;
  2468. } else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
  2469. /* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
  2470. *pszEncoding = estrdup((const char*)szValuePtr);
  2471. szValuePtr = szValuePtr+8;
  2472. ByteCount -= 8;
  2473. /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX */
  2474. to = zend_multibyte_fetch_encoding(ImageInfo->encode_jis TSRMLS_CC);
  2475. from = zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le TSRMLS_CC);
  2476. if (!to || !from || zend_multibyte_encoding_converter(
  2477. (unsigned char**)pszInfoPtr,
  2478. &len,
  2479. (unsigned char*)szValuePtr,
  2480. ByteCount,
  2481. to,
  2482. from
  2483. TSRMLS_CC) == (size_t)-1) {
  2484. len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
  2485. }
  2486. return len;
  2487. } else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
  2488. /* 8 NULL means undefined and should be ASCII... */
  2489. *pszEncoding = estrdup("UNDEFINED");
  2490. szValuePtr = szValuePtr+8;
  2491. ByteCount -= 8;
  2492. }
  2493. }
  2494. /* Olympus has this padded with trailing spaces. Remove these first. */
  2495. if (ByteCount>0) {
  2496. for (a=ByteCount-1;a && szValuePtr[a]==' ';a--) {
  2497. (szValuePtr)[a] = '\0';
  2498. }
  2499. }
  2500. /* normal text without encoding */
  2501. exif_process_string(pszInfoPtr, szValuePtr, ByteCount TSRMLS_CC);
  2502. return strlen(*pszInfoPtr);
  2503. }
  2504. /* }}} */
  2505. /* {{{ exif_process_unicode
  2506. * Process unicode field in IFD. */
  2507. static int exif_process_unicode(image_info_type *ImageInfo, xp_field_type *xp_field, int tag, char *szValuePtr, int ByteCount TSRMLS_DC)
  2508. {
  2509. xp_field->tag = tag;
  2510. xp_field->value = NULL;
  2511. /* XXX this will fail again if encoding_converter returns on error something different than SIZE_MAX */
  2512. if (zend_multibyte_encoding_converter(
  2513. (unsigned char**)&xp_field->value,
  2514. &xp_field->size,
  2515. (unsigned char*)szValuePtr,
  2516. ByteCount,
  2517. zend_multibyte_fetch_encoding(ImageInfo->encode_unicode TSRMLS_CC),
  2518. zend_multibyte_fetch_encoding(ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le TSRMLS_CC)
  2519. TSRMLS_CC) == (size_t)-1) {
  2520. xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
  2521. }
  2522. return xp_field->size;
  2523. }
  2524. /* }}} */
  2525. /* {{{ exif_process_IFD_in_MAKERNOTE
  2526. * Process nested IFDs directories in Maker Note. */
  2527. static int exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * value_ptr, int value_len, char *offset_base, size_t IFDlength, size_t displacement TSRMLS_DC)
  2528. {
  2529. int de, i=0, section_index = SECTION_MAKERNOTE;
  2530. int NumDirEntries, old_motorola_intel, offset_diff;
  2531. const maker_note_type *maker_note;
  2532. char *dir_start;
  2533. int data_len;
  2534. for (i=0; i<=sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
  2535. if (i==sizeof(maker_note_array)/sizeof(maker_note_type)) {
  2536. #ifdef EXIF_DEBUG
  2537. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "No maker note data found. Detected maker: %s (length = %d)", ImageInfo->make, strlen(ImageInfo->make));
  2538. #endif
  2539. /* unknown manufacturer, not an error, use it as a string */
  2540. return TRUE;
  2541. }
  2542. maker_note = maker_note_array+i;
  2543. /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "check (%s,%s)", maker_note->make?maker_note->make:"", maker_note->model?maker_note->model:"");*/
  2544. if (maker_note->make && (!ImageInfo->make || strcmp(maker_note->make, ImageInfo->make)))
  2545. continue;
  2546. if (maker_note->model && (!ImageInfo->model || strcmp(maker_note->model, ImageInfo->model)))
  2547. continue;
  2548. if (maker_note->id_string && strncmp(maker_note->id_string, value_ptr, maker_note->id_string_len))
  2549. continue;
  2550. break;
  2551. }
  2552. if (maker_note->offset >= value_len) {
  2553. /* Do not go past the value end */
  2554. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "IFD data too short: 0x%04X offset 0x%04X", value_len, maker_note->offset);
  2555. return FALSE;
  2556. }
  2557. dir_start = value_ptr + maker_note->offset;
  2558. #ifdef EXIF_DEBUG
  2559. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process %s @x%04X + 0x%04X=%d: %s", exif_get_sectionname(section_index), (int)dir_start-(int)offset_base+maker_note->offset+displacement, value_len, value_len, exif_char_dump(value_ptr, value_len, (int)dir_start-(int)offset_base+maker_note->offset+displacement));
  2560. #endif
  2561. ImageInfo->sections_found |= FOUND_MAKERNOTE;
  2562. old_motorola_intel = ImageInfo->motorola_intel;
  2563. switch (maker_note->byte_order) {
  2564. case MN_ORDER_INTEL:
  2565. ImageInfo->motorola_intel = 0;
  2566. break;
  2567. case MN_ORDER_MOTOROLA:
  2568. ImageInfo->motorola_intel = 1;
  2569. break;
  2570. default:
  2571. case MN_ORDER_NORMAL:
  2572. break;
  2573. }
  2574. NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
  2575. switch (maker_note->offset_mode) {
  2576. case MN_OFFSET_MAKER:
  2577. offset_base = value_ptr;
  2578. data_len = value_len;
  2579. break;
  2580. case MN_OFFSET_GUESS:
  2581. if (maker_note->offset + 10 + 4 >= value_len) {
  2582. /* Can not read dir_start+10 since it's beyond value end */
  2583. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "IFD data too short: 0x%04X", value_len);
  2584. return FALSE;
  2585. }
  2586. offset_diff = 2 + NumDirEntries*12 + 4 - php_ifd_get32u(dir_start+10, ImageInfo->motorola_intel);
  2587. #ifdef EXIF_DEBUG
  2588. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Using automatic offset correction: 0x%04X", ((int)dir_start-(int)offset_base+maker_note->offset+displacement) + offset_diff);
  2589. #endif
  2590. if (offset_diff < 0 || offset_diff >= value_len ) {
  2591. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "IFD data bad offset: 0x%04X length 0x%04X", offset_diff, value_len);
  2592. return FALSE;
  2593. }
  2594. offset_base = value_ptr + offset_diff;
  2595. data_len = value_len - offset_diff;
  2596. break;
  2597. default:
  2598. case MN_OFFSET_NORMAL:
  2599. break;
  2600. }
  2601. if ((2+NumDirEntries*12) > value_len) {
  2602. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: 2 + 0x%04X*12 = 0x%04X > 0x%04X", NumDirEntries, 2+NumDirEntries*12, value_len);
  2603. return FALSE;
  2604. }
  2605. for (de=0;de<NumDirEntries;de++) {
  2606. if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
  2607. offset_base, data_len, displacement, section_index, 0, maker_note->tag_table TSRMLS_CC)) {
  2608. return FALSE;
  2609. }
  2610. }
  2611. ImageInfo->motorola_intel = old_motorola_intel;
  2612. /* NextDirOffset (must be NULL) = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);*/
  2613. #ifdef EXIF_DEBUG
  2614. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(SECTION_MAKERNOTE));
  2615. #endif
  2616. return TRUE;
  2617. }
  2618. /* }}} */
  2619. /* {{{ exif_process_IFD_TAG
  2620. * Process one of the nested IFDs directories. */
  2621. static int exif_process_IFD_TAG(image_info_type *ImageInfo, char *dir_entry, char *offset_base, size_t IFDlength, size_t displacement, int section_index, int ReadNextIFD, tag_table_type tag_table TSRMLS_DC)
  2622. {
  2623. size_t length;
  2624. int tag, format, components;
  2625. char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
  2626. size_t byte_count, offset_val, fpos, fgot;
  2627. int64_t byte_count_signed;
  2628. xp_field_type *tmp_xp;
  2629. #ifdef EXIF_DEBUG
  2630. char *dump_data;
  2631. int dump_free;
  2632. #endif /* EXIF_DEBUG */
  2633. /* Protect against corrupt headers */
  2634. if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
  2635. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "corrupt EXIF header: maximum directory nesting level reached");
  2636. return FALSE;
  2637. }
  2638. ImageInfo->ifd_nesting_level++;
  2639. tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
  2640. format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
  2641. components = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);
  2642. if (!format || format > NUM_FORMATS) {
  2643. /* (-1) catches illegal zero case as unsigned underflows to positive large. */
  2644. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal format code 0x%04X, suppose BYTE", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), format);
  2645. format = TAG_FMT_BYTE;
  2646. /*return TRUE;*/
  2647. }
  2648. if (components < 0) {
  2649. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal components(%d)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), components);
  2650. return FALSE;
  2651. }
  2652. byte_count_signed = (int64_t)components * php_tiff_bytes_per_format[format];
  2653. if (byte_count_signed < 0 || (byte_count_signed > INT32_MAX)) {
  2654. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal byte_count", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC));
  2655. return FALSE;
  2656. }
  2657. byte_count = (size_t)byte_count_signed;
  2658. if (byte_count > 4) {
  2659. offset_val = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
  2660. /* If its bigger than 4 bytes, the dir entry contains an offset. */
  2661. value_ptr = offset_base+offset_val;
  2662. /*
  2663. dir_entry is ImageInfo->file.list[sn].data+2+i*12
  2664. offset_base is ImageInfo->file.list[sn].data-dir_offset
  2665. dir_entry - offset_base is dir_offset+2+i*12
  2666. */
  2667. if (byte_count > IFDlength || offset_val > IFDlength-byte_count || value_ptr < dir_entry || offset_val < (size_t)(dir_entry-offset_base)) {
  2668. /* It is important to check for IMAGE_FILETYPE_TIFF
  2669. * JPEG does not use absolute pointers instead its pointers are
  2670. * relative to the start of the TIFF header in APP1 section. */
  2671. if (byte_count > ImageInfo->FileSize || offset_val>ImageInfo->FileSize-byte_count || (ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_II && ImageInfo->FileType!=IMAGE_FILETYPE_TIFF_MM && ImageInfo->FileType!=IMAGE_FILETYPE_JPEG)) {
  2672. if (value_ptr < dir_entry) {
  2673. /* we can read this if offset_val > 0 */
  2674. /* some files have their values in other parts of the file */
  2675. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal pointer offset(x%04X < x%04X)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val, dir_entry);
  2676. } else {
  2677. /* this is for sure not allowed */
  2678. /* exception are IFD pointers */
  2679. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Process tag(x%04X=%s): Illegal pointer offset(x%04X + x%04X = x%04X > x%04X)", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val, byte_count, offset_val+byte_count, IFDlength);
  2680. }
  2681. return FALSE;
  2682. }
  2683. if (byte_count>sizeof(cbuf)) {
  2684. /* mark as outside range and get buffer */
  2685. value_ptr = safe_emalloc(byte_count, 1, 0);
  2686. outside = value_ptr;
  2687. } else {
  2688. /* In most cases we only access a small range so
  2689. * it is faster to use a static buffer there
  2690. * BUT it offers also the possibility to have
  2691. * pointers read without the need to free them
  2692. * explicitley before returning. */
  2693. memset(&cbuf, 0, sizeof(cbuf));
  2694. value_ptr = cbuf;
  2695. }
  2696. fpos = php_stream_tell(ImageInfo->infile);
  2697. php_stream_seek(ImageInfo->infile, displacement+offset_val, SEEK_SET);
  2698. fgot = php_stream_tell(ImageInfo->infile);
  2699. if (fgot!=displacement+offset_val) {
  2700. EFREE_IF(outside);
  2701. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, displacement+offset_val);
  2702. return FALSE;
  2703. }
  2704. fgot = php_stream_read(ImageInfo->infile, value_ptr, byte_count);
  2705. php_stream_seek(ImageInfo->infile, fpos, SEEK_SET);
  2706. if (fgot<byte_count) {
  2707. EFREE_IF(outside);
  2708. EXIF_ERRLOG_FILEEOF(ImageInfo)
  2709. return FALSE;
  2710. }
  2711. }
  2712. } else {
  2713. /* 4 bytes or less and value is in the dir entry itself */
  2714. value_ptr = dir_entry+8;
  2715. offset_val= value_ptr-offset_base;
  2716. }
  2717. ImageInfo->sections_found |= FOUND_ANY_TAG;
  2718. #ifdef EXIF_DEBUG
  2719. dump_data = exif_dump_data(&dump_free, format, components, length, ImageInfo->motorola_intel, value_ptr TSRMLS_CC);
  2720. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process tag(x%04X=%s,@x%04X + x%04X(=%d)): %s%s %s", tag, exif_get_tagname(tag, tagname, -12, tag_table TSRMLS_CC), offset_val+displacement, byte_count, byte_count, (components>1)&&format!=TAG_FMT_UNDEFINED&&format!=TAG_FMT_STRING?"ARRAY OF ":"", exif_get_tagformat(format), dump_data);
  2721. if (dump_free) {
  2722. efree(dump_data);
  2723. }
  2724. #endif
  2725. if (section_index==SECTION_THUMBNAIL) {
  2726. if (!ImageInfo->Thumbnail.data) {
  2727. switch(tag) {
  2728. case TAG_IMAGEWIDTH:
  2729. case TAG_COMP_IMAGE_WIDTH:
  2730. ImageInfo->Thumbnail.width = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2731. break;
  2732. case TAG_IMAGEHEIGHT:
  2733. case TAG_COMP_IMAGE_HEIGHT:
  2734. ImageInfo->Thumbnail.height = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2735. break;
  2736. case TAG_STRIP_OFFSETS:
  2737. case TAG_JPEG_INTERCHANGE_FORMAT:
  2738. /* accept both formats */
  2739. ImageInfo->Thumbnail.offset = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2740. break;
  2741. case TAG_STRIP_BYTE_COUNTS:
  2742. if (ImageInfo->FileType == IMAGE_FILETYPE_TIFF_II || ImageInfo->FileType == IMAGE_FILETYPE_TIFF_MM) {
  2743. ImageInfo->Thumbnail.filetype = ImageInfo->FileType;
  2744. } else {
  2745. /* motorola is easier to read */
  2746. ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_TIFF_MM;
  2747. }
  2748. ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2749. break;
  2750. case TAG_JPEG_INTERCHANGE_FORMAT_LEN:
  2751. if (ImageInfo->Thumbnail.filetype == IMAGE_FILETYPE_UNKNOWN) {
  2752. ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_JPEG;
  2753. ImageInfo->Thumbnail.size = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2754. }
  2755. break;
  2756. }
  2757. }
  2758. } else {
  2759. if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
  2760. switch(tag) {
  2761. case TAG_COPYRIGHT:
  2762. /* check for "<photographer> NUL <editor> NUL" */
  2763. if (byte_count>1 && (length=php_strnlen(value_ptr, byte_count)) > 0) {
  2764. if (length<byte_count-1) {
  2765. /* When there are any characters after the first NUL */
  2766. ImageInfo->CopyrightPhotographer = estrdup(value_ptr);
  2767. ImageInfo->CopyrightEditor = estrndup(value_ptr+length+1, byte_count-length-1);
  2768. spprintf(&ImageInfo->Copyright, 0, "%s, %s", ImageInfo->CopyrightPhotographer, ImageInfo->CopyrightEditor);
  2769. /* format = TAG_FMT_UNDEFINED; this musn't be ASCII */
  2770. /* but we are not supposed to change this */
  2771. /* keep in mind that image_info does not store editor value */
  2772. } else {
  2773. ImageInfo->Copyright = estrndup(value_ptr, byte_count);
  2774. }
  2775. }
  2776. break;
  2777. case TAG_USERCOMMENT:
  2778. ImageInfo->UserCommentLength = exif_process_user_comment(ImageInfo, &(ImageInfo->UserComment), &(ImageInfo->UserCommentEncoding), value_ptr, byte_count TSRMLS_CC);
  2779. break;
  2780. case TAG_XP_TITLE:
  2781. case TAG_XP_COMMENTS:
  2782. case TAG_XP_AUTHOR:
  2783. case TAG_XP_KEYWORDS:
  2784. case TAG_XP_SUBJECT:
  2785. tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
  2786. ImageInfo->sections_found |= FOUND_WINXP;
  2787. ImageInfo->xp_fields.list = tmp_xp;
  2788. ImageInfo->xp_fields.count++;
  2789. exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count TSRMLS_CC);
  2790. break;
  2791. case TAG_FNUMBER:
  2792. /* Simplest way of expressing aperture, so I trust it the most.
  2793. (overwrite previously computed value if there is one) */
  2794. ImageInfo->ApertureFNumber = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2795. break;
  2796. case TAG_APERTURE:
  2797. case TAG_MAX_APERTURE:
  2798. /* More relevant info always comes earlier, so only use this field if we don't
  2799. have appropriate aperture information yet. */
  2800. if (ImageInfo->ApertureFNumber == 0) {
  2801. ImageInfo->ApertureFNumber
  2802. = (float)exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)*0.5);
  2803. }
  2804. break;
  2805. case TAG_SHUTTERSPEED:
  2806. /* More complicated way of expressing exposure time, so only use
  2807. this value if we don't already have it from somewhere else.
  2808. SHUTTERSPEED comes after EXPOSURE TIME
  2809. */
  2810. if (ImageInfo->ExposureTime == 0) {
  2811. ImageInfo->ExposureTime
  2812. = (float)(1/exp(exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)*log(2)));
  2813. }
  2814. break;
  2815. case TAG_EXPOSURETIME:
  2816. ImageInfo->ExposureTime = -1;
  2817. break;
  2818. case TAG_COMP_IMAGE_WIDTH:
  2819. ImageInfo->ExifImageWidth = exif_convert_any_to_int(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2820. break;
  2821. case TAG_FOCALPLANE_X_RES:
  2822. ImageInfo->FocalplaneXRes = exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2823. break;
  2824. case TAG_SUBJECT_DISTANCE:
  2825. /* Inidcates the distacne the autofocus camera is focused to.
  2826. Tends to be less accurate as distance increases. */
  2827. ImageInfo->Distance = (float)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC);
  2828. break;
  2829. case TAG_FOCALPLANE_RESOLUTION_UNIT:
  2830. switch((int)exif_convert_any_format(value_ptr, format, ImageInfo->motorola_intel TSRMLS_CC)) {
  2831. case 1: ImageInfo->FocalplaneUnits = 25.4; break; /* inch */
  2832. case 2:
  2833. /* According to the information I was using, 2 measn meters.
  2834. But looking at the Cannon powershot's files, inches is the only
  2835. sensible value. */
  2836. ImageInfo->FocalplaneUnits = 25.4;
  2837. break;
  2838. case 3: ImageInfo->FocalplaneUnits = 10; break; /* centimeter */
  2839. case 4: ImageInfo->FocalplaneUnits = 1; break; /* milimeter */
  2840. case 5: ImageInfo->FocalplaneUnits = .001; break; /* micrometer */
  2841. }
  2842. break;
  2843. case TAG_SUB_IFD:
  2844. if (format==TAG_FMT_IFD) {
  2845. /* If this is called we are either in a TIFFs thumbnail or a JPEG where we cannot handle it */
  2846. /* TIFF thumbnail: our data structure cannot store a thumbnail of a thumbnail */
  2847. /* JPEG do we have the data area and what to do with it */
  2848. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Skip SUB IFD");
  2849. }
  2850. break;
  2851. case TAG_MAKE:
  2852. ImageInfo->make = estrndup(value_ptr, byte_count);
  2853. break;
  2854. case TAG_MODEL:
  2855. ImageInfo->model = estrndup(value_ptr, byte_count);
  2856. break;
  2857. case TAG_MAKER_NOTE:
  2858. if (!exif_process_IFD_in_MAKERNOTE(ImageInfo, value_ptr, byte_count, offset_base, IFDlength, displacement TSRMLS_CC)) {
  2859. EFREE_IF(outside);
  2860. return FALSE;
  2861. }
  2862. break;
  2863. case TAG_EXIF_IFD_POINTER:
  2864. case TAG_GPS_IFD_POINTER:
  2865. case TAG_INTEROP_IFD_POINTER:
  2866. if (ReadNextIFD) {
  2867. char *Subdir_start;
  2868. int sub_section_index = 0;
  2869. switch(tag) {
  2870. case TAG_EXIF_IFD_POINTER:
  2871. #ifdef EXIF_DEBUG
  2872. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found EXIF");
  2873. #endif
  2874. ImageInfo->sections_found |= FOUND_EXIF;
  2875. sub_section_index = SECTION_EXIF;
  2876. break;
  2877. case TAG_GPS_IFD_POINTER:
  2878. #ifdef EXIF_DEBUG
  2879. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found GPS");
  2880. #endif
  2881. ImageInfo->sections_found |= FOUND_GPS;
  2882. sub_section_index = SECTION_GPS;
  2883. break;
  2884. case TAG_INTEROP_IFD_POINTER:
  2885. #ifdef EXIF_DEBUG
  2886. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Found INTEROPERABILITY");
  2887. #endif
  2888. ImageInfo->sections_found |= FOUND_INTEROP;
  2889. sub_section_index = SECTION_INTEROP;
  2890. break;
  2891. }
  2892. Subdir_start = offset_base + php_ifd_get32u(value_ptr, ImageInfo->motorola_intel);
  2893. if (Subdir_start < offset_base || Subdir_start > offset_base+IFDlength) {
  2894. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD Pointer");
  2895. return FALSE;
  2896. }
  2897. if (!exif_process_IFD_in_JPEG(ImageInfo, Subdir_start, offset_base, IFDlength, displacement, sub_section_index TSRMLS_CC)) {
  2898. return FALSE;
  2899. }
  2900. #ifdef EXIF_DEBUG
  2901. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Subsection %s done", exif_get_sectionname(sub_section_index));
  2902. #endif
  2903. }
  2904. }
  2905. }
  2906. exif_iif_add_tag(ImageInfo, section_index, exif_get_tagname(tag, tagname, sizeof(tagname), tag_table TSRMLS_CC), tag, format, components, value_ptr TSRMLS_CC);
  2907. EFREE_IF(outside);
  2908. return TRUE;
  2909. }
  2910. /* }}} */
  2911. /* {{{ exif_process_IFD_in_JPEG
  2912. * Process one of the nested IFDs directories. */
  2913. static int exif_process_IFD_in_JPEG(image_info_type *ImageInfo, char *dir_start, char *offset_base, size_t IFDlength, size_t displacement, int section_index TSRMLS_DC)
  2914. {
  2915. int de;
  2916. int NumDirEntries;
  2917. int NextDirOffset;
  2918. #ifdef EXIF_DEBUG
  2919. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process %s (x%04X(=%d))", exif_get_sectionname(section_index), IFDlength, IFDlength);
  2920. #endif
  2921. ImageInfo->sections_found |= FOUND_IFD0;
  2922. if ((dir_start + 2) >= (offset_base+IFDlength)) {
  2923. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
  2924. return FALSE;
  2925. }
  2926. NumDirEntries = php_ifd_get16u(dir_start, ImageInfo->motorola_intel);
  2927. if ((dir_start+2+NumDirEntries*12) > (offset_base+IFDlength)) {
  2928. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size: x%04X + 2 + x%04X*12 = x%04X > x%04X", (int)((size_t)dir_start+2-(size_t)offset_base), NumDirEntries, (int)((size_t)dir_start+2+NumDirEntries*12-(size_t)offset_base), IFDlength);
  2929. return FALSE;
  2930. }
  2931. for (de=0;de<NumDirEntries;de++) {
  2932. if (!exif_process_IFD_TAG(ImageInfo, dir_start + 2 + 12 * de,
  2933. offset_base, IFDlength, displacement, section_index, 1, exif_get_tag_table(section_index) TSRMLS_CC)) {
  2934. return FALSE;
  2935. }
  2936. }
  2937. /*
  2938. * Ignore IFD2 if it purportedly exists
  2939. */
  2940. if (section_index == SECTION_THUMBNAIL) {
  2941. return TRUE;
  2942. }
  2943. /*
  2944. * Hack to make it process IDF1 I hope
  2945. * There are 2 IDFs, the second one holds the keys (0x0201 and 0x0202) to the thumbnail
  2946. */
  2947. if ((dir_start+2+12*de + 4) >= (offset_base+IFDlength)) {
  2948. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD size");
  2949. return FALSE;
  2950. }
  2951. NextDirOffset = php_ifd_get32u(dir_start+2+12*de, ImageInfo->motorola_intel);
  2952. if (NextDirOffset) {
  2953. /* the next line seems false but here IFDlength means length of all IFDs */
  2954. if (offset_base + NextDirOffset < offset_base || offset_base + NextDirOffset > offset_base+IFDlength) {
  2955. exif_error_docref("exif_read_data#error_ifd" EXIFERR_CC, ImageInfo, E_WARNING, "Illegal IFD offset");
  2956. return FALSE;
  2957. }
  2958. /* That is the IFD for the first thumbnail */
  2959. #ifdef EXIF_DEBUG
  2960. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Expect next IFD to be thumbnail");
  2961. #endif
  2962. if (exif_process_IFD_in_JPEG(ImageInfo, offset_base + NextDirOffset, offset_base, IFDlength, displacement, SECTION_THUMBNAIL TSRMLS_CC)) {
  2963. #ifdef EXIF_DEBUG
  2964. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail size: 0x%04X", ImageInfo->Thumbnail.size);
  2965. #endif
  2966. if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
  2967. && ImageInfo->Thumbnail.size
  2968. && ImageInfo->Thumbnail.offset
  2969. && ImageInfo->read_thumbnail
  2970. ) {
  2971. exif_thumbnail_extract(ImageInfo, offset_base, IFDlength TSRMLS_CC);
  2972. }
  2973. return TRUE;
  2974. } else {
  2975. return FALSE;
  2976. }
  2977. }
  2978. return TRUE;
  2979. }
  2980. /* }}} */
  2981. /* {{{ exif_process_TIFF_in_JPEG
  2982. Process a TIFF header in a JPEG file
  2983. */
  2984. static void exif_process_TIFF_in_JPEG(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
  2985. {
  2986. unsigned exif_value_2a, offset_of_ifd;
  2987. /* set the thumbnail stuff to nothing so we can test to see if they get set up */
  2988. if (memcmp(CharBuf, "II", 2) == 0) {
  2989. ImageInfo->motorola_intel = 0;
  2990. } else if (memcmp(CharBuf, "MM", 2) == 0) {
  2991. ImageInfo->motorola_intel = 1;
  2992. } else {
  2993. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF alignment marker");
  2994. return;
  2995. }
  2996. /* Check the next two values for correctness. */
  2997. if (length < 8) {
  2998. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
  2999. return;
  3000. }
  3001. exif_value_2a = php_ifd_get16u(CharBuf+2, ImageInfo->motorola_intel);
  3002. offset_of_ifd = php_ifd_get32u(CharBuf+4, ImageInfo->motorola_intel);
  3003. if (exif_value_2a != 0x2a || offset_of_ifd < 0x08) {
  3004. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF start (1)");
  3005. return;
  3006. }
  3007. if (offset_of_ifd > length) {
  3008. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid IFD start");
  3009. return;
  3010. }
  3011. ImageInfo->sections_found |= FOUND_IFD0;
  3012. /* First directory starts at offset 8. Offsets starts at 0. */
  3013. exif_process_IFD_in_JPEG(ImageInfo, CharBuf+offset_of_ifd, CharBuf, length/*-14*/, displacement, SECTION_IFD0 TSRMLS_CC);
  3014. #ifdef EXIF_DEBUG
  3015. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process TIFF in JPEG done");
  3016. #endif
  3017. /* Compute the CCD width, in milimeters. */
  3018. if (ImageInfo->FocalplaneXRes != 0) {
  3019. ImageInfo->CCDWidth = (float)(ImageInfo->ExifImageWidth * ImageInfo->FocalplaneUnits / ImageInfo->FocalplaneXRes);
  3020. }
  3021. }
  3022. /* }}} */
  3023. /* {{{ exif_process_APP1
  3024. Process an JPEG APP1 block marker
  3025. Describes all the drivel that most digital cameras include...
  3026. */
  3027. static void exif_process_APP1(image_info_type *ImageInfo, char *CharBuf, size_t length, size_t displacement TSRMLS_DC)
  3028. {
  3029. /* Check the APP1 for Exif Identifier Code */
  3030. static const uchar ExifHeader[] = {0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
  3031. if (length <= 8 || memcmp(CharBuf+2, ExifHeader, 6)) {
  3032. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Incorrect APP1 Exif Identifier Code");
  3033. return;
  3034. }
  3035. exif_process_TIFF_in_JPEG(ImageInfo, CharBuf + 8, length - 8, displacement+8 TSRMLS_CC);
  3036. #ifdef EXIF_DEBUG
  3037. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process APP1/EXIF done");
  3038. #endif
  3039. }
  3040. /* }}} */
  3041. /* {{{ exif_process_APP12
  3042. Process an JPEG APP12 block marker used by OLYMPUS
  3043. */
  3044. static void exif_process_APP12(image_info_type *ImageInfo, char *buffer, size_t length TSRMLS_DC)
  3045. {
  3046. size_t l1, l2=0;
  3047. if ((l1 = php_strnlen(buffer+2, length-2)) > 0) {
  3048. exif_iif_add_tag(ImageInfo, SECTION_APP12, "Company", TAG_NONE, TAG_FMT_STRING, l1, buffer+2 TSRMLS_CC);
  3049. if (length > 2+l1+1) {
  3050. l2 = php_strnlen(buffer+2+l1+1, length-2-l1-1);
  3051. exif_iif_add_tag(ImageInfo, SECTION_APP12, "Info", TAG_NONE, TAG_FMT_STRING, l2, buffer+2+l1+1 TSRMLS_CC);
  3052. }
  3053. }
  3054. #ifdef EXIF_DEBUG
  3055. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section APP12 with l1=%d, l2=%d done", l1, l2);
  3056. #endif
  3057. }
  3058. /* }}} */
  3059. /* {{{ exif_scan_JPEG_header
  3060. * Parse the marker stream until SOS or EOI is seen; */
  3061. static int exif_scan_JPEG_header(image_info_type *ImageInfo TSRMLS_DC)
  3062. {
  3063. int section, sn;
  3064. int marker = 0, last_marker = M_PSEUDO, comment_correction=1;
  3065. unsigned int ll, lh;
  3066. uchar *Data;
  3067. size_t fpos, size, got, itemlen;
  3068. jpeg_sof_info sof_info;
  3069. for(section=0;;section++) {
  3070. #ifdef EXIF_DEBUG
  3071. fpos = php_stream_tell(ImageInfo->infile);
  3072. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Needing section %d @ 0x%08X", ImageInfo->file.count, fpos);
  3073. #endif
  3074. /* get marker byte, swallowing possible padding */
  3075. /* some software does not count the length bytes of COM section */
  3076. /* one company doing so is very much envolved in JPEG... so we accept too */
  3077. if (last_marker==M_COM && comment_correction) {
  3078. comment_correction = 2;
  3079. }
  3080. do {
  3081. if ((marker = php_stream_getc(ImageInfo->infile)) == EOF) {
  3082. EXIF_ERRLOG_CORRUPT(ImageInfo)
  3083. return FALSE;
  3084. }
  3085. if (last_marker==M_COM && comment_correction>0) {
  3086. if (marker!=0xFF) {
  3087. marker = 0xff;
  3088. comment_correction--;
  3089. } else {
  3090. last_marker = M_PSEUDO; /* stop skipping 0 for M_COM */
  3091. }
  3092. }
  3093. } while (marker == 0xff);
  3094. if (last_marker==M_COM && !comment_correction) {
  3095. exif_error_docref("exif_read_data#error_mcom" EXIFERR_CC, ImageInfo, E_NOTICE, "Image has corrupt COM section: some software set wrong length information");
  3096. }
  3097. if (last_marker==M_COM && comment_correction)
  3098. return M_EOI; /* ah illegal: char after COM section not 0xFF */
  3099. fpos = php_stream_tell(ImageInfo->infile);
  3100. if (marker == 0xff) {
  3101. /* 0xff is legal padding, but if we get that many, something's wrong. */
  3102. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "To many padding bytes");
  3103. return FALSE;
  3104. }
  3105. /* Read the length of the section. */
  3106. if ((lh = php_stream_getc(ImageInfo->infile)) == EOF) {
  3107. EXIF_ERRLOG_CORRUPT(ImageInfo)
  3108. return FALSE;
  3109. }
  3110. if ((ll = php_stream_getc(ImageInfo->infile)) == EOF) {
  3111. EXIF_ERRLOG_CORRUPT(ImageInfo)
  3112. return FALSE;
  3113. }
  3114. itemlen = (lh << 8) | ll;
  3115. if (itemlen < 2) {
  3116. #ifdef EXIF_DEBUG
  3117. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "%s, Section length: 0x%02X%02X", EXIF_ERROR_CORRUPT, lh, ll);
  3118. #else
  3119. EXIF_ERRLOG_CORRUPT(ImageInfo)
  3120. #endif
  3121. return FALSE;
  3122. }
  3123. sn = exif_file_sections_add(ImageInfo, marker, itemlen+1, NULL);
  3124. Data = ImageInfo->file.list[sn].data;
  3125. /* Store first two pre-read bytes. */
  3126. Data[0] = (uchar)lh;
  3127. Data[1] = (uchar)ll;
  3128. got = php_stream_read(ImageInfo->infile, (char*)(Data+2), itemlen-2); /* Read the whole section. */
  3129. if (got != itemlen-2) {
  3130. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error reading from file: got=x%04X(=%d) != itemlen-2=x%04X(=%d)", got, got, itemlen-2, itemlen-2);
  3131. return FALSE;
  3132. }
  3133. #ifdef EXIF_DEBUG
  3134. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Process section(x%02X=%s) @ x%04X + x%04X(=%d)", marker, exif_get_markername(marker), fpos, itemlen, itemlen);
  3135. #endif
  3136. switch(marker) {
  3137. case M_SOS: /* stop before hitting compressed data */
  3138. /* If reading entire image is requested, read the rest of the data. */
  3139. if (ImageInfo->read_all) {
  3140. /* Determine how much file is left. */
  3141. fpos = php_stream_tell(ImageInfo->infile);
  3142. size = ImageInfo->FileSize - fpos;
  3143. sn = exif_file_sections_add(ImageInfo, M_PSEUDO, size, NULL);
  3144. Data = ImageInfo->file.list[sn].data;
  3145. got = php_stream_read(ImageInfo->infile, (char*)Data, size);
  3146. if (got != size) {
  3147. EXIF_ERRLOG_FILEEOF(ImageInfo)
  3148. return FALSE;
  3149. }
  3150. }
  3151. return TRUE;
  3152. case M_EOI: /* in case it's a tables-only JPEG stream */
  3153. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "No image in jpeg!");
  3154. return (ImageInfo->sections_found&(~FOUND_COMPUTED)) ? TRUE : FALSE;
  3155. case M_COM: /* Comment section */
  3156. exif_process_COM(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
  3157. break;
  3158. case M_EXIF:
  3159. if (!(ImageInfo->sections_found&FOUND_IFD0)) {
  3160. /*ImageInfo->sections_found |= FOUND_EXIF;*/
  3161. /* Seen files from some 'U-lead' software with Vivitar scanner
  3162. that uses marker 31 later in the file (no clue what for!) */
  3163. exif_process_APP1(ImageInfo, (char *)Data, itemlen, fpos TSRMLS_CC);
  3164. }
  3165. break;
  3166. case M_APP12:
  3167. exif_process_APP12(ImageInfo, (char *)Data, itemlen TSRMLS_CC);
  3168. break;
  3169. case M_SOF0:
  3170. case M_SOF1:
  3171. case M_SOF2:
  3172. case M_SOF3:
  3173. case M_SOF5:
  3174. case M_SOF6:
  3175. case M_SOF7:
  3176. case M_SOF9:
  3177. case M_SOF10:
  3178. case M_SOF11:
  3179. case M_SOF13:
  3180. case M_SOF14:
  3181. case M_SOF15:
  3182. if ((itemlen - 2) < 6) {
  3183. return FALSE;
  3184. }
  3185. exif_process_SOFn(Data, marker, &sof_info);
  3186. ImageInfo->Width = sof_info.width;
  3187. ImageInfo->Height = sof_info.height;
  3188. if (sof_info.num_components == 3) {
  3189. ImageInfo->IsColor = 1;
  3190. } else {
  3191. ImageInfo->IsColor = 0;
  3192. }
  3193. break;
  3194. default:
  3195. /* skip any other marker silently. */
  3196. break;
  3197. }
  3198. /* keep track of last marker */
  3199. last_marker = marker;
  3200. }
  3201. #ifdef EXIF_DEBUG
  3202. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Done");
  3203. #endif
  3204. return TRUE;
  3205. }
  3206. /* }}} */
  3207. /* {{{ exif_scan_thumbnail
  3208. * scan JPEG in thumbnail (memory) */
  3209. static int exif_scan_thumbnail(image_info_type *ImageInfo TSRMLS_DC)
  3210. {
  3211. uchar c, *data = (uchar*)ImageInfo->Thumbnail.data;
  3212. int n, marker;
  3213. size_t length=2, pos=0;
  3214. jpeg_sof_info sof_info;
  3215. if (!data) {
  3216. return FALSE; /* nothing to do here */
  3217. }
  3218. if (memcmp(data, "\xFF\xD8\xFF", 3)) {
  3219. if (!ImageInfo->Thumbnail.width && !ImageInfo->Thumbnail.height) {
  3220. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Thumbnail is not a JPEG image");
  3221. }
  3222. return FALSE;
  3223. }
  3224. for (;;) {
  3225. pos += length;
  3226. if (pos>=ImageInfo->Thumbnail.size)
  3227. return FALSE;
  3228. c = data[pos++];
  3229. if (pos>=ImageInfo->Thumbnail.size)
  3230. return FALSE;
  3231. if (c != 0xFF) {
  3232. return FALSE;
  3233. }
  3234. n = 8;
  3235. while ((c = data[pos++]) == 0xFF && n--) {
  3236. if (pos+3>=ImageInfo->Thumbnail.size)
  3237. return FALSE;
  3238. /* +3 = pos++ of next check when reaching marker + 2 bytes for length */
  3239. }
  3240. if (c == 0xFF)
  3241. return FALSE;
  3242. marker = c;
  3243. length = php_jpg_get16(data+pos);
  3244. if (pos+length>=ImageInfo->Thumbnail.size) {
  3245. return FALSE;
  3246. }
  3247. #ifdef EXIF_DEBUG
  3248. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: process section(x%02X=%s) @ x%04X + x%04X", marker, exif_get_markername(marker), pos, length);
  3249. #endif
  3250. switch (marker) {
  3251. case M_SOF0:
  3252. case M_SOF1:
  3253. case M_SOF2:
  3254. case M_SOF3:
  3255. case M_SOF5:
  3256. case M_SOF6:
  3257. case M_SOF7:
  3258. case M_SOF9:
  3259. case M_SOF10:
  3260. case M_SOF11:
  3261. case M_SOF13:
  3262. case M_SOF14:
  3263. case M_SOF15:
  3264. /* handle SOFn block */
  3265. exif_process_SOFn(data+pos, marker, &sof_info);
  3266. ImageInfo->Thumbnail.height = sof_info.height;
  3267. ImageInfo->Thumbnail.width = sof_info.width;
  3268. #ifdef EXIF_DEBUG
  3269. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Thumbnail: size: %d * %d", sof_info.width, sof_info.height);
  3270. #endif
  3271. return TRUE;
  3272. case M_SOS:
  3273. case M_EOI:
  3274. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
  3275. return FALSE;
  3276. break;
  3277. default:
  3278. /* just skip */
  3279. break;
  3280. }
  3281. }
  3282. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Could not compute size of thumbnail");
  3283. return FALSE;
  3284. }
  3285. /* }}} */
  3286. /* {{{ exif_process_IFD_in_TIFF
  3287. * Parse the TIFF header; */
  3288. static int exif_process_IFD_in_TIFF(image_info_type *ImageInfo, size_t dir_offset, int section_index TSRMLS_DC)
  3289. {
  3290. int i, sn, num_entries, sub_section_index = 0;
  3291. unsigned char *dir_entry;
  3292. char tagname[64];
  3293. size_t ifd_size, dir_size, entry_offset, next_offset, entry_length, entry_value=0, fgot;
  3294. int entry_tag , entry_type;
  3295. tag_table_type tag_table = exif_get_tag_table(section_index);
  3296. if (ImageInfo->ifd_nesting_level > MAX_IFD_NESTING_LEVEL) {
  3297. return FALSE;
  3298. }
  3299. if (ImageInfo->FileSize >= dir_offset+2) {
  3300. sn = exif_file_sections_add(ImageInfo, M_PSEUDO, 2, NULL);
  3301. #ifdef EXIF_DEBUG
  3302. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, 2);
  3303. #endif
  3304. php_stream_seek(ImageInfo->infile, dir_offset, SEEK_SET); /* we do not know the order of sections */
  3305. php_stream_read(ImageInfo->infile, (char*)ImageInfo->file.list[sn].data, 2);
  3306. num_entries = php_ifd_get16u(ImageInfo->file.list[sn].data, ImageInfo->motorola_intel);
  3307. dir_size = 2/*num dir entries*/ +12/*length of entry*/*num_entries +4/* offset to next ifd (points to thumbnail or NULL)*/;
  3308. if (ImageInfo->FileSize >= dir_offset+dir_size) {
  3309. #ifdef EXIF_DEBUG
  3310. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD dir(x%04X + x%04X), IFD entries(%d)", ImageInfo->FileSize, dir_offset+2, dir_size-2, num_entries);
  3311. #endif
  3312. if (exif_file_sections_realloc(ImageInfo, sn, dir_size TSRMLS_CC)) {
  3313. return FALSE;
  3314. }
  3315. php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+2), dir_size-2);
  3316. /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Dump: %s", exif_char_dump(ImageInfo->file.list[sn].data, dir_size, 0));*/
  3317. next_offset = php_ifd_get32u(ImageInfo->file.list[sn].data + dir_size - 4, ImageInfo->motorola_intel);
  3318. #ifdef EXIF_DEBUG
  3319. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF done, next offset x%04X", next_offset);
  3320. #endif
  3321. /* now we have the directory we can look how long it should be */
  3322. ifd_size = dir_size;
  3323. for(i=0;i<num_entries;i++) {
  3324. dir_entry = ImageInfo->file.list[sn].data+2+i*12;
  3325. entry_tag = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
  3326. entry_type = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
  3327. if (entry_type > NUM_FORMATS) {
  3328. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: tag(0x%04X,%12s): Illegal format code 0x%04X, switching to BYTE", entry_tag, exif_get_tagname(entry_tag, tagname, -12, tag_table TSRMLS_CC), entry_type);
  3329. /* Since this is repeated in exif_process_IFD_TAG make it a notice here */
  3330. /* and make it a warning in the exif_process_IFD_TAG which is called */
  3331. /* elsewhere. */
  3332. entry_type = TAG_FMT_BYTE;
  3333. /*The next line would break the image on writeback: */
  3334. /* php_ifd_set16u(dir_entry+2, entry_type, ImageInfo->motorola_intel);*/
  3335. }
  3336. entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel) * php_tiff_bytes_per_format[entry_type];
  3337. if (entry_length <= 4) {
  3338. switch(entry_type) {
  3339. case TAG_FMT_USHORT:
  3340. entry_value = php_ifd_get16u(dir_entry+8, ImageInfo->motorola_intel);
  3341. break;
  3342. case TAG_FMT_SSHORT:
  3343. entry_value = php_ifd_get16s(dir_entry+8, ImageInfo->motorola_intel);
  3344. break;
  3345. case TAG_FMT_ULONG:
  3346. entry_value = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
  3347. break;
  3348. case TAG_FMT_SLONG:
  3349. entry_value = php_ifd_get32s(dir_entry+8, ImageInfo->motorola_intel);
  3350. break;
  3351. }
  3352. switch(entry_tag) {
  3353. case TAG_IMAGEWIDTH:
  3354. case TAG_COMP_IMAGE_WIDTH:
  3355. ImageInfo->Width = entry_value;
  3356. break;
  3357. case TAG_IMAGEHEIGHT:
  3358. case TAG_COMP_IMAGE_HEIGHT:
  3359. ImageInfo->Height = entry_value;
  3360. break;
  3361. case TAG_PHOTOMETRIC_INTERPRETATION:
  3362. switch (entry_value) {
  3363. case PMI_BLACK_IS_ZERO:
  3364. case PMI_WHITE_IS_ZERO:
  3365. case PMI_TRANSPARENCY_MASK:
  3366. ImageInfo->IsColor = 0;
  3367. break;
  3368. case PMI_RGB:
  3369. case PMI_PALETTE_COLOR:
  3370. case PMI_SEPARATED:
  3371. case PMI_YCBCR:
  3372. case PMI_CIELAB:
  3373. ImageInfo->IsColor = 1;
  3374. break;
  3375. }
  3376. break;
  3377. }
  3378. } else {
  3379. entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
  3380. /* if entry needs expading ifd cache and entry is at end of current ifd cache. */
  3381. /* otherwise there may be huge holes between two entries */
  3382. if (entry_offset + entry_length > dir_offset + ifd_size
  3383. && entry_offset == dir_offset + ifd_size) {
  3384. ifd_size = entry_offset + entry_length - dir_offset;
  3385. #ifdef EXIF_DEBUG
  3386. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Resize struct: x%04X + x%04X - x%04X = x%04X", entry_offset, entry_length, dir_offset, ifd_size);
  3387. #endif
  3388. }
  3389. }
  3390. }
  3391. if (ImageInfo->FileSize >= dir_offset + ImageInfo->file.list[sn].size) {
  3392. if (ifd_size > dir_size) {
  3393. if (dir_offset + ifd_size > ImageInfo->FileSize) {
  3394. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
  3395. return FALSE;
  3396. }
  3397. if (exif_file_sections_realloc(ImageInfo, sn, ifd_size TSRMLS_CC)) {
  3398. return FALSE;
  3399. }
  3400. /* read values not stored in directory itself */
  3401. #ifdef EXIF_DEBUG
  3402. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF: filesize(x%04X), IFD(x%04X + x%04X)", ImageInfo->FileSize, dir_offset, ifd_size);
  3403. #endif
  3404. php_stream_read(ImageInfo->infile, (char*)(ImageInfo->file.list[sn].data+dir_size), ifd_size-dir_size);
  3405. #ifdef EXIF_DEBUG
  3406. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read from TIFF, done");
  3407. #endif
  3408. }
  3409. /* now process the tags */
  3410. for(i=0;i<num_entries;i++) {
  3411. dir_entry = ImageInfo->file.list[sn].data+2+i*12;
  3412. entry_tag = php_ifd_get16u(dir_entry+0, ImageInfo->motorola_intel);
  3413. entry_type = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
  3414. /*entry_length = php_ifd_get32u(dir_entry+4, ImageInfo->motorola_intel);*/
  3415. if (entry_tag == TAG_EXIF_IFD_POINTER ||
  3416. entry_tag == TAG_INTEROP_IFD_POINTER ||
  3417. entry_tag == TAG_GPS_IFD_POINTER ||
  3418. entry_tag == TAG_SUB_IFD
  3419. ) {
  3420. switch(entry_tag) {
  3421. case TAG_EXIF_IFD_POINTER:
  3422. ImageInfo->sections_found |= FOUND_EXIF;
  3423. sub_section_index = SECTION_EXIF;
  3424. break;
  3425. case TAG_GPS_IFD_POINTER:
  3426. ImageInfo->sections_found |= FOUND_GPS;
  3427. sub_section_index = SECTION_GPS;
  3428. break;
  3429. case TAG_INTEROP_IFD_POINTER:
  3430. ImageInfo->sections_found |= FOUND_INTEROP;
  3431. sub_section_index = SECTION_INTEROP;
  3432. break;
  3433. case TAG_SUB_IFD:
  3434. ImageInfo->sections_found |= FOUND_THUMBNAIL;
  3435. sub_section_index = SECTION_THUMBNAIL;
  3436. break;
  3437. }
  3438. entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
  3439. #ifdef EXIF_DEBUG
  3440. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @x%04X", exif_get_sectionname(sub_section_index), entry_offset);
  3441. #endif
  3442. ImageInfo->ifd_nesting_level++;
  3443. exif_process_IFD_in_TIFF(ImageInfo, entry_offset, sub_section_index TSRMLS_CC);
  3444. if (section_index!=SECTION_THUMBNAIL && entry_tag==TAG_SUB_IFD) {
  3445. if (ImageInfo->Thumbnail.filetype != IMAGE_FILETYPE_UNKNOWN
  3446. && ImageInfo->Thumbnail.size
  3447. && ImageInfo->Thumbnail.offset
  3448. && ImageInfo->read_thumbnail
  3449. ) {
  3450. #ifdef EXIF_DEBUG
  3451. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "%s THUMBNAIL @0x%04X + 0x%04X", ImageInfo->Thumbnail.data ? "Ignore" : "Read", ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
  3452. #endif
  3453. if (!ImageInfo->Thumbnail.data) {
  3454. ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
  3455. php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
  3456. fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
  3457. if (fgot < ImageInfo->Thumbnail.size) {
  3458. EXIF_ERRLOG_THUMBEOF(ImageInfo)
  3459. efree(ImageInfo->Thumbnail.data);
  3460. ImageInfo->Thumbnail.data = NULL;
  3461. } else {
  3462. exif_thumbnail_build(ImageInfo TSRMLS_CC);
  3463. }
  3464. }
  3465. }
  3466. }
  3467. #ifdef EXIF_DEBUG
  3468. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s done", exif_get_sectionname(sub_section_index));
  3469. #endif
  3470. } else {
  3471. if (!exif_process_IFD_TAG(ImageInfo, (char*)dir_entry,
  3472. (char*)(ImageInfo->file.list[sn].data-dir_offset),
  3473. ifd_size, 0, section_index, 0, tag_table TSRMLS_CC)) {
  3474. return FALSE;
  3475. }
  3476. }
  3477. }
  3478. /* If we had a thumbnail in a SUB_IFD we have ANOTHER image in NEXT IFD */
  3479. if (next_offset && section_index != SECTION_THUMBNAIL) {
  3480. /* this should be a thumbnail IFD */
  3481. /* the thumbnail itself is stored at Tag=StripOffsets */
  3482. #ifdef EXIF_DEBUG
  3483. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) at x%04X", next_offset);
  3484. #endif
  3485. ImageInfo->ifd_nesting_level++;
  3486. exif_process_IFD_in_TIFF(ImageInfo, next_offset, SECTION_THUMBNAIL TSRMLS_CC);
  3487. #ifdef EXIF_DEBUG
  3488. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "%s THUMBNAIL @0x%04X + 0x%04X", ImageInfo->Thumbnail.data ? "Ignore" : "Read", ImageInfo->Thumbnail.offset, ImageInfo->Thumbnail.size);
  3489. #endif
  3490. if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
  3491. ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
  3492. php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
  3493. fgot = php_stream_read(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
  3494. if (fgot < ImageInfo->Thumbnail.size) {
  3495. EXIF_ERRLOG_THUMBEOF(ImageInfo)
  3496. efree(ImageInfo->Thumbnail.data);
  3497. ImageInfo->Thumbnail.data = NULL;
  3498. } else {
  3499. exif_thumbnail_build(ImageInfo TSRMLS_CC);
  3500. }
  3501. }
  3502. #ifdef EXIF_DEBUG
  3503. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Read next IFD (THUMBNAIL) done");
  3504. #endif
  3505. }
  3506. return TRUE;
  3507. } else {
  3508. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD(x%04X)", ImageInfo->FileSize, dir_offset+ImageInfo->file.list[sn].size);
  3509. return FALSE;
  3510. }
  3511. } else {
  3512. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than size of IFD dir(x%04X)", ImageInfo->FileSize, dir_offset+dir_size);
  3513. return FALSE;
  3514. }
  3515. } else {
  3516. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Error in TIFF: filesize(x%04X) less than start of IFD dir(x%04X)", ImageInfo->FileSize, dir_offset+2);
  3517. return FALSE;
  3518. }
  3519. }
  3520. /* }}} */
  3521. /* {{{ exif_scan_FILE_header
  3522. * Parse the marker stream until SOS or EOI is seen; */
  3523. static int exif_scan_FILE_header(image_info_type *ImageInfo TSRMLS_DC)
  3524. {
  3525. unsigned char file_header[8];
  3526. int ret = FALSE;
  3527. ImageInfo->FileType = IMAGE_FILETYPE_UNKNOWN;
  3528. if (ImageInfo->FileSize >= 2) {
  3529. php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
  3530. if (php_stream_read(ImageInfo->infile, (char*)file_header, 2) != 2) {
  3531. return FALSE;
  3532. }
  3533. if ((file_header[0]==0xff) && (file_header[1]==M_SOI)) {
  3534. ImageInfo->FileType = IMAGE_FILETYPE_JPEG;
  3535. if (exif_scan_JPEG_header(ImageInfo TSRMLS_CC)) {
  3536. ret = TRUE;
  3537. } else {
  3538. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid JPEG file");
  3539. }
  3540. } else if (ImageInfo->FileSize >= 8) {
  3541. if (php_stream_read(ImageInfo->infile, (char*)(file_header+2), 6) != 6) {
  3542. return FALSE;
  3543. }
  3544. if (!memcmp(file_header, "II\x2A\x00", 4)) {
  3545. ImageInfo->FileType = IMAGE_FILETYPE_TIFF_II;
  3546. ImageInfo->motorola_intel = 0;
  3547. #ifdef EXIF_DEBUG
  3548. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/II format");
  3549. #endif
  3550. ImageInfo->sections_found |= FOUND_IFD0;
  3551. if (exif_process_IFD_in_TIFF(ImageInfo,
  3552. php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
  3553. SECTION_IFD0 TSRMLS_CC)) {
  3554. ret = TRUE;
  3555. } else {
  3556. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
  3557. }
  3558. } else if (!memcmp(file_header, "MM\x00\x2a", 4)) {
  3559. ImageInfo->FileType = IMAGE_FILETYPE_TIFF_MM;
  3560. ImageInfo->motorola_intel = 1;
  3561. #ifdef EXIF_DEBUG
  3562. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "File has TIFF/MM format");
  3563. #endif
  3564. ImageInfo->sections_found |= FOUND_IFD0;
  3565. if (exif_process_IFD_in_TIFF(ImageInfo,
  3566. php_ifd_get32u(file_header + 4, ImageInfo->motorola_intel),
  3567. SECTION_IFD0 TSRMLS_CC)) {
  3568. ret = TRUE;
  3569. } else {
  3570. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Invalid TIFF file");
  3571. }
  3572. } else {
  3573. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File not supported");
  3574. return FALSE;
  3575. }
  3576. }
  3577. } else {
  3578. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "File too small (%d)", ImageInfo->FileSize);
  3579. }
  3580. return ret;
  3581. }
  3582. /* }}} */
  3583. /* {{{ exif_discard_imageinfo
  3584. Discard data scanned by exif_read_file.
  3585. */
  3586. static int exif_discard_imageinfo(image_info_type *ImageInfo)
  3587. {
  3588. int i;
  3589. EFREE_IF(ImageInfo->FileName);
  3590. EFREE_IF(ImageInfo->UserComment);
  3591. EFREE_IF(ImageInfo->UserCommentEncoding);
  3592. EFREE_IF(ImageInfo->Copyright);
  3593. EFREE_IF(ImageInfo->CopyrightPhotographer);
  3594. EFREE_IF(ImageInfo->CopyrightEditor);
  3595. EFREE_IF(ImageInfo->Thumbnail.data);
  3596. EFREE_IF(ImageInfo->encode_unicode);
  3597. EFREE_IF(ImageInfo->decode_unicode_be);
  3598. EFREE_IF(ImageInfo->decode_unicode_le);
  3599. EFREE_IF(ImageInfo->encode_jis);
  3600. EFREE_IF(ImageInfo->decode_jis_be);
  3601. EFREE_IF(ImageInfo->decode_jis_le);
  3602. EFREE_IF(ImageInfo->make);
  3603. EFREE_IF(ImageInfo->model);
  3604. for (i=0; i<ImageInfo->xp_fields.count; i++) {
  3605. EFREE_IF(ImageInfo->xp_fields.list[i].value);
  3606. }
  3607. EFREE_IF(ImageInfo->xp_fields.list);
  3608. for (i=0; i<SECTION_COUNT; i++) {
  3609. exif_iif_free(ImageInfo, i);
  3610. }
  3611. exif_file_sections_free(ImageInfo);
  3612. memset(ImageInfo, 0, sizeof(*ImageInfo));
  3613. return TRUE;
  3614. }
  3615. /* }}} */
  3616. /* {{{ exif_read_file
  3617. */
  3618. static int exif_read_file(image_info_type *ImageInfo, char *FileName, int read_thumbnail, int read_all TSRMLS_DC)
  3619. {
  3620. int ret;
  3621. struct stat st;
  3622. /* Start with an empty image information structure. */
  3623. memset(ImageInfo, 0, sizeof(*ImageInfo));
  3624. ImageInfo->motorola_intel = -1; /* flag as unknown */
  3625. ImageInfo->infile = php_stream_open_wrapper(FileName, "rb", STREAM_MUST_SEEK|IGNORE_PATH, NULL);
  3626. if (!ImageInfo->infile) {
  3627. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Unable to open file");
  3628. return FALSE;
  3629. }
  3630. if (php_stream_is(ImageInfo->infile, PHP_STREAM_IS_STDIO)) {
  3631. if (VCWD_STAT(FileName, &st) >= 0) {
  3632. if ((st.st_mode & S_IFMT) != S_IFREG) {
  3633. exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Not a file");
  3634. php_stream_close(ImageInfo->infile);
  3635. return FALSE;
  3636. }
  3637. /* Store file date/time. */
  3638. ImageInfo->FileDateTime = st.st_mtime;
  3639. ImageInfo->FileSize = st.st_size;
  3640. /*exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Opened stream is file: %d", ImageInfo->FileSize);*/
  3641. }
  3642. } else {
  3643. if (!ImageInfo->FileSize) {
  3644. php_stream_seek(ImageInfo->infile, 0, SEEK_END);
  3645. ImageInfo->FileSize = php_stream_tell(ImageInfo->infile);
  3646. php_stream_seek(ImageInfo->infile, 0, SEEK_SET);
  3647. }
  3648. }
  3649. php_basename(FileName, strlen(FileName), NULL, 0, &(ImageInfo->FileName), NULL TSRMLS_CC);
  3650. ImageInfo->read_thumbnail = read_thumbnail;
  3651. ImageInfo->read_all = read_all;
  3652. ImageInfo->Thumbnail.filetype = IMAGE_FILETYPE_UNKNOWN;
  3653. ImageInfo->encode_unicode = safe_estrdup(EXIF_G(encode_unicode));
  3654. ImageInfo->decode_unicode_be = safe_estrdup(EXIF_G(decode_unicode_be));
  3655. ImageInfo->decode_unicode_le = safe_estrdup(EXIF_G(decode_unicode_le));
  3656. ImageInfo->encode_jis = safe_estrdup(EXIF_G(encode_jis));
  3657. ImageInfo->decode_jis_be = safe_estrdup(EXIF_G(decode_jis_be));
  3658. ImageInfo->decode_jis_le = safe_estrdup(EXIF_G(decode_jis_le));
  3659. ImageInfo->ifd_nesting_level = 0;
  3660. /* Scan the JPEG headers. */
  3661. ret = exif_scan_FILE_header(ImageInfo TSRMLS_CC);
  3662. php_stream_close(ImageInfo->infile);
  3663. return ret;
  3664. }
  3665. /* }}} */
  3666. /* {{{ proto array exif_read_data(string filename [, sections_needed [, sub_arrays[, read_thumbnail]]])
  3667. Reads header data from the JPEG/TIFF image filename and optionally reads the internal thumbnails */
  3668. PHP_FUNCTION(exif_read_data)
  3669. {
  3670. char *p_name, *p_sections_needed = NULL;
  3671. int p_name_len, p_sections_needed_len = 0;
  3672. zend_bool sub_arrays=0, read_thumbnail=0, read_all=0;
  3673. int i, ret, sections_needed=0;
  3674. image_info_type ImageInfo;
  3675. char tmp[64], *sections_str, *s;
  3676. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|sbb", &p_name, &p_name_len, &p_sections_needed, &p_sections_needed_len, &sub_arrays, &read_thumbnail) == FAILURE) {
  3677. return;
  3678. }
  3679. memset(&ImageInfo, 0, sizeof(ImageInfo));
  3680. if (p_sections_needed) {
  3681. spprintf(&sections_str, 0, ",%s,", p_sections_needed);
  3682. /* sections_str DOES start with , and SPACES are NOT allowed in names */
  3683. s = sections_str;
  3684. while (*++s) {
  3685. if (*s == ' ') {
  3686. *s = ',';
  3687. }
  3688. }
  3689. for (i = 0; i < SECTION_COUNT; i++) {
  3690. snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
  3691. if (strstr(sections_str, tmp)) {
  3692. sections_needed |= 1<<i;
  3693. }
  3694. }
  3695. EFREE_IF(sections_str);
  3696. /* now see what we need */
  3697. #ifdef EXIF_DEBUG
  3698. sections_str = exif_get_sectionlist(sections_needed TSRMLS_CC);
  3699. if (!sections_str) {
  3700. RETURN_FALSE;
  3701. }
  3702. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections needed: %s", sections_str[0] ? sections_str : "None");
  3703. EFREE_IF(sections_str);
  3704. #endif
  3705. }
  3706. ret = exif_read_file(&ImageInfo, p_name, read_thumbnail, read_all TSRMLS_CC);
  3707. sections_str = exif_get_sectionlist(ImageInfo.sections_found TSRMLS_CC);
  3708. #ifdef EXIF_DEBUG
  3709. if (sections_str)
  3710. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Sections found: %s", sections_str[0] ? sections_str : "None");
  3711. #endif
  3712. ImageInfo.sections_found |= FOUND_COMPUTED|FOUND_FILE;/* do not inform about in debug*/
  3713. if (ret == FALSE || (sections_needed && !(sections_needed&ImageInfo.sections_found))) {
  3714. /* array_init must be checked at last! otherwise the array must be freed if a later test fails. */
  3715. exif_discard_imageinfo(&ImageInfo);
  3716. EFREE_IF(sections_str);
  3717. RETURN_FALSE;
  3718. }
  3719. array_init(return_value);
  3720. #ifdef EXIF_DEBUG
  3721. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section FILE");
  3722. #endif
  3723. /* now we can add our information */
  3724. exif_iif_add_str(&ImageInfo, SECTION_FILE, "FileName", ImageInfo.FileName TSRMLS_CC);
  3725. exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileDateTime", ImageInfo.FileDateTime TSRMLS_CC);
  3726. exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileSize", ImageInfo.FileSize TSRMLS_CC);
  3727. exif_iif_add_int(&ImageInfo, SECTION_FILE, "FileType", ImageInfo.FileType TSRMLS_CC);
  3728. exif_iif_add_str(&ImageInfo, SECTION_FILE, "MimeType", (char*)php_image_type_to_mime_type(ImageInfo.FileType) TSRMLS_CC);
  3729. exif_iif_add_str(&ImageInfo, SECTION_FILE, "SectionsFound", sections_str ? sections_str : "NONE" TSRMLS_CC);
  3730. #ifdef EXIF_DEBUG
  3731. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Generate section COMPUTED");
  3732. #endif
  3733. if (ImageInfo.Width>0 && ImageInfo.Height>0) {
  3734. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "html" TSRMLS_CC, "width=\"%d\" height=\"%d\"", ImageInfo.Width, ImageInfo.Height);
  3735. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Height", ImageInfo.Height TSRMLS_CC);
  3736. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Width", ImageInfo.Width TSRMLS_CC);
  3737. }
  3738. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "IsColor", ImageInfo.IsColor TSRMLS_CC);
  3739. if (ImageInfo.motorola_intel != -1) {
  3740. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "ByteOrderMotorola", ImageInfo.motorola_intel TSRMLS_CC);
  3741. }
  3742. if (ImageInfo.FocalLength) {
  3743. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocalLength" TSRMLS_CC, "%4.1Fmm", ImageInfo.FocalLength);
  3744. if(ImageInfo.CCDWidth) {
  3745. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "35mmFocalLength" TSRMLS_CC, "%dmm", (int)(ImageInfo.FocalLength/ImageInfo.CCDWidth*35+0.5));
  3746. }
  3747. }
  3748. if(ImageInfo.CCDWidth) {
  3749. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "CCDWidth" TSRMLS_CC, "%dmm", (int)ImageInfo.CCDWidth);
  3750. }
  3751. if(ImageInfo.ExposureTime>0) {
  3752. if(ImageInfo.ExposureTime <= 0.5) {
  3753. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s (1/%d)", ImageInfo.ExposureTime, (int)(0.5 + 1/ImageInfo.ExposureTime));
  3754. } else {
  3755. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ExposureTime" TSRMLS_CC, "%0.3F s", ImageInfo.ExposureTime);
  3756. }
  3757. }
  3758. if(ImageInfo.ApertureFNumber) {
  3759. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "ApertureFNumber" TSRMLS_CC, "f/%.1F", ImageInfo.ApertureFNumber);
  3760. }
  3761. if(ImageInfo.Distance) {
  3762. if(ImageInfo.Distance<0) {
  3763. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "FocusDistance", "Infinite" TSRMLS_CC);
  3764. } else {
  3765. exif_iif_add_fmt(&ImageInfo, SECTION_COMPUTED, "FocusDistance" TSRMLS_CC, "%0.2Fm", ImageInfo.Distance);
  3766. }
  3767. }
  3768. if (ImageInfo.UserComment) {
  3769. exif_iif_add_buffer(&ImageInfo, SECTION_COMPUTED, "UserComment", ImageInfo.UserCommentLength, ImageInfo.UserComment TSRMLS_CC);
  3770. if (ImageInfo.UserCommentEncoding && strlen(ImageInfo.UserCommentEncoding)) {
  3771. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "UserCommentEncoding", ImageInfo.UserCommentEncoding TSRMLS_CC);
  3772. }
  3773. }
  3774. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright", ImageInfo.Copyright TSRMLS_CC);
  3775. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Photographer", ImageInfo.CopyrightPhotographer TSRMLS_CC);
  3776. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Copyright.Editor", ImageInfo.CopyrightEditor TSRMLS_CC);
  3777. for (i=0; i<ImageInfo.xp_fields.count; i++) {
  3778. exif_iif_add_str(&ImageInfo, SECTION_WINXP, exif_get_tagname(ImageInfo.xp_fields.list[i].tag, NULL, 0, exif_get_tag_table(SECTION_WINXP) TSRMLS_CC), ImageInfo.xp_fields.list[i].value TSRMLS_CC);
  3779. }
  3780. if (ImageInfo.Thumbnail.size) {
  3781. if (read_thumbnail) {
  3782. /* not exif_iif_add_str : this is a buffer */
  3783. exif_iif_add_tag(&ImageInfo, SECTION_THUMBNAIL, "THUMBNAIL", TAG_NONE, TAG_FMT_UNDEFINED, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.data TSRMLS_CC);
  3784. }
  3785. if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
  3786. /* try to evaluate if thumbnail data is present */
  3787. exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
  3788. }
  3789. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.FileType", ImageInfo.Thumbnail.filetype TSRMLS_CC);
  3790. exif_iif_add_str(&ImageInfo, SECTION_COMPUTED, "Thumbnail.MimeType", (char*)php_image_type_to_mime_type(ImageInfo.Thumbnail.filetype) TSRMLS_CC);
  3791. }
  3792. if (ImageInfo.Thumbnail.width && ImageInfo.Thumbnail.height) {
  3793. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Height", ImageInfo.Thumbnail.height TSRMLS_CC);
  3794. exif_iif_add_int(&ImageInfo, SECTION_COMPUTED, "Thumbnail.Width", ImageInfo.Thumbnail.width TSRMLS_CC);
  3795. }
  3796. EFREE_IF(sections_str);
  3797. #ifdef EXIF_DEBUG
  3798. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Adding image infos");
  3799. #endif
  3800. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FILE TSRMLS_CC);
  3801. add_assoc_image_info(return_value, 1, &ImageInfo, SECTION_COMPUTED TSRMLS_CC);
  3802. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_ANY_TAG TSRMLS_CC);
  3803. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_IFD0 TSRMLS_CC);
  3804. add_assoc_image_info(return_value, 1, &ImageInfo, SECTION_THUMBNAIL TSRMLS_CC);
  3805. add_assoc_image_info(return_value, 1, &ImageInfo, SECTION_COMMENT TSRMLS_CC);
  3806. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_EXIF TSRMLS_CC);
  3807. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_GPS TSRMLS_CC);
  3808. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_INTEROP TSRMLS_CC);
  3809. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_FPIX TSRMLS_CC);
  3810. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_APP12 TSRMLS_CC);
  3811. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_WINXP TSRMLS_CC);
  3812. add_assoc_image_info(return_value, sub_arrays, &ImageInfo, SECTION_MAKERNOTE TSRMLS_CC);
  3813. #ifdef EXIF_DEBUG
  3814. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
  3815. #endif
  3816. exif_discard_imageinfo(&ImageInfo);
  3817. #ifdef EXIF_DEBUG
  3818. php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(p_name), E_NOTICE, "done");
  3819. #endif
  3820. }
  3821. /* }}} */
  3822. /* {{{ proto string exif_thumbnail(string filename [, &width, &height [, &imagetype]])
  3823. Reads the embedded thumbnail */
  3824. PHP_FUNCTION(exif_thumbnail)
  3825. {
  3826. zval *p_width = 0, *p_height = 0, *p_imagetype = 0;
  3827. char *p_name;
  3828. int p_name_len, ret, arg_c = ZEND_NUM_ARGS();
  3829. image_info_type ImageInfo;
  3830. memset(&ImageInfo, 0, sizeof(ImageInfo));
  3831. if (arg_c!=1 && arg_c!=3 && arg_c!=4) {
  3832. WRONG_PARAM_COUNT;
  3833. }
  3834. if (zend_parse_parameters(arg_c TSRMLS_CC, "p|z/z/z/", &p_name, &p_name_len, &p_width, &p_height, &p_imagetype) == FAILURE) {
  3835. return;
  3836. }
  3837. ret = exif_read_file(&ImageInfo, p_name, 1, 0 TSRMLS_CC);
  3838. if (ret==FALSE) {
  3839. exif_discard_imageinfo(&ImageInfo);
  3840. RETURN_FALSE;
  3841. }
  3842. #ifdef EXIF_DEBUG
  3843. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Thumbnail data %d %d %d, %d x %d", ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size, ImageInfo.Thumbnail.filetype, ImageInfo.Thumbnail.width, ImageInfo.Thumbnail.height);
  3844. #endif
  3845. if (!ImageInfo.Thumbnail.data || !ImageInfo.Thumbnail.size) {
  3846. exif_discard_imageinfo(&ImageInfo);
  3847. RETURN_FALSE;
  3848. }
  3849. #ifdef EXIF_DEBUG
  3850. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Returning thumbnail(%d)", ImageInfo.Thumbnail.size);
  3851. #endif
  3852. ZVAL_STRINGL(return_value, ImageInfo.Thumbnail.data, ImageInfo.Thumbnail.size, 1);
  3853. if (arg_c >= 3) {
  3854. if (!ImageInfo.Thumbnail.width || !ImageInfo.Thumbnail.height) {
  3855. exif_scan_thumbnail(&ImageInfo TSRMLS_CC);
  3856. }
  3857. zval_dtor(p_width);
  3858. zval_dtor(p_height);
  3859. ZVAL_LONG(p_width, ImageInfo.Thumbnail.width);
  3860. ZVAL_LONG(p_height, ImageInfo.Thumbnail.height);
  3861. }
  3862. if (arg_c >= 4) {
  3863. zval_dtor(p_imagetype);
  3864. ZVAL_LONG(p_imagetype, ImageInfo.Thumbnail.filetype);
  3865. }
  3866. #ifdef EXIF_DEBUG
  3867. exif_error_docref(NULL EXIFERR_CC, &ImageInfo, E_NOTICE, "Discarding info");
  3868. #endif
  3869. exif_discard_imageinfo(&ImageInfo);
  3870. #ifdef EXIF_DEBUG
  3871. php_error_docref1(NULL TSRMLS_CC, p_name, E_NOTICE, "Done");
  3872. #endif
  3873. }
  3874. /* }}} */
  3875. /* {{{ proto int exif_imagetype(string imagefile)
  3876. Get the type of an image */
  3877. PHP_FUNCTION(exif_imagetype)
  3878. {
  3879. char *imagefile;
  3880. int imagefile_len;
  3881. php_stream * stream;
  3882. int itype = 0;
  3883. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &imagefile, &imagefile_len) == FAILURE) {
  3884. return;
  3885. }
  3886. stream = php_stream_open_wrapper(imagefile, "rb", IGNORE_PATH|REPORT_ERRORS, NULL);
  3887. if (stream == NULL) {
  3888. RETURN_FALSE;
  3889. }
  3890. itype = php_getimagetype(stream, NULL TSRMLS_CC);
  3891. php_stream_close(stream);
  3892. if (itype == IMAGE_FILETYPE_UNKNOWN) {
  3893. RETURN_FALSE;
  3894. } else {
  3895. ZVAL_LONG(return_value, itype);
  3896. }
  3897. }
  3898. /* }}} */
  3899. #endif
  3900. /*
  3901. * Local variables:
  3902. * tab-width: 4
  3903. * c-basic-offset: 4
  3904. * End:
  3905. * vim600: sw=4 ts=4 tw=78 fdm=marker
  3906. * vim<600: sw=4 ts=4 tw=78
  3907. */