tif_dirread.c 275 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447
  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 "tiffconf.h"
  35. #include "tiffiop.h"
  36. #include <float.h>
  37. #include <limits.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #define FAILED_FII ((uint32_t)-1)
  41. #ifdef HAVE_IEEEFP
  42. #define TIFFCvtIEEEFloatToNative(tif, n, fp)
  43. #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
  44. #else
  45. /* If your machine does not support IEEE floating point then you will need to
  46. * add support to tif_machdep.c to convert between the native format and
  47. * IEEE format. */
  48. extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
  49. extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
  50. #endif
  51. enum TIFFReadDirEntryErr
  52. {
  53. TIFFReadDirEntryErrOk = 0,
  54. TIFFReadDirEntryErrCount = 1,
  55. TIFFReadDirEntryErrType = 2,
  56. TIFFReadDirEntryErrIo = 3,
  57. TIFFReadDirEntryErrRange = 4,
  58. TIFFReadDirEntryErrPsdif = 5,
  59. TIFFReadDirEntryErrSizesan = 6,
  60. TIFFReadDirEntryErrAlloc = 7,
  61. };
  62. static enum TIFFReadDirEntryErr
  63. TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
  64. static enum TIFFReadDirEntryErr
  65. TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
  66. static enum TIFFReadDirEntryErr
  67. TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
  68. static enum TIFFReadDirEntryErr
  69. TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
  70. static enum TIFFReadDirEntryErr
  71. TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
  72. static enum TIFFReadDirEntryErr
  73. TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
  74. static enum TIFFReadDirEntryErr
  75. TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
  76. static enum TIFFReadDirEntryErr
  77. TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
  78. static enum TIFFReadDirEntryErr
  79. TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
  80. static enum TIFFReadDirEntryErr
  81. TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
  82. static enum TIFFReadDirEntryErr
  83. TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
  84. static enum TIFFReadDirEntryErr
  85. TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
  86. uint32_t desttypesize, void **value);
  87. static enum TIFFReadDirEntryErr
  88. TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
  89. static enum TIFFReadDirEntryErr
  90. TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
  91. static enum TIFFReadDirEntryErr
  92. TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
  93. static enum TIFFReadDirEntryErr
  94. TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
  95. static enum TIFFReadDirEntryErr
  96. TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
  97. static enum TIFFReadDirEntryErr
  98. TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
  99. static enum TIFFReadDirEntryErr
  100. TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
  101. static enum TIFFReadDirEntryErr
  102. TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
  103. static enum TIFFReadDirEntryErr
  104. TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
  105. static enum TIFFReadDirEntryErr
  106. TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
  107. static enum TIFFReadDirEntryErr
  108. TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
  109. static enum TIFFReadDirEntryErr
  110. TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
  111. uint16_t *value);
  112. static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
  113. uint8_t *value);
  114. static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
  115. int8_t *value);
  116. static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
  117. uint16_t *value);
  118. static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
  119. int16_t *value);
  120. static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
  121. uint32_t *value);
  122. static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
  123. int32_t *value);
  124. static enum TIFFReadDirEntryErr
  125. TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry,
  126. uint64_t *value);
  127. static enum TIFFReadDirEntryErr
  128. TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry,
  129. int64_t *value);
  130. static enum TIFFReadDirEntryErr
  131. TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
  132. double *value);
  133. static enum TIFFReadDirEntryErr
  134. TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
  135. double *value);
  136. static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
  137. float *value);
  138. static enum TIFFReadDirEntryErr
  139. TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
  140. #if 0
  141. static enum TIFFReadDirEntryErr
  142. TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
  143. TIFFRational_t *value);
  144. #endif
  145. static enum TIFFReadDirEntryErr
  146. TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
  147. static enum TIFFReadDirEntryErr
  148. TIFFReadDirEntryCheckRangeByteShort(uint16_t value);
  149. static enum TIFFReadDirEntryErr
  150. TIFFReadDirEntryCheckRangeByteSshort(int16_t value);
  151. static enum TIFFReadDirEntryErr
  152. TIFFReadDirEntryCheckRangeByteLong(uint32_t value);
  153. static enum TIFFReadDirEntryErr
  154. TIFFReadDirEntryCheckRangeByteSlong(int32_t value);
  155. static enum TIFFReadDirEntryErr
  156. TIFFReadDirEntryCheckRangeByteLong8(uint64_t value);
  157. static enum TIFFReadDirEntryErr
  158. TIFFReadDirEntryCheckRangeByteSlong8(int64_t value);
  159. static enum TIFFReadDirEntryErr
  160. TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value);
  161. static enum TIFFReadDirEntryErr
  162. TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value);
  163. static enum TIFFReadDirEntryErr
  164. TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value);
  165. static enum TIFFReadDirEntryErr
  166. TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value);
  167. static enum TIFFReadDirEntryErr
  168. TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value);
  169. static enum TIFFReadDirEntryErr
  170. TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value);
  171. static enum TIFFReadDirEntryErr
  172. TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value);
  173. static enum TIFFReadDirEntryErr
  174. TIFFReadDirEntryCheckRangeShortSbyte(int8_t value);
  175. static enum TIFFReadDirEntryErr
  176. TIFFReadDirEntryCheckRangeShortSshort(int16_t value);
  177. static enum TIFFReadDirEntryErr
  178. TIFFReadDirEntryCheckRangeShortLong(uint32_t value);
  179. static enum TIFFReadDirEntryErr
  180. TIFFReadDirEntryCheckRangeShortSlong(int32_t value);
  181. static enum TIFFReadDirEntryErr
  182. TIFFReadDirEntryCheckRangeShortLong8(uint64_t value);
  183. static enum TIFFReadDirEntryErr
  184. TIFFReadDirEntryCheckRangeShortSlong8(int64_t value);
  185. static enum TIFFReadDirEntryErr
  186. TIFFReadDirEntryCheckRangeSshortShort(uint16_t value);
  187. static enum TIFFReadDirEntryErr
  188. TIFFReadDirEntryCheckRangeSshortLong(uint32_t value);
  189. static enum TIFFReadDirEntryErr
  190. TIFFReadDirEntryCheckRangeSshortSlong(int32_t value);
  191. static enum TIFFReadDirEntryErr
  192. TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value);
  193. static enum TIFFReadDirEntryErr
  194. TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value);
  195. static enum TIFFReadDirEntryErr
  196. TIFFReadDirEntryCheckRangeLongSbyte(int8_t value);
  197. static enum TIFFReadDirEntryErr
  198. TIFFReadDirEntryCheckRangeLongSshort(int16_t value);
  199. static enum TIFFReadDirEntryErr
  200. TIFFReadDirEntryCheckRangeLongSlong(int32_t value);
  201. static enum TIFFReadDirEntryErr
  202. TIFFReadDirEntryCheckRangeLongLong8(uint64_t value);
  203. static enum TIFFReadDirEntryErr
  204. TIFFReadDirEntryCheckRangeLongSlong8(int64_t value);
  205. static enum TIFFReadDirEntryErr
  206. TIFFReadDirEntryCheckRangeSlongLong(uint32_t value);
  207. static enum TIFFReadDirEntryErr
  208. TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value);
  209. static enum TIFFReadDirEntryErr
  210. TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value);
  211. static enum TIFFReadDirEntryErr
  212. TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value);
  213. static enum TIFFReadDirEntryErr
  214. TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value);
  215. static enum TIFFReadDirEntryErr
  216. TIFFReadDirEntryCheckRangeLong8Slong(int32_t value);
  217. static enum TIFFReadDirEntryErr
  218. TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value);
  219. static enum TIFFReadDirEntryErr
  220. TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value);
  221. static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
  222. tmsize_t size, void *dest);
  223. static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
  224. const char *module, const char *tagname,
  225. int recover);
  226. static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
  227. uint16_t dircount);
  228. static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
  229. uint16_t dircount,
  230. uint16_t tagid);
  231. static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
  232. uint32_t *fii);
  233. static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
  234. uint16_t dircount);
  235. static void MissingRequired(TIFF *, const char *);
  236. static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
  237. static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
  238. TIFFDirEntry **pdir, uint64_t *nextdiroff);
  239. static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
  240. static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
  241. uint64_t **lpp);
  242. static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *);
  243. static void ChopUpSingleUncompressedStrip(TIFF *);
  244. static void TryChopUpUncompressedBigTiff(TIFF *);
  245. static uint64_t TIFFReadUInt64(const uint8_t *value);
  246. static int _TIFFGetMaxColorChannels(uint16_t photometric);
  247. static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
  248. typedef union _UInt64Aligned_t
  249. {
  250. double d;
  251. uint64_t l;
  252. uint32_t i[2];
  253. uint16_t s[4];
  254. uint8_t c[8];
  255. } UInt64Aligned_t;
  256. /*
  257. Unaligned safe copy of a uint64_t value from an octet array.
  258. */
  259. static uint64_t TIFFReadUInt64(const uint8_t *value)
  260. {
  261. UInt64Aligned_t result;
  262. result.c[0] = value[0];
  263. result.c[1] = value[1];
  264. result.c[2] = value[2];
  265. result.c[3] = value[3];
  266. result.c[4] = value[4];
  267. result.c[5] = value[5];
  268. result.c[6] = value[6];
  269. result.c[7] = value[7];
  270. return result.l;
  271. }
  272. static enum TIFFReadDirEntryErr
  273. TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
  274. {
  275. enum TIFFReadDirEntryErr err;
  276. if (direntry->tdir_count != 1)
  277. return (TIFFReadDirEntryErrCount);
  278. switch (direntry->tdir_type)
  279. {
  280. case TIFF_BYTE:
  281. case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
  282. field_readcount==1 */
  283. TIFFReadDirEntryCheckedByte(tif, direntry, value);
  284. return (TIFFReadDirEntryErrOk);
  285. case TIFF_SBYTE:
  286. {
  287. int8_t m;
  288. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  289. err = TIFFReadDirEntryCheckRangeByteSbyte(m);
  290. if (err != TIFFReadDirEntryErrOk)
  291. return (err);
  292. *value = (uint8_t)m;
  293. return (TIFFReadDirEntryErrOk);
  294. }
  295. case TIFF_SHORT:
  296. {
  297. uint16_t m;
  298. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  299. err = TIFFReadDirEntryCheckRangeByteShort(m);
  300. if (err != TIFFReadDirEntryErrOk)
  301. return (err);
  302. *value = (uint8_t)m;
  303. return (TIFFReadDirEntryErrOk);
  304. }
  305. case TIFF_SSHORT:
  306. {
  307. int16_t m;
  308. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  309. err = TIFFReadDirEntryCheckRangeByteSshort(m);
  310. if (err != TIFFReadDirEntryErrOk)
  311. return (err);
  312. *value = (uint8_t)m;
  313. return (TIFFReadDirEntryErrOk);
  314. }
  315. case TIFF_LONG:
  316. {
  317. uint32_t m;
  318. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  319. err = TIFFReadDirEntryCheckRangeByteLong(m);
  320. if (err != TIFFReadDirEntryErrOk)
  321. return (err);
  322. *value = (uint8_t)m;
  323. return (TIFFReadDirEntryErrOk);
  324. }
  325. case TIFF_SLONG:
  326. {
  327. int32_t m;
  328. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  329. err = TIFFReadDirEntryCheckRangeByteSlong(m);
  330. if (err != TIFFReadDirEntryErrOk)
  331. return (err);
  332. *value = (uint8_t)m;
  333. return (TIFFReadDirEntryErrOk);
  334. }
  335. case TIFF_LONG8:
  336. {
  337. uint64_t m;
  338. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  339. if (err != TIFFReadDirEntryErrOk)
  340. return (err);
  341. err = TIFFReadDirEntryCheckRangeByteLong8(m);
  342. if (err != TIFFReadDirEntryErrOk)
  343. return (err);
  344. *value = (uint8_t)m;
  345. return (TIFFReadDirEntryErrOk);
  346. }
  347. case TIFF_SLONG8:
  348. {
  349. int64_t m;
  350. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  351. if (err != TIFFReadDirEntryErrOk)
  352. return (err);
  353. err = TIFFReadDirEntryCheckRangeByteSlong8(m);
  354. if (err != TIFFReadDirEntryErrOk)
  355. return (err);
  356. *value = (uint8_t)m;
  357. return (TIFFReadDirEntryErrOk);
  358. }
  359. default:
  360. return (TIFFReadDirEntryErrType);
  361. }
  362. }
  363. static enum TIFFReadDirEntryErr
  364. TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
  365. {
  366. enum TIFFReadDirEntryErr err;
  367. if (direntry->tdir_count != 1)
  368. return (TIFFReadDirEntryErrCount);
  369. switch (direntry->tdir_type)
  370. {
  371. case TIFF_BYTE:
  372. case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
  373. field_readcount==1 */
  374. {
  375. uint8_t m;
  376. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  377. err = TIFFReadDirEntryCheckRangeSbyteByte(m);
  378. if (err != TIFFReadDirEntryErrOk)
  379. return (err);
  380. *value = (int8_t)m;
  381. return (TIFFReadDirEntryErrOk);
  382. }
  383. case TIFF_SBYTE:
  384. {
  385. TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
  386. return (TIFFReadDirEntryErrOk);
  387. }
  388. case TIFF_SHORT:
  389. {
  390. uint16_t m;
  391. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  392. err = TIFFReadDirEntryCheckRangeSbyteShort(m);
  393. if (err != TIFFReadDirEntryErrOk)
  394. return (err);
  395. *value = (int8_t)m;
  396. return (TIFFReadDirEntryErrOk);
  397. }
  398. case TIFF_SSHORT:
  399. {
  400. int16_t m;
  401. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  402. err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
  403. if (err != TIFFReadDirEntryErrOk)
  404. return (err);
  405. *value = (int8_t)m;
  406. return (TIFFReadDirEntryErrOk);
  407. }
  408. case TIFF_LONG:
  409. {
  410. uint32_t m;
  411. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  412. err = TIFFReadDirEntryCheckRangeSbyteLong(m);
  413. if (err != TIFFReadDirEntryErrOk)
  414. return (err);
  415. *value = (int8_t)m;
  416. return (TIFFReadDirEntryErrOk);
  417. }
  418. case TIFF_SLONG:
  419. {
  420. int32_t m;
  421. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  422. err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
  423. if (err != TIFFReadDirEntryErrOk)
  424. return (err);
  425. *value = (int8_t)m;
  426. return (TIFFReadDirEntryErrOk);
  427. }
  428. case TIFF_LONG8:
  429. {
  430. uint64_t m;
  431. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  432. if (err != TIFFReadDirEntryErrOk)
  433. return (err);
  434. err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
  435. if (err != TIFFReadDirEntryErrOk)
  436. return (err);
  437. *value = (int8_t)m;
  438. return (TIFFReadDirEntryErrOk);
  439. }
  440. case TIFF_SLONG8:
  441. {
  442. int64_t m;
  443. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  444. if (err != TIFFReadDirEntryErrOk)
  445. return (err);
  446. err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
  447. if (err != TIFFReadDirEntryErrOk)
  448. return (err);
  449. *value = (int8_t)m;
  450. return (TIFFReadDirEntryErrOk);
  451. }
  452. default:
  453. return (TIFFReadDirEntryErrType);
  454. }
  455. } /*-- TIFFReadDirEntrySbyte() --*/
  456. static enum TIFFReadDirEntryErr
  457. TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
  458. {
  459. enum TIFFReadDirEntryErr err;
  460. if (direntry->tdir_count != 1)
  461. return (TIFFReadDirEntryErrCount);
  462. switch (direntry->tdir_type)
  463. {
  464. case TIFF_BYTE:
  465. {
  466. uint8_t m;
  467. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  468. *value = (uint16_t)m;
  469. return (TIFFReadDirEntryErrOk);
  470. }
  471. case TIFF_SBYTE:
  472. {
  473. int8_t m;
  474. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  475. err = TIFFReadDirEntryCheckRangeShortSbyte(m);
  476. if (err != TIFFReadDirEntryErrOk)
  477. return (err);
  478. *value = (uint16_t)m;
  479. return (TIFFReadDirEntryErrOk);
  480. }
  481. case TIFF_SHORT:
  482. TIFFReadDirEntryCheckedShort(tif, direntry, value);
  483. return (TIFFReadDirEntryErrOk);
  484. case TIFF_SSHORT:
  485. {
  486. int16_t m;
  487. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  488. err = TIFFReadDirEntryCheckRangeShortSshort(m);
  489. if (err != TIFFReadDirEntryErrOk)
  490. return (err);
  491. *value = (uint16_t)m;
  492. return (TIFFReadDirEntryErrOk);
  493. }
  494. case TIFF_LONG:
  495. {
  496. uint32_t m;
  497. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  498. err = TIFFReadDirEntryCheckRangeShortLong(m);
  499. if (err != TIFFReadDirEntryErrOk)
  500. return (err);
  501. *value = (uint16_t)m;
  502. return (TIFFReadDirEntryErrOk);
  503. }
  504. case TIFF_SLONG:
  505. {
  506. int32_t m;
  507. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  508. err = TIFFReadDirEntryCheckRangeShortSlong(m);
  509. if (err != TIFFReadDirEntryErrOk)
  510. return (err);
  511. *value = (uint16_t)m;
  512. return (TIFFReadDirEntryErrOk);
  513. }
  514. case TIFF_LONG8:
  515. {
  516. uint64_t m;
  517. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  518. if (err != TIFFReadDirEntryErrOk)
  519. return (err);
  520. err = TIFFReadDirEntryCheckRangeShortLong8(m);
  521. if (err != TIFFReadDirEntryErrOk)
  522. return (err);
  523. *value = (uint16_t)m;
  524. return (TIFFReadDirEntryErrOk);
  525. }
  526. case TIFF_SLONG8:
  527. {
  528. int64_t m;
  529. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  530. if (err != TIFFReadDirEntryErrOk)
  531. return (err);
  532. err = TIFFReadDirEntryCheckRangeShortSlong8(m);
  533. if (err != TIFFReadDirEntryErrOk)
  534. return (err);
  535. *value = (uint16_t)m;
  536. return (TIFFReadDirEntryErrOk);
  537. }
  538. default:
  539. return (TIFFReadDirEntryErrType);
  540. }
  541. } /*-- TIFFReadDirEntryShort() --*/
  542. static enum TIFFReadDirEntryErr
  543. TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
  544. {
  545. enum TIFFReadDirEntryErr err;
  546. if (direntry->tdir_count != 1)
  547. return (TIFFReadDirEntryErrCount);
  548. switch (direntry->tdir_type)
  549. {
  550. case TIFF_BYTE:
  551. {
  552. uint8_t m;
  553. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  554. *value = (int16_t)m;
  555. return (TIFFReadDirEntryErrOk);
  556. }
  557. case TIFF_SBYTE:
  558. {
  559. int8_t m;
  560. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  561. *value = (int16_t)m;
  562. return (TIFFReadDirEntryErrOk);
  563. }
  564. case TIFF_SHORT:
  565. {
  566. uint16_t m;
  567. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  568. err = TIFFReadDirEntryCheckRangeSshortShort(m);
  569. if (err != TIFFReadDirEntryErrOk)
  570. return (err);
  571. *value = (uint16_t)m;
  572. return (TIFFReadDirEntryErrOk);
  573. }
  574. case TIFF_SSHORT:
  575. TIFFReadDirEntryCheckedSshort(tif, direntry, value);
  576. return (TIFFReadDirEntryErrOk);
  577. case TIFF_LONG:
  578. {
  579. uint32_t m;
  580. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  581. err = TIFFReadDirEntryCheckRangeSshortLong(m);
  582. if (err != TIFFReadDirEntryErrOk)
  583. return (err);
  584. *value = (int16_t)m;
  585. return (TIFFReadDirEntryErrOk);
  586. }
  587. case TIFF_SLONG:
  588. {
  589. int32_t m;
  590. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  591. err = TIFFReadDirEntryCheckRangeSshortSlong(m);
  592. if (err != TIFFReadDirEntryErrOk)
  593. return (err);
  594. *value = (int16_t)m;
  595. return (TIFFReadDirEntryErrOk);
  596. }
  597. case TIFF_LONG8:
  598. {
  599. uint64_t m;
  600. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  601. if (err != TIFFReadDirEntryErrOk)
  602. return (err);
  603. err = TIFFReadDirEntryCheckRangeSshortLong8(m);
  604. if (err != TIFFReadDirEntryErrOk)
  605. return (err);
  606. *value = (int16_t)m;
  607. return (TIFFReadDirEntryErrOk);
  608. }
  609. case TIFF_SLONG8:
  610. {
  611. int64_t m;
  612. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  613. if (err != TIFFReadDirEntryErrOk)
  614. return (err);
  615. err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
  616. if (err != TIFFReadDirEntryErrOk)
  617. return (err);
  618. *value = (int16_t)m;
  619. return (TIFFReadDirEntryErrOk);
  620. }
  621. default:
  622. return (TIFFReadDirEntryErrType);
  623. }
  624. } /*-- TIFFReadDirEntrySshort() --*/
  625. static enum TIFFReadDirEntryErr
  626. TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
  627. {
  628. enum TIFFReadDirEntryErr err;
  629. if (direntry->tdir_count != 1)
  630. return (TIFFReadDirEntryErrCount);
  631. switch (direntry->tdir_type)
  632. {
  633. case TIFF_BYTE:
  634. {
  635. uint8_t m;
  636. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  637. *value = (uint32_t)m;
  638. return (TIFFReadDirEntryErrOk);
  639. }
  640. case TIFF_SBYTE:
  641. {
  642. int8_t m;
  643. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  644. err = TIFFReadDirEntryCheckRangeLongSbyte(m);
  645. if (err != TIFFReadDirEntryErrOk)
  646. return (err);
  647. *value = (uint32_t)m;
  648. return (TIFFReadDirEntryErrOk);
  649. }
  650. case TIFF_SHORT:
  651. {
  652. uint16_t m;
  653. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  654. *value = (uint32_t)m;
  655. return (TIFFReadDirEntryErrOk);
  656. }
  657. case TIFF_SSHORT:
  658. {
  659. int16_t m;
  660. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  661. err = TIFFReadDirEntryCheckRangeLongSshort(m);
  662. if (err != TIFFReadDirEntryErrOk)
  663. return (err);
  664. *value = (uint32_t)m;
  665. return (TIFFReadDirEntryErrOk);
  666. }
  667. case TIFF_LONG:
  668. TIFFReadDirEntryCheckedLong(tif, direntry, value);
  669. return (TIFFReadDirEntryErrOk);
  670. case TIFF_SLONG:
  671. {
  672. int32_t m;
  673. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  674. err = TIFFReadDirEntryCheckRangeLongSlong(m);
  675. if (err != TIFFReadDirEntryErrOk)
  676. return (err);
  677. *value = (uint32_t)m;
  678. return (TIFFReadDirEntryErrOk);
  679. }
  680. case TIFF_LONG8:
  681. {
  682. uint64_t m;
  683. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  684. if (err != TIFFReadDirEntryErrOk)
  685. return (err);
  686. err = TIFFReadDirEntryCheckRangeLongLong8(m);
  687. if (err != TIFFReadDirEntryErrOk)
  688. return (err);
  689. *value = (uint32_t)m;
  690. return (TIFFReadDirEntryErrOk);
  691. }
  692. case TIFF_SLONG8:
  693. {
  694. int64_t m;
  695. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  696. if (err != TIFFReadDirEntryErrOk)
  697. return (err);
  698. err = TIFFReadDirEntryCheckRangeLongSlong8(m);
  699. if (err != TIFFReadDirEntryErrOk)
  700. return (err);
  701. *value = (uint32_t)m;
  702. return (TIFFReadDirEntryErrOk);
  703. }
  704. default:
  705. return (TIFFReadDirEntryErrType);
  706. }
  707. } /*-- TIFFReadDirEntryLong() --*/
  708. static enum TIFFReadDirEntryErr
  709. TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
  710. {
  711. enum TIFFReadDirEntryErr err;
  712. if (direntry->tdir_count != 1)
  713. return (TIFFReadDirEntryErrCount);
  714. switch (direntry->tdir_type)
  715. {
  716. case TIFF_BYTE:
  717. {
  718. uint8_t m;
  719. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  720. *value = (int32_t)m;
  721. return (TIFFReadDirEntryErrOk);
  722. }
  723. case TIFF_SBYTE:
  724. {
  725. int8_t m;
  726. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  727. *value = (int32_t)m;
  728. return (TIFFReadDirEntryErrOk);
  729. }
  730. case TIFF_SHORT:
  731. {
  732. uint16_t m;
  733. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  734. *value = (int32_t)m;
  735. return (TIFFReadDirEntryErrOk);
  736. }
  737. case TIFF_SSHORT:
  738. {
  739. int16_t m;
  740. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  741. *value = (int32_t)m;
  742. return (TIFFReadDirEntryErrOk);
  743. }
  744. case TIFF_LONG:
  745. {
  746. uint32_t m;
  747. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  748. err = TIFFReadDirEntryCheckRangeSlongLong(m);
  749. if (err != TIFFReadDirEntryErrOk)
  750. return (err);
  751. *value = (int32_t)m;
  752. return (TIFFReadDirEntryErrOk);
  753. }
  754. case TIFF_SLONG:
  755. TIFFReadDirEntryCheckedSlong(tif, direntry, value);
  756. return (TIFFReadDirEntryErrOk);
  757. case TIFF_LONG8:
  758. {
  759. uint64_t m;
  760. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  761. if (err != TIFFReadDirEntryErrOk)
  762. return (err);
  763. err = TIFFReadDirEntryCheckRangeSlongLong8(m);
  764. if (err != TIFFReadDirEntryErrOk)
  765. return (err);
  766. *value = (int32_t)m;
  767. return (TIFFReadDirEntryErrOk);
  768. }
  769. case TIFF_SLONG8:
  770. {
  771. int64_t m;
  772. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  773. if (err != TIFFReadDirEntryErrOk)
  774. return (err);
  775. err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
  776. if (err != TIFFReadDirEntryErrOk)
  777. return (err);
  778. *value = (int32_t)m;
  779. return (TIFFReadDirEntryErrOk);
  780. }
  781. default:
  782. return (TIFFReadDirEntryErrType);
  783. }
  784. } /*-- TIFFReadDirEntrySlong() --*/
  785. static enum TIFFReadDirEntryErr
  786. TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
  787. {
  788. enum TIFFReadDirEntryErr err;
  789. if (direntry->tdir_count != 1)
  790. return (TIFFReadDirEntryErrCount);
  791. switch (direntry->tdir_type)
  792. {
  793. case TIFF_BYTE:
  794. {
  795. uint8_t m;
  796. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  797. *value = (uint64_t)m;
  798. return (TIFFReadDirEntryErrOk);
  799. }
  800. case TIFF_SBYTE:
  801. {
  802. int8_t m;
  803. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  804. err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
  805. if (err != TIFFReadDirEntryErrOk)
  806. return (err);
  807. *value = (uint64_t)m;
  808. return (TIFFReadDirEntryErrOk);
  809. }
  810. case TIFF_SHORT:
  811. {
  812. uint16_t m;
  813. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  814. *value = (uint64_t)m;
  815. return (TIFFReadDirEntryErrOk);
  816. }
  817. case TIFF_SSHORT:
  818. {
  819. int16_t m;
  820. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  821. err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
  822. if (err != TIFFReadDirEntryErrOk)
  823. return (err);
  824. *value = (uint64_t)m;
  825. return (TIFFReadDirEntryErrOk);
  826. }
  827. case TIFF_LONG:
  828. {
  829. uint32_t m;
  830. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  831. *value = (uint64_t)m;
  832. return (TIFFReadDirEntryErrOk);
  833. }
  834. case TIFF_SLONG:
  835. {
  836. int32_t m;
  837. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  838. err = TIFFReadDirEntryCheckRangeLong8Slong(m);
  839. if (err != TIFFReadDirEntryErrOk)
  840. return (err);
  841. *value = (uint64_t)m;
  842. return (TIFFReadDirEntryErrOk);
  843. }
  844. case TIFF_LONG8:
  845. err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
  846. return (err);
  847. case TIFF_SLONG8:
  848. {
  849. int64_t m;
  850. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  851. if (err != TIFFReadDirEntryErrOk)
  852. return (err);
  853. err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
  854. if (err != TIFFReadDirEntryErrOk)
  855. return (err);
  856. *value = (uint64_t)m;
  857. return (TIFFReadDirEntryErrOk);
  858. }
  859. default:
  860. return (TIFFReadDirEntryErrType);
  861. }
  862. } /*-- TIFFReadDirEntryLong8() --*/
  863. static enum TIFFReadDirEntryErr
  864. TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
  865. {
  866. enum TIFFReadDirEntryErr err;
  867. if (direntry->tdir_count != 1)
  868. return (TIFFReadDirEntryErrCount);
  869. switch (direntry->tdir_type)
  870. {
  871. case TIFF_BYTE:
  872. {
  873. uint8_t m;
  874. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  875. *value = (int64_t)m;
  876. return (TIFFReadDirEntryErrOk);
  877. }
  878. case TIFF_SBYTE:
  879. {
  880. int8_t m;
  881. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  882. *value = (int64_t)m;
  883. return (TIFFReadDirEntryErrOk);
  884. }
  885. case TIFF_SHORT:
  886. {
  887. uint16_t m;
  888. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  889. *value = (int64_t)m;
  890. return (TIFFReadDirEntryErrOk);
  891. }
  892. case TIFF_SSHORT:
  893. {
  894. int16_t m;
  895. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  896. *value = (int64_t)m;
  897. return (TIFFReadDirEntryErrOk);
  898. }
  899. case TIFF_LONG:
  900. {
  901. uint32_t m;
  902. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  903. *value = (int64_t)m;
  904. return (TIFFReadDirEntryErrOk);
  905. }
  906. case TIFF_SLONG:
  907. {
  908. int32_t m;
  909. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  910. *value = (int64_t)m;
  911. return (TIFFReadDirEntryErrOk);
  912. }
  913. case TIFF_LONG8:
  914. {
  915. uint64_t m;
  916. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  917. if (err != TIFFReadDirEntryErrOk)
  918. return (err);
  919. err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
  920. if (err != TIFFReadDirEntryErrOk)
  921. return (err);
  922. *value = (int64_t)m;
  923. return (TIFFReadDirEntryErrOk);
  924. }
  925. case TIFF_SLONG8:
  926. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
  927. return (err);
  928. default:
  929. return (TIFFReadDirEntryErrType);
  930. }
  931. } /*-- TIFFReadDirEntrySlong8() --*/
  932. static enum TIFFReadDirEntryErr
  933. TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
  934. {
  935. enum TIFFReadDirEntryErr err;
  936. if (direntry->tdir_count != 1)
  937. return (TIFFReadDirEntryErrCount);
  938. switch (direntry->tdir_type)
  939. {
  940. case TIFF_BYTE:
  941. {
  942. uint8_t m;
  943. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  944. *value = (float)m;
  945. return (TIFFReadDirEntryErrOk);
  946. }
  947. case TIFF_SBYTE:
  948. {
  949. int8_t m;
  950. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  951. *value = (float)m;
  952. return (TIFFReadDirEntryErrOk);
  953. }
  954. case TIFF_SHORT:
  955. {
  956. uint16_t m;
  957. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  958. *value = (float)m;
  959. return (TIFFReadDirEntryErrOk);
  960. }
  961. case TIFF_SSHORT:
  962. {
  963. int16_t m;
  964. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  965. *value = (float)m;
  966. return (TIFFReadDirEntryErrOk);
  967. }
  968. case TIFF_LONG:
  969. {
  970. uint32_t m;
  971. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  972. *value = (float)m;
  973. return (TIFFReadDirEntryErrOk);
  974. }
  975. case TIFF_SLONG:
  976. {
  977. int32_t m;
  978. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  979. *value = (float)m;
  980. return (TIFFReadDirEntryErrOk);
  981. }
  982. case TIFF_LONG8:
  983. {
  984. uint64_t m;
  985. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  986. if (err != TIFFReadDirEntryErrOk)
  987. return (err);
  988. *value = (float)m;
  989. return (TIFFReadDirEntryErrOk);
  990. }
  991. case TIFF_SLONG8:
  992. {
  993. int64_t m;
  994. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  995. if (err != TIFFReadDirEntryErrOk)
  996. return (err);
  997. *value = (float)m;
  998. return (TIFFReadDirEntryErrOk);
  999. }
  1000. case TIFF_RATIONAL:
  1001. {
  1002. double m;
  1003. err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
  1004. if (err != TIFFReadDirEntryErrOk)
  1005. return (err);
  1006. *value = (float)m;
  1007. return (TIFFReadDirEntryErrOk);
  1008. }
  1009. case TIFF_SRATIONAL:
  1010. {
  1011. double m;
  1012. err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
  1013. if (err != TIFFReadDirEntryErrOk)
  1014. return (err);
  1015. *value = (float)m;
  1016. return (TIFFReadDirEntryErrOk);
  1017. }
  1018. case TIFF_FLOAT:
  1019. TIFFReadDirEntryCheckedFloat(tif, direntry, value);
  1020. return (TIFFReadDirEntryErrOk);
  1021. case TIFF_DOUBLE:
  1022. {
  1023. double m;
  1024. err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
  1025. if (err != TIFFReadDirEntryErrOk)
  1026. return (err);
  1027. if ((m > FLT_MAX) || (m < -FLT_MAX))
  1028. return (TIFFReadDirEntryErrRange);
  1029. *value = (float)m;
  1030. return (TIFFReadDirEntryErrOk);
  1031. }
  1032. default:
  1033. return (TIFFReadDirEntryErrType);
  1034. }
  1035. }
  1036. static enum TIFFReadDirEntryErr
  1037. TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
  1038. {
  1039. enum TIFFReadDirEntryErr err;
  1040. if (direntry->tdir_count != 1)
  1041. return (TIFFReadDirEntryErrCount);
  1042. switch (direntry->tdir_type)
  1043. {
  1044. case TIFF_BYTE:
  1045. {
  1046. uint8_t m;
  1047. TIFFReadDirEntryCheckedByte(tif, direntry, &m);
  1048. *value = (double)m;
  1049. return (TIFFReadDirEntryErrOk);
  1050. }
  1051. case TIFF_SBYTE:
  1052. {
  1053. int8_t m;
  1054. TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
  1055. *value = (double)m;
  1056. return (TIFFReadDirEntryErrOk);
  1057. }
  1058. case TIFF_SHORT:
  1059. {
  1060. uint16_t m;
  1061. TIFFReadDirEntryCheckedShort(tif, direntry, &m);
  1062. *value = (double)m;
  1063. return (TIFFReadDirEntryErrOk);
  1064. }
  1065. case TIFF_SSHORT:
  1066. {
  1067. int16_t m;
  1068. TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
  1069. *value = (double)m;
  1070. return (TIFFReadDirEntryErrOk);
  1071. }
  1072. case TIFF_LONG:
  1073. {
  1074. uint32_t m;
  1075. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  1076. *value = (double)m;
  1077. return (TIFFReadDirEntryErrOk);
  1078. }
  1079. case TIFF_SLONG:
  1080. {
  1081. int32_t m;
  1082. TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
  1083. *value = (double)m;
  1084. return (TIFFReadDirEntryErrOk);
  1085. }
  1086. case TIFF_LONG8:
  1087. {
  1088. uint64_t m;
  1089. err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
  1090. if (err != TIFFReadDirEntryErrOk)
  1091. return (err);
  1092. *value = (double)m;
  1093. return (TIFFReadDirEntryErrOk);
  1094. }
  1095. case TIFF_SLONG8:
  1096. {
  1097. int64_t m;
  1098. err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
  1099. if (err != TIFFReadDirEntryErrOk)
  1100. return (err);
  1101. *value = (double)m;
  1102. return (TIFFReadDirEntryErrOk);
  1103. }
  1104. case TIFF_RATIONAL:
  1105. err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
  1106. return (err);
  1107. case TIFF_SRATIONAL:
  1108. err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
  1109. return (err);
  1110. case TIFF_FLOAT:
  1111. {
  1112. float m;
  1113. TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
  1114. *value = (double)m;
  1115. return (TIFFReadDirEntryErrOk);
  1116. }
  1117. case TIFF_DOUBLE:
  1118. err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
  1119. return (err);
  1120. default:
  1121. return (TIFFReadDirEntryErrType);
  1122. }
  1123. }
  1124. static enum TIFFReadDirEntryErr
  1125. TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
  1126. {
  1127. enum TIFFReadDirEntryErr err;
  1128. if (direntry->tdir_count != 1)
  1129. return (TIFFReadDirEntryErrCount);
  1130. switch (direntry->tdir_type)
  1131. {
  1132. case TIFF_LONG:
  1133. case TIFF_IFD:
  1134. {
  1135. uint32_t m;
  1136. TIFFReadDirEntryCheckedLong(tif, direntry, &m);
  1137. *value = (uint64_t)m;
  1138. return (TIFFReadDirEntryErrOk);
  1139. }
  1140. case TIFF_LONG8:
  1141. case TIFF_IFD8:
  1142. err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
  1143. return (err);
  1144. default:
  1145. return (TIFFReadDirEntryErrType);
  1146. }
  1147. }
  1148. #define INITIAL_THRESHOLD (1024 * 1024)
  1149. #define THRESHOLD_MULTIPLIER 10
  1150. #define MAX_THRESHOLD \
  1151. (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
  1152. INITIAL_THRESHOLD)
  1153. static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
  1154. uint64_t offset,
  1155. tmsize_t size,
  1156. void **pdest)
  1157. {
  1158. #if SIZEOF_SIZE_T == 8
  1159. tmsize_t threshold = INITIAL_THRESHOLD;
  1160. #endif
  1161. tmsize_t already_read = 0;
  1162. assert(!isMapped(tif));
  1163. if (!SeekOK(tif, offset))
  1164. return (TIFFReadDirEntryErrIo);
  1165. /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
  1166. /* so as to avoid allocating too much memory in case the file is too */
  1167. /* short. We could ask for the file size, but this might be */
  1168. /* expensive with some I/O layers (think of reading a gzipped file) */
  1169. /* Restrict to 64 bit processes, so as to avoid reallocs() */
  1170. /* on 32 bit processes where virtual memory is scarce. */
  1171. while (already_read < size)
  1172. {
  1173. void *new_dest;
  1174. tmsize_t bytes_read;
  1175. tmsize_t to_read = size - already_read;
  1176. #if SIZEOF_SIZE_T == 8
  1177. if (to_read >= threshold && threshold < MAX_THRESHOLD)
  1178. {
  1179. to_read = threshold;
  1180. threshold *= THRESHOLD_MULTIPLIER;
  1181. }
  1182. #endif
  1183. new_dest =
  1184. (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
  1185. if (new_dest == NULL)
  1186. {
  1187. TIFFErrorExtR(tif, tif->tif_name,
  1188. "Failed to allocate memory for %s "
  1189. "(%" TIFF_SSIZE_FORMAT
  1190. " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
  1191. "TIFFReadDirEntryArray", (tmsize_t)1,
  1192. already_read + to_read);
  1193. return TIFFReadDirEntryErrAlloc;
  1194. }
  1195. *pdest = new_dest;
  1196. bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
  1197. already_read += bytes_read;
  1198. if (bytes_read != to_read)
  1199. {
  1200. return TIFFReadDirEntryErrIo;
  1201. }
  1202. }
  1203. return TIFFReadDirEntryErrOk;
  1204. }
  1205. /* Caution: if raising that value, make sure int32 / uint32 overflows can't
  1206. * occur elsewhere */
  1207. #define MAX_SIZE_TAG_DATA 2147483647U
  1208. static enum TIFFReadDirEntryErr
  1209. TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
  1210. uint32_t *count, uint32_t desttypesize,
  1211. void **value, uint64_t maxcount)
  1212. {
  1213. int typesize;
  1214. uint32_t datasize;
  1215. void *data;
  1216. uint64_t target_count64;
  1217. int original_datasize_clamped;
  1218. typesize = TIFFDataWidth(direntry->tdir_type);
  1219. target_count64 =
  1220. (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
  1221. if ((target_count64 == 0) || (typesize == 0))
  1222. {
  1223. *value = 0;
  1224. return (TIFFReadDirEntryErrOk);
  1225. }
  1226. (void)desttypesize;
  1227. /* We just want to know if the original tag size is more than 4 bytes
  1228. * (classic TIFF) or 8 bytes (BigTIFF)
  1229. */
  1230. original_datasize_clamped =
  1231. ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
  1232. typesize;
  1233. /*
  1234. * As a sanity check, make sure we have no more than a 2GB tag array
  1235. * in either the current data type or the dest data type. This also
  1236. * avoids problems with overflow of tmsize_t on 32bit systems.
  1237. */
  1238. if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
  1239. return (TIFFReadDirEntryErrSizesan);
  1240. if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
  1241. return (TIFFReadDirEntryErrSizesan);
  1242. *count = (uint32_t)target_count64;
  1243. datasize = (*count) * typesize;
  1244. assert((tmsize_t)datasize > 0);
  1245. if (datasize > 100 * 1024 * 1024)
  1246. {
  1247. /* Before allocating a huge amount of memory for corrupted files, check
  1248. * if size of requested memory is not greater than file size.
  1249. */
  1250. const uint64_t filesize = TIFFGetFileSize(tif);
  1251. if (datasize > filesize)
  1252. {
  1253. TIFFWarningExtR(tif, "ReadDirEntryArray",
  1254. "Requested memory size for tag %d (0x%x) %" PRIu32
  1255. " is greater than filesize %" PRIu64
  1256. ". Memory not allocated, tag not read",
  1257. direntry->tdir_tag, direntry->tdir_tag, datasize,
  1258. filesize);
  1259. return (TIFFReadDirEntryErrAlloc);
  1260. }
  1261. }
  1262. if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
  1263. return TIFFReadDirEntryErrIo;
  1264. if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
  1265. (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
  1266. {
  1267. data = NULL;
  1268. }
  1269. else
  1270. {
  1271. data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
  1272. if (data == 0)
  1273. return (TIFFReadDirEntryErrAlloc);
  1274. }
  1275. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1276. {
  1277. /* Only the condition on original_datasize_clamped. The second
  1278. * one is implied, but Coverity Scan cannot see it. */
  1279. if (original_datasize_clamped <= 4 && datasize <= 4)
  1280. _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
  1281. else
  1282. {
  1283. enum TIFFReadDirEntryErr err;
  1284. uint32_t offset = direntry->tdir_offset.toff_long;
  1285. if (tif->tif_flags & TIFF_SWAB)
  1286. TIFFSwabLong(&offset);
  1287. if (isMapped(tif))
  1288. err = TIFFReadDirEntryData(tif, (uint64_t)offset,
  1289. (tmsize_t)datasize, data);
  1290. else
  1291. err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
  1292. (tmsize_t)datasize, &data);
  1293. if (err != TIFFReadDirEntryErrOk)
  1294. {
  1295. _TIFFfreeExt(tif, data);
  1296. return (err);
  1297. }
  1298. }
  1299. }
  1300. else
  1301. {
  1302. /* See above comment for the Classic TIFF case */
  1303. if (original_datasize_clamped <= 8 && datasize <= 8)
  1304. _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
  1305. else
  1306. {
  1307. enum TIFFReadDirEntryErr err;
  1308. uint64_t offset = direntry->tdir_offset.toff_long8;
  1309. if (tif->tif_flags & TIFF_SWAB)
  1310. TIFFSwabLong8(&offset);
  1311. if (isMapped(tif))
  1312. err = TIFFReadDirEntryData(tif, (uint64_t)offset,
  1313. (tmsize_t)datasize, data);
  1314. else
  1315. err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
  1316. (tmsize_t)datasize, &data);
  1317. if (err != TIFFReadDirEntryErrOk)
  1318. {
  1319. _TIFFfreeExt(tif, data);
  1320. return (err);
  1321. }
  1322. }
  1323. }
  1324. *value = data;
  1325. return (TIFFReadDirEntryErrOk);
  1326. }
  1327. static enum TIFFReadDirEntryErr
  1328. TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
  1329. uint32_t desttypesize, void **value)
  1330. {
  1331. return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
  1332. value, ~((uint64_t)0));
  1333. }
  1334. static enum TIFFReadDirEntryErr
  1335. TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
  1336. {
  1337. enum TIFFReadDirEntryErr err;
  1338. uint32_t count;
  1339. void *origdata;
  1340. uint8_t *data;
  1341. switch (direntry->tdir_type)
  1342. {
  1343. case TIFF_ASCII:
  1344. case TIFF_UNDEFINED:
  1345. case TIFF_BYTE:
  1346. case TIFF_SBYTE:
  1347. case TIFF_SHORT:
  1348. case TIFF_SSHORT:
  1349. case TIFF_LONG:
  1350. case TIFF_SLONG:
  1351. case TIFF_LONG8:
  1352. case TIFF_SLONG8:
  1353. break;
  1354. default:
  1355. return (TIFFReadDirEntryErrType);
  1356. }
  1357. err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
  1358. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  1359. {
  1360. *value = 0;
  1361. return (err);
  1362. }
  1363. switch (direntry->tdir_type)
  1364. {
  1365. case TIFF_ASCII:
  1366. case TIFF_UNDEFINED:
  1367. case TIFF_BYTE:
  1368. *value = (uint8_t *)origdata;
  1369. return (TIFFReadDirEntryErrOk);
  1370. case TIFF_SBYTE:
  1371. {
  1372. int8_t *m;
  1373. uint32_t n;
  1374. m = (int8_t *)origdata;
  1375. for (n = 0; n < count; n++)
  1376. {
  1377. err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
  1378. if (err != TIFFReadDirEntryErrOk)
  1379. {
  1380. _TIFFfreeExt(tif, origdata);
  1381. return (err);
  1382. }
  1383. m++;
  1384. }
  1385. *value = (uint8_t *)origdata;
  1386. return (TIFFReadDirEntryErrOk);
  1387. }
  1388. }
  1389. data = (uint8_t *)_TIFFmallocExt(tif, count);
  1390. if (data == 0)
  1391. {
  1392. _TIFFfreeExt(tif, origdata);
  1393. return (TIFFReadDirEntryErrAlloc);
  1394. }
  1395. switch (direntry->tdir_type)
  1396. {
  1397. case TIFF_SHORT:
  1398. {
  1399. uint16_t *ma;
  1400. uint8_t *mb;
  1401. uint32_t n;
  1402. ma = (uint16_t *)origdata;
  1403. mb = data;
  1404. for (n = 0; n < count; n++)
  1405. {
  1406. if (tif->tif_flags & TIFF_SWAB)
  1407. TIFFSwabShort(ma);
  1408. err = TIFFReadDirEntryCheckRangeByteShort(*ma);
  1409. if (err != TIFFReadDirEntryErrOk)
  1410. break;
  1411. *mb++ = (uint8_t)(*ma++);
  1412. }
  1413. }
  1414. break;
  1415. case TIFF_SSHORT:
  1416. {
  1417. int16_t *ma;
  1418. uint8_t *mb;
  1419. uint32_t n;
  1420. ma = (int16_t *)origdata;
  1421. mb = data;
  1422. for (n = 0; n < count; n++)
  1423. {
  1424. if (tif->tif_flags & TIFF_SWAB)
  1425. TIFFSwabShort((uint16_t *)ma);
  1426. err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
  1427. if (err != TIFFReadDirEntryErrOk)
  1428. break;
  1429. *mb++ = (uint8_t)(*ma++);
  1430. }
  1431. }
  1432. break;
  1433. case TIFF_LONG:
  1434. {
  1435. uint32_t *ma;
  1436. uint8_t *mb;
  1437. uint32_t n;
  1438. ma = (uint32_t *)origdata;
  1439. mb = data;
  1440. for (n = 0; n < count; n++)
  1441. {
  1442. if (tif->tif_flags & TIFF_SWAB)
  1443. TIFFSwabLong(ma);
  1444. err = TIFFReadDirEntryCheckRangeByteLong(*ma);
  1445. if (err != TIFFReadDirEntryErrOk)
  1446. break;
  1447. *mb++ = (uint8_t)(*ma++);
  1448. }
  1449. }
  1450. break;
  1451. case TIFF_SLONG:
  1452. {
  1453. int32_t *ma;
  1454. uint8_t *mb;
  1455. uint32_t n;
  1456. ma = (int32_t *)origdata;
  1457. mb = data;
  1458. for (n = 0; n < count; n++)
  1459. {
  1460. if (tif->tif_flags & TIFF_SWAB)
  1461. TIFFSwabLong((uint32_t *)ma);
  1462. err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
  1463. if (err != TIFFReadDirEntryErrOk)
  1464. break;
  1465. *mb++ = (uint8_t)(*ma++);
  1466. }
  1467. }
  1468. break;
  1469. case TIFF_LONG8:
  1470. {
  1471. uint64_t *ma;
  1472. uint8_t *mb;
  1473. uint32_t n;
  1474. ma = (uint64_t *)origdata;
  1475. mb = data;
  1476. for (n = 0; n < count; n++)
  1477. {
  1478. if (tif->tif_flags & TIFF_SWAB)
  1479. TIFFSwabLong8(ma);
  1480. err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
  1481. if (err != TIFFReadDirEntryErrOk)
  1482. break;
  1483. *mb++ = (uint8_t)(*ma++);
  1484. }
  1485. }
  1486. break;
  1487. case TIFF_SLONG8:
  1488. {
  1489. int64_t *ma;
  1490. uint8_t *mb;
  1491. uint32_t n;
  1492. ma = (int64_t *)origdata;
  1493. mb = data;
  1494. for (n = 0; n < count; n++)
  1495. {
  1496. if (tif->tif_flags & TIFF_SWAB)
  1497. TIFFSwabLong8((uint64_t *)ma);
  1498. err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
  1499. if (err != TIFFReadDirEntryErrOk)
  1500. break;
  1501. *mb++ = (uint8_t)(*ma++);
  1502. }
  1503. }
  1504. break;
  1505. }
  1506. _TIFFfreeExt(tif, origdata);
  1507. if (err != TIFFReadDirEntryErrOk)
  1508. {
  1509. _TIFFfreeExt(tif, data);
  1510. return (err);
  1511. }
  1512. *value = data;
  1513. return (TIFFReadDirEntryErrOk);
  1514. }
  1515. static enum TIFFReadDirEntryErr
  1516. TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
  1517. {
  1518. enum TIFFReadDirEntryErr err;
  1519. uint32_t count;
  1520. void *origdata;
  1521. int8_t *data;
  1522. switch (direntry->tdir_type)
  1523. {
  1524. case TIFF_UNDEFINED:
  1525. case TIFF_BYTE:
  1526. case TIFF_SBYTE:
  1527. case TIFF_SHORT:
  1528. case TIFF_SSHORT:
  1529. case TIFF_LONG:
  1530. case TIFF_SLONG:
  1531. case TIFF_LONG8:
  1532. case TIFF_SLONG8:
  1533. break;
  1534. default:
  1535. return (TIFFReadDirEntryErrType);
  1536. }
  1537. err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
  1538. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  1539. {
  1540. *value = 0;
  1541. return (err);
  1542. }
  1543. switch (direntry->tdir_type)
  1544. {
  1545. case TIFF_UNDEFINED:
  1546. case TIFF_BYTE:
  1547. {
  1548. uint8_t *m;
  1549. uint32_t n;
  1550. m = (uint8_t *)origdata;
  1551. for (n = 0; n < count; n++)
  1552. {
  1553. err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
  1554. if (err != TIFFReadDirEntryErrOk)
  1555. {
  1556. _TIFFfreeExt(tif, origdata);
  1557. return (err);
  1558. }
  1559. m++;
  1560. }
  1561. *value = (int8_t *)origdata;
  1562. return (TIFFReadDirEntryErrOk);
  1563. }
  1564. case TIFF_SBYTE:
  1565. *value = (int8_t *)origdata;
  1566. return (TIFFReadDirEntryErrOk);
  1567. }
  1568. data = (int8_t *)_TIFFmallocExt(tif, count);
  1569. if (data == 0)
  1570. {
  1571. _TIFFfreeExt(tif, origdata);
  1572. return (TIFFReadDirEntryErrAlloc);
  1573. }
  1574. switch (direntry->tdir_type)
  1575. {
  1576. case TIFF_SHORT:
  1577. {
  1578. uint16_t *ma;
  1579. int8_t *mb;
  1580. uint32_t n;
  1581. ma = (uint16_t *)origdata;
  1582. mb = data;
  1583. for (n = 0; n < count; n++)
  1584. {
  1585. if (tif->tif_flags & TIFF_SWAB)
  1586. TIFFSwabShort(ma);
  1587. err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
  1588. if (err != TIFFReadDirEntryErrOk)
  1589. break;
  1590. *mb++ = (int8_t)(*ma++);
  1591. }
  1592. }
  1593. break;
  1594. case TIFF_SSHORT:
  1595. {
  1596. int16_t *ma;
  1597. int8_t *mb;
  1598. uint32_t n;
  1599. ma = (int16_t *)origdata;
  1600. mb = data;
  1601. for (n = 0; n < count; n++)
  1602. {
  1603. if (tif->tif_flags & TIFF_SWAB)
  1604. TIFFSwabShort((uint16_t *)ma);
  1605. err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
  1606. if (err != TIFFReadDirEntryErrOk)
  1607. break;
  1608. *mb++ = (int8_t)(*ma++);
  1609. }
  1610. }
  1611. break;
  1612. case TIFF_LONG:
  1613. {
  1614. uint32_t *ma;
  1615. int8_t *mb;
  1616. uint32_t n;
  1617. ma = (uint32_t *)origdata;
  1618. mb = data;
  1619. for (n = 0; n < count; n++)
  1620. {
  1621. if (tif->tif_flags & TIFF_SWAB)
  1622. TIFFSwabLong(ma);
  1623. err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
  1624. if (err != TIFFReadDirEntryErrOk)
  1625. break;
  1626. *mb++ = (int8_t)(*ma++);
  1627. }
  1628. }
  1629. break;
  1630. case TIFF_SLONG:
  1631. {
  1632. int32_t *ma;
  1633. int8_t *mb;
  1634. uint32_t n;
  1635. ma = (int32_t *)origdata;
  1636. mb = data;
  1637. for (n = 0; n < count; n++)
  1638. {
  1639. if (tif->tif_flags & TIFF_SWAB)
  1640. TIFFSwabLong((uint32_t *)ma);
  1641. err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
  1642. if (err != TIFFReadDirEntryErrOk)
  1643. break;
  1644. *mb++ = (int8_t)(*ma++);
  1645. }
  1646. }
  1647. break;
  1648. case TIFF_LONG8:
  1649. {
  1650. uint64_t *ma;
  1651. int8_t *mb;
  1652. uint32_t n;
  1653. ma = (uint64_t *)origdata;
  1654. mb = data;
  1655. for (n = 0; n < count; n++)
  1656. {
  1657. if (tif->tif_flags & TIFF_SWAB)
  1658. TIFFSwabLong8(ma);
  1659. err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
  1660. if (err != TIFFReadDirEntryErrOk)
  1661. break;
  1662. *mb++ = (int8_t)(*ma++);
  1663. }
  1664. }
  1665. break;
  1666. case TIFF_SLONG8:
  1667. {
  1668. int64_t *ma;
  1669. int8_t *mb;
  1670. uint32_t n;
  1671. ma = (int64_t *)origdata;
  1672. mb = data;
  1673. for (n = 0; n < count; n++)
  1674. {
  1675. if (tif->tif_flags & TIFF_SWAB)
  1676. TIFFSwabLong8((uint64_t *)ma);
  1677. err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
  1678. if (err != TIFFReadDirEntryErrOk)
  1679. break;
  1680. *mb++ = (int8_t)(*ma++);
  1681. }
  1682. }
  1683. break;
  1684. }
  1685. _TIFFfreeExt(tif, origdata);
  1686. if (err != TIFFReadDirEntryErrOk)
  1687. {
  1688. _TIFFfreeExt(tif, data);
  1689. return (err);
  1690. }
  1691. *value = data;
  1692. return (TIFFReadDirEntryErrOk);
  1693. }
  1694. static enum TIFFReadDirEntryErr
  1695. TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
  1696. {
  1697. enum TIFFReadDirEntryErr err;
  1698. uint32_t count;
  1699. void *origdata;
  1700. uint16_t *data;
  1701. switch (direntry->tdir_type)
  1702. {
  1703. case TIFF_BYTE:
  1704. case TIFF_SBYTE:
  1705. case TIFF_SHORT:
  1706. case TIFF_SSHORT:
  1707. case TIFF_LONG:
  1708. case TIFF_SLONG:
  1709. case TIFF_LONG8:
  1710. case TIFF_SLONG8:
  1711. break;
  1712. default:
  1713. return (TIFFReadDirEntryErrType);
  1714. }
  1715. err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
  1716. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  1717. {
  1718. *value = 0;
  1719. return (err);
  1720. }
  1721. switch (direntry->tdir_type)
  1722. {
  1723. case TIFF_SHORT:
  1724. *value = (uint16_t *)origdata;
  1725. if (tif->tif_flags & TIFF_SWAB)
  1726. TIFFSwabArrayOfShort(*value, count);
  1727. return (TIFFReadDirEntryErrOk);
  1728. case TIFF_SSHORT:
  1729. {
  1730. int16_t *m;
  1731. uint32_t n;
  1732. m = (int16_t *)origdata;
  1733. for (n = 0; n < count; n++)
  1734. {
  1735. if (tif->tif_flags & TIFF_SWAB)
  1736. TIFFSwabShort((uint16_t *)m);
  1737. err = TIFFReadDirEntryCheckRangeShortSshort(*m);
  1738. if (err != TIFFReadDirEntryErrOk)
  1739. {
  1740. _TIFFfreeExt(tif, origdata);
  1741. return (err);
  1742. }
  1743. m++;
  1744. }
  1745. *value = (uint16_t *)origdata;
  1746. return (TIFFReadDirEntryErrOk);
  1747. }
  1748. }
  1749. data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
  1750. if (data == 0)
  1751. {
  1752. _TIFFfreeExt(tif, origdata);
  1753. return (TIFFReadDirEntryErrAlloc);
  1754. }
  1755. switch (direntry->tdir_type)
  1756. {
  1757. case TIFF_BYTE:
  1758. {
  1759. uint8_t *ma;
  1760. uint16_t *mb;
  1761. uint32_t n;
  1762. ma = (uint8_t *)origdata;
  1763. mb = data;
  1764. for (n = 0; n < count; n++)
  1765. *mb++ = (uint16_t)(*ma++);
  1766. }
  1767. break;
  1768. case TIFF_SBYTE:
  1769. {
  1770. int8_t *ma;
  1771. uint16_t *mb;
  1772. uint32_t n;
  1773. ma = (int8_t *)origdata;
  1774. mb = data;
  1775. for (n = 0; n < count; n++)
  1776. {
  1777. err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
  1778. if (err != TIFFReadDirEntryErrOk)
  1779. break;
  1780. *mb++ = (uint16_t)(*ma++);
  1781. }
  1782. }
  1783. break;
  1784. case TIFF_LONG:
  1785. {
  1786. uint32_t *ma;
  1787. uint16_t *mb;
  1788. uint32_t n;
  1789. ma = (uint32_t *)origdata;
  1790. mb = data;
  1791. for (n = 0; n < count; n++)
  1792. {
  1793. if (tif->tif_flags & TIFF_SWAB)
  1794. TIFFSwabLong(ma);
  1795. err = TIFFReadDirEntryCheckRangeShortLong(*ma);
  1796. if (err != TIFFReadDirEntryErrOk)
  1797. break;
  1798. *mb++ = (uint16_t)(*ma++);
  1799. }
  1800. }
  1801. break;
  1802. case TIFF_SLONG:
  1803. {
  1804. int32_t *ma;
  1805. uint16_t *mb;
  1806. uint32_t n;
  1807. ma = (int32_t *)origdata;
  1808. mb = data;
  1809. for (n = 0; n < count; n++)
  1810. {
  1811. if (tif->tif_flags & TIFF_SWAB)
  1812. TIFFSwabLong((uint32_t *)ma);
  1813. err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
  1814. if (err != TIFFReadDirEntryErrOk)
  1815. break;
  1816. *mb++ = (uint16_t)(*ma++);
  1817. }
  1818. }
  1819. break;
  1820. case TIFF_LONG8:
  1821. {
  1822. uint64_t *ma;
  1823. uint16_t *mb;
  1824. uint32_t n;
  1825. ma = (uint64_t *)origdata;
  1826. mb = data;
  1827. for (n = 0; n < count; n++)
  1828. {
  1829. if (tif->tif_flags & TIFF_SWAB)
  1830. TIFFSwabLong8(ma);
  1831. err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
  1832. if (err != TIFFReadDirEntryErrOk)
  1833. break;
  1834. *mb++ = (uint16_t)(*ma++);
  1835. }
  1836. }
  1837. break;
  1838. case TIFF_SLONG8:
  1839. {
  1840. int64_t *ma;
  1841. uint16_t *mb;
  1842. uint32_t n;
  1843. ma = (int64_t *)origdata;
  1844. mb = data;
  1845. for (n = 0; n < count; n++)
  1846. {
  1847. if (tif->tif_flags & TIFF_SWAB)
  1848. TIFFSwabLong8((uint64_t *)ma);
  1849. err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
  1850. if (err != TIFFReadDirEntryErrOk)
  1851. break;
  1852. *mb++ = (uint16_t)(*ma++);
  1853. }
  1854. }
  1855. break;
  1856. }
  1857. _TIFFfreeExt(tif, origdata);
  1858. if (err != TIFFReadDirEntryErrOk)
  1859. {
  1860. _TIFFfreeExt(tif, data);
  1861. return (err);
  1862. }
  1863. *value = data;
  1864. return (TIFFReadDirEntryErrOk);
  1865. }
  1866. static enum TIFFReadDirEntryErr
  1867. TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
  1868. {
  1869. enum TIFFReadDirEntryErr err;
  1870. uint32_t count;
  1871. void *origdata;
  1872. int16_t *data;
  1873. switch (direntry->tdir_type)
  1874. {
  1875. case TIFF_BYTE:
  1876. case TIFF_SBYTE:
  1877. case TIFF_SHORT:
  1878. case TIFF_SSHORT:
  1879. case TIFF_LONG:
  1880. case TIFF_SLONG:
  1881. case TIFF_LONG8:
  1882. case TIFF_SLONG8:
  1883. break;
  1884. default:
  1885. return (TIFFReadDirEntryErrType);
  1886. }
  1887. err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
  1888. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  1889. {
  1890. *value = 0;
  1891. return (err);
  1892. }
  1893. switch (direntry->tdir_type)
  1894. {
  1895. case TIFF_SHORT:
  1896. {
  1897. uint16_t *m;
  1898. uint32_t n;
  1899. m = (uint16_t *)origdata;
  1900. for (n = 0; n < count; n++)
  1901. {
  1902. if (tif->tif_flags & TIFF_SWAB)
  1903. TIFFSwabShort(m);
  1904. err = TIFFReadDirEntryCheckRangeSshortShort(*m);
  1905. if (err != TIFFReadDirEntryErrOk)
  1906. {
  1907. _TIFFfreeExt(tif, origdata);
  1908. return (err);
  1909. }
  1910. m++;
  1911. }
  1912. *value = (int16_t *)origdata;
  1913. return (TIFFReadDirEntryErrOk);
  1914. }
  1915. case TIFF_SSHORT:
  1916. *value = (int16_t *)origdata;
  1917. if (tif->tif_flags & TIFF_SWAB)
  1918. TIFFSwabArrayOfShort((uint16_t *)(*value), count);
  1919. return (TIFFReadDirEntryErrOk);
  1920. }
  1921. data = (int16_t *)_TIFFmallocExt(tif, count * 2);
  1922. if (data == 0)
  1923. {
  1924. _TIFFfreeExt(tif, origdata);
  1925. return (TIFFReadDirEntryErrAlloc);
  1926. }
  1927. switch (direntry->tdir_type)
  1928. {
  1929. case TIFF_BYTE:
  1930. {
  1931. uint8_t *ma;
  1932. int16_t *mb;
  1933. uint32_t n;
  1934. ma = (uint8_t *)origdata;
  1935. mb = data;
  1936. for (n = 0; n < count; n++)
  1937. *mb++ = (int16_t)(*ma++);
  1938. }
  1939. break;
  1940. case TIFF_SBYTE:
  1941. {
  1942. int8_t *ma;
  1943. int16_t *mb;
  1944. uint32_t n;
  1945. ma = (int8_t *)origdata;
  1946. mb = data;
  1947. for (n = 0; n < count; n++)
  1948. *mb++ = (int16_t)(*ma++);
  1949. }
  1950. break;
  1951. case TIFF_LONG:
  1952. {
  1953. uint32_t *ma;
  1954. int16_t *mb;
  1955. uint32_t n;
  1956. ma = (uint32_t *)origdata;
  1957. mb = data;
  1958. for (n = 0; n < count; n++)
  1959. {
  1960. if (tif->tif_flags & TIFF_SWAB)
  1961. TIFFSwabLong(ma);
  1962. err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
  1963. if (err != TIFFReadDirEntryErrOk)
  1964. break;
  1965. *mb++ = (int16_t)(*ma++);
  1966. }
  1967. }
  1968. break;
  1969. case TIFF_SLONG:
  1970. {
  1971. int32_t *ma;
  1972. int16_t *mb;
  1973. uint32_t n;
  1974. ma = (int32_t *)origdata;
  1975. mb = data;
  1976. for (n = 0; n < count; n++)
  1977. {
  1978. if (tif->tif_flags & TIFF_SWAB)
  1979. TIFFSwabLong((uint32_t *)ma);
  1980. err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
  1981. if (err != TIFFReadDirEntryErrOk)
  1982. break;
  1983. *mb++ = (int16_t)(*ma++);
  1984. }
  1985. }
  1986. break;
  1987. case TIFF_LONG8:
  1988. {
  1989. uint64_t *ma;
  1990. int16_t *mb;
  1991. uint32_t n;
  1992. ma = (uint64_t *)origdata;
  1993. mb = data;
  1994. for (n = 0; n < count; n++)
  1995. {
  1996. if (tif->tif_flags & TIFF_SWAB)
  1997. TIFFSwabLong8(ma);
  1998. err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
  1999. if (err != TIFFReadDirEntryErrOk)
  2000. break;
  2001. *mb++ = (int16_t)(*ma++);
  2002. }
  2003. }
  2004. break;
  2005. case TIFF_SLONG8:
  2006. {
  2007. int64_t *ma;
  2008. int16_t *mb;
  2009. uint32_t n;
  2010. ma = (int64_t *)origdata;
  2011. mb = data;
  2012. for (n = 0; n < count; n++)
  2013. {
  2014. if (tif->tif_flags & TIFF_SWAB)
  2015. TIFFSwabLong8((uint64_t *)ma);
  2016. err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
  2017. if (err != TIFFReadDirEntryErrOk)
  2018. break;
  2019. *mb++ = (int16_t)(*ma++);
  2020. }
  2021. }
  2022. break;
  2023. }
  2024. _TIFFfreeExt(tif, origdata);
  2025. if (err != TIFFReadDirEntryErrOk)
  2026. {
  2027. _TIFFfreeExt(tif, data);
  2028. return (err);
  2029. }
  2030. *value = data;
  2031. return (TIFFReadDirEntryErrOk);
  2032. }
  2033. static enum TIFFReadDirEntryErr
  2034. TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
  2035. {
  2036. enum TIFFReadDirEntryErr err;
  2037. uint32_t count;
  2038. void *origdata;
  2039. uint32_t *data;
  2040. switch (direntry->tdir_type)
  2041. {
  2042. case TIFF_BYTE:
  2043. case TIFF_SBYTE:
  2044. case TIFF_SHORT:
  2045. case TIFF_SSHORT:
  2046. case TIFF_LONG:
  2047. case TIFF_SLONG:
  2048. case TIFF_LONG8:
  2049. case TIFF_SLONG8:
  2050. break;
  2051. default:
  2052. return (TIFFReadDirEntryErrType);
  2053. }
  2054. err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
  2055. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2056. {
  2057. *value = 0;
  2058. return (err);
  2059. }
  2060. switch (direntry->tdir_type)
  2061. {
  2062. case TIFF_LONG:
  2063. *value = (uint32_t *)origdata;
  2064. if (tif->tif_flags & TIFF_SWAB)
  2065. TIFFSwabArrayOfLong(*value, count);
  2066. return (TIFFReadDirEntryErrOk);
  2067. case TIFF_SLONG:
  2068. {
  2069. int32_t *m;
  2070. uint32_t n;
  2071. m = (int32_t *)origdata;
  2072. for (n = 0; n < count; n++)
  2073. {
  2074. if (tif->tif_flags & TIFF_SWAB)
  2075. TIFFSwabLong((uint32_t *)m);
  2076. err = TIFFReadDirEntryCheckRangeLongSlong(*m);
  2077. if (err != TIFFReadDirEntryErrOk)
  2078. {
  2079. _TIFFfreeExt(tif, origdata);
  2080. return (err);
  2081. }
  2082. m++;
  2083. }
  2084. *value = (uint32_t *)origdata;
  2085. return (TIFFReadDirEntryErrOk);
  2086. }
  2087. }
  2088. data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
  2089. if (data == 0)
  2090. {
  2091. _TIFFfreeExt(tif, origdata);
  2092. return (TIFFReadDirEntryErrAlloc);
  2093. }
  2094. switch (direntry->tdir_type)
  2095. {
  2096. case TIFF_BYTE:
  2097. {
  2098. uint8_t *ma;
  2099. uint32_t *mb;
  2100. uint32_t n;
  2101. ma = (uint8_t *)origdata;
  2102. mb = data;
  2103. for (n = 0; n < count; n++)
  2104. *mb++ = (uint32_t)(*ma++);
  2105. }
  2106. break;
  2107. case TIFF_SBYTE:
  2108. {
  2109. int8_t *ma;
  2110. uint32_t *mb;
  2111. uint32_t n;
  2112. ma = (int8_t *)origdata;
  2113. mb = data;
  2114. for (n = 0; n < count; n++)
  2115. {
  2116. err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
  2117. if (err != TIFFReadDirEntryErrOk)
  2118. break;
  2119. *mb++ = (uint32_t)(*ma++);
  2120. }
  2121. }
  2122. break;
  2123. case TIFF_SHORT:
  2124. {
  2125. uint16_t *ma;
  2126. uint32_t *mb;
  2127. uint32_t n;
  2128. ma = (uint16_t *)origdata;
  2129. mb = data;
  2130. for (n = 0; n < count; n++)
  2131. {
  2132. if (tif->tif_flags & TIFF_SWAB)
  2133. TIFFSwabShort(ma);
  2134. *mb++ = (uint32_t)(*ma++);
  2135. }
  2136. }
  2137. break;
  2138. case TIFF_SSHORT:
  2139. {
  2140. int16_t *ma;
  2141. uint32_t *mb;
  2142. uint32_t n;
  2143. ma = (int16_t *)origdata;
  2144. mb = data;
  2145. for (n = 0; n < count; n++)
  2146. {
  2147. if (tif->tif_flags & TIFF_SWAB)
  2148. TIFFSwabShort((uint16_t *)ma);
  2149. err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
  2150. if (err != TIFFReadDirEntryErrOk)
  2151. break;
  2152. *mb++ = (uint32_t)(*ma++);
  2153. }
  2154. }
  2155. break;
  2156. case TIFF_LONG8:
  2157. {
  2158. uint64_t *ma;
  2159. uint32_t *mb;
  2160. uint32_t n;
  2161. ma = (uint64_t *)origdata;
  2162. mb = data;
  2163. for (n = 0; n < count; n++)
  2164. {
  2165. if (tif->tif_flags & TIFF_SWAB)
  2166. TIFFSwabLong8(ma);
  2167. err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
  2168. if (err != TIFFReadDirEntryErrOk)
  2169. break;
  2170. *mb++ = (uint32_t)(*ma++);
  2171. }
  2172. }
  2173. break;
  2174. case TIFF_SLONG8:
  2175. {
  2176. int64_t *ma;
  2177. uint32_t *mb;
  2178. uint32_t n;
  2179. ma = (int64_t *)origdata;
  2180. mb = data;
  2181. for (n = 0; n < count; n++)
  2182. {
  2183. if (tif->tif_flags & TIFF_SWAB)
  2184. TIFFSwabLong8((uint64_t *)ma);
  2185. err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
  2186. if (err != TIFFReadDirEntryErrOk)
  2187. break;
  2188. *mb++ = (uint32_t)(*ma++);
  2189. }
  2190. }
  2191. break;
  2192. }
  2193. _TIFFfreeExt(tif, origdata);
  2194. if (err != TIFFReadDirEntryErrOk)
  2195. {
  2196. _TIFFfreeExt(tif, data);
  2197. return (err);
  2198. }
  2199. *value = data;
  2200. return (TIFFReadDirEntryErrOk);
  2201. }
  2202. static enum TIFFReadDirEntryErr
  2203. TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
  2204. {
  2205. enum TIFFReadDirEntryErr err;
  2206. uint32_t count;
  2207. void *origdata;
  2208. int32_t *data;
  2209. switch (direntry->tdir_type)
  2210. {
  2211. case TIFF_BYTE:
  2212. case TIFF_SBYTE:
  2213. case TIFF_SHORT:
  2214. case TIFF_SSHORT:
  2215. case TIFF_LONG:
  2216. case TIFF_SLONG:
  2217. case TIFF_LONG8:
  2218. case TIFF_SLONG8:
  2219. break;
  2220. default:
  2221. return (TIFFReadDirEntryErrType);
  2222. }
  2223. err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
  2224. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2225. {
  2226. *value = 0;
  2227. return (err);
  2228. }
  2229. switch (direntry->tdir_type)
  2230. {
  2231. case TIFF_LONG:
  2232. {
  2233. uint32_t *m;
  2234. uint32_t n;
  2235. m = (uint32_t *)origdata;
  2236. for (n = 0; n < count; n++)
  2237. {
  2238. if (tif->tif_flags & TIFF_SWAB)
  2239. TIFFSwabLong((uint32_t *)m);
  2240. err = TIFFReadDirEntryCheckRangeSlongLong(*m);
  2241. if (err != TIFFReadDirEntryErrOk)
  2242. {
  2243. _TIFFfreeExt(tif, origdata);
  2244. return (err);
  2245. }
  2246. m++;
  2247. }
  2248. *value = (int32_t *)origdata;
  2249. return (TIFFReadDirEntryErrOk);
  2250. }
  2251. case TIFF_SLONG:
  2252. *value = (int32_t *)origdata;
  2253. if (tif->tif_flags & TIFF_SWAB)
  2254. TIFFSwabArrayOfLong((uint32_t *)(*value), count);
  2255. return (TIFFReadDirEntryErrOk);
  2256. }
  2257. data = (int32_t *)_TIFFmallocExt(tif, count * 4);
  2258. if (data == 0)
  2259. {
  2260. _TIFFfreeExt(tif, origdata);
  2261. return (TIFFReadDirEntryErrAlloc);
  2262. }
  2263. switch (direntry->tdir_type)
  2264. {
  2265. case TIFF_BYTE:
  2266. {
  2267. uint8_t *ma;
  2268. int32_t *mb;
  2269. uint32_t n;
  2270. ma = (uint8_t *)origdata;
  2271. mb = data;
  2272. for (n = 0; n < count; n++)
  2273. *mb++ = (int32_t)(*ma++);
  2274. }
  2275. break;
  2276. case TIFF_SBYTE:
  2277. {
  2278. int8_t *ma;
  2279. int32_t *mb;
  2280. uint32_t n;
  2281. ma = (int8_t *)origdata;
  2282. mb = data;
  2283. for (n = 0; n < count; n++)
  2284. *mb++ = (int32_t)(*ma++);
  2285. }
  2286. break;
  2287. case TIFF_SHORT:
  2288. {
  2289. uint16_t *ma;
  2290. int32_t *mb;
  2291. uint32_t n;
  2292. ma = (uint16_t *)origdata;
  2293. mb = data;
  2294. for (n = 0; n < count; n++)
  2295. {
  2296. if (tif->tif_flags & TIFF_SWAB)
  2297. TIFFSwabShort(ma);
  2298. *mb++ = (int32_t)(*ma++);
  2299. }
  2300. }
  2301. break;
  2302. case TIFF_SSHORT:
  2303. {
  2304. int16_t *ma;
  2305. int32_t *mb;
  2306. uint32_t n;
  2307. ma = (int16_t *)origdata;
  2308. mb = data;
  2309. for (n = 0; n < count; n++)
  2310. {
  2311. if (tif->tif_flags & TIFF_SWAB)
  2312. TIFFSwabShort((uint16_t *)ma);
  2313. *mb++ = (int32_t)(*ma++);
  2314. }
  2315. }
  2316. break;
  2317. case TIFF_LONG8:
  2318. {
  2319. uint64_t *ma;
  2320. int32_t *mb;
  2321. uint32_t n;
  2322. ma = (uint64_t *)origdata;
  2323. mb = data;
  2324. for (n = 0; n < count; n++)
  2325. {
  2326. if (tif->tif_flags & TIFF_SWAB)
  2327. TIFFSwabLong8(ma);
  2328. err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
  2329. if (err != TIFFReadDirEntryErrOk)
  2330. break;
  2331. *mb++ = (int32_t)(*ma++);
  2332. }
  2333. }
  2334. break;
  2335. case TIFF_SLONG8:
  2336. {
  2337. int64_t *ma;
  2338. int32_t *mb;
  2339. uint32_t n;
  2340. ma = (int64_t *)origdata;
  2341. mb = data;
  2342. for (n = 0; n < count; n++)
  2343. {
  2344. if (tif->tif_flags & TIFF_SWAB)
  2345. TIFFSwabLong8((uint64_t *)ma);
  2346. err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
  2347. if (err != TIFFReadDirEntryErrOk)
  2348. break;
  2349. *mb++ = (int32_t)(*ma++);
  2350. }
  2351. }
  2352. break;
  2353. }
  2354. _TIFFfreeExt(tif, origdata);
  2355. if (err != TIFFReadDirEntryErrOk)
  2356. {
  2357. _TIFFfreeExt(tif, data);
  2358. return (err);
  2359. }
  2360. *value = data;
  2361. return (TIFFReadDirEntryErrOk);
  2362. }
  2363. static enum TIFFReadDirEntryErr
  2364. TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
  2365. uint64_t **value, uint64_t maxcount)
  2366. {
  2367. enum TIFFReadDirEntryErr err;
  2368. uint32_t count;
  2369. void *origdata;
  2370. uint64_t *data;
  2371. switch (direntry->tdir_type)
  2372. {
  2373. case TIFF_BYTE:
  2374. case TIFF_SBYTE:
  2375. case TIFF_SHORT:
  2376. case TIFF_SSHORT:
  2377. case TIFF_LONG:
  2378. case TIFF_SLONG:
  2379. case TIFF_LONG8:
  2380. case TIFF_SLONG8:
  2381. break;
  2382. default:
  2383. return (TIFFReadDirEntryErrType);
  2384. }
  2385. err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
  2386. maxcount);
  2387. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2388. {
  2389. *value = 0;
  2390. return (err);
  2391. }
  2392. switch (direntry->tdir_type)
  2393. {
  2394. case TIFF_LONG8:
  2395. *value = (uint64_t *)origdata;
  2396. if (tif->tif_flags & TIFF_SWAB)
  2397. TIFFSwabArrayOfLong8(*value, count);
  2398. return (TIFFReadDirEntryErrOk);
  2399. case TIFF_SLONG8:
  2400. {
  2401. int64_t *m;
  2402. uint32_t n;
  2403. m = (int64_t *)origdata;
  2404. for (n = 0; n < count; n++)
  2405. {
  2406. if (tif->tif_flags & TIFF_SWAB)
  2407. TIFFSwabLong8((uint64_t *)m);
  2408. err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
  2409. if (err != TIFFReadDirEntryErrOk)
  2410. {
  2411. _TIFFfreeExt(tif, origdata);
  2412. return (err);
  2413. }
  2414. m++;
  2415. }
  2416. *value = (uint64_t *)origdata;
  2417. return (TIFFReadDirEntryErrOk);
  2418. }
  2419. }
  2420. data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
  2421. if (data == 0)
  2422. {
  2423. _TIFFfreeExt(tif, origdata);
  2424. return (TIFFReadDirEntryErrAlloc);
  2425. }
  2426. switch (direntry->tdir_type)
  2427. {
  2428. case TIFF_BYTE:
  2429. {
  2430. uint8_t *ma;
  2431. uint64_t *mb;
  2432. uint32_t n;
  2433. ma = (uint8_t *)origdata;
  2434. mb = data;
  2435. for (n = 0; n < count; n++)
  2436. *mb++ = (uint64_t)(*ma++);
  2437. }
  2438. break;
  2439. case TIFF_SBYTE:
  2440. {
  2441. int8_t *ma;
  2442. uint64_t *mb;
  2443. uint32_t n;
  2444. ma = (int8_t *)origdata;
  2445. mb = data;
  2446. for (n = 0; n < count; n++)
  2447. {
  2448. err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
  2449. if (err != TIFFReadDirEntryErrOk)
  2450. break;
  2451. *mb++ = (uint64_t)(*ma++);
  2452. }
  2453. }
  2454. break;
  2455. case TIFF_SHORT:
  2456. {
  2457. uint16_t *ma;
  2458. uint64_t *mb;
  2459. uint32_t n;
  2460. ma = (uint16_t *)origdata;
  2461. mb = data;
  2462. for (n = 0; n < count; n++)
  2463. {
  2464. if (tif->tif_flags & TIFF_SWAB)
  2465. TIFFSwabShort(ma);
  2466. *mb++ = (uint64_t)(*ma++);
  2467. }
  2468. }
  2469. break;
  2470. case TIFF_SSHORT:
  2471. {
  2472. int16_t *ma;
  2473. uint64_t *mb;
  2474. uint32_t n;
  2475. ma = (int16_t *)origdata;
  2476. mb = data;
  2477. for (n = 0; n < count; n++)
  2478. {
  2479. if (tif->tif_flags & TIFF_SWAB)
  2480. TIFFSwabShort((uint16_t *)ma);
  2481. err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
  2482. if (err != TIFFReadDirEntryErrOk)
  2483. break;
  2484. *mb++ = (uint64_t)(*ma++);
  2485. }
  2486. }
  2487. break;
  2488. case TIFF_LONG:
  2489. {
  2490. uint32_t *ma;
  2491. uint64_t *mb;
  2492. uint32_t n;
  2493. ma = (uint32_t *)origdata;
  2494. mb = data;
  2495. for (n = 0; n < count; n++)
  2496. {
  2497. if (tif->tif_flags & TIFF_SWAB)
  2498. TIFFSwabLong(ma);
  2499. *mb++ = (uint64_t)(*ma++);
  2500. }
  2501. }
  2502. break;
  2503. case TIFF_SLONG:
  2504. {
  2505. int32_t *ma;
  2506. uint64_t *mb;
  2507. uint32_t n;
  2508. ma = (int32_t *)origdata;
  2509. mb = data;
  2510. for (n = 0; n < count; n++)
  2511. {
  2512. if (tif->tif_flags & TIFF_SWAB)
  2513. TIFFSwabLong((uint32_t *)ma);
  2514. err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
  2515. if (err != TIFFReadDirEntryErrOk)
  2516. break;
  2517. *mb++ = (uint64_t)(*ma++);
  2518. }
  2519. }
  2520. break;
  2521. }
  2522. _TIFFfreeExt(tif, origdata);
  2523. if (err != TIFFReadDirEntryErrOk)
  2524. {
  2525. _TIFFfreeExt(tif, data);
  2526. return (err);
  2527. }
  2528. *value = data;
  2529. return (TIFFReadDirEntryErrOk);
  2530. }
  2531. static enum TIFFReadDirEntryErr
  2532. TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
  2533. {
  2534. return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
  2535. ~((uint64_t)0));
  2536. }
  2537. static enum TIFFReadDirEntryErr
  2538. TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
  2539. {
  2540. enum TIFFReadDirEntryErr err;
  2541. uint32_t count;
  2542. void *origdata;
  2543. int64_t *data;
  2544. switch (direntry->tdir_type)
  2545. {
  2546. case TIFF_BYTE:
  2547. case TIFF_SBYTE:
  2548. case TIFF_SHORT:
  2549. case TIFF_SSHORT:
  2550. case TIFF_LONG:
  2551. case TIFF_SLONG:
  2552. case TIFF_LONG8:
  2553. case TIFF_SLONG8:
  2554. break;
  2555. default:
  2556. return (TIFFReadDirEntryErrType);
  2557. }
  2558. err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
  2559. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2560. {
  2561. *value = 0;
  2562. return (err);
  2563. }
  2564. switch (direntry->tdir_type)
  2565. {
  2566. case TIFF_LONG8:
  2567. {
  2568. uint64_t *m;
  2569. uint32_t n;
  2570. m = (uint64_t *)origdata;
  2571. for (n = 0; n < count; n++)
  2572. {
  2573. if (tif->tif_flags & TIFF_SWAB)
  2574. TIFFSwabLong8(m);
  2575. err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
  2576. if (err != TIFFReadDirEntryErrOk)
  2577. {
  2578. _TIFFfreeExt(tif, origdata);
  2579. return (err);
  2580. }
  2581. m++;
  2582. }
  2583. *value = (int64_t *)origdata;
  2584. return (TIFFReadDirEntryErrOk);
  2585. }
  2586. case TIFF_SLONG8:
  2587. *value = (int64_t *)origdata;
  2588. if (tif->tif_flags & TIFF_SWAB)
  2589. TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
  2590. return (TIFFReadDirEntryErrOk);
  2591. }
  2592. data = (int64_t *)_TIFFmallocExt(tif, count * 8);
  2593. if (data == 0)
  2594. {
  2595. _TIFFfreeExt(tif, origdata);
  2596. return (TIFFReadDirEntryErrAlloc);
  2597. }
  2598. switch (direntry->tdir_type)
  2599. {
  2600. case TIFF_BYTE:
  2601. {
  2602. uint8_t *ma;
  2603. int64_t *mb;
  2604. uint32_t n;
  2605. ma = (uint8_t *)origdata;
  2606. mb = data;
  2607. for (n = 0; n < count; n++)
  2608. *mb++ = (int64_t)(*ma++);
  2609. }
  2610. break;
  2611. case TIFF_SBYTE:
  2612. {
  2613. int8_t *ma;
  2614. int64_t *mb;
  2615. uint32_t n;
  2616. ma = (int8_t *)origdata;
  2617. mb = data;
  2618. for (n = 0; n < count; n++)
  2619. *mb++ = (int64_t)(*ma++);
  2620. }
  2621. break;
  2622. case TIFF_SHORT:
  2623. {
  2624. uint16_t *ma;
  2625. int64_t *mb;
  2626. uint32_t n;
  2627. ma = (uint16_t *)origdata;
  2628. mb = data;
  2629. for (n = 0; n < count; n++)
  2630. {
  2631. if (tif->tif_flags & TIFF_SWAB)
  2632. TIFFSwabShort(ma);
  2633. *mb++ = (int64_t)(*ma++);
  2634. }
  2635. }
  2636. break;
  2637. case TIFF_SSHORT:
  2638. {
  2639. int16_t *ma;
  2640. int64_t *mb;
  2641. uint32_t n;
  2642. ma = (int16_t *)origdata;
  2643. mb = data;
  2644. for (n = 0; n < count; n++)
  2645. {
  2646. if (tif->tif_flags & TIFF_SWAB)
  2647. TIFFSwabShort((uint16_t *)ma);
  2648. *mb++ = (int64_t)(*ma++);
  2649. }
  2650. }
  2651. break;
  2652. case TIFF_LONG:
  2653. {
  2654. uint32_t *ma;
  2655. int64_t *mb;
  2656. uint32_t n;
  2657. ma = (uint32_t *)origdata;
  2658. mb = data;
  2659. for (n = 0; n < count; n++)
  2660. {
  2661. if (tif->tif_flags & TIFF_SWAB)
  2662. TIFFSwabLong(ma);
  2663. *mb++ = (int64_t)(*ma++);
  2664. }
  2665. }
  2666. break;
  2667. case TIFF_SLONG:
  2668. {
  2669. int32_t *ma;
  2670. int64_t *mb;
  2671. uint32_t n;
  2672. ma = (int32_t *)origdata;
  2673. mb = data;
  2674. for (n = 0; n < count; n++)
  2675. {
  2676. if (tif->tif_flags & TIFF_SWAB)
  2677. TIFFSwabLong((uint32_t *)ma);
  2678. *mb++ = (int64_t)(*ma++);
  2679. }
  2680. }
  2681. break;
  2682. }
  2683. _TIFFfreeExt(tif, origdata);
  2684. *value = data;
  2685. return (TIFFReadDirEntryErrOk);
  2686. }
  2687. static enum TIFFReadDirEntryErr
  2688. TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
  2689. {
  2690. enum TIFFReadDirEntryErr err;
  2691. uint32_t count;
  2692. void *origdata;
  2693. float *data;
  2694. switch (direntry->tdir_type)
  2695. {
  2696. case TIFF_BYTE:
  2697. case TIFF_SBYTE:
  2698. case TIFF_SHORT:
  2699. case TIFF_SSHORT:
  2700. case TIFF_LONG:
  2701. case TIFF_SLONG:
  2702. case TIFF_LONG8:
  2703. case TIFF_SLONG8:
  2704. case TIFF_RATIONAL:
  2705. case TIFF_SRATIONAL:
  2706. case TIFF_FLOAT:
  2707. case TIFF_DOUBLE:
  2708. break;
  2709. default:
  2710. return (TIFFReadDirEntryErrType);
  2711. }
  2712. err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
  2713. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2714. {
  2715. *value = 0;
  2716. return (err);
  2717. }
  2718. switch (direntry->tdir_type)
  2719. {
  2720. case TIFF_FLOAT:
  2721. if (tif->tif_flags & TIFF_SWAB)
  2722. TIFFSwabArrayOfLong((uint32_t *)origdata, count);
  2723. TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
  2724. *value = (float *)origdata;
  2725. return (TIFFReadDirEntryErrOk);
  2726. }
  2727. data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
  2728. if (data == 0)
  2729. {
  2730. _TIFFfreeExt(tif, origdata);
  2731. return (TIFFReadDirEntryErrAlloc);
  2732. }
  2733. switch (direntry->tdir_type)
  2734. {
  2735. case TIFF_BYTE:
  2736. {
  2737. uint8_t *ma;
  2738. float *mb;
  2739. uint32_t n;
  2740. ma = (uint8_t *)origdata;
  2741. mb = data;
  2742. for (n = 0; n < count; n++)
  2743. *mb++ = (float)(*ma++);
  2744. }
  2745. break;
  2746. case TIFF_SBYTE:
  2747. {
  2748. int8_t *ma;
  2749. float *mb;
  2750. uint32_t n;
  2751. ma = (int8_t *)origdata;
  2752. mb = data;
  2753. for (n = 0; n < count; n++)
  2754. *mb++ = (float)(*ma++);
  2755. }
  2756. break;
  2757. case TIFF_SHORT:
  2758. {
  2759. uint16_t *ma;
  2760. float *mb;
  2761. uint32_t n;
  2762. ma = (uint16_t *)origdata;
  2763. mb = data;
  2764. for (n = 0; n < count; n++)
  2765. {
  2766. if (tif->tif_flags & TIFF_SWAB)
  2767. TIFFSwabShort(ma);
  2768. *mb++ = (float)(*ma++);
  2769. }
  2770. }
  2771. break;
  2772. case TIFF_SSHORT:
  2773. {
  2774. int16_t *ma;
  2775. float *mb;
  2776. uint32_t n;
  2777. ma = (int16_t *)origdata;
  2778. mb = data;
  2779. for (n = 0; n < count; n++)
  2780. {
  2781. if (tif->tif_flags & TIFF_SWAB)
  2782. TIFFSwabShort((uint16_t *)ma);
  2783. *mb++ = (float)(*ma++);
  2784. }
  2785. }
  2786. break;
  2787. case TIFF_LONG:
  2788. {
  2789. uint32_t *ma;
  2790. float *mb;
  2791. uint32_t n;
  2792. ma = (uint32_t *)origdata;
  2793. mb = data;
  2794. for (n = 0; n < count; n++)
  2795. {
  2796. if (tif->tif_flags & TIFF_SWAB)
  2797. TIFFSwabLong(ma);
  2798. *mb++ = (float)(*ma++);
  2799. }
  2800. }
  2801. break;
  2802. case TIFF_SLONG:
  2803. {
  2804. int32_t *ma;
  2805. float *mb;
  2806. uint32_t n;
  2807. ma = (int32_t *)origdata;
  2808. mb = data;
  2809. for (n = 0; n < count; n++)
  2810. {
  2811. if (tif->tif_flags & TIFF_SWAB)
  2812. TIFFSwabLong((uint32_t *)ma);
  2813. *mb++ = (float)(*ma++);
  2814. }
  2815. }
  2816. break;
  2817. case TIFF_LONG8:
  2818. {
  2819. uint64_t *ma;
  2820. float *mb;
  2821. uint32_t n;
  2822. ma = (uint64_t *)origdata;
  2823. mb = data;
  2824. for (n = 0; n < count; n++)
  2825. {
  2826. if (tif->tif_flags & TIFF_SWAB)
  2827. TIFFSwabLong8(ma);
  2828. *mb++ = (float)(*ma++);
  2829. }
  2830. }
  2831. break;
  2832. case TIFF_SLONG8:
  2833. {
  2834. int64_t *ma;
  2835. float *mb;
  2836. uint32_t n;
  2837. ma = (int64_t *)origdata;
  2838. mb = data;
  2839. for (n = 0; n < count; n++)
  2840. {
  2841. if (tif->tif_flags & TIFF_SWAB)
  2842. TIFFSwabLong8((uint64_t *)ma);
  2843. *mb++ = (float)(*ma++);
  2844. }
  2845. }
  2846. break;
  2847. case TIFF_RATIONAL:
  2848. {
  2849. uint32_t *ma;
  2850. uint32_t maa;
  2851. uint32_t mab;
  2852. float *mb;
  2853. uint32_t n;
  2854. ma = (uint32_t *)origdata;
  2855. mb = data;
  2856. for (n = 0; n < count; n++)
  2857. {
  2858. if (tif->tif_flags & TIFF_SWAB)
  2859. TIFFSwabLong(ma);
  2860. maa = *ma++;
  2861. if (tif->tif_flags & TIFF_SWAB)
  2862. TIFFSwabLong(ma);
  2863. mab = *ma++;
  2864. if (mab == 0)
  2865. *mb++ = 0.0;
  2866. else
  2867. *mb++ = (float)maa / (float)mab;
  2868. }
  2869. }
  2870. break;
  2871. case TIFF_SRATIONAL:
  2872. {
  2873. uint32_t *ma;
  2874. int32_t maa;
  2875. uint32_t mab;
  2876. float *mb;
  2877. uint32_t n;
  2878. ma = (uint32_t *)origdata;
  2879. mb = data;
  2880. for (n = 0; n < count; n++)
  2881. {
  2882. if (tif->tif_flags & TIFF_SWAB)
  2883. TIFFSwabLong(ma);
  2884. maa = *(int32_t *)ma;
  2885. ma++;
  2886. if (tif->tif_flags & TIFF_SWAB)
  2887. TIFFSwabLong(ma);
  2888. mab = *ma++;
  2889. if (mab == 0)
  2890. *mb++ = 0.0;
  2891. else
  2892. *mb++ = (float)maa / (float)mab;
  2893. }
  2894. }
  2895. break;
  2896. case TIFF_DOUBLE:
  2897. {
  2898. double *ma;
  2899. float *mb;
  2900. uint32_t n;
  2901. if (tif->tif_flags & TIFF_SWAB)
  2902. TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
  2903. TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
  2904. ma = (double *)origdata;
  2905. mb = data;
  2906. for (n = 0; n < count; n++)
  2907. {
  2908. double val = *ma++;
  2909. if (val > FLT_MAX)
  2910. val = FLT_MAX;
  2911. else if (val < -FLT_MAX)
  2912. val = -FLT_MAX;
  2913. *mb++ = (float)val;
  2914. }
  2915. }
  2916. break;
  2917. }
  2918. _TIFFfreeExt(tif, origdata);
  2919. *value = data;
  2920. return (TIFFReadDirEntryErrOk);
  2921. }
  2922. static enum TIFFReadDirEntryErr
  2923. TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
  2924. {
  2925. enum TIFFReadDirEntryErr err;
  2926. uint32_t count;
  2927. void *origdata;
  2928. double *data;
  2929. switch (direntry->tdir_type)
  2930. {
  2931. case TIFF_BYTE:
  2932. case TIFF_SBYTE:
  2933. case TIFF_SHORT:
  2934. case TIFF_SSHORT:
  2935. case TIFF_LONG:
  2936. case TIFF_SLONG:
  2937. case TIFF_LONG8:
  2938. case TIFF_SLONG8:
  2939. case TIFF_RATIONAL:
  2940. case TIFF_SRATIONAL:
  2941. case TIFF_FLOAT:
  2942. case TIFF_DOUBLE:
  2943. break;
  2944. default:
  2945. return (TIFFReadDirEntryErrType);
  2946. }
  2947. err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
  2948. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  2949. {
  2950. *value = 0;
  2951. return (err);
  2952. }
  2953. switch (direntry->tdir_type)
  2954. {
  2955. case TIFF_DOUBLE:
  2956. if (tif->tif_flags & TIFF_SWAB)
  2957. TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
  2958. TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
  2959. *value = (double *)origdata;
  2960. return (TIFFReadDirEntryErrOk);
  2961. }
  2962. data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
  2963. if (data == 0)
  2964. {
  2965. _TIFFfreeExt(tif, origdata);
  2966. return (TIFFReadDirEntryErrAlloc);
  2967. }
  2968. switch (direntry->tdir_type)
  2969. {
  2970. case TIFF_BYTE:
  2971. {
  2972. uint8_t *ma;
  2973. double *mb;
  2974. uint32_t n;
  2975. ma = (uint8_t *)origdata;
  2976. mb = data;
  2977. for (n = 0; n < count; n++)
  2978. *mb++ = (double)(*ma++);
  2979. }
  2980. break;
  2981. case TIFF_SBYTE:
  2982. {
  2983. int8_t *ma;
  2984. double *mb;
  2985. uint32_t n;
  2986. ma = (int8_t *)origdata;
  2987. mb = data;
  2988. for (n = 0; n < count; n++)
  2989. *mb++ = (double)(*ma++);
  2990. }
  2991. break;
  2992. case TIFF_SHORT:
  2993. {
  2994. uint16_t *ma;
  2995. double *mb;
  2996. uint32_t n;
  2997. ma = (uint16_t *)origdata;
  2998. mb = data;
  2999. for (n = 0; n < count; n++)
  3000. {
  3001. if (tif->tif_flags & TIFF_SWAB)
  3002. TIFFSwabShort(ma);
  3003. *mb++ = (double)(*ma++);
  3004. }
  3005. }
  3006. break;
  3007. case TIFF_SSHORT:
  3008. {
  3009. int16_t *ma;
  3010. double *mb;
  3011. uint32_t n;
  3012. ma = (int16_t *)origdata;
  3013. mb = data;
  3014. for (n = 0; n < count; n++)
  3015. {
  3016. if (tif->tif_flags & TIFF_SWAB)
  3017. TIFFSwabShort((uint16_t *)ma);
  3018. *mb++ = (double)(*ma++);
  3019. }
  3020. }
  3021. break;
  3022. case TIFF_LONG:
  3023. {
  3024. uint32_t *ma;
  3025. double *mb;
  3026. uint32_t n;
  3027. ma = (uint32_t *)origdata;
  3028. mb = data;
  3029. for (n = 0; n < count; n++)
  3030. {
  3031. if (tif->tif_flags & TIFF_SWAB)
  3032. TIFFSwabLong(ma);
  3033. *mb++ = (double)(*ma++);
  3034. }
  3035. }
  3036. break;
  3037. case TIFF_SLONG:
  3038. {
  3039. int32_t *ma;
  3040. double *mb;
  3041. uint32_t n;
  3042. ma = (int32_t *)origdata;
  3043. mb = data;
  3044. for (n = 0; n < count; n++)
  3045. {
  3046. if (tif->tif_flags & TIFF_SWAB)
  3047. TIFFSwabLong((uint32_t *)ma);
  3048. *mb++ = (double)(*ma++);
  3049. }
  3050. }
  3051. break;
  3052. case TIFF_LONG8:
  3053. {
  3054. uint64_t *ma;
  3055. double *mb;
  3056. uint32_t n;
  3057. ma = (uint64_t *)origdata;
  3058. mb = data;
  3059. for (n = 0; n < count; n++)
  3060. {
  3061. if (tif->tif_flags & TIFF_SWAB)
  3062. TIFFSwabLong8(ma);
  3063. *mb++ = (double)(*ma++);
  3064. }
  3065. }
  3066. break;
  3067. case TIFF_SLONG8:
  3068. {
  3069. int64_t *ma;
  3070. double *mb;
  3071. uint32_t n;
  3072. ma = (int64_t *)origdata;
  3073. mb = data;
  3074. for (n = 0; n < count; n++)
  3075. {
  3076. if (tif->tif_flags & TIFF_SWAB)
  3077. TIFFSwabLong8((uint64_t *)ma);
  3078. *mb++ = (double)(*ma++);
  3079. }
  3080. }
  3081. break;
  3082. case TIFF_RATIONAL:
  3083. {
  3084. uint32_t *ma;
  3085. uint32_t maa;
  3086. uint32_t mab;
  3087. double *mb;
  3088. uint32_t n;
  3089. ma = (uint32_t *)origdata;
  3090. mb = data;
  3091. for (n = 0; n < count; n++)
  3092. {
  3093. if (tif->tif_flags & TIFF_SWAB)
  3094. TIFFSwabLong(ma);
  3095. maa = *ma++;
  3096. if (tif->tif_flags & TIFF_SWAB)
  3097. TIFFSwabLong(ma);
  3098. mab = *ma++;
  3099. if (mab == 0)
  3100. *mb++ = 0.0;
  3101. else
  3102. *mb++ = (double)maa / (double)mab;
  3103. }
  3104. }
  3105. break;
  3106. case TIFF_SRATIONAL:
  3107. {
  3108. uint32_t *ma;
  3109. int32_t maa;
  3110. uint32_t mab;
  3111. double *mb;
  3112. uint32_t n;
  3113. ma = (uint32_t *)origdata;
  3114. mb = data;
  3115. for (n = 0; n < count; n++)
  3116. {
  3117. if (tif->tif_flags & TIFF_SWAB)
  3118. TIFFSwabLong(ma);
  3119. maa = *(int32_t *)ma;
  3120. ma++;
  3121. if (tif->tif_flags & TIFF_SWAB)
  3122. TIFFSwabLong(ma);
  3123. mab = *ma++;
  3124. if (mab == 0)
  3125. *mb++ = 0.0;
  3126. else
  3127. *mb++ = (double)maa / (double)mab;
  3128. }
  3129. }
  3130. break;
  3131. case TIFF_FLOAT:
  3132. {
  3133. float *ma;
  3134. double *mb;
  3135. uint32_t n;
  3136. if (tif->tif_flags & TIFF_SWAB)
  3137. TIFFSwabArrayOfLong((uint32_t *)origdata, count);
  3138. TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
  3139. ma = (float *)origdata;
  3140. mb = data;
  3141. for (n = 0; n < count; n++)
  3142. *mb++ = (double)(*ma++);
  3143. }
  3144. break;
  3145. }
  3146. _TIFFfreeExt(tif, origdata);
  3147. *value = data;
  3148. return (TIFFReadDirEntryErrOk);
  3149. }
  3150. static enum TIFFReadDirEntryErr
  3151. TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
  3152. {
  3153. enum TIFFReadDirEntryErr err;
  3154. uint32_t count;
  3155. void *origdata;
  3156. uint64_t *data;
  3157. switch (direntry->tdir_type)
  3158. {
  3159. case TIFF_LONG:
  3160. case TIFF_LONG8:
  3161. case TIFF_IFD:
  3162. case TIFF_IFD8:
  3163. break;
  3164. default:
  3165. return (TIFFReadDirEntryErrType);
  3166. }
  3167. err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
  3168. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  3169. {
  3170. *value = 0;
  3171. return (err);
  3172. }
  3173. switch (direntry->tdir_type)
  3174. {
  3175. case TIFF_LONG8:
  3176. case TIFF_IFD8:
  3177. *value = (uint64_t *)origdata;
  3178. if (tif->tif_flags & TIFF_SWAB)
  3179. TIFFSwabArrayOfLong8(*value, count);
  3180. return (TIFFReadDirEntryErrOk);
  3181. }
  3182. data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
  3183. if (data == 0)
  3184. {
  3185. _TIFFfreeExt(tif, origdata);
  3186. return (TIFFReadDirEntryErrAlloc);
  3187. }
  3188. switch (direntry->tdir_type)
  3189. {
  3190. case TIFF_LONG:
  3191. case TIFF_IFD:
  3192. {
  3193. uint32_t *ma;
  3194. uint64_t *mb;
  3195. uint32_t n;
  3196. ma = (uint32_t *)origdata;
  3197. mb = data;
  3198. for (n = 0; n < count; n++)
  3199. {
  3200. if (tif->tif_flags & TIFF_SWAB)
  3201. TIFFSwabLong(ma);
  3202. *mb++ = (uint64_t)(*ma++);
  3203. }
  3204. }
  3205. break;
  3206. }
  3207. _TIFFfreeExt(tif, origdata);
  3208. *value = data;
  3209. return (TIFFReadDirEntryErrOk);
  3210. }
  3211. static enum TIFFReadDirEntryErr
  3212. TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
  3213. uint16_t *value)
  3214. {
  3215. enum TIFFReadDirEntryErr err;
  3216. uint16_t *m;
  3217. uint16_t *na;
  3218. uint16_t nb;
  3219. if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel)
  3220. return (TIFFReadDirEntryErrCount);
  3221. err = TIFFReadDirEntryShortArray(tif, direntry, &m);
  3222. if (err != TIFFReadDirEntryErrOk || m == NULL)
  3223. return (err);
  3224. na = m;
  3225. nb = tif->tif_dir.td_samplesperpixel;
  3226. *value = *na++;
  3227. nb--;
  3228. while (nb > 0)
  3229. {
  3230. if (*na++ != *value)
  3231. {
  3232. err = TIFFReadDirEntryErrPsdif;
  3233. break;
  3234. }
  3235. nb--;
  3236. }
  3237. _TIFFfreeExt(tif, m);
  3238. return (err);
  3239. }
  3240. static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
  3241. uint8_t *value)
  3242. {
  3243. (void)tif;
  3244. *value = *(uint8_t *)(&direntry->tdir_offset);
  3245. }
  3246. static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
  3247. int8_t *value)
  3248. {
  3249. (void)tif;
  3250. *value = *(int8_t *)(&direntry->tdir_offset);
  3251. }
  3252. static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
  3253. uint16_t *value)
  3254. {
  3255. *value = direntry->tdir_offset.toff_short;
  3256. /* *value=*(uint16_t*)(&direntry->tdir_offset); */
  3257. if (tif->tif_flags & TIFF_SWAB)
  3258. TIFFSwabShort(value);
  3259. }
  3260. static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
  3261. int16_t *value)
  3262. {
  3263. *value = *(int16_t *)(&direntry->tdir_offset);
  3264. if (tif->tif_flags & TIFF_SWAB)
  3265. TIFFSwabShort((uint16_t *)value);
  3266. }
  3267. static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
  3268. uint32_t *value)
  3269. {
  3270. *value = *(uint32_t *)(&direntry->tdir_offset);
  3271. if (tif->tif_flags & TIFF_SWAB)
  3272. TIFFSwabLong(value);
  3273. }
  3274. static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
  3275. int32_t *value)
  3276. {
  3277. *value = *(int32_t *)(&direntry->tdir_offset);
  3278. if (tif->tif_flags & TIFF_SWAB)
  3279. TIFFSwabLong((uint32_t *)value);
  3280. }
  3281. static enum TIFFReadDirEntryErr
  3282. TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
  3283. {
  3284. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3285. {
  3286. enum TIFFReadDirEntryErr err;
  3287. uint32_t offset = direntry->tdir_offset.toff_long;
  3288. if (tif->tif_flags & TIFF_SWAB)
  3289. TIFFSwabLong(&offset);
  3290. err = TIFFReadDirEntryData(tif, offset, 8, value);
  3291. if (err != TIFFReadDirEntryErrOk)
  3292. return (err);
  3293. }
  3294. else
  3295. *value = direntry->tdir_offset.toff_long8;
  3296. if (tif->tif_flags & TIFF_SWAB)
  3297. TIFFSwabLong8(value);
  3298. return (TIFFReadDirEntryErrOk);
  3299. }
  3300. static enum TIFFReadDirEntryErr
  3301. TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
  3302. {
  3303. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3304. {
  3305. enum TIFFReadDirEntryErr err;
  3306. uint32_t offset = direntry->tdir_offset.toff_long;
  3307. if (tif->tif_flags & TIFF_SWAB)
  3308. TIFFSwabLong(&offset);
  3309. err = TIFFReadDirEntryData(tif, offset, 8, value);
  3310. if (err != TIFFReadDirEntryErrOk)
  3311. return (err);
  3312. }
  3313. else
  3314. *value = *(int64_t *)(&direntry->tdir_offset);
  3315. if (tif->tif_flags & TIFF_SWAB)
  3316. TIFFSwabLong8((uint64_t *)value);
  3317. return (TIFFReadDirEntryErrOk);
  3318. }
  3319. static enum TIFFReadDirEntryErr
  3320. TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
  3321. double *value)
  3322. {
  3323. UInt64Aligned_t m;
  3324. assert(sizeof(double) == 8);
  3325. assert(sizeof(uint64_t) == 8);
  3326. assert(sizeof(uint32_t) == 4);
  3327. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3328. {
  3329. enum TIFFReadDirEntryErr err;
  3330. uint32_t offset = direntry->tdir_offset.toff_long;
  3331. if (tif->tif_flags & TIFF_SWAB)
  3332. TIFFSwabLong(&offset);
  3333. err = TIFFReadDirEntryData(tif, offset, 8, m.i);
  3334. if (err != TIFFReadDirEntryErrOk)
  3335. return (err);
  3336. }
  3337. else
  3338. m.l = direntry->tdir_offset.toff_long8;
  3339. if (tif->tif_flags & TIFF_SWAB)
  3340. TIFFSwabArrayOfLong(m.i, 2);
  3341. /* Not completely sure what we should do when m.i[1]==0, but some */
  3342. /* sanitizers do not like division by 0.0: */
  3343. /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
  3344. if (m.i[0] == 0 || m.i[1] == 0)
  3345. *value = 0.0;
  3346. else
  3347. *value = (double)m.i[0] / (double)m.i[1];
  3348. return (TIFFReadDirEntryErrOk);
  3349. }
  3350. static enum TIFFReadDirEntryErr
  3351. TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
  3352. double *value)
  3353. {
  3354. UInt64Aligned_t m;
  3355. assert(sizeof(double) == 8);
  3356. assert(sizeof(uint64_t) == 8);
  3357. assert(sizeof(int32_t) == 4);
  3358. assert(sizeof(uint32_t) == 4);
  3359. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3360. {
  3361. enum TIFFReadDirEntryErr err;
  3362. uint32_t offset = direntry->tdir_offset.toff_long;
  3363. if (tif->tif_flags & TIFF_SWAB)
  3364. TIFFSwabLong(&offset);
  3365. err = TIFFReadDirEntryData(tif, offset, 8, m.i);
  3366. if (err != TIFFReadDirEntryErrOk)
  3367. return (err);
  3368. }
  3369. else
  3370. m.l = direntry->tdir_offset.toff_long8;
  3371. if (tif->tif_flags & TIFF_SWAB)
  3372. TIFFSwabArrayOfLong(m.i, 2);
  3373. /* Not completely sure what we should do when m.i[1]==0, but some */
  3374. /* sanitizers do not like division by 0.0: */
  3375. /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
  3376. if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
  3377. *value = 0.0;
  3378. else
  3379. *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
  3380. return (TIFFReadDirEntryErrOk);
  3381. }
  3382. #if 0
  3383. static enum TIFFReadDirEntryErr
  3384. TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
  3385. TIFFRational_t *value)
  3386. { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
  3387. directly --*/
  3388. UInt64Aligned_t m;
  3389. assert(sizeof(double) == 8);
  3390. assert(sizeof(uint64_t) == 8);
  3391. assert(sizeof(uint32_t) == 4);
  3392. if (direntry->tdir_count != 1)
  3393. return (TIFFReadDirEntryErrCount);
  3394. if (direntry->tdir_type != TIFF_RATIONAL &&
  3395. direntry->tdir_type != TIFF_SRATIONAL)
  3396. return (TIFFReadDirEntryErrType);
  3397. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3398. {
  3399. enum TIFFReadDirEntryErr err;
  3400. uint32_t offset = direntry->tdir_offset.toff_long;
  3401. if (tif->tif_flags & TIFF_SWAB)
  3402. TIFFSwabLong(&offset);
  3403. err = TIFFReadDirEntryData(tif, offset, 8, m.i);
  3404. if (err != TIFFReadDirEntryErrOk)
  3405. return (err);
  3406. }
  3407. else
  3408. {
  3409. m.l = direntry->tdir_offset.toff_long8;
  3410. }
  3411. if (tif->tif_flags & TIFF_SWAB)
  3412. TIFFSwabArrayOfLong(m.i, 2);
  3413. value->uNum = m.i[0];
  3414. value->uDenom = m.i[1];
  3415. return (TIFFReadDirEntryErrOk);
  3416. } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
  3417. #endif
  3418. static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
  3419. float *value)
  3420. {
  3421. union
  3422. {
  3423. float f;
  3424. uint32_t i;
  3425. } float_union;
  3426. assert(sizeof(float) == 4);
  3427. assert(sizeof(uint32_t) == 4);
  3428. assert(sizeof(float_union) == 4);
  3429. float_union.i = *(uint32_t *)(&direntry->tdir_offset);
  3430. *value = float_union.f;
  3431. if (tif->tif_flags & TIFF_SWAB)
  3432. TIFFSwabLong((uint32_t *)value);
  3433. }
  3434. static enum TIFFReadDirEntryErr
  3435. TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
  3436. {
  3437. assert(sizeof(double) == 8);
  3438. assert(sizeof(uint64_t) == 8);
  3439. assert(sizeof(UInt64Aligned_t) == 8);
  3440. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3441. {
  3442. enum TIFFReadDirEntryErr err;
  3443. uint32_t offset = direntry->tdir_offset.toff_long;
  3444. if (tif->tif_flags & TIFF_SWAB)
  3445. TIFFSwabLong(&offset);
  3446. err = TIFFReadDirEntryData(tif, offset, 8, value);
  3447. if (err != TIFFReadDirEntryErrOk)
  3448. return (err);
  3449. }
  3450. else
  3451. {
  3452. UInt64Aligned_t uint64_union;
  3453. uint64_union.l = direntry->tdir_offset.toff_long8;
  3454. *value = uint64_union.d;
  3455. }
  3456. if (tif->tif_flags & TIFF_SWAB)
  3457. TIFFSwabLong8((uint64_t *)value);
  3458. return (TIFFReadDirEntryErrOk);
  3459. }
  3460. static enum TIFFReadDirEntryErr
  3461. TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
  3462. {
  3463. if (value < 0)
  3464. return (TIFFReadDirEntryErrRange);
  3465. else
  3466. return (TIFFReadDirEntryErrOk);
  3467. }
  3468. static enum TIFFReadDirEntryErr
  3469. TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
  3470. {
  3471. if (value > 0xFF)
  3472. return (TIFFReadDirEntryErrRange);
  3473. else
  3474. return (TIFFReadDirEntryErrOk);
  3475. }
  3476. static enum TIFFReadDirEntryErr
  3477. TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
  3478. {
  3479. if ((value < 0) || (value > 0xFF))
  3480. return (TIFFReadDirEntryErrRange);
  3481. else
  3482. return (TIFFReadDirEntryErrOk);
  3483. }
  3484. static enum TIFFReadDirEntryErr
  3485. TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
  3486. {
  3487. if (value > 0xFF)
  3488. return (TIFFReadDirEntryErrRange);
  3489. else
  3490. return (TIFFReadDirEntryErrOk);
  3491. }
  3492. static enum TIFFReadDirEntryErr
  3493. TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
  3494. {
  3495. if ((value < 0) || (value > 0xFF))
  3496. return (TIFFReadDirEntryErrRange);
  3497. else
  3498. return (TIFFReadDirEntryErrOk);
  3499. }
  3500. static enum TIFFReadDirEntryErr
  3501. TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
  3502. {
  3503. if (value > 0xFF)
  3504. return (TIFFReadDirEntryErrRange);
  3505. else
  3506. return (TIFFReadDirEntryErrOk);
  3507. }
  3508. static enum TIFFReadDirEntryErr
  3509. TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
  3510. {
  3511. if ((value < 0) || (value > 0xFF))
  3512. return (TIFFReadDirEntryErrRange);
  3513. else
  3514. return (TIFFReadDirEntryErrOk);
  3515. }
  3516. static enum TIFFReadDirEntryErr
  3517. TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
  3518. {
  3519. if (value > 0x7F)
  3520. return (TIFFReadDirEntryErrRange);
  3521. else
  3522. return (TIFFReadDirEntryErrOk);
  3523. }
  3524. static enum TIFFReadDirEntryErr
  3525. TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
  3526. {
  3527. if (value > 0x7F)
  3528. return (TIFFReadDirEntryErrRange);
  3529. else
  3530. return (TIFFReadDirEntryErrOk);
  3531. }
  3532. static enum TIFFReadDirEntryErr
  3533. TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
  3534. {
  3535. if ((value < -0x80) || (value > 0x7F))
  3536. return (TIFFReadDirEntryErrRange);
  3537. else
  3538. return (TIFFReadDirEntryErrOk);
  3539. }
  3540. static enum TIFFReadDirEntryErr
  3541. TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
  3542. {
  3543. if (value > 0x7F)
  3544. return (TIFFReadDirEntryErrRange);
  3545. else
  3546. return (TIFFReadDirEntryErrOk);
  3547. }
  3548. static enum TIFFReadDirEntryErr
  3549. TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
  3550. {
  3551. if ((value < -0x80) || (value > 0x7F))
  3552. return (TIFFReadDirEntryErrRange);
  3553. else
  3554. return (TIFFReadDirEntryErrOk);
  3555. }
  3556. static enum TIFFReadDirEntryErr
  3557. TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
  3558. {
  3559. if (value > 0x7F)
  3560. return (TIFFReadDirEntryErrRange);
  3561. else
  3562. return (TIFFReadDirEntryErrOk);
  3563. }
  3564. static enum TIFFReadDirEntryErr
  3565. TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
  3566. {
  3567. if ((value < -0x80) || (value > 0x7F))
  3568. return (TIFFReadDirEntryErrRange);
  3569. else
  3570. return (TIFFReadDirEntryErrOk);
  3571. }
  3572. static enum TIFFReadDirEntryErr
  3573. TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
  3574. {
  3575. if (value < 0)
  3576. return (TIFFReadDirEntryErrRange);
  3577. else
  3578. return (TIFFReadDirEntryErrOk);
  3579. }
  3580. static enum TIFFReadDirEntryErr
  3581. TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
  3582. {
  3583. if (value < 0)
  3584. return (TIFFReadDirEntryErrRange);
  3585. else
  3586. return (TIFFReadDirEntryErrOk);
  3587. }
  3588. static enum TIFFReadDirEntryErr
  3589. TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
  3590. {
  3591. if (value > 0xFFFF)
  3592. return (TIFFReadDirEntryErrRange);
  3593. else
  3594. return (TIFFReadDirEntryErrOk);
  3595. }
  3596. static enum TIFFReadDirEntryErr
  3597. TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
  3598. {
  3599. if ((value < 0) || (value > 0xFFFF))
  3600. return (TIFFReadDirEntryErrRange);
  3601. else
  3602. return (TIFFReadDirEntryErrOk);
  3603. }
  3604. static enum TIFFReadDirEntryErr
  3605. TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
  3606. {
  3607. if (value > 0xFFFF)
  3608. return (TIFFReadDirEntryErrRange);
  3609. else
  3610. return (TIFFReadDirEntryErrOk);
  3611. }
  3612. static enum TIFFReadDirEntryErr
  3613. TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
  3614. {
  3615. if ((value < 0) || (value > 0xFFFF))
  3616. return (TIFFReadDirEntryErrRange);
  3617. else
  3618. return (TIFFReadDirEntryErrOk);
  3619. }
  3620. static enum TIFFReadDirEntryErr
  3621. TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
  3622. {
  3623. if (value > 0x7FFF)
  3624. return (TIFFReadDirEntryErrRange);
  3625. else
  3626. return (TIFFReadDirEntryErrOk);
  3627. }
  3628. static enum TIFFReadDirEntryErr
  3629. TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
  3630. {
  3631. if (value > 0x7FFF)
  3632. return (TIFFReadDirEntryErrRange);
  3633. else
  3634. return (TIFFReadDirEntryErrOk);
  3635. }
  3636. static enum TIFFReadDirEntryErr
  3637. TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
  3638. {
  3639. if ((value < -0x8000) || (value > 0x7FFF))
  3640. return (TIFFReadDirEntryErrRange);
  3641. else
  3642. return (TIFFReadDirEntryErrOk);
  3643. }
  3644. static enum TIFFReadDirEntryErr
  3645. TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
  3646. {
  3647. if (value > 0x7FFF)
  3648. return (TIFFReadDirEntryErrRange);
  3649. else
  3650. return (TIFFReadDirEntryErrOk);
  3651. }
  3652. static enum TIFFReadDirEntryErr
  3653. TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
  3654. {
  3655. if ((value < -0x8000) || (value > 0x7FFF))
  3656. return (TIFFReadDirEntryErrRange);
  3657. else
  3658. return (TIFFReadDirEntryErrOk);
  3659. }
  3660. static enum TIFFReadDirEntryErr
  3661. TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
  3662. {
  3663. if (value < 0)
  3664. return (TIFFReadDirEntryErrRange);
  3665. else
  3666. return (TIFFReadDirEntryErrOk);
  3667. }
  3668. static enum TIFFReadDirEntryErr
  3669. TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
  3670. {
  3671. if (value < 0)
  3672. return (TIFFReadDirEntryErrRange);
  3673. else
  3674. return (TIFFReadDirEntryErrOk);
  3675. }
  3676. static enum TIFFReadDirEntryErr
  3677. TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
  3678. {
  3679. if (value < 0)
  3680. return (TIFFReadDirEntryErrRange);
  3681. else
  3682. return (TIFFReadDirEntryErrOk);
  3683. }
  3684. static enum TIFFReadDirEntryErr
  3685. TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
  3686. {
  3687. if (value > UINT32_MAX)
  3688. return (TIFFReadDirEntryErrRange);
  3689. else
  3690. return (TIFFReadDirEntryErrOk);
  3691. }
  3692. static enum TIFFReadDirEntryErr
  3693. TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
  3694. {
  3695. if ((value < 0) || (value > (int64_t)UINT32_MAX))
  3696. return (TIFFReadDirEntryErrRange);
  3697. else
  3698. return (TIFFReadDirEntryErrOk);
  3699. }
  3700. static enum TIFFReadDirEntryErr
  3701. TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
  3702. {
  3703. if (value > 0x7FFFFFFFUL)
  3704. return (TIFFReadDirEntryErrRange);
  3705. else
  3706. return (TIFFReadDirEntryErrOk);
  3707. }
  3708. /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
  3709. static enum TIFFReadDirEntryErr
  3710. TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
  3711. {
  3712. if (value > 0x7FFFFFFF)
  3713. return (TIFFReadDirEntryErrRange);
  3714. else
  3715. return (TIFFReadDirEntryErrOk);
  3716. }
  3717. /* Check that the 8-byte signed value can fit in a 4-byte signed range */
  3718. static enum TIFFReadDirEntryErr
  3719. TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
  3720. {
  3721. if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
  3722. return (TIFFReadDirEntryErrRange);
  3723. else
  3724. return (TIFFReadDirEntryErrOk);
  3725. }
  3726. static enum TIFFReadDirEntryErr
  3727. TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
  3728. {
  3729. if (value < 0)
  3730. return (TIFFReadDirEntryErrRange);
  3731. else
  3732. return (TIFFReadDirEntryErrOk);
  3733. }
  3734. static enum TIFFReadDirEntryErr
  3735. TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
  3736. {
  3737. if (value < 0)
  3738. return (TIFFReadDirEntryErrRange);
  3739. else
  3740. return (TIFFReadDirEntryErrOk);
  3741. }
  3742. static enum TIFFReadDirEntryErr
  3743. TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
  3744. {
  3745. if (value < 0)
  3746. return (TIFFReadDirEntryErrRange);
  3747. else
  3748. return (TIFFReadDirEntryErrOk);
  3749. }
  3750. static enum TIFFReadDirEntryErr
  3751. TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
  3752. {
  3753. if (value < 0)
  3754. return (TIFFReadDirEntryErrRange);
  3755. else
  3756. return (TIFFReadDirEntryErrOk);
  3757. }
  3758. static enum TIFFReadDirEntryErr
  3759. TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
  3760. {
  3761. if (value > INT64_MAX)
  3762. return (TIFFReadDirEntryErrRange);
  3763. else
  3764. return (TIFFReadDirEntryErrOk);
  3765. }
  3766. static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
  3767. tmsize_t size, void *dest)
  3768. {
  3769. assert(size > 0);
  3770. if (!isMapped(tif))
  3771. {
  3772. if (!SeekOK(tif, offset))
  3773. return (TIFFReadDirEntryErrIo);
  3774. if (!ReadOK(tif, dest, size))
  3775. return (TIFFReadDirEntryErrIo);
  3776. }
  3777. else
  3778. {
  3779. size_t ma, mb;
  3780. ma = (size_t)offset;
  3781. if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
  3782. {
  3783. return TIFFReadDirEntryErrIo;
  3784. }
  3785. mb = ma + size;
  3786. if (mb > (uint64_t)tif->tif_size)
  3787. return (TIFFReadDirEntryErrIo);
  3788. _TIFFmemcpy(dest, tif->tif_base + ma, size);
  3789. }
  3790. return (TIFFReadDirEntryErrOk);
  3791. }
  3792. static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
  3793. const char *module, const char *tagname,
  3794. int recover)
  3795. {
  3796. if (!recover)
  3797. {
  3798. switch (err)
  3799. {
  3800. case TIFFReadDirEntryErrCount:
  3801. TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
  3802. tagname);
  3803. break;
  3804. case TIFFReadDirEntryErrType:
  3805. TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
  3806. tagname);
  3807. break;
  3808. case TIFFReadDirEntryErrIo:
  3809. TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
  3810. tagname);
  3811. break;
  3812. case TIFFReadDirEntryErrRange:
  3813. TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
  3814. tagname);
  3815. break;
  3816. case TIFFReadDirEntryErrPsdif:
  3817. TIFFErrorExtR(
  3818. tif, module,
  3819. "Cannot handle different values per sample for \"%s\"",
  3820. tagname);
  3821. break;
  3822. case TIFFReadDirEntryErrSizesan:
  3823. TIFFErrorExtR(tif, module,
  3824. "Sanity check on size of \"%s\" value failed",
  3825. tagname);
  3826. break;
  3827. case TIFFReadDirEntryErrAlloc:
  3828. TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
  3829. tagname);
  3830. break;
  3831. default:
  3832. assert(0); /* we should never get here */
  3833. break;
  3834. }
  3835. }
  3836. else
  3837. {
  3838. switch (err)
  3839. {
  3840. case TIFFReadDirEntryErrCount:
  3841. TIFFWarningExtR(tif, module,
  3842. "Incorrect count for \"%s\"; tag ignored",
  3843. tagname);
  3844. break;
  3845. case TIFFReadDirEntryErrType:
  3846. TIFFWarningExtR(tif, module,
  3847. "Incompatible type for \"%s\"; tag ignored",
  3848. tagname);
  3849. break;
  3850. case TIFFReadDirEntryErrIo:
  3851. TIFFWarningExtR(
  3852. tif, module,
  3853. "IO error during reading of \"%s\"; tag ignored", tagname);
  3854. break;
  3855. case TIFFReadDirEntryErrRange:
  3856. TIFFWarningExtR(tif, module,
  3857. "Incorrect value for \"%s\"; tag ignored",
  3858. tagname);
  3859. break;
  3860. case TIFFReadDirEntryErrPsdif:
  3861. TIFFWarningExtR(tif, module,
  3862. "Cannot handle different values per sample for "
  3863. "\"%s\"; tag ignored",
  3864. tagname);
  3865. break;
  3866. case TIFFReadDirEntryErrSizesan:
  3867. TIFFWarningExtR(
  3868. tif, module,
  3869. "Sanity check on size of \"%s\" value failed; tag ignored",
  3870. tagname);
  3871. break;
  3872. case TIFFReadDirEntryErrAlloc:
  3873. TIFFWarningExtR(tif, module,
  3874. "Out of memory reading of \"%s\"; tag ignored",
  3875. tagname);
  3876. break;
  3877. default:
  3878. assert(0); /* we should never get here */
  3879. break;
  3880. }
  3881. }
  3882. }
  3883. /*
  3884. * Return the maximum number of color channels specified for a given photometric
  3885. * type. 0 is returned if photometric type isn't supported or no default value
  3886. * is defined by the specification.
  3887. */
  3888. static int _TIFFGetMaxColorChannels(uint16_t photometric)
  3889. {
  3890. switch (photometric)
  3891. {
  3892. case PHOTOMETRIC_PALETTE:
  3893. case PHOTOMETRIC_MINISWHITE:
  3894. case PHOTOMETRIC_MINISBLACK:
  3895. return 1;
  3896. case PHOTOMETRIC_YCBCR:
  3897. case PHOTOMETRIC_RGB:
  3898. case PHOTOMETRIC_CIELAB:
  3899. case PHOTOMETRIC_LOGLUV:
  3900. case PHOTOMETRIC_ITULAB:
  3901. case PHOTOMETRIC_ICCLAB:
  3902. return 3;
  3903. case PHOTOMETRIC_SEPARATED:
  3904. case PHOTOMETRIC_MASK:
  3905. return 4;
  3906. case PHOTOMETRIC_LOGL:
  3907. case PHOTOMETRIC_CFA:
  3908. default:
  3909. return 0;
  3910. }
  3911. }
  3912. static int ByteCountLooksBad(TIFF *tif)
  3913. {
  3914. /*
  3915. * Assume we have wrong StripByteCount value (in case
  3916. * of single strip) in following cases:
  3917. * - it is equal to zero along with StripOffset;
  3918. * - it is larger than file itself (in case of uncompressed
  3919. * image);
  3920. * - it is smaller than the size of the bytes per row
  3921. * multiplied on the number of rows. The last case should
  3922. * not be checked in the case of writing new image,
  3923. * because we may do not know the exact strip size
  3924. * until the whole image will be written and directory
  3925. * dumped out.
  3926. */
  3927. uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
  3928. uint64_t offset = TIFFGetStrileOffset(tif, 0);
  3929. uint64_t filesize;
  3930. if (offset == 0)
  3931. return 0;
  3932. if (bytecount == 0)
  3933. return 1;
  3934. if (tif->tif_dir.td_compression != COMPRESSION_NONE)
  3935. return 0;
  3936. filesize = TIFFGetFileSize(tif);
  3937. if (offset <= filesize && bytecount > filesize - offset)
  3938. return 1;
  3939. if (tif->tif_mode == O_RDONLY)
  3940. {
  3941. uint64_t scanlinesize = TIFFScanlineSize64(tif);
  3942. if (tif->tif_dir.td_imagelength > 0 &&
  3943. scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
  3944. {
  3945. return 1;
  3946. }
  3947. if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
  3948. return 1;
  3949. }
  3950. return 0;
  3951. }
  3952. /*
  3953. * To evaluate the IFD data size when reading, save the offset and data size of
  3954. * all data that does not fit into the IFD entries themselves.
  3955. */
  3956. static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
  3957. {
  3958. const uint64_t data_width = TIFFDataWidth(dp->tdir_type);
  3959. if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
  3960. {
  3961. TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
  3962. "Too large IFD data size");
  3963. return false;
  3964. }
  3965. const uint64_t datalength = dp->tdir_count * data_width;
  3966. if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
  3967. {
  3968. if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
  3969. {
  3970. TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
  3971. "Too large IFD data size");
  3972. return false;
  3973. }
  3974. tif->tif_dir.td_dirdatasize_read += datalength;
  3975. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3976. {
  3977. /* The offset of TIFFDirEntry are not swapped when read in. That has
  3978. * to be done when used. */
  3979. uint32_t offset = dp->tdir_offset.toff_long;
  3980. if (tif->tif_flags & TIFF_SWAB)
  3981. TIFFSwabLong(&offset);
  3982. tif->tif_dir
  3983. .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
  3984. .offset = (uint64_t)offset;
  3985. }
  3986. else
  3987. {
  3988. tif->tif_dir
  3989. .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
  3990. .offset = dp->tdir_offset.toff_long8;
  3991. if (tif->tif_flags & TIFF_SWAB)
  3992. TIFFSwabLong8(
  3993. &tif->tif_dir
  3994. .td_dirdatasize_offsets[tif->tif_dir
  3995. .td_dirdatasize_Noffsets]
  3996. .offset);
  3997. }
  3998. tif->tif_dir
  3999. .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
  4000. .length = datalength;
  4001. tif->tif_dir.td_dirdatasize_Noffsets++;
  4002. }
  4003. return true;
  4004. }
  4005. /*
  4006. * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
  4007. */
  4008. static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
  4009. {
  4010. const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
  4011. const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
  4012. /* Compare offsets */
  4013. if (ta->offset > tb->offset)
  4014. return 1;
  4015. else if (ta->offset < tb->offset)
  4016. return -1;
  4017. else
  4018. return 0;
  4019. }
  4020. /*
  4021. * Determine the IFD data size after reading an IFD from the file that can be
  4022. * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size
  4023. * includes the IFD entries themselves as well as the data that does not fit
  4024. * directly into the IFD entries but is located directly after the IFD entries
  4025. * in the file.
  4026. */
  4027. static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
  4028. {
  4029. /* IFD data size is only needed if file-writing is enabled.
  4030. * This also avoids the seek() to EOF to determine the file size, which
  4031. * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */
  4032. if (tif->tif_mode == O_RDONLY)
  4033. return;
  4034. /* Sort TIFFEntryOffsetAndLength array in ascending order. */
  4035. qsort(tif->tif_dir.td_dirdatasize_offsets,
  4036. tif->tif_dir.td_dirdatasize_Noffsets,
  4037. sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength);
  4038. /* Get offset of end of IFD entry space. */
  4039. uint64_t IFDendoffset;
  4040. if (!(tif->tif_flags & TIFF_BIGTIFF))
  4041. IFDendoffset = tif->tif_diroff + 2 + dircount * 12 + 4;
  4042. else
  4043. IFDendoffset = tif->tif_diroff + 8 + dircount * 20 + 8;
  4044. /* Check which offsets are right behind IFD entries. However, LibTIFF
  4045. * increments the writing address for every external data to an even offset.
  4046. * Thus gaps of 1 byte can occur. */
  4047. uint64_t size = 0;
  4048. uint64_t offset;
  4049. uint32_t i;
  4050. for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
  4051. {
  4052. offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
  4053. if (offset == IFDendoffset)
  4054. {
  4055. size += tif->tif_dir.td_dirdatasize_offsets[i].length;
  4056. IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
  4057. }
  4058. else if (offset == IFDendoffset + 1)
  4059. {
  4060. /* Add gap byte after previous IFD data set. */
  4061. size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
  4062. IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
  4063. }
  4064. else
  4065. {
  4066. /* Further data is no more continuously after IFD */
  4067. break;
  4068. }
  4069. }
  4070. /* Check for gap byte of some easy cases. This should cover 90% of cases.
  4071. * Otherwise, IFD will be re-written even it might be safely overwritten. */
  4072. if (tif->tif_nextdiroff != 0)
  4073. {
  4074. if (tif->tif_nextdiroff == IFDendoffset + 1)
  4075. size++;
  4076. }
  4077. else
  4078. {
  4079. /* Check for IFD data ends at EOF. Then IFD can always be safely
  4080. * overwritten. */
  4081. offset = TIFFSeekFile(tif, 0, SEEK_END);
  4082. if (offset == IFDendoffset)
  4083. {
  4084. tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
  4085. return;
  4086. }
  4087. }
  4088. /* Finally, add the size of the IFD tag entries themselves. */
  4089. if (!(tif->tif_flags & TIFF_BIGTIFF))
  4090. tif->tif_dir.td_dirdatasize_read = 2 + dircount * 12 + 4 + size;
  4091. else
  4092. tif->tif_dir.td_dirdatasize_read = 8 + dircount * 20 + 8 + size;
  4093. } /*-- CalcFinalIFDdatasizeReading() --*/
  4094. /*
  4095. * Read the next TIFF directory from a file and convert it to the internal
  4096. * format. We read directories sequentially.
  4097. */
  4098. int TIFFReadDirectory(TIFF *tif)
  4099. {
  4100. static const char module[] = "TIFFReadDirectory";
  4101. TIFFDirEntry *dir;
  4102. uint16_t dircount;
  4103. TIFFDirEntry *dp;
  4104. uint16_t di;
  4105. const TIFFField *fip;
  4106. uint32_t fii = FAILED_FII;
  4107. toff_t nextdiroff;
  4108. int bitspersample_read = FALSE;
  4109. int color_channels;
  4110. if (tif->tif_nextdiroff == 0)
  4111. {
  4112. /* In this special case, tif_diroff needs also to be set to 0.
  4113. * This is behind the last IFD, thus no checking or reading necessary.
  4114. */
  4115. tif->tif_diroff = tif->tif_nextdiroff;
  4116. return 0;
  4117. }
  4118. nextdiroff = tif->tif_nextdiroff;
  4119. /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
  4120. * reading of the directory. Otherwise, invalid IFD offsets could corrupt
  4121. * the IFD list. */
  4122. if (!_TIFFCheckDirNumberAndOffset(tif,
  4123. tif->tif_curdir ==
  4124. TIFF_NON_EXISTENT_DIR_NUMBER
  4125. ? 0
  4126. : tif->tif_curdir + 1,
  4127. nextdiroff))
  4128. {
  4129. return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
  4130. IFDs) */
  4131. }
  4132. dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
  4133. if (!dircount)
  4134. {
  4135. TIFFErrorExtR(tif, module,
  4136. "Failed to read directory at offset %" PRIu64,
  4137. nextdiroff);
  4138. return 0;
  4139. }
  4140. /* Set global values after a valid directory has been fetched.
  4141. * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
  4142. * beginning. */
  4143. if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
  4144. tif->tif_curdir = 0;
  4145. else
  4146. tif->tif_curdir++;
  4147. TIFFReadDirectoryCheckOrder(tif, dir, dircount);
  4148. /*
  4149. * Mark duplicates of any tag to be ignored (bugzilla 1994)
  4150. * to avoid certain pathological problems.
  4151. */
  4152. {
  4153. TIFFDirEntry *ma;
  4154. uint16_t mb;
  4155. for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
  4156. {
  4157. TIFFDirEntry *na;
  4158. uint16_t nb;
  4159. for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
  4160. {
  4161. if (ma->tdir_tag == na->tdir_tag)
  4162. {
  4163. na->tdir_ignore = TRUE;
  4164. }
  4165. }
  4166. }
  4167. }
  4168. tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
  4169. tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
  4170. tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
  4171. /* free any old stuff and reinit */
  4172. TIFFFreeDirectory(tif);
  4173. TIFFDefaultDirectory(tif);
  4174. /* After setup a fresh directory indicate that now active IFD is also
  4175. * present on file, even if its entries could not be read successfully
  4176. * below. */
  4177. tif->tif_dir.td_iswrittentofile = TRUE;
  4178. /* Allocate arrays for offset values outside IFD entry for IFD data size
  4179. * checking. Note: Counter are reset within TIFFFreeDirectory(). */
  4180. tif->tif_dir.td_dirdatasize_offsets =
  4181. (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
  4182. tif, dircount * sizeof(TIFFEntryOffsetAndLength));
  4183. if (tif->tif_dir.td_dirdatasize_offsets == NULL)
  4184. {
  4185. TIFFErrorExtR(
  4186. tif, module,
  4187. "Failed to allocate memory for counting IFD data size at reading");
  4188. goto bad;
  4189. }
  4190. /*
  4191. * Electronic Arts writes gray-scale TIFF files
  4192. * without a PlanarConfiguration directory entry.
  4193. * Thus we setup a default value here, even though
  4194. * the TIFF spec says there is no default value.
  4195. * After PlanarConfiguration is preset in TIFFDefaultDirectory()
  4196. * the following setting is not needed, but does not harm either.
  4197. */
  4198. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  4199. /*
  4200. * Setup default value and then make a pass over
  4201. * the fields to check type and tag information,
  4202. * and to extract info required to size data
  4203. * structures. A second pass is made afterwards
  4204. * to read in everything not taken in the first pass.
  4205. * But we must process the Compression tag first
  4206. * in order to merge in codec-private tag definitions (otherwise
  4207. * we may get complaints about unknown tags). However, the
  4208. * Compression tag may be dependent on the SamplesPerPixel
  4209. * tag value because older TIFF specs permitted Compression
  4210. * to be written as a SamplesPerPixel-count tag entry.
  4211. * Thus if we don't first figure out the correct SamplesPerPixel
  4212. * tag value then we may end up ignoring the Compression tag
  4213. * value because it has an incorrect count value (if the
  4214. * true value of SamplesPerPixel is not 1).
  4215. */
  4216. dp =
  4217. TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
  4218. if (dp)
  4219. {
  4220. if (!TIFFFetchNormalTag(tif, dp, 0))
  4221. goto bad;
  4222. dp->tdir_ignore = TRUE;
  4223. }
  4224. dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
  4225. if (dp)
  4226. {
  4227. /*
  4228. * The 5.0 spec says the Compression tag has one value, while
  4229. * earlier specs say it has one value per sample. Because of
  4230. * this, we accept the tag if one value is supplied with either
  4231. * count.
  4232. */
  4233. uint16_t value;
  4234. enum TIFFReadDirEntryErr err;
  4235. err = TIFFReadDirEntryShort(tif, dp, &value);
  4236. if (err == TIFFReadDirEntryErrCount)
  4237. err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
  4238. if (err != TIFFReadDirEntryErrOk)
  4239. {
  4240. TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
  4241. goto bad;
  4242. }
  4243. if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
  4244. goto bad;
  4245. dp->tdir_ignore = TRUE;
  4246. }
  4247. else
  4248. {
  4249. if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
  4250. goto bad;
  4251. }
  4252. /*
  4253. * First real pass over the directory.
  4254. */
  4255. for (di = 0, dp = dir; di < dircount; di++, dp++)
  4256. {
  4257. if (!dp->tdir_ignore)
  4258. {
  4259. TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
  4260. if (fii == FAILED_FII)
  4261. {
  4262. if (tif->tif_warn_about_unknown_tags)
  4263. {
  4264. TIFFWarningExtR(tif, module,
  4265. "Unknown field with tag %" PRIu16
  4266. " (0x%" PRIx16 ") encountered",
  4267. dp->tdir_tag, dp->tdir_tag);
  4268. }
  4269. /* the following knowingly leaks the
  4270. anonymous field structure */
  4271. const TIFFField *fld = _TIFFCreateAnonField(
  4272. tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
  4273. if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
  4274. {
  4275. TIFFWarningExtR(
  4276. tif, module,
  4277. "Registering anonymous field with tag %" PRIu16
  4278. " (0x%" PRIx16 ") failed",
  4279. dp->tdir_tag, dp->tdir_tag);
  4280. dp->tdir_ignore = TRUE;
  4281. }
  4282. else
  4283. {
  4284. TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
  4285. assert(fii != FAILED_FII);
  4286. }
  4287. }
  4288. }
  4289. if (!dp->tdir_ignore)
  4290. {
  4291. fip = tif->tif_fields[fii];
  4292. if (fip->field_bit == FIELD_IGNORE)
  4293. dp->tdir_ignore = TRUE;
  4294. else
  4295. {
  4296. switch (dp->tdir_tag)
  4297. {
  4298. case TIFFTAG_STRIPOFFSETS:
  4299. case TIFFTAG_STRIPBYTECOUNTS:
  4300. case TIFFTAG_TILEOFFSETS:
  4301. case TIFFTAG_TILEBYTECOUNTS:
  4302. TIFFSetFieldBit(tif, fip->field_bit);
  4303. break;
  4304. case TIFFTAG_IMAGEWIDTH:
  4305. case TIFFTAG_IMAGELENGTH:
  4306. case TIFFTAG_IMAGEDEPTH:
  4307. case TIFFTAG_TILELENGTH:
  4308. case TIFFTAG_TILEWIDTH:
  4309. case TIFFTAG_TILEDEPTH:
  4310. case TIFFTAG_PLANARCONFIG:
  4311. case TIFFTAG_ROWSPERSTRIP:
  4312. case TIFFTAG_EXTRASAMPLES:
  4313. if (!TIFFFetchNormalTag(tif, dp, 0))
  4314. goto bad;
  4315. dp->tdir_ignore = TRUE;
  4316. break;
  4317. default:
  4318. if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
  4319. dp->tdir_ignore = TRUE;
  4320. break;
  4321. }
  4322. }
  4323. }
  4324. }
  4325. /*
  4326. * XXX: OJPEG hack.
  4327. * If a) compression is OJPEG, b) planarconfig tag says it's separate,
  4328. * c) strip offsets/bytecounts tag are both present and
  4329. * d) both contain exactly one value, then we consistently find
  4330. * that the buggy implementation of the buggy compression scheme
  4331. * matches contig planarconfig best. So we 'fix-up' the tag here
  4332. */
  4333. if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
  4334. (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
  4335. {
  4336. if (!_TIFFFillStriles(tif))
  4337. goto bad;
  4338. dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
  4339. TIFFTAG_STRIPOFFSETS);
  4340. if ((dp != 0) && (dp->tdir_count == 1))
  4341. {
  4342. dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
  4343. TIFFTAG_STRIPBYTECOUNTS);
  4344. if ((dp != 0) && (dp->tdir_count == 1))
  4345. {
  4346. tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
  4347. TIFFWarningExtR(tif, module,
  4348. "Planarconfig tag value assumed incorrect, "
  4349. "assuming data is contig instead of chunky");
  4350. }
  4351. }
  4352. }
  4353. /*
  4354. * Allocate directory structure and setup defaults.
  4355. */
  4356. if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
  4357. {
  4358. MissingRequired(tif, "ImageLength");
  4359. goto bad;
  4360. }
  4361. /*
  4362. * Second pass: extract other information.
  4363. */
  4364. for (di = 0, dp = dir; di < dircount; di++, dp++)
  4365. {
  4366. if (!dp->tdir_ignore)
  4367. {
  4368. switch (dp->tdir_tag)
  4369. {
  4370. case TIFFTAG_MINSAMPLEVALUE:
  4371. case TIFFTAG_MAXSAMPLEVALUE:
  4372. case TIFFTAG_BITSPERSAMPLE:
  4373. case TIFFTAG_DATATYPE:
  4374. case TIFFTAG_SAMPLEFORMAT:
  4375. /*
  4376. * The MinSampleValue, MaxSampleValue, BitsPerSample
  4377. * DataType and SampleFormat tags are supposed to be
  4378. * written as one value/sample, but some vendors
  4379. * incorrectly write one value only -- so we accept
  4380. * that as well (yuck). Other vendors write correct
  4381. * value for NumberOfSamples, but incorrect one for
  4382. * BitsPerSample and friends, and we will read this
  4383. * too.
  4384. */
  4385. {
  4386. uint16_t value;
  4387. enum TIFFReadDirEntryErr err;
  4388. err = TIFFReadDirEntryShort(tif, dp, &value);
  4389. if (!EvaluateIFDdatasizeReading(tif, dp))
  4390. goto bad;
  4391. if (err == TIFFReadDirEntryErrCount)
  4392. err =
  4393. TIFFReadDirEntryPersampleShort(tif, dp, &value);
  4394. if (err != TIFFReadDirEntryErrOk)
  4395. {
  4396. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4397. TIFFReadDirEntryOutputErr(
  4398. tif, err, module,
  4399. fip ? fip->field_name : "unknown tagname", 0);
  4400. goto bad;
  4401. }
  4402. if (!TIFFSetField(tif, dp->tdir_tag, value))
  4403. goto bad;
  4404. if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
  4405. bitspersample_read = TRUE;
  4406. }
  4407. break;
  4408. case TIFFTAG_SMINSAMPLEVALUE:
  4409. case TIFFTAG_SMAXSAMPLEVALUE:
  4410. {
  4411. double *data = NULL;
  4412. enum TIFFReadDirEntryErr err;
  4413. uint32_t saved_flags;
  4414. int m;
  4415. if (dp->tdir_count !=
  4416. (uint64_t)tif->tif_dir.td_samplesperpixel)
  4417. err = TIFFReadDirEntryErrCount;
  4418. else
  4419. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  4420. if (!EvaluateIFDdatasizeReading(tif, dp))
  4421. goto bad;
  4422. if (err != TIFFReadDirEntryErrOk)
  4423. {
  4424. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4425. TIFFReadDirEntryOutputErr(
  4426. tif, err, module,
  4427. fip ? fip->field_name : "unknown tagname", 0);
  4428. goto bad;
  4429. }
  4430. saved_flags = tif->tif_flags;
  4431. tif->tif_flags |= TIFF_PERSAMPLE;
  4432. m = TIFFSetField(tif, dp->tdir_tag, data);
  4433. tif->tif_flags = saved_flags;
  4434. _TIFFfreeExt(tif, data);
  4435. if (!m)
  4436. goto bad;
  4437. }
  4438. break;
  4439. case TIFFTAG_STRIPOFFSETS:
  4440. case TIFFTAG_TILEOFFSETS:
  4441. {
  4442. switch (dp->tdir_type)
  4443. {
  4444. case TIFF_SHORT:
  4445. case TIFF_LONG:
  4446. case TIFF_LONG8:
  4447. break;
  4448. default:
  4449. /* Warn except if directory typically created with
  4450. * TIFFDeferStrileArrayWriting() */
  4451. if (!(tif->tif_mode == O_RDWR &&
  4452. dp->tdir_count == 0 && dp->tdir_type == 0 &&
  4453. dp->tdir_offset.toff_long8 == 0))
  4454. {
  4455. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4456. TIFFWarningExtR(
  4457. tif, module, "Invalid data type for tag %s",
  4458. fip ? fip->field_name : "unknown tagname");
  4459. }
  4460. break;
  4461. }
  4462. _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
  4463. sizeof(TIFFDirEntry));
  4464. if (!EvaluateIFDdatasizeReading(tif, dp))
  4465. goto bad;
  4466. }
  4467. break;
  4468. case TIFFTAG_STRIPBYTECOUNTS:
  4469. case TIFFTAG_TILEBYTECOUNTS:
  4470. {
  4471. switch (dp->tdir_type)
  4472. {
  4473. case TIFF_SHORT:
  4474. case TIFF_LONG:
  4475. case TIFF_LONG8:
  4476. break;
  4477. default:
  4478. /* Warn except if directory typically created with
  4479. * TIFFDeferStrileArrayWriting() */
  4480. if (!(tif->tif_mode == O_RDWR &&
  4481. dp->tdir_count == 0 && dp->tdir_type == 0 &&
  4482. dp->tdir_offset.toff_long8 == 0))
  4483. {
  4484. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4485. TIFFWarningExtR(
  4486. tif, module, "Invalid data type for tag %s",
  4487. fip ? fip->field_name : "unknown tagname");
  4488. }
  4489. break;
  4490. }
  4491. _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
  4492. sizeof(TIFFDirEntry));
  4493. if (!EvaluateIFDdatasizeReading(tif, dp))
  4494. goto bad;
  4495. }
  4496. break;
  4497. case TIFFTAG_COLORMAP:
  4498. case TIFFTAG_TRANSFERFUNCTION:
  4499. {
  4500. enum TIFFReadDirEntryErr err;
  4501. uint32_t countpersample;
  4502. uint32_t countrequired;
  4503. uint32_t incrementpersample;
  4504. uint16_t *value = NULL;
  4505. /* It would be dangerous to instantiate those tag values */
  4506. /* since if td_bitspersample has not yet been read (due to
  4507. */
  4508. /* unordered tags), it could be read afterwards with a */
  4509. /* values greater than the default one (1), which may cause
  4510. */
  4511. /* crashes in user code */
  4512. if (!bitspersample_read)
  4513. {
  4514. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4515. TIFFWarningExtR(
  4516. tif, module,
  4517. "Ignoring %s since BitsPerSample tag not found",
  4518. fip ? fip->field_name : "unknown tagname");
  4519. continue;
  4520. }
  4521. /* ColorMap or TransferFunction for high bit */
  4522. /* depths do not make much sense and could be */
  4523. /* used as a denial of service vector */
  4524. if (tif->tif_dir.td_bitspersample > 24)
  4525. {
  4526. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4527. TIFFWarningExtR(
  4528. tif, module,
  4529. "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
  4530. fip ? fip->field_name : "unknown tagname",
  4531. tif->tif_dir.td_bitspersample);
  4532. continue;
  4533. }
  4534. countpersample = (1U << tif->tif_dir.td_bitspersample);
  4535. if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
  4536. (dp->tdir_count == (uint64_t)countpersample))
  4537. {
  4538. countrequired = countpersample;
  4539. incrementpersample = 0;
  4540. }
  4541. else
  4542. {
  4543. countrequired = 3 * countpersample;
  4544. incrementpersample = countpersample;
  4545. }
  4546. if (dp->tdir_count != (uint64_t)countrequired)
  4547. err = TIFFReadDirEntryErrCount;
  4548. else
  4549. err = TIFFReadDirEntryShortArray(tif, dp, &value);
  4550. if (!EvaluateIFDdatasizeReading(tif, dp))
  4551. goto bad;
  4552. if (err != TIFFReadDirEntryErrOk)
  4553. {
  4554. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4555. TIFFReadDirEntryOutputErr(
  4556. tif, err, module,
  4557. fip ? fip->field_name : "unknown tagname", 1);
  4558. }
  4559. else
  4560. {
  4561. TIFFSetField(tif, dp->tdir_tag, value,
  4562. value + incrementpersample,
  4563. value + 2 * incrementpersample);
  4564. _TIFFfreeExt(tif, value);
  4565. }
  4566. }
  4567. break;
  4568. /* BEGIN REV 4.0 COMPATIBILITY */
  4569. case TIFFTAG_OSUBFILETYPE:
  4570. {
  4571. uint16_t valueo;
  4572. uint32_t value;
  4573. if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
  4574. TIFFReadDirEntryErrOk)
  4575. {
  4576. switch (valueo)
  4577. {
  4578. case OFILETYPE_REDUCEDIMAGE:
  4579. value = FILETYPE_REDUCEDIMAGE;
  4580. break;
  4581. case OFILETYPE_PAGE:
  4582. value = FILETYPE_PAGE;
  4583. break;
  4584. default:
  4585. value = 0;
  4586. break;
  4587. }
  4588. if (value != 0)
  4589. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
  4590. }
  4591. }
  4592. break;
  4593. /* END REV 4.0 COMPATIBILITY */
  4594. #if 0
  4595. case TIFFTAG_EP_BATTERYLEVEL:
  4596. /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
  4597. * LibTiff defines it as ASCII and converts RATIONAL to an
  4598. * ASCII string. */
  4599. switch (dp->tdir_type)
  4600. {
  4601. case TIFF_RATIONAL:
  4602. {
  4603. /* Read rational and convert to ASCII*/
  4604. enum TIFFReadDirEntryErr err;
  4605. TIFFRational_t rValue;
  4606. err = TIFFReadDirEntryCheckedRationalDirect(
  4607. tif, dp, &rValue);
  4608. if (err != TIFFReadDirEntryErrOk)
  4609. {
  4610. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4611. TIFFReadDirEntryOutputErr(
  4612. tif, err, module,
  4613. fip ? fip->field_name : "unknown tagname",
  4614. 1);
  4615. }
  4616. else
  4617. {
  4618. char szAux[32];
  4619. snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
  4620. rValue.uNum, rValue.uDenom);
  4621. TIFFSetField(tif, dp->tdir_tag, szAux);
  4622. }
  4623. }
  4624. break;
  4625. case TIFF_ASCII:
  4626. (void)TIFFFetchNormalTag(tif, dp, TRUE);
  4627. break;
  4628. default:
  4629. fip = TIFFFieldWithTag(tif, dp->tdir_tag);
  4630. TIFFWarningExtR(tif, module,
  4631. "Invalid data type for tag %s. "
  4632. "ASCII or RATIONAL expected",
  4633. fip ? fip->field_name
  4634. : "unknown tagname");
  4635. break;
  4636. }
  4637. break;
  4638. #endif
  4639. default:
  4640. (void)TIFFFetchNormalTag(tif, dp, TRUE);
  4641. break;
  4642. } /* -- switch (dp->tdir_tag) -- */
  4643. } /* -- if (!dp->tdir_ignore) */
  4644. } /* -- for-loop -- */
  4645. /* Evaluate final IFD data size. */
  4646. CalcFinalIFDdatasizeReading(tif, dircount);
  4647. /*
  4648. * OJPEG hack:
  4649. * - If a) compression is OJPEG, and b) photometric tag is missing,
  4650. * then we consistently find that photometric should be YCbCr
  4651. * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
  4652. * then we consistently find that the buggy implementation of the
  4653. * buggy compression scheme matches photometric YCbCr instead.
  4654. * - If a) compression is OJPEG, and b) bitspersample tag is missing,
  4655. * then we consistently find bitspersample should be 8.
  4656. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  4657. * and c) photometric is RGB or YCbCr, then we consistently find
  4658. * samplesperpixel should be 3
  4659. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  4660. * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
  4661. * find samplesperpixel should be 3
  4662. */
  4663. if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
  4664. {
  4665. if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
  4666. {
  4667. TIFFWarningExtR(
  4668. tif, module,
  4669. "Photometric tag is missing, assuming data is YCbCr");
  4670. if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
  4671. goto bad;
  4672. }
  4673. else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
  4674. {
  4675. tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
  4676. TIFFWarningExtR(tif, module,
  4677. "Photometric tag value assumed incorrect, "
  4678. "assuming data is YCbCr instead of RGB");
  4679. }
  4680. if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
  4681. {
  4682. TIFFWarningExtR(
  4683. tif, module,
  4684. "BitsPerSample tag is missing, assuming 8 bits per sample");
  4685. if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
  4686. goto bad;
  4687. }
  4688. if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
  4689. {
  4690. if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
  4691. {
  4692. TIFFWarningExtR(tif, module,
  4693. "SamplesPerPixel tag is missing, "
  4694. "assuming correct SamplesPerPixel value is 3");
  4695. if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
  4696. goto bad;
  4697. }
  4698. if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
  4699. {
  4700. TIFFWarningExtR(tif, module,
  4701. "SamplesPerPixel tag is missing, "
  4702. "applying correct SamplesPerPixel value of 3");
  4703. if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
  4704. goto bad;
  4705. }
  4706. else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
  4707. (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
  4708. {
  4709. /*
  4710. * SamplesPerPixel tag is missing, but is not required
  4711. * by spec. Assume correct SamplesPerPixel value of 1.
  4712. */
  4713. if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
  4714. goto bad;
  4715. }
  4716. }
  4717. }
  4718. /*
  4719. * Setup appropriate structures (by strip or by tile)
  4720. * We do that only after the above OJPEG hack which alters SamplesPerPixel
  4721. * and thus influences the number of strips in the separate planarconfig.
  4722. */
  4723. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
  4724. {
  4725. tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
  4726. tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
  4727. tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
  4728. tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
  4729. tif->tif_flags &= ~TIFF_ISTILED;
  4730. }
  4731. else
  4732. {
  4733. tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
  4734. tif->tif_flags |= TIFF_ISTILED;
  4735. }
  4736. if (!tif->tif_dir.td_nstrips)
  4737. {
  4738. TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
  4739. isTiled(tif) ? "tiles" : "strips");
  4740. goto bad;
  4741. }
  4742. tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
  4743. if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
  4744. tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
  4745. if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
  4746. {
  4747. #ifdef OJPEG_SUPPORT
  4748. if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
  4749. (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
  4750. {
  4751. /*
  4752. * XXX: OJPEG hack.
  4753. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  4754. * and c) the number of strips is 1,
  4755. * then we tolerate the absence of stripoffsets tag,
  4756. * because, presumably, all required data is in the
  4757. * JpegInterchangeFormat stream.
  4758. */
  4759. TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  4760. }
  4761. else
  4762. #endif
  4763. {
  4764. MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
  4765. goto bad;
  4766. }
  4767. }
  4768. if (tif->tif_mode == O_RDWR &&
  4769. tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
  4770. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  4771. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  4772. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
  4773. tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
  4774. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  4775. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  4776. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
  4777. {
  4778. /* Directory typically created with TIFFDeferStrileArrayWriting() */
  4779. TIFFSetupStrips(tif);
  4780. }
  4781. else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
  4782. {
  4783. if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
  4784. {
  4785. if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
  4786. tif->tif_dir.td_nstrips,
  4787. &tif->tif_dir.td_stripoffset_p))
  4788. {
  4789. goto bad;
  4790. }
  4791. }
  4792. if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
  4793. {
  4794. if (!TIFFFetchStripThing(
  4795. tif, &(tif->tif_dir.td_stripbytecount_entry),
  4796. tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
  4797. {
  4798. goto bad;
  4799. }
  4800. }
  4801. }
  4802. /*
  4803. * Make sure all non-color channels are extrasamples.
  4804. * If it's not the case, define them as such.
  4805. */
  4806. color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
  4807. if (color_channels &&
  4808. tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
  4809. color_channels)
  4810. {
  4811. uint16_t old_extrasamples;
  4812. uint16_t *new_sampleinfo;
  4813. TIFFWarningExtR(
  4814. tif, module,
  4815. "Sum of Photometric type-related "
  4816. "color channels and ExtraSamples doesn't match SamplesPerPixel. "
  4817. "Defining non-color channels as ExtraSamples.");
  4818. old_extrasamples = tif->tif_dir.td_extrasamples;
  4819. tif->tif_dir.td_extrasamples =
  4820. (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
  4821. // sampleinfo should contain information relative to these new extra
  4822. // samples
  4823. new_sampleinfo = (uint16_t *)_TIFFcallocExt(
  4824. tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
  4825. if (!new_sampleinfo)
  4826. {
  4827. TIFFErrorExtR(tif, module,
  4828. "Failed to allocate memory for "
  4829. "temporary new sampleinfo array "
  4830. "(%" PRIu16 " 16 bit elements)",
  4831. tif->tif_dir.td_extrasamples);
  4832. goto bad;
  4833. }
  4834. if (old_extrasamples > 0)
  4835. memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
  4836. old_extrasamples * sizeof(uint16_t));
  4837. _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
  4838. tif->tif_dir.td_extrasamples);
  4839. _TIFFfreeExt(tif, new_sampleinfo);
  4840. }
  4841. /*
  4842. * Verify Palette image has a Colormap.
  4843. */
  4844. if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
  4845. !TIFFFieldSet(tif, FIELD_COLORMAP))
  4846. {
  4847. if (tif->tif_dir.td_bitspersample >= 8 &&
  4848. tif->tif_dir.td_samplesperpixel == 3)
  4849. tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
  4850. else if (tif->tif_dir.td_bitspersample >= 8)
  4851. tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
  4852. else
  4853. {
  4854. MissingRequired(tif, "Colormap");
  4855. goto bad;
  4856. }
  4857. }
  4858. /*
  4859. * OJPEG hack:
  4860. * We do no further messing with strip/tile offsets/bytecounts in OJPEG
  4861. * TIFFs
  4862. */
  4863. if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
  4864. {
  4865. /*
  4866. * Attempt to deal with a missing StripByteCounts tag.
  4867. */
  4868. if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
  4869. {
  4870. /*
  4871. * Some manufacturers violate the spec by not giving
  4872. * the size of the strips. In this case, assume there
  4873. * is one uncompressed strip of data.
  4874. */
  4875. if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
  4876. tif->tif_dir.td_nstrips > 1) ||
  4877. (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
  4878. tif->tif_dir.td_nstrips !=
  4879. (uint32_t)tif->tif_dir.td_samplesperpixel))
  4880. {
  4881. MissingRequired(tif, "StripByteCounts");
  4882. goto bad;
  4883. }
  4884. TIFFWarningExtR(
  4885. tif, module,
  4886. "TIFF directory is missing required "
  4887. "\"StripByteCounts\" field, calculating from imagelength");
  4888. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  4889. goto bad;
  4890. }
  4891. else if (tif->tif_dir.td_nstrips == 1 &&
  4892. !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
  4893. {
  4894. /*
  4895. * XXX: Plexus (and others) sometimes give a value of
  4896. * zero for a tag when they don't know what the
  4897. * correct value is! Try and handle the simple case
  4898. * of estimating the size of a one strip image.
  4899. */
  4900. TIFFWarningExtR(tif, module,
  4901. "Bogus \"StripByteCounts\" field, ignoring and "
  4902. "calculating from imagelength");
  4903. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  4904. goto bad;
  4905. }
  4906. else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
  4907. tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
  4908. tif->tif_dir.td_nstrips > 2 &&
  4909. tif->tif_dir.td_compression == COMPRESSION_NONE &&
  4910. TIFFGetStrileByteCount(tif, 0) !=
  4911. TIFFGetStrileByteCount(tif, 1) &&
  4912. TIFFGetStrileByteCount(tif, 0) != 0 &&
  4913. TIFFGetStrileByteCount(tif, 1) != 0)
  4914. {
  4915. /*
  4916. * XXX: Some vendors fill StripByteCount array with
  4917. * absolutely wrong values (it can be equal to
  4918. * StripOffset array, for example). Catch this case
  4919. * here.
  4920. *
  4921. * We avoid this check if deferring strile loading
  4922. * as it would always force us to load the strip/tile
  4923. * information.
  4924. */
  4925. TIFFWarningExtR(tif, module,
  4926. "Wrong \"StripByteCounts\" field, ignoring and "
  4927. "calculating from imagelength");
  4928. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  4929. goto bad;
  4930. }
  4931. }
  4932. if (dir)
  4933. {
  4934. _TIFFfreeExt(tif, dir);
  4935. dir = NULL;
  4936. }
  4937. if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  4938. {
  4939. if (tif->tif_dir.td_bitspersample >= 16)
  4940. tif->tif_dir.td_maxsamplevalue = 0xFFFF;
  4941. else
  4942. tif->tif_dir.td_maxsamplevalue =
  4943. (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
  4944. }
  4945. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  4946. /*
  4947. * XXX: We can optimize checking for the strip bounds using the sorted
  4948. * bytecounts array. See also comments for TIFFAppendToStrip()
  4949. * function in tif_write.c.
  4950. */
  4951. if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
  4952. {
  4953. uint32_t strip;
  4954. tif->tif_dir.td_stripbytecountsorted = 1;
  4955. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
  4956. {
  4957. if (TIFFGetStrileOffset(tif, strip - 1) >
  4958. TIFFGetStrileOffset(tif, strip))
  4959. {
  4960. tif->tif_dir.td_stripbytecountsorted = 0;
  4961. break;
  4962. }
  4963. }
  4964. }
  4965. #endif
  4966. /*
  4967. * An opportunity for compression mode dependent tag fixup
  4968. */
  4969. (*tif->tif_fixuptags)(tif);
  4970. /*
  4971. * Some manufacturers make life difficult by writing
  4972. * large amounts of uncompressed data as a single strip.
  4973. * This is contrary to the recommendations of the spec.
  4974. * The following makes an attempt at breaking such images
  4975. * into strips closer to the recommended 8k bytes. A
  4976. * side effect, however, is that the RowsPerStrip tag
  4977. * value may be changed.
  4978. */
  4979. if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
  4980. (tif->tif_dir.td_nstrips == 1) &&
  4981. (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
  4982. ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
  4983. {
  4984. ChopUpSingleUncompressedStrip(tif);
  4985. }
  4986. /* There are also uncompressed striped files with strips larger than */
  4987. /* 2 GB, which make them unfriendly with a lot of code. If possible, */
  4988. /* try to expose smaller "virtual" strips. */
  4989. if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
  4990. tif->tif_dir.td_compression == COMPRESSION_NONE &&
  4991. (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
  4992. TIFFStripSize64(tif) > 0x7FFFFFFFUL)
  4993. {
  4994. TryChopUpUncompressedBigTiff(tif);
  4995. }
  4996. /*
  4997. * Clear the dirty directory flag.
  4998. */
  4999. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  5000. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  5001. /*
  5002. * Reinitialize i/o since we are starting on a new directory.
  5003. */
  5004. tif->tif_row = (uint32_t)-1;
  5005. tif->tif_curstrip = (uint32_t)-1;
  5006. tif->tif_col = (uint32_t)-1;
  5007. tif->tif_curtile = (uint32_t)-1;
  5008. tif->tif_tilesize = (tmsize_t)-1;
  5009. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  5010. if (!tif->tif_scanlinesize)
  5011. {
  5012. TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
  5013. return (0);
  5014. }
  5015. if (isTiled(tif))
  5016. {
  5017. tif->tif_tilesize = TIFFTileSize(tif);
  5018. if (!tif->tif_tilesize)
  5019. {
  5020. TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
  5021. return (0);
  5022. }
  5023. }
  5024. else
  5025. {
  5026. if (!TIFFStripSize(tif))
  5027. {
  5028. TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
  5029. return (0);
  5030. }
  5031. }
  5032. return (1);
  5033. bad:
  5034. if (dir)
  5035. _TIFFfreeExt(tif, dir);
  5036. return (0);
  5037. } /*-- TIFFReadDirectory() --*/
  5038. static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
  5039. uint16_t dircount)
  5040. {
  5041. static const char module[] = "TIFFReadDirectoryCheckOrder";
  5042. uint32_t m;
  5043. uint16_t n;
  5044. TIFFDirEntry *o;
  5045. m = 0;
  5046. for (n = 0, o = dir; n < dircount; n++, o++)
  5047. {
  5048. if (o->tdir_tag < m)
  5049. {
  5050. TIFFWarningExtR(tif, module,
  5051. "Invalid TIFF directory; tags are not sorted in "
  5052. "ascending order");
  5053. break;
  5054. }
  5055. m = o->tdir_tag + 1;
  5056. }
  5057. }
  5058. static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
  5059. uint16_t dircount,
  5060. uint16_t tagid)
  5061. {
  5062. TIFFDirEntry *m;
  5063. uint16_t n;
  5064. (void)tif;
  5065. for (m = dir, n = 0; n < dircount; m++, n++)
  5066. {
  5067. if (m->tdir_tag == tagid)
  5068. return (m);
  5069. }
  5070. return (0);
  5071. }
  5072. static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
  5073. uint32_t *fii)
  5074. {
  5075. int32_t ma, mb, mc;
  5076. ma = -1;
  5077. mc = (int32_t)tif->tif_nfields;
  5078. while (1)
  5079. {
  5080. if (ma + 1 == mc)
  5081. {
  5082. *fii = FAILED_FII;
  5083. return;
  5084. }
  5085. mb = (ma + mc) / 2;
  5086. if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
  5087. break;
  5088. if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
  5089. ma = mb;
  5090. else
  5091. mc = mb;
  5092. }
  5093. while (1)
  5094. {
  5095. if (mb == 0)
  5096. break;
  5097. if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
  5098. break;
  5099. mb--;
  5100. }
  5101. *fii = mb;
  5102. }
  5103. /*
  5104. * Read custom directory from the arbitrary offset.
  5105. * The code is very similar to TIFFReadDirectory().
  5106. */
  5107. int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff,
  5108. const TIFFFieldArray *infoarray)
  5109. {
  5110. static const char module[] = "TIFFReadCustomDirectory";
  5111. TIFFDirEntry *dir;
  5112. uint16_t dircount;
  5113. TIFFDirEntry *dp;
  5114. uint16_t di;
  5115. const TIFFField *fip;
  5116. uint32_t fii;
  5117. assert(infoarray != NULL);
  5118. dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
  5119. if (!dircount)
  5120. {
  5121. TIFFErrorExtR(tif, module,
  5122. "Failed to read custom directory at offset %" PRIu64,
  5123. diroff);
  5124. return 0;
  5125. }
  5126. TIFFReadDirectoryCheckOrder(tif, dir, dircount);
  5127. /*
  5128. * Mark duplicates of any tag to be ignored (bugzilla 1994)
  5129. * to avoid certain pathological problems.
  5130. */
  5131. {
  5132. TIFFDirEntry *ma;
  5133. uint16_t mb;
  5134. for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
  5135. {
  5136. TIFFDirEntry *na;
  5137. uint16_t nb;
  5138. for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
  5139. {
  5140. if (ma->tdir_tag == na->tdir_tag)
  5141. {
  5142. na->tdir_ignore = TRUE;
  5143. }
  5144. }
  5145. }
  5146. }
  5147. /* Free any old stuff and reinit. */
  5148. TIFFFreeDirectory(tif);
  5149. /* Even if custom directories do not need the default settings of a standard
  5150. * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
  5151. * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
  5152. * be initialized, which is done in TIFFDefaultDirectory().
  5153. * After that, the field array for the custom tags needs to be setup again.
  5154. */
  5155. TIFFDefaultDirectory(tif);
  5156. _TIFFSetupFields(tif, infoarray);
  5157. /* Allocate arrays for offset values outside IFD entry for IFD data size
  5158. * checking. Note: Counter are reset within TIFFFreeDirectory(). */
  5159. tif->tif_dir.td_dirdatasize_offsets =
  5160. (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
  5161. tif, dircount * sizeof(TIFFEntryOffsetAndLength));
  5162. if (tif->tif_dir.td_dirdatasize_offsets == NULL)
  5163. {
  5164. TIFFErrorExtR(
  5165. tif, module,
  5166. "Failed to allocate memory for counting IFD data size at reading");
  5167. if (dir)
  5168. _TIFFfreeExt(tif, dir);
  5169. return 0;
  5170. }
  5171. for (di = 0, dp = dir; di < dircount; di++, dp++)
  5172. {
  5173. TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
  5174. if (fii == FAILED_FII)
  5175. {
  5176. if (tif->tif_warn_about_unknown_tags)
  5177. {
  5178. TIFFWarningExtR(tif, module,
  5179. "Unknown field with tag %" PRIu16 " (0x%" PRIx16
  5180. ") encountered",
  5181. dp->tdir_tag, dp->tdir_tag);
  5182. }
  5183. const TIFFField *fld = _TIFFCreateAnonField(
  5184. tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
  5185. if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
  5186. {
  5187. if (tif->tif_warn_about_unknown_tags)
  5188. {
  5189. TIFFWarningExtR(
  5190. tif, module,
  5191. "Registering anonymous field with tag %" PRIu16
  5192. " (0x%" PRIx16 ") failed",
  5193. dp->tdir_tag, dp->tdir_tag);
  5194. }
  5195. dp->tdir_ignore = TRUE;
  5196. }
  5197. else
  5198. {
  5199. TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
  5200. assert(fii != FAILED_FII);
  5201. }
  5202. }
  5203. if (!dp->tdir_ignore)
  5204. {
  5205. fip = tif->tif_fields[fii];
  5206. if (fip->field_bit == FIELD_IGNORE)
  5207. dp->tdir_ignore = TRUE;
  5208. else
  5209. {
  5210. /* check data type */
  5211. while ((fip->field_type != TIFF_ANY) &&
  5212. (fip->field_type != dp->tdir_type))
  5213. {
  5214. fii++;
  5215. if ((fii == tif->tif_nfields) ||
  5216. (tif->tif_fields[fii]->field_tag !=
  5217. (uint32_t)dp->tdir_tag))
  5218. {
  5219. fii = 0xFFFF;
  5220. break;
  5221. }
  5222. fip = tif->tif_fields[fii];
  5223. }
  5224. if (fii == 0xFFFF)
  5225. {
  5226. TIFFWarningExtR(tif, module,
  5227. "Wrong data type %" PRIu16
  5228. " for \"%s\"; tag ignored",
  5229. dp->tdir_type, fip->field_name);
  5230. dp->tdir_ignore = TRUE;
  5231. }
  5232. else
  5233. {
  5234. /* check count if known in advance */
  5235. if ((fip->field_readcount != TIFF_VARIABLE) &&
  5236. (fip->field_readcount != TIFF_VARIABLE2))
  5237. {
  5238. uint32_t expected;
  5239. if (fip->field_readcount == TIFF_SPP)
  5240. expected =
  5241. (uint32_t)tif->tif_dir.td_samplesperpixel;
  5242. else
  5243. expected = (uint32_t)fip->field_readcount;
  5244. if (!CheckDirCount(tif, dp, expected))
  5245. dp->tdir_ignore = TRUE;
  5246. }
  5247. }
  5248. }
  5249. if (!dp->tdir_ignore)
  5250. {
  5251. switch (dp->tdir_tag)
  5252. {
  5253. case EXIFTAG_SUBJECTDISTANCE:
  5254. if (!TIFFFieldIsAnonymous(fip))
  5255. {
  5256. /* should only be called on a Exif directory */
  5257. /* when exifFields[] is active */
  5258. (void)TIFFFetchSubjectDistance(tif, dp);
  5259. }
  5260. else
  5261. {
  5262. (void)TIFFFetchNormalTag(tif, dp, TRUE);
  5263. }
  5264. break;
  5265. default:
  5266. (void)TIFFFetchNormalTag(tif, dp, TRUE);
  5267. break;
  5268. }
  5269. } /*-- if (!dp->tdir_ignore) */
  5270. }
  5271. }
  5272. /* Evaluate final IFD data size. */
  5273. CalcFinalIFDdatasizeReading(tif, dircount);
  5274. /* To be able to return from SubIFD or custom-IFD to main-IFD */
  5275. tif->tif_setdirectory_force_absolute = TRUE;
  5276. if (dir)
  5277. _TIFFfreeExt(tif, dir);
  5278. return 1;
  5279. }
  5280. /*
  5281. * EXIF is important special case of custom IFD, so we have a special
  5282. * function to read it.
  5283. */
  5284. int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
  5285. {
  5286. return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
  5287. }
  5288. /*
  5289. *--: EXIF-GPS custom directory reading as another special case of custom IFD.
  5290. */
  5291. int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
  5292. {
  5293. return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
  5294. }
  5295. static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
  5296. uint16_t dircount)
  5297. {
  5298. static const char module[] = "EstimateStripByteCounts";
  5299. TIFFDirEntry *dp;
  5300. TIFFDirectory *td = &tif->tif_dir;
  5301. uint32_t strip;
  5302. /* Do not try to load stripbytecount as we will compute it */
  5303. if (!_TIFFFillStrilesInternal(tif, 0))
  5304. return -1;
  5305. const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
  5306. uint64_t filesize = 0;
  5307. if (allocsize > 100 * 1024 * 1024)
  5308. {
  5309. /* Before allocating a huge amount of memory for corrupted files, check
  5310. * if size of requested memory is not greater than file size. */
  5311. filesize = TIFFGetFileSize(tif);
  5312. if (allocsize > filesize)
  5313. {
  5314. TIFFWarningExtR(
  5315. tif, module,
  5316. "Requested memory size for StripByteCounts of %" PRIu64
  5317. " is greater than filesize %" PRIu64 ". Memory not allocated",
  5318. allocsize, filesize);
  5319. return -1;
  5320. }
  5321. }
  5322. if (td->td_stripbytecount_p)
  5323. _TIFFfreeExt(tif, td->td_stripbytecount_p);
  5324. td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
  5325. tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
  5326. if (td->td_stripbytecount_p == NULL)
  5327. return -1;
  5328. if (td->td_compression != COMPRESSION_NONE)
  5329. {
  5330. uint64_t space;
  5331. uint16_t n;
  5332. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5333. space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
  5334. else
  5335. space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
  5336. /* calculate amount of space used by indirect values */
  5337. for (dp = dir, n = dircount; n > 0; n--, dp++)
  5338. {
  5339. uint32_t typewidth;
  5340. uint64_t datasize;
  5341. typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
  5342. if (typewidth == 0)
  5343. {
  5344. TIFFErrorExtR(
  5345. tif, module,
  5346. "Cannot determine size of unknown tag type %" PRIu16,
  5347. dp->tdir_type);
  5348. return -1;
  5349. }
  5350. if (dp->tdir_count > UINT64_MAX / typewidth)
  5351. return -1;
  5352. datasize = (uint64_t)typewidth * dp->tdir_count;
  5353. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5354. {
  5355. if (datasize <= 4)
  5356. datasize = 0;
  5357. }
  5358. else
  5359. {
  5360. if (datasize <= 8)
  5361. datasize = 0;
  5362. }
  5363. if (space > UINT64_MAX - datasize)
  5364. return -1;
  5365. space += datasize;
  5366. }
  5367. if (filesize == 0)
  5368. filesize = TIFFGetFileSize(tif);
  5369. if (filesize < space)
  5370. /* we should perhaps return in error ? */
  5371. space = filesize;
  5372. else
  5373. space = filesize - space;
  5374. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  5375. space /= td->td_samplesperpixel;
  5376. for (strip = 0; strip < td->td_nstrips; strip++)
  5377. td->td_stripbytecount_p[strip] = space;
  5378. /*
  5379. * This gross hack handles the case were the offset to
  5380. * the last strip is past the place where we think the strip
  5381. * should begin. Since a strip of data must be contiguous,
  5382. * it's safe to assume that we've overestimated the amount
  5383. * of data in the strip and trim this number back accordingly.
  5384. */
  5385. strip--;
  5386. if (td->td_stripoffset_p[strip] >
  5387. UINT64_MAX - td->td_stripbytecount_p[strip])
  5388. return -1;
  5389. if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
  5390. filesize)
  5391. {
  5392. if (td->td_stripoffset_p[strip] >= filesize)
  5393. {
  5394. /* Not sure what we should in that case... */
  5395. td->td_stripbytecount_p[strip] = 0;
  5396. }
  5397. else
  5398. {
  5399. td->td_stripbytecount_p[strip] =
  5400. filesize - td->td_stripoffset_p[strip];
  5401. }
  5402. }
  5403. }
  5404. else if (isTiled(tif))
  5405. {
  5406. uint64_t bytespertile = TIFFTileSize64(tif);
  5407. for (strip = 0; strip < td->td_nstrips; strip++)
  5408. td->td_stripbytecount_p[strip] = bytespertile;
  5409. }
  5410. else
  5411. {
  5412. uint64_t rowbytes = TIFFScanlineSize64(tif);
  5413. uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
  5414. for (strip = 0; strip < td->td_nstrips; strip++)
  5415. {
  5416. if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
  5417. return -1;
  5418. td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
  5419. }
  5420. }
  5421. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  5422. if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  5423. td->td_rowsperstrip = td->td_imagelength;
  5424. return 1;
  5425. }
  5426. static void MissingRequired(TIFF *tif, const char *tagname)
  5427. {
  5428. static const char module[] = "MissingRequired";
  5429. TIFFErrorExtR(tif, module,
  5430. "TIFF directory is missing required \"%s\" field", tagname);
  5431. }
  5432. static unsigned long hashFuncOffsetToNumber(const void *elt)
  5433. {
  5434. const TIFFOffsetAndDirNumber *offsetAndDirNumber =
  5435. (const TIFFOffsetAndDirNumber *)elt;
  5436. const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
  5437. ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
  5438. return hash;
  5439. }
  5440. static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
  5441. {
  5442. const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
  5443. (const TIFFOffsetAndDirNumber *)elt1;
  5444. const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
  5445. (const TIFFOffsetAndDirNumber *)elt2;
  5446. return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
  5447. }
  5448. static unsigned long hashFuncNumberToOffset(const void *elt)
  5449. {
  5450. const TIFFOffsetAndDirNumber *offsetAndDirNumber =
  5451. (const TIFFOffsetAndDirNumber *)elt;
  5452. return offsetAndDirNumber->dirNumber;
  5453. }
  5454. static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
  5455. {
  5456. const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
  5457. (const TIFFOffsetAndDirNumber *)elt1;
  5458. const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
  5459. (const TIFFOffsetAndDirNumber *)elt2;
  5460. return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
  5461. }
  5462. /*
  5463. * Check the directory number and offset against the list of already seen
  5464. * directory numbers and offsets. This is a trick to prevent IFD looping.
  5465. * The one can create TIFF file with looped directory pointers. We will
  5466. * maintain a list of already seen directories and check every IFD offset
  5467. * and its IFD number against that list. However, the offset of an IFD number
  5468. * can change - e.g. when writing updates to file.
  5469. * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
  5470. * or an error has occurred.
  5471. */
  5472. int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
  5473. {
  5474. if (diroff == 0) /* no more directories */
  5475. return 0;
  5476. if (tif->tif_map_dir_offset_to_number == NULL)
  5477. {
  5478. tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
  5479. hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
  5480. if (tif->tif_map_dir_offset_to_number == NULL)
  5481. {
  5482. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5483. "Not enough memory");
  5484. return 1;
  5485. }
  5486. }
  5487. if (tif->tif_map_dir_number_to_offset == NULL)
  5488. {
  5489. /* No free callback for this map, as it shares the same items as
  5490. * tif->tif_map_dir_offset_to_number. */
  5491. tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
  5492. hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
  5493. if (tif->tif_map_dir_number_to_offset == NULL)
  5494. {
  5495. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5496. "Not enough memory");
  5497. return 1;
  5498. }
  5499. }
  5500. /* Check if offset is already in the list:
  5501. * - yes: check, if offset is at the same IFD number - if not, it is an IFD
  5502. * loop
  5503. * - no: add to list or update offset at that IFD number
  5504. */
  5505. TIFFOffsetAndDirNumber entry;
  5506. entry.offset = diroff;
  5507. entry.dirNumber = dirn;
  5508. TIFFOffsetAndDirNumber *foundEntry =
  5509. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5510. tif->tif_map_dir_offset_to_number, &entry);
  5511. if (foundEntry)
  5512. {
  5513. if (foundEntry->dirNumber == dirn)
  5514. {
  5515. return 1;
  5516. }
  5517. else
  5518. {
  5519. TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5520. "TIFF directory %d has IFD looping to directory %u "
  5521. "at offset 0x%" PRIx64 " (%" PRIu64 ")",
  5522. (int)dirn - 1, foundEntry->dirNumber, diroff,
  5523. diroff);
  5524. return 0;
  5525. }
  5526. }
  5527. /* Check if offset of an IFD has been changed and update offset of that IFD
  5528. * number. */
  5529. foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5530. tif->tif_map_dir_number_to_offset, &entry);
  5531. if (foundEntry)
  5532. {
  5533. if (foundEntry->offset != diroff)
  5534. {
  5535. TIFFOffsetAndDirNumber entryOld;
  5536. entryOld.offset = foundEntry->offset;
  5537. entryOld.dirNumber = dirn;
  5538. /* We must remove first from tif_map_dir_number_to_offset as the */
  5539. /* entry is owned (and thus freed) by */
  5540. /* tif_map_dir_offset_to_number */
  5541. TIFFOffsetAndDirNumber *foundEntryOld =
  5542. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5543. tif->tif_map_dir_number_to_offset, &entryOld);
  5544. if (foundEntryOld)
  5545. {
  5546. TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
  5547. foundEntryOld);
  5548. }
  5549. foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5550. tif->tif_map_dir_offset_to_number, &entryOld);
  5551. if (foundEntryOld)
  5552. {
  5553. TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
  5554. foundEntryOld);
  5555. }
  5556. TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
  5557. sizeof(TIFFOffsetAndDirNumber));
  5558. if (entryPtr == NULL)
  5559. {
  5560. return 0;
  5561. }
  5562. /* Add IFD offset and dirn to IFD directory list */
  5563. *entryPtr = entry;
  5564. if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
  5565. {
  5566. TIFFErrorExtR(
  5567. tif, "_TIFFCheckDirNumberAndOffset",
  5568. "Insertion in tif_map_dir_offset_to_number failed");
  5569. return 0;
  5570. }
  5571. if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
  5572. {
  5573. TIFFErrorExtR(
  5574. tif, "_TIFFCheckDirNumberAndOffset",
  5575. "Insertion in tif_map_dir_number_to_offset failed");
  5576. return 0;
  5577. }
  5578. }
  5579. return 1;
  5580. }
  5581. /* Arbitrary (hopefully big enough) limit */
  5582. if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
  5583. TIFF_MAX_DIR_COUNT)
  5584. {
  5585. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5586. "Cannot handle more than %u TIFF directories",
  5587. TIFF_MAX_DIR_COUNT);
  5588. return 0;
  5589. }
  5590. TIFFOffsetAndDirNumber *entryPtr =
  5591. (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
  5592. if (entryPtr == NULL)
  5593. {
  5594. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5595. "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
  5596. return 0;
  5597. }
  5598. /* Add IFD offset and dirn to IFD directory list */
  5599. *entryPtr = entry;
  5600. if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
  5601. {
  5602. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5603. "Insertion in tif_map_dir_offset_to_number failed");
  5604. return 0;
  5605. }
  5606. if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
  5607. {
  5608. TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
  5609. "Insertion in tif_map_dir_number_to_offset failed");
  5610. return 0;
  5611. }
  5612. return 1;
  5613. } /* --- _TIFFCheckDirNumberAndOffset() ---*/
  5614. /*
  5615. * Retrieve the matching IFD directory number of a given IFD offset
  5616. * from the list of directories already seen.
  5617. * Returns 1 if the offset was in the list and the directory number
  5618. * can be returned.
  5619. * Otherwise returns 0 or if an error occurred.
  5620. */
  5621. int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
  5622. {
  5623. if (diroff == 0) /* no more directories */
  5624. return 0;
  5625. /* Check if offset is already in the list and return matching directory
  5626. * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
  5627. * search again in IFD list.
  5628. */
  5629. if (tif->tif_map_dir_offset_to_number == NULL)
  5630. return 0;
  5631. TIFFOffsetAndDirNumber entry;
  5632. entry.offset = diroff;
  5633. entry.dirNumber = 0; /* not used */
  5634. TIFFOffsetAndDirNumber *foundEntry =
  5635. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5636. tif->tif_map_dir_offset_to_number, &entry);
  5637. if (foundEntry)
  5638. {
  5639. *dirn = foundEntry->dirNumber;
  5640. return 1;
  5641. }
  5642. /* This updates the directory list for all main-IFDs in the file. */
  5643. TIFFNumberOfDirectories(tif);
  5644. foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5645. tif->tif_map_dir_offset_to_number, &entry);
  5646. if (foundEntry)
  5647. {
  5648. *dirn = foundEntry->dirNumber;
  5649. return 1;
  5650. }
  5651. return 0;
  5652. } /*--- _TIFFGetDirNumberFromOffset() ---*/
  5653. /*
  5654. * Retrieve the matching IFD directory offset of a given IFD number
  5655. * from the list of directories already seen.
  5656. * Returns 1 if the offset was in the list of already seen IFDs and the
  5657. * directory offset can be returned. The directory list is not updated.
  5658. * Otherwise returns 0 or if an error occurred.
  5659. */
  5660. int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
  5661. {
  5662. if (tif->tif_map_dir_number_to_offset == NULL)
  5663. return 0;
  5664. TIFFOffsetAndDirNumber entry;
  5665. entry.offset = 0; /* not used */
  5666. entry.dirNumber = dirn;
  5667. TIFFOffsetAndDirNumber *foundEntry =
  5668. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5669. tif->tif_map_dir_number_to_offset, &entry);
  5670. if (foundEntry)
  5671. {
  5672. *diroff = foundEntry->offset;
  5673. return 1;
  5674. }
  5675. return 0;
  5676. } /*--- _TIFFGetOffsetFromDirNumber() ---*/
  5677. /*
  5678. * Remove an entry from the directory list of already seen directories
  5679. * by directory offset.
  5680. * If an entry is to be removed from the list, it is also okay if the entry
  5681. * is not in the list or the list does not exist.
  5682. */
  5683. int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
  5684. {
  5685. if (tif->tif_map_dir_offset_to_number == NULL)
  5686. return 1;
  5687. TIFFOffsetAndDirNumber entryOld;
  5688. entryOld.offset = diroff;
  5689. entryOld.dirNumber = 0;
  5690. /* We must remove first from tif_map_dir_number_to_offset as the
  5691. * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
  5692. * However, we need firstly to find the directory number from offset. */
  5693. TIFFOffsetAndDirNumber *foundEntryOldOff =
  5694. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5695. tif->tif_map_dir_offset_to_number, &entryOld);
  5696. if (foundEntryOldOff)
  5697. {
  5698. entryOld.dirNumber = foundEntryOldOff->dirNumber;
  5699. if (tif->tif_map_dir_number_to_offset != NULL)
  5700. {
  5701. TIFFOffsetAndDirNumber *foundEntryOldDir =
  5702. (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
  5703. tif->tif_map_dir_number_to_offset, &entryOld);
  5704. if (foundEntryOldDir)
  5705. {
  5706. TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
  5707. foundEntryOldDir);
  5708. TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
  5709. foundEntryOldOff);
  5710. return 1;
  5711. }
  5712. }
  5713. else
  5714. {
  5715. TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
  5716. "Unexpectedly tif_map_dir_number_to_offset is "
  5717. "missing but tif_map_dir_offset_to_number exists.");
  5718. return 0;
  5719. }
  5720. }
  5721. return 1;
  5722. } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
  5723. /*
  5724. * Check the count field of a directory entry against a known value. The
  5725. * caller is expected to skip/ignore the tag if there is a mismatch.
  5726. */
  5727. static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
  5728. {
  5729. if ((uint64_t)count > dir->tdir_count)
  5730. {
  5731. const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  5732. TIFFWarningExtR(tif, tif->tif_name,
  5733. "incorrect count for field \"%s\" (%" PRIu64
  5734. ", expecting %" PRIu32 "); tag ignored",
  5735. fip ? fip->field_name : "unknown tagname",
  5736. dir->tdir_count, count);
  5737. return (0);
  5738. }
  5739. else if ((uint64_t)count < dir->tdir_count)
  5740. {
  5741. const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  5742. TIFFWarningExtR(tif, tif->tif_name,
  5743. "incorrect count for field \"%s\" (%" PRIu64
  5744. ", expecting %" PRIu32 "); tag trimmed",
  5745. fip ? fip->field_name : "unknown tagname",
  5746. dir->tdir_count, count);
  5747. dir->tdir_count = count;
  5748. return (1);
  5749. }
  5750. return (1);
  5751. }
  5752. /*
  5753. * Read IFD structure from the specified offset. If the pointer to
  5754. * nextdiroff variable has been specified, read it too. Function returns a
  5755. * number of fields in the directory or 0 if failed.
  5756. */
  5757. static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
  5758. TIFFDirEntry **pdir, uint64_t *nextdiroff)
  5759. {
  5760. static const char module[] = "TIFFFetchDirectory";
  5761. void *origdir;
  5762. uint16_t dircount16;
  5763. uint32_t dirsize;
  5764. TIFFDirEntry *dir;
  5765. uint8_t *ma;
  5766. TIFFDirEntry *mb;
  5767. uint16_t n;
  5768. assert(pdir);
  5769. tif->tif_diroff = diroff;
  5770. if (nextdiroff)
  5771. *nextdiroff = 0;
  5772. if (!isMapped(tif))
  5773. {
  5774. if (!SeekOK(tif, tif->tif_diroff))
  5775. {
  5776. TIFFErrorExtR(tif, module,
  5777. "%s: Seek error accessing TIFF directory",
  5778. tif->tif_name);
  5779. return 0;
  5780. }
  5781. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5782. {
  5783. if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
  5784. {
  5785. TIFFErrorExtR(tif, module,
  5786. "%s: Can not read TIFF directory count",
  5787. tif->tif_name);
  5788. return 0;
  5789. }
  5790. if (tif->tif_flags & TIFF_SWAB)
  5791. TIFFSwabShort(&dircount16);
  5792. if (dircount16 > 4096)
  5793. {
  5794. TIFFErrorExtR(tif, module,
  5795. "Sanity check on directory count failed, this is "
  5796. "probably not a valid IFD offset");
  5797. return 0;
  5798. }
  5799. dirsize = 12;
  5800. }
  5801. else
  5802. {
  5803. uint64_t dircount64;
  5804. if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
  5805. {
  5806. TIFFErrorExtR(tif, module,
  5807. "%s: Can not read TIFF directory count",
  5808. tif->tif_name);
  5809. return 0;
  5810. }
  5811. if (tif->tif_flags & TIFF_SWAB)
  5812. TIFFSwabLong8(&dircount64);
  5813. if (dircount64 > 4096)
  5814. {
  5815. TIFFErrorExtR(tif, module,
  5816. "Sanity check on directory count failed, this is "
  5817. "probably not a valid IFD offset");
  5818. return 0;
  5819. }
  5820. dircount16 = (uint16_t)dircount64;
  5821. dirsize = 20;
  5822. }
  5823. origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
  5824. "to read TIFF directory");
  5825. if (origdir == NULL)
  5826. return 0;
  5827. if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
  5828. {
  5829. TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
  5830. tif->tif_name);
  5831. _TIFFfreeExt(tif, origdir);
  5832. return 0;
  5833. }
  5834. /*
  5835. * Read offset to next directory for sequential scans if
  5836. * needed.
  5837. */
  5838. if (nextdiroff)
  5839. {
  5840. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5841. {
  5842. uint32_t nextdiroff32;
  5843. if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
  5844. nextdiroff32 = 0;
  5845. if (tif->tif_flags & TIFF_SWAB)
  5846. TIFFSwabLong(&nextdiroff32);
  5847. *nextdiroff = nextdiroff32;
  5848. }
  5849. else
  5850. {
  5851. if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
  5852. *nextdiroff = 0;
  5853. if (tif->tif_flags & TIFF_SWAB)
  5854. TIFFSwabLong8(nextdiroff);
  5855. }
  5856. }
  5857. }
  5858. else
  5859. {
  5860. tmsize_t m;
  5861. tmsize_t off;
  5862. if (tif->tif_diroff > (uint64_t)INT64_MAX)
  5863. {
  5864. TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
  5865. return (0);
  5866. }
  5867. off = (tmsize_t)tif->tif_diroff;
  5868. /*
  5869. * Check for integer overflow when validating the dir_off,
  5870. * otherwise a very high offset may cause an OOB read and
  5871. * crash the client. Make two comparisons instead of
  5872. *
  5873. * off + sizeof(uint16_t) > tif->tif_size
  5874. *
  5875. * to avoid overflow.
  5876. */
  5877. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5878. {
  5879. m = off + sizeof(uint16_t);
  5880. if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
  5881. (m > tif->tif_size))
  5882. {
  5883. TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
  5884. return 0;
  5885. }
  5886. else
  5887. {
  5888. _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
  5889. }
  5890. off += sizeof(uint16_t);
  5891. if (tif->tif_flags & TIFF_SWAB)
  5892. TIFFSwabShort(&dircount16);
  5893. if (dircount16 > 4096)
  5894. {
  5895. TIFFErrorExtR(tif, module,
  5896. "Sanity check on directory count failed, this is "
  5897. "probably not a valid IFD offset");
  5898. return 0;
  5899. }
  5900. dirsize = 12;
  5901. }
  5902. else
  5903. {
  5904. uint64_t dircount64;
  5905. m = off + sizeof(uint64_t);
  5906. if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
  5907. (m > tif->tif_size))
  5908. {
  5909. TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
  5910. return 0;
  5911. }
  5912. else
  5913. {
  5914. _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
  5915. }
  5916. off += sizeof(uint64_t);
  5917. if (tif->tif_flags & TIFF_SWAB)
  5918. TIFFSwabLong8(&dircount64);
  5919. if (dircount64 > 4096)
  5920. {
  5921. TIFFErrorExtR(tif, module,
  5922. "Sanity check on directory count failed, this is "
  5923. "probably not a valid IFD offset");
  5924. return 0;
  5925. }
  5926. dircount16 = (uint16_t)dircount64;
  5927. dirsize = 20;
  5928. }
  5929. if (dircount16 == 0)
  5930. {
  5931. TIFFErrorExtR(tif, module,
  5932. "Sanity check on directory count failed, zero tag "
  5933. "directories not supported");
  5934. return 0;
  5935. }
  5936. /* Before allocating a huge amount of memory for corrupted files, check
  5937. * if size of requested memory is not greater than file size. */
  5938. uint64_t filesize = TIFFGetFileSize(tif);
  5939. uint64_t allocsize = (uint64_t)dircount16 * dirsize;
  5940. if (allocsize > filesize)
  5941. {
  5942. TIFFWarningExtR(
  5943. tif, module,
  5944. "Requested memory size for TIFF directory of %" PRIu64
  5945. " is greater than filesize %" PRIu64
  5946. ". Memory not allocated, TIFF directory not read",
  5947. allocsize, filesize);
  5948. return 0;
  5949. }
  5950. origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
  5951. "to read TIFF directory");
  5952. if (origdir == NULL)
  5953. return 0;
  5954. m = off + dircount16 * dirsize;
  5955. if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
  5956. (m > tif->tif_size))
  5957. {
  5958. TIFFErrorExtR(tif, module, "Can not read TIFF directory");
  5959. _TIFFfreeExt(tif, origdir);
  5960. return 0;
  5961. }
  5962. else
  5963. {
  5964. _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
  5965. }
  5966. if (nextdiroff)
  5967. {
  5968. off += dircount16 * dirsize;
  5969. if (!(tif->tif_flags & TIFF_BIGTIFF))
  5970. {
  5971. uint32_t nextdiroff32;
  5972. m = off + sizeof(uint32_t);
  5973. if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
  5974. (m > tif->tif_size))
  5975. nextdiroff32 = 0;
  5976. else
  5977. _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
  5978. sizeof(uint32_t));
  5979. if (tif->tif_flags & TIFF_SWAB)
  5980. TIFFSwabLong(&nextdiroff32);
  5981. *nextdiroff = nextdiroff32;
  5982. }
  5983. else
  5984. {
  5985. m = off + sizeof(uint64_t);
  5986. if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
  5987. (m > tif->tif_size))
  5988. *nextdiroff = 0;
  5989. else
  5990. _TIFFmemcpy(nextdiroff, tif->tif_base + off,
  5991. sizeof(uint64_t));
  5992. if (tif->tif_flags & TIFF_SWAB)
  5993. TIFFSwabLong8(nextdiroff);
  5994. }
  5995. }
  5996. }
  5997. /* No check against filesize needed here because "dir" should have same size
  5998. * than "origdir" checked above. */
  5999. dir = (TIFFDirEntry *)_TIFFCheckMalloc(
  6000. tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
  6001. if (dir == 0)
  6002. {
  6003. _TIFFfreeExt(tif, origdir);
  6004. return 0;
  6005. }
  6006. ma = (uint8_t *)origdir;
  6007. mb = dir;
  6008. for (n = 0; n < dircount16; n++)
  6009. {
  6010. mb->tdir_ignore = FALSE;
  6011. if (tif->tif_flags & TIFF_SWAB)
  6012. TIFFSwabShort((uint16_t *)ma);
  6013. mb->tdir_tag = *(uint16_t *)ma;
  6014. ma += sizeof(uint16_t);
  6015. if (tif->tif_flags & TIFF_SWAB)
  6016. TIFFSwabShort((uint16_t *)ma);
  6017. mb->tdir_type = *(uint16_t *)ma;
  6018. ma += sizeof(uint16_t);
  6019. if (!(tif->tif_flags & TIFF_BIGTIFF))
  6020. {
  6021. if (tif->tif_flags & TIFF_SWAB)
  6022. TIFFSwabLong((uint32_t *)ma);
  6023. mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
  6024. ma += sizeof(uint32_t);
  6025. mb->tdir_offset.toff_long8 = 0;
  6026. *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
  6027. ma += sizeof(uint32_t);
  6028. }
  6029. else
  6030. {
  6031. if (tif->tif_flags & TIFF_SWAB)
  6032. TIFFSwabLong8((uint64_t *)ma);
  6033. mb->tdir_count = TIFFReadUInt64(ma);
  6034. ma += sizeof(uint64_t);
  6035. mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
  6036. ma += sizeof(uint64_t);
  6037. }
  6038. mb++;
  6039. }
  6040. _TIFFfreeExt(tif, origdir);
  6041. *pdir = dir;
  6042. return dircount16;
  6043. }
  6044. /*
  6045. * Fetch a tag that is not handled by special case code.
  6046. */
  6047. static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
  6048. {
  6049. static const char module[] = "TIFFFetchNormalTag";
  6050. enum TIFFReadDirEntryErr err;
  6051. uint32_t fii;
  6052. const TIFFField *fip = NULL;
  6053. TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
  6054. if (fii == FAILED_FII)
  6055. {
  6056. TIFFErrorExtR(tif, "TIFFFetchNormalTag",
  6057. "No definition found for tag %" PRIu16, dp->tdir_tag);
  6058. return 0;
  6059. }
  6060. fip = tif->tif_fields[fii];
  6061. assert(fip != NULL); /* should not happen */
  6062. assert(fip->set_get_field_type !=
  6063. TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
  6064. this in specialized code */
  6065. assert(fip->set_get_field_type !=
  6066. TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
  6067. the case for pseudo-tags */
  6068. err = TIFFReadDirEntryErrOk;
  6069. switch (fip->set_get_field_type)
  6070. {
  6071. case TIFF_SETGET_UNDEFINED:
  6072. TIFFErrorExtR(
  6073. tif, "TIFFFetchNormalTag",
  6074. "Defined set_get_field_type of custom tag %u (%s) is "
  6075. "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
  6076. fip->field_tag, fip->field_name);
  6077. break;
  6078. case TIFF_SETGET_ASCII:
  6079. {
  6080. uint8_t *data;
  6081. assert(fip->field_passcount == 0);
  6082. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  6083. if (err == TIFFReadDirEntryErrOk)
  6084. {
  6085. size_t mb = 0;
  6086. int n;
  6087. if (data != NULL)
  6088. {
  6089. if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
  6090. {
  6091. /* optimization: if data is known to be 0 terminated, we
  6092. * can use strlen() */
  6093. mb = strlen((const char *)data);
  6094. }
  6095. else
  6096. {
  6097. /* general case. equivalent to non-portable */
  6098. /* mb = strnlen((const char*)data,
  6099. * (uint32_t)dp->tdir_count); */
  6100. uint8_t *ma = data;
  6101. while (mb < (uint32_t)dp->tdir_count)
  6102. {
  6103. if (*ma == 0)
  6104. break;
  6105. ma++;
  6106. mb++;
  6107. }
  6108. }
  6109. }
  6110. if (!EvaluateIFDdatasizeReading(tif, dp))
  6111. {
  6112. if (data != NULL)
  6113. _TIFFfreeExt(tif, data);
  6114. return (0);
  6115. }
  6116. if (mb + 1 < (uint32_t)dp->tdir_count)
  6117. TIFFWarningExtR(
  6118. tif, module,
  6119. "ASCII value for tag \"%s\" contains null byte in "
  6120. "value; value incorrectly truncated during reading due "
  6121. "to implementation limitations",
  6122. fip->field_name);
  6123. else if (mb + 1 > (uint32_t)dp->tdir_count)
  6124. {
  6125. TIFFWarningExtR(tif, module,
  6126. "ASCII value for tag \"%s\" does not end "
  6127. "in null byte. Forcing it to be null",
  6128. fip->field_name);
  6129. /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
  6130. * larger than MAX_SIZE_TAG_DATA */
  6131. assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
  6132. uint8_t *o =
  6133. _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
  6134. if (o == NULL)
  6135. {
  6136. if (data != NULL)
  6137. _TIFFfreeExt(tif, data);
  6138. return (0);
  6139. }
  6140. if (dp->tdir_count > 0)
  6141. {
  6142. _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
  6143. }
  6144. o[(uint32_t)dp->tdir_count] = 0;
  6145. if (data != 0)
  6146. _TIFFfreeExt(tif, data);
  6147. data = o;
  6148. }
  6149. n = TIFFSetField(tif, dp->tdir_tag, data);
  6150. if (data != 0)
  6151. _TIFFfreeExt(tif, data);
  6152. if (!n)
  6153. return (0);
  6154. }
  6155. }
  6156. break;
  6157. case TIFF_SETGET_UINT8:
  6158. {
  6159. uint8_t data = 0;
  6160. assert(fip->field_readcount == 1);
  6161. assert(fip->field_passcount == 0);
  6162. err = TIFFReadDirEntryByte(tif, dp, &data);
  6163. if (err == TIFFReadDirEntryErrOk)
  6164. {
  6165. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6166. return (0);
  6167. }
  6168. }
  6169. break;
  6170. case TIFF_SETGET_SINT8:
  6171. {
  6172. int8_t data = 0;
  6173. assert(fip->field_readcount == 1);
  6174. assert(fip->field_passcount == 0);
  6175. err = TIFFReadDirEntrySbyte(tif, dp, &data);
  6176. if (err == TIFFReadDirEntryErrOk)
  6177. {
  6178. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6179. return (0);
  6180. }
  6181. }
  6182. break;
  6183. case TIFF_SETGET_UINT16:
  6184. {
  6185. uint16_t data;
  6186. assert(fip->field_readcount == 1);
  6187. assert(fip->field_passcount == 0);
  6188. err = TIFFReadDirEntryShort(tif, dp, &data);
  6189. if (err == TIFFReadDirEntryErrOk)
  6190. {
  6191. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6192. return (0);
  6193. }
  6194. }
  6195. break;
  6196. case TIFF_SETGET_SINT16:
  6197. {
  6198. int16_t data;
  6199. assert(fip->field_readcount == 1);
  6200. assert(fip->field_passcount == 0);
  6201. err = TIFFReadDirEntrySshort(tif, dp, &data);
  6202. if (err == TIFFReadDirEntryErrOk)
  6203. {
  6204. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6205. return (0);
  6206. }
  6207. }
  6208. break;
  6209. case TIFF_SETGET_UINT32:
  6210. {
  6211. uint32_t data;
  6212. assert(fip->field_readcount == 1);
  6213. assert(fip->field_passcount == 0);
  6214. err = TIFFReadDirEntryLong(tif, dp, &data);
  6215. if (err == TIFFReadDirEntryErrOk)
  6216. {
  6217. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6218. return (0);
  6219. }
  6220. }
  6221. break;
  6222. case TIFF_SETGET_SINT32:
  6223. {
  6224. int32_t data;
  6225. assert(fip->field_readcount == 1);
  6226. assert(fip->field_passcount == 0);
  6227. err = TIFFReadDirEntrySlong(tif, dp, &data);
  6228. if (err == TIFFReadDirEntryErrOk)
  6229. {
  6230. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6231. return (0);
  6232. }
  6233. }
  6234. break;
  6235. case TIFF_SETGET_UINT64:
  6236. {
  6237. uint64_t data;
  6238. assert(fip->field_readcount == 1);
  6239. assert(fip->field_passcount == 0);
  6240. err = TIFFReadDirEntryLong8(tif, dp, &data);
  6241. if (err == TIFFReadDirEntryErrOk)
  6242. {
  6243. if (!EvaluateIFDdatasizeReading(tif, dp))
  6244. return 0;
  6245. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6246. return (0);
  6247. }
  6248. }
  6249. break;
  6250. case TIFF_SETGET_SINT64:
  6251. {
  6252. int64_t data;
  6253. assert(fip->field_readcount == 1);
  6254. assert(fip->field_passcount == 0);
  6255. err = TIFFReadDirEntrySlong8(tif, dp, &data);
  6256. if (err == TIFFReadDirEntryErrOk)
  6257. {
  6258. if (!EvaluateIFDdatasizeReading(tif, dp))
  6259. return 0;
  6260. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6261. return (0);
  6262. }
  6263. }
  6264. break;
  6265. case TIFF_SETGET_FLOAT:
  6266. {
  6267. float data;
  6268. assert(fip->field_readcount == 1);
  6269. assert(fip->field_passcount == 0);
  6270. err = TIFFReadDirEntryFloat(tif, dp, &data);
  6271. if (err == TIFFReadDirEntryErrOk)
  6272. {
  6273. if (!EvaluateIFDdatasizeReading(tif, dp))
  6274. return 0;
  6275. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6276. return (0);
  6277. }
  6278. }
  6279. break;
  6280. case TIFF_SETGET_DOUBLE:
  6281. {
  6282. double data;
  6283. assert(fip->field_readcount == 1);
  6284. assert(fip->field_passcount == 0);
  6285. err = TIFFReadDirEntryDouble(tif, dp, &data);
  6286. if (err == TIFFReadDirEntryErrOk)
  6287. {
  6288. if (!EvaluateIFDdatasizeReading(tif, dp))
  6289. return 0;
  6290. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6291. return (0);
  6292. }
  6293. }
  6294. break;
  6295. case TIFF_SETGET_IFD8:
  6296. {
  6297. uint64_t data;
  6298. assert(fip->field_readcount == 1);
  6299. assert(fip->field_passcount == 0);
  6300. err = TIFFReadDirEntryIfd8(tif, dp, &data);
  6301. if (err == TIFFReadDirEntryErrOk)
  6302. {
  6303. if (!EvaluateIFDdatasizeReading(tif, dp))
  6304. return 0;
  6305. if (!TIFFSetField(tif, dp->tdir_tag, data))
  6306. return (0);
  6307. }
  6308. }
  6309. break;
  6310. case TIFF_SETGET_UINT16_PAIR:
  6311. {
  6312. uint16_t *data;
  6313. assert(fip->field_readcount == 2);
  6314. assert(fip->field_passcount == 0);
  6315. if (dp->tdir_count != 2)
  6316. {
  6317. TIFFWarningExtR(tif, module,
  6318. "incorrect count for field \"%s\", expected 2, "
  6319. "got %" PRIu64,
  6320. fip->field_name, dp->tdir_count);
  6321. return (0);
  6322. }
  6323. err = TIFFReadDirEntryShortArray(tif, dp, &data);
  6324. if (err == TIFFReadDirEntryErrOk)
  6325. {
  6326. int m;
  6327. assert(data); /* avoid CLang static Analyzer false positive */
  6328. m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
  6329. _TIFFfreeExt(tif, data);
  6330. if (!m)
  6331. return (0);
  6332. }
  6333. }
  6334. break;
  6335. case TIFF_SETGET_C0_UINT8:
  6336. {
  6337. uint8_t *data;
  6338. assert(fip->field_readcount >= 1);
  6339. assert(fip->field_passcount == 0);
  6340. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6341. {
  6342. TIFFWarningExtR(tif, module,
  6343. "incorrect count for field \"%s\", expected "
  6344. "%d, got %" PRIu64,
  6345. fip->field_name, (int)fip->field_readcount,
  6346. dp->tdir_count);
  6347. return (0);
  6348. }
  6349. else
  6350. {
  6351. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  6352. if (err == TIFFReadDirEntryErrOk)
  6353. {
  6354. if (!EvaluateIFDdatasizeReading(tif, dp))
  6355. {
  6356. if (data != 0)
  6357. _TIFFfreeExt(tif, data);
  6358. return 0;
  6359. }
  6360. int m;
  6361. m = TIFFSetField(tif, dp->tdir_tag, data);
  6362. if (data != 0)
  6363. _TIFFfreeExt(tif, data);
  6364. if (!m)
  6365. return (0);
  6366. }
  6367. }
  6368. }
  6369. break;
  6370. case TIFF_SETGET_C0_SINT8:
  6371. {
  6372. int8_t *data;
  6373. assert(fip->field_readcount >= 1);
  6374. assert(fip->field_passcount == 0);
  6375. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6376. {
  6377. TIFFWarningExtR(tif, module,
  6378. "incorrect count for field \"%s\", expected "
  6379. "%d, got %" PRIu64,
  6380. fip->field_name, (int)fip->field_readcount,
  6381. dp->tdir_count);
  6382. return (0);
  6383. }
  6384. else
  6385. {
  6386. err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
  6387. if (err == TIFFReadDirEntryErrOk)
  6388. {
  6389. if (!EvaluateIFDdatasizeReading(tif, dp))
  6390. {
  6391. if (data != 0)
  6392. _TIFFfreeExt(tif, data);
  6393. return 0;
  6394. }
  6395. int m;
  6396. m = TIFFSetField(tif, dp->tdir_tag, data);
  6397. if (data != 0)
  6398. _TIFFfreeExt(tif, data);
  6399. if (!m)
  6400. return (0);
  6401. }
  6402. }
  6403. }
  6404. break;
  6405. case TIFF_SETGET_C0_UINT16:
  6406. {
  6407. uint16_t *data;
  6408. assert(fip->field_readcount >= 1);
  6409. assert(fip->field_passcount == 0);
  6410. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6411. {
  6412. TIFFWarningExtR(tif, module,
  6413. "incorrect count for field \"%s\", expected "
  6414. "%d, got %" PRIu64,
  6415. fip->field_name, (int)fip->field_readcount,
  6416. dp->tdir_count);
  6417. return (0);
  6418. }
  6419. else
  6420. {
  6421. err = TIFFReadDirEntryShortArray(tif, dp, &data);
  6422. if (err == TIFFReadDirEntryErrOk)
  6423. {
  6424. if (!EvaluateIFDdatasizeReading(tif, dp))
  6425. {
  6426. if (data != 0)
  6427. _TIFFfreeExt(tif, data);
  6428. return 0;
  6429. }
  6430. int m;
  6431. m = TIFFSetField(tif, dp->tdir_tag, data);
  6432. if (data != 0)
  6433. _TIFFfreeExt(tif, data);
  6434. if (!m)
  6435. return (0);
  6436. }
  6437. }
  6438. }
  6439. break;
  6440. case TIFF_SETGET_C0_SINT16:
  6441. {
  6442. int16_t *data;
  6443. assert(fip->field_readcount >= 1);
  6444. assert(fip->field_passcount == 0);
  6445. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6446. {
  6447. TIFFWarningExtR(tif, module,
  6448. "incorrect count for field \"%s\", expected "
  6449. "%d, got %" PRIu64,
  6450. fip->field_name, (int)fip->field_readcount,
  6451. dp->tdir_count);
  6452. return (0);
  6453. }
  6454. else
  6455. {
  6456. err = TIFFReadDirEntrySshortArray(tif, dp, &data);
  6457. if (err == TIFFReadDirEntryErrOk)
  6458. {
  6459. if (!EvaluateIFDdatasizeReading(tif, dp))
  6460. {
  6461. if (data != 0)
  6462. _TIFFfreeExt(tif, data);
  6463. return 0;
  6464. }
  6465. int m;
  6466. m = TIFFSetField(tif, dp->tdir_tag, data);
  6467. if (data != 0)
  6468. _TIFFfreeExt(tif, data);
  6469. if (!m)
  6470. return (0);
  6471. }
  6472. }
  6473. }
  6474. break;
  6475. case TIFF_SETGET_C0_UINT32:
  6476. {
  6477. uint32_t *data;
  6478. assert(fip->field_readcount >= 1);
  6479. assert(fip->field_passcount == 0);
  6480. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6481. {
  6482. TIFFWarningExtR(tif, module,
  6483. "incorrect count for field \"%s\", expected "
  6484. "%d, got %" PRIu64,
  6485. fip->field_name, (int)fip->field_readcount,
  6486. dp->tdir_count);
  6487. return (0);
  6488. }
  6489. else
  6490. {
  6491. err = TIFFReadDirEntryLongArray(tif, dp, &data);
  6492. if (err == TIFFReadDirEntryErrOk)
  6493. {
  6494. if (!EvaluateIFDdatasizeReading(tif, dp))
  6495. {
  6496. if (data != 0)
  6497. _TIFFfreeExt(tif, data);
  6498. return 0;
  6499. }
  6500. int m;
  6501. m = TIFFSetField(tif, dp->tdir_tag, data);
  6502. if (data != 0)
  6503. _TIFFfreeExt(tif, data);
  6504. if (!m)
  6505. return (0);
  6506. }
  6507. }
  6508. }
  6509. break;
  6510. case TIFF_SETGET_C0_SINT32:
  6511. {
  6512. int32_t *data;
  6513. assert(fip->field_readcount >= 1);
  6514. assert(fip->field_passcount == 0);
  6515. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6516. {
  6517. TIFFWarningExtR(tif, module,
  6518. "incorrect count for field \"%s\", expected "
  6519. "%d, got %" PRIu64,
  6520. fip->field_name, (int)fip->field_readcount,
  6521. dp->tdir_count);
  6522. return (0);
  6523. }
  6524. else
  6525. {
  6526. err = TIFFReadDirEntrySlongArray(tif, dp, &data);
  6527. if (err == TIFFReadDirEntryErrOk)
  6528. {
  6529. if (!EvaluateIFDdatasizeReading(tif, dp))
  6530. {
  6531. if (data != 0)
  6532. _TIFFfreeExt(tif, data);
  6533. return 0;
  6534. }
  6535. int m;
  6536. m = TIFFSetField(tif, dp->tdir_tag, data);
  6537. if (data != 0)
  6538. _TIFFfreeExt(tif, data);
  6539. if (!m)
  6540. return (0);
  6541. }
  6542. }
  6543. }
  6544. break;
  6545. case TIFF_SETGET_C0_UINT64:
  6546. {
  6547. uint64_t *data;
  6548. assert(fip->field_readcount >= 1);
  6549. assert(fip->field_passcount == 0);
  6550. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6551. {
  6552. TIFFWarningExtR(tif, module,
  6553. "incorrect count for field \"%s\", expected "
  6554. "%d, got %" PRIu64,
  6555. fip->field_name, (int)fip->field_readcount,
  6556. dp->tdir_count);
  6557. return (0);
  6558. }
  6559. else
  6560. {
  6561. err = TIFFReadDirEntryLong8Array(tif, dp, &data);
  6562. if (err == TIFFReadDirEntryErrOk)
  6563. {
  6564. if (!EvaluateIFDdatasizeReading(tif, dp))
  6565. {
  6566. if (data != 0)
  6567. _TIFFfreeExt(tif, data);
  6568. return 0;
  6569. }
  6570. int m;
  6571. m = TIFFSetField(tif, dp->tdir_tag, data);
  6572. if (data != 0)
  6573. _TIFFfreeExt(tif, data);
  6574. if (!m)
  6575. return (0);
  6576. }
  6577. }
  6578. }
  6579. break;
  6580. case TIFF_SETGET_C0_SINT64:
  6581. {
  6582. int64_t *data;
  6583. assert(fip->field_readcount >= 1);
  6584. assert(fip->field_passcount == 0);
  6585. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6586. {
  6587. TIFFWarningExtR(tif, module,
  6588. "incorrect count for field \"%s\", expected "
  6589. "%d, got %" PRIu64,
  6590. fip->field_name, (int)fip->field_readcount,
  6591. dp->tdir_count);
  6592. return (0);
  6593. }
  6594. else
  6595. {
  6596. err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
  6597. if (err == TIFFReadDirEntryErrOk)
  6598. {
  6599. if (!EvaluateIFDdatasizeReading(tif, dp))
  6600. {
  6601. if (data != 0)
  6602. _TIFFfreeExt(tif, data);
  6603. return 0;
  6604. }
  6605. int m;
  6606. m = TIFFSetField(tif, dp->tdir_tag, data);
  6607. if (data != 0)
  6608. _TIFFfreeExt(tif, data);
  6609. if (!m)
  6610. return (0);
  6611. }
  6612. }
  6613. }
  6614. break;
  6615. case TIFF_SETGET_C0_FLOAT:
  6616. {
  6617. float *data;
  6618. assert(fip->field_readcount >= 1);
  6619. assert(fip->field_passcount == 0);
  6620. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6621. {
  6622. TIFFWarningExtR(tif, module,
  6623. "incorrect count for field \"%s\", expected "
  6624. "%d, got %" PRIu64,
  6625. fip->field_name, (int)fip->field_readcount,
  6626. dp->tdir_count);
  6627. return (0);
  6628. }
  6629. else
  6630. {
  6631. err = TIFFReadDirEntryFloatArray(tif, dp, &data);
  6632. if (err == TIFFReadDirEntryErrOk)
  6633. {
  6634. if (!EvaluateIFDdatasizeReading(tif, dp))
  6635. {
  6636. if (data != 0)
  6637. _TIFFfreeExt(tif, data);
  6638. return 0;
  6639. }
  6640. int m;
  6641. m = TIFFSetField(tif, dp->tdir_tag, data);
  6642. if (data != 0)
  6643. _TIFFfreeExt(tif, data);
  6644. if (!m)
  6645. return (0);
  6646. }
  6647. }
  6648. }
  6649. break;
  6650. /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
  6651. * into Double-Arrays. */
  6652. case TIFF_SETGET_C0_DOUBLE:
  6653. {
  6654. double *data;
  6655. assert(fip->field_readcount >= 1);
  6656. assert(fip->field_passcount == 0);
  6657. if (dp->tdir_count != (uint64_t)fip->field_readcount)
  6658. {
  6659. TIFFWarningExtR(tif, module,
  6660. "incorrect count for field \"%s\", expected "
  6661. "%d, got %" PRIu64,
  6662. fip->field_name, (int)fip->field_readcount,
  6663. dp->tdir_count);
  6664. return (0);
  6665. }
  6666. else
  6667. {
  6668. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  6669. if (err == TIFFReadDirEntryErrOk)
  6670. {
  6671. if (!EvaluateIFDdatasizeReading(tif, dp))
  6672. {
  6673. if (data != 0)
  6674. _TIFFfreeExt(tif, data);
  6675. return 0;
  6676. }
  6677. int m;
  6678. m = TIFFSetField(tif, dp->tdir_tag, data);
  6679. if (data != 0)
  6680. _TIFFfreeExt(tif, data);
  6681. if (!m)
  6682. return (0);
  6683. }
  6684. }
  6685. }
  6686. break;
  6687. case TIFF_SETGET_C16_ASCII:
  6688. {
  6689. uint8_t *data;
  6690. assert(fip->field_readcount == TIFF_VARIABLE);
  6691. assert(fip->field_passcount == 1);
  6692. if (dp->tdir_count > 0xFFFF)
  6693. err = TIFFReadDirEntryErrCount;
  6694. else
  6695. {
  6696. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  6697. if (err == TIFFReadDirEntryErrOk)
  6698. {
  6699. if (!EvaluateIFDdatasizeReading(tif, dp))
  6700. {
  6701. if (data != 0)
  6702. _TIFFfreeExt(tif, data);
  6703. return 0;
  6704. }
  6705. int m;
  6706. if (data != 0 && dp->tdir_count > 0 &&
  6707. data[dp->tdir_count - 1] != '\0')
  6708. {
  6709. TIFFWarningExtR(tif, module,
  6710. "ASCII value for ASCII array tag "
  6711. "\"%s\" does not end in null "
  6712. "byte. Forcing it to be null",
  6713. fip->field_name);
  6714. /* Enlarge buffer and add terminating null. */
  6715. uint8_t *o =
  6716. _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
  6717. if (o == NULL)
  6718. {
  6719. if (data != NULL)
  6720. _TIFFfreeExt(tif, data);
  6721. return (0);
  6722. }
  6723. if (dp->tdir_count > 0)
  6724. {
  6725. _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
  6726. }
  6727. o[(uint32_t)dp->tdir_count] = 0;
  6728. dp->tdir_count++; /* Increment for added null. */
  6729. if (data != 0)
  6730. _TIFFfreeExt(tif, data);
  6731. data = o;
  6732. }
  6733. m = TIFFSetField(tif, dp->tdir_tag,
  6734. (uint16_t)(dp->tdir_count), data);
  6735. if (data != 0)
  6736. _TIFFfreeExt(tif, data);
  6737. if (!m)
  6738. return (0);
  6739. }
  6740. }
  6741. }
  6742. break;
  6743. case TIFF_SETGET_C16_UINT8:
  6744. {
  6745. uint8_t *data;
  6746. assert(fip->field_readcount == TIFF_VARIABLE);
  6747. assert(fip->field_passcount == 1);
  6748. if (dp->tdir_count > 0xFFFF)
  6749. err = TIFFReadDirEntryErrCount;
  6750. else
  6751. {
  6752. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  6753. if (err == TIFFReadDirEntryErrOk)
  6754. {
  6755. if (!EvaluateIFDdatasizeReading(tif, dp))
  6756. {
  6757. if (data != 0)
  6758. _TIFFfreeExt(tif, data);
  6759. return 0;
  6760. }
  6761. int m;
  6762. m = TIFFSetField(tif, dp->tdir_tag,
  6763. (uint16_t)(dp->tdir_count), data);
  6764. if (data != 0)
  6765. _TIFFfreeExt(tif, data);
  6766. if (!m)
  6767. return (0);
  6768. }
  6769. }
  6770. }
  6771. break;
  6772. case TIFF_SETGET_C16_SINT8:
  6773. {
  6774. int8_t *data;
  6775. assert(fip->field_readcount == TIFF_VARIABLE);
  6776. assert(fip->field_passcount == 1);
  6777. if (dp->tdir_count > 0xFFFF)
  6778. err = TIFFReadDirEntryErrCount;
  6779. else
  6780. {
  6781. err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
  6782. if (err == TIFFReadDirEntryErrOk)
  6783. {
  6784. if (!EvaluateIFDdatasizeReading(tif, dp))
  6785. {
  6786. if (data != 0)
  6787. _TIFFfreeExt(tif, data);
  6788. return 0;
  6789. }
  6790. int m;
  6791. m = TIFFSetField(tif, dp->tdir_tag,
  6792. (uint16_t)(dp->tdir_count), data);
  6793. if (data != 0)
  6794. _TIFFfreeExt(tif, data);
  6795. if (!m)
  6796. return (0);
  6797. }
  6798. }
  6799. }
  6800. break;
  6801. case TIFF_SETGET_C16_UINT16:
  6802. {
  6803. uint16_t *data;
  6804. assert(fip->field_readcount == TIFF_VARIABLE);
  6805. assert(fip->field_passcount == 1);
  6806. if (dp->tdir_count > 0xFFFF)
  6807. err = TIFFReadDirEntryErrCount;
  6808. else
  6809. {
  6810. err = TIFFReadDirEntryShortArray(tif, dp, &data);
  6811. if (err == TIFFReadDirEntryErrOk)
  6812. {
  6813. if (!EvaluateIFDdatasizeReading(tif, dp))
  6814. {
  6815. if (data != 0)
  6816. _TIFFfreeExt(tif, data);
  6817. return 0;
  6818. }
  6819. int m;
  6820. m = TIFFSetField(tif, dp->tdir_tag,
  6821. (uint16_t)(dp->tdir_count), data);
  6822. if (data != 0)
  6823. _TIFFfreeExt(tif, data);
  6824. if (!m)
  6825. return (0);
  6826. }
  6827. }
  6828. }
  6829. break;
  6830. case TIFF_SETGET_C16_SINT16:
  6831. {
  6832. int16_t *data;
  6833. assert(fip->field_readcount == TIFF_VARIABLE);
  6834. assert(fip->field_passcount == 1);
  6835. if (dp->tdir_count > 0xFFFF)
  6836. err = TIFFReadDirEntryErrCount;
  6837. else
  6838. {
  6839. err = TIFFReadDirEntrySshortArray(tif, dp, &data);
  6840. if (err == TIFFReadDirEntryErrOk)
  6841. {
  6842. if (!EvaluateIFDdatasizeReading(tif, dp))
  6843. {
  6844. if (data != 0)
  6845. _TIFFfreeExt(tif, data);
  6846. return 0;
  6847. }
  6848. int m;
  6849. m = TIFFSetField(tif, dp->tdir_tag,
  6850. (uint16_t)(dp->tdir_count), data);
  6851. if (data != 0)
  6852. _TIFFfreeExt(tif, data);
  6853. if (!m)
  6854. return (0);
  6855. }
  6856. }
  6857. }
  6858. break;
  6859. case TIFF_SETGET_C16_UINT32:
  6860. {
  6861. uint32_t *data;
  6862. assert(fip->field_readcount == TIFF_VARIABLE);
  6863. assert(fip->field_passcount == 1);
  6864. if (dp->tdir_count > 0xFFFF)
  6865. err = TIFFReadDirEntryErrCount;
  6866. else
  6867. {
  6868. err = TIFFReadDirEntryLongArray(tif, dp, &data);
  6869. if (err == TIFFReadDirEntryErrOk)
  6870. {
  6871. if (!EvaluateIFDdatasizeReading(tif, dp))
  6872. {
  6873. if (data != 0)
  6874. _TIFFfreeExt(tif, data);
  6875. return 0;
  6876. }
  6877. int m;
  6878. m = TIFFSetField(tif, dp->tdir_tag,
  6879. (uint16_t)(dp->tdir_count), data);
  6880. if (data != 0)
  6881. _TIFFfreeExt(tif, data);
  6882. if (!m)
  6883. return (0);
  6884. }
  6885. }
  6886. }
  6887. break;
  6888. case TIFF_SETGET_C16_SINT32:
  6889. {
  6890. int32_t *data;
  6891. assert(fip->field_readcount == TIFF_VARIABLE);
  6892. assert(fip->field_passcount == 1);
  6893. if (dp->tdir_count > 0xFFFF)
  6894. err = TIFFReadDirEntryErrCount;
  6895. else
  6896. {
  6897. err = TIFFReadDirEntrySlongArray(tif, dp, &data);
  6898. if (err == TIFFReadDirEntryErrOk)
  6899. {
  6900. if (!EvaluateIFDdatasizeReading(tif, dp))
  6901. {
  6902. if (data != 0)
  6903. _TIFFfreeExt(tif, data);
  6904. return 0;
  6905. }
  6906. int m;
  6907. m = TIFFSetField(tif, dp->tdir_tag,
  6908. (uint16_t)(dp->tdir_count), data);
  6909. if (data != 0)
  6910. _TIFFfreeExt(tif, data);
  6911. if (!m)
  6912. return (0);
  6913. }
  6914. }
  6915. }
  6916. break;
  6917. case TIFF_SETGET_C16_UINT64:
  6918. {
  6919. uint64_t *data;
  6920. assert(fip->field_readcount == TIFF_VARIABLE);
  6921. assert(fip->field_passcount == 1);
  6922. if (dp->tdir_count > 0xFFFF)
  6923. err = TIFFReadDirEntryErrCount;
  6924. else
  6925. {
  6926. err = TIFFReadDirEntryLong8Array(tif, dp, &data);
  6927. if (err == TIFFReadDirEntryErrOk)
  6928. {
  6929. if (!EvaluateIFDdatasizeReading(tif, dp))
  6930. {
  6931. if (data != 0)
  6932. _TIFFfreeExt(tif, data);
  6933. return 0;
  6934. }
  6935. int m;
  6936. m = TIFFSetField(tif, dp->tdir_tag,
  6937. (uint16_t)(dp->tdir_count), data);
  6938. if (data != 0)
  6939. _TIFFfreeExt(tif, data);
  6940. if (!m)
  6941. return (0);
  6942. }
  6943. }
  6944. }
  6945. break;
  6946. case TIFF_SETGET_C16_SINT64:
  6947. {
  6948. int64_t *data;
  6949. assert(fip->field_readcount == TIFF_VARIABLE);
  6950. assert(fip->field_passcount == 1);
  6951. if (dp->tdir_count > 0xFFFF)
  6952. err = TIFFReadDirEntryErrCount;
  6953. else
  6954. {
  6955. err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
  6956. if (err == TIFFReadDirEntryErrOk)
  6957. {
  6958. if (!EvaluateIFDdatasizeReading(tif, dp))
  6959. {
  6960. if (data != 0)
  6961. _TIFFfreeExt(tif, data);
  6962. return 0;
  6963. }
  6964. int m;
  6965. m = TIFFSetField(tif, dp->tdir_tag,
  6966. (uint16_t)(dp->tdir_count), data);
  6967. if (data != 0)
  6968. _TIFFfreeExt(tif, data);
  6969. if (!m)
  6970. return (0);
  6971. }
  6972. }
  6973. }
  6974. break;
  6975. case TIFF_SETGET_C16_FLOAT:
  6976. {
  6977. float *data;
  6978. assert(fip->field_readcount == TIFF_VARIABLE);
  6979. assert(fip->field_passcount == 1);
  6980. if (dp->tdir_count > 0xFFFF)
  6981. err = TIFFReadDirEntryErrCount;
  6982. else
  6983. {
  6984. err = TIFFReadDirEntryFloatArray(tif, dp, &data);
  6985. if (err == TIFFReadDirEntryErrOk)
  6986. {
  6987. if (!EvaluateIFDdatasizeReading(tif, dp))
  6988. {
  6989. if (data != 0)
  6990. _TIFFfreeExt(tif, data);
  6991. return 0;
  6992. }
  6993. int m;
  6994. m = TIFFSetField(tif, dp->tdir_tag,
  6995. (uint16_t)(dp->tdir_count), data);
  6996. if (data != 0)
  6997. _TIFFfreeExt(tif, data);
  6998. if (!m)
  6999. return (0);
  7000. }
  7001. }
  7002. }
  7003. break;
  7004. case TIFF_SETGET_C16_DOUBLE:
  7005. {
  7006. double *data;
  7007. assert(fip->field_readcount == TIFF_VARIABLE);
  7008. assert(fip->field_passcount == 1);
  7009. if (dp->tdir_count > 0xFFFF)
  7010. err = TIFFReadDirEntryErrCount;
  7011. else
  7012. {
  7013. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  7014. if (err == TIFFReadDirEntryErrOk)
  7015. {
  7016. if (!EvaluateIFDdatasizeReading(tif, dp))
  7017. {
  7018. if (data != 0)
  7019. _TIFFfreeExt(tif, data);
  7020. return 0;
  7021. }
  7022. int m;
  7023. m = TIFFSetField(tif, dp->tdir_tag,
  7024. (uint16_t)(dp->tdir_count), data);
  7025. if (data != 0)
  7026. _TIFFfreeExt(tif, data);
  7027. if (!m)
  7028. return (0);
  7029. }
  7030. }
  7031. }
  7032. break;
  7033. case TIFF_SETGET_C16_IFD8:
  7034. {
  7035. uint64_t *data;
  7036. assert(fip->field_readcount == TIFF_VARIABLE);
  7037. assert(fip->field_passcount == 1);
  7038. if (dp->tdir_count > 0xFFFF)
  7039. err = TIFFReadDirEntryErrCount;
  7040. else
  7041. {
  7042. err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
  7043. if (err == TIFFReadDirEntryErrOk)
  7044. {
  7045. if (!EvaluateIFDdatasizeReading(tif, dp))
  7046. {
  7047. if (data != 0)
  7048. _TIFFfreeExt(tif, data);
  7049. return 0;
  7050. }
  7051. int m;
  7052. m = TIFFSetField(tif, dp->tdir_tag,
  7053. (uint16_t)(dp->tdir_count), data);
  7054. if (data != 0)
  7055. _TIFFfreeExt(tif, data);
  7056. if (!m)
  7057. return (0);
  7058. }
  7059. }
  7060. }
  7061. break;
  7062. case TIFF_SETGET_C32_ASCII:
  7063. {
  7064. uint8_t *data;
  7065. assert(fip->field_readcount == TIFF_VARIABLE2);
  7066. assert(fip->field_passcount == 1);
  7067. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  7068. if (err == TIFFReadDirEntryErrOk)
  7069. {
  7070. if (!EvaluateIFDdatasizeReading(tif, dp))
  7071. {
  7072. if (data != 0)
  7073. _TIFFfreeExt(tif, data);
  7074. return 0;
  7075. }
  7076. int m;
  7077. if (data != 0 && dp->tdir_count > 0 &&
  7078. data[dp->tdir_count - 1] != '\0')
  7079. {
  7080. TIFFWarningExtR(
  7081. tif, module,
  7082. "ASCII value for ASCII array tag \"%s\" does not end "
  7083. "in null byte. Forcing it to be null",
  7084. fip->field_name);
  7085. /* Enlarge buffer and add terminating null. */
  7086. uint8_t *o =
  7087. _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
  7088. if (o == NULL)
  7089. {
  7090. if (data != NULL)
  7091. _TIFFfreeExt(tif, data);
  7092. return (0);
  7093. }
  7094. if (dp->tdir_count > 0)
  7095. {
  7096. _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
  7097. }
  7098. o[(uint32_t)dp->tdir_count] = 0;
  7099. dp->tdir_count++; /* Increment for added null. */
  7100. if (data != 0)
  7101. _TIFFfreeExt(tif, data);
  7102. data = o;
  7103. }
  7104. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7105. data);
  7106. if (data != 0)
  7107. _TIFFfreeExt(tif, data);
  7108. if (!m)
  7109. return (0);
  7110. }
  7111. }
  7112. break;
  7113. case TIFF_SETGET_C32_UINT8:
  7114. {
  7115. uint8_t *data;
  7116. uint32_t count = 0;
  7117. assert(fip->field_readcount == TIFF_VARIABLE2);
  7118. assert(fip->field_passcount == 1);
  7119. if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
  7120. dp->tdir_type == TIFF_LONG)
  7121. {
  7122. /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
  7123. * data type LONG instead of UNDEFINED. Work around this
  7124. * frequently found issue */
  7125. void *origdata;
  7126. err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
  7127. if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
  7128. {
  7129. data = NULL;
  7130. }
  7131. else
  7132. {
  7133. if (tif->tif_flags & TIFF_SWAB)
  7134. TIFFSwabArrayOfLong((uint32_t *)origdata, count);
  7135. data = (uint8_t *)origdata;
  7136. count = (uint32_t)(count * 4);
  7137. }
  7138. }
  7139. else
  7140. {
  7141. err = TIFFReadDirEntryByteArray(tif, dp, &data);
  7142. count = (uint32_t)(dp->tdir_count);
  7143. }
  7144. if (err == TIFFReadDirEntryErrOk)
  7145. {
  7146. if (!EvaluateIFDdatasizeReading(tif, dp))
  7147. {
  7148. if (data != 0)
  7149. _TIFFfreeExt(tif, data);
  7150. return 0;
  7151. }
  7152. int m;
  7153. m = TIFFSetField(tif, dp->tdir_tag, count, data);
  7154. if (data != 0)
  7155. _TIFFfreeExt(tif, data);
  7156. if (!m)
  7157. return (0);
  7158. }
  7159. }
  7160. break;
  7161. case TIFF_SETGET_C32_SINT8:
  7162. {
  7163. int8_t *data = NULL;
  7164. assert(fip->field_readcount == TIFF_VARIABLE2);
  7165. assert(fip->field_passcount == 1);
  7166. err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
  7167. if (err == TIFFReadDirEntryErrOk)
  7168. {
  7169. if (!EvaluateIFDdatasizeReading(tif, dp))
  7170. {
  7171. if (data != 0)
  7172. _TIFFfreeExt(tif, data);
  7173. return 0;
  7174. }
  7175. int m;
  7176. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7177. data);
  7178. if (data != 0)
  7179. _TIFFfreeExt(tif, data);
  7180. if (!m)
  7181. return (0);
  7182. }
  7183. }
  7184. break;
  7185. case TIFF_SETGET_C32_UINT16:
  7186. {
  7187. uint16_t *data;
  7188. assert(fip->field_readcount == TIFF_VARIABLE2);
  7189. assert(fip->field_passcount == 1);
  7190. err = TIFFReadDirEntryShortArray(tif, dp, &data);
  7191. if (err == TIFFReadDirEntryErrOk)
  7192. {
  7193. if (!EvaluateIFDdatasizeReading(tif, dp))
  7194. {
  7195. if (data != 0)
  7196. _TIFFfreeExt(tif, data);
  7197. return 0;
  7198. }
  7199. int m;
  7200. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7201. data);
  7202. if (data != 0)
  7203. _TIFFfreeExt(tif, data);
  7204. if (!m)
  7205. return (0);
  7206. }
  7207. }
  7208. break;
  7209. case TIFF_SETGET_C32_SINT16:
  7210. {
  7211. int16_t *data = NULL;
  7212. assert(fip->field_readcount == TIFF_VARIABLE2);
  7213. assert(fip->field_passcount == 1);
  7214. err = TIFFReadDirEntrySshortArray(tif, dp, &data);
  7215. if (err == TIFFReadDirEntryErrOk)
  7216. {
  7217. if (!EvaluateIFDdatasizeReading(tif, dp))
  7218. {
  7219. if (data != 0)
  7220. _TIFFfreeExt(tif, data);
  7221. return 0;
  7222. }
  7223. int m;
  7224. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7225. data);
  7226. if (data != 0)
  7227. _TIFFfreeExt(tif, data);
  7228. if (!m)
  7229. return (0);
  7230. }
  7231. }
  7232. break;
  7233. case TIFF_SETGET_C32_UINT32:
  7234. {
  7235. uint32_t *data;
  7236. assert(fip->field_readcount == TIFF_VARIABLE2);
  7237. assert(fip->field_passcount == 1);
  7238. err = TIFFReadDirEntryLongArray(tif, dp, &data);
  7239. if (err == TIFFReadDirEntryErrOk)
  7240. {
  7241. if (!EvaluateIFDdatasizeReading(tif, dp))
  7242. {
  7243. if (data != 0)
  7244. _TIFFfreeExt(tif, data);
  7245. return 0;
  7246. }
  7247. int m;
  7248. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7249. data);
  7250. if (data != 0)
  7251. _TIFFfreeExt(tif, data);
  7252. if (!m)
  7253. return (0);
  7254. }
  7255. }
  7256. break;
  7257. case TIFF_SETGET_C32_SINT32:
  7258. {
  7259. int32_t *data = NULL;
  7260. assert(fip->field_readcount == TIFF_VARIABLE2);
  7261. assert(fip->field_passcount == 1);
  7262. err = TIFFReadDirEntrySlongArray(tif, dp, &data);
  7263. if (err == TIFFReadDirEntryErrOk)
  7264. {
  7265. if (!EvaluateIFDdatasizeReading(tif, dp))
  7266. {
  7267. if (data != 0)
  7268. _TIFFfreeExt(tif, data);
  7269. return 0;
  7270. }
  7271. int m;
  7272. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7273. data);
  7274. if (data != 0)
  7275. _TIFFfreeExt(tif, data);
  7276. if (!m)
  7277. return (0);
  7278. }
  7279. }
  7280. break;
  7281. case TIFF_SETGET_C32_UINT64:
  7282. {
  7283. uint64_t *data;
  7284. assert(fip->field_readcount == TIFF_VARIABLE2);
  7285. assert(fip->field_passcount == 1);
  7286. err = TIFFReadDirEntryLong8Array(tif, dp, &data);
  7287. if (err == TIFFReadDirEntryErrOk)
  7288. {
  7289. if (!EvaluateIFDdatasizeReading(tif, dp))
  7290. {
  7291. if (data != 0)
  7292. _TIFFfreeExt(tif, data);
  7293. return 0;
  7294. }
  7295. int m;
  7296. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7297. data);
  7298. if (data != 0)
  7299. _TIFFfreeExt(tif, data);
  7300. if (!m)
  7301. return (0);
  7302. }
  7303. }
  7304. break;
  7305. case TIFF_SETGET_C32_SINT64:
  7306. {
  7307. int64_t *data = NULL;
  7308. assert(fip->field_readcount == TIFF_VARIABLE2);
  7309. assert(fip->field_passcount == 1);
  7310. err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
  7311. if (err == TIFFReadDirEntryErrOk)
  7312. {
  7313. if (!EvaluateIFDdatasizeReading(tif, dp))
  7314. {
  7315. if (data != 0)
  7316. _TIFFfreeExt(tif, data);
  7317. return 0;
  7318. }
  7319. int m;
  7320. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7321. data);
  7322. if (data != 0)
  7323. _TIFFfreeExt(tif, data);
  7324. if (!m)
  7325. return (0);
  7326. }
  7327. }
  7328. break;
  7329. case TIFF_SETGET_C32_FLOAT:
  7330. {
  7331. float *data;
  7332. assert(fip->field_readcount == TIFF_VARIABLE2);
  7333. assert(fip->field_passcount == 1);
  7334. err = TIFFReadDirEntryFloatArray(tif, dp, &data);
  7335. if (err == TIFFReadDirEntryErrOk)
  7336. {
  7337. if (!EvaluateIFDdatasizeReading(tif, dp))
  7338. {
  7339. if (data != 0)
  7340. _TIFFfreeExt(tif, data);
  7341. return 0;
  7342. }
  7343. int m;
  7344. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7345. data);
  7346. if (data != 0)
  7347. _TIFFfreeExt(tif, data);
  7348. if (!m)
  7349. return (0);
  7350. }
  7351. }
  7352. break;
  7353. case TIFF_SETGET_C32_DOUBLE:
  7354. {
  7355. double *data;
  7356. assert(fip->field_readcount == TIFF_VARIABLE2);
  7357. assert(fip->field_passcount == 1);
  7358. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  7359. if (err == TIFFReadDirEntryErrOk)
  7360. {
  7361. if (!EvaluateIFDdatasizeReading(tif, dp))
  7362. {
  7363. if (data != 0)
  7364. _TIFFfreeExt(tif, data);
  7365. return 0;
  7366. }
  7367. int m;
  7368. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7369. data);
  7370. if (data != 0)
  7371. _TIFFfreeExt(tif, data);
  7372. if (!m)
  7373. return (0);
  7374. }
  7375. }
  7376. break;
  7377. case TIFF_SETGET_C32_IFD8:
  7378. {
  7379. uint64_t *data;
  7380. assert(fip->field_readcount == TIFF_VARIABLE2);
  7381. assert(fip->field_passcount == 1);
  7382. err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
  7383. if (err == TIFFReadDirEntryErrOk)
  7384. {
  7385. if (!EvaluateIFDdatasizeReading(tif, dp))
  7386. {
  7387. if (data != 0)
  7388. _TIFFfreeExt(tif, data);
  7389. return 0;
  7390. }
  7391. int m;
  7392. m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
  7393. data);
  7394. if (data != 0)
  7395. _TIFFfreeExt(tif, data);
  7396. if (!m)
  7397. return (0);
  7398. }
  7399. }
  7400. break;
  7401. default:
  7402. assert(0); /* we should never get here */
  7403. break;
  7404. }
  7405. if (err != TIFFReadDirEntryErrOk)
  7406. {
  7407. TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
  7408. return (0);
  7409. }
  7410. return (1);
  7411. }
  7412. /*
  7413. * Fetch a set of offsets or lengths.
  7414. * While this routine says "strips", in fact it's also used for tiles.
  7415. */
  7416. static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
  7417. uint64_t **lpp)
  7418. {
  7419. static const char module[] = "TIFFFetchStripThing";
  7420. enum TIFFReadDirEntryErr err;
  7421. uint64_t *data;
  7422. err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
  7423. if (err != TIFFReadDirEntryErrOk)
  7424. {
  7425. const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  7426. TIFFReadDirEntryOutputErr(tif, err, module,
  7427. fip ? fip->field_name : "unknown tagname", 0);
  7428. return (0);
  7429. }
  7430. if (dir->tdir_count < (uint64_t)nstrips)
  7431. {
  7432. uint64_t *resizeddata;
  7433. const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  7434. const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
  7435. uint32_t max_nstrips = 1000000;
  7436. if (pszMax)
  7437. max_nstrips = (uint32_t)atoi(pszMax);
  7438. TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
  7439. fip ? fip->field_name : "unknown tagname",
  7440. (nstrips <= max_nstrips));
  7441. if (nstrips > max_nstrips)
  7442. {
  7443. _TIFFfreeExt(tif, data);
  7444. return (0);
  7445. }
  7446. const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
  7447. if (allocsize > 100 * 1024 * 1024)
  7448. {
  7449. /* Before allocating a huge amount of memory for corrupted files,
  7450. * check if size of requested memory is not greater than file size.
  7451. */
  7452. const uint64_t filesize = TIFFGetFileSize(tif);
  7453. if (allocsize > filesize)
  7454. {
  7455. TIFFWarningExtR(
  7456. tif, module,
  7457. "Requested memory size for StripArray of %" PRIu64
  7458. " is greater than filesize %" PRIu64
  7459. ". Memory not allocated",
  7460. allocsize, filesize);
  7461. _TIFFfreeExt(tif, data);
  7462. return (0);
  7463. }
  7464. }
  7465. resizeddata = (uint64_t *)_TIFFCheckMalloc(
  7466. tif, nstrips, sizeof(uint64_t), "for strip array");
  7467. if (resizeddata == 0)
  7468. {
  7469. _TIFFfreeExt(tif, data);
  7470. return (0);
  7471. }
  7472. if (dir->tdir_count)
  7473. _TIFFmemcpy(resizeddata, data,
  7474. (uint32_t)dir->tdir_count * sizeof(uint64_t));
  7475. _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
  7476. (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
  7477. _TIFFfreeExt(tif, data);
  7478. data = resizeddata;
  7479. }
  7480. *lpp = data;
  7481. return (1);
  7482. }
  7483. /*
  7484. * Fetch and set the SubjectDistance EXIF tag.
  7485. */
  7486. static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
  7487. {
  7488. static const char module[] = "TIFFFetchSubjectDistance";
  7489. enum TIFFReadDirEntryErr err;
  7490. UInt64Aligned_t m;
  7491. m.l = 0;
  7492. assert(sizeof(double) == 8);
  7493. assert(sizeof(uint64_t) == 8);
  7494. assert(sizeof(uint32_t) == 4);
  7495. if (dir->tdir_count != 1)
  7496. err = TIFFReadDirEntryErrCount;
  7497. else if (dir->tdir_type != TIFF_RATIONAL)
  7498. err = TIFFReadDirEntryErrType;
  7499. else
  7500. {
  7501. if (!(tif->tif_flags & TIFF_BIGTIFF))
  7502. {
  7503. uint32_t offset;
  7504. offset = *(uint32_t *)(&dir->tdir_offset);
  7505. if (tif->tif_flags & TIFF_SWAB)
  7506. TIFFSwabLong(&offset);
  7507. err = TIFFReadDirEntryData(tif, offset, 8, m.i);
  7508. }
  7509. else
  7510. {
  7511. m.l = dir->tdir_offset.toff_long8;
  7512. err = TIFFReadDirEntryErrOk;
  7513. }
  7514. }
  7515. if (err == TIFFReadDirEntryErrOk)
  7516. {
  7517. double n;
  7518. if (tif->tif_flags & TIFF_SWAB)
  7519. TIFFSwabArrayOfLong(m.i, 2);
  7520. if (m.i[0] == 0)
  7521. n = 0.0;
  7522. else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
  7523. /*
  7524. * XXX: Numerator 0xFFFFFFFF means that we have infinite
  7525. * distance. Indicate that with a negative floating point
  7526. * SubjectDistance value.
  7527. */
  7528. n = -1.0;
  7529. else
  7530. n = (double)m.i[0] / (double)m.i[1];
  7531. return (TIFFSetField(tif, dir->tdir_tag, n));
  7532. }
  7533. else
  7534. {
  7535. TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
  7536. return (0);
  7537. }
  7538. }
  7539. static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
  7540. uint64_t stripbytes,
  7541. uint32_t rowsperstrip)
  7542. {
  7543. TIFFDirectory *td = &tif->tif_dir;
  7544. uint64_t bytecount;
  7545. uint64_t offset;
  7546. uint64_t last_offset;
  7547. uint64_t last_bytecount;
  7548. uint32_t i;
  7549. uint64_t *newcounts;
  7550. uint64_t *newoffsets;
  7551. offset = TIFFGetStrileOffset(tif, 0);
  7552. last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
  7553. last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
  7554. if (last_offset > UINT64_MAX - last_bytecount ||
  7555. last_offset + last_bytecount < offset)
  7556. {
  7557. return;
  7558. }
  7559. bytecount = last_offset + last_bytecount - offset;
  7560. /* Before allocating a huge amount of memory for corrupted files, check if
  7561. * size of StripByteCount and StripOffset tags is not greater than
  7562. * file size.
  7563. */
  7564. const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
  7565. if (allocsize > 100 * 1024 * 1024)
  7566. {
  7567. const uint64_t filesize = TIFFGetFileSize(tif);
  7568. if (allocsize > filesize)
  7569. {
  7570. TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
  7571. "Requested memory size for StripByteCount and "
  7572. "StripOffsets %" PRIu64
  7573. " is greater than filesize %" PRIu64
  7574. ". Memory not allocated",
  7575. allocsize, filesize);
  7576. return;
  7577. }
  7578. }
  7579. newcounts =
  7580. (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
  7581. "for chopped \"StripByteCounts\" array");
  7582. newoffsets = (uint64_t *)_TIFFCheckMalloc(
  7583. tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
  7584. if (newcounts == NULL || newoffsets == NULL)
  7585. {
  7586. /*
  7587. * Unable to allocate new strip information, give up and use
  7588. * the original one strip information.
  7589. */
  7590. if (newcounts != NULL)
  7591. _TIFFfreeExt(tif, newcounts);
  7592. if (newoffsets != NULL)
  7593. _TIFFfreeExt(tif, newoffsets);
  7594. return;
  7595. }
  7596. /*
  7597. * Fill the strip information arrays with new bytecounts and offsets
  7598. * that reflect the broken-up format.
  7599. */
  7600. for (i = 0; i < nstrips; i++)
  7601. {
  7602. if (stripbytes > bytecount)
  7603. stripbytes = bytecount;
  7604. newcounts[i] = stripbytes;
  7605. newoffsets[i] = stripbytes ? offset : 0;
  7606. offset += stripbytes;
  7607. bytecount -= stripbytes;
  7608. }
  7609. /*
  7610. * Replace old single strip info with multi-strip info.
  7611. */
  7612. td->td_stripsperimage = td->td_nstrips = nstrips;
  7613. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  7614. _TIFFfreeExt(tif, td->td_stripbytecount_p);
  7615. _TIFFfreeExt(tif, td->td_stripoffset_p);
  7616. td->td_stripbytecount_p = newcounts;
  7617. td->td_stripoffset_p = newoffsets;
  7618. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  7619. td->td_stripbytecountsorted = 1;
  7620. #endif
  7621. tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
  7622. }
  7623. /*
  7624. * Replace a single strip (tile) of uncompressed data by multiple strips
  7625. * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
  7626. * dealing with large images or for dealing with machines with a limited
  7627. * amount memory.
  7628. */
  7629. static void ChopUpSingleUncompressedStrip(TIFF *tif)
  7630. {
  7631. register TIFFDirectory *td = &tif->tif_dir;
  7632. uint64_t bytecount;
  7633. uint64_t offset;
  7634. uint32_t rowblock;
  7635. uint64_t rowblockbytes;
  7636. uint64_t stripbytes;
  7637. uint32_t nstrips;
  7638. uint32_t rowsperstrip;
  7639. bytecount = TIFFGetStrileByteCount(tif, 0);
  7640. /* On a newly created file, just re-opened to be filled, we */
  7641. /* don't want strip chop to trigger as it is going to cause issues */
  7642. /* later ( StripOffsets and StripByteCounts improperly filled) . */
  7643. if (bytecount == 0 && tif->tif_mode != O_RDONLY)
  7644. return;
  7645. offset = TIFFGetStrileByteCount(tif, 0);
  7646. assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
  7647. if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
  7648. rowblock = td->td_ycbcrsubsampling[1];
  7649. else
  7650. rowblock = 1;
  7651. rowblockbytes = TIFFVTileSize64(tif, rowblock);
  7652. /*
  7653. * Make the rows hold at least one scanline, but fill specified amount
  7654. * of data if possible.
  7655. */
  7656. if (rowblockbytes > STRIP_SIZE_DEFAULT)
  7657. {
  7658. stripbytes = rowblockbytes;
  7659. rowsperstrip = rowblock;
  7660. }
  7661. else if (rowblockbytes > 0)
  7662. {
  7663. uint32_t rowblocksperstrip;
  7664. rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
  7665. rowsperstrip = rowblocksperstrip * rowblock;
  7666. stripbytes = rowblocksperstrip * rowblockbytes;
  7667. }
  7668. else
  7669. return;
  7670. /*
  7671. * never increase the number of rows per strip
  7672. */
  7673. if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
  7674. return;
  7675. nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
  7676. if (nstrips == 0)
  7677. return;
  7678. /* If we are going to allocate a lot of memory, make sure that the */
  7679. /* file is as big as needed */
  7680. if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
  7681. (offset >= TIFFGetFileSize(tif) ||
  7682. stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
  7683. {
  7684. return;
  7685. }
  7686. allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
  7687. }
  7688. /*
  7689. * Replace a file with contiguous strips > 2 GB of uncompressed data by
  7690. * multiple smaller strips. This is useful for
  7691. * dealing with large images or for dealing with machines with a limited
  7692. * amount memory.
  7693. */
  7694. static void TryChopUpUncompressedBigTiff(TIFF *tif)
  7695. {
  7696. TIFFDirectory *td = &tif->tif_dir;
  7697. uint32_t rowblock;
  7698. uint64_t rowblockbytes;
  7699. uint32_t i;
  7700. uint64_t stripsize;
  7701. uint32_t rowblocksperstrip;
  7702. uint32_t rowsperstrip;
  7703. uint64_t stripbytes;
  7704. uint32_t nstrips;
  7705. stripsize = TIFFStripSize64(tif);
  7706. assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
  7707. assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
  7708. assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
  7709. TIFF_STRIPCHOP);
  7710. assert(stripsize > 0x7FFFFFFFUL);
  7711. /* On a newly created file, just re-opened to be filled, we */
  7712. /* don't want strip chop to trigger as it is going to cause issues */
  7713. /* later ( StripOffsets and StripByteCounts improperly filled) . */
  7714. if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
  7715. return;
  7716. if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
  7717. rowblock = td->td_ycbcrsubsampling[1];
  7718. else
  7719. rowblock = 1;
  7720. rowblockbytes = TIFFVStripSize64(tif, rowblock);
  7721. if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
  7722. {
  7723. /* In case of file with gigantic width */
  7724. return;
  7725. }
  7726. /* Check that the strips are contiguous and of the expected size */
  7727. for (i = 0; i < td->td_nstrips; i++)
  7728. {
  7729. if (i == td->td_nstrips - 1)
  7730. {
  7731. if (TIFFGetStrileByteCount(tif, i) <
  7732. TIFFVStripSize64(tif,
  7733. td->td_imagelength - i * td->td_rowsperstrip))
  7734. {
  7735. return;
  7736. }
  7737. }
  7738. else
  7739. {
  7740. if (TIFFGetStrileByteCount(tif, i) != stripsize)
  7741. {
  7742. return;
  7743. }
  7744. if (i > 0 && TIFFGetStrileOffset(tif, i) !=
  7745. TIFFGetStrileOffset(tif, i - 1) +
  7746. TIFFGetStrileByteCount(tif, i - 1))
  7747. {
  7748. return;
  7749. }
  7750. }
  7751. }
  7752. /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
  7753. rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
  7754. if (rowblocksperstrip == 0)
  7755. rowblocksperstrip = 1;
  7756. rowsperstrip = rowblocksperstrip * rowblock;
  7757. stripbytes = rowblocksperstrip * rowblockbytes;
  7758. assert(stripbytes <= 0x7FFFFFFFUL);
  7759. if (rowsperstrip == 0)
  7760. return;
  7761. nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
  7762. if (nstrips == 0)
  7763. return;
  7764. /* If we are going to allocate a lot of memory, make sure that the */
  7765. /* file is as big as needed */
  7766. if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
  7767. {
  7768. uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
  7769. uint64_t filesize = TIFFGetFileSize(tif);
  7770. uint64_t last_bytecount =
  7771. TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
  7772. if (last_offset > filesize || last_bytecount > filesize - last_offset)
  7773. {
  7774. return;
  7775. }
  7776. }
  7777. allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
  7778. }
  7779. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  7780. static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
  7781. {
  7782. return a + b;
  7783. }
  7784. /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
  7785. * strip/tile of number strile. Also fetch the neighbouring values using a
  7786. * 4096 byte page size.
  7787. */
  7788. static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
  7789. int strile, uint64_t *panVals)
  7790. {
  7791. static const char module[] = "_TIFFPartialReadStripArray";
  7792. #define IO_CACHE_PAGE_SIZE 4096
  7793. size_t sizeofval;
  7794. const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
  7795. int sizeofvalint;
  7796. uint64_t nBaseOffset;
  7797. uint64_t nOffset;
  7798. uint64_t nOffsetStartPage;
  7799. uint64_t nOffsetEndPage;
  7800. tmsize_t nToRead;
  7801. tmsize_t nRead;
  7802. uint64_t nLastStripOffset;
  7803. int iStartBefore;
  7804. int i;
  7805. const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
  7806. unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
  7807. assert(dirent->tdir_count > 4);
  7808. if (dirent->tdir_type == TIFF_SHORT)
  7809. {
  7810. sizeofval = sizeof(uint16_t);
  7811. }
  7812. else if (dirent->tdir_type == TIFF_LONG)
  7813. {
  7814. sizeofval = sizeof(uint32_t);
  7815. }
  7816. else if (dirent->tdir_type == TIFF_LONG8)
  7817. {
  7818. sizeofval = sizeof(uint64_t);
  7819. }
  7820. else if (dirent->tdir_type == TIFF_SLONG8)
  7821. {
  7822. /* Non conformant but used by some images as in */
  7823. /* https://github.com/OSGeo/gdal/issues/2165 */
  7824. sizeofval = sizeof(int64_t);
  7825. }
  7826. else
  7827. {
  7828. TIFFErrorExtR(tif, module,
  7829. "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
  7830. panVals[strile] = 0;
  7831. return 0;
  7832. }
  7833. sizeofvalint = (int)(sizeofval);
  7834. if (tif->tif_flags & TIFF_BIGTIFF)
  7835. {
  7836. uint64_t offset = dirent->tdir_offset.toff_long8;
  7837. if (bSwab)
  7838. TIFFSwabLong8(&offset);
  7839. nBaseOffset = offset;
  7840. }
  7841. else
  7842. {
  7843. uint32_t offset = dirent->tdir_offset.toff_long;
  7844. if (bSwab)
  7845. TIFFSwabLong(&offset);
  7846. nBaseOffset = offset;
  7847. }
  7848. /* To avoid later unsigned integer overflows */
  7849. if (nBaseOffset > (uint64_t)INT64_MAX)
  7850. {
  7851. TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
  7852. strile);
  7853. panVals[strile] = 0;
  7854. return 0;
  7855. }
  7856. nOffset = nBaseOffset + sizeofval * strile;
  7857. nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
  7858. nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
  7859. if (nOffset + sizeofval > nOffsetEndPage)
  7860. nOffsetEndPage += IO_CACHE_PAGE_SIZE;
  7861. #undef IO_CACHE_PAGE_SIZE
  7862. nLastStripOffset = nBaseOffset + arraySize * sizeofval;
  7863. if (nLastStripOffset < nOffsetEndPage)
  7864. nOffsetEndPage = nLastStripOffset;
  7865. if (nOffsetStartPage >= nOffsetEndPage)
  7866. {
  7867. TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
  7868. strile);
  7869. panVals[strile] = 0;
  7870. return 0;
  7871. }
  7872. if (!SeekOK(tif, nOffsetStartPage))
  7873. {
  7874. panVals[strile] = 0;
  7875. return 0;
  7876. }
  7877. nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
  7878. nRead = TIFFReadFile(tif, buffer, nToRead);
  7879. if (nRead < nToRead)
  7880. {
  7881. TIFFErrorExtR(tif, module,
  7882. "Cannot read offset/size for strile around ~%d", strile);
  7883. return 0;
  7884. }
  7885. iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
  7886. if (strile + iStartBefore < 0)
  7887. iStartBefore = -strile;
  7888. for (i = iStartBefore;
  7889. (uint32_t)(strile + i) < arraySize &&
  7890. _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
  7891. nOffsetEndPage;
  7892. ++i)
  7893. {
  7894. if (dirent->tdir_type == TIFF_SHORT)
  7895. {
  7896. uint16_t val;
  7897. memcpy(&val,
  7898. buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
  7899. sizeof(val));
  7900. if (bSwab)
  7901. TIFFSwabShort(&val);
  7902. panVals[strile + i] = val;
  7903. }
  7904. else if (dirent->tdir_type == TIFF_LONG)
  7905. {
  7906. uint32_t val;
  7907. memcpy(&val,
  7908. buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
  7909. sizeof(val));
  7910. if (bSwab)
  7911. TIFFSwabLong(&val);
  7912. panVals[strile + i] = val;
  7913. }
  7914. else if (dirent->tdir_type == TIFF_LONG8)
  7915. {
  7916. uint64_t val;
  7917. memcpy(&val,
  7918. buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
  7919. sizeof(val));
  7920. if (bSwab)
  7921. TIFFSwabLong8(&val);
  7922. panVals[strile + i] = val;
  7923. }
  7924. else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
  7925. {
  7926. /* Non conformant data type */
  7927. int64_t val;
  7928. memcpy(&val,
  7929. buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
  7930. sizeof(val));
  7931. if (bSwab)
  7932. TIFFSwabLong8((uint64_t *)&val);
  7933. panVals[strile + i] = (uint64_t)val;
  7934. }
  7935. }
  7936. return 1;
  7937. }
  7938. static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
  7939. TIFFDirEntry *dirent, uint64_t **parray)
  7940. {
  7941. static const char module[] = "_TIFFFetchStrileValue";
  7942. TIFFDirectory *td = &tif->tif_dir;
  7943. if (strile >= dirent->tdir_count)
  7944. {
  7945. return 0;
  7946. }
  7947. if (strile >= td->td_stripoffsetbyteallocsize)
  7948. {
  7949. uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
  7950. uint32_t nStripArrayAllocNew;
  7951. uint64_t nArraySize64;
  7952. size_t nArraySize;
  7953. uint64_t *offsetArray;
  7954. uint64_t *bytecountArray;
  7955. if (strile > 1000000)
  7956. {
  7957. uint64_t filesize = TIFFGetFileSize(tif);
  7958. /* Avoid excessive memory allocation attempt */
  7959. /* For such a big blockid we need at least a TIFF_LONG per strile */
  7960. /* for the offset array. */
  7961. if (strile > filesize / sizeof(uint32_t))
  7962. {
  7963. TIFFErrorExtR(tif, module, "File too short");
  7964. return 0;
  7965. }
  7966. }
  7967. if (td->td_stripoffsetbyteallocsize == 0 &&
  7968. td->td_nstrips < 1024 * 1024)
  7969. {
  7970. nStripArrayAllocNew = td->td_nstrips;
  7971. }
  7972. else
  7973. {
  7974. #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
  7975. #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
  7976. nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
  7977. if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
  7978. nStripArrayAllocNew *= 2;
  7979. nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
  7980. }
  7981. assert(strile < nStripArrayAllocNew);
  7982. nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
  7983. nArraySize = (size_t)(nArraySize64);
  7984. #if SIZEOF_SIZE_T == 4
  7985. if (nArraySize != nArraySize64)
  7986. {
  7987. TIFFErrorExtR(tif, module,
  7988. "Cannot allocate strip offset and bytecount arrays");
  7989. return 0;
  7990. }
  7991. #endif
  7992. offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
  7993. nArraySize));
  7994. bytecountArray = (uint64_t *)(_TIFFreallocExt(
  7995. tif, td->td_stripbytecount_p, nArraySize));
  7996. if (offsetArray)
  7997. td->td_stripoffset_p = offsetArray;
  7998. if (bytecountArray)
  7999. td->td_stripbytecount_p = bytecountArray;
  8000. if (offsetArray && bytecountArray)
  8001. {
  8002. td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
  8003. /* Initialize new entries to ~0 / -1 */
  8004. /* coverity[overrun-buffer-arg] */
  8005. memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
  8006. (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
  8007. sizeof(uint64_t));
  8008. /* coverity[overrun-buffer-arg] */
  8009. memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
  8010. (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
  8011. sizeof(uint64_t));
  8012. }
  8013. else
  8014. {
  8015. TIFFErrorExtR(tif, module,
  8016. "Cannot allocate strip offset and bytecount arrays");
  8017. _TIFFfreeExt(tif, td->td_stripoffset_p);
  8018. td->td_stripoffset_p = NULL;
  8019. _TIFFfreeExt(tif, td->td_stripbytecount_p);
  8020. td->td_stripbytecount_p = NULL;
  8021. td->td_stripoffsetbyteallocsize = 0;
  8022. }
  8023. }
  8024. if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
  8025. return 0;
  8026. if (~((*parray)[strile]) == 0)
  8027. {
  8028. if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray))
  8029. {
  8030. (*parray)[strile] = 0;
  8031. return 0;
  8032. }
  8033. }
  8034. return 1;
  8035. }
  8036. static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
  8037. TIFFDirEntry *dirent,
  8038. uint64_t **parray,
  8039. int *pbErr)
  8040. {
  8041. TIFFDirectory *td = &tif->tif_dir;
  8042. if (pbErr)
  8043. *pbErr = 0;
  8044. if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
  8045. !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
  8046. {
  8047. if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) ||
  8048. /* If the values may fit in the toff_long/toff_long8 member */
  8049. /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
  8050. dirent->tdir_count <= 4)
  8051. {
  8052. if (!_TIFFFillStriles(tif))
  8053. {
  8054. if (pbErr)
  8055. *pbErr = 1;
  8056. /* Do not return, as we want this function to always */
  8057. /* return the same value if called several times with */
  8058. /* the same arguments */
  8059. }
  8060. }
  8061. else
  8062. {
  8063. if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
  8064. {
  8065. if (pbErr)
  8066. *pbErr = 1;
  8067. return 0;
  8068. }
  8069. }
  8070. }
  8071. if (*parray == NULL || strile >= td->td_nstrips)
  8072. {
  8073. if (pbErr)
  8074. *pbErr = 1;
  8075. return 0;
  8076. }
  8077. return (*parray)[strile];
  8078. }
  8079. /* Return the value of the TileOffsets/StripOffsets array for the specified
  8080. * tile/strile */
  8081. uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
  8082. {
  8083. return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
  8084. }
  8085. /* Return the value of the TileOffsets/StripOffsets array for the specified
  8086. * tile/strile */
  8087. uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
  8088. {
  8089. TIFFDirectory *td = &tif->tif_dir;
  8090. return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
  8091. &(td->td_stripoffset_entry),
  8092. &(td->td_stripoffset_p), pbErr);
  8093. }
  8094. /* Return the value of the TileByteCounts/StripByteCounts array for the
  8095. * specified tile/strile */
  8096. uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
  8097. {
  8098. return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
  8099. }
  8100. /* Return the value of the TileByteCounts/StripByteCounts array for the
  8101. * specified tile/strile */
  8102. uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
  8103. {
  8104. TIFFDirectory *td = &tif->tif_dir;
  8105. return _TIFFGetStrileOffsetOrByteCountValue(
  8106. tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
  8107. pbErr);
  8108. }
  8109. int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
  8110. static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
  8111. {
  8112. register TIFFDirectory *td = &tif->tif_dir;
  8113. int return_value = 1;
  8114. /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
  8115. if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
  8116. (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
  8117. return 1;
  8118. if (tif->tif_flags & TIFF_LAZYSTRILELOAD)
  8119. {
  8120. /* In case of lazy loading, reload completely the arrays */
  8121. _TIFFfreeExt(tif, td->td_stripoffset_p);
  8122. _TIFFfreeExt(tif, td->td_stripbytecount_p);
  8123. td->td_stripoffset_p = NULL;
  8124. td->td_stripbytecount_p = NULL;
  8125. td->td_stripoffsetbyteallocsize = 0;
  8126. tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;
  8127. }
  8128. /* If stripoffset array is already loaded, exit with success */
  8129. if (td->td_stripoffset_p != NULL)
  8130. return 1;
  8131. /* If tdir_count was canceled, then we already got there, but in error */
  8132. if (td->td_stripoffset_entry.tdir_count == 0)
  8133. return 0;
  8134. if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
  8135. &td->td_stripoffset_p))
  8136. {
  8137. return_value = 0;
  8138. }
  8139. if (loadStripByteCount &&
  8140. !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
  8141. td->td_nstrips, &td->td_stripbytecount_p))
  8142. {
  8143. return_value = 0;
  8144. }
  8145. _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
  8146. _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
  8147. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  8148. if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
  8149. {
  8150. uint32_t strip;
  8151. tif->tif_dir.td_stripbytecountsorted = 1;
  8152. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
  8153. {
  8154. if (tif->tif_dir.td_stripoffset_p[strip - 1] >
  8155. tif->tif_dir.td_stripoffset_p[strip])
  8156. {
  8157. tif->tif_dir.td_stripbytecountsorted = 0;
  8158. break;
  8159. }
  8160. }
  8161. }
  8162. #endif
  8163. return return_value;
  8164. }