Coverage Report

Created: 2022-10-31 07:00

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