LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_dir.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 Tag Get & Set Routines.
29 * (and also some miscellaneous stuff)
30 */
36#include "tiffiop.h"
37#include <float.h> /*--: for Rational2Double */
38#include <limits.h>
39#include <math.h>
40
41/*
42 * These are used in the backwards compatibility code...
43 */
48#define DATATYPE_VOID 0 /* !untyped data */
53#define DATATYPE_INT 1 /* !signed integer data */
58#define DATATYPE_UINT 2 /* !unsigned integer data */
63#define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
64
86static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb,
87 size_t elem_size)
88{
89 void *old = *vpp;
90 *vpp = NULL;
91 if (vp)
92 {
93 tmsize_t bytes = _TIFFMultiplySSize(NULL, (tmsize_t)nmemb,
94 (tmsize_t)elem_size, NULL);
95 if (bytes)
96 *vpp = (void *)_TIFFmallocExt(tif, bytes);
97 if (*vpp)
98 _TIFFmemcpy(*vpp, vp, bytes);
99 }
100 if (old)
101 _TIFFfreeExt(tif, old);
102}
103void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n)
104{
105 setByteArray(NULL, vpp, vp, n, 1);
106}
107void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
108{
109 setByteArray(tif, vpp, vp, n, 1);
110}
111
126static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n)
127{
128 setByteArray(tif, (void **)cpp, cp, n, 1);
129}
130
131void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n)
132{
133 setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t));
134}
135void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp,
136 uint32_t n)
137{
138 setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t));
139}
140
141void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n)
142{
143 setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t));
144}
145void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp,
146 uint32_t n)
147{
148 setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t));
149}
150
165static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp,
166 uint32_t n)
167{
168 setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t));
169}
170
171void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n)
172{
173 setByteArray(NULL, (void **)fpp, fp, n, sizeof(float));
174}
175void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n)
176{
177 setByteArray(tif, (void **)fpp, fp, n, sizeof(float));
178}
179
180void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n)
181{
182 setByteArray(NULL, (void **)dpp, dp, n, sizeof(double));
183}
184void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp,
185 uint32_t n)
186{
187 setByteArray(tif, (void **)dpp, dp, n, sizeof(double));
188}
189
208static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value,
209 size_t nmemb)
210{
211 tmsize_t bytes;
212
213 if (*vpp)
214 _TIFFfreeExt(tif, *vpp);
215 /* Size the allocation in the same checked way as setByteArray(). */
216 bytes = _TIFFMultiplySSize(NULL, (tmsize_t)nmemb, (tmsize_t)sizeof(double),
217 NULL);
218 *vpp = bytes ? (double *)_TIFFmallocExt(tif, bytes) : NULL;
219 if (*vpp)
220 {
221 while (nmemb--)
222 ((double *)*vpp)[nmemb] = value;
223 }
224}
225
241static int setShortArrayWithCount(TIFF *tif, uint16_t **wpp, uint32_t *countp,
242 const uint16_t *wp, uint32_t n)
243{
244 _TIFFsetShortArrayExt(tif, wpp, wp, n);
245 if (*wpp == NULL)
246 {
247 *countp = 0;
248 return 0;
249 }
250 *countp = n;
251 return 1;
252}
253
264static void cancelColorMap(TIFF *tif)
265{
266 TIFFDirectory *td = &tif->tif_dir;
267 int i;
268
269 for (i = 0; i < 3; i++)
270 {
271 if (td->td_colormap[i] != NULL)
272 {
273 _TIFFfreeExt(tif, td->td_colormap[i]);
274 td->td_colormap[i] = NULL;
275 }
276 }
277 td->td_colormap_count = 0;
278 td->td_colormap_alloc = 0;
280}
281
293{
294 TIFFDirectory *td = &tif->tif_dir;
295 int i;
296
297 for (i = 0; i < 3; i++)
298 {
299 if (td->td_transferfunction[i] != NULL)
300 {
302 td->td_transferfunction[i] = NULL;
303 }
304 }
307}
308
325static int setColorMap(TIFF *tif, va_list ap, uint32_t count)
326{
327 TIFFDirectory *td = &tif->tif_dir;
328 static const char module[] = "setColorMap";
329 uint16_t *cm[3];
330 int i;
331
332 for (i = 0; i < 3; i++)
333 {
334 cm[i] = va_arg(ap, uint16_t *);
335 if (cm[i] == NULL) /* typically missing param */
336 {
337 TIFFErrorExtR(tif, module,
338 "%s: ColorMap requires three arrays of %" PRIu32
339 " values each",
340 tif->tif_name, count);
341 return 0; /* nothing installed yet, leave the old value alone */
342 }
343 }
344 for (i = 0; i < 3; i++)
345 {
346 if (!setShortArrayWithCount(tif, &td->td_colormap[i],
347 &td->td_colormap_count, cm[i], count))
348 {
349 cancelColorMap(tif);
350 return 0;
351 }
352 }
353 td->td_colormap_alloc = count;
354 return 1;
355}
356
372static int setTransferFunction(TIFF *tif, va_list ap, uint32_t count)
373{
374 TIFFDirectory *td = &tif->tif_dir;
375 static const char module[] = "setTransferFunction";
376 uint16_t *tf[3];
377 uint32_t i;
378 uint32_t v;
379
380 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
381 for (i = 0; i < v; i++)
382 {
383 tf[i] = va_arg(ap, uint16_t *);
384 if (tf[i] == NULL) /* typically missing param */
385 {
386 TIFFErrorExtR(tif, module,
387 "%s: TransferFunction requires %" PRIu32
388 " curves of %" PRIu32 " values each",
389 tif->tif_name, v, count);
390 return 0; /* nothing installed yet, leave the old value alone */
391 }
392 }
393 for (i = 0; i < v; i++)
394 {
396 &td->td_transferfunction_count, tf[i],
397 count))
398 {
400 return 0;
401 }
402 }
403 return 1;
404}
405
406/*
407 * Install extra samples information.
408 */
441static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
442{
443/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
444#define EXTRASAMPLE_COREL_UNASSALPHA 999
445
446 uint16_t *va;
447 uint32_t i;
448 TIFFDirectory *td = &tif->tif_dir;
449 static const char module[] = "setExtraSamples";
450
451 *v = (uint16_t)va_arg(ap, uint16_vap);
452 if ((uint16_t)*v > td->td_samplesperpixel)
453 return 0;
454 va = va_arg(ap, uint16_t *);
455 if (*v > 0 && va == NULL) /* typically missing param */
456 return 0;
457 for (i = 0; i < *v; i++)
458 {
459 if (va[i] > EXTRASAMPLE_UNASSALPHA)
460 {
461 /*
462 * XXX: Corel Draw is known to produce incorrect
463 * ExtraSamples tags which must be patched here if we
464 * want to be able to open some of the damaged TIFF
465 * files:
466 */
467 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
469 else
470 return 0;
471 }
472 }
473
474 if (td->td_transferfunction[0] != NULL &&
475 (td->td_samplesperpixel - *v > 1) &&
476 !(td->td_samplesperpixel - td->td_extrasamples > 1))
477 {
478 TIFFWarningExtR(tif, module,
479 "ExtraSamples tag value is changing, "
480 "but TransferFunction was read with a different value. "
481 "Canceling it");
483 }
484
485 td->td_extrasamples = (uint16_t)*v;
487 return 1;
488
489#undef EXTRASAMPLE_COREL_UNASSALPHA
490}
491
492/*
493 * Count ink names separated by \0. Returns
494 * zero if the ink names are not as expected.
495 */
512static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
513{
514 uint16_t i = 0;
515
516 if (slen > 0)
517 {
518 const char *ep = s + slen;
519 const char *cp = s;
520 do
521 {
522 for (; cp < ep && *cp != '\0'; cp++)
523 {
524 }
525 if (cp >= ep)
526 goto bad;
527 cp++; /* skip \0 */
528 i++;
529 } while (cp < ep);
530 return (i);
531 }
532bad:
533 TIFFErrorExtR(tif, "TIFFSetField",
534 "%s: Invalid InkNames value; no null at given buffer end "
535 "location %" PRIu32 ", after %" PRIu16 " ink",
536 tif->tif_name, slen, i);
537 return (0);
538}
539
554static void removeCustomValue(TIFFDirectory *td, int i)
555{
556 for (; i < td->td_customValueCount - 1; i++)
557 td->td_customValues[i] = td->td_customValues[i + 1];
559}
560
599static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
600{
601 static const char module[] = "_TIFFVSetField";
602
603 TIFFDirectory *td = &tif->tif_dir;
604 int status = 1;
605 uint32_t v32, v;
606 double dblval;
607 char *s;
608 void *old_custom_value = NULL;
609 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
610 uint32_t standard_tag = tag;
611 if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */
612 return 0;
613 /*
614 * We want to force the custom code to be used for custom
615 * fields even if the tag happens to match a well known
616 * one - important for reinterpreted handling of standard
617 * tag values in custom directories (i.e. EXIF)
618 */
619 if (fip->field_bit == FIELD_CUSTOM)
620 {
621 standard_tag = 0;
622 }
623
624 switch (standard_tag)
625 {
627 td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t);
628 break;
630 td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t);
631 break;
633 td->td_imagelength = (uint32_t)va_arg(ap, uint32_t);
634 break;
636 v = (uint16_t)va_arg(ap, uint16_vap);
637 /*
638 * ColorMap and TransferFunction were allocated with
639 * 2**BitsPerSample entries per plane, for the BitsPerSample in
640 * effect when they were set. A ColorMap keeps those planes for
641 * any BitsPerSample they are still long enough for, as below;
642 * beyond that capacity, and for the TransferFunction on any
643 * change, the tag would be the wrong length for the directory
644 * it describes, so cancel it, as SamplesPerPixel already does
645 * for SMinSampleValue and SMaxSampleValue (see
646 * http://bugzilla.maptools.org/show_bug.cgi?id=2500), and as
647 * SamplesPerPixel and ExtraSamples already do for the number of
648 * TransferFunction curves (see
649 * http://bugzilla.maptools.org/show_bug.cgi?id=2820).
650 *
651 * Only a tag the caller actually set is cancelled: the default
652 * TransferFunction built by TIFFGetFieldDefaulted() does not set
653 * FIELD_TRANSFERFUNCTION and is not the caller's to lose.
654 */
655 if (v != td->td_bitspersample)
656 {
658 {
659 /*
660 * A BitsPerSample that needs no more entries than are
661 * allocated leaves planes that still describe the
662 * directory, so only the recorded length has to follow.
663 * The capacity is kept, so narrowing and then widening
664 * again restores the original entries. Discarding the
665 * planes would write a Palette image with no ColorMap,
666 * which cannot be read back.
667 */
668 uint64_t newcount = v < 32 ? (uint64_t)1 << v : 0;
669
670 if (newcount != 0 && newcount <= td->td_colormap_alloc)
671 td->td_colormap_count = (uint32_t)newcount;
672 else
673 {
674 TIFFWarningExtR(tif, module,
675 "BitsPerSample tag value is changing, "
676 "but ColorMap was read with a "
677 "different value. Canceling it");
678 cancelColorMap(tif);
679 }
680 }
682 {
683 TIFFWarningExtR(tif, module,
684 "BitsPerSample tag value is changing, "
685 "but TransferFunction was read with a "
686 "different value. Canceling it");
688 }
689 }
690 td->td_bitspersample = (uint16_t)v;
691 /*
692 * If the data require post-decoding processing to byte-swap
693 * samples, set it up here. Note that since tags are required
694 * to be ordered, compression code can override this behavior
695 * in the setup method if it wants to roll the post decoding
696 * work in with its normal work.
697 */
698 if (tif->tif_flags & TIFF_SWAB)
699 {
700 if (td->td_bitspersample == 8)
702 else if (td->td_bitspersample == 16)
704 else if (td->td_bitspersample == 24)
706 else if (td->td_bitspersample == 32)
708 else if (td->td_bitspersample == 64)
710 else if (td->td_bitspersample == 128) /* two 64's */
712 }
713 break;
715 v = (uint16_t)va_arg(ap, uint16_vap);
716 /*
717 * If we're changing the compression scheme, notify the
718 * previous module so that it can cleanup any state it's
719 * setup.
720 */
722 {
723 if ((uint32_t)td->td_compression == v)
724 break;
725 (*tif->tif_cleanup)(tif);
726 tif->tif_flags &= ~TIFF_CODERSETUP;
727 }
728 /*
729 * Setup new compression routine state.
730 */
731 if ((status = TIFFSetCompressionScheme(tif, (int)v)) != 0)
732 td->td_compression = (uint16_t)v;
733 else
734 status = 0;
735 break;
737 td->td_photometric = (uint16_t)va_arg(ap, uint16_vap);
738 break;
740 td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap);
741 break;
743 v = (uint16_t)va_arg(ap, uint16_vap);
744 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
745 goto badvalue;
746 td->td_fillorder = (uint16_t)v;
747 break;
749 v = (uint16_t)va_arg(ap, uint16_vap);
751 goto badvalue;
752 else
753 td->td_orientation = (uint16_t)v;
754 break;
756 v = (uint16_t)va_arg(ap, uint16_vap);
757 if (v == 0)
758 goto badvalue;
759 if (v != td->td_samplesperpixel)
760 {
761 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
762 if (td->td_sminsamplevalue != NULL)
763 {
764 TIFFWarningExtR(tif, module,
765 "SamplesPerPixel tag value is changing, "
766 "but SMinSampleValue tag was read with a "
767 "different value. Canceling it");
769 _TIFFfreeExt(tif, td->td_sminsamplevalue);
770 td->td_sminsamplevalue = NULL;
771 }
772 if (td->td_smaxsamplevalue != NULL)
773 {
774 TIFFWarningExtR(tif, module,
775 "SamplesPerPixel tag value is changing, "
776 "but SMaxSampleValue tag was read with a "
777 "different value. Canceling it");
779 _TIFFfreeExt(tif, td->td_smaxsamplevalue);
780 td->td_smaxsamplevalue = NULL;
781 }
782 /* Test if 3 transfer functions instead of just one are now
783 needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820
784 */
785 if (td->td_transferfunction[0] != NULL &&
786 (v - td->td_extrasamples > 1) &&
787 !(td->td_samplesperpixel - td->td_extrasamples > 1))
788 {
789 TIFFWarningExtR(tif, module,
790 "SamplesPerPixel tag value is changing, "
791 "but TransferFunction was read with a "
792 "different value. Canceling it");
794 }
795 }
796 td->td_samplesperpixel = (uint16_t)v;
797 break;
799 v32 = (uint32_t)va_arg(ap, uint32_t);
800 if (v32 == 0)
801 goto badvalue32;
802 td->td_rowsperstrip = v32;
804 {
805 td->td_tilelength = v32;
806 td->td_tilewidth = td->td_imagewidth;
807 }
808 break;
810 td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
811 break;
813 td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
814 break;
816 if (tif->tif_flags & TIFF_PERSAMPLE)
817 _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue,
818 va_arg(ap, double *),
819 td->td_samplesperpixel);
820 else
821 setDoubleArrayOneValue(tif, &td->td_sminsamplevalue,
822 va_arg(ap, double),
823 td->td_samplesperpixel);
824 break;
826 if (tif->tif_flags & TIFF_PERSAMPLE)
827 _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue,
828 va_arg(ap, double *),
829 td->td_samplesperpixel);
830 else
831 setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue,
832 va_arg(ap, double),
833 td->td_samplesperpixel);
834 break;
836 dblval = va_arg(ap, double);
837 if (isnan(dblval) || dblval < 0)
838 goto badvaluedouble;
839 td->td_xresolution = _TIFFClampDoubleToFloat(dblval);
840 break;
842 dblval = va_arg(ap, double);
843 if (isnan(dblval) || dblval < 0)
844 goto badvaluedouble;
845 td->td_yresolution = _TIFFClampDoubleToFloat(dblval);
846 break;
848 v = (uint16_t)va_arg(ap, uint16_vap);
850 goto badvalue;
851 td->td_planarconfig = (uint16_t)v;
852 break;
854 td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
855 break;
857 td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
858 break;
860 v = (uint16_t)va_arg(ap, uint16_vap);
861 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
862 goto badvalue;
863 td->td_resolutionunit = (uint16_t)v;
864 break;
866 td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap);
867 td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap);
868 break;
870 td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap);
871 td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap);
872 break;
873 case TIFFTAG_COLORMAP:
874 if (td->td_bitspersample >= 32)
875 {
876 v = td->td_bitspersample;
877 goto badvalue;
878 }
879 v32 = 1U << td->td_bitspersample;
880 if (!setColorMap(tif, ap, v32))
881 status = 0;
882 break;
884 if (!setExtraSamples(tif, ap, &v))
885 goto badvalue;
886 break;
887 case TIFFTAG_MATTEING:
888 td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0);
889 if (td->td_extrasamples)
890 {
891 uint16_t sv = EXTRASAMPLE_ASSOCALPHA;
892 _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1);
893 }
894 break;
896 v32 = (uint32_t)va_arg(ap, uint32_t);
897 if (v32 % 16)
898 {
899 if (tif->tif_mode != O_RDONLY)
900 goto badvalue32;
902 tif, tif->tif_name,
903 "Nonstandard tile width %" PRIu32 ", convert file", v32);
904 }
905 td->td_tilewidth = v32;
906 tif->tif_flags |= TIFF_ISTILED;
907 break;
909 v32 = (uint32_t)va_arg(ap, uint32_t);
910 if (v32 % 16)
911 {
912 if (tif->tif_mode != O_RDONLY)
913 goto badvalue32;
915 tif, tif->tif_name,
916 "Nonstandard tile length %" PRIu32 ", convert file", v32);
917 }
918 td->td_tilelength = v32;
919 tif->tif_flags |= TIFF_ISTILED;
920 break;
922 v32 = (uint32_t)va_arg(ap, uint32_t);
923 if (v32 == 0)
924 goto badvalue32;
925 td->td_tiledepth = v32;
926 break;
927 case TIFFTAG_DATATYPE:
928 v = (uint16_t)va_arg(ap, uint16_vap);
929 switch (v)
930 {
931 case DATATYPE_VOID:
933 break;
934 case DATATYPE_INT:
936 break;
937 case DATATYPE_UINT:
939 break;
940 case DATATYPE_IEEEFP:
942 break;
943 default:
944 goto badvalue;
945 }
946 td->td_sampleformat = (uint16_t)v;
947 break;
949 v = (uint16_t)va_arg(ap, uint16_vap);
951 goto badvalue;
952 td->td_sampleformat = (uint16_t)v;
953
954 /* Try to fix up the SWAB function for complex data. */
955 if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT &&
956 td->td_bitspersample == 32 &&
959 else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT ||
960 td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) &&
961 td->td_bitspersample == 64 &&
964 break;
966 td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t);
967 break;
968 case TIFFTAG_SUBIFD:
969 if ((tif->tif_flags & TIFF_INSUBIFD) == 0)
970 {
971 td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap);
972 _TIFFsetLong8Array(tif, &td->td_subifd,
973 (uint64_t *)va_arg(ap, uint64_t *),
974 (uint32_t)td->td_nsubifd);
975 }
976 else
977 {
978 TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs",
979 tif->tif_name);
980 status = 0;
981 }
982 break;
984 td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap);
985 break;
987 td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap);
988 td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap);
989 break;
991 if (td->td_bitspersample >= 32)
992 {
993 v = td->td_bitspersample;
994 goto badvalue;
995 }
996 if (!setTransferFunction(tif, ap, 1U << td->td_bitspersample))
997 status = 0;
998 break;
1000 /* XXX should check for null range */
1001 _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite,
1002 va_arg(ap, float *), 6);
1003 break;
1004 case TIFFTAG_INKNAMES:
1005 {
1006 v = (uint16_t)va_arg(ap, uint16_vap);
1007 s = va_arg(ap, char *);
1008 uint16_t ninksinstring;
1009 ninksinstring = countInkNamesString(tif, v, s);
1010 status = ninksinstring > 0;
1011 if (ninksinstring > 0)
1012 {
1013 _TIFFsetNString(tif, &td->td_inknames, s, v);
1014 td->td_inknameslen = (int)v;
1015 /* Set NumberOfInks to the value ninksinstring */
1017 {
1018 if (td->td_numberofinks != ninksinstring)
1019 {
1021 tif, module,
1022 "Warning %s; Tag %s:\n Value %" PRIu16
1023 " of NumberOfInks is different from the number of "
1024 "inks %" PRIu16
1025 ".\n -> NumberOfInks value adapted to %" PRIu16 "",
1026 tif->tif_name, fip->field_name, td->td_numberofinks,
1027 ninksinstring, ninksinstring);
1028 td->td_numberofinks = ninksinstring;
1029 }
1030 }
1031 else
1032 {
1033 td->td_numberofinks = ninksinstring;
1035 }
1037 {
1038 if (td->td_numberofinks != td->td_samplesperpixel)
1039 {
1040 TIFFErrorExtR(tif, module,
1041 "Warning %s; Tag %s:\n Value %" PRIu16
1042 " of NumberOfInks is different from the "
1043 "SamplesPerPixel value %" PRIu16 "",
1044 tif->tif_name, fip->field_name,
1045 td->td_numberofinks,
1046 td->td_samplesperpixel);
1047 }
1048 }
1049 }
1050 }
1051 break;
1053 v = (uint16_t)va_arg(ap, uint16_vap);
1054 /* If InkNames already set also NumberOfInks is set accordingly and
1055 * should be equal */
1056 if (TIFFFieldSet(tif, FIELD_INKNAMES))
1057 {
1058 if (v != td->td_numberofinks)
1059 {
1061 tif, module,
1062 "Error %s; Tag %s:\n It is not possible to set the "
1063 "value %" PRIu32
1064 " for NumberOfInks\n which is different from the "
1065 "number of inks in the InkNames tag (%" PRIu16 ")",
1066 tif->tif_name, fip->field_name, v, td->td_numberofinks);
1067 /* Do not set / overwrite number of inks already set by
1068 * InkNames case accordingly. */
1069 status = 0;
1070 }
1071 }
1072 else
1073 {
1074 td->td_numberofinks = (uint16_t)v;
1076 {
1077 if (td->td_numberofinks != td->td_samplesperpixel)
1078 {
1079 TIFFErrorExtR(tif, module,
1080 "Warning %s; Tag %s:\n Value %" PRIu32
1081 " of NumberOfInks is different from the "
1082 "SamplesPerPixel value %" PRIu16 "",
1083 tif->tif_name, fip->field_name, v,
1084 td->td_samplesperpixel);
1085 }
1086 }
1087 }
1088 break;
1089 case TIFFTAG_PERSAMPLE:
1090 v = (uint16_t)va_arg(ap, uint16_vap);
1091 if (v == PERSAMPLE_MULTI)
1092 tif->tif_flags |= TIFF_PERSAMPLE;
1093 else
1094 tif->tif_flags &= ~TIFF_PERSAMPLE;
1095 break;
1096 default:
1097 {
1098 TIFFTagValue *tv;
1099 int tv_size, iCustom;
1100
1101 /*
1102 * This can happen if multiple images are open with different
1103 * codecs which have private tags. The global tag information
1104 * table may then have tags that are valid for one file but not
1105 * the other. If the client tries to set a tag that is not valid
1106 * for the image's codec then we'll arrive here. This
1107 * happens, for example, when tiffcp is used to convert between
1108 * compression schemes and codec-specific tags are blindly copied.
1109 *
1110 * This also happens when a FIELD_IGNORE tag is written.
1111 */
1112 if (fip->field_bit == FIELD_IGNORE)
1113 {
1115 tif, module,
1116 "%s: Ignored %stag \"%s\" (not supported by libtiff)",
1117 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
1118 fip->field_name);
1119 status = 0;
1120 break;
1121 }
1122 if (fip->field_bit != FIELD_CUSTOM)
1123 {
1125 tif, module,
1126 "%s: Invalid %stag \"%s\" (not supported by codec)",
1127 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
1128 fip->field_name);
1129 status = 0;
1130 break;
1131 }
1132
1133 /*
1134 * Find the existing entry for this custom value.
1135 */
1136 tv = NULL;
1137 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++)
1138 {
1139 if (td->td_customValues[iCustom].info->field_tag == tag)
1140 {
1141 tv = td->td_customValues + iCustom;
1142 old_custom_value = tv->value;
1143 tv->value = NULL;
1144 break;
1145 }
1146 }
1147
1148 /*
1149 * Grow the custom list if the entry was not found.
1150 */
1151 if (tv == NULL)
1152 {
1153 TIFFTagValue *new_customValues;
1154
1155 const tmsize_t new_custom_count =
1156 (tmsize_t)td->td_customValueCount + 1;
1157 const tmsize_t custom_values_size =
1159 new_custom_count, module);
1160 if (custom_values_size == 0)
1161 {
1162 status = 0;
1163 goto end;
1164 }
1165 new_customValues = (TIFFTagValue *)_TIFFreallocExt(
1166 tif, td->td_customValues, custom_values_size);
1167 if (!new_customValues)
1168 {
1169 TIFFErrorExtR(tif, module,
1170 "%s: Failed to allocate space for list of "
1171 "custom values",
1172 tif->tif_name);
1173 status = 0;
1174 goto end;
1175 }
1176
1177 td->td_customValueCount++;
1178 td->td_customValues = new_customValues;
1179
1180 tv = td->td_customValues + (td->td_customValueCount - 1);
1181 tv->info = fip;
1182 tv->value = NULL;
1183 tv->count = 0;
1184 }
1185
1186 /*
1187 * Set custom value ... save a copy of the custom tag value.
1188 */
1189 /*--: Rational2Double: For Rationals evaluate "set_get_field_type"
1190 * to determine internal storage size. */
1191 tv_size = TIFFFieldSetGetSize(fip);
1192 if (tv_size == 0)
1193 {
1194 status = 0;
1195 TIFFErrorExtR(tif, module, "%s: Bad field type %u for \"%s\"",
1196 tif->tif_name, fip->field_type, fip->field_name);
1197 removeCustomValue(td, iCustom);
1198 goto end;
1199 }
1200
1201 if (fip->field_type == TIFF_ASCII)
1202 {
1203 uint32_t ma;
1204 const char *mb;
1205 if (fip->field_passcount)
1206 {
1207 assert(fip->field_writecount == TIFF_VARIABLE2);
1208 ma = (uint32_t)va_arg(ap, uint32_t);
1209 mb = (const char *)va_arg(ap, const char *);
1210 }
1211 else
1212 {
1213 mb = (const char *)va_arg(ap, const char *);
1214 size_t len = strlen(mb) + 1;
1215 if (len >= 0x80000000U)
1216 {
1217 status = 0;
1218 TIFFErrorExtR(tif, module,
1219 "%s: Too long string value for \"%s\". "
1220 "Maximum supported is 2147483647 bytes",
1221 tif->tif_name, fip->field_name);
1222 removeCustomValue(td, iCustom);
1223 goto end;
1224 }
1225 ma = (uint32_t)len;
1226 }
1227 tv->count = (int)ma;
1228 setByteArray(tif, &tv->value, mb, ma, 1);
1229 if (!tv->value && ma != 0)
1230 {
1231 removeCustomValue(td, iCustom);
1232 status = 0;
1233 goto end;
1234 }
1235 }
1236 else
1237 {
1238 if (fip->field_passcount)
1239 {
1240 if (fip->field_writecount == TIFF_VARIABLE2)
1241 tv->count = (int)va_arg(ap, uint32_t);
1242 else
1243 tv->count = va_arg(ap, int);
1244 }
1245 else if (fip->field_writecount == TIFF_VARIABLE ||
1247 tv->count = 1;
1248 else if (fip->field_writecount == TIFF_SPP)
1249 tv->count = td->td_samplesperpixel;
1250 else
1251 tv->count = fip->field_writecount;
1252
1253 if (tv->count == 0)
1254 {
1255 TIFFWarningExtR(tif, module,
1256 "%s: Null count for \"%s\" (type "
1257 "%u, writecount %d, passcount %d)",
1258 tif->tif_name, fip->field_name,
1259 fip->field_type, fip->field_writecount,
1260 fip->field_passcount);
1261 break;
1262 }
1263
1264 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
1265 "custom tag binary object");
1266 if (!tv->value)
1267 {
1268 removeCustomValue(td, iCustom);
1269 status = 0;
1270 goto end;
1271 }
1272
1273 if (fip->field_tag == TIFFTAG_DOTRANGE &&
1274 strcmp(fip->field_name, "DotRange") == 0)
1275 {
1276 /* TODO: This is an evil exception and should not have been
1277 handled this way ... likely best if we move it into
1278 the directory structure with an explicit field in
1279 libtiff 4.1 and assign it a FIELD_ value */
1280 uint16_t v2[2];
1281 v2[0] = (uint16_t)va_arg(ap, int);
1282 v2[1] = (uint16_t)va_arg(ap, int);
1283 _TIFFmemcpy(tv->value, &v2, 4);
1284 }
1285
1286 else if (fip->field_passcount ||
1289 fip->field_writecount == TIFF_SPP || tv->count > 1)
1290 {
1291 /*--: Rational2Double: For Rationals tv_size is set above to
1292 * 4 or 8 according to fip->set_get_field_type! */
1293 /* The size must be computed in tmsize_t: tv->count and
1294 * tv_size are int, and their int product can overflow
1295 * (and wrap to a bogus memcpy size) for counts that
1296 * _TIFFCheckMalloc() above accepted, since it allows
1297 * sizes up to TIFF_TMSIZE_T_MAX. */
1298 _TIFFmemcpy(tv->value, va_arg(ap, void *),
1299 (tmsize_t)tv->count * (tmsize_t)tv_size);
1300 /* Test here for too big values for LONG8, IFD8, SLONG8 in
1301 * ClassicTIFF and delete custom field from custom list */
1302 if (!(tif->tif_flags & TIFF_BIGTIFF))
1303 {
1304 if (tv->info->field_type == TIFF_LONG8 ||
1305 tv->info->field_type == TIFF_IFD8)
1306 {
1307 uint64_t *pui64 = (uint64_t *)tv->value;
1308 for (int i = 0; i < tv->count; i++)
1309 {
1310 if (pui64[i] > 0xffffffffu)
1311 {
1313 tif, module,
1314 "%s: Bad %s value %" PRIu64
1315 " at %d. array position for \"%s\" tag "
1316 "%u in ClassicTIFF. Tag won't be "
1317 "written to file",
1318 tif->tif_name,
1319 (tv->info->field_type == TIFF_LONG8
1320 ? "LONG8"
1321 : "IFD8"),
1322 pui64[i], i, fip->field_name, tag);
1323 goto badvalueifd8long8;
1324 }
1325 }
1326 }
1327 else if (tv->info->field_type == TIFF_SLONG8)
1328 {
1329 int64_t *pi64 = (int64_t *)tv->value;
1330 for (int i = 0; i < tv->count; i++)
1331 {
1332 if (pi64[i] > 2147483647 ||
1333 pi64[i] < (-2147483647 - 1))
1334 {
1336 tif, module,
1337 "%s: Bad SLONG8 value %" PRIi64
1338 " at %d. array position for \"%s\" tag "
1339 "%u in ClassicTIFF. Tag won't be "
1340 "written to file",
1341 tif->tif_name, pi64[i], i,
1342 fip->field_name, tag);
1343 goto badvalueifd8long8;
1344 }
1345 }
1346 }
1347 }
1348 }
1349 else
1350 {
1351 char *val = (char *)tv->value;
1352 assert(tv->count == 1);
1353
1354 switch (fip->field_type)
1355 {
1356 case TIFF_BYTE:
1357 case TIFF_UNDEFINED:
1358 {
1359 uint8_t v2 = (uint8_t)va_arg(ap, int);
1360 _TIFFmemcpy(val, &v2, tv_size);
1361 }
1362 break;
1363 case TIFF_SBYTE:
1364 {
1365 int8_t v2 = (int8_t)va_arg(ap, int);
1366 _TIFFmemcpy(val, &v2, tv_size);
1367 }
1368 break;
1369 case TIFF_SHORT:
1370 {
1371 uint16_t v2 = (uint16_t)va_arg(ap, int);
1372 _TIFFmemcpy(val, &v2, tv_size);
1373 }
1374 break;
1375 case TIFF_SSHORT:
1376 {
1377 int16_t v2 = (int16_t)va_arg(ap, int);
1378 _TIFFmemcpy(val, &v2, tv_size);
1379 }
1380 break;
1381 case TIFF_LONG:
1382 case TIFF_IFD:
1383 {
1384 uint32_t v2 = va_arg(ap, uint32_t);
1385 _TIFFmemcpy(val, &v2, tv_size);
1386 }
1387 break;
1388 case TIFF_SLONG:
1389 {
1390 int32_t v2 = va_arg(ap, int32_t);
1391 _TIFFmemcpy(val, &v2, tv_size);
1392 }
1393 break;
1394 case TIFF_LONG8:
1395 case TIFF_IFD8:
1396 {
1397 uint64_t v2 = va_arg(ap, uint64_t);
1398 _TIFFmemcpy(val, &v2, tv_size);
1399 /* Test here for too big values for ClassicTIFF and
1400 * delete custom field from custom list */
1401 if (!(tif->tif_flags & TIFF_BIGTIFF) &&
1402 (v2 > 0xffffffffu))
1403 {
1405 tif, module,
1406 "%s: Bad LONG8 or IFD8 value %" PRIu64
1407 " for \"%s\" tag %u in ClassicTIFF. Tag "
1408 "won't be written to file",
1409 tif->tif_name, v2, fip->field_name, tag);
1410 goto badvalueifd8long8;
1411 }
1412 }
1413 break;
1414 case TIFF_SLONG8:
1415 {
1416 int64_t v2 = va_arg(ap, int64_t);
1417 _TIFFmemcpy(val, &v2, tv_size);
1418 /* Test here for too big values for ClassicTIFF and
1419 * delete custom field from custom list */
1420 if (!(tif->tif_flags & TIFF_BIGTIFF) &&
1421 ((v2 > 2147483647) || (v2 < (-2147483647 - 1))))
1422 {
1424 tif, module,
1425 "%s: Bad SLONG8 value %" PRIi64
1426 " for \"%s\" tag %u in ClassicTIFF. Tag "
1427 "won't be written to file",
1428 tif->tif_name, v2, fip->field_name, tag);
1429 goto badvalueifd8long8;
1430 }
1431 }
1432 break;
1433 case TIFF_RATIONAL:
1434 case TIFF_SRATIONAL:
1435 /*-- Rational2Double: For Rationals tv_size is set
1436 * above to 4 or 8 according to
1437 * fip->set_get_field_type!
1438 */
1439 {
1440 if (tv_size == 8)
1441 {
1442 double v2 = va_arg(ap, double);
1443 _TIFFmemcpy(val, &v2, tv_size);
1444 }
1445 else
1446 {
1447 /*-- default should be tv_size == 4 */
1448 float v3 = (float)va_arg(ap, double);
1449 _TIFFmemcpy(val, &v3, tv_size);
1450 /*-- ToDo: After Testing, this should be
1451 * removed and tv_size==4 should be set as
1452 * default. */
1453 if (tv_size != 4)
1454 {
1455 TIFFErrorExtR(tif, module,
1456 "Rational2Double: "
1457 ".set_get_field_type "
1458 "in not 4 but %d",
1459 tv_size);
1460 }
1461 }
1462 }
1463 break;
1464 case TIFF_FLOAT:
1465 {
1466 float v2 =
1467 _TIFFClampDoubleToFloat(va_arg(ap, double));
1468 _TIFFmemcpy(val, &v2, tv_size);
1469 }
1470 break;
1471 case TIFF_DOUBLE:
1472 {
1473 double v2 = va_arg(ap, double);
1474 _TIFFmemcpy(val, &v2, tv_size);
1475 }
1476 break;
1477 case TIFF_NOTYPE:
1478 case TIFF_ASCII:
1479 default:
1480 _TIFFmemset(val, 0, tv_size);
1481 status = 0;
1482 break;
1483 }
1484 }
1485 }
1486 if (old_custom_value)
1487 {
1488 _TIFFfreeExt(tif, old_custom_value);
1489 old_custom_value = NULL;
1490 }
1491 }
1492 }
1493 if (status)
1494 {
1495 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1496 if (fip2)
1497 TIFFSetFieldBit(tif, fip2->field_bit);
1499 }
1500
1501end:
1502 if (old_custom_value)
1503 _TIFFfreeExt(tif, old_custom_value);
1504 va_end(ap);
1505 return (status);
1506badvalue:
1507{
1508 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1509 TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
1510 tif->tif_name, v, fip2 ? fip2->field_name : "Unknown");
1511 va_end(ap);
1512}
1513 return (0);
1514badvalue32:
1515{
1516 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1517 TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
1518 tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown");
1519 va_end(ap);
1520}
1521 return (0);
1522badvaluedouble:
1523{
1524 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1525 TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name,
1526 dblval, fip2 ? fip2->field_name : "Unknown");
1527 va_end(ap);
1528}
1529 return (0);
1530badvalueifd8long8:
1531{
1532 /* Error message issued already above. */
1533 TIFFTagValue *tv2 = NULL;
1534 int iCustom2, iC2;
1535 /* Find the existing entry for this custom value. */
1536 for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++)
1537 {
1538 if (td->td_customValues[iCustom2].info->field_tag == tag)
1539 {
1540 tv2 = td->td_customValues + (iCustom2);
1541 break;
1542 }
1543 }
1544 if (tv2 != NULL)
1545 {
1546 /* Remove custom field from custom list */
1547 if (tv2->value != NULL)
1548 {
1549 _TIFFfreeExt(tif, tv2->value);
1550 tv2->value = NULL;
1551 }
1552 /* Shorten list and close gap in customValues list.
1553 * Re-allocation of td_customValues not necessary here. */
1554 td->td_customValueCount--;
1555 for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++)
1556 {
1557 td->td_customValues[iC2] = td->td_customValues[iC2 + 1];
1558 }
1559 }
1560 else
1561 {
1562 assert(0);
1563 }
1564 if (old_custom_value)
1565 _TIFFfreeExt(tif, old_custom_value);
1566 va_end(ap);
1567}
1568 return (0);
1569} /*-- _TIFFVSetField() --*/
1570
1571/*
1572 * Return 1/0 according to whether or not
1573 * it is permissible to set the tag's value.
1574 * Note that we allow ImageLength to be changed
1575 * so that we can append and extend to images.
1576 * Any other tag may not be altered once writing
1577 * has commenced, unless its value has no effect
1578 * on the format of the data that is written.
1579 */
1594static int OkToChangeTag(TIFF *tif, uint32_t tag)
1595{
1596 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
1597 if (!fip)
1598 { /* unknown tag */
1599 TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32,
1600 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
1601 return (0);
1602 }
1603 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
1604 !fip->field_oktochange)
1605 {
1606 /*
1607 * Consult info table to see if tag can be changed
1608 * after we've started writing. We only allow changes
1609 * to those tags that don't/shouldn't affect the
1610 * compression and/or format of the data.
1611 */
1612 TIFFErrorExtR(tif, "TIFFSetField",
1613 "%s: Cannot modify tag \"%s\" while writing",
1614 tif->tif_name, fip->field_name);
1615 return (0);
1616 }
1617 return (1);
1618}
1619
1620/*
1621 * Record the value of a field in the
1622 * internal directory structure. The
1623 * field will be written to the file
1624 * when/if the directory structure is
1625 * updated.
1626 */
1627int TIFFSetField(TIFF *tif, uint32_t tag, ...)
1628{
1629 va_list ap;
1630 int status;
1631
1632 va_start(ap, tag);
1633 status = TIFFVSetField(tif, tag, ap);
1634 va_end(ap);
1635 return (status);
1636}
1637
1638/*
1639 * Clear the contents of the field in the internal structure.
1640 */
1641int TIFFUnsetField(TIFF *tif, uint32_t tag)
1642{
1643 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
1644 TIFFDirectory *td = &tif->tif_dir;
1645
1646 if (!fip)
1647 return 0;
1648
1649 if (fip->field_bit != FIELD_CUSTOM)
1650 TIFFClrFieldBit(tif, fip->field_bit);
1651 else
1652 {
1653 TIFFTagValue *tv = NULL;
1654 int i;
1655
1656 for (i = 0; i < td->td_customValueCount; i++)
1657 {
1658
1659 tv = td->td_customValues + i;
1660 if (tv->info->field_tag == tag)
1661 break;
1662 }
1663
1664 if (i < td->td_customValueCount)
1665 {
1666 _TIFFfreeExt(tif, tv->value);
1667 for (; i < td->td_customValueCount - 1; i++)
1668 {
1669 td->td_customValues[i] = td->td_customValues[i + 1];
1670 }
1671 td->td_customValueCount--;
1672 }
1673 }
1674
1676
1677 return (1);
1678}
1679
1680/*
1681 * Like TIFFSetField, but taking a varargs
1682 * parameter list. This routine is useful
1683 * for building higher-level interfaces on
1684 * top of the library.
1685 */
1686int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
1687{
1688 return OkToChangeTag(tif, tag)
1689 ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap)
1690 : 0;
1691}
1692
1720static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
1721{
1722 TIFFDirectory *td = &tif->tif_dir;
1723 int ret_val = 1;
1724 uint32_t standard_tag = tag;
1725 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
1726 if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */
1727 return 0;
1728
1729 /*
1730 * We want to force the custom code to be used for custom
1731 * fields even if the tag happens to match a well known
1732 * one - important for reinterpreted handling of standard
1733 * tag values in custom directories (i.e. EXIF)
1734 */
1735 if (fip->field_bit == FIELD_CUSTOM)
1736 {
1737 standard_tag = 0;
1738 }
1739
1740 switch (standard_tag)
1741 {
1743 *va_arg(ap, uint32_t *) = td->td_subfiletype;
1744 break;
1745 case TIFFTAG_IMAGEWIDTH:
1746 *va_arg(ap, uint32_t *) = td->td_imagewidth;
1747 break;
1749 *va_arg(ap, uint32_t *) = td->td_imagelength;
1750 break;
1752 *va_arg(ap, uint16_t *) = td->td_bitspersample;
1753 break;
1755 *va_arg(ap, uint16_t *) = td->td_compression;
1756 break;
1758 *va_arg(ap, uint16_t *) = td->td_photometric;
1759 break;
1761 *va_arg(ap, uint16_t *) = td->td_threshholding;
1762 break;
1763 case TIFFTAG_FILLORDER:
1764 *va_arg(ap, uint16_t *) = td->td_fillorder;
1765 break;
1767 *va_arg(ap, uint16_t *) = td->td_orientation;
1768 break;
1770 *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
1771 break;
1773 *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
1774 break;
1776 *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
1777 break;
1779 *va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
1780 break;
1782 if (tif->tif_flags & TIFF_PERSAMPLE)
1783 *va_arg(ap, double **) = td->td_sminsamplevalue;
1784 else
1785 {
1786 /* libtiff historically treats this as a single value. */
1787 uint16_t i;
1788 double v = td->td_sminsamplevalue[0];
1789 for (i = 1; i < td->td_samplesperpixel; ++i)
1790 if (td->td_sminsamplevalue[i] < v)
1791 v = td->td_sminsamplevalue[i];
1792 *va_arg(ap, double *) = v;
1793 }
1794 break;
1796 if (tif->tif_flags & TIFF_PERSAMPLE)
1797 *va_arg(ap, double **) = td->td_smaxsamplevalue;
1798 else
1799 {
1800 /* libtiff historically treats this as a single value. */
1801 uint16_t i;
1802 double v = td->td_smaxsamplevalue[0];
1803 for (i = 1; i < td->td_samplesperpixel; ++i)
1804 if (td->td_smaxsamplevalue[i] > v)
1805 v = td->td_smaxsamplevalue[i];
1806 *va_arg(ap, double *) = v;
1807 }
1808 break;
1810 *va_arg(ap, float *) = td->td_xresolution;
1811 break;
1813 *va_arg(ap, float *) = td->td_yresolution;
1814 break;
1816 *va_arg(ap, uint16_t *) = td->td_planarconfig;
1817 break;
1818 case TIFFTAG_XPOSITION:
1819 *va_arg(ap, float *) = td->td_xposition;
1820 break;
1821 case TIFFTAG_YPOSITION:
1822 *va_arg(ap, float *) = td->td_yposition;
1823 break;
1825 *va_arg(ap, uint16_t *) = td->td_resolutionunit;
1826 break;
1827 case TIFFTAG_PAGENUMBER:
1828 *va_arg(ap, uint16_t *) = td->td_pagenumber[0];
1829 *va_arg(ap, uint16_t *) = td->td_pagenumber[1];
1830 break;
1832 *va_arg(ap, uint16_t *) = td->td_halftonehints[0];
1833 *va_arg(ap, uint16_t *) = td->td_halftonehints[1];
1834 break;
1835 case TIFFTAG_COLORMAP:
1836 *va_arg(ap, const uint16_t **) = td->td_colormap[0];
1837 *va_arg(ap, const uint16_t **) = td->td_colormap[1];
1838 *va_arg(ap, const uint16_t **) = td->td_colormap[2];
1839 break;
1842 _TIFFFillStriles(tif);
1843 *va_arg(ap, const uint64_t **) = td->td_stripoffset_p;
1844 if (td->td_stripoffset_p == NULL)
1845 ret_val = 0;
1846 break;
1849 _TIFFFillStriles(tif);
1850 *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p;
1851 if (td->td_stripbytecount_p == NULL)
1852 ret_val = 0;
1853 break;
1854 case TIFFTAG_MATTEING:
1855 *va_arg(ap, uint16_t *) =
1856 (td->td_extrasamples == 1 && td->td_sampleinfo &&
1858 break;
1860 *va_arg(ap, uint16_t *) = td->td_extrasamples;
1861 *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
1862 break;
1863 case TIFFTAG_TILEWIDTH:
1864 *va_arg(ap, uint32_t *) = td->td_tilewidth;
1865 break;
1866 case TIFFTAG_TILELENGTH:
1867 *va_arg(ap, uint32_t *) = td->td_tilelength;
1868 break;
1869 case TIFFTAG_TILEDEPTH:
1870 *va_arg(ap, uint32_t *) = td->td_tiledepth;
1871 break;
1872 case TIFFTAG_DATATYPE:
1873 switch (td->td_sampleformat)
1874 {
1875 case SAMPLEFORMAT_UINT:
1876 *va_arg(ap, uint16_t *) = DATATYPE_UINT;
1877 break;
1878 case SAMPLEFORMAT_INT:
1879 *va_arg(ap, uint16_t *) = DATATYPE_INT;
1880 break;
1882 *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP;
1883 break;
1884 case SAMPLEFORMAT_VOID:
1885 *va_arg(ap, uint16_t *) = DATATYPE_VOID;
1886 break;
1887 default:
1888 break;
1889 }
1890 break;
1892 *va_arg(ap, uint16_t *) = td->td_sampleformat;
1893 break;
1894 case TIFFTAG_IMAGEDEPTH:
1895 *va_arg(ap, uint32_t *) = td->td_imagedepth;
1896 break;
1897 case TIFFTAG_SUBIFD:
1898 *va_arg(ap, uint16_t *) = td->td_nsubifd;
1899 *va_arg(ap, const uint64_t **) = td->td_subifd;
1900 break;
1902 *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
1903 break;
1905 *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
1906 *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
1907 break;
1909 *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
1910 if (td->td_samplesperpixel - td->td_extrasamples > 1)
1911 {
1912 *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
1913 *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
1914 }
1915 else
1916 {
1917 *va_arg(ap, const uint16_t **) = NULL;
1918 *va_arg(ap, const uint16_t **) = NULL;
1919 }
1920 break;
1922 *va_arg(ap, const float **) = td->td_refblackwhite;
1923 break;
1924 case TIFFTAG_INKNAMES:
1925 *va_arg(ap, const char **) = td->td_inknames;
1926 break;
1928 *va_arg(ap, uint16_t *) = td->td_numberofinks;
1929 break;
1930 default:
1931 {
1932 int i;
1933
1934 /*
1935 * This can happen if multiple images are open
1936 * with different codecs which have private
1937 * tags. The global tag information table may
1938 * then have tags that are valid for one file
1939 * but not the other. If the client tries to
1940 * get a tag that is not valid for the image's
1941 * codec then we'll arrive here.
1942 */
1943 if (fip->field_bit != FIELD_CUSTOM)
1944 {
1945 TIFFErrorExtR(tif, "_TIFFVGetField",
1946 "%s: Invalid %stag \"%s\" "
1947 "(not supported by codec)",
1948 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
1949 fip->field_name);
1950 ret_val = 0;
1951 break;
1952 }
1953
1954 /*
1955 * Do we have a custom value?
1956 */
1957 ret_val = 0;
1958 for (i = 0; i < td->td_customValueCount; i++)
1959 {
1960 TIFFTagValue *tv = td->td_customValues + i;
1961
1962 if (tv->info->field_tag != tag)
1963 continue;
1964
1965 /* Without a count, the caller cannot tell that there is no
1966 * value. */
1967 if (tv->value == NULL && !fip->field_passcount)
1968 break;
1969
1970 if (fip->field_passcount)
1971 {
1972 if (fip->field_readcount == TIFF_VARIABLE2)
1973 *va_arg(ap, uint32_t *) = (uint32_t)tv->count;
1974 else /* Assume TIFF_VARIABLE */
1975 *va_arg(ap, uint16_t *) = (uint16_t)tv->count;
1976 *va_arg(ap, const void **) = tv->value;
1977 ret_val = 1;
1978 }
1979 else if (fip->field_tag == TIFFTAG_DOTRANGE &&
1980 strcmp(fip->field_name, "DotRange") == 0)
1981 {
1982 /* TODO: This is an evil exception and should not have been
1983 handled this way ... likely best if we move it into
1984 the directory structure with an explicit field in
1985 libtiff 4.1 and assign it a FIELD_ value */
1986 *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0];
1987 *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1];
1988 ret_val = 1;
1989 }
1990 else
1991 {
1992 if (fip->field_type == TIFF_ASCII ||
1995 fip->field_readcount == TIFF_SPP || tv->count > 1)
1996 {
1997 *va_arg(ap, void **) = tv->value;
1998 ret_val = 1;
1999 }
2000 else
2001 {
2002 char *val = (char *)tv->value;
2003 assert(tv->count == 1);
2004 switch (fip->field_type)
2005 {
2006 case TIFF_BYTE:
2007 case TIFF_UNDEFINED:
2008 *va_arg(ap, uint8_t *) = *(uint8_t *)val;
2009 ret_val = 1;
2010 break;
2011 case TIFF_SBYTE:
2012 *va_arg(ap, int8_t *) = *(int8_t *)val;
2013 ret_val = 1;
2014 break;
2015 case TIFF_SHORT:
2016 *va_arg(ap, uint16_t *) = *(uint16_t *)val;
2017 ret_val = 1;
2018 break;
2019 case TIFF_SSHORT:
2020 *va_arg(ap, int16_t *) = *(int16_t *)val;
2021 ret_val = 1;
2022 break;
2023 case TIFF_LONG:
2024 case TIFF_IFD:
2025 *va_arg(ap, uint32_t *) = *(uint32_t *)val;
2026 ret_val = 1;
2027 break;
2028 case TIFF_SLONG:
2029 *va_arg(ap, int32_t *) = *(int32_t *)val;
2030 ret_val = 1;
2031 break;
2032 case TIFF_LONG8:
2033 case TIFF_IFD8:
2034 *va_arg(ap, uint64_t *) = *(uint64_t *)val;
2035 ret_val = 1;
2036 break;
2037 case TIFF_SLONG8:
2038 *va_arg(ap, int64_t *) = *(int64_t *)val;
2039 ret_val = 1;
2040 break;
2041 case TIFF_RATIONAL:
2042 case TIFF_SRATIONAL:
2043 {
2044 /*-- Rational2Double: For Rationals evaluate
2045 * "set_get_field_type" to determine internal
2046 * storage size and return value size. */
2047 int tv_size = TIFFFieldSetGetSize(fip);
2048 if (tv_size == 8)
2049 {
2050 *va_arg(ap, double *) = *(double *)val;
2051 ret_val = 1;
2052 }
2053 else
2054 {
2055 /*-- default should be tv_size == 4 */
2056 *va_arg(ap, float *) = *(float *)val;
2057 ret_val = 1;
2058 /*-- ToDo: After Testing, this should be
2059 * removed and tv_size==4 should be set as
2060 * default. */
2061 if (tv_size != 4)
2062 {
2063 TIFFErrorExtR(tif, "_TIFFVGetField",
2064 "Rational2Double: "
2065 ".set_get_field_type "
2066 "in not 4 but %d",
2067 tv_size);
2068 }
2069 }
2070 }
2071 break;
2072 case TIFF_FLOAT:
2073 *va_arg(ap, float *) = *(float *)val;
2074 ret_val = 1;
2075 break;
2076 case TIFF_DOUBLE:
2077 *va_arg(ap, double *) = *(double *)val;
2078 ret_val = 1;
2079 break;
2080 case TIFF_NOTYPE:
2081 case TIFF_ASCII:
2082 default:
2083 ret_val = 0;
2084 break;
2085 }
2086 }
2087 }
2088 break;
2089 }
2090 }
2091 }
2092 return (ret_val);
2093}
2094
2095/*
2096 * Return the value of a field in the
2097 * internal directory structure.
2098 */
2099int TIFFGetField(TIFF *tif, uint32_t tag, ...)
2100{
2101 int status;
2102 va_list ap;
2103
2104 va_start(ap, tag);
2105 status = TIFFVGetField(tif, tag, ap);
2106 va_end(ap);
2107 return (status);
2108}
2109
2110/*
2111 * Like TIFFGetField, but taking a varargs
2112 * parameter list. This routine is useful
2113 * for building higher-level interfaces on
2114 * top of the library.
2115 */
2116int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
2117{
2118 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
2119 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit))
2120 ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap)
2121 : 0);
2122}
2123
2138#define CleanupField(member) \
2139 { \
2140 if (td->member) \
2141 { \
2142 _TIFFfreeExt(tif, td->member); \
2143 td->member = 0; \
2144 } \
2145 }
2146
2147/*
2148 * Reset tif->tif_dir structure to zero and
2149 * initialize some IFD strile counter and index parameters.
2150 */
2152{
2153 _TIFFmemset(td, 0, sizeof(*td));
2154 td->td_curstrip = NOSTRIP; /* invalid strip = NOSTRIP */
2155 td->td_row = (uint32_t)-1; /* read/write pre-increment */
2156 td->td_col = (uint32_t)-1; /* read/write pre-increment */
2157 td->td_scanlinesize = 0; /* initialize to zero */
2158 td->td_curtile = NOTILE; /* invalid tile = NOTILE */
2159 td->td_tilesize = (tmsize_t)-1; /* invalidate tilezize */
2160}
2161
2162/*
2163 * Release storage associated with a directory.
2164 */
2166{
2167 TIFFDirectory *td = &tif->tif_dir;
2168 int i;
2169
2170 (*tif->tif_cleanup)(tif);
2171 _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset));
2172 CleanupField(td_sminsamplevalue);
2173 CleanupField(td_smaxsamplevalue);
2174 CleanupField(td_colormap[0]);
2175 CleanupField(td_colormap[1]);
2176 CleanupField(td_colormap[2]);
2177 td->td_colormap_count = 0;
2178 td->td_colormap_alloc = 0;
2179 CleanupField(td_sampleinfo);
2180 CleanupField(td_subifd);
2181 CleanupField(td_inknames);
2182 CleanupField(td_refblackwhite);
2183 CleanupField(td_transferfunction[0]);
2184 CleanupField(td_transferfunction[1]);
2185 CleanupField(td_transferfunction[2]);
2187 CleanupField(td_stripoffset_p);
2188 CleanupField(td_stripbytecount_p);
2192
2193 /* Cleanup custom tag values */
2194 for (i = 0; i < td->td_customValueCount; i++)
2195 {
2196 if (td->td_customValues[i].value)
2197 _TIFFfreeExt(tif, td->td_customValues[i].value);
2198 }
2199
2200 td->td_customValueCount = 0;
2201 CleanupField(td_customValues);
2202
2203 _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
2205
2206 /* Reset some internal parameters for IFD data size checking. */
2209 if (tif->tif_dir.td_dirdatasize_offsets != NULL)
2210 {
2212 tif->tif_dir.td_dirdatasize_offsets = NULL;
2214 }
2216 /* Note: tif->tif_dir structure is set to zero in TIFFDefaultDirectory() */
2217}
2218#undef CleanupField
2219
2220/*
2221 * Client Tag extension support (from Niles Ritter).
2222 */
2231
2233{
2235 _TIFFextender = extender;
2236 return (prev);
2237}
2238
2239/*
2240 * Setup for a new directory. Should we automatically call
2241 * TIFFWriteDirectory() if the current one is dirty?
2242 *
2243 * The newly created directory will not exist on the file till
2244 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
2245 */
2247{
2248 /* Free previously allocated memory and setup default values. */
2249 TIFFFreeDirectory(tif);
2251 tif->tif_diroff = 0;
2252 tif->tif_nextdiroff = 0;
2253 tif->tif_curoff = 0;
2255 return 0;
2256}
2257
2259{
2260 int retcode = 0;
2261
2262 /* Free previously allocated memory and setup default values. */
2263 TIFFFreeDirectory(tif);
2265
2266 /*
2267 * Reset the field definitions to match the application provided list.
2268 * Hopefully TIFFDefaultDirectory() won't have done anything irreversible
2269 * based on it's assumption this is an image directory.
2270 *
2271 * Check for valid infoarray and return default directory on error.
2272 */
2273 if (infoarray)
2274 {
2275 _TIFFSetupFields(tif, infoarray);
2276 }
2277 else
2278 {
2279 TIFFErrorExtR(tif, "TIFFCreateCustomDirectory",
2280 "Invalid TIFFFieldArray provided (NULL pointer)");
2281 retcode = 1;
2282 }
2283
2284 tif->tif_diroff = 0;
2285 tif->tif_nextdiroff = 0;
2286 tif->tif_curoff = 0;
2287 /* invalidate directory index */
2289 /* invalidate IFD loop lists */
2291 /* To be able to return from SubIFD or custom-IFD to main-IFD */
2293 return retcode;
2294}
2295
2302
2303/*
2304 * Creates the EXIF GPS custom directory
2305 */
2312
2313/*
2314 * Setup a default directory structure.
2315 */
2317{
2318 TIFFDirectory *td = &tif->tif_dir;
2320
2323 /* Reset tif->tif_dir structure to zero and
2324 * initialize some IFD strile counter and index parameters. */
2327 td->td_bitspersample = 1;
2330 td->td_samplesperpixel = 1;
2331 td->td_rowsperstrip = (uint32_t)-1;
2332 td->td_tilewidth = 0;
2333 td->td_tilelength = 0;
2334 td->td_tiledepth = 1;
2335#ifdef STRIPBYTECOUNTSORTED_UNUSED
2336 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
2337#endif
2340 td->td_imagedepth = 1;
2341 td->td_ycbcrsubsampling[0] = 2;
2342 td->td_ycbcrsubsampling[1] = 2;
2345 tif->tif_foundfield = NULL;
2348 tif->tif_tagmethods.printdir = NULL;
2349 /* additional default values */
2352 td->td_subfiletype = 0;
2353 td->td_minsamplevalue = 0;
2354 /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
2355 * Therefore, td_maxsamplevalue has to be re-calculated in
2356 * TIFFGetFieldDefaulted(). */
2357 td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */
2358 td->td_extrasamples = 0;
2359 td->td_sampleinfo = NULL;
2360
2361 /*
2362 * Give client code a chance to install their own
2363 * tag extensions & methods, prior to compression overloads,
2364 * but do some prior cleanup first.
2365 * (http://trac.osgeo.org/gdal/ticket/5054)
2366 */
2367 if (tif->tif_nfieldscompat > 0)
2368 {
2369 uint32_t i;
2370
2371 for (i = 0; i < tif->tif_nfieldscompat; i++)
2372 {
2373 if (tif->tif_fieldscompat[i].allocated_size)
2374 _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
2375 }
2376 _TIFFfreeExt(tif, tif->tif_fieldscompat);
2377 tif->tif_nfieldscompat = 0;
2378 tif->tif_fieldscompat = NULL;
2379 }
2380 if (_TIFFextender)
2381 (*_TIFFextender)(tif);
2383 /*
2384 * NB: The directory is marked dirty as a result of setting
2385 * up the default compression scheme. However, this really
2386 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
2387 * if the user does something. We could just do the setup
2388 * by hand, but it seems better to use the normal mechanism
2389 * (i.e. TIFFSetField).
2390 */
2391 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
2392
2393 /*
2394 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
2395 * we clear the ISTILED flag when setting up a new directory.
2396 * Should we also be clearing stuff like INSUBIFD?
2397 */
2398 tif->tif_flags &= ~TIFF_ISTILED;
2399
2400 return (1);
2401}
2402
2428static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off,
2429 tdir_t *nextdirnum)
2430{
2431 static const char module[] = "TIFFAdvanceDirectory";
2432
2433 /* Add this directory to the directory list, if not already in. */
2434 if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
2435 {
2436 TIFFErrorExtR(tif, module,
2437 "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
2438 ") might cause an IFD loop",
2439 *nextdirnum, *nextdiroff, *nextdiroff);
2440 *nextdiroff = 0;
2441 *nextdirnum = 0;
2442 return (0);
2443 }
2444
2445 if (isMapped(tif))
2446 {
2447 uint64_t poff = *nextdiroff;
2448 if (!(tif->tif_flags & TIFF_BIGTIFF))
2449 {
2450 tmsize_t poffa, poffb, poffc, poffd;
2451 uint16_t dircount;
2452 uint32_t nextdir32;
2453 if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint16_t) ||
2454 poff > (uint64_t)tif->tif_size - sizeof(uint16_t))
2455 {
2456 TIFFErrorExtR(tif, module,
2457 "%s:%d: %s: Error fetching directory count",
2458 __FILE__, __LINE__, tif->tif_name);
2459 *nextdiroff = 0;
2460 return (0);
2461 }
2462 poffa = (tmsize_t)poff;
2463 poffb = poffa + (tmsize_t)sizeof(uint16_t);
2464 _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t));
2465 if (tif->tif_flags & TIFF_SWAB)
2466 TIFFSwabShort(&dircount);
2467 if (poffb >
2468 TIFF_TMSIZE_T_MAX - dircount * 12 - (tmsize_t)sizeof(uint32_t))
2469 {
2470 TIFFErrorExtR(tif, module, "Error fetching directory link");
2471 return (0);
2472 }
2473 poffc = poffb + dircount * 12;
2474 poffd = poffc + (tmsize_t)sizeof(uint32_t);
2475 if (poffd > tif->tif_size)
2476 {
2477 TIFFErrorExtR(tif, module, "Error fetching directory link");
2478 return (0);
2479 }
2480 if (off != NULL)
2481 *off = (uint64_t)poffc;
2482 _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t));
2483 if (tif->tif_flags & TIFF_SWAB)
2484 TIFFSwabLong(&nextdir32);
2485 *nextdiroff = nextdir32;
2486 }
2487 else
2488 {
2489 tmsize_t poffa, poffb, poffc, poffd;
2490 uint64_t dircount64;
2491 if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t))
2492 {
2493 TIFFErrorExtR(tif, module,
2494 "%s:%d: %s: Error fetching directory count",
2495 __FILE__, __LINE__, tif->tif_name);
2496 return (0);
2497 }
2498 poffa = (tmsize_t)poff;
2499 poffb = poffa + (tmsize_t)sizeof(uint64_t);
2500 if (poffb > tif->tif_size)
2501 {
2502 TIFFErrorExtR(tif, module,
2503 "%s:%d: %s: Error fetching directory count",
2504 __FILE__, __LINE__, tif->tif_name);
2505 return (0);
2506 }
2507 _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t));
2508 if (tif->tif_flags & TIFF_SWAB)
2509 TIFFSwabLong8(&dircount64);
2510 if (dircount64 > 0xFFFF)
2511 {
2512 TIFFErrorExtR(tif, module,
2513 "Sanity check on directory count failed");
2514 return (0);
2515 }
2516 if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount64 * 20) -
2517 (tmsize_t)sizeof(uint64_t))
2518 {
2519 TIFFErrorExtR(tif, module, "Error fetching directory link");
2520 return (0);
2521 }
2522 poffc = poffb + (tmsize_t)(dircount64 * 20);
2523 poffd = poffc + (tmsize_t)sizeof(uint64_t);
2524 if (poffd > tif->tif_size)
2525 {
2526 TIFFErrorExtR(tif, module, "Error fetching directory link");
2527 return (0);
2528 }
2529 if (off != NULL)
2530 *off = (uint64_t)poffc;
2531 _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t));
2532 if (tif->tif_flags & TIFF_SWAB)
2533 TIFFSwabLong8(nextdiroff);
2534 }
2535 }
2536 else
2537 {
2538 if (!(tif->tif_flags & TIFF_BIGTIFF))
2539 {
2540 uint16_t dircount;
2541 uint32_t nextdir32;
2542 if (!SeekOK(tif, *nextdiroff) ||
2543 !ReadOK(tif, &dircount, sizeof(uint16_t)))
2544 {
2545 TIFFErrorExtR(tif, module,
2546 "%s:%d: %s: Error fetching directory count",
2547 __FILE__, __LINE__, tif->tif_name);
2548 return (0);
2549 }
2550 if (tif->tif_flags & TIFF_SWAB)
2551 TIFFSwabShort(&dircount);
2552 if (off != NULL)
2553 *off = TIFFSeekFile(tif, dircount * 12U, SEEK_CUR);
2554 else
2555 (void)TIFFSeekFile(tif, dircount * 12U, SEEK_CUR);
2556 if (!ReadOK(tif, &nextdir32, sizeof(uint32_t)))
2557 {
2558 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
2559 tif->tif_name);
2560 return (0);
2561 }
2562 if (tif->tif_flags & TIFF_SWAB)
2563 TIFFSwabLong(&nextdir32);
2564 *nextdiroff = nextdir32;
2565 }
2566 else
2567 {
2568 uint64_t dircount64;
2569 if (!SeekOK(tif, *nextdiroff) ||
2570 !ReadOK(tif, &dircount64, sizeof(uint64_t)))
2571 {
2572 TIFFErrorExtR(tif, module,
2573 "%s:%d: %s: Error fetching directory count",
2574 __FILE__, __LINE__, tif->tif_name);
2575 return (0);
2576 }
2577 if (tif->tif_flags & TIFF_SWAB)
2578 TIFFSwabLong8(&dircount64);
2579 if (dircount64 > 0xFFFF)
2580 {
2581 TIFFErrorExtR(tif, module,
2582 "%s:%d: %s: Error fetching directory count",
2583 __FILE__, __LINE__, tif->tif_name);
2584 return (0);
2585 }
2586 if (off != NULL)
2587 *off = TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR);
2588 else
2589 (void)TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR);
2590 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
2591 {
2592 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
2593 tif->tif_name);
2594 return (0);
2595 }
2596 if (tif->tif_flags & TIFF_SWAB)
2597 TIFFSwabLong8(nextdiroff);
2598 }
2599 }
2600 if (*nextdiroff != 0)
2601 {
2602 (*nextdirnum)++;
2603 /* Check next directory for IFD looping and if so, set it as last
2604 * directory. */
2605 if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
2606 {
2608 tif, module,
2609 "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64
2610 ") might be an IFD loop. Treating directory %d as "
2611 "last directory",
2612 *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1);
2613 *nextdiroff = 0;
2614 (*nextdirnum)--;
2615 }
2616 }
2617 return (1);
2618}
2619
2620/*
2621 * Count the number of directories in a file.
2622 */
2624{
2625 uint64_t nextdiroff;
2626 tdir_t nextdirnum;
2627 tdir_t n;
2628 if (!(tif->tif_flags & TIFF_BIGTIFF))
2629 nextdiroff = tif->tif_header.classic.tiff_diroff;
2630 else
2631 nextdiroff = tif->tif_header.big.tiff_diroff;
2632 nextdirnum = 0;
2633 n = 0;
2634 while (nextdiroff != 0 &&
2635 TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
2636 {
2637 ++n;
2638 }
2639 /* Update number of main-IFDs in file. */
2640 tif->tif_curdircount = n;
2641 return (n);
2642}
2643
2644/*
2645 * Set the n-th directory as the current directory.
2646 * NB: Directories are numbered starting at 0.
2647 */
2649{
2650 uint64_t nextdiroff;
2651 tdir_t nextdirnum = 0;
2652 tdir_t n;
2653
2655 {
2656 /* tif_setdirectory_force_absolute=1 will force parsing the main IFD
2657 * chain from the beginning, thus IFD directory list needs to be cleared
2658 * from possible SubIFD offsets.
2659 */
2660 _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
2661 }
2662
2663 /* Even faster path, if offset is available within IFD loop hash list. */
2665 _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff))
2666 {
2667 /* Set parameters for following TIFFReadDirectory() below. */
2668 tif->tif_nextdiroff = nextdiroff;
2669 tif->tif_curdir = dirn;
2670 /* Reset to relative stepping */
2672 }
2673 else
2674 {
2675
2676 /* Fast path when we just advance relative to the current directory:
2677 * start at the current dir offset and continue to seek from there.
2678 * Check special cases when relative is not allowed:
2679 * - jump back from SubIFD or custom directory
2680 * - right after TIFFWriteDirectory() jump back to that directory
2681 * using TIFFSetDirectory() */
2682 const int relative = (dirn >= tif->tif_curdir) &&
2683 (tif->tif_diroff != 0) &&
2685
2686 if (relative)
2687 {
2688 nextdiroff = tif->tif_diroff;
2689 dirn -= tif->tif_curdir;
2690 nextdirnum = tif->tif_curdir;
2691 }
2692 else if (!(tif->tif_flags & TIFF_BIGTIFF))
2693 nextdiroff = tif->tif_header.classic.tiff_diroff;
2694 else
2695 nextdiroff = tif->tif_header.big.tiff_diroff;
2696
2697 /* Reset to relative stepping */
2699
2700 for (n = dirn; n > 0 && nextdiroff != 0; n--)
2701 if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
2702 return (0);
2703 /* If the n-th directory could not be reached (does not exist),
2704 * return here without touching anything further. */
2705 if (nextdiroff == 0 || n > 0)
2706 return (0);
2707
2708 tif->tif_nextdiroff = nextdiroff;
2709
2710 /* Set curdir to the actual directory index. */
2711 if (relative)
2712 tif->tif_curdir += dirn - n;
2713 else
2714 tif->tif_curdir = dirn - n;
2715 }
2716
2717 /* The -1 decrement is because TIFFReadDirectory will increment
2718 * tif_curdir after successfully reading the directory. */
2719 if (tif->tif_curdir == 0)
2721 else
2722 tif->tif_curdir--;
2723
2724 tdir_t curdir = tif->tif_curdir;
2725
2726 int retval = TIFFReadDirectory(tif);
2727
2728 if (!retval && tif->tif_curdir == curdir)
2729 {
2730 /* If tif_curdir has not be incremented, TIFFFetchDirectory() in
2731 * TIFFReadDirectory() has failed and tif_curdir shall be set
2732 * specifically. */
2734 }
2735 return (retval);
2736}
2737
2738/*
2739 * Set the current directory to be the directory
2740 * located at the specified file offset. This interface
2741 * is used mainly to access directories linked with
2742 * the SubIFD tag (e.g. thumbnail images).
2743 */
2744int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
2745{
2746 /* Match nextdiroff and curdir for consistent IFD-loop checking.
2747 * Only with TIFFSetSubDirectory() the IFD list can be corrupted with
2748 * invalid offsets within the main IFD tree. In the case of several subIFDs
2749 * of a main image, there are two possibilities that are not even mutually
2750 * exclusive. a.) The subIFD tag contains an array with all offsets of the
2751 * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters.
2752 * (refer to
2753 * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.)
2754 */
2755 int retval;
2756 uint32_t curdir = 0;
2757 int8_t probablySubIFD = 0;
2758 if (diroff == 0)
2759 {
2760 /* Special case to set tif_diroff=0, which is done in
2761 * TIFFReadDirectory() below to indicate that the currently read IFD is
2762 * treated as a new, fresh IFD. */
2765 }
2766 else
2767 {
2768 if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir))
2769 {
2770 /* Non-existing offsets might point to a SubIFD or invalid IFD.*/
2771 probablySubIFD = 1;
2772 }
2773 /* -1 because TIFFReadDirectory() will increment tif_curdir. */
2774 if (curdir >= 1)
2775 tif->tif_curdir = curdir - 1;
2776 else
2778 }
2779 curdir = tif->tif_curdir;
2780
2781 tif->tif_nextdiroff = diroff;
2782 retval = TIFFReadDirectory(tif);
2783
2784 /* tif_curdir is incremented in TIFFReadDirectory(), but if it has not been
2785 * incremented, TIFFFetchDirectory() has failed there and tif_curdir shall
2786 * be set specifically. */
2787 if (!retval && diroff != 0 && tif->tif_curdir == curdir)
2788 {
2790 }
2791
2792 if (probablySubIFD)
2793 {
2794 if (retval)
2795 {
2796 /* Reset IFD list to start new one for SubIFD chain and also start
2797 * SubIFD chain with tif_curdir=0 for IFD loop checking. */
2798 /* invalidate IFD loop lists */
2800 tif->tif_curdir = 0; /* first directory of new chain */
2801 /* add this offset to new IFD list */
2802 retval = _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff);
2803 }
2804 /* To be able to return from SubIFD or custom-IFD to main-IFD */
2806 }
2807
2808 return (retval);
2809}
2810
2811/*
2812 * Return file offset of the current directory.
2813 */
2814uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); }
2815
2816/*
2817 * Return an indication of whether or not we are
2818 * at the last directory in the file.
2819 */
2820int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); }
2821
2822/*
2823 * Unlink the specified directory from the directory chain.
2824 * Note: First directory starts with number dirn=1.
2825 * This is different to TIFFSetDirectory() where the first directory starts with
2826 * zero.
2827 */
2829{
2830 static const char module[] = "TIFFUnlinkDirectory";
2831 uint64_t nextdir;
2832 tdir_t nextdirnum;
2833 uint64_t off;
2834 tdir_t n;
2835
2836 if (tif->tif_mode == O_RDONLY)
2837 {
2838 TIFFErrorExtR(tif, module,
2839 "Can not unlink directory in read-only file");
2840 return (0);
2841 }
2842 if (dirn == 0)
2843 {
2844 TIFFErrorExtR(tif, module,
2845 "For TIFFUnlinkDirectory() first directory starts with "
2846 "number 1 and not 0");
2847 return (0);
2848 }
2849 /*
2850 * Go to the directory before the one we want
2851 * to unlink and nab the offset of the link
2852 * field we'll need to patch.
2853 */
2854 if (!(tif->tif_flags & TIFF_BIGTIFF))
2855 {
2856 nextdir = tif->tif_header.classic.tiff_diroff;
2857 off = 4;
2858 }
2859 else
2860 {
2861 nextdir = tif->tif_header.big.tiff_diroff;
2862 off = 8;
2863 }
2864 nextdirnum = 0; /* First directory is dirn=0 */
2865
2866 for (n = dirn - 1; n > 0; n--)
2867 {
2868 if (nextdir == 0)
2869 {
2870 TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn);
2871 return (0);
2872 }
2873 if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum))
2874 return (0);
2875 }
2876 /*
2877 * Advance to the directory to be unlinked and fetch
2878 * the offset of the directory that follows.
2879 */
2880 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum))
2881 return (0);
2882 /*
2883 * Go back and patch the link field of the preceding
2884 * directory to point to the offset of the directory
2885 * that follows.
2886 */
2887 (void)TIFFSeekFile(tif, off, SEEK_SET);
2888 if (!(tif->tif_flags & TIFF_BIGTIFF))
2889 {
2890 uint32_t nextdir32;
2891 nextdir32 = (uint32_t)nextdir;
2892 assert((uint64_t)nextdir32 == nextdir);
2893 if (tif->tif_flags & TIFF_SWAB)
2894 TIFFSwabLong(&nextdir32);
2895 if (!WriteOK(tif, &nextdir32, sizeof(uint32_t)))
2896 {
2897 TIFFErrorExtR(tif, module, "Error writing directory link");
2898 return (0);
2899 }
2900 }
2901 else
2902 {
2903 /* Need local swap because nextdir has to be used unswapped below. */
2904 uint64_t nextdir64 = nextdir;
2905 if (tif->tif_flags & TIFF_SWAB)
2906 TIFFSwabLong8(&nextdir64);
2907 if (!WriteOK(tif, &nextdir64, sizeof(uint64_t)))
2908 {
2909 TIFFErrorExtR(tif, module, "Error writing directory link");
2910 return (0);
2911 }
2912 }
2913
2914 /* For dirn=1 (first directory) also update the libtiff internal
2915 * base offset variables. */
2916 if (dirn == 1)
2917 {
2918 if (!(tif->tif_flags & TIFF_BIGTIFF))
2919 tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir;
2920 else
2921 tif->tif_header.big.tiff_diroff = nextdir;
2922 }
2923
2924 /*
2925 * Leave directory state setup safely. We don't have
2926 * facilities for doing inserting and removing directories,
2927 * so it's safest to just invalidate everything. This
2928 * means that the caller can only append to the directory
2929 * chain.
2930 */
2931 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
2932 {
2933 _TIFFfreeExt(tif, tif->tif_rawdata);
2934 tif->tif_rawdata = NULL;
2935 tif->tif_rawdatasize = 0;
2936 tif->tif_rawcc = 0;
2937 tif->tif_rawcp = NULL;
2938 tif->tif_rawdataoff = 0;
2939 tif->tif_rawdataloaded = 0;
2940 }
2943 TIFFFreeDirectory(tif);
2945 tif->tif_diroff = 0; /* force link on next write */
2946 tif->tif_nextdiroff = 0; /* next write must be at end */
2947 tif->tif_lastdiroff = 0; /* will be updated on next link */
2948 tif->tif_curoff = 0;
2950 if (tif->tif_curdircount > 0)
2951 tif->tif_curdircount--;
2952 else
2954 _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
2955 return (1);
2956}
float _TIFFClampDoubleToFloat(double val)
Clamp a double to the finite float range before conversion.
Definition tif_aux.c:602
tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second, const char *where)
Check and multiply two signed-size operands.
Definition tif_aux.c:91
int TIFFSetCompressionScheme(TIFF *tif, int scheme)
Reset methods and install the selected compression implementation.
int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
Register or validate a chain index/offset pair for loop detection.
int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
Resolve an index from the existing number-to-offset map.
int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
Resolve a directory offset using the chain maps.
#define CleanupField(member)
Release and clear one directory-owned allocation.
Definition tif_dir.c:2138
int TIFFDefaultDirectory(TIFF *tif)
Install the image schema, default values, and core tag methods.
Definition tif_dir.c:2316
void _TIFFResetTifDirAndInitStrileCounters(TIFFDirectory *td)
Zero directory storage and initialise traversal sentinels.
Definition tif_dir.c:2151
static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off, tdir_t *nextdirnum)
Read one chain link and update traversal/map bookkeeping.
Definition tif_dir.c:2428
#define FIELD_TRANSFERFUNCTION
Index the transfer function presence bit; not an on-disk tag number.
Definition tif_dir.h:580
#define FIELD_SMAXSAMPLEVALUE
Index the per-sample maximum presence bit; not an on-disk tag number.
Definition tif_dir.h:544
#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
static const TIFFFieldArray exifFieldArray
Wrap the static EXIF field schema.
Definition tif_dirinfo.c:79
#define NOSTRIP
Represent the absence of a current strip with UINT32_MAX.
Definition tif_dir.h:250
#define FIELD_IGNORE
Mark a processed-but-discarded tag; bit zero is reserved.
Definition tif_dir.h:412
const TIFFFieldArray * _TIFFGetFields(void)
Return the built-in image field schema.
#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
static const TIFFFieldArray tiffFieldArray
Wrap the static image field schema.
Definition tif_dirinfo.c:78
#define FIELD_NUMBEROFINKS
Index the number of inks presence bit; not an on-disk tag number.
Definition tif_dir.h:595
#define FIELD_TILEDIMENSIONS
Index the tile dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:424
static const TIFFFieldArray gpsFieldArray
Wrap the static GPS field schema.
Definition tif_dirinfo.c:80
#define FIELD_COMPRESSION
Index the compression presence bit; not an on-disk tag number.
Definition tif_dir.h:451
void _TIFFSetupFields(TIFF *tif, const TIFFFieldArray *fieldarray)
Replace the registered field-pointer table with a schema.
#define FIELD_YCBCRPOSITIONING
Index the YCbCr positioning presence bit; not an on-disk tag number.
Definition tif_dir.h:569
const TIFFFieldArray * _TIFFGetGpsFields(void)
Return the built-in GPS field schema.
#define NOTILE
Represent the absence of a current tile with UINT32_MAX.
Definition tif_dir.h:264
#define TIFFClrFieldBit(tif, field)
Clear a directory presence bit.
Definition tif_dir.h:688
static TIFFExtendProc _TIFFextender
Store the process-wide borrowed directory-extension callback.
Definition tif_dir.c:2230
#define FIELD_INKNAMES
Index the ink names presence bit; not an on-disk tag number.
Definition tif_dir.h:585
#define TIFFSetFieldBit(tif, field)
Set a directory presence bit.
Definition tif_dir.h:675
#define FIELD_SAMPLESPERPIXEL
Index the samples per pixel presence bit; not an on-disk tag number.
Definition tif_dir.h:476
const TIFFFieldArray * _TIFFGetExifFields(void)
Return the built-in EXIF field schema.
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
Leave decoded bytes unchanged.
Definition tif_read.c:2209
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
Swap each 3-byte sample in place.
Definition tif_read.c:2226
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
Swap each 2-byte sample in place.
Definition tif_read.c:2216
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
Swap each 4-byte sample in place.
Definition tif_read.c:2236
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
Swap each 8-byte sample in place.
Definition tif_read.c:2246
void _TIFFCleanupIFDOffsetAndNumberMaps(TIFF *tif)
Release a handle's directory-offset/index lookup maps.
Definition tif_close.c:127
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n)
Replace an owned long array with a copied array.
Definition tif_dir.c:141
static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb, size_t elem_size)
Discard an old allocation and copy a replacement byte product.
Definition tif_dir.c:86
void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp, uint32_t n)
Replace an owned long array with a copied array.
Definition tif_dir.c:145
void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n)
Replace an owned byte array with a copied array.
Definition tif_dir.c:103
void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n)
Replace an owned float array with a copied array.
Definition tif_dir.c:175
static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n)
Replace an owned string byte sequence with a copy.
Definition tif_dir.c:126
static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp, uint32_t n)
Replace an owned 64-bit integer array with a copy.
Definition tif_dir.c:165
void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
Replace an owned byte array with a copied array.
Definition tif_dir.c:107
void * _TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
Resize a handle-aware block or release it for a nonpositive size.
Definition tif_open.c:257
static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value, size_t nmemb)
Replace an owned double array with one repeated value.
Definition tif_dir.c:208
void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n)
Replace an owned double array with a copied array.
Definition tif_dir.c:180
void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp, uint32_t n)
Replace an owned double array with a copied array.
Definition tif_dir.c:184
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n)
Replace an owned short array with a copied array.
Definition tif_dir.c:131
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
static int setShortArrayWithCount(TIFF *tif, uint16_t **wpp, uint32_t *countp, const uint16_t *wp, uint32_t n)
Install a short array together with the number of elements it holds.
Definition tif_dir.c:241
void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp, uint32_t n)
Replace an owned short array with a copied array.
Definition tif_dir.c:135
void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n)
Replace an owned float array with a copied array.
Definition tif_dir.c:171
#define TIFF_NON_EXISTENT_DIR_NUMBER
Mark an unknown or nonexistent directory index.
Definition tiffiop.h:95
#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 TIFF_PERSAMPLE
Select per-sample arrays for supported sample-valued fields.
Definition tiffiop.h:523
#define TIFF_BUF4WRITE
Interpret the raw buffer and its byte count as pending output.
Definition tiffiop.h:513
#define SeekOK(tif, off)
Test whether an absolute seek reaches the requested file position.
Definition tiffiop.h:1017
#define isPseudoTag(t)
Test whether a tag identifier is outside the on-disk 16-bit range.
Definition tiffiop.h:821
#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_DIRTYDIRECT
Mark a current directory that needs writing.
Definition tiffiop.h:421
#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
#define TIFF_ISTILED
Mark tile-based rather than strip-based image storage.
Definition tiffiop.h:456
int _TIFFFillStriles(TIFF *tif)
Materialise deferred strile arrays when required.
static void cancelTransferFunction(TIFF *tif)
Drop a TransferFunction that no longer describes the directory it belongs to.
Definition tif_dir.c:292
#define DATATYPE_INT
Identify legacy DataType signed integer samples.
Definition tif_dir.c:53
static void cancelColorMap(TIFF *tif)
Drop a ColorMap that no longer describes the directory it belongs to.
Definition tif_dir.c:264
static int setTransferFunction(TIFF *tif, va_list ap, uint32_t count)
Install the TransferFunction curves.
Definition tif_dir.c:372
#define DATATYPE_UINT
Identify legacy DataType unsigned integer samples.
Definition tif_dir.c:58
#define DATATYPE_IEEEFP
Identify legacy DataType IEEE floating-point samples.
Definition tif_dir.c:63
static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
Implement core and generic custom-tag queries.
Definition tif_dir.c:1720
static int OkToChangeTag(TIFF *tif, uint32_t tag)
Check registration and tag mutability during writing.
Definition tif_dir.c:1594
#define DATATYPE_VOID
Identify legacy DataType untyped samples.
Definition tif_dir.c:48
static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
Implement core and generic custom-tag storage.
Definition tif_dir.c:599
#define EXTRASAMPLE_COREL_UNASSALPHA
Recognise Corel's nonstandard unassociated-alpha code.
static int setColorMap(TIFF *tif, va_list ap, uint32_t count)
Install the ColorMap arrays.
Definition tif_dir.c:325
static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
Count terminated ink names within a byte extent.
Definition tif_dir.c:512
static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
Validate and install extra-sample interpretations.
Definition tif_dir.c:441
static void removeCustomValue(TIFFDirectory *td, int i)
Remove a custom tag entry whose value could not be set.
Definition tif_dir.c:554
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 TIFFSwabLong(uint32_t *lp)
Reverse the byte order of one value.
Definition tif_swab.c:51
#define FIELD_CUSTOM
Identify a field stored through the custom-metadata mechanism.
Definition tiffio.h:1316
#define TIFF_SPP
Mark a field whose count follows SamplesPerPixel.
Definition tiffio.h:1302
#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 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
int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
Select and read an image directory at a file offset.
Definition tif_dir.c:2744
void TIFFFreeDirectory(TIFF *tif)
Release the current directory's owned values and codec state.
Definition tif_dir.c:2165
int TIFFLastDirectory(TIFF *tif)
Test whether the cached next-directory link is zero.
Definition tif_dir.c:2820
int TIFFCreateGPSDirectory(TIFF *tif)
Create a fresh in-memory EXIF GPS metadata directory.
Definition tif_dir.c:2306
int TIFFSetDirectory(TIFF *tif, tdir_t dirn)
Select and read an image directory by its main-chain index.
Definition tif_dir.c:2648
int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn)
Remove an image directory from the on-file main chain.
Definition tif_dir.c:2828
int TIFFCreateDirectory(TIFF *tif)
Replace current values with a fresh in-memory image directory.
Definition tif_dir.c:2246
int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray)
Replace current values with a fresh custom metadata directory.
Definition tif_dir.c:2258
int TIFFCreateEXIFDirectory(TIFF *tif)
Create a fresh in-memory EXIF metadata directory.
Definition tif_dir.c:2296
uint64_t TIFFCurrentDirOffset(TIFF *tif)
Read the cached file offset of the current directory.
Definition tif_dir.c:2814
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Find a registered field by tag number.
void(* TIFFExtendProc)(TIFF *tif)
Extend the image-directory field table during default initialisation.
Definition tiffio.h:1026
const TIFFField * TIFFFindField(TIFF *tif, uint32_t tag, TIFFDataType dt)
Find a registered field by tag number and file data type.
TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender)
Replace the process-wide image-directory extension callback.
Definition tif_dir.c:2232
int TIFFFieldSetGetSize(const TIFFField *fip)
Report the internal element storage width of a field.
int TIFFReadDirectory(TIFF *tif)
Read and install the next image directory.
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define PERSAMPLE_MULTI
Present as multiple values.
Definition tiff.h:1639
#define TIFFTAG_PERSAMPLE
Pseudo-tag: Interface for per sample tags.
Definition tiff.h:1635
int TIFFUnsetField(TIFF *tif, uint32_t tag)
Clear tag presence or remove a stored custom value.
Definition tif_dir.c:1641
int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
Query a present tag in the current directory.
Definition tif_dir.c:2116
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
int TIFFGetField(TIFF *tif, uint32_t tag,...)
Query a present tag in the current directory.
Definition tif_dir.c:2099
int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1686
#define COMPRESSION_NONE
Dump mode.
Definition tiff.h:302
#define TIFFTAG_BITSPERSAMPLE
TIFF tag: Bits per channel (sample).
Definition tiff.h:298
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define TIFFTAG_RESOLUTIONUNIT
TIFF tag: Units of resolutions.
Definition tiff.h:516
#define TIFFTAG_DOTRANGE
TIFF tag: 0% and 100% dot codes (introduced in TIFF 6.0).
Definition tiff.h:596
#define TIFFTAG_SUBIFD
TIFF tag: Subimage descriptors.
Definition tiff.h:584
#define EXTRASAMPLE_UNASSALPHA
Unassociated alpha data (introduced in TIFF 6.0).
Definition tiff.h:606
#define YCBCRPOSITION_CENTERED
As in PostScript Level 2 (introduced in TIFF 6.0).
Definition tiff.h:733
#define RESUNIT_CENTIMETER
Resolution measured in pixels per centimetre.
Definition tiff.h:522
#define EXTRASAMPLE_ASSOCALPHA
Associated alpha data (introduced in TIFF 6.0).
Definition tiff.h:604
#define ORIENTATION_TOPLEFT
Row 0 top, col 0 lhs.
Definition tiff.h:440
#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 TIFFTAG_DATATYPE
TIFF tag: Legacy sample-type indicator; superseded by TIFFTAG_SAMPLEFORMAT (obsoleted by TIFF 6....
Definition tiff.h:770
#define TIFFTAG_YPOSITION
TIFF tag: Y page offset of image lhs.
Definition tiff.h:480
#define FILLORDER_LSB2MSB
Least significant -> most.
Definition tiff.h:426
#define SAMPLEFORMAT_COMPLEXINT
Complex signed int (introduced in TIFF 6.0).
Definition tiff.h:618
#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 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 RESUNIT_NONE
No meaningful units.
Definition tiff.h:518
#define TIFFTAG_XRESOLUTION
TIFF tag: Pixels/resolution in x.
Definition tiff.h:466
#define TIFFTAG_ORIENTATION
TIFF tag: Image orientation (obsoleted by TIFF 5.0).
Definition tiff.h:438
#define TIFFTAG_HALFTONEHINTS
TIFF tag: Highlight+shadow info (introduced in TIFF 6.0).
Definition tiff.h:562
#define TIFFTAG_MATTEING
TIFF tag: Legacy alpha indicator; superseded by TIFFTAG_EXTRASAMPLES (obsoleted by TIFF 6....
Definition tiff.h:765
#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 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 FILLORDER_MSB2LSB
Most significant -> least.
Definition tiff.h:424
#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 ORIENTATION_LEFTBOT
Row 0 lhs, col 0 bottom.
Definition tiff.h:454
#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 SAMPLEFORMAT_COMPLEXIEEEFP
Complex ieee floating (introduced in TIFF 6.0).
Definition tiff.h:620
#define TIFFTAG_REFERENCEBLACKWHITE
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:737
#define RESUNIT_INCH
Resolution measured in pixels per inch.
Definition tiff.h:520
#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 THRESHHOLD_BILEVEL
B&w art scan.
Definition tiff.h:412
#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 SAMPLEFORMAT_VOID
Untyped data (introduced in TIFF 6.0).
Definition tiff.h:616
#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 PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
int uint16_vap
Represent a uint16_t value after default variadic argument promotion.
Definition tiff.h:159
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
Definition tiffio.h:91
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
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_colormap_alloc
Number of entries allocated in each ColorMap plane, zero when none is installed.
Definition tif_dir.h:228
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
uint32_t td_dirdatasize_Noffsets
Number of ranges collected in td_dirdatasize_offsets.
Definition tif_dir.h:377
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
uint16_t * td_sampleinfo
Owned ExtraSamples interpretation array of td_extrasamples entries, or NULL.
Definition tif_dir.h:237
tmsize_t td_tilesize
Cached decoded tile size in bytes; minus one when invalidated.
Definition tif_dir.h:259
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
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
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
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
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
uint32_t td_fieldsset[FIELDSET_ITEMS]
Presence bitmap indexed by FIELD_* bit numbers.
Definition tif_dir.h:151
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
uint32_t td_col
Current pixel column; UINT32_MAX before traversal starts.
Definition tif_dir.h:253
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
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_stripoffsetbyteallocsize
Allocated element capacity of lazy strile offset/count caches, not byte size.
Definition tif_dir.h:286
TIFFEntryOffsetAndLength * td_dirdatasize_offsets
Owned auxiliary array of external IFD-value ranges, or NULL.
Definition tif_dir.h:380
uint32_t td_curtile
Current tile index, or NOTILE.
Definition tif_dir.h:256
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
uint32_t td_curstrip
Current strip index, or NOSTRIP.
Definition tif_dir.h:242
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
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
Definition tiffio.h:1577
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Definition tiffio.h:1575
TIFFPrintMethod printdir
Optional codec/extension directory-print callback.
Definition tiffio.h:1579
Store one custom field value and its descriptor.
Definition tif_dir.h:46
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 borrowed schema or owned compatibility batch.
Definition tif_dir.h:962
uint32_t allocated_size
Descriptor capacity when owned; zero identifies a static schema.
Definition tif_dir.h:970
TIFFField * fields
Descriptor array; static/borrowed or owned according to allocated_size and creator.
Definition tif_dir.h:978
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
unsigned char field_oktochange
Nonzero to permit changes after writing starts; ImageLength is separately exempt.
Definition tif_dir.h:1028
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
short field_writecount
Fixed setter count or variable/sample-count sentinel.
Definition tif_dir.h:1003
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
const TIFFField * tif_foundfield
Borrowed cached field descriptor; valid only while that descriptor lives.
Definition tiffiop.h:740
tdir_t tif_curdircount
Cache the number of main IFDs in the file.
Definition tiffiop.h:604
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
Definition tiffiop.h:742
int tif_setdirectory_force_absolute
Nonzero to force absolute rather than relative directory stepping.
Definition tiffiop.h:570
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
uint64_t tif_curoff
Tracked current file byte offset for read/write operations.
Definition tiffiop.h:606
uint64_t tif_nextdiroff
Next directory byte offset from the current chain link.
Definition tiffiop.h:560
TIFFPostMethod tif_postdecode
Callback transforming decoded data, for example by byte swapping.
Definition tiffiop.h:727
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
tmsize_t tif_size
Mapped-file region length in bytes.
Definition tiffiop.h:705
tdir_t tif_curdir
Track the current or last-written directory index.
Definition tiffiop.h:599
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
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
size_t tif_nfieldscompat
Number of entries in tif_fieldscompat.
Definition tiffiop.h:753
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
TIFFFieldArray * tif_fieldscompat
Owned array of legacy field-registration batches.
Definition tiffiop.h:751
uint8_t * tif_base
Mapped-file base provided by tif_mapproc; released through tif_unmapproc by TIFFCleanup() only while ...
Definition tiffiop.h:703
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