tif_dirwrite.c 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * TIFF Library.
  26. *
  27. * Directory Write Support Routines.
  28. */
  29. #include "tiffiop.h"
  30. #include <float.h> /*--: for Rational2Double */
  31. #include <math.h> /*--: for Rational2Double */
  32. #ifdef HAVE_IEEEFP
  33. #define TIFFCvtNativeToIEEEFloat(tif, n, fp)
  34. #define TIFFCvtNativeToIEEEDouble(tif, n, dp)
  35. #else
  36. /* If your machine does not support IEEE floating point then you will need to
  37. * add support to tif_machdep.c to convert between the native format and
  38. * IEEE format. */
  39. extern void TIFFCvtNativeToIEEEFloat(TIFF *tif, uint32_t n, float *fp);
  40. extern void TIFFCvtNativeToIEEEDouble(TIFF *tif, uint32_t n, double *dp);
  41. #endif
  42. static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone,
  43. uint64_t *pdiroff);
  44. static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir,
  45. TIFFDirEntry *dir,
  46. uint16_t tag, uint32_t count,
  47. double *value);
  48. static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir,
  49. TIFFDirEntry *dir, uint16_t tag,
  50. uint32_t count, char *value);
  51. static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir,
  52. TIFFDirEntry *dir, uint16_t tag,
  53. uint32_t count, uint8_t *value);
  54. static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir,
  55. TIFFDirEntry *dir, uint16_t tag,
  56. uint32_t count, uint8_t *value);
  57. static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir,
  58. TIFFDirEntry *dir, uint16_t tag,
  59. uint32_t count, int8_t *value);
  60. static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir,
  61. TIFFDirEntry *dir, uint16_t tag,
  62. uint16_t value);
  63. static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir,
  64. TIFFDirEntry *dir, uint16_t tag,
  65. uint32_t count, uint16_t *value);
  66. static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir,
  67. TIFFDirEntry *dir, uint16_t tag,
  68. uint16_t value);
  69. static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir,
  70. TIFFDirEntry *dir, uint16_t tag,
  71. uint32_t count, int16_t *value);
  72. static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir,
  73. TIFFDirEntry *dir, uint16_t tag,
  74. uint32_t value);
  75. static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir,
  76. TIFFDirEntry *dir, uint16_t tag,
  77. uint32_t count, uint32_t *value);
  78. static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir,
  79. TIFFDirEntry *dir, uint16_t tag,
  80. uint32_t count, int32_t *value);
  81. static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir,
  82. TIFFDirEntry *dir, uint16_t tag,
  83. uint32_t count, uint64_t *value);
  84. static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir,
  85. TIFFDirEntry *dir, uint16_t tag,
  86. uint32_t count, int64_t *value);
  87. static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir,
  88. TIFFDirEntry *dir, uint16_t tag,
  89. double value);
  90. static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir,
  91. TIFFDirEntry *dir, uint16_t tag,
  92. uint32_t count, float *value);
  93. static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir,
  94. TIFFDirEntry *dir, uint16_t tag,
  95. uint32_t count, float *value);
  96. static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir,
  97. TIFFDirEntry *dir, uint16_t tag,
  98. uint32_t count, float *value);
  99. static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir,
  100. TIFFDirEntry *dir, uint16_t tag,
  101. uint32_t count, double *value);
  102. static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir,
  103. TIFFDirEntry *dir, uint16_t tag,
  104. uint32_t count, uint32_t *value);
  105. static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir,
  106. TIFFDirEntry *dir, uint16_t tag,
  107. uint32_t value);
  108. static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir,
  109. TIFFDirEntry *dir, uint16_t tag,
  110. uint32_t count, uint64_t *value);
  111. static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir,
  112. TIFFDirEntry *dir, uint16_t tag,
  113. uint32_t count, uint64_t *value);
  114. static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir,
  115. TIFFDirEntry *dir);
  116. static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir,
  117. TIFFDirEntry *dir);
  118. static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir,
  119. TIFFDirEntry *dir);
  120. static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir,
  121. TIFFDirEntry *dir, uint16_t tag,
  122. uint32_t count, char *value);
  123. static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir,
  124. TIFFDirEntry *dir,
  125. uint16_t tag,
  126. uint32_t count,
  127. uint8_t *value);
  128. static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir,
  129. TIFFDirEntry *dir,
  130. uint16_t tag, uint32_t count,
  131. uint8_t *value);
  132. static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir,
  133. TIFFDirEntry *dir,
  134. uint16_t tag, uint32_t count,
  135. int8_t *value);
  136. static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir,
  137. TIFFDirEntry *dir, uint16_t tag,
  138. uint16_t value);
  139. static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir,
  140. TIFFDirEntry *dir,
  141. uint16_t tag, uint32_t count,
  142. uint16_t *value);
  143. static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir,
  144. TIFFDirEntry *dir,
  145. uint16_t tag, uint32_t count,
  146. int16_t *value);
  147. static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir,
  148. TIFFDirEntry *dir, uint16_t tag,
  149. uint32_t value);
  150. static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir,
  151. TIFFDirEntry *dir,
  152. uint16_t tag, uint32_t count,
  153. uint32_t *value);
  154. static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir,
  155. TIFFDirEntry *dir,
  156. uint16_t tag, uint32_t count,
  157. int32_t *value);
  158. static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir,
  159. TIFFDirEntry *dir,
  160. uint16_t tag, uint32_t count,
  161. uint64_t *value);
  162. static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir,
  163. TIFFDirEntry *dir,
  164. uint16_t tag, uint32_t count,
  165. int64_t *value);
  166. static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir,
  167. TIFFDirEntry *dir, uint16_t tag,
  168. double value);
  169. static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir,
  170. TIFFDirEntry *dir,
  171. uint16_t tag,
  172. uint32_t count,
  173. float *value);
  174. static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir,
  175. TIFFDirEntry *dir,
  176. uint16_t tag,
  177. uint32_t count,
  178. float *value);
  179. /*--: Rational2Double: New functions to support true double-precision for custom
  180. * rational tag types. */
  181. static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir,
  182. TIFFDirEntry *dir,
  183. uint16_t tag,
  184. uint32_t count,
  185. double *value);
  186. static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir,
  187. TIFFDirEntry *dir,
  188. uint16_t tag,
  189. uint32_t count,
  190. double *value);
  191. static int
  192. TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF *tif, uint32_t *ndir,
  193. TIFFDirEntry *dir, uint16_t tag,
  194. uint32_t count, double *value);
  195. static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray(
  196. TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count,
  197. double *value);
  198. static void DoubleToRational(double value, uint32_t *num, uint32_t *denom);
  199. static void DoubleToSrational(double value, int32_t *num, int32_t *denom);
  200. static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir,
  201. TIFFDirEntry *dir,
  202. uint16_t tag, uint32_t count,
  203. float *value);
  204. static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir,
  205. TIFFDirEntry *dir,
  206. uint16_t tag, uint32_t count,
  207. double *value);
  208. static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir,
  209. TIFFDirEntry *dir, uint16_t tag,
  210. uint32_t count,
  211. uint32_t *value);
  212. static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir,
  213. TIFFDirEntry *dir,
  214. uint16_t tag, uint32_t count,
  215. uint64_t *value);
  216. static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir,
  217. TIFFDirEntry *dir, uint16_t tag,
  218. uint16_t datatype, uint32_t count,
  219. uint32_t datalength, void *data);
  220. static int TIFFLinkDirectory(TIFF *);
  221. /*
  222. * Write the contents of the current directory
  223. * to the specified file. This routine doesn't
  224. * handle overwriting a directory with auxiliary
  225. * storage that's been changed.
  226. */
  227. int TIFFWriteDirectory(TIFF *tif)
  228. {
  229. return TIFFWriteDirectorySec(tif, TRUE, TRUE, NULL);
  230. }
  231. /*
  232. * This is an advanced writing function that must be used in a particular
  233. * sequence, and generally together with TIFFForceStrileArrayWriting(),
  234. * to make its intended effect. Its aim is to modify the location
  235. * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
  236. * More precisely, when TIFFWriteCheck() will be called, the tag entries for
  237. * those arrays will be written with type = count = offset = 0 as a temporary
  238. * value.
  239. *
  240. * Its effect is only valid for the current directory, and before
  241. * TIFFWriteDirectory() is first called, and will be reset when
  242. * changing directory.
  243. *
  244. * The typical sequence of calls is:
  245. * TIFFOpen()
  246. * [ TIFFCreateDirectory(tif) ]
  247. * Set fields with calls to TIFFSetField(tif, ...)
  248. * TIFFDeferStrileArrayWriting(tif)
  249. * TIFFWriteCheck(tif, ...)
  250. * TIFFWriteDirectory(tif)
  251. * ... potentially create other directories and come back to the above directory
  252. * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
  253. *
  254. * Returns 1 in case of success, 0 otherwise.
  255. */
  256. int TIFFDeferStrileArrayWriting(TIFF *tif)
  257. {
  258. static const char module[] = "TIFFDeferStrileArrayWriting";
  259. if (tif->tif_mode == O_RDONLY)
  260. {
  261. TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");
  262. return 0;
  263. }
  264. if (tif->tif_diroff != 0)
  265. {
  266. TIFFErrorExtR(tif, module, "Directory has already been written");
  267. return 0;
  268. }
  269. tif->tif_dir.td_deferstrilearraywriting = TRUE;
  270. return 1;
  271. }
  272. /*
  273. * Similar to TIFFWriteDirectory(), writes the directory out
  274. * but leaves all data structures in memory so that it can be
  275. * written again. This will make a partially written TIFF file
  276. * readable before it is successfully completed/closed.
  277. */
  278. int TIFFCheckpointDirectory(TIFF *tif)
  279. {
  280. int rc;
  281. /* Setup the strips arrays, if they haven't already been. */
  282. if (tif->tif_dir.td_stripoffset_p == NULL)
  283. (void)TIFFSetupStrips(tif);
  284. rc = TIFFWriteDirectorySec(tif, TRUE, FALSE, NULL);
  285. (void)TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
  286. return rc;
  287. }
  288. int TIFFWriteCustomDirectory(TIFF *tif, uint64_t *pdiroff)
  289. {
  290. return TIFFWriteDirectorySec(tif, FALSE, FALSE, pdiroff);
  291. }
  292. /*
  293. * Similar to TIFFWriteDirectorySec(), but if the directory has already
  294. * been written once, it is relocated to the end of the file, in case it
  295. * has changed in size. Note that this will result in the loss of the
  296. * previously used directory space.
  297. */
  298. static int TIFFRewriteDirectorySec(TIFF *tif, int isimage, int imagedone,
  299. uint64_t *pdiroff)
  300. {
  301. static const char module[] = "TIFFRewriteDirectory";
  302. /* We don't need to do anything special if it hasn't been written. */
  303. if (tif->tif_diroff == 0)
  304. return TIFFWriteDirectory(tif);
  305. /*
  306. * Find and zero the pointer to this directory, so that TIFFLinkDirectory
  307. * will cause it to be added after this directories current pre-link.
  308. */
  309. uint64_t torewritediroff = tif->tif_diroff;
  310. if (!(tif->tif_flags & TIFF_BIGTIFF))
  311. {
  312. if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
  313. {
  314. tif->tif_header.classic.tiff_diroff = 0;
  315. tif->tif_diroff = 0;
  316. TIFFSeekFile(tif, 4, SEEK_SET);
  317. if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff), 4))
  318. {
  319. TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header");
  320. return (0);
  321. }
  322. }
  323. else if (tif->tif_diroff > 0xFFFFFFFFU)
  324. {
  325. TIFFErrorExtR(tif, module,
  326. "tif->tif_diroff exceeds 32 bit range allowed for "
  327. "Classic TIFF");
  328. return (0);
  329. }
  330. else
  331. {
  332. uint32_t nextdir;
  333. nextdir = tif->tif_header.classic.tiff_diroff;
  334. while (1)
  335. {
  336. uint16_t dircount;
  337. uint32_t nextnextdir;
  338. if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2))
  339. {
  340. TIFFErrorExtR(tif, module,
  341. "Error fetching directory count");
  342. return (0);
  343. }
  344. if (tif->tif_flags & TIFF_SWAB)
  345. TIFFSwabShort(&dircount);
  346. (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET);
  347. if (!ReadOK(tif, &nextnextdir, 4))
  348. {
  349. TIFFErrorExtR(tif, module, "Error fetching directory link");
  350. return (0);
  351. }
  352. if (tif->tif_flags & TIFF_SWAB)
  353. TIFFSwabLong(&nextnextdir);
  354. if (nextnextdir == tif->tif_diroff)
  355. {
  356. uint32_t m;
  357. m = 0;
  358. (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12,
  359. SEEK_SET);
  360. if (!WriteOK(tif, &m, 4))
  361. {
  362. TIFFErrorExtR(tif, module,
  363. "Error writing directory link");
  364. return (0);
  365. }
  366. tif->tif_diroff = 0;
  367. /* Force a full-traversal to reach the zeroed pointer */
  368. tif->tif_lastdiroff = 0;
  369. break;
  370. }
  371. nextdir = nextnextdir;
  372. }
  373. }
  374. /* Remove skipped offset from IFD loop directory list. */
  375. _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff);
  376. }
  377. else
  378. {
  379. if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
  380. {
  381. tif->tif_header.big.tiff_diroff = 0;
  382. tif->tif_diroff = 0;
  383. TIFFSeekFile(tif, 8, SEEK_SET);
  384. if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff), 8))
  385. {
  386. TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header");
  387. return (0);
  388. }
  389. }
  390. else
  391. {
  392. uint64_t nextdir;
  393. nextdir = tif->tif_header.big.tiff_diroff;
  394. while (1)
  395. {
  396. uint64_t dircount64;
  397. uint16_t dircount;
  398. uint64_t nextnextdir;
  399. if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8))
  400. {
  401. TIFFErrorExtR(tif, module,
  402. "Error fetching directory count");
  403. return (0);
  404. }
  405. if (tif->tif_flags & TIFF_SWAB)
  406. TIFFSwabLong8(&dircount64);
  407. if (dircount64 > 0xFFFF)
  408. {
  409. TIFFErrorExtR(tif, module,
  410. "Sanity check on tag count failed, likely "
  411. "corrupt TIFF");
  412. return (0);
  413. }
  414. dircount = (uint16_t)dircount64;
  415. (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET);
  416. if (!ReadOK(tif, &nextnextdir, 8))
  417. {
  418. TIFFErrorExtR(tif, module, "Error fetching directory link");
  419. return (0);
  420. }
  421. if (tif->tif_flags & TIFF_SWAB)
  422. TIFFSwabLong8(&nextnextdir);
  423. if (nextnextdir == tif->tif_diroff)
  424. {
  425. uint64_t m;
  426. m = 0;
  427. (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20,
  428. SEEK_SET);
  429. if (!WriteOK(tif, &m, 8))
  430. {
  431. TIFFErrorExtR(tif, module,
  432. "Error writing directory link");
  433. return (0);
  434. }
  435. tif->tif_diroff = 0;
  436. /* Force a full-traversal to reach the zeroed pointer */
  437. tif->tif_lastdiroff = 0;
  438. break;
  439. }
  440. nextdir = nextnextdir;
  441. }
  442. }
  443. /* Remove skipped offset from IFD loop directory list. */
  444. _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff);
  445. }
  446. /*
  447. * Now use TIFFWriteDirectorySec() normally.
  448. */
  449. return TIFFWriteDirectorySec(tif, isimage, imagedone, pdiroff);
  450. } /*-- TIFFRewriteDirectorySec() --*/
  451. /*
  452. * Similar to TIFFWriteDirectory(), but if the directory has already
  453. * been written once, it is relocated to the end of the file, in case it
  454. * has changed in size. Note that this will result in the loss of the
  455. * previously used directory space.
  456. */
  457. int TIFFRewriteDirectory(TIFF *tif)
  458. {
  459. return TIFFRewriteDirectorySec(tif, TRUE, TRUE, NULL);
  460. }
  461. static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone,
  462. uint64_t *pdiroff)
  463. {
  464. static const char module[] = "TIFFWriteDirectorySec";
  465. uint32_t ndir;
  466. TIFFDirEntry *dir;
  467. uint32_t dirsize;
  468. void *dirmem;
  469. uint32_t m;
  470. if (tif->tif_mode == O_RDONLY)
  471. return (1);
  472. _TIFFFillStriles(tif);
  473. /*
  474. * Clear write state so that subsequent images with
  475. * different characteristics get the right buffers
  476. * setup for them.
  477. */
  478. if (imagedone)
  479. {
  480. if (tif->tif_flags & TIFF_POSTENCODE)
  481. {
  482. tif->tif_flags &= ~TIFF_POSTENCODE;
  483. if (!(*tif->tif_postencode)(tif))
  484. {
  485. TIFFErrorExtR(tif, module,
  486. "Error post-encoding before directory write");
  487. return (0);
  488. }
  489. }
  490. (*tif->tif_close)(tif); /* shutdown encoder */
  491. /*
  492. * Flush any data that might have been written
  493. * by the compression close+cleanup routines. But
  494. * be careful not to write stuff if we didn't add data
  495. * in the previous steps as the "rawcc" data may well be
  496. * a previously read tile/strip in mixed read/write mode.
  497. */
  498. if (tif->tif_rawcc > 0 && (tif->tif_flags & TIFF_BEENWRITING) != 0)
  499. {
  500. if (!TIFFFlushData1(tif))
  501. {
  502. TIFFErrorExtR(tif, module,
  503. "Error flushing data before directory write");
  504. return (0);
  505. }
  506. }
  507. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  508. {
  509. _TIFFfreeExt(tif, tif->tif_rawdata);
  510. tif->tif_rawdata = NULL;
  511. tif->tif_rawcp = NULL;
  512. tif->tif_rawcc = 0;
  513. tif->tif_rawdatasize = 0;
  514. tif->tif_rawdataoff = 0;
  515. tif->tif_rawdataloaded = 0;
  516. }
  517. tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP);
  518. }
  519. if (TIFFFieldSet(tif, FIELD_COMPRESSION) &&
  520. (tif->tif_dir.td_compression == COMPRESSION_DEFLATE))
  521. {
  522. TIFFWarningExtR(tif, module,
  523. "Creating TIFF with legacy Deflate codec identifier, "
  524. "COMPRESSION_ADOBE_DEFLATE is more widely supported");
  525. }
  526. dir = NULL;
  527. dirmem = NULL;
  528. dirsize = 0;
  529. while (1)
  530. {
  531. /* The first loop only determines "ndir" and uses TIFFLinkDirectory() to
  532. * set the offset at which the IFD is to be written to the file.
  533. * The second loop writes IFD entries to the file. */
  534. ndir = 0;
  535. if (dir == NULL)
  536. tif->tif_dir.td_dirdatasize_write = 0;
  537. if (isimage)
  538. {
  539. /*-- Step 1: Process named tags for an image with FIELD bits
  540. * associated. --*/
  541. if (TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
  542. {
  543. if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
  544. TIFFTAG_IMAGEWIDTH,
  545. tif->tif_dir.td_imagewidth))
  546. goto bad;
  547. if (!TIFFWriteDirectoryTagShortLong(
  548. tif, &ndir, dir, TIFFTAG_IMAGELENGTH,
  549. tif->tif_dir.td_imagelength))
  550. goto bad;
  551. }
  552. if (TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
  553. {
  554. if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
  555. TIFFTAG_TILEWIDTH,
  556. tif->tif_dir.td_tilewidth))
  557. goto bad;
  558. if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
  559. TIFFTAG_TILELENGTH,
  560. tif->tif_dir.td_tilelength))
  561. goto bad;
  562. }
  563. if (TIFFFieldSet(tif, FIELD_RESOLUTION))
  564. {
  565. if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir,
  566. TIFFTAG_XRESOLUTION,
  567. tif->tif_dir.td_xresolution))
  568. goto bad;
  569. if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir,
  570. TIFFTAG_YRESOLUTION,
  571. tif->tif_dir.td_yresolution))
  572. goto bad;
  573. }
  574. if (TIFFFieldSet(tif, FIELD_POSITION))
  575. {
  576. if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir,
  577. TIFFTAG_XPOSITION,
  578. tif->tif_dir.td_xposition))
  579. goto bad;
  580. if (!TIFFWriteDirectoryTagRational(tif, &ndir, dir,
  581. TIFFTAG_YPOSITION,
  582. tif->tif_dir.td_yposition))
  583. goto bad;
  584. }
  585. if (TIFFFieldSet(tif, FIELD_SUBFILETYPE))
  586. {
  587. if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
  588. TIFFTAG_SUBFILETYPE,
  589. tif->tif_dir.td_subfiletype))
  590. goto bad;
  591. }
  592. if (TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
  593. {
  594. if (!TIFFWriteDirectoryTagShortPerSample(
  595. tif, &ndir, dir, TIFFTAG_BITSPERSAMPLE,
  596. tif->tif_dir.td_bitspersample))
  597. goto bad;
  598. }
  599. if (TIFFFieldSet(tif, FIELD_COMPRESSION))
  600. {
  601. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  602. TIFFTAG_COMPRESSION,
  603. tif->tif_dir.td_compression))
  604. goto bad;
  605. }
  606. if (TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
  607. {
  608. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  609. TIFFTAG_PHOTOMETRIC,
  610. tif->tif_dir.td_photometric))
  611. goto bad;
  612. }
  613. if (TIFFFieldSet(tif, FIELD_THRESHHOLDING))
  614. {
  615. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  616. TIFFTAG_THRESHHOLDING,
  617. tif->tif_dir.td_threshholding))
  618. goto bad;
  619. }
  620. if (TIFFFieldSet(tif, FIELD_FILLORDER))
  621. {
  622. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  623. TIFFTAG_FILLORDER,
  624. tif->tif_dir.td_fillorder))
  625. goto bad;
  626. }
  627. if (TIFFFieldSet(tif, FIELD_ORIENTATION))
  628. {
  629. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  630. TIFFTAG_ORIENTATION,
  631. tif->tif_dir.td_orientation))
  632. goto bad;
  633. }
  634. if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
  635. {
  636. if (!TIFFWriteDirectoryTagShort(
  637. tif, &ndir, dir, TIFFTAG_SAMPLESPERPIXEL,
  638. tif->tif_dir.td_samplesperpixel))
  639. goto bad;
  640. }
  641. if (TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  642. {
  643. if (!TIFFWriteDirectoryTagShortLong(
  644. tif, &ndir, dir, TIFFTAG_ROWSPERSTRIP,
  645. tif->tif_dir.td_rowsperstrip))
  646. goto bad;
  647. }
  648. if (TIFFFieldSet(tif, FIELD_MINSAMPLEVALUE))
  649. {
  650. if (!TIFFWriteDirectoryTagShortPerSample(
  651. tif, &ndir, dir, TIFFTAG_MINSAMPLEVALUE,
  652. tif->tif_dir.td_minsamplevalue))
  653. goto bad;
  654. }
  655. if (TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  656. {
  657. if (!TIFFWriteDirectoryTagShortPerSample(
  658. tif, &ndir, dir, TIFFTAG_MAXSAMPLEVALUE,
  659. tif->tif_dir.td_maxsamplevalue))
  660. goto bad;
  661. }
  662. if (TIFFFieldSet(tif, FIELD_PLANARCONFIG))
  663. {
  664. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  665. TIFFTAG_PLANARCONFIG,
  666. tif->tif_dir.td_planarconfig))
  667. goto bad;
  668. }
  669. if (TIFFFieldSet(tif, FIELD_RESOLUTIONUNIT))
  670. {
  671. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  672. TIFFTAG_RESOLUTIONUNIT,
  673. tif->tif_dir.td_resolutionunit))
  674. goto bad;
  675. }
  676. if (TIFFFieldSet(tif, FIELD_PAGENUMBER))
  677. {
  678. if (!TIFFWriteDirectoryTagShortArray(
  679. tif, &ndir, dir, TIFFTAG_PAGENUMBER, 2,
  680. &tif->tif_dir.td_pagenumber[0]))
  681. goto bad;
  682. }
  683. if (TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
  684. {
  685. if (!isTiled(tif))
  686. {
  687. if (!TIFFWriteDirectoryTagLongLong8Array(
  688. tif, &ndir, dir, TIFFTAG_STRIPBYTECOUNTS,
  689. tif->tif_dir.td_nstrips,
  690. tif->tif_dir.td_stripbytecount_p))
  691. goto bad;
  692. }
  693. else
  694. {
  695. if (!TIFFWriteDirectoryTagLongLong8Array(
  696. tif, &ndir, dir, TIFFTAG_TILEBYTECOUNTS,
  697. tif->tif_dir.td_nstrips,
  698. tif->tif_dir.td_stripbytecount_p))
  699. goto bad;
  700. }
  701. }
  702. if (TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
  703. {
  704. if (!isTiled(tif))
  705. {
  706. /* td_stripoffset_p might be NULL in an odd OJPEG case. See
  707. * tif_dirread.c around line 3634.
  708. * XXX: OJPEG hack.
  709. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  710. * and c) the number of strips is 1,
  711. * then we tolerate the absence of stripoffsets tag,
  712. * because, presumably, all required data is in the
  713. * JpegInterchangeFormat stream.
  714. * We can get here when using tiffset on such a file.
  715. * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
  716. */
  717. if (tif->tif_dir.td_stripoffset_p != NULL &&
  718. !TIFFWriteDirectoryTagLongLong8Array(
  719. tif, &ndir, dir, TIFFTAG_STRIPOFFSETS,
  720. tif->tif_dir.td_nstrips,
  721. tif->tif_dir.td_stripoffset_p))
  722. goto bad;
  723. }
  724. else
  725. {
  726. if (!TIFFWriteDirectoryTagLongLong8Array(
  727. tif, &ndir, dir, TIFFTAG_TILEOFFSETS,
  728. tif->tif_dir.td_nstrips,
  729. tif->tif_dir.td_stripoffset_p))
  730. goto bad;
  731. }
  732. }
  733. if (TIFFFieldSet(tif, FIELD_COLORMAP))
  734. {
  735. if (!TIFFWriteDirectoryTagColormap(tif, &ndir, dir))
  736. goto bad;
  737. }
  738. if (TIFFFieldSet(tif, FIELD_EXTRASAMPLES))
  739. {
  740. if (tif->tif_dir.td_extrasamples)
  741. {
  742. uint16_t na;
  743. uint16_t *nb;
  744. TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &na, &nb);
  745. if (!TIFFWriteDirectoryTagShortArray(
  746. tif, &ndir, dir, TIFFTAG_EXTRASAMPLES, na, nb))
  747. goto bad;
  748. }
  749. }
  750. if (TIFFFieldSet(tif, FIELD_SAMPLEFORMAT))
  751. {
  752. if (!TIFFWriteDirectoryTagShortPerSample(
  753. tif, &ndir, dir, TIFFTAG_SAMPLEFORMAT,
  754. tif->tif_dir.td_sampleformat))
  755. goto bad;
  756. }
  757. if (TIFFFieldSet(tif, FIELD_SMINSAMPLEVALUE))
  758. {
  759. if (!TIFFWriteDirectoryTagSampleformatArray(
  760. tif, &ndir, dir, TIFFTAG_SMINSAMPLEVALUE,
  761. tif->tif_dir.td_samplesperpixel,
  762. tif->tif_dir.td_sminsamplevalue))
  763. goto bad;
  764. }
  765. if (TIFFFieldSet(tif, FIELD_SMAXSAMPLEVALUE))
  766. {
  767. if (!TIFFWriteDirectoryTagSampleformatArray(
  768. tif, &ndir, dir, TIFFTAG_SMAXSAMPLEVALUE,
  769. tif->tif_dir.td_samplesperpixel,
  770. tif->tif_dir.td_smaxsamplevalue))
  771. goto bad;
  772. }
  773. if (TIFFFieldSet(tif, FIELD_IMAGEDEPTH))
  774. {
  775. if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
  776. TIFFTAG_IMAGEDEPTH,
  777. tif->tif_dir.td_imagedepth))
  778. goto bad;
  779. }
  780. if (TIFFFieldSet(tif, FIELD_TILEDEPTH))
  781. {
  782. if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
  783. TIFFTAG_TILEDEPTH,
  784. tif->tif_dir.td_tiledepth))
  785. goto bad;
  786. }
  787. if (TIFFFieldSet(tif, FIELD_HALFTONEHINTS))
  788. {
  789. if (!TIFFWriteDirectoryTagShortArray(
  790. tif, &ndir, dir, TIFFTAG_HALFTONEHINTS, 2,
  791. &tif->tif_dir.td_halftonehints[0]))
  792. goto bad;
  793. }
  794. if (TIFFFieldSet(tif, FIELD_YCBCRSUBSAMPLING))
  795. {
  796. if (!TIFFWriteDirectoryTagShortArray(
  797. tif, &ndir, dir, TIFFTAG_YCBCRSUBSAMPLING, 2,
  798. &tif->tif_dir.td_ycbcrsubsampling[0]))
  799. goto bad;
  800. }
  801. if (TIFFFieldSet(tif, FIELD_YCBCRPOSITIONING))
  802. {
  803. if (!TIFFWriteDirectoryTagShort(
  804. tif, &ndir, dir, TIFFTAG_YCBCRPOSITIONING,
  805. tif->tif_dir.td_ycbcrpositioning))
  806. goto bad;
  807. }
  808. if (TIFFFieldSet(tif, FIELD_REFBLACKWHITE))
  809. {
  810. if (!TIFFWriteDirectoryTagRationalArray(
  811. tif, &ndir, dir, TIFFTAG_REFERENCEBLACKWHITE, 6,
  812. tif->tif_dir.td_refblackwhite))
  813. goto bad;
  814. }
  815. if (TIFFFieldSet(tif, FIELD_TRANSFERFUNCTION))
  816. {
  817. if (!TIFFWriteDirectoryTagTransferfunction(tif, &ndir, dir))
  818. goto bad;
  819. }
  820. if (TIFFFieldSet(tif, FIELD_INKNAMES))
  821. {
  822. if (!TIFFWriteDirectoryTagAscii(
  823. tif, &ndir, dir, TIFFTAG_INKNAMES,
  824. tif->tif_dir.td_inknameslen, tif->tif_dir.td_inknames))
  825. goto bad;
  826. }
  827. if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
  828. {
  829. if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
  830. TIFFTAG_NUMBEROFINKS,
  831. tif->tif_dir.td_numberofinks))
  832. goto bad;
  833. }
  834. if (TIFFFieldSet(tif, FIELD_SUBIFD))
  835. {
  836. if (!TIFFWriteDirectoryTagSubifd(tif, &ndir, dir))
  837. goto bad;
  838. }
  839. /*-- Step 2: Process named tags for an image with FIELD bits
  840. added by a codec.
  841. Attention: There is only code for some field_types,
  842. which are actually used by current codecs. --*/
  843. {
  844. uint32_t n;
  845. for (n = 0; n < tif->tif_nfields; n++)
  846. {
  847. const TIFFField *o;
  848. o = tif->tif_fields[n];
  849. if ((o->field_bit >= FIELD_CODEC) &&
  850. (TIFFFieldSet(tif, o->field_bit)))
  851. {
  852. switch (o->set_get_field_type)
  853. {
  854. case TIFF_SETGET_ASCII:
  855. {
  856. uint32_t pa;
  857. char *pb;
  858. assert(o->field_type == TIFF_ASCII);
  859. assert(o->field_readcount == TIFF_VARIABLE);
  860. assert(o->field_passcount == 0);
  861. TIFFGetField(tif, o->field_tag, &pb);
  862. pa = (uint32_t)(strlen(pb) + 1);
  863. if (!TIFFWriteDirectoryTagAscii(
  864. tif, &ndir, dir, (uint16_t)o->field_tag,
  865. pa, pb))
  866. goto bad;
  867. }
  868. break;
  869. case TIFF_SETGET_UINT16:
  870. {
  871. uint16_t p;
  872. assert(o->field_type == TIFF_SHORT);
  873. assert(o->field_readcount == 1);
  874. assert(o->field_passcount == 0);
  875. TIFFGetField(tif, o->field_tag, &p);
  876. if (!TIFFWriteDirectoryTagShort(
  877. tif, &ndir, dir, (uint16_t)o->field_tag,
  878. p))
  879. goto bad;
  880. }
  881. break;
  882. case TIFF_SETGET_UINT32:
  883. {
  884. uint32_t p;
  885. assert(o->field_type == TIFF_LONG);
  886. assert(o->field_readcount == 1);
  887. assert(o->field_passcount == 0);
  888. TIFFGetField(tif, o->field_tag, &p);
  889. if (!TIFFWriteDirectoryTagLong(
  890. tif, &ndir, dir, (uint16_t)o->field_tag,
  891. p))
  892. goto bad;
  893. }
  894. break;
  895. case TIFF_SETGET_C32_UINT8:
  896. {
  897. uint32_t pa;
  898. void *pb;
  899. assert(o->field_type == TIFF_UNDEFINED);
  900. assert(o->field_readcount == TIFF_VARIABLE2);
  901. assert(o->field_passcount == 1);
  902. TIFFGetField(tif, o->field_tag, &pa, &pb);
  903. if (!TIFFWriteDirectoryTagUndefinedArray(
  904. tif, &ndir, dir, (uint16_t)o->field_tag,
  905. pa, pb))
  906. goto bad;
  907. }
  908. break;
  909. default:
  910. TIFFErrorExtR(
  911. tif, module,
  912. "Cannot write tag %" PRIu32 " (%s)",
  913. TIFFFieldTag(o),
  914. o->field_name ? o->field_name : "unknown");
  915. goto bad;
  916. }
  917. }
  918. }
  919. }
  920. }
  921. /*-- Step 3: Process custom tags without FIELD bit for an image
  922. * or for custom IFDs (e.g. EXIF) with !isimage. --*/
  923. for (m = 0; m < (uint32_t)(tif->tif_dir.td_customValueCount); m++)
  924. {
  925. uint16_t tag =
  926. (uint16_t)tif->tif_dir.td_customValues[m].info->field_tag;
  927. uint32_t count = tif->tif_dir.td_customValues[m].count;
  928. switch (tif->tif_dir.td_customValues[m].info->field_type)
  929. {
  930. case TIFF_ASCII:
  931. if (!TIFFWriteDirectoryTagAscii(
  932. tif, &ndir, dir, tag, count,
  933. tif->tif_dir.td_customValues[m].value))
  934. goto bad;
  935. break;
  936. case TIFF_UNDEFINED:
  937. if (!TIFFWriteDirectoryTagUndefinedArray(
  938. tif, &ndir, dir, tag, count,
  939. tif->tif_dir.td_customValues[m].value))
  940. goto bad;
  941. break;
  942. case TIFF_BYTE:
  943. if (!TIFFWriteDirectoryTagByteArray(
  944. tif, &ndir, dir, tag, count,
  945. tif->tif_dir.td_customValues[m].value))
  946. goto bad;
  947. break;
  948. case TIFF_SBYTE:
  949. if (!TIFFWriteDirectoryTagSbyteArray(
  950. tif, &ndir, dir, tag, count,
  951. tif->tif_dir.td_customValues[m].value))
  952. goto bad;
  953. break;
  954. case TIFF_SHORT:
  955. if (!TIFFWriteDirectoryTagShortArray(
  956. tif, &ndir, dir, tag, count,
  957. tif->tif_dir.td_customValues[m].value))
  958. goto bad;
  959. break;
  960. case TIFF_SSHORT:
  961. if (!TIFFWriteDirectoryTagSshortArray(
  962. tif, &ndir, dir, tag, count,
  963. tif->tif_dir.td_customValues[m].value))
  964. goto bad;
  965. break;
  966. case TIFF_LONG:
  967. if (!TIFFWriteDirectoryTagLongArray(
  968. tif, &ndir, dir, tag, count,
  969. tif->tif_dir.td_customValues[m].value))
  970. goto bad;
  971. break;
  972. case TIFF_SLONG:
  973. if (!TIFFWriteDirectoryTagSlongArray(
  974. tif, &ndir, dir, tag, count,
  975. tif->tif_dir.td_customValues[m].value))
  976. goto bad;
  977. break;
  978. case TIFF_LONG8:
  979. if (!TIFFWriteDirectoryTagLong8Array(
  980. tif, &ndir, dir, tag, count,
  981. tif->tif_dir.td_customValues[m].value))
  982. goto bad;
  983. break;
  984. case TIFF_SLONG8:
  985. if (!TIFFWriteDirectoryTagSlong8Array(
  986. tif, &ndir, dir, tag, count,
  987. tif->tif_dir.td_customValues[m].value))
  988. goto bad;
  989. break;
  990. case TIFF_RATIONAL:
  991. {
  992. /*-- Rational2Double: For Rationals evaluate
  993. * "set_get_field_type" to determine internal storage size.
  994. */
  995. int tv_size;
  996. tv_size = TIFFFieldSetGetSize(
  997. tif->tif_dir.td_customValues[m].info);
  998. if (tv_size == 8)
  999. {
  1000. if (!TIFFWriteDirectoryTagRationalDoubleArray(
  1001. tif, &ndir, dir, tag, count,
  1002. tif->tif_dir.td_customValues[m].value))
  1003. goto bad;
  1004. }
  1005. else
  1006. {
  1007. /*-- default should be tv_size == 4 */
  1008. if (!TIFFWriteDirectoryTagRationalArray(
  1009. tif, &ndir, dir, tag, count,
  1010. tif->tif_dir.td_customValues[m].value))
  1011. goto bad;
  1012. /*-- ToDo: After Testing, this should be removed and
  1013. * tv_size==4 should be set as default. */
  1014. if (tv_size != 4)
  1015. {
  1016. TIFFErrorExtR(
  1017. tif, "TIFFLib: _TIFFWriteDirectorySec()",
  1018. "Rational2Double: .set_get_field_type is "
  1019. "not 4 but %d",
  1020. tv_size);
  1021. }
  1022. }
  1023. }
  1024. break;
  1025. case TIFF_SRATIONAL:
  1026. {
  1027. /*-- Rational2Double: For Rationals evaluate
  1028. * "set_get_field_type" to determine internal storage size.
  1029. */
  1030. int tv_size;
  1031. tv_size = TIFFFieldSetGetSize(
  1032. tif->tif_dir.td_customValues[m].info);
  1033. if (tv_size == 8)
  1034. {
  1035. if (!TIFFWriteDirectoryTagSrationalDoubleArray(
  1036. tif, &ndir, dir, tag, count,
  1037. tif->tif_dir.td_customValues[m].value))
  1038. goto bad;
  1039. }
  1040. else
  1041. {
  1042. /*-- default should be tv_size == 4 */
  1043. if (!TIFFWriteDirectoryTagSrationalArray(
  1044. tif, &ndir, dir, tag, count,
  1045. tif->tif_dir.td_customValues[m].value))
  1046. goto bad;
  1047. /*-- ToDo: After Testing, this should be removed and
  1048. * tv_size==4 should be set as default. */
  1049. if (tv_size != 4)
  1050. {
  1051. TIFFErrorExtR(
  1052. tif, "TIFFLib: _TIFFWriteDirectorySec()",
  1053. "Rational2Double: .set_get_field_type is "
  1054. "not 4 but %d",
  1055. tv_size);
  1056. }
  1057. }
  1058. }
  1059. break;
  1060. case TIFF_FLOAT:
  1061. if (!TIFFWriteDirectoryTagFloatArray(
  1062. tif, &ndir, dir, tag, count,
  1063. tif->tif_dir.td_customValues[m].value))
  1064. goto bad;
  1065. break;
  1066. case TIFF_DOUBLE:
  1067. if (!TIFFWriteDirectoryTagDoubleArray(
  1068. tif, &ndir, dir, tag, count,
  1069. tif->tif_dir.td_customValues[m].value))
  1070. goto bad;
  1071. break;
  1072. case TIFF_IFD:
  1073. if (!TIFFWriteDirectoryTagIfdArray(
  1074. tif, &ndir, dir, tag, count,
  1075. tif->tif_dir.td_customValues[m].value))
  1076. goto bad;
  1077. break;
  1078. case TIFF_IFD8:
  1079. if (!TIFFWriteDirectoryTagIfdIfd8Array(
  1080. tif, &ndir, dir, tag, count,
  1081. tif->tif_dir.td_customValues[m].value))
  1082. goto bad;
  1083. break;
  1084. default:
  1085. assert(0); /* we should never get here */
  1086. break;
  1087. }
  1088. }
  1089. /* "break" if IFD has been written above in second pass.*/
  1090. if (dir != NULL)
  1091. break;
  1092. /* Evaluate IFD data size: Finally, add the size of the IFD tag entries
  1093. * themselves. */
  1094. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1095. tif->tif_dir.td_dirdatasize_write += 2 + ndir * 12 + 4;
  1096. else
  1097. tif->tif_dir.td_dirdatasize_write += 8 + ndir * 20 + 8;
  1098. /* Setup a new directory within first pass. */
  1099. dir = _TIFFmallocExt(tif, ndir * sizeof(TIFFDirEntry));
  1100. if (dir == NULL)
  1101. {
  1102. TIFFErrorExtR(tif, module, "Out of memory");
  1103. goto bad;
  1104. }
  1105. if (isimage)
  1106. {
  1107. /* Check, weather the IFD to be written is new or an already written
  1108. * IFD can be overwritten or needs to be re-written to a different
  1109. * location in the file because the IFD is extended with additional
  1110. * tags or the IFD data size is increased.
  1111. * - tif_diroff == 0, if a new directory has to be linked.
  1112. * - tif_diroff != 0, IFD has been re-read from file and will be
  1113. * overwritten or re-written.
  1114. */
  1115. if (tif->tif_diroff == 0)
  1116. {
  1117. if (!TIFFLinkDirectory(tif))
  1118. goto bad;
  1119. }
  1120. else if (tif->tif_dir.td_dirdatasize_write >
  1121. tif->tif_dir.td_dirdatasize_read)
  1122. {
  1123. if (dir != NULL)
  1124. {
  1125. _TIFFfreeExt(tif, dir);
  1126. dir = NULL;
  1127. }
  1128. if (!TIFFRewriteDirectorySec(tif, isimage, imagedone, pdiroff))
  1129. goto bad;
  1130. return (1);
  1131. }
  1132. }
  1133. else
  1134. {
  1135. /* For !isimage, which means custom-IFD like EXIFIFD or
  1136. * checkpointing an IFD, determine whether to overwrite or append at
  1137. * the end of the file.
  1138. */
  1139. if (!((tif->tif_dir.td_dirdatasize_read > 0) &&
  1140. (tif->tif_dir.td_dirdatasize_write <=
  1141. tif->tif_dir.td_dirdatasize_read)))
  1142. {
  1143. /* Append at end of file and increment to an even offset. */
  1144. tif->tif_diroff =
  1145. (TIFFSeekFile(tif, 0, SEEK_END) + 1) & (~((toff_t)1));
  1146. }
  1147. }
  1148. /* Return IFD offset */
  1149. if (pdiroff != NULL)
  1150. *pdiroff = tif->tif_diroff;
  1151. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1152. dirsize = 2 + ndir * 12 + 4;
  1153. else
  1154. dirsize = 8 + ndir * 20 + 8;
  1155. /* Append IFD data stright after the IFD tag entries.
  1156. * Data that does not fit into an IFD tag entry is written to the file
  1157. * in the second pass of the while loop. That offset is stored in "dir".
  1158. */
  1159. tif->tif_dataoff = tif->tif_diroff + dirsize;
  1160. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1161. tif->tif_dataoff = (uint32_t)tif->tif_dataoff;
  1162. if ((tif->tif_dataoff < tif->tif_diroff) ||
  1163. (tif->tif_dataoff < (uint64_t)dirsize))
  1164. {
  1165. TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
  1166. goto bad;
  1167. }
  1168. if (tif->tif_dataoff & 1)
  1169. tif->tif_dataoff++;
  1170. } /* while() */
  1171. if (isimage)
  1172. {
  1173. /* For SubIFDs remember offset of SubIFD tag within main IFD.
  1174. * However, might be already done in TIFFWriteDirectoryTagSubifd() if
  1175. * there are more than one SubIFD. */
  1176. if (TIFFFieldSet(tif, FIELD_SUBIFD) && (tif->tif_subifdoff == 0))
  1177. {
  1178. uint32_t na;
  1179. TIFFDirEntry *nb;
  1180. for (na = 0, nb = dir;; na++, nb++)
  1181. {
  1182. if (na == ndir)
  1183. {
  1184. TIFFErrorExtR(tif, module, "Cannot find SubIFD tag");
  1185. goto bad;
  1186. }
  1187. if (nb->tdir_tag == TIFFTAG_SUBIFD)
  1188. break;
  1189. }
  1190. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1191. tif->tif_subifdoff = tif->tif_diroff + 2 + na * 12 + 8;
  1192. else
  1193. tif->tif_subifdoff = tif->tif_diroff + 8 + na * 20 + 12;
  1194. }
  1195. }
  1196. /* Copy/swab IFD entries from "dir" into "dirmem",
  1197. * which is then written to file. */
  1198. dirmem = _TIFFmallocExt(tif, dirsize);
  1199. if (dirmem == NULL)
  1200. {
  1201. TIFFErrorExtR(tif, module, "Out of memory");
  1202. goto bad;
  1203. }
  1204. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1205. {
  1206. uint8_t *n;
  1207. uint32_t nTmp;
  1208. TIFFDirEntry *o;
  1209. n = dirmem;
  1210. *(uint16_t *)n = (uint16_t)ndir;
  1211. if (tif->tif_flags & TIFF_SWAB)
  1212. TIFFSwabShort((uint16_t *)n);
  1213. n += 2;
  1214. o = dir;
  1215. for (m = 0; m < ndir; m++)
  1216. {
  1217. *(uint16_t *)n = o->tdir_tag;
  1218. if (tif->tif_flags & TIFF_SWAB)
  1219. TIFFSwabShort((uint16_t *)n);
  1220. n += 2;
  1221. *(uint16_t *)n = o->tdir_type;
  1222. if (tif->tif_flags & TIFF_SWAB)
  1223. TIFFSwabShort((uint16_t *)n);
  1224. n += 2;
  1225. nTmp = (uint32_t)o->tdir_count;
  1226. _TIFFmemcpy(n, &nTmp, 4);
  1227. if (tif->tif_flags & TIFF_SWAB)
  1228. TIFFSwabLong((uint32_t *)n);
  1229. n += 4;
  1230. /* This is correct. The data has been */
  1231. /* swabbed previously in TIFFWriteDirectoryTagData */
  1232. _TIFFmemcpy(n, &o->tdir_offset, 4);
  1233. n += 4;
  1234. o++;
  1235. }
  1236. nTmp = (uint32_t)tif->tif_nextdiroff;
  1237. if (tif->tif_flags & TIFF_SWAB)
  1238. TIFFSwabLong(&nTmp);
  1239. _TIFFmemcpy(n, &nTmp, 4);
  1240. }
  1241. else
  1242. {
  1243. uint8_t *n;
  1244. TIFFDirEntry *o;
  1245. n = dirmem;
  1246. *(uint64_t *)n = ndir;
  1247. if (tif->tif_flags & TIFF_SWAB)
  1248. TIFFSwabLong8((uint64_t *)n);
  1249. n += 8;
  1250. o = dir;
  1251. for (m = 0; m < ndir; m++)
  1252. {
  1253. *(uint16_t *)n = o->tdir_tag;
  1254. if (tif->tif_flags & TIFF_SWAB)
  1255. TIFFSwabShort((uint16_t *)n);
  1256. n += 2;
  1257. *(uint16_t *)n = o->tdir_type;
  1258. if (tif->tif_flags & TIFF_SWAB)
  1259. TIFFSwabShort((uint16_t *)n);
  1260. n += 2;
  1261. _TIFFmemcpy(n, &o->tdir_count, 8);
  1262. if (tif->tif_flags & TIFF_SWAB)
  1263. TIFFSwabLong8((uint64_t *)n);
  1264. n += 8;
  1265. _TIFFmemcpy(n, &o->tdir_offset, 8);
  1266. n += 8;
  1267. o++;
  1268. }
  1269. _TIFFmemcpy(n, &tif->tif_nextdiroff, 8);
  1270. if (tif->tif_flags & TIFF_SWAB)
  1271. TIFFSwabLong8((uint64_t *)n);
  1272. }
  1273. _TIFFfreeExt(tif, dir);
  1274. dir = NULL;
  1275. if (!SeekOK(tif, tif->tif_diroff))
  1276. {
  1277. TIFFErrorExtR(tif, module,
  1278. "IO error writing directory at seek to offset");
  1279. goto bad;
  1280. }
  1281. if (!WriteOK(tif, dirmem, (tmsize_t)dirsize))
  1282. {
  1283. TIFFErrorExtR(tif, module, "IO error writing directory");
  1284. goto bad;
  1285. }
  1286. _TIFFfreeExt(tif, dirmem);
  1287. /* Increment tif_curdir if IFD wasn't already written to file and no error
  1288. * occurred during IFD writing above. */
  1289. if (isimage && !tif->tif_dir.td_iswrittentofile)
  1290. {
  1291. if (!((tif->tif_flags & TIFF_INSUBIFD) &&
  1292. !(TIFFFieldSet(tif, FIELD_SUBIFD))))
  1293. {
  1294. /*-- Normal main-IFD case --*/
  1295. if (tif->tif_curdircount != TIFF_NON_EXISTENT_DIR_NUMBER)
  1296. {
  1297. tif->tif_curdir = tif->tif_curdircount;
  1298. }
  1299. else
  1300. {
  1301. /*ToDo SU: NEW_IFD_CURDIR_INCREMENTING: Delete this
  1302. * unexpected case after some testing time. */
  1303. /* Attention: tif->tif_curdircount is already set within
  1304. * TIFFNumberOfDirectories() */
  1305. tif->tif_curdircount = TIFFNumberOfDirectories(tif);
  1306. tif->tif_curdir = tif->tif_curdircount;
  1307. TIFFErrorExtR(
  1308. tif, module,
  1309. "tif_curdircount is TIFF_NON_EXISTENT_DIR_NUMBER, "
  1310. "not expected !! Line %d",
  1311. __LINE__);
  1312. goto bad;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. /*-- SubIFD case -- */
  1318. /* tif_curdir is always set to 0 for all SubIFDs. */
  1319. tif->tif_curdir = 0;
  1320. }
  1321. }
  1322. /* Increment tif_curdircount only if main-IFD of an image was not already
  1323. * present on file. */
  1324. /* Check in combination with (... && !(TIFFFieldSet(tif, FIELD_SUBIFD)))
  1325. * is necessary here because TIFF_INSUBIFD was already set above for the
  1326. * next SubIFD when this main-IFD (with FIELD_SUBIFD) is currently being
  1327. * written. */
  1328. if (isimage && !tif->tif_dir.td_iswrittentofile &&
  1329. !((tif->tif_flags & TIFF_INSUBIFD) &&
  1330. !(TIFFFieldSet(tif, FIELD_SUBIFD))))
  1331. tif->tif_curdircount++;
  1332. tif->tif_dir.td_iswrittentofile = TRUE;
  1333. /* Reset SubIFD writing stage after last SubIFD has been written. */
  1334. if (imagedone && (tif->tif_flags & TIFF_INSUBIFD) && tif->tif_nsubifd == 0)
  1335. tif->tif_flags &= ~TIFF_INSUBIFD;
  1336. /* Add or update this directory to the IFD list. */
  1337. if (!_TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, tif->tif_diroff))
  1338. {
  1339. TIFFErrorExtR(tif, module,
  1340. "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
  1341. ") might cause an IFD loop",
  1342. tif->tif_curdir, tif->tif_diroff, tif->tif_diroff);
  1343. }
  1344. if (imagedone)
  1345. {
  1346. TIFFFreeDirectory(tif);
  1347. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  1348. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  1349. /* Reset directory-related state for subsequent directories. */
  1350. TIFFCreateDirectory(tif);
  1351. }
  1352. else
  1353. {
  1354. /* IFD is only checkpointed to file (or a custom IFD like EXIF is
  1355. * written), thus set IFD data size written to file. */
  1356. tif->tif_dir.td_dirdatasize_read = tif->tif_dir.td_dirdatasize_write;
  1357. }
  1358. return (1);
  1359. bad:
  1360. if (dir != NULL)
  1361. _TIFFfreeExt(tif, dir);
  1362. if (dirmem != NULL)
  1363. _TIFFfreeExt(tif, dirmem);
  1364. return (0);
  1365. }
  1366. static int8_t TIFFClampDoubleToInt8(double val)
  1367. {
  1368. if (val > 127)
  1369. return 127;
  1370. if (val < -128 || val != val)
  1371. return -128;
  1372. return (int8_t)val;
  1373. }
  1374. static int16_t TIFFClampDoubleToInt16(double val)
  1375. {
  1376. if (val > 32767)
  1377. return 32767;
  1378. if (val < -32768 || val != val)
  1379. return -32768;
  1380. return (int16_t)val;
  1381. }
  1382. static int32_t TIFFClampDoubleToInt32(double val)
  1383. {
  1384. if (val > 0x7FFFFFFF)
  1385. return 0x7FFFFFFF;
  1386. if (val < -0x7FFFFFFF - 1 || val != val)
  1387. return -0x7FFFFFFF - 1;
  1388. return (int32_t)val;
  1389. }
  1390. static uint8_t TIFFClampDoubleToUInt8(double val)
  1391. {
  1392. if (val < 0)
  1393. return 0;
  1394. if (val > 255 || val != val)
  1395. return 255;
  1396. return (uint8_t)val;
  1397. }
  1398. static uint16_t TIFFClampDoubleToUInt16(double val)
  1399. {
  1400. if (val < 0)
  1401. return 0;
  1402. if (val > 65535 || val != val)
  1403. return 65535;
  1404. return (uint16_t)val;
  1405. }
  1406. static uint32_t TIFFClampDoubleToUInt32(double val)
  1407. {
  1408. if (val < 0)
  1409. return 0;
  1410. if (val > 0xFFFFFFFFU || val != val)
  1411. return 0xFFFFFFFFU;
  1412. return (uint32_t)val;
  1413. }
  1414. static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir,
  1415. TIFFDirEntry *dir,
  1416. uint16_t tag, uint32_t count,
  1417. double *value)
  1418. {
  1419. static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
  1420. void *conv;
  1421. uint32_t i;
  1422. int ok;
  1423. conv = _TIFFmallocExt(tif, count * sizeof(double));
  1424. if (conv == NULL)
  1425. {
  1426. TIFFErrorExtR(tif, module, "Out of memory");
  1427. return (0);
  1428. }
  1429. switch (tif->tif_dir.td_sampleformat)
  1430. {
  1431. case SAMPLEFORMAT_IEEEFP:
  1432. if (tif->tif_dir.td_bitspersample <= 32)
  1433. {
  1434. for (i = 0; i < count; ++i)
  1435. ((float *)conv)[i] = _TIFFClampDoubleToFloat(value[i]);
  1436. ok = TIFFWriteDirectoryTagFloatArray(tif, ndir, dir, tag, count,
  1437. (float *)conv);
  1438. }
  1439. else
  1440. {
  1441. ok = TIFFWriteDirectoryTagDoubleArray(tif, ndir, dir, tag,
  1442. count, value);
  1443. }
  1444. break;
  1445. case SAMPLEFORMAT_INT:
  1446. if (tif->tif_dir.td_bitspersample <= 8)
  1447. {
  1448. for (i = 0; i < count; ++i)
  1449. ((int8_t *)conv)[i] = TIFFClampDoubleToInt8(value[i]);
  1450. ok = TIFFWriteDirectoryTagSbyteArray(tif, ndir, dir, tag, count,
  1451. (int8_t *)conv);
  1452. }
  1453. else if (tif->tif_dir.td_bitspersample <= 16)
  1454. {
  1455. for (i = 0; i < count; ++i)
  1456. ((int16_t *)conv)[i] = TIFFClampDoubleToInt16(value[i]);
  1457. ok = TIFFWriteDirectoryTagSshortArray(tif, ndir, dir, tag,
  1458. count, (int16_t *)conv);
  1459. }
  1460. else
  1461. {
  1462. for (i = 0; i < count; ++i)
  1463. ((int32_t *)conv)[i] = TIFFClampDoubleToInt32(value[i]);
  1464. ok = TIFFWriteDirectoryTagSlongArray(tif, ndir, dir, tag, count,
  1465. (int32_t *)conv);
  1466. }
  1467. break;
  1468. case SAMPLEFORMAT_UINT:
  1469. if (tif->tif_dir.td_bitspersample <= 8)
  1470. {
  1471. for (i = 0; i < count; ++i)
  1472. ((uint8_t *)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
  1473. ok = TIFFWriteDirectoryTagByteArray(tif, ndir, dir, tag, count,
  1474. (uint8_t *)conv);
  1475. }
  1476. else if (tif->tif_dir.td_bitspersample <= 16)
  1477. {
  1478. for (i = 0; i < count; ++i)
  1479. ((uint16_t *)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
  1480. ok = TIFFWriteDirectoryTagShortArray(tif, ndir, dir, tag, count,
  1481. (uint16_t *)conv);
  1482. }
  1483. else
  1484. {
  1485. for (i = 0; i < count; ++i)
  1486. ((uint32_t *)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
  1487. ok = TIFFWriteDirectoryTagLongArray(tif, ndir, dir, tag, count,
  1488. (uint32_t *)conv);
  1489. }
  1490. break;
  1491. default:
  1492. ok = 0;
  1493. }
  1494. _TIFFfreeExt(tif, conv);
  1495. return (ok);
  1496. }
  1497. static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir,
  1498. TIFFDirEntry *dir, uint16_t tag,
  1499. uint32_t count, char *value)
  1500. {
  1501. return (
  1502. TIFFWriteDirectoryTagCheckedAscii(tif, ndir, dir, tag, count, value));
  1503. }
  1504. static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir,
  1505. TIFFDirEntry *dir, uint16_t tag,
  1506. uint32_t count, uint8_t *value)
  1507. {
  1508. return (TIFFWriteDirectoryTagCheckedUndefinedArray(tif, ndir, dir, tag,
  1509. count, value));
  1510. }
  1511. static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir,
  1512. TIFFDirEntry *dir, uint16_t tag,
  1513. uint32_t count, uint8_t *value)
  1514. {
  1515. return (TIFFWriteDirectoryTagCheckedByteArray(tif, ndir, dir, tag, count,
  1516. value));
  1517. }
  1518. static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir,
  1519. TIFFDirEntry *dir, uint16_t tag,
  1520. uint32_t count, int8_t *value)
  1521. {
  1522. return (TIFFWriteDirectoryTagCheckedSbyteArray(tif, ndir, dir, tag, count,
  1523. value));
  1524. }
  1525. static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir,
  1526. TIFFDirEntry *dir, uint16_t tag,
  1527. uint16_t value)
  1528. {
  1529. return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag, value));
  1530. }
  1531. static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir,
  1532. TIFFDirEntry *dir, uint16_t tag,
  1533. uint32_t count, uint16_t *value)
  1534. {
  1535. return (TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count,
  1536. value));
  1537. }
  1538. static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir,
  1539. TIFFDirEntry *dir, uint16_t tag,
  1540. uint16_t value)
  1541. {
  1542. static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
  1543. uint16_t *m;
  1544. uint16_t *na;
  1545. uint16_t nb;
  1546. int o;
  1547. if (dir == NULL)
  1548. {
  1549. /* only evaluate IFD data size and inc. ndir */
  1550. return (TIFFWriteDirectoryTagCheckedShortArray(
  1551. tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, NULL));
  1552. }
  1553. m = _TIFFmallocExt(tif, tif->tif_dir.td_samplesperpixel * sizeof(uint16_t));
  1554. if (m == NULL)
  1555. {
  1556. TIFFErrorExtR(tif, module, "Out of memory");
  1557. return (0);
  1558. }
  1559. for (na = m, nb = 0; nb < tif->tif_dir.td_samplesperpixel; na++, nb++)
  1560. *na = value;
  1561. o = TIFFWriteDirectoryTagCheckedShortArray(
  1562. tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, m);
  1563. _TIFFfreeExt(tif, m);
  1564. return (o);
  1565. }
  1566. static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir,
  1567. TIFFDirEntry *dir, uint16_t tag,
  1568. uint32_t count, int16_t *value)
  1569. {
  1570. return (TIFFWriteDirectoryTagCheckedSshortArray(tif, ndir, dir, tag, count,
  1571. value));
  1572. }
  1573. static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir,
  1574. TIFFDirEntry *dir, uint16_t tag,
  1575. uint32_t value)
  1576. {
  1577. return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value));
  1578. }
  1579. static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir,
  1580. TIFFDirEntry *dir, uint16_t tag,
  1581. uint32_t count, uint32_t *value)
  1582. {
  1583. return (TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count,
  1584. value));
  1585. }
  1586. static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir,
  1587. TIFFDirEntry *dir, uint16_t tag,
  1588. uint32_t count, int32_t *value)
  1589. {
  1590. return (TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count,
  1591. value));
  1592. }
  1593. /************************************************************************/
  1594. /* TIFFWriteDirectoryTagLong8Array() */
  1595. /* */
  1596. /* Write either Long8 or Long array depending on file type. */
  1597. /************************************************************************/
  1598. static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir,
  1599. TIFFDirEntry *dir, uint16_t tag,
  1600. uint32_t count, uint64_t *value)
  1601. {
  1602. static const char module[] = "TIFFWriteDirectoryTagLong8Array";
  1603. uint64_t *ma;
  1604. uint32_t mb;
  1605. uint32_t *p;
  1606. uint32_t *q;
  1607. int o;
  1608. /* is this just a counting pass? */
  1609. if (dir == NULL)
  1610. {
  1611. /* only evaluate IFD data size and inc. ndir */
  1612. return (TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag,
  1613. count, value));
  1614. }
  1615. /* We always write Long8 for BigTIFF, no checking needed. */
  1616. if (tif->tif_flags & TIFF_BIGTIFF)
  1617. return (TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag,
  1618. count, value));
  1619. /*
  1620. ** For classic tiff we want to verify everything is in range for long
  1621. ** and convert to long format.
  1622. */
  1623. p = _TIFFmallocExt(tif, count * sizeof(uint32_t));
  1624. if (p == NULL)
  1625. {
  1626. TIFFErrorExtR(tif, module, "Out of memory");
  1627. return (0);
  1628. }
  1629. for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
  1630. {
  1631. if (*ma > 0xFFFFFFFF)
  1632. {
  1633. TIFFErrorExtR(tif, module,
  1634. "Attempt to write unsigned long value %" PRIu64
  1635. " larger than 0xFFFFFFFF for tag %d in Classic TIFF "
  1636. "file. TIFF file writing aborted",
  1637. *ma, tag);
  1638. _TIFFfreeExt(tif, p);
  1639. return (0);
  1640. }
  1641. *q = (uint32_t)(*ma);
  1642. }
  1643. o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count, p);
  1644. _TIFFfreeExt(tif, p);
  1645. return (o);
  1646. }
  1647. /************************************************************************/
  1648. /* TIFFWriteDirectoryTagSlong8Array() */
  1649. /* */
  1650. /* Write either SLong8 or SLong array depending on file type. */
  1651. /************************************************************************/
  1652. static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir,
  1653. TIFFDirEntry *dir, uint16_t tag,
  1654. uint32_t count, int64_t *value)
  1655. {
  1656. static const char module[] = "TIFFWriteDirectoryTagSlong8Array";
  1657. int64_t *ma;
  1658. uint32_t mb;
  1659. int32_t *p;
  1660. int32_t *q;
  1661. int o;
  1662. /* is this just a counting pass? */
  1663. if (dir == NULL)
  1664. {
  1665. /* only evaluate IFD data size and inc. ndir */
  1666. return (TIFFWriteDirectoryTagCheckedSlong8Array(tif, ndir, dir, tag,
  1667. count, value));
  1668. }
  1669. /* We always write SLong8 for BigTIFF, no checking needed. */
  1670. if (tif->tif_flags & TIFF_BIGTIFF)
  1671. return (TIFFWriteDirectoryTagCheckedSlong8Array(tif, ndir, dir, tag,
  1672. count, value));
  1673. /*
  1674. ** For classic tiff we want to verify everything is in range for signed-long
  1675. ** and convert to signed-long format.
  1676. */
  1677. p = _TIFFmallocExt(tif, count * sizeof(uint32_t));
  1678. if (p == NULL)
  1679. {
  1680. TIFFErrorExtR(tif, module, "Out of memory");
  1681. return (0);
  1682. }
  1683. for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
  1684. {
  1685. if (*ma > (2147483647))
  1686. {
  1687. TIFFErrorExtR(tif, module,
  1688. "Attempt to write signed long value %" PRIi64
  1689. " larger than 0x7FFFFFFF (2147483647) for tag %d in "
  1690. "Classic TIFF file. TIFF writing to file aborted",
  1691. *ma, tag);
  1692. _TIFFfreeExt(tif, p);
  1693. return (0);
  1694. }
  1695. else if (*ma < (-2147483647 - 1))
  1696. {
  1697. TIFFErrorExtR(tif, module,
  1698. "Attempt to write signed long value %" PRIi64
  1699. " smaller than 0x80000000 (-2147483648) for tag %d "
  1700. "in Classic TIFF file. TIFF writing to file aborted",
  1701. *ma, tag);
  1702. _TIFFfreeExt(tif, p);
  1703. return (0);
  1704. }
  1705. *q = (int32_t)(*ma);
  1706. }
  1707. o = TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count, p);
  1708. _TIFFfreeExt(tif, p);
  1709. return (o);
  1710. }
  1711. static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir,
  1712. TIFFDirEntry *dir, uint16_t tag,
  1713. double value)
  1714. {
  1715. return (TIFFWriteDirectoryTagCheckedRational(tif, ndir, dir, tag, value));
  1716. }
  1717. static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir,
  1718. TIFFDirEntry *dir, uint16_t tag,
  1719. uint32_t count, float *value)
  1720. {
  1721. return (TIFFWriteDirectoryTagCheckedRationalArray(tif, ndir, dir, tag,
  1722. count, value));
  1723. }
  1724. static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir,
  1725. TIFFDirEntry *dir, uint16_t tag,
  1726. uint32_t count, float *value)
  1727. {
  1728. return (TIFFWriteDirectoryTagCheckedSrationalArray(tif, ndir, dir, tag,
  1729. count, value));
  1730. }
  1731. /*-- Rational2Double: additional write functions */
  1732. static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir,
  1733. TIFFDirEntry *dir,
  1734. uint16_t tag,
  1735. uint32_t count,
  1736. double *value)
  1737. {
  1738. return (TIFFWriteDirectoryTagCheckedRationalDoubleArray(tif, ndir, dir, tag,
  1739. count, value));
  1740. }
  1741. static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir,
  1742. TIFFDirEntry *dir,
  1743. uint16_t tag,
  1744. uint32_t count,
  1745. double *value)
  1746. {
  1747. return (TIFFWriteDirectoryTagCheckedSrationalDoubleArray(
  1748. tif, ndir, dir, tag, count, value));
  1749. }
  1750. static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir,
  1751. TIFFDirEntry *dir, uint16_t tag,
  1752. uint32_t count, float *value)
  1753. {
  1754. return (TIFFWriteDirectoryTagCheckedFloatArray(tif, ndir, dir, tag, count,
  1755. value));
  1756. }
  1757. static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir,
  1758. TIFFDirEntry *dir, uint16_t tag,
  1759. uint32_t count, double *value)
  1760. {
  1761. return (TIFFWriteDirectoryTagCheckedDoubleArray(tif, ndir, dir, tag, count,
  1762. value));
  1763. }
  1764. static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir,
  1765. TIFFDirEntry *dir, uint16_t tag,
  1766. uint32_t count, uint32_t *value)
  1767. {
  1768. return (TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count,
  1769. value));
  1770. }
  1771. static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir,
  1772. TIFFDirEntry *dir, uint16_t tag,
  1773. uint32_t value)
  1774. {
  1775. if (value <= 0xFFFF)
  1776. return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag,
  1777. (uint16_t)value));
  1778. else
  1779. return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value));
  1780. }
  1781. static int _WriteAsType(TIFF *tif, uint64_t strile_size,
  1782. uint64_t uncompressed_threshold)
  1783. {
  1784. const uint16_t compression = tif->tif_dir.td_compression;
  1785. if (compression == COMPRESSION_NONE)
  1786. {
  1787. return strile_size > uncompressed_threshold;
  1788. }
  1789. else if (compression == COMPRESSION_JPEG ||
  1790. compression == COMPRESSION_LZW ||
  1791. compression == COMPRESSION_ADOBE_DEFLATE ||
  1792. compression == COMPRESSION_DEFLATE ||
  1793. compression == COMPRESSION_LZMA ||
  1794. compression == COMPRESSION_LERC ||
  1795. compression == COMPRESSION_ZSTD ||
  1796. compression == COMPRESSION_WEBP || compression == COMPRESSION_JXL)
  1797. {
  1798. /* For a few select compression types, we assume that in the worst */
  1799. /* case the compressed size will be 10 times the uncompressed size. */
  1800. /* This is overly pessismistic ! */
  1801. return strile_size >= uncompressed_threshold / 10;
  1802. }
  1803. return 1;
  1804. }
  1805. static int WriteAsLong8(TIFF *tif, uint64_t strile_size)
  1806. {
  1807. return _WriteAsType(tif, strile_size, 0xFFFFFFFFU);
  1808. }
  1809. static int WriteAsLong4(TIFF *tif, uint64_t strile_size)
  1810. {
  1811. return _WriteAsType(tif, strile_size, 0xFFFFU);
  1812. }
  1813. /************************************************************************/
  1814. /* TIFFWriteDirectoryTagLongLong8Array() */
  1815. /* */
  1816. /* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */
  1817. /* on strile size and Classic/BigTIFF mode. */
  1818. /************************************************************************/
  1819. static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir,
  1820. TIFFDirEntry *dir, uint16_t tag,
  1821. uint32_t count, uint64_t *value)
  1822. {
  1823. static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
  1824. int o;
  1825. int write_aslong4;
  1826. if (tif->tif_dir.td_deferstrilearraywriting)
  1827. {
  1828. if (dir == NULL)
  1829. {
  1830. /* This is just a counting pass to count IFD entries.
  1831. * For deferstrilearraywriting no extra bytes will be written
  1832. * into IFD space. */
  1833. (*ndir)++;
  1834. return 1;
  1835. }
  1836. return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0,
  1837. NULL);
  1838. }
  1839. if (tif->tif_flags & TIFF_BIGTIFF)
  1840. {
  1841. int write_aslong8 = 1;
  1842. /* In the case of ByteCounts array, we may be able to write them on LONG
  1843. * if the strip/tilesize is not too big. Also do that for count > 1 in
  1844. * the case someone would want to create a single-strip file with a
  1845. * growing height, in which case using LONG8 will be safer. */
  1846. if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
  1847. {
  1848. write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
  1849. }
  1850. else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
  1851. {
  1852. write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
  1853. }
  1854. if (write_aslong8)
  1855. {
  1856. return TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag,
  1857. count, value);
  1858. }
  1859. }
  1860. write_aslong4 = 1;
  1861. if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
  1862. {
  1863. write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
  1864. }
  1865. else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
  1866. {
  1867. write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
  1868. }
  1869. if (write_aslong4)
  1870. {
  1871. /*
  1872. ** For classic tiff we want to verify everything is in range for LONG
  1873. ** and convert to long format.
  1874. */
  1875. uint32_t *p = _TIFFmallocExt(tif, count * sizeof(uint32_t));
  1876. uint32_t *q;
  1877. uint64_t *ma;
  1878. uint32_t mb;
  1879. if (p == NULL)
  1880. {
  1881. TIFFErrorExtR(tif, module, "Out of memory");
  1882. return (0);
  1883. }
  1884. for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
  1885. {
  1886. if (*ma > 0xFFFFFFFF)
  1887. {
  1888. TIFFErrorExtR(tif, module,
  1889. "Attempt to write value larger than 0xFFFFFFFF "
  1890. "in LONG array.");
  1891. _TIFFfreeExt(tif, p);
  1892. return (0);
  1893. }
  1894. *q = (uint32_t)(*ma);
  1895. }
  1896. o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count,
  1897. p);
  1898. _TIFFfreeExt(tif, p);
  1899. }
  1900. else
  1901. {
  1902. uint16_t *p = _TIFFmallocExt(tif, count * sizeof(uint16_t));
  1903. uint16_t *q;
  1904. uint64_t *ma;
  1905. uint32_t mb;
  1906. if (p == NULL)
  1907. {
  1908. TIFFErrorExtR(tif, module, "Out of memory");
  1909. return (0);
  1910. }
  1911. for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
  1912. {
  1913. if (*ma > 0xFFFF)
  1914. {
  1915. /* Should not happen normally given the check we did before */
  1916. TIFFErrorExtR(tif, module,
  1917. "Attempt to write value larger than 0xFFFF in "
  1918. "SHORT array.");
  1919. _TIFFfreeExt(tif, p);
  1920. return (0);
  1921. }
  1922. *q = (uint16_t)(*ma);
  1923. }
  1924. o = TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count,
  1925. p);
  1926. _TIFFfreeExt(tif, p);
  1927. }
  1928. return (o);
  1929. }
  1930. /************************************************************************/
  1931. /* TIFFWriteDirectoryTagIfdIfd8Array() */
  1932. /* */
  1933. /* Write either IFD8 or IFD array depending on file type. */
  1934. /************************************************************************/
  1935. static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir,
  1936. TIFFDirEntry *dir, uint16_t tag,
  1937. uint32_t count, uint64_t *value)
  1938. {
  1939. static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
  1940. uint64_t *ma;
  1941. uint32_t mb;
  1942. uint32_t *p;
  1943. uint32_t *q;
  1944. int o;
  1945. /* We always write IFD8 for BigTIFF, no checking needed. */
  1946. if (tif->tif_flags & TIFF_BIGTIFF)
  1947. return TIFFWriteDirectoryTagCheckedIfd8Array(tif, ndir, dir, tag, count,
  1948. value);
  1949. /*
  1950. ** For classic tiff we want to verify everything is in range for IFD
  1951. ** and convert to long format.
  1952. */
  1953. p = _TIFFmallocExt(tif, count * sizeof(uint32_t));
  1954. if (p == NULL)
  1955. {
  1956. TIFFErrorExtR(tif, module, "Out of memory");
  1957. return (0);
  1958. }
  1959. for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
  1960. {
  1961. if (*ma > 0xFFFFFFFF)
  1962. {
  1963. TIFFErrorExtR(tif, module,
  1964. "Attempt to write value larger than 0xFFFFFFFF in "
  1965. "Classic TIFF file.");
  1966. _TIFFfreeExt(tif, p);
  1967. return (0);
  1968. }
  1969. *q = (uint32_t)(*ma);
  1970. }
  1971. o = TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count, p);
  1972. _TIFFfreeExt(tif, p);
  1973. return (o);
  1974. }
  1975. /*
  1976. * Auxiliary function to determine the IFD data size to be written to the file.
  1977. * The IFD data size is finally the size of the IFD tag entries plus the IFD
  1978. * data that is written directly after the IFD tag entries.
  1979. */
  1980. static void EvaluateIFDdatasizeWrite(TIFF *tif, uint32_t count,
  1981. uint32_t typesize, uint32_t *ndir)
  1982. {
  1983. uint64_t datalength = (uint64_t)count * typesize;
  1984. if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
  1985. {
  1986. /* LibTIFF increments write address to an even offset, thus datalength
  1987. * written is also incremented. */
  1988. if (datalength & 1)
  1989. datalength++;
  1990. tif->tif_dir.td_dirdatasize_write += datalength;
  1991. }
  1992. (*ndir)++;
  1993. }
  1994. static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir,
  1995. TIFFDirEntry *dir)
  1996. {
  1997. static const char module[] = "TIFFWriteDirectoryTagColormap";
  1998. uint32_t m;
  1999. uint16_t *n;
  2000. int o;
  2001. m = (1 << tif->tif_dir.td_bitspersample);
  2002. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2003. {
  2004. EvaluateIFDdatasizeWrite(tif, 3 * m, sizeof(uint16_t), ndir);
  2005. return 1;
  2006. }
  2007. n = _TIFFmallocExt(tif, 3 * m * sizeof(uint16_t));
  2008. if (n == NULL)
  2009. {
  2010. TIFFErrorExtR(tif, module, "Out of memory");
  2011. return (0);
  2012. }
  2013. _TIFFmemcpy(&n[0], tif->tif_dir.td_colormap[0], m * sizeof(uint16_t));
  2014. _TIFFmemcpy(&n[m], tif->tif_dir.td_colormap[1], m * sizeof(uint16_t));
  2015. _TIFFmemcpy(&n[2 * m], tif->tif_dir.td_colormap[2], m * sizeof(uint16_t));
  2016. o = TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, TIFFTAG_COLORMAP,
  2017. 3 * m, n);
  2018. _TIFFfreeExt(tif, n);
  2019. return (o);
  2020. }
  2021. static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir,
  2022. TIFFDirEntry *dir)
  2023. {
  2024. static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
  2025. uint32_t m;
  2026. uint16_t n;
  2027. uint16_t *o;
  2028. int p;
  2029. /* TIFFTAG_TRANSFERFUNCTION expects (1 or 3) pointer to arrays with
  2030. * (1 << BitsPerSample) * uint16_t values.
  2031. */
  2032. m = (1 << tif->tif_dir.td_bitspersample);
  2033. /* clang-format off */
  2034. n = (tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples) > 1 ? 3 : 1;
  2035. /* clang-format on */
  2036. /* Check for proper number of transferfunctions */
  2037. for (int i = 0; i < n; i++)
  2038. {
  2039. if (tif->tif_dir.td_transferfunction[i] == NULL)
  2040. {
  2041. TIFFWarningExtR(tif, module,
  2042. "Too few TransferFunctions provided. Tag "
  2043. "not written to file");
  2044. return (1); /* Not an error; only tag is not written. */
  2045. }
  2046. }
  2047. /*
  2048. * Check if the table can be written as a single column,
  2049. * or if it must be written as 3 columns. Note that we
  2050. * write a 3-column tag if there are 2 samples/pixel and
  2051. * a single column of data won't suffice--hmm.
  2052. */
  2053. if (n == 3)
  2054. {
  2055. if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],
  2056. tif->tif_dir.td_transferfunction[2],
  2057. m * sizeof(uint16_t)) &&
  2058. !_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],
  2059. tif->tif_dir.td_transferfunction[1],
  2060. m * sizeof(uint16_t)))
  2061. n = 1;
  2062. }
  2063. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2064. {
  2065. EvaluateIFDdatasizeWrite(tif, n * m, 2, ndir);
  2066. return 1;
  2067. }
  2068. o = _TIFFmallocExt(tif, n * m * sizeof(uint16_t));
  2069. if (o == NULL)
  2070. {
  2071. TIFFErrorExtR(tif, module, "Out of memory");
  2072. return (0);
  2073. }
  2074. _TIFFmemcpy(&o[0], tif->tif_dir.td_transferfunction[0],
  2075. m * sizeof(uint16_t));
  2076. if (n > 1)
  2077. _TIFFmemcpy(&o[m], tif->tif_dir.td_transferfunction[1],
  2078. m * sizeof(uint16_t));
  2079. if (n > 2)
  2080. _TIFFmemcpy(&o[2 * m], tif->tif_dir.td_transferfunction[2],
  2081. m * sizeof(uint16_t));
  2082. p = TIFFWriteDirectoryTagCheckedShortArray(
  2083. tif, ndir, dir, TIFFTAG_TRANSFERFUNCTION, n * m, o);
  2084. _TIFFfreeExt(tif, o);
  2085. return (p);
  2086. }
  2087. static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir,
  2088. TIFFDirEntry *dir)
  2089. {
  2090. static const char module[] = "TIFFWriteDirectoryTagSubifd";
  2091. uint64_t m;
  2092. int n;
  2093. if (tif->tif_dir.td_nsubifd == 0)
  2094. return (1);
  2095. m = tif->tif_dataoff;
  2096. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2097. {
  2098. uint32_t *o;
  2099. uint64_t *pa;
  2100. uint32_t *pb;
  2101. uint16_t p;
  2102. o = _TIFFmallocExt(tif, tif->tif_dir.td_nsubifd * sizeof(uint32_t));
  2103. if (o == NULL)
  2104. {
  2105. TIFFErrorExtR(tif, module, "Out of memory");
  2106. return (0);
  2107. }
  2108. pa = tif->tif_dir.td_subifd;
  2109. pb = o;
  2110. for (p = 0; p < tif->tif_dir.td_nsubifd; p++)
  2111. {
  2112. assert(pa != 0);
  2113. /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which
  2114. * is illegal) */
  2115. if (*pa > 0xFFFFFFFFUL)
  2116. {
  2117. TIFFErrorExtR(tif, module, "Illegal value for SubIFD tag");
  2118. _TIFFfreeExt(tif, o);
  2119. return (0);
  2120. }
  2121. *pb++ = (uint32_t)(*pa++);
  2122. }
  2123. n = TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, TIFFTAG_SUBIFD,
  2124. tif->tif_dir.td_nsubifd, o);
  2125. _TIFFfreeExt(tif, o);
  2126. }
  2127. else
  2128. n = TIFFWriteDirectoryTagCheckedIfd8Array(
  2129. tif, ndir, dir, TIFFTAG_SUBIFD, tif->tif_dir.td_nsubifd,
  2130. tif->tif_dir.td_subifd);
  2131. if (dir == NULL)
  2132. /* Just have evaluated IFD data size and incremented ndir
  2133. * above in sub-functions. */
  2134. return (n);
  2135. if (!n)
  2136. return (0);
  2137. /*
  2138. * Total hack: if this directory includes a SubIFD
  2139. * tag then force the next <n> directories to be
  2140. * written as ``sub directories'' of this one. This
  2141. * is used to write things like thumbnails and
  2142. * image masks that one wants to keep out of the
  2143. * normal directory linkage access mechanism.
  2144. */
  2145. tif->tif_flags |= TIFF_INSUBIFD;
  2146. tif->tif_nsubifd = tif->tif_dir.td_nsubifd;
  2147. if (tif->tif_dir.td_nsubifd == 1)
  2148. tif->tif_subifdoff = 0;
  2149. else
  2150. tif->tif_subifdoff = m;
  2151. return (1);
  2152. }
  2153. static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir,
  2154. TIFFDirEntry *dir, uint16_t tag,
  2155. uint32_t count, char *value)
  2156. {
  2157. assert(sizeof(char) == 1);
  2158. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2159. {
  2160. EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
  2161. return 1;
  2162. }
  2163. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_ASCII, count,
  2164. count, value));
  2165. }
  2166. static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir,
  2167. TIFFDirEntry *dir,
  2168. uint16_t tag,
  2169. uint32_t count,
  2170. uint8_t *value)
  2171. {
  2172. assert(sizeof(uint8_t) == 1);
  2173. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2174. {
  2175. EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
  2176. return 1;
  2177. }
  2178. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_UNDEFINED,
  2179. count, count, value));
  2180. }
  2181. static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir,
  2182. TIFFDirEntry *dir,
  2183. uint16_t tag, uint32_t count,
  2184. uint8_t *value)
  2185. {
  2186. assert(sizeof(uint8_t) == 1);
  2187. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2188. {
  2189. EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
  2190. return 1;
  2191. }
  2192. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_BYTE, count,
  2193. count, value));
  2194. }
  2195. static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir,
  2196. TIFFDirEntry *dir,
  2197. uint16_t tag, uint32_t count,
  2198. int8_t *value)
  2199. {
  2200. assert(sizeof(int8_t) == 1);
  2201. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2202. {
  2203. EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
  2204. return 1;
  2205. }
  2206. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SBYTE, count,
  2207. count, value));
  2208. }
  2209. static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir,
  2210. TIFFDirEntry *dir, uint16_t tag,
  2211. uint16_t value)
  2212. {
  2213. uint16_t m;
  2214. assert(sizeof(uint16_t) == 2);
  2215. if (dir == NULL)
  2216. {
  2217. /* No additional data to IFD data size just increment ndir. */
  2218. (*ndir)++;
  2219. return 1;
  2220. }
  2221. m = value;
  2222. if (tif->tif_flags & TIFF_SWAB)
  2223. TIFFSwabShort(&m);
  2224. return (
  2225. TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, 1, 2, &m));
  2226. }
  2227. static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir,
  2228. TIFFDirEntry *dir,
  2229. uint16_t tag, uint32_t count,
  2230. uint16_t *value)
  2231. {
  2232. assert(count < 0x80000000);
  2233. assert(sizeof(uint16_t) == 2);
  2234. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2235. {
  2236. EvaluateIFDdatasizeWrite(tif, count, 2, ndir);
  2237. return 1;
  2238. }
  2239. if (tif->tif_flags & TIFF_SWAB)
  2240. TIFFSwabArrayOfShort(value, count);
  2241. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, count,
  2242. count * 2, value));
  2243. }
  2244. static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir,
  2245. TIFFDirEntry *dir,
  2246. uint16_t tag, uint32_t count,
  2247. int16_t *value)
  2248. {
  2249. assert(count < 0x80000000);
  2250. assert(sizeof(int16_t) == 2);
  2251. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2252. {
  2253. EvaluateIFDdatasizeWrite(tif, count, 2, ndir);
  2254. return 1;
  2255. }
  2256. if (tif->tif_flags & TIFF_SWAB)
  2257. TIFFSwabArrayOfShort((uint16_t *)value, count);
  2258. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SSHORT, count,
  2259. count * 2, value));
  2260. }
  2261. static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir,
  2262. TIFFDirEntry *dir, uint16_t tag,
  2263. uint32_t value)
  2264. {
  2265. uint32_t m;
  2266. assert(sizeof(uint32_t) == 4);
  2267. if (dir == NULL)
  2268. {
  2269. /* No additional data to IFD data size just increment ndir. */
  2270. (*ndir)++;
  2271. return 1;
  2272. }
  2273. m = value;
  2274. if (tif->tif_flags & TIFF_SWAB)
  2275. TIFFSwabLong(&m);
  2276. return (
  2277. TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, 1, 4, &m));
  2278. }
  2279. static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir,
  2280. TIFFDirEntry *dir,
  2281. uint16_t tag, uint32_t count,
  2282. uint32_t *value)
  2283. {
  2284. assert(count < 0x40000000);
  2285. assert(sizeof(uint32_t) == 4);
  2286. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2287. {
  2288. EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
  2289. return 1;
  2290. }
  2291. if (tif->tif_flags & TIFF_SWAB)
  2292. TIFFSwabArrayOfLong(value, count);
  2293. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, count,
  2294. count * 4, value));
  2295. }
  2296. static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir,
  2297. TIFFDirEntry *dir,
  2298. uint16_t tag, uint32_t count,
  2299. int32_t *value)
  2300. {
  2301. assert(count < 0x40000000);
  2302. assert(sizeof(int32_t) == 4);
  2303. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2304. {
  2305. EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
  2306. return 1;
  2307. }
  2308. if (tif->tif_flags & TIFF_SWAB)
  2309. TIFFSwabArrayOfLong((uint32_t *)value, count);
  2310. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG, count,
  2311. count * 4, value));
  2312. }
  2313. static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir,
  2314. TIFFDirEntry *dir,
  2315. uint16_t tag, uint32_t count,
  2316. uint64_t *value)
  2317. {
  2318. assert(count < 0x20000000);
  2319. assert(sizeof(uint64_t) == 8);
  2320. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2321. {
  2322. TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedLong8Array",
  2323. "LONG8 not allowed for ClassicTIFF");
  2324. return (0);
  2325. }
  2326. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2327. {
  2328. EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
  2329. return 1;
  2330. }
  2331. if (tif->tif_flags & TIFF_SWAB)
  2332. TIFFSwabArrayOfLong8(value, count);
  2333. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG8, count,
  2334. count * 8, value));
  2335. }
  2336. static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir,
  2337. TIFFDirEntry *dir,
  2338. uint16_t tag, uint32_t count,
  2339. int64_t *value)
  2340. {
  2341. assert(count < 0x20000000);
  2342. assert(sizeof(int64_t) == 8);
  2343. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2344. {
  2345. TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedSlong8Array",
  2346. "SLONG8 not allowed for ClassicTIFF");
  2347. return (0);
  2348. }
  2349. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2350. {
  2351. EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
  2352. return 1;
  2353. }
  2354. if (tif->tif_flags & TIFF_SWAB)
  2355. TIFFSwabArrayOfLong8((uint64_t *)value, count);
  2356. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG8, count,
  2357. count * 8, value));
  2358. }
  2359. static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir,
  2360. TIFFDirEntry *dir, uint16_t tag,
  2361. double value)
  2362. {
  2363. static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
  2364. uint32_t m[2];
  2365. assert(sizeof(uint32_t) == 4);
  2366. if (value < 0)
  2367. {
  2368. TIFFErrorExtR(tif, module, "Negative value is illegal");
  2369. return 0;
  2370. }
  2371. else if (value != value)
  2372. {
  2373. TIFFErrorExtR(tif, module, "Not-a-number value is illegal");
  2374. return 0;
  2375. }
  2376. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2377. {
  2378. tif->tif_dir.td_dirdatasize_write +=
  2379. (tif->tif_flags & TIFF_BIGTIFF) ? 0 : 0x8U;
  2380. (*ndir)++;
  2381. return 1;
  2382. }
  2383. DoubleToRational(value, &m[0], &m[1]);
  2384. if (tif->tif_flags & TIFF_SWAB)
  2385. {
  2386. TIFFSwabLong(&m[0]);
  2387. TIFFSwabLong(&m[1]);
  2388. }
  2389. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, 1, 8,
  2390. &m[0]));
  2391. }
  2392. static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir,
  2393. TIFFDirEntry *dir,
  2394. uint16_t tag,
  2395. uint32_t count,
  2396. float *value)
  2397. {
  2398. static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
  2399. uint32_t *m;
  2400. float *na;
  2401. uint32_t *nb;
  2402. uint32_t nc;
  2403. int o;
  2404. assert(sizeof(uint32_t) == 4);
  2405. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2406. {
  2407. EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir);
  2408. return 1;
  2409. }
  2410. m = _TIFFmallocExt(tif, count * 2 * sizeof(uint32_t));
  2411. if (m == NULL)
  2412. {
  2413. TIFFErrorExtR(tif, module, "Out of memory");
  2414. return (0);
  2415. }
  2416. for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
  2417. {
  2418. DoubleToRational(*na, &nb[0], &nb[1]);
  2419. }
  2420. if (tif->tif_flags & TIFF_SWAB)
  2421. TIFFSwabArrayOfLong(m, count * 2);
  2422. o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count,
  2423. count * 8, &m[0]);
  2424. _TIFFfreeExt(tif, m);
  2425. return (o);
  2426. }
  2427. static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir,
  2428. TIFFDirEntry *dir,
  2429. uint16_t tag,
  2430. uint32_t count,
  2431. float *value)
  2432. {
  2433. static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
  2434. int32_t *m;
  2435. float *na;
  2436. int32_t *nb;
  2437. uint32_t nc;
  2438. int o;
  2439. assert(sizeof(int32_t) == 4);
  2440. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2441. {
  2442. EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir);
  2443. return 1;
  2444. }
  2445. m = _TIFFmallocExt(tif, count * 2 * sizeof(int32_t));
  2446. if (m == NULL)
  2447. {
  2448. TIFFErrorExtR(tif, module, "Out of memory");
  2449. return (0);
  2450. }
  2451. for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
  2452. {
  2453. DoubleToSrational(*na, &nb[0], &nb[1]);
  2454. }
  2455. if (tif->tif_flags & TIFF_SWAB)
  2456. TIFFSwabArrayOfLong((uint32_t *)m, count * 2);
  2457. o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count,
  2458. count * 8, &m[0]);
  2459. _TIFFfreeExt(tif, m);
  2460. return (o);
  2461. }
  2462. /*-- Rational2Double: additional write functions for double arrays */
  2463. static int
  2464. TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF *tif, uint32_t *ndir,
  2465. TIFFDirEntry *dir, uint16_t tag,
  2466. uint32_t count, double *value)
  2467. {
  2468. static const char module[] =
  2469. "TIFFWriteDirectoryTagCheckedRationalDoubleArray";
  2470. uint32_t *m;
  2471. double *na;
  2472. uint32_t *nb;
  2473. uint32_t nc;
  2474. int o;
  2475. assert(sizeof(uint32_t) == 4);
  2476. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2477. {
  2478. EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir);
  2479. return 1;
  2480. }
  2481. m = _TIFFmallocExt(tif, count * 2 * sizeof(uint32_t));
  2482. if (m == NULL)
  2483. {
  2484. TIFFErrorExtR(tif, module, "Out of memory");
  2485. return (0);
  2486. }
  2487. for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
  2488. {
  2489. DoubleToRational(*na, &nb[0], &nb[1]);
  2490. }
  2491. if (tif->tif_flags & TIFF_SWAB)
  2492. TIFFSwabArrayOfLong(m, count * 2);
  2493. o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count,
  2494. count * 8, &m[0]);
  2495. _TIFFfreeExt(tif, m);
  2496. return (o);
  2497. } /*-- TIFFWriteDirectoryTagCheckedRationalDoubleArray() ------- */
  2498. static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray(
  2499. TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count,
  2500. double *value)
  2501. {
  2502. static const char module[] =
  2503. "TIFFWriteDirectoryTagCheckedSrationalDoubleArray";
  2504. int32_t *m;
  2505. double *na;
  2506. int32_t *nb;
  2507. uint32_t nc;
  2508. int o;
  2509. assert(sizeof(int32_t) == 4);
  2510. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2511. {
  2512. EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir);
  2513. return 1;
  2514. }
  2515. m = _TIFFmallocExt(tif, count * 2 * sizeof(int32_t));
  2516. if (m == NULL)
  2517. {
  2518. TIFFErrorExtR(tif, module, "Out of memory");
  2519. return (0);
  2520. }
  2521. for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
  2522. {
  2523. DoubleToSrational(*na, &nb[0], &nb[1]);
  2524. }
  2525. if (tif->tif_flags & TIFF_SWAB)
  2526. TIFFSwabArrayOfLong((uint32_t *)m, count * 2);
  2527. o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count,
  2528. count * 8, &m[0]);
  2529. _TIFFfreeExt(tif, m);
  2530. return (o);
  2531. } /*--- TIFFWriteDirectoryTagCheckedSrationalDoubleArray() -------- */
  2532. /** ----- Rational2Double: Double To Rational Conversion
  2533. ----------------------------------------------------------
  2534. * There is a mathematical theorem to convert real numbers into a rational
  2535. (integer fraction) number.
  2536. * This is called "continuous fraction" which uses the Euclidean algorithm to
  2537. find the greatest common divisor (GCD).
  2538. * (ref. e.g. https://de.wikipedia.org/wiki/Kettenbruch or
  2539. https://en.wikipedia.org/wiki/Continued_fraction
  2540. * https://en.wikipedia.org/wiki/Euclidean_algorithm)
  2541. * The following functions implement the
  2542. * - ToRationalEuclideanGCD() auxiliary function which mainly
  2543. implements euclidean GCD
  2544. * - DoubleToRational() conversion function for un-signed
  2545. rationals
  2546. * - DoubleToSrational() conversion function for signed rationals
  2547. ------------------------------------------------------------------------------------------------------------------*/
  2548. /**---- ToRationalEuclideanGCD() -----------------------------------------
  2549. * Calculates the rational fractional of a double input value
  2550. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2551. ------------------------------------------------------------------------*/
  2552. static void ToRationalEuclideanGCD(double value, int blnUseSignedRange,
  2553. int blnUseSmallRange, uint64_t *ullNum,
  2554. uint64_t *ullDenom)
  2555. {
  2556. /* Internally, the integer variables can be bigger than the external ones,
  2557. * as long as the result will fit into the external variable size.
  2558. */
  2559. uint64_t numSum[3] = {0, 1, 0}, denomSum[3] = {1, 0, 0};
  2560. uint64_t aux, bigNum, bigDenom;
  2561. uint64_t returnLimit;
  2562. int i;
  2563. uint64_t nMax;
  2564. double fMax;
  2565. unsigned long maxDenom;
  2566. /*-- nMax and fMax defines the initial accuracy of the starting fractional,
  2567. * or better, the highest used integer numbers used within the starting
  2568. * fractional (bigNum/bigDenom). There are two approaches, which can
  2569. * accidentally lead to different accuracies just depending on the value.
  2570. * Therefore, blnUseSmallRange steers this behavior.
  2571. * For long long nMax = ((9223372036854775807-1)/2); for long nMax =
  2572. * ((2147483647-1)/2);
  2573. */
  2574. if (blnUseSmallRange)
  2575. {
  2576. nMax = (uint64_t)((2147483647 - 1) / 2); /* for ULONG range */
  2577. }
  2578. else
  2579. {
  2580. nMax = ((9223372036854775807 - 1) / 2); /* for ULLONG range */
  2581. }
  2582. fMax = (double)nMax;
  2583. /*-- For the Euclidean GCD define the denominator range, so that it stays
  2584. * within size of unsigned long variables. maxDenom should be LONG_MAX for
  2585. * negative values and ULONG_MAX for positive ones. Also the final returned
  2586. * value of ullNum and ullDenom is limited according to signed- or
  2587. * unsigned-range.
  2588. */
  2589. if (blnUseSignedRange)
  2590. {
  2591. maxDenom = 2147483647UL; /*LONG_MAX = 0x7FFFFFFFUL*/
  2592. returnLimit = maxDenom;
  2593. }
  2594. else
  2595. {
  2596. maxDenom = 0xFFFFFFFFUL; /*ULONG_MAX = 0xFFFFFFFFUL*/
  2597. returnLimit = maxDenom;
  2598. }
  2599. /*-- First generate a rational fraction (bigNum/bigDenom) which represents
  2600. *the value as a rational number with the highest accuracy. Therefore,
  2601. *uint64_t (uint64_t) is needed. This rational fraction is then reduced
  2602. *using the Euclidean algorithm to find the greatest common divisor (GCD).
  2603. * bigNum = big numinator of value without fraction (or cut residual
  2604. *fraction) bigDenom = big denominator of value
  2605. *-- Break-criteria so that uint64_t cast to "bigNum" introduces no error
  2606. *and bigDenom has no overflow, and stop with enlargement of fraction when
  2607. *the double-value of it reaches an integer number without fractional part.
  2608. */
  2609. bigDenom = 1;
  2610. while ((value != floor(value)) && (value < fMax) && (bigDenom < nMax))
  2611. {
  2612. bigDenom <<= 1;
  2613. value *= 2;
  2614. }
  2615. bigNum = (uint64_t)value;
  2616. /*-- Start Euclidean algorithm to find the greatest common divisor (GCD) --
  2617. */
  2618. #define MAX_ITERATIONS 64
  2619. for (i = 0; i < MAX_ITERATIONS; i++)
  2620. {
  2621. uint64_t val;
  2622. /* if bigDenom is not zero, calculate integer part of fraction. */
  2623. if (bigDenom == 0)
  2624. {
  2625. break;
  2626. }
  2627. val = bigNum / bigDenom;
  2628. /* Set bigDenom to reminder of bigNum/bigDenom and bigNum to previous
  2629. * denominator bigDenom. */
  2630. aux = bigNum;
  2631. bigNum = bigDenom;
  2632. bigDenom = aux % bigDenom;
  2633. /* calculate next denominator and check for its given maximum */
  2634. aux = val;
  2635. if (denomSum[1] * val + denomSum[0] >= maxDenom)
  2636. {
  2637. aux = (maxDenom - denomSum[0]) / denomSum[1];
  2638. if (aux * 2 >= val || denomSum[1] >= maxDenom)
  2639. i = (MAX_ITERATIONS +
  2640. 1); /* exit but execute rest of for-loop */
  2641. else
  2642. break;
  2643. }
  2644. /* calculate next numerator to numSum2 and save previous one to numSum0;
  2645. * numSum1 just copy of numSum2. */
  2646. numSum[2] = aux * numSum[1] + numSum[0];
  2647. numSum[0] = numSum[1];
  2648. numSum[1] = numSum[2];
  2649. /* calculate next denominator to denomSum2 and save previous one to
  2650. * denomSum0; denomSum1 just copy of denomSum2. */
  2651. denomSum[2] = aux * denomSum[1] + denomSum[0];
  2652. denomSum[0] = denomSum[1];
  2653. denomSum[1] = denomSum[2];
  2654. }
  2655. /*-- Check and adapt for final variable size and return values; reduces
  2656. * internal accuracy; denominator is kept in ULONG-range with maxDenom -- */
  2657. while (numSum[1] > returnLimit || denomSum[1] > returnLimit)
  2658. {
  2659. numSum[1] = numSum[1] / 2;
  2660. denomSum[1] = denomSum[1] / 2;
  2661. }
  2662. /* return values */
  2663. *ullNum = numSum[1];
  2664. *ullDenom = denomSum[1];
  2665. } /*-- ToRationalEuclideanGCD() -------------- */
  2666. /**---- DoubleToRational() -----------------------------------------------
  2667. * Calculates the rational fractional of a double input value
  2668. * for UN-SIGNED rationals,
  2669. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2670. ------------------------------------------------------------------------*/
  2671. static void DoubleToRational(double value, uint32_t *num, uint32_t *denom)
  2672. {
  2673. /*---- UN-SIGNED RATIONAL ---- */
  2674. double dblDiff, dblDiff2;
  2675. uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
  2676. /*-- Check for negative values. If so it is an error. */
  2677. /* Test written that way to catch NaN */
  2678. if (!(value >= 0))
  2679. {
  2680. *num = *denom = 0;
  2681. TIFFErrorExt(0, "TIFFLib: DoubleToRational()",
  2682. " Negative Value for Unsigned Rational given.");
  2683. return;
  2684. }
  2685. /*-- Check for too big numbers (> ULONG_MAX) -- */
  2686. if (value > 0xFFFFFFFFUL)
  2687. {
  2688. *num = 0xFFFFFFFFU;
  2689. *denom = 0;
  2690. return;
  2691. }
  2692. /*-- Check for easy integer numbers -- */
  2693. if (value == (uint32_t)(value))
  2694. {
  2695. *num = (uint32_t)value;
  2696. *denom = 1;
  2697. return;
  2698. }
  2699. /*-- Check for too small numbers for "unsigned long" type rationals -- */
  2700. if (value < 1.0 / (double)0xFFFFFFFFUL)
  2701. {
  2702. *num = 0;
  2703. *denom = 0xFFFFFFFFU;
  2704. return;
  2705. }
  2706. /*-- There are two approaches using the Euclidean algorithm,
  2707. * which can accidentally lead to different accuracies just depending on
  2708. * the value. Try both and define which one was better.
  2709. */
  2710. ToRationalEuclideanGCD(value, FALSE, FALSE, &ullNum, &ullDenom);
  2711. ToRationalEuclideanGCD(value, FALSE, TRUE, &ullNum2, &ullDenom2);
  2712. /*-- Double-Check, that returned values fit into ULONG :*/
  2713. if (ullNum > 0xFFFFFFFFUL || ullDenom > 0xFFFFFFFFUL ||
  2714. ullNum2 > 0xFFFFFFFFUL || ullDenom2 > 0xFFFFFFFFUL)
  2715. {
  2716. TIFFErrorExt(0, "TIFFLib: DoubleToRational()",
  2717. " Num or Denom exceeds ULONG: val=%14.6f, num=%12" PRIu64
  2718. ", denom=%12" PRIu64 " | num2=%12" PRIu64
  2719. ", denom2=%12" PRIu64 "",
  2720. value, ullNum, ullDenom, ullNum2, ullDenom2);
  2721. assert(0);
  2722. }
  2723. /* Check, which one has higher accuracy and take that. */
  2724. dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
  2725. dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
  2726. if (dblDiff < dblDiff2)
  2727. {
  2728. *num = (uint32_t)ullNum;
  2729. *denom = (uint32_t)ullDenom;
  2730. }
  2731. else
  2732. {
  2733. *num = (uint32_t)ullNum2;
  2734. *denom = (uint32_t)ullDenom2;
  2735. }
  2736. } /*-- DoubleToRational() -------------- */
  2737. /**---- DoubleToSrational() -----------------------------------------------
  2738. * Calculates the rational fractional of a double input value
  2739. * for SIGNED rationals,
  2740. * using the Euclidean algorithm to find the greatest common divisor (GCD)
  2741. ------------------------------------------------------------------------*/
  2742. static void DoubleToSrational(double value, int32_t *num, int32_t *denom)
  2743. {
  2744. /*---- SIGNED RATIONAL ----*/
  2745. int neg = 1;
  2746. double dblDiff, dblDiff2;
  2747. uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
  2748. /*-- Check for negative values and use then the positive one for internal
  2749. * calculations, but take the sign into account before returning. */
  2750. if (value < 0)
  2751. {
  2752. neg = -1;
  2753. value = -value;
  2754. }
  2755. /*-- Check for too big numbers (> LONG_MAX) -- */
  2756. if (value > 0x7FFFFFFFL)
  2757. {
  2758. *num = 0x7FFFFFFFL;
  2759. *denom = 0;
  2760. return;
  2761. }
  2762. /*-- Check for easy numbers -- */
  2763. if (value == (int32_t)(value))
  2764. {
  2765. *num = (int32_t)(neg * value);
  2766. *denom = 1;
  2767. return;
  2768. }
  2769. /*-- Check for too small numbers for "long" type rationals -- */
  2770. if (value < 1.0 / (double)0x7FFFFFFFL)
  2771. {
  2772. *num = 0;
  2773. *denom = 0x7FFFFFFFL;
  2774. return;
  2775. }
  2776. /*-- There are two approaches using the Euclidean algorithm,
  2777. * which can accidentally lead to different accuracies just depending on
  2778. * the value. Try both and define which one was better. Furthermore, set
  2779. * behavior of ToRationalEuclideanGCD() to the range of signed-long.
  2780. */
  2781. ToRationalEuclideanGCD(value, TRUE, FALSE, &ullNum, &ullDenom);
  2782. ToRationalEuclideanGCD(value, TRUE, TRUE, &ullNum2, &ullDenom2);
  2783. /*-- Double-Check, that returned values fit into LONG :*/
  2784. if (ullNum > 0x7FFFFFFFL || ullDenom > 0x7FFFFFFFL ||
  2785. ullNum2 > 0x7FFFFFFFL || ullDenom2 > 0x7FFFFFFFL)
  2786. {
  2787. TIFFErrorExt(0, "TIFFLib: DoubleToSrational()",
  2788. " Num or Denom exceeds LONG: val=%14.6f, num=%12" PRIu64
  2789. ", denom=%12" PRIu64 " | num2=%12" PRIu64
  2790. ", denom2=%12" PRIu64 "",
  2791. neg * value, ullNum, ullDenom, ullNum2, ullDenom2);
  2792. assert(0);
  2793. }
  2794. /* Check, which one has higher accuracy and take that. */
  2795. dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
  2796. dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
  2797. if (dblDiff < dblDiff2)
  2798. {
  2799. *num = (int32_t)(neg * (long)ullNum);
  2800. *denom = (int32_t)ullDenom;
  2801. }
  2802. else
  2803. {
  2804. *num = (int32_t)(neg * (long)ullNum2);
  2805. *denom = (int32_t)ullDenom2;
  2806. }
  2807. } /*-- DoubleToSrational() --------------*/
  2808. static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir,
  2809. TIFFDirEntry *dir,
  2810. uint16_t tag, uint32_t count,
  2811. float *value)
  2812. {
  2813. assert(count < 0x40000000);
  2814. assert(sizeof(float) == 4);
  2815. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2816. {
  2817. EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
  2818. return 1;
  2819. }
  2820. TIFFCvtNativeToIEEEFloat(tif, count, value);
  2821. if (tif->tif_flags & TIFF_SWAB)
  2822. TIFFSwabArrayOfFloat(value, count);
  2823. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_FLOAT, count,
  2824. count * 4, value));
  2825. }
  2826. static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir,
  2827. TIFFDirEntry *dir,
  2828. uint16_t tag, uint32_t count,
  2829. double *value)
  2830. {
  2831. assert(count < 0x20000000);
  2832. assert(sizeof(double) == 8);
  2833. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2834. {
  2835. EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
  2836. return 1;
  2837. }
  2838. TIFFCvtNativeToIEEEDouble(tif, count, value);
  2839. if (tif->tif_flags & TIFF_SWAB)
  2840. TIFFSwabArrayOfDouble(value, count);
  2841. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_DOUBLE, count,
  2842. count * 8, value));
  2843. }
  2844. static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir,
  2845. TIFFDirEntry *dir, uint16_t tag,
  2846. uint32_t count, uint32_t *value)
  2847. {
  2848. assert(count < 0x40000000);
  2849. assert(sizeof(uint32_t) == 4);
  2850. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2851. {
  2852. EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
  2853. return 1;
  2854. }
  2855. if (tif->tif_flags & TIFF_SWAB)
  2856. TIFFSwabArrayOfLong(value, count);
  2857. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD, count,
  2858. count * 4, value));
  2859. }
  2860. static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir,
  2861. TIFFDirEntry *dir,
  2862. uint16_t tag, uint32_t count,
  2863. uint64_t *value)
  2864. {
  2865. assert(count < 0x20000000);
  2866. assert(sizeof(uint64_t) == 8);
  2867. assert(tif->tif_flags & TIFF_BIGTIFF);
  2868. if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
  2869. {
  2870. EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
  2871. return 1;
  2872. }
  2873. if (tif->tif_flags & TIFF_SWAB)
  2874. TIFFSwabArrayOfLong8(value, count);
  2875. return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD8, count,
  2876. count * 8, value));
  2877. }
  2878. static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir,
  2879. TIFFDirEntry *dir, uint16_t tag,
  2880. uint16_t datatype, uint32_t count,
  2881. uint32_t datalength, void *data)
  2882. {
  2883. static const char module[] = "TIFFWriteDirectoryTagData";
  2884. uint32_t m;
  2885. m = 0;
  2886. while (m < (*ndir))
  2887. {
  2888. assert(dir[m].tdir_tag != tag);
  2889. if (dir[m].tdir_tag > tag)
  2890. break;
  2891. m++;
  2892. }
  2893. if (m < (*ndir))
  2894. {
  2895. uint32_t n;
  2896. for (n = *ndir; n > m; n--)
  2897. dir[n] = dir[n - 1];
  2898. }
  2899. dir[m].tdir_tag = tag;
  2900. dir[m].tdir_type = datatype;
  2901. dir[m].tdir_count = count;
  2902. dir[m].tdir_offset.toff_long8 = 0;
  2903. if (datalength <= ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
  2904. {
  2905. if (data && datalength)
  2906. {
  2907. _TIFFmemcpy(&dir[m].tdir_offset, data, datalength);
  2908. }
  2909. }
  2910. else
  2911. {
  2912. uint64_t na, nb;
  2913. na = tif->tif_dataoff;
  2914. nb = na + datalength;
  2915. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2916. nb = (uint32_t)nb;
  2917. if ((nb < na) || (nb < datalength))
  2918. {
  2919. TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
  2920. return (0);
  2921. }
  2922. if (!SeekOK(tif, na))
  2923. {
  2924. TIFFErrorExtR(tif, module, "IO error writing tag data");
  2925. return (0);
  2926. }
  2927. if (datalength >= 0x80000000UL)
  2928. {
  2929. TIFFErrorExtR(tif, module,
  2930. "libtiff does not allow writing more than 2147483647 "
  2931. "bytes in a tag");
  2932. return (0);
  2933. }
  2934. if (!WriteOK(tif, data, (tmsize_t)datalength))
  2935. {
  2936. TIFFErrorExtR(tif, module, "IO error writing tag data");
  2937. return (0);
  2938. }
  2939. tif->tif_dataoff = nb;
  2940. if (tif->tif_dataoff & 1)
  2941. tif->tif_dataoff++;
  2942. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2943. {
  2944. uint32_t o;
  2945. o = (uint32_t)na;
  2946. if (tif->tif_flags & TIFF_SWAB)
  2947. TIFFSwabLong(&o);
  2948. _TIFFmemcpy(&dir[m].tdir_offset, &o, 4);
  2949. }
  2950. else
  2951. {
  2952. dir[m].tdir_offset.toff_long8 = na;
  2953. if (tif->tif_flags & TIFF_SWAB)
  2954. TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
  2955. }
  2956. }
  2957. (*ndir)++;
  2958. return (1);
  2959. }
  2960. /*
  2961. * Link the current directory into the directory chain for the file.
  2962. */
  2963. static int TIFFLinkDirectory(TIFF *tif)
  2964. {
  2965. static const char module[] = "TIFFLinkDirectory";
  2966. tif->tif_diroff = (TIFFSeekFile(tif, 0, SEEK_END) + 1) & (~((toff_t)1));
  2967. /*
  2968. * Handle SubIFDs
  2969. */
  2970. if (tif->tif_flags & TIFF_INSUBIFD)
  2971. {
  2972. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2973. {
  2974. uint32_t m;
  2975. m = (uint32_t)tif->tif_diroff;
  2976. if (tif->tif_flags & TIFF_SWAB)
  2977. TIFFSwabLong(&m);
  2978. (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  2979. if (!WriteOK(tif, &m, 4))
  2980. {
  2981. TIFFErrorExtR(tif, module,
  2982. "Error writing SubIFD directory link");
  2983. return (0);
  2984. }
  2985. /*
  2986. * Advance to the next SubIFD or, if this is
  2987. * the last one configured, reverting back to the
  2988. * normal directory linkage is done in TIFFWriteDirectorySec()
  2989. * by tif->tif_flags &= ~TIFF_INSUBIFD;.
  2990. */
  2991. if (--tif->tif_nsubifd)
  2992. tif->tif_subifdoff += 4;
  2993. return (1);
  2994. }
  2995. else
  2996. {
  2997. uint64_t m;
  2998. m = tif->tif_diroff;
  2999. if (tif->tif_flags & TIFF_SWAB)
  3000. TIFFSwabLong8(&m);
  3001. (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  3002. if (!WriteOK(tif, &m, 8))
  3003. {
  3004. TIFFErrorExtR(tif, module,
  3005. "Error writing SubIFD directory link");
  3006. return (0);
  3007. }
  3008. /*
  3009. * Advance to the next SubIFD or, if this is
  3010. * the last one configured, reverting back to the
  3011. * normal directory linkage is done in TIFFWriteDirectorySec()
  3012. * by tif->tif_flags &= ~TIFF_INSUBIFD;.
  3013. */
  3014. if (--tif->tif_nsubifd)
  3015. tif->tif_subifdoff += 8;
  3016. return (1);
  3017. }
  3018. }
  3019. /*
  3020. * Handle main-IFDs
  3021. */
  3022. tdir_t ndir = 1; /* count current number of main-IFDs */
  3023. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3024. {
  3025. uint32_t m;
  3026. uint32_t nextdir;
  3027. m = (uint32_t)(tif->tif_diroff);
  3028. if (tif->tif_flags & TIFF_SWAB)
  3029. TIFFSwabLong(&m);
  3030. if (tif->tif_header.classic.tiff_diroff == 0)
  3031. {
  3032. /*
  3033. * First directory, overwrite offset in header.
  3034. */
  3035. tif->tif_header.classic.tiff_diroff = (uint32_t)tif->tif_diroff;
  3036. tif->tif_lastdiroff = tif->tif_diroff;
  3037. (void)TIFFSeekFile(tif, 4, SEEK_SET);
  3038. if (!WriteOK(tif, &m, 4))
  3039. {
  3040. TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header");
  3041. return (0);
  3042. }
  3043. if (!tif->tif_dir.td_iswrittentofile)
  3044. tif->tif_curdircount = 0;
  3045. return (1);
  3046. }
  3047. /*
  3048. * Not the first directory, search to the last and append.
  3049. */
  3050. tdir_t dirn = 0;
  3051. if (tif->tif_lastdiroff != 0 &&
  3052. _TIFFGetDirNumberFromOffset(tif, tif->tif_lastdiroff, &dirn))
  3053. {
  3054. /* Start searching from the lastely written IFD. Thus get its IFD
  3055. * number. */
  3056. nextdir = (uint32_t)tif->tif_lastdiroff;
  3057. ndir = dirn + 1;
  3058. }
  3059. else
  3060. {
  3061. nextdir = tif->tif_header.classic.tiff_diroff;
  3062. ndir = 1; /* start searching from the first IFD */
  3063. }
  3064. while (1)
  3065. {
  3066. uint16_t dircount;
  3067. uint32_t nextnextdir;
  3068. if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2))
  3069. {
  3070. TIFFErrorExtR(tif, module, "Error fetching directory count");
  3071. return (0);
  3072. }
  3073. if (tif->tif_flags & TIFF_SWAB)
  3074. TIFFSwabShort(&dircount);
  3075. (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET);
  3076. if (!ReadOK(tif, &nextnextdir, 4))
  3077. {
  3078. TIFFErrorExtR(tif, module, "Error fetching directory link");
  3079. return (0);
  3080. }
  3081. if (tif->tif_flags & TIFF_SWAB)
  3082. TIFFSwabLong(&nextnextdir);
  3083. if (nextnextdir == 0)
  3084. {
  3085. (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12, SEEK_SET);
  3086. if (!WriteOK(tif, &m, 4))
  3087. {
  3088. TIFFErrorExtR(tif, module, "Error writing directory link");
  3089. return (0);
  3090. }
  3091. tif->tif_lastdiroff = tif->tif_diroff;
  3092. break;
  3093. }
  3094. nextdir = nextnextdir;
  3095. ndir++;
  3096. }
  3097. }
  3098. else
  3099. {
  3100. /*- BigTIFF -*/
  3101. uint64_t m;
  3102. uint64_t nextdir;
  3103. m = tif->tif_diroff;
  3104. if (tif->tif_flags & TIFF_SWAB)
  3105. TIFFSwabLong8(&m);
  3106. if (tif->tif_header.big.tiff_diroff == 0)
  3107. {
  3108. /*
  3109. * First directory, overwrite offset in header.
  3110. */
  3111. tif->tif_header.big.tiff_diroff = tif->tif_diroff;
  3112. tif->tif_lastdiroff = tif->tif_diroff;
  3113. (void)TIFFSeekFile(tif, 8, SEEK_SET);
  3114. if (!WriteOK(tif, &m, 8))
  3115. {
  3116. TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header");
  3117. return (0);
  3118. }
  3119. if (!tif->tif_dir.td_iswrittentofile)
  3120. tif->tif_curdircount = 0;
  3121. return (1);
  3122. }
  3123. /*
  3124. * Not the first directory, search to the last and append.
  3125. */
  3126. tdir_t dirn = 0;
  3127. if (tif->tif_lastdiroff != 0 &&
  3128. _TIFFGetDirNumberFromOffset(tif, tif->tif_lastdiroff, &dirn))
  3129. {
  3130. /* Start searching from the lastely written IFD. Thus get its IFD
  3131. * number. */
  3132. nextdir = tif->tif_lastdiroff;
  3133. ndir = dirn + 1;
  3134. }
  3135. else
  3136. {
  3137. nextdir = tif->tif_header.big.tiff_diroff;
  3138. ndir = 1; /* start searching from the first IFD */
  3139. }
  3140. while (1)
  3141. {
  3142. uint64_t dircount64;
  3143. uint16_t dircount;
  3144. uint64_t nextnextdir;
  3145. if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8))
  3146. {
  3147. TIFFErrorExtR(tif, module, "Error fetching directory count");
  3148. return (0);
  3149. }
  3150. if (tif->tif_flags & TIFF_SWAB)
  3151. TIFFSwabLong8(&dircount64);
  3152. if (dircount64 > 0xFFFF)
  3153. {
  3154. TIFFErrorExtR(tif, module,
  3155. "Sanity check on tag count failed, "
  3156. "likely corrupt TIFF");
  3157. return (0);
  3158. }
  3159. dircount = (uint16_t)dircount64;
  3160. (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET);
  3161. if (!ReadOK(tif, &nextnextdir, 8))
  3162. {
  3163. TIFFErrorExtR(tif, module, "Error fetching directory link");
  3164. return (0);
  3165. }
  3166. if (tif->tif_flags & TIFF_SWAB)
  3167. TIFFSwabLong8(&nextnextdir);
  3168. if (nextnextdir == 0)
  3169. {
  3170. (void)TIFFSeekFile(tif, nextdir + 8 + dircount * 20, SEEK_SET);
  3171. if (!WriteOK(tif, &m, 8))
  3172. {
  3173. TIFFErrorExtR(tif, module, "Error writing directory link");
  3174. return (0);
  3175. }
  3176. tif->tif_lastdiroff = tif->tif_diroff;
  3177. break;
  3178. }
  3179. nextdir = nextnextdir;
  3180. ndir++;
  3181. }
  3182. }
  3183. /* Offset of next IFD is written to file.
  3184. * Update number of main-IFDs in file.
  3185. * However, tif_curdircount shall count only newly written main-IFDs with
  3186. * entries and not only number of linked offsets! Thus, tif_curdircount is
  3187. * incremented at the end of TIFFWriteDirectorySec().
  3188. * TIFF_NON_EXISTENT_DIR_NUMBER means 'dont know number of IFDs'
  3189. * 0 means 'empty file opened for writing, but no IFD written yet' */
  3190. if (!tif->tif_dir.td_iswrittentofile && !(tif->tif_flags & TIFF_INSUBIFD))
  3191. {
  3192. tif->tif_curdircount = ndir;
  3193. }
  3194. return (1);
  3195. }
  3196. /************************************************************************/
  3197. /* TIFFRewriteField() */
  3198. /* */
  3199. /* Rewrite a field in the directory on disk without regard to */
  3200. /* updating the TIFF directory structure in memory. Currently */
  3201. /* only supported for field that already exist in the on-disk */
  3202. /* directory. Mainly used for updating stripoffset / */
  3203. /* stripbytecount values after the directory is already on */
  3204. /* disk. */
  3205. /* */
  3206. /* Returns zero on failure, and one on success. */
  3207. /************************************************************************/
  3208. int _TIFFRewriteField(TIFF *tif, uint16_t tag, TIFFDataType in_datatype,
  3209. tmsize_t count, void *data)
  3210. {
  3211. static const char module[] = "TIFFResetField";
  3212. /* const TIFFField* fip = NULL; */
  3213. uint16_t dircount;
  3214. tmsize_t dirsize;
  3215. uint8_t direntry_raw[20];
  3216. uint16_t entry_tag = 0;
  3217. uint16_t entry_type = 0;
  3218. uint64_t entry_count = 0;
  3219. uint64_t entry_offset = 0;
  3220. int value_in_entry = 0;
  3221. uint64_t read_offset;
  3222. uint8_t *buf_to_write = NULL;
  3223. TIFFDataType datatype;
  3224. /* -------------------------------------------------------------------- */
  3225. /* Find field definition. */
  3226. /* -------------------------------------------------------------------- */
  3227. /*fip =*/TIFFFindField(tif, tag, TIFF_ANY);
  3228. /* -------------------------------------------------------------------- */
  3229. /* Do some checking this is a straight forward case. */
  3230. /* -------------------------------------------------------------------- */
  3231. if (isMapped(tif))
  3232. {
  3233. TIFFErrorExtR(tif, module,
  3234. "Memory mapped files not currently supported for "
  3235. "this operation.");
  3236. return 0;
  3237. }
  3238. if (tif->tif_diroff == 0)
  3239. {
  3240. TIFFErrorExtR(
  3241. tif, module,
  3242. "Attempt to reset field on directory not already on disk.");
  3243. return 0;
  3244. }
  3245. /* -------------------------------------------------------------------- */
  3246. /* Read the directory entry count. */
  3247. /* -------------------------------------------------------------------- */
  3248. if (!SeekOK(tif, tif->tif_diroff))
  3249. {
  3250. TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory",
  3251. tif->tif_name);
  3252. return 0;
  3253. }
  3254. read_offset = tif->tif_diroff;
  3255. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3256. {
  3257. if (!ReadOK(tif, &dircount, sizeof(uint16_t)))
  3258. {
  3259. TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count",
  3260. tif->tif_name);
  3261. return 0;
  3262. }
  3263. if (tif->tif_flags & TIFF_SWAB)
  3264. TIFFSwabShort(&dircount);
  3265. dirsize = 12;
  3266. read_offset += 2;
  3267. }
  3268. else
  3269. {
  3270. uint64_t dircount64;
  3271. if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
  3272. {
  3273. TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count",
  3274. tif->tif_name);
  3275. return 0;
  3276. }
  3277. if (tif->tif_flags & TIFF_SWAB)
  3278. TIFFSwabLong8(&dircount64);
  3279. dircount = (uint16_t)dircount64;
  3280. dirsize = 20;
  3281. read_offset += 8;
  3282. }
  3283. /* -------------------------------------------------------------------- */
  3284. /* Read through directory to find target tag. */
  3285. /* -------------------------------------------------------------------- */
  3286. while (dircount > 0)
  3287. {
  3288. if (!ReadOK(tif, direntry_raw, dirsize))
  3289. {
  3290. TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory entry.",
  3291. tif->tif_name);
  3292. return 0;
  3293. }
  3294. memcpy(&entry_tag, direntry_raw + 0, sizeof(uint16_t));
  3295. if (tif->tif_flags & TIFF_SWAB)
  3296. TIFFSwabShort(&entry_tag);
  3297. if (entry_tag == tag)
  3298. break;
  3299. read_offset += dirsize;
  3300. }
  3301. if (entry_tag != tag)
  3302. {
  3303. TIFFErrorExtR(tif, module, "%s: Could not find tag %" PRIu16 ".",
  3304. tif->tif_name, tag);
  3305. return 0;
  3306. }
  3307. /* -------------------------------------------------------------------- */
  3308. /* Extract the type, count and offset for this entry. */
  3309. /* -------------------------------------------------------------------- */
  3310. memcpy(&entry_type, direntry_raw + 2, sizeof(uint16_t));
  3311. if (tif->tif_flags & TIFF_SWAB)
  3312. TIFFSwabShort(&entry_type);
  3313. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3314. {
  3315. uint32_t value;
  3316. memcpy(&value, direntry_raw + 4, sizeof(uint32_t));
  3317. if (tif->tif_flags & TIFF_SWAB)
  3318. TIFFSwabLong(&value);
  3319. entry_count = value;
  3320. memcpy(&value, direntry_raw + 8, sizeof(uint32_t));
  3321. if (tif->tif_flags & TIFF_SWAB)
  3322. TIFFSwabLong(&value);
  3323. entry_offset = value;
  3324. }
  3325. else
  3326. {
  3327. memcpy(&entry_count, direntry_raw + 4, sizeof(uint64_t));
  3328. if (tif->tif_flags & TIFF_SWAB)
  3329. TIFFSwabLong8(&entry_count);
  3330. memcpy(&entry_offset, direntry_raw + 12, sizeof(uint64_t));
  3331. if (tif->tif_flags & TIFF_SWAB)
  3332. TIFFSwabLong8(&entry_offset);
  3333. }
  3334. /* -------------------------------------------------------------------- */
  3335. /* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
  3336. /* -------------------------------------------------------------------- */
  3337. if (entry_offset == 0 && entry_count == 0 && entry_type == 0)
  3338. {
  3339. if (tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS)
  3340. {
  3341. entry_type =
  3342. (tif->tif_flags & TIFF_BIGTIFF) ? TIFF_LONG8 : TIFF_LONG;
  3343. }
  3344. else
  3345. {
  3346. int write_aslong8 = 1;
  3347. if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
  3348. {
  3349. write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
  3350. }
  3351. else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
  3352. {
  3353. write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
  3354. }
  3355. if (write_aslong8)
  3356. {
  3357. entry_type = TIFF_LONG8;
  3358. }
  3359. else
  3360. {
  3361. int write_aslong4 = 1;
  3362. if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
  3363. {
  3364. write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
  3365. }
  3366. else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
  3367. {
  3368. write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
  3369. }
  3370. if (write_aslong4)
  3371. {
  3372. entry_type = TIFF_LONG;
  3373. }
  3374. else
  3375. {
  3376. entry_type = TIFF_SHORT;
  3377. }
  3378. }
  3379. }
  3380. }
  3381. /* -------------------------------------------------------------------- */
  3382. /* What data type do we want to write this as? */
  3383. /* -------------------------------------------------------------------- */
  3384. if (TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags & TIFF_BIGTIFF))
  3385. {
  3386. if (in_datatype == TIFF_LONG8)
  3387. datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG;
  3388. else if (in_datatype == TIFF_SLONG8)
  3389. datatype = TIFF_SLONG;
  3390. else if (in_datatype == TIFF_IFD8)
  3391. datatype = TIFF_IFD;
  3392. else
  3393. datatype = in_datatype;
  3394. }
  3395. else
  3396. {
  3397. if (in_datatype == TIFF_LONG8 &&
  3398. (entry_type == TIFF_SHORT || entry_type == TIFF_LONG ||
  3399. entry_type == TIFF_LONG8))
  3400. datatype = entry_type;
  3401. else if (in_datatype == TIFF_SLONG8 &&
  3402. (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8))
  3403. datatype = entry_type;
  3404. else if (in_datatype == TIFF_IFD8 &&
  3405. (entry_type == TIFF_IFD || entry_type == TIFF_IFD8))
  3406. datatype = entry_type;
  3407. else
  3408. datatype = in_datatype;
  3409. }
  3410. /* -------------------------------------------------------------------- */
  3411. /* Prepare buffer of actual data to write. This includes */
  3412. /* swabbing as needed. */
  3413. /* -------------------------------------------------------------------- */
  3414. buf_to_write = (uint8_t *)_TIFFCheckMalloc(
  3415. tif, count, TIFFDataWidth(datatype), "for field buffer.");
  3416. if (!buf_to_write)
  3417. return 0;
  3418. if (datatype == in_datatype)
  3419. memcpy(buf_to_write, data, count * TIFFDataWidth(datatype));
  3420. else if (datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8)
  3421. {
  3422. tmsize_t i;
  3423. for (i = 0; i < count; i++)
  3424. {
  3425. ((int32_t *)buf_to_write)[i] = (int32_t)((int64_t *)data)[i];
  3426. if ((int64_t)((int32_t *)buf_to_write)[i] != ((int64_t *)data)[i])
  3427. {
  3428. _TIFFfreeExt(tif, buf_to_write);
  3429. TIFFErrorExtR(tif, module,
  3430. "Value exceeds 32bit range of output type.");
  3431. return 0;
  3432. }
  3433. }
  3434. }
  3435. else if ((datatype == TIFF_LONG && in_datatype == TIFF_LONG8) ||
  3436. (datatype == TIFF_IFD && in_datatype == TIFF_IFD8))
  3437. {
  3438. tmsize_t i;
  3439. for (i = 0; i < count; i++)
  3440. {
  3441. ((uint32_t *)buf_to_write)[i] = (uint32_t)((uint64_t *)data)[i];
  3442. if ((uint64_t)((uint32_t *)buf_to_write)[i] !=
  3443. ((uint64_t *)data)[i])
  3444. {
  3445. _TIFFfreeExt(tif, buf_to_write);
  3446. TIFFErrorExtR(tif, module,
  3447. "Value exceeds 32bit range of output type.");
  3448. return 0;
  3449. }
  3450. }
  3451. }
  3452. else if (datatype == TIFF_SHORT && in_datatype == TIFF_LONG8)
  3453. {
  3454. tmsize_t i;
  3455. for (i = 0; i < count; i++)
  3456. {
  3457. ((uint16_t *)buf_to_write)[i] = (uint16_t)((uint64_t *)data)[i];
  3458. if ((uint64_t)((uint16_t *)buf_to_write)[i] !=
  3459. ((uint64_t *)data)[i])
  3460. {
  3461. _TIFFfreeExt(tif, buf_to_write);
  3462. TIFFErrorExtR(tif, module,
  3463. "Value exceeds 16bit range of output type.");
  3464. return 0;
  3465. }
  3466. }
  3467. }
  3468. else
  3469. {
  3470. TIFFErrorExtR(tif, module, "Unhandled type conversion.");
  3471. return 0;
  3472. }
  3473. if (TIFFDataWidth(datatype) > 1 && (tif->tif_flags & TIFF_SWAB))
  3474. {
  3475. if (TIFFDataWidth(datatype) == 2)
  3476. TIFFSwabArrayOfShort((uint16_t *)buf_to_write, count);
  3477. else if (TIFFDataWidth(datatype) == 4)
  3478. TIFFSwabArrayOfLong((uint32_t *)buf_to_write, count);
  3479. else if (TIFFDataWidth(datatype) == 8)
  3480. TIFFSwabArrayOfLong8((uint64_t *)buf_to_write, count);
  3481. }
  3482. /* -------------------------------------------------------------------- */
  3483. /* Is this a value that fits into the directory entry? */
  3484. /* -------------------------------------------------------------------- */
  3485. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3486. {
  3487. if (TIFFDataWidth(datatype) * count <= 4)
  3488. {
  3489. entry_offset = read_offset + 8;
  3490. value_in_entry = 1;
  3491. }
  3492. }
  3493. else
  3494. {
  3495. if (TIFFDataWidth(datatype) * count <= 8)
  3496. {
  3497. entry_offset = read_offset + 12;
  3498. value_in_entry = 1;
  3499. }
  3500. }
  3501. if ((tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) &&
  3502. tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
  3503. tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
  3504. tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0)
  3505. {
  3506. tif->tif_dir.td_stripoffset_entry.tdir_type = datatype;
  3507. tif->tif_dir.td_stripoffset_entry.tdir_count = count;
  3508. }
  3509. else if ((tag == TIFFTAG_TILEBYTECOUNTS ||
  3510. tag == TIFFTAG_STRIPBYTECOUNTS) &&
  3511. tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
  3512. tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
  3513. tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
  3514. {
  3515. tif->tif_dir.td_stripbytecount_entry.tdir_type = datatype;
  3516. tif->tif_dir.td_stripbytecount_entry.tdir_count = count;
  3517. }
  3518. /* -------------------------------------------------------------------- */
  3519. /* If the tag type, and count match, then we just write it out */
  3520. /* over the old values without altering the directory entry at */
  3521. /* all. */
  3522. /* -------------------------------------------------------------------- */
  3523. if (entry_count == (uint64_t)count && entry_type == (uint16_t)datatype)
  3524. {
  3525. if (!SeekOK(tif, entry_offset))
  3526. {
  3527. _TIFFfreeExt(tif, buf_to_write);
  3528. TIFFErrorExtR(tif, module,
  3529. "%s: Seek error accessing TIFF directory",
  3530. tif->tif_name);
  3531. return 0;
  3532. }
  3533. if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype)))
  3534. {
  3535. _TIFFfreeExt(tif, buf_to_write);
  3536. TIFFErrorExtR(tif, module, "Error writing directory link");
  3537. return (0);
  3538. }
  3539. _TIFFfreeExt(tif, buf_to_write);
  3540. return 1;
  3541. }
  3542. /* -------------------------------------------------------------------- */
  3543. /* Otherwise, we write the new tag data at the end of the file. */
  3544. /* -------------------------------------------------------------------- */
  3545. if (!value_in_entry)
  3546. {
  3547. entry_offset = TIFFSeekFile(tif, 0, SEEK_END);
  3548. if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype)))
  3549. {
  3550. _TIFFfreeExt(tif, buf_to_write);
  3551. TIFFErrorExtR(tif, module, "Error writing directory link");
  3552. return (0);
  3553. }
  3554. }
  3555. else
  3556. {
  3557. if (count * TIFFDataWidth(datatype) == 4)
  3558. {
  3559. uint32_t value;
  3560. memcpy(&value, buf_to_write, count * TIFFDataWidth(datatype));
  3561. entry_offset = value;
  3562. }
  3563. else
  3564. {
  3565. memcpy(&entry_offset, buf_to_write,
  3566. count * TIFFDataWidth(datatype));
  3567. }
  3568. }
  3569. _TIFFfreeExt(tif, buf_to_write);
  3570. buf_to_write = 0;
  3571. /* -------------------------------------------------------------------- */
  3572. /* Adjust the directory entry. */
  3573. /* -------------------------------------------------------------------- */
  3574. entry_type = datatype;
  3575. entry_count = (uint64_t)count;
  3576. memcpy(direntry_raw + 2, &entry_type, sizeof(uint16_t));
  3577. if (tif->tif_flags & TIFF_SWAB)
  3578. TIFFSwabShort((uint16_t *)(direntry_raw + 2));
  3579. if (!(tif->tif_flags & TIFF_BIGTIFF))
  3580. {
  3581. uint32_t value;
  3582. value = (uint32_t)entry_count;
  3583. memcpy(direntry_raw + 4, &value, sizeof(uint32_t));
  3584. if (tif->tif_flags & TIFF_SWAB)
  3585. TIFFSwabLong((uint32_t *)(direntry_raw + 4));
  3586. value = (uint32_t)entry_offset;
  3587. memcpy(direntry_raw + 8, &value, sizeof(uint32_t));
  3588. if (tif->tif_flags & TIFF_SWAB)
  3589. TIFFSwabLong((uint32_t *)(direntry_raw + 8));
  3590. }
  3591. else
  3592. {
  3593. memcpy(direntry_raw + 4, &entry_count, sizeof(uint64_t));
  3594. if (tif->tif_flags & TIFF_SWAB)
  3595. TIFFSwabLong8((uint64_t *)(direntry_raw + 4));
  3596. memcpy(direntry_raw + 12, &entry_offset, sizeof(uint64_t));
  3597. if (tif->tif_flags & TIFF_SWAB)
  3598. TIFFSwabLong8((uint64_t *)(direntry_raw + 12));
  3599. }
  3600. /* -------------------------------------------------------------------- */
  3601. /* Write the directory entry out to disk. */
  3602. /* -------------------------------------------------------------------- */
  3603. if (!SeekOK(tif, read_offset))
  3604. {
  3605. TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory",
  3606. tif->tif_name);
  3607. return 0;
  3608. }
  3609. if (!WriteOK(tif, direntry_raw, dirsize))
  3610. {
  3611. TIFFErrorExtR(tif, module, "%s: Can not write TIFF directory entry.",
  3612. tif->tif_name);
  3613. return 0;
  3614. }
  3615. return 1;
  3616. }