tif_dirwrite.c 135 KB

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