tif_dirread.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676
  1. /* $Id: tif_dirread.c,v 1.208 2017-04-27 15:46:22 erouault Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Directory Read Support Routines.
  29. */
  30. /* Suggested pending improvements:
  31. * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
  32. * eliminating current use of the IGNORE value, and therefore eliminating
  33. * current irrational behaviour on tags with tag id code 0
  34. * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
  35. * the pointer to the appropriate TIFFField structure early on in
  36. * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
  37. */
  38. #include "tiffiop.h"
  39. #include <float.h>
  40. #define IGNORE 0 /* tag placeholder used below */
  41. #define FAILED_FII ((uint32) -1)
  42. #ifdef HAVE_IEEEFP
  43. # define TIFFCvtIEEEFloatToNative(tif, n, fp)
  44. # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
  45. #else
  46. extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
  47. extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
  48. #endif
  49. enum TIFFReadDirEntryErr {
  50. TIFFReadDirEntryErrOk = 0,
  51. TIFFReadDirEntryErrCount = 1,
  52. TIFFReadDirEntryErrType = 2,
  53. TIFFReadDirEntryErrIo = 3,
  54. TIFFReadDirEntryErrRange = 4,
  55. TIFFReadDirEntryErrPsdif = 5,
  56. TIFFReadDirEntryErrSizesan = 6,
  57. TIFFReadDirEntryErrAlloc = 7,
  58. };
  59. static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
  60. static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
  61. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
  62. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
  63. static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
  64. static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
  65. static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
  66. static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
  67. static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
  68. static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
  69. static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
  70. static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
  71. static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
  72. static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
  73. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
  74. static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
  75. static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
  76. static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
  77. static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
  78. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
  79. #if 0
  80. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
  81. #endif
  82. static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
  83. static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
  84. static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
  85. static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
  86. static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
  87. static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
  88. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
  89. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
  90. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
  91. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
  92. static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
  93. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
  94. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
  95. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
  96. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
  97. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
  98. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
  99. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
  100. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
  101. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
  102. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
  103. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
  104. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
  105. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
  106. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
  107. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
  108. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
  109. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
  110. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
  111. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
  112. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
  113. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
  114. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
  115. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
  116. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
  117. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
  118. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
  119. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
  120. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
  121. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
  122. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
  123. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
  124. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
  125. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
  126. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
  127. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
  128. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
  129. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
  130. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
  131. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
  132. static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
  133. static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
  134. static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
  135. static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
  136. static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
  137. static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
  138. static void MissingRequired(TIFF*, const char*);
  139. static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
  140. static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
  141. static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
  142. static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
  143. static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
  144. static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
  145. static void ChopUpSingleUncompressedStrip(TIFF*);
  146. static uint64 TIFFReadUInt64(const uint8 *value);
  147. static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount );
  148. typedef union _UInt64Aligned_t
  149. {
  150. double d;
  151. uint64 l;
  152. uint32 i[2];
  153. uint16 s[4];
  154. uint8 c[8];
  155. } UInt64Aligned_t;
  156. /*
  157. Unaligned safe copy of a uint64 value from an octet array.
  158. */
  159. static uint64 TIFFReadUInt64(const uint8 *value)
  160. {
  161. UInt64Aligned_t result;
  162. result.c[0]=value[0];
  163. result.c[1]=value[1];
  164. result.c[2]=value[2];
  165. result.c[3]=value[3];
  166. result.c[4]=value[4];
  167. result.c[5]=value[5];
  168. result.c[6]=value[6];
  169. result.c[7]=value[7];
  170. return result.l;
  171. }
  172. static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
  173. {
  174. enum TIFFReadDirEntryErr err;
  175. if (direntry->tdir_count!=1)
  176. return(TIFFReadDirEntryErrCount);
  177. switch (direntry->tdir_type)
  178. {
  179. case TIFF_BYTE:
  180. TIFFReadDirEntryCheckedByte(tif,direntry,value);
  181. return(TIFFReadDirEntryErrOk);
  182. case TIFF_SBYTE:
  183. {
  184. int8 m;
  185. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  186. err=TIFFReadDirEntryCheckRangeByteSbyte(m);
  187. if (err!=TIFFReadDirEntryErrOk)
  188. return(err);
  189. *value=(uint8)m;
  190. return(TIFFReadDirEntryErrOk);
  191. }
  192. case TIFF_SHORT:
  193. {
  194. uint16 m;
  195. TIFFReadDirEntryCheckedShort(tif,direntry,&m);
  196. err=TIFFReadDirEntryCheckRangeByteShort(m);
  197. if (err!=TIFFReadDirEntryErrOk)
  198. return(err);
  199. *value=(uint8)m;
  200. return(TIFFReadDirEntryErrOk);
  201. }
  202. case TIFF_SSHORT:
  203. {
  204. int16 m;
  205. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  206. err=TIFFReadDirEntryCheckRangeByteSshort(m);
  207. if (err!=TIFFReadDirEntryErrOk)
  208. return(err);
  209. *value=(uint8)m;
  210. return(TIFFReadDirEntryErrOk);
  211. }
  212. case TIFF_LONG:
  213. {
  214. uint32 m;
  215. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  216. err=TIFFReadDirEntryCheckRangeByteLong(m);
  217. if (err!=TIFFReadDirEntryErrOk)
  218. return(err);
  219. *value=(uint8)m;
  220. return(TIFFReadDirEntryErrOk);
  221. }
  222. case TIFF_SLONG:
  223. {
  224. int32 m;
  225. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  226. err=TIFFReadDirEntryCheckRangeByteSlong(m);
  227. if (err!=TIFFReadDirEntryErrOk)
  228. return(err);
  229. *value=(uint8)m;
  230. return(TIFFReadDirEntryErrOk);
  231. }
  232. case TIFF_LONG8:
  233. {
  234. uint64 m;
  235. err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
  236. if (err!=TIFFReadDirEntryErrOk)
  237. return(err);
  238. err=TIFFReadDirEntryCheckRangeByteLong8(m);
  239. if (err!=TIFFReadDirEntryErrOk)
  240. return(err);
  241. *value=(uint8)m;
  242. return(TIFFReadDirEntryErrOk);
  243. }
  244. case TIFF_SLONG8:
  245. {
  246. int64 m;
  247. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  248. if (err!=TIFFReadDirEntryErrOk)
  249. return(err);
  250. err=TIFFReadDirEntryCheckRangeByteSlong8(m);
  251. if (err!=TIFFReadDirEntryErrOk)
  252. return(err);
  253. *value=(uint8)m;
  254. return(TIFFReadDirEntryErrOk);
  255. }
  256. default:
  257. return(TIFFReadDirEntryErrType);
  258. }
  259. }
  260. static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
  261. {
  262. enum TIFFReadDirEntryErr err;
  263. if (direntry->tdir_count!=1)
  264. return(TIFFReadDirEntryErrCount);
  265. switch (direntry->tdir_type)
  266. {
  267. case TIFF_BYTE:
  268. {
  269. uint8 m;
  270. TIFFReadDirEntryCheckedByte(tif,direntry,&m);
  271. *value=(uint16)m;
  272. return(TIFFReadDirEntryErrOk);
  273. }
  274. case TIFF_SBYTE:
  275. {
  276. int8 m;
  277. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  278. err=TIFFReadDirEntryCheckRangeShortSbyte(m);
  279. if (err!=TIFFReadDirEntryErrOk)
  280. return(err);
  281. *value=(uint16)m;
  282. return(TIFFReadDirEntryErrOk);
  283. }
  284. case TIFF_SHORT:
  285. TIFFReadDirEntryCheckedShort(tif,direntry,value);
  286. return(TIFFReadDirEntryErrOk);
  287. case TIFF_SSHORT:
  288. {
  289. int16 m;
  290. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  291. err=TIFFReadDirEntryCheckRangeShortSshort(m);
  292. if (err!=TIFFReadDirEntryErrOk)
  293. return(err);
  294. *value=(uint16)m;
  295. return(TIFFReadDirEntryErrOk);
  296. }
  297. case TIFF_LONG:
  298. {
  299. uint32 m;
  300. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  301. err=TIFFReadDirEntryCheckRangeShortLong(m);
  302. if (err!=TIFFReadDirEntryErrOk)
  303. return(err);
  304. *value=(uint16)m;
  305. return(TIFFReadDirEntryErrOk);
  306. }
  307. case TIFF_SLONG:
  308. {
  309. int32 m;
  310. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  311. err=TIFFReadDirEntryCheckRangeShortSlong(m);
  312. if (err!=TIFFReadDirEntryErrOk)
  313. return(err);
  314. *value=(uint16)m;
  315. return(TIFFReadDirEntryErrOk);
  316. }
  317. case TIFF_LONG8:
  318. {
  319. uint64 m;
  320. err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
  321. if (err!=TIFFReadDirEntryErrOk)
  322. return(err);
  323. err=TIFFReadDirEntryCheckRangeShortLong8(m);
  324. if (err!=TIFFReadDirEntryErrOk)
  325. return(err);
  326. *value=(uint16)m;
  327. return(TIFFReadDirEntryErrOk);
  328. }
  329. case TIFF_SLONG8:
  330. {
  331. int64 m;
  332. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  333. if (err!=TIFFReadDirEntryErrOk)
  334. return(err);
  335. err=TIFFReadDirEntryCheckRangeShortSlong8(m);
  336. if (err!=TIFFReadDirEntryErrOk)
  337. return(err);
  338. *value=(uint16)m;
  339. return(TIFFReadDirEntryErrOk);
  340. }
  341. default:
  342. return(TIFFReadDirEntryErrType);
  343. }
  344. }
  345. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
  346. {
  347. enum TIFFReadDirEntryErr err;
  348. if (direntry->tdir_count!=1)
  349. return(TIFFReadDirEntryErrCount);
  350. switch (direntry->tdir_type)
  351. {
  352. case TIFF_BYTE:
  353. {
  354. uint8 m;
  355. TIFFReadDirEntryCheckedByte(tif,direntry,&m);
  356. *value=(uint32)m;
  357. return(TIFFReadDirEntryErrOk);
  358. }
  359. case TIFF_SBYTE:
  360. {
  361. int8 m;
  362. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  363. err=TIFFReadDirEntryCheckRangeLongSbyte(m);
  364. if (err!=TIFFReadDirEntryErrOk)
  365. return(err);
  366. *value=(uint32)m;
  367. return(TIFFReadDirEntryErrOk);
  368. }
  369. case TIFF_SHORT:
  370. {
  371. uint16 m;
  372. TIFFReadDirEntryCheckedShort(tif,direntry,&m);
  373. *value=(uint32)m;
  374. return(TIFFReadDirEntryErrOk);
  375. }
  376. case TIFF_SSHORT:
  377. {
  378. int16 m;
  379. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  380. err=TIFFReadDirEntryCheckRangeLongSshort(m);
  381. if (err!=TIFFReadDirEntryErrOk)
  382. return(err);
  383. *value=(uint32)m;
  384. return(TIFFReadDirEntryErrOk);
  385. }
  386. case TIFF_LONG:
  387. TIFFReadDirEntryCheckedLong(tif,direntry,value);
  388. return(TIFFReadDirEntryErrOk);
  389. case TIFF_SLONG:
  390. {
  391. int32 m;
  392. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  393. err=TIFFReadDirEntryCheckRangeLongSlong(m);
  394. if (err!=TIFFReadDirEntryErrOk)
  395. return(err);
  396. *value=(uint32)m;
  397. return(TIFFReadDirEntryErrOk);
  398. }
  399. case TIFF_LONG8:
  400. {
  401. uint64 m;
  402. err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
  403. if (err!=TIFFReadDirEntryErrOk)
  404. return(err);
  405. err=TIFFReadDirEntryCheckRangeLongLong8(m);
  406. if (err!=TIFFReadDirEntryErrOk)
  407. return(err);
  408. *value=(uint32)m;
  409. return(TIFFReadDirEntryErrOk);
  410. }
  411. case TIFF_SLONG8:
  412. {
  413. int64 m;
  414. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  415. if (err!=TIFFReadDirEntryErrOk)
  416. return(err);
  417. err=TIFFReadDirEntryCheckRangeLongSlong8(m);
  418. if (err!=TIFFReadDirEntryErrOk)
  419. return(err);
  420. *value=(uint32)m;
  421. return(TIFFReadDirEntryErrOk);
  422. }
  423. default:
  424. return(TIFFReadDirEntryErrType);
  425. }
  426. }
  427. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
  428. {
  429. enum TIFFReadDirEntryErr err;
  430. if (direntry->tdir_count!=1)
  431. return(TIFFReadDirEntryErrCount);
  432. switch (direntry->tdir_type)
  433. {
  434. case TIFF_BYTE:
  435. {
  436. uint8 m;
  437. TIFFReadDirEntryCheckedByte(tif,direntry,&m);
  438. *value=(uint64)m;
  439. return(TIFFReadDirEntryErrOk);
  440. }
  441. case TIFF_SBYTE:
  442. {
  443. int8 m;
  444. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  445. err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
  446. if (err!=TIFFReadDirEntryErrOk)
  447. return(err);
  448. *value=(uint64)m;
  449. return(TIFFReadDirEntryErrOk);
  450. }
  451. case TIFF_SHORT:
  452. {
  453. uint16 m;
  454. TIFFReadDirEntryCheckedShort(tif,direntry,&m);
  455. *value=(uint64)m;
  456. return(TIFFReadDirEntryErrOk);
  457. }
  458. case TIFF_SSHORT:
  459. {
  460. int16 m;
  461. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  462. err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
  463. if (err!=TIFFReadDirEntryErrOk)
  464. return(err);
  465. *value=(uint64)m;
  466. return(TIFFReadDirEntryErrOk);
  467. }
  468. case TIFF_LONG:
  469. {
  470. uint32 m;
  471. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  472. *value=(uint64)m;
  473. return(TIFFReadDirEntryErrOk);
  474. }
  475. case TIFF_SLONG:
  476. {
  477. int32 m;
  478. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  479. err=TIFFReadDirEntryCheckRangeLong8Slong(m);
  480. if (err!=TIFFReadDirEntryErrOk)
  481. return(err);
  482. *value=(uint64)m;
  483. return(TIFFReadDirEntryErrOk);
  484. }
  485. case TIFF_LONG8:
  486. err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
  487. return(err);
  488. case TIFF_SLONG8:
  489. {
  490. int64 m;
  491. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  492. if (err!=TIFFReadDirEntryErrOk)
  493. return(err);
  494. err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
  495. if (err!=TIFFReadDirEntryErrOk)
  496. return(err);
  497. *value=(uint64)m;
  498. return(TIFFReadDirEntryErrOk);
  499. }
  500. default:
  501. return(TIFFReadDirEntryErrType);
  502. }
  503. }
  504. static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
  505. {
  506. enum TIFFReadDirEntryErr err;
  507. if (direntry->tdir_count!=1)
  508. return(TIFFReadDirEntryErrCount);
  509. switch (direntry->tdir_type)
  510. {
  511. case TIFF_BYTE:
  512. {
  513. uint8 m;
  514. TIFFReadDirEntryCheckedByte(tif,direntry,&m);
  515. *value=(float)m;
  516. return(TIFFReadDirEntryErrOk);
  517. }
  518. case TIFF_SBYTE:
  519. {
  520. int8 m;
  521. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  522. *value=(float)m;
  523. return(TIFFReadDirEntryErrOk);
  524. }
  525. case TIFF_SHORT:
  526. {
  527. uint16 m;
  528. TIFFReadDirEntryCheckedShort(tif,direntry,&m);
  529. *value=(float)m;
  530. return(TIFFReadDirEntryErrOk);
  531. }
  532. case TIFF_SSHORT:
  533. {
  534. int16 m;
  535. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  536. *value=(float)m;
  537. return(TIFFReadDirEntryErrOk);
  538. }
  539. case TIFF_LONG:
  540. {
  541. uint32 m;
  542. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  543. *value=(float)m;
  544. return(TIFFReadDirEntryErrOk);
  545. }
  546. case TIFF_SLONG:
  547. {
  548. int32 m;
  549. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  550. *value=(float)m;
  551. return(TIFFReadDirEntryErrOk);
  552. }
  553. case TIFF_LONG8:
  554. {
  555. uint64 m;
  556. err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
  557. if (err!=TIFFReadDirEntryErrOk)
  558. return(err);
  559. #if defined(__WIN32__) && (_MSC_VER < 1500)
  560. /*
  561. * XXX: MSVC 6.0 does not support conversion
  562. * of 64-bit integers into floating point
  563. * values.
  564. */
  565. *value = _TIFFUInt64ToFloat(m);
  566. #else
  567. *value=(float)m;
  568. #endif
  569. return(TIFFReadDirEntryErrOk);
  570. }
  571. case TIFF_SLONG8:
  572. {
  573. int64 m;
  574. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  575. if (err!=TIFFReadDirEntryErrOk)
  576. return(err);
  577. *value=(float)m;
  578. return(TIFFReadDirEntryErrOk);
  579. }
  580. case TIFF_RATIONAL:
  581. {
  582. double m;
  583. err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
  584. if (err!=TIFFReadDirEntryErrOk)
  585. return(err);
  586. *value=(float)m;
  587. return(TIFFReadDirEntryErrOk);
  588. }
  589. case TIFF_SRATIONAL:
  590. {
  591. double m;
  592. err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
  593. if (err!=TIFFReadDirEntryErrOk)
  594. return(err);
  595. *value=(float)m;
  596. return(TIFFReadDirEntryErrOk);
  597. }
  598. case TIFF_FLOAT:
  599. TIFFReadDirEntryCheckedFloat(tif,direntry,value);
  600. return(TIFFReadDirEntryErrOk);
  601. case TIFF_DOUBLE:
  602. {
  603. double m;
  604. err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
  605. if (err!=TIFFReadDirEntryErrOk)
  606. return(err);
  607. *value=(float)m;
  608. return(TIFFReadDirEntryErrOk);
  609. }
  610. default:
  611. return(TIFFReadDirEntryErrType);
  612. }
  613. }
  614. static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
  615. {
  616. enum TIFFReadDirEntryErr err;
  617. if (direntry->tdir_count!=1)
  618. return(TIFFReadDirEntryErrCount);
  619. switch (direntry->tdir_type)
  620. {
  621. case TIFF_BYTE:
  622. {
  623. uint8 m;
  624. TIFFReadDirEntryCheckedByte(tif,direntry,&m);
  625. *value=(double)m;
  626. return(TIFFReadDirEntryErrOk);
  627. }
  628. case TIFF_SBYTE:
  629. {
  630. int8 m;
  631. TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
  632. *value=(double)m;
  633. return(TIFFReadDirEntryErrOk);
  634. }
  635. case TIFF_SHORT:
  636. {
  637. uint16 m;
  638. TIFFReadDirEntryCheckedShort(tif,direntry,&m);
  639. *value=(double)m;
  640. return(TIFFReadDirEntryErrOk);
  641. }
  642. case TIFF_SSHORT:
  643. {
  644. int16 m;
  645. TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
  646. *value=(double)m;
  647. return(TIFFReadDirEntryErrOk);
  648. }
  649. case TIFF_LONG:
  650. {
  651. uint32 m;
  652. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  653. *value=(double)m;
  654. return(TIFFReadDirEntryErrOk);
  655. }
  656. case TIFF_SLONG:
  657. {
  658. int32 m;
  659. TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
  660. *value=(double)m;
  661. return(TIFFReadDirEntryErrOk);
  662. }
  663. case TIFF_LONG8:
  664. {
  665. uint64 m;
  666. err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
  667. if (err!=TIFFReadDirEntryErrOk)
  668. return(err);
  669. #if defined(__WIN32__) && (_MSC_VER < 1500)
  670. /*
  671. * XXX: MSVC 6.0 does not support conversion
  672. * of 64-bit integers into floating point
  673. * values.
  674. */
  675. *value = _TIFFUInt64ToDouble(m);
  676. #else
  677. *value = (double)m;
  678. #endif
  679. return(TIFFReadDirEntryErrOk);
  680. }
  681. case TIFF_SLONG8:
  682. {
  683. int64 m;
  684. err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
  685. if (err!=TIFFReadDirEntryErrOk)
  686. return(err);
  687. *value=(double)m;
  688. return(TIFFReadDirEntryErrOk);
  689. }
  690. case TIFF_RATIONAL:
  691. err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
  692. return(err);
  693. case TIFF_SRATIONAL:
  694. err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
  695. return(err);
  696. case TIFF_FLOAT:
  697. {
  698. float m;
  699. TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
  700. *value=(double)m;
  701. return(TIFFReadDirEntryErrOk);
  702. }
  703. case TIFF_DOUBLE:
  704. err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
  705. return(err);
  706. default:
  707. return(TIFFReadDirEntryErrType);
  708. }
  709. }
  710. static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
  711. {
  712. enum TIFFReadDirEntryErr err;
  713. if (direntry->tdir_count!=1)
  714. return(TIFFReadDirEntryErrCount);
  715. switch (direntry->tdir_type)
  716. {
  717. case TIFF_LONG:
  718. case TIFF_IFD:
  719. {
  720. uint32 m;
  721. TIFFReadDirEntryCheckedLong(tif,direntry,&m);
  722. *value=(uint64)m;
  723. return(TIFFReadDirEntryErrOk);
  724. }
  725. case TIFF_LONG8:
  726. case TIFF_IFD8:
  727. err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
  728. return(err);
  729. default:
  730. return(TIFFReadDirEntryErrType);
  731. }
  732. }
  733. static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
  734. {
  735. int typesize;
  736. uint32 datasize;
  737. void* data;
  738. typesize=TIFFDataWidth(direntry->tdir_type);
  739. if ((direntry->tdir_count==0)||(typesize==0))
  740. {
  741. *value=0;
  742. return(TIFFReadDirEntryErrOk);
  743. }
  744. (void) desttypesize;
  745. /*
  746. * As a sanity check, make sure we have no more than a 2GB tag array
  747. * in either the current data type or the dest data type. This also
  748. * avoids problems with overflow of tmsize_t on 32bit systems.
  749. */
  750. if ((uint64)(2147483647/typesize)<direntry->tdir_count)
  751. return(TIFFReadDirEntryErrSizesan);
  752. if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
  753. return(TIFFReadDirEntryErrSizesan);
  754. *count=(uint32)direntry->tdir_count;
  755. datasize=(*count)*typesize;
  756. assert((tmsize_t)datasize>0);
  757. data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
  758. if (data==0)
  759. return(TIFFReadDirEntryErrAlloc);
  760. if (!(tif->tif_flags&TIFF_BIGTIFF))
  761. {
  762. if (datasize<=4)
  763. _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
  764. else
  765. {
  766. enum TIFFReadDirEntryErr err;
  767. uint32 offset = direntry->tdir_offset.toff_long;
  768. if (tif->tif_flags&TIFF_SWAB)
  769. TIFFSwabLong(&offset);
  770. err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
  771. if (err!=TIFFReadDirEntryErrOk)
  772. {
  773. _TIFFfree(data);
  774. return(err);
  775. }
  776. }
  777. }
  778. else
  779. {
  780. if (datasize<=8)
  781. _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
  782. else
  783. {
  784. enum TIFFReadDirEntryErr err;
  785. uint64 offset = direntry->tdir_offset.toff_long8;
  786. if (tif->tif_flags&TIFF_SWAB)
  787. TIFFSwabLong8(&offset);
  788. err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
  789. if (err!=TIFFReadDirEntryErrOk)
  790. {
  791. _TIFFfree(data);
  792. return(err);
  793. }
  794. }
  795. }
  796. *value=data;
  797. return(TIFFReadDirEntryErrOk);
  798. }
  799. static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
  800. {
  801. enum TIFFReadDirEntryErr err;
  802. uint32 count;
  803. void* origdata;
  804. uint8* data;
  805. switch (direntry->tdir_type)
  806. {
  807. case TIFF_ASCII:
  808. case TIFF_UNDEFINED:
  809. case TIFF_BYTE:
  810. case TIFF_SBYTE:
  811. case TIFF_SHORT:
  812. case TIFF_SSHORT:
  813. case TIFF_LONG:
  814. case TIFF_SLONG:
  815. case TIFF_LONG8:
  816. case TIFF_SLONG8:
  817. break;
  818. default:
  819. return(TIFFReadDirEntryErrType);
  820. }
  821. err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
  822. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  823. {
  824. *value=0;
  825. return(err);
  826. }
  827. switch (direntry->tdir_type)
  828. {
  829. case TIFF_ASCII:
  830. case TIFF_UNDEFINED:
  831. case TIFF_BYTE:
  832. *value=(uint8*)origdata;
  833. return(TIFFReadDirEntryErrOk);
  834. case TIFF_SBYTE:
  835. {
  836. int8* m;
  837. uint32 n;
  838. m=(int8*)origdata;
  839. for (n=0; n<count; n++)
  840. {
  841. err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
  842. if (err!=TIFFReadDirEntryErrOk)
  843. {
  844. _TIFFfree(origdata);
  845. return(err);
  846. }
  847. m++;
  848. }
  849. *value=(uint8*)origdata;
  850. return(TIFFReadDirEntryErrOk);
  851. }
  852. }
  853. data=(uint8*)_TIFFmalloc(count);
  854. if (data==0)
  855. {
  856. _TIFFfree(origdata);
  857. return(TIFFReadDirEntryErrAlloc);
  858. }
  859. switch (direntry->tdir_type)
  860. {
  861. case TIFF_SHORT:
  862. {
  863. uint16* ma;
  864. uint8* mb;
  865. uint32 n;
  866. ma=(uint16*)origdata;
  867. mb=data;
  868. for (n=0; n<count; n++)
  869. {
  870. if (tif->tif_flags&TIFF_SWAB)
  871. TIFFSwabShort(ma);
  872. err=TIFFReadDirEntryCheckRangeByteShort(*ma);
  873. if (err!=TIFFReadDirEntryErrOk)
  874. break;
  875. *mb++=(uint8)(*ma++);
  876. }
  877. }
  878. break;
  879. case TIFF_SSHORT:
  880. {
  881. int16* ma;
  882. uint8* mb;
  883. uint32 n;
  884. ma=(int16*)origdata;
  885. mb=data;
  886. for (n=0; n<count; n++)
  887. {
  888. if (tif->tif_flags&TIFF_SWAB)
  889. TIFFSwabShort((uint16*)ma);
  890. err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
  891. if (err!=TIFFReadDirEntryErrOk)
  892. break;
  893. *mb++=(uint8)(*ma++);
  894. }
  895. }
  896. break;
  897. case TIFF_LONG:
  898. {
  899. uint32* ma;
  900. uint8* mb;
  901. uint32 n;
  902. ma=(uint32*)origdata;
  903. mb=data;
  904. for (n=0; n<count; n++)
  905. {
  906. if (tif->tif_flags&TIFF_SWAB)
  907. TIFFSwabLong(ma);
  908. err=TIFFReadDirEntryCheckRangeByteLong(*ma);
  909. if (err!=TIFFReadDirEntryErrOk)
  910. break;
  911. *mb++=(uint8)(*ma++);
  912. }
  913. }
  914. break;
  915. case TIFF_SLONG:
  916. {
  917. int32* ma;
  918. uint8* mb;
  919. uint32 n;
  920. ma=(int32*)origdata;
  921. mb=data;
  922. for (n=0; n<count; n++)
  923. {
  924. if (tif->tif_flags&TIFF_SWAB)
  925. TIFFSwabLong((uint32*)ma);
  926. err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
  927. if (err!=TIFFReadDirEntryErrOk)
  928. break;
  929. *mb++=(uint8)(*ma++);
  930. }
  931. }
  932. break;
  933. case TIFF_LONG8:
  934. {
  935. uint64* ma;
  936. uint8* mb;
  937. uint32 n;
  938. ma=(uint64*)origdata;
  939. mb=data;
  940. for (n=0; n<count; n++)
  941. {
  942. if (tif->tif_flags&TIFF_SWAB)
  943. TIFFSwabLong8(ma);
  944. err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
  945. if (err!=TIFFReadDirEntryErrOk)
  946. break;
  947. *mb++=(uint8)(*ma++);
  948. }
  949. }
  950. break;
  951. case TIFF_SLONG8:
  952. {
  953. int64* ma;
  954. uint8* mb;
  955. uint32 n;
  956. ma=(int64*)origdata;
  957. mb=data;
  958. for (n=0; n<count; n++)
  959. {
  960. if (tif->tif_flags&TIFF_SWAB)
  961. TIFFSwabLong8((uint64*)ma);
  962. err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
  963. if (err!=TIFFReadDirEntryErrOk)
  964. break;
  965. *mb++=(uint8)(*ma++);
  966. }
  967. }
  968. break;
  969. }
  970. _TIFFfree(origdata);
  971. if (err!=TIFFReadDirEntryErrOk)
  972. {
  973. _TIFFfree(data);
  974. return(err);
  975. }
  976. *value=data;
  977. return(TIFFReadDirEntryErrOk);
  978. }
  979. static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
  980. {
  981. enum TIFFReadDirEntryErr err;
  982. uint32 count;
  983. void* origdata;
  984. int8* data;
  985. switch (direntry->tdir_type)
  986. {
  987. case TIFF_UNDEFINED:
  988. case TIFF_BYTE:
  989. case TIFF_SBYTE:
  990. case TIFF_SHORT:
  991. case TIFF_SSHORT:
  992. case TIFF_LONG:
  993. case TIFF_SLONG:
  994. case TIFF_LONG8:
  995. case TIFF_SLONG8:
  996. break;
  997. default:
  998. return(TIFFReadDirEntryErrType);
  999. }
  1000. err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
  1001. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1002. {
  1003. *value=0;
  1004. return(err);
  1005. }
  1006. switch (direntry->tdir_type)
  1007. {
  1008. case TIFF_UNDEFINED:
  1009. case TIFF_BYTE:
  1010. {
  1011. uint8* m;
  1012. uint32 n;
  1013. m=(uint8*)origdata;
  1014. for (n=0; n<count; n++)
  1015. {
  1016. err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
  1017. if (err!=TIFFReadDirEntryErrOk)
  1018. {
  1019. _TIFFfree(origdata);
  1020. return(err);
  1021. }
  1022. m++;
  1023. }
  1024. *value=(int8*)origdata;
  1025. return(TIFFReadDirEntryErrOk);
  1026. }
  1027. case TIFF_SBYTE:
  1028. *value=(int8*)origdata;
  1029. return(TIFFReadDirEntryErrOk);
  1030. }
  1031. data=(int8*)_TIFFmalloc(count);
  1032. if (data==0)
  1033. {
  1034. _TIFFfree(origdata);
  1035. return(TIFFReadDirEntryErrAlloc);
  1036. }
  1037. switch (direntry->tdir_type)
  1038. {
  1039. case TIFF_SHORT:
  1040. {
  1041. uint16* ma;
  1042. int8* mb;
  1043. uint32 n;
  1044. ma=(uint16*)origdata;
  1045. mb=data;
  1046. for (n=0; n<count; n++)
  1047. {
  1048. if (tif->tif_flags&TIFF_SWAB)
  1049. TIFFSwabShort(ma);
  1050. err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
  1051. if (err!=TIFFReadDirEntryErrOk)
  1052. break;
  1053. *mb++=(int8)(*ma++);
  1054. }
  1055. }
  1056. break;
  1057. case TIFF_SSHORT:
  1058. {
  1059. int16* ma;
  1060. int8* mb;
  1061. uint32 n;
  1062. ma=(int16*)origdata;
  1063. mb=data;
  1064. for (n=0; n<count; n++)
  1065. {
  1066. if (tif->tif_flags&TIFF_SWAB)
  1067. TIFFSwabShort((uint16*)ma);
  1068. err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
  1069. if (err!=TIFFReadDirEntryErrOk)
  1070. break;
  1071. *mb++=(int8)(*ma++);
  1072. }
  1073. }
  1074. break;
  1075. case TIFF_LONG:
  1076. {
  1077. uint32* ma;
  1078. int8* mb;
  1079. uint32 n;
  1080. ma=(uint32*)origdata;
  1081. mb=data;
  1082. for (n=0; n<count; n++)
  1083. {
  1084. if (tif->tif_flags&TIFF_SWAB)
  1085. TIFFSwabLong(ma);
  1086. err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
  1087. if (err!=TIFFReadDirEntryErrOk)
  1088. break;
  1089. *mb++=(int8)(*ma++);
  1090. }
  1091. }
  1092. break;
  1093. case TIFF_SLONG:
  1094. {
  1095. int32* ma;
  1096. int8* mb;
  1097. uint32 n;
  1098. ma=(int32*)origdata;
  1099. mb=data;
  1100. for (n=0; n<count; n++)
  1101. {
  1102. if (tif->tif_flags&TIFF_SWAB)
  1103. TIFFSwabLong((uint32*)ma);
  1104. err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
  1105. if (err!=TIFFReadDirEntryErrOk)
  1106. break;
  1107. *mb++=(int8)(*ma++);
  1108. }
  1109. }
  1110. break;
  1111. case TIFF_LONG8:
  1112. {
  1113. uint64* ma;
  1114. int8* mb;
  1115. uint32 n;
  1116. ma=(uint64*)origdata;
  1117. mb=data;
  1118. for (n=0; n<count; n++)
  1119. {
  1120. if (tif->tif_flags&TIFF_SWAB)
  1121. TIFFSwabLong8(ma);
  1122. err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
  1123. if (err!=TIFFReadDirEntryErrOk)
  1124. break;
  1125. *mb++=(int8)(*ma++);
  1126. }
  1127. }
  1128. break;
  1129. case TIFF_SLONG8:
  1130. {
  1131. int64* ma;
  1132. int8* mb;
  1133. uint32 n;
  1134. ma=(int64*)origdata;
  1135. mb=data;
  1136. for (n=0; n<count; n++)
  1137. {
  1138. if (tif->tif_flags&TIFF_SWAB)
  1139. TIFFSwabLong8((uint64*)ma);
  1140. err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
  1141. if (err!=TIFFReadDirEntryErrOk)
  1142. break;
  1143. *mb++=(int8)(*ma++);
  1144. }
  1145. }
  1146. break;
  1147. }
  1148. _TIFFfree(origdata);
  1149. if (err!=TIFFReadDirEntryErrOk)
  1150. {
  1151. _TIFFfree(data);
  1152. return(err);
  1153. }
  1154. *value=data;
  1155. return(TIFFReadDirEntryErrOk);
  1156. }
  1157. static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
  1158. {
  1159. enum TIFFReadDirEntryErr err;
  1160. uint32 count;
  1161. void* origdata;
  1162. uint16* data;
  1163. switch (direntry->tdir_type)
  1164. {
  1165. case TIFF_BYTE:
  1166. case TIFF_SBYTE:
  1167. case TIFF_SHORT:
  1168. case TIFF_SSHORT:
  1169. case TIFF_LONG:
  1170. case TIFF_SLONG:
  1171. case TIFF_LONG8:
  1172. case TIFF_SLONG8:
  1173. break;
  1174. default:
  1175. return(TIFFReadDirEntryErrType);
  1176. }
  1177. err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
  1178. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1179. {
  1180. *value=0;
  1181. return(err);
  1182. }
  1183. switch (direntry->tdir_type)
  1184. {
  1185. case TIFF_SHORT:
  1186. *value=(uint16*)origdata;
  1187. if (tif->tif_flags&TIFF_SWAB)
  1188. TIFFSwabArrayOfShort(*value,count);
  1189. return(TIFFReadDirEntryErrOk);
  1190. case TIFF_SSHORT:
  1191. {
  1192. int16* m;
  1193. uint32 n;
  1194. m=(int16*)origdata;
  1195. for (n=0; n<count; n++)
  1196. {
  1197. if (tif->tif_flags&TIFF_SWAB)
  1198. TIFFSwabShort((uint16*)m);
  1199. err=TIFFReadDirEntryCheckRangeShortSshort(*m);
  1200. if (err!=TIFFReadDirEntryErrOk)
  1201. {
  1202. _TIFFfree(origdata);
  1203. return(err);
  1204. }
  1205. m++;
  1206. }
  1207. *value=(uint16*)origdata;
  1208. return(TIFFReadDirEntryErrOk);
  1209. }
  1210. }
  1211. data=(uint16*)_TIFFmalloc(count*2);
  1212. if (data==0)
  1213. {
  1214. _TIFFfree(origdata);
  1215. return(TIFFReadDirEntryErrAlloc);
  1216. }
  1217. switch (direntry->tdir_type)
  1218. {
  1219. case TIFF_BYTE:
  1220. {
  1221. uint8* ma;
  1222. uint16* mb;
  1223. uint32 n;
  1224. ma=(uint8*)origdata;
  1225. mb=data;
  1226. for (n=0; n<count; n++)
  1227. *mb++=(uint16)(*ma++);
  1228. }
  1229. break;
  1230. case TIFF_SBYTE:
  1231. {
  1232. int8* ma;
  1233. uint16* mb;
  1234. uint32 n;
  1235. ma=(int8*)origdata;
  1236. mb=data;
  1237. for (n=0; n<count; n++)
  1238. {
  1239. err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
  1240. if (err!=TIFFReadDirEntryErrOk)
  1241. break;
  1242. *mb++=(uint16)(*ma++);
  1243. }
  1244. }
  1245. break;
  1246. case TIFF_LONG:
  1247. {
  1248. uint32* ma;
  1249. uint16* mb;
  1250. uint32 n;
  1251. ma=(uint32*)origdata;
  1252. mb=data;
  1253. for (n=0; n<count; n++)
  1254. {
  1255. if (tif->tif_flags&TIFF_SWAB)
  1256. TIFFSwabLong(ma);
  1257. err=TIFFReadDirEntryCheckRangeShortLong(*ma);
  1258. if (err!=TIFFReadDirEntryErrOk)
  1259. break;
  1260. *mb++=(uint16)(*ma++);
  1261. }
  1262. }
  1263. break;
  1264. case TIFF_SLONG:
  1265. {
  1266. int32* ma;
  1267. uint16* mb;
  1268. uint32 n;
  1269. ma=(int32*)origdata;
  1270. mb=data;
  1271. for (n=0; n<count; n++)
  1272. {
  1273. if (tif->tif_flags&TIFF_SWAB)
  1274. TIFFSwabLong((uint32*)ma);
  1275. err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
  1276. if (err!=TIFFReadDirEntryErrOk)
  1277. break;
  1278. *mb++=(uint16)(*ma++);
  1279. }
  1280. }
  1281. break;
  1282. case TIFF_LONG8:
  1283. {
  1284. uint64* ma;
  1285. uint16* mb;
  1286. uint32 n;
  1287. ma=(uint64*)origdata;
  1288. mb=data;
  1289. for (n=0; n<count; n++)
  1290. {
  1291. if (tif->tif_flags&TIFF_SWAB)
  1292. TIFFSwabLong8(ma);
  1293. err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
  1294. if (err!=TIFFReadDirEntryErrOk)
  1295. break;
  1296. *mb++=(uint16)(*ma++);
  1297. }
  1298. }
  1299. break;
  1300. case TIFF_SLONG8:
  1301. {
  1302. int64* ma;
  1303. uint16* mb;
  1304. uint32 n;
  1305. ma=(int64*)origdata;
  1306. mb=data;
  1307. for (n=0; n<count; n++)
  1308. {
  1309. if (tif->tif_flags&TIFF_SWAB)
  1310. TIFFSwabLong8((uint64*)ma);
  1311. err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
  1312. if (err!=TIFFReadDirEntryErrOk)
  1313. break;
  1314. *mb++=(uint16)(*ma++);
  1315. }
  1316. }
  1317. break;
  1318. }
  1319. _TIFFfree(origdata);
  1320. if (err!=TIFFReadDirEntryErrOk)
  1321. {
  1322. _TIFFfree(data);
  1323. return(err);
  1324. }
  1325. *value=data;
  1326. return(TIFFReadDirEntryErrOk);
  1327. }
  1328. static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
  1329. {
  1330. enum TIFFReadDirEntryErr err;
  1331. uint32 count;
  1332. void* origdata;
  1333. int16* data;
  1334. switch (direntry->tdir_type)
  1335. {
  1336. case TIFF_BYTE:
  1337. case TIFF_SBYTE:
  1338. case TIFF_SHORT:
  1339. case TIFF_SSHORT:
  1340. case TIFF_LONG:
  1341. case TIFF_SLONG:
  1342. case TIFF_LONG8:
  1343. case TIFF_SLONG8:
  1344. break;
  1345. default:
  1346. return(TIFFReadDirEntryErrType);
  1347. }
  1348. err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
  1349. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1350. {
  1351. *value=0;
  1352. return(err);
  1353. }
  1354. switch (direntry->tdir_type)
  1355. {
  1356. case TIFF_SHORT:
  1357. {
  1358. uint16* m;
  1359. uint32 n;
  1360. m=(uint16*)origdata;
  1361. for (n=0; n<count; n++)
  1362. {
  1363. if (tif->tif_flags&TIFF_SWAB)
  1364. TIFFSwabShort(m);
  1365. err=TIFFReadDirEntryCheckRangeSshortShort(*m);
  1366. if (err!=TIFFReadDirEntryErrOk)
  1367. {
  1368. _TIFFfree(origdata);
  1369. return(err);
  1370. }
  1371. m++;
  1372. }
  1373. *value=(int16*)origdata;
  1374. return(TIFFReadDirEntryErrOk);
  1375. }
  1376. case TIFF_SSHORT:
  1377. *value=(int16*)origdata;
  1378. if (tif->tif_flags&TIFF_SWAB)
  1379. TIFFSwabArrayOfShort((uint16*)(*value),count);
  1380. return(TIFFReadDirEntryErrOk);
  1381. }
  1382. data=(int16*)_TIFFmalloc(count*2);
  1383. if (data==0)
  1384. {
  1385. _TIFFfree(origdata);
  1386. return(TIFFReadDirEntryErrAlloc);
  1387. }
  1388. switch (direntry->tdir_type)
  1389. {
  1390. case TIFF_BYTE:
  1391. {
  1392. uint8* ma;
  1393. int16* mb;
  1394. uint32 n;
  1395. ma=(uint8*)origdata;
  1396. mb=data;
  1397. for (n=0; n<count; n++)
  1398. *mb++=(int16)(*ma++);
  1399. }
  1400. break;
  1401. case TIFF_SBYTE:
  1402. {
  1403. int8* ma;
  1404. int16* mb;
  1405. uint32 n;
  1406. ma=(int8*)origdata;
  1407. mb=data;
  1408. for (n=0; n<count; n++)
  1409. *mb++=(int16)(*ma++);
  1410. }
  1411. break;
  1412. case TIFF_LONG:
  1413. {
  1414. uint32* ma;
  1415. int16* mb;
  1416. uint32 n;
  1417. ma=(uint32*)origdata;
  1418. mb=data;
  1419. for (n=0; n<count; n++)
  1420. {
  1421. if (tif->tif_flags&TIFF_SWAB)
  1422. TIFFSwabLong(ma);
  1423. err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
  1424. if (err!=TIFFReadDirEntryErrOk)
  1425. break;
  1426. *mb++=(int16)(*ma++);
  1427. }
  1428. }
  1429. break;
  1430. case TIFF_SLONG:
  1431. {
  1432. int32* ma;
  1433. int16* mb;
  1434. uint32 n;
  1435. ma=(int32*)origdata;
  1436. mb=data;
  1437. for (n=0; n<count; n++)
  1438. {
  1439. if (tif->tif_flags&TIFF_SWAB)
  1440. TIFFSwabLong((uint32*)ma);
  1441. err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
  1442. if (err!=TIFFReadDirEntryErrOk)
  1443. break;
  1444. *mb++=(int16)(*ma++);
  1445. }
  1446. }
  1447. break;
  1448. case TIFF_LONG8:
  1449. {
  1450. uint64* ma;
  1451. int16* mb;
  1452. uint32 n;
  1453. ma=(uint64*)origdata;
  1454. mb=data;
  1455. for (n=0; n<count; n++)
  1456. {
  1457. if (tif->tif_flags&TIFF_SWAB)
  1458. TIFFSwabLong8(ma);
  1459. err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
  1460. if (err!=TIFFReadDirEntryErrOk)
  1461. break;
  1462. *mb++=(int16)(*ma++);
  1463. }
  1464. }
  1465. break;
  1466. case TIFF_SLONG8:
  1467. {
  1468. int64* ma;
  1469. int16* mb;
  1470. uint32 n;
  1471. ma=(int64*)origdata;
  1472. mb=data;
  1473. for (n=0; n<count; n++)
  1474. {
  1475. if (tif->tif_flags&TIFF_SWAB)
  1476. TIFFSwabLong8((uint64*)ma);
  1477. err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
  1478. if (err!=TIFFReadDirEntryErrOk)
  1479. break;
  1480. *mb++=(int16)(*ma++);
  1481. }
  1482. }
  1483. break;
  1484. }
  1485. _TIFFfree(origdata);
  1486. if (err!=TIFFReadDirEntryErrOk)
  1487. {
  1488. _TIFFfree(data);
  1489. return(err);
  1490. }
  1491. *value=data;
  1492. return(TIFFReadDirEntryErrOk);
  1493. }
  1494. static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
  1495. {
  1496. enum TIFFReadDirEntryErr err;
  1497. uint32 count;
  1498. void* origdata;
  1499. uint32* data;
  1500. switch (direntry->tdir_type)
  1501. {
  1502. case TIFF_BYTE:
  1503. case TIFF_SBYTE:
  1504. case TIFF_SHORT:
  1505. case TIFF_SSHORT:
  1506. case TIFF_LONG:
  1507. case TIFF_SLONG:
  1508. case TIFF_LONG8:
  1509. case TIFF_SLONG8:
  1510. break;
  1511. default:
  1512. return(TIFFReadDirEntryErrType);
  1513. }
  1514. err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
  1515. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1516. {
  1517. *value=0;
  1518. return(err);
  1519. }
  1520. switch (direntry->tdir_type)
  1521. {
  1522. case TIFF_LONG:
  1523. *value=(uint32*)origdata;
  1524. if (tif->tif_flags&TIFF_SWAB)
  1525. TIFFSwabArrayOfLong(*value,count);
  1526. return(TIFFReadDirEntryErrOk);
  1527. case TIFF_SLONG:
  1528. {
  1529. int32* m;
  1530. uint32 n;
  1531. m=(int32*)origdata;
  1532. for (n=0; n<count; n++)
  1533. {
  1534. if (tif->tif_flags&TIFF_SWAB)
  1535. TIFFSwabLong((uint32*)m);
  1536. err=TIFFReadDirEntryCheckRangeLongSlong(*m);
  1537. if (err!=TIFFReadDirEntryErrOk)
  1538. {
  1539. _TIFFfree(origdata);
  1540. return(err);
  1541. }
  1542. m++;
  1543. }
  1544. *value=(uint32*)origdata;
  1545. return(TIFFReadDirEntryErrOk);
  1546. }
  1547. }
  1548. data=(uint32*)_TIFFmalloc(count*4);
  1549. if (data==0)
  1550. {
  1551. _TIFFfree(origdata);
  1552. return(TIFFReadDirEntryErrAlloc);
  1553. }
  1554. switch (direntry->tdir_type)
  1555. {
  1556. case TIFF_BYTE:
  1557. {
  1558. uint8* ma;
  1559. uint32* mb;
  1560. uint32 n;
  1561. ma=(uint8*)origdata;
  1562. mb=data;
  1563. for (n=0; n<count; n++)
  1564. *mb++=(uint32)(*ma++);
  1565. }
  1566. break;
  1567. case TIFF_SBYTE:
  1568. {
  1569. int8* ma;
  1570. uint32* mb;
  1571. uint32 n;
  1572. ma=(int8*)origdata;
  1573. mb=data;
  1574. for (n=0; n<count; n++)
  1575. {
  1576. err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
  1577. if (err!=TIFFReadDirEntryErrOk)
  1578. break;
  1579. *mb++=(uint32)(*ma++);
  1580. }
  1581. }
  1582. break;
  1583. case TIFF_SHORT:
  1584. {
  1585. uint16* ma;
  1586. uint32* mb;
  1587. uint32 n;
  1588. ma=(uint16*)origdata;
  1589. mb=data;
  1590. for (n=0; n<count; n++)
  1591. {
  1592. if (tif->tif_flags&TIFF_SWAB)
  1593. TIFFSwabShort(ma);
  1594. *mb++=(uint32)(*ma++);
  1595. }
  1596. }
  1597. break;
  1598. case TIFF_SSHORT:
  1599. {
  1600. int16* ma;
  1601. uint32* mb;
  1602. uint32 n;
  1603. ma=(int16*)origdata;
  1604. mb=data;
  1605. for (n=0; n<count; n++)
  1606. {
  1607. if (tif->tif_flags&TIFF_SWAB)
  1608. TIFFSwabShort((uint16*)ma);
  1609. err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
  1610. if (err!=TIFFReadDirEntryErrOk)
  1611. break;
  1612. *mb++=(uint32)(*ma++);
  1613. }
  1614. }
  1615. break;
  1616. case TIFF_LONG8:
  1617. {
  1618. uint64* ma;
  1619. uint32* mb;
  1620. uint32 n;
  1621. ma=(uint64*)origdata;
  1622. mb=data;
  1623. for (n=0; n<count; n++)
  1624. {
  1625. if (tif->tif_flags&TIFF_SWAB)
  1626. TIFFSwabLong8(ma);
  1627. err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
  1628. if (err!=TIFFReadDirEntryErrOk)
  1629. break;
  1630. *mb++=(uint32)(*ma++);
  1631. }
  1632. }
  1633. break;
  1634. case TIFF_SLONG8:
  1635. {
  1636. int64* ma;
  1637. uint32* mb;
  1638. uint32 n;
  1639. ma=(int64*)origdata;
  1640. mb=data;
  1641. for (n=0; n<count; n++)
  1642. {
  1643. if (tif->tif_flags&TIFF_SWAB)
  1644. TIFFSwabLong8((uint64*)ma);
  1645. err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
  1646. if (err!=TIFFReadDirEntryErrOk)
  1647. break;
  1648. *mb++=(uint32)(*ma++);
  1649. }
  1650. }
  1651. break;
  1652. }
  1653. _TIFFfree(origdata);
  1654. if (err!=TIFFReadDirEntryErrOk)
  1655. {
  1656. _TIFFfree(data);
  1657. return(err);
  1658. }
  1659. *value=data;
  1660. return(TIFFReadDirEntryErrOk);
  1661. }
  1662. static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
  1663. {
  1664. enum TIFFReadDirEntryErr err;
  1665. uint32 count;
  1666. void* origdata;
  1667. int32* data;
  1668. switch (direntry->tdir_type)
  1669. {
  1670. case TIFF_BYTE:
  1671. case TIFF_SBYTE:
  1672. case TIFF_SHORT:
  1673. case TIFF_SSHORT:
  1674. case TIFF_LONG:
  1675. case TIFF_SLONG:
  1676. case TIFF_LONG8:
  1677. case TIFF_SLONG8:
  1678. break;
  1679. default:
  1680. return(TIFFReadDirEntryErrType);
  1681. }
  1682. err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
  1683. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1684. {
  1685. *value=0;
  1686. return(err);
  1687. }
  1688. switch (direntry->tdir_type)
  1689. {
  1690. case TIFF_LONG:
  1691. {
  1692. uint32* m;
  1693. uint32 n;
  1694. m=(uint32*)origdata;
  1695. for (n=0; n<count; n++)
  1696. {
  1697. if (tif->tif_flags&TIFF_SWAB)
  1698. TIFFSwabLong((uint32*)m);
  1699. err=TIFFReadDirEntryCheckRangeSlongLong(*m);
  1700. if (err!=TIFFReadDirEntryErrOk)
  1701. {
  1702. _TIFFfree(origdata);
  1703. return(err);
  1704. }
  1705. m++;
  1706. }
  1707. *value=(int32*)origdata;
  1708. return(TIFFReadDirEntryErrOk);
  1709. }
  1710. case TIFF_SLONG:
  1711. *value=(int32*)origdata;
  1712. if (tif->tif_flags&TIFF_SWAB)
  1713. TIFFSwabArrayOfLong((uint32*)(*value),count);
  1714. return(TIFFReadDirEntryErrOk);
  1715. }
  1716. data=(int32*)_TIFFmalloc(count*4);
  1717. if (data==0)
  1718. {
  1719. _TIFFfree(origdata);
  1720. return(TIFFReadDirEntryErrAlloc);
  1721. }
  1722. switch (direntry->tdir_type)
  1723. {
  1724. case TIFF_BYTE:
  1725. {
  1726. uint8* ma;
  1727. int32* mb;
  1728. uint32 n;
  1729. ma=(uint8*)origdata;
  1730. mb=data;
  1731. for (n=0; n<count; n++)
  1732. *mb++=(int32)(*ma++);
  1733. }
  1734. break;
  1735. case TIFF_SBYTE:
  1736. {
  1737. int8* ma;
  1738. int32* mb;
  1739. uint32 n;
  1740. ma=(int8*)origdata;
  1741. mb=data;
  1742. for (n=0; n<count; n++)
  1743. *mb++=(int32)(*ma++);
  1744. }
  1745. break;
  1746. case TIFF_SHORT:
  1747. {
  1748. uint16* ma;
  1749. int32* mb;
  1750. uint32 n;
  1751. ma=(uint16*)origdata;
  1752. mb=data;
  1753. for (n=0; n<count; n++)
  1754. {
  1755. if (tif->tif_flags&TIFF_SWAB)
  1756. TIFFSwabShort(ma);
  1757. *mb++=(int32)(*ma++);
  1758. }
  1759. }
  1760. break;
  1761. case TIFF_SSHORT:
  1762. {
  1763. int16* ma;
  1764. int32* mb;
  1765. uint32 n;
  1766. ma=(int16*)origdata;
  1767. mb=data;
  1768. for (n=0; n<count; n++)
  1769. {
  1770. if (tif->tif_flags&TIFF_SWAB)
  1771. TIFFSwabShort((uint16*)ma);
  1772. *mb++=(int32)(*ma++);
  1773. }
  1774. }
  1775. break;
  1776. case TIFF_LONG8:
  1777. {
  1778. uint64* ma;
  1779. int32* mb;
  1780. uint32 n;
  1781. ma=(uint64*)origdata;
  1782. mb=data;
  1783. for (n=0; n<count; n++)
  1784. {
  1785. if (tif->tif_flags&TIFF_SWAB)
  1786. TIFFSwabLong8(ma);
  1787. err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
  1788. if (err!=TIFFReadDirEntryErrOk)
  1789. break;
  1790. *mb++=(int32)(*ma++);
  1791. }
  1792. }
  1793. break;
  1794. case TIFF_SLONG8:
  1795. {
  1796. int64* ma;
  1797. int32* mb;
  1798. uint32 n;
  1799. ma=(int64*)origdata;
  1800. mb=data;
  1801. for (n=0; n<count; n++)
  1802. {
  1803. if (tif->tif_flags&TIFF_SWAB)
  1804. TIFFSwabLong8((uint64*)ma);
  1805. err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
  1806. if (err!=TIFFReadDirEntryErrOk)
  1807. break;
  1808. *mb++=(int32)(*ma++);
  1809. }
  1810. }
  1811. break;
  1812. }
  1813. _TIFFfree(origdata);
  1814. if (err!=TIFFReadDirEntryErrOk)
  1815. {
  1816. _TIFFfree(data);
  1817. return(err);
  1818. }
  1819. *value=data;
  1820. return(TIFFReadDirEntryErrOk);
  1821. }
  1822. static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
  1823. {
  1824. enum TIFFReadDirEntryErr err;
  1825. uint32 count;
  1826. void* origdata;
  1827. uint64* data;
  1828. switch (direntry->tdir_type)
  1829. {
  1830. case TIFF_BYTE:
  1831. case TIFF_SBYTE:
  1832. case TIFF_SHORT:
  1833. case TIFF_SSHORT:
  1834. case TIFF_LONG:
  1835. case TIFF_SLONG:
  1836. case TIFF_LONG8:
  1837. case TIFF_SLONG8:
  1838. break;
  1839. default:
  1840. return(TIFFReadDirEntryErrType);
  1841. }
  1842. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  1843. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  1844. {
  1845. *value=0;
  1846. return(err);
  1847. }
  1848. switch (direntry->tdir_type)
  1849. {
  1850. case TIFF_LONG8:
  1851. *value=(uint64*)origdata;
  1852. if (tif->tif_flags&TIFF_SWAB)
  1853. TIFFSwabArrayOfLong8(*value,count);
  1854. return(TIFFReadDirEntryErrOk);
  1855. case TIFF_SLONG8:
  1856. {
  1857. int64* m;
  1858. uint32 n;
  1859. m=(int64*)origdata;
  1860. for (n=0; n<count; n++)
  1861. {
  1862. if (tif->tif_flags&TIFF_SWAB)
  1863. TIFFSwabLong8((uint64*)m);
  1864. err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
  1865. if (err!=TIFFReadDirEntryErrOk)
  1866. {
  1867. _TIFFfree(origdata);
  1868. return(err);
  1869. }
  1870. m++;
  1871. }
  1872. *value=(uint64*)origdata;
  1873. return(TIFFReadDirEntryErrOk);
  1874. }
  1875. }
  1876. data=(uint64*)_TIFFmalloc(count*8);
  1877. if (data==0)
  1878. {
  1879. _TIFFfree(origdata);
  1880. return(TIFFReadDirEntryErrAlloc);
  1881. }
  1882. switch (direntry->tdir_type)
  1883. {
  1884. case TIFF_BYTE:
  1885. {
  1886. uint8* ma;
  1887. uint64* mb;
  1888. uint32 n;
  1889. ma=(uint8*)origdata;
  1890. mb=data;
  1891. for (n=0; n<count; n++)
  1892. *mb++=(uint64)(*ma++);
  1893. }
  1894. break;
  1895. case TIFF_SBYTE:
  1896. {
  1897. int8* ma;
  1898. uint64* mb;
  1899. uint32 n;
  1900. ma=(int8*)origdata;
  1901. mb=data;
  1902. for (n=0; n<count; n++)
  1903. {
  1904. err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
  1905. if (err!=TIFFReadDirEntryErrOk)
  1906. break;
  1907. *mb++=(uint64)(*ma++);
  1908. }
  1909. }
  1910. break;
  1911. case TIFF_SHORT:
  1912. {
  1913. uint16* ma;
  1914. uint64* mb;
  1915. uint32 n;
  1916. ma=(uint16*)origdata;
  1917. mb=data;
  1918. for (n=0; n<count; n++)
  1919. {
  1920. if (tif->tif_flags&TIFF_SWAB)
  1921. TIFFSwabShort(ma);
  1922. *mb++=(uint64)(*ma++);
  1923. }
  1924. }
  1925. break;
  1926. case TIFF_SSHORT:
  1927. {
  1928. int16* ma;
  1929. uint64* mb;
  1930. uint32 n;
  1931. ma=(int16*)origdata;
  1932. mb=data;
  1933. for (n=0; n<count; n++)
  1934. {
  1935. if (tif->tif_flags&TIFF_SWAB)
  1936. TIFFSwabShort((uint16*)ma);
  1937. err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
  1938. if (err!=TIFFReadDirEntryErrOk)
  1939. break;
  1940. *mb++=(uint64)(*ma++);
  1941. }
  1942. }
  1943. break;
  1944. case TIFF_LONG:
  1945. {
  1946. uint32* ma;
  1947. uint64* mb;
  1948. uint32 n;
  1949. ma=(uint32*)origdata;
  1950. mb=data;
  1951. for (n=0; n<count; n++)
  1952. {
  1953. if (tif->tif_flags&TIFF_SWAB)
  1954. TIFFSwabLong(ma);
  1955. *mb++=(uint64)(*ma++);
  1956. }
  1957. }
  1958. break;
  1959. case TIFF_SLONG:
  1960. {
  1961. int32* ma;
  1962. uint64* mb;
  1963. uint32 n;
  1964. ma=(int32*)origdata;
  1965. mb=data;
  1966. for (n=0; n<count; n++)
  1967. {
  1968. if (tif->tif_flags&TIFF_SWAB)
  1969. TIFFSwabLong((uint32*)ma);
  1970. err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
  1971. if (err!=TIFFReadDirEntryErrOk)
  1972. break;
  1973. *mb++=(uint64)(*ma++);
  1974. }
  1975. }
  1976. break;
  1977. }
  1978. _TIFFfree(origdata);
  1979. if (err!=TIFFReadDirEntryErrOk)
  1980. {
  1981. _TIFFfree(data);
  1982. return(err);
  1983. }
  1984. *value=data;
  1985. return(TIFFReadDirEntryErrOk);
  1986. }
  1987. static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
  1988. {
  1989. enum TIFFReadDirEntryErr err;
  1990. uint32 count;
  1991. void* origdata;
  1992. int64* data;
  1993. switch (direntry->tdir_type)
  1994. {
  1995. case TIFF_BYTE:
  1996. case TIFF_SBYTE:
  1997. case TIFF_SHORT:
  1998. case TIFF_SSHORT:
  1999. case TIFF_LONG:
  2000. case TIFF_SLONG:
  2001. case TIFF_LONG8:
  2002. case TIFF_SLONG8:
  2003. break;
  2004. default:
  2005. return(TIFFReadDirEntryErrType);
  2006. }
  2007. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  2008. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2009. {
  2010. *value=0;
  2011. return(err);
  2012. }
  2013. switch (direntry->tdir_type)
  2014. {
  2015. case TIFF_LONG8:
  2016. {
  2017. uint64* m;
  2018. uint32 n;
  2019. m=(uint64*)origdata;
  2020. for (n=0; n<count; n++)
  2021. {
  2022. if (tif->tif_flags&TIFF_SWAB)
  2023. TIFFSwabLong8(m);
  2024. err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
  2025. if (err!=TIFFReadDirEntryErrOk)
  2026. {
  2027. _TIFFfree(origdata);
  2028. return(err);
  2029. }
  2030. m++;
  2031. }
  2032. *value=(int64*)origdata;
  2033. return(TIFFReadDirEntryErrOk);
  2034. }
  2035. case TIFF_SLONG8:
  2036. *value=(int64*)origdata;
  2037. if (tif->tif_flags&TIFF_SWAB)
  2038. TIFFSwabArrayOfLong8((uint64*)(*value),count);
  2039. return(TIFFReadDirEntryErrOk);
  2040. }
  2041. data=(int64*)_TIFFmalloc(count*8);
  2042. if (data==0)
  2043. {
  2044. _TIFFfree(origdata);
  2045. return(TIFFReadDirEntryErrAlloc);
  2046. }
  2047. switch (direntry->tdir_type)
  2048. {
  2049. case TIFF_BYTE:
  2050. {
  2051. uint8* ma;
  2052. int64* mb;
  2053. uint32 n;
  2054. ma=(uint8*)origdata;
  2055. mb=data;
  2056. for (n=0; n<count; n++)
  2057. *mb++=(int64)(*ma++);
  2058. }
  2059. break;
  2060. case TIFF_SBYTE:
  2061. {
  2062. int8* ma;
  2063. int64* mb;
  2064. uint32 n;
  2065. ma=(int8*)origdata;
  2066. mb=data;
  2067. for (n=0; n<count; n++)
  2068. *mb++=(int64)(*ma++);
  2069. }
  2070. break;
  2071. case TIFF_SHORT:
  2072. {
  2073. uint16* ma;
  2074. int64* mb;
  2075. uint32 n;
  2076. ma=(uint16*)origdata;
  2077. mb=data;
  2078. for (n=0; n<count; n++)
  2079. {
  2080. if (tif->tif_flags&TIFF_SWAB)
  2081. TIFFSwabShort(ma);
  2082. *mb++=(int64)(*ma++);
  2083. }
  2084. }
  2085. break;
  2086. case TIFF_SSHORT:
  2087. {
  2088. int16* ma;
  2089. int64* mb;
  2090. uint32 n;
  2091. ma=(int16*)origdata;
  2092. mb=data;
  2093. for (n=0; n<count; n++)
  2094. {
  2095. if (tif->tif_flags&TIFF_SWAB)
  2096. TIFFSwabShort((uint16*)ma);
  2097. *mb++=(int64)(*ma++);
  2098. }
  2099. }
  2100. break;
  2101. case TIFF_LONG:
  2102. {
  2103. uint32* ma;
  2104. int64* mb;
  2105. uint32 n;
  2106. ma=(uint32*)origdata;
  2107. mb=data;
  2108. for (n=0; n<count; n++)
  2109. {
  2110. if (tif->tif_flags&TIFF_SWAB)
  2111. TIFFSwabLong(ma);
  2112. *mb++=(int64)(*ma++);
  2113. }
  2114. }
  2115. break;
  2116. case TIFF_SLONG:
  2117. {
  2118. int32* ma;
  2119. int64* mb;
  2120. uint32 n;
  2121. ma=(int32*)origdata;
  2122. mb=data;
  2123. for (n=0; n<count; n++)
  2124. {
  2125. if (tif->tif_flags&TIFF_SWAB)
  2126. TIFFSwabLong((uint32*)ma);
  2127. *mb++=(int64)(*ma++);
  2128. }
  2129. }
  2130. break;
  2131. }
  2132. _TIFFfree(origdata);
  2133. *value=data;
  2134. return(TIFFReadDirEntryErrOk);
  2135. }
  2136. static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
  2137. {
  2138. enum TIFFReadDirEntryErr err;
  2139. uint32 count;
  2140. void* origdata;
  2141. float* data;
  2142. switch (direntry->tdir_type)
  2143. {
  2144. case TIFF_BYTE:
  2145. case TIFF_SBYTE:
  2146. case TIFF_SHORT:
  2147. case TIFF_SSHORT:
  2148. case TIFF_LONG:
  2149. case TIFF_SLONG:
  2150. case TIFF_LONG8:
  2151. case TIFF_SLONG8:
  2152. case TIFF_RATIONAL:
  2153. case TIFF_SRATIONAL:
  2154. case TIFF_FLOAT:
  2155. case TIFF_DOUBLE:
  2156. break;
  2157. default:
  2158. return(TIFFReadDirEntryErrType);
  2159. }
  2160. err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
  2161. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2162. {
  2163. *value=0;
  2164. return(err);
  2165. }
  2166. switch (direntry->tdir_type)
  2167. {
  2168. case TIFF_FLOAT:
  2169. if (tif->tif_flags&TIFF_SWAB)
  2170. TIFFSwabArrayOfLong((uint32*)origdata,count);
  2171. TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
  2172. *value=(float*)origdata;
  2173. return(TIFFReadDirEntryErrOk);
  2174. }
  2175. data=(float*)_TIFFmalloc(count*sizeof(float));
  2176. if (data==0)
  2177. {
  2178. _TIFFfree(origdata);
  2179. return(TIFFReadDirEntryErrAlloc);
  2180. }
  2181. switch (direntry->tdir_type)
  2182. {
  2183. case TIFF_BYTE:
  2184. {
  2185. uint8* ma;
  2186. float* mb;
  2187. uint32 n;
  2188. ma=(uint8*)origdata;
  2189. mb=data;
  2190. for (n=0; n<count; n++)
  2191. *mb++=(float)(*ma++);
  2192. }
  2193. break;
  2194. case TIFF_SBYTE:
  2195. {
  2196. int8* ma;
  2197. float* mb;
  2198. uint32 n;
  2199. ma=(int8*)origdata;
  2200. mb=data;
  2201. for (n=0; n<count; n++)
  2202. *mb++=(float)(*ma++);
  2203. }
  2204. break;
  2205. case TIFF_SHORT:
  2206. {
  2207. uint16* ma;
  2208. float* mb;
  2209. uint32 n;
  2210. ma=(uint16*)origdata;
  2211. mb=data;
  2212. for (n=0; n<count; n++)
  2213. {
  2214. if (tif->tif_flags&TIFF_SWAB)
  2215. TIFFSwabShort(ma);
  2216. *mb++=(float)(*ma++);
  2217. }
  2218. }
  2219. break;
  2220. case TIFF_SSHORT:
  2221. {
  2222. int16* ma;
  2223. float* mb;
  2224. uint32 n;
  2225. ma=(int16*)origdata;
  2226. mb=data;
  2227. for (n=0; n<count; n++)
  2228. {
  2229. if (tif->tif_flags&TIFF_SWAB)
  2230. TIFFSwabShort((uint16*)ma);
  2231. *mb++=(float)(*ma++);
  2232. }
  2233. }
  2234. break;
  2235. case TIFF_LONG:
  2236. {
  2237. uint32* ma;
  2238. float* mb;
  2239. uint32 n;
  2240. ma=(uint32*)origdata;
  2241. mb=data;
  2242. for (n=0; n<count; n++)
  2243. {
  2244. if (tif->tif_flags&TIFF_SWAB)
  2245. TIFFSwabLong(ma);
  2246. *mb++=(float)(*ma++);
  2247. }
  2248. }
  2249. break;
  2250. case TIFF_SLONG:
  2251. {
  2252. int32* ma;
  2253. float* mb;
  2254. uint32 n;
  2255. ma=(int32*)origdata;
  2256. mb=data;
  2257. for (n=0; n<count; n++)
  2258. {
  2259. if (tif->tif_flags&TIFF_SWAB)
  2260. TIFFSwabLong((uint32*)ma);
  2261. *mb++=(float)(*ma++);
  2262. }
  2263. }
  2264. break;
  2265. case TIFF_LONG8:
  2266. {
  2267. uint64* ma;
  2268. float* mb;
  2269. uint32 n;
  2270. ma=(uint64*)origdata;
  2271. mb=data;
  2272. for (n=0; n<count; n++)
  2273. {
  2274. if (tif->tif_flags&TIFF_SWAB)
  2275. TIFFSwabLong8(ma);
  2276. #if defined(__WIN32__) && (_MSC_VER < 1500)
  2277. /*
  2278. * XXX: MSVC 6.0 does not support
  2279. * conversion of 64-bit integers into
  2280. * floating point values.
  2281. */
  2282. *mb++ = _TIFFUInt64ToFloat(*ma++);
  2283. #else
  2284. *mb++ = (float)(*ma++);
  2285. #endif
  2286. }
  2287. }
  2288. break;
  2289. case TIFF_SLONG8:
  2290. {
  2291. int64* ma;
  2292. float* mb;
  2293. uint32 n;
  2294. ma=(int64*)origdata;
  2295. mb=data;
  2296. for (n=0; n<count; n++)
  2297. {
  2298. if (tif->tif_flags&TIFF_SWAB)
  2299. TIFFSwabLong8((uint64*)ma);
  2300. *mb++=(float)(*ma++);
  2301. }
  2302. }
  2303. break;
  2304. case TIFF_RATIONAL:
  2305. {
  2306. uint32* ma;
  2307. uint32 maa;
  2308. uint32 mab;
  2309. float* mb;
  2310. uint32 n;
  2311. ma=(uint32*)origdata;
  2312. mb=data;
  2313. for (n=0; n<count; n++)
  2314. {
  2315. if (tif->tif_flags&TIFF_SWAB)
  2316. TIFFSwabLong(ma);
  2317. maa=*ma++;
  2318. if (tif->tif_flags&TIFF_SWAB)
  2319. TIFFSwabLong(ma);
  2320. mab=*ma++;
  2321. if (mab==0)
  2322. *mb++=0.0;
  2323. else
  2324. *mb++=(float)maa/(float)mab;
  2325. }
  2326. }
  2327. break;
  2328. case TIFF_SRATIONAL:
  2329. {
  2330. uint32* ma;
  2331. int32 maa;
  2332. uint32 mab;
  2333. float* mb;
  2334. uint32 n;
  2335. ma=(uint32*)origdata;
  2336. mb=data;
  2337. for (n=0; n<count; n++)
  2338. {
  2339. if (tif->tif_flags&TIFF_SWAB)
  2340. TIFFSwabLong(ma);
  2341. maa=*(int32*)ma;
  2342. ma++;
  2343. if (tif->tif_flags&TIFF_SWAB)
  2344. TIFFSwabLong(ma);
  2345. mab=*ma++;
  2346. if (mab==0)
  2347. *mb++=0.0;
  2348. else
  2349. *mb++=(float)maa/(float)mab;
  2350. }
  2351. }
  2352. break;
  2353. case TIFF_DOUBLE:
  2354. {
  2355. double* ma;
  2356. float* mb;
  2357. uint32 n;
  2358. if (tif->tif_flags&TIFF_SWAB)
  2359. TIFFSwabArrayOfLong8((uint64*)origdata,count);
  2360. TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
  2361. ma=(double*)origdata;
  2362. mb=data;
  2363. for (n=0; n<count; n++)
  2364. {
  2365. double val = *ma++;
  2366. if( val > FLT_MAX )
  2367. val = FLT_MAX;
  2368. else if( val < -FLT_MAX )
  2369. val = -FLT_MAX;
  2370. *mb++=(float)val;
  2371. }
  2372. }
  2373. break;
  2374. }
  2375. _TIFFfree(origdata);
  2376. *value=data;
  2377. return(TIFFReadDirEntryErrOk);
  2378. }
  2379. static enum TIFFReadDirEntryErr
  2380. TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
  2381. {
  2382. enum TIFFReadDirEntryErr err;
  2383. uint32 count;
  2384. void* origdata;
  2385. double* data;
  2386. switch (direntry->tdir_type)
  2387. {
  2388. case TIFF_BYTE:
  2389. case TIFF_SBYTE:
  2390. case TIFF_SHORT:
  2391. case TIFF_SSHORT:
  2392. case TIFF_LONG:
  2393. case TIFF_SLONG:
  2394. case TIFF_LONG8:
  2395. case TIFF_SLONG8:
  2396. case TIFF_RATIONAL:
  2397. case TIFF_SRATIONAL:
  2398. case TIFF_FLOAT:
  2399. case TIFF_DOUBLE:
  2400. break;
  2401. default:
  2402. return(TIFFReadDirEntryErrType);
  2403. }
  2404. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  2405. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2406. {
  2407. *value=0;
  2408. return(err);
  2409. }
  2410. switch (direntry->tdir_type)
  2411. {
  2412. case TIFF_DOUBLE:
  2413. if (tif->tif_flags&TIFF_SWAB)
  2414. TIFFSwabArrayOfLong8((uint64*)origdata,count);
  2415. TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
  2416. *value=(double*)origdata;
  2417. return(TIFFReadDirEntryErrOk);
  2418. }
  2419. data=(double*)_TIFFmalloc(count*sizeof(double));
  2420. if (data==0)
  2421. {
  2422. _TIFFfree(origdata);
  2423. return(TIFFReadDirEntryErrAlloc);
  2424. }
  2425. switch (direntry->tdir_type)
  2426. {
  2427. case TIFF_BYTE:
  2428. {
  2429. uint8* ma;
  2430. double* mb;
  2431. uint32 n;
  2432. ma=(uint8*)origdata;
  2433. mb=data;
  2434. for (n=0; n<count; n++)
  2435. *mb++=(double)(*ma++);
  2436. }
  2437. break;
  2438. case TIFF_SBYTE:
  2439. {
  2440. int8* ma;
  2441. double* mb;
  2442. uint32 n;
  2443. ma=(int8*)origdata;
  2444. mb=data;
  2445. for (n=0; n<count; n++)
  2446. *mb++=(double)(*ma++);
  2447. }
  2448. break;
  2449. case TIFF_SHORT:
  2450. {
  2451. uint16* ma;
  2452. double* mb;
  2453. uint32 n;
  2454. ma=(uint16*)origdata;
  2455. mb=data;
  2456. for (n=0; n<count; n++)
  2457. {
  2458. if (tif->tif_flags&TIFF_SWAB)
  2459. TIFFSwabShort(ma);
  2460. *mb++=(double)(*ma++);
  2461. }
  2462. }
  2463. break;
  2464. case TIFF_SSHORT:
  2465. {
  2466. int16* ma;
  2467. double* mb;
  2468. uint32 n;
  2469. ma=(int16*)origdata;
  2470. mb=data;
  2471. for (n=0; n<count; n++)
  2472. {
  2473. if (tif->tif_flags&TIFF_SWAB)
  2474. TIFFSwabShort((uint16*)ma);
  2475. *mb++=(double)(*ma++);
  2476. }
  2477. }
  2478. break;
  2479. case TIFF_LONG:
  2480. {
  2481. uint32* ma;
  2482. double* mb;
  2483. uint32 n;
  2484. ma=(uint32*)origdata;
  2485. mb=data;
  2486. for (n=0; n<count; n++)
  2487. {
  2488. if (tif->tif_flags&TIFF_SWAB)
  2489. TIFFSwabLong(ma);
  2490. *mb++=(double)(*ma++);
  2491. }
  2492. }
  2493. break;
  2494. case TIFF_SLONG:
  2495. {
  2496. int32* ma;
  2497. double* mb;
  2498. uint32 n;
  2499. ma=(int32*)origdata;
  2500. mb=data;
  2501. for (n=0; n<count; n++)
  2502. {
  2503. if (tif->tif_flags&TIFF_SWAB)
  2504. TIFFSwabLong((uint32*)ma);
  2505. *mb++=(double)(*ma++);
  2506. }
  2507. }
  2508. break;
  2509. case TIFF_LONG8:
  2510. {
  2511. uint64* ma;
  2512. double* mb;
  2513. uint32 n;
  2514. ma=(uint64*)origdata;
  2515. mb=data;
  2516. for (n=0; n<count; n++)
  2517. {
  2518. if (tif->tif_flags&TIFF_SWAB)
  2519. TIFFSwabLong8(ma);
  2520. #if defined(__WIN32__) && (_MSC_VER < 1500)
  2521. /*
  2522. * XXX: MSVC 6.0 does not support
  2523. * conversion of 64-bit integers into
  2524. * floating point values.
  2525. */
  2526. *mb++ = _TIFFUInt64ToDouble(*ma++);
  2527. #else
  2528. *mb++ = (double)(*ma++);
  2529. #endif
  2530. }
  2531. }
  2532. break;
  2533. case TIFF_SLONG8:
  2534. {
  2535. int64* ma;
  2536. double* mb;
  2537. uint32 n;
  2538. ma=(int64*)origdata;
  2539. mb=data;
  2540. for (n=0; n<count; n++)
  2541. {
  2542. if (tif->tif_flags&TIFF_SWAB)
  2543. TIFFSwabLong8((uint64*)ma);
  2544. *mb++=(double)(*ma++);
  2545. }
  2546. }
  2547. break;
  2548. case TIFF_RATIONAL:
  2549. {
  2550. uint32* ma;
  2551. uint32 maa;
  2552. uint32 mab;
  2553. double* mb;
  2554. uint32 n;
  2555. ma=(uint32*)origdata;
  2556. mb=data;
  2557. for (n=0; n<count; n++)
  2558. {
  2559. if (tif->tif_flags&TIFF_SWAB)
  2560. TIFFSwabLong(ma);
  2561. maa=*ma++;
  2562. if (tif->tif_flags&TIFF_SWAB)
  2563. TIFFSwabLong(ma);
  2564. mab=*ma++;
  2565. if (mab==0)
  2566. *mb++=0.0;
  2567. else
  2568. *mb++=(double)maa/(double)mab;
  2569. }
  2570. }
  2571. break;
  2572. case TIFF_SRATIONAL:
  2573. {
  2574. uint32* ma;
  2575. int32 maa;
  2576. uint32 mab;
  2577. double* mb;
  2578. uint32 n;
  2579. ma=(uint32*)origdata;
  2580. mb=data;
  2581. for (n=0; n<count; n++)
  2582. {
  2583. if (tif->tif_flags&TIFF_SWAB)
  2584. TIFFSwabLong(ma);
  2585. maa=*(int32*)ma;
  2586. ma++;
  2587. if (tif->tif_flags&TIFF_SWAB)
  2588. TIFFSwabLong(ma);
  2589. mab=*ma++;
  2590. if (mab==0)
  2591. *mb++=0.0;
  2592. else
  2593. *mb++=(double)maa/(double)mab;
  2594. }
  2595. }
  2596. break;
  2597. case TIFF_FLOAT:
  2598. {
  2599. float* ma;
  2600. double* mb;
  2601. uint32 n;
  2602. if (tif->tif_flags&TIFF_SWAB)
  2603. TIFFSwabArrayOfLong((uint32*)origdata,count);
  2604. TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
  2605. ma=(float*)origdata;
  2606. mb=data;
  2607. for (n=0; n<count; n++)
  2608. *mb++=(double)(*ma++);
  2609. }
  2610. break;
  2611. }
  2612. _TIFFfree(origdata);
  2613. *value=data;
  2614. return(TIFFReadDirEntryErrOk);
  2615. }
  2616. static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
  2617. {
  2618. enum TIFFReadDirEntryErr err;
  2619. uint32 count;
  2620. void* origdata;
  2621. uint64* data;
  2622. switch (direntry->tdir_type)
  2623. {
  2624. case TIFF_LONG:
  2625. case TIFF_LONG8:
  2626. case TIFF_IFD:
  2627. case TIFF_IFD8:
  2628. break;
  2629. default:
  2630. return(TIFFReadDirEntryErrType);
  2631. }
  2632. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  2633. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2634. {
  2635. *value=0;
  2636. return(err);
  2637. }
  2638. switch (direntry->tdir_type)
  2639. {
  2640. case TIFF_LONG8:
  2641. case TIFF_IFD8:
  2642. *value=(uint64*)origdata;
  2643. if (tif->tif_flags&TIFF_SWAB)
  2644. TIFFSwabArrayOfLong8(*value,count);
  2645. return(TIFFReadDirEntryErrOk);
  2646. }
  2647. data=(uint64*)_TIFFmalloc(count*8);
  2648. if (data==0)
  2649. {
  2650. _TIFFfree(origdata);
  2651. return(TIFFReadDirEntryErrAlloc);
  2652. }
  2653. switch (direntry->tdir_type)
  2654. {
  2655. case TIFF_LONG:
  2656. case TIFF_IFD:
  2657. {
  2658. uint32* ma;
  2659. uint64* mb;
  2660. uint32 n;
  2661. ma=(uint32*)origdata;
  2662. mb=data;
  2663. for (n=0; n<count; n++)
  2664. {
  2665. if (tif->tif_flags&TIFF_SWAB)
  2666. TIFFSwabLong(ma);
  2667. *mb++=(uint64)(*ma++);
  2668. }
  2669. }
  2670. break;
  2671. }
  2672. _TIFFfree(origdata);
  2673. *value=data;
  2674. return(TIFFReadDirEntryErrOk);
  2675. }
  2676. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
  2677. {
  2678. enum TIFFReadDirEntryErr err;
  2679. uint16* m;
  2680. uint16* na;
  2681. uint16 nb;
  2682. if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
  2683. return(TIFFReadDirEntryErrCount);
  2684. err=TIFFReadDirEntryShortArray(tif,direntry,&m);
  2685. if (err!=TIFFReadDirEntryErrOk)
  2686. return(err);
  2687. na=m;
  2688. nb=tif->tif_dir.td_samplesperpixel;
  2689. *value=*na++;
  2690. nb--;
  2691. while (nb>0)
  2692. {
  2693. if (*na++!=*value)
  2694. {
  2695. err=TIFFReadDirEntryErrPsdif;
  2696. break;
  2697. }
  2698. nb--;
  2699. }
  2700. _TIFFfree(m);
  2701. return(err);
  2702. }
  2703. #if 0
  2704. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2705. {
  2706. enum TIFFReadDirEntryErr err;
  2707. double* m;
  2708. double* na;
  2709. uint16 nb;
  2710. if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
  2711. return(TIFFReadDirEntryErrCount);
  2712. err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
  2713. if (err!=TIFFReadDirEntryErrOk)
  2714. return(err);
  2715. na=m;
  2716. nb=tif->tif_dir.td_samplesperpixel;
  2717. *value=*na++;
  2718. nb--;
  2719. while (nb>0)
  2720. {
  2721. if (*na++!=*value)
  2722. {
  2723. err=TIFFReadDirEntryErrPsdif;
  2724. break;
  2725. }
  2726. nb--;
  2727. }
  2728. _TIFFfree(m);
  2729. return(err);
  2730. }
  2731. #endif
  2732. static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
  2733. {
  2734. (void) tif;
  2735. *value=*(uint8*)(&direntry->tdir_offset);
  2736. }
  2737. static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
  2738. {
  2739. (void) tif;
  2740. *value=*(int8*)(&direntry->tdir_offset);
  2741. }
  2742. static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
  2743. {
  2744. *value = direntry->tdir_offset.toff_short;
  2745. /* *value=*(uint16*)(&direntry->tdir_offset); */
  2746. if (tif->tif_flags&TIFF_SWAB)
  2747. TIFFSwabShort(value);
  2748. }
  2749. static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
  2750. {
  2751. *value=*(int16*)(&direntry->tdir_offset);
  2752. if (tif->tif_flags&TIFF_SWAB)
  2753. TIFFSwabShort((uint16*)value);
  2754. }
  2755. static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
  2756. {
  2757. *value=*(uint32*)(&direntry->tdir_offset);
  2758. if (tif->tif_flags&TIFF_SWAB)
  2759. TIFFSwabLong(value);
  2760. }
  2761. static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
  2762. {
  2763. *value=*(int32*)(&direntry->tdir_offset);
  2764. if (tif->tif_flags&TIFF_SWAB)
  2765. TIFFSwabLong((uint32*)value);
  2766. }
  2767. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
  2768. {
  2769. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2770. {
  2771. enum TIFFReadDirEntryErr err;
  2772. uint32 offset = direntry->tdir_offset.toff_long;
  2773. if (tif->tif_flags&TIFF_SWAB)
  2774. TIFFSwabLong(&offset);
  2775. err=TIFFReadDirEntryData(tif,offset,8,value);
  2776. if (err!=TIFFReadDirEntryErrOk)
  2777. return(err);
  2778. }
  2779. else
  2780. *value = direntry->tdir_offset.toff_long8;
  2781. if (tif->tif_flags&TIFF_SWAB)
  2782. TIFFSwabLong8(value);
  2783. return(TIFFReadDirEntryErrOk);
  2784. }
  2785. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
  2786. {
  2787. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2788. {
  2789. enum TIFFReadDirEntryErr err;
  2790. uint32 offset = direntry->tdir_offset.toff_long;
  2791. if (tif->tif_flags&TIFF_SWAB)
  2792. TIFFSwabLong(&offset);
  2793. err=TIFFReadDirEntryData(tif,offset,8,value);
  2794. if (err!=TIFFReadDirEntryErrOk)
  2795. return(err);
  2796. }
  2797. else
  2798. *value=*(int64*)(&direntry->tdir_offset);
  2799. if (tif->tif_flags&TIFF_SWAB)
  2800. TIFFSwabLong8((uint64*)value);
  2801. return(TIFFReadDirEntryErrOk);
  2802. }
  2803. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2804. {
  2805. UInt64Aligned_t m;
  2806. assert(sizeof(double)==8);
  2807. assert(sizeof(uint64)==8);
  2808. assert(sizeof(uint32)==4);
  2809. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2810. {
  2811. enum TIFFReadDirEntryErr err;
  2812. uint32 offset = direntry->tdir_offset.toff_long;
  2813. if (tif->tif_flags&TIFF_SWAB)
  2814. TIFFSwabLong(&offset);
  2815. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  2816. if (err!=TIFFReadDirEntryErrOk)
  2817. return(err);
  2818. }
  2819. else
  2820. m.l = direntry->tdir_offset.toff_long8;
  2821. if (tif->tif_flags&TIFF_SWAB)
  2822. TIFFSwabArrayOfLong(m.i,2);
  2823. /* Not completely sure what we should do when m.i[1]==0, but some */
  2824. /* sanitizers do not like division by 0.0: */
  2825. /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
  2826. if (m.i[0]==0 || m.i[1]==0)
  2827. *value=0.0;
  2828. else
  2829. *value=(double)m.i[0]/(double)m.i[1];
  2830. return(TIFFReadDirEntryErrOk);
  2831. }
  2832. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2833. {
  2834. UInt64Aligned_t m;
  2835. assert(sizeof(double)==8);
  2836. assert(sizeof(uint64)==8);
  2837. assert(sizeof(int32)==4);
  2838. assert(sizeof(uint32)==4);
  2839. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2840. {
  2841. enum TIFFReadDirEntryErr err;
  2842. uint32 offset = direntry->tdir_offset.toff_long;
  2843. if (tif->tif_flags&TIFF_SWAB)
  2844. TIFFSwabLong(&offset);
  2845. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  2846. if (err!=TIFFReadDirEntryErrOk)
  2847. return(err);
  2848. }
  2849. else
  2850. m.l=direntry->tdir_offset.toff_long8;
  2851. if (tif->tif_flags&TIFF_SWAB)
  2852. TIFFSwabArrayOfLong(m.i,2);
  2853. /* Not completely sure what we should do when m.i[1]==0, but some */
  2854. /* sanitizers do not like division by 0.0: */
  2855. /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
  2856. if ((int32)m.i[0]==0 || m.i[1]==0)
  2857. *value=0.0;
  2858. else
  2859. *value=(double)((int32)m.i[0])/(double)m.i[1];
  2860. return(TIFFReadDirEntryErrOk);
  2861. }
  2862. static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
  2863. {
  2864. union
  2865. {
  2866. float f;
  2867. uint32 i;
  2868. } float_union;
  2869. assert(sizeof(float)==4);
  2870. assert(sizeof(uint32)==4);
  2871. assert(sizeof(float_union)==4);
  2872. float_union.i=*(uint32*)(&direntry->tdir_offset);
  2873. *value=float_union.f;
  2874. if (tif->tif_flags&TIFF_SWAB)
  2875. TIFFSwabLong((uint32*)value);
  2876. }
  2877. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2878. {
  2879. assert(sizeof(double)==8);
  2880. assert(sizeof(uint64)==8);
  2881. assert(sizeof(UInt64Aligned_t)==8);
  2882. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2883. {
  2884. enum TIFFReadDirEntryErr err;
  2885. uint32 offset = direntry->tdir_offset.toff_long;
  2886. if (tif->tif_flags&TIFF_SWAB)
  2887. TIFFSwabLong(&offset);
  2888. err=TIFFReadDirEntryData(tif,offset,8,value);
  2889. if (err!=TIFFReadDirEntryErrOk)
  2890. return(err);
  2891. }
  2892. else
  2893. {
  2894. UInt64Aligned_t uint64_union;
  2895. uint64_union.l=direntry->tdir_offset.toff_long8;
  2896. *value=uint64_union.d;
  2897. }
  2898. if (tif->tif_flags&TIFF_SWAB)
  2899. TIFFSwabLong8((uint64*)value);
  2900. return(TIFFReadDirEntryErrOk);
  2901. }
  2902. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
  2903. {
  2904. if (value<0)
  2905. return(TIFFReadDirEntryErrRange);
  2906. else
  2907. return(TIFFReadDirEntryErrOk);
  2908. }
  2909. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
  2910. {
  2911. if (value>0xFF)
  2912. return(TIFFReadDirEntryErrRange);
  2913. else
  2914. return(TIFFReadDirEntryErrOk);
  2915. }
  2916. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
  2917. {
  2918. if ((value<0)||(value>0xFF))
  2919. return(TIFFReadDirEntryErrRange);
  2920. else
  2921. return(TIFFReadDirEntryErrOk);
  2922. }
  2923. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
  2924. {
  2925. if (value>0xFF)
  2926. return(TIFFReadDirEntryErrRange);
  2927. else
  2928. return(TIFFReadDirEntryErrOk);
  2929. }
  2930. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
  2931. {
  2932. if ((value<0)||(value>0xFF))
  2933. return(TIFFReadDirEntryErrRange);
  2934. else
  2935. return(TIFFReadDirEntryErrOk);
  2936. }
  2937. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
  2938. {
  2939. if (value>0xFF)
  2940. return(TIFFReadDirEntryErrRange);
  2941. else
  2942. return(TIFFReadDirEntryErrOk);
  2943. }
  2944. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
  2945. {
  2946. if ((value<0)||(value>0xFF))
  2947. return(TIFFReadDirEntryErrRange);
  2948. else
  2949. return(TIFFReadDirEntryErrOk);
  2950. }
  2951. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
  2952. {
  2953. if (value>0x7F)
  2954. return(TIFFReadDirEntryErrRange);
  2955. else
  2956. return(TIFFReadDirEntryErrOk);
  2957. }
  2958. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
  2959. {
  2960. if (value>0x7F)
  2961. return(TIFFReadDirEntryErrRange);
  2962. else
  2963. return(TIFFReadDirEntryErrOk);
  2964. }
  2965. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
  2966. {
  2967. if ((value<-0x80)||(value>0x7F))
  2968. return(TIFFReadDirEntryErrRange);
  2969. else
  2970. return(TIFFReadDirEntryErrOk);
  2971. }
  2972. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
  2973. {
  2974. if (value>0x7F)
  2975. return(TIFFReadDirEntryErrRange);
  2976. else
  2977. return(TIFFReadDirEntryErrOk);
  2978. }
  2979. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
  2980. {
  2981. if ((value<-0x80)||(value>0x7F))
  2982. return(TIFFReadDirEntryErrRange);
  2983. else
  2984. return(TIFFReadDirEntryErrOk);
  2985. }
  2986. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
  2987. {
  2988. if (value>0x7F)
  2989. return(TIFFReadDirEntryErrRange);
  2990. else
  2991. return(TIFFReadDirEntryErrOk);
  2992. }
  2993. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
  2994. {
  2995. if ((value<-0x80)||(value>0x7F))
  2996. return(TIFFReadDirEntryErrRange);
  2997. else
  2998. return(TIFFReadDirEntryErrOk);
  2999. }
  3000. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
  3001. {
  3002. if (value<0)
  3003. return(TIFFReadDirEntryErrRange);
  3004. else
  3005. return(TIFFReadDirEntryErrOk);
  3006. }
  3007. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
  3008. {
  3009. if (value<0)
  3010. return(TIFFReadDirEntryErrRange);
  3011. else
  3012. return(TIFFReadDirEntryErrOk);
  3013. }
  3014. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
  3015. {
  3016. if (value>0xFFFF)
  3017. return(TIFFReadDirEntryErrRange);
  3018. else
  3019. return(TIFFReadDirEntryErrOk);
  3020. }
  3021. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
  3022. {
  3023. if ((value<0)||(value>0xFFFF))
  3024. return(TIFFReadDirEntryErrRange);
  3025. else
  3026. return(TIFFReadDirEntryErrOk);
  3027. }
  3028. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
  3029. {
  3030. if (value>0xFFFF)
  3031. return(TIFFReadDirEntryErrRange);
  3032. else
  3033. return(TIFFReadDirEntryErrOk);
  3034. }
  3035. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
  3036. {
  3037. if ((value<0)||(value>0xFFFF))
  3038. return(TIFFReadDirEntryErrRange);
  3039. else
  3040. return(TIFFReadDirEntryErrOk);
  3041. }
  3042. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
  3043. {
  3044. if (value>0x7FFF)
  3045. return(TIFFReadDirEntryErrRange);
  3046. else
  3047. return(TIFFReadDirEntryErrOk);
  3048. }
  3049. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
  3050. {
  3051. if (value>0x7FFF)
  3052. return(TIFFReadDirEntryErrRange);
  3053. else
  3054. return(TIFFReadDirEntryErrOk);
  3055. }
  3056. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
  3057. {
  3058. if ((value<-0x8000)||(value>0x7FFF))
  3059. return(TIFFReadDirEntryErrRange);
  3060. else
  3061. return(TIFFReadDirEntryErrOk);
  3062. }
  3063. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
  3064. {
  3065. if (value>0x7FFF)
  3066. return(TIFFReadDirEntryErrRange);
  3067. else
  3068. return(TIFFReadDirEntryErrOk);
  3069. }
  3070. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
  3071. {
  3072. if ((value<-0x8000)||(value>0x7FFF))
  3073. return(TIFFReadDirEntryErrRange);
  3074. else
  3075. return(TIFFReadDirEntryErrOk);
  3076. }
  3077. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
  3078. {
  3079. if (value<0)
  3080. return(TIFFReadDirEntryErrRange);
  3081. else
  3082. return(TIFFReadDirEntryErrOk);
  3083. }
  3084. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
  3085. {
  3086. if (value<0)
  3087. return(TIFFReadDirEntryErrRange);
  3088. else
  3089. return(TIFFReadDirEntryErrOk);
  3090. }
  3091. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
  3092. {
  3093. if (value<0)
  3094. return(TIFFReadDirEntryErrRange);
  3095. else
  3096. return(TIFFReadDirEntryErrOk);
  3097. }
  3098. /*
  3099. * Largest 32-bit unsigned integer value.
  3100. */
  3101. #define TIFF_UINT32_MAX 0xFFFFFFFFU
  3102. static enum TIFFReadDirEntryErr
  3103. TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
  3104. {
  3105. if (value > TIFF_UINT32_MAX)
  3106. return(TIFFReadDirEntryErrRange);
  3107. else
  3108. return(TIFFReadDirEntryErrOk);
  3109. }
  3110. static enum TIFFReadDirEntryErr
  3111. TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
  3112. {
  3113. if ((value < 0) || (value > (int64) TIFF_UINT32_MAX))
  3114. return(TIFFReadDirEntryErrRange);
  3115. else
  3116. return(TIFFReadDirEntryErrOk);
  3117. }
  3118. #undef TIFF_UINT32_MAX
  3119. static enum TIFFReadDirEntryErr
  3120. TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
  3121. {
  3122. if (value > 0x7FFFFFFFUL)
  3123. return(TIFFReadDirEntryErrRange);
  3124. else
  3125. return(TIFFReadDirEntryErrOk);
  3126. }
  3127. /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
  3128. static enum TIFFReadDirEntryErr
  3129. TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
  3130. {
  3131. if (value > 0x7FFFFFFF)
  3132. return(TIFFReadDirEntryErrRange);
  3133. else
  3134. return(TIFFReadDirEntryErrOk);
  3135. }
  3136. /* Check that the 8-byte signed value can fit in a 4-byte signed range */
  3137. static enum TIFFReadDirEntryErr
  3138. TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
  3139. {
  3140. if ((value < 0-((int64) 0x7FFFFFFF+1)) || (value > 0x7FFFFFFF))
  3141. return(TIFFReadDirEntryErrRange);
  3142. else
  3143. return(TIFFReadDirEntryErrOk);
  3144. }
  3145. static enum TIFFReadDirEntryErr
  3146. TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
  3147. {
  3148. if (value < 0)
  3149. return(TIFFReadDirEntryErrRange);
  3150. else
  3151. return(TIFFReadDirEntryErrOk);
  3152. }
  3153. static enum TIFFReadDirEntryErr
  3154. TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
  3155. {
  3156. if (value < 0)
  3157. return(TIFFReadDirEntryErrRange);
  3158. else
  3159. return(TIFFReadDirEntryErrOk);
  3160. }
  3161. static enum TIFFReadDirEntryErr
  3162. TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
  3163. {
  3164. if (value < 0)
  3165. return(TIFFReadDirEntryErrRange);
  3166. else
  3167. return(TIFFReadDirEntryErrOk);
  3168. }
  3169. static enum TIFFReadDirEntryErr
  3170. TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
  3171. {
  3172. if (value < 0)
  3173. return(TIFFReadDirEntryErrRange);
  3174. else
  3175. return(TIFFReadDirEntryErrOk);
  3176. }
  3177. /*
  3178. * Largest 64-bit signed integer value.
  3179. */
  3180. #define TIFF_INT64_MAX ((int64)(((uint64) ~0) >> 1))
  3181. static enum TIFFReadDirEntryErr
  3182. TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
  3183. {
  3184. if (value > TIFF_INT64_MAX)
  3185. return(TIFFReadDirEntryErrRange);
  3186. else
  3187. return(TIFFReadDirEntryErrOk);
  3188. }
  3189. #undef TIFF_INT64_MAX
  3190. static enum TIFFReadDirEntryErr
  3191. TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
  3192. {
  3193. assert(size>0);
  3194. if (!isMapped(tif)) {
  3195. if (!SeekOK(tif,offset))
  3196. return(TIFFReadDirEntryErrIo);
  3197. if (!ReadOK(tif,dest,size))
  3198. return(TIFFReadDirEntryErrIo);
  3199. } else {
  3200. size_t ma,mb;
  3201. ma=(size_t)offset;
  3202. mb=ma+size;
  3203. if (((uint64)ma!=offset)
  3204. || (mb < ma)
  3205. || (mb - ma != (size_t) size)
  3206. || (mb < (size_t)size)
  3207. || (mb > (size_t)tif->tif_size)
  3208. )
  3209. return(TIFFReadDirEntryErrIo);
  3210. _TIFFmemcpy(dest,tif->tif_base+ma,size);
  3211. }
  3212. return(TIFFReadDirEntryErrOk);
  3213. }
  3214. static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
  3215. {
  3216. if (!recover) {
  3217. switch (err) {
  3218. case TIFFReadDirEntryErrCount:
  3219. TIFFErrorExt(tif->tif_clientdata, module,
  3220. "Incorrect count for \"%s\"",
  3221. tagname);
  3222. break;
  3223. case TIFFReadDirEntryErrType:
  3224. TIFFErrorExt(tif->tif_clientdata, module,
  3225. "Incompatible type for \"%s\"",
  3226. tagname);
  3227. break;
  3228. case TIFFReadDirEntryErrIo:
  3229. TIFFErrorExt(tif->tif_clientdata, module,
  3230. "IO error during reading of \"%s\"",
  3231. tagname);
  3232. break;
  3233. case TIFFReadDirEntryErrRange:
  3234. TIFFErrorExt(tif->tif_clientdata, module,
  3235. "Incorrect value for \"%s\"",
  3236. tagname);
  3237. break;
  3238. case TIFFReadDirEntryErrPsdif:
  3239. TIFFErrorExt(tif->tif_clientdata, module,
  3240. "Cannot handle different values per sample for \"%s\"",
  3241. tagname);
  3242. break;
  3243. case TIFFReadDirEntryErrSizesan:
  3244. TIFFErrorExt(tif->tif_clientdata, module,
  3245. "Sanity check on size of \"%s\" value failed",
  3246. tagname);
  3247. break;
  3248. case TIFFReadDirEntryErrAlloc:
  3249. TIFFErrorExt(tif->tif_clientdata, module,
  3250. "Out of memory reading of \"%s\"",
  3251. tagname);
  3252. break;
  3253. default:
  3254. assert(0); /* we should never get here */
  3255. break;
  3256. }
  3257. } else {
  3258. switch (err) {
  3259. case TIFFReadDirEntryErrCount:
  3260. TIFFWarningExt(tif->tif_clientdata, module,
  3261. "Incorrect count for \"%s\"; tag ignored",
  3262. tagname);
  3263. break;
  3264. case TIFFReadDirEntryErrType:
  3265. TIFFWarningExt(tif->tif_clientdata, module,
  3266. "Incompatible type for \"%s\"; tag ignored",
  3267. tagname);
  3268. break;
  3269. case TIFFReadDirEntryErrIo:
  3270. TIFFWarningExt(tif->tif_clientdata, module,
  3271. "IO error during reading of \"%s\"; tag ignored",
  3272. tagname);
  3273. break;
  3274. case TIFFReadDirEntryErrRange:
  3275. TIFFWarningExt(tif->tif_clientdata, module,
  3276. "Incorrect value for \"%s\"; tag ignored",
  3277. tagname);
  3278. break;
  3279. case TIFFReadDirEntryErrPsdif:
  3280. TIFFWarningExt(tif->tif_clientdata, module,
  3281. "Cannot handle different values per sample for \"%s\"; tag ignored",
  3282. tagname);
  3283. break;
  3284. case TIFFReadDirEntryErrSizesan:
  3285. TIFFWarningExt(tif->tif_clientdata, module,
  3286. "Sanity check on size of \"%s\" value failed; tag ignored",
  3287. tagname);
  3288. break;
  3289. case TIFFReadDirEntryErrAlloc:
  3290. TIFFWarningExt(tif->tif_clientdata, module,
  3291. "Out of memory reading of \"%s\"; tag ignored",
  3292. tagname);
  3293. break;
  3294. default:
  3295. assert(0); /* we should never get here */
  3296. break;
  3297. }
  3298. }
  3299. }
  3300. /*
  3301. * Read the next TIFF directory from a file and convert it to the internal
  3302. * format. We read directories sequentially.
  3303. */
  3304. int
  3305. TIFFReadDirectory(TIFF* tif)
  3306. {
  3307. static const char module[] = "TIFFReadDirectory";
  3308. TIFFDirEntry* dir;
  3309. uint16 dircount;
  3310. TIFFDirEntry* dp;
  3311. uint16 di;
  3312. const TIFFField* fip;
  3313. uint32 fii=FAILED_FII;
  3314. toff_t nextdiroff;
  3315. int bitspersample_read = FALSE;
  3316. tif->tif_diroff=tif->tif_nextdiroff;
  3317. if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
  3318. return 0; /* last offset or bad offset (IFD looping) */
  3319. (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
  3320. tif->tif_curdir++;
  3321. nextdiroff = tif->tif_nextdiroff;
  3322. dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
  3323. if (!dircount)
  3324. {
  3325. TIFFErrorExt(tif->tif_clientdata,module,
  3326. "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
  3327. return 0;
  3328. }
  3329. TIFFReadDirectoryCheckOrder(tif,dir,dircount);
  3330. /*
  3331. * Mark duplicates of any tag to be ignored (bugzilla 1994)
  3332. * to avoid certain pathological problems.
  3333. */
  3334. {
  3335. TIFFDirEntry* ma;
  3336. uint16 mb;
  3337. for (ma=dir, mb=0; mb<dircount; ma++, mb++)
  3338. {
  3339. TIFFDirEntry* na;
  3340. uint16 nb;
  3341. for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
  3342. {
  3343. if (ma->tdir_tag==na->tdir_tag)
  3344. na->tdir_tag=IGNORE;
  3345. }
  3346. }
  3347. }
  3348. tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
  3349. tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
  3350. /* free any old stuff and reinit */
  3351. TIFFFreeDirectory(tif);
  3352. TIFFDefaultDirectory(tif);
  3353. /*
  3354. * Electronic Arts writes gray-scale TIFF files
  3355. * without a PlanarConfiguration directory entry.
  3356. * Thus we setup a default value here, even though
  3357. * the TIFF spec says there is no default value.
  3358. */
  3359. TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
  3360. /*
  3361. * Setup default value and then make a pass over
  3362. * the fields to check type and tag information,
  3363. * and to extract info required to size data
  3364. * structures. A second pass is made afterwards
  3365. * to read in everything not taken in the first pass.
  3366. * But we must process the Compression tag first
  3367. * in order to merge in codec-private tag definitions (otherwise
  3368. * we may get complaints about unknown tags). However, the
  3369. * Compression tag may be dependent on the SamplesPerPixel
  3370. * tag value because older TIFF specs permitted Compression
  3371. * to be written as a SamplesPerPixel-count tag entry.
  3372. * Thus if we don't first figure out the correct SamplesPerPixel
  3373. * tag value then we may end up ignoring the Compression tag
  3374. * value because it has an incorrect count value (if the
  3375. * true value of SamplesPerPixel is not 1).
  3376. */
  3377. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
  3378. if (dp)
  3379. {
  3380. if (!TIFFFetchNormalTag(tif,dp,0))
  3381. goto bad;
  3382. dp->tdir_tag=IGNORE;
  3383. }
  3384. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
  3385. if (dp)
  3386. {
  3387. /*
  3388. * The 5.0 spec says the Compression tag has one value, while
  3389. * earlier specs say it has one value per sample. Because of
  3390. * this, we accept the tag if one value is supplied with either
  3391. * count.
  3392. */
  3393. uint16 value;
  3394. enum TIFFReadDirEntryErr err;
  3395. err=TIFFReadDirEntryShort(tif,dp,&value);
  3396. if (err==TIFFReadDirEntryErrCount)
  3397. err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
  3398. if (err!=TIFFReadDirEntryErrOk)
  3399. {
  3400. TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
  3401. goto bad;
  3402. }
  3403. if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
  3404. goto bad;
  3405. dp->tdir_tag=IGNORE;
  3406. }
  3407. else
  3408. {
  3409. if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
  3410. goto bad;
  3411. }
  3412. /*
  3413. * First real pass over the directory.
  3414. */
  3415. for (di=0, dp=dir; di<dircount; di++, dp++)
  3416. {
  3417. if (dp->tdir_tag!=IGNORE)
  3418. {
  3419. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  3420. if (fii == FAILED_FII)
  3421. {
  3422. TIFFWarningExt(tif->tif_clientdata, module,
  3423. "Unknown field with tag %d (0x%x) encountered",
  3424. dp->tdir_tag,dp->tdir_tag);
  3425. /* the following knowingly leaks the
  3426. anonymous field structure */
  3427. if (!_TIFFMergeFields(tif,
  3428. _TIFFCreateAnonField(tif,
  3429. dp->tdir_tag,
  3430. (TIFFDataType) dp->tdir_type),
  3431. 1)) {
  3432. TIFFWarningExt(tif->tif_clientdata,
  3433. module,
  3434. "Registering anonymous field with tag %d (0x%x) failed",
  3435. dp->tdir_tag,
  3436. dp->tdir_tag);
  3437. dp->tdir_tag=IGNORE;
  3438. } else {
  3439. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  3440. assert(fii != FAILED_FII);
  3441. }
  3442. }
  3443. }
  3444. if (dp->tdir_tag!=IGNORE)
  3445. {
  3446. fip=tif->tif_fields[fii];
  3447. if (fip->field_bit==FIELD_IGNORE)
  3448. dp->tdir_tag=IGNORE;
  3449. else
  3450. {
  3451. switch (dp->tdir_tag)
  3452. {
  3453. case TIFFTAG_STRIPOFFSETS:
  3454. case TIFFTAG_STRIPBYTECOUNTS:
  3455. case TIFFTAG_TILEOFFSETS:
  3456. case TIFFTAG_TILEBYTECOUNTS:
  3457. TIFFSetFieldBit(tif,fip->field_bit);
  3458. break;
  3459. case TIFFTAG_IMAGEWIDTH:
  3460. case TIFFTAG_IMAGELENGTH:
  3461. case TIFFTAG_IMAGEDEPTH:
  3462. case TIFFTAG_TILELENGTH:
  3463. case TIFFTAG_TILEWIDTH:
  3464. case TIFFTAG_TILEDEPTH:
  3465. case TIFFTAG_PLANARCONFIG:
  3466. case TIFFTAG_ROWSPERSTRIP:
  3467. case TIFFTAG_EXTRASAMPLES:
  3468. if (!TIFFFetchNormalTag(tif,dp,0))
  3469. goto bad;
  3470. dp->tdir_tag=IGNORE;
  3471. break;
  3472. }
  3473. }
  3474. }
  3475. }
  3476. /*
  3477. * XXX: OJPEG hack.
  3478. * If a) compression is OJPEG, b) planarconfig tag says it's separate,
  3479. * c) strip offsets/bytecounts tag are both present and
  3480. * d) both contain exactly one value, then we consistently find
  3481. * that the buggy implementation of the buggy compression scheme
  3482. * matches contig planarconfig best. So we 'fix-up' the tag here
  3483. */
  3484. if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
  3485. (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
  3486. {
  3487. if (!_TIFFFillStriles(tif))
  3488. goto bad;
  3489. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
  3490. if ((dp!=0)&&(dp->tdir_count==1))
  3491. {
  3492. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
  3493. TIFFTAG_STRIPBYTECOUNTS);
  3494. if ((dp!=0)&&(dp->tdir_count==1))
  3495. {
  3496. tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
  3497. TIFFWarningExt(tif->tif_clientdata,module,
  3498. "Planarconfig tag value assumed incorrect, "
  3499. "assuming data is contig instead of chunky");
  3500. }
  3501. }
  3502. }
  3503. /*
  3504. * Allocate directory structure and setup defaults.
  3505. */
  3506. if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
  3507. {
  3508. MissingRequired(tif,"ImageLength");
  3509. goto bad;
  3510. }
  3511. /*
  3512. * Setup appropriate structures (by strip or by tile)
  3513. */
  3514. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  3515. tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
  3516. tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
  3517. tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
  3518. tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
  3519. tif->tif_flags &= ~TIFF_ISTILED;
  3520. } else {
  3521. tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
  3522. tif->tif_flags |= TIFF_ISTILED;
  3523. }
  3524. if (!tif->tif_dir.td_nstrips) {
  3525. TIFFErrorExt(tif->tif_clientdata, module,
  3526. "Cannot handle zero number of %s",
  3527. isTiled(tif) ? "tiles" : "strips");
  3528. goto bad;
  3529. }
  3530. tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
  3531. if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
  3532. tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
  3533. if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
  3534. #ifdef OJPEG_SUPPORT
  3535. if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
  3536. (isTiled(tif)==0) &&
  3537. (tif->tif_dir.td_nstrips==1)) {
  3538. /*
  3539. * XXX: OJPEG hack.
  3540. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  3541. * and c) the number of strips is 1,
  3542. * then we tolerate the absence of stripoffsets tag,
  3543. * because, presumably, all required data is in the
  3544. * JpegInterchangeFormat stream.
  3545. */
  3546. TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  3547. } else
  3548. #endif
  3549. {
  3550. MissingRequired(tif,
  3551. isTiled(tif) ? "TileOffsets" : "StripOffsets");
  3552. goto bad;
  3553. }
  3554. }
  3555. /*
  3556. * Second pass: extract other information.
  3557. */
  3558. for (di=0, dp=dir; di<dircount; di++, dp++)
  3559. {
  3560. switch (dp->tdir_tag)
  3561. {
  3562. case IGNORE:
  3563. break;
  3564. case TIFFTAG_MINSAMPLEVALUE:
  3565. case TIFFTAG_MAXSAMPLEVALUE:
  3566. case TIFFTAG_BITSPERSAMPLE:
  3567. case TIFFTAG_DATATYPE:
  3568. case TIFFTAG_SAMPLEFORMAT:
  3569. /*
  3570. * The MinSampleValue, MaxSampleValue, BitsPerSample
  3571. * DataType and SampleFormat tags are supposed to be
  3572. * written as one value/sample, but some vendors
  3573. * incorrectly write one value only -- so we accept
  3574. * that as well (yuck). Other vendors write correct
  3575. * value for NumberOfSamples, but incorrect one for
  3576. * BitsPerSample and friends, and we will read this
  3577. * too.
  3578. */
  3579. {
  3580. uint16 value;
  3581. enum TIFFReadDirEntryErr err;
  3582. err=TIFFReadDirEntryShort(tif,dp,&value);
  3583. if (err==TIFFReadDirEntryErrCount)
  3584. err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
  3585. if (err!=TIFFReadDirEntryErrOk)
  3586. {
  3587. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3588. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  3589. goto bad;
  3590. }
  3591. if (!TIFFSetField(tif,dp->tdir_tag,value))
  3592. goto bad;
  3593. if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )
  3594. bitspersample_read = TRUE;
  3595. }
  3596. break;
  3597. case TIFFTAG_SMINSAMPLEVALUE:
  3598. case TIFFTAG_SMAXSAMPLEVALUE:
  3599. {
  3600. double *data = NULL;
  3601. enum TIFFReadDirEntryErr err;
  3602. uint32 saved_flags;
  3603. int m;
  3604. if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
  3605. err = TIFFReadDirEntryErrCount;
  3606. else
  3607. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  3608. if (err!=TIFFReadDirEntryErrOk)
  3609. {
  3610. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3611. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  3612. goto bad;
  3613. }
  3614. saved_flags = tif->tif_flags;
  3615. tif->tif_flags |= TIFF_PERSAMPLE;
  3616. m = TIFFSetField(tif,dp->tdir_tag,data);
  3617. tif->tif_flags = saved_flags;
  3618. _TIFFfree(data);
  3619. if (!m)
  3620. goto bad;
  3621. }
  3622. break;
  3623. case TIFFTAG_STRIPOFFSETS:
  3624. case TIFFTAG_TILEOFFSETS:
  3625. #if defined(DEFER_STRILE_LOAD)
  3626. _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
  3627. dp, sizeof(TIFFDirEntry) );
  3628. #else
  3629. if( tif->tif_dir.td_stripoffset != NULL )
  3630. {
  3631. TIFFErrorExt(tif->tif_clientdata, module,
  3632. "tif->tif_dir.td_stripoffset is "
  3633. "already allocated. Likely duplicated "
  3634. "StripOffsets/TileOffsets tag");
  3635. goto bad;
  3636. }
  3637. if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
  3638. goto bad;
  3639. #endif
  3640. break;
  3641. case TIFFTAG_STRIPBYTECOUNTS:
  3642. case TIFFTAG_TILEBYTECOUNTS:
  3643. #if defined(DEFER_STRILE_LOAD)
  3644. _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
  3645. dp, sizeof(TIFFDirEntry) );
  3646. #else
  3647. if( tif->tif_dir.td_stripbytecount != NULL )
  3648. {
  3649. TIFFErrorExt(tif->tif_clientdata, module,
  3650. "tif->tif_dir.td_stripbytecount is "
  3651. "already allocated. Likely duplicated "
  3652. "StripByteCounts/TileByteCounts tag");
  3653. goto bad;
  3654. }
  3655. if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
  3656. goto bad;
  3657. #endif
  3658. break;
  3659. case TIFFTAG_COLORMAP:
  3660. case TIFFTAG_TRANSFERFUNCTION:
  3661. {
  3662. enum TIFFReadDirEntryErr err;
  3663. uint32 countpersample;
  3664. uint32 countrequired;
  3665. uint32 incrementpersample;
  3666. uint16* value=NULL;
  3667. /* It would be dangerous to instantiate those tag values */
  3668. /* since if td_bitspersample has not yet been read (due to */
  3669. /* unordered tags), it could be read afterwards with a */
  3670. /* values greater than the default one (1), which may cause */
  3671. /* crashes in user code */
  3672. if( !bitspersample_read )
  3673. {
  3674. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3675. TIFFWarningExt(tif->tif_clientdata,module,
  3676. "Ignoring %s since BitsPerSample tag not found",
  3677. fip ? fip->field_name : "unknown tagname");
  3678. continue;
  3679. }
  3680. /* ColorMap or TransferFunction for high bit */
  3681. /* depths do not make much sense and could be */
  3682. /* used as a denial of service vector */
  3683. if (tif->tif_dir.td_bitspersample > 24)
  3684. {
  3685. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3686. TIFFWarningExt(tif->tif_clientdata,module,
  3687. "Ignoring %s because BitsPerSample=%d>24",
  3688. fip ? fip->field_name : "unknown tagname",
  3689. tif->tif_dir.td_bitspersample);
  3690. continue;
  3691. }
  3692. countpersample=(1U<<tif->tif_dir.td_bitspersample);
  3693. if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
  3694. {
  3695. countrequired=countpersample;
  3696. incrementpersample=0;
  3697. }
  3698. else
  3699. {
  3700. countrequired=3*countpersample;
  3701. incrementpersample=countpersample;
  3702. }
  3703. if (dp->tdir_count!=(uint64)countrequired)
  3704. err=TIFFReadDirEntryErrCount;
  3705. else
  3706. err=TIFFReadDirEntryShortArray(tif,dp,&value);
  3707. if (err!=TIFFReadDirEntryErrOk)
  3708. {
  3709. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3710. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
  3711. }
  3712. else
  3713. {
  3714. TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
  3715. _TIFFfree(value);
  3716. }
  3717. }
  3718. break;
  3719. /* BEGIN REV 4.0 COMPATIBILITY */
  3720. case TIFFTAG_OSUBFILETYPE:
  3721. {
  3722. uint16 valueo;
  3723. uint32 value;
  3724. if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
  3725. {
  3726. switch (valueo)
  3727. {
  3728. case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
  3729. case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
  3730. default: value=0; break;
  3731. }
  3732. if (value!=0)
  3733. TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
  3734. }
  3735. }
  3736. break;
  3737. /* END REV 4.0 COMPATIBILITY */
  3738. default:
  3739. (void) TIFFFetchNormalTag(tif, dp, TRUE);
  3740. break;
  3741. }
  3742. }
  3743. /*
  3744. * OJPEG hack:
  3745. * - If a) compression is OJPEG, and b) photometric tag is missing,
  3746. * then we consistently find that photometric should be YCbCr
  3747. * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
  3748. * then we consistently find that the buggy implementation of the
  3749. * buggy compression scheme matches photometric YCbCr instead.
  3750. * - If a) compression is OJPEG, and b) bitspersample tag is missing,
  3751. * then we consistently find bitspersample should be 8.
  3752. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  3753. * and c) photometric is RGB or YCbCr, then we consistently find
  3754. * samplesperpixel should be 3
  3755. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  3756. * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
  3757. * find samplesperpixel should be 3
  3758. */
  3759. if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
  3760. {
  3761. if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
  3762. {
  3763. TIFFWarningExt(tif->tif_clientdata, module,
  3764. "Photometric tag is missing, assuming data is YCbCr");
  3765. if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
  3766. goto bad;
  3767. }
  3768. else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
  3769. {
  3770. tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
  3771. TIFFWarningExt(tif->tif_clientdata, module,
  3772. "Photometric tag value assumed incorrect, "
  3773. "assuming data is YCbCr instead of RGB");
  3774. }
  3775. if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
  3776. {
  3777. TIFFWarningExt(tif->tif_clientdata,module,
  3778. "BitsPerSample tag is missing, assuming 8 bits per sample");
  3779. if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
  3780. goto bad;
  3781. }
  3782. if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
  3783. {
  3784. if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
  3785. {
  3786. TIFFWarningExt(tif->tif_clientdata,module,
  3787. "SamplesPerPixel tag is missing, "
  3788. "assuming correct SamplesPerPixel value is 3");
  3789. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  3790. goto bad;
  3791. }
  3792. if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
  3793. {
  3794. TIFFWarningExt(tif->tif_clientdata,module,
  3795. "SamplesPerPixel tag is missing, "
  3796. "applying correct SamplesPerPixel value of 3");
  3797. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  3798. goto bad;
  3799. }
  3800. else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
  3801. || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
  3802. {
  3803. /*
  3804. * SamplesPerPixel tag is missing, but is not required
  3805. * by spec. Assume correct SamplesPerPixel value of 1.
  3806. */
  3807. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
  3808. goto bad;
  3809. }
  3810. }
  3811. }
  3812. /*
  3813. * Verify Palette image has a Colormap.
  3814. */
  3815. if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
  3816. !TIFFFieldSet(tif, FIELD_COLORMAP)) {
  3817. if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
  3818. tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
  3819. else if (tif->tif_dir.td_bitspersample>=8)
  3820. tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
  3821. else {
  3822. MissingRequired(tif, "Colormap");
  3823. goto bad;
  3824. }
  3825. }
  3826. /*
  3827. * OJPEG hack:
  3828. * We do no further messing with strip/tile offsets/bytecounts in OJPEG
  3829. * TIFFs
  3830. */
  3831. if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
  3832. {
  3833. /*
  3834. * Attempt to deal with a missing StripByteCounts tag.
  3835. */
  3836. if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
  3837. /*
  3838. * Some manufacturers violate the spec by not giving
  3839. * the size of the strips. In this case, assume there
  3840. * is one uncompressed strip of data.
  3841. */
  3842. if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
  3843. tif->tif_dir.td_nstrips > 1) ||
  3844. (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
  3845. tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
  3846. MissingRequired(tif, "StripByteCounts");
  3847. goto bad;
  3848. }
  3849. TIFFWarningExt(tif->tif_clientdata, module,
  3850. "TIFF directory is missing required "
  3851. "\"StripByteCounts\" field, calculating from imagelength");
  3852. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  3853. goto bad;
  3854. /*
  3855. * Assume we have wrong StripByteCount value (in case
  3856. * of single strip) in following cases:
  3857. * - it is equal to zero along with StripOffset;
  3858. * - it is larger than file itself (in case of uncompressed
  3859. * image);
  3860. * - it is smaller than the size of the bytes per row
  3861. * multiplied on the number of rows. The last case should
  3862. * not be checked in the case of writing new image,
  3863. * because we may do not know the exact strip size
  3864. * until the whole image will be written and directory
  3865. * dumped out.
  3866. */
  3867. #define BYTECOUNTLOOKSBAD \
  3868. ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
  3869. (tif->tif_dir.td_compression == COMPRESSION_NONE && \
  3870. tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
  3871. (tif->tif_mode == O_RDONLY && \
  3872. tif->tif_dir.td_compression == COMPRESSION_NONE && \
  3873. tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
  3874. } else if (tif->tif_dir.td_nstrips == 1
  3875. && _TIFFFillStriles(tif)
  3876. && tif->tif_dir.td_stripoffset[0] != 0
  3877. && BYTECOUNTLOOKSBAD) {
  3878. /*
  3879. * XXX: Plexus (and others) sometimes give a value of
  3880. * zero for a tag when they don't know what the
  3881. * correct value is! Try and handle the simple case
  3882. * of estimating the size of a one strip image.
  3883. */
  3884. TIFFWarningExt(tif->tif_clientdata, module,
  3885. "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
  3886. if(EstimateStripByteCounts(tif, dir, dircount) < 0)
  3887. goto bad;
  3888. #if !defined(DEFER_STRILE_LOAD)
  3889. } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
  3890. && tif->tif_dir.td_nstrips > 2
  3891. && tif->tif_dir.td_compression == COMPRESSION_NONE
  3892. && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
  3893. && tif->tif_dir.td_stripbytecount[0] != 0
  3894. && tif->tif_dir.td_stripbytecount[1] != 0 ) {
  3895. /*
  3896. * XXX: Some vendors fill StripByteCount array with
  3897. * absolutely wrong values (it can be equal to
  3898. * StripOffset array, for example). Catch this case
  3899. * here.
  3900. *
  3901. * We avoid this check if deferring strile loading
  3902. * as it would always force us to load the strip/tile
  3903. * information.
  3904. */
  3905. TIFFWarningExt(tif->tif_clientdata, module,
  3906. "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
  3907. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  3908. goto bad;
  3909. #endif /* !defined(DEFER_STRILE_LOAD) */
  3910. }
  3911. }
  3912. if (dir)
  3913. {
  3914. _TIFFfree(dir);
  3915. dir=NULL;
  3916. }
  3917. if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  3918. {
  3919. if (tif->tif_dir.td_bitspersample>=16)
  3920. tif->tif_dir.td_maxsamplevalue=0xFFFF;
  3921. else
  3922. tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
  3923. }
  3924. /*
  3925. * XXX: We can optimize checking for the strip bounds using the sorted
  3926. * bytecounts array. See also comments for TIFFAppendToStrip()
  3927. * function in tif_write.c.
  3928. */
  3929. #if !defined(DEFER_STRILE_LOAD)
  3930. if (tif->tif_dir.td_nstrips > 1) {
  3931. uint32 strip;
  3932. tif->tif_dir.td_stripbytecountsorted = 1;
  3933. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
  3934. if (tif->tif_dir.td_stripoffset[strip - 1] >
  3935. tif->tif_dir.td_stripoffset[strip]) {
  3936. tif->tif_dir.td_stripbytecountsorted = 0;
  3937. break;
  3938. }
  3939. }
  3940. }
  3941. #endif /* !defined(DEFER_STRILE_LOAD) */
  3942. /*
  3943. * An opportunity for compression mode dependent tag fixup
  3944. */
  3945. (*tif->tif_fixuptags)(tif);
  3946. /*
  3947. * Some manufacturers make life difficult by writing
  3948. * large amounts of uncompressed data as a single strip.
  3949. * This is contrary to the recommendations of the spec.
  3950. * The following makes an attempt at breaking such images
  3951. * into strips closer to the recommended 8k bytes. A
  3952. * side effect, however, is that the RowsPerStrip tag
  3953. * value may be changed.
  3954. */
  3955. if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
  3956. (tif->tif_dir.td_nstrips==1)&&
  3957. (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
  3958. ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
  3959. {
  3960. if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
  3961. return 0;
  3962. ChopUpSingleUncompressedStrip(tif);
  3963. }
  3964. /*
  3965. * Clear the dirty directory flag.
  3966. */
  3967. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  3968. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  3969. /*
  3970. * Reinitialize i/o since we are starting on a new directory.
  3971. */
  3972. tif->tif_row = (uint32) -1;
  3973. tif->tif_curstrip = (uint32) -1;
  3974. tif->tif_col = (uint32) -1;
  3975. tif->tif_curtile = (uint32) -1;
  3976. tif->tif_tilesize = (tmsize_t) -1;
  3977. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  3978. if (!tif->tif_scanlinesize) {
  3979. TIFFErrorExt(tif->tif_clientdata, module,
  3980. "Cannot handle zero scanline size");
  3981. return (0);
  3982. }
  3983. if (isTiled(tif)) {
  3984. tif->tif_tilesize = TIFFTileSize(tif);
  3985. if (!tif->tif_tilesize) {
  3986. TIFFErrorExt(tif->tif_clientdata, module,
  3987. "Cannot handle zero tile size");
  3988. return (0);
  3989. }
  3990. } else {
  3991. if (!TIFFStripSize(tif)) {
  3992. TIFFErrorExt(tif->tif_clientdata, module,
  3993. "Cannot handle zero strip size");
  3994. return (0);
  3995. }
  3996. }
  3997. return (1);
  3998. bad:
  3999. if (dir)
  4000. _TIFFfree(dir);
  4001. return (0);
  4002. }
  4003. static void
  4004. TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  4005. {
  4006. static const char module[] = "TIFFReadDirectoryCheckOrder";
  4007. uint16 m;
  4008. uint16 n;
  4009. TIFFDirEntry* o;
  4010. m=0;
  4011. for (n=0, o=dir; n<dircount; n++, o++)
  4012. {
  4013. if (o->tdir_tag<m)
  4014. {
  4015. TIFFWarningExt(tif->tif_clientdata,module,
  4016. "Invalid TIFF directory; tags are not sorted in ascending order");
  4017. break;
  4018. }
  4019. m=o->tdir_tag+1;
  4020. }
  4021. }
  4022. static TIFFDirEntry*
  4023. TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
  4024. {
  4025. TIFFDirEntry* m;
  4026. uint16 n;
  4027. (void) tif;
  4028. for (m=dir, n=0; n<dircount; m++, n++)
  4029. {
  4030. if (m->tdir_tag==tagid)
  4031. return(m);
  4032. }
  4033. return(0);
  4034. }
  4035. static void
  4036. TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
  4037. {
  4038. int32 ma,mb,mc;
  4039. ma=-1;
  4040. mc=(int32)tif->tif_nfields;
  4041. while (1)
  4042. {
  4043. if (ma+1==mc)
  4044. {
  4045. *fii = FAILED_FII;
  4046. return;
  4047. }
  4048. mb=(ma+mc)/2;
  4049. if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
  4050. break;
  4051. if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
  4052. ma=mb;
  4053. else
  4054. mc=mb;
  4055. }
  4056. while (1)
  4057. {
  4058. if (mb==0)
  4059. break;
  4060. if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
  4061. break;
  4062. mb--;
  4063. }
  4064. *fii=mb;
  4065. }
  4066. /*
  4067. * Read custom directory from the arbitrary offset.
  4068. * The code is very similar to TIFFReadDirectory().
  4069. */
  4070. int
  4071. TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
  4072. const TIFFFieldArray* infoarray)
  4073. {
  4074. static const char module[] = "TIFFReadCustomDirectory";
  4075. TIFFDirEntry* dir;
  4076. uint16 dircount;
  4077. TIFFDirEntry* dp;
  4078. uint16 di;
  4079. const TIFFField* fip;
  4080. uint32 fii;
  4081. _TIFFSetupFields(tif, infoarray);
  4082. dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
  4083. if (!dircount)
  4084. {
  4085. TIFFErrorExt(tif->tif_clientdata,module,
  4086. "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
  4087. return 0;
  4088. }
  4089. TIFFFreeDirectory(tif);
  4090. _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
  4091. TIFFReadDirectoryCheckOrder(tif,dir,dircount);
  4092. for (di=0, dp=dir; di<dircount; di++, dp++)
  4093. {
  4094. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4095. if (fii == FAILED_FII)
  4096. {
  4097. TIFFWarningExt(tif->tif_clientdata, module,
  4098. "Unknown field with tag %d (0x%x) encountered",
  4099. dp->tdir_tag, dp->tdir_tag);
  4100. if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
  4101. dp->tdir_tag,
  4102. (TIFFDataType) dp->tdir_type),
  4103. 1)) {
  4104. TIFFWarningExt(tif->tif_clientdata, module,
  4105. "Registering anonymous field with tag %d (0x%x) failed",
  4106. dp->tdir_tag, dp->tdir_tag);
  4107. dp->tdir_tag=IGNORE;
  4108. } else {
  4109. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4110. assert( fii != FAILED_FII );
  4111. }
  4112. }
  4113. if (dp->tdir_tag!=IGNORE)
  4114. {
  4115. fip=tif->tif_fields[fii];
  4116. if (fip->field_bit==FIELD_IGNORE)
  4117. dp->tdir_tag=IGNORE;
  4118. else
  4119. {
  4120. /* check data type */
  4121. while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
  4122. {
  4123. fii++;
  4124. if ((fii==tif->tif_nfields)||
  4125. (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
  4126. {
  4127. fii=0xFFFF;
  4128. break;
  4129. }
  4130. fip=tif->tif_fields[fii];
  4131. }
  4132. if (fii==0xFFFF)
  4133. {
  4134. TIFFWarningExt(tif->tif_clientdata, module,
  4135. "Wrong data type %d for \"%s\"; tag ignored",
  4136. dp->tdir_type,fip->field_name);
  4137. dp->tdir_tag=IGNORE;
  4138. }
  4139. else
  4140. {
  4141. /* check count if known in advance */
  4142. if ((fip->field_readcount!=TIFF_VARIABLE)&&
  4143. (fip->field_readcount!=TIFF_VARIABLE2))
  4144. {
  4145. uint32 expected;
  4146. if (fip->field_readcount==TIFF_SPP)
  4147. expected=(uint32)tif->tif_dir.td_samplesperpixel;
  4148. else
  4149. expected=(uint32)fip->field_readcount;
  4150. if (!CheckDirCount(tif,dp,expected))
  4151. dp->tdir_tag=IGNORE;
  4152. }
  4153. }
  4154. }
  4155. switch (dp->tdir_tag)
  4156. {
  4157. case IGNORE:
  4158. break;
  4159. case EXIFTAG_SUBJECTDISTANCE:
  4160. (void) TIFFFetchSubjectDistance(tif,dp);
  4161. break;
  4162. default:
  4163. (void) TIFFFetchNormalTag(tif, dp, TRUE);
  4164. break;
  4165. }
  4166. }
  4167. }
  4168. if (dir)
  4169. _TIFFfree(dir);
  4170. return 1;
  4171. }
  4172. /*
  4173. * EXIF is important special case of custom IFD, so we have a special
  4174. * function to read it.
  4175. */
  4176. int
  4177. TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
  4178. {
  4179. const TIFFFieldArray* exifFieldArray;
  4180. exifFieldArray = _TIFFGetExifFields();
  4181. return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
  4182. }
  4183. static int
  4184. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  4185. {
  4186. static const char module[] = "EstimateStripByteCounts";
  4187. TIFFDirEntry *dp;
  4188. TIFFDirectory *td = &tif->tif_dir;
  4189. uint32 strip;
  4190. /* Do not try to load stripbytecount as we will compute it */
  4191. if( !_TIFFFillStrilesInternal( tif, 0 ) )
  4192. return -1;
  4193. if (td->td_stripbytecount)
  4194. _TIFFfree(td->td_stripbytecount);
  4195. td->td_stripbytecount = (uint64*)
  4196. _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
  4197. "for \"StripByteCounts\" array");
  4198. if( td->td_stripbytecount == NULL )
  4199. return -1;
  4200. if (td->td_compression != COMPRESSION_NONE) {
  4201. uint64 space;
  4202. uint64 filesize;
  4203. uint16 n;
  4204. filesize = TIFFGetFileSize(tif);
  4205. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4206. space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
  4207. else
  4208. space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
  4209. /* calculate amount of space used by indirect values */
  4210. for (dp = dir, n = dircount; n > 0; n--, dp++)
  4211. {
  4212. uint32 typewidth;
  4213. uint64 datasize;
  4214. typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
  4215. if (typewidth == 0) {
  4216. TIFFErrorExt(tif->tif_clientdata, module,
  4217. "Cannot determine size of unknown tag type %d",
  4218. dp->tdir_type);
  4219. return -1;
  4220. }
  4221. datasize=(uint64)typewidth*dp->tdir_count;
  4222. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4223. {
  4224. if (datasize<=4)
  4225. datasize=0;
  4226. }
  4227. else
  4228. {
  4229. if (datasize<=8)
  4230. datasize=0;
  4231. }
  4232. space+=datasize;
  4233. }
  4234. space = filesize - space;
  4235. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  4236. space /= td->td_samplesperpixel;
  4237. for (strip = 0; strip < td->td_nstrips; strip++)
  4238. td->td_stripbytecount[strip] = space;
  4239. /*
  4240. * This gross hack handles the case were the offset to
  4241. * the last strip is past the place where we think the strip
  4242. * should begin. Since a strip of data must be contiguous,
  4243. * it's safe to assume that we've overestimated the amount
  4244. * of data in the strip and trim this number back accordingly.
  4245. */
  4246. strip--;
  4247. if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
  4248. td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
  4249. } else if (isTiled(tif)) {
  4250. uint64 bytespertile = TIFFTileSize64(tif);
  4251. for (strip = 0; strip < td->td_nstrips; strip++)
  4252. td->td_stripbytecount[strip] = bytespertile;
  4253. } else {
  4254. uint64 rowbytes = TIFFScanlineSize64(tif);
  4255. uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
  4256. for (strip = 0; strip < td->td_nstrips; strip++)
  4257. td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
  4258. }
  4259. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  4260. if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  4261. td->td_rowsperstrip = td->td_imagelength;
  4262. return 1;
  4263. }
  4264. static void
  4265. MissingRequired(TIFF* tif, const char* tagname)
  4266. {
  4267. static const char module[] = "MissingRequired";
  4268. TIFFErrorExt(tif->tif_clientdata, module,
  4269. "TIFF directory is missing required \"%s\" field",
  4270. tagname);
  4271. }
  4272. /*
  4273. * Check the directory offset against the list of already seen directory
  4274. * offsets. This is a trick to prevent IFD looping. The one can create TIFF
  4275. * file with looped directory pointers. We will maintain a list of already
  4276. * seen directories and check every IFD offset against that list.
  4277. */
  4278. static int
  4279. TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
  4280. {
  4281. uint16 n;
  4282. if (diroff == 0) /* no more directories */
  4283. return 0;
  4284. if (tif->tif_dirnumber == 65535) {
  4285. TIFFErrorExt(tif->tif_clientdata, "TIFFCheckDirOffset",
  4286. "Cannot handle more than 65535 TIFF directories");
  4287. return 0;
  4288. }
  4289. for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
  4290. if (tif->tif_dirlist[n] == diroff)
  4291. return 0;
  4292. }
  4293. tif->tif_dirnumber++;
  4294. if (tif->tif_dirlist == NULL || tif->tif_dirnumber > tif->tif_dirlistsize) {
  4295. uint64* new_dirlist;
  4296. /*
  4297. * XXX: Reduce memory allocation granularity of the dirlist
  4298. * array.
  4299. */
  4300. new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
  4301. tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
  4302. if (!new_dirlist)
  4303. return 0;
  4304. if( tif->tif_dirnumber >= 32768 )
  4305. tif->tif_dirlistsize = 65535;
  4306. else
  4307. tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
  4308. tif->tif_dirlist = new_dirlist;
  4309. }
  4310. tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
  4311. return 1;
  4312. }
  4313. /*
  4314. * Check the count field of a directory entry against a known value. The
  4315. * caller is expected to skip/ignore the tag if there is a mismatch.
  4316. */
  4317. static int
  4318. CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
  4319. {
  4320. if ((uint64)count > dir->tdir_count) {
  4321. const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  4322. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  4323. "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
  4324. fip ? fip->field_name : "unknown tagname",
  4325. dir->tdir_count, count);
  4326. return (0);
  4327. } else if ((uint64)count < dir->tdir_count) {
  4328. const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  4329. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  4330. "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
  4331. fip ? fip->field_name : "unknown tagname",
  4332. dir->tdir_count, count);
  4333. dir->tdir_count = count;
  4334. return (1);
  4335. }
  4336. return (1);
  4337. }
  4338. /*
  4339. * Read IFD structure from the specified offset. If the pointer to
  4340. * nextdiroff variable has been specified, read it too. Function returns a
  4341. * number of fields in the directory or 0 if failed.
  4342. */
  4343. static uint16
  4344. TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
  4345. uint64 *nextdiroff)
  4346. {
  4347. static const char module[] = "TIFFFetchDirectory";
  4348. void* origdir;
  4349. uint16 dircount16;
  4350. uint32 dirsize;
  4351. TIFFDirEntry* dir;
  4352. uint8* ma;
  4353. TIFFDirEntry* mb;
  4354. uint16 n;
  4355. assert(pdir);
  4356. tif->tif_diroff = diroff;
  4357. if (nextdiroff)
  4358. *nextdiroff = 0;
  4359. if (!isMapped(tif)) {
  4360. if (!SeekOK(tif, tif->tif_diroff)) {
  4361. TIFFErrorExt(tif->tif_clientdata, module,
  4362. "%s: Seek error accessing TIFF directory",
  4363. tif->tif_name);
  4364. return 0;
  4365. }
  4366. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4367. {
  4368. if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
  4369. TIFFErrorExt(tif->tif_clientdata, module,
  4370. "%s: Can not read TIFF directory count",
  4371. tif->tif_name);
  4372. return 0;
  4373. }
  4374. if (tif->tif_flags & TIFF_SWAB)
  4375. TIFFSwabShort(&dircount16);
  4376. if (dircount16>4096)
  4377. {
  4378. TIFFErrorExt(tif->tif_clientdata, module,
  4379. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4380. return 0;
  4381. }
  4382. dirsize = 12;
  4383. } else {
  4384. uint64 dircount64;
  4385. if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
  4386. TIFFErrorExt(tif->tif_clientdata, module,
  4387. "%s: Can not read TIFF directory count",
  4388. tif->tif_name);
  4389. return 0;
  4390. }
  4391. if (tif->tif_flags & TIFF_SWAB)
  4392. TIFFSwabLong8(&dircount64);
  4393. if (dircount64>4096)
  4394. {
  4395. TIFFErrorExt(tif->tif_clientdata, module,
  4396. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4397. return 0;
  4398. }
  4399. dircount16 = (uint16)dircount64;
  4400. dirsize = 20;
  4401. }
  4402. origdir = _TIFFCheckMalloc(tif, dircount16,
  4403. dirsize, "to read TIFF directory");
  4404. if (origdir == NULL)
  4405. return 0;
  4406. if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
  4407. TIFFErrorExt(tif->tif_clientdata, module,
  4408. "%.100s: Can not read TIFF directory",
  4409. tif->tif_name);
  4410. _TIFFfree(origdir);
  4411. return 0;
  4412. }
  4413. /*
  4414. * Read offset to next directory for sequential scans if
  4415. * needed.
  4416. */
  4417. if (nextdiroff)
  4418. {
  4419. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4420. {
  4421. uint32 nextdiroff32;
  4422. if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
  4423. nextdiroff32 = 0;
  4424. if (tif->tif_flags&TIFF_SWAB)
  4425. TIFFSwabLong(&nextdiroff32);
  4426. *nextdiroff=nextdiroff32;
  4427. } else {
  4428. if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
  4429. *nextdiroff = 0;
  4430. if (tif->tif_flags&TIFF_SWAB)
  4431. TIFFSwabLong8(nextdiroff);
  4432. }
  4433. }
  4434. } else {
  4435. tmsize_t m;
  4436. tmsize_t off = (tmsize_t) tif->tif_diroff;
  4437. if ((uint64)off!=tif->tif_diroff)
  4438. {
  4439. TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
  4440. return(0);
  4441. }
  4442. /*
  4443. * Check for integer overflow when validating the dir_off,
  4444. * otherwise a very high offset may cause an OOB read and
  4445. * crash the client. Make two comparisons instead of
  4446. *
  4447. * off + sizeof(uint16) > tif->tif_size
  4448. *
  4449. * to avoid overflow.
  4450. */
  4451. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4452. {
  4453. m=off+sizeof(uint16);
  4454. if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
  4455. TIFFErrorExt(tif->tif_clientdata, module,
  4456. "Can not read TIFF directory count");
  4457. return 0;
  4458. } else {
  4459. _TIFFmemcpy(&dircount16, tif->tif_base + off,
  4460. sizeof(uint16));
  4461. }
  4462. off += sizeof (uint16);
  4463. if (tif->tif_flags & TIFF_SWAB)
  4464. TIFFSwabShort(&dircount16);
  4465. if (dircount16>4096)
  4466. {
  4467. TIFFErrorExt(tif->tif_clientdata, module,
  4468. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4469. return 0;
  4470. }
  4471. dirsize = 12;
  4472. }
  4473. else
  4474. {
  4475. uint64 dircount64;
  4476. m=off+sizeof(uint64);
  4477. if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
  4478. TIFFErrorExt(tif->tif_clientdata, module,
  4479. "Can not read TIFF directory count");
  4480. return 0;
  4481. } else {
  4482. _TIFFmemcpy(&dircount64, tif->tif_base + off,
  4483. sizeof(uint64));
  4484. }
  4485. off += sizeof (uint64);
  4486. if (tif->tif_flags & TIFF_SWAB)
  4487. TIFFSwabLong8(&dircount64);
  4488. if (dircount64>4096)
  4489. {
  4490. TIFFErrorExt(tif->tif_clientdata, module,
  4491. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4492. return 0;
  4493. }
  4494. dircount16 = (uint16)dircount64;
  4495. dirsize = 20;
  4496. }
  4497. if (dircount16 == 0 )
  4498. {
  4499. TIFFErrorExt(tif->tif_clientdata, module,
  4500. "Sanity check on directory count failed, zero tag directories not supported");
  4501. return 0;
  4502. }
  4503. origdir = _TIFFCheckMalloc(tif, dircount16,
  4504. dirsize,
  4505. "to read TIFF directory");
  4506. if (origdir == NULL)
  4507. return 0;
  4508. m=off+dircount16*dirsize;
  4509. if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
  4510. TIFFErrorExt(tif->tif_clientdata, module,
  4511. "Can not read TIFF directory");
  4512. _TIFFfree(origdir);
  4513. return 0;
  4514. } else {
  4515. _TIFFmemcpy(origdir, tif->tif_base + off,
  4516. dircount16 * dirsize);
  4517. }
  4518. if (nextdiroff) {
  4519. off += dircount16 * dirsize;
  4520. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4521. {
  4522. uint32 nextdiroff32;
  4523. m=off+sizeof(uint32);
  4524. if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
  4525. nextdiroff32 = 0;
  4526. else
  4527. _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
  4528. sizeof (uint32));
  4529. if (tif->tif_flags&TIFF_SWAB)
  4530. TIFFSwabLong(&nextdiroff32);
  4531. *nextdiroff = nextdiroff32;
  4532. }
  4533. else
  4534. {
  4535. m=off+sizeof(uint64);
  4536. if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
  4537. *nextdiroff = 0;
  4538. else
  4539. _TIFFmemcpy(nextdiroff, tif->tif_base + off,
  4540. sizeof (uint64));
  4541. if (tif->tif_flags&TIFF_SWAB)
  4542. TIFFSwabLong8(nextdiroff);
  4543. }
  4544. }
  4545. }
  4546. dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
  4547. sizeof(TIFFDirEntry),
  4548. "to read TIFF directory");
  4549. if (dir==0)
  4550. {
  4551. _TIFFfree(origdir);
  4552. return 0;
  4553. }
  4554. ma=(uint8*)origdir;
  4555. mb=dir;
  4556. for (n=0; n<dircount16; n++)
  4557. {
  4558. if (tif->tif_flags&TIFF_SWAB)
  4559. TIFFSwabShort((uint16*)ma);
  4560. mb->tdir_tag=*(uint16*)ma;
  4561. ma+=sizeof(uint16);
  4562. if (tif->tif_flags&TIFF_SWAB)
  4563. TIFFSwabShort((uint16*)ma);
  4564. mb->tdir_type=*(uint16*)ma;
  4565. ma+=sizeof(uint16);
  4566. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4567. {
  4568. if (tif->tif_flags&TIFF_SWAB)
  4569. TIFFSwabLong((uint32*)ma);
  4570. mb->tdir_count=(uint64)(*(uint32*)ma);
  4571. ma+=sizeof(uint32);
  4572. *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
  4573. ma+=sizeof(uint32);
  4574. }
  4575. else
  4576. {
  4577. if (tif->tif_flags&TIFF_SWAB)
  4578. TIFFSwabLong8((uint64*)ma);
  4579. mb->tdir_count=TIFFReadUInt64(ma);
  4580. ma+=sizeof(uint64);
  4581. mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
  4582. ma+=sizeof(uint64);
  4583. }
  4584. mb++;
  4585. }
  4586. _TIFFfree(origdir);
  4587. *pdir = dir;
  4588. return dircount16;
  4589. }
  4590. /*
  4591. * Fetch a tag that is not handled by special case code.
  4592. */
  4593. static int
  4594. TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
  4595. {
  4596. static const char module[] = "TIFFFetchNormalTag";
  4597. enum TIFFReadDirEntryErr err;
  4598. uint32 fii;
  4599. const TIFFField* fip = NULL;
  4600. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4601. if( fii == FAILED_FII )
  4602. {
  4603. TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
  4604. "No definition found for tag %d",
  4605. dp->tdir_tag);
  4606. return 0;
  4607. }
  4608. fip=tif->tif_fields[fii];
  4609. assert(fip != NULL); /* should not happen */
  4610. assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
  4611. assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
  4612. err=TIFFReadDirEntryErrOk;
  4613. switch (fip->set_field_type)
  4614. {
  4615. case TIFF_SETGET_UNDEFINED:
  4616. break;
  4617. case TIFF_SETGET_ASCII:
  4618. {
  4619. uint8* data;
  4620. assert(fip->field_passcount==0);
  4621. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4622. if (err==TIFFReadDirEntryErrOk)
  4623. {
  4624. uint8* ma;
  4625. uint32 mb;
  4626. int n;
  4627. ma=data;
  4628. mb=0;
  4629. while (mb<(uint32)dp->tdir_count)
  4630. {
  4631. if (*ma==0)
  4632. break;
  4633. ma++;
  4634. mb++;
  4635. }
  4636. if (mb+1<(uint32)dp->tdir_count)
  4637. TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" contains null byte in value; value incorrectly truncated during reading due to implementation limitations",fip->field_name);
  4638. else if (mb+1>(uint32)dp->tdir_count)
  4639. {
  4640. uint8* o;
  4641. TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
  4642. if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
  4643. o=NULL;
  4644. else
  4645. o=_TIFFmalloc((uint32)dp->tdir_count+1);
  4646. if (o==NULL)
  4647. {
  4648. if (data!=NULL)
  4649. _TIFFfree(data);
  4650. return(0);
  4651. }
  4652. _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
  4653. o[(uint32)dp->tdir_count]=0;
  4654. if (data!=0)
  4655. _TIFFfree(data);
  4656. data=o;
  4657. }
  4658. n=TIFFSetField(tif,dp->tdir_tag,data);
  4659. if (data!=0)
  4660. _TIFFfree(data);
  4661. if (!n)
  4662. return(0);
  4663. }
  4664. }
  4665. break;
  4666. case TIFF_SETGET_UINT8:
  4667. {
  4668. uint8 data=0;
  4669. assert(fip->field_readcount==1);
  4670. assert(fip->field_passcount==0);
  4671. err=TIFFReadDirEntryByte(tif,dp,&data);
  4672. if (err==TIFFReadDirEntryErrOk)
  4673. {
  4674. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4675. return(0);
  4676. }
  4677. }
  4678. break;
  4679. case TIFF_SETGET_UINT16:
  4680. {
  4681. uint16 data;
  4682. assert(fip->field_readcount==1);
  4683. assert(fip->field_passcount==0);
  4684. err=TIFFReadDirEntryShort(tif,dp,&data);
  4685. if (err==TIFFReadDirEntryErrOk)
  4686. {
  4687. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4688. return(0);
  4689. }
  4690. }
  4691. break;
  4692. case TIFF_SETGET_UINT32:
  4693. {
  4694. uint32 data;
  4695. assert(fip->field_readcount==1);
  4696. assert(fip->field_passcount==0);
  4697. err=TIFFReadDirEntryLong(tif,dp,&data);
  4698. if (err==TIFFReadDirEntryErrOk)
  4699. {
  4700. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4701. return(0);
  4702. }
  4703. }
  4704. break;
  4705. case TIFF_SETGET_UINT64:
  4706. {
  4707. uint64 data;
  4708. assert(fip->field_readcount==1);
  4709. assert(fip->field_passcount==0);
  4710. err=TIFFReadDirEntryLong8(tif,dp,&data);
  4711. if (err==TIFFReadDirEntryErrOk)
  4712. {
  4713. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4714. return(0);
  4715. }
  4716. }
  4717. break;
  4718. case TIFF_SETGET_FLOAT:
  4719. {
  4720. float data;
  4721. assert(fip->field_readcount==1);
  4722. assert(fip->field_passcount==0);
  4723. err=TIFFReadDirEntryFloat(tif,dp,&data);
  4724. if (err==TIFFReadDirEntryErrOk)
  4725. {
  4726. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4727. return(0);
  4728. }
  4729. }
  4730. break;
  4731. case TIFF_SETGET_DOUBLE:
  4732. {
  4733. double data;
  4734. assert(fip->field_readcount==1);
  4735. assert(fip->field_passcount==0);
  4736. err=TIFFReadDirEntryDouble(tif,dp,&data);
  4737. if (err==TIFFReadDirEntryErrOk)
  4738. {
  4739. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4740. return(0);
  4741. }
  4742. }
  4743. break;
  4744. case TIFF_SETGET_IFD8:
  4745. {
  4746. uint64 data;
  4747. assert(fip->field_readcount==1);
  4748. assert(fip->field_passcount==0);
  4749. err=TIFFReadDirEntryIfd8(tif,dp,&data);
  4750. if (err==TIFFReadDirEntryErrOk)
  4751. {
  4752. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4753. return(0);
  4754. }
  4755. }
  4756. break;
  4757. case TIFF_SETGET_UINT16_PAIR:
  4758. {
  4759. uint16* data;
  4760. assert(fip->field_readcount==2);
  4761. assert(fip->field_passcount==0);
  4762. if (dp->tdir_count!=2) {
  4763. TIFFWarningExt(tif->tif_clientdata,module,
  4764. "incorrect count for field \"%s\", expected 2, got %d",
  4765. fip->field_name,(int)dp->tdir_count);
  4766. return(0);
  4767. }
  4768. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4769. if (err==TIFFReadDirEntryErrOk)
  4770. {
  4771. int m;
  4772. m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
  4773. _TIFFfree(data);
  4774. if (!m)
  4775. return(0);
  4776. }
  4777. }
  4778. break;
  4779. case TIFF_SETGET_C0_UINT8:
  4780. {
  4781. uint8* data;
  4782. assert(fip->field_readcount>=1);
  4783. assert(fip->field_passcount==0);
  4784. if (dp->tdir_count!=(uint64)fip->field_readcount) {
  4785. TIFFWarningExt(tif->tif_clientdata,module,
  4786. "incorrect count for field \"%s\", expected %d, got %d",
  4787. fip->field_name,(int) fip->field_readcount, (int)dp->tdir_count);
  4788. return 0;
  4789. }
  4790. else
  4791. {
  4792. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4793. if (err==TIFFReadDirEntryErrOk)
  4794. {
  4795. int m;
  4796. m=TIFFSetField(tif,dp->tdir_tag,data);
  4797. if (data!=0)
  4798. _TIFFfree(data);
  4799. if (!m)
  4800. return(0);
  4801. }
  4802. }
  4803. }
  4804. break;
  4805. case TIFF_SETGET_C0_UINT16:
  4806. {
  4807. uint16* data;
  4808. assert(fip->field_readcount>=1);
  4809. assert(fip->field_passcount==0);
  4810. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4811. /* corrupt file */;
  4812. else
  4813. {
  4814. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4815. if (err==TIFFReadDirEntryErrOk)
  4816. {
  4817. int m;
  4818. m=TIFFSetField(tif,dp->tdir_tag,data);
  4819. if (data!=0)
  4820. _TIFFfree(data);
  4821. if (!m)
  4822. return(0);
  4823. }
  4824. }
  4825. }
  4826. break;
  4827. case TIFF_SETGET_C0_UINT32:
  4828. {
  4829. uint32* data;
  4830. assert(fip->field_readcount>=1);
  4831. assert(fip->field_passcount==0);
  4832. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4833. /* corrupt file */;
  4834. else
  4835. {
  4836. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  4837. if (err==TIFFReadDirEntryErrOk)
  4838. {
  4839. int m;
  4840. m=TIFFSetField(tif,dp->tdir_tag,data);
  4841. if (data!=0)
  4842. _TIFFfree(data);
  4843. if (!m)
  4844. return(0);
  4845. }
  4846. }
  4847. }
  4848. break;
  4849. case TIFF_SETGET_C0_FLOAT:
  4850. {
  4851. float* data;
  4852. assert(fip->field_readcount>=1);
  4853. assert(fip->field_passcount==0);
  4854. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4855. /* corrupt file */;
  4856. else
  4857. {
  4858. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  4859. if (err==TIFFReadDirEntryErrOk)
  4860. {
  4861. int m;
  4862. m=TIFFSetField(tif,dp->tdir_tag,data);
  4863. if (data!=0)
  4864. _TIFFfree(data);
  4865. if (!m)
  4866. return(0);
  4867. }
  4868. }
  4869. }
  4870. break;
  4871. case TIFF_SETGET_C16_ASCII:
  4872. {
  4873. uint8* data;
  4874. assert(fip->field_readcount==TIFF_VARIABLE);
  4875. assert(fip->field_passcount==1);
  4876. if (dp->tdir_count>0xFFFF)
  4877. err=TIFFReadDirEntryErrCount;
  4878. else
  4879. {
  4880. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4881. if (err==TIFFReadDirEntryErrOk)
  4882. {
  4883. int m;
  4884. if( dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
  4885. {
  4886. TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
  4887. data[dp->tdir_count-1] = '\0';
  4888. }
  4889. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4890. if (data!=0)
  4891. _TIFFfree(data);
  4892. if (!m)
  4893. return(0);
  4894. }
  4895. }
  4896. }
  4897. break;
  4898. case TIFF_SETGET_C16_UINT8:
  4899. {
  4900. uint8* data;
  4901. assert(fip->field_readcount==TIFF_VARIABLE);
  4902. assert(fip->field_passcount==1);
  4903. if (dp->tdir_count>0xFFFF)
  4904. err=TIFFReadDirEntryErrCount;
  4905. else
  4906. {
  4907. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4908. if (err==TIFFReadDirEntryErrOk)
  4909. {
  4910. int m;
  4911. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4912. if (data!=0)
  4913. _TIFFfree(data);
  4914. if (!m)
  4915. return(0);
  4916. }
  4917. }
  4918. }
  4919. break;
  4920. case TIFF_SETGET_C16_UINT16:
  4921. {
  4922. uint16* data;
  4923. assert(fip->field_readcount==TIFF_VARIABLE);
  4924. assert(fip->field_passcount==1);
  4925. if (dp->tdir_count>0xFFFF)
  4926. err=TIFFReadDirEntryErrCount;
  4927. else
  4928. {
  4929. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4930. if (err==TIFFReadDirEntryErrOk)
  4931. {
  4932. int m;
  4933. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4934. if (data!=0)
  4935. _TIFFfree(data);
  4936. if (!m)
  4937. return(0);
  4938. }
  4939. }
  4940. }
  4941. break;
  4942. case TIFF_SETGET_C16_UINT32:
  4943. {
  4944. uint32* data;
  4945. assert(fip->field_readcount==TIFF_VARIABLE);
  4946. assert(fip->field_passcount==1);
  4947. if (dp->tdir_count>0xFFFF)
  4948. err=TIFFReadDirEntryErrCount;
  4949. else
  4950. {
  4951. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  4952. if (err==TIFFReadDirEntryErrOk)
  4953. {
  4954. int m;
  4955. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4956. if (data!=0)
  4957. _TIFFfree(data);
  4958. if (!m)
  4959. return(0);
  4960. }
  4961. }
  4962. }
  4963. break;
  4964. case TIFF_SETGET_C16_UINT64:
  4965. {
  4966. uint64* data;
  4967. assert(fip->field_readcount==TIFF_VARIABLE);
  4968. assert(fip->field_passcount==1);
  4969. if (dp->tdir_count>0xFFFF)
  4970. err=TIFFReadDirEntryErrCount;
  4971. else
  4972. {
  4973. err=TIFFReadDirEntryLong8Array(tif,dp,&data);
  4974. if (err==TIFFReadDirEntryErrOk)
  4975. {
  4976. int m;
  4977. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4978. if (data!=0)
  4979. _TIFFfree(data);
  4980. if (!m)
  4981. return(0);
  4982. }
  4983. }
  4984. }
  4985. break;
  4986. case TIFF_SETGET_C16_FLOAT:
  4987. {
  4988. float* data;
  4989. assert(fip->field_readcount==TIFF_VARIABLE);
  4990. assert(fip->field_passcount==1);
  4991. if (dp->tdir_count>0xFFFF)
  4992. err=TIFFReadDirEntryErrCount;
  4993. else
  4994. {
  4995. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  4996. if (err==TIFFReadDirEntryErrOk)
  4997. {
  4998. int m;
  4999. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  5000. if (data!=0)
  5001. _TIFFfree(data);
  5002. if (!m)
  5003. return(0);
  5004. }
  5005. }
  5006. }
  5007. break;
  5008. case TIFF_SETGET_C16_DOUBLE:
  5009. {
  5010. double* data;
  5011. assert(fip->field_readcount==TIFF_VARIABLE);
  5012. assert(fip->field_passcount==1);
  5013. if (dp->tdir_count>0xFFFF)
  5014. err=TIFFReadDirEntryErrCount;
  5015. else
  5016. {
  5017. err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
  5018. if (err==TIFFReadDirEntryErrOk)
  5019. {
  5020. int m;
  5021. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  5022. if (data!=0)
  5023. _TIFFfree(data);
  5024. if (!m)
  5025. return(0);
  5026. }
  5027. }
  5028. }
  5029. break;
  5030. case TIFF_SETGET_C16_IFD8:
  5031. {
  5032. uint64* data;
  5033. assert(fip->field_readcount==TIFF_VARIABLE);
  5034. assert(fip->field_passcount==1);
  5035. if (dp->tdir_count>0xFFFF)
  5036. err=TIFFReadDirEntryErrCount;
  5037. else
  5038. {
  5039. err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
  5040. if (err==TIFFReadDirEntryErrOk)
  5041. {
  5042. int m;
  5043. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  5044. if (data!=0)
  5045. _TIFFfree(data);
  5046. if (!m)
  5047. return(0);
  5048. }
  5049. }
  5050. }
  5051. break;
  5052. case TIFF_SETGET_C32_ASCII:
  5053. {
  5054. uint8* data;
  5055. assert(fip->field_readcount==TIFF_VARIABLE2);
  5056. assert(fip->field_passcount==1);
  5057. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  5058. if (err==TIFFReadDirEntryErrOk)
  5059. {
  5060. int m;
  5061. if( dp->tdir_count > 0 && data[dp->tdir_count-1] != '\0' )
  5062. {
  5063. TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte. Forcing it to be null",fip->field_name);
  5064. data[dp->tdir_count-1] = '\0';
  5065. }
  5066. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5067. if (data!=0)
  5068. _TIFFfree(data);
  5069. if (!m)
  5070. return(0);
  5071. }
  5072. }
  5073. break;
  5074. case TIFF_SETGET_C32_UINT8:
  5075. {
  5076. uint8* data;
  5077. assert(fip->field_readcount==TIFF_VARIABLE2);
  5078. assert(fip->field_passcount==1);
  5079. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  5080. if (err==TIFFReadDirEntryErrOk)
  5081. {
  5082. int m;
  5083. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5084. if (data!=0)
  5085. _TIFFfree(data);
  5086. if (!m)
  5087. return(0);
  5088. }
  5089. }
  5090. break;
  5091. case TIFF_SETGET_C32_SINT8:
  5092. {
  5093. int8* data = NULL;
  5094. assert(fip->field_readcount==TIFF_VARIABLE2);
  5095. assert(fip->field_passcount==1);
  5096. err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
  5097. if (err==TIFFReadDirEntryErrOk)
  5098. {
  5099. int m;
  5100. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5101. if (data!=0)
  5102. _TIFFfree(data);
  5103. if (!m)
  5104. return(0);
  5105. }
  5106. }
  5107. break;
  5108. case TIFF_SETGET_C32_UINT16:
  5109. {
  5110. uint16* data;
  5111. assert(fip->field_readcount==TIFF_VARIABLE2);
  5112. assert(fip->field_passcount==1);
  5113. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  5114. if (err==TIFFReadDirEntryErrOk)
  5115. {
  5116. int m;
  5117. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5118. if (data!=0)
  5119. _TIFFfree(data);
  5120. if (!m)
  5121. return(0);
  5122. }
  5123. }
  5124. break;
  5125. case TIFF_SETGET_C32_SINT16:
  5126. {
  5127. int16* data = NULL;
  5128. assert(fip->field_readcount==TIFF_VARIABLE2);
  5129. assert(fip->field_passcount==1);
  5130. err=TIFFReadDirEntrySshortArray(tif,dp,&data);
  5131. if (err==TIFFReadDirEntryErrOk)
  5132. {
  5133. int m;
  5134. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5135. if (data!=0)
  5136. _TIFFfree(data);
  5137. if (!m)
  5138. return(0);
  5139. }
  5140. }
  5141. break;
  5142. case TIFF_SETGET_C32_UINT32:
  5143. {
  5144. uint32* data;
  5145. assert(fip->field_readcount==TIFF_VARIABLE2);
  5146. assert(fip->field_passcount==1);
  5147. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  5148. if (err==TIFFReadDirEntryErrOk)
  5149. {
  5150. int m;
  5151. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5152. if (data!=0)
  5153. _TIFFfree(data);
  5154. if (!m)
  5155. return(0);
  5156. }
  5157. }
  5158. break;
  5159. case TIFF_SETGET_C32_SINT32:
  5160. {
  5161. int32* data = NULL;
  5162. assert(fip->field_readcount==TIFF_VARIABLE2);
  5163. assert(fip->field_passcount==1);
  5164. err=TIFFReadDirEntrySlongArray(tif,dp,&data);
  5165. if (err==TIFFReadDirEntryErrOk)
  5166. {
  5167. int m;
  5168. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5169. if (data!=0)
  5170. _TIFFfree(data);
  5171. if (!m)
  5172. return(0);
  5173. }
  5174. }
  5175. break;
  5176. case TIFF_SETGET_C32_UINT64:
  5177. {
  5178. uint64* data;
  5179. assert(fip->field_readcount==TIFF_VARIABLE2);
  5180. assert(fip->field_passcount==1);
  5181. err=TIFFReadDirEntryLong8Array(tif,dp,&data);
  5182. if (err==TIFFReadDirEntryErrOk)
  5183. {
  5184. int m;
  5185. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5186. if (data!=0)
  5187. _TIFFfree(data);
  5188. if (!m)
  5189. return(0);
  5190. }
  5191. }
  5192. break;
  5193. case TIFF_SETGET_C32_SINT64:
  5194. {
  5195. int64* data = NULL;
  5196. assert(fip->field_readcount==TIFF_VARIABLE2);
  5197. assert(fip->field_passcount==1);
  5198. err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
  5199. if (err==TIFFReadDirEntryErrOk)
  5200. {
  5201. int m;
  5202. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5203. if (data!=0)
  5204. _TIFFfree(data);
  5205. if (!m)
  5206. return(0);
  5207. }
  5208. }
  5209. break;
  5210. case TIFF_SETGET_C32_FLOAT:
  5211. {
  5212. float* data;
  5213. assert(fip->field_readcount==TIFF_VARIABLE2);
  5214. assert(fip->field_passcount==1);
  5215. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  5216. if (err==TIFFReadDirEntryErrOk)
  5217. {
  5218. int m;
  5219. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5220. if (data!=0)
  5221. _TIFFfree(data);
  5222. if (!m)
  5223. return(0);
  5224. }
  5225. }
  5226. break;
  5227. case TIFF_SETGET_C32_DOUBLE:
  5228. {
  5229. double* data;
  5230. assert(fip->field_readcount==TIFF_VARIABLE2);
  5231. assert(fip->field_passcount==1);
  5232. err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
  5233. if (err==TIFFReadDirEntryErrOk)
  5234. {
  5235. int m;
  5236. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5237. if (data!=0)
  5238. _TIFFfree(data);
  5239. if (!m)
  5240. return(0);
  5241. }
  5242. }
  5243. break;
  5244. case TIFF_SETGET_C32_IFD8:
  5245. {
  5246. uint64* data;
  5247. assert(fip->field_readcount==TIFF_VARIABLE2);
  5248. assert(fip->field_passcount==1);
  5249. err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
  5250. if (err==TIFFReadDirEntryErrOk)
  5251. {
  5252. int m;
  5253. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5254. if (data!=0)
  5255. _TIFFfree(data);
  5256. if (!m)
  5257. return(0);
  5258. }
  5259. }
  5260. break;
  5261. default:
  5262. assert(0); /* we should never get here */
  5263. break;
  5264. }
  5265. if (err!=TIFFReadDirEntryErrOk)
  5266. {
  5267. TIFFReadDirEntryOutputErr(tif,err,module,fip->field_name,recover);
  5268. return(0);
  5269. }
  5270. return(1);
  5271. }
  5272. /*
  5273. * Fetch a set of offsets or lengths.
  5274. * While this routine says "strips", in fact it's also used for tiles.
  5275. */
  5276. static int
  5277. TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
  5278. {
  5279. static const char module[] = "TIFFFetchStripThing";
  5280. enum TIFFReadDirEntryErr err;
  5281. uint64* data;
  5282. err=TIFFReadDirEntryLong8Array(tif,dir,&data);
  5283. if (err!=TIFFReadDirEntryErrOk)
  5284. {
  5285. const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
  5286. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  5287. return(0);
  5288. }
  5289. if (dir->tdir_count!=(uint64)nstrips)
  5290. {
  5291. uint64* resizeddata;
  5292. resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
  5293. if (resizeddata==0) {
  5294. _TIFFfree(data);
  5295. return(0);
  5296. }
  5297. if (dir->tdir_count<(uint64)nstrips)
  5298. {
  5299. _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
  5300. _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
  5301. }
  5302. else
  5303. _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
  5304. _TIFFfree(data);
  5305. data=resizeddata;
  5306. }
  5307. *lpp=data;
  5308. return(1);
  5309. }
  5310. /*
  5311. * Fetch and set the SubjectDistance EXIF tag.
  5312. */
  5313. static int
  5314. TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
  5315. {
  5316. static const char module[] = "TIFFFetchSubjectDistance";
  5317. enum TIFFReadDirEntryErr err;
  5318. UInt64Aligned_t m;
  5319. m.l=0;
  5320. assert(sizeof(double)==8);
  5321. assert(sizeof(uint64)==8);
  5322. assert(sizeof(uint32)==4);
  5323. if (dir->tdir_count!=1)
  5324. err=TIFFReadDirEntryErrCount;
  5325. else if (dir->tdir_type!=TIFF_RATIONAL)
  5326. err=TIFFReadDirEntryErrType;
  5327. else
  5328. {
  5329. if (!(tif->tif_flags&TIFF_BIGTIFF))
  5330. {
  5331. uint32 offset;
  5332. offset=*(uint32*)(&dir->tdir_offset);
  5333. if (tif->tif_flags&TIFF_SWAB)
  5334. TIFFSwabLong(&offset);
  5335. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  5336. }
  5337. else
  5338. {
  5339. m.l=dir->tdir_offset.toff_long8;
  5340. err=TIFFReadDirEntryErrOk;
  5341. }
  5342. }
  5343. if (err==TIFFReadDirEntryErrOk)
  5344. {
  5345. double n;
  5346. if (tif->tif_flags&TIFF_SWAB)
  5347. TIFFSwabArrayOfLong(m.i,2);
  5348. if (m.i[0]==0)
  5349. n=0.0;
  5350. else if (m.i[0]==0xFFFFFFFF)
  5351. /*
  5352. * XXX: Numerator 0xFFFFFFFF means that we have infinite
  5353. * distance. Indicate that with a negative floating point
  5354. * SubjectDistance value.
  5355. */
  5356. n=-1.0;
  5357. else
  5358. n=(double)m.i[0]/(double)m.i[1];
  5359. return(TIFFSetField(tif,dir->tdir_tag,n));
  5360. }
  5361. else
  5362. {
  5363. TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
  5364. return(0);
  5365. }
  5366. }
  5367. /*
  5368. * Replace a single strip (tile) of uncompressed data by multiple strips
  5369. * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
  5370. * dealing with large images or for dealing with machines with a limited
  5371. * amount memory.
  5372. */
  5373. static void
  5374. ChopUpSingleUncompressedStrip(TIFF* tif)
  5375. {
  5376. register TIFFDirectory *td = &tif->tif_dir;
  5377. uint64 bytecount;
  5378. uint64 offset;
  5379. uint32 rowblock;
  5380. uint64 rowblockbytes;
  5381. uint64 stripbytes;
  5382. uint32 strip;
  5383. uint32 nstrips;
  5384. uint32 rowsperstrip;
  5385. uint64* newcounts;
  5386. uint64* newoffsets;
  5387. bytecount = td->td_stripbytecount[0];
  5388. offset = td->td_stripoffset[0];
  5389. assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
  5390. if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
  5391. (!isUpSampled(tif)))
  5392. rowblock = td->td_ycbcrsubsampling[1];
  5393. else
  5394. rowblock = 1;
  5395. rowblockbytes = TIFFVTileSize64(tif, rowblock);
  5396. /*
  5397. * Make the rows hold at least one scanline, but fill specified amount
  5398. * of data if possible.
  5399. */
  5400. if (rowblockbytes > STRIP_SIZE_DEFAULT) {
  5401. stripbytes = rowblockbytes;
  5402. rowsperstrip = rowblock;
  5403. } else if (rowblockbytes > 0 ) {
  5404. uint32 rowblocksperstrip;
  5405. rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
  5406. rowsperstrip = rowblocksperstrip * rowblock;
  5407. stripbytes = rowblocksperstrip * rowblockbytes;
  5408. }
  5409. else
  5410. return;
  5411. /*
  5412. * never increase the number of rows per strip
  5413. */
  5414. if (rowsperstrip >= td->td_rowsperstrip)
  5415. return;
  5416. nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
  5417. if( nstrips == 0 )
  5418. return;
  5419. newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
  5420. "for chopped \"StripByteCounts\" array");
  5421. newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint64),
  5422. "for chopped \"StripOffsets\" array");
  5423. if (newcounts == NULL || newoffsets == NULL) {
  5424. /*
  5425. * Unable to allocate new strip information, give up and use
  5426. * the original one strip information.
  5427. */
  5428. if (newcounts != NULL)
  5429. _TIFFfree(newcounts);
  5430. if (newoffsets != NULL)
  5431. _TIFFfree(newoffsets);
  5432. return;
  5433. }
  5434. /*
  5435. * Fill the strip information arrays with new bytecounts and offsets
  5436. * that reflect the broken-up format.
  5437. */
  5438. for (strip = 0; strip < nstrips; strip++) {
  5439. if (stripbytes > bytecount)
  5440. stripbytes = bytecount;
  5441. newcounts[strip] = stripbytes;
  5442. newoffsets[strip] = stripbytes ? offset : 0;
  5443. offset += stripbytes;
  5444. bytecount -= stripbytes;
  5445. }
  5446. /*
  5447. * Replace old single strip info with multi-strip info.
  5448. */
  5449. td->td_stripsperimage = td->td_nstrips = nstrips;
  5450. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  5451. _TIFFfree(td->td_stripbytecount);
  5452. _TIFFfree(td->td_stripoffset);
  5453. td->td_stripbytecount = newcounts;
  5454. td->td_stripoffset = newoffsets;
  5455. td->td_stripbytecountsorted = 1;
  5456. }
  5457. int _TIFFFillStriles( TIFF *tif )
  5458. {
  5459. return _TIFFFillStrilesInternal( tif, 1 );
  5460. }
  5461. static int _TIFFFillStrilesInternal( TIFF *tif, int loadStripByteCount )
  5462. {
  5463. #if defined(DEFER_STRILE_LOAD)
  5464. register TIFFDirectory *td = &tif->tif_dir;
  5465. int return_value = 1;
  5466. if( td->td_stripoffset != NULL )
  5467. return 1;
  5468. if( td->td_stripoffset_entry.tdir_count == 0 )
  5469. return 0;
  5470. if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
  5471. td->td_nstrips,&td->td_stripoffset))
  5472. {
  5473. return_value = 0;
  5474. }
  5475. if (loadStripByteCount &&
  5476. !TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
  5477. td->td_nstrips,&td->td_stripbytecount))
  5478. {
  5479. return_value = 0;
  5480. }
  5481. _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
  5482. _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
  5483. if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
  5484. uint32 strip;
  5485. tif->tif_dir.td_stripbytecountsorted = 1;
  5486. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
  5487. if (tif->tif_dir.td_stripoffset[strip - 1] >
  5488. tif->tif_dir.td_stripoffset[strip]) {
  5489. tif->tif_dir.td_stripbytecountsorted = 0;
  5490. break;
  5491. }
  5492. }
  5493. }
  5494. return return_value;
  5495. #else /* !defined(DEFER_STRILE_LOAD) */
  5496. (void) tif;
  5497. (void) loadStripByteCount;
  5498. return 1;
  5499. #endif
  5500. }
  5501. /* vim: set ts=8 sts=8 sw=8 noet: */
  5502. /*
  5503. * Local Variables:
  5504. * mode: c
  5505. * c-basic-offset: 8
  5506. * fill-column: 78
  5507. * End:
  5508. */