exif.c 160 KB

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