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