LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_dirwrite.c
Go to the documentation of this file.
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 */
35#include "tiffiop.h"
36#include <float.h> /*--: for Rational2Double */
37#include <math.h> /*--: for Rational2Double */
38
39#ifdef HAVE_IEEEFP
53#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
67#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
68#else
69/* If your machine does not support IEEE floating point then you will need to
70 * add support to tif_machdep.c to convert between the native format and
71 * IEEE format. */
85extern void TIFFCvtNativeToIEEEFloat(TIFF *tif, uint32_t n, float *fp);
99extern void TIFFCvtNativeToIEEEDouble(TIFF *tif, uint32_t n, double *dp);
100#endif
101
102static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone,
103 uint64_t *pdiroff);
104
105static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir,
106 TIFFDirEntry *dir,
107 uint16_t tag, uint32_t count,
108 double *value);
109
110static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir,
111 TIFFDirEntry *dir, uint16_t tag,
112 uint32_t count, char *value);
113static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir,
114 TIFFDirEntry *dir, uint16_t tag,
115 uint32_t count, uint8_t *value);
116static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir,
117 TIFFDirEntry *dir, uint16_t tag,
118 uint32_t count, uint8_t *value);
119static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir,
120 TIFFDirEntry *dir, uint16_t tag,
121 uint32_t count, int8_t *value);
122static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir,
123 TIFFDirEntry *dir, uint16_t tag,
124 uint16_t value);
125static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir,
126 TIFFDirEntry *dir, uint16_t tag,
127 uint32_t count, uint16_t *value);
128static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir,
129 TIFFDirEntry *dir, uint16_t tag,
130 uint16_t value);
131static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir,
132 TIFFDirEntry *dir, uint16_t tag,
133 uint32_t count, int16_t *value);
134static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir,
135 TIFFDirEntry *dir, uint16_t tag,
136 uint32_t value);
137static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir,
138 TIFFDirEntry *dir, uint16_t tag,
139 uint32_t count, uint32_t *value);
140static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir,
141 TIFFDirEntry *dir, uint16_t tag,
142 uint32_t count, int32_t *value);
143static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir,
144 TIFFDirEntry *dir, uint16_t tag,
145 uint32_t count, uint64_t *value);
146static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir,
147 TIFFDirEntry *dir, uint16_t tag,
148 uint32_t count, int64_t *value);
149static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir,
150 TIFFDirEntry *dir, uint16_t tag,
151 double value);
152static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir,
153 TIFFDirEntry *dir, uint16_t tag,
154 uint32_t count, float *value);
155static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir,
156 TIFFDirEntry *dir, uint16_t tag,
157 uint32_t count, float *value);
158static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir,
159 TIFFDirEntry *dir, uint16_t tag,
160 uint32_t count, float *value);
161static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir,
162 TIFFDirEntry *dir, uint16_t tag,
163 uint32_t count, double *value);
164static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir,
165 TIFFDirEntry *dir, uint16_t tag,
166 uint32_t count, uint32_t *value);
167static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir,
168 TIFFDirEntry *dir, uint16_t tag,
169 uint32_t value);
170static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir,
171 TIFFDirEntry *dir, uint16_t tag,
172 uint32_t count, uint64_t *value);
173static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir,
174 TIFFDirEntry *dir, uint16_t tag,
175 uint32_t count, uint64_t *value);
176static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir,
177 TIFFDirEntry *dir);
178static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir,
179 TIFFDirEntry *dir);
180static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir,
181 TIFFDirEntry *dir);
182
183static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir,
184 TIFFDirEntry *dir, uint16_t tag,
185 uint32_t count, char *value);
186static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir,
187 TIFFDirEntry *dir,
188 uint16_t tag,
189 uint32_t count,
190 uint8_t *value);
191static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir,
192 TIFFDirEntry *dir,
193 uint16_t tag, uint32_t count,
194 uint8_t *value);
195static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir,
196 TIFFDirEntry *dir,
197 uint16_t tag, uint32_t count,
198 int8_t *value);
199static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir,
200 TIFFDirEntry *dir, uint16_t tag,
201 uint16_t value);
202static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir,
203 TIFFDirEntry *dir,
204 uint16_t tag, uint32_t count,
205 uint16_t *value);
206static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir,
207 TIFFDirEntry *dir,
208 uint16_t tag, uint32_t count,
209 int16_t *value);
210static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir,
211 TIFFDirEntry *dir, uint16_t tag,
212 uint32_t value);
213static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir,
214 TIFFDirEntry *dir,
215 uint16_t tag, uint32_t count,
216 uint32_t *value);
217static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir,
218 TIFFDirEntry *dir,
219 uint16_t tag, uint32_t count,
220 int32_t *value);
221static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir,
222 TIFFDirEntry *dir,
223 uint16_t tag, uint32_t count,
224 uint64_t *value);
225static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir,
226 TIFFDirEntry *dir,
227 uint16_t tag, uint32_t count,
228 int64_t *value);
229static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir,
230 TIFFDirEntry *dir, uint16_t tag,
231 double value);
232static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir,
233 TIFFDirEntry *dir,
234 uint16_t tag,
235 uint32_t count,
236 float *value);
237static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir,
238 TIFFDirEntry *dir,
239 uint16_t tag,
240 uint32_t count,
241 float *value);
242
243/*--: Rational2Double: New functions to support true double-precision for custom
244 * rational tag types. */
245static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir,
246 TIFFDirEntry *dir,
247 uint16_t tag,
248 uint32_t count,
249 double *value);
250static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir,
251 TIFFDirEntry *dir,
252 uint16_t tag,
253 uint32_t count,
254 double *value);
255static int
257 TIFFDirEntry *dir, uint16_t tag,
258 uint32_t count, double *value);
260 TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count,
261 double *value);
262static void DoubleToRational(double value, uint32_t *num, uint32_t *denom);
263static void DoubleToSrational(double value, int32_t *num, int32_t *denom);
264
265static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir,
266 TIFFDirEntry *dir,
267 uint16_t tag, uint32_t count,
268 float *value);
269static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir,
270 TIFFDirEntry *dir,
271 uint16_t tag, uint32_t count,
272 double *value);
273static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir,
274 TIFFDirEntry *dir, uint16_t tag,
275 uint32_t count,
276 uint32_t *value);
277static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir,
278 TIFFDirEntry *dir,
279 uint16_t tag, uint32_t count,
280 uint64_t *value);
281
282static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir,
283 TIFFDirEntry *dir, uint16_t tag,
284 uint16_t datatype, uint32_t count,
285 uint32_t datalength, void *data);
286
287static int TIFFLinkDirectory(TIFF *);
288
289/*
290 * Write the contents of the current directory
291 * to the specified file. This routine doesn't
292 * handle overwriting a directory with auxiliary
293 * storage that's been changed.
294 */
296{
297 return TIFFWriteDirectorySec(tif, TRUE, TRUE, NULL);
298}
299
300/*
301 * This is an advanced writing function that must be used in a particular
302 * sequence, and generally together with TIFFForceStrileArrayWriting(),
303 * to make its intended effect. Its aim is to modify the location
304 * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
305 * More precisely, when TIFFWriteCheck() will be called, the tag entries for
306 * those arrays will be written with type = count = offset = 0 as a temporary
307 * value.
308 *
309 * Its effect is only valid for the current directory, and before
310 * TIFFWriteDirectory() is first called, and will be reset when
311 * changing directory.
312 *
313 * The typical sequence of calls is:
314 * TIFFOpen()
315 * [ TIFFCreateDirectory(tif) ]
316 * Set fields with calls to TIFFSetField(tif, ...)
317 * TIFFDeferStrileArrayWriting(tif)
318 * TIFFWriteCheck(tif, ...)
319 * TIFFWriteDirectory(tif)
320 * ... potentially create other directories and come back to the above directory
321 * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
322 *
323 * Returns 1 in case of success, 0 otherwise.
324 */
326{
327 static const char module[] = "TIFFDeferStrileArrayWriting";
328 if (tif->tif_mode == O_RDONLY)
329 {
330 TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");
331 return 0;
332 }
333 if (tif->tif_diroff != 0)
334 {
335 TIFFErrorExtR(tif, module, "Directory has already been written");
336 return 0;
337 }
338
340 return 1;
341}
342
343/*
344 * Similar to TIFFWriteDirectory(), writes the directory out
345 * but leaves all data structures in memory so that it can be
346 * written again. This will make a partially written TIFF file
347 * readable before it is successfully completed/closed.
348 */
350{
351 int rc;
352 /* Setup the strips arrays, if they haven't already been. */
353 if (tif->tif_dir.td_stripoffset_p == NULL)
354 (void)TIFFSetupStrips(tif);
355 rc = TIFFWriteDirectorySec(tif, TRUE, FALSE, NULL);
356 (void)TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
357 return rc;
358}
359
360int TIFFWriteCustomDirectory(TIFF *tif, uint64_t *pdiroff)
361{
362 return TIFFWriteDirectorySec(tif, FALSE, FALSE, pdiroff);
363}
364
365/*
366 * Similar to TIFFWriteDirectorySec(), but if the directory has already
367 * been written once, it is relocated to the end of the file, in case it
368 * has changed in size. Note that this will result in the loss of the
369 * previously used directory space.
370 */
371
399static int TIFFRewriteDirectorySec(TIFF *tif, int isimage, int imagedone,
400 uint64_t *pdiroff)
401{
402 static const char module[] = "TIFFRewriteDirectory";
403
404 /* We don't need to do anything special if it hasn't been written. */
405 if (tif->tif_diroff == 0)
406 return TIFFWriteDirectory(tif);
407
408 /*
409 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
410 * will cause it to be added after this directories current pre-link.
411 */
412 uint64_t torewritediroff = tif->tif_diroff;
413
414 if (!(tif->tif_flags & TIFF_BIGTIFF))
415 {
416 if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
417 {
419 tif->tif_diroff = 0;
420
421 TIFFSeekFile(tif, 4, SEEK_SET);
422 if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff), 4))
423 {
424 TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header");
425 return (0);
426 }
427 }
428 else if (tif->tif_diroff > 0xFFFFFFFFU)
429 {
430 TIFFErrorExtR(tif, module,
431 "tif->tif_diroff exceeds 32 bit range allowed for "
432 "Classic TIFF");
433 return (0);
434 }
435 else
436 {
437 uint32_t nextdir;
438 tdir_t nextdirnum = 0;
439 nextdir = tif->tif_header.classic.tiff_diroff;
440 while (1)
441 {
442 uint16_t dircount;
443 uint32_t nextnextdir;
444
445 if (!_TIFFCheckDirNumberAndOffset(tif, nextdirnum, nextdir))
446 {
447 TIFFErrorExtR(tif, module, "Error IFD loop detected");
448 return (0);
449 }
450
451 if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2))
452 {
453 TIFFErrorExtR(tif, module,
454 "Error fetching directory count");
455 return (0);
456 }
457 if (tif->tif_flags & TIFF_SWAB)
458 TIFFSwabShort(&dircount);
459 (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12U, SEEK_SET);
460 if (!ReadOK(tif, &nextnextdir, 4))
461 {
462 TIFFErrorExtR(tif, module, "Error fetching directory link");
463 return (0);
464 }
465 if (tif->tif_flags & TIFF_SWAB)
466 TIFFSwabLong(&nextnextdir);
467 if (nextnextdir == tif->tif_diroff)
468 {
469 uint32_t m;
470 m = 0;
471 (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12U,
472 SEEK_SET);
473 if (!WriteOK(tif, &m, 4))
474 {
475 TIFFErrorExtR(tif, module,
476 "Error writing directory link");
477 return (0);
478 }
479 tif->tif_diroff = 0;
480 /* Force a full-traversal to reach the zeroed pointer */
481 tif->tif_lastdiroff = 0;
482 break;
483 }
484 nextdir = nextnextdir;
485 nextdirnum++;
486 }
487 }
488 /* Remove skipped offset from IFD loop directory list. */
489 _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff);
490 }
491 else
492 {
493 if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
494 {
495 tif->tif_header.big.tiff_diroff = 0;
496 tif->tif_diroff = 0;
497
498 TIFFSeekFile(tif, 8, SEEK_SET);
499 if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff), 8))
500 {
501 TIFFErrorExtR(tif, tif->tif_name, "Error updating TIFF header");
502 return (0);
503 }
504 }
505 else
506 {
507 uint64_t nextdir;
508 tdir_t nextdirnum = 0;
509 nextdir = tif->tif_header.big.tiff_diroff;
510 while (1)
511 {
512 uint64_t dircount64;
513 uint64_t nextnextdir;
514
515 if (!_TIFFCheckDirNumberAndOffset(tif, nextdirnum, nextdir))
516 {
517 TIFFErrorExtR(tif, module, "Error IFD loop detected");
518 return (0);
519 }
520
521 if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8))
522 {
523 TIFFErrorExtR(tif, module,
524 "Error fetching directory count");
525 return (0);
526 }
527 if (tif->tif_flags & TIFF_SWAB)
528 TIFFSwabLong8(&dircount64);
529 if (dircount64 > 0xFFFF)
530 {
531 TIFFErrorExtR(tif, module,
532 "Sanity check on tag count failed, likely "
533 "corrupt TIFF");
534 return (0);
535 }
536 (void)TIFFSeekFile(tif, nextdir + 8 + dircount64 * 20,
537 SEEK_SET);
538 if (!ReadOK(tif, &nextnextdir, 8))
539 {
540 TIFFErrorExtR(tif, module, "Error fetching directory link");
541 return (0);
542 }
543 if (tif->tif_flags & TIFF_SWAB)
544 TIFFSwabLong8(&nextnextdir);
545 if (nextnextdir == tif->tif_diroff)
546 {
547 uint64_t m;
548 m = 0;
549 (void)TIFFSeekFile(tif, nextdir + 8 + dircount64 * 20,
550 SEEK_SET);
551 if (!WriteOK(tif, &m, 8))
552 {
553 TIFFErrorExtR(tif, module,
554 "Error writing directory link");
555 return (0);
556 }
557 tif->tif_diroff = 0;
558 /* Force a full-traversal to reach the zeroed pointer */
559 tif->tif_lastdiroff = 0;
560 break;
561 }
562 nextdir = nextnextdir;
563 nextdirnum++;
564 }
565 }
566 /* Remove skipped offset from IFD loop directory list. */
567 _TIFFRemoveEntryFromDirectoryListByOffset(tif, torewritediroff);
568 }
569
570 /*
571 * Now use TIFFWriteDirectorySec() normally.
572 */
573 return TIFFWriteDirectorySec(tif, isimage, imagedone, pdiroff);
574} /*-- TIFFRewriteDirectorySec() --*/
575
576/*
577 * Similar to TIFFWriteDirectory(), but if the directory has already
578 * been written once, it is relocated to the end of the file, in case it
579 * has changed in size. Note that this will result in the loss of the
580 * previously used directory space.
581 */
583{
584 return TIFFRewriteDirectorySec(tif, TRUE, TRUE, NULL);
585}
586
620static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone,
621 uint64_t *pdiroff)
622{
623 static const char module[] = "TIFFWriteDirectorySec";
624 uint32_t ndir;
625 TIFFDirEntry *dir;
626 uint32_t dirsize;
627 void *dirmem;
628 uint32_t m;
629 if (tif->tif_mode == O_RDONLY)
630 return (1);
631
632 _TIFFFillStriles(tif);
633
634 /*
635 * Clear write state so that subsequent images with
636 * different characteristics get the right buffers
637 * setup for them.
638 */
639 if (imagedone)
640 {
641 if (tif->tif_flags & TIFF_POSTENCODE)
642 {
643 tif->tif_flags &= ~TIFF_POSTENCODE;
644 if (!(*tif->tif_postencode)(tif))
645 {
646 TIFFErrorExtR(tif, module,
647 "Error post-encoding before directory write");
648 return (0);
649 }
650 }
651 (*tif->tif_close)(tif); /* shutdown encoder */
652 /*
653 * Flush any data that might have been written
654 * by the compression close+cleanup routines. But
655 * be careful not to write stuff if we didn't add data
656 * in the previous steps as the "rawcc" data may well be
657 * a previously read tile/strip in mixed read/write mode.
658 */
659 if (tif->tif_rawcc > 0 && (tif->tif_flags & TIFF_BEENWRITING) != 0)
660 {
661 if (!TIFFFlushData1(tif))
662 {
663 TIFFErrorExtR(tif, module,
664 "Error flushing data before directory write");
665 return (0);
666 }
667 }
668 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
669 {
670 _TIFFfreeExt(tif, tif->tif_rawdata);
671 tif->tif_rawdata = NULL;
672 tif->tif_rawcp = NULL;
673 tif->tif_rawcc = 0;
674 tif->tif_rawdatasize = 0;
675 tif->tif_rawdataoff = 0;
676 tif->tif_rawdataloaded = 0;
677 }
679 }
680
683 {
684 TIFFWarningExtR(tif, module,
685 "Creating TIFF with legacy Deflate codec identifier, "
686 "COMPRESSION_ADOBE_DEFLATE is more widely supported");
687 }
688 dir = NULL;
689 dirmem = NULL;
690 dirsize = 0;
691 while (1)
692 {
693 /* The first loop only determines "ndir" and uses TIFFLinkDirectory() to
694 * set the offset at which the IFD is to be written to the file.
695 * The second loop writes IFD entries to the file. */
696 ndir = 0;
697 if (dir == NULL)
699 if (isimage)
700 {
701 /*-- Step 1: Process named tags for an image with FIELD bits
702 * associated. --*/
704 {
705 if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
708 goto bad;
710 tif, &ndir, dir, TIFFTAG_IMAGELENGTH,
712 goto bad;
713 }
715 {
716 if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
718 tif->tif_dir.td_tilewidth))
719 goto bad;
720 if (!TIFFWriteDirectoryTagShortLong(tif, &ndir, dir,
723 goto bad;
724 }
726 {
728 tif, &ndir, dir, TIFFTAG_XRESOLUTION,
729 (double)tif->tif_dir.td_xresolution))
730 goto bad;
732 tif, &ndir, dir, TIFFTAG_YRESOLUTION,
733 (double)tif->tif_dir.td_yresolution))
734 goto bad;
735 }
737 {
739 tif, &ndir, dir, TIFFTAG_XPOSITION,
740 (double)tif->tif_dir.td_xposition))
741 goto bad;
743 tif, &ndir, dir, TIFFTAG_YPOSITION,
744 (double)tif->tif_dir.td_yposition))
745 goto bad;
746 }
748 {
749 if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
752 goto bad;
753 }
755 {
757 tif, &ndir, dir, TIFFTAG_BITSPERSAMPLE,
759 goto bad;
760 }
762 {
763 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
766 goto bad;
767 }
769 {
770 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
773 goto bad;
774 }
776 {
777 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
780 goto bad;
781 }
783 {
784 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
786 tif->tif_dir.td_fillorder))
787 goto bad;
788 }
790 {
791 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
794 goto bad;
795 }
797 {
799 tif, &ndir, dir, TIFFTAG_SAMPLESPERPIXEL,
801 goto bad;
802 }
804 {
806 tif, &ndir, dir, TIFFTAG_ROWSPERSTRIP,
808 goto bad;
809 }
811 {
813 tif, &ndir, dir, TIFFTAG_MINSAMPLEVALUE,
815 goto bad;
816 }
818 {
820 tif, &ndir, dir, TIFFTAG_MAXSAMPLEVALUE,
822 goto bad;
823 }
825 {
826 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
829 goto bad;
830 }
832 {
833 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
836 goto bad;
837 }
839 {
841 tif, &ndir, dir, TIFFTAG_PAGENUMBER, 2,
842 &tif->tif_dir.td_pagenumber[0]))
843 goto bad;
844 }
846 {
847 /* Check td_stripbytecount_p for NULL pointer (bug#749) */
848 if (tif->tif_dir.td_stripbytecount_p == NULL)
849 {
851 tif, module,
852 "StripByteCount array is not set, pointer is NULL");
853 goto bad;
854 }
855 if (!isTiled(tif))
856 {
858 tif, &ndir, dir, TIFFTAG_STRIPBYTECOUNTS,
859 tif->tif_dir.td_nstrips,
861 goto bad;
862 }
863 else
864 {
866 tif, &ndir, dir, TIFFTAG_TILEBYTECOUNTS,
867 tif->tif_dir.td_nstrips,
869 goto bad;
870 }
871 }
873 {
874 /* Check td_stripoffset_p for NULL pointer (bug#749) */
875 if (tif->tif_dir.td_stripoffset_p == NULL)
876 {
878 tif, module,
879 "StripByteOffset array is not set, pointer is NULL");
880 goto bad;
881 }
882 if (!isTiled(tif))
883 {
884 /* td_stripoffset_p might be NULL in an odd OJPEG case. See
885 * tif_dirread.c around line 3634.
886 * XXX: OJPEG hack.
887 * If a) compression is OJPEG, b) it's not a tiled TIFF,
888 * and c) the number of strips is 1,
889 * then we tolerate the absence of stripoffsets tag,
890 * because, presumably, all required data is in the
891 * JpegInterchangeFormat stream.
892 * We can get here when using tiffset on such a file.
893 * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
894 */
895 if (tif->tif_dir.td_stripoffset_p != NULL &&
897 tif, &ndir, dir, TIFFTAG_STRIPOFFSETS,
898 tif->tif_dir.td_nstrips,
900 goto bad;
901 }
902 else
903 {
905 tif, &ndir, dir, TIFFTAG_TILEOFFSETS,
906 tif->tif_dir.td_nstrips,
908 goto bad;
909 }
910 }
912 {
913 if (!TIFFWriteDirectoryTagColormap(tif, &ndir, dir))
914 goto bad;
915 }
917 {
918 if (tif->tif_dir.td_extrasamples)
919 {
920 uint16_t na;
921 uint16_t *nb;
924 tif, &ndir, dir, TIFFTAG_EXTRASAMPLES, na, nb))
925 goto bad;
926 }
927 }
929 {
931 tif, &ndir, dir, TIFFTAG_SAMPLEFORMAT,
933 goto bad;
934 }
936 {
938 tif, &ndir, dir, TIFFTAG_SMINSAMPLEVALUE,
941 goto bad;
942 }
944 {
946 tif, &ndir, dir, TIFFTAG_SMAXSAMPLEVALUE,
949 goto bad;
950 }
952 {
953 if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
956 goto bad;
957 }
959 {
960 if (!TIFFWriteDirectoryTagLong(tif, &ndir, dir,
962 tif->tif_dir.td_tiledepth))
963 goto bad;
964 }
966 {
968 tif, &ndir, dir, TIFFTAG_HALFTONEHINTS, 2,
969 &tif->tif_dir.td_halftonehints[0]))
970 goto bad;
971 }
973 {
975 tif, &ndir, dir, TIFFTAG_YCBCRSUBSAMPLING, 2,
977 goto bad;
978 }
980 {
982 tif, &ndir, dir, TIFFTAG_YCBCRPOSITIONING,
984 goto bad;
985 }
987 {
989 tif, &ndir, dir, TIFFTAG_REFERENCEBLACKWHITE, 6,
991 goto bad;
992 }
994 {
995 if (!TIFFWriteDirectoryTagTransferfunction(tif, &ndir, dir))
996 goto bad;
997 }
999 {
1001 tif, &ndir, dir, TIFFTAG_INKNAMES,
1002 (uint32_t)tif->tif_dir.td_inknameslen,
1003 tif->tif_dir.td_inknames))
1004 goto bad;
1005 }
1007 {
1008 if (!TIFFWriteDirectoryTagShort(tif, &ndir, dir,
1011 goto bad;
1012 }
1013 if (TIFFFieldSet(tif, FIELD_SUBIFD))
1014 {
1015 if (!TIFFWriteDirectoryTagSubifd(tif, &ndir, dir))
1016 goto bad;
1017 }
1018 /*-- Step 2: Process named tags for an image with FIELD bits
1019 added by a codec.
1020 Attention: There is only code for some field_types,
1021 which are actually used by current codecs. --*/
1022 {
1023 uint32_t n;
1024 for (n = 0; n < tif->tif_nfields; n++)
1025 {
1026 const TIFFField *o;
1027 o = tif->tif_fields[n];
1028 if ((o->field_bit >= FIELD_CODEC) &&
1029 (TIFFFieldSet(tif, o->field_bit)))
1030 {
1031 /* The field table keeps the tags of a codec that was
1032 * previously set on this directory, and codecs share
1033 * the field bits from FIELD_CODEC on. Such a tag looks
1034 * set, but TIFFGetField() fails and leaves its
1035 * arguments uninitialized: skip it. */
1036 switch (o->set_get_field_type)
1037 {
1038 case TIFF_SETGET_ASCII:
1039 {
1040 uint32_t pa;
1041 char *pb;
1042 assert(o->field_type == TIFF_ASCII);
1043 assert(o->field_readcount == TIFF_VARIABLE);
1044 assert(o->field_passcount == 0);
1045 if (!TIFFGetField(tif, o->field_tag, &pb))
1046 break;
1047 pa = (uint32_t)(strlen(pb) + 1);
1049 tif, &ndir, dir, (uint16_t)o->field_tag,
1050 pa, pb))
1051 goto bad;
1052 }
1053 break;
1054 case TIFF_SETGET_UINT16:
1055 {
1056 uint16_t p;
1057 assert(o->field_type == TIFF_SHORT);
1058 assert(o->field_readcount == 1);
1059 assert(o->field_passcount == 0);
1060 if (!TIFFGetField(tif, o->field_tag, &p))
1061 break;
1063 tif, &ndir, dir, (uint16_t)o->field_tag,
1064 p))
1065 goto bad;
1066 }
1067 break;
1068 case TIFF_SETGET_UINT32:
1069 {
1070 uint32_t p;
1071 assert(o->field_type == TIFF_LONG);
1072 assert(o->field_readcount == 1);
1073 assert(o->field_passcount == 0);
1074 if (!TIFFGetField(tif, o->field_tag, &p))
1075 break;
1077 tif, &ndir, dir, (uint16_t)o->field_tag,
1078 p))
1079 goto bad;
1080 }
1081 break;
1083 {
1084 uint32_t pa;
1085 void *pb;
1086 assert(o->field_type == TIFF_UNDEFINED);
1087 assert(o->field_readcount == TIFF_VARIABLE2);
1088 assert(o->field_passcount == 1);
1089 if (!TIFFGetField(tif, o->field_tag, &pa, &pb))
1090 break;
1092 tif, &ndir, dir, (uint16_t)o->field_tag,
1093 pa, (uint8_t *)pb))
1094 goto bad;
1095 }
1096 break;
1098 case TIFF_SETGET_SINT8:
1099 case TIFF_SETGET_SINT16:
1100 case TIFF_SETGET_SINT32:
1101 case TIFF_SETGET_UINT64:
1102 case TIFF_SETGET_SINT64:
1103 case TIFF_SETGET_FLOAT:
1104 case TIFF_SETGET_DOUBLE:
1105 case TIFF_SETGET_IFD8:
1106 case TIFF_SETGET_INT:
1143 case TIFF_SETGET_UINT8:
1144 case TIFF_SETGET_OTHER:
1145 default:
1147 tif, module,
1148 "Cannot write tag %" PRIu32 " (%s)",
1149 TIFFFieldTag(o),
1150 o->field_name ? o->field_name : "unknown");
1151 goto bad;
1152 }
1153 }
1154 }
1155 }
1156 }
1157 /*-- Step 3: Process custom tags without FIELD bit for an image
1158 * or for custom IFDs (e.g. EXIF) with !isimage. --*/
1159 for (m = 0; m < (uint32_t)(tif->tif_dir.td_customValueCount); m++)
1160 {
1161 uint16_t tag =
1162 (uint16_t)tif->tif_dir.td_customValues[m].info->field_tag;
1163 uint32_t count = (uint32_t)tif->tif_dir.td_customValues[m].count;
1164 switch (tif->tif_dir.td_customValues[m].info->field_type)
1165 {
1166 case TIFF_ASCII:
1168 tif, &ndir, dir, tag, count,
1169 (char *)tif->tif_dir.td_customValues[m].value))
1170 goto bad;
1171 break;
1172 case TIFF_UNDEFINED:
1174 tif, &ndir, dir, tag, count,
1175 (uint8_t *)tif->tif_dir.td_customValues[m].value))
1176 goto bad;
1177 break;
1178 case TIFF_BYTE:
1180 tif, &ndir, dir, tag, count,
1181 (uint8_t *)tif->tif_dir.td_customValues[m].value))
1182 goto bad;
1183 break;
1184 case TIFF_SBYTE:
1186 tif, &ndir, dir, tag, count,
1187 (int8_t *)tif->tif_dir.td_customValues[m].value))
1188 goto bad;
1189 break;
1190 case TIFF_SHORT:
1192 tif, &ndir, dir, tag, count,
1193 (uint16_t *)tif->tif_dir.td_customValues[m].value))
1194 goto bad;
1195 break;
1196 case TIFF_SSHORT:
1198 tif, &ndir, dir, tag, count,
1199 (int16_t *)tif->tif_dir.td_customValues[m].value))
1200 goto bad;
1201 break;
1202 case TIFF_LONG:
1204 tif, &ndir, dir, tag, count,
1205 (uint32_t *)tif->tif_dir.td_customValues[m].value))
1206 goto bad;
1207 break;
1208 case TIFF_SLONG:
1210 tif, &ndir, dir, tag, count,
1211 (int32_t *)tif->tif_dir.td_customValues[m].value))
1212 goto bad;
1213 break;
1214 case TIFF_LONG8:
1216 tif, &ndir, dir, tag, count,
1217 (uint64_t *)tif->tif_dir.td_customValues[m].value))
1218 goto bad;
1219 break;
1220 case TIFF_SLONG8:
1222 tif, &ndir, dir, tag, count,
1223 (int64_t *)tif->tif_dir.td_customValues[m].value))
1224 goto bad;
1225 break;
1226 case TIFF_RATIONAL:
1227 {
1228 /*-- Rational2Double: For Rationals evaluate
1229 * "set_get_field_type" to determine internal storage size.
1230 */
1231 int tv_size;
1232 tv_size = TIFFFieldSetGetSize(
1233 tif->tif_dir.td_customValues[m].info);
1234 if (tv_size == 8)
1235 {
1237 tif, &ndir, dir, tag, count,
1238 (double *)tif->tif_dir.td_customValues[m]
1239 .value))
1240 goto bad;
1241 }
1242 else
1243 {
1244 /*-- default should be tv_size == 4 */
1246 tif, &ndir, dir, tag, count,
1247 (float *)tif->tif_dir.td_customValues[m].value))
1248 goto bad;
1249 /*-- ToDo: After Testing, this should be removed and
1250 * tv_size==4 should be set as default. */
1251 if (tv_size != 4)
1252 {
1254 tif, "TIFFLib: _TIFFWriteDirectorySec()",
1255 "Rational2Double: .set_get_field_type is "
1256 "not 4 but %d",
1257 tv_size);
1258 }
1259 }
1260 }
1261 break;
1262 case TIFF_SRATIONAL:
1263 {
1264 /*-- Rational2Double: For Rationals evaluate
1265 * "set_get_field_type" to determine internal storage size.
1266 */
1267 int tv_size;
1268 tv_size = TIFFFieldSetGetSize(
1269 tif->tif_dir.td_customValues[m].info);
1270 if (tv_size == 8)
1271 {
1273 tif, &ndir, dir, tag, count,
1274 (double *)tif->tif_dir.td_customValues[m]
1275 .value))
1276 goto bad;
1277 }
1278 else
1279 {
1280 /*-- default should be tv_size == 4 */
1282 tif, &ndir, dir, tag, count,
1283 (float *)tif->tif_dir.td_customValues[m].value))
1284 goto bad;
1285 /*-- ToDo: After Testing, this should be removed and
1286 * tv_size==4 should be set as default. */
1287 if (tv_size != 4)
1288 {
1290 tif, "TIFFLib: _TIFFWriteDirectorySec()",
1291 "Rational2Double: .set_get_field_type is "
1292 "not 4 but %d",
1293 tv_size);
1294 }
1295 }
1296 }
1297 break;
1298 case TIFF_FLOAT:
1300 tif, &ndir, dir, tag, count,
1301 (float *)tif->tif_dir.td_customValues[m].value))
1302 goto bad;
1303 break;
1304 case TIFF_DOUBLE:
1306 tif, &ndir, dir, tag, count,
1307 (double *)tif->tif_dir.td_customValues[m].value))
1308 goto bad;
1309 break;
1310 case TIFF_IFD:
1312 tif, &ndir, dir, tag, count,
1313 (uint32_t *)tif->tif_dir.td_customValues[m].value))
1314 goto bad;
1315 break;
1316 case TIFF_IFD8:
1318 tif, &ndir, dir, tag, count,
1319 (uint64_t *)tif->tif_dir.td_customValues[m].value))
1320 goto bad;
1321 break;
1322 case TIFF_NOTYPE:
1323 default:
1324 TIFFErrorExtR(tif, module,
1325 "Cannot write custom field with tag %" PRIu32
1326 ": unhandled field type %u",
1327 (uint32_t)tif->tif_dir.td_customValues[m]
1328 .info->field_tag,
1329 (unsigned)tif->tif_dir.td_customValues[m]
1330 .info->field_type);
1331 goto bad;
1332 }
1333 }
1334 /* "break" if IFD has been written above in second pass.*/
1335 if (dir != NULL)
1336 break;
1337
1338 /* Evaluate IFD data size: Finally, add the size of the IFD tag entries
1339 * themselves. */
1340 if (!(tif->tif_flags & TIFF_BIGTIFF))
1341 tif->tif_dir.td_dirdatasize_write += 2 + ndir * 12 + 4;
1342 else
1343 tif->tif_dir.td_dirdatasize_write += 8 + ndir * 20 + 8;
1344
1345 /* Setup a new directory within first pass. */
1346 dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, ndir, sizeof(TIFFDirEntry),
1347 module);
1348 if (dir == NULL)
1349 {
1350 TIFFErrorExtR(tif, module, "Out of memory");
1351 goto bad;
1352 }
1353 if (isimage)
1354 {
1355 /* Check, weather the IFD to be written is new or an already written
1356 * IFD can be overwritten or needs to be re-written to a different
1357 * location in the file because the IFD is extended with additional
1358 * tags or the IFD data size is increased.
1359 * - tif_diroff == 0, if a new directory has to be linked.
1360 * - tif_diroff != 0, IFD has been re-read from file and will be
1361 * overwritten or re-written.
1362 */
1363 if (tif->tif_diroff == 0)
1364 {
1365 if (!TIFFLinkDirectory(tif))
1366 goto bad;
1367 }
1368 else if (tif->tif_dir.td_dirdatasize_write >
1370 {
1371 if (dir != NULL)
1372 {
1373 _TIFFfreeExt(tif, dir);
1374 dir = NULL;
1375 }
1376 if (!TIFFRewriteDirectorySec(tif, isimage, imagedone, pdiroff))
1377 goto bad;
1378 return (1);
1379 }
1380 }
1381 else
1382 {
1383 /* For !isimage, which means custom-IFD like EXIFIFD or
1384 * checkpointing an IFD, determine whether to overwrite or append at
1385 * the end of the file.
1386 */
1387 if (!((tif->tif_dir.td_dirdatasize_read > 0) &&
1390 {
1391 /* Append at end of file and increment to an even offset. */
1392 const toff_t end_offset = TIFFSeekFile(tif, 0, SEEK_END);
1393 if (end_offset == (toff_t)-1)
1394 {
1395 TIFFErrorExtR(tif, module,
1396 "Error seeking to end of file before "
1397 "writing custom directory");
1398 goto bad;
1399 }
1400 tif->tif_diroff = (end_offset + 1) & (~((toff_t)1));
1401 }
1402 }
1403 /* Return IFD offset */
1404 if (pdiroff != NULL)
1405 *pdiroff = tif->tif_diroff;
1406 if (!(tif->tif_flags & TIFF_BIGTIFF))
1407 dirsize = 2 + ndir * 12 + 4;
1408 else
1409 dirsize = 8 + ndir * 20 + 8;
1410 /* Append IFD data stright after the IFD tag entries.
1411 * Data that does not fit into an IFD tag entry is written to the file
1412 * in the second pass of the while loop. That offset is stored in "dir".
1413 */
1414 tif->tif_dataoff = tif->tif_diroff + dirsize;
1415 if (!(tif->tif_flags & TIFF_BIGTIFF))
1416 tif->tif_dataoff = (uint32_t)tif->tif_dataoff;
1417 if ((tif->tif_dataoff < tif->tif_diroff) ||
1418 (tif->tif_dataoff < (uint64_t)dirsize))
1419 {
1420 TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
1421 goto bad;
1422 }
1423 if (tif->tif_dataoff & 1)
1424 tif->tif_dataoff++;
1425 } /* while() */
1426 if (isimage)
1427 {
1428 /* For SubIFDs remember offset of SubIFD tag within main IFD.
1429 * However, might be already done in TIFFWriteDirectoryTagSubifd() if
1430 * there are more than one SubIFD. */
1431 if (TIFFFieldSet(tif, FIELD_SUBIFD) && (tif->tif_subifdoff == 0))
1432 {
1433 uint32_t na;
1434 TIFFDirEntry *nb;
1435 for (na = 0, nb = dir;; na++, nb++)
1436 {
1437 if (na == ndir)
1438 {
1439 TIFFErrorExtR(tif, module, "Cannot find SubIFD tag");
1440 goto bad;
1441 }
1442 if (nb->tdir_tag == TIFFTAG_SUBIFD)
1443 break;
1444 }
1445 if (!(tif->tif_flags & TIFF_BIGTIFF))
1446 tif->tif_subifdoff = tif->tif_diroff + 2 + na * 12 + 8;
1447 else
1448 tif->tif_subifdoff = tif->tif_diroff + 8 + na * 20 + 12;
1449 }
1450 }
1451 /* Copy/swab IFD entries from "dir" into "dirmem",
1452 * which is then written to file. */
1453 dirmem = _TIFFmallocExt(tif, dirsize);
1454 if (dirmem == NULL)
1455 {
1456 TIFFErrorExtR(tif, module, "Out of memory");
1457 goto bad;
1458 }
1459 if (!(tif->tif_flags & TIFF_BIGTIFF))
1460 {
1461 uint8_t *n;
1462 uint32_t nTmp;
1463 TIFFDirEntry *o;
1464 n = (uint8_t *)dirmem;
1465 *(uint16_t *)n = (uint16_t)ndir;
1466 if (tif->tif_flags & TIFF_SWAB)
1467 TIFFSwabShort((uint16_t *)n);
1468 n += 2;
1469 o = dir;
1470 for (m = 0; m < ndir; m++)
1471 {
1472 *(uint16_t *)n = o->tdir_tag;
1473 if (tif->tif_flags & TIFF_SWAB)
1474 TIFFSwabShort((uint16_t *)n);
1475 n += 2;
1476 *(uint16_t *)n = o->tdir_type;
1477 if (tif->tif_flags & TIFF_SWAB)
1478 TIFFSwabShort((uint16_t *)n);
1479 n += 2;
1480 nTmp = (uint32_t)o->tdir_count;
1481 _TIFFmemcpy(n, &nTmp, 4);
1482 if (tif->tif_flags & TIFF_SWAB)
1483 TIFFSwabLong((uint32_t *)n);
1484 n += 4;
1485 /* This is correct. The data has been */
1486 /* swabbed previously in TIFFWriteDirectoryTagData */
1487 _TIFFmemcpy(n, &o->tdir_offset, 4);
1488 n += 4;
1489 o++;
1490 }
1491 nTmp = (uint32_t)tif->tif_nextdiroff;
1492 if (tif->tif_flags & TIFF_SWAB)
1493 TIFFSwabLong(&nTmp);
1494 _TIFFmemcpy(n, &nTmp, 4);
1495 }
1496 else
1497 {
1498 uint8_t *n;
1499 TIFFDirEntry *o;
1500 n = (uint8_t *)dirmem;
1501 *(uint64_t *)n = ndir;
1502 if (tif->tif_flags & TIFF_SWAB)
1503 TIFFSwabLong8((uint64_t *)n);
1504 n += 8;
1505 o = dir;
1506 for (m = 0; m < ndir; m++)
1507 {
1508 *(uint16_t *)n = o->tdir_tag;
1509 if (tif->tif_flags & TIFF_SWAB)
1510 TIFFSwabShort((uint16_t *)n);
1511 n += 2;
1512 *(uint16_t *)n = o->tdir_type;
1513 if (tif->tif_flags & TIFF_SWAB)
1514 TIFFSwabShort((uint16_t *)n);
1515 n += 2;
1516 _TIFFmemcpy(n, &o->tdir_count, 8);
1517 if (tif->tif_flags & TIFF_SWAB)
1518 TIFFSwabLong8((uint64_t *)n);
1519 n += 8;
1520 _TIFFmemcpy(n, &o->tdir_offset, 8);
1521 n += 8;
1522 o++;
1523 }
1524 _TIFFmemcpy(n, &tif->tif_nextdiroff, 8);
1525 if (tif->tif_flags & TIFF_SWAB)
1526 TIFFSwabLong8((uint64_t *)n);
1527 }
1528 _TIFFfreeExt(tif, dir);
1529 dir = NULL;
1530 if (!SeekOK(tif, tif->tif_diroff))
1531 {
1532 TIFFErrorExtR(tif, module,
1533 "IO error writing directory at seek to offset");
1534 goto bad;
1535 }
1536 if (!WriteOK(tif, dirmem, (tmsize_t)dirsize))
1537 {
1538 TIFFErrorExtR(tif, module, "IO error writing directory");
1539 goto bad;
1540 }
1541 _TIFFfreeExt(tif, dirmem);
1542 dirmem = NULL;
1543
1544 /* Increment tif_curdir if IFD wasn't already written to file and no error
1545 * occurred during IFD writing above. */
1546 if (isimage && !tif->tif_dir.td_iswrittentofile)
1547 {
1548 if (!((tif->tif_flags & TIFF_INSUBIFD) &&
1549 !(TIFFFieldSet(tif, FIELD_SUBIFD))))
1550 {
1551 /*-- Normal main-IFD case --*/
1553 {
1554 tif->tif_curdir = tif->tif_curdircount;
1555 }
1556 else
1557 {
1558 /*ToDo SU: NEW_IFD_CURDIR_INCREMENTING: Delete this
1559 * unexpected case after some testing time. */
1560 /* Attention: tif->tif_curdircount is already set within
1561 * TIFFNumberOfDirectories() */
1563 tif->tif_curdir = tif->tif_curdircount;
1565 tif, module,
1566 "tif_curdircount is TIFF_NON_EXISTENT_DIR_NUMBER, "
1567 "not expected !! Line %d",
1568 __LINE__);
1569 goto bad;
1570 }
1571 }
1572 else
1573 {
1574 /*-- SubIFD case -- */
1575 /* tif_curdir is always set to 0 for all SubIFDs. */
1576 tif->tif_curdir = 0;
1577 }
1578 }
1579 /* Increment tif_curdircount only if main-IFD of an image was not already
1580 * present on file. */
1581 /* Check in combination with (... && !(TIFFFieldSet(tif, FIELD_SUBIFD)))
1582 * is necessary here because TIFF_INSUBIFD was already set above for the
1583 * next SubIFD when this main-IFD (with FIELD_SUBIFD) is currently being
1584 * written. */
1585 if (isimage && !tif->tif_dir.td_iswrittentofile &&
1586 !((tif->tif_flags & TIFF_INSUBIFD) &&
1587 !(TIFFFieldSet(tif, FIELD_SUBIFD))))
1588 tif->tif_curdircount++;
1589
1591
1592 /* Reset SubIFD writing stage after last SubIFD has been written. */
1593 if (imagedone && (tif->tif_flags & TIFF_INSUBIFD) && tif->tif_nsubifd == 0)
1594 tif->tif_flags &= ~TIFF_INSUBIFD;
1595
1596 /* Add or update this directory to the IFD list. */
1598 {
1599 TIFFErrorExtR(tif, module,
1600 "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
1601 ") might cause an IFD loop",
1602 tif->tif_curdir, tif->tif_diroff, tif->tif_diroff);
1603 }
1604
1605 if (imagedone)
1606 {
1607 TIFFFreeDirectory(tif);
1608 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1609 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
1610 /* Reset directory-related state for subsequent directories. */
1612 }
1613 else
1614 {
1615 /* IFD is only checkpointed to file (or a custom IFD like EXIF is
1616 * written), thus set IFD data size written to file. */
1618 }
1619 return (1);
1620bad:
1621 if (dir != NULL)
1622 _TIFFfreeExt(tif, dir);
1623 if (dirmem != NULL)
1624 _TIFFfreeExt(tif, dirmem);
1625 return (0);
1626}
1627
1639static int8_t TIFFClampDoubleToInt8(double val)
1640{
1641 if (val > 127)
1642 return 127;
1643 if (val < -128 || isnan(val))
1644 return -128;
1645 return (int8_t)val;
1646}
1647
1659static int16_t TIFFClampDoubleToInt16(double val)
1660{
1661 if (val > 32767)
1662 return 32767;
1663 if (val < -32768 || isnan(val))
1664 return -32768;
1665 return (int16_t)val;
1666}
1667
1679static int32_t TIFFClampDoubleToInt32(double val)
1680{
1681 if (val > 0x7FFFFFFF)
1682 return 0x7FFFFFFF;
1683 if (val < -0x7FFFFFFF - 1 || isnan(val))
1684 return -0x7FFFFFFF - 1;
1685 return (int32_t)val;
1686}
1687
1699static uint8_t TIFFClampDoubleToUInt8(double val)
1700{
1701 if (val < 0)
1702 return 0;
1703 if (val > 255 || isnan(val))
1704 return 255;
1705 return (uint8_t)val;
1706}
1707
1719static uint16_t TIFFClampDoubleToUInt16(double val)
1720{
1721 if (val < 0)
1722 return 0;
1723 if (val > 65535 || isnan(val))
1724 return 65535;
1725 return (uint16_t)val;
1726}
1727
1739static uint32_t TIFFClampDoubleToUInt32(double val)
1740{
1741 if (val < 0)
1742 return 0;
1743 if (val > 0xFFFFFFFFU || isnan(val))
1744 return 0xFFFFFFFFU;
1745 return (uint32_t)val;
1746}
1747
1777static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir,
1778 TIFFDirEntry *dir,
1779 uint16_t tag, uint32_t count,
1780 double *value)
1781{
1782 static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
1783 void *conv;
1784 uint32_t i;
1785 int ok;
1786 conv = _TIFFCheckMalloc(tif, count, sizeof(double), module);
1787 if (conv == NULL)
1788 {
1789 TIFFErrorExtR(tif, module, "Out of memory");
1790 return (0);
1791 }
1792
1793 switch (tif->tif_dir.td_sampleformat)
1794 {
1796 if (tif->tif_dir.td_bitspersample <= 32)
1797 {
1798 for (i = 0; i < count; ++i)
1799 ((float *)conv)[i] = _TIFFClampDoubleToFloat(value[i]);
1800 ok = TIFFWriteDirectoryTagFloatArray(tif, ndir, dir, tag, count,
1801 (float *)conv);
1802 }
1803 else
1804 {
1805 ok = TIFFWriteDirectoryTagDoubleArray(tif, ndir, dir, tag,
1806 count, value);
1807 }
1808 break;
1809 case SAMPLEFORMAT_INT:
1810 if (tif->tif_dir.td_bitspersample <= 8)
1811 {
1812 for (i = 0; i < count; ++i)
1813 ((int8_t *)conv)[i] = TIFFClampDoubleToInt8(value[i]);
1814 ok = TIFFWriteDirectoryTagSbyteArray(tif, ndir, dir, tag, count,
1815 (int8_t *)conv);
1816 }
1817 else if (tif->tif_dir.td_bitspersample <= 16)
1818 {
1819 for (i = 0; i < count; ++i)
1820 ((int16_t *)conv)[i] = TIFFClampDoubleToInt16(value[i]);
1821 ok = TIFFWriteDirectoryTagSshortArray(tif, ndir, dir, tag,
1822 count, (int16_t *)conv);
1823 }
1824 else
1825 {
1826 for (i = 0; i < count; ++i)
1827 ((int32_t *)conv)[i] = TIFFClampDoubleToInt32(value[i]);
1828 ok = TIFFWriteDirectoryTagSlongArray(tif, ndir, dir, tag, count,
1829 (int32_t *)conv);
1830 }
1831 break;
1832 case SAMPLEFORMAT_UINT:
1833 if (tif->tif_dir.td_bitspersample <= 8)
1834 {
1835 for (i = 0; i < count; ++i)
1836 ((uint8_t *)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
1837 ok = TIFFWriteDirectoryTagByteArray(tif, ndir, dir, tag, count,
1838 (uint8_t *)conv);
1839 }
1840 else if (tif->tif_dir.td_bitspersample <= 16)
1841 {
1842 for (i = 0; i < count; ++i)
1843 ((uint16_t *)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
1844 ok = TIFFWriteDirectoryTagShortArray(tif, ndir, dir, tag, count,
1845 (uint16_t *)conv);
1846 }
1847 else
1848 {
1849 for (i = 0; i < count; ++i)
1850 ((uint32_t *)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
1851 ok = TIFFWriteDirectoryTagLongArray(tif, ndir, dir, tag, count,
1852 (uint32_t *)conv);
1853 }
1854 break;
1855 default:
1856 ok = 0;
1857 }
1858
1859 _TIFFfreeExt(tif, conv);
1860 return (ok);
1861}
1862
1885static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir,
1886 TIFFDirEntry *dir, uint16_t tag,
1887 uint32_t count, char *value)
1888{
1889 return (
1890 TIFFWriteDirectoryTagCheckedAscii(tif, ndir, dir, tag, count, value));
1891}
1892
1915static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir,
1916 TIFFDirEntry *dir, uint16_t tag,
1917 uint32_t count, uint8_t *value)
1918{
1919 return (TIFFWriteDirectoryTagCheckedUndefinedArray(tif, ndir, dir, tag,
1920 count, value));
1921}
1922
1945static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir,
1946 TIFFDirEntry *dir, uint16_t tag,
1947 uint32_t count, uint8_t *value)
1948{
1949 return (TIFFWriteDirectoryTagCheckedByteArray(tif, ndir, dir, tag, count,
1950 value));
1951}
1952
1975static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir,
1976 TIFFDirEntry *dir, uint16_t tag,
1977 uint32_t count, int8_t *value)
1978{
1979 return (TIFFWriteDirectoryTagCheckedSbyteArray(tif, ndir, dir, tag, count,
1980 value));
1981}
1982
2000static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir,
2001 TIFFDirEntry *dir, uint16_t tag,
2002 uint16_t value)
2003{
2004 return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag, value));
2005}
2006
2032static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir,
2033 TIFFDirEntry *dir, uint16_t tag,
2034 uint32_t count, uint16_t *value)
2035{
2036 return (TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count,
2037 value));
2038}
2039
2063static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir,
2064 TIFFDirEntry *dir, uint16_t tag,
2065 uint16_t value)
2066{
2067 static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
2068 uint16_t *m;
2069 uint16_t *na;
2070 uint16_t nb;
2071 int o;
2072 if (dir == NULL)
2073 {
2074 /* only evaluate IFD data size and inc. ndir */
2076 tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, NULL));
2077 }
2078 m = (uint16_t *)_TIFFCheckMalloc(tif, tif->tif_dir.td_samplesperpixel,
2079 sizeof(uint16_t), module);
2080 if (m == NULL)
2081 {
2082 TIFFErrorExtR(tif, module, "Out of memory");
2083 return (0);
2084 }
2085 for (na = m, nb = 0; nb < tif->tif_dir.td_samplesperpixel; na++, nb++)
2086 *na = value;
2088 tif, ndir, dir, tag, tif->tif_dir.td_samplesperpixel, m);
2089 _TIFFfreeExt(tif, m);
2090 return (o);
2091}
2092
2118static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir,
2119 TIFFDirEntry *dir, uint16_t tag,
2120 uint32_t count, int16_t *value)
2121{
2122 return (TIFFWriteDirectoryTagCheckedSshortArray(tif, ndir, dir, tag, count,
2123 value));
2124}
2125
2143static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir,
2144 TIFFDirEntry *dir, uint16_t tag,
2145 uint32_t value)
2146{
2147 return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value));
2148}
2149
2175static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir,
2176 TIFFDirEntry *dir, uint16_t tag,
2177 uint32_t count, uint32_t *value)
2178{
2179 return (TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count,
2180 value));
2181}
2182
2208static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir,
2209 TIFFDirEntry *dir, uint16_t tag,
2210 uint32_t count, int32_t *value)
2211{
2212 return (TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count,
2213 value));
2214}
2215
2216/************************************************************************/
2217/* TIFFWriteDirectoryTagLong8Array() */
2218/* */
2219/* Write either Long8 or Long array depending on file type. */
2220/************************************************************************/
2248static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir,
2249 TIFFDirEntry *dir, uint16_t tag,
2250 uint32_t count, uint64_t *value)
2251{
2252 static const char module[] = "TIFFWriteDirectoryTagLong8Array";
2253 uint64_t *ma;
2254 uint32_t mb;
2255 uint32_t *p;
2256 uint32_t *q;
2257 int o;
2258
2259 /* We always write Long8 for BigTIFF, no checking needed. */
2260 if (tif->tif_flags & TIFF_BIGTIFF)
2261 return (TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag,
2262 count, value));
2263
2264 /*
2265 ** For classic tiff we want to verify everything is in range for long
2266 ** and convert to long format.
2267 */
2268 p = (uint32_t *)_TIFFCheckMalloc(tif, count, sizeof(uint32_t), module);
2269 if (p == NULL)
2270 {
2271 TIFFErrorExtR(tif, module, "Out of memory");
2272 return (0);
2273 }
2274
2275 for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
2276 {
2277 if (*ma > 0xFFFFFFFF)
2278 {
2279 TIFFErrorExtR(tif, module,
2280 "Attempt to write unsigned long value %" PRIu64
2281 " larger than 0xFFFFFFFF for tag %d in Classic TIFF "
2282 "file. TIFF file writing aborted",
2283 *ma, tag);
2284 _TIFFfreeExt(tif, p);
2285 return (0);
2286 }
2287 *q = (uint32_t)(*ma);
2288 }
2289
2290 o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count, p);
2291 _TIFFfreeExt(tif, p);
2292
2293 return (o);
2294}
2295
2296/************************************************************************/
2297/* TIFFWriteDirectoryTagSlong8Array() */
2298/* */
2299/* Write either SLong8 or SLong array depending on file type. */
2300/************************************************************************/
2328static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir,
2329 TIFFDirEntry *dir, uint16_t tag,
2330 uint32_t count, int64_t *value)
2331{
2332 static const char module[] = "TIFFWriteDirectoryTagSlong8Array";
2333 int64_t *ma;
2334 uint32_t mb;
2335 int32_t *p;
2336 int32_t *q;
2337 int o;
2338
2339 /* We always write SLong8 for BigTIFF, no checking needed. */
2340 if (tif->tif_flags & TIFF_BIGTIFF)
2341 return (TIFFWriteDirectoryTagCheckedSlong8Array(tif, ndir, dir, tag,
2342 count, value));
2343
2344 /*
2345 ** For classic tiff we want to verify everything is in range for signed-long
2346 ** and convert to signed-long format.
2347 */
2348 p = (int32_t *)_TIFFCheckMalloc(tif, count, sizeof(int32_t), module);
2349 if (p == NULL)
2350 {
2351 TIFFErrorExtR(tif, module, "Out of memory");
2352 return (0);
2353 }
2354
2355 for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
2356 {
2357 if (*ma > (2147483647))
2358 {
2359 TIFFErrorExtR(tif, module,
2360 "Attempt to write signed long value %" PRIi64
2361 " larger than 0x7FFFFFFF (2147483647) for tag %d in "
2362 "Classic TIFF file. TIFF writing to file aborted",
2363 *ma, tag);
2364 _TIFFfreeExt(tif, p);
2365 return (0);
2366 }
2367 else if (*ma < (-2147483647 - 1))
2368 {
2369 TIFFErrorExtR(tif, module,
2370 "Attempt to write signed long value %" PRIi64
2371 " smaller than 0x80000000 (-2147483648) for tag %d "
2372 "in Classic TIFF file. TIFF writing to file aborted",
2373 *ma, tag);
2374 _TIFFfreeExt(tif, p);
2375 return (0);
2376 }
2377 *q = (int32_t)(*ma);
2378 }
2379
2380 o = TIFFWriteDirectoryTagCheckedSlongArray(tif, ndir, dir, tag, count, p);
2381 _TIFFfreeExt(tif, p);
2382
2383 return (o);
2384}
2385
2407static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir,
2408 TIFFDirEntry *dir, uint16_t tag,
2409 double value)
2410{
2411 return (TIFFWriteDirectoryTagCheckedRational(tif, ndir, dir, tag, value));
2412}
2413
2440static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir,
2441 TIFFDirEntry *dir, uint16_t tag,
2442 uint32_t count, float *value)
2443{
2444 return (TIFFWriteDirectoryTagCheckedRationalArray(tif, ndir, dir, tag,
2445 count, value));
2446}
2447
2474static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir,
2475 TIFFDirEntry *dir, uint16_t tag,
2476 uint32_t count, float *value)
2477{
2478 return (TIFFWriteDirectoryTagCheckedSrationalArray(tif, ndir, dir, tag,
2479 count, value));
2480}
2481
2482/*-- Rational2Double: additional write functions */
2509static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir,
2510 TIFFDirEntry *dir,
2511 uint16_t tag,
2512 uint32_t count,
2513 double *value)
2514{
2515 return (TIFFWriteDirectoryTagCheckedRationalDoubleArray(tif, ndir, dir, tag,
2516 count, value));
2517}
2518
2545static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir,
2546 TIFFDirEntry *dir,
2547 uint16_t tag,
2548 uint32_t count,
2549 double *value)
2550{
2552 tif, ndir, dir, tag, count, value));
2553}
2554
2581static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir,
2582 TIFFDirEntry *dir, uint16_t tag,
2583 uint32_t count, float *value)
2584{
2585 return (TIFFWriteDirectoryTagCheckedFloatArray(tif, ndir, dir, tag, count,
2586 value));
2587}
2588
2615static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir,
2616 TIFFDirEntry *dir, uint16_t tag,
2617 uint32_t count, double *value)
2618{
2619 return (TIFFWriteDirectoryTagCheckedDoubleArray(tif, ndir, dir, tag, count,
2620 value));
2621}
2622
2648static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir,
2649 TIFFDirEntry *dir, uint16_t tag,
2650 uint32_t count, uint32_t *value)
2651{
2652 return (TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count,
2653 value));
2654}
2655
2673static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir,
2674 TIFFDirEntry *dir, uint16_t tag,
2675 uint32_t value)
2676{
2677 if (value <= 0xFFFF)
2678 return (TIFFWriteDirectoryTagCheckedShort(tif, ndir, dir, tag,
2679 (uint16_t)value));
2680 else
2681 return (TIFFWriteDirectoryTagCheckedLong(tif, ndir, dir, tag, value));
2682}
2683
2700static int _WriteAsType(TIFF *tif, uint64_t strile_size,
2701 uint64_t uncompressed_threshold)
2702{
2703 const uint16_t compression = tif->tif_dir.td_compression;
2704 if (compression == COMPRESSION_NONE)
2705 {
2706 return strile_size > uncompressed_threshold;
2707 }
2708 else if (compression == COMPRESSION_JPEG ||
2709 compression == COMPRESSION_LZW ||
2710 compression == COMPRESSION_ADOBE_DEFLATE ||
2711 compression == COMPRESSION_DEFLATE ||
2712 compression == COMPRESSION_LZMA ||
2713 compression == COMPRESSION_LERC ||
2714 compression == COMPRESSION_ZSTD ||
2715 compression == COMPRESSION_WEBP || compression == COMPRESSION_JXL)
2716 {
2717 /* For a few select compression types, we assume that in the worst */
2718 /* case the compressed size will be 10 times the uncompressed size. */
2719 /* This is overly pessismistic ! */
2720 return strile_size >= uncompressed_threshold / 10;
2721 }
2722 return 1;
2723}
2724
2740static int WriteAsLong8(TIFF *tif, uint64_t strile_size)
2741{
2742 return _WriteAsType(tif, strile_size, 0xFFFFFFFFU);
2743}
2744
2760static int WriteAsLong4(TIFF *tif, uint64_t strile_size)
2761{
2762 return _WriteAsType(tif, strile_size, 0xFFFFU);
2763}
2764
2765/************************************************************************/
2766/* TIFFWriteDirectoryTagLongLong8Array() */
2767/* */
2768/* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */
2769/* on strile size and Classic/BigTIFF mode. */
2770/************************************************************************/
2771
2805static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir,
2806 TIFFDirEntry *dir, uint16_t tag,
2807 uint32_t count, uint64_t *value)
2808{
2809 static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
2810 int o;
2811 int write_aslong4;
2812
2814 {
2815 if (dir == NULL)
2816 {
2817 /* This is just a counting pass to count IFD entries.
2818 * For deferstrilearraywriting no extra bytes will be written
2819 * into IFD space. */
2820 (*ndir)++;
2821 return 1;
2822 }
2823 return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0,
2824 NULL);
2825 }
2826
2827 if (tif->tif_flags & TIFF_BIGTIFF)
2828 {
2829 int write_aslong8 = 1;
2830 /* In the case of ByteCounts array, we may be able to write them on LONG
2831 * if the strip/tilesize is not too big. Also do that for count > 1 in
2832 * the case someone would want to create a single-strip file with a
2833 * growing height, in which case using LONG8 will be safer. */
2834 if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
2835 {
2836 write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
2837 }
2838 else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
2839 {
2840 write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
2841 }
2842 if (write_aslong8)
2843 {
2844 return TIFFWriteDirectoryTagCheckedLong8Array(tif, ndir, dir, tag,
2845 count, value);
2846 }
2847 }
2848
2849 write_aslong4 = 1;
2850 if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
2851 {
2852 write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
2853 }
2854 else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
2855 {
2856 write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
2857 }
2858 if (write_aslong4)
2859 {
2860 /*
2861 ** For classic tiff we want to verify everything is in range for LONG
2862 ** and convert to long format.
2863 */
2864
2865 uint32_t *p =
2866 (uint32_t *)_TIFFCheckMalloc(tif, count, sizeof(uint32_t), module);
2867 uint32_t *q;
2868 uint64_t *ma;
2869 uint32_t mb;
2870
2871 if (p == NULL)
2872 {
2873 TIFFErrorExtR(tif, module, "Out of memory");
2874 return (0);
2875 }
2876
2877 for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
2878 {
2879 if (*ma > 0xFFFFFFFF)
2880 {
2881 TIFFErrorExtR(tif, module,
2882 "Attempt to write value larger than 0xFFFFFFFF "
2883 "in LONG array.");
2884 _TIFFfreeExt(tif, p);
2885 return (0);
2886 }
2887 *q = (uint32_t)(*ma);
2888 }
2889
2890 o = TIFFWriteDirectoryTagCheckedLongArray(tif, ndir, dir, tag, count,
2891 p);
2892 _TIFFfreeExt(tif, p);
2893 }
2894 else
2895 {
2896 uint16_t *p =
2897 (uint16_t *)_TIFFCheckMalloc(tif, count, sizeof(uint16_t), module);
2898 uint16_t *q;
2899 uint64_t *ma;
2900 uint32_t mb;
2901
2902 if (p == NULL)
2903 {
2904 TIFFErrorExtR(tif, module, "Out of memory");
2905 return (0);
2906 }
2907
2908 for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
2909 {
2910 if (*ma > 0xFFFF)
2911 {
2912 /* Should not happen normally given the check we did before */
2913 TIFFErrorExtR(tif, module,
2914 "Attempt to write value larger than 0xFFFF in "
2915 "SHORT array.");
2916 _TIFFfreeExt(tif, p);
2917 return (0);
2918 }
2919 *q = (uint16_t)(*ma);
2920 }
2921
2922 o = TIFFWriteDirectoryTagCheckedShortArray(tif, ndir, dir, tag, count,
2923 p);
2924 _TIFFfreeExt(tif, p);
2925 }
2926
2927 return (o);
2928}
2929
2930/************************************************************************/
2931/* TIFFWriteDirectoryTagIfdIfd8Array() */
2932/* */
2933/* Write either IFD8 or IFD array depending on file type. */
2934/************************************************************************/
2935
2963static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir,
2964 TIFFDirEntry *dir, uint16_t tag,
2965 uint32_t count, uint64_t *value)
2966{
2967 static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
2968 uint64_t *ma;
2969 uint32_t mb;
2970 uint32_t *p;
2971 uint32_t *q;
2972 int o;
2973
2974 /* We always write IFD8 for BigTIFF, no checking needed. */
2975 if (tif->tif_flags & TIFF_BIGTIFF)
2976 return TIFFWriteDirectoryTagCheckedIfd8Array(tif, ndir, dir, tag, count,
2977 value);
2978
2979 /*
2980 ** For classic tiff we want to verify everything is in range for IFD
2981 ** and convert to long format.
2982 */
2983
2984 p = (uint32_t *)_TIFFCheckMalloc(tif, count, sizeof(uint32_t), module);
2985 if (p == NULL)
2986 {
2987 TIFFErrorExtR(tif, module, "Out of memory");
2988 return (0);
2989 }
2990
2991 for (q = p, ma = value, mb = 0; mb < count; ma++, mb++, q++)
2992 {
2993 if (*ma > 0xFFFFFFFF)
2994 {
2995 TIFFErrorExtR(tif, module,
2996 "Attempt to write value larger than 0xFFFFFFFF in "
2997 "Classic TIFF file.");
2998 _TIFFfreeExt(tif, p);
2999 return (0);
3000 }
3001 *q = (uint32_t)(*ma);
3002 }
3003
3004 o = TIFFWriteDirectoryTagCheckedIfdArray(tif, ndir, dir, tag, count, p);
3005 _TIFFfreeExt(tif, p);
3006
3007 return (o);
3008}
3009
3010/*
3011 * Auxiliary function to determine the IFD data size to be written to the file.
3012 * The IFD data size is finally the size of the IFD tag entries plus the IFD
3013 * data that is written directly after the IFD tag entries.
3014 */
3030static void EvaluateIFDdatasizeWrite(TIFF *tif, uint32_t count,
3031 uint32_t typesize, uint32_t *ndir)
3032{
3033 uint64_t datalength = (uint64_t)count * typesize;
3034 if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
3035 {
3036 /* LibTIFF increments write address to an even offset, thus datalength
3037 * written is also incremented. */
3038 if (datalength & 1)
3039 datalength++;
3040 tif->tif_dir.td_dirdatasize_write += datalength;
3041 }
3042 (*ndir)++;
3043}
3044
3071static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir,
3072 TIFFDirEntry *dir)
3073{
3074 static const char module[] = "TIFFWriteDirectoryTagColormap";
3075 uint32_t m;
3076 uint32_t count;
3077 uint64_t count64;
3078 tmsize_t total_values;
3079 tmsize_t plane_bytes;
3080 uint16_t *n;
3081 int o;
3082 m = tif->tif_dir.td_colormap_count;
3083 if (m == 0)
3084 {
3085 TIFFWarningExtR(tif, module,
3086 "No ColorMap entries stored. Tag not written to file");
3087 return (1); /* Not an error; only tag is not written. */
3088 }
3089 count64 = _TIFFMultiply64(tif, 3U, m, module);
3090 if (count64 == 0)
3091 return (0);
3092 count = _TIFFCastUInt64ToUInt32(tif, count64, module);
3093 total_values = _TIFFCastUInt64ToSSize(tif, count64, module);
3094 plane_bytes = _TIFFCastUInt64ToSSize(
3095 tif, _TIFFMultiply64(tif, m, sizeof(uint16_t), module), module);
3096 if (count == 0 || total_values == 0 || plane_bytes == 0)
3097 return (0);
3098 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3099 {
3100 EvaluateIFDdatasizeWrite(tif, count, sizeof(uint16_t), ndir);
3101 return 1;
3102 }
3103
3104 n = (uint16_t *)_TIFFCheckMalloc(tif, total_values, sizeof(uint16_t),
3105 module);
3106 if (n == NULL)
3107 {
3108 TIFFErrorExtR(tif, module, "Out of memory");
3109 return (0);
3110 }
3111 _TIFFmemcpy(&n[0], tif->tif_dir.td_colormap[0], plane_bytes);
3112 _TIFFmemcpy(&n[m], tif->tif_dir.td_colormap[1], plane_bytes);
3113 _TIFFmemcpy(&n[2 * m], tif->tif_dir.td_colormap[2], plane_bytes);
3115 count, n);
3116 _TIFFfreeExt(tif, n);
3117 return (o);
3118}
3119
3146static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir,
3147 TIFFDirEntry *dir)
3148{
3149 static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
3150 uint32_t m;
3151 uint32_t count;
3152 uint64_t count64;
3153 tmsize_t total_values;
3154 tmsize_t plane_bytes;
3155 uint16_t n;
3156 uint16_t *o;
3157 int p;
3158 /* TIFFTAG_TRANSFERFUNCTION is (1 or 3) arrays of
3159 * td_transferfunction_count uint16_t values, that count being the
3160 * length they were allocated with.
3161 */
3163 if (m == 0)
3164 {
3166 tif, module,
3167 "No TransferFunction entries stored. Tag not written to file");
3168 return (1); /* Not an error; only tag is not written. */
3169 }
3170 plane_bytes = _TIFFCastUInt64ToSSize(
3171 tif, _TIFFMultiply64(tif, m, sizeof(uint16_t), module), module);
3172 if (plane_bytes == 0)
3173 return (0);
3174 /* clang-format off */
3175 n = (tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples) > 1 ? 3 : 1;
3176 /* clang-format on */
3177
3178 /* Check for proper number of transferfunctions */
3179 for (int i = 0; i < n; i++)
3180 {
3181 if (tif->tif_dir.td_transferfunction[i] == NULL)
3182 {
3183 TIFFWarningExtR(tif, module,
3184 "Too few TransferFunctions provided. Tag "
3185 "not written to file");
3186 return (1); /* Not an error; only tag is not written. */
3187 }
3188 }
3189 /*
3190 * Check if the table can be written as a single column,
3191 * or if it must be written as 3 columns. Note that we
3192 * write a 3-column tag if there are 2 samples/pixel and
3193 * a single column of data won't suffice--hmm.
3194 */
3195 if (n == 3)
3196 {
3198 tif->tif_dir.td_transferfunction[2], plane_bytes) &&
3200 tif->tif_dir.td_transferfunction[1], plane_bytes))
3201 n = 1;
3202 }
3203 count64 = _TIFFMultiply64(tif, n, m, module);
3204 if (count64 == 0)
3205 return (0);
3206 count = _TIFFCastUInt64ToUInt32(tif, count64, module);
3207 total_values = _TIFFCastUInt64ToSSize(tif, count64, module);
3208 if (count == 0 || total_values == 0)
3209 return (0);
3210 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3211 {
3212 EvaluateIFDdatasizeWrite(tif, count, 2, ndir);
3213 return 1;
3214 }
3215
3216 o = (uint16_t *)_TIFFCheckMalloc(tif, total_values, sizeof(uint16_t),
3217 module);
3218 if (o == NULL)
3219 {
3220 TIFFErrorExtR(tif, module, "Out of memory");
3221 return (0);
3222 }
3223 _TIFFmemcpy(&o[0], tif->tif_dir.td_transferfunction[0], plane_bytes);
3224 if (n > 1)
3225 _TIFFmemcpy(&o[m], tif->tif_dir.td_transferfunction[1], plane_bytes);
3226 if (n > 2)
3227 _TIFFmemcpy(&o[2 * m], tif->tif_dir.td_transferfunction[2],
3228 plane_bytes);
3230 tif, ndir, dir, TIFFTAG_TRANSFERFUNCTION, count, o);
3231 _TIFFfreeExt(tif, o);
3232 return (p);
3233}
3234
3263static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir,
3264 TIFFDirEntry *dir)
3265{
3266 static const char module[] = "TIFFWriteDirectoryTagSubifd";
3267 uint64_t m;
3268 int n;
3269 if (tif->tif_dir.td_nsubifd == 0)
3270 return (1);
3271 m = tif->tif_dataoff;
3272 if (!(tif->tif_flags & TIFF_BIGTIFF))
3273 {
3274 uint32_t *o;
3275 uint64_t *pa;
3276 uint32_t *pb;
3277 uint16_t p;
3278 o = (uint32_t *)_TIFFCheckMalloc(tif, tif->tif_dir.td_nsubifd,
3279 sizeof(uint32_t), module);
3280 if (o == NULL)
3281 {
3282 TIFFErrorExtR(tif, module, "Out of memory");
3283 return (0);
3284 }
3285 pa = tif->tif_dir.td_subifd;
3286 pb = o;
3287 for (p = 0; p < tif->tif_dir.td_nsubifd; p++)
3288 {
3289 assert(pa != 0);
3290
3291 /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which
3292 * is illegal) */
3293 if (*pa > 0xFFFFFFFFUL)
3294 {
3295 TIFFErrorExtR(tif, module, "Illegal value for SubIFD tag");
3296 _TIFFfreeExt(tif, o);
3297 return (0);
3298 }
3299 *pb++ = (uint32_t)(*pa++);
3300 }
3302 tif->tif_dir.td_nsubifd, o);
3303 _TIFFfreeExt(tif, o);
3304 }
3305 else
3307 tif, ndir, dir, TIFFTAG_SUBIFD, tif->tif_dir.td_nsubifd,
3308 tif->tif_dir.td_subifd);
3309
3310 if (dir == NULL)
3311 /* Just have evaluated IFD data size and incremented ndir
3312 * above in sub-functions. */
3313 return (n);
3314
3315 if (!n)
3316 return (0);
3317 /*
3318 * Total hack: if this directory includes a SubIFD
3319 * tag then force the next <n> directories to be
3320 * written as ``sub directories'' of this one. This
3321 * is used to write things like thumbnails and
3322 * image masks that one wants to keep out of the
3323 * normal directory linkage access mechanism.
3324 */
3325 tif->tif_flags |= TIFF_INSUBIFD;
3326 tif->tif_nsubifd = tif->tif_dir.td_nsubifd;
3327 if (tif->tif_dir.td_nsubifd == 1)
3328 tif->tif_subifdoff = 0;
3329 else
3330 tif->tif_subifdoff = m;
3331 return (1);
3332}
3333
3356static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir,
3357 TIFFDirEntry *dir, uint16_t tag,
3358 uint32_t count, char *value)
3359{
3360 assert(sizeof(char) == 1);
3361 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3362 {
3363 EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
3364 return 1;
3365 }
3366 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_ASCII, count,
3367 count, value));
3368}
3369
3392static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir,
3393 TIFFDirEntry *dir,
3394 uint16_t tag,
3395 uint32_t count,
3396 uint8_t *value)
3397{
3398 assert(sizeof(uint8_t) == 1);
3399 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3400 {
3401 EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
3402 return 1;
3403 }
3404 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_UNDEFINED,
3405 count, count, value));
3406}
3407
3430static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir,
3431 TIFFDirEntry *dir,
3432 uint16_t tag, uint32_t count,
3433 uint8_t *value)
3434{
3435 assert(sizeof(uint8_t) == 1);
3436 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3437 {
3438 EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
3439 return 1;
3440 }
3441 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_BYTE, count,
3442 count, value));
3443}
3444
3467static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir,
3468 TIFFDirEntry *dir,
3469 uint16_t tag, uint32_t count,
3470 int8_t *value)
3471{
3472 assert(sizeof(int8_t) == 1);
3473 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3474 {
3475 EvaluateIFDdatasizeWrite(tif, count, 1, ndir);
3476 return 1;
3477 }
3478 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SBYTE, count,
3479 count, value));
3480}
3481
3499static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir,
3500 TIFFDirEntry *dir, uint16_t tag,
3501 uint16_t value)
3502{
3503 uint16_t m;
3504 assert(sizeof(uint16_t) == 2);
3505 if (dir == NULL)
3506 {
3507 /* No additional data to IFD data size just increment ndir. */
3508 (*ndir)++;
3509 return 1;
3510 }
3511 m = value;
3512 if (tif->tif_flags & TIFF_SWAB)
3513 TIFFSwabShort(&m);
3514 return (
3515 TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, 1, 2, &m));
3516}
3517
3543static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir,
3544 TIFFDirEntry *dir,
3545 uint16_t tag, uint32_t count,
3546 uint16_t *value)
3547{
3548 static const char module[] = "TIFFWriteDirectoryTagCheckedShortArray";
3549 if (count > UINT32_MAX / 2)
3550 {
3551 TIFFErrorExtR(tif, module,
3552 "Tag %" PRIu16 ": count %" PRIu32
3553 " exceeds maximum of %" PRIu32 " entries",
3554 tag, count, UINT32_MAX / 2);
3555 return (0);
3556 }
3557 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3558 {
3559 EvaluateIFDdatasizeWrite(tif, count, 2, ndir);
3560 return 1;
3561 }
3562 if (tif->tif_flags & TIFF_SWAB)
3563 TIFFSwabArrayOfShort(value, count);
3564 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SHORT, count,
3565 count * 2, value));
3566}
3567
3592static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir,
3593 TIFFDirEntry *dir,
3594 uint16_t tag, uint32_t count,
3595 int16_t *value)
3596{
3597 static const char module[] = "TIFFWriteDirectoryTagCheckedSshortArray";
3598 if (count > UINT32_MAX / 2)
3599 {
3600 TIFFErrorExtR(tif, module,
3601 "Tag %" PRIu16 ": count %" PRIu32
3602 " exceeds maximum of %" PRIu32 " entries",
3603 tag, count, UINT32_MAX / 2);
3604 return (0);
3605 }
3606 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3607 {
3608 EvaluateIFDdatasizeWrite(tif, count, 2, ndir);
3609 return 1;
3610 }
3611 if (tif->tif_flags & TIFF_SWAB)
3612 TIFFSwabArrayOfShort((uint16_t *)value, count);
3613 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SSHORT, count,
3614 count * 2, value));
3615}
3616
3634static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir,
3635 TIFFDirEntry *dir, uint16_t tag,
3636 uint32_t value)
3637{
3638 uint32_t m;
3639 assert(sizeof(uint32_t) == 4);
3640 if (dir == NULL)
3641 {
3642 /* No additional data to IFD data size just increment ndir. */
3643 (*ndir)++;
3644 return 1;
3645 }
3646 m = value;
3647 if (tif->tif_flags & TIFF_SWAB)
3648 TIFFSwabLong(&m);
3649 return (
3650 TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, 1, 4, &m));
3651}
3652
3677static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir,
3678 TIFFDirEntry *dir,
3679 uint16_t tag, uint32_t count,
3680 uint32_t *value)
3681{
3682 if (count > UINT32_MAX / 4)
3683 return (0);
3684 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3685 {
3686 EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
3687 return 1;
3688 }
3689 if (tif->tif_flags & TIFF_SWAB)
3690 TIFFSwabArrayOfLong(value, count);
3691 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG, count,
3692 count * 4, value));
3693}
3694
3719static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir,
3720 TIFFDirEntry *dir,
3721 uint16_t tag, uint32_t count,
3722 int32_t *value)
3723{
3724 if (count > UINT32_MAX / 4)
3725 return (0);
3726 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3727 {
3728 EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
3729 return 1;
3730 }
3731 if (tif->tif_flags & TIFF_SWAB)
3732 TIFFSwabArrayOfLong((uint32_t *)value, count);
3733 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG, count,
3734 count * 4, value));
3735}
3736
3764static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir,
3765 TIFFDirEntry *dir,
3766 uint16_t tag, uint32_t count,
3767 uint64_t *value)
3768{
3769 if (!(tif->tif_flags & TIFF_BIGTIFF))
3770 {
3771 TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedLong8Array",
3772 "LONG8 not allowed for ClassicTIFF");
3773 return (0);
3774 }
3775 if (count > UINT32_MAX / 8)
3776 return (0);
3777 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3778 {
3779 EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
3780 return 1;
3781 }
3782 if (tif->tif_flags & TIFF_SWAB)
3783 TIFFSwabArrayOfLong8(value, count);
3784 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_LONG8, count,
3785 count * 8, value));
3786}
3787
3815static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir,
3816 TIFFDirEntry *dir,
3817 uint16_t tag, uint32_t count,
3818 int64_t *value)
3819{
3820 if (!(tif->tif_flags & TIFF_BIGTIFF))
3821 {
3822 TIFFErrorExtR(tif, "TIFFWriteDirectoryTagCheckedSlong8Array",
3823 "SLONG8 not allowed for ClassicTIFF");
3824 return (0);
3825 }
3826 if (count > UINT32_MAX / 8)
3827 return (0);
3828 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3829 {
3830 EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
3831 return 1;
3832 }
3833 if (tif->tif_flags & TIFF_SWAB)
3834 TIFFSwabArrayOfLong8((uint64_t *)value, count);
3835 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SLONG8, count,
3836 count * 8, value));
3837}
3838
3859static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir,
3860 TIFFDirEntry *dir, uint16_t tag,
3861 double value)
3862{
3863 static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
3864 uint32_t m[2];
3865 assert(sizeof(uint32_t) == 4);
3866 if (value < 0)
3867 {
3868 TIFFErrorExtR(tif, module, "Negative value is illegal");
3869 return 0;
3870 }
3871 else if (isnan(value))
3872 {
3873 TIFFErrorExtR(tif, module, "Not-a-number value is illegal");
3874 return 0;
3875 }
3876
3877 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3878 {
3880 (tif->tif_flags & TIFF_BIGTIFF) ? 0 : 0x8U;
3881 (*ndir)++;
3882 return 1;
3883 }
3884
3885 DoubleToRational(value, &m[0], &m[1]);
3886
3887 if (tif->tif_flags & TIFF_SWAB)
3888 {
3889 TIFFSwabLong(&m[0]);
3890 TIFFSwabLong(&m[1]);
3891 }
3892 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, 1, 8,
3893 &m[0]));
3894}
3895
3922static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir,
3923 TIFFDirEntry *dir,
3924 uint16_t tag,
3925 uint32_t count,
3926 float *value)
3927{
3928 static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
3929 uint32_t *m;
3930 float *na;
3931 uint32_t *nb;
3932 uint32_t nc;
3933 int o;
3934 if (count > UINT32_MAX / 8)
3935 {
3936 TIFFErrorExtR(tif, module,
3937 "Tag %" PRIu16 ": count %" PRIu32
3938 " exceeds maximum of %" PRIu32 " entries",
3939 tag, count, UINT32_MAX / 8);
3940 return (0);
3941 }
3942 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
3943 {
3944 EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir);
3945 return 1;
3946 }
3947 m = (uint32_t *)_TIFFCheckMalloc(tif, count, 2 * sizeof(uint32_t),
3948 "for rational array");
3949 if (m == NULL)
3950 {
3951 TIFFErrorExtR(tif, module, "Out of memory");
3952 return (0);
3953 }
3954 for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
3955 {
3956 DoubleToRational((double)*na, &nb[0], &nb[1]);
3957 }
3958 if (tif->tif_flags & TIFF_SWAB)
3959 TIFFSwabArrayOfLong(m, count * 2);
3960 o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count,
3961 count * 8, &m[0]);
3962 _TIFFfreeExt(tif, m);
3963 return (o);
3964}
3965
3992static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir,
3993 TIFFDirEntry *dir,
3994 uint16_t tag,
3995 uint32_t count,
3996 float *value)
3997{
3998 static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
3999 int32_t *m;
4000 float *na;
4001 int32_t *nb;
4002 uint32_t nc;
4003 int o;
4004 if (count > UINT32_MAX / 8)
4005 {
4006 TIFFErrorExtR(tif, module,
4007 "Tag %" PRIu16 ": count %" PRIu32
4008 " exceeds maximum of %" PRIu32 " entries",
4009 tag, count, UINT32_MAX / 8);
4010 return (0);
4011 }
4012 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4013 {
4014 EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir);
4015 return 1;
4016 }
4017 m = (int32_t *)_TIFFCheckMalloc(tif, count, 2 * sizeof(int32_t),
4018 "for srational array");
4019 if (m == NULL)
4020 {
4021 TIFFErrorExtR(tif, module, "Out of memory");
4022 return (0);
4023 }
4024 for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
4025 {
4026 DoubleToSrational((double)*na, &nb[0], &nb[1]);
4027 }
4028 if (tif->tif_flags & TIFF_SWAB)
4029 TIFFSwabArrayOfLong((uint32_t *)m, count * 2);
4030 o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count,
4031 count * 8, &m[0]);
4032 _TIFFfreeExt(tif, m);
4033 return (o);
4034}
4035
4036/*-- Rational2Double: additional write functions for double arrays */
4063static int
4065 TIFFDirEntry *dir, uint16_t tag,
4066 uint32_t count, double *value)
4067{
4068 static const char module[] =
4069 "TIFFWriteDirectoryTagCheckedRationalDoubleArray";
4070 uint32_t *m;
4071 double *na;
4072 uint32_t *nb;
4073 uint32_t nc;
4074 int o;
4075 if (count > UINT32_MAX / 8)
4076 {
4077 TIFFErrorExtR(tif, module,
4078 "Tag %" PRIu16 ": count %" PRIu32
4079 " exceeds maximum of %" PRIu32 " entries",
4080 tag, count, UINT32_MAX / 8);
4081 return (0);
4082 }
4083 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4084 {
4085 EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(uint32_t), ndir);
4086 return 1;
4087 }
4088 m = (uint32_t *)_TIFFCheckMalloc(tif, count, 2 * sizeof(uint32_t),
4089 "for rational double array");
4090 if (m == NULL)
4091 {
4092 TIFFErrorExtR(tif, module, "Out of memory");
4093 return (0);
4094 }
4095 for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
4096 {
4097 DoubleToRational((double)*na, &nb[0], &nb[1]);
4098 }
4099 if (tif->tif_flags & TIFF_SWAB)
4100 TIFFSwabArrayOfLong(m, count * 2);
4101 o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_RATIONAL, count,
4102 count * 8, &m[0]);
4103 _TIFFfreeExt(tif, m);
4104 return (o);
4105} /*-- TIFFWriteDirectoryTagCheckedRationalDoubleArray() ------- */
4106
4134 TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count,
4135 double *value)
4136{
4137 static const char module[] =
4138 "TIFFWriteDirectoryTagCheckedSrationalDoubleArray";
4139 int32_t *m;
4140 double *na;
4141 int32_t *nb;
4142 uint32_t nc;
4143 int o;
4144 if (count > UINT32_MAX / 8)
4145 {
4146 TIFFErrorExtR(tif, module,
4147 "Tag %" PRIu16 ": count %" PRIu32
4148 " exceeds maximum of %" PRIu32 " entries",
4149 tag, count, UINT32_MAX / 8);
4150 return (0);
4151 }
4152 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4153 {
4154 EvaluateIFDdatasizeWrite(tif, count * 2, sizeof(int32_t), ndir);
4155 return 1;
4156 }
4157 m = (int32_t *)_TIFFCheckMalloc(tif, count, 2 * sizeof(int32_t),
4158 "for srational double array");
4159 if (m == NULL)
4160 {
4161 TIFFErrorExtR(tif, module, "Out of memory");
4162 return (0);
4163 }
4164 for (na = value, nb = m, nc = 0; nc < count; na++, nb += 2, nc++)
4165 {
4166 DoubleToSrational((double)*na, &nb[0], &nb[1]);
4167 }
4168 if (tif->tif_flags & TIFF_SWAB)
4169 TIFFSwabArrayOfLong((uint32_t *)m, count * 2);
4170 o = TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_SRATIONAL, count,
4171 count * 8, &m[0]);
4172 _TIFFfreeExt(tif, m);
4173 return (o);
4174} /*--- TIFFWriteDirectoryTagCheckedSrationalDoubleArray() -------- */
4175
4227static void ToRationalEuclideanGCD(double value, int blnUseSignedRange,
4228 int blnUseSmallRange, uint64_t *ullNum,
4229 uint64_t *ullDenom)
4230{
4231 /* Internally, the integer variables can be bigger than the external ones,
4232 * as long as the result will fit into the external variable size.
4233 */
4234 uint64_t numSum[3] = {0, 1, 0}, denomSum[3] = {1, 0, 0};
4235 uint64_t aux, bigNum, bigDenom;
4236 uint64_t returnLimit;
4237 int i;
4238 uint64_t nMax;
4239 double fMax;
4240 unsigned long maxDenom;
4241 /*-- nMax and fMax defines the initial accuracy of the starting fractional,
4242 * or better, the highest used integer numbers used within the starting
4243 * fractional (bigNum/bigDenom). There are two approaches, which can
4244 * accidentally lead to different accuracies just depending on the value.
4245 * Therefore, blnUseSmallRange steers this behavior.
4246 * For long long nMax = ((9223372036854775807-1)/2); for long nMax =
4247 * ((2147483647-1)/2);
4248 */
4249 if (blnUseSmallRange)
4250 {
4251 nMax = (uint64_t)((2147483647 - 1) / 2); /* for ULONG range */
4252 }
4253 else
4254 {
4255 nMax = ((9223372036854775807 - 1) / 2); /* for ULLONG range */
4256 }
4257 fMax = (double)nMax;
4258
4259 /*-- For the Euclidean GCD define the denominator range, so that it stays
4260 * within size of unsigned long variables. maxDenom should be LONG_MAX for
4261 * negative values and ULONG_MAX for positive ones. Also the final returned
4262 * value of ullNum and ullDenom is limited according to signed- or
4263 * unsigned-range.
4264 */
4265 if (blnUseSignedRange)
4266 {
4267 maxDenom = 2147483647UL; /*LONG_MAX = 0x7FFFFFFFUL*/
4268 returnLimit = maxDenom;
4269 }
4270 else
4271 {
4272 maxDenom = 0xFFFFFFFFUL; /*ULONG_MAX = 0xFFFFFFFFUL*/
4273 returnLimit = maxDenom;
4274 }
4275
4276 /*-- First generate a rational fraction (bigNum/bigDenom) which represents
4277 *the value as a rational number with the highest accuracy. Therefore,
4278 *uint64_t (uint64_t) is needed. This rational fraction is then reduced
4279 *using the Euclidean algorithm to find the greatest common divisor (GCD).
4280 * bigNum = big numinator of value without fraction (or cut residual
4281 *fraction) bigDenom = big denominator of value
4282 *-- Break-criteria so that uint64_t cast to "bigNum" introduces no error
4283 *and bigDenom has no overflow, and stop with enlargement of fraction when
4284 *the double-value of it reaches an integer number without fractional part.
4285 */
4286 bigDenom = 1;
4287 while ((!TIFF_DOUBLE_EQ(value, floor(value))) && (value < fMax) &&
4288 (bigDenom < nMax))
4289 {
4290 bigDenom <<= 1;
4291 value *= 2;
4292 }
4293 bigNum = (uint64_t)value;
4294
4295 /*-- Start Euclidean algorithm to find the greatest common divisor (GCD) --
4296 */
4297#define MAX_ITERATIONS 64
4298 for (i = 0; i < MAX_ITERATIONS; i++)
4299 {
4300 uint64_t val;
4301 /* if bigDenom is not zero, calculate integer part of fraction. */
4302 if (bigDenom == 0)
4303 {
4304 break;
4305 }
4306 val = bigNum / bigDenom;
4307
4308 /* Set bigDenom to reminder of bigNum/bigDenom and bigNum to previous
4309 * denominator bigDenom. */
4310 aux = bigNum;
4311 bigNum = bigDenom;
4312 bigDenom = aux % bigDenom;
4313
4314 /* calculate next denominator and check for its given maximum */
4315 aux = val;
4316 if (denomSum[1] * val + denomSum[0] >= maxDenom)
4317 {
4318 aux = (maxDenom - denomSum[0]) / denomSum[1];
4319 if (aux * 2 >= val || denomSum[1] >= maxDenom)
4320 i = (MAX_ITERATIONS +
4321 1); /* exit but execute rest of for-loop */
4322 else
4323 break;
4324 }
4325 /* calculate next numerator to numSum2 and save previous one to numSum0;
4326 * numSum1 just copy of numSum2. */
4327 numSum[2] = aux * numSum[1] + numSum[0];
4328 numSum[0] = numSum[1];
4329 numSum[1] = numSum[2];
4330 /* calculate next denominator to denomSum2 and save previous one to
4331 * denomSum0; denomSum1 just copy of denomSum2. */
4332 denomSum[2] = aux * denomSum[1] + denomSum[0];
4333 denomSum[0] = denomSum[1];
4334 denomSum[1] = denomSum[2];
4335 }
4336
4337 /*-- Check and adapt for final variable size and return values; reduces
4338 * internal accuracy; denominator is kept in ULONG-range with maxDenom -- */
4339 while (numSum[1] > returnLimit || denomSum[1] > returnLimit)
4340 {
4341 numSum[1] = numSum[1] / 2;
4342 denomSum[1] = denomSum[1] / 2;
4343 }
4344
4345 /* return values */
4346 *ullNum = numSum[1];
4347 *ullDenom = denomSum[1];
4348
4349} /*-- ToRationalEuclideanGCD() -------------- */
4350
4374static void DoubleToRational(double value, uint32_t *num, uint32_t *denom)
4375{
4376 /*---- UN-SIGNED RATIONAL ---- */
4377 double dblDiff, dblDiff2;
4378 uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
4379
4380 /*-- Check for negative values. If so it is an error. */
4381 /* Test written that way to catch NaN */
4382 if (!(value >= 0))
4383 {
4384 *num = *denom = 0;
4385 TIFFErrorExt(0, "TIFFLib: DoubleToRational()",
4386 " Negative Value for Unsigned Rational given.");
4387 return;
4388 }
4389
4390 /*-- Check for too big numbers (> ULONG_MAX) -- */
4391 if (value > 0xFFFFFFFFUL)
4392 {
4393 *num = 0xFFFFFFFFU;
4394 *denom = 0;
4395 return;
4396 }
4397 /*-- Check for easy integer numbers -- */
4398 if (TIFF_DOUBLE_EQ(value, (double)(uint32_t)value))
4399 {
4400 *num = (uint32_t)value;
4401 *denom = 1;
4402 return;
4403 }
4404 /*-- Check for too small numbers for "unsigned long" type rationals -- */
4405 if (value < 1.0 / (double)0xFFFFFFFFUL)
4406 {
4407 *num = 0;
4408 *denom = 0xFFFFFFFFU;
4409 return;
4410 }
4411
4412 /*-- There are two approaches using the Euclidean algorithm,
4413 * which can accidentally lead to different accuracies just depending on
4414 * the value. Try both and define which one was better.
4415 */
4416 ToRationalEuclideanGCD(value, FALSE, FALSE, &ullNum, &ullDenom);
4417 ToRationalEuclideanGCD(value, FALSE, TRUE, &ullNum2, &ullDenom2);
4418 /*-- Double-Check, that returned values fit into ULONG :*/
4419 if (ullNum > 0xFFFFFFFFUL || ullDenom > 0xFFFFFFFFUL ||
4420 ullNum2 > 0xFFFFFFFFUL || ullDenom2 > 0xFFFFFFFFUL)
4421 {
4422 TIFFErrorExt(0, "TIFFLib: DoubleToRational()",
4423 " Num or Denom exceeds ULONG: val=%14.6f, num=%12" PRIu64
4424 ", denom=%12" PRIu64 " | num2=%12" PRIu64
4425 ", denom2=%12" PRIu64 "",
4426 value, ullNum, ullDenom, ullNum2, ullDenom2);
4427 *num = 0xFFFFFFFFU;
4428 *denom = 0;
4429 return;
4430 }
4431
4432 /* Check, which one has higher accuracy and take that. */
4433 dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
4434 dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
4435 if (dblDiff < dblDiff2)
4436 {
4437 *num = (uint32_t)ullNum;
4438 *denom = (uint32_t)ullDenom;
4439 }
4440 else
4441 {
4442 *num = (uint32_t)ullNum2;
4443 *denom = (uint32_t)ullDenom2;
4444 }
4445} /*-- DoubleToRational() -------------- */
4446
4447/*---- DoubleToSrational() -----------------------------------------------
4448* Calculates the rational fractional of a double input value
4449* for SIGNED rationals,
4450* using the Euclidean algorithm to find the greatest common divisor (GCD)
4451------------------------------------------------------------------------*/
4472static void DoubleToSrational(double value, int32_t *num, int32_t *denom)
4473{
4474 /*---- SIGNED RATIONAL ----*/
4475 int neg = 1;
4476 double dblDiff, dblDiff2;
4477 uint64_t ullNum, ullDenom, ullNum2, ullDenom2;
4478
4479 /*-- Check for negative values and use then the positive one for internal
4480 * calculations, but take the sign into account before returning. */
4481 if (value < 0)
4482 {
4483 neg = -1;
4484 value = -value;
4485 }
4486
4487 /*-- Check for too big numbers (> LONG_MAX) -- */
4488 if (value > 0x7FFFFFFFL)
4489 {
4490 *num = 0x7FFFFFFFL;
4491 *denom = 0;
4492 return;
4493 }
4494 /*-- Check for easy numbers -- */
4495 if (TIFF_DOUBLE_EQ(value, (double)(int32_t)value))
4496 {
4497 *num = (int32_t)(neg * value);
4498 *denom = 1;
4499 return;
4500 }
4501 /*-- Check for too small numbers for "long" type rationals -- */
4502 if (value < 1.0 / (double)0x7FFFFFFFL)
4503 {
4504 *num = 0;
4505 *denom = 0x7FFFFFFFL;
4506 return;
4507 }
4508
4509 /*-- There are two approaches using the Euclidean algorithm,
4510 * which can accidentally lead to different accuracies just depending on
4511 * the value. Try both and define which one was better. Furthermore, set
4512 * behavior of ToRationalEuclideanGCD() to the range of signed-long.
4513 */
4514 ToRationalEuclideanGCD(value, TRUE, FALSE, &ullNum, &ullDenom);
4515 ToRationalEuclideanGCD(value, TRUE, TRUE, &ullNum2, &ullDenom2);
4516 /*-- Double-Check, that returned values fit into LONG :*/
4517 if (ullNum > 0x7FFFFFFFL || ullDenom > 0x7FFFFFFFL ||
4518 ullNum2 > 0x7FFFFFFFL || ullDenom2 > 0x7FFFFFFFL)
4519 {
4520 TIFFErrorExt(0, "TIFFLib: DoubleToSrational()",
4521 " Num or Denom exceeds LONG: val=%14.6f, num=%12" PRIu64
4522 ", denom=%12" PRIu64 " | num2=%12" PRIu64
4523 ", denom2=%12" PRIu64 "",
4524 neg * value, ullNum, ullDenom, ullNum2, ullDenom2);
4525 *num = (int32_t)(neg * 0x7FFFFFFFL);
4526 *denom = 0;
4527 return;
4528 }
4529
4530 /* Check, which one has higher accuracy and take that. */
4531 dblDiff = fabs(value - ((double)ullNum / (double)ullDenom));
4532 dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2));
4533 if (dblDiff < dblDiff2)
4534 {
4535 *num = (int32_t)(neg * (long)ullNum);
4536 *denom = (int32_t)ullDenom;
4537 }
4538 else
4539 {
4540 *num = (int32_t)(neg * (long)ullNum2);
4541 *denom = (int32_t)ullDenom2;
4542 }
4543} /*-- DoubleToSrational() --------------*/
4544
4570static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir,
4571 TIFFDirEntry *dir,
4572 uint16_t tag, uint32_t count,
4573 float *value)
4574{
4575 assert(sizeof(float) == 4);
4576 if (count > UINT32_MAX / 4)
4577 return (0);
4578 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4579 {
4580 EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
4581 return 1;
4582 }
4583 TIFFCvtNativeToIEEEFloat(tif, count, value);
4584 if (tif->tif_flags & TIFF_SWAB)
4585 TIFFSwabArrayOfFloat(value, count);
4586 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_FLOAT, count,
4587 count * 4, value));
4588}
4589
4615static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir,
4616 TIFFDirEntry *dir,
4617 uint16_t tag, uint32_t count,
4618 double *value)
4619{
4620 assert(sizeof(double) == 8);
4621 if (count > UINT32_MAX / 8)
4622 return (0);
4623 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4624 {
4625 EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
4626 return 1;
4627 }
4628 TIFFCvtNativeToIEEEDouble(tif, count, value);
4629 if (tif->tif_flags & TIFF_SWAB)
4630 TIFFSwabArrayOfDouble(value, count);
4631 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_DOUBLE, count,
4632 count * 8, value));
4633}
4634
4659static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir,
4660 TIFFDirEntry *dir, uint16_t tag,
4661 uint32_t count, uint32_t *value)
4662{
4663 if (count > UINT32_MAX / 4)
4664 return (0);
4665 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4666 {
4667 EvaluateIFDdatasizeWrite(tif, count, 4, ndir);
4668 return 1;
4669 }
4670 if (tif->tif_flags & TIFF_SWAB)
4671 TIFFSwabArrayOfLong(value, count);
4672 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD, count,
4673 count * 4, value));
4674}
4675
4701static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir,
4702 TIFFDirEntry *dir,
4703 uint16_t tag, uint32_t count,
4704 uint64_t *value)
4705{
4706 assert(tif->tif_flags & TIFF_BIGTIFF);
4707 if (count > UINT32_MAX / 8)
4708 return (0);
4709 if (dir == NULL) /* Just evaluate IFD data size and increment ndir. */
4710 {
4711 EvaluateIFDdatasizeWrite(tif, count, 8, ndir);
4712 return 1;
4713 }
4714 if (tif->tif_flags & TIFF_SWAB)
4715 TIFFSwabArrayOfLong8(value, count);
4716 return (TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_IFD8, count,
4717 count * 8, value));
4718}
4719
4750static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir,
4751 TIFFDirEntry *dir, uint16_t tag,
4752 uint16_t datatype, uint32_t count,
4753 uint32_t datalength, void *data)
4754{
4755 static const char module[] = "TIFFWriteDirectoryTagData";
4756 uint32_t m;
4757 m = 0;
4758 while (m < (*ndir))
4759 {
4760 assert(dir[m].tdir_tag != tag);
4761 if (dir[m].tdir_tag > tag)
4762 break;
4763 m++;
4764 }
4765 if (m < (*ndir))
4766 {
4767 uint32_t n;
4768 for (n = *ndir; n > m; n--)
4769 dir[n] = dir[n - 1];
4770 }
4771 dir[m].tdir_tag = tag;
4772 dir[m].tdir_type = datatype;
4773 dir[m].tdir_count = count;
4774 dir[m].tdir_offset.toff_long8 = 0;
4775 if (datalength <= ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
4776 {
4777 if (data && datalength)
4778 {
4779 _TIFFmemcpy(&dir[m].tdir_offset, data, datalength);
4780 }
4781 }
4782 else
4783 {
4784 uint64_t na, nb;
4785 na = tif->tif_dataoff;
4786 nb = na + datalength;
4787 if (!(tif->tif_flags & TIFF_BIGTIFF))
4788 nb = (uint32_t)nb;
4789 if ((nb < na) || (nb < datalength))
4790 {
4791 TIFFErrorExtR(tif, module, "Maximum TIFF file size exceeded");
4792 return (0);
4793 }
4794 if (!SeekOK(tif, na))
4795 {
4796 TIFFErrorExtR(tif, module, "IO error writing tag data");
4797 return (0);
4798 }
4799 if (datalength >= 0x80000000UL)
4800 {
4801 TIFFErrorExtR(tif, module,
4802 "libtiff does not allow writing more than 2147483647 "
4803 "bytes in a tag");
4804 return (0);
4805 }
4806 if (!WriteOK(tif, data, (tmsize_t)datalength))
4807 {
4808 TIFFErrorExtR(tif, module, "IO error writing tag data");
4809 return (0);
4810 }
4811 tif->tif_dataoff = nb;
4812 if (tif->tif_dataoff & 1)
4813 tif->tif_dataoff++;
4814 if (!(tif->tif_flags & TIFF_BIGTIFF))
4815 {
4816 uint32_t o;
4817 o = (uint32_t)na;
4818 if (tif->tif_flags & TIFF_SWAB)
4819 TIFFSwabLong(&o);
4820 _TIFFmemcpy(&dir[m].tdir_offset, &o, 4);
4821 }
4822 else
4823 {
4824 dir[m].tdir_offset.toff_long8 = na;
4825 if (tif->tif_flags & TIFF_SWAB)
4826 TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
4827 }
4828 }
4829 (*ndir)++;
4830 return (1);
4831}
4832
4833/*
4834 * Link the current directory into the directory chain for the file.
4835 */
4857static int TIFFLinkDirectory(TIFF *tif)
4858{
4859 static const char module[] = "TIFFLinkDirectory";
4860
4861 const toff_t end_offset = TIFFSeekFile(tif, 0, SEEK_END);
4862 if (end_offset == (toff_t)-1)
4863 {
4864 TIFFErrorExtR(tif, module,
4865 "Error seeking to end of file before linking directory");
4866 return 0;
4867 }
4868 tif->tif_diroff = (end_offset + 1) & (~((toff_t)1));
4869
4870 /*
4871 * Handle SubIFDs
4872 */
4873 if (tif->tif_flags & TIFF_INSUBIFD)
4874 {
4875 if (!(tif->tif_flags & TIFF_BIGTIFF))
4876 {
4877 uint32_t m;
4878 m = (uint32_t)tif->tif_diroff;
4879 if (tif->tif_flags & TIFF_SWAB)
4880 TIFFSwabLong(&m);
4881 (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
4882 if (!WriteOK(tif, &m, 4))
4883 {
4884 TIFFErrorExtR(tif, module,
4885 "Error writing SubIFD directory link");
4886 return (0);
4887 }
4888
4889 /*
4890 * Advance to the next SubIFD or, if this is
4891 * the last one configured, reverting back to the
4892 * normal directory linkage is done in TIFFWriteDirectorySec()
4893 * by tif->tif_flags &= ~TIFF_INSUBIFD;.
4894 */
4895 if (--tif->tif_nsubifd)
4896 tif->tif_subifdoff += 4;
4897 return (1);
4898 }
4899 else
4900 {
4901 uint64_t m;
4902 m = tif->tif_diroff;
4903 if (tif->tif_flags & TIFF_SWAB)
4904 TIFFSwabLong8(&m);
4905 (void)TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
4906 if (!WriteOK(tif, &m, 8))
4907 {
4908 TIFFErrorExtR(tif, module,
4909 "Error writing SubIFD directory link");
4910 return (0);
4911 }
4912
4913 /*
4914 * Advance to the next SubIFD or, if this is
4915 * the last one configured, reverting back to the
4916 * normal directory linkage is done in TIFFWriteDirectorySec()
4917 * by tif->tif_flags &= ~TIFF_INSUBIFD;.
4918 */
4919 if (--tif->tif_nsubifd)
4920 tif->tif_subifdoff += 8;
4921 return (1);
4922 }
4923 }
4924
4925 /*
4926 * Handle main-IFDs
4927 */
4928 tdir_t ndir = 1; /* count current number of main-IFDs */
4929 if (!(tif->tif_flags & TIFF_BIGTIFF))
4930 {
4931 uint32_t m;
4932 uint32_t nextdir;
4933 m = (uint32_t)(tif->tif_diroff);
4934 if (tif->tif_flags & TIFF_SWAB)
4935 TIFFSwabLong(&m);
4936 if (tif->tif_header.classic.tiff_diroff == 0)
4937 {
4938 /*
4939 * First directory, overwrite offset in header.
4940 */
4941 tif->tif_header.classic.tiff_diroff = (uint32_t)tif->tif_diroff;
4942 tif->tif_lastdiroff = tif->tif_diroff;
4943 (void)TIFFSeekFile(tif, 4, SEEK_SET);
4944 if (!WriteOK(tif, &m, 4))
4945 {
4946 TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header");
4947 return (0);
4948 }
4949 if (!tif->tif_dir.td_iswrittentofile)
4950 tif->tif_curdircount = 0;
4951 return (1);
4952 }
4953 /*
4954 * Not the first directory, search to the last and append.
4955 */
4956 tdir_t dirn = 0;
4957 if (tif->tif_lastdiroff != 0 &&
4959 {
4960 /* Start searching from the lastely written IFD. Thus get its IFD
4961 * number. */
4962 nextdir = (uint32_t)tif->tif_lastdiroff;
4963 ndir = dirn + 1;
4964 }
4965 else
4966 {
4967 nextdir = tif->tif_header.classic.tiff_diroff;
4968 ndir = 1; /* start searching from the first IFD */
4969 }
4970
4971 while (1)
4972 {
4973 uint16_t dircount;
4974 uint32_t nextnextdir;
4975
4976 /* Update IDF loop list and check for IFD loop.
4977 * ndir is IFD ID plus one. */
4978 if (!_TIFFCheckDirNumberAndOffset(tif, ndir - 1, nextdir))
4979 {
4980 TIFFErrorExtR(tif, module, "Error IFD loop detected");
4981 return 0; /* bad offset (IFD looping or more than
4982 TIFF_MAX_DIR_COUNT IFDs) */
4983 }
4984 if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount, 2))
4985 {
4986 TIFFErrorExtR(tif, module, "Error fetching directory count");
4987 return (0);
4988 }
4989 if (tif->tif_flags & TIFF_SWAB)
4990 TIFFSwabShort(&dircount);
4991 (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12U, SEEK_SET);
4992 if (!ReadOK(tif, &nextnextdir, 4))
4993 {
4994 TIFFErrorExtR(tif, module, "Error fetching directory link");
4995 return (0);
4996 }
4997 if (tif->tif_flags & TIFF_SWAB)
4998 TIFFSwabLong(&nextnextdir);
4999 if (nextnextdir == 0)
5000 {
5001 (void)TIFFSeekFile(tif, nextdir + 2 + dircount * 12U, SEEK_SET);
5002 if (!WriteOK(tif, &m, 4))
5003 {
5004 TIFFErrorExtR(tif, module, "Error writing directory link");
5005 return (0);
5006 }
5007 tif->tif_lastdiroff = tif->tif_diroff;
5008 break;
5009 }
5010 nextdir = nextnextdir;
5011 ndir++;
5012 }
5013 }
5014 else
5015 {
5016 /*- BigTIFF -*/
5017 uint64_t m;
5018 uint64_t nextdir;
5019 m = tif->tif_diroff;
5020 if (tif->tif_flags & TIFF_SWAB)
5021 TIFFSwabLong8(&m);
5022 if (tif->tif_header.big.tiff_diroff == 0)
5023 {
5024 /*
5025 * First directory, overwrite offset in header.
5026 */
5028 tif->tif_lastdiroff = tif->tif_diroff;
5029 (void)TIFFSeekFile(tif, 8, SEEK_SET);
5030 if (!WriteOK(tif, &m, 8))
5031 {
5032 TIFFErrorExtR(tif, tif->tif_name, "Error writing TIFF header");
5033 return (0);
5034 }
5035 if (!tif->tif_dir.td_iswrittentofile)
5036 tif->tif_curdircount = 0;
5037 return (1);
5038 }
5039 /*
5040 * Not the first directory, search to the last and append.
5041 */
5042 tdir_t dirn = 0;
5043 if (tif->tif_lastdiroff != 0 &&
5045 {
5046 /* Start searching from the lastely written IFD. Thus get its IFD
5047 * number. */
5048 nextdir = tif->tif_lastdiroff;
5049 ndir = dirn + 1;
5050 }
5051 else
5052 {
5053 nextdir = tif->tif_header.big.tiff_diroff;
5054 ndir = 1; /* start searching from the first IFD */
5055 }
5056 while (1)
5057 {
5058 uint64_t dircount64;
5059 uint64_t nextnextdir;
5060
5061 /* Update IDF loop list and check for IFD loop. */
5062 if (!_TIFFCheckDirNumberAndOffset(tif, ndir - 1, nextdir))
5063 {
5064 TIFFErrorExtR(tif, module, "Error IFD loop detected");
5065 return 0; /* bad offset (IFD looping or more than
5066 TIFF_MAX_DIR_COUNT IFDs) */
5067 }
5068 if (!SeekOK(tif, nextdir) || !ReadOK(tif, &dircount64, 8))
5069 {
5070 TIFFErrorExtR(tif, module, "Error fetching directory count");
5071 return (0);
5072 }
5073 if (tif->tif_flags & TIFF_SWAB)
5074 TIFFSwabLong8(&dircount64);
5075 if (dircount64 > 0xFFFF)
5076 {
5077 TIFFErrorExtR(tif, module,
5078 "Sanity check on tag count failed, "
5079 "likely corrupt TIFF");
5080 return (0);
5081 }
5082 (void)TIFFSeekFile(tif, nextdir + 8 + dircount64 * 20, SEEK_SET);
5083 if (!ReadOK(tif, &nextnextdir, 8))
5084 {
5085 TIFFErrorExtR(tif, module, "Error fetching directory link");
5086 return (0);
5087 }
5088 if (tif->tif_flags & TIFF_SWAB)
5089 TIFFSwabLong8(&nextnextdir);
5090 if (nextnextdir == 0)
5091 {
5092 (void)TIFFSeekFile(tif, nextdir + 8 + dircount64 * 20,
5093 SEEK_SET);
5094 if (!WriteOK(tif, &m, 8))
5095 {
5096 TIFFErrorExtR(tif, module, "Error writing directory link");
5097 return (0);
5098 }
5099 tif->tif_lastdiroff = tif->tif_diroff;
5100 break;
5101 }
5102 nextdir = nextnextdir;
5103 ndir++;
5104 }
5105 }
5106 /* Offset of next IFD is written to file.
5107 * Update number of main-IFDs in file.
5108 * However, tif_curdircount shall count only newly written main-IFDs with
5109 * entries and not only number of linked offsets! Thus, tif_curdircount is
5110 * incremented at the end of TIFFWriteDirectorySec().
5111 * TIFF_NON_EXISTENT_DIR_NUMBER means 'dont know number of IFDs'
5112 * 0 means 'empty file opened for writing, but no IFD written yet' */
5113 if (!tif->tif_dir.td_iswrittentofile && !(tif->tif_flags & TIFF_INSUBIFD))
5114 {
5115 tif->tif_curdircount = ndir;
5116 }
5117 return (1);
5118}
5119
5120/************************************************************************/
5121/* TIFFRewriteField() */
5122/* */
5123/* Rewrite a field in the directory on disk without regard to */
5124/* updating the TIFF directory structure in memory. Currently */
5125/* only supported for field that already exist in the on-disk */
5126/* directory. Mainly used for updating stripoffset / */
5127/* stripbytecount values after the directory is already on */
5128/* disk. */
5129/* */
5130/* Returns zero on failure, and one on success. */
5131/************************************************************************/
5132
5133int _TIFFRewriteField(TIFF *tif, uint16_t tag, TIFFDataType in_datatype,
5134 tmsize_t count, void *data)
5135{
5136 static const char module[] = "TIFFResetField";
5137 /* const TIFFField* fip = NULL; */
5138 uint16_t dircount;
5139 tmsize_t dirsize;
5140 uint8_t direntry_raw[20];
5141 uint16_t entry_tag = 0;
5142 uint16_t entry_type = 0;
5143 uint64_t entry_count = 0;
5144 uint64_t entry_offset = 0;
5145 int value_in_entry = 0;
5146 uint64_t read_offset;
5147 uint8_t *buf_to_write = NULL;
5148 TIFFDataType datatype;
5149
5150 /* -------------------------------------------------------------------- */
5151 /* Find field definition. */
5152 /* -------------------------------------------------------------------- */
5153 /*fip =*/TIFFFindField(tif, tag, TIFF_ANY);
5154
5155 /* -------------------------------------------------------------------- */
5156 /* Do some checking this is a straight forward case. */
5157 /* -------------------------------------------------------------------- */
5158 if (isMapped(tif))
5159 {
5160 TIFFErrorExtR(tif, module,
5161 "Memory mapped files not currently supported for "
5162 "this operation.");
5163 return 0;
5164 }
5165
5166 if (tif->tif_diroff == 0)
5167 {
5169 tif, module,
5170 "Attempt to reset field on directory not already on disk.");
5171 return 0;
5172 }
5173
5174 /* -------------------------------------------------------------------- */
5175 /* Read the directory entry count. */
5176 /* -------------------------------------------------------------------- */
5177 if (!SeekOK(tif, tif->tif_diroff))
5178 {
5179 TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory",
5180 tif->tif_name);
5181 return 0;
5182 }
5183
5184 read_offset = tif->tif_diroff;
5185
5186 if (!(tif->tif_flags & TIFF_BIGTIFF))
5187 {
5188 if (!ReadOK(tif, &dircount, sizeof(uint16_t)))
5189 {
5190 TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count",
5191 tif->tif_name);
5192 return 0;
5193 }
5194 if (tif->tif_flags & TIFF_SWAB)
5195 TIFFSwabShort(&dircount);
5196 dirsize = 12;
5197 read_offset += 2;
5198 }
5199 else
5200 {
5201 uint64_t dircount64;
5202 if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
5203 {
5204 TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory count",
5205 tif->tif_name);
5206 return 0;
5207 }
5208 if (tif->tif_flags & TIFF_SWAB)
5209 TIFFSwabLong8(&dircount64);
5210 dircount = (uint16_t)dircount64;
5211 dirsize = 20;
5212 read_offset += 8;
5213 }
5214
5215 /* -------------------------------------------------------------------- */
5216 /* Read through directory to find target tag. */
5217 /* -------------------------------------------------------------------- */
5218 while (dircount > 0)
5219 {
5220 if (!ReadOK(tif, direntry_raw, dirsize))
5221 {
5222 TIFFErrorExtR(tif, module, "%s: Can not read TIFF directory entry.",
5223 tif->tif_name);
5224 return 0;
5225 }
5226
5227 memcpy(&entry_tag, direntry_raw + 0, sizeof(uint16_t));
5228 if (tif->tif_flags & TIFF_SWAB)
5229 TIFFSwabShort(&entry_tag);
5230
5231 if (entry_tag == tag)
5232 break;
5233
5234 read_offset += (uint64_t)dirsize;
5235 }
5236
5237 if (entry_tag != tag)
5238 {
5239 TIFFErrorExtR(tif, module, "%s: Could not find tag %" PRIu16 ".",
5240 tif->tif_name, tag);
5241 return 0;
5242 }
5243
5244 /* -------------------------------------------------------------------- */
5245 /* Extract the type, count and offset for this entry. */
5246 /* -------------------------------------------------------------------- */
5247 memcpy(&entry_type, direntry_raw + 2, sizeof(uint16_t));
5248 if (tif->tif_flags & TIFF_SWAB)
5249 TIFFSwabShort(&entry_type);
5250
5251 if (!(tif->tif_flags & TIFF_BIGTIFF))
5252 {
5253 uint32_t value;
5254
5255 memcpy(&value, direntry_raw + 4, sizeof(uint32_t));
5256 if (tif->tif_flags & TIFF_SWAB)
5257 TIFFSwabLong(&value);
5258 entry_count = value;
5259
5260 memcpy(&value, direntry_raw + 8, sizeof(uint32_t));
5261 if (tif->tif_flags & TIFF_SWAB)
5262 TIFFSwabLong(&value);
5263 entry_offset = value;
5264 }
5265 else
5266 {
5267 memcpy(&entry_count, direntry_raw + 4, sizeof(uint64_t));
5268 if (tif->tif_flags & TIFF_SWAB)
5269 TIFFSwabLong8(&entry_count);
5270
5271 memcpy(&entry_offset, direntry_raw + 12, sizeof(uint64_t));
5272 if (tif->tif_flags & TIFF_SWAB)
5273 TIFFSwabLong8(&entry_offset);
5274 }
5275
5276 /* -------------------------------------------------------------------- */
5277 /* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
5278 /* -------------------------------------------------------------------- */
5279 if (entry_offset == 0 && entry_count == 0 && entry_type == 0)
5280 {
5281 if (tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS)
5282 {
5283 entry_type =
5285 }
5286 else
5287 {
5288 int write_aslong8 = 1;
5289 if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
5290 {
5291 write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
5292 }
5293 else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
5294 {
5295 write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
5296 }
5297 if (write_aslong8)
5298 {
5299 entry_type = TIFF_LONG8;
5300 }
5301 else
5302 {
5303 int write_aslong4 = 1;
5304 if (count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS)
5305 {
5306 write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
5307 }
5308 else if (count > 1 && tag == TIFFTAG_TILEBYTECOUNTS)
5309 {
5310 write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
5311 }
5312 if (write_aslong4)
5313 {
5314 entry_type = TIFF_LONG;
5315 }
5316 else
5317 {
5318 entry_type = TIFF_SHORT;
5319 }
5320 }
5321 }
5322 }
5323
5324 /* -------------------------------------------------------------------- */
5325 /* What data type do we want to write this as? */
5326 /* -------------------------------------------------------------------- */
5327 if (TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags & TIFF_BIGTIFF))
5328 {
5329 if (in_datatype == TIFF_LONG8)
5330 datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG;
5331 else if (in_datatype == TIFF_SLONG8)
5332 datatype = TIFF_SLONG;
5333 else if (in_datatype == TIFF_IFD8)
5334 datatype = TIFF_IFD;
5335 else
5336 datatype = in_datatype;
5337 }
5338 else
5339 {
5340 if (in_datatype == TIFF_LONG8 &&
5341 (entry_type == TIFF_SHORT || entry_type == TIFF_LONG ||
5342 entry_type == TIFF_LONG8))
5343 datatype = (TIFFDataType)entry_type;
5344 else if (in_datatype == TIFF_SLONG8 &&
5345 (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8))
5346 datatype = (TIFFDataType)entry_type;
5347 else if (in_datatype == TIFF_IFD8 &&
5348 (entry_type == TIFF_IFD || entry_type == TIFF_IFD8))
5349 datatype = (TIFFDataType)entry_type;
5350 else
5351 datatype = in_datatype;
5352 }
5353
5354 /* -------------------------------------------------------------------- */
5355 /* Prepare buffer of actual data to write. This includes */
5356 /* swabbing as needed. */
5357 /* -------------------------------------------------------------------- */
5358 buf_to_write = (uint8_t *)_TIFFCheckMalloc(
5359 tif, count, TIFFDataWidth(datatype), "for field buffer.");
5360 if (!buf_to_write)
5361 return 0;
5362
5363 if (datatype == in_datatype)
5364 memcpy(buf_to_write, data,
5365 (size_t)count * (size_t)TIFFDataWidth(datatype));
5366 else if (datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8)
5367 {
5368 tmsize_t i;
5369
5370 for (i = 0; i < count; i++)
5371 {
5372 ((int32_t *)buf_to_write)[i] = (int32_t)((int64_t *)data)[i];
5373 if ((int64_t)((int32_t *)buf_to_write)[i] != ((int64_t *)data)[i])
5374 {
5375 _TIFFfreeExt(tif, buf_to_write);
5376 TIFFErrorExtR(tif, module,
5377 "Value exceeds 32bit range of output type.");
5378 return 0;
5379 }
5380 }
5381 }
5382 else if ((datatype == TIFF_LONG && in_datatype == TIFF_LONG8) ||
5383 (datatype == TIFF_IFD && in_datatype == TIFF_IFD8))
5384 {
5385 tmsize_t i;
5386
5387 for (i = 0; i < count; i++)
5388 {
5389 ((uint32_t *)buf_to_write)[i] = (uint32_t)((uint64_t *)data)[i];
5390 if ((uint64_t)((uint32_t *)buf_to_write)[i] !=
5391 ((uint64_t *)data)[i])
5392 {
5393 _TIFFfreeExt(tif, buf_to_write);
5394 TIFFErrorExtR(tif, module,
5395 "Value exceeds 32bit range of output type.");
5396 return 0;
5397 }
5398 }
5399 }
5400 else if (datatype == TIFF_SHORT && in_datatype == TIFF_LONG8)
5401 {
5402 tmsize_t i;
5403
5404 for (i = 0; i < count; i++)
5405 {
5406 ((uint16_t *)buf_to_write)[i] = (uint16_t)((uint64_t *)data)[i];
5407 if ((uint64_t)((uint16_t *)buf_to_write)[i] !=
5408 ((uint64_t *)data)[i])
5409 {
5410 _TIFFfreeExt(tif, buf_to_write);
5411 TIFFErrorExtR(tif, module,
5412 "Value exceeds 16bit range of output type.");
5413 return 0;
5414 }
5415 }
5416 }
5417 else
5418 {
5419 TIFFErrorExtR(tif, module, "Unhandled type conversion.");
5420 return 0;
5421 }
5422
5423 if (TIFFDataWidth(datatype) > 1 && (tif->tif_flags & TIFF_SWAB))
5424 {
5425 if (TIFFDataWidth(datatype) == 2)
5426 TIFFSwabArrayOfShort((uint16_t *)buf_to_write, count);
5427 else if (TIFFDataWidth(datatype) == 4)
5428 TIFFSwabArrayOfLong((uint32_t *)buf_to_write, count);
5429 else if (TIFFDataWidth(datatype) == 8)
5430 TIFFSwabArrayOfLong8((uint64_t *)buf_to_write, count);
5431 }
5432
5433 /* -------------------------------------------------------------------- */
5434 /* Is this a value that fits into the directory entry? */
5435 /* -------------------------------------------------------------------- */
5436 if (!(tif->tif_flags & TIFF_BIGTIFF))
5437 {
5438 if (TIFFDataWidth(datatype) * count <= 4)
5439 {
5440 entry_offset = read_offset + 8;
5441 value_in_entry = 1;
5442 }
5443 }
5444 else
5445 {
5446 if (TIFFDataWidth(datatype) * count <= 8)
5447 {
5448 entry_offset = read_offset + 12;
5449 value_in_entry = 1;
5450 }
5451 }
5452
5453 if ((tag == TIFFTAG_TILEOFFSETS || tag == TIFFTAG_STRIPOFFSETS) &&
5457 {
5458 tif->tif_dir.td_stripoffset_entry.tdir_type = (uint16_t)datatype;
5459 tif->tif_dir.td_stripoffset_entry.tdir_count = (uint64_t)count;
5460 }
5461 else if ((tag == TIFFTAG_TILEBYTECOUNTS ||
5462 tag == TIFFTAG_STRIPBYTECOUNTS) &&
5466 {
5467 tif->tif_dir.td_stripbytecount_entry.tdir_type = (uint16_t)datatype;
5468 tif->tif_dir.td_stripbytecount_entry.tdir_count = (uint64_t)count;
5469 }
5470
5471 /* -------------------------------------------------------------------- */
5472 /* If the tag type, and count match, then we just write it out */
5473 /* over the old values without altering the directory entry at */
5474 /* all. */
5475 /* -------------------------------------------------------------------- */
5476 if (entry_count == (uint64_t)count && entry_type == (uint16_t)datatype)
5477 {
5478 if (!SeekOK(tif, entry_offset))
5479 {
5480 _TIFFfreeExt(tif, buf_to_write);
5481 TIFFErrorExtR(tif, module,
5482 "%s: Seek error accessing TIFF directory",
5483 tif->tif_name);
5484 return 0;
5485 }
5486 if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype)))
5487 {
5488 _TIFFfreeExt(tif, buf_to_write);
5489 TIFFErrorExtR(tif, module, "Error writing directory link");
5490 return (0);
5491 }
5492
5493 _TIFFfreeExt(tif, buf_to_write);
5494 return 1;
5495 }
5496
5497 /* -------------------------------------------------------------------- */
5498 /* Otherwise, we write the new tag data at the end of the file. */
5499 /* -------------------------------------------------------------------- */
5500 if (!value_in_entry)
5501 {
5502 entry_offset = TIFFSeekFile(tif, 0, SEEK_END);
5503
5504 if (!WriteOK(tif, buf_to_write, count * TIFFDataWidth(datatype)))
5505 {
5506 _TIFFfreeExt(tif, buf_to_write);
5507 TIFFErrorExtR(tif, module, "Error writing directory link");
5508 return (0);
5509 }
5510 }
5511 else
5512 {
5513 if (count * TIFFDataWidth(datatype) == 4)
5514 {
5515 uint32_t value;
5516 memcpy(&value, buf_to_write,
5517 (size_t)count * (size_t)TIFFDataWidth(datatype));
5518 entry_offset = value;
5519 }
5520 else
5521 {
5522 memcpy(&entry_offset, buf_to_write,
5523 (size_t)count * (size_t)TIFFDataWidth(datatype));
5524 }
5525 }
5526
5527 _TIFFfreeExt(tif, buf_to_write);
5528 buf_to_write = 0;
5529
5530 /* -------------------------------------------------------------------- */
5531 /* Adjust the directory entry. */
5532 /* -------------------------------------------------------------------- */
5533 entry_type = (uint16_t)datatype;
5534 entry_count = (uint64_t)count;
5535 memcpy(direntry_raw + 2, &entry_type, sizeof(uint16_t));
5536 if (tif->tif_flags & TIFF_SWAB)
5537 TIFFSwabShort((uint16_t *)(direntry_raw + 2));
5538
5539 if (!(tif->tif_flags & TIFF_BIGTIFF))
5540 {
5541 uint32_t value;
5542
5543 value = (uint32_t)entry_count;
5544 memcpy(direntry_raw + 4, &value, sizeof(uint32_t));
5545 if (tif->tif_flags & TIFF_SWAB)
5546 TIFFSwabLong((uint32_t *)(direntry_raw + 4));
5547
5548 value = (uint32_t)entry_offset;
5549 memcpy(direntry_raw + 8, &value, sizeof(uint32_t));
5550 if (tif->tif_flags & TIFF_SWAB)
5551 TIFFSwabLong((uint32_t *)(direntry_raw + 8));
5552 }
5553 else
5554 {
5555 memcpy(direntry_raw + 4, &entry_count, sizeof(uint64_t));
5556 if (tif->tif_flags & TIFF_SWAB)
5557 TIFFSwabLong8((uint64_t *)(direntry_raw + 4));
5558
5559 memcpy(direntry_raw + 12, &entry_offset, sizeof(uint64_t));
5560 if (tif->tif_flags & TIFF_SWAB)
5561 TIFFSwabLong8((uint64_t *)(direntry_raw + 12));
5562 }
5563
5564 /* -------------------------------------------------------------------- */
5565 /* Write the directory entry out to disk. */
5566 /* -------------------------------------------------------------------- */
5567 if (!SeekOK(tif, read_offset))
5568 {
5569 TIFFErrorExtR(tif, module, "%s: Seek error accessing TIFF directory",
5570 tif->tif_name);
5571 return 0;
5572 }
5573
5574 if (!WriteOK(tif, direntry_raw, dirsize))
5575 {
5576 TIFFErrorExtR(tif, module, "%s: Can not write TIFF directory entry.",
5577 tif->tif_name);
5578 return 0;
5579 }
5580
5581 return 1;
5582}
uint32_t _TIFFCastUInt64ToUInt32(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:153
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:140
float _TIFFClampDoubleToFloat(double val)
Clamp a double to the finite float range before conversion.
Definition tif_aux.c:602
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
Attempt to remove a shared directory-map entry by offset.
int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
Register or validate a chain index/offset pair for loop detection.
int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
Resolve a directory offset using the chain maps.
#define FIELD_RESOLUTION
Index the resolution presence bit; not an on-disk tag number.
Definition tif_dir.h:429
#define FIELD_CODEC
Start the range reserved for codec-private presence bits.
Definition tif_dir.h:602
#define FIELD_SUBFILETYPE
Index the subfile type presence bit; not an on-disk tag number.
Definition tif_dir.h:441
#define FIELD_STRIPOFFSETS
Index the strile offsets presence bit; not an on-disk tag number.
Definition tif_dir.h:519
#define FIELD_TRANSFERFUNCTION
Index the transfer function presence bit; not an on-disk tag number.
Definition tif_dir.h:580
#define FIELD_HALFTONEHINTS
Index the halftone hints presence bit; not an on-disk tag number.
Definition tif_dir.h:559
#define FIELD_ORIENTATION
Index the orientation presence bit; not an on-disk tag number.
Definition tif_dir.h:471
#define FIELD_MINSAMPLEVALUE
Index the minimum sample value presence bit; not an on-disk tag number.
Definition tif_dir.h:487
#define FIELD_FILLORDER
Index the fill order presence bit; not an on-disk tag number.
Definition tif_dir.h:466
#define FIELD_SMAXSAMPLEVALUE
Index the per-sample maximum presence bit; not an on-disk tag number.
Definition tif_dir.h:544
#define FIELD_IMAGEDIMENSIONS
Index the image dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:419
#define FIELD_YCBCRSUBSAMPLING
Index the YCbCr subsampling presence bit; not an on-disk tag number.
Definition tif_dir.h:564
#define FIELD_COLORMAP
Index the colormap presence bit; not an on-disk tag number.
Definition tif_dir.h:524
#define FIELD_SUBIFD
Index the child IFD presence bit; not an on-disk tag number.
Definition tif_dir.h:590
#define FIELD_EXTRASAMPLES
Index the extra samples presence bit; not an on-disk tag number.
Definition tif_dir.h:529
#define FIELD_IMAGEDEPTH
Index the image depth presence bit; not an on-disk tag number.
Definition tif_dir.h:549
#define FIELD_SMINSAMPLEVALUE
Index the per-sample minimum presence bit; not an on-disk tag number.
Definition tif_dir.h:539
#define TIFFFieldSet(tif, field)
Test a directory presence bit.
Definition tif_dir.h:662
#define FIELD_MAXSAMPLEVALUE
Index the maximum sample value presence bit; not an on-disk tag number.
Definition tif_dir.h:493
#define FIELD_NUMBEROFINKS
Index the number of inks presence bit; not an on-disk tag number.
Definition tif_dir.h:595
#define FIELD_TILEDEPTH
Index the tile depth presence bit; not an on-disk tag number.
Definition tif_dir.h:554
#define FIELD_TILEDIMENSIONS
Index the tile dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:424
#define FIELD_POSITION
Index the position presence bit; not an on-disk tag number.
Definition tif_dir.h:434
#define FIELD_ROWSPERSTRIP
Index the rows per strip presence bit; not an on-disk tag number.
Definition tif_dir.h:481
#define FIELD_COMPRESSION
Index the compression presence bit; not an on-disk tag number.
Definition tif_dir.h:451
#define FIELD_STRIPBYTECOUNTS
Index the strile byte counts presence bit; not an on-disk tag number.
Definition tif_dir.h:514
#define FIELD_YCBCRPOSITIONING
Index the YCbCr positioning presence bit; not an on-disk tag number.
Definition tif_dir.h:569
#define FIELD_REFBLACKWHITE
Index the reference black/white presence bit; not an on-disk tag number.
Definition tif_dir.h:575
#define FIELD_SAMPLEFORMAT
Index the sample format presence bit; not an on-disk tag number.
Definition tif_dir.h:534
#define FIELD_PAGENUMBER
Index the page number presence bit; not an on-disk tag number.
Definition tif_dir.h:509
#define FIELD_BITSPERSAMPLE
Index the bits per sample presence bit; not an on-disk tag number.
Definition tif_dir.h:446
#define FIELD_INKNAMES
Index the ink names presence bit; not an on-disk tag number.
Definition tif_dir.h:585
#define FIELD_RESOLUTIONUNIT
Index the resolution unit presence bit; not an on-disk tag number.
Definition tif_dir.h:504
#define FIELD_PLANARCONFIG
Index the planar configuration presence bit; not an on-disk tag number.
Definition tif_dir.h:499
#define FIELD_SAMPLESPERPIXEL
Index the samples per pixel presence bit; not an on-disk tag number.
Definition tif_dir.h:476
#define FIELD_PHOTOMETRIC
Index the photometric presence bit; not an on-disk tag number.
Definition tif_dir.h:456
#define FIELD_THRESHHOLDING
Index the thresholding presence bit; not an on-disk tag number.
Definition tif_dir.h:461
@ TIFF_SETGET_C16_UINT8
Unsigned 8-bit array with 16-bit explicit count.
Definition tif_dir.h:792
@ TIFF_SETGET_C0_SINT64
Signed 64-bit array with implicit count.
Definition tif_dir.h:782
@ TIFF_SETGET_DOUBLE
Double scalar storage.
Definition tif_dir.h:758
@ TIFF_SETGET_SINT64
Signed 64-bit scalar storage.
Definition tif_dir.h:754
@ TIFF_SETGET_C32_SINT32
Signed 32-bit array with 32-bit explicit count.
Definition tif_dir.h:826
@ TIFF_SETGET_C16_FLOAT
Float array with 16-bit explicit count.
Definition tif_dir.h:808
@ TIFF_SETGET_UNDEFINED
Unsupported or unspecified argument representation.
Definition tif_dir.h:736
@ TIFF_SETGET_SINT8
Signed 8-bit scalar storage.
Definition tif_dir.h:742
@ TIFF_SETGET_C32_ASCII
Character array with 32-bit explicit count.
Definition tif_dir.h:814
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
@ TIFF_SETGET_C32_UINT8
Unsigned 8-bit array with 32-bit explicit count.
Definition tif_dir.h:816
@ TIFF_SETGET_IFD8
64-bit directory-offset scalar storage.
Definition tif_dir.h:760
@ TIFF_SETGET_C32_UINT16
Unsigned 16-bit array with 32-bit explicit count.
Definition tif_dir.h:820
@ TIFF_SETGET_C16_DOUBLE
Double array with 16-bit explicit count.
Definition tif_dir.h:810
@ TIFF_SETGET_UINT64
Unsigned 64-bit scalar storage.
Definition tif_dir.h:752
@ TIFF_SETGET_C0_UINT32
Unsigned 32-bit array with implicit count.
Definition tif_dir.h:776
@ TIFF_SETGET_C16_UINT32
Unsigned 32-bit array with 16-bit explicit count.
Definition tif_dir.h:800
@ TIFF_SETGET_UINT16_PAIR
Two separate promoted uint16_t values / uint16_t output slots.
Definition tif_dir.h:764
@ TIFF_SETGET_C32_SINT16
Signed 16-bit array with 32-bit explicit count.
Definition tif_dir.h:822
@ TIFF_SETGET_C16_UINT16
Unsigned 16-bit array with 16-bit explicit count.
Definition tif_dir.h:796
@ TIFF_SETGET_C16_SINT64
Signed 64-bit array with 16-bit explicit count.
Definition tif_dir.h:806
@ TIFF_SETGET_C0_FLOAT
Float array with implicit count.
Definition tif_dir.h:784
@ TIFF_SETGET_C0_SINT32
Signed 32-bit array with implicit count.
Definition tif_dir.h:778
@ TIFF_SETGET_C16_UINT64
Unsigned 64-bit array with 16-bit explicit count.
Definition tif_dir.h:804
@ TIFF_SETGET_C32_UINT32
Unsigned 32-bit array with 32-bit explicit count.
Definition tif_dir.h:824
@ TIFF_SETGET_C0_ASCII
Character array with implicit count.
Definition tif_dir.h:766
@ TIFF_SETGET_C32_SINT8
Signed 8-bit array with 32-bit explicit count.
Definition tif_dir.h:818
@ TIFF_SETGET_UINT16
Unsigned 16-bit scalar storage.
Definition tif_dir.h:744
@ TIFF_SETGET_C16_SINT16
Signed 16-bit array with 16-bit explicit count.
Definition tif_dir.h:798
@ TIFF_SETGET_C0_UINT8
Unsigned 8-bit array with implicit count.
Definition tif_dir.h:768
@ TIFF_SETGET_ASCII
Null-terminated string representation.
Definition tif_dir.h:738
@ TIFF_SETGET_C0_SINT8
Signed 8-bit array with implicit count.
Definition tif_dir.h:770
@ TIFF_SETGET_C16_SINT8
Signed 8-bit array with 16-bit explicit count.
Definition tif_dir.h:794
@ TIFF_SETGET_OTHER
Tag-specific calling convention outside the regular encodings.
Definition tif_dir.h:838
@ TIFF_SETGET_C0_UINT64
Unsigned 64-bit array with implicit count.
Definition tif_dir.h:780
@ TIFF_SETGET_FLOAT
Float scalar storage.
Definition tif_dir.h:756
@ TIFF_SETGET_C16_SINT32
Signed 32-bit array with 16-bit explicit count.
Definition tif_dir.h:802
@ TIFF_SETGET_SINT16
Signed 16-bit scalar storage.
Definition tif_dir.h:746
@ TIFF_SETGET_C0_SINT16
Signed 16-bit array with implicit count.
Definition tif_dir.h:774
@ TIFF_SETGET_C32_IFD8
64-bit directory-offset array with 32-bit explicit count.
Definition tif_dir.h:836
@ TIFF_SETGET_UINT32
Unsigned 32-bit scalar storage.
Definition tif_dir.h:748
@ TIFF_SETGET_C32_SINT64
Signed 64-bit array with 32-bit explicit count.
Definition tif_dir.h:830
@ TIFF_SETGET_C16_ASCII
Character array with 16-bit explicit count.
Definition tif_dir.h:790
@ TIFF_SETGET_C32_DOUBLE
Double array with 32-bit explicit count.
Definition tif_dir.h:834
@ TIFF_SETGET_UINT8
Unsigned 8-bit scalar storage.
Definition tif_dir.h:740
@ TIFF_SETGET_C0_UINT16
Unsigned 16-bit array with implicit count.
Definition tif_dir.h:772
@ TIFF_SETGET_C32_UINT64
Unsigned 64-bit array with 32-bit explicit count.
Definition tif_dir.h:828
@ TIFF_SETGET_C0_IFD8
64-bit directory-offset array with implicit count.
Definition tif_dir.h:788
@ TIFF_SETGET_C16_IFD8
64-bit directory-offset array with 16-bit explicit count.
Definition tif_dir.h:812
@ TIFF_SETGET_SINT32
Signed 32-bit scalar storage.
Definition tif_dir.h:750
@ TIFF_SETGET_C32_FLOAT
Float array with 32-bit explicit count.
Definition tif_dir.h:832
@ TIFF_SETGET_C0_DOUBLE
Double array with implicit count.
Definition tif_dir.h:786
static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t value)
Account for or emit ShortLong tag data.
static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int32_t *value)
Account for or emit Slong array tag data.
#define MAX_ITERATIONS
Bound the rational approximation loop to 64 iterations.
static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int32_t *value)
Account for or emit Slong array tag data.
static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t value)
Account for or emit Long tag data.
static uint16_t TIFFClampDoubleToUInt16(double val)
Clamp and convert a double to uint16.
static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int8_t *value)
Account for or emit Sbyte array tag data.
static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit RationalDouble array tag data.
static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, char *value)
Account for or emit Ascii tag data.
static int _WriteAsType(TIFF *tif, uint64_t strile_size, uint64_t uncompressed_threshold)
Choose whether a strile count needs a wider integer type.
static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint32_t *value)
Account for or emit Ifd array tag data.
static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit Double array tag data.
static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t value)
Account for or emit Long tag data.
static int TIFFRewriteDirectorySec(TIFF *tif, int isimage, int imagedone, uint64_t *pdiroff)
Detach and relocate an IFD using the requested writer mode.
static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Rational array tag data.
static void EvaluateIFDdatasizeWrite(TIFF *tif, uint32_t count, uint32_t typesize, uint32_t *ndir)
Accumulate external payload size and one IFD entry.
static void DoubleToSrational(double value, int32_t *num, int32_t *denom)
Convert a non-NaN double into signed rational components.
#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
Recognise already compatible double storage without conversion.
static int TIFFWriteDirectoryTagCheckedAscii(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, char *value)
Account for or emit Ascii tag data.
static int TIFFLinkDirectory(TIFF *)
Link a new IFD at an even end-of-file offset.
static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit SrationalDouble array tag data.
static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int8_t *value)
Account for or emit Sbyte array tag data.
static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint8_t *value)
Account for or emit Undefined array tag data.
static int TIFFWriteDirectoryTagCheckedShort(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint16_t value)
Account for or emit Short tag data.
static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit Double array tag data.
static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint32_t *value)
Account for or emit Long array tag data.
static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int64_t *value)
Account for or emit Slong8 array tag data.
static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint8_t *value)
Account for or emit Byte array tag data.
static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir)
Account for or emit Colormap tag data.
static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir)
Account for or emit Transferfunction tag data.
static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint32_t *value)
Account for or emit Ifd array tag data.
static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Srational array tag data.
static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int64_t *value)
Account for or emit Slong8 array tag data.
static int32_t TIFFClampDoubleToInt32(double val)
Clamp and convert a double to int32.
static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Srational array tag data.
static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint8_t *value)
Account for or emit Byte array tag data.
static int8_t TIFFClampDoubleToInt8(double val)
Clamp and convert a double to int8.
static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint16_t *value)
Account for or emit Short array tag data.
static int TIFFWriteDirectoryTagData(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint16_t datatype, uint32_t count, uint32_t datalength, void *data)
Insert one sorted IFD entry and emit its raw payload.
static int WriteAsLong4(TIFF *tif, uint64_t strile_size)
Choose whether a strile count needs a wider integer type.
static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, double value)
Account for or emit Rational tag data.
static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Rational array tag data.
static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit Sampleformat array tag data.
static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint64_t *value)
Account for or emit Long8 array tag data.
static void DoubleToRational(double value, uint32_t *num, uint32_t *denom)
Convert a double into unsigned 32-bit rational components.
int _TIFFRewriteField(TIFF *tif, uint16_t tag, TIFFDataType in_datatype, tmsize_t count, void *data)
Patch an existing field in the current on-file IFD.
static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint64_t *value)
Account for or emit IfdIfd8 array tag data.
static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Float array tag data.
static int TIFFWriteDirectoryTagLongLong8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint64_t *value)
Account for or emit LongLong8 array tag data.
static uint32_t TIFFClampDoubleToUInt32(double val)
Clamp and convert a double to uint32.
static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int16_t *value)
Account for or emit Sshort array tag data.
static void ToRationalEuclideanGCD(double value, int blnUseSignedRange, int blnUseSmallRange, uint64_t *ullNum, uint64_t *ullDenom)
Approximate a nonnegative scalar by bounded rational components.
static int WriteAsLong8(TIFF *tif, uint64_t strile_size)
Choose whether a strile count needs a wider integer type.
static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone, uint64_t *pdiroff)
Serialise the current IFD using image/custom and completion modes.
static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint16_t *value)
Account for or emit Short array tag data.
static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, float *value)
Account for or emit Float array tag data.
static int TIFFWriteDirectoryTagShortPerSample(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint16_t value)
Account for or emit Short per sample tag data.
static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, int16_t *value)
Account for or emit Sshort array tag data.
static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint8_t *value)
Account for or emit Undefined array tag data.
static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint64_t *value)
Account for or emit Ifd8 array tag data.
static int TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit RationalDouble array tag data.
static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, double *value)
Account for or emit SrationalDouble array tag data.
static int TIFFWriteDirectoryTagShort(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint16_t value)
Account for or emit Short tag data.
static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint64_t *value)
Account for or emit Long8 array tag data.
static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, uint32_t count, uint32_t *value)
Account for or emit Long array tag data.
static int16_t TIFFClampDoubleToInt16(double val)
Clamp and convert a double to int16.
static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir, uint16_t tag, double value)
Account for or emit Rational tag data.
static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32_t *ndir, TIFFDirEntry *dir)
Account for or emit Subifd tag data.
#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
Recognise already compatible float storage without conversion.
static uint8_t TIFFClampDoubleToUInt8(double val)
Clamp and convert a double to uint8.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
Definition tif_aux.c:274
#define TIFF_NON_EXISTENT_DIR_NUMBER
Mark an unknown or nonexistent directory index.
Definition tiffiop.h:95
#define TIFF_DOUBLE_EQ(x, y)
Test whether a double difference is not greater than zero in magnitude.
Definition tiffiop.h:1271
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define WriteOK(tif, buf, size)
Test whether one client write transfers the entire requested byte count.
Definition tiffiop.h:1035
#define TIFF_MYBUFFER
Mark a raw buffer owned and released by the handle.
Definition tiffiop.h:451
#define TIFF_BUFFERSETUP
Mark completed data-buffer setup.
Definition tiffiop.h:426
#define ReadOK(tif, buf, size)
Test whether one client read transfers the entire requested byte count.
Definition tiffiop.h:1000
#define TIFF_POSTENCODE
Request a pending codec postencode call.
Definition tiffiop.h:471
#define SeekOK(tif, off)
Test whether an absolute seek reaches the requested file position.
Definition tiffiop.h:1017
#define TIFF_INSUBIFD
Mark writing within a SubIFD sequence.
Definition tiffiop.h:476
#define isMapped(tif)
Test whether TIFF_MAPPED is set on a handle.
Definition tiffiop.h:845
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFF_BEENWRITING
Record that scanline or image-data writing has begun.
Definition tiffiop.h:436
#define TIFFSeekFile(tif, off, whence)
Invoke the handle's client seek callback.
Definition tiffiop.h:920
#define TIFF_BIGTIFF
Mark the BigTIFF file format.
Definition tiffiop.h:508
#define TIFF_SWAB
Request swapping multibyte file values to or from host order.
Definition tiffiop.h:441
int _TIFFFillStriles(TIFF *tif)
Materialise deferred strile arrays when required.
void TIFFSwabArrayOfLong8(uint64_t *lp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:144
void TIFFSwabArrayOfDouble(double *dp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:228
void TIFFSwabArrayOfShort(uint16_t *wp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:87
void TIFFSwabLong8(uint64_t *lp)
Reverse the byte order of one value.
Definition tif_swab.c:66
void TIFFSwabShort(uint16_t *wp)
Reverse the byte order of one value.
Definition tif_swab.c:39
void TIFFSwabArrayOfLong(uint32_t *lp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:123
void TIFFSwabLong(uint32_t *lp)
Reverse the byte order of one value.
Definition tif_swab.c:51
void TIFFSwabArrayOfFloat(float *fp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:186
#define TIFF_VARIABLE
Mark a variable-length field with a 16-bit count convention.
Definition tiffio.h:1297
#define TIFF_ANY
Match any data type when searching for a field descriptor.
Definition tiffio.h:1289
#define TIFF_VARIABLE2
Mark a variable-length field with a 32-bit count convention.
Definition tiffio.h:1310
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:68
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tdir_t TIFFNumberOfDirectories(TIFF *tif)
Count reachable main-chain directories from the cached file header.
Definition tif_dir.c:2623
void TIFFFreeDirectory(TIFF *tif)
Release the current directory's owned values and codec state.
Definition tif_dir.c:2165
int TIFFCreateDirectory(TIFF *tif)
Replace current values with a fresh in-memory image directory.
Definition tif_dir.c:2246
int TIFFDataWidth(TIFFDataType type)
Report the on-disk byte width of a TIFF data type.
uint32_t TIFFFieldTag(const TIFFField *fip)
Read a field descriptor's tag number.
const TIFFField * TIFFFindField(TIFF *tif, uint32_t tag, TIFFDataType dt)
Find a registered field by tag number and file data type.
int TIFFFieldSetGetSize(const TIFFField *fip)
Report the internal element storage width of a field.
int TIFFCheckpointDirectory(TIFF *tif)
Write the current image directory while retaining its values.
int TIFFRewriteDirectory(TIFF *tif)
Relocate the current image IFD and repair its main-chain link.
int TIFFDeferStrileArrayWriting(TIFF *tif)
Reserve strile-array entries for deferred payload writing.
int TIFFWriteDirectory(TIFF *tif)
Finish the current image directory and prepare a fresh one.
int TIFFWriteCustomDirectory(TIFF *tif, uint64_t *pdiroff)
Write current custom values and return their IFD offset.
int TIFFSetupStrips(TIFF *tif)
Replace strile offset/count arrays from current geometry.
Definition tif_write.c:729
uint64_t TIFFTileSize64(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:303
uint64_t TIFFStripSize64(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:236
void TIFFSetWriteOffset(TIFF *tif, toff_t off)
Change the cached next payload write offset.
Definition tif_write.c:1244
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
Compare two byte ranges lexicographically.
Definition tif_unix.c:502
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Query a present tag or a supported default.
Definition tif_aux.c:591
int TIFFGetField(TIFF *tif, uint32_t tag,...)
Query a present tag in the current directory.
Definition tif_dir.c:2099
#define COMPRESSION_NONE
Dump mode.
Definition tiff.h:302
#define TIFFTAG_BITSPERSAMPLE
TIFF tag: Bits per channel (sample).
Definition tiff.h:298
#define TIFFTAG_RESOLUTIONUNIT
TIFF tag: Units of resolutions.
Definition tiff.h:516
#define COMPRESSION_ZSTD
Zstandard compression; not registered in the Adobe-maintained registry.
Definition tiff.h:372
#define TIFFTAG_SUBIFD
TIFF tag: Subimage descriptors.
Definition tiff.h:584
#define TIFFTAG_FILLORDER
TIFF tag: Data order within a byte.
Definition tiff.h:422
#define TIFFTAG_SAMPLESPERPIXEL
TIFF tag: Samples per pixel.
Definition tiff.h:456
#define COMPRESSION_DEFLATE
Deflate compression, legacy tag.
Definition tiff.h:349
#define COMPRESSION_JXL
JPEG XL compression; not registered in the Adobe-maintained registry.
Definition tiff.h:378
#define TIFFTAG_YPOSITION
TIFF tag: Y page offset of image lhs.
Definition tiff.h:480
#define SAMPLEFORMAT_UINT
Unsigned integer data (introduced in TIFF 6.0).
Definition tiff.h:610
#define TIFFTAG_COLORMAP
TIFF tag: RGB map for palette image.
Definition tiff.h:560
#define TIFFTAG_IMAGEDEPTH
TIFF tag: Z depth of image.
Definition tiff.h:772
#define TIFFTAG_PHOTOMETRIC
TIFF tag: Photometric interpretation.
Definition tiff.h:382
#define TIFFTAG_TILELENGTH
TIFF tag: Tile height in pixels (introduced in TIFF 6.0).
Definition tiff.h:566
#define TIFFTAG_MAXSAMPLEVALUE
TIFF tag: Maximum sample value (obsoleted by TIFF 5.0).
Definition tiff.h:464
#define TIFFTAG_TILEDEPTH
TIFF tag: Z depth/data tile.
Definition tiff.h:774
#define COMPRESSION_LZMA
LZMA2.
Definition tiff.h:367
#define TIFFTAG_EXTRASAMPLES
TIFF tag: Info about extra samples (introduced in TIFF 6.0).
Definition tiff.h:600
#define TIFFTAG_IMAGEWIDTH
TIFF tag: Image width in pixels.
Definition tiff.h:294
#define COMPRESSION_WEBP
WebP compression; not registered in the Adobe-maintained registry.
Definition tiff.h:374
#define TIFFTAG_XRESOLUTION
TIFF tag: Pixels/resolution in x.
Definition tiff.h:466
#define COMPRESSION_LERC
ESRI LERC compression.
Definition tiff.h:364
#define TIFFTAG_ORIENTATION
TIFF tag: Image orientation (obsoleted by TIFF 5.0).
Definition tiff.h:438
#define COMPRESSION_ADOBE_DEFLATE
Deflate compression, as recognized by Adobe.
Definition tiff.h:351
#define TIFFTAG_HALFTONEHINTS
TIFF tag: Highlight+shadow info (introduced in TIFF 6.0).
Definition tiff.h:562
#define TIFFTAG_MINSAMPLEVALUE
TIFF tag: Minimum sample value (obsoleted by TIFF 5.0).
Definition tiff.h:462
#define TIFFTAG_NUMBEROFINKS
TIFF tag: Number of inks (introduced in TIFF 6.0).
Definition tiff.h:594
#define SAMPLEFORMAT_IEEEFP
IEEE floating point data (introduced in TIFF 6.0).
Definition tiff.h:614
#define TIFFTAG_TRANSFERFUNCTION
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:538
#define TIFFTAG_YRESOLUTION
TIFF tag: Pixels/resolution in y.
Definition tiff.h:468
#define COMPRESSION_LZW
Lempel-Ziv & Welch.
Definition tiff.h:314
#define TIFFTAG_TILEBYTECOUNTS
TIFF tag: Byte counts for tiles (introduced in TIFF 6.0).
Definition tiff.h:570
#define TIFFTAG_STRIPBYTECOUNTS
TIFF tag: Bytes counts for strips.
Definition tiff.h:460
#define TIFFTAG_SMINSAMPLEVALUE
TIFF tag: Variable MinSampleValue (introduced in TIFF 6.0).
Definition tiff.h:622
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define TIFFTAG_TILEOFFSETS
TIFF tag: Offsets to data tiles (introduced in TIFF 6.0).
Definition tiff.h:568
#define TIFFTAG_ROWSPERSTRIP
TIFF tag: Rows per strip of data.
Definition tiff.h:458
#define TIFFTAG_SMAXSAMPLEVALUE
TIFF tag: Variable MaxSampleValue (introduced in TIFF 6.0).
Definition tiff.h:624
#define TIFFTAG_SAMPLEFORMAT
TIFF tag: Data sample format (introduced in TIFF 6.0).
Definition tiff.h:608
#define TIFFTAG_REFERENCEBLACKWHITE
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:737
#define TIFFTAG_TILEWIDTH
TIFF tag: Tile width in pixels (introduced in TIFF 6.0).
Definition tiff.h:564
#define TIFFTAG_IMAGELENGTH
TIFF tag: Image height in pixels.
Definition tiff.h:296
#define TIFFTAG_PAGENUMBER
TIFF tag: Page numbers of multi-page.
Definition tiff.h:524
#define TIFFTAG_COMPRESSION
TIFF tag: Data compression technique.
Definition tiff.h:300
#define TIFFTAG_PLANARCONFIG
TIFF tag: Storage organization.
Definition tiff.h:470
#define TIFFTAG_STRIPOFFSETS
TIFF tag: Offsets to data strips.
Definition tiff.h:436
#define TIFFTAG_SUBFILETYPE
TIFF tag: Subfile data descriptor.
Definition tiff.h:275
#define TIFFTAG_XPOSITION
TIFF tag: X page offset of image lhs.
Definition tiff.h:478
#define TIFFTAG_YCBCRPOSITIONING
TIFF tag: Subsample positioning (introduced in TIFF 6.0).
Definition tiff.h:731
#define TIFFTAG_INKNAMES
TIFF tag: Ascii names of inks (introduced in TIFF 6.0).
Definition tiff.h:592
#define TIFFTAG_THRESHHOLDING
TIFF tag: Thresholding used on data (obsoleted by TIFF 5.0).
Definition tiff.h:410
#define SAMPLEFORMAT_INT
Signed integer data (introduced in TIFF 6.0).
Definition tiff.h:612
#define COMPRESSION_JPEG
JPEG DCT compression (introduced after TIFF 6.0).
Definition tiff.h:318
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
TIFFDataType
Identify the on-disk representation of a TIFF directory field.
Definition tiff.h:229
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
uint32_t tdir_t
Represent a zero-based directory index, rather than a file offset.
Definition tiffio.h:96
@ TIFF_SSHORT
Signed 16-bit integer.
Definition tiff.h:247
@ TIFF_SLONG
Signed 32-bit integer.
Definition tiff.h:249
@ TIFF_BYTE
Unsigned 8-bit integer.
Definition tiff.h:233
@ TIFF_SBYTE
Signed 8-bit integer.
Definition tiff.h:243
@ TIFF_IFD
Unsigned 32-bit IFD byte offset.
Definition tiff.h:257
@ TIFF_SHORT
Unsigned 16-bit integer.
Definition tiff.h:237
@ TIFF_UNDEFINED
Untyped 8-bit data.
Definition tiff.h:245
@ TIFF_LONG
Unsigned 32-bit integer.
Definition tiff.h:239
@ TIFF_SLONG8
BigTIFF signed 64-bit integer.
Definition tiff.h:261
@ TIFF_LONG8
BigTIFF unsigned 64-bit integer.
Definition tiff.h:259
@ TIFF_ASCII
Sequence of 8-bit bytes ending in a null byte.
Definition tiff.h:235
@ TIFF_NOTYPE
Placeholder for an unspecified field type.
Definition tiff.h:231
@ TIFF_FLOAT
32-bit IEEE floating-point value.
Definition tiff.h:253
@ TIFF_DOUBLE
64-bit IEEE floating-point value.
Definition tiff.h:255
@ TIFF_IFD8
BigTIFF unsigned 64-bit IFD byte offset.
Definition tiff.h:263
@ TIFF_SRATIONAL
Signed 32-bit numerator and denominator.
Definition tiff.h:251
@ TIFF_RATIONAL
Unsigned 32-bit numerator and denominator.
Definition tiff.h:241
Represent a parsed directory entry before value conversion.
Definition tif_dir.h:82
uint64_t tdir_count
Number of file-type elements, normalised to host order.
Definition tif_dir.h:89
uint16_t tdir_type
On-disk TIFFDataType code.
Definition tif_dir.h:86
uint64_t toff_long8
Raw 64-bit view of inline bytes or offset storage.
Definition tif_dir.h:99
uint16_t tdir_tag
Directory tag identifier.
Definition tif_dir.h:84
union TIFFDirEntry::@0 tdir_offset
Overlay raw directory inline bytes and offset-width views.
int td_customValueCount
Number of live records in td_customValues, including partial setter entries.
Definition tif_dir.h:346
uint16_t td_pagenumber[2]
Page index and total-page count as two uint16_t values.
Definition tif_dir.h:206
float td_xposition
Horizontal image position in resolution units.
Definition tif_dir.h:202
uint32_t td_tiledepth
Tile depth in slices.
Definition tif_dir.h:164
uint16_t td_fillorder
FILLORDER_* bit order within file bytes.
Definition tif_dir.h:178
float td_xresolution
Horizontal pixels per resolution unit.
Definition tif_dir.h:194
uint64_t td_dirdatasize_read
Reusable IFD span including entry space and contiguous external data; UINT64_MAX permits extension at...
Definition tif_dir.h:375
int td_inknameslen
Byte length of the concatenated ink-name strings, including terminators.
Definition tif_dir.h:333
float * td_refblackwhite
Owned six-float reference black/white array, or NULL.
Definition tif_dir.h:327
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
Definition tif_dir.h:184
uint32_t td_imagewidth
Image width in pixels.
Definition tif_dir.h:154
uint16_t td_compression
COMPRESSION_* codec identifier.
Definition tif_dir.h:172
uint16_t td_photometric
PHOTOMETRIC_* interpretation.
Definition tif_dir.h:174
uint32_t td_tilewidth
Tile width in pixels.
Definition tif_dir.h:160
float td_yresolution
Vertical pixels per resolution unit.
Definition tif_dir.h:196
uint16_t td_extrasamples
Number of extra samples described by td_sampleinfo.
Definition tif_dir.h:232
uint16_t td_threshholding
THRESHHOLD_* bilevel rendering convention.
Definition tif_dir.h:176
uint16_t td_minsamplevalue
Legacy unsigned minimum sample value.
Definition tif_dir.h:186
double * td_sminsamplevalue
Owned SamplesPerPixel-length array of minimum values, or NULL.
Definition tif_dir.h:190
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
char * td_inknames
Owned concatenated null-terminated ink names, or NULL.
Definition tif_dir.h:335
TIFFDirEntry td_stripoffset_entry
Deferred offset entry; tag/type/count are swapped but offset storage is raw.
Definition tif_dir.h:301
uint16_t * td_transferfunction[3]
Up to three owned gamma curves of td_transferfunction_count entries each.
Definition tif_dir.h:320
uint16_t td_maxsamplevalue
Legacy unsigned maximum sample value.
Definition tif_dir.h:188
uint32_t td_imagedepth
Image depth in slices.
Definition tif_dir.h:158
uint16_t td_orientation
ORIENTATION_* storage orientation.
Definition tif_dir.h:180
uint16_t td_resolutionunit
RESUNIT_* unit for resolution and position values.
Definition tif_dir.h:198
uint32_t td_colormap_count
Number of entries in each ColorMap plane that the directory uses, zero when none is installed.
Definition tif_dir.h:223
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
TIFFTagValue * td_customValues
Owned array of custom-value records, each owning its value.
Definition tif_dir.h:348
double * td_smaxsamplevalue
Owned SamplesPerPixel-length array of maximum values, or NULL.
Definition tif_dir.h:192
uint64_t * td_stripbytecount_p
Owned byte-count array/cache, possibly NULL or partial; use TIFFGetStrileByteCount().
Definition tif_dir.h:281
unsigned char td_iswrittentofile
Nonzero when the current directory has an on-file representation.
Definition tif_dir.h:359
uint64_t * td_stripoffset_p
Owned offset array/cache, possibly NULL or partial; use TIFFGetStrileOffset().
Definition tif_dir.h:276
uint16_t * td_colormap[3]
Three owned palette planes of td_colormap_alloc entries each, or NULL slots; presence is FIELD_COLORM...
Definition tif_dir.h:218
uint16_t td_sampleformat
SAMPLEFORMAT_* interpretation of samples.
Definition tif_dir.h:170
uint16_t td_ycbcrsubsampling[2]
Horizontal and vertical YCbCr subsampling factors.
Definition tif_dir.h:312
uint64_t td_dirdatasize_write
Accumulated IFD write size in bytes, including entry space after counting.
Definition tif_dir.h:369
unsigned char td_deferstrilearraywriting
Nonzero to reserve strile entries for later explicit array writing.
Definition tif_dir.h:354
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_subfiletype
NewSubfileType flag mask.
Definition tif_dir.h:166
uint32_t td_nstrips
Total strile count across all planes; logical array length.
Definition tif_dir.h:271
uint16_t td_numberofinks
Number of inks, kept consistent with accepted InkNames where possible.
Definition tif_dir.h:340
TIFFDirEntry td_stripbytecount_entry
Deferred byte-count entry; offset storage still requires swapping.
Definition tif_dir.h:305
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
uint32_t td_tilelength
Tile length in rows.
Definition tif_dir.h:162
uint16_t td_nsubifd
Number of child-directory offsets in td_subifd.
Definition tif_dir.h:307
uint64_t * td_subifd
Owned array of td_nsubifd child-directory offsets, or NULL.
Definition tif_dir.h:309
float td_yposition
Vertical image position in resolution units.
Definition tif_dir.h:204
uint32_t td_transferfunction_count
Number of entries in each installed TransferFunction curve, zero when none is installed; the only rec...
Definition tif_dir.h:325
uint16_t td_ycbcrpositioning
YCBCRPOSITION_* placement convention.
Definition tif_dir.h:314
uint16_t td_halftonehints[2]
Highlight and shadow grayscale breakpoints.
Definition tif_dir.h:230
uint64_t tiff_diroff
First IFD byte offset; zero if none is linked.
Definition tiff.h:205
uint32_t tiff_diroff
First IFD byte offset; zero if none is linked.
Definition tiff.h:187
void * value
Owned custom-value allocation, or NULL after clearing/failure.
Definition tif_dir.h:56
int count
Number of internally stored elements; after a partial setter failure it may be zero or a nonzero coun...
Definition tif_dir.h:54
const TIFFField * info
Borrowed live descriptor; must outlive this custom value.
Definition tif_dir.h:48
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
TIFFDataType field_type
On-disk TIFFDataType, distinct from the argument representation.
Definition tif_dir.h:1009
unsigned char field_passcount
Nonzero for an explicit count argument; tag-specific exceptions still apply.
Definition tif_dir.h:1033
const char * field_name
Borrowed or anonymous-owned null-terminated name; must remain live while used.
Definition tif_dir.h:1038
unsigned short field_bit
FIELD_* presence index; FIELD_CUSTOM shares storage through the custom list.
Definition tif_dir.h:1023
uint32_t field_tag
Tag identifier in this schema namespace.
Definition tif_dir.h:995
short field_readcount
Fixed getter count or TIFF_VARIABLE/TIFF_VARIABLE2/TIFF_SPP rule.
Definition tif_dir.h:1001
TIFFSetGetFieldType set_get_field_type
Internal storage/count representation for tag access and conversion.
Definition tif_dir.h:1018
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
uint64_t tif_subifdoff
File byte offset at which the next SubIFD link is patched.
Definition tiffiop.h:618
tdir_t tif_curdircount
Cache the number of main IFDs in the file.
Definition tiffiop.h:604
uint16_t tif_nsubifd
Number of remaining SubIFD links to write.
Definition tiffiop.h:616
tmsize_t tif_rawdataloaded
Number of bytes loaded into the raw-buffer window.
Definition tiffiop.h:690
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
size_t tif_nfields
Number of entries in tif_fields.
Definition tiffiop.h:735
uint64_t tif_nextdiroff
Next directory byte offset from the current chain link.
Definition tiffiop.h:560
TIFFField ** tif_fields
Owned sorted array of field pointers; descriptors have mixed ownership.
Definition tiffiop.h:733
uint64_t tif_diroff
Current directory byte offset, or zero for an unlinked in-memory IFD.
Definition tiffiop.h:558
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFVoidMethod tif_close
Codec close callback; distinct from the client I/O close callback.
Definition tiffiop.h:657
uint64_t tif_dataoff
Byte offset used when placing directory data in the file.
Definition tiffiop.h:613
tdir_t tif_curdir
Track the current or last-written directory index.
Definition tiffiop.h:599
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
TIFFHeaderUnion tif_header
Inline decoded classic TIFF or BigTIFF file header.
Definition tiffiop.h:578
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
tmsize_t tif_rawdataoff
Byte offset of the raw-buffer window within the strip.
Definition tiffiop.h:688
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
uint64_t tif_lastdiroff
Byte offset of the last directory written so far.
Definition tiffiop.h:562
int tif_mode
Current access mode using the O_* constants.
Definition tiffiop.h:400
Declare private library state, callbacks, and implementation helpers.
TIFFHeaderClassic classic
Header when version is TIFF_VERSION_CLASSIC.
Definition tiffiop.h:365
TIFFHeaderBig big
Header when version is TIFF_VERSION_BIG.
Definition tiffiop.h:367