tif_dirwrite.c 135 KB

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