tif_dirread.c 254 KB

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