tif_dirread.c 138 KB

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