tif_dirread.c 169 KB

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