tif_dirwrite.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000
  1. /* $Id: tif_dirwrite.c,v 1.85 2017-01-11 16:09:02 erouault Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Directory Write Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. #include <float.h>
  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 n, float* fp);
  37. extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
  38. #endif
  39. static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
  40. static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
  41. #if 0
  42. static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  43. #endif
  44. static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
  45. static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
  46. #ifdef notdef
  47. static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
  48. #endif
  49. static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
  50. #if 0
  51. static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
  52. #endif
  53. #ifdef notdef
  54. static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
  55. #endif
  56. static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
  57. #if 0
  58. static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
  59. #endif
  60. static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
  61. static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
  62. static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
  63. #ifdef notdef
  64. static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
  65. #endif
  66. static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
  67. #if 0
  68. static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
  69. #endif
  70. static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
  71. static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
  72. #if 0
  73. static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
  74. #endif
  75. #ifdef notdef
  76. static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
  77. #endif
  78. static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
  79. #if 0
  80. static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
  81. #endif
  82. #ifdef notdef
  83. static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
  84. #endif
  85. static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  86. #ifdef notdef
  87. static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
  88. #endif
  89. static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
  90. static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  91. static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  92. static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  93. #ifdef notdef
  94. static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
  95. #endif
  96. static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  97. #if 0
  98. static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
  99. #endif
  100. #ifdef notdef
  101. static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  102. #endif
  103. static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
  104. #if 0
  105. static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  106. #endif
  107. static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
  108. #ifdef notdef
  109. static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  110. #endif
  111. static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
  112. static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  113. static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  114. #ifdef notdef
  115. static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  116. #endif
  117. static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
  118. static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
  119. static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
  120. static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
  121. static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
  122. #ifdef notdef
  123. static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
  124. #endif
  125. static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
  126. #ifdef notdef
  127. static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
  128. #endif
  129. static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
  130. static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
  131. static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
  132. #ifdef notdef
  133. static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
  134. #endif
  135. static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
  136. static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
  137. static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
  138. #ifdef notdef
  139. static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
  140. #endif
  141. static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
  142. #ifdef notdef
  143. static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
  144. #endif
  145. static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  146. #ifdef notdef
  147. static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
  148. #endif
  149. static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
  150. static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  151. static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  152. static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  153. #ifdef notdef
  154. static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
  155. #endif
  156. static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
  157. #ifdef notdef
  158. static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
  159. #endif
  160. static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
  161. static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
  162. static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
  163. static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
  164. static int TIFFLinkDirectory(TIFF*);
  165. /*
  166. * Write the contents of the current directory
  167. * to the specified file. This routine doesn't
  168. * handle overwriting a directory with auxiliary
  169. * storage that's been changed.
  170. */
  171. int
  172. TIFFWriteDirectory(TIFF* tif)
  173. {
  174. return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
  175. }
  176. /*
  177. * Similar to TIFFWriteDirectory(), writes the directory out
  178. * but leaves all data structures in memory so that it can be
  179. * written again. This will make a partially written TIFF file
  180. * readable before it is successfully completed/closed.
  181. */
  182. int
  183. TIFFCheckpointDirectory(TIFF* tif)
  184. {
  185. int rc;
  186. /* Setup the strips arrays, if they haven't already been. */
  187. if (tif->tif_dir.td_stripoffset == NULL)
  188. (void) TIFFSetupStrips(tif);
  189. rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
  190. (void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
  191. return rc;
  192. }
  193. int
  194. TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
  195. {
  196. return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
  197. }
  198. /*
  199. * Similar to TIFFWriteDirectory(), but if the directory has already
  200. * been written once, it is relocated to the end of the file, in case it
  201. * has changed in size. Note that this will result in the loss of the
  202. * previously used directory space.
  203. */
  204. int
  205. TIFFRewriteDirectory( TIFF *tif )
  206. {
  207. static const char module[] = "TIFFRewriteDirectory";
  208. /* We don't need to do anything special if it hasn't been written. */
  209. if( tif->tif_diroff == 0 )
  210. return TIFFWriteDirectory( tif );
  211. /*
  212. * Find and zero the pointer to this directory, so that TIFFLinkDirectory
  213. * will cause it to be added after this directories current pre-link.
  214. */
  215. if (!(tif->tif_flags&TIFF_BIGTIFF))
  216. {
  217. if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
  218. {
  219. tif->tif_header.classic.tiff_diroff = 0;
  220. tif->tif_diroff = 0;
  221. TIFFSeekFile(tif,4,SEEK_SET);
  222. if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
  223. {
  224. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  225. "Error updating TIFF header");
  226. return (0);
  227. }
  228. }
  229. else
  230. {
  231. uint32 nextdir;
  232. nextdir = tif->tif_header.classic.tiff_diroff;
  233. while(1) {
  234. uint16 dircount;
  235. uint32 nextnextdir;
  236. if (!SeekOK(tif, nextdir) ||
  237. !ReadOK(tif, &dircount, 2)) {
  238. TIFFErrorExt(tif->tif_clientdata, module,
  239. "Error fetching directory count");
  240. return (0);
  241. }
  242. if (tif->tif_flags & TIFF_SWAB)
  243. TIFFSwabShort(&dircount);
  244. (void) TIFFSeekFile(tif,
  245. nextdir+2+dircount*12, SEEK_SET);
  246. if (!ReadOK(tif, &nextnextdir, 4)) {
  247. TIFFErrorExt(tif->tif_clientdata, module,
  248. "Error fetching directory link");
  249. return (0);
  250. }
  251. if (tif->tif_flags & TIFF_SWAB)
  252. TIFFSwabLong(&nextnextdir);
  253. if (nextnextdir==tif->tif_diroff)
  254. {
  255. uint32 m;
  256. m=0;
  257. (void) TIFFSeekFile(tif,
  258. nextdir+2+dircount*12, SEEK_SET);
  259. if (!WriteOK(tif, &m, 4)) {
  260. TIFFErrorExt(tif->tif_clientdata, module,
  261. "Error writing directory link");
  262. return (0);
  263. }
  264. tif->tif_diroff=0;
  265. break;
  266. }
  267. nextdir=nextnextdir;
  268. }
  269. }
  270. }
  271. else
  272. {
  273. if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
  274. {
  275. tif->tif_header.big.tiff_diroff = 0;
  276. tif->tif_diroff = 0;
  277. TIFFSeekFile(tif,8,SEEK_SET);
  278. if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
  279. {
  280. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  281. "Error updating TIFF header");
  282. return (0);
  283. }
  284. }
  285. else
  286. {
  287. uint64 nextdir;
  288. nextdir = tif->tif_header.big.tiff_diroff;
  289. while(1) {
  290. uint64 dircount64;
  291. uint16 dircount;
  292. uint64 nextnextdir;
  293. if (!SeekOK(tif, nextdir) ||
  294. !ReadOK(tif, &dircount64, 8)) {
  295. TIFFErrorExt(tif->tif_clientdata, module,
  296. "Error fetching directory count");
  297. return (0);
  298. }
  299. if (tif->tif_flags & TIFF_SWAB)
  300. TIFFSwabLong8(&dircount64);
  301. if (dircount64>0xFFFF)
  302. {
  303. TIFFErrorExt(tif->tif_clientdata, module,
  304. "Sanity check on tag count failed, likely corrupt TIFF");
  305. return (0);
  306. }
  307. dircount=(uint16)dircount64;
  308. (void) TIFFSeekFile(tif,
  309. nextdir+8+dircount*20, SEEK_SET);
  310. if (!ReadOK(tif, &nextnextdir, 8)) {
  311. TIFFErrorExt(tif->tif_clientdata, module,
  312. "Error fetching directory link");
  313. return (0);
  314. }
  315. if (tif->tif_flags & TIFF_SWAB)
  316. TIFFSwabLong8(&nextnextdir);
  317. if (nextnextdir==tif->tif_diroff)
  318. {
  319. uint64 m;
  320. m=0;
  321. (void) TIFFSeekFile(tif,
  322. nextdir+8+dircount*20, SEEK_SET);
  323. if (!WriteOK(tif, &m, 8)) {
  324. TIFFErrorExt(tif->tif_clientdata, module,
  325. "Error writing directory link");
  326. return (0);
  327. }
  328. tif->tif_diroff=0;
  329. break;
  330. }
  331. nextdir=nextnextdir;
  332. }
  333. }
  334. }
  335. /*
  336. * Now use TIFFWriteDirectory() normally.
  337. */
  338. return TIFFWriteDirectory( tif );
  339. }
  340. static int
  341. TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
  342. {
  343. static const char module[] = "TIFFWriteDirectorySec";
  344. uint32 ndir;
  345. TIFFDirEntry* dir;
  346. uint32 dirsize;
  347. void* dirmem;
  348. uint32 m;
  349. if (tif->tif_mode == O_RDONLY)
  350. return (1);
  351. _TIFFFillStriles( tif );
  352. /*
  353. * Clear write state so that subsequent images with
  354. * different characteristics get the right buffers
  355. * setup for them.
  356. */
  357. if (imagedone)
  358. {
  359. if (tif->tif_flags & TIFF_POSTENCODE)
  360. {
  361. tif->tif_flags &= ~TIFF_POSTENCODE;
  362. if (!(*tif->tif_postencode)(tif))
  363. {
  364. TIFFErrorExt(tif->tif_clientdata,module,
  365. "Error post-encoding before directory write");
  366. return (0);
  367. }
  368. }
  369. (*tif->tif_close)(tif); /* shutdown encoder */
  370. /*
  371. * Flush any data that might have been written
  372. * by the compression close+cleanup routines. But
  373. * be careful not to write stuff if we didn't add data
  374. * in the previous steps as the "rawcc" data may well be
  375. * a previously read tile/strip in mixed read/write mode.
  376. */
  377. if (tif->tif_rawcc > 0
  378. && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
  379. {
  380. if( !TIFFFlushData1(tif) )
  381. {
  382. TIFFErrorExt(tif->tif_clientdata, module,
  383. "Error flushing data before directory write");
  384. return (0);
  385. }
  386. }
  387. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  388. {
  389. _TIFFfree(tif->tif_rawdata);
  390. tif->tif_rawdata = NULL;
  391. tif->tif_rawcc = 0;
  392. tif->tif_rawdatasize = 0;
  393. tif->tif_rawdataoff = 0;
  394. tif->tif_rawdataloaded = 0;
  395. }
  396. tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
  397. }
  398. dir=NULL;
  399. dirmem=NULL;
  400. dirsize=0;
  401. while (1)
  402. {
  403. ndir=0;
  404. if (isimage)
  405. {
  406. if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
  407. {
  408. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
  409. goto bad;
  410. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
  411. goto bad;
  412. }
  413. if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
  414. {
  415. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
  416. goto bad;
  417. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
  418. goto bad;
  419. }
  420. if (TIFFFieldSet(tif,FIELD_RESOLUTION))
  421. {
  422. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
  423. goto bad;
  424. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
  425. goto bad;
  426. }
  427. if (TIFFFieldSet(tif,FIELD_POSITION))
  428. {
  429. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
  430. goto bad;
  431. if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
  432. goto bad;
  433. }
  434. if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
  435. {
  436. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
  437. goto bad;
  438. }
  439. if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
  440. {
  441. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
  442. goto bad;
  443. }
  444. if (TIFFFieldSet(tif,FIELD_COMPRESSION))
  445. {
  446. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
  447. goto bad;
  448. }
  449. if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
  450. {
  451. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
  452. goto bad;
  453. }
  454. if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
  455. {
  456. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
  457. goto bad;
  458. }
  459. if (TIFFFieldSet(tif,FIELD_FILLORDER))
  460. {
  461. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
  462. goto bad;
  463. }
  464. if (TIFFFieldSet(tif,FIELD_ORIENTATION))
  465. {
  466. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
  467. goto bad;
  468. }
  469. if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
  470. {
  471. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
  472. goto bad;
  473. }
  474. if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
  475. {
  476. if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
  477. goto bad;
  478. }
  479. if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
  480. {
  481. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
  482. goto bad;
  483. }
  484. if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
  485. {
  486. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
  487. goto bad;
  488. }
  489. if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
  490. {
  491. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
  492. goto bad;
  493. }
  494. if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
  495. {
  496. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
  497. goto bad;
  498. }
  499. if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
  500. {
  501. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
  502. goto bad;
  503. }
  504. if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
  505. {
  506. if (!isTiled(tif))
  507. {
  508. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
  509. goto bad;
  510. }
  511. else
  512. {
  513. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
  514. goto bad;
  515. }
  516. }
  517. if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
  518. {
  519. if (!isTiled(tif))
  520. {
  521. /* td_stripoffset might be NULL in an odd OJPEG case. See
  522. * tif_dirread.c around line 3634.
  523. * XXX: OJPEG hack.
  524. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  525. * and c) the number of strips is 1,
  526. * then we tolerate the absence of stripoffsets tag,
  527. * because, presumably, all required data is in the
  528. * JpegInterchangeFormat stream.
  529. * We can get here when using tiffset on such a file.
  530. * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
  531. */
  532. if (tif->tif_dir.td_stripoffset != NULL &&
  533. !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
  534. goto bad;
  535. }
  536. else
  537. {
  538. if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
  539. goto bad;
  540. }
  541. }
  542. if (TIFFFieldSet(tif,FIELD_COLORMAP))
  543. {
  544. if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
  545. goto bad;
  546. }
  547. if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
  548. {
  549. if (tif->tif_dir.td_extrasamples)
  550. {
  551. uint16 na;
  552. uint16* nb;
  553. TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
  554. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
  555. goto bad;
  556. }
  557. }
  558. if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
  559. {
  560. if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
  561. goto bad;
  562. }
  563. if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
  564. {
  565. if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
  566. goto bad;
  567. }
  568. if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
  569. {
  570. if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
  571. goto bad;
  572. }
  573. if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
  574. {
  575. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
  576. goto bad;
  577. }
  578. if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
  579. {
  580. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
  581. goto bad;
  582. }
  583. if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
  584. {
  585. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
  586. goto bad;
  587. }
  588. if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
  589. {
  590. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
  591. goto bad;
  592. }
  593. if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
  594. {
  595. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
  596. goto bad;
  597. }
  598. if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
  599. {
  600. if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
  601. goto bad;
  602. }
  603. if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
  604. {
  605. if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
  606. goto bad;
  607. }
  608. if (TIFFFieldSet(tif,FIELD_INKNAMES))
  609. {
  610. if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
  611. goto bad;
  612. }
  613. if (TIFFFieldSet(tif,FIELD_SUBIFD))
  614. {
  615. if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
  616. goto bad;
  617. }
  618. {
  619. uint32 n;
  620. for (n=0; n<tif->tif_nfields; n++) {
  621. const TIFFField* o;
  622. o = tif->tif_fields[n];
  623. if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
  624. {
  625. switch (o->get_field_type)
  626. {
  627. case TIFF_SETGET_ASCII:
  628. {
  629. uint32 pa;
  630. char* pb;
  631. assert(o->field_type==TIFF_ASCII);
  632. assert(o->field_readcount==TIFF_VARIABLE);
  633. assert(o->field_passcount==0);
  634. TIFFGetField(tif,o->field_tag,&pb);
  635. pa=(uint32)(strlen(pb));
  636. if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
  637. goto bad;
  638. }
  639. break;
  640. case TIFF_SETGET_UINT16:
  641. {
  642. uint16 p;
  643. assert(o->field_type==TIFF_SHORT);
  644. assert(o->field_readcount==1);
  645. assert(o->field_passcount==0);
  646. TIFFGetField(tif,o->field_tag,&p);
  647. if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))
  648. goto bad;
  649. }
  650. break;
  651. case TIFF_SETGET_UINT32:
  652. {
  653. uint32 p;
  654. assert(o->field_type==TIFF_LONG);
  655. assert(o->field_readcount==1);
  656. assert(o->field_passcount==0);
  657. TIFFGetField(tif,o->field_tag,&p);
  658. if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))
  659. goto bad;
  660. }
  661. break;
  662. case TIFF_SETGET_C32_UINT8:
  663. {
  664. uint32 pa;
  665. void* pb;
  666. assert(o->field_type==TIFF_UNDEFINED);
  667. assert(o->field_readcount==TIFF_VARIABLE2);
  668. assert(o->field_passcount==1);
  669. TIFFGetField(tif,o->field_tag,&pa,&pb);
  670. if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
  671. goto bad;
  672. }
  673. break;
  674. default:
  675. assert(0); /* we should never get here */
  676. break;
  677. }
  678. }
  679. }
  680. }
  681. }
  682. for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
  683. {
  684. uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;
  685. uint32 count = tif->tif_dir.td_customValues[m].count;
  686. switch (tif->tif_dir.td_customValues[m].info->field_type)
  687. {
  688. case TIFF_ASCII:
  689. if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  690. goto bad;
  691. break;
  692. case TIFF_UNDEFINED:
  693. if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  694. goto bad;
  695. break;
  696. case TIFF_BYTE:
  697. if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  698. goto bad;
  699. break;
  700. case TIFF_SBYTE:
  701. if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  702. goto bad;
  703. break;
  704. case TIFF_SHORT:
  705. if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  706. goto bad;
  707. break;
  708. case TIFF_SSHORT:
  709. if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  710. goto bad;
  711. break;
  712. case TIFF_LONG:
  713. if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  714. goto bad;
  715. break;
  716. case TIFF_SLONG:
  717. if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  718. goto bad;
  719. break;
  720. case TIFF_LONG8:
  721. if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  722. goto bad;
  723. break;
  724. case TIFF_SLONG8:
  725. if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  726. goto bad;
  727. break;
  728. case TIFF_RATIONAL:
  729. if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  730. goto bad;
  731. break;
  732. case TIFF_SRATIONAL:
  733. if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  734. goto bad;
  735. break;
  736. case TIFF_FLOAT:
  737. if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  738. goto bad;
  739. break;
  740. case TIFF_DOUBLE:
  741. if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  742. goto bad;
  743. break;
  744. case TIFF_IFD:
  745. if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  746. goto bad;
  747. break;
  748. case TIFF_IFD8:
  749. if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))
  750. goto bad;
  751. break;
  752. default:
  753. assert(0); /* we should never get here */
  754. break;
  755. }
  756. }
  757. if (dir!=NULL)
  758. break;
  759. dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
  760. if (dir==NULL)
  761. {
  762. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  763. goto bad;
  764. }
  765. if (isimage)
  766. {
  767. if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
  768. goto bad;
  769. }
  770. else
  771. tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
  772. if (pdiroff!=NULL)
  773. *pdiroff=tif->tif_diroff;
  774. if (!(tif->tif_flags&TIFF_BIGTIFF))
  775. dirsize=2+ndir*12+4;
  776. else
  777. dirsize=8+ndir*20+8;
  778. tif->tif_dataoff=tif->tif_diroff+dirsize;
  779. if (!(tif->tif_flags&TIFF_BIGTIFF))
  780. tif->tif_dataoff=(uint32)tif->tif_dataoff;
  781. if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
  782. {
  783. TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
  784. goto bad;
  785. }
  786. if (tif->tif_dataoff&1)
  787. tif->tif_dataoff++;
  788. if (isimage)
  789. tif->tif_curdir++;
  790. }
  791. if (isimage)
  792. {
  793. if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
  794. {
  795. uint32 na;
  796. TIFFDirEntry* nb;
  797. for (na=0, nb=dir; ; na++, nb++)
  798. {
  799. assert(na<ndir);
  800. if (nb->tdir_tag==TIFFTAG_SUBIFD)
  801. break;
  802. }
  803. if (!(tif->tif_flags&TIFF_BIGTIFF))
  804. tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
  805. else
  806. tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
  807. }
  808. }
  809. dirmem=_TIFFmalloc(dirsize);
  810. if (dirmem==NULL)
  811. {
  812. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  813. goto bad;
  814. }
  815. if (!(tif->tif_flags&TIFF_BIGTIFF))
  816. {
  817. uint8* n;
  818. uint32 nTmp;
  819. TIFFDirEntry* o;
  820. n=dirmem;
  821. *(uint16*)n=(uint16)ndir;
  822. if (tif->tif_flags&TIFF_SWAB)
  823. TIFFSwabShort((uint16*)n);
  824. n+=2;
  825. o=dir;
  826. for (m=0; m<ndir; m++)
  827. {
  828. *(uint16*)n=o->tdir_tag;
  829. if (tif->tif_flags&TIFF_SWAB)
  830. TIFFSwabShort((uint16*)n);
  831. n+=2;
  832. *(uint16*)n=o->tdir_type;
  833. if (tif->tif_flags&TIFF_SWAB)
  834. TIFFSwabShort((uint16*)n);
  835. n+=2;
  836. nTmp = (uint32)o->tdir_count;
  837. _TIFFmemcpy(n,&nTmp,4);
  838. if (tif->tif_flags&TIFF_SWAB)
  839. TIFFSwabLong((uint32*)n);
  840. n+=4;
  841. /* This is correct. The data has been */
  842. /* swabbed previously in TIFFWriteDirectoryTagData */
  843. _TIFFmemcpy(n,&o->tdir_offset,4);
  844. n+=4;
  845. o++;
  846. }
  847. nTmp = (uint32)tif->tif_nextdiroff;
  848. if (tif->tif_flags&TIFF_SWAB)
  849. TIFFSwabLong(&nTmp);
  850. _TIFFmemcpy(n,&nTmp,4);
  851. }
  852. else
  853. {
  854. uint8* n;
  855. TIFFDirEntry* o;
  856. n=dirmem;
  857. *(uint64*)n=ndir;
  858. if (tif->tif_flags&TIFF_SWAB)
  859. TIFFSwabLong8((uint64*)n);
  860. n+=8;
  861. o=dir;
  862. for (m=0; m<ndir; m++)
  863. {
  864. *(uint16*)n=o->tdir_tag;
  865. if (tif->tif_flags&TIFF_SWAB)
  866. TIFFSwabShort((uint16*)n);
  867. n+=2;
  868. *(uint16*)n=o->tdir_type;
  869. if (tif->tif_flags&TIFF_SWAB)
  870. TIFFSwabShort((uint16*)n);
  871. n+=2;
  872. _TIFFmemcpy(n,&o->tdir_count,8);
  873. if (tif->tif_flags&TIFF_SWAB)
  874. TIFFSwabLong8((uint64*)n);
  875. n+=8;
  876. _TIFFmemcpy(n,&o->tdir_offset,8);
  877. n+=8;
  878. o++;
  879. }
  880. _TIFFmemcpy(n,&tif->tif_nextdiroff,8);
  881. if (tif->tif_flags&TIFF_SWAB)
  882. TIFFSwabLong8((uint64*)n);
  883. }
  884. _TIFFfree(dir);
  885. dir=NULL;
  886. if (!SeekOK(tif,tif->tif_diroff))
  887. {
  888. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
  889. goto bad;
  890. }
  891. if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
  892. {
  893. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
  894. goto bad;
  895. }
  896. _TIFFfree(dirmem);
  897. if (imagedone)
  898. {
  899. TIFFFreeDirectory(tif);
  900. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  901. tif->tif_flags &= ~TIFF_DIRTYSTRIP;
  902. (*tif->tif_cleanup)(tif);
  903. /*
  904. * Reset directory-related state for subsequent
  905. * directories.
  906. */
  907. TIFFCreateDirectory(tif);
  908. }
  909. return(1);
  910. bad:
  911. if (dir!=NULL)
  912. _TIFFfree(dir);
  913. if (dirmem!=NULL)
  914. _TIFFfree(dirmem);
  915. return(0);
  916. }
  917. static float TIFFClampDoubleToFloat( double val )
  918. {
  919. if( val > FLT_MAX )
  920. return FLT_MAX;
  921. if( val < -FLT_MAX )
  922. return -FLT_MAX;
  923. return (float)val;
  924. }
  925. static int8 TIFFClampDoubleToInt8( double val )
  926. {
  927. if( val > 127 )
  928. return 127;
  929. if( val < -128 || val != val )
  930. return -128;
  931. return (int8)val;
  932. }
  933. static int16 TIFFClampDoubleToInt16( double val )
  934. {
  935. if( val > 32767 )
  936. return 32767;
  937. if( val < -32768 || val != val )
  938. return -32768;
  939. return (int16)val;
  940. }
  941. static int32 TIFFClampDoubleToInt32( double val )
  942. {
  943. if( val > 0x7FFFFFFF )
  944. return 0x7FFFFFFF;
  945. if( val < -0x7FFFFFFF-1 || val != val )
  946. return -0x7FFFFFFF-1;
  947. return (int32)val;
  948. }
  949. static uint8 TIFFClampDoubleToUInt8( double val )
  950. {
  951. if( val < 0 )
  952. return 0;
  953. if( val > 255 || val != val )
  954. return 255;
  955. return (uint8)val;
  956. }
  957. static uint16 TIFFClampDoubleToUInt16( double val )
  958. {
  959. if( val < 0 )
  960. return 0;
  961. if( val > 65535 || val != val )
  962. return 65535;
  963. return (uint16)val;
  964. }
  965. static uint32 TIFFClampDoubleToUInt32( double val )
  966. {
  967. if( val < 0 )
  968. return 0;
  969. if( val > 0xFFFFFFFFU || val != val )
  970. return 0xFFFFFFFFU;
  971. return (uint32)val;
  972. }
  973. static int
  974. TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
  975. {
  976. static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
  977. void* conv;
  978. uint32 i;
  979. int ok;
  980. conv = _TIFFmalloc(count*sizeof(double));
  981. if (conv == NULL)
  982. {
  983. TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
  984. return (0);
  985. }
  986. switch (tif->tif_dir.td_sampleformat)
  987. {
  988. case SAMPLEFORMAT_IEEEFP:
  989. if (tif->tif_dir.td_bitspersample<=32)
  990. {
  991. for (i = 0; i < count; ++i)
  992. ((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);
  993. ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
  994. }
  995. else
  996. {
  997. ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
  998. }
  999. break;
  1000. case SAMPLEFORMAT_INT:
  1001. if (tif->tif_dir.td_bitspersample<=8)
  1002. {
  1003. for (i = 0; i < count; ++i)
  1004. ((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
  1005. ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
  1006. }
  1007. else if (tif->tif_dir.td_bitspersample<=16)
  1008. {
  1009. for (i = 0; i < count; ++i)
  1010. ((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
  1011. ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
  1012. }
  1013. else
  1014. {
  1015. for (i = 0; i < count; ++i)
  1016. ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
  1017. ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
  1018. }
  1019. break;
  1020. case SAMPLEFORMAT_UINT:
  1021. if (tif->tif_dir.td_bitspersample<=8)
  1022. {
  1023. for (i = 0; i < count; ++i)
  1024. ((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
  1025. ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
  1026. }
  1027. else if (tif->tif_dir.td_bitspersample<=16)
  1028. {
  1029. for (i = 0; i < count; ++i)
  1030. ((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
  1031. ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
  1032. }
  1033. else
  1034. {
  1035. for (i = 0; i < count; ++i)
  1036. ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
  1037. ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
  1038. }
  1039. break;
  1040. default:
  1041. ok = 0;
  1042. }
  1043. _TIFFfree(conv);
  1044. return (ok);
  1045. }
  1046. #if 0
  1047. static int
  1048. TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  1049. {
  1050. switch (tif->tif_dir.td_sampleformat)
  1051. {
  1052. case SAMPLEFORMAT_IEEEFP:
  1053. if (tif->tif_dir.td_bitspersample<=32)
  1054. return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
  1055. else
  1056. return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
  1057. case SAMPLEFORMAT_INT:
  1058. if (tif->tif_dir.td_bitspersample<=8)
  1059. return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
  1060. else if (tif->tif_dir.td_bitspersample<=16)
  1061. return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
  1062. else
  1063. return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
  1064. case SAMPLEFORMAT_UINT:
  1065. if (tif->tif_dir.td_bitspersample<=8)
  1066. return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
  1067. else if (tif->tif_dir.td_bitspersample<=16)
  1068. return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));
  1069. else
  1070. return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
  1071. default:
  1072. return(1);
  1073. }
  1074. }
  1075. #endif
  1076. static int
  1077. TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
  1078. {
  1079. if (dir==NULL)
  1080. {
  1081. (*ndir)++;
  1082. return(1);
  1083. }
  1084. return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
  1085. }
  1086. static int
  1087. TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
  1088. {
  1089. if (dir==NULL)
  1090. {
  1091. (*ndir)++;
  1092. return(1);
  1093. }
  1094. return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
  1095. }
  1096. #ifdef notdef
  1097. static int
  1098. TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
  1099. {
  1100. if (dir==NULL)
  1101. {
  1102. (*ndir)++;
  1103. return(1);
  1104. }
  1105. return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
  1106. }
  1107. #endif
  1108. static int
  1109. TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
  1110. {
  1111. if (dir==NULL)
  1112. {
  1113. (*ndir)++;
  1114. return(1);
  1115. }
  1116. return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
  1117. }
  1118. #if 0
  1119. static int
  1120. TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
  1121. {
  1122. static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
  1123. uint8* m;
  1124. uint8* na;
  1125. uint16 nb;
  1126. int o;
  1127. if (dir==NULL)
  1128. {
  1129. (*ndir)++;
  1130. return(1);
  1131. }
  1132. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8));
  1133. if (m==NULL)
  1134. {
  1135. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1136. return(0);
  1137. }
  1138. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1139. *na=value;
  1140. o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1141. _TIFFfree(m);
  1142. return(o);
  1143. }
  1144. #endif
  1145. #ifdef notdef
  1146. static int
  1147. TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
  1148. {
  1149. if (dir==NULL)
  1150. {
  1151. (*ndir)++;
  1152. return(1);
  1153. }
  1154. return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
  1155. }
  1156. #endif
  1157. static int
  1158. TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
  1159. {
  1160. if (dir==NULL)
  1161. {
  1162. (*ndir)++;
  1163. return(1);
  1164. }
  1165. return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
  1166. }
  1167. #if 0
  1168. static int
  1169. TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
  1170. {
  1171. static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
  1172. int8* m;
  1173. int8* na;
  1174. uint16 nb;
  1175. int o;
  1176. if (dir==NULL)
  1177. {
  1178. (*ndir)++;
  1179. return(1);
  1180. }
  1181. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8));
  1182. if (m==NULL)
  1183. {
  1184. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1185. return(0);
  1186. }
  1187. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1188. *na=value;
  1189. o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1190. _TIFFfree(m);
  1191. return(o);
  1192. }
  1193. #endif
  1194. static int
  1195. TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
  1196. {
  1197. if (dir==NULL)
  1198. {
  1199. (*ndir)++;
  1200. return(1);
  1201. }
  1202. return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
  1203. }
  1204. static int
  1205. TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
  1206. {
  1207. if (dir==NULL)
  1208. {
  1209. (*ndir)++;
  1210. return(1);
  1211. }
  1212. return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
  1213. }
  1214. static int
  1215. TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
  1216. {
  1217. static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
  1218. uint16* m;
  1219. uint16* na;
  1220. uint16 nb;
  1221. int o;
  1222. if (dir==NULL)
  1223. {
  1224. (*ndir)++;
  1225. return(1);
  1226. }
  1227. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
  1228. if (m==NULL)
  1229. {
  1230. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1231. return(0);
  1232. }
  1233. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1234. *na=value;
  1235. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1236. _TIFFfree(m);
  1237. return(o);
  1238. }
  1239. #ifdef notdef
  1240. static int
  1241. TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
  1242. {
  1243. if (dir==NULL)
  1244. {
  1245. (*ndir)++;
  1246. return(1);
  1247. }
  1248. return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
  1249. }
  1250. #endif
  1251. static int
  1252. TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
  1253. {
  1254. if (dir==NULL)
  1255. {
  1256. (*ndir)++;
  1257. return(1);
  1258. }
  1259. return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
  1260. }
  1261. #if 0
  1262. static int
  1263. TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
  1264. {
  1265. static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
  1266. int16* m;
  1267. int16* na;
  1268. uint16 nb;
  1269. int o;
  1270. if (dir==NULL)
  1271. {
  1272. (*ndir)++;
  1273. return(1);
  1274. }
  1275. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16));
  1276. if (m==NULL)
  1277. {
  1278. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1279. return(0);
  1280. }
  1281. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1282. *na=value;
  1283. o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1284. _TIFFfree(m);
  1285. return(o);
  1286. }
  1287. #endif
  1288. static int
  1289. TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
  1290. {
  1291. if (dir==NULL)
  1292. {
  1293. (*ndir)++;
  1294. return(1);
  1295. }
  1296. return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
  1297. }
  1298. static int
  1299. TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
  1300. {
  1301. if (dir==NULL)
  1302. {
  1303. (*ndir)++;
  1304. return(1);
  1305. }
  1306. return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
  1307. }
  1308. #if 0
  1309. static int
  1310. TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
  1311. {
  1312. static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
  1313. uint32* m;
  1314. uint32* na;
  1315. uint16 nb;
  1316. int o;
  1317. if (dir==NULL)
  1318. {
  1319. (*ndir)++;
  1320. return(1);
  1321. }
  1322. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32));
  1323. if (m==NULL)
  1324. {
  1325. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1326. return(0);
  1327. }
  1328. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1329. *na=value;
  1330. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1331. _TIFFfree(m);
  1332. return(o);
  1333. }
  1334. #endif
  1335. #ifdef notdef
  1336. static int
  1337. TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
  1338. {
  1339. if (dir==NULL)
  1340. {
  1341. (*ndir)++;
  1342. return(1);
  1343. }
  1344. return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
  1345. }
  1346. #endif
  1347. static int
  1348. TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
  1349. {
  1350. if (dir==NULL)
  1351. {
  1352. (*ndir)++;
  1353. return(1);
  1354. }
  1355. return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
  1356. }
  1357. #if 0
  1358. static int
  1359. TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
  1360. {
  1361. static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
  1362. int32* m;
  1363. int32* na;
  1364. uint16 nb;
  1365. int o;
  1366. if (dir==NULL)
  1367. {
  1368. (*ndir)++;
  1369. return(1);
  1370. }
  1371. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));
  1372. if (m==NULL)
  1373. {
  1374. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1375. return(0);
  1376. }
  1377. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1378. *na=value;
  1379. o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1380. _TIFFfree(m);
  1381. return(o);
  1382. }
  1383. #endif
  1384. #ifdef notdef
  1385. static int
  1386. TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
  1387. {
  1388. if (dir==NULL)
  1389. {
  1390. (*ndir)++;
  1391. return(1);
  1392. }
  1393. return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
  1394. }
  1395. #endif
  1396. static int
  1397. TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  1398. {
  1399. if (dir==NULL)
  1400. {
  1401. (*ndir)++;
  1402. return(1);
  1403. }
  1404. return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
  1405. }
  1406. #ifdef notdef
  1407. static int
  1408. TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
  1409. {
  1410. if (dir==NULL)
  1411. {
  1412. (*ndir)++;
  1413. return(1);
  1414. }
  1415. return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
  1416. }
  1417. #endif
  1418. static int
  1419. TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
  1420. {
  1421. if (dir==NULL)
  1422. {
  1423. (*ndir)++;
  1424. return(1);
  1425. }
  1426. return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
  1427. }
  1428. static int
  1429. TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  1430. {
  1431. if (dir==NULL)
  1432. {
  1433. (*ndir)++;
  1434. return(1);
  1435. }
  1436. return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
  1437. }
  1438. static int
  1439. TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  1440. {
  1441. if (dir==NULL)
  1442. {
  1443. (*ndir)++;
  1444. return(1);
  1445. }
  1446. return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
  1447. }
  1448. static int
  1449. TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  1450. {
  1451. if (dir==NULL)
  1452. {
  1453. (*ndir)++;
  1454. return(1);
  1455. }
  1456. return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
  1457. }
  1458. #ifdef notdef
  1459. static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
  1460. {
  1461. if (dir==NULL)
  1462. {
  1463. (*ndir)++;
  1464. return(1);
  1465. }
  1466. return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
  1467. }
  1468. #endif
  1469. static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  1470. {
  1471. if (dir==NULL)
  1472. {
  1473. (*ndir)++;
  1474. return(1);
  1475. }
  1476. return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
  1477. }
  1478. #if 0
  1479. static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
  1480. {
  1481. static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
  1482. float* m;
  1483. float* na;
  1484. uint16 nb;
  1485. int o;
  1486. if (dir==NULL)
  1487. {
  1488. (*ndir)++;
  1489. return(1);
  1490. }
  1491. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
  1492. if (m==NULL)
  1493. {
  1494. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1495. return(0);
  1496. }
  1497. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1498. *na=value;
  1499. o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1500. _TIFFfree(m);
  1501. return(o);
  1502. }
  1503. #endif
  1504. #ifdef notdef
  1505. static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  1506. {
  1507. if (dir==NULL)
  1508. {
  1509. (*ndir)++;
  1510. return(1);
  1511. }
  1512. return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
  1513. }
  1514. #endif
  1515. static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
  1516. {
  1517. if (dir==NULL)
  1518. {
  1519. (*ndir)++;
  1520. return(1);
  1521. }
  1522. return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
  1523. }
  1524. #if 0
  1525. static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  1526. {
  1527. static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
  1528. double* m;
  1529. double* na;
  1530. uint16 nb;
  1531. int o;
  1532. if (dir==NULL)
  1533. {
  1534. (*ndir)++;
  1535. return(1);
  1536. }
  1537. m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
  1538. if (m==NULL)
  1539. {
  1540. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1541. return(0);
  1542. }
  1543. for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
  1544. *na=value;
  1545. o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
  1546. _TIFFfree(m);
  1547. return(o);
  1548. }
  1549. #endif
  1550. static int
  1551. TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
  1552. {
  1553. if (dir==NULL)
  1554. {
  1555. (*ndir)++;
  1556. return(1);
  1557. }
  1558. return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
  1559. }
  1560. #ifdef notdef
  1561. static int
  1562. TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  1563. {
  1564. if (dir==NULL)
  1565. {
  1566. (*ndir)++;
  1567. return(1);
  1568. }
  1569. return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
  1570. }
  1571. #endif
  1572. static int
  1573. TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
  1574. {
  1575. if (dir==NULL)
  1576. {
  1577. (*ndir)++;
  1578. return(1);
  1579. }
  1580. if (value<=0xFFFF)
  1581. return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16)value));
  1582. else
  1583. return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
  1584. }
  1585. /************************************************************************/
  1586. /* TIFFWriteDirectoryTagLongLong8Array() */
  1587. /* */
  1588. /* Write out LONG8 array as LONG8 for BigTIFF or LONG for */
  1589. /* Classic TIFF with some checking. */
  1590. /************************************************************************/
  1591. static int
  1592. TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  1593. {
  1594. static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
  1595. uint64* ma;
  1596. uint32 mb;
  1597. uint32* p;
  1598. uint32* q;
  1599. int o;
  1600. /* is this just a counting pass? */
  1601. if (dir==NULL)
  1602. {
  1603. (*ndir)++;
  1604. return(1);
  1605. }
  1606. /* We always write LONG8 for BigTIFF, no checking needed. */
  1607. if( tif->tif_flags&TIFF_BIGTIFF )
  1608. return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
  1609. tag,count,value);
  1610. /*
  1611. ** For classic tiff we want to verify everything is in range for LONG
  1612. ** and convert to long format.
  1613. */
  1614. p = _TIFFmalloc(count*sizeof(uint32));
  1615. if (p==NULL)
  1616. {
  1617. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1618. return(0);
  1619. }
  1620. for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
  1621. {
  1622. if (*ma>0xFFFFFFFF)
  1623. {
  1624. TIFFErrorExt(tif->tif_clientdata,module,
  1625. "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
  1626. _TIFFfree(p);
  1627. return(0);
  1628. }
  1629. *q= (uint32)(*ma);
  1630. }
  1631. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
  1632. _TIFFfree(p);
  1633. return(o);
  1634. }
  1635. /************************************************************************/
  1636. /* TIFFWriteDirectoryTagIfdIfd8Array() */
  1637. /* */
  1638. /* Write either IFD8 or IFD array depending on file type. */
  1639. /************************************************************************/
  1640. static int
  1641. TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  1642. {
  1643. static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
  1644. uint64* ma;
  1645. uint32 mb;
  1646. uint32* p;
  1647. uint32* q;
  1648. int o;
  1649. /* is this just a counting pass? */
  1650. if (dir==NULL)
  1651. {
  1652. (*ndir)++;
  1653. return(1);
  1654. }
  1655. /* We always write IFD8 for BigTIFF, no checking needed. */
  1656. if( tif->tif_flags&TIFF_BIGTIFF )
  1657. return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
  1658. tag,count,value);
  1659. /*
  1660. ** For classic tiff we want to verify everything is in range for IFD
  1661. ** and convert to long format.
  1662. */
  1663. p = _TIFFmalloc(count*sizeof(uint32));
  1664. if (p==NULL)
  1665. {
  1666. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1667. return(0);
  1668. }
  1669. for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
  1670. {
  1671. if (*ma>0xFFFFFFFF)
  1672. {
  1673. TIFFErrorExt(tif->tif_clientdata,module,
  1674. "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
  1675. _TIFFfree(p);
  1676. return(0);
  1677. }
  1678. *q= (uint32)(*ma);
  1679. }
  1680. o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
  1681. _TIFFfree(p);
  1682. return(o);
  1683. }
  1684. #ifdef notdef
  1685. static int
  1686. TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  1687. {
  1688. static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
  1689. uint64* ma;
  1690. uint32 mb;
  1691. uint8 n;
  1692. int o;
  1693. if (dir==NULL)
  1694. {
  1695. (*ndir)++;
  1696. return(1);
  1697. }
  1698. n=0;
  1699. for (ma=value, mb=0; mb<count; ma++, mb++)
  1700. {
  1701. if ((n==0)&&(*ma>0xFFFF))
  1702. n=1;
  1703. if ((n==1)&&(*ma>0xFFFFFFFF))
  1704. {
  1705. n=2;
  1706. break;
  1707. }
  1708. }
  1709. if (n==0)
  1710. {
  1711. uint16* p;
  1712. uint16* q;
  1713. p=_TIFFmalloc(count*sizeof(uint16));
  1714. if (p==NULL)
  1715. {
  1716. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1717. return(0);
  1718. }
  1719. for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
  1720. *q=(uint16)(*ma);
  1721. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
  1722. _TIFFfree(p);
  1723. }
  1724. else if (n==1)
  1725. {
  1726. uint32* p;
  1727. uint32* q;
  1728. p=_TIFFmalloc(count*sizeof(uint32));
  1729. if (p==NULL)
  1730. {
  1731. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1732. return(0);
  1733. }
  1734. for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
  1735. *q=(uint32)(*ma);
  1736. o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
  1737. _TIFFfree(p);
  1738. }
  1739. else
  1740. {
  1741. assert(n==2);
  1742. o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
  1743. }
  1744. return(o);
  1745. }
  1746. #endif
  1747. static int
  1748. TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
  1749. {
  1750. static const char module[] = "TIFFWriteDirectoryTagColormap";
  1751. uint32 m;
  1752. uint16* n;
  1753. int o;
  1754. if (dir==NULL)
  1755. {
  1756. (*ndir)++;
  1757. return(1);
  1758. }
  1759. m=(1<<tif->tif_dir.td_bitspersample);
  1760. n=_TIFFmalloc(3*m*sizeof(uint16));
  1761. if (n==NULL)
  1762. {
  1763. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1764. return(0);
  1765. }
  1766. _TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
  1767. _TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
  1768. _TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
  1769. o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
  1770. _TIFFfree(n);
  1771. return(o);
  1772. }
  1773. static int
  1774. TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
  1775. {
  1776. static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
  1777. uint32 m;
  1778. uint16 n;
  1779. uint16* o;
  1780. int p;
  1781. if (dir==NULL)
  1782. {
  1783. (*ndir)++;
  1784. return(1);
  1785. }
  1786. m=(1<<tif->tif_dir.td_bitspersample);
  1787. n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
  1788. /*
  1789. * Check if the table can be written as a single column,
  1790. * or if it must be written as 3 columns. Note that we
  1791. * write a 3-column tag if there are 2 samples/pixel and
  1792. * a single column of data won't suffice--hmm.
  1793. */
  1794. if (n>3)
  1795. n=3;
  1796. if (n==3)
  1797. {
  1798. if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16)))
  1799. n=2;
  1800. }
  1801. if (n==2)
  1802. {
  1803. if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16)))
  1804. n=1;
  1805. }
  1806. if (n==0)
  1807. n=1;
  1808. o=_TIFFmalloc(n*m*sizeof(uint16));
  1809. if (o==NULL)
  1810. {
  1811. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1812. return(0);
  1813. }
  1814. _TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
  1815. if (n>1)
  1816. _TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
  1817. if (n>2)
  1818. _TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
  1819. p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
  1820. _TIFFfree(o);
  1821. return(p);
  1822. }
  1823. static int
  1824. TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
  1825. {
  1826. static const char module[] = "TIFFWriteDirectoryTagSubifd";
  1827. uint64 m;
  1828. int n;
  1829. if (tif->tif_dir.td_nsubifd==0)
  1830. return(1);
  1831. if (dir==NULL)
  1832. {
  1833. (*ndir)++;
  1834. return(1);
  1835. }
  1836. m=tif->tif_dataoff;
  1837. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1838. {
  1839. uint32* o;
  1840. uint64* pa;
  1841. uint32* pb;
  1842. uint16 p;
  1843. o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
  1844. if (o==NULL)
  1845. {
  1846. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  1847. return(0);
  1848. }
  1849. pa=tif->tif_dir.td_subifd;
  1850. pb=o;
  1851. for (p=0; p < tif->tif_dir.td_nsubifd; p++)
  1852. {
  1853. assert(pa != 0);
  1854. assert(*pa <= 0xFFFFFFFFUL);
  1855. *pb++=(uint32)(*pa++);
  1856. }
  1857. n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
  1858. _TIFFfree(o);
  1859. }
  1860. else
  1861. n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
  1862. if (!n)
  1863. return(0);
  1864. /*
  1865. * Total hack: if this directory includes a SubIFD
  1866. * tag then force the next <n> directories to be
  1867. * written as ``sub directories'' of this one. This
  1868. * is used to write things like thumbnails and
  1869. * image masks that one wants to keep out of the
  1870. * normal directory linkage access mechanism.
  1871. */
  1872. tif->tif_flags|=TIFF_INSUBIFD;
  1873. tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
  1874. if (tif->tif_dir.td_nsubifd==1)
  1875. tif->tif_subifdoff=0;
  1876. else
  1877. tif->tif_subifdoff=m;
  1878. return(1);
  1879. }
  1880. static int
  1881. TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
  1882. {
  1883. assert(sizeof(char)==1);
  1884. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
  1885. }
  1886. static int
  1887. TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
  1888. {
  1889. assert(sizeof(uint8)==1);
  1890. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
  1891. }
  1892. #ifdef notdef
  1893. static int
  1894. TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
  1895. {
  1896. assert(sizeof(uint8)==1);
  1897. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
  1898. }
  1899. #endif
  1900. static int
  1901. TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
  1902. {
  1903. assert(sizeof(uint8)==1);
  1904. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
  1905. }
  1906. #ifdef notdef
  1907. static int
  1908. TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
  1909. {
  1910. assert(sizeof(int8)==1);
  1911. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
  1912. }
  1913. #endif
  1914. static int
  1915. TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
  1916. {
  1917. assert(sizeof(int8)==1);
  1918. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
  1919. }
  1920. static int
  1921. TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
  1922. {
  1923. uint16 m;
  1924. assert(sizeof(uint16)==2);
  1925. m=value;
  1926. if (tif->tif_flags&TIFF_SWAB)
  1927. TIFFSwabShort(&m);
  1928. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
  1929. }
  1930. static int
  1931. TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
  1932. {
  1933. assert(count<0x80000000);
  1934. assert(sizeof(uint16)==2);
  1935. if (tif->tif_flags&TIFF_SWAB)
  1936. TIFFSwabArrayOfShort(value,count);
  1937. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
  1938. }
  1939. #ifdef notdef
  1940. static int
  1941. TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
  1942. {
  1943. int16 m;
  1944. assert(sizeof(int16)==2);
  1945. m=value;
  1946. if (tif->tif_flags&TIFF_SWAB)
  1947. TIFFSwabShort((uint16*)(&m));
  1948. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
  1949. }
  1950. #endif
  1951. static int
  1952. TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
  1953. {
  1954. assert(count<0x80000000);
  1955. assert(sizeof(int16)==2);
  1956. if (tif->tif_flags&TIFF_SWAB)
  1957. TIFFSwabArrayOfShort((uint16*)value,count);
  1958. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
  1959. }
  1960. static int
  1961. TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
  1962. {
  1963. uint32 m;
  1964. assert(sizeof(uint32)==4);
  1965. m=value;
  1966. if (tif->tif_flags&TIFF_SWAB)
  1967. TIFFSwabLong(&m);
  1968. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
  1969. }
  1970. static int
  1971. TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
  1972. {
  1973. assert(count<0x40000000);
  1974. assert(sizeof(uint32)==4);
  1975. if (tif->tif_flags&TIFF_SWAB)
  1976. TIFFSwabArrayOfLong(value,count);
  1977. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
  1978. }
  1979. #ifdef notdef
  1980. static int
  1981. TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
  1982. {
  1983. int32 m;
  1984. assert(sizeof(int32)==4);
  1985. m=value;
  1986. if (tif->tif_flags&TIFF_SWAB)
  1987. TIFFSwabLong((uint32*)(&m));
  1988. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
  1989. }
  1990. #endif
  1991. static int
  1992. TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
  1993. {
  1994. assert(count<0x40000000);
  1995. assert(sizeof(int32)==4);
  1996. if (tif->tif_flags&TIFF_SWAB)
  1997. TIFFSwabArrayOfLong((uint32*)value,count);
  1998. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
  1999. }
  2000. #ifdef notdef
  2001. static int
  2002. TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
  2003. {
  2004. uint64 m;
  2005. assert(sizeof(uint64)==8);
  2006. assert(tif->tif_flags&TIFF_BIGTIFF);
  2007. m=value;
  2008. if (tif->tif_flags&TIFF_SWAB)
  2009. TIFFSwabLong8(&m);
  2010. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
  2011. }
  2012. #endif
  2013. static int
  2014. TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  2015. {
  2016. assert(count<0x20000000);
  2017. assert(sizeof(uint64)==8);
  2018. assert(tif->tif_flags&TIFF_BIGTIFF);
  2019. if (tif->tif_flags&TIFF_SWAB)
  2020. TIFFSwabArrayOfLong8(value,count);
  2021. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
  2022. }
  2023. #ifdef notdef
  2024. static int
  2025. TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
  2026. {
  2027. int64 m;
  2028. assert(sizeof(int64)==8);
  2029. assert(tif->tif_flags&TIFF_BIGTIFF);
  2030. m=value;
  2031. if (tif->tif_flags&TIFF_SWAB)
  2032. TIFFSwabLong8((uint64*)(&m));
  2033. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
  2034. }
  2035. #endif
  2036. static int
  2037. TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
  2038. {
  2039. assert(count<0x20000000);
  2040. assert(sizeof(int64)==8);
  2041. assert(tif->tif_flags&TIFF_BIGTIFF);
  2042. if (tif->tif_flags&TIFF_SWAB)
  2043. TIFFSwabArrayOfLong8((uint64*)value,count);
  2044. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
  2045. }
  2046. static int
  2047. TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  2048. {
  2049. static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
  2050. uint32 m[2];
  2051. assert(sizeof(uint32)==4);
  2052. if( value < 0 )
  2053. {
  2054. TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
  2055. return 0;
  2056. }
  2057. else if( value != value )
  2058. {
  2059. TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
  2060. return 0;
  2061. }
  2062. else if (value==0.0)
  2063. {
  2064. m[0]=0;
  2065. m[1]=1;
  2066. }
  2067. else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
  2068. {
  2069. m[0]=(uint32)value;
  2070. m[1]=1;
  2071. }
  2072. else if (value<1.0)
  2073. {
  2074. m[0]=(uint32)(value*0xFFFFFFFF);
  2075. m[1]=0xFFFFFFFF;
  2076. }
  2077. else
  2078. {
  2079. m[0]=0xFFFFFFFF;
  2080. m[1]=(uint32)(0xFFFFFFFF/value);
  2081. }
  2082. if (tif->tif_flags&TIFF_SWAB)
  2083. {
  2084. TIFFSwabLong(&m[0]);
  2085. TIFFSwabLong(&m[1]);
  2086. }
  2087. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
  2088. }
  2089. static int
  2090. TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  2091. {
  2092. static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
  2093. uint32* m;
  2094. float* na;
  2095. uint32* nb;
  2096. uint32 nc;
  2097. int o;
  2098. assert(sizeof(uint32)==4);
  2099. m=_TIFFmalloc(count*2*sizeof(uint32));
  2100. if (m==NULL)
  2101. {
  2102. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2103. return(0);
  2104. }
  2105. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2106. {
  2107. if (*na<=0.0 || *na != *na)
  2108. {
  2109. nb[0]=0;
  2110. nb[1]=1;
  2111. }
  2112. else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
  2113. *na==(float)(uint32)(*na))
  2114. {
  2115. nb[0]=(uint32)(*na);
  2116. nb[1]=1;
  2117. }
  2118. else if (*na<1.0)
  2119. {
  2120. nb[0]=(uint32)((double)(*na)*0xFFFFFFFF);
  2121. nb[1]=0xFFFFFFFF;
  2122. }
  2123. else
  2124. {
  2125. nb[0]=0xFFFFFFFF;
  2126. nb[1]=(uint32)((double)0xFFFFFFFF/(*na));
  2127. }
  2128. }
  2129. if (tif->tif_flags&TIFF_SWAB)
  2130. TIFFSwabArrayOfLong(m,count*2);
  2131. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
  2132. _TIFFfree(m);
  2133. return(o);
  2134. }
  2135. static int
  2136. TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  2137. {
  2138. static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
  2139. int32* m;
  2140. float* na;
  2141. int32* nb;
  2142. uint32 nc;
  2143. int o;
  2144. assert(sizeof(int32)==4);
  2145. m=_TIFFmalloc(count*2*sizeof(int32));
  2146. if (m==NULL)
  2147. {
  2148. TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
  2149. return(0);
  2150. }
  2151. for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
  2152. {
  2153. if (*na<0.0)
  2154. {
  2155. if (*na==(int32)(*na))
  2156. {
  2157. nb[0]=(int32)(*na);
  2158. nb[1]=1;
  2159. }
  2160. else if (*na>-1.0)
  2161. {
  2162. nb[0]=-(int32)((double)(-*na)*0x7FFFFFFF);
  2163. nb[1]=0x7FFFFFFF;
  2164. }
  2165. else
  2166. {
  2167. nb[0]=-0x7FFFFFFF;
  2168. nb[1]=(int32)((double)0x7FFFFFFF/(-*na));
  2169. }
  2170. }
  2171. else
  2172. {
  2173. if (*na==(int32)(*na))
  2174. {
  2175. nb[0]=(int32)(*na);
  2176. nb[1]=1;
  2177. }
  2178. else if (*na<1.0)
  2179. {
  2180. nb[0]=(int32)((double)(*na)*0x7FFFFFFF);
  2181. nb[1]=0x7FFFFFFF;
  2182. }
  2183. else
  2184. {
  2185. nb[0]=0x7FFFFFFF;
  2186. nb[1]=(int32)((double)0x7FFFFFFF/(*na));
  2187. }
  2188. }
  2189. }
  2190. if (tif->tif_flags&TIFF_SWAB)
  2191. TIFFSwabArrayOfLong((uint32*)m,count*2);
  2192. o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
  2193. _TIFFfree(m);
  2194. return(o);
  2195. }
  2196. #ifdef notdef
  2197. static int
  2198. TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
  2199. {
  2200. float m;
  2201. assert(sizeof(float)==4);
  2202. m=value;
  2203. TIFFCvtNativeToIEEEFloat(tif,1,&m);
  2204. if (tif->tif_flags&TIFF_SWAB)
  2205. TIFFSwabFloat(&m);
  2206. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
  2207. }
  2208. #endif
  2209. static int
  2210. TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
  2211. {
  2212. assert(count<0x40000000);
  2213. assert(sizeof(float)==4);
  2214. TIFFCvtNativeToIEEEFloat(tif,count,&value);
  2215. if (tif->tif_flags&TIFF_SWAB)
  2216. TIFFSwabArrayOfFloat(value,count);
  2217. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
  2218. }
  2219. #ifdef notdef
  2220. static int
  2221. TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
  2222. {
  2223. double m;
  2224. assert(sizeof(double)==8);
  2225. m=value;
  2226. TIFFCvtNativeToIEEEDouble(tif,1,&m);
  2227. if (tif->tif_flags&TIFF_SWAB)
  2228. TIFFSwabDouble(&m);
  2229. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
  2230. }
  2231. #endif
  2232. static int
  2233. TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
  2234. {
  2235. assert(count<0x20000000);
  2236. assert(sizeof(double)==8);
  2237. TIFFCvtNativeToIEEEDouble(tif,count,&value);
  2238. if (tif->tif_flags&TIFF_SWAB)
  2239. TIFFSwabArrayOfDouble(value,count);
  2240. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
  2241. }
  2242. static int
  2243. TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
  2244. {
  2245. assert(count<0x40000000);
  2246. assert(sizeof(uint32)==4);
  2247. if (tif->tif_flags&TIFF_SWAB)
  2248. TIFFSwabArrayOfLong(value,count);
  2249. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
  2250. }
  2251. static int
  2252. TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
  2253. {
  2254. assert(count<0x20000000);
  2255. assert(sizeof(uint64)==8);
  2256. assert(tif->tif_flags&TIFF_BIGTIFF);
  2257. if (tif->tif_flags&TIFF_SWAB)
  2258. TIFFSwabArrayOfLong8(value,count);
  2259. return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
  2260. }
  2261. static int
  2262. TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
  2263. {
  2264. static const char module[] = "TIFFWriteDirectoryTagData";
  2265. uint32 m;
  2266. m=0;
  2267. while (m<(*ndir))
  2268. {
  2269. assert(dir[m].tdir_tag!=tag);
  2270. if (dir[m].tdir_tag>tag)
  2271. break;
  2272. m++;
  2273. }
  2274. if (m<(*ndir))
  2275. {
  2276. uint32 n;
  2277. for (n=*ndir; n>m; n--)
  2278. dir[n]=dir[n-1];
  2279. }
  2280. dir[m].tdir_tag=tag;
  2281. dir[m].tdir_type=datatype;
  2282. dir[m].tdir_count=count;
  2283. dir[m].tdir_offset.toff_long8 = 0;
  2284. if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
  2285. _TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
  2286. else
  2287. {
  2288. uint64 na,nb;
  2289. na=tif->tif_dataoff;
  2290. nb=na+datalength;
  2291. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2292. nb=(uint32)nb;
  2293. if ((nb<na)||(nb<datalength))
  2294. {
  2295. TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
  2296. return(0);
  2297. }
  2298. if (!SeekOK(tif,na))
  2299. {
  2300. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
  2301. return(0);
  2302. }
  2303. assert(datalength<0x80000000UL);
  2304. if (!WriteOK(tif,data,(tmsize_t)datalength))
  2305. {
  2306. TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
  2307. return(0);
  2308. }
  2309. tif->tif_dataoff=nb;
  2310. if (tif->tif_dataoff&1)
  2311. tif->tif_dataoff++;
  2312. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2313. {
  2314. uint32 o;
  2315. o=(uint32)na;
  2316. if (tif->tif_flags&TIFF_SWAB)
  2317. TIFFSwabLong(&o);
  2318. _TIFFmemcpy(&dir[m].tdir_offset,&o,4);
  2319. }
  2320. else
  2321. {
  2322. dir[m].tdir_offset.toff_long8 = na;
  2323. if (tif->tif_flags&TIFF_SWAB)
  2324. TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
  2325. }
  2326. }
  2327. (*ndir)++;
  2328. return(1);
  2329. }
  2330. /*
  2331. * Link the current directory into the directory chain for the file.
  2332. */
  2333. static int
  2334. TIFFLinkDirectory(TIFF* tif)
  2335. {
  2336. static const char module[] = "TIFFLinkDirectory";
  2337. tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
  2338. /*
  2339. * Handle SubIFDs
  2340. */
  2341. if (tif->tif_flags & TIFF_INSUBIFD)
  2342. {
  2343. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2344. {
  2345. uint32 m;
  2346. m = (uint32)tif->tif_diroff;
  2347. if (tif->tif_flags & TIFF_SWAB)
  2348. TIFFSwabLong(&m);
  2349. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  2350. if (!WriteOK(tif, &m, 4)) {
  2351. TIFFErrorExt(tif->tif_clientdata, module,
  2352. "Error writing SubIFD directory link");
  2353. return (0);
  2354. }
  2355. /*
  2356. * Advance to the next SubIFD or, if this is
  2357. * the last one configured, revert back to the
  2358. * normal directory linkage.
  2359. */
  2360. if (--tif->tif_nsubifd)
  2361. tif->tif_subifdoff += 4;
  2362. else
  2363. tif->tif_flags &= ~TIFF_INSUBIFD;
  2364. return (1);
  2365. }
  2366. else
  2367. {
  2368. uint64 m;
  2369. m = tif->tif_diroff;
  2370. if (tif->tif_flags & TIFF_SWAB)
  2371. TIFFSwabLong8(&m);
  2372. (void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
  2373. if (!WriteOK(tif, &m, 8)) {
  2374. TIFFErrorExt(tif->tif_clientdata, module,
  2375. "Error writing SubIFD directory link");
  2376. return (0);
  2377. }
  2378. /*
  2379. * Advance to the next SubIFD or, if this is
  2380. * the last one configured, revert back to the
  2381. * normal directory linkage.
  2382. */
  2383. if (--tif->tif_nsubifd)
  2384. tif->tif_subifdoff += 8;
  2385. else
  2386. tif->tif_flags &= ~TIFF_INSUBIFD;
  2387. return (1);
  2388. }
  2389. }
  2390. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2391. {
  2392. uint32 m;
  2393. uint32 nextdir;
  2394. m = (uint32)(tif->tif_diroff);
  2395. if (tif->tif_flags & TIFF_SWAB)
  2396. TIFFSwabLong(&m);
  2397. if (tif->tif_header.classic.tiff_diroff == 0) {
  2398. /*
  2399. * First directory, overwrite offset in header.
  2400. */
  2401. tif->tif_header.classic.tiff_diroff = (uint32) tif->tif_diroff;
  2402. (void) TIFFSeekFile(tif,4, SEEK_SET);
  2403. if (!WriteOK(tif, &m, 4)) {
  2404. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  2405. "Error writing TIFF header");
  2406. return (0);
  2407. }
  2408. return (1);
  2409. }
  2410. /*
  2411. * Not the first directory, search to the last and append.
  2412. */
  2413. nextdir = tif->tif_header.classic.tiff_diroff;
  2414. while(1) {
  2415. uint16 dircount;
  2416. uint32 nextnextdir;
  2417. if (!SeekOK(tif, nextdir) ||
  2418. !ReadOK(tif, &dircount, 2)) {
  2419. TIFFErrorExt(tif->tif_clientdata, module,
  2420. "Error fetching directory count");
  2421. return (0);
  2422. }
  2423. if (tif->tif_flags & TIFF_SWAB)
  2424. TIFFSwabShort(&dircount);
  2425. (void) TIFFSeekFile(tif,
  2426. nextdir+2+dircount*12, SEEK_SET);
  2427. if (!ReadOK(tif, &nextnextdir, 4)) {
  2428. TIFFErrorExt(tif->tif_clientdata, module,
  2429. "Error fetching directory link");
  2430. return (0);
  2431. }
  2432. if (tif->tif_flags & TIFF_SWAB)
  2433. TIFFSwabLong(&nextnextdir);
  2434. if (nextnextdir==0)
  2435. {
  2436. (void) TIFFSeekFile(tif,
  2437. nextdir+2+dircount*12, SEEK_SET);
  2438. if (!WriteOK(tif, &m, 4)) {
  2439. TIFFErrorExt(tif->tif_clientdata, module,
  2440. "Error writing directory link");
  2441. return (0);
  2442. }
  2443. break;
  2444. }
  2445. nextdir=nextnextdir;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. uint64 m;
  2451. uint64 nextdir;
  2452. m = tif->tif_diroff;
  2453. if (tif->tif_flags & TIFF_SWAB)
  2454. TIFFSwabLong8(&m);
  2455. if (tif->tif_header.big.tiff_diroff == 0) {
  2456. /*
  2457. * First directory, overwrite offset in header.
  2458. */
  2459. tif->tif_header.big.tiff_diroff = tif->tif_diroff;
  2460. (void) TIFFSeekFile(tif,8, SEEK_SET);
  2461. if (!WriteOK(tif, &m, 8)) {
  2462. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  2463. "Error writing TIFF header");
  2464. return (0);
  2465. }
  2466. return (1);
  2467. }
  2468. /*
  2469. * Not the first directory, search to the last and append.
  2470. */
  2471. nextdir = tif->tif_header.big.tiff_diroff;
  2472. while(1) {
  2473. uint64 dircount64;
  2474. uint16 dircount;
  2475. uint64 nextnextdir;
  2476. if (!SeekOK(tif, nextdir) ||
  2477. !ReadOK(tif, &dircount64, 8)) {
  2478. TIFFErrorExt(tif->tif_clientdata, module,
  2479. "Error fetching directory count");
  2480. return (0);
  2481. }
  2482. if (tif->tif_flags & TIFF_SWAB)
  2483. TIFFSwabLong8(&dircount64);
  2484. if (dircount64>0xFFFF)
  2485. {
  2486. TIFFErrorExt(tif->tif_clientdata, module,
  2487. "Sanity check on tag count failed, likely corrupt TIFF");
  2488. return (0);
  2489. }
  2490. dircount=(uint16)dircount64;
  2491. (void) TIFFSeekFile(tif,
  2492. nextdir+8+dircount*20, SEEK_SET);
  2493. if (!ReadOK(tif, &nextnextdir, 8)) {
  2494. TIFFErrorExt(tif->tif_clientdata, module,
  2495. "Error fetching directory link");
  2496. return (0);
  2497. }
  2498. if (tif->tif_flags & TIFF_SWAB)
  2499. TIFFSwabLong8(&nextnextdir);
  2500. if (nextnextdir==0)
  2501. {
  2502. (void) TIFFSeekFile(tif,
  2503. nextdir+8+dircount*20, SEEK_SET);
  2504. if (!WriteOK(tif, &m, 8)) {
  2505. TIFFErrorExt(tif->tif_clientdata, module,
  2506. "Error writing directory link");
  2507. return (0);
  2508. }
  2509. break;
  2510. }
  2511. nextdir=nextnextdir;
  2512. }
  2513. }
  2514. return (1);
  2515. }
  2516. /************************************************************************/
  2517. /* TIFFRewriteField() */
  2518. /* */
  2519. /* Rewrite a field in the directory on disk without regard to */
  2520. /* updating the TIFF directory structure in memory. Currently */
  2521. /* only supported for field that already exist in the on-disk */
  2522. /* directory. Mainly used for updating stripoffset / */
  2523. /* stripbytecount values after the directory is already on */
  2524. /* disk. */
  2525. /* */
  2526. /* Returns zero on failure, and one on success. */
  2527. /************************************************************************/
  2528. int
  2529. _TIFFRewriteField(TIFF* tif, uint16 tag, TIFFDataType in_datatype,
  2530. tmsize_t count, void* data)
  2531. {
  2532. static const char module[] = "TIFFResetField";
  2533. /* const TIFFField* fip = NULL; */
  2534. uint16 dircount;
  2535. tmsize_t dirsize;
  2536. uint8 direntry_raw[20];
  2537. uint16 entry_tag = 0;
  2538. uint16 entry_type = 0;
  2539. uint64 entry_count = 0;
  2540. uint64 entry_offset = 0;
  2541. int value_in_entry = 0;
  2542. uint64 read_offset;
  2543. uint8 *buf_to_write = NULL;
  2544. TIFFDataType datatype;
  2545. /* -------------------------------------------------------------------- */
  2546. /* Find field definition. */
  2547. /* -------------------------------------------------------------------- */
  2548. /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
  2549. /* -------------------------------------------------------------------- */
  2550. /* Do some checking this is a straight forward case. */
  2551. /* -------------------------------------------------------------------- */
  2552. if( isMapped(tif) )
  2553. {
  2554. TIFFErrorExt( tif->tif_clientdata, module,
  2555. "Memory mapped files not currently supported for this operation." );
  2556. return 0;
  2557. }
  2558. if( tif->tif_diroff == 0 )
  2559. {
  2560. TIFFErrorExt( tif->tif_clientdata, module,
  2561. "Attempt to reset field on directory not already on disk." );
  2562. return 0;
  2563. }
  2564. /* -------------------------------------------------------------------- */
  2565. /* Read the directory entry count. */
  2566. /* -------------------------------------------------------------------- */
  2567. if (!SeekOK(tif, tif->tif_diroff)) {
  2568. TIFFErrorExt(tif->tif_clientdata, module,
  2569. "%s: Seek error accessing TIFF directory",
  2570. tif->tif_name);
  2571. return 0;
  2572. }
  2573. read_offset = tif->tif_diroff;
  2574. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2575. {
  2576. if (!ReadOK(tif, &dircount, sizeof (uint16))) {
  2577. TIFFErrorExt(tif->tif_clientdata, module,
  2578. "%s: Can not read TIFF directory count",
  2579. tif->tif_name);
  2580. return 0;
  2581. }
  2582. if (tif->tif_flags & TIFF_SWAB)
  2583. TIFFSwabShort(&dircount);
  2584. dirsize = 12;
  2585. read_offset += 2;
  2586. } else {
  2587. uint64 dircount64;
  2588. if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
  2589. TIFFErrorExt(tif->tif_clientdata, module,
  2590. "%s: Can not read TIFF directory count",
  2591. tif->tif_name);
  2592. return 0;
  2593. }
  2594. if (tif->tif_flags & TIFF_SWAB)
  2595. TIFFSwabLong8(&dircount64);
  2596. dircount = (uint16)dircount64;
  2597. dirsize = 20;
  2598. read_offset += 8;
  2599. }
  2600. /* -------------------------------------------------------------------- */
  2601. /* Read through directory to find target tag. */
  2602. /* -------------------------------------------------------------------- */
  2603. while( dircount > 0 )
  2604. {
  2605. if (!ReadOK(tif, direntry_raw, dirsize)) {
  2606. TIFFErrorExt(tif->tif_clientdata, module,
  2607. "%s: Can not read TIFF directory entry.",
  2608. tif->tif_name);
  2609. return 0;
  2610. }
  2611. memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
  2612. if (tif->tif_flags&TIFF_SWAB)
  2613. TIFFSwabShort( &entry_tag );
  2614. if( entry_tag == tag )
  2615. break;
  2616. read_offset += dirsize;
  2617. }
  2618. if( entry_tag != tag )
  2619. {
  2620. TIFFErrorExt(tif->tif_clientdata, module,
  2621. "%s: Could not find tag %d.",
  2622. tif->tif_name, tag );
  2623. return 0;
  2624. }
  2625. /* -------------------------------------------------------------------- */
  2626. /* Extract the type, count and offset for this entry. */
  2627. /* -------------------------------------------------------------------- */
  2628. memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
  2629. if (tif->tif_flags&TIFF_SWAB)
  2630. TIFFSwabShort( &entry_type );
  2631. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2632. {
  2633. uint32 value;
  2634. memcpy( &value, direntry_raw + 4, sizeof(uint32) );
  2635. if (tif->tif_flags&TIFF_SWAB)
  2636. TIFFSwabLong( &value );
  2637. entry_count = value;
  2638. memcpy( &value, direntry_raw + 8, sizeof(uint32) );
  2639. if (tif->tif_flags&TIFF_SWAB)
  2640. TIFFSwabLong( &value );
  2641. entry_offset = value;
  2642. }
  2643. else
  2644. {
  2645. memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
  2646. if (tif->tif_flags&TIFF_SWAB)
  2647. TIFFSwabLong8( &entry_count );
  2648. memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
  2649. if (tif->tif_flags&TIFF_SWAB)
  2650. TIFFSwabLong8( &entry_offset );
  2651. }
  2652. /* -------------------------------------------------------------------- */
  2653. /* What data type do we want to write this as? */
  2654. /* -------------------------------------------------------------------- */
  2655. if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
  2656. {
  2657. if( in_datatype == TIFF_LONG8 )
  2658. datatype = TIFF_LONG;
  2659. else if( in_datatype == TIFF_SLONG8 )
  2660. datatype = TIFF_SLONG;
  2661. else if( in_datatype == TIFF_IFD8 )
  2662. datatype = TIFF_IFD;
  2663. else
  2664. datatype = in_datatype;
  2665. }
  2666. else
  2667. datatype = in_datatype;
  2668. /* -------------------------------------------------------------------- */
  2669. /* Prepare buffer of actual data to write. This includes */
  2670. /* swabbing as needed. */
  2671. /* -------------------------------------------------------------------- */
  2672. buf_to_write =
  2673. (uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
  2674. "for field buffer.");
  2675. if (!buf_to_write)
  2676. return 0;
  2677. if( datatype == in_datatype )
  2678. memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
  2679. else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
  2680. {
  2681. tmsize_t i;
  2682. for( i = 0; i < count; i++ )
  2683. {
  2684. ((int32 *) buf_to_write)[i] =
  2685. (int32) ((int64 *) data)[i];
  2686. if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
  2687. {
  2688. _TIFFfree( buf_to_write );
  2689. TIFFErrorExt( tif->tif_clientdata, module,
  2690. "Value exceeds 32bit range of output type." );
  2691. return 0;
  2692. }
  2693. }
  2694. }
  2695. else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
  2696. || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
  2697. {
  2698. tmsize_t i;
  2699. for( i = 0; i < count; i++ )
  2700. {
  2701. ((uint32 *) buf_to_write)[i] =
  2702. (uint32) ((uint64 *) data)[i];
  2703. if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
  2704. {
  2705. _TIFFfree( buf_to_write );
  2706. TIFFErrorExt( tif->tif_clientdata, module,
  2707. "Value exceeds 32bit range of output type." );
  2708. return 0;
  2709. }
  2710. }
  2711. }
  2712. if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
  2713. {
  2714. if( TIFFDataWidth(datatype) == 2 )
  2715. TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
  2716. else if( TIFFDataWidth(datatype) == 4 )
  2717. TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
  2718. else if( TIFFDataWidth(datatype) == 8 )
  2719. TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
  2720. }
  2721. /* -------------------------------------------------------------------- */
  2722. /* Is this a value that fits into the directory entry? */
  2723. /* -------------------------------------------------------------------- */
  2724. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2725. {
  2726. if( TIFFDataWidth(datatype) * count <= 4 )
  2727. {
  2728. entry_offset = read_offset + 8;
  2729. value_in_entry = 1;
  2730. }
  2731. }
  2732. else
  2733. {
  2734. if( TIFFDataWidth(datatype) * count <= 8 )
  2735. {
  2736. entry_offset = read_offset + 12;
  2737. value_in_entry = 1;
  2738. }
  2739. }
  2740. /* -------------------------------------------------------------------- */
  2741. /* If the tag type, and count match, then we just write it out */
  2742. /* over the old values without altering the directory entry at */
  2743. /* all. */
  2744. /* -------------------------------------------------------------------- */
  2745. if( entry_count == (uint64)count && entry_type == (uint16) datatype )
  2746. {
  2747. if (!SeekOK(tif, entry_offset)) {
  2748. _TIFFfree( buf_to_write );
  2749. TIFFErrorExt(tif->tif_clientdata, module,
  2750. "%s: Seek error accessing TIFF directory",
  2751. tif->tif_name);
  2752. return 0;
  2753. }
  2754. if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
  2755. _TIFFfree( buf_to_write );
  2756. TIFFErrorExt(tif->tif_clientdata, module,
  2757. "Error writing directory link");
  2758. return (0);
  2759. }
  2760. _TIFFfree( buf_to_write );
  2761. return 1;
  2762. }
  2763. /* -------------------------------------------------------------------- */
  2764. /* Otherwise, we write the new tag data at the end of the file. */
  2765. /* -------------------------------------------------------------------- */
  2766. if( !value_in_entry )
  2767. {
  2768. entry_offset = TIFFSeekFile(tif,0,SEEK_END);
  2769. if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
  2770. _TIFFfree( buf_to_write );
  2771. TIFFErrorExt(tif->tif_clientdata, module,
  2772. "Error writing directory link");
  2773. return (0);
  2774. }
  2775. }
  2776. else
  2777. {
  2778. memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
  2779. }
  2780. _TIFFfree( buf_to_write );
  2781. buf_to_write = 0;
  2782. /* -------------------------------------------------------------------- */
  2783. /* Adjust the directory entry. */
  2784. /* -------------------------------------------------------------------- */
  2785. entry_type = datatype;
  2786. memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
  2787. if (tif->tif_flags&TIFF_SWAB)
  2788. TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
  2789. if (!(tif->tif_flags&TIFF_BIGTIFF))
  2790. {
  2791. uint32 value;
  2792. value = (uint32) entry_count;
  2793. memcpy( direntry_raw + 4, &value, sizeof(uint32) );
  2794. if (tif->tif_flags&TIFF_SWAB)
  2795. TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
  2796. value = (uint32) entry_offset;
  2797. memcpy( direntry_raw + 8, &value, sizeof(uint32) );
  2798. if (tif->tif_flags&TIFF_SWAB)
  2799. TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
  2800. }
  2801. else
  2802. {
  2803. memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
  2804. if (tif->tif_flags&TIFF_SWAB)
  2805. TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
  2806. memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
  2807. if (tif->tif_flags&TIFF_SWAB)
  2808. TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
  2809. }
  2810. /* -------------------------------------------------------------------- */
  2811. /* Write the directory entry out to disk. */
  2812. /* -------------------------------------------------------------------- */
  2813. if (!SeekOK(tif, read_offset )) {
  2814. TIFFErrorExt(tif->tif_clientdata, module,
  2815. "%s: Seek error accessing TIFF directory",
  2816. tif->tif_name);
  2817. return 0;
  2818. }
  2819. if (!WriteOK(tif, direntry_raw,dirsize))
  2820. {
  2821. TIFFErrorExt(tif->tif_clientdata, module,
  2822. "%s: Can not write TIFF directory entry.",
  2823. tif->tif_name);
  2824. return 0;
  2825. }
  2826. return 1;
  2827. }
  2828. /* vim: set ts=8 sts=8 sw=8 noet: */
  2829. /*
  2830. * Local Variables:
  2831. * mode: c
  2832. * c-basic-offset: 8
  2833. * fill-column: 78
  2834. * End:
  2835. */