2
0

tif_dirread.c 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600
  1. /* $Id: tif_dirread.c,v 1.174 2012-02-01 02:24:47 fwarmerdam 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. if (err!=TIFFReadDirEntryErrOk)
  2132. {
  2133. _TIFFfree(data);
  2134. return(err);
  2135. }
  2136. *value=data;
  2137. return(TIFFReadDirEntryErrOk);
  2138. }
  2139. static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
  2140. {
  2141. enum TIFFReadDirEntryErr err;
  2142. uint32 count;
  2143. void* origdata;
  2144. float* data;
  2145. switch (direntry->tdir_type)
  2146. {
  2147. case TIFF_BYTE:
  2148. case TIFF_SBYTE:
  2149. case TIFF_SHORT:
  2150. case TIFF_SSHORT:
  2151. case TIFF_LONG:
  2152. case TIFF_SLONG:
  2153. case TIFF_LONG8:
  2154. case TIFF_SLONG8:
  2155. case TIFF_RATIONAL:
  2156. case TIFF_SRATIONAL:
  2157. case TIFF_FLOAT:
  2158. case TIFF_DOUBLE:
  2159. break;
  2160. default:
  2161. return(TIFFReadDirEntryErrType);
  2162. }
  2163. err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
  2164. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2165. {
  2166. *value=0;
  2167. return(err);
  2168. }
  2169. switch (direntry->tdir_type)
  2170. {
  2171. case TIFF_FLOAT:
  2172. if (tif->tif_flags&TIFF_SWAB)
  2173. TIFFSwabArrayOfLong((uint32*)origdata,count);
  2174. TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
  2175. *value=(float*)origdata;
  2176. return(TIFFReadDirEntryErrOk);
  2177. }
  2178. data=(float*)_TIFFmalloc(count*sizeof(float));
  2179. if (data==0)
  2180. {
  2181. _TIFFfree(origdata);
  2182. return(TIFFReadDirEntryErrAlloc);
  2183. }
  2184. switch (direntry->tdir_type)
  2185. {
  2186. case TIFF_BYTE:
  2187. {
  2188. uint8* ma;
  2189. float* mb;
  2190. uint32 n;
  2191. ma=(uint8*)origdata;
  2192. mb=data;
  2193. for (n=0; n<count; n++)
  2194. *mb++=(float)(*ma++);
  2195. }
  2196. break;
  2197. case TIFF_SBYTE:
  2198. {
  2199. int8* ma;
  2200. float* mb;
  2201. uint32 n;
  2202. ma=(int8*)origdata;
  2203. mb=data;
  2204. for (n=0; n<count; n++)
  2205. *mb++=(float)(*ma++);
  2206. }
  2207. break;
  2208. case TIFF_SHORT:
  2209. {
  2210. uint16* ma;
  2211. float* mb;
  2212. uint32 n;
  2213. ma=(uint16*)origdata;
  2214. mb=data;
  2215. for (n=0; n<count; n++)
  2216. {
  2217. if (tif->tif_flags&TIFF_SWAB)
  2218. TIFFSwabShort(ma);
  2219. *mb++=(float)(*ma++);
  2220. }
  2221. }
  2222. break;
  2223. case TIFF_SSHORT:
  2224. {
  2225. int16* ma;
  2226. float* mb;
  2227. uint32 n;
  2228. ma=(int16*)origdata;
  2229. mb=data;
  2230. for (n=0; n<count; n++)
  2231. {
  2232. if (tif->tif_flags&TIFF_SWAB)
  2233. TIFFSwabShort((uint16*)ma);
  2234. *mb++=(float)(*ma++);
  2235. }
  2236. }
  2237. break;
  2238. case TIFF_LONG:
  2239. {
  2240. uint32* ma;
  2241. float* mb;
  2242. uint32 n;
  2243. ma=(uint32*)origdata;
  2244. mb=data;
  2245. for (n=0; n<count; n++)
  2246. {
  2247. if (tif->tif_flags&TIFF_SWAB)
  2248. TIFFSwabLong(ma);
  2249. *mb++=(float)(*ma++);
  2250. }
  2251. }
  2252. break;
  2253. case TIFF_SLONG:
  2254. {
  2255. int32* ma;
  2256. float* mb;
  2257. uint32 n;
  2258. ma=(int32*)origdata;
  2259. mb=data;
  2260. for (n=0; n<count; n++)
  2261. {
  2262. if (tif->tif_flags&TIFF_SWAB)
  2263. TIFFSwabLong((uint32*)ma);
  2264. *mb++=(float)(*ma++);
  2265. }
  2266. }
  2267. break;
  2268. case TIFF_LONG8:
  2269. {
  2270. uint64* ma;
  2271. float* mb;
  2272. uint32 n;
  2273. ma=(uint64*)origdata;
  2274. mb=data;
  2275. for (n=0; n<count; n++)
  2276. {
  2277. if (tif->tif_flags&TIFF_SWAB)
  2278. TIFFSwabLong8(ma);
  2279. #if defined(__WIN32__) && (_MSC_VER < 1500)
  2280. /*
  2281. * XXX: MSVC 6.0 does not support
  2282. * conversion of 64-bit integers into
  2283. * floating point values.
  2284. */
  2285. *mb++ = _TIFFUInt64ToFloat(*ma++);
  2286. #else
  2287. *mb++ = (float)(*ma++);
  2288. #endif
  2289. }
  2290. }
  2291. break;
  2292. case TIFF_SLONG8:
  2293. {
  2294. int64* ma;
  2295. float* mb;
  2296. uint32 n;
  2297. ma=(int64*)origdata;
  2298. mb=data;
  2299. for (n=0; n<count; n++)
  2300. {
  2301. if (tif->tif_flags&TIFF_SWAB)
  2302. TIFFSwabLong8((uint64*)ma);
  2303. *mb++=(float)(*ma++);
  2304. }
  2305. }
  2306. break;
  2307. case TIFF_RATIONAL:
  2308. {
  2309. uint32* ma;
  2310. uint32 maa;
  2311. uint32 mab;
  2312. float* mb;
  2313. uint32 n;
  2314. ma=(uint32*)origdata;
  2315. mb=data;
  2316. for (n=0; n<count; n++)
  2317. {
  2318. if (tif->tif_flags&TIFF_SWAB)
  2319. TIFFSwabLong(ma);
  2320. maa=*ma++;
  2321. if (tif->tif_flags&TIFF_SWAB)
  2322. TIFFSwabLong(ma);
  2323. mab=*ma++;
  2324. if (mab==0)
  2325. *mb++=0.0;
  2326. else
  2327. *mb++=(float)maa/(float)mab;
  2328. }
  2329. }
  2330. break;
  2331. case TIFF_SRATIONAL:
  2332. {
  2333. uint32* ma;
  2334. int32 maa;
  2335. uint32 mab;
  2336. float* mb;
  2337. uint32 n;
  2338. ma=(uint32*)origdata;
  2339. mb=data;
  2340. for (n=0; n<count; n++)
  2341. {
  2342. if (tif->tif_flags&TIFF_SWAB)
  2343. TIFFSwabLong(ma);
  2344. maa=*(int32*)ma;
  2345. ma++;
  2346. if (tif->tif_flags&TIFF_SWAB)
  2347. TIFFSwabLong(ma);
  2348. mab=*ma++;
  2349. if (mab==0)
  2350. *mb++=0.0;
  2351. else
  2352. *mb++=(float)maa/(float)mab;
  2353. }
  2354. }
  2355. break;
  2356. case TIFF_DOUBLE:
  2357. {
  2358. double* ma;
  2359. float* mb;
  2360. uint32 n;
  2361. if (tif->tif_flags&TIFF_SWAB)
  2362. TIFFSwabArrayOfLong8((uint64*)origdata,count);
  2363. TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
  2364. ma=(double*)origdata;
  2365. mb=data;
  2366. for (n=0; n<count; n++)
  2367. *mb++=(float)(*ma++);
  2368. }
  2369. break;
  2370. }
  2371. _TIFFfree(origdata);
  2372. if (err!=TIFFReadDirEntryErrOk)
  2373. {
  2374. _TIFFfree(data);
  2375. return(err);
  2376. }
  2377. *value=data;
  2378. return(TIFFReadDirEntryErrOk);
  2379. }
  2380. static enum TIFFReadDirEntryErr
  2381. TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
  2382. {
  2383. enum TIFFReadDirEntryErr err;
  2384. uint32 count;
  2385. void* origdata;
  2386. double* data;
  2387. switch (direntry->tdir_type)
  2388. {
  2389. case TIFF_BYTE:
  2390. case TIFF_SBYTE:
  2391. case TIFF_SHORT:
  2392. case TIFF_SSHORT:
  2393. case TIFF_LONG:
  2394. case TIFF_SLONG:
  2395. case TIFF_LONG8:
  2396. case TIFF_SLONG8:
  2397. case TIFF_RATIONAL:
  2398. case TIFF_SRATIONAL:
  2399. case TIFF_FLOAT:
  2400. case TIFF_DOUBLE:
  2401. break;
  2402. default:
  2403. return(TIFFReadDirEntryErrType);
  2404. }
  2405. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  2406. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2407. {
  2408. *value=0;
  2409. return(err);
  2410. }
  2411. switch (direntry->tdir_type)
  2412. {
  2413. case TIFF_DOUBLE:
  2414. if (tif->tif_flags&TIFF_SWAB)
  2415. TIFFSwabArrayOfLong8((uint64*)origdata,count);
  2416. TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
  2417. *value=(double*)origdata;
  2418. return(TIFFReadDirEntryErrOk);
  2419. }
  2420. data=(double*)_TIFFmalloc(count*sizeof(double));
  2421. if (data==0)
  2422. {
  2423. _TIFFfree(origdata);
  2424. return(TIFFReadDirEntryErrAlloc);
  2425. }
  2426. switch (direntry->tdir_type)
  2427. {
  2428. case TIFF_BYTE:
  2429. {
  2430. uint8* ma;
  2431. double* mb;
  2432. uint32 n;
  2433. ma=(uint8*)origdata;
  2434. mb=data;
  2435. for (n=0; n<count; n++)
  2436. *mb++=(double)(*ma++);
  2437. }
  2438. break;
  2439. case TIFF_SBYTE:
  2440. {
  2441. int8* ma;
  2442. double* mb;
  2443. uint32 n;
  2444. ma=(int8*)origdata;
  2445. mb=data;
  2446. for (n=0; n<count; n++)
  2447. *mb++=(double)(*ma++);
  2448. }
  2449. break;
  2450. case TIFF_SHORT:
  2451. {
  2452. uint16* ma;
  2453. double* mb;
  2454. uint32 n;
  2455. ma=(uint16*)origdata;
  2456. mb=data;
  2457. for (n=0; n<count; n++)
  2458. {
  2459. if (tif->tif_flags&TIFF_SWAB)
  2460. TIFFSwabShort(ma);
  2461. *mb++=(double)(*ma++);
  2462. }
  2463. }
  2464. break;
  2465. case TIFF_SSHORT:
  2466. {
  2467. int16* ma;
  2468. double* mb;
  2469. uint32 n;
  2470. ma=(int16*)origdata;
  2471. mb=data;
  2472. for (n=0; n<count; n++)
  2473. {
  2474. if (tif->tif_flags&TIFF_SWAB)
  2475. TIFFSwabShort((uint16*)ma);
  2476. *mb++=(double)(*ma++);
  2477. }
  2478. }
  2479. break;
  2480. case TIFF_LONG:
  2481. {
  2482. uint32* ma;
  2483. double* mb;
  2484. uint32 n;
  2485. ma=(uint32*)origdata;
  2486. mb=data;
  2487. for (n=0; n<count; n++)
  2488. {
  2489. if (tif->tif_flags&TIFF_SWAB)
  2490. TIFFSwabLong(ma);
  2491. *mb++=(double)(*ma++);
  2492. }
  2493. }
  2494. break;
  2495. case TIFF_SLONG:
  2496. {
  2497. int32* ma;
  2498. double* mb;
  2499. uint32 n;
  2500. ma=(int32*)origdata;
  2501. mb=data;
  2502. for (n=0; n<count; n++)
  2503. {
  2504. if (tif->tif_flags&TIFF_SWAB)
  2505. TIFFSwabLong((uint32*)ma);
  2506. *mb++=(double)(*ma++);
  2507. }
  2508. }
  2509. break;
  2510. case TIFF_LONG8:
  2511. {
  2512. uint64* ma;
  2513. double* mb;
  2514. uint32 n;
  2515. ma=(uint64*)origdata;
  2516. mb=data;
  2517. for (n=0; n<count; n++)
  2518. {
  2519. if (tif->tif_flags&TIFF_SWAB)
  2520. TIFFSwabLong8(ma);
  2521. #if defined(__WIN32__) && (_MSC_VER < 1500)
  2522. /*
  2523. * XXX: MSVC 6.0 does not support
  2524. * conversion of 64-bit integers into
  2525. * floating point values.
  2526. */
  2527. *mb++ = _TIFFUInt64ToDouble(*ma++);
  2528. #else
  2529. *mb++ = (double)(*ma++);
  2530. #endif
  2531. }
  2532. }
  2533. break;
  2534. case TIFF_SLONG8:
  2535. {
  2536. int64* ma;
  2537. double* mb;
  2538. uint32 n;
  2539. ma=(int64*)origdata;
  2540. mb=data;
  2541. for (n=0; n<count; n++)
  2542. {
  2543. if (tif->tif_flags&TIFF_SWAB)
  2544. TIFFSwabLong8((uint64*)ma);
  2545. *mb++=(double)(*ma++);
  2546. }
  2547. }
  2548. break;
  2549. case TIFF_RATIONAL:
  2550. {
  2551. uint32* ma;
  2552. uint32 maa;
  2553. uint32 mab;
  2554. double* mb;
  2555. uint32 n;
  2556. ma=(uint32*)origdata;
  2557. mb=data;
  2558. for (n=0; n<count; n++)
  2559. {
  2560. if (tif->tif_flags&TIFF_SWAB)
  2561. TIFFSwabLong(ma);
  2562. maa=*ma++;
  2563. if (tif->tif_flags&TIFF_SWAB)
  2564. TIFFSwabLong(ma);
  2565. mab=*ma++;
  2566. if (mab==0)
  2567. *mb++=0.0;
  2568. else
  2569. *mb++=(double)maa/(double)mab;
  2570. }
  2571. }
  2572. break;
  2573. case TIFF_SRATIONAL:
  2574. {
  2575. uint32* ma;
  2576. int32 maa;
  2577. uint32 mab;
  2578. double* mb;
  2579. uint32 n;
  2580. ma=(uint32*)origdata;
  2581. mb=data;
  2582. for (n=0; n<count; n++)
  2583. {
  2584. if (tif->tif_flags&TIFF_SWAB)
  2585. TIFFSwabLong(ma);
  2586. maa=*(int32*)ma;
  2587. ma++;
  2588. if (tif->tif_flags&TIFF_SWAB)
  2589. TIFFSwabLong(ma);
  2590. mab=*ma++;
  2591. if (mab==0)
  2592. *mb++=0.0;
  2593. else
  2594. *mb++=(double)maa/(double)mab;
  2595. }
  2596. }
  2597. break;
  2598. case TIFF_FLOAT:
  2599. {
  2600. float* ma;
  2601. double* mb;
  2602. uint32 n;
  2603. if (tif->tif_flags&TIFF_SWAB)
  2604. TIFFSwabArrayOfLong((uint32*)origdata,count);
  2605. TIFFCvtIEEEFloatToNative(tif,count,(float*)origdata);
  2606. ma=(float*)origdata;
  2607. mb=data;
  2608. for (n=0; n<count; n++)
  2609. *mb++=(double)(*ma++);
  2610. }
  2611. break;
  2612. }
  2613. _TIFFfree(origdata);
  2614. if (err!=TIFFReadDirEntryErrOk)
  2615. {
  2616. _TIFFfree(data);
  2617. return(err);
  2618. }
  2619. *value=data;
  2620. return(TIFFReadDirEntryErrOk);
  2621. }
  2622. static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
  2623. {
  2624. enum TIFFReadDirEntryErr err;
  2625. uint32 count;
  2626. void* origdata;
  2627. uint64* data;
  2628. switch (direntry->tdir_type)
  2629. {
  2630. case TIFF_LONG:
  2631. case TIFF_LONG8:
  2632. case TIFF_IFD:
  2633. case TIFF_IFD8:
  2634. break;
  2635. default:
  2636. return(TIFFReadDirEntryErrType);
  2637. }
  2638. err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
  2639. if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
  2640. {
  2641. *value=0;
  2642. return(err);
  2643. }
  2644. switch (direntry->tdir_type)
  2645. {
  2646. case TIFF_LONG8:
  2647. case TIFF_IFD8:
  2648. *value=(uint64*)origdata;
  2649. if (tif->tif_flags&TIFF_SWAB)
  2650. TIFFSwabArrayOfLong8(*value,count);
  2651. return(TIFFReadDirEntryErrOk);
  2652. }
  2653. data=(uint64*)_TIFFmalloc(count*8);
  2654. if (data==0)
  2655. {
  2656. _TIFFfree(origdata);
  2657. return(TIFFReadDirEntryErrAlloc);
  2658. }
  2659. switch (direntry->tdir_type)
  2660. {
  2661. case TIFF_LONG:
  2662. case TIFF_IFD:
  2663. {
  2664. uint32* ma;
  2665. uint64* mb;
  2666. uint32 n;
  2667. ma=(uint32*)origdata;
  2668. mb=data;
  2669. for (n=0; n<count; n++)
  2670. {
  2671. if (tif->tif_flags&TIFF_SWAB)
  2672. TIFFSwabLong(ma);
  2673. *mb++=(uint64)(*ma++);
  2674. }
  2675. }
  2676. break;
  2677. }
  2678. _TIFFfree(origdata);
  2679. if (err!=TIFFReadDirEntryErrOk)
  2680. {
  2681. _TIFFfree(data);
  2682. return(err);
  2683. }
  2684. *value=data;
  2685. return(TIFFReadDirEntryErrOk);
  2686. }
  2687. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
  2688. {
  2689. enum TIFFReadDirEntryErr err;
  2690. uint16* m;
  2691. uint16* na;
  2692. uint16 nb;
  2693. if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
  2694. return(TIFFReadDirEntryErrCount);
  2695. err=TIFFReadDirEntryShortArray(tif,direntry,&m);
  2696. if (err!=TIFFReadDirEntryErrOk)
  2697. return(err);
  2698. na=m;
  2699. nb=tif->tif_dir.td_samplesperpixel;
  2700. *value=*na++;
  2701. nb--;
  2702. while (nb>0)
  2703. {
  2704. if (*na++!=*value)
  2705. {
  2706. err=TIFFReadDirEntryErrPsdif;
  2707. break;
  2708. }
  2709. nb--;
  2710. }
  2711. _TIFFfree(m);
  2712. return(err);
  2713. }
  2714. #if 0
  2715. static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2716. {
  2717. enum TIFFReadDirEntryErr err;
  2718. double* m;
  2719. double* na;
  2720. uint16 nb;
  2721. if (direntry->tdir_count<(uint64)tif->tif_dir.td_samplesperpixel)
  2722. return(TIFFReadDirEntryErrCount);
  2723. err=TIFFReadDirEntryDoubleArray(tif,direntry,&m);
  2724. if (err!=TIFFReadDirEntryErrOk)
  2725. return(err);
  2726. na=m;
  2727. nb=tif->tif_dir.td_samplesperpixel;
  2728. *value=*na++;
  2729. nb--;
  2730. while (nb>0)
  2731. {
  2732. if (*na++!=*value)
  2733. {
  2734. err=TIFFReadDirEntryErrPsdif;
  2735. break;
  2736. }
  2737. nb--;
  2738. }
  2739. _TIFFfree(m);
  2740. return(err);
  2741. }
  2742. #endif
  2743. static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
  2744. {
  2745. (void) tif;
  2746. *value=*(uint8*)(&direntry->tdir_offset);
  2747. }
  2748. static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value)
  2749. {
  2750. (void) tif;
  2751. *value=*(int8*)(&direntry->tdir_offset);
  2752. }
  2753. static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
  2754. {
  2755. *value = direntry->tdir_offset.toff_short;
  2756. /* *value=*(uint16*)(&direntry->tdir_offset); */
  2757. if (tif->tif_flags&TIFF_SWAB)
  2758. TIFFSwabShort(value);
  2759. }
  2760. static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value)
  2761. {
  2762. *value=*(int16*)(&direntry->tdir_offset);
  2763. if (tif->tif_flags&TIFF_SWAB)
  2764. TIFFSwabShort((uint16*)value);
  2765. }
  2766. static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
  2767. {
  2768. *value=*(uint32*)(&direntry->tdir_offset);
  2769. if (tif->tif_flags&TIFF_SWAB)
  2770. TIFFSwabLong(value);
  2771. }
  2772. static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value)
  2773. {
  2774. *value=*(int32*)(&direntry->tdir_offset);
  2775. if (tif->tif_flags&TIFF_SWAB)
  2776. TIFFSwabLong((uint32*)value);
  2777. }
  2778. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
  2779. {
  2780. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2781. {
  2782. enum TIFFReadDirEntryErr err;
  2783. uint32 offset = direntry->tdir_offset.toff_long;
  2784. if (tif->tif_flags&TIFF_SWAB)
  2785. TIFFSwabLong(&offset);
  2786. err=TIFFReadDirEntryData(tif,offset,8,value);
  2787. if (err!=TIFFReadDirEntryErrOk)
  2788. return(err);
  2789. }
  2790. else
  2791. *value = direntry->tdir_offset.toff_long8;
  2792. if (tif->tif_flags&TIFF_SWAB)
  2793. TIFFSwabLong8(value);
  2794. return(TIFFReadDirEntryErrOk);
  2795. }
  2796. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value)
  2797. {
  2798. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2799. {
  2800. enum TIFFReadDirEntryErr err;
  2801. uint32 offset = direntry->tdir_offset.toff_long;
  2802. if (tif->tif_flags&TIFF_SWAB)
  2803. TIFFSwabLong(&offset);
  2804. err=TIFFReadDirEntryData(tif,offset,8,value);
  2805. if (err!=TIFFReadDirEntryErrOk)
  2806. return(err);
  2807. }
  2808. else
  2809. *value=*(int64*)(&direntry->tdir_offset);
  2810. if (tif->tif_flags&TIFF_SWAB)
  2811. TIFFSwabLong8((uint64*)value);
  2812. return(TIFFReadDirEntryErrOk);
  2813. }
  2814. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2815. {
  2816. UInt64Aligned_t m;
  2817. assert(sizeof(double)==8);
  2818. assert(sizeof(uint64)==8);
  2819. assert(sizeof(uint32)==4);
  2820. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2821. {
  2822. enum TIFFReadDirEntryErr err;
  2823. uint32 offset = direntry->tdir_offset.toff_long;
  2824. if (tif->tif_flags&TIFF_SWAB)
  2825. TIFFSwabLong(&offset);
  2826. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  2827. if (err!=TIFFReadDirEntryErrOk)
  2828. return(err);
  2829. }
  2830. else
  2831. m.l = direntry->tdir_offset.toff_long8;
  2832. if (tif->tif_flags&TIFF_SWAB)
  2833. TIFFSwabArrayOfLong(m.i,2);
  2834. if (m.i[0]==0)
  2835. *value=0.0;
  2836. else
  2837. *value=(double)m.i[0]/(double)m.i[1];
  2838. return(TIFFReadDirEntryErrOk);
  2839. }
  2840. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2841. {
  2842. UInt64Aligned_t m;
  2843. assert(sizeof(double)==8);
  2844. assert(sizeof(uint64)==8);
  2845. assert(sizeof(int32)==4);
  2846. assert(sizeof(uint32)==4);
  2847. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2848. {
  2849. enum TIFFReadDirEntryErr err;
  2850. uint32 offset = direntry->tdir_offset.toff_long;
  2851. if (tif->tif_flags&TIFF_SWAB)
  2852. TIFFSwabLong(&offset);
  2853. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  2854. if (err!=TIFFReadDirEntryErrOk)
  2855. return(err);
  2856. }
  2857. else
  2858. m.l=direntry->tdir_offset.toff_long8;
  2859. if (tif->tif_flags&TIFF_SWAB)
  2860. TIFFSwabArrayOfLong(m.i,2);
  2861. if ((int32)m.i[0]==0)
  2862. *value=0.0;
  2863. else
  2864. *value=(double)((int32)m.i[0])/(double)m.i[1];
  2865. return(TIFFReadDirEntryErrOk);
  2866. }
  2867. static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
  2868. {
  2869. union
  2870. {
  2871. float f;
  2872. uint32 i;
  2873. } float_union;
  2874. assert(sizeof(float)==4);
  2875. assert(sizeof(uint32)==4);
  2876. assert(sizeof(float_union)==4);
  2877. float_union.i=*(uint32*)(&direntry->tdir_offset);
  2878. *value=float_union.f;
  2879. if (tif->tif_flags&TIFF_SWAB)
  2880. TIFFSwabLong((uint32*)value);
  2881. }
  2882. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
  2883. {
  2884. assert(sizeof(double)==8);
  2885. assert(sizeof(uint64)==8);
  2886. assert(sizeof(UInt64Aligned_t)==8);
  2887. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2888. {
  2889. enum TIFFReadDirEntryErr err;
  2890. uint32 offset = direntry->tdir_offset.toff_long;
  2891. if (tif->tif_flags&TIFF_SWAB)
  2892. TIFFSwabLong(&offset);
  2893. err=TIFFReadDirEntryData(tif,offset,8,value);
  2894. if (err!=TIFFReadDirEntryErrOk)
  2895. return(err);
  2896. }
  2897. else
  2898. {
  2899. UInt64Aligned_t uint64_union;
  2900. uint64_union.l=direntry->tdir_offset.toff_long8;
  2901. *value=uint64_union.d;
  2902. }
  2903. if (tif->tif_flags&TIFF_SWAB)
  2904. TIFFSwabLong8((uint64*)value);
  2905. return(TIFFReadDirEntryErrOk);
  2906. }
  2907. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value)
  2908. {
  2909. if (value<0)
  2910. return(TIFFReadDirEntryErrRange);
  2911. else
  2912. return(TIFFReadDirEntryErrOk);
  2913. }
  2914. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value)
  2915. {
  2916. if (value>0xFF)
  2917. return(TIFFReadDirEntryErrRange);
  2918. else
  2919. return(TIFFReadDirEntryErrOk);
  2920. }
  2921. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value)
  2922. {
  2923. if ((value<0)||(value>0xFF))
  2924. return(TIFFReadDirEntryErrRange);
  2925. else
  2926. return(TIFFReadDirEntryErrOk);
  2927. }
  2928. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value)
  2929. {
  2930. if (value>0xFF)
  2931. return(TIFFReadDirEntryErrRange);
  2932. else
  2933. return(TIFFReadDirEntryErrOk);
  2934. }
  2935. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value)
  2936. {
  2937. if ((value<0)||(value>0xFF))
  2938. return(TIFFReadDirEntryErrRange);
  2939. else
  2940. return(TIFFReadDirEntryErrOk);
  2941. }
  2942. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value)
  2943. {
  2944. if (value>0xFF)
  2945. return(TIFFReadDirEntryErrRange);
  2946. else
  2947. return(TIFFReadDirEntryErrOk);
  2948. }
  2949. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value)
  2950. {
  2951. if ((value<0)||(value>0xFF))
  2952. return(TIFFReadDirEntryErrRange);
  2953. else
  2954. return(TIFFReadDirEntryErrOk);
  2955. }
  2956. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value)
  2957. {
  2958. if (value>0x7F)
  2959. return(TIFFReadDirEntryErrRange);
  2960. else
  2961. return(TIFFReadDirEntryErrOk);
  2962. }
  2963. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value)
  2964. {
  2965. if (value>0x7F)
  2966. return(TIFFReadDirEntryErrRange);
  2967. else
  2968. return(TIFFReadDirEntryErrOk);
  2969. }
  2970. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value)
  2971. {
  2972. if ((value<-0x80)||(value>0x7F))
  2973. return(TIFFReadDirEntryErrRange);
  2974. else
  2975. return(TIFFReadDirEntryErrOk);
  2976. }
  2977. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value)
  2978. {
  2979. if (value>0x7F)
  2980. return(TIFFReadDirEntryErrRange);
  2981. else
  2982. return(TIFFReadDirEntryErrOk);
  2983. }
  2984. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value)
  2985. {
  2986. if ((value<-0x80)||(value>0x7F))
  2987. return(TIFFReadDirEntryErrRange);
  2988. else
  2989. return(TIFFReadDirEntryErrOk);
  2990. }
  2991. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value)
  2992. {
  2993. if (value>0x7F)
  2994. return(TIFFReadDirEntryErrRange);
  2995. else
  2996. return(TIFFReadDirEntryErrOk);
  2997. }
  2998. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value)
  2999. {
  3000. if ((value<-0x80)||(value>0x7F))
  3001. return(TIFFReadDirEntryErrRange);
  3002. else
  3003. return(TIFFReadDirEntryErrOk);
  3004. }
  3005. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value)
  3006. {
  3007. if (value<0)
  3008. return(TIFFReadDirEntryErrRange);
  3009. else
  3010. return(TIFFReadDirEntryErrOk);
  3011. }
  3012. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value)
  3013. {
  3014. if (value<0)
  3015. return(TIFFReadDirEntryErrRange);
  3016. else
  3017. return(TIFFReadDirEntryErrOk);
  3018. }
  3019. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value)
  3020. {
  3021. if (value>0xFFFF)
  3022. return(TIFFReadDirEntryErrRange);
  3023. else
  3024. return(TIFFReadDirEntryErrOk);
  3025. }
  3026. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value)
  3027. {
  3028. if ((value<0)||(value>0xFFFF))
  3029. return(TIFFReadDirEntryErrRange);
  3030. else
  3031. return(TIFFReadDirEntryErrOk);
  3032. }
  3033. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value)
  3034. {
  3035. if (value>0xFFFF)
  3036. return(TIFFReadDirEntryErrRange);
  3037. else
  3038. return(TIFFReadDirEntryErrOk);
  3039. }
  3040. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value)
  3041. {
  3042. if ((value<0)||(value>0xFFFF))
  3043. return(TIFFReadDirEntryErrRange);
  3044. else
  3045. return(TIFFReadDirEntryErrOk);
  3046. }
  3047. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value)
  3048. {
  3049. if (value>0x7FFF)
  3050. return(TIFFReadDirEntryErrRange);
  3051. else
  3052. return(TIFFReadDirEntryErrOk);
  3053. }
  3054. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value)
  3055. {
  3056. if (value>0x7FFF)
  3057. return(TIFFReadDirEntryErrRange);
  3058. else
  3059. return(TIFFReadDirEntryErrOk);
  3060. }
  3061. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value)
  3062. {
  3063. if ((value<-0x8000)||(value>0x7FFF))
  3064. return(TIFFReadDirEntryErrRange);
  3065. else
  3066. return(TIFFReadDirEntryErrOk);
  3067. }
  3068. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value)
  3069. {
  3070. if (value>0x7FFF)
  3071. return(TIFFReadDirEntryErrRange);
  3072. else
  3073. return(TIFFReadDirEntryErrOk);
  3074. }
  3075. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value)
  3076. {
  3077. if ((value<-0x8000)||(value>0x7FFF))
  3078. return(TIFFReadDirEntryErrRange);
  3079. else
  3080. return(TIFFReadDirEntryErrOk);
  3081. }
  3082. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value)
  3083. {
  3084. if (value<0)
  3085. return(TIFFReadDirEntryErrRange);
  3086. else
  3087. return(TIFFReadDirEntryErrOk);
  3088. }
  3089. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value)
  3090. {
  3091. if (value<0)
  3092. return(TIFFReadDirEntryErrRange);
  3093. else
  3094. return(TIFFReadDirEntryErrOk);
  3095. }
  3096. static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value)
  3097. {
  3098. if (value<0)
  3099. return(TIFFReadDirEntryErrRange);
  3100. else
  3101. return(TIFFReadDirEntryErrOk);
  3102. }
  3103. /*
  3104. * Largest 32-bit unsigned integer value.
  3105. */
  3106. #if defined(__WIN32__) && defined(_MSC_VER)
  3107. # define TIFF_UINT32_MAX 0xFFFFFFFFI64
  3108. #else
  3109. # define TIFF_UINT32_MAX 0xFFFFFFFFLL
  3110. #endif
  3111. static enum TIFFReadDirEntryErr
  3112. TIFFReadDirEntryCheckRangeLongLong8(uint64 value)
  3113. {
  3114. if (value > TIFF_UINT32_MAX)
  3115. return(TIFFReadDirEntryErrRange);
  3116. else
  3117. return(TIFFReadDirEntryErrOk);
  3118. }
  3119. static enum TIFFReadDirEntryErr
  3120. TIFFReadDirEntryCheckRangeLongSlong8(int64 value)
  3121. {
  3122. if ((value<0) || (value > TIFF_UINT32_MAX))
  3123. return(TIFFReadDirEntryErrRange);
  3124. else
  3125. return(TIFFReadDirEntryErrOk);
  3126. }
  3127. #undef TIFF_UINT32_MAX
  3128. static enum TIFFReadDirEntryErr
  3129. TIFFReadDirEntryCheckRangeSlongLong(uint32 value)
  3130. {
  3131. if (value > 0x7FFFFFFFUL)
  3132. return(TIFFReadDirEntryErrRange);
  3133. else
  3134. return(TIFFReadDirEntryErrOk);
  3135. }
  3136. static enum TIFFReadDirEntryErr
  3137. TIFFReadDirEntryCheckRangeSlongLong8(uint64 value)
  3138. {
  3139. if (value > 0x7FFFFFFFUL)
  3140. return(TIFFReadDirEntryErrRange);
  3141. else
  3142. return(TIFFReadDirEntryErrOk);
  3143. }
  3144. static enum TIFFReadDirEntryErr
  3145. TIFFReadDirEntryCheckRangeSlongSlong8(int64 value)
  3146. {
  3147. if ((value < 0L-0x80000000L) || (value > 0x7FFFFFFFL))
  3148. return(TIFFReadDirEntryErrRange);
  3149. else
  3150. return(TIFFReadDirEntryErrOk);
  3151. }
  3152. static enum TIFFReadDirEntryErr
  3153. TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value)
  3154. {
  3155. if (value < 0)
  3156. return(TIFFReadDirEntryErrRange);
  3157. else
  3158. return(TIFFReadDirEntryErrOk);
  3159. }
  3160. static enum TIFFReadDirEntryErr
  3161. TIFFReadDirEntryCheckRangeLong8Sshort(int16 value)
  3162. {
  3163. if (value < 0)
  3164. return(TIFFReadDirEntryErrRange);
  3165. else
  3166. return(TIFFReadDirEntryErrOk);
  3167. }
  3168. static enum TIFFReadDirEntryErr
  3169. TIFFReadDirEntryCheckRangeLong8Slong(int32 value)
  3170. {
  3171. if (value < 0)
  3172. return(TIFFReadDirEntryErrRange);
  3173. else
  3174. return(TIFFReadDirEntryErrOk);
  3175. }
  3176. static enum TIFFReadDirEntryErr
  3177. TIFFReadDirEntryCheckRangeLong8Slong8(int64 value)
  3178. {
  3179. if (value < 0)
  3180. return(TIFFReadDirEntryErrRange);
  3181. else
  3182. return(TIFFReadDirEntryErrOk);
  3183. }
  3184. /*
  3185. * Largest 64-bit signed integer value.
  3186. */
  3187. #if defined(__WIN32__) && defined(_MSC_VER)
  3188. # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFI64
  3189. #else
  3190. # define TIFF_INT64_MAX 0x7FFFFFFFFFFFFFFFLL
  3191. #endif
  3192. static enum TIFFReadDirEntryErr
  3193. TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value)
  3194. {
  3195. if (value > TIFF_INT64_MAX)
  3196. return(TIFFReadDirEntryErrRange);
  3197. else
  3198. return(TIFFReadDirEntryErrOk);
  3199. }
  3200. #undef TIFF_INT64_MAX
  3201. static enum TIFFReadDirEntryErr
  3202. TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest)
  3203. {
  3204. assert(size>0);
  3205. if (!isMapped(tif)) {
  3206. if (!SeekOK(tif,offset))
  3207. return(TIFFReadDirEntryErrIo);
  3208. if (!ReadOK(tif,dest,size))
  3209. return(TIFFReadDirEntryErrIo);
  3210. } else {
  3211. tmsize_t ma,mb;
  3212. ma=(tmsize_t)offset;
  3213. mb=ma+size;
  3214. if (((uint64)ma!=offset)||(mb<ma)||(mb<size)||(mb>tif->tif_size))
  3215. return(TIFFReadDirEntryErrIo);
  3216. _TIFFmemcpy(dest,tif->tif_base+ma,size);
  3217. }
  3218. return(TIFFReadDirEntryErrOk);
  3219. }
  3220. static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover)
  3221. {
  3222. if (!recover) {
  3223. switch (err) {
  3224. case TIFFReadDirEntryErrCount:
  3225. TIFFErrorExt(tif->tif_clientdata, module,
  3226. "Incorrect count for \"%s\"",
  3227. tagname);
  3228. break;
  3229. case TIFFReadDirEntryErrType:
  3230. TIFFErrorExt(tif->tif_clientdata, module,
  3231. "Incompatible type for \"%s\"",
  3232. tagname);
  3233. break;
  3234. case TIFFReadDirEntryErrIo:
  3235. TIFFErrorExt(tif->tif_clientdata, module,
  3236. "IO error during reading of \"%s\"",
  3237. tagname);
  3238. break;
  3239. case TIFFReadDirEntryErrRange:
  3240. TIFFErrorExt(tif->tif_clientdata, module,
  3241. "Incorrect value for \"%s\"",
  3242. tagname);
  3243. break;
  3244. case TIFFReadDirEntryErrPsdif:
  3245. TIFFErrorExt(tif->tif_clientdata, module,
  3246. "Cannot handle different values per sample for \"%s\"",
  3247. tagname);
  3248. break;
  3249. case TIFFReadDirEntryErrSizesan:
  3250. TIFFErrorExt(tif->tif_clientdata, module,
  3251. "Sanity check on size of \"%s\" value failed",
  3252. tagname);
  3253. break;
  3254. case TIFFReadDirEntryErrAlloc:
  3255. TIFFErrorExt(tif->tif_clientdata, module,
  3256. "Out of memory reading of \"%s\"",
  3257. tagname);
  3258. break;
  3259. default:
  3260. assert(0); /* we should never get here */
  3261. break;
  3262. }
  3263. } else {
  3264. switch (err) {
  3265. case TIFFReadDirEntryErrCount:
  3266. TIFFErrorExt(tif->tif_clientdata, module,
  3267. "Incorrect count for \"%s\"; tag ignored",
  3268. tagname);
  3269. break;
  3270. case TIFFReadDirEntryErrType:
  3271. TIFFWarningExt(tif->tif_clientdata, module,
  3272. "Incompatible type for \"%s\"; tag ignored",
  3273. tagname);
  3274. break;
  3275. case TIFFReadDirEntryErrIo:
  3276. TIFFWarningExt(tif->tif_clientdata, module,
  3277. "IO error during reading of \"%s\"; tag ignored",
  3278. tagname);
  3279. break;
  3280. case TIFFReadDirEntryErrRange:
  3281. TIFFWarningExt(tif->tif_clientdata, module,
  3282. "Incorrect value for \"%s\"; tag ignored",
  3283. tagname);
  3284. break;
  3285. case TIFFReadDirEntryErrPsdif:
  3286. TIFFWarningExt(tif->tif_clientdata, module,
  3287. "Cannot handle different values per sample for \"%s\"; tag ignored",
  3288. tagname);
  3289. break;
  3290. case TIFFReadDirEntryErrSizesan:
  3291. TIFFWarningExt(tif->tif_clientdata, module,
  3292. "Sanity check on size of \"%s\" value failed; tag ignored",
  3293. tagname);
  3294. break;
  3295. case TIFFReadDirEntryErrAlloc:
  3296. TIFFWarningExt(tif->tif_clientdata, module,
  3297. "Out of memory reading of \"%s\"; tag ignored",
  3298. tagname);
  3299. break;
  3300. default:
  3301. assert(0); /* we should never get here */
  3302. break;
  3303. }
  3304. }
  3305. }
  3306. /*
  3307. * Read the next TIFF directory from a file and convert it to the internal
  3308. * format. We read directories sequentially.
  3309. */
  3310. int
  3311. TIFFReadDirectory(TIFF* tif)
  3312. {
  3313. static const char module[] = "TIFFReadDirectory";
  3314. TIFFDirEntry* dir;
  3315. uint16 dircount;
  3316. TIFFDirEntry* dp;
  3317. uint16 di;
  3318. const TIFFField* fip;
  3319. uint32 fii=FAILED_FII;
  3320. toff_t nextdiroff;
  3321. tif->tif_diroff=tif->tif_nextdiroff;
  3322. if (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))
  3323. return 0; /* last offset or bad offset (IFD looping) */
  3324. (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
  3325. tif->tif_curdir++;
  3326. nextdiroff = tif->tif_nextdiroff;
  3327. dircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);
  3328. if (!dircount)
  3329. {
  3330. TIFFErrorExt(tif->tif_clientdata,module,
  3331. "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);
  3332. return 0;
  3333. }
  3334. TIFFReadDirectoryCheckOrder(tif,dir,dircount);
  3335. /*
  3336. * Mark duplicates of any tag to be ignored (bugzilla 1994)
  3337. * to avoid certain pathological problems.
  3338. */
  3339. {
  3340. TIFFDirEntry* ma;
  3341. uint16 mb;
  3342. for (ma=dir, mb=0; mb<dircount; ma++, mb++)
  3343. {
  3344. TIFFDirEntry* na;
  3345. uint16 nb;
  3346. for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
  3347. {
  3348. if (ma->tdir_tag==na->tdir_tag)
  3349. na->tdir_tag=IGNORE;
  3350. }
  3351. }
  3352. }
  3353. tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
  3354. tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
  3355. /* free any old stuff and reinit */
  3356. TIFFFreeDirectory(tif);
  3357. TIFFDefaultDirectory(tif);
  3358. /*
  3359. * Electronic Arts writes gray-scale TIFF files
  3360. * without a PlanarConfiguration directory entry.
  3361. * Thus we setup a default value here, even though
  3362. * the TIFF spec says there is no default value.
  3363. */
  3364. TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
  3365. /*
  3366. * Setup default value and then make a pass over
  3367. * the fields to check type and tag information,
  3368. * and to extract info required to size data
  3369. * structures. A second pass is made afterwards
  3370. * to read in everthing not taken in the first pass.
  3371. * But we must process the Compression tag first
  3372. * in order to merge in codec-private tag definitions (otherwise
  3373. * we may get complaints about unknown tags). However, the
  3374. * Compression tag may be dependent on the SamplesPerPixel
  3375. * tag value because older TIFF specs permited Compression
  3376. * to be written as a SamplesPerPixel-count tag entry.
  3377. * Thus if we don't first figure out the correct SamplesPerPixel
  3378. * tag value then we may end up ignoring the Compression tag
  3379. * value because it has an incorrect count value (if the
  3380. * true value of SamplesPerPixel is not 1).
  3381. */
  3382. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);
  3383. if (dp)
  3384. {
  3385. if (!TIFFFetchNormalTag(tif,dp,0))
  3386. goto bad;
  3387. dp->tdir_tag=IGNORE;
  3388. }
  3389. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);
  3390. if (dp)
  3391. {
  3392. /*
  3393. * The 5.0 spec says the Compression tag has one value, while
  3394. * earlier specs say it has one value per sample. Because of
  3395. * this, we accept the tag if one value is supplied with either
  3396. * count.
  3397. */
  3398. uint16 value;
  3399. enum TIFFReadDirEntryErr err;
  3400. err=TIFFReadDirEntryShort(tif,dp,&value);
  3401. if (err==TIFFReadDirEntryErrCount)
  3402. err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
  3403. if (err!=TIFFReadDirEntryErrOk)
  3404. {
  3405. TIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);
  3406. goto bad;
  3407. }
  3408. if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))
  3409. goto bad;
  3410. dp->tdir_tag=IGNORE;
  3411. }
  3412. else
  3413. {
  3414. if (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))
  3415. goto bad;
  3416. }
  3417. /*
  3418. * First real pass over the directory.
  3419. */
  3420. for (di=0, dp=dir; di<dircount; di++, dp++)
  3421. {
  3422. if (dp->tdir_tag!=IGNORE)
  3423. {
  3424. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  3425. if (fii == FAILED_FII)
  3426. {
  3427. TIFFWarningExt(tif->tif_clientdata, module,
  3428. "Unknown field with tag %d (0x%x) encountered",
  3429. dp->tdir_tag,dp->tdir_tag);
  3430. /* the following knowingly leaks the
  3431. anonymous field structure */
  3432. if (!_TIFFMergeFields(tif,
  3433. _TIFFCreateAnonField(tif,
  3434. dp->tdir_tag,
  3435. (TIFFDataType) dp->tdir_type),
  3436. 1)) {
  3437. TIFFWarningExt(tif->tif_clientdata,
  3438. module,
  3439. "Registering anonymous field with tag %d (0x%x) failed",
  3440. dp->tdir_tag,
  3441. dp->tdir_tag);
  3442. dp->tdir_tag=IGNORE;
  3443. } else {
  3444. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  3445. assert(fii != FAILED_FII);
  3446. }
  3447. }
  3448. }
  3449. if (dp->tdir_tag!=IGNORE)
  3450. {
  3451. fip=tif->tif_fields[fii];
  3452. if (fip->field_bit==FIELD_IGNORE)
  3453. dp->tdir_tag=IGNORE;
  3454. else
  3455. {
  3456. switch (dp->tdir_tag)
  3457. {
  3458. case TIFFTAG_STRIPOFFSETS:
  3459. case TIFFTAG_STRIPBYTECOUNTS:
  3460. case TIFFTAG_TILEOFFSETS:
  3461. case TIFFTAG_TILEBYTECOUNTS:
  3462. TIFFSetFieldBit(tif,fip->field_bit);
  3463. break;
  3464. case TIFFTAG_IMAGEWIDTH:
  3465. case TIFFTAG_IMAGELENGTH:
  3466. case TIFFTAG_IMAGEDEPTH:
  3467. case TIFFTAG_TILELENGTH:
  3468. case TIFFTAG_TILEWIDTH:
  3469. case TIFFTAG_TILEDEPTH:
  3470. case TIFFTAG_PLANARCONFIG:
  3471. case TIFFTAG_ROWSPERSTRIP:
  3472. case TIFFTAG_EXTRASAMPLES:
  3473. if (!TIFFFetchNormalTag(tif,dp,0))
  3474. goto bad;
  3475. dp->tdir_tag=IGNORE;
  3476. break;
  3477. }
  3478. }
  3479. }
  3480. }
  3481. /*
  3482. * XXX: OJPEG hack.
  3483. * If a) compression is OJPEG, b) planarconfig tag says it's separate,
  3484. * c) strip offsets/bytecounts tag are both present and
  3485. * d) both contain exactly one value, then we consistently find
  3486. * that the buggy implementation of the buggy compression scheme
  3487. * matches contig planarconfig best. So we 'fix-up' the tag here
  3488. */
  3489. if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&
  3490. (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))
  3491. {
  3492. if (!_TIFFFillStriles(tif))
  3493. goto bad;
  3494. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);
  3495. if ((dp!=0)&&(dp->tdir_count==1))
  3496. {
  3497. dp=TIFFReadDirectoryFindEntry(tif,dir,dircount,
  3498. TIFFTAG_STRIPBYTECOUNTS);
  3499. if ((dp!=0)&&(dp->tdir_count==1))
  3500. {
  3501. tif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;
  3502. TIFFWarningExt(tif->tif_clientdata,module,
  3503. "Planarconfig tag value assumed incorrect, "
  3504. "assuming data is contig instead of chunky");
  3505. }
  3506. }
  3507. }
  3508. /*
  3509. * Allocate directory structure and setup defaults.
  3510. */
  3511. if (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
  3512. {
  3513. MissingRequired(tif,"ImageLength");
  3514. goto bad;
  3515. }
  3516. /*
  3517. * Setup appropriate structures (by strip or by tile)
  3518. */
  3519. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  3520. tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
  3521. tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
  3522. tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
  3523. tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
  3524. tif->tif_flags &= ~TIFF_ISTILED;
  3525. } else {
  3526. tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
  3527. tif->tif_flags |= TIFF_ISTILED;
  3528. }
  3529. if (!tif->tif_dir.td_nstrips) {
  3530. TIFFErrorExt(tif->tif_clientdata, module,
  3531. "Cannot handle zero number of %s",
  3532. isTiled(tif) ? "tiles" : "strips");
  3533. goto bad;
  3534. }
  3535. tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
  3536. if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
  3537. tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
  3538. if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
  3539. if ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&
  3540. (isTiled(tif)==0) &&
  3541. (tif->tif_dir.td_nstrips==1)) {
  3542. /*
  3543. * XXX: OJPEG hack.
  3544. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  3545. * and c) the number of strips is 1,
  3546. * then we tolerate the absence of stripoffsets tag,
  3547. * because, presumably, all required data is in the
  3548. * JpegInterchangeFormat stream.
  3549. */
  3550. TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  3551. } else {
  3552. MissingRequired(tif,
  3553. isTiled(tif) ? "TileOffsets" : "StripOffsets");
  3554. goto bad;
  3555. }
  3556. }
  3557. /*
  3558. * Second pass: extract other information.
  3559. */
  3560. for (di=0, dp=dir; di<dircount; di++, dp++)
  3561. {
  3562. switch (dp->tdir_tag)
  3563. {
  3564. case IGNORE:
  3565. break;
  3566. case TIFFTAG_MINSAMPLEVALUE:
  3567. case TIFFTAG_MAXSAMPLEVALUE:
  3568. case TIFFTAG_BITSPERSAMPLE:
  3569. case TIFFTAG_DATATYPE:
  3570. case TIFFTAG_SAMPLEFORMAT:
  3571. /*
  3572. * The MinSampleValue, MaxSampleValue, BitsPerSample
  3573. * DataType and SampleFormat tags are supposed to be
  3574. * written as one value/sample, but some vendors
  3575. * incorrectly write one value only -- so we accept
  3576. * that as well (yech). Other vendors write correct
  3577. * value for NumberOfSamples, but incorrect one for
  3578. * BitsPerSample and friends, and we will read this
  3579. * too.
  3580. */
  3581. {
  3582. uint16 value;
  3583. enum TIFFReadDirEntryErr err;
  3584. err=TIFFReadDirEntryShort(tif,dp,&value);
  3585. if (err==TIFFReadDirEntryErrCount)
  3586. err=TIFFReadDirEntryPersampleShort(tif,dp,&value);
  3587. if (err!=TIFFReadDirEntryErrOk)
  3588. {
  3589. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3590. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  3591. goto bad;
  3592. }
  3593. if (!TIFFSetField(tif,dp->tdir_tag,value))
  3594. goto bad;
  3595. }
  3596. break;
  3597. case TIFFTAG_SMINSAMPLEVALUE:
  3598. case TIFFTAG_SMAXSAMPLEVALUE:
  3599. {
  3600. double *data;
  3601. enum TIFFReadDirEntryErr err;
  3602. uint32 saved_flags;
  3603. int m;
  3604. if (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)
  3605. err = TIFFReadDirEntryErrCount;
  3606. else
  3607. err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
  3608. if (err!=TIFFReadDirEntryErrOk)
  3609. {
  3610. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3611. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  3612. goto bad;
  3613. }
  3614. saved_flags = tif->tif_flags;
  3615. tif->tif_flags |= TIFF_PERSAMPLE;
  3616. m = TIFFSetField(tif,dp->tdir_tag,data);
  3617. tif->tif_flags = saved_flags;
  3618. _TIFFfree(data);
  3619. if (!m)
  3620. goto bad;
  3621. }
  3622. break;
  3623. case TIFFTAG_STRIPOFFSETS:
  3624. case TIFFTAG_TILEOFFSETS:
  3625. #if defined(DEFER_STRILE_LOAD)
  3626. _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),
  3627. dp, sizeof(TIFFDirEntry) );
  3628. #else
  3629. if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))
  3630. goto bad;
  3631. #endif
  3632. break;
  3633. case TIFFTAG_STRIPBYTECOUNTS:
  3634. case TIFFTAG_TILEBYTECOUNTS:
  3635. #if defined(DEFER_STRILE_LOAD)
  3636. _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),
  3637. dp, sizeof(TIFFDirEntry) );
  3638. #else
  3639. if (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))
  3640. goto bad;
  3641. #endif
  3642. break;
  3643. case TIFFTAG_COLORMAP:
  3644. case TIFFTAG_TRANSFERFUNCTION:
  3645. {
  3646. enum TIFFReadDirEntryErr err;
  3647. uint32 countpersample;
  3648. uint32 countrequired;
  3649. uint32 incrementpersample;
  3650. uint16* value=NULL;
  3651. countpersample=(1L<<tif->tif_dir.td_bitspersample);
  3652. if ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))
  3653. {
  3654. countrequired=countpersample;
  3655. incrementpersample=0;
  3656. }
  3657. else
  3658. {
  3659. countrequired=3*countpersample;
  3660. incrementpersample=countpersample;
  3661. }
  3662. if (dp->tdir_count!=(uint64)countrequired)
  3663. err=TIFFReadDirEntryErrCount;
  3664. else
  3665. err=TIFFReadDirEntryShortArray(tif,dp,&value);
  3666. if (err!=TIFFReadDirEntryErrOk)
  3667. {
  3668. fip = TIFFFieldWithTag(tif,dp->tdir_tag);
  3669. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);
  3670. }
  3671. else
  3672. {
  3673. TIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);
  3674. _TIFFfree(value);
  3675. }
  3676. }
  3677. break;
  3678. /* BEGIN REV 4.0 COMPATIBILITY */
  3679. case TIFFTAG_OSUBFILETYPE:
  3680. {
  3681. uint16 valueo;
  3682. uint32 value;
  3683. if (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)
  3684. {
  3685. switch (valueo)
  3686. {
  3687. case OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;
  3688. case OFILETYPE_PAGE: value=FILETYPE_PAGE; break;
  3689. default: value=0; break;
  3690. }
  3691. if (value!=0)
  3692. TIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);
  3693. }
  3694. }
  3695. break;
  3696. /* END REV 4.0 COMPATIBILITY */
  3697. default:
  3698. (void) TIFFFetchNormalTag(tif, dp, TRUE);
  3699. break;
  3700. }
  3701. }
  3702. /*
  3703. * OJPEG hack:
  3704. * - If a) compression is OJPEG, and b) photometric tag is missing,
  3705. * then we consistently find that photometric should be YCbCr
  3706. * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
  3707. * then we consistently find that the buggy implementation of the
  3708. * buggy compression scheme matches photometric YCbCr instead.
  3709. * - If a) compression is OJPEG, and b) bitspersample tag is missing,
  3710. * then we consistently find bitspersample should be 8.
  3711. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  3712. * and c) photometric is RGB or YCbCr, then we consistently find
  3713. * samplesperpixel should be 3
  3714. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  3715. * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
  3716. * find samplesperpixel should be 3
  3717. */
  3718. if (tif->tif_dir.td_compression==COMPRESSION_OJPEG)
  3719. {
  3720. if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
  3721. {
  3722. TIFFWarningExt(tif->tif_clientdata, module,
  3723. "Photometric tag is missing, assuming data is YCbCr");
  3724. if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
  3725. goto bad;
  3726. }
  3727. else if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
  3728. {
  3729. tif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;
  3730. TIFFWarningExt(tif->tif_clientdata, module,
  3731. "Photometric tag value assumed incorrect, "
  3732. "assuming data is YCbCr instead of RGB");
  3733. }
  3734. if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
  3735. {
  3736. TIFFWarningExt(tif->tif_clientdata,module,
  3737. "BitsPerSample tag is missing, assuming 8 bits per sample");
  3738. if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
  3739. goto bad;
  3740. }
  3741. if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
  3742. {
  3743. if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)
  3744. {
  3745. TIFFWarningExt(tif->tif_clientdata,module,
  3746. "SamplesPerPixel tag is missing, "
  3747. "assuming correct SamplesPerPixel value is 3");
  3748. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  3749. goto bad;
  3750. }
  3751. if (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)
  3752. {
  3753. TIFFWarningExt(tif->tif_clientdata,module,
  3754. "SamplesPerPixel tag is missing, "
  3755. "applying correct SamplesPerPixel value of 3");
  3756. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  3757. goto bad;
  3758. }
  3759. else if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)
  3760. || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))
  3761. {
  3762. /*
  3763. * SamplesPerPixel tag is missing, but is not required
  3764. * by spec. Assume correct SamplesPerPixel value of 1.
  3765. */
  3766. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
  3767. goto bad;
  3768. }
  3769. }
  3770. }
  3771. /*
  3772. * Verify Palette image has a Colormap.
  3773. */
  3774. if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
  3775. !TIFFFieldSet(tif, FIELD_COLORMAP)) {
  3776. if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
  3777. tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
  3778. else if (tif->tif_dir.td_bitspersample>=8)
  3779. tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
  3780. else {
  3781. MissingRequired(tif, "Colormap");
  3782. goto bad;
  3783. }
  3784. }
  3785. /*
  3786. * OJPEG hack:
  3787. * We do no further messing with strip/tile offsets/bytecounts in OJPEG
  3788. * TIFFs
  3789. */
  3790. if (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)
  3791. {
  3792. /*
  3793. * Attempt to deal with a missing StripByteCounts tag.
  3794. */
  3795. if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
  3796. /*
  3797. * Some manufacturers violate the spec by not giving
  3798. * the size of the strips. In this case, assume there
  3799. * is one uncompressed strip of data.
  3800. */
  3801. if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
  3802. tif->tif_dir.td_nstrips > 1) ||
  3803. (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
  3804. tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {
  3805. MissingRequired(tif, "StripByteCounts");
  3806. goto bad;
  3807. }
  3808. TIFFWarningExt(tif->tif_clientdata, module,
  3809. "TIFF directory is missing required "
  3810. "\"StripByteCounts\" field, calculating from imagelength");
  3811. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  3812. goto bad;
  3813. /*
  3814. * Assume we have wrong StripByteCount value (in case
  3815. * of single strip) in following cases:
  3816. * - it is equal to zero along with StripOffset;
  3817. * - it is larger than file itself (in case of uncompressed
  3818. * image);
  3819. * - it is smaller than the size of the bytes per row
  3820. * multiplied on the number of rows. The last case should
  3821. * not be checked in the case of writing new image,
  3822. * because we may do not know the exact strip size
  3823. * until the whole image will be written and directory
  3824. * dumped out.
  3825. */
  3826. #define BYTECOUNTLOOKSBAD \
  3827. ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \
  3828. (tif->tif_dir.td_compression == COMPRESSION_NONE && \
  3829. tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \
  3830. (tif->tif_mode == O_RDONLY && \
  3831. tif->tif_dir.td_compression == COMPRESSION_NONE && \
  3832. tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )
  3833. } else if (tif->tif_dir.td_nstrips == 1
  3834. && _TIFFFillStriles(tif)
  3835. && tif->tif_dir.td_stripoffset[0] != 0
  3836. && BYTECOUNTLOOKSBAD) {
  3837. /*
  3838. * XXX: Plexus (and others) sometimes give a value of
  3839. * zero for a tag when they don't know what the
  3840. * correct value is! Try and handle the simple case
  3841. * of estimating the size of a one strip image.
  3842. */
  3843. TIFFWarningExt(tif->tif_clientdata, module,
  3844. "Bogus \"StripByteCounts\" field, ignoring and calculating from imagelength");
  3845. if(EstimateStripByteCounts(tif, dir, dircount) < 0)
  3846. goto bad;
  3847. #if !defined(DEFER_STRILE_LOAD)
  3848. } else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG
  3849. && tif->tif_dir.td_nstrips > 2
  3850. && tif->tif_dir.td_compression == COMPRESSION_NONE
  3851. && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]
  3852. && tif->tif_dir.td_stripbytecount[0] != 0
  3853. && tif->tif_dir.td_stripbytecount[1] != 0 ) {
  3854. /*
  3855. * XXX: Some vendors fill StripByteCount array with
  3856. * absolutely wrong values (it can be equal to
  3857. * StripOffset array, for example). Catch this case
  3858. * here.
  3859. *
  3860. * We avoid this check if deferring strile loading
  3861. * as it would always force us to load the strip/tile
  3862. * information.
  3863. */
  3864. TIFFWarningExt(tif->tif_clientdata, module,
  3865. "Wrong \"StripByteCounts\" field, ignoring and calculating from imagelength");
  3866. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  3867. goto bad;
  3868. #endif /* !defined(DEFER_STRILE_LOAD) */
  3869. }
  3870. }
  3871. if (dir)
  3872. {
  3873. _TIFFfree(dir);
  3874. dir=NULL;
  3875. }
  3876. if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  3877. {
  3878. if (tif->tif_dir.td_bitspersample>=16)
  3879. tif->tif_dir.td_maxsamplevalue=0xFFFF;
  3880. else
  3881. tif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);
  3882. }
  3883. /*
  3884. * XXX: We can optimize checking for the strip bounds using the sorted
  3885. * bytecounts array. See also comments for TIFFAppendToStrip()
  3886. * function in tif_write.c.
  3887. */
  3888. #if !defined(DEFER_STRILE_LOAD)
  3889. if (tif->tif_dir.td_nstrips > 1) {
  3890. uint32 strip;
  3891. tif->tif_dir.td_stripbytecountsorted = 1;
  3892. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
  3893. if (tif->tif_dir.td_stripoffset[strip - 1] >
  3894. tif->tif_dir.td_stripoffset[strip]) {
  3895. tif->tif_dir.td_stripbytecountsorted = 0;
  3896. break;
  3897. }
  3898. }
  3899. }
  3900. #endif /* !defined(DEFER_STRILE_LOAD) */
  3901. /*
  3902. * An opportunity for compression mode dependent tag fixup
  3903. */
  3904. (*tif->tif_fixuptags)(tif);
  3905. /*
  3906. * Some manufacturers make life difficult by writing
  3907. * large amounts of uncompressed data as a single strip.
  3908. * This is contrary to the recommendations of the spec.
  3909. * The following makes an attempt at breaking such images
  3910. * into strips closer to the recommended 8k bytes. A
  3911. * side effect, however, is that the RowsPerStrip tag
  3912. * value may be changed.
  3913. */
  3914. if ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&
  3915. (tif->tif_dir.td_nstrips==1)&&
  3916. (tif->tif_dir.td_compression==COMPRESSION_NONE)&&
  3917. ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))
  3918. {
  3919. if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )
  3920. return 0;
  3921. ChopUpSingleUncompressedStrip(tif);
  3922. }
  3923. /*
  3924. * Clear the dirty directory flag.
  3925. */
  3926. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  3927. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  3928. /*
  3929. * Reinitialize i/o since we are starting on a new directory.
  3930. */
  3931. tif->tif_row = (uint32) -1;
  3932. tif->tif_curstrip = (uint32) -1;
  3933. tif->tif_col = (uint32) -1;
  3934. tif->tif_curtile = (uint32) -1;
  3935. tif->tif_tilesize = (tmsize_t) -1;
  3936. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  3937. if (!tif->tif_scanlinesize) {
  3938. TIFFErrorExt(tif->tif_clientdata, module,
  3939. "Cannot handle zero scanline size");
  3940. return (0);
  3941. }
  3942. if (isTiled(tif)) {
  3943. tif->tif_tilesize = TIFFTileSize(tif);
  3944. if (!tif->tif_tilesize) {
  3945. TIFFErrorExt(tif->tif_clientdata, module,
  3946. "Cannot handle zero tile size");
  3947. return (0);
  3948. }
  3949. } else {
  3950. if (!TIFFStripSize(tif)) {
  3951. TIFFErrorExt(tif->tif_clientdata, module,
  3952. "Cannot handle zero strip size");
  3953. return (0);
  3954. }
  3955. }
  3956. return (1);
  3957. bad:
  3958. if (dir)
  3959. _TIFFfree(dir);
  3960. return (0);
  3961. }
  3962. static void
  3963. TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  3964. {
  3965. static const char module[] = "TIFFReadDirectoryCheckOrder";
  3966. uint16 m;
  3967. uint16 n;
  3968. TIFFDirEntry* o;
  3969. m=0;
  3970. for (n=0, o=dir; n<dircount; n++, o++)
  3971. {
  3972. if (o->tdir_tag<m)
  3973. {
  3974. TIFFWarningExt(tif->tif_clientdata,module,
  3975. "Invalid TIFF directory; tags are not sorted in ascending order");
  3976. break;
  3977. }
  3978. m=o->tdir_tag+1;
  3979. }
  3980. }
  3981. static TIFFDirEntry*
  3982. TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
  3983. {
  3984. TIFFDirEntry* m;
  3985. uint16 n;
  3986. (void) tif;
  3987. for (m=dir, n=0; n<dircount; m++, n++)
  3988. {
  3989. if (m->tdir_tag==tagid)
  3990. return(m);
  3991. }
  3992. return(0);
  3993. }
  3994. static void
  3995. TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii)
  3996. {
  3997. int32 ma,mb,mc;
  3998. ma=-1;
  3999. mc=(int32)tif->tif_nfields;
  4000. while (1)
  4001. {
  4002. if (ma+1==mc)
  4003. {
  4004. *fii = FAILED_FII;
  4005. return;
  4006. }
  4007. mb=(ma+mc)/2;
  4008. if (tif->tif_fields[mb]->field_tag==(uint32)tagid)
  4009. break;
  4010. if (tif->tif_fields[mb]->field_tag<(uint32)tagid)
  4011. ma=mb;
  4012. else
  4013. mc=mb;
  4014. }
  4015. while (1)
  4016. {
  4017. if (mb==0)
  4018. break;
  4019. if (tif->tif_fields[mb-1]->field_tag!=(uint32)tagid)
  4020. break;
  4021. mb--;
  4022. }
  4023. *fii=mb;
  4024. }
  4025. /*
  4026. * Read custom directory from the arbitarry offset.
  4027. * The code is very similar to TIFFReadDirectory().
  4028. */
  4029. int
  4030. TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
  4031. const TIFFFieldArray* infoarray)
  4032. {
  4033. static const char module[] = "TIFFReadCustomDirectory";
  4034. TIFFDirEntry* dir;
  4035. uint16 dircount;
  4036. TIFFDirEntry* dp;
  4037. uint16 di;
  4038. const TIFFField* fip;
  4039. uint32 fii;
  4040. _TIFFSetupFields(tif, infoarray);
  4041. dircount=TIFFFetchDirectory(tif,diroff,&dir,NULL);
  4042. if (!dircount)
  4043. {
  4044. TIFFErrorExt(tif->tif_clientdata,module,
  4045. "Failed to read custom directory at offset " TIFF_UINT64_FORMAT,diroff);
  4046. return 0;
  4047. }
  4048. TIFFFreeDirectory(tif);
  4049. _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
  4050. TIFFReadDirectoryCheckOrder(tif,dir,dircount);
  4051. for (di=0, dp=dir; di<dircount; di++, dp++)
  4052. {
  4053. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4054. if (fii == FAILED_FII)
  4055. {
  4056. TIFFWarningExt(tif->tif_clientdata, module,
  4057. "Unknown field with tag %d (0x%x) encountered",
  4058. dp->tdir_tag, dp->tdir_tag);
  4059. if (!_TIFFMergeFields(tif, _TIFFCreateAnonField(tif,
  4060. dp->tdir_tag,
  4061. (TIFFDataType) dp->tdir_type),
  4062. 1)) {
  4063. TIFFWarningExt(tif->tif_clientdata, module,
  4064. "Registering anonymous field with tag %d (0x%x) failed",
  4065. dp->tdir_tag, dp->tdir_tag);
  4066. dp->tdir_tag=IGNORE;
  4067. } else {
  4068. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4069. assert( fii != FAILED_FII );
  4070. }
  4071. }
  4072. if (dp->tdir_tag!=IGNORE)
  4073. {
  4074. fip=tif->tif_fields[fii];
  4075. if (fip->field_bit==FIELD_IGNORE)
  4076. dp->tdir_tag=IGNORE;
  4077. else
  4078. {
  4079. /* check data type */
  4080. while ((fip->field_type!=TIFF_ANY)&&(fip->field_type!=dp->tdir_type))
  4081. {
  4082. fii++;
  4083. if ((fii==tif->tif_nfields)||
  4084. (tif->tif_fields[fii]->field_tag!=(uint32)dp->tdir_tag))
  4085. {
  4086. fii=0xFFFF;
  4087. break;
  4088. }
  4089. fip=tif->tif_fields[fii];
  4090. }
  4091. if (fii==0xFFFF)
  4092. {
  4093. TIFFWarningExt(tif->tif_clientdata, module,
  4094. "Wrong data type %d for \"%s\"; tag ignored",
  4095. dp->tdir_type,fip->field_name);
  4096. dp->tdir_tag=IGNORE;
  4097. }
  4098. else
  4099. {
  4100. /* check count if known in advance */
  4101. if ((fip->field_readcount!=TIFF_VARIABLE)&&
  4102. (fip->field_readcount!=TIFF_VARIABLE2))
  4103. {
  4104. uint32 expected;
  4105. if (fip->field_readcount==TIFF_SPP)
  4106. expected=(uint32)tif->tif_dir.td_samplesperpixel;
  4107. else
  4108. expected=(uint32)fip->field_readcount;
  4109. if (!CheckDirCount(tif,dp,expected))
  4110. dp->tdir_tag=IGNORE;
  4111. }
  4112. }
  4113. }
  4114. switch (dp->tdir_tag)
  4115. {
  4116. case IGNORE:
  4117. break;
  4118. case EXIFTAG_SUBJECTDISTANCE:
  4119. (void) TIFFFetchSubjectDistance(tif,dp);
  4120. break;
  4121. default:
  4122. (void) TIFFFetchNormalTag(tif, dp, TRUE);
  4123. break;
  4124. }
  4125. }
  4126. }
  4127. if (dir)
  4128. _TIFFfree(dir);
  4129. return 1;
  4130. }
  4131. /*
  4132. * EXIF is important special case of custom IFD, so we have a special
  4133. * function to read it.
  4134. */
  4135. int
  4136. TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
  4137. {
  4138. const TIFFFieldArray* exifFieldArray;
  4139. exifFieldArray = _TIFFGetExifFields();
  4140. return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
  4141. }
  4142. static int
  4143. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  4144. {
  4145. static const char module[] = "EstimateStripByteCounts";
  4146. TIFFDirEntry *dp;
  4147. TIFFDirectory *td = &tif->tif_dir;
  4148. uint32 strip;
  4149. _TIFFFillStriles( tif );
  4150. if (td->td_stripbytecount)
  4151. _TIFFfree(td->td_stripbytecount);
  4152. td->td_stripbytecount = (uint64*)
  4153. _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),
  4154. "for \"StripByteCounts\" array");
  4155. if( td->td_stripbytecount == NULL )
  4156. return -1;
  4157. if (td->td_compression != COMPRESSION_NONE) {
  4158. uint64 space;
  4159. uint64 filesize;
  4160. uint16 n;
  4161. filesize = TIFFGetFileSize(tif);
  4162. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4163. space=sizeof(TIFFHeaderClassic)+2+dircount*12+4;
  4164. else
  4165. space=sizeof(TIFFHeaderBig)+8+dircount*20+8;
  4166. /* calculate amount of space used by indirect values */
  4167. for (dp = dir, n = dircount; n > 0; n--, dp++)
  4168. {
  4169. uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
  4170. uint64 datasize;
  4171. typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);
  4172. if (typewidth == 0) {
  4173. TIFFErrorExt(tif->tif_clientdata, module,
  4174. "Cannot determine size of unknown tag type %d",
  4175. dp->tdir_type);
  4176. return -1;
  4177. }
  4178. datasize=(uint64)typewidth*dp->tdir_count;
  4179. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4180. {
  4181. if (datasize<=4)
  4182. datasize=0;
  4183. }
  4184. else
  4185. {
  4186. if (datasize<=8)
  4187. datasize=0;
  4188. }
  4189. space+=datasize;
  4190. }
  4191. space = filesize - space;
  4192. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  4193. space /= td->td_samplesperpixel;
  4194. for (strip = 0; strip < td->td_nstrips; strip++)
  4195. td->td_stripbytecount[strip] = space;
  4196. /*
  4197. * This gross hack handles the case were the offset to
  4198. * the last strip is past the place where we think the strip
  4199. * should begin. Since a strip of data must be contiguous,
  4200. * it's safe to assume that we've overestimated the amount
  4201. * of data in the strip and trim this number back accordingly.
  4202. */
  4203. strip--;
  4204. if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)
  4205. td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];
  4206. } else if (isTiled(tif)) {
  4207. uint64 bytespertile = TIFFTileSize64(tif);
  4208. for (strip = 0; strip < td->td_nstrips; strip++)
  4209. td->td_stripbytecount[strip] = bytespertile;
  4210. } else {
  4211. uint64 rowbytes = TIFFScanlineSize64(tif);
  4212. uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
  4213. for (strip = 0; strip < td->td_nstrips; strip++)
  4214. td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
  4215. }
  4216. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  4217. if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  4218. td->td_rowsperstrip = td->td_imagelength;
  4219. return 1;
  4220. }
  4221. static void
  4222. MissingRequired(TIFF* tif, const char* tagname)
  4223. {
  4224. static const char module[] = "MissingRequired";
  4225. TIFFErrorExt(tif->tif_clientdata, module,
  4226. "TIFF directory is missing required \"%s\" field",
  4227. tagname);
  4228. }
  4229. /*
  4230. * Check the directory offset against the list of already seen directory
  4231. * offsets. This is a trick to prevent IFD looping. The one can create TIFF
  4232. * file with looped directory pointers. We will maintain a list of already
  4233. * seen directories and check every IFD offset against that list.
  4234. */
  4235. static int
  4236. TIFFCheckDirOffset(TIFF* tif, uint64 diroff)
  4237. {
  4238. uint16 n;
  4239. if (diroff == 0) /* no more directories */
  4240. return 0;
  4241. for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
  4242. if (tif->tif_dirlist[n] == diroff)
  4243. return 0;
  4244. }
  4245. tif->tif_dirnumber++;
  4246. if (tif->tif_dirnumber > tif->tif_dirlistsize) {
  4247. uint64* new_dirlist;
  4248. /*
  4249. * XXX: Reduce memory allocation granularity of the dirlist
  4250. * array.
  4251. */
  4252. new_dirlist = (uint64*)_TIFFCheckRealloc(tif, tif->tif_dirlist,
  4253. tif->tif_dirnumber, 2 * sizeof(uint64), "for IFD list");
  4254. if (!new_dirlist)
  4255. return 0;
  4256. tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
  4257. tif->tif_dirlist = new_dirlist;
  4258. }
  4259. tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
  4260. return 1;
  4261. }
  4262. /*
  4263. * Check the count field of a directory entry against a known value. The
  4264. * caller is expected to skip/ignore the tag if there is a mismatch.
  4265. */
  4266. static int
  4267. CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
  4268. {
  4269. if ((uint64)count > dir->tdir_count) {
  4270. const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  4271. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  4272. "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag ignored",
  4273. fip ? fip->field_name : "unknown tagname",
  4274. dir->tdir_count, count);
  4275. return (0);
  4276. } else if ((uint64)count < dir->tdir_count) {
  4277. const TIFFField* fip = TIFFFieldWithTag(tif, dir->tdir_tag);
  4278. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  4279. "incorrect count for field \"%s\" (" TIFF_UINT64_FORMAT ", expecting %u); tag trimmed",
  4280. fip ? fip->field_name : "unknown tagname",
  4281. dir->tdir_count, count);
  4282. dir->tdir_count = count;
  4283. return (1);
  4284. }
  4285. return (1);
  4286. }
  4287. /*
  4288. * Read IFD structure from the specified offset. If the pointer to
  4289. * nextdiroff variable has been specified, read it too. Function returns a
  4290. * number of fields in the directory or 0 if failed.
  4291. */
  4292. static uint16
  4293. TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir,
  4294. uint64 *nextdiroff)
  4295. {
  4296. static const char module[] = "TIFFFetchDirectory";
  4297. void* origdir;
  4298. uint16 dircount16;
  4299. uint32 dirsize;
  4300. TIFFDirEntry* dir;
  4301. uint8* ma;
  4302. TIFFDirEntry* mb;
  4303. uint16 n;
  4304. assert(pdir);
  4305. tif->tif_diroff = diroff;
  4306. if (nextdiroff)
  4307. *nextdiroff = 0;
  4308. if (!isMapped(tif)) {
  4309. if (!SeekOK(tif, tif->tif_diroff)) {
  4310. TIFFErrorExt(tif->tif_clientdata, module,
  4311. "%s: Seek error accessing TIFF directory",
  4312. tif->tif_name);
  4313. return 0;
  4314. }
  4315. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4316. {
  4317. if (!ReadOK(tif, &dircount16, sizeof (uint16))) {
  4318. TIFFErrorExt(tif->tif_clientdata, module,
  4319. "%s: Can not read TIFF directory count",
  4320. tif->tif_name);
  4321. return 0;
  4322. }
  4323. if (tif->tif_flags & TIFF_SWAB)
  4324. TIFFSwabShort(&dircount16);
  4325. if (dircount16>4096)
  4326. {
  4327. TIFFErrorExt(tif->tif_clientdata, module,
  4328. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4329. return 0;
  4330. }
  4331. dirsize = 12;
  4332. } else {
  4333. uint64 dircount64;
  4334. if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
  4335. TIFFErrorExt(tif->tif_clientdata, module,
  4336. "%s: Can not read TIFF directory count",
  4337. tif->tif_name);
  4338. return 0;
  4339. }
  4340. if (tif->tif_flags & TIFF_SWAB)
  4341. TIFFSwabLong8(&dircount64);
  4342. if (dircount64>4096)
  4343. {
  4344. TIFFErrorExt(tif->tif_clientdata, module,
  4345. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4346. return 0;
  4347. }
  4348. dircount16 = (uint16)dircount64;
  4349. dirsize = 20;
  4350. }
  4351. origdir = _TIFFCheckMalloc(tif, dircount16,
  4352. dirsize, "to read TIFF directory");
  4353. if (origdir == NULL)
  4354. return 0;
  4355. if (!ReadOK(tif, origdir, (tmsize_t)(dircount16*dirsize))) {
  4356. TIFFErrorExt(tif->tif_clientdata, module,
  4357. "%.100s: Can not read TIFF directory",
  4358. tif->tif_name);
  4359. _TIFFfree(origdir);
  4360. return 0;
  4361. }
  4362. /*
  4363. * Read offset to next directory for sequential scans if
  4364. * needed.
  4365. */
  4366. if (nextdiroff)
  4367. {
  4368. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4369. {
  4370. uint32 nextdiroff32;
  4371. if (!ReadOK(tif, &nextdiroff32, sizeof(uint32)))
  4372. nextdiroff32 = 0;
  4373. if (tif->tif_flags&TIFF_SWAB)
  4374. TIFFSwabLong(&nextdiroff32);
  4375. *nextdiroff=nextdiroff32;
  4376. } else {
  4377. if (!ReadOK(tif, nextdiroff, sizeof(uint64)))
  4378. *nextdiroff = 0;
  4379. if (tif->tif_flags&TIFF_SWAB)
  4380. TIFFSwabLong8(nextdiroff);
  4381. }
  4382. }
  4383. } else {
  4384. tmsize_t m;
  4385. tmsize_t off = (tmsize_t) tif->tif_diroff;
  4386. if ((uint64)off!=tif->tif_diroff)
  4387. {
  4388. TIFFErrorExt(tif->tif_clientdata,module,"Can not read TIFF directory count");
  4389. return(0);
  4390. }
  4391. /*
  4392. * Check for integer overflow when validating the dir_off,
  4393. * otherwise a very high offset may cause an OOB read and
  4394. * crash the client. Make two comparisons instead of
  4395. *
  4396. * off + sizeof(uint16) > tif->tif_size
  4397. *
  4398. * to avoid overflow.
  4399. */
  4400. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4401. {
  4402. m=off+sizeof(uint16);
  4403. if ((m<off)||(m<(tmsize_t)sizeof(uint16))||(m>tif->tif_size)) {
  4404. TIFFErrorExt(tif->tif_clientdata, module,
  4405. "Can not read TIFF directory count");
  4406. return 0;
  4407. } else {
  4408. _TIFFmemcpy(&dircount16, tif->tif_base + off,
  4409. sizeof(uint16));
  4410. }
  4411. off += sizeof (uint16);
  4412. if (tif->tif_flags & TIFF_SWAB)
  4413. TIFFSwabShort(&dircount16);
  4414. if (dircount16>4096)
  4415. {
  4416. TIFFErrorExt(tif->tif_clientdata, module,
  4417. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4418. return 0;
  4419. }
  4420. dirsize = 12;
  4421. }
  4422. else
  4423. {
  4424. tmsize_t m;
  4425. uint64 dircount64;
  4426. m=off+sizeof(uint64);
  4427. if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size)) {
  4428. TIFFErrorExt(tif->tif_clientdata, module,
  4429. "Can not read TIFF directory count");
  4430. return 0;
  4431. } else {
  4432. _TIFFmemcpy(&dircount64, tif->tif_base + off,
  4433. sizeof(uint64));
  4434. }
  4435. off += sizeof (uint64);
  4436. if (tif->tif_flags & TIFF_SWAB)
  4437. TIFFSwabLong8(&dircount64);
  4438. if (dircount64>4096)
  4439. {
  4440. TIFFErrorExt(tif->tif_clientdata, module,
  4441. "Sanity check on directory count failed, this is probably not a valid IFD offset");
  4442. return 0;
  4443. }
  4444. dircount16 = (uint16)dircount64;
  4445. dirsize = 20;
  4446. }
  4447. if (dircount16 == 0 )
  4448. {
  4449. TIFFErrorExt(tif->tif_clientdata, module,
  4450. "Sanity check on directory count failed, zero tag directories not supported");
  4451. return 0;
  4452. }
  4453. origdir = _TIFFCheckMalloc(tif, dircount16,
  4454. dirsize,
  4455. "to read TIFF directory");
  4456. if (origdir == NULL)
  4457. return 0;
  4458. m=off+dircount16*dirsize;
  4459. if ((m<off)||(m<(tmsize_t)(dircount16*dirsize))||(m>tif->tif_size)) {
  4460. TIFFErrorExt(tif->tif_clientdata, module,
  4461. "Can not read TIFF directory");
  4462. _TIFFfree(origdir);
  4463. return 0;
  4464. } else {
  4465. _TIFFmemcpy(origdir, tif->tif_base + off,
  4466. dircount16 * dirsize);
  4467. }
  4468. if (nextdiroff) {
  4469. off += dircount16 * dirsize;
  4470. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4471. {
  4472. uint32 nextdiroff32;
  4473. m=off+sizeof(uint32);
  4474. if ((m<off)||(m<(tmsize_t)sizeof(uint32))||(m>tif->tif_size))
  4475. nextdiroff32 = 0;
  4476. else
  4477. _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
  4478. sizeof (uint32));
  4479. if (tif->tif_flags&TIFF_SWAB)
  4480. TIFFSwabLong(&nextdiroff32);
  4481. *nextdiroff = nextdiroff32;
  4482. }
  4483. else
  4484. {
  4485. m=off+sizeof(uint64);
  4486. if ((m<off)||(m<(tmsize_t)sizeof(uint64))||(m>tif->tif_size))
  4487. *nextdiroff = 0;
  4488. else
  4489. _TIFFmemcpy(nextdiroff, tif->tif_base + off,
  4490. sizeof (uint64));
  4491. if (tif->tif_flags&TIFF_SWAB)
  4492. TIFFSwabLong8(nextdiroff);
  4493. }
  4494. }
  4495. }
  4496. dir = (TIFFDirEntry*)_TIFFCheckMalloc(tif, dircount16,
  4497. sizeof(TIFFDirEntry),
  4498. "to read TIFF directory");
  4499. if (dir==0)
  4500. {
  4501. _TIFFfree(origdir);
  4502. return 0;
  4503. }
  4504. ma=(uint8*)origdir;
  4505. mb=dir;
  4506. for (n=0; n<dircount16; n++)
  4507. {
  4508. if (tif->tif_flags&TIFF_SWAB)
  4509. TIFFSwabShort((uint16*)ma);
  4510. mb->tdir_tag=*(uint16*)ma;
  4511. ma+=sizeof(uint16);
  4512. if (tif->tif_flags&TIFF_SWAB)
  4513. TIFFSwabShort((uint16*)ma);
  4514. mb->tdir_type=*(uint16*)ma;
  4515. ma+=sizeof(uint16);
  4516. if (!(tif->tif_flags&TIFF_BIGTIFF))
  4517. {
  4518. if (tif->tif_flags&TIFF_SWAB)
  4519. TIFFSwabLong((uint32*)ma);
  4520. mb->tdir_count=(uint64)(*(uint32*)ma);
  4521. ma+=sizeof(uint32);
  4522. *(uint32*)(&mb->tdir_offset)=*(uint32*)ma;
  4523. ma+=sizeof(uint32);
  4524. }
  4525. else
  4526. {
  4527. if (tif->tif_flags&TIFF_SWAB)
  4528. TIFFSwabLong8((uint64*)ma);
  4529. mb->tdir_count=TIFFReadUInt64(ma);
  4530. ma+=sizeof(uint64);
  4531. mb->tdir_offset.toff_long8=TIFFReadUInt64(ma);
  4532. ma+=sizeof(uint64);
  4533. }
  4534. mb++;
  4535. }
  4536. _TIFFfree(origdir);
  4537. *pdir = dir;
  4538. return dircount16;
  4539. }
  4540. /*
  4541. * Fetch a tag that is not handled by special case code.
  4542. */
  4543. static int
  4544. TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
  4545. {
  4546. static const char module[] = "TIFFFetchNormalTag";
  4547. enum TIFFReadDirEntryErr err;
  4548. uint32 fii;
  4549. const TIFFField* fip = NULL;
  4550. TIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);
  4551. if( fii == FAILED_FII )
  4552. {
  4553. TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
  4554. "No definition found for tag %d",
  4555. dp->tdir_tag);
  4556. return 0;
  4557. }
  4558. fip=tif->tif_fields[fii];
  4559. assert(fip->set_field_type!=TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with this in specialized code */
  4560. assert(fip->set_field_type!=TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only the case for pseudo-tags */
  4561. err=TIFFReadDirEntryErrOk;
  4562. switch (fip->set_field_type)
  4563. {
  4564. case TIFF_SETGET_UNDEFINED:
  4565. break;
  4566. case TIFF_SETGET_ASCII:
  4567. {
  4568. uint8* data;
  4569. assert(fip->field_passcount==0);
  4570. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4571. if (err==TIFFReadDirEntryErrOk)
  4572. {
  4573. uint8* ma;
  4574. uint32 mb;
  4575. int n;
  4576. ma=data;
  4577. mb=0;
  4578. while (mb<(uint32)dp->tdir_count)
  4579. {
  4580. if (*ma==0)
  4581. break;
  4582. ma++;
  4583. mb++;
  4584. }
  4585. if (mb+1<(uint32)dp->tdir_count)
  4586. 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);
  4587. else if (mb+1>(uint32)dp->tdir_count)
  4588. {
  4589. uint8* o;
  4590. TIFFWarningExt(tif->tif_clientdata,module,"ASCII value for tag \"%s\" does not end in null byte",fip->field_name);
  4591. if ((uint32)dp->tdir_count+1!=dp->tdir_count+1)
  4592. o=NULL;
  4593. else
  4594. o=_TIFFmalloc((uint32)dp->tdir_count+1);
  4595. if (o==NULL)
  4596. {
  4597. if (data!=NULL)
  4598. _TIFFfree(data);
  4599. return(0);
  4600. }
  4601. _TIFFmemcpy(o,data,(uint32)dp->tdir_count);
  4602. o[(uint32)dp->tdir_count]=0;
  4603. if (data!=0)
  4604. _TIFFfree(data);
  4605. data=o;
  4606. }
  4607. n=TIFFSetField(tif,dp->tdir_tag,data);
  4608. if (data!=0)
  4609. _TIFFfree(data);
  4610. if (!n)
  4611. return(0);
  4612. }
  4613. }
  4614. break;
  4615. case TIFF_SETGET_UINT8:
  4616. {
  4617. uint8 data;
  4618. assert(fip->field_readcount==1);
  4619. assert(fip->field_passcount==0);
  4620. err=TIFFReadDirEntryByte(tif,dp,&data);
  4621. if (err==TIFFReadDirEntryErrOk)
  4622. {
  4623. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4624. return(0);
  4625. }
  4626. }
  4627. break;
  4628. case TIFF_SETGET_UINT16:
  4629. {
  4630. uint16 data;
  4631. assert(fip->field_readcount==1);
  4632. assert(fip->field_passcount==0);
  4633. err=TIFFReadDirEntryShort(tif,dp,&data);
  4634. if (err==TIFFReadDirEntryErrOk)
  4635. {
  4636. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4637. return(0);
  4638. }
  4639. }
  4640. break;
  4641. case TIFF_SETGET_UINT32:
  4642. {
  4643. uint32 data;
  4644. assert(fip->field_readcount==1);
  4645. assert(fip->field_passcount==0);
  4646. err=TIFFReadDirEntryLong(tif,dp,&data);
  4647. if (err==TIFFReadDirEntryErrOk)
  4648. {
  4649. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4650. return(0);
  4651. }
  4652. }
  4653. break;
  4654. case TIFF_SETGET_UINT64:
  4655. {
  4656. uint64 data;
  4657. assert(fip->field_readcount==1);
  4658. assert(fip->field_passcount==0);
  4659. err=TIFFReadDirEntryLong8(tif,dp,&data);
  4660. if (err==TIFFReadDirEntryErrOk)
  4661. {
  4662. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4663. return(0);
  4664. }
  4665. }
  4666. break;
  4667. case TIFF_SETGET_FLOAT:
  4668. {
  4669. float data;
  4670. assert(fip->field_readcount==1);
  4671. assert(fip->field_passcount==0);
  4672. err=TIFFReadDirEntryFloat(tif,dp,&data);
  4673. if (err==TIFFReadDirEntryErrOk)
  4674. {
  4675. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4676. return(0);
  4677. }
  4678. }
  4679. break;
  4680. case TIFF_SETGET_DOUBLE:
  4681. {
  4682. double data;
  4683. assert(fip->field_readcount==1);
  4684. assert(fip->field_passcount==0);
  4685. err=TIFFReadDirEntryDouble(tif,dp,&data);
  4686. if (err==TIFFReadDirEntryErrOk)
  4687. {
  4688. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4689. return(0);
  4690. }
  4691. }
  4692. break;
  4693. case TIFF_SETGET_IFD8:
  4694. {
  4695. uint64 data;
  4696. assert(fip->field_readcount==1);
  4697. assert(fip->field_passcount==0);
  4698. err=TIFFReadDirEntryIfd8(tif,dp,&data);
  4699. if (err==TIFFReadDirEntryErrOk)
  4700. {
  4701. if (!TIFFSetField(tif,dp->tdir_tag,data))
  4702. return(0);
  4703. }
  4704. }
  4705. break;
  4706. case TIFF_SETGET_UINT16_PAIR:
  4707. {
  4708. uint16* data;
  4709. assert(fip->field_readcount==2);
  4710. assert(fip->field_passcount==0);
  4711. if (dp->tdir_count!=2)
  4712. return(0);
  4713. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4714. if (err==TIFFReadDirEntryErrOk)
  4715. {
  4716. int m;
  4717. m=TIFFSetField(tif,dp->tdir_tag,data[0],data[1]);
  4718. _TIFFfree(data);
  4719. if (!m)
  4720. return(0);
  4721. }
  4722. }
  4723. break;
  4724. case TIFF_SETGET_C0_UINT8:
  4725. {
  4726. uint8* data;
  4727. assert(fip->field_readcount>=1);
  4728. assert(fip->field_passcount==0);
  4729. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4730. /* corrupt file */;
  4731. else
  4732. {
  4733. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4734. if (err==TIFFReadDirEntryErrOk)
  4735. {
  4736. int m;
  4737. m=TIFFSetField(tif,dp->tdir_tag,data);
  4738. if (data!=0)
  4739. _TIFFfree(data);
  4740. if (!m)
  4741. return(0);
  4742. }
  4743. }
  4744. }
  4745. break;
  4746. case TIFF_SETGET_C0_UINT16:
  4747. {
  4748. uint16* data;
  4749. assert(fip->field_readcount>=1);
  4750. assert(fip->field_passcount==0);
  4751. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4752. /* corrupt file */;
  4753. else
  4754. {
  4755. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4756. if (err==TIFFReadDirEntryErrOk)
  4757. {
  4758. int m;
  4759. m=TIFFSetField(tif,dp->tdir_tag,data);
  4760. if (data!=0)
  4761. _TIFFfree(data);
  4762. if (!m)
  4763. return(0);
  4764. }
  4765. }
  4766. }
  4767. break;
  4768. case TIFF_SETGET_C0_UINT32:
  4769. {
  4770. uint32* data;
  4771. assert(fip->field_readcount>=1);
  4772. assert(fip->field_passcount==0);
  4773. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4774. /* corrupt file */;
  4775. else
  4776. {
  4777. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  4778. if (err==TIFFReadDirEntryErrOk)
  4779. {
  4780. int m;
  4781. m=TIFFSetField(tif,dp->tdir_tag,data);
  4782. if (data!=0)
  4783. _TIFFfree(data);
  4784. if (!m)
  4785. return(0);
  4786. }
  4787. }
  4788. }
  4789. break;
  4790. case TIFF_SETGET_C0_FLOAT:
  4791. {
  4792. float* data;
  4793. assert(fip->field_readcount>=1);
  4794. assert(fip->field_passcount==0);
  4795. if (dp->tdir_count!=(uint64)fip->field_readcount)
  4796. /* corrupt file */;
  4797. else
  4798. {
  4799. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  4800. if (err==TIFFReadDirEntryErrOk)
  4801. {
  4802. int m;
  4803. m=TIFFSetField(tif,dp->tdir_tag,data);
  4804. if (data!=0)
  4805. _TIFFfree(data);
  4806. if (!m)
  4807. return(0);
  4808. }
  4809. }
  4810. }
  4811. break;
  4812. case TIFF_SETGET_C16_ASCII:
  4813. {
  4814. uint8* data;
  4815. assert(fip->field_readcount==TIFF_VARIABLE);
  4816. assert(fip->field_passcount==1);
  4817. if (dp->tdir_count>0xFFFF)
  4818. err=TIFFReadDirEntryErrCount;
  4819. else
  4820. {
  4821. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4822. if (err==TIFFReadDirEntryErrOk)
  4823. {
  4824. int m;
  4825. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4826. if (data!=0)
  4827. _TIFFfree(data);
  4828. if (!m)
  4829. return(0);
  4830. }
  4831. }
  4832. }
  4833. break;
  4834. case TIFF_SETGET_C16_UINT8:
  4835. {
  4836. uint8* data;
  4837. assert(fip->field_readcount==TIFF_VARIABLE);
  4838. assert(fip->field_passcount==1);
  4839. if (dp->tdir_count>0xFFFF)
  4840. err=TIFFReadDirEntryErrCount;
  4841. else
  4842. {
  4843. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4844. if (err==TIFFReadDirEntryErrOk)
  4845. {
  4846. int m;
  4847. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4848. if (data!=0)
  4849. _TIFFfree(data);
  4850. if (!m)
  4851. return(0);
  4852. }
  4853. }
  4854. }
  4855. break;
  4856. case TIFF_SETGET_C16_UINT16:
  4857. {
  4858. uint16* data;
  4859. assert(fip->field_readcount==TIFF_VARIABLE);
  4860. assert(fip->field_passcount==1);
  4861. if (dp->tdir_count>0xFFFF)
  4862. err=TIFFReadDirEntryErrCount;
  4863. else
  4864. {
  4865. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  4866. if (err==TIFFReadDirEntryErrOk)
  4867. {
  4868. int m;
  4869. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4870. if (data!=0)
  4871. _TIFFfree(data);
  4872. if (!m)
  4873. return(0);
  4874. }
  4875. }
  4876. }
  4877. break;
  4878. case TIFF_SETGET_C16_UINT32:
  4879. {
  4880. uint32* data;
  4881. assert(fip->field_readcount==TIFF_VARIABLE);
  4882. assert(fip->field_passcount==1);
  4883. if (dp->tdir_count>0xFFFF)
  4884. err=TIFFReadDirEntryErrCount;
  4885. else
  4886. {
  4887. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  4888. if (err==TIFFReadDirEntryErrOk)
  4889. {
  4890. int m;
  4891. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4892. if (data!=0)
  4893. _TIFFfree(data);
  4894. if (!m)
  4895. return(0);
  4896. }
  4897. }
  4898. }
  4899. break;
  4900. case TIFF_SETGET_C16_UINT64:
  4901. {
  4902. uint64* data;
  4903. assert(fip->field_readcount==TIFF_VARIABLE);
  4904. assert(fip->field_passcount==1);
  4905. if (dp->tdir_count>0xFFFF)
  4906. err=TIFFReadDirEntryErrCount;
  4907. else
  4908. {
  4909. err=TIFFReadDirEntryLong8Array(tif,dp,&data);
  4910. if (err==TIFFReadDirEntryErrOk)
  4911. {
  4912. int m;
  4913. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4914. if (data!=0)
  4915. _TIFFfree(data);
  4916. if (!m)
  4917. return(0);
  4918. }
  4919. }
  4920. }
  4921. break;
  4922. case TIFF_SETGET_C16_FLOAT:
  4923. {
  4924. float* data;
  4925. assert(fip->field_readcount==TIFF_VARIABLE);
  4926. assert(fip->field_passcount==1);
  4927. if (dp->tdir_count>0xFFFF)
  4928. err=TIFFReadDirEntryErrCount;
  4929. else
  4930. {
  4931. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  4932. if (err==TIFFReadDirEntryErrOk)
  4933. {
  4934. int m;
  4935. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4936. if (data!=0)
  4937. _TIFFfree(data);
  4938. if (!m)
  4939. return(0);
  4940. }
  4941. }
  4942. }
  4943. break;
  4944. case TIFF_SETGET_C16_DOUBLE:
  4945. {
  4946. double* data;
  4947. assert(fip->field_readcount==TIFF_VARIABLE);
  4948. assert(fip->field_passcount==1);
  4949. if (dp->tdir_count>0xFFFF)
  4950. err=TIFFReadDirEntryErrCount;
  4951. else
  4952. {
  4953. err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
  4954. if (err==TIFFReadDirEntryErrOk)
  4955. {
  4956. int m;
  4957. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4958. if (data!=0)
  4959. _TIFFfree(data);
  4960. if (!m)
  4961. return(0);
  4962. }
  4963. }
  4964. }
  4965. break;
  4966. case TIFF_SETGET_C16_IFD8:
  4967. {
  4968. uint64* data;
  4969. assert(fip->field_readcount==TIFF_VARIABLE);
  4970. assert(fip->field_passcount==1);
  4971. if (dp->tdir_count>0xFFFF)
  4972. err=TIFFReadDirEntryErrCount;
  4973. else
  4974. {
  4975. err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
  4976. if (err==TIFFReadDirEntryErrOk)
  4977. {
  4978. int m;
  4979. m=TIFFSetField(tif,dp->tdir_tag,(uint16)(dp->tdir_count),data);
  4980. if (data!=0)
  4981. _TIFFfree(data);
  4982. if (!m)
  4983. return(0);
  4984. }
  4985. }
  4986. }
  4987. break;
  4988. case TIFF_SETGET_C32_ASCII:
  4989. {
  4990. uint8* data;
  4991. assert(fip->field_readcount==TIFF_VARIABLE2);
  4992. assert(fip->field_passcount==1);
  4993. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  4994. if (err==TIFFReadDirEntryErrOk)
  4995. {
  4996. int m;
  4997. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  4998. if (data!=0)
  4999. _TIFFfree(data);
  5000. if (!m)
  5001. return(0);
  5002. }
  5003. }
  5004. break;
  5005. case TIFF_SETGET_C32_UINT8:
  5006. {
  5007. uint8* data;
  5008. assert(fip->field_readcount==TIFF_VARIABLE2);
  5009. assert(fip->field_passcount==1);
  5010. err=TIFFReadDirEntryByteArray(tif,dp,&data);
  5011. if (err==TIFFReadDirEntryErrOk)
  5012. {
  5013. int m;
  5014. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5015. if (data!=0)
  5016. _TIFFfree(data);
  5017. if (!m)
  5018. return(0);
  5019. }
  5020. }
  5021. break;
  5022. case TIFF_SETGET_C32_SINT8:
  5023. {
  5024. int8* data = NULL;
  5025. assert(fip->field_readcount==TIFF_VARIABLE2);
  5026. assert(fip->field_passcount==1);
  5027. err=TIFFReadDirEntrySbyteArray(tif,dp,&data);
  5028. if (err==TIFFReadDirEntryErrOk)
  5029. {
  5030. int m;
  5031. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5032. if (data!=0)
  5033. _TIFFfree(data);
  5034. if (!m)
  5035. return(0);
  5036. }
  5037. }
  5038. break;
  5039. case TIFF_SETGET_C32_UINT16:
  5040. {
  5041. uint16* data;
  5042. assert(fip->field_readcount==TIFF_VARIABLE2);
  5043. assert(fip->field_passcount==1);
  5044. err=TIFFReadDirEntryShortArray(tif,dp,&data);
  5045. if (err==TIFFReadDirEntryErrOk)
  5046. {
  5047. int m;
  5048. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5049. if (data!=0)
  5050. _TIFFfree(data);
  5051. if (!m)
  5052. return(0);
  5053. }
  5054. }
  5055. break;
  5056. case TIFF_SETGET_C32_SINT16:
  5057. {
  5058. int16* data = NULL;
  5059. assert(fip->field_readcount==TIFF_VARIABLE2);
  5060. assert(fip->field_passcount==1);
  5061. err=TIFFReadDirEntrySshortArray(tif,dp,&data);
  5062. if (err==TIFFReadDirEntryErrOk)
  5063. {
  5064. int m;
  5065. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5066. if (data!=0)
  5067. _TIFFfree(data);
  5068. if (!m)
  5069. return(0);
  5070. }
  5071. }
  5072. break;
  5073. case TIFF_SETGET_C32_UINT32:
  5074. {
  5075. uint32* data;
  5076. assert(fip->field_readcount==TIFF_VARIABLE2);
  5077. assert(fip->field_passcount==1);
  5078. err=TIFFReadDirEntryLongArray(tif,dp,&data);
  5079. if (err==TIFFReadDirEntryErrOk)
  5080. {
  5081. int m;
  5082. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5083. if (data!=0)
  5084. _TIFFfree(data);
  5085. if (!m)
  5086. return(0);
  5087. }
  5088. }
  5089. break;
  5090. case TIFF_SETGET_C32_SINT32:
  5091. {
  5092. int32* data = NULL;
  5093. assert(fip->field_readcount==TIFF_VARIABLE2);
  5094. assert(fip->field_passcount==1);
  5095. err=TIFFReadDirEntrySlongArray(tif,dp,&data);
  5096. if (err==TIFFReadDirEntryErrOk)
  5097. {
  5098. int m;
  5099. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5100. if (data!=0)
  5101. _TIFFfree(data);
  5102. if (!m)
  5103. return(0);
  5104. }
  5105. }
  5106. break;
  5107. case TIFF_SETGET_C32_UINT64:
  5108. {
  5109. uint64* data;
  5110. assert(fip->field_readcount==TIFF_VARIABLE2);
  5111. assert(fip->field_passcount==1);
  5112. err=TIFFReadDirEntryLong8Array(tif,dp,&data);
  5113. if (err==TIFFReadDirEntryErrOk)
  5114. {
  5115. int m;
  5116. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5117. if (data!=0)
  5118. _TIFFfree(data);
  5119. if (!m)
  5120. return(0);
  5121. }
  5122. }
  5123. break;
  5124. case TIFF_SETGET_C32_SINT64:
  5125. {
  5126. int64* data = NULL;
  5127. assert(fip->field_readcount==TIFF_VARIABLE2);
  5128. assert(fip->field_passcount==1);
  5129. err=TIFFReadDirEntrySlong8Array(tif,dp,&data);
  5130. if (err==TIFFReadDirEntryErrOk)
  5131. {
  5132. int m;
  5133. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5134. if (data!=0)
  5135. _TIFFfree(data);
  5136. if (!m)
  5137. return(0);
  5138. }
  5139. }
  5140. break;
  5141. case TIFF_SETGET_C32_FLOAT:
  5142. {
  5143. float* data;
  5144. assert(fip->field_readcount==TIFF_VARIABLE2);
  5145. assert(fip->field_passcount==1);
  5146. err=TIFFReadDirEntryFloatArray(tif,dp,&data);
  5147. if (err==TIFFReadDirEntryErrOk)
  5148. {
  5149. int m;
  5150. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5151. if (data!=0)
  5152. _TIFFfree(data);
  5153. if (!m)
  5154. return(0);
  5155. }
  5156. }
  5157. break;
  5158. case TIFF_SETGET_C32_DOUBLE:
  5159. {
  5160. double* data;
  5161. assert(fip->field_readcount==TIFF_VARIABLE2);
  5162. assert(fip->field_passcount==1);
  5163. err=TIFFReadDirEntryDoubleArray(tif,dp,&data);
  5164. if (err==TIFFReadDirEntryErrOk)
  5165. {
  5166. int m;
  5167. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5168. if (data!=0)
  5169. _TIFFfree(data);
  5170. if (!m)
  5171. return(0);
  5172. }
  5173. }
  5174. break;
  5175. case TIFF_SETGET_C32_IFD8:
  5176. {
  5177. uint64* data;
  5178. assert(fip->field_readcount==TIFF_VARIABLE2);
  5179. assert(fip->field_passcount==1);
  5180. err=TIFFReadDirEntryIfd8Array(tif,dp,&data);
  5181. if (err==TIFFReadDirEntryErrOk)
  5182. {
  5183. int m;
  5184. m=TIFFSetField(tif,dp->tdir_tag,(uint32)(dp->tdir_count),data);
  5185. if (data!=0)
  5186. _TIFFfree(data);
  5187. if (!m)
  5188. return(0);
  5189. }
  5190. }
  5191. break;
  5192. default:
  5193. assert(0); /* we should never get here */
  5194. break;
  5195. }
  5196. if (err!=TIFFReadDirEntryErrOk)
  5197. {
  5198. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",recover);
  5199. return(0);
  5200. }
  5201. return(1);
  5202. }
  5203. /*
  5204. * Fetch a set of offsets or lengths.
  5205. * While this routine says "strips", in fact it's also used for tiles.
  5206. */
  5207. static int
  5208. TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp)
  5209. {
  5210. static const char module[] = "TIFFFetchStripThing";
  5211. enum TIFFReadDirEntryErr err;
  5212. uint64* data;
  5213. err=TIFFReadDirEntryLong8Array(tif,dir,&data);
  5214. if (err!=TIFFReadDirEntryErrOk)
  5215. {
  5216. const TIFFField* fip = TIFFFieldWithTag(tif,dir->tdir_tag);
  5217. TIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);
  5218. return(0);
  5219. }
  5220. if (dir->tdir_count!=(uint64)nstrips)
  5221. {
  5222. uint64* resizeddata;
  5223. resizeddata=(uint64*)_TIFFCheckMalloc(tif,nstrips,sizeof(uint64),"for strip array");
  5224. if (resizeddata==0) {
  5225. _TIFFfree(data);
  5226. return(0);
  5227. }
  5228. if (dir->tdir_count<(uint64)nstrips)
  5229. {
  5230. _TIFFmemcpy(resizeddata,data,(uint32)dir->tdir_count*sizeof(uint64));
  5231. _TIFFmemset(resizeddata+(uint32)dir->tdir_count,0,(nstrips-(uint32)dir->tdir_count)*sizeof(uint64));
  5232. }
  5233. else
  5234. _TIFFmemcpy(resizeddata,data,nstrips*sizeof(uint64));
  5235. _TIFFfree(data);
  5236. data=resizeddata;
  5237. }
  5238. *lpp=data;
  5239. return(1);
  5240. }
  5241. /*
  5242. * Fetch and set the SubjectDistance EXIF tag.
  5243. */
  5244. static int
  5245. TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
  5246. {
  5247. static const char module[] = "TIFFFetchSubjectDistance";
  5248. enum TIFFReadDirEntryErr err;
  5249. UInt64Aligned_t m;
  5250. m.l=0;
  5251. assert(sizeof(double)==8);
  5252. assert(sizeof(uint64)==8);
  5253. assert(sizeof(uint32)==4);
  5254. if (dir->tdir_count!=1)
  5255. err=TIFFReadDirEntryErrCount;
  5256. else if (dir->tdir_type!=TIFF_RATIONAL)
  5257. err=TIFFReadDirEntryErrType;
  5258. else
  5259. {
  5260. if (!(tif->tif_flags&TIFF_BIGTIFF))
  5261. {
  5262. uint32 offset;
  5263. offset=*(uint32*)(&dir->tdir_offset);
  5264. if (tif->tif_flags&TIFF_SWAB)
  5265. TIFFSwabLong(&offset);
  5266. err=TIFFReadDirEntryData(tif,offset,8,m.i);
  5267. }
  5268. else
  5269. {
  5270. m.l=dir->tdir_offset.toff_long8;
  5271. err=TIFFReadDirEntryErrOk;
  5272. }
  5273. }
  5274. if (err==TIFFReadDirEntryErrOk)
  5275. {
  5276. double n;
  5277. if (tif->tif_flags&TIFF_SWAB)
  5278. TIFFSwabArrayOfLong(m.i,2);
  5279. if (m.i[0]==0)
  5280. n=0.0;
  5281. else if (m.i[0]==0xFFFFFFFF)
  5282. /*
  5283. * XXX: Numerator 0xFFFFFFFF means that we have infinite
  5284. * distance. Indicate that with a negative floating point
  5285. * SubjectDistance value.
  5286. */
  5287. n=-1.0;
  5288. else
  5289. n=(double)m.i[0]/(double)m.i[1];
  5290. return(TIFFSetField(tif,dir->tdir_tag,n));
  5291. }
  5292. else
  5293. {
  5294. TIFFReadDirEntryOutputErr(tif,err,module,"SubjectDistance",TRUE);
  5295. return(0);
  5296. }
  5297. }
  5298. /*
  5299. * Replace a single strip (tile) of uncompressed data by multiple strips
  5300. * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
  5301. * dealing with large images or for dealing with machines with a limited
  5302. * amount memory.
  5303. */
  5304. static void
  5305. ChopUpSingleUncompressedStrip(TIFF* tif)
  5306. {
  5307. register TIFFDirectory *td = &tif->tif_dir;
  5308. uint64 bytecount;
  5309. uint64 offset;
  5310. uint32 rowblock;
  5311. uint64 rowblockbytes;
  5312. uint64 stripbytes;
  5313. uint32 strip;
  5314. uint64 nstrips64;
  5315. uint32 nstrips32;
  5316. uint32 rowsperstrip;
  5317. uint64* newcounts;
  5318. uint64* newoffsets;
  5319. bytecount = td->td_stripbytecount[0];
  5320. offset = td->td_stripoffset[0];
  5321. assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
  5322. if ((td->td_photometric == PHOTOMETRIC_YCBCR)&&
  5323. (!isUpSampled(tif)))
  5324. rowblock = td->td_ycbcrsubsampling[1];
  5325. else
  5326. rowblock = 1;
  5327. rowblockbytes = TIFFVTileSize64(tif, rowblock);
  5328. /*
  5329. * Make the rows hold at least one scanline, but fill specified amount
  5330. * of data if possible.
  5331. */
  5332. if (rowblockbytes > STRIP_SIZE_DEFAULT) {
  5333. stripbytes = rowblockbytes;
  5334. rowsperstrip = rowblock;
  5335. } else if (rowblockbytes > 0 ) {
  5336. uint32 rowblocksperstrip;
  5337. rowblocksperstrip = (uint32) (STRIP_SIZE_DEFAULT / rowblockbytes);
  5338. rowsperstrip = rowblocksperstrip * rowblock;
  5339. stripbytes = rowblocksperstrip * rowblockbytes;
  5340. }
  5341. else
  5342. return;
  5343. /*
  5344. * never increase the number of strips in an image
  5345. */
  5346. if (rowsperstrip >= td->td_rowsperstrip)
  5347. return;
  5348. nstrips64 = TIFFhowmany_64(bytecount, stripbytes);
  5349. if ((nstrips64==0)||(nstrips64>0xFFFFFFFF)) /* something is wonky, do nothing. */
  5350. return;
  5351. nstrips32 = (uint32)nstrips64;
  5352. newcounts = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
  5353. "for chopped \"StripByteCounts\" array");
  5354. newoffsets = (uint64*) _TIFFCheckMalloc(tif, nstrips32, sizeof (uint64),
  5355. "for chopped \"StripOffsets\" array");
  5356. if (newcounts == NULL || newoffsets == NULL) {
  5357. /*
  5358. * Unable to allocate new strip information, give up and use
  5359. * the original one strip information.
  5360. */
  5361. if (newcounts != NULL)
  5362. _TIFFfree(newcounts);
  5363. if (newoffsets != NULL)
  5364. _TIFFfree(newoffsets);
  5365. return;
  5366. }
  5367. /*
  5368. * Fill the strip information arrays with new bytecounts and offsets
  5369. * that reflect the broken-up format.
  5370. */
  5371. for (strip = 0; strip < nstrips32; strip++) {
  5372. if (stripbytes > bytecount)
  5373. stripbytes = bytecount;
  5374. newcounts[strip] = stripbytes;
  5375. newoffsets[strip] = offset;
  5376. offset += stripbytes;
  5377. bytecount -= stripbytes;
  5378. }
  5379. /*
  5380. * Replace old single strip info with multi-strip info.
  5381. */
  5382. td->td_stripsperimage = td->td_nstrips = nstrips32;
  5383. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  5384. _TIFFfree(td->td_stripbytecount);
  5385. _TIFFfree(td->td_stripoffset);
  5386. td->td_stripbytecount = newcounts;
  5387. td->td_stripoffset = newoffsets;
  5388. td->td_stripbytecountsorted = 1;
  5389. }
  5390. int _TIFFFillStriles( TIFF *tif )
  5391. {
  5392. #if defined(DEFER_STRILE_LOAD)
  5393. register TIFFDirectory *td = &tif->tif_dir;
  5394. int return_value = 1;
  5395. if( td->td_stripoffset != NULL )
  5396. return 1;
  5397. if( td->td_stripoffset_entry.tdir_count == 0 )
  5398. return 0;
  5399. if (!TIFFFetchStripThing(tif,&(td->td_stripoffset_entry),
  5400. td->td_nstrips,&td->td_stripoffset))
  5401. {
  5402. return_value = 0;
  5403. }
  5404. if (!TIFFFetchStripThing(tif,&(td->td_stripbytecount_entry),
  5405. td->td_nstrips,&td->td_stripbytecount))
  5406. {
  5407. return_value = 0;
  5408. }
  5409. _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
  5410. _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
  5411. if (tif->tif_dir.td_nstrips > 1 && return_value == 1 ) {
  5412. uint32 strip;
  5413. tif->tif_dir.td_stripbytecountsorted = 1;
  5414. for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {
  5415. if (tif->tif_dir.td_stripoffset[strip - 1] >
  5416. tif->tif_dir.td_stripoffset[strip]) {
  5417. tif->tif_dir.td_stripbytecountsorted = 0;
  5418. break;
  5419. }
  5420. }
  5421. }
  5422. return return_value;
  5423. #else /* !defined(DEFER_STRILE_LOAD) */
  5424. (void) tif;
  5425. return 1;
  5426. #endif
  5427. }
  5428. /* vim: set ts=8 sts=8 sw=8 noet: */
  5429. /*
  5430. * Local Variables:
  5431. * mode: c
  5432. * c-basic-offset: 8
  5433. * fill-column: 78
  5434. * End:
  5435. */