tif_dirwrite.c 86 KB

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