tif_dirwrite.c 146 KB

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