LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_dirread.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 Read Support Routines.
29 */
30
31/* Suggested pending improvements:
32 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33 * the pointer to the appropriate TIFFField structure early on in
34 * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
35 */
36
42#include "tiffconf.h"
43
44#include "tiffiop.h"
45#include <float.h>
46#include <limits.h>
47#include <stdlib.h>
48#include <string.h>
49
54#define FAILED_FII ((uint32_t)-1)
55
56#ifdef HAVE_IEEEFP
70#define TIFFCvtIEEEFloatToNative(tif, n, fp)
84#define TIFFCvtIEEEDoubleToNative(tif, n, dp)
85#else
86/* If your machine does not support IEEE floating point then you will need to
87 * add support to tif_machdep.c to convert between the native format and
88 * IEEE format. */
102extern void TIFFCvtIEEEFloatToNative(TIFF *tif, uint32_t n, float *fp);
116extern void TIFFCvtIEEEDoubleToNative(TIFF *tif, uint32_t n, double *dp);
117#endif
118
119static enum TIFFReadDirEntryErr
120TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value);
121static enum TIFFReadDirEntryErr
122TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value);
123static enum TIFFReadDirEntryErr
124TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value);
125static enum TIFFReadDirEntryErr
126TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value);
127static enum TIFFReadDirEntryErr
128TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value);
129static enum TIFFReadDirEntryErr
130TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value);
131static enum TIFFReadDirEntryErr
132TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
133static enum TIFFReadDirEntryErr
134TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value);
135static enum TIFFReadDirEntryErr
136TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value);
137static enum TIFFReadDirEntryErr
138TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
139static enum TIFFReadDirEntryErr
140TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value);
141
142static enum TIFFReadDirEntryErr
143TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
144 uint32_t desttypesize, void **value);
145static enum TIFFReadDirEntryErr
146TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value);
147static enum TIFFReadDirEntryErr
148TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value);
149static enum TIFFReadDirEntryErr
150TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value);
151static enum TIFFReadDirEntryErr
152TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value);
153static enum TIFFReadDirEntryErr
154TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value);
155static enum TIFFReadDirEntryErr
156TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value);
157static enum TIFFReadDirEntryErr
158TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
159static enum TIFFReadDirEntryErr
160TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value);
161static enum TIFFReadDirEntryErr
162TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value);
163static enum TIFFReadDirEntryErr
164TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value);
165static enum TIFFReadDirEntryErr
166TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value);
167
168static enum TIFFReadDirEntryErr
170 uint16_t *value);
171
172static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
173 uint8_t *value);
174static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
175 int8_t *value);
176static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
177 uint16_t *value);
178static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
179 int16_t *value);
180static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
181 uint32_t *value);
182static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
183 int32_t *value);
184static enum TIFFReadDirEntryErr
186 uint64_t *value);
187static enum TIFFReadDirEntryErr
189 int64_t *value);
190static enum TIFFReadDirEntryErr
192 double *value);
193static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
194 float *value);
195static enum TIFFReadDirEntryErr
196TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
197#if 0
198static enum TIFFReadDirEntryErr
199TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
200 TIFFRational_t *value);
201#endif
202static enum TIFFReadDirEntryErr
204static enum TIFFReadDirEntryErr
206static enum TIFFReadDirEntryErr
208static enum TIFFReadDirEntryErr
210static enum TIFFReadDirEntryErr
212static enum TIFFReadDirEntryErr
214static enum TIFFReadDirEntryErr
216
217static enum TIFFReadDirEntryErr
219static enum TIFFReadDirEntryErr
221static enum TIFFReadDirEntryErr
223static enum TIFFReadDirEntryErr
225static enum TIFFReadDirEntryErr
227static enum TIFFReadDirEntryErr
229static enum TIFFReadDirEntryErr
231
232static enum TIFFReadDirEntryErr
234static enum TIFFReadDirEntryErr
236static enum TIFFReadDirEntryErr
238static enum TIFFReadDirEntryErr
240static enum TIFFReadDirEntryErr
242static enum TIFFReadDirEntryErr
244
245static enum TIFFReadDirEntryErr
247static enum TIFFReadDirEntryErr
249static enum TIFFReadDirEntryErr
251static enum TIFFReadDirEntryErr
253static enum TIFFReadDirEntryErr
255
256static enum TIFFReadDirEntryErr
258static enum TIFFReadDirEntryErr
260static enum TIFFReadDirEntryErr
262static enum TIFFReadDirEntryErr
264static enum TIFFReadDirEntryErr
266
267static enum TIFFReadDirEntryErr
269static enum TIFFReadDirEntryErr
271static enum TIFFReadDirEntryErr
273
274static enum TIFFReadDirEntryErr
276static enum TIFFReadDirEntryErr
278static enum TIFFReadDirEntryErr
280static enum TIFFReadDirEntryErr
282
283static enum TIFFReadDirEntryErr
285
286static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
287 tmsize_t size, void *dest);
288static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
289 const char *module, const char *tagname,
290 int recover);
291
292static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
293 uint16_t dircount);
295 uint16_t dircount,
296 uint16_t tagid);
297static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
298 uint32_t *fii);
299
300static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
301 uint16_t dircount);
302static void MissingRequired(TIFF *, const char *);
303static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
304static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
305 TIFFDirEntry **pdir, uint64_t *nextdiroff);
306static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover);
307static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
308 uint64_t **lpp);
312static uint64_t TIFFReadUInt64(const uint8_t *value);
313static int _TIFFGetMaxColorChannels(uint16_t photometric);
314
315static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount);
316
338
339/*
340 Unaligned safe copy of a uint64_t value from an octet array.
341*/
353static uint64_t TIFFReadUInt64(const uint8_t *value)
354{
355 UInt64Aligned_t result;
356
357 result.c[0] = value[0];
358 result.c[1] = value[1];
359 result.c[2] = value[2];
360 result.c[3] = value[3];
361 result.c[4] = value[4];
362 result.c[5] = value[5];
363 result.c[6] = value[6];
364 result.c[7] = value[7];
365
366 return result.l;
367}
368
393static enum TIFFReadDirEntryErr
394TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
395{
396 enum TIFFReadDirEntryErr err;
397 if (direntry->tdir_count != 1)
399 switch (direntry->tdir_type)
400 {
401 case TIFF_BYTE:
402 case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
403 field_readcount==1 */
404 TIFFReadDirEntryCheckedByte(tif, direntry, value);
405 return (TIFFReadDirEntryErrOk);
406 case TIFF_SBYTE:
407 {
408 int8_t m;
409 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
411 if (err != TIFFReadDirEntryErrOk)
412 return (err);
413 *value = (uint8_t)m;
414 return (TIFFReadDirEntryErrOk);
415 }
416 case TIFF_SHORT:
417 {
418 uint16_t m;
419 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
421 if (err != TIFFReadDirEntryErrOk)
422 return (err);
423 *value = (uint8_t)m;
424 return (TIFFReadDirEntryErrOk);
425 }
426 case TIFF_SSHORT:
427 {
428 int16_t m;
429 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
431 if (err != TIFFReadDirEntryErrOk)
432 return (err);
433 *value = (uint8_t)m;
434 return (TIFFReadDirEntryErrOk);
435 }
436 case TIFF_LONG:
437 {
438 uint32_t m;
439 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
441 if (err != TIFFReadDirEntryErrOk)
442 return (err);
443 *value = (uint8_t)m;
444 return (TIFFReadDirEntryErrOk);
445 }
446 case TIFF_SLONG:
447 {
448 int32_t m;
449 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
451 if (err != TIFFReadDirEntryErrOk)
452 return (err);
453 *value = (uint8_t)m;
454 return (TIFFReadDirEntryErrOk);
455 }
456 case TIFF_LONG8:
457 {
458 uint64_t m;
459 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
460 if (err != TIFFReadDirEntryErrOk)
461 return (err);
463 if (err != TIFFReadDirEntryErrOk)
464 return (err);
465 *value = (uint8_t)m;
466 return (TIFFReadDirEntryErrOk);
467 }
468 case TIFF_SLONG8:
469 {
470 int64_t m;
471 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
472 if (err != TIFFReadDirEntryErrOk)
473 return (err);
475 if (err != TIFFReadDirEntryErrOk)
476 return (err);
477 *value = (uint8_t)m;
478 return (TIFFReadDirEntryErrOk);
479 }
480 default:
482 }
483}
484
509static enum TIFFReadDirEntryErr
510TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
511{
512 enum TIFFReadDirEntryErr err;
513 if (direntry->tdir_count != 1)
515 switch (direntry->tdir_type)
516 {
517 case TIFF_BYTE:
518 case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
519 field_readcount==1 */
520 {
521 uint8_t m;
522 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
524 if (err != TIFFReadDirEntryErrOk)
525 return (err);
526 *value = (int8_t)m;
527 return (TIFFReadDirEntryErrOk);
528 }
529 case TIFF_SBYTE:
530 {
531 TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
532 return (TIFFReadDirEntryErrOk);
533 }
534 case TIFF_SHORT:
535 {
536 uint16_t m;
537 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
539 if (err != TIFFReadDirEntryErrOk)
540 return (err);
541 *value = (int8_t)m;
542 return (TIFFReadDirEntryErrOk);
543 }
544 case TIFF_SSHORT:
545 {
546 int16_t m;
547 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
549 if (err != TIFFReadDirEntryErrOk)
550 return (err);
551 *value = (int8_t)m;
552 return (TIFFReadDirEntryErrOk);
553 }
554 case TIFF_LONG:
555 {
556 uint32_t m;
557 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
559 if (err != TIFFReadDirEntryErrOk)
560 return (err);
561 *value = (int8_t)m;
562 return (TIFFReadDirEntryErrOk);
563 }
564 case TIFF_SLONG:
565 {
566 int32_t m;
567 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
569 if (err != TIFFReadDirEntryErrOk)
570 return (err);
571 *value = (int8_t)m;
572 return (TIFFReadDirEntryErrOk);
573 }
574 case TIFF_LONG8:
575 {
576 uint64_t m;
577 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
578 if (err != TIFFReadDirEntryErrOk)
579 return (err);
581 if (err != TIFFReadDirEntryErrOk)
582 return (err);
583 *value = (int8_t)m;
584 return (TIFFReadDirEntryErrOk);
585 }
586 case TIFF_SLONG8:
587 {
588 int64_t m;
589 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
590 if (err != TIFFReadDirEntryErrOk)
591 return (err);
593 if (err != TIFFReadDirEntryErrOk)
594 return (err);
595 *value = (int8_t)m;
596 return (TIFFReadDirEntryErrOk);
597 }
598 default:
600 }
601} /*-- TIFFReadDirEntrySbyte() --*/
602
627static enum TIFFReadDirEntryErr
628TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
629{
630 enum TIFFReadDirEntryErr err;
631 if (direntry->tdir_count != 1)
633 switch (direntry->tdir_type)
634 {
635 case TIFF_BYTE:
636 {
637 uint8_t m;
638 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
639 *value = (uint16_t)m;
640 return (TIFFReadDirEntryErrOk);
641 }
642 case TIFF_SBYTE:
643 {
644 int8_t m;
645 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
647 if (err != TIFFReadDirEntryErrOk)
648 return (err);
649 *value = (uint16_t)m;
650 return (TIFFReadDirEntryErrOk);
651 }
652 case TIFF_SHORT:
653 TIFFReadDirEntryCheckedShort(tif, direntry, value);
654 return (TIFFReadDirEntryErrOk);
655 case TIFF_SSHORT:
656 {
657 int16_t m;
658 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
660 if (err != TIFFReadDirEntryErrOk)
661 return (err);
662 *value = (uint16_t)m;
663 return (TIFFReadDirEntryErrOk);
664 }
665 case TIFF_LONG:
666 {
667 uint32_t m;
668 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
670 if (err != TIFFReadDirEntryErrOk)
671 return (err);
672 *value = (uint16_t)m;
673 return (TIFFReadDirEntryErrOk);
674 }
675 case TIFF_SLONG:
676 {
677 int32_t m;
678 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
680 if (err != TIFFReadDirEntryErrOk)
681 return (err);
682 *value = (uint16_t)m;
683 return (TIFFReadDirEntryErrOk);
684 }
685 case TIFF_LONG8:
686 {
687 uint64_t m;
688 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
689 if (err != TIFFReadDirEntryErrOk)
690 return (err);
692 if (err != TIFFReadDirEntryErrOk)
693 return (err);
694 *value = (uint16_t)m;
695 return (TIFFReadDirEntryErrOk);
696 }
697 case TIFF_SLONG8:
698 {
699 int64_t m;
700 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
701 if (err != TIFFReadDirEntryErrOk)
702 return (err);
704 if (err != TIFFReadDirEntryErrOk)
705 return (err);
706 *value = (uint16_t)m;
707 return (TIFFReadDirEntryErrOk);
708 }
709 default:
711 }
712} /*-- TIFFReadDirEntryShort() --*/
713
738static enum TIFFReadDirEntryErr
739TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
740{
741 enum TIFFReadDirEntryErr err;
742 if (direntry->tdir_count != 1)
744 switch (direntry->tdir_type)
745 {
746 case TIFF_BYTE:
747 {
748 uint8_t m;
749 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
750 *value = (int16_t)m;
751 return (TIFFReadDirEntryErrOk);
752 }
753 case TIFF_SBYTE:
754 {
755 int8_t m;
756 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
757 *value = (int16_t)m;
758 return (TIFFReadDirEntryErrOk);
759 }
760 case TIFF_SHORT:
761 {
762 uint16_t m;
763 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
765 if (err != TIFFReadDirEntryErrOk)
766 return (err);
767 *value = (int16_t)m;
768 return (TIFFReadDirEntryErrOk);
769 }
770 case TIFF_SSHORT:
771 TIFFReadDirEntryCheckedSshort(tif, direntry, value);
772 return (TIFFReadDirEntryErrOk);
773 case TIFF_LONG:
774 {
775 uint32_t m;
776 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
778 if (err != TIFFReadDirEntryErrOk)
779 return (err);
780 *value = (int16_t)m;
781 return (TIFFReadDirEntryErrOk);
782 }
783 case TIFF_SLONG:
784 {
785 int32_t m;
786 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
788 if (err != TIFFReadDirEntryErrOk)
789 return (err);
790 *value = (int16_t)m;
791 return (TIFFReadDirEntryErrOk);
792 }
793 case TIFF_LONG8:
794 {
795 uint64_t m;
796 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
797 if (err != TIFFReadDirEntryErrOk)
798 return (err);
800 if (err != TIFFReadDirEntryErrOk)
801 return (err);
802 *value = (int16_t)m;
803 return (TIFFReadDirEntryErrOk);
804 }
805 case TIFF_SLONG8:
806 {
807 int64_t m;
808 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
809 if (err != TIFFReadDirEntryErrOk)
810 return (err);
812 if (err != TIFFReadDirEntryErrOk)
813 return (err);
814 *value = (int16_t)m;
815 return (TIFFReadDirEntryErrOk);
816 }
817 default:
819 }
820} /*-- TIFFReadDirEntrySshort() --*/
821
847static enum TIFFReadDirEntryErr
848TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
849{
850 enum TIFFReadDirEntryErr err;
851 if (direntry->tdir_count != 1)
853 switch (direntry->tdir_type)
854 {
855 case TIFF_BYTE:
856 {
857 uint8_t m;
858 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
859 *value = (uint32_t)m;
860 return (TIFFReadDirEntryErrOk);
861 }
862 case TIFF_SBYTE:
863 {
864 int8_t m;
865 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
867 if (err != TIFFReadDirEntryErrOk)
868 return (err);
869 *value = (uint32_t)m;
870 return (TIFFReadDirEntryErrOk);
871 }
872 case TIFF_SHORT:
873 {
874 uint16_t m;
875 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
876 *value = (uint32_t)m;
877 return (TIFFReadDirEntryErrOk);
878 }
879 case TIFF_SSHORT:
880 {
881 int16_t m;
882 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
884 if (err != TIFFReadDirEntryErrOk)
885 return (err);
886 *value = (uint32_t)m;
887 return (TIFFReadDirEntryErrOk);
888 }
889 case TIFF_LONG:
890 case TIFF_IFD:
891 TIFFReadDirEntryCheckedLong(tif, direntry, value);
892 return (TIFFReadDirEntryErrOk);
893 case TIFF_SLONG:
894 {
895 int32_t m;
896 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
898 if (err != TIFFReadDirEntryErrOk)
899 return (err);
900 *value = (uint32_t)m;
901 return (TIFFReadDirEntryErrOk);
902 }
903 case TIFF_LONG8:
904 case TIFF_IFD8:
905 {
906 uint64_t m;
907 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
908 if (err != TIFFReadDirEntryErrOk)
909 return (err);
911 if (err != TIFFReadDirEntryErrOk)
912 return (err);
913 *value = (uint32_t)m;
914 return (TIFFReadDirEntryErrOk);
915 }
916 case TIFF_SLONG8:
917 {
918 int64_t m;
919 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
920 if (err != TIFFReadDirEntryErrOk)
921 return (err);
923 if (err != TIFFReadDirEntryErrOk)
924 return (err);
925 *value = (uint32_t)m;
926 return (TIFFReadDirEntryErrOk);
927 }
928 default:
930 }
931} /*-- TIFFReadDirEntryLong() --*/
932
957static enum TIFFReadDirEntryErr
958TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
959{
960 enum TIFFReadDirEntryErr err;
961 if (direntry->tdir_count != 1)
963 switch (direntry->tdir_type)
964 {
965 case TIFF_BYTE:
966 {
967 uint8_t m;
968 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
969 *value = (int32_t)m;
970 return (TIFFReadDirEntryErrOk);
971 }
972 case TIFF_SBYTE:
973 {
974 int8_t m;
975 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
976 *value = (int32_t)m;
977 return (TIFFReadDirEntryErrOk);
978 }
979 case TIFF_SHORT:
980 {
981 uint16_t m;
982 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
983 *value = (int32_t)m;
984 return (TIFFReadDirEntryErrOk);
985 }
986 case TIFF_SSHORT:
987 {
988 int16_t m;
989 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
990 *value = (int32_t)m;
991 return (TIFFReadDirEntryErrOk);
992 }
993 case TIFF_LONG:
994 {
995 uint32_t m;
996 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
998 if (err != TIFFReadDirEntryErrOk)
999 return (err);
1000 *value = (int32_t)m;
1001 return (TIFFReadDirEntryErrOk);
1002 }
1003 case TIFF_SLONG:
1004 TIFFReadDirEntryCheckedSlong(tif, direntry, value);
1005 return (TIFFReadDirEntryErrOk);
1006 case TIFF_LONG8:
1007 {
1008 uint64_t m;
1009 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1010 if (err != TIFFReadDirEntryErrOk)
1011 return (err);
1013 if (err != TIFFReadDirEntryErrOk)
1014 return (err);
1015 *value = (int32_t)m;
1016 return (TIFFReadDirEntryErrOk);
1017 }
1018 case TIFF_SLONG8:
1019 {
1020 int64_t m;
1021 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1022 if (err != TIFFReadDirEntryErrOk)
1023 return (err);
1025 if (err != TIFFReadDirEntryErrOk)
1026 return (err);
1027 *value = (int32_t)m;
1028 return (TIFFReadDirEntryErrOk);
1029 }
1030 default:
1031 return (TIFFReadDirEntryErrType);
1032 }
1033} /*-- TIFFReadDirEntrySlong() --*/
1034
1060static enum TIFFReadDirEntryErr
1061TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1062{
1063 enum TIFFReadDirEntryErr err;
1064 if (direntry->tdir_count != 1)
1065 return (TIFFReadDirEntryErrCount);
1066 switch (direntry->tdir_type)
1067 {
1068 case TIFF_BYTE:
1069 {
1070 uint8_t m;
1071 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1072 *value = (uint64_t)m;
1073 return (TIFFReadDirEntryErrOk);
1074 }
1075 case TIFF_SBYTE:
1076 {
1077 int8_t m;
1078 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1080 if (err != TIFFReadDirEntryErrOk)
1081 return (err);
1082 *value = (uint64_t)m;
1083 return (TIFFReadDirEntryErrOk);
1084 }
1085 case TIFF_SHORT:
1086 {
1087 uint16_t m;
1088 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1089 *value = (uint64_t)m;
1090 return (TIFFReadDirEntryErrOk);
1091 }
1092 case TIFF_SSHORT:
1093 {
1094 int16_t m;
1095 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1097 if (err != TIFFReadDirEntryErrOk)
1098 return (err);
1099 *value = (uint64_t)m;
1100 return (TIFFReadDirEntryErrOk);
1101 }
1102 case TIFF_LONG:
1103 case TIFF_IFD:
1104 {
1105 uint32_t m;
1106 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1107 *value = (uint64_t)m;
1108 return (TIFFReadDirEntryErrOk);
1109 }
1110 case TIFF_SLONG:
1111 {
1112 int32_t m;
1113 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1115 if (err != TIFFReadDirEntryErrOk)
1116 return (err);
1117 *value = (uint64_t)m;
1118 return (TIFFReadDirEntryErrOk);
1119 }
1120 case TIFF_LONG8:
1121 case TIFF_IFD8:
1122 err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1123 return (err);
1124 case TIFF_SLONG8:
1125 {
1126 int64_t m;
1127 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1128 if (err != TIFFReadDirEntryErrOk)
1129 return (err);
1131 if (err != TIFFReadDirEntryErrOk)
1132 return (err);
1133 *value = (uint64_t)m;
1134 return (TIFFReadDirEntryErrOk);
1135 }
1136 default:
1137 return (TIFFReadDirEntryErrType);
1138 }
1139} /*-- TIFFReadDirEntryLong8() --*/
1140
1165static enum TIFFReadDirEntryErr
1166TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
1167{
1168 enum TIFFReadDirEntryErr err;
1169 if (direntry->tdir_count != 1)
1170 return (TIFFReadDirEntryErrCount);
1171 switch (direntry->tdir_type)
1172 {
1173 case TIFF_BYTE:
1174 {
1175 uint8_t m;
1176 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1177 *value = (int64_t)m;
1178 return (TIFFReadDirEntryErrOk);
1179 }
1180 case TIFF_SBYTE:
1181 {
1182 int8_t m;
1183 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1184 *value = (int64_t)m;
1185 return (TIFFReadDirEntryErrOk);
1186 }
1187 case TIFF_SHORT:
1188 {
1189 uint16_t m;
1190 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1191 *value = (int64_t)m;
1192 return (TIFFReadDirEntryErrOk);
1193 }
1194 case TIFF_SSHORT:
1195 {
1196 int16_t m;
1197 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1198 *value = (int64_t)m;
1199 return (TIFFReadDirEntryErrOk);
1200 }
1201 case TIFF_LONG:
1202 {
1203 uint32_t m;
1204 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1205 *value = (int64_t)m;
1206 return (TIFFReadDirEntryErrOk);
1207 }
1208 case TIFF_SLONG:
1209 {
1210 int32_t m;
1211 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1212 *value = (int64_t)m;
1213 return (TIFFReadDirEntryErrOk);
1214 }
1215 case TIFF_LONG8:
1216 {
1217 uint64_t m;
1218 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1219 if (err != TIFFReadDirEntryErrOk)
1220 return (err);
1222 if (err != TIFFReadDirEntryErrOk)
1223 return (err);
1224 *value = (int64_t)m;
1225 return (TIFFReadDirEntryErrOk);
1226 }
1227 case TIFF_SLONG8:
1228 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
1229 return (err);
1230 default:
1231 return (TIFFReadDirEntryErrType);
1232 }
1233} /*-- TIFFReadDirEntrySlong8() --*/
1234
1263static enum TIFFReadDirEntryErr
1264TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
1265{
1266 enum TIFFReadDirEntryErr err;
1267 if (direntry->tdir_count != 1)
1268 return (TIFFReadDirEntryErrCount);
1269 switch (direntry->tdir_type)
1270 {
1271 case TIFF_BYTE:
1272 {
1273 uint8_t m;
1274 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1275 *value = (float)m;
1276 return (TIFFReadDirEntryErrOk);
1277 }
1278 case TIFF_SBYTE:
1279 {
1280 int8_t m;
1281 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1282 *value = (float)m;
1283 return (TIFFReadDirEntryErrOk);
1284 }
1285 case TIFF_SHORT:
1286 {
1287 uint16_t m;
1288 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1289 *value = (float)m;
1290 return (TIFFReadDirEntryErrOk);
1291 }
1292 case TIFF_SSHORT:
1293 {
1294 int16_t m;
1295 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1296 *value = (float)m;
1297 return (TIFFReadDirEntryErrOk);
1298 }
1299 case TIFF_LONG:
1300 {
1301 uint32_t m;
1302 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1303 *value = (float)m;
1304 return (TIFFReadDirEntryErrOk);
1305 }
1306 case TIFF_SLONG:
1307 {
1308 int32_t m;
1309 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1310 *value = (float)m;
1311 return (TIFFReadDirEntryErrOk);
1312 }
1313 case TIFF_LONG8:
1314 {
1315 uint64_t m;
1316 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1317 if (err != TIFFReadDirEntryErrOk)
1318 return (err);
1319 *value = (float)m;
1320 return (TIFFReadDirEntryErrOk);
1321 }
1322 case TIFF_SLONG8:
1323 {
1324 int64_t m;
1325 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1326 if (err != TIFFReadDirEntryErrOk)
1327 return (err);
1328 *value = (float)m;
1329 return (TIFFReadDirEntryErrOk);
1330 }
1331 case TIFF_RATIONAL:
1332 {
1333 double m;
1334 err = _TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1335 if (err != TIFFReadDirEntryErrOk)
1336 return (err);
1337 *value = (float)m;
1338 return (TIFFReadDirEntryErrOk);
1339 }
1340 case TIFF_SRATIONAL:
1341 {
1342 double m;
1343 err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1344 if (err != TIFFReadDirEntryErrOk)
1345 return (err);
1346 *value = (float)m;
1347 return (TIFFReadDirEntryErrOk);
1348 }
1349 case TIFF_FLOAT:
1350 TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1351 return (TIFFReadDirEntryErrOk);
1352 case TIFF_DOUBLE:
1353 {
1354 double m;
1355 err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1356 if (err != TIFFReadDirEntryErrOk)
1357 return (err);
1358 if ((m > (double)FLT_MAX) || (m < -(double)FLT_MAX))
1359 return (TIFFReadDirEntryErrRange);
1360 *value = (float)m;
1361 return (TIFFReadDirEntryErrOk);
1362 }
1363 default:
1364 return (TIFFReadDirEntryErrType);
1365 }
1366}
1367
1396static enum TIFFReadDirEntryErr
1397TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1398{
1399 enum TIFFReadDirEntryErr err;
1400 if (direntry->tdir_count != 1)
1401 return (TIFFReadDirEntryErrCount);
1402 switch (direntry->tdir_type)
1403 {
1404 case TIFF_BYTE:
1405 {
1406 uint8_t m;
1407 TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1408 *value = (double)m;
1409 return (TIFFReadDirEntryErrOk);
1410 }
1411 case TIFF_SBYTE:
1412 {
1413 int8_t m;
1414 TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1415 *value = (double)m;
1416 return (TIFFReadDirEntryErrOk);
1417 }
1418 case TIFF_SHORT:
1419 {
1420 uint16_t m;
1421 TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1422 *value = (double)m;
1423 return (TIFFReadDirEntryErrOk);
1424 }
1425 case TIFF_SSHORT:
1426 {
1427 int16_t m;
1428 TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1429 *value = (double)m;
1430 return (TIFFReadDirEntryErrOk);
1431 }
1432 case TIFF_LONG:
1433 {
1434 uint32_t m;
1435 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1436 *value = (double)m;
1437 return (TIFFReadDirEntryErrOk);
1438 }
1439 case TIFF_SLONG:
1440 {
1441 int32_t m;
1442 TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1443 *value = (double)m;
1444 return (TIFFReadDirEntryErrOk);
1445 }
1446 case TIFF_LONG8:
1447 {
1448 uint64_t m;
1449 err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1450 if (err != TIFFReadDirEntryErrOk)
1451 return (err);
1452 *value = (double)m;
1453 return (TIFFReadDirEntryErrOk);
1454 }
1455 case TIFF_SLONG8:
1456 {
1457 int64_t m;
1458 err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1459 if (err != TIFFReadDirEntryErrOk)
1460 return (err);
1461 *value = (double)m;
1462 return (TIFFReadDirEntryErrOk);
1463 }
1464 case TIFF_RATIONAL:
1465 err = _TIFFReadDirEntryCheckedRational(tif, direntry, value);
1466 return (err);
1467 case TIFF_SRATIONAL:
1468 err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1469 return (err);
1470 case TIFF_FLOAT:
1471 {
1472 float m;
1473 TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1474 *value = (double)m;
1475 return (TIFFReadDirEntryErrOk);
1476 }
1477 case TIFF_DOUBLE:
1478 err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1479 return (err);
1480 default:
1481 return (TIFFReadDirEntryErrType);
1482 }
1483}
1484
1508static enum TIFFReadDirEntryErr
1509TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1510{
1511 enum TIFFReadDirEntryErr err;
1512 if (direntry->tdir_count != 1)
1513 return (TIFFReadDirEntryErrCount);
1514 switch (direntry->tdir_type)
1515 {
1516 case TIFF_LONG:
1517 case TIFF_IFD:
1518 {
1519 uint32_t m;
1520 TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1521 *value = (uint64_t)m;
1522 return (TIFFReadDirEntryErrOk);
1523 }
1524 case TIFF_LONG8:
1525 case TIFF_IFD8:
1526 err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1527 return (err);
1528 default:
1529 return (TIFFReadDirEntryErrType);
1530 }
1531}
1532
1537#define INITIAL_THRESHOLD (1024 * 1024)
1542#define THRESHOLD_MULTIPLIER 10
1550#define MAX_THRESHOLD \
1551 (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
1552 INITIAL_THRESHOLD)
1553
1578 uint64_t offset,
1579 tmsize_t size,
1580 void **pdest)
1581{
1582#if SIZEOF_SIZE_T == 8
1583 tmsize_t threshold = INITIAL_THRESHOLD;
1584#endif
1585 tmsize_t already_read = 0;
1586
1587 assert(!isMapped(tif));
1588 if (size <= 0)
1590
1591 if (!SeekOK(tif, offset))
1592 return (TIFFReadDirEntryErrIo);
1593
1594 /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1595 /* so as to avoid allocating too much memory in case the file is too */
1596 /* short. We could ask for the file size, but this might be */
1597 /* expensive with some I/O layers (think of reading a gzipped file) */
1598 /* Restrict to 64 bit processes, so as to avoid reallocs() */
1599 /* on 32 bit processes where virtual memory is scarce. */
1600 while (already_read < size)
1601 {
1602 void *new_dest;
1603 tmsize_t bytes_read;
1604 tmsize_t to_read = size - already_read;
1605#if SIZEOF_SIZE_T == 8
1606 if (to_read >= threshold && threshold < MAX_THRESHOLD)
1607 {
1608 to_read = threshold;
1609 threshold *= THRESHOLD_MULTIPLIER;
1610 }
1611#endif
1612
1613 new_dest =
1614 (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1615 if (new_dest == NULL)
1616 {
1617 TIFFErrorExtR(tif, tif->tif_name,
1618 "Failed to allocate memory for %s "
1620 " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1621 "TIFFReadDirEntryArray", (tmsize_t)1,
1622 already_read + to_read);
1624 }
1625 *pdest = new_dest;
1626
1627 bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1628 if (bytes_read < 0)
1629 return TIFFReadDirEntryErrIo;
1630 already_read += bytes_read;
1631 if (bytes_read != to_read)
1632 {
1633 return TIFFReadDirEntryErrIo;
1634 }
1635 }
1636 return TIFFReadDirEntryErrOk;
1637}
1638
1639/* Caution: if raising that value, make sure int32 / uint32 overflows can't
1640 * occur elsewhere */
1645#define MAX_SIZE_TAG_DATA 2147483647U
1646
1677static enum TIFFReadDirEntryErr
1679 uint32_t *count, uint32_t desttypesize,
1680 void **value, uint64_t maxcount)
1681{
1682 int typesize;
1683 uint32_t datasize;
1684 void *data;
1685 uint64_t target_count64;
1686 int original_datasize_clamped;
1687 typesize = TIFFDataWidth((TIFFDataType)direntry->tdir_type);
1688
1689 target_count64 =
1690 (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1691
1692 if ((target_count64 == 0) || (typesize == 0))
1693 {
1694 *value = 0;
1695 return (TIFFReadDirEntryErrOk);
1696 }
1697 (void)desttypesize;
1698
1699 /* We just want to know if the original tag size is more than 4 bytes
1700 * (classic TIFF) or 8 bytes (BigTIFF)
1701 */
1702 original_datasize_clamped =
1703 ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
1704 typesize;
1705
1706 /*
1707 * As a sanity check, make sure we have no more than a 2GB tag array
1708 * in either the current data type or the dest data type. This also
1709 * avoids problems with overflow of tmsize_t on 32bit systems.
1710 */
1711 if ((uint64_t)(MAX_SIZE_TAG_DATA / (unsigned int)typesize) < target_count64)
1713 if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1715
1716 *count = (uint32_t)target_count64;
1717 datasize = (uint32_t)(*count) * (unsigned int)typesize;
1718 assert((tmsize_t)datasize > 0);
1719
1720 if (datasize > 100 * 1024 * 1024)
1721 {
1722 /* Before allocating a huge amount of memory for corrupted files, check
1723 * if size of requested memory is not greater than file size.
1724 */
1725 const uint64_t filesize = TIFFGetFileSize(tif);
1726 if (datasize > filesize)
1727 {
1728 TIFFWarningExtR(tif, "ReadDirEntryArray",
1729 "Requested memory size for tag %d (0x%x) %" PRIu32
1730 " is greater than filesize %" PRIu64
1731 ". Memory not allocated, tag not read",
1732 direntry->tdir_tag, direntry->tdir_tag, datasize,
1733 filesize);
1734 return (TIFFReadDirEntryErrAlloc);
1735 }
1736 }
1737
1738 if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1739 return TIFFReadDirEntryErrIo;
1740
1741 if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1742 (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1743 {
1744 data = NULL;
1745 }
1746 else
1747 {
1748 data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1749 if (data == 0)
1750 return (TIFFReadDirEntryErrAlloc);
1751 }
1752 if (!(tif->tif_flags & TIFF_BIGTIFF))
1753 {
1754 /* Only the condition on original_datasize_clamped. The second
1755 * one is implied, but Coverity Scan cannot see it. */
1756 if (original_datasize_clamped <= 4 && datasize <= 4)
1757 _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1758 else
1759 {
1760 enum TIFFReadDirEntryErr err;
1761 uint32_t offset = direntry->tdir_offset.toff_long;
1762 if (tif->tif_flags & TIFF_SWAB)
1763 TIFFSwabLong(&offset);
1764 if (isMapped(tif))
1765 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1766 (tmsize_t)datasize, data);
1767 else
1768 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1769 (tmsize_t)datasize, &data);
1770 if (err != TIFFReadDirEntryErrOk)
1771 {
1772 _TIFFfreeExt(tif, data);
1773 return (err);
1774 }
1775 }
1776 }
1777 else
1778 {
1779 /* See above comment for the Classic TIFF case */
1780 if (original_datasize_clamped <= 8 && datasize <= 8)
1781 _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1782 else
1783 {
1784 enum TIFFReadDirEntryErr err;
1785 uint64_t offset = direntry->tdir_offset.toff_long8;
1786 if (tif->tif_flags & TIFF_SWAB)
1787 TIFFSwabLong8(&offset);
1788 if (isMapped(tif))
1789 err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1790 (tmsize_t)datasize, data);
1791 else
1792 err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1793 (tmsize_t)datasize, &data);
1794 if (err != TIFFReadDirEntryErrOk)
1795 {
1796 _TIFFfreeExt(tif, data);
1797 return (err);
1798 }
1799 }
1800 }
1801 *value = data;
1802 return (TIFFReadDirEntryErrOk);
1803}
1804
1834static enum TIFFReadDirEntryErr
1835TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1836 uint32_t desttypesize, void **value)
1837{
1838 return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1839 value, ~((uint64_t)0));
1840}
1841
1869static enum TIFFReadDirEntryErr
1870TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
1871{
1872 enum TIFFReadDirEntryErr err;
1873 uint32_t count;
1874 void *origdata;
1875 uint8_t *data;
1876 switch (direntry->tdir_type)
1877 {
1878 case TIFF_ASCII:
1879 case TIFF_UNDEFINED:
1880 case TIFF_BYTE:
1881 case TIFF_SBYTE:
1882 case TIFF_SHORT:
1883 case TIFF_SSHORT:
1884 case TIFF_LONG:
1885 case TIFF_SLONG:
1886 case TIFF_LONG8:
1887 case TIFF_SLONG8:
1888 break;
1889 default:
1890 return (TIFFReadDirEntryErrType);
1891 }
1892 err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1893 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1894 {
1895 *value = 0;
1896 return (err);
1897 }
1898 switch (direntry->tdir_type)
1899 {
1900 case TIFF_ASCII:
1901 case TIFF_UNDEFINED:
1902 case TIFF_BYTE:
1903 *value = (uint8_t *)origdata;
1904 return (TIFFReadDirEntryErrOk);
1905 case TIFF_SBYTE:
1906 {
1907 int8_t *m;
1908 uint32_t n;
1909 m = (int8_t *)origdata;
1910 for (n = 0; n < count; n++)
1911 {
1913 if (err != TIFFReadDirEntryErrOk)
1914 {
1915 _TIFFfreeExt(tif, origdata);
1916 return (err);
1917 }
1918 m++;
1919 }
1920 *value = (uint8_t *)origdata;
1921 return (TIFFReadDirEntryErrOk);
1922 }
1923 default:
1924 break;
1925 }
1926 data = (uint8_t *)_TIFFmallocExt(tif, count);
1927 if (data == 0)
1928 {
1929 _TIFFfreeExt(tif, origdata);
1930 return (TIFFReadDirEntryErrAlloc);
1931 }
1932 switch (direntry->tdir_type)
1933 {
1934 case TIFF_SHORT:
1935 {
1936 uint16_t *ma;
1937 uint8_t *mb;
1938 uint32_t n;
1939 ma = (uint16_t *)origdata;
1940 mb = data;
1941 for (n = 0; n < count; n++)
1942 {
1943 if (tif->tif_flags & TIFF_SWAB)
1944 TIFFSwabShort(ma);
1946 if (err != TIFFReadDirEntryErrOk)
1947 break;
1948 *mb++ = (uint8_t)(*ma++);
1949 }
1950 }
1951 break;
1952 case TIFF_SSHORT:
1953 {
1954 int16_t *ma;
1955 uint8_t *mb;
1956 uint32_t n;
1957 ma = (int16_t *)origdata;
1958 mb = data;
1959 for (n = 0; n < count; n++)
1960 {
1961 if (tif->tif_flags & TIFF_SWAB)
1962 TIFFSwabShort((uint16_t *)ma);
1964 if (err != TIFFReadDirEntryErrOk)
1965 break;
1966 *mb++ = (uint8_t)(*ma++);
1967 }
1968 }
1969 break;
1970 case TIFF_LONG:
1971 {
1972 uint32_t *ma;
1973 uint8_t *mb;
1974 uint32_t n;
1975 ma = (uint32_t *)origdata;
1976 mb = data;
1977 for (n = 0; n < count; n++)
1978 {
1979 if (tif->tif_flags & TIFF_SWAB)
1980 TIFFSwabLong(ma);
1982 if (err != TIFFReadDirEntryErrOk)
1983 break;
1984 *mb++ = (uint8_t)(*ma++);
1985 }
1986 }
1987 break;
1988 case TIFF_SLONG:
1989 {
1990 int32_t *ma;
1991 uint8_t *mb;
1992 uint32_t n;
1993 ma = (int32_t *)origdata;
1994 mb = data;
1995 for (n = 0; n < count; n++)
1996 {
1997 if (tif->tif_flags & TIFF_SWAB)
1998 TIFFSwabLong((uint32_t *)ma);
2000 if (err != TIFFReadDirEntryErrOk)
2001 break;
2002 *mb++ = (uint8_t)(*ma++);
2003 }
2004 }
2005 break;
2006 case TIFF_LONG8:
2007 {
2008 uint64_t *ma;
2009 uint8_t *mb;
2010 uint32_t n;
2011 ma = (uint64_t *)origdata;
2012 mb = data;
2013 for (n = 0; n < count; n++)
2014 {
2015 if (tif->tif_flags & TIFF_SWAB)
2016 TIFFSwabLong8(ma);
2018 if (err != TIFFReadDirEntryErrOk)
2019 break;
2020 *mb++ = (uint8_t)(*ma++);
2021 }
2022 }
2023 break;
2024 case TIFF_SLONG8:
2025 {
2026 int64_t *ma;
2027 uint8_t *mb;
2028 uint32_t n;
2029 ma = (int64_t *)origdata;
2030 mb = data;
2031 for (n = 0; n < count; n++)
2032 {
2033 if (tif->tif_flags & TIFF_SWAB)
2034 TIFFSwabLong8((uint64_t *)ma);
2036 if (err != TIFFReadDirEntryErrOk)
2037 break;
2038 *mb++ = (uint8_t)(*ma++);
2039 }
2040 }
2041 break;
2042 default:
2043 break;
2044 }
2045 _TIFFfreeExt(tif, origdata);
2046 if (err != TIFFReadDirEntryErrOk)
2047 {
2048 _TIFFfreeExt(tif, data);
2049 return (err);
2050 }
2051 *value = data;
2052 return (TIFFReadDirEntryErrOk);
2053}
2054
2082static enum TIFFReadDirEntryErr
2083TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
2084{
2085 enum TIFFReadDirEntryErr err;
2086 uint32_t count;
2087 void *origdata;
2088 int8_t *data;
2089 switch (direntry->tdir_type)
2090 {
2091 case TIFF_UNDEFINED:
2092 case TIFF_BYTE:
2093 case TIFF_SBYTE:
2094 case TIFF_SHORT:
2095 case TIFF_SSHORT:
2096 case TIFF_LONG:
2097 case TIFF_SLONG:
2098 case TIFF_LONG8:
2099 case TIFF_SLONG8:
2100 break;
2101 default:
2102 return (TIFFReadDirEntryErrType);
2103 }
2104 err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
2105 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2106 {
2107 *value = 0;
2108 return (err);
2109 }
2110 switch (direntry->tdir_type)
2111 {
2112 case TIFF_UNDEFINED:
2113 case TIFF_BYTE:
2114 {
2115 uint8_t *m;
2116 uint32_t n;
2117 m = (uint8_t *)origdata;
2118 for (n = 0; n < count; n++)
2119 {
2121 if (err != TIFFReadDirEntryErrOk)
2122 {
2123 _TIFFfreeExt(tif, origdata);
2124 return (err);
2125 }
2126 m++;
2127 }
2128 *value = (int8_t *)origdata;
2129 return (TIFFReadDirEntryErrOk);
2130 }
2131 case TIFF_SBYTE:
2132 *value = (int8_t *)origdata;
2133 return (TIFFReadDirEntryErrOk);
2134 default:
2135 break;
2136 }
2137 data = (int8_t *)_TIFFmallocExt(tif, count);
2138 if (data == 0)
2139 {
2140 _TIFFfreeExt(tif, origdata);
2141 return (TIFFReadDirEntryErrAlloc);
2142 }
2143 switch (direntry->tdir_type)
2144 {
2145 case TIFF_SHORT:
2146 {
2147 uint16_t *ma;
2148 int8_t *mb;
2149 uint32_t n;
2150 ma = (uint16_t *)origdata;
2151 mb = data;
2152 for (n = 0; n < count; n++)
2153 {
2154 if (tif->tif_flags & TIFF_SWAB)
2155 TIFFSwabShort(ma);
2157 if (err != TIFFReadDirEntryErrOk)
2158 break;
2159 *mb++ = (int8_t)(*ma++);
2160 }
2161 }
2162 break;
2163 case TIFF_SSHORT:
2164 {
2165 int16_t *ma;
2166 int8_t *mb;
2167 uint32_t n;
2168 ma = (int16_t *)origdata;
2169 mb = data;
2170 for (n = 0; n < count; n++)
2171 {
2172 if (tif->tif_flags & TIFF_SWAB)
2173 TIFFSwabShort((uint16_t *)ma);
2175 if (err != TIFFReadDirEntryErrOk)
2176 break;
2177 *mb++ = (int8_t)(*ma++);
2178 }
2179 }
2180 break;
2181 case TIFF_LONG:
2182 {
2183 uint32_t *ma;
2184 int8_t *mb;
2185 uint32_t n;
2186 ma = (uint32_t *)origdata;
2187 mb = data;
2188 for (n = 0; n < count; n++)
2189 {
2190 if (tif->tif_flags & TIFF_SWAB)
2191 TIFFSwabLong(ma);
2193 if (err != TIFFReadDirEntryErrOk)
2194 break;
2195 *mb++ = (int8_t)(*ma++);
2196 }
2197 }
2198 break;
2199 case TIFF_SLONG:
2200 {
2201 int32_t *ma;
2202 int8_t *mb;
2203 uint32_t n;
2204 ma = (int32_t *)origdata;
2205 mb = data;
2206 for (n = 0; n < count; n++)
2207 {
2208 if (tif->tif_flags & TIFF_SWAB)
2209 TIFFSwabLong((uint32_t *)ma);
2211 if (err != TIFFReadDirEntryErrOk)
2212 break;
2213 *mb++ = (int8_t)(*ma++);
2214 }
2215 }
2216 break;
2217 case TIFF_LONG8:
2218 {
2219 uint64_t *ma;
2220 int8_t *mb;
2221 uint32_t n;
2222 ma = (uint64_t *)origdata;
2223 mb = data;
2224 for (n = 0; n < count; n++)
2225 {
2226 if (tif->tif_flags & TIFF_SWAB)
2227 TIFFSwabLong8(ma);
2229 if (err != TIFFReadDirEntryErrOk)
2230 break;
2231 *mb++ = (int8_t)(*ma++);
2232 }
2233 }
2234 break;
2235 case TIFF_SLONG8:
2236 {
2237 int64_t *ma;
2238 int8_t *mb;
2239 uint32_t n;
2240 ma = (int64_t *)origdata;
2241 mb = data;
2242 for (n = 0; n < count; n++)
2243 {
2244 if (tif->tif_flags & TIFF_SWAB)
2245 TIFFSwabLong8((uint64_t *)ma);
2247 if (err != TIFFReadDirEntryErrOk)
2248 break;
2249 *mb++ = (int8_t)(*ma++);
2250 }
2251 }
2252 break;
2253 default:
2254 break;
2255 }
2256 _TIFFfreeExt(tif, origdata);
2257 if (err != TIFFReadDirEntryErrOk)
2258 {
2259 _TIFFfreeExt(tif, data);
2260 return (err);
2261 }
2262 *value = data;
2263 return (TIFFReadDirEntryErrOk);
2264}
2265
2293static enum TIFFReadDirEntryErr
2294TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
2295{
2296 enum TIFFReadDirEntryErr err;
2297 uint32_t count;
2298 void *origdata;
2299 uint16_t *data;
2300 switch (direntry->tdir_type)
2301 {
2302 case TIFF_BYTE:
2303 case TIFF_SBYTE:
2304 case TIFF_SHORT:
2305 case TIFF_SSHORT:
2306 case TIFF_LONG:
2307 case TIFF_SLONG:
2308 case TIFF_LONG8:
2309 case TIFF_SLONG8:
2310 break;
2311 default:
2312 return (TIFFReadDirEntryErrType);
2313 }
2314 err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
2315 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2316 {
2317 *value = 0;
2318 return (err);
2319 }
2320 switch (direntry->tdir_type)
2321 {
2322 case TIFF_SHORT:
2323 *value = (uint16_t *)origdata;
2324 if (tif->tif_flags & TIFF_SWAB)
2325 TIFFSwabArrayOfShort(*value, count);
2326 return (TIFFReadDirEntryErrOk);
2327 case TIFF_SSHORT:
2328 {
2329 int16_t *m;
2330 uint32_t n;
2331 m = (int16_t *)origdata;
2332 for (n = 0; n < count; n++)
2333 {
2334 if (tif->tif_flags & TIFF_SWAB)
2335 TIFFSwabShort((uint16_t *)m);
2337 if (err != TIFFReadDirEntryErrOk)
2338 {
2339 _TIFFfreeExt(tif, origdata);
2340 return (err);
2341 }
2342 m++;
2343 }
2344 *value = (uint16_t *)origdata;
2345 return (TIFFReadDirEntryErrOk);
2346 }
2347 default:
2348 break;
2349 }
2350 data = (uint16_t *)_TIFFCheckMalloc(tif, count, sizeof(uint16_t),
2351 "TIFFReadDirEntryShortArray");
2352 if (data == 0)
2353 {
2354 _TIFFfreeExt(tif, origdata);
2355 return (TIFFReadDirEntryErrAlloc);
2356 }
2357 switch (direntry->tdir_type)
2358 {
2359 case TIFF_BYTE:
2360 {
2361 uint8_t *ma;
2362 uint16_t *mb;
2363 uint32_t n;
2364 ma = (uint8_t *)origdata;
2365 mb = data;
2366 for (n = 0; n < count; n++)
2367 *mb++ = (uint16_t)(*ma++);
2368 }
2369 break;
2370 case TIFF_SBYTE:
2371 {
2372 int8_t *ma;
2373 uint16_t *mb;
2374 uint32_t n;
2375 ma = (int8_t *)origdata;
2376 mb = data;
2377 for (n = 0; n < count; n++)
2378 {
2380 if (err != TIFFReadDirEntryErrOk)
2381 break;
2382 *mb++ = (uint16_t)(*ma++);
2383 }
2384 }
2385 break;
2386 case TIFF_LONG:
2387 {
2388 uint32_t *ma;
2389 uint16_t *mb;
2390 uint32_t n;
2391 ma = (uint32_t *)origdata;
2392 mb = data;
2393 for (n = 0; n < count; n++)
2394 {
2395 if (tif->tif_flags & TIFF_SWAB)
2396 TIFFSwabLong(ma);
2398 if (err != TIFFReadDirEntryErrOk)
2399 break;
2400 *mb++ = (uint16_t)(*ma++);
2401 }
2402 }
2403 break;
2404 case TIFF_SLONG:
2405 {
2406 int32_t *ma;
2407 uint16_t *mb;
2408 uint32_t n;
2409 ma = (int32_t *)origdata;
2410 mb = data;
2411 for (n = 0; n < count; n++)
2412 {
2413 if (tif->tif_flags & TIFF_SWAB)
2414 TIFFSwabLong((uint32_t *)ma);
2416 if (err != TIFFReadDirEntryErrOk)
2417 break;
2418 *mb++ = (uint16_t)(*ma++);
2419 }
2420 }
2421 break;
2422 case TIFF_LONG8:
2423 {
2424 uint64_t *ma;
2425 uint16_t *mb;
2426 uint32_t n;
2427 ma = (uint64_t *)origdata;
2428 mb = data;
2429 for (n = 0; n < count; n++)
2430 {
2431 if (tif->tif_flags & TIFF_SWAB)
2432 TIFFSwabLong8(ma);
2434 if (err != TIFFReadDirEntryErrOk)
2435 break;
2436 *mb++ = (uint16_t)(*ma++);
2437 }
2438 }
2439 break;
2440 case TIFF_SLONG8:
2441 {
2442 int64_t *ma;
2443 uint16_t *mb;
2444 uint32_t n;
2445 ma = (int64_t *)origdata;
2446 mb = data;
2447 for (n = 0; n < count; n++)
2448 {
2449 if (tif->tif_flags & TIFF_SWAB)
2450 TIFFSwabLong8((uint64_t *)ma);
2452 if (err != TIFFReadDirEntryErrOk)
2453 break;
2454 *mb++ = (uint16_t)(*ma++);
2455 }
2456 }
2457 break;
2458 default:
2459 break;
2460 }
2461 _TIFFfreeExt(tif, origdata);
2462 if (err != TIFFReadDirEntryErrOk)
2463 {
2464 _TIFFfreeExt(tif, data);
2465 return (err);
2466 }
2467 *value = data;
2468 return (TIFFReadDirEntryErrOk);
2469}
2470
2499static enum TIFFReadDirEntryErr
2500TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
2501{
2502 enum TIFFReadDirEntryErr err;
2503 uint32_t count;
2504 void *origdata;
2505 int16_t *data;
2506 switch (direntry->tdir_type)
2507 {
2508 case TIFF_BYTE:
2509 case TIFF_SBYTE:
2510 case TIFF_SHORT:
2511 case TIFF_SSHORT:
2512 case TIFF_LONG:
2513 case TIFF_SLONG:
2514 case TIFF_LONG8:
2515 case TIFF_SLONG8:
2516 break;
2517 default:
2518 return (TIFFReadDirEntryErrType);
2519 }
2520 err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
2521 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2522 {
2523 *value = 0;
2524 return (err);
2525 }
2526 switch (direntry->tdir_type)
2527 {
2528 case TIFF_SHORT:
2529 {
2530 uint16_t *m;
2531 uint32_t n;
2532 m = (uint16_t *)origdata;
2533 for (n = 0; n < count; n++)
2534 {
2535 if (tif->tif_flags & TIFF_SWAB)
2536 TIFFSwabShort(m);
2538 if (err != TIFFReadDirEntryErrOk)
2539 {
2540 _TIFFfreeExt(tif, origdata);
2541 return (err);
2542 }
2543 m++;
2544 }
2545 *value = (int16_t *)origdata;
2546 return (TIFFReadDirEntryErrOk);
2547 }
2548 case TIFF_SSHORT:
2549 *value = (int16_t *)origdata;
2550 if (tif->tif_flags & TIFF_SWAB)
2551 TIFFSwabArrayOfShort((uint16_t *)(*value), count);
2552 return (TIFFReadDirEntryErrOk);
2553 default:
2554 break;
2555 }
2556 data = (int16_t *)_TIFFCheckMalloc(tif, count, sizeof(int16_t),
2557 "TIFFReadDirEntrySshortArray");
2558 if (data == 0)
2559 {
2560 _TIFFfreeExt(tif, origdata);
2561 return (TIFFReadDirEntryErrAlloc);
2562 }
2563 switch (direntry->tdir_type)
2564 {
2565 case TIFF_BYTE:
2566 {
2567 uint8_t *ma;
2568 int16_t *mb;
2569 uint32_t n;
2570 ma = (uint8_t *)origdata;
2571 mb = data;
2572 for (n = 0; n < count; n++)
2573 *mb++ = (int16_t)(*ma++);
2574 }
2575 break;
2576 case TIFF_SBYTE:
2577 {
2578 int8_t *ma;
2579 int16_t *mb;
2580 uint32_t n;
2581 ma = (int8_t *)origdata;
2582 mb = data;
2583 for (n = 0; n < count; n++)
2584 *mb++ = (int16_t)(*ma++);
2585 }
2586 break;
2587 case TIFF_LONG:
2588 {
2589 uint32_t *ma;
2590 int16_t *mb;
2591 uint32_t n;
2592 ma = (uint32_t *)origdata;
2593 mb = data;
2594 for (n = 0; n < count; n++)
2595 {
2596 if (tif->tif_flags & TIFF_SWAB)
2597 TIFFSwabLong(ma);
2599 if (err != TIFFReadDirEntryErrOk)
2600 break;
2601 *mb++ = (int16_t)(*ma++);
2602 }
2603 }
2604 break;
2605 case TIFF_SLONG:
2606 {
2607 int32_t *ma;
2608 int16_t *mb;
2609 uint32_t n;
2610 ma = (int32_t *)origdata;
2611 mb = data;
2612 for (n = 0; n < count; n++)
2613 {
2614 if (tif->tif_flags & TIFF_SWAB)
2615 TIFFSwabLong((uint32_t *)ma);
2617 if (err != TIFFReadDirEntryErrOk)
2618 break;
2619 *mb++ = (int16_t)(*ma++);
2620 }
2621 }
2622 break;
2623 case TIFF_LONG8:
2624 {
2625 uint64_t *ma;
2626 int16_t *mb;
2627 uint32_t n;
2628 ma = (uint64_t *)origdata;
2629 mb = data;
2630 for (n = 0; n < count; n++)
2631 {
2632 if (tif->tif_flags & TIFF_SWAB)
2633 TIFFSwabLong8(ma);
2635 if (err != TIFFReadDirEntryErrOk)
2636 break;
2637 *mb++ = (int16_t)(*ma++);
2638 }
2639 }
2640 break;
2641 case TIFF_SLONG8:
2642 {
2643 int64_t *ma;
2644 int16_t *mb;
2645 uint32_t n;
2646 ma = (int64_t *)origdata;
2647 mb = data;
2648 for (n = 0; n < count; n++)
2649 {
2650 if (tif->tif_flags & TIFF_SWAB)
2651 TIFFSwabLong8((uint64_t *)ma);
2653 if (err != TIFFReadDirEntryErrOk)
2654 break;
2655 *mb++ = (int16_t)(*ma++);
2656 }
2657 }
2658 break;
2659 default:
2660 break;
2661 }
2662 _TIFFfreeExt(tif, origdata);
2663 if (err != TIFFReadDirEntryErrOk)
2664 {
2665 _TIFFfreeExt(tif, data);
2666 return (err);
2667 }
2668 *value = data;
2669 return (TIFFReadDirEntryErrOk);
2670}
2671
2700static enum TIFFReadDirEntryErr
2701TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
2702{
2703 enum TIFFReadDirEntryErr err;
2704 uint32_t count;
2705 void *origdata;
2706 uint32_t *data;
2707 switch (direntry->tdir_type)
2708 {
2709 case TIFF_BYTE:
2710 case TIFF_SBYTE:
2711 case TIFF_SHORT:
2712 case TIFF_SSHORT:
2713 case TIFF_LONG:
2714 case TIFF_SLONG:
2715 case TIFF_LONG8:
2716 case TIFF_SLONG8:
2717 case TIFF_IFD:
2718 case TIFF_IFD8:
2719 break;
2720 default:
2721 return (TIFFReadDirEntryErrType);
2722 }
2723 err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2724 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2725 {
2726 *value = 0;
2727 return (err);
2728 }
2729 switch (direntry->tdir_type)
2730 {
2731 case TIFF_LONG:
2732 case TIFF_IFD:
2733 *value = (uint32_t *)origdata;
2734 if (tif->tif_flags & TIFF_SWAB)
2735 TIFFSwabArrayOfLong(*value, count);
2736 return (TIFFReadDirEntryErrOk);
2737 case TIFF_SLONG:
2738 {
2739 int32_t *m;
2740 uint32_t n;
2741 m = (int32_t *)origdata;
2742 for (n = 0; n < count; n++)
2743 {
2744 if (tif->tif_flags & TIFF_SWAB)
2745 TIFFSwabLong((uint32_t *)m);
2747 if (err != TIFFReadDirEntryErrOk)
2748 {
2749 _TIFFfreeExt(tif, origdata);
2750 return (err);
2751 }
2752 m++;
2753 }
2754 *value = (uint32_t *)origdata;
2755 return (TIFFReadDirEntryErrOk);
2756 }
2757 default:
2758 break;
2759 }
2760 data = (uint32_t *)_TIFFCheckMalloc(tif, count, sizeof(uint32_t),
2761 "TIFFReadDirEntryLongArray");
2762 if (data == 0)
2763 {
2764 _TIFFfreeExt(tif, origdata);
2765 return (TIFFReadDirEntryErrAlloc);
2766 }
2767 switch (direntry->tdir_type)
2768 {
2769 case TIFF_BYTE:
2770 {
2771 uint8_t *ma;
2772 uint32_t *mb;
2773 uint32_t n;
2774 ma = (uint8_t *)origdata;
2775 mb = data;
2776 for (n = 0; n < count; n++)
2777 *mb++ = (uint32_t)(*ma++);
2778 }
2779 break;
2780 case TIFF_SBYTE:
2781 {
2782 int8_t *ma;
2783 uint32_t *mb;
2784 uint32_t n;
2785 ma = (int8_t *)origdata;
2786 mb = data;
2787 for (n = 0; n < count; n++)
2788 {
2790 if (err != TIFFReadDirEntryErrOk)
2791 break;
2792 *mb++ = (uint32_t)(*ma++);
2793 }
2794 }
2795 break;
2796 case TIFF_SHORT:
2797 {
2798 uint16_t *ma;
2799 uint32_t *mb;
2800 uint32_t n;
2801 ma = (uint16_t *)origdata;
2802 mb = data;
2803 for (n = 0; n < count; n++)
2804 {
2805 if (tif->tif_flags & TIFF_SWAB)
2806 TIFFSwabShort(ma);
2807 *mb++ = (uint32_t)(*ma++);
2808 }
2809 }
2810 break;
2811 case TIFF_SSHORT:
2812 {
2813 int16_t *ma;
2814 uint32_t *mb;
2815 uint32_t n;
2816 ma = (int16_t *)origdata;
2817 mb = data;
2818 for (n = 0; n < count; n++)
2819 {
2820 if (tif->tif_flags & TIFF_SWAB)
2821 TIFFSwabShort((uint16_t *)ma);
2823 if (err != TIFFReadDirEntryErrOk)
2824 break;
2825 *mb++ = (uint32_t)(*ma++);
2826 }
2827 }
2828 break;
2829 case TIFF_LONG8:
2830 case TIFF_IFD8:
2831 {
2832 uint64_t *ma;
2833 uint32_t *mb;
2834 uint32_t n;
2835 ma = (uint64_t *)origdata;
2836 mb = data;
2837 for (n = 0; n < count; n++)
2838 {
2839 if (tif->tif_flags & TIFF_SWAB)
2840 TIFFSwabLong8(ma);
2842 if (err != TIFFReadDirEntryErrOk)
2843 break;
2844 *mb++ = (uint32_t)(*ma++);
2845 }
2846 }
2847 break;
2848 case TIFF_SLONG8:
2849 {
2850 int64_t *ma;
2851 uint32_t *mb;
2852 uint32_t n;
2853 ma = (int64_t *)origdata;
2854 mb = data;
2855 for (n = 0; n < count; n++)
2856 {
2857 if (tif->tif_flags & TIFF_SWAB)
2858 TIFFSwabLong8((uint64_t *)ma);
2860 if (err != TIFFReadDirEntryErrOk)
2861 break;
2862 *mb++ = (uint32_t)(*ma++);
2863 }
2864 }
2865 break;
2866 default:
2867 break;
2868 }
2869 _TIFFfreeExt(tif, origdata);
2870 if (err != TIFFReadDirEntryErrOk)
2871 {
2872 _TIFFfreeExt(tif, data);
2873 return (err);
2874 }
2875 *value = data;
2876 return (TIFFReadDirEntryErrOk);
2877}
2878
2907static enum TIFFReadDirEntryErr
2908TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
2909{
2910 enum TIFFReadDirEntryErr err;
2911 uint32_t count;
2912 void *origdata;
2913 int32_t *data;
2914 switch (direntry->tdir_type)
2915 {
2916 case TIFF_BYTE:
2917 case TIFF_SBYTE:
2918 case TIFF_SHORT:
2919 case TIFF_SSHORT:
2920 case TIFF_LONG:
2921 case TIFF_SLONG:
2922 case TIFF_LONG8:
2923 case TIFF_SLONG8:
2924 break;
2925 default:
2926 return (TIFFReadDirEntryErrType);
2927 }
2928 err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2929 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2930 {
2931 *value = 0;
2932 return (err);
2933 }
2934 switch (direntry->tdir_type)
2935 {
2936 case TIFF_LONG:
2937 {
2938 uint32_t *m;
2939 uint32_t n;
2940 m = (uint32_t *)origdata;
2941 for (n = 0; n < count; n++)
2942 {
2943 if (tif->tif_flags & TIFF_SWAB)
2944 TIFFSwabLong((uint32_t *)m);
2946 if (err != TIFFReadDirEntryErrOk)
2947 {
2948 _TIFFfreeExt(tif, origdata);
2949 return (err);
2950 }
2951 m++;
2952 }
2953 *value = (int32_t *)origdata;
2954 return (TIFFReadDirEntryErrOk);
2955 }
2956 case TIFF_SLONG:
2957 *value = (int32_t *)origdata;
2958 if (tif->tif_flags & TIFF_SWAB)
2959 TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2960 return (TIFFReadDirEntryErrOk);
2961 default:
2962 break;
2963 }
2964 data = (int32_t *)_TIFFCheckMalloc(tif, count, sizeof(int32_t),
2965 "TIFFReadDirEntrySlongArray");
2966 if (data == 0)
2967 {
2968 _TIFFfreeExt(tif, origdata);
2969 return (TIFFReadDirEntryErrAlloc);
2970 }
2971 switch (direntry->tdir_type)
2972 {
2973 case TIFF_BYTE:
2974 {
2975 uint8_t *ma;
2976 int32_t *mb;
2977 uint32_t n;
2978 ma = (uint8_t *)origdata;
2979 mb = data;
2980 for (n = 0; n < count; n++)
2981 *mb++ = (int32_t)(*ma++);
2982 }
2983 break;
2984 case TIFF_SBYTE:
2985 {
2986 int8_t *ma;
2987 int32_t *mb;
2988 uint32_t n;
2989 ma = (int8_t *)origdata;
2990 mb = data;
2991 for (n = 0; n < count; n++)
2992 *mb++ = (int32_t)(*ma++);
2993 }
2994 break;
2995 case TIFF_SHORT:
2996 {
2997 uint16_t *ma;
2998 int32_t *mb;
2999 uint32_t n;
3000 ma = (uint16_t *)origdata;
3001 mb = data;
3002 for (n = 0; n < count; n++)
3003 {
3004 if (tif->tif_flags & TIFF_SWAB)
3005 TIFFSwabShort(ma);
3006 *mb++ = (int32_t)(*ma++);
3007 }
3008 }
3009 break;
3010 case TIFF_SSHORT:
3011 {
3012 int16_t *ma;
3013 int32_t *mb;
3014 uint32_t n;
3015 ma = (int16_t *)origdata;
3016 mb = data;
3017 for (n = 0; n < count; n++)
3018 {
3019 if (tif->tif_flags & TIFF_SWAB)
3020 TIFFSwabShort((uint16_t *)ma);
3021 *mb++ = (int32_t)(*ma++);
3022 }
3023 }
3024 break;
3025 case TIFF_LONG8:
3026 {
3027 uint64_t *ma;
3028 int32_t *mb;
3029 uint32_t n;
3030 ma = (uint64_t *)origdata;
3031 mb = data;
3032 for (n = 0; n < count; n++)
3033 {
3034 if (tif->tif_flags & TIFF_SWAB)
3035 TIFFSwabLong8(ma);
3037 if (err != TIFFReadDirEntryErrOk)
3038 break;
3039 *mb++ = (int32_t)(*ma++);
3040 }
3041 }
3042 break;
3043 case TIFF_SLONG8:
3044 {
3045 int64_t *ma;
3046 int32_t *mb;
3047 uint32_t n;
3048 ma = (int64_t *)origdata;
3049 mb = data;
3050 for (n = 0; n < count; n++)
3051 {
3052 if (tif->tif_flags & TIFF_SWAB)
3053 TIFFSwabLong8((uint64_t *)ma);
3055 if (err != TIFFReadDirEntryErrOk)
3056 break;
3057 *mb++ = (int32_t)(*ma++);
3058 }
3059 }
3060 break;
3061 default:
3062 break;
3063 }
3064 _TIFFfreeExt(tif, origdata);
3065 if (err != TIFFReadDirEntryErrOk)
3066 {
3067 _TIFFfreeExt(tif, data);
3068 return (err);
3069 }
3070 *value = data;
3071 return (TIFFReadDirEntryErrOk);
3072}
3073
3105static enum TIFFReadDirEntryErr
3107 uint64_t **value, uint64_t maxcount)
3108{
3109 enum TIFFReadDirEntryErr err;
3110 uint32_t count;
3111 void *origdata;
3112 uint64_t *data;
3113 switch (direntry->tdir_type)
3114 {
3115 case TIFF_BYTE:
3116 case TIFF_SBYTE:
3117 case TIFF_SHORT:
3118 case TIFF_SSHORT:
3119 case TIFF_LONG:
3120 case TIFF_SLONG:
3121 case TIFF_LONG8:
3122 case TIFF_SLONG8:
3123 case TIFF_IFD:
3124 case TIFF_IFD8:
3125 break;
3126 default:
3127 return (TIFFReadDirEntryErrType);
3128 }
3129 err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
3130 maxcount);
3131 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3132 {
3133 *value = 0;
3134 return (err);
3135 }
3136 switch (direntry->tdir_type)
3137 {
3138 case TIFF_LONG8:
3139 case TIFF_IFD8:
3140 *value = (uint64_t *)origdata;
3141 if (tif->tif_flags & TIFF_SWAB)
3142 TIFFSwabArrayOfLong8(*value, count);
3143 return (TIFFReadDirEntryErrOk);
3144 case TIFF_SLONG8:
3145 {
3146 int64_t *m;
3147 uint32_t n;
3148 m = (int64_t *)origdata;
3149 for (n = 0; n < count; n++)
3150 {
3151 if (tif->tif_flags & TIFF_SWAB)
3152 TIFFSwabLong8((uint64_t *)m);
3154 if (err != TIFFReadDirEntryErrOk)
3155 {
3156 _TIFFfreeExt(tif, origdata);
3157 return (err);
3158 }
3159 m++;
3160 }
3161 *value = (uint64_t *)origdata;
3162 return (TIFFReadDirEntryErrOk);
3163 }
3164 default:
3165 break;
3166 }
3167 data = (uint64_t *)_TIFFCheckMalloc(tif, count, sizeof(uint64_t),
3168 "TIFFReadDirEntryLong8ArrayWithLimit");
3169 if (data == 0)
3170 {
3171 _TIFFfreeExt(tif, origdata);
3172 return (TIFFReadDirEntryErrAlloc);
3173 }
3174 switch (direntry->tdir_type)
3175 {
3176 case TIFF_BYTE:
3177 {
3178 uint8_t *ma;
3179 uint64_t *mb;
3180 uint32_t n;
3181 ma = (uint8_t *)origdata;
3182 mb = data;
3183 for (n = 0; n < count; n++)
3184 *mb++ = (uint64_t)(*ma++);
3185 }
3186 break;
3187 case TIFF_SBYTE:
3188 {
3189 int8_t *ma;
3190 uint64_t *mb;
3191 uint32_t n;
3192 ma = (int8_t *)origdata;
3193 mb = data;
3194 for (n = 0; n < count; n++)
3195 {
3197 if (err != TIFFReadDirEntryErrOk)
3198 break;
3199 *mb++ = (uint64_t)(*ma++);
3200 }
3201 }
3202 break;
3203 case TIFF_SHORT:
3204 {
3205 uint16_t *ma;
3206 uint64_t *mb;
3207 uint32_t n;
3208 ma = (uint16_t *)origdata;
3209 mb = data;
3210 for (n = 0; n < count; n++)
3211 {
3212 if (tif->tif_flags & TIFF_SWAB)
3213 TIFFSwabShort(ma);
3214 *mb++ = (uint64_t)(*ma++);
3215 }
3216 }
3217 break;
3218 case TIFF_SSHORT:
3219 {
3220 int16_t *ma;
3221 uint64_t *mb;
3222 uint32_t n;
3223 ma = (int16_t *)origdata;
3224 mb = data;
3225 for (n = 0; n < count; n++)
3226 {
3227 if (tif->tif_flags & TIFF_SWAB)
3228 TIFFSwabShort((uint16_t *)ma);
3230 if (err != TIFFReadDirEntryErrOk)
3231 break;
3232 *mb++ = (uint64_t)(*ma++);
3233 }
3234 }
3235 break;
3236 case TIFF_LONG:
3237 case TIFF_IFD:
3238 {
3239 uint32_t *ma;
3240 uint64_t *mb;
3241 uint32_t n;
3242 ma = (uint32_t *)origdata;
3243 mb = data;
3244 for (n = 0; n < count; n++)
3245 {
3246 if (tif->tif_flags & TIFF_SWAB)
3247 TIFFSwabLong(ma);
3248 *mb++ = (uint64_t)(*ma++);
3249 }
3250 }
3251 break;
3252 case TIFF_SLONG:
3253 {
3254 int32_t *ma;
3255 uint64_t *mb;
3256 uint32_t n;
3257 ma = (int32_t *)origdata;
3258 mb = data;
3259 for (n = 0; n < count; n++)
3260 {
3261 if (tif->tif_flags & TIFF_SWAB)
3262 TIFFSwabLong((uint32_t *)ma);
3264 if (err != TIFFReadDirEntryErrOk)
3265 break;
3266 *mb++ = (uint64_t)(*ma++);
3267 }
3268 }
3269 break;
3270 default:
3271 break;
3272 }
3273 _TIFFfreeExt(tif, origdata);
3274 if (err != TIFFReadDirEntryErrOk)
3275 {
3276 _TIFFfreeExt(tif, data);
3277 return (err);
3278 }
3279 *value = data;
3280 return (TIFFReadDirEntryErrOk);
3281}
3282
3302static enum TIFFReadDirEntryErr
3303TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
3304{
3305 return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
3306 ~((uint64_t)0));
3307}
3308
3336static enum TIFFReadDirEntryErr
3337TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
3338{
3339 enum TIFFReadDirEntryErr err;
3340 uint32_t count;
3341 void *origdata;
3342 int64_t *data;
3343 switch (direntry->tdir_type)
3344 {
3345 case TIFF_BYTE:
3346 case TIFF_SBYTE:
3347 case TIFF_SHORT:
3348 case TIFF_SSHORT:
3349 case TIFF_LONG:
3350 case TIFF_SLONG:
3351 case TIFF_LONG8:
3352 case TIFF_SLONG8:
3353 break;
3354 default:
3355 return (TIFFReadDirEntryErrType);
3356 }
3357 err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3358 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3359 {
3360 *value = 0;
3361 return (err);
3362 }
3363 switch (direntry->tdir_type)
3364 {
3365 case TIFF_LONG8:
3366 {
3367 uint64_t *m;
3368 uint32_t n;
3369 m = (uint64_t *)origdata;
3370 for (n = 0; n < count; n++)
3371 {
3372 if (tif->tif_flags & TIFF_SWAB)
3373 TIFFSwabLong8(m);
3375 if (err != TIFFReadDirEntryErrOk)
3376 {
3377 _TIFFfreeExt(tif, origdata);
3378 return (err);
3379 }
3380 m++;
3381 }
3382 *value = (int64_t *)origdata;
3383 return (TIFFReadDirEntryErrOk);
3384 }
3385 case TIFF_SLONG8:
3386 *value = (int64_t *)origdata;
3387 if (tif->tif_flags & TIFF_SWAB)
3388 TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
3389 return (TIFFReadDirEntryErrOk);
3390 default:
3391 break;
3392 }
3393 data = (int64_t *)_TIFFCheckMalloc(tif, count, sizeof(int64_t),
3394 "TIFFReadDirEntrySlong8Array");
3395 if (data == 0)
3396 {
3397 _TIFFfreeExt(tif, origdata);
3398 return (TIFFReadDirEntryErrAlloc);
3399 }
3400 switch (direntry->tdir_type)
3401 {
3402 case TIFF_BYTE:
3403 {
3404 uint8_t *ma;
3405 int64_t *mb;
3406 uint32_t n;
3407 ma = (uint8_t *)origdata;
3408 mb = data;
3409 for (n = 0; n < count; n++)
3410 *mb++ = (int64_t)(*ma++);
3411 }
3412 break;
3413 case TIFF_SBYTE:
3414 {
3415 int8_t *ma;
3416 int64_t *mb;
3417 uint32_t n;
3418 ma = (int8_t *)origdata;
3419 mb = data;
3420 for (n = 0; n < count; n++)
3421 *mb++ = (int64_t)(*ma++);
3422 }
3423 break;
3424 case TIFF_SHORT:
3425 {
3426 uint16_t *ma;
3427 int64_t *mb;
3428 uint32_t n;
3429 ma = (uint16_t *)origdata;
3430 mb = data;
3431 for (n = 0; n < count; n++)
3432 {
3433 if (tif->tif_flags & TIFF_SWAB)
3434 TIFFSwabShort(ma);
3435 *mb++ = (int64_t)(*ma++);
3436 }
3437 }
3438 break;
3439 case TIFF_SSHORT:
3440 {
3441 int16_t *ma;
3442 int64_t *mb;
3443 uint32_t n;
3444 ma = (int16_t *)origdata;
3445 mb = data;
3446 for (n = 0; n < count; n++)
3447 {
3448 if (tif->tif_flags & TIFF_SWAB)
3449 TIFFSwabShort((uint16_t *)ma);
3450 *mb++ = (int64_t)(*ma++);
3451 }
3452 }
3453 break;
3454 case TIFF_LONG:
3455 {
3456 uint32_t *ma;
3457 int64_t *mb;
3458 uint32_t n;
3459 ma = (uint32_t *)origdata;
3460 mb = data;
3461 for (n = 0; n < count; n++)
3462 {
3463 if (tif->tif_flags & TIFF_SWAB)
3464 TIFFSwabLong(ma);
3465 *mb++ = (int64_t)(*ma++);
3466 }
3467 }
3468 break;
3469 case TIFF_SLONG:
3470 {
3471 int32_t *ma;
3472 int64_t *mb;
3473 uint32_t n;
3474 ma = (int32_t *)origdata;
3475 mb = data;
3476 for (n = 0; n < count; n++)
3477 {
3478 if (tif->tif_flags & TIFF_SWAB)
3479 TIFFSwabLong((uint32_t *)ma);
3480 *mb++ = (int64_t)(*ma++);
3481 }
3482 }
3483 break;
3484 default:
3485 break;
3486 }
3487 _TIFFfreeExt(tif, origdata);
3488 *value = data;
3489 return (TIFFReadDirEntryErrOk);
3490}
3491
3524static enum TIFFReadDirEntryErr
3525TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
3526{
3527 enum TIFFReadDirEntryErr err;
3528 uint32_t count;
3529 void *origdata;
3530 float *data;
3531 switch (direntry->tdir_type)
3532 {
3533 case TIFF_BYTE:
3534 case TIFF_SBYTE:
3535 case TIFF_SHORT:
3536 case TIFF_SSHORT:
3537 case TIFF_LONG:
3538 case TIFF_SLONG:
3539 case TIFF_LONG8:
3540 case TIFF_SLONG8:
3541 case TIFF_RATIONAL:
3542 case TIFF_SRATIONAL:
3543 case TIFF_FLOAT:
3544 case TIFF_DOUBLE:
3545 break;
3546 default:
3547 return (TIFFReadDirEntryErrType);
3548 }
3549 err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
3550 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3551 {
3552 *value = 0;
3553 return (err);
3554 }
3555 switch (direntry->tdir_type)
3556 {
3557 case TIFF_FLOAT:
3558 if (tif->tif_flags & TIFF_SWAB)
3559 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3560 TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3561 *value = (float *)origdata;
3562 return (TIFFReadDirEntryErrOk);
3563 default:
3564 break;
3565 }
3566 data = (float *)_TIFFCheckMalloc(tif, count, sizeof(float),
3567 "TIFFReadDirEntryFloatArray");
3568 if (data == 0)
3569 {
3570 _TIFFfreeExt(tif, origdata);
3571 return (TIFFReadDirEntryErrAlloc);
3572 }
3573 switch (direntry->tdir_type)
3574 {
3575 case TIFF_BYTE:
3576 {
3577 uint8_t *ma;
3578 float *mb;
3579 uint32_t n;
3580 ma = (uint8_t *)origdata;
3581 mb = data;
3582 for (n = 0; n < count; n++)
3583 *mb++ = (float)(*ma++);
3584 }
3585 break;
3586 case TIFF_SBYTE:
3587 {
3588 int8_t *ma;
3589 float *mb;
3590 uint32_t n;
3591 ma = (int8_t *)origdata;
3592 mb = data;
3593 for (n = 0; n < count; n++)
3594 *mb++ = (float)(*ma++);
3595 }
3596 break;
3597 case TIFF_SHORT:
3598 {
3599 uint16_t *ma;
3600 float *mb;
3601 uint32_t n;
3602 ma = (uint16_t *)origdata;
3603 mb = data;
3604 for (n = 0; n < count; n++)
3605 {
3606 if (tif->tif_flags & TIFF_SWAB)
3607 TIFFSwabShort(ma);
3608 *mb++ = (float)(*ma++);
3609 }
3610 }
3611 break;
3612 case TIFF_SSHORT:
3613 {
3614 int16_t *ma;
3615 float *mb;
3616 uint32_t n;
3617 ma = (int16_t *)origdata;
3618 mb = data;
3619 for (n = 0; n < count; n++)
3620 {
3621 if (tif->tif_flags & TIFF_SWAB)
3622 TIFFSwabShort((uint16_t *)ma);
3623 *mb++ = (float)(*ma++);
3624 }
3625 }
3626 break;
3627 case TIFF_LONG:
3628 {
3629 uint32_t *ma;
3630 float *mb;
3631 uint32_t n;
3632 ma = (uint32_t *)origdata;
3633 mb = data;
3634 for (n = 0; n < count; n++)
3635 {
3636 if (tif->tif_flags & TIFF_SWAB)
3637 TIFFSwabLong(ma);
3638 *mb++ = (float)(*ma++);
3639 }
3640 }
3641 break;
3642 case TIFF_SLONG:
3643 {
3644 int32_t *ma;
3645 float *mb;
3646 uint32_t n;
3647 ma = (int32_t *)origdata;
3648 mb = data;
3649 for (n = 0; n < count; n++)
3650 {
3651 if (tif->tif_flags & TIFF_SWAB)
3652 TIFFSwabLong((uint32_t *)ma);
3653 *mb++ = (float)(*ma++);
3654 }
3655 }
3656 break;
3657 case TIFF_LONG8:
3658 {
3659 uint64_t *ma;
3660 float *mb;
3661 uint32_t n;
3662 ma = (uint64_t *)origdata;
3663 mb = data;
3664 for (n = 0; n < count; n++)
3665 {
3666 if (tif->tif_flags & TIFF_SWAB)
3667 TIFFSwabLong8(ma);
3668 *mb++ = (float)(*ma++);
3669 }
3670 }
3671 break;
3672 case TIFF_SLONG8:
3673 {
3674 int64_t *ma;
3675 float *mb;
3676 uint32_t n;
3677 ma = (int64_t *)origdata;
3678 mb = data;
3679 for (n = 0; n < count; n++)
3680 {
3681 if (tif->tif_flags & TIFF_SWAB)
3682 TIFFSwabLong8((uint64_t *)ma);
3683 *mb++ = (float)(*ma++);
3684 }
3685 }
3686 break;
3687 case TIFF_RATIONAL:
3688 {
3689 uint32_t *ma;
3690 uint32_t maa;
3691 uint32_t mab;
3692 float *mb;
3693 uint32_t n;
3694 ma = (uint32_t *)origdata;
3695 mb = data;
3696 for (n = 0; n < count; n++)
3697 {
3698 if (tif->tif_flags & TIFF_SWAB)
3699 TIFFSwabLong(ma);
3700 maa = *ma++;
3701 if (tif->tif_flags & TIFF_SWAB)
3702 TIFFSwabLong(ma);
3703 mab = *ma++;
3704 if (mab == 0)
3705 *mb++ = 0.0;
3706 else
3707 *mb++ = (float)maa / (float)mab;
3708 }
3709 }
3710 break;
3711 case TIFF_SRATIONAL:
3712 {
3713 uint32_t *ma;
3714 int32_t maa;
3715 uint32_t mab;
3716 float *mb;
3717 uint32_t n;
3718 ma = (uint32_t *)origdata;
3719 mb = data;
3720 for (n = 0; n < count; n++)
3721 {
3722 if (tif->tif_flags & TIFF_SWAB)
3723 TIFFSwabLong(ma);
3724 maa = *(int32_t *)ma;
3725 ma++;
3726 if (tif->tif_flags & TIFF_SWAB)
3727 TIFFSwabLong(ma);
3728 mab = *ma++;
3729 if (mab == 0)
3730 *mb++ = 0.0;
3731 else
3732 *mb++ = (float)maa / (float)mab;
3733 }
3734 }
3735 break;
3736 case TIFF_DOUBLE:
3737 {
3738 double *ma;
3739 float *mb;
3740 uint32_t n;
3741 if (tif->tif_flags & TIFF_SWAB)
3742 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3743 TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3744 ma = (double *)origdata;
3745 mb = data;
3746 for (n = 0; n < count; n++)
3747 {
3748 double val = *ma++;
3749 if (val > (double)FLT_MAX)
3750 val = (double)FLT_MAX;
3751 else if (val < -(double)FLT_MAX)
3752 val = -(double)FLT_MAX;
3753 *mb++ = (float)val;
3754 }
3755 }
3756 break;
3757 default:
3758 break;
3759 }
3760 _TIFFfreeExt(tif, origdata);
3761 *value = data;
3762 return (TIFFReadDirEntryErrOk);
3763}
3764
3795static enum TIFFReadDirEntryErr
3796TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
3797{
3798 enum TIFFReadDirEntryErr err;
3799 uint32_t count;
3800 void *origdata;
3801 double *data;
3802 switch (direntry->tdir_type)
3803 {
3804 case TIFF_BYTE:
3805 case TIFF_SBYTE:
3806 case TIFF_SHORT:
3807 case TIFF_SSHORT:
3808 case TIFF_LONG:
3809 case TIFF_SLONG:
3810 case TIFF_LONG8:
3811 case TIFF_SLONG8:
3812 case TIFF_RATIONAL:
3813 case TIFF_SRATIONAL:
3814 case TIFF_FLOAT:
3815 case TIFF_DOUBLE:
3816 break;
3817 default:
3818 return (TIFFReadDirEntryErrType);
3819 }
3820 err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3821 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3822 {
3823 *value = 0;
3824 return (err);
3825 }
3826 switch (direntry->tdir_type)
3827 {
3828 case TIFF_DOUBLE:
3829 if (tif->tif_flags & TIFF_SWAB)
3830 TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3831 TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3832 *value = (double *)origdata;
3833 return (TIFFReadDirEntryErrOk);
3834 default:
3835 break;
3836 }
3837 data = (double *)_TIFFmallocExt(tif,
3838 (tmsize_t)((size_t)count * sizeof(double)));
3839 if (data == 0)
3840 {
3841 _TIFFfreeExt(tif, origdata);
3842 return (TIFFReadDirEntryErrAlloc);
3843 }
3844 switch (direntry->tdir_type)
3845 {
3846 case TIFF_BYTE:
3847 {
3848 uint8_t *ma;
3849 double *mb;
3850 uint32_t n;
3851 ma = (uint8_t *)origdata;
3852 mb = data;
3853 for (n = 0; n < count; n++)
3854 *mb++ = (double)(*ma++);
3855 }
3856 break;
3857 case TIFF_SBYTE:
3858 {
3859 int8_t *ma;
3860 double *mb;
3861 uint32_t n;
3862 ma = (int8_t *)origdata;
3863 mb = data;
3864 for (n = 0; n < count; n++)
3865 *mb++ = (double)(*ma++);
3866 }
3867 break;
3868 case TIFF_SHORT:
3869 {
3870 uint16_t *ma;
3871 double *mb;
3872 uint32_t n;
3873 ma = (uint16_t *)origdata;
3874 mb = data;
3875 for (n = 0; n < count; n++)
3876 {
3877 if (tif->tif_flags & TIFF_SWAB)
3878 TIFFSwabShort(ma);
3879 *mb++ = (double)(*ma++);
3880 }
3881 }
3882 break;
3883 case TIFF_SSHORT:
3884 {
3885 int16_t *ma;
3886 double *mb;
3887 uint32_t n;
3888 ma = (int16_t *)origdata;
3889 mb = data;
3890 for (n = 0; n < count; n++)
3891 {
3892 if (tif->tif_flags & TIFF_SWAB)
3893 TIFFSwabShort((uint16_t *)ma);
3894 *mb++ = (double)(*ma++);
3895 }
3896 }
3897 break;
3898 case TIFF_LONG:
3899 {
3900 uint32_t *ma;
3901 double *mb;
3902 uint32_t n;
3903 ma = (uint32_t *)origdata;
3904 mb = data;
3905 for (n = 0; n < count; n++)
3906 {
3907 if (tif->tif_flags & TIFF_SWAB)
3908 TIFFSwabLong(ma);
3909 *mb++ = (double)(*ma++);
3910 }
3911 }
3912 break;
3913 case TIFF_SLONG:
3914 {
3915 int32_t *ma;
3916 double *mb;
3917 uint32_t n;
3918 ma = (int32_t *)origdata;
3919 mb = data;
3920 for (n = 0; n < count; n++)
3921 {
3922 if (tif->tif_flags & TIFF_SWAB)
3923 TIFFSwabLong((uint32_t *)ma);
3924 *mb++ = (double)(*ma++);
3925 }
3926 }
3927 break;
3928 case TIFF_LONG8:
3929 {
3930 uint64_t *ma;
3931 double *mb;
3932 uint32_t n;
3933 ma = (uint64_t *)origdata;
3934 mb = data;
3935 for (n = 0; n < count; n++)
3936 {
3937 if (tif->tif_flags & TIFF_SWAB)
3938 TIFFSwabLong8(ma);
3939 *mb++ = (double)(*ma++);
3940 }
3941 }
3942 break;
3943 case TIFF_SLONG8:
3944 {
3945 int64_t *ma;
3946 double *mb;
3947 uint32_t n;
3948 ma = (int64_t *)origdata;
3949 mb = data;
3950 for (n = 0; n < count; n++)
3951 {
3952 if (tif->tif_flags & TIFF_SWAB)
3953 TIFFSwabLong8((uint64_t *)ma);
3954 *mb++ = (double)(*ma++);
3955 }
3956 }
3957 break;
3958 case TIFF_RATIONAL:
3959 {
3960 uint32_t *ma;
3961 uint32_t maa;
3962 uint32_t mab;
3963 double *mb;
3964 uint32_t n;
3965 ma = (uint32_t *)origdata;
3966 mb = data;
3967 for (n = 0; n < count; n++)
3968 {
3969 if (tif->tif_flags & TIFF_SWAB)
3970 TIFFSwabLong(ma);
3971 maa = *ma++;
3972 if (tif->tif_flags & TIFF_SWAB)
3973 TIFFSwabLong(ma);
3974 mab = *ma++;
3975 if (mab == 0)
3976 *mb++ = 0.0;
3977 else
3978 *mb++ = (double)maa / (double)mab;
3979 }
3980 }
3981 break;
3982 case TIFF_SRATIONAL:
3983 {
3984 uint32_t *ma;
3985 int32_t maa;
3986 uint32_t mab;
3987 double *mb;
3988 uint32_t n;
3989 ma = (uint32_t *)origdata;
3990 mb = data;
3991 for (n = 0; n < count; n++)
3992 {
3993 if (tif->tif_flags & TIFF_SWAB)
3994 TIFFSwabLong(ma);
3995 maa = *(int32_t *)ma;
3996 ma++;
3997 if (tif->tif_flags & TIFF_SWAB)
3998 TIFFSwabLong(ma);
3999 mab = *ma++;
4000 if (mab == 0)
4001 *mb++ = 0.0;
4002 else
4003 *mb++ = (double)maa / (double)mab;
4004 }
4005 }
4006 break;
4007 case TIFF_FLOAT:
4008 {
4009 float *ma;
4010 double *mb;
4011 uint32_t n;
4012 if (tif->tif_flags & TIFF_SWAB)
4013 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
4014 TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
4015 ma = (float *)origdata;
4016 mb = data;
4017 for (n = 0; n < count; n++)
4018 *mb++ = (double)(*ma++);
4019 }
4020 break;
4021 default:
4022 break;
4023 }
4024 _TIFFfreeExt(tif, origdata);
4025 *value = data;
4026 return (TIFFReadDirEntryErrOk);
4027}
4028
4054static enum TIFFReadDirEntryErr
4055TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
4056{
4057 enum TIFFReadDirEntryErr err;
4058 uint32_t count;
4059 void *origdata;
4060 uint64_t *data;
4061 switch (direntry->tdir_type)
4062 {
4063 case TIFF_LONG:
4064 case TIFF_LONG8:
4065 case TIFF_IFD:
4066 case TIFF_IFD8:
4067 break;
4068 default:
4069 return (TIFFReadDirEntryErrType);
4070 }
4071 err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
4072 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
4073 {
4074 *value = 0;
4075 return (err);
4076 }
4077 switch (direntry->tdir_type)
4078 {
4079 case TIFF_LONG8:
4080 case TIFF_IFD8:
4081 *value = (uint64_t *)origdata;
4082 if (tif->tif_flags & TIFF_SWAB)
4083 TIFFSwabArrayOfLong8(*value, count);
4084 return (TIFFReadDirEntryErrOk);
4085 default:
4086 break;
4087 }
4088 data = (uint64_t *)_TIFFCheckMalloc(tif, count, sizeof(uint64_t),
4089 "TIFFReadDirEntryIfd8Array");
4090 if (data == 0)
4091 {
4092 _TIFFfreeExt(tif, origdata);
4093 return (TIFFReadDirEntryErrAlloc);
4094 }
4095 switch (direntry->tdir_type)
4096 {
4097 case TIFF_LONG:
4098 case TIFF_IFD:
4099 {
4100 uint32_t *ma;
4101 uint64_t *mb;
4102 uint32_t n;
4103 ma = (uint32_t *)origdata;
4104 mb = data;
4105 for (n = 0; n < count; n++)
4106 {
4107 if (tif->tif_flags & TIFF_SWAB)
4108 TIFFSwabLong(ma);
4109 *mb++ = (uint64_t)(*ma++);
4110 }
4111 }
4112 break;
4113 default:
4114 break;
4115 }
4116 _TIFFfreeExt(tif, origdata);
4117 *value = data;
4118 return (TIFFReadDirEntryErrOk);
4119}
4120
4145static enum TIFFReadDirEntryErr
4147 uint16_t *value)
4148{
4149 enum TIFFReadDirEntryErr err;
4150 uint16_t *m;
4151 uint16_t *na;
4152 uint16_t nb;
4153 if (direntry->tdir_count != (uint64_t)tif->tif_dir.td_samplesperpixel)
4154 {
4155 const TIFFField *fip = TIFFFieldWithTag(tif, direntry->tdir_tag);
4156 if (direntry->tdir_count == 0)
4157 {
4159 }
4160 else if (direntry->tdir_count <
4161 (uint64_t)tif->tif_dir.td_samplesperpixel)
4162 {
4164 tif, "TIFFReadDirEntryPersampleShort",
4165 "Tag %s entry count is %" PRIu64
4166 " , whereas it should be SamplesPerPixel=%d. Assuming that "
4167 "missing entries are all at the value of the first one",
4168 fip ? fip->field_name : "unknown tagname", direntry->tdir_count,
4170 }
4171 else
4172 {
4173 TIFFWarningExtR(tif, "TIFFReadDirEntryPersampleShort",
4174 "Tag %s entry count is %" PRIu64
4175 " , whereas it should be SamplesPerPixel=%d. "
4176 "Ignoring extra entries",
4177 fip ? fip->field_name : "unknown tagname",
4178 direntry->tdir_count,
4180 }
4181 }
4182 err = TIFFReadDirEntryShortArray(tif, direntry, &m);
4183 if (err != TIFFReadDirEntryErrOk || m == NULL)
4184 return (err);
4185 na = m;
4186 nb = tif->tif_dir.td_samplesperpixel;
4187 if (direntry->tdir_count < nb)
4188 nb = (uint16_t)direntry->tdir_count;
4189 *value = *na++;
4190 nb--;
4191 while (nb > 0)
4192 {
4193 if (*na++ != *value)
4194 {
4196 break;
4197 }
4198 nb--;
4199 }
4200 _TIFFfreeExt(tif, m);
4201 return (err);
4202}
4203
4220 uint8_t *value)
4221{
4222 (void)tif;
4223 *value = *(uint8_t *)(&direntry->tdir_offset);
4224}
4225
4242 int8_t *value)
4243{
4244 (void)tif;
4245 *value = *(int8_t *)(&direntry->tdir_offset);
4246}
4247
4265 uint16_t *value)
4266{
4267 *value = direntry->tdir_offset.toff_short;
4268 /* *value=*(uint16_t*)(&direntry->tdir_offset); */
4269 if (tif->tif_flags & TIFF_SWAB)
4270 TIFFSwabShort(value);
4271}
4272
4290 int16_t *value)
4291{
4292 *value = *(int16_t *)(&direntry->tdir_offset);
4293 if (tif->tif_flags & TIFF_SWAB)
4294 TIFFSwabShort((uint16_t *)value);
4295}
4296
4314 uint32_t *value)
4315{
4316 *value = *(uint32_t *)(&direntry->tdir_offset);
4317 if (tif->tif_flags & TIFF_SWAB)
4318 TIFFSwabLong(value);
4319}
4320
4338 int32_t *value)
4339{
4340 *value = *(int32_t *)(&direntry->tdir_offset);
4341 if (tif->tif_flags & TIFF_SWAB)
4342 TIFFSwabLong((uint32_t *)value);
4343}
4344
4365static enum TIFFReadDirEntryErr
4366TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
4367{
4368 if (!(tif->tif_flags & TIFF_BIGTIFF))
4369 {
4370 enum TIFFReadDirEntryErr err;
4371 uint32_t offset = direntry->tdir_offset.toff_long;
4372 if (tif->tif_flags & TIFF_SWAB)
4373 TIFFSwabLong(&offset);
4374 err = TIFFReadDirEntryData(tif, offset, 8, value);
4375 if (err != TIFFReadDirEntryErrOk)
4376 return (err);
4377 }
4378 else
4379 *value = direntry->tdir_offset.toff_long8;
4380 if (tif->tif_flags & TIFF_SWAB)
4381 TIFFSwabLong8(value);
4382 return (TIFFReadDirEntryErrOk);
4383}
4384
4405static enum TIFFReadDirEntryErr
4406TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
4407{
4408 if (!(tif->tif_flags & TIFF_BIGTIFF))
4409 {
4410 enum TIFFReadDirEntryErr err;
4411 uint32_t offset = direntry->tdir_offset.toff_long;
4412 if (tif->tif_flags & TIFF_SWAB)
4413 TIFFSwabLong(&offset);
4414 err = TIFFReadDirEntryData(tif, offset, 8, value);
4415 if (err != TIFFReadDirEntryErrOk)
4416 return (err);
4417 }
4418 else
4419 *value = *(int64_t *)(&direntry->tdir_offset);
4420 if (tif->tif_flags & TIFF_SWAB)
4421 TIFFSwabLong8((uint64_t *)value);
4422 return (TIFFReadDirEntryErrOk);
4423}
4424
4427 double *value)
4428{
4430
4431 assert(sizeof(double) == 8);
4432 assert(sizeof(uint64_t) == 8);
4433 assert(sizeof(uint32_t) == 4);
4434 if (!(tif->tif_flags & TIFF_BIGTIFF))
4435 {
4436 enum TIFFReadDirEntryErr err;
4437 uint32_t offset = direntry->tdir_offset.toff_long;
4438 if (tif->tif_flags & TIFF_SWAB)
4439 TIFFSwabLong(&offset);
4440 err = TIFFReadDirEntryData(tif, offset, 8, m.i);
4441 if (err != TIFFReadDirEntryErrOk)
4442 return (err);
4443 }
4444 else
4445 m.l = direntry->tdir_offset.toff_long8;
4446 if (tif->tif_flags & TIFF_SWAB)
4447 TIFFSwabArrayOfLong(m.i, 2);
4448 /* Not completely sure what we should do when m.i[1]==0, but some */
4449 /* sanitizers do not like division by 0.0: */
4450 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
4451 if (m.i[0] == 0 || m.i[1] == 0)
4452 *value = 0.0;
4453 else
4454 *value = (double)m.i[0] / (double)m.i[1];
4455 return (TIFFReadDirEntryErrOk);
4456}
4457
4479static enum TIFFReadDirEntryErr
4481 double *value)
4482{
4484 assert(sizeof(double) == 8);
4485 assert(sizeof(uint64_t) == 8);
4486 assert(sizeof(int32_t) == 4);
4487 assert(sizeof(uint32_t) == 4);
4488 if (!(tif->tif_flags & TIFF_BIGTIFF))
4489 {
4490 enum TIFFReadDirEntryErr err;
4491 uint32_t offset = direntry->tdir_offset.toff_long;
4492 if (tif->tif_flags & TIFF_SWAB)
4493 TIFFSwabLong(&offset);
4494 err = TIFFReadDirEntryData(tif, offset, 8, m.i);
4495 if (err != TIFFReadDirEntryErrOk)
4496 return (err);
4497 }
4498 else
4499 m.l = direntry->tdir_offset.toff_long8;
4500 if (tif->tif_flags & TIFF_SWAB)
4501 TIFFSwabArrayOfLong(m.i, 2);
4502 /* Not completely sure what we should do when m.i[1]==0, but some */
4503 /* sanitizers do not like division by 0.0: */
4504 /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
4505 if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
4506 *value = 0.0;
4507 else
4508 *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
4509 return (TIFFReadDirEntryErrOk);
4510}
4511
4512#if 0
4533static enum TIFFReadDirEntryErr
4534TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
4535 TIFFRational_t *value)
4536{ /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
4537 directly --*/
4539
4540 assert(sizeof(double) == 8);
4541 assert(sizeof(uint64_t) == 8);
4542 assert(sizeof(uint32_t) == 4);
4543
4544 if (direntry->tdir_count != 1)
4545 return (TIFFReadDirEntryErrCount);
4546
4547 if (direntry->tdir_type != TIFF_RATIONAL &&
4548 direntry->tdir_type != TIFF_SRATIONAL)
4549 return (TIFFReadDirEntryErrType);
4550
4551 if (!(tif->tif_flags & TIFF_BIGTIFF))
4552 {
4553 enum TIFFReadDirEntryErr err;
4554 uint32_t offset = direntry->tdir_offset.toff_long;
4555 if (tif->tif_flags & TIFF_SWAB)
4556 TIFFSwabLong(&offset);
4557 err = TIFFReadDirEntryData(tif, offset, 8, m.i);
4558 if (err != TIFFReadDirEntryErrOk)
4559 return (err);
4560 }
4561 else
4562 {
4563 m.l = direntry->tdir_offset.toff_long8;
4564 }
4565
4566 if (tif->tif_flags & TIFF_SWAB)
4567 TIFFSwabArrayOfLong(m.i, 2);
4568
4569 value->uNum = m.i[0];
4570 value->uDenom = m.i[1];
4571 return (TIFFReadDirEntryErrOk);
4572} /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
4573#endif
4574
4592 float *value)
4593{
4594 union
4595 {
4596 float f;
4597 uint32_t i;
4598 } float_union;
4599 assert(sizeof(float) == 4);
4600 assert(sizeof(uint32_t) == 4);
4601 assert(sizeof(float_union) == 4);
4602 float_union.i = *(uint32_t *)(&direntry->tdir_offset);
4603 *value = float_union.f;
4604 if (tif->tif_flags & TIFF_SWAB)
4605 TIFFSwabLong((uint32_t *)value);
4606}
4607
4628static enum TIFFReadDirEntryErr
4629TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
4630{
4631 assert(sizeof(double) == 8);
4632 assert(sizeof(uint64_t) == 8);
4633 assert(sizeof(UInt64Aligned_t) == 8);
4634 if (!(tif->tif_flags & TIFF_BIGTIFF))
4635 {
4636 enum TIFFReadDirEntryErr err;
4637 uint32_t offset = direntry->tdir_offset.toff_long;
4638 if (tif->tif_flags & TIFF_SWAB)
4639 TIFFSwabLong(&offset);
4640 err = TIFFReadDirEntryData(tif, offset, 8, value);
4641 if (err != TIFFReadDirEntryErrOk)
4642 return (err);
4643 }
4644 else
4645 {
4646 UInt64Aligned_t uint64_union;
4647 uint64_union.l = direntry->tdir_offset.toff_long8;
4648 *value = uint64_union.d;
4649 }
4650 if (tif->tif_flags & TIFF_SWAB)
4651 TIFFSwabLong8((uint64_t *)value);
4652 return (TIFFReadDirEntryErrOk);
4653}
4654
4667static enum TIFFReadDirEntryErr
4669{
4670 if (value < 0)
4671 return (TIFFReadDirEntryErrRange);
4672 else
4673 return (TIFFReadDirEntryErrOk);
4674}
4675
4688static enum TIFFReadDirEntryErr
4690{
4691 if (value > 0xFF)
4692 return (TIFFReadDirEntryErrRange);
4693 else
4694 return (TIFFReadDirEntryErrOk);
4695}
4696
4709static enum TIFFReadDirEntryErr
4711{
4712 if ((value < 0) || (value > 0xFF))
4713 return (TIFFReadDirEntryErrRange);
4714 else
4715 return (TIFFReadDirEntryErrOk);
4716}
4717
4730static enum TIFFReadDirEntryErr
4732{
4733 if (value > 0xFF)
4734 return (TIFFReadDirEntryErrRange);
4735 else
4736 return (TIFFReadDirEntryErrOk);
4737}
4738
4751static enum TIFFReadDirEntryErr
4753{
4754 if ((value < 0) || (value > 0xFF))
4755 return (TIFFReadDirEntryErrRange);
4756 else
4757 return (TIFFReadDirEntryErrOk);
4758}
4759
4772static enum TIFFReadDirEntryErr
4774{
4775 if (value > 0xFF)
4776 return (TIFFReadDirEntryErrRange);
4777 else
4778 return (TIFFReadDirEntryErrOk);
4779}
4780
4793static enum TIFFReadDirEntryErr
4795{
4796 if ((value < 0) || (value > 0xFF))
4797 return (TIFFReadDirEntryErrRange);
4798 else
4799 return (TIFFReadDirEntryErrOk);
4800}
4801
4814static enum TIFFReadDirEntryErr
4816{
4817 if (value > 0x7F)
4818 return (TIFFReadDirEntryErrRange);
4819 else
4820 return (TIFFReadDirEntryErrOk);
4821}
4822
4835static enum TIFFReadDirEntryErr
4837{
4838 if (value > 0x7F)
4839 return (TIFFReadDirEntryErrRange);
4840 else
4841 return (TIFFReadDirEntryErrOk);
4842}
4843
4856static enum TIFFReadDirEntryErr
4858{
4859 if ((value < -0x80) || (value > 0x7F))
4860 return (TIFFReadDirEntryErrRange);
4861 else
4862 return (TIFFReadDirEntryErrOk);
4863}
4864
4877static enum TIFFReadDirEntryErr
4879{
4880 if (value > 0x7F)
4881 return (TIFFReadDirEntryErrRange);
4882 else
4883 return (TIFFReadDirEntryErrOk);
4884}
4885
4898static enum TIFFReadDirEntryErr
4900{
4901 if ((value < -0x80) || (value > 0x7F))
4902 return (TIFFReadDirEntryErrRange);
4903 else
4904 return (TIFFReadDirEntryErrOk);
4905}
4906
4919static enum TIFFReadDirEntryErr
4921{
4922 if (value > 0x7F)
4923 return (TIFFReadDirEntryErrRange);
4924 else
4925 return (TIFFReadDirEntryErrOk);
4926}
4927
4940static enum TIFFReadDirEntryErr
4942{
4943 if ((value < -0x80) || (value > 0x7F))
4944 return (TIFFReadDirEntryErrRange);
4945 else
4946 return (TIFFReadDirEntryErrOk);
4947}
4948
4961static enum TIFFReadDirEntryErr
4963{
4964 if (value < 0)
4965 return (TIFFReadDirEntryErrRange);
4966 else
4967 return (TIFFReadDirEntryErrOk);
4968}
4969
4982static enum TIFFReadDirEntryErr
4984{
4985 if (value < 0)
4986 return (TIFFReadDirEntryErrRange);
4987 else
4988 return (TIFFReadDirEntryErrOk);
4989}
4990
5003static enum TIFFReadDirEntryErr
5005{
5006 if (value > 0xFFFF)
5007 return (TIFFReadDirEntryErrRange);
5008 else
5009 return (TIFFReadDirEntryErrOk);
5010}
5011
5024static enum TIFFReadDirEntryErr
5026{
5027 if ((value < 0) || (value > 0xFFFF))
5028 return (TIFFReadDirEntryErrRange);
5029 else
5030 return (TIFFReadDirEntryErrOk);
5031}
5032
5045static enum TIFFReadDirEntryErr
5047{
5048 if (value > 0xFFFF)
5049 return (TIFFReadDirEntryErrRange);
5050 else
5051 return (TIFFReadDirEntryErrOk);
5052}
5053
5066static enum TIFFReadDirEntryErr
5068{
5069 if ((value < 0) || (value > 0xFFFF))
5070 return (TIFFReadDirEntryErrRange);
5071 else
5072 return (TIFFReadDirEntryErrOk);
5073}
5074
5087static enum TIFFReadDirEntryErr
5089{
5090 if (value > 0x7FFF)
5091 return (TIFFReadDirEntryErrRange);
5092 else
5093 return (TIFFReadDirEntryErrOk);
5094}
5095
5108static enum TIFFReadDirEntryErr
5110{
5111 if (value > 0x7FFF)
5112 return (TIFFReadDirEntryErrRange);
5113 else
5114 return (TIFFReadDirEntryErrOk);
5115}
5116
5129static enum TIFFReadDirEntryErr
5131{
5132 if ((value < -0x8000) || (value > 0x7FFF))
5133 return (TIFFReadDirEntryErrRange);
5134 else
5135 return (TIFFReadDirEntryErrOk);
5136}
5137
5150static enum TIFFReadDirEntryErr
5152{
5153 if (value > 0x7FFF)
5154 return (TIFFReadDirEntryErrRange);
5155 else
5156 return (TIFFReadDirEntryErrOk);
5157}
5158
5171static enum TIFFReadDirEntryErr
5173{
5174 if ((value < -0x8000) || (value > 0x7FFF))
5175 return (TIFFReadDirEntryErrRange);
5176 else
5177 return (TIFFReadDirEntryErrOk);
5178}
5179
5192static enum TIFFReadDirEntryErr
5194{
5195 if (value < 0)
5196 return (TIFFReadDirEntryErrRange);
5197 else
5198 return (TIFFReadDirEntryErrOk);
5199}
5200
5213static enum TIFFReadDirEntryErr
5215{
5216 if (value < 0)
5217 return (TIFFReadDirEntryErrRange);
5218 else
5219 return (TIFFReadDirEntryErrOk);
5220}
5221
5234static enum TIFFReadDirEntryErr
5236{
5237 if (value < 0)
5238 return (TIFFReadDirEntryErrRange);
5239 else
5240 return (TIFFReadDirEntryErrOk);
5241}
5242
5255static enum TIFFReadDirEntryErr
5257{
5258 if (value > UINT32_MAX)
5259 return (TIFFReadDirEntryErrRange);
5260 else
5261 return (TIFFReadDirEntryErrOk);
5262}
5263
5276static enum TIFFReadDirEntryErr
5278{
5279 if ((value < 0) || (value > (int64_t)UINT32_MAX))
5280 return (TIFFReadDirEntryErrRange);
5281 else
5282 return (TIFFReadDirEntryErrOk);
5283}
5284
5297static enum TIFFReadDirEntryErr
5299{
5300 if (value > 0x7FFFFFFFUL)
5301 return (TIFFReadDirEntryErrRange);
5302 else
5303 return (TIFFReadDirEntryErrOk);
5304}
5305
5306/* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
5319static enum TIFFReadDirEntryErr
5321{
5322 if (value > 0x7FFFFFFF)
5323 return (TIFFReadDirEntryErrRange);
5324 else
5325 return (TIFFReadDirEntryErrOk);
5326}
5327
5328/* Check that the 8-byte signed value can fit in a 4-byte signed range */
5342static enum TIFFReadDirEntryErr
5344{
5345 if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
5346 return (TIFFReadDirEntryErrRange);
5347 else
5348 return (TIFFReadDirEntryErrOk);
5349}
5350
5363static enum TIFFReadDirEntryErr
5365{
5366 if (value < 0)
5367 return (TIFFReadDirEntryErrRange);
5368 else
5369 return (TIFFReadDirEntryErrOk);
5370}
5371
5384static enum TIFFReadDirEntryErr
5386{
5387 if (value < 0)
5388 return (TIFFReadDirEntryErrRange);
5389 else
5390 return (TIFFReadDirEntryErrOk);
5391}
5392
5405static enum TIFFReadDirEntryErr
5407{
5408 if (value < 0)
5409 return (TIFFReadDirEntryErrRange);
5410 else
5411 return (TIFFReadDirEntryErrOk);
5412}
5413
5426static enum TIFFReadDirEntryErr
5428{
5429 if (value < 0)
5430 return (TIFFReadDirEntryErrRange);
5431 else
5432 return (TIFFReadDirEntryErrOk);
5433}
5434
5447static enum TIFFReadDirEntryErr
5449{
5450 if (value > INT64_MAX)
5451 return (TIFFReadDirEntryErrRange);
5452 else
5453 return (TIFFReadDirEntryErrOk);
5454}
5455
5474static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
5475 tmsize_t size, void *dest)
5476{
5477 if (size <= 0)
5479 if (!isMapped(tif))
5480 {
5481 if (!SeekOK(tif, offset))
5482 return (TIFFReadDirEntryErrIo);
5483 if (!ReadOK(tif, dest, size))
5484 return (TIFFReadDirEntryErrIo);
5485 }
5486 else
5487 {
5488 size_t ma, mb;
5489 ma = (size_t)offset;
5490 if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
5491 {
5492 return TIFFReadDirEntryErrIo;
5493 }
5494 mb = (uint64_t)ma + (uint64_t)size;
5495 if (mb > (uint64_t)tif->tif_size)
5496 return (TIFFReadDirEntryErrIo);
5497 _TIFFmemcpy(dest, tif->tif_base + ma, size);
5498 }
5499 return (TIFFReadDirEntryErrOk);
5500}
5501
5520 const char *module, const char *tagname,
5521 int recover)
5522{
5523 if (!recover)
5524 {
5525 switch (err)
5526 {
5528 TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
5529 tagname);
5530 break;
5532 TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
5533 tagname);
5534 break;
5536 TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
5537 tagname);
5538 break;
5540 TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
5541 tagname);
5542 break;
5545 tif, module,
5546 "Cannot handle different values per sample for \"%s\"",
5547 tagname);
5548 break;
5550 TIFFErrorExtR(tif, module,
5551 "Sanity check on size of \"%s\" value failed",
5552 tagname);
5553 break;
5555 TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
5556 tagname);
5557 break;
5559 default:
5560 assert(0); /* we should never get here */
5561 break;
5562 }
5563 }
5564 else
5565 {
5566 switch (err)
5567 {
5569 TIFFWarningExtR(tif, module,
5570 "Incorrect count for \"%s\"; tag ignored",
5571 tagname);
5572 break;
5574 TIFFWarningExtR(tif, module,
5575 "Incompatible type for \"%s\"; tag ignored",
5576 tagname);
5577 break;
5580 tif, module,
5581 "IO error during reading of \"%s\"; tag ignored", tagname);
5582 break;
5584 TIFFWarningExtR(tif, module,
5585 "Incorrect value for \"%s\"; tag ignored",
5586 tagname);
5587 break;
5589 TIFFWarningExtR(tif, module,
5590 "Cannot handle different values per sample for "
5591 "\"%s\"; tag ignored",
5592 tagname);
5593 break;
5596 tif, module,
5597 "Sanity check on size of \"%s\" value failed; tag ignored",
5598 tagname);
5599 break;
5601 TIFFWarningExtR(tif, module,
5602 "Out of memory reading of \"%s\"; tag ignored",
5603 tagname);
5604 break;
5606 default:
5607 assert(0); /* we should never get here */
5608 break;
5609 }
5610 }
5611}
5612
5613/*
5614 * Return the maximum number of color channels specified for a given photometric
5615 * type. 0 is returned if photometric type isn't supported or no default value
5616 * is defined by the specification.
5617 */
5630static int _TIFFGetMaxColorChannels(uint16_t photometric)
5631{
5632 switch (photometric)
5633 {
5637 return 1;
5638 case PHOTOMETRIC_YCBCR:
5639 case PHOTOMETRIC_RGB:
5640 case PHOTOMETRIC_CIELAB:
5641 case PHOTOMETRIC_LOGLUV:
5642 case PHOTOMETRIC_ITULAB:
5643 case PHOTOMETRIC_ICCLAB:
5644 return 3;
5646 case PHOTOMETRIC_MASK:
5647 return 4;
5648 case PHOTOMETRIC_LOGL:
5649 case PHOTOMETRIC_CFA:
5650 default:
5651 return 0;
5652 }
5653}
5654
5671static int ByteCountLooksBad(TIFF *tif)
5672{
5673 /*
5674 * Assume we have wrong StripByteCount value (in case
5675 * of single strip) in following cases:
5676 * - it is equal to zero along with StripOffset;
5677 * - it is larger than file itself (in case of uncompressed
5678 * image);
5679 * - it is smaller than the size of the bytes per row
5680 * multiplied on the number of rows. The last case should
5681 * not be checked in the case of writing new image,
5682 * because we may do not know the exact strip size
5683 * until the whole image will be written and directory
5684 * dumped out.
5685 */
5686 uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
5687 uint64_t offset = TIFFGetStrileOffset(tif, 0);
5688 uint64_t filesize;
5689
5690 if (offset == 0)
5691 return 0;
5692 if (bytecount == 0)
5693 return 1;
5695 return 0;
5696 filesize = TIFFGetFileSize(tif);
5697 if (offset <= filesize && bytecount > filesize - offset)
5698 return 1;
5699 if (tif->tif_mode == O_RDONLY)
5700 {
5701 uint64_t scanlinesize = TIFFScanlineSize64(tif);
5702 if (tif->tif_dir.td_imagelength > 0 &&
5703 scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
5704 {
5705 return 1;
5706 }
5707 if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
5708 return 1;
5709 }
5710 return 0;
5711}
5712
5713/*
5714 * To evaluate the IFD data size when reading, save the offset and data size of
5715 * all data that does not fit into the IFD entries themselves.
5716 */
5734{
5735 const uint64_t data_width =
5736 (uint64_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
5737 if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
5738 {
5739 TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
5740 "Too large IFD data size");
5741 return false;
5742 }
5743 const uint64_t datalength = dp->tdir_count * data_width;
5744 if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
5745 {
5746 if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
5747 {
5748 TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
5749 "Too large IFD data size");
5750 return false;
5751 }
5752 tif->tif_dir.td_dirdatasize_read += datalength;
5753 if (!(tif->tif_flags & TIFF_BIGTIFF))
5754 {
5755 /* The offset of TIFFDirEntry are not swapped when read in. That has
5756 * to be done when used. */
5757 uint32_t offset = dp->tdir_offset.toff_long;
5758 if (tif->tif_flags & TIFF_SWAB)
5759 TIFFSwabLong(&offset);
5760 tif->tif_dir
5762 .offset = (uint64_t)offset;
5763 }
5764 else
5765 {
5766 tif->tif_dir
5769 if (tif->tif_flags & TIFF_SWAB)
5771 &tif->tif_dir
5774 .offset);
5775 }
5776 tif->tif_dir
5778 .length = datalength;
5780 }
5781 return true;
5782}
5783
5784/*
5785 * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
5786 */
5799static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
5800{
5803 /* Compare offsets */
5804 if (ta->offset > tb->offset)
5805 return 1;
5806 else if (ta->offset < tb->offset)
5807 return -1;
5808 else
5809 return 0;
5810}
5811
5812/*
5813 * Determine the IFD data size after reading an IFD from the file that can be
5814 * overwritten and saving it in tif_dir.td_dirdatasize_read. This data size
5815 * includes the IFD entries themselves as well as the data that does not fit
5816 * directly into the IFD entries but is located directly after the IFD entries
5817 * in the file.
5818 */
5835static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
5836{
5837 /* IFD data size is only needed if file-writing is enabled.
5838 * This also avoids the seek() to EOF to determine the file size, which
5839 * causes the stdin-streaming-friendly mode of libtiff for GDAL to fail. */
5840 if (tif->tif_mode == O_RDONLY)
5841 return;
5842
5843 /* Sort TIFFEntryOffsetAndLength array in ascending order. */
5847
5848 /* Get offset of end of IFD entry space. */
5849 uint64_t IFDendoffset;
5850 if (!(tif->tif_flags & TIFF_BIGTIFF))
5851 IFDendoffset = tif->tif_diroff + 2 + (uint64_t)dircount * 12 + 4;
5852 else
5853 IFDendoffset = tif->tif_diroff + 8 + (uint64_t)dircount * 20 + 8;
5854
5855 /* Check which offsets are right behind IFD entries. However, LibTIFF
5856 * increments the writing address for every external data to an even offset.
5857 * Thus gaps of 1 byte can occur. */
5858 uint64_t size = 0;
5859 uint64_t offset;
5860 uint32_t i;
5861 for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
5862 {
5863 offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
5864 if (offset == IFDendoffset)
5865 {
5866 size += tif->tif_dir.td_dirdatasize_offsets[i].length;
5867 IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
5868 }
5869 else if (offset == IFDendoffset + 1)
5870 {
5871 /* Add gap byte after previous IFD data set. */
5872 size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
5873 IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
5874 }
5875 else
5876 {
5877 /* Further data is no more continuously after IFD */
5878 break;
5879 }
5880 }
5881 /* Check for gap byte of some easy cases. This should cover 90% of cases.
5882 * Otherwise, IFD will be re-written even it might be safely overwritten. */
5883 if (tif->tif_nextdiroff != 0)
5884 {
5885 if (tif->tif_nextdiroff == IFDendoffset + 1)
5886 size++;
5887 }
5888 else
5889 {
5890 /* Check for IFD data ends at EOF. Then IFD can always be safely
5891 * overwritten. */
5892 offset = TIFFSeekFile(tif, 0, SEEK_END);
5893 if (offset == IFDendoffset)
5894 {
5895 tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
5896 return;
5897 }
5898 }
5899
5900 /* Finally, add the size of the IFD tag entries themselves. */
5901 if (!(tif->tif_flags & TIFF_BIGTIFF))
5902 tif->tif_dir.td_dirdatasize_read = 2U + dircount * 12U + 4U + size;
5903 else
5904 tif->tif_dir.td_dirdatasize_read = 8U + dircount * 20U + 8U + size;
5905} /*-- CalcFinalIFDdatasizeReading() --*/
5906
5907/*
5908 * Read the next TIFF directory from a file and convert it to the internal
5909 * format. We read directories sequentially.
5910 */
5912{
5913 static const char module[] = "TIFFReadDirectory";
5914 TIFFDirEntry *dir;
5915 uint16_t dircount;
5916 TIFFDirEntry *dp;
5917 uint16_t di;
5918 const TIFFField *fip;
5919 uint32_t fii = FAILED_FII;
5920 toff_t nextdiroff;
5921 int bitspersample_read = FALSE;
5922 int color_channels;
5923
5924 if (tif->tif_nextdiroff == 0)
5925 {
5926 /* In this special case, tif_diroff needs also to be set to 0.
5927 * This is behind the last IFD, thus no checking or reading necessary.
5928 */
5929 tif->tif_diroff = tif->tif_nextdiroff;
5930 return 0;
5931 }
5932
5933 nextdiroff = tif->tif_nextdiroff;
5934 /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
5935 * reading of the directory. Otherwise, invalid IFD offsets could corrupt
5936 * the IFD list. */
5938 tif->tif_curdir ==
5940 ? 0
5941 : tif->tif_curdir + 1,
5942 nextdiroff))
5943 {
5944 return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
5945 IFDs) */
5946 }
5947 dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
5948 if (!dircount)
5949 {
5950 TIFFErrorExtR(tif, module,
5951 "Failed to read directory at offset %" PRIu64,
5952 nextdiroff);
5953 return 0;
5954 }
5955 /* Set global values after a valid directory has been fetched.
5956 * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
5957 * beginning. */
5959 tif->tif_curdir = 0;
5960 else
5961 tif->tif_curdir++;
5962
5963 TIFFReadDirectoryCheckOrder(tif, dir, dircount);
5964
5965 /*
5966 * Mark duplicates of any tag to be ignored (bugzilla 1994)
5967 * to avoid certain pathological problems.
5968 */
5969 {
5970 TIFFDirEntry *ma;
5971 uint16_t mb;
5972 for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
5973 {
5974 TIFFDirEntry *na;
5975 uint16_t nb;
5976 for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
5977 na++, nb++)
5978 {
5979 if (ma->tdir_tag == na->tdir_tag)
5980 {
5981 na->tdir_ignore = TRUE;
5982 }
5983 }
5984 }
5985 }
5986
5987 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
5988 tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */
5989 tif->tif_flags &= ~TIFF_POSTENCODE; /* reset before new dir */
5990 tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
5991
5992 /* When changing directory, in deferred strile loading mode, we must also
5993 * unset the TIFF_LAZYSTRILELOAD_DONE bit if it was initially set,
5994 * to make sure the strile offset/bytecount are read again (when they fit
5995 * in the tag data area).
5996 */
5997 tif->tif_flags &= ~TIFF_LAZYSTRILELOAD_DONE;
5998
5999 /* Free any old stuff and reinit i/o and other parameters within
6000 * TIFFDefaultDirectory() since we are starting on a new directory. */
6001 TIFFFreeDirectory(tif);
6003
6004 /* After setup a fresh directory indicate that now active IFD is also
6005 * present on file, even if its entries could not be read successfully
6006 * below. */
6008
6009 /* Allocate arrays for offset values outside IFD entry for IFD data size
6010 * checking. Note: Counter are reset within TIFFFreeDirectory(). */
6013 tif,
6014 (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
6015 if (tif->tif_dir.td_dirdatasize_offsets == NULL)
6016 {
6018 tif, module,
6019 "Failed to allocate memory for counting IFD data size at reading");
6020 goto bad;
6021 }
6022 /*
6023 * Electronic Arts writes gray-scale TIFF files
6024 * without a PlanarConfiguration directory entry.
6025 * Thus we setup a default value here, even though
6026 * the TIFF spec says there is no default value.
6027 * After PlanarConfiguration is preset in TIFFDefaultDirectory()
6028 * the following setting is not needed, but does not harm either.
6029 */
6031 /*
6032 * Setup default value and then make a pass over
6033 * the fields to check type and tag information,
6034 * and to extract info required to size data
6035 * structures. A second pass is made afterwards
6036 * to read in everything not taken in the first pass.
6037 * But we must process the Compression tag first
6038 * in order to merge in codec-private tag definitions (otherwise
6039 * we may get complaints about unknown tags). However, the
6040 * Compression tag may be dependent on the SamplesPerPixel
6041 * tag value because older TIFF specs permitted Compression
6042 * to be written as a SamplesPerPixel-count tag entry.
6043 * Thus if we don't first figure out the correct SamplesPerPixel
6044 * tag value then we may end up ignoring the Compression tag
6045 * value because it has an incorrect count value (if the
6046 * true value of SamplesPerPixel is not 1).
6047 */
6048 dp =
6050 if (dp)
6051 {
6052 if (!TIFFFetchNormalTag(tif, dp, 0))
6053 goto bad;
6054 dp->tdir_ignore = TRUE;
6055 }
6056 dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
6057 if (dp)
6058 {
6059 /*
6060 * The 5.0 spec says the Compression tag has one value, while
6061 * earlier specs say it has one value per sample. Because of
6062 * this, we accept the tag if one value is supplied with either
6063 * count.
6064 */
6065 uint16_t value;
6066 enum TIFFReadDirEntryErr err;
6067 err = TIFFReadDirEntryShort(tif, dp, &value);
6068 if (err == TIFFReadDirEntryErrCount)
6069 err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
6070 if (err != TIFFReadDirEntryErrOk)
6071 {
6072 TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
6073 goto bad;
6074 }
6075 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
6076 goto bad;
6077 dp->tdir_ignore = TRUE;
6078 }
6079 else
6080 {
6082 goto bad;
6083 }
6084 /*
6085 * First real pass over the directory.
6086 */
6087 for (di = 0, dp = dir; di < dircount; di++, dp++)
6088 {
6089 if (!dp->tdir_ignore)
6090 {
6092 if (fii == FAILED_FII)
6093 {
6095 {
6096 TIFFWarningExtR(tif, module,
6097 "Unknown field with tag %" PRIu16
6098 " (0x%" PRIx16 ") encountered",
6099 dp->tdir_tag, dp->tdir_tag);
6100 }
6101 /* the following knowingly leaks the
6102 anonymous field structure */
6103 const TIFFField *fld = _TIFFCreateAnonField(
6104 tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
6105 if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
6106 {
6108 tif, module,
6109 "Registering anonymous field with tag %" PRIu16
6110 " (0x%" PRIx16 ") failed",
6111 dp->tdir_tag, dp->tdir_tag);
6112 dp->tdir_ignore = TRUE;
6113 }
6114 else
6115 {
6117 assert(fii != FAILED_FII);
6118 }
6119 }
6120 }
6121 if (!dp->tdir_ignore)
6122 {
6123 fip = tif->tif_fields[fii];
6124 if (fip->field_bit == FIELD_IGNORE)
6125 dp->tdir_ignore = TRUE;
6126 else
6127 {
6128 switch (dp->tdir_tag)
6129 {
6134 TIFFSetFieldBit(tif, fip->field_bit);
6135 break;
6136 case TIFFTAG_IMAGEWIDTH:
6138 case TIFFTAG_IMAGEDEPTH:
6139 case TIFFTAG_TILELENGTH:
6140 case TIFFTAG_TILEWIDTH:
6141 case TIFFTAG_TILEDEPTH:
6145 if (!TIFFFetchNormalTag(tif, dp, 0))
6146 goto bad;
6147 dp->tdir_ignore = TRUE;
6148 break;
6149 default:
6151 dp->tdir_ignore = TRUE;
6152 break;
6153 }
6154 }
6155 }
6156 }
6157 /*
6158 * XXX: OJPEG hack.
6159 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
6160 * c) strip offsets/bytecounts tag are both present and
6161 * d) both contain exactly one value, then we consistently find
6162 * that the buggy implementation of the buggy compression scheme
6163 * matches contig planarconfig best. So we 'fix-up' the tag here
6164 */
6167 {
6168 if (!_TIFFFillStriles(tif))
6169 goto bad;
6170 dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
6172 if ((dp != 0) && (dp->tdir_count == 1))
6173 {
6174 dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
6176 if ((dp != 0) && (dp->tdir_count == 1))
6177 {
6179 TIFFWarningExtR(tif, module,
6180 "Planarconfig tag value assumed incorrect, "
6181 "assuming data is contig instead of chunky");
6182 }
6183 }
6184 }
6185 /*
6186 * Allocate directory structure and setup defaults.
6187 */
6189 {
6190 MissingRequired(tif, "ImageLength");
6191 goto bad;
6192 }
6193
6194 /*
6195 * Second pass: extract other information.
6196 */
6197 for (di = 0, dp = dir; di < dircount; di++, dp++)
6198 {
6199 if (!dp->tdir_ignore)
6200 {
6201 switch (dp->tdir_tag)
6202 {
6206 case TIFFTAG_DATATYPE:
6208 /*
6209 * The MinSampleValue, MaxSampleValue, BitsPerSample
6210 * DataType and SampleFormat tags are supposed to be
6211 * written as one value/sample, but some vendors
6212 * incorrectly write one value only -- so we accept
6213 * that as well (yuck). Other vendors write correct
6214 * value for NumberOfSamples, but incorrect one for
6215 * BitsPerSample and friends, and we will read this
6216 * too.
6217 */
6218 {
6219 uint16_t value;
6220 enum TIFFReadDirEntryErr err;
6221 err = TIFFReadDirEntryShort(tif, dp, &value);
6222 if (!EvaluateIFDdatasizeReading(tif, dp))
6223 goto bad;
6224 if (err == TIFFReadDirEntryErrCount)
6225 err =
6226 TIFFReadDirEntryPersampleShort(tif, dp, &value);
6227 if (err != TIFFReadDirEntryErrOk)
6228 {
6229 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6231 tif, err, module,
6232 fip ? fip->field_name : "unknown tagname", 0);
6233 goto bad;
6234 }
6235 if (!TIFFSetField(tif, dp->tdir_tag, value))
6236 goto bad;
6238 bitspersample_read = TRUE;
6239 }
6240 break;
6243 {
6244
6245 double *data = NULL;
6246 enum TIFFReadDirEntryErr err;
6247 uint32_t saved_flags;
6248 int m;
6249 if (dp->tdir_count !=
6250 (uint64_t)tif->tif_dir.td_samplesperpixel)
6252 else
6253 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6254 if (!EvaluateIFDdatasizeReading(tif, dp))
6255 goto bad;
6256 if (err != TIFFReadDirEntryErrOk)
6257 {
6258 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6260 tif, err, module,
6261 fip ? fip->field_name : "unknown tagname", 0);
6262 goto bad;
6263 }
6264 saved_flags = tif->tif_flags;
6265 tif->tif_flags |= TIFF_PERSAMPLE;
6266 m = TIFFSetField(tif, dp->tdir_tag, data);
6267 tif->tif_flags = saved_flags;
6268 _TIFFfreeExt(tif, data);
6269 if (!m)
6270 goto bad;
6271 }
6272 break;
6275 {
6276 switch (dp->tdir_type)
6277 {
6278 case TIFF_SHORT:
6279 case TIFF_LONG:
6280 case TIFF_LONG8:
6281 break;
6282 default:
6283 /* Warn except if directory typically created with
6284 * TIFFDeferStrileArrayWriting() */
6285 if (!(tif->tif_mode == O_RDWR &&
6286 dp->tdir_count == 0 && dp->tdir_type == 0 &&
6287 dp->tdir_offset.toff_long8 == 0))
6288 {
6289 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6291 tif, module, "Invalid data type for tag %s",
6292 fip ? fip->field_name : "unknown tagname");
6293 }
6294 break;
6295 }
6297 sizeof(TIFFDirEntry));
6298 if (!EvaluateIFDdatasizeReading(tif, dp))
6299 goto bad;
6300 }
6301 break;
6304 {
6305 switch (dp->tdir_type)
6306 {
6307 case TIFF_SHORT:
6308 case TIFF_LONG:
6309 case TIFF_LONG8:
6310 break;
6311 default:
6312 /* Warn except if directory typically created with
6313 * TIFFDeferStrileArrayWriting() */
6314 if (!(tif->tif_mode == O_RDWR &&
6315 dp->tdir_count == 0 && dp->tdir_type == 0 &&
6316 dp->tdir_offset.toff_long8 == 0))
6317 {
6318 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6320 tif, module, "Invalid data type for tag %s",
6321 fip ? fip->field_name : "unknown tagname");
6322 }
6323 break;
6324 }
6326 sizeof(TIFFDirEntry));
6327 if (!EvaluateIFDdatasizeReading(tif, dp))
6328 goto bad;
6329 }
6330 break;
6331 case TIFFTAG_COLORMAP:
6333 {
6334 enum TIFFReadDirEntryErr err;
6335 uint32_t countpersample;
6336 uint32_t countrequired;
6337 uint32_t incrementpersample;
6338 uint16_t *value = NULL;
6339 /* It would be dangerous to instantiate those tag values */
6340 /* since if td_bitspersample has not yet been read (due to
6341 */
6342 /* unordered tags), it could be read afterwards with a */
6343 /* values greater than the default one (1), which may cause
6344 */
6345 /* crashes in user code */
6346 if (!bitspersample_read)
6347 {
6348 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6350 tif, module,
6351 "Ignoring %s since BitsPerSample tag not found",
6352 fip ? fip->field_name : "unknown tagname");
6353 continue;
6354 }
6355 /* ColorMap or TransferFunction for high bit */
6356 /* depths do not make much sense and could be */
6357 /* used as a denial of service vector */
6358 if (tif->tif_dir.td_bitspersample > 24)
6359 {
6360 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6362 tif, module,
6363 "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
6364 fip ? fip->field_name : "unknown tagname",
6366 continue;
6367 }
6368 countpersample = (1U << tif->tif_dir.td_bitspersample);
6369 if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
6370 (dp->tdir_count == (uint64_t)countpersample))
6371 {
6372 countrequired = countpersample;
6373 incrementpersample = 0;
6374 }
6375 else
6376 {
6377 countrequired = 3 * countpersample;
6378 incrementpersample = countpersample;
6379 }
6380 if (dp->tdir_count != (uint64_t)countrequired)
6382 else
6383 err = TIFFReadDirEntryShortArray(tif, dp, &value);
6384 if (!EvaluateIFDdatasizeReading(tif, dp))
6385 goto bad;
6386 if (err != TIFFReadDirEntryErrOk)
6387 {
6388 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6390 tif, err, module,
6391 fip ? fip->field_name : "unknown tagname", 1);
6392 }
6393 else
6394 {
6395 TIFFSetField(tif, dp->tdir_tag, value,
6396 value + incrementpersample,
6397 value + 2 * incrementpersample);
6398 _TIFFfreeExt(tif, value);
6399 }
6400 }
6401 break;
6402 /* BEGIN REV 4.0 COMPATIBILITY */
6404 {
6405 uint16_t valueo;
6406 uint32_t value;
6407 if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
6409 {
6410 switch (valueo)
6411 {
6413 value = FILETYPE_REDUCEDIMAGE;
6414 break;
6415 case OFILETYPE_PAGE:
6416 value = FILETYPE_PAGE;
6417 break;
6418 default:
6419 value = 0;
6420 break;
6421 }
6422 if (value != 0)
6423 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
6424 }
6425 }
6426 break;
6427 /* END REV 4.0 COMPATIBILITY */
6428#if 0
6430 /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
6431 * LibTiff defines it as ASCII and converts RATIONAL to an
6432 * ASCII string. */
6433 switch (dp->tdir_type)
6434 {
6435 case TIFF_RATIONAL:
6436 {
6437 /* Read rational and convert to ASCII*/
6438 enum TIFFReadDirEntryErr err;
6439 TIFFRational_t rValue;
6440 err = TIFFReadDirEntryCheckedRationalDirect(
6441 tif, dp, &rValue);
6442 if (err != TIFFReadDirEntryErrOk)
6443 {
6444 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6446 tif, err, module,
6447 fip ? fip->field_name : "unknown tagname",
6448 1);
6449 }
6450 else
6451 {
6452 char szAux[32];
6453 snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
6454 rValue.uNum, rValue.uDenom);
6455 TIFFSetField(tif, dp->tdir_tag, szAux);
6456 }
6457 }
6458 break;
6459 case TIFF_ASCII:
6460 (void)TIFFFetchNormalTag(tif, dp, TRUE);
6461 break;
6462 default:
6463 fip = TIFFFieldWithTag(tif, dp->tdir_tag);
6464 TIFFWarningExtR(tif, module,
6465 "Invalid data type for tag %s. "
6466 "ASCII or RATIONAL expected",
6467 fip ? fip->field_name
6468 : "unknown tagname");
6469 break;
6470 }
6471 break;
6472#endif
6473 default:
6474 (void)TIFFFetchNormalTag(tif, dp, TRUE);
6475 break;
6476 } /* -- switch (dp->tdir_tag) -- */
6477 } /* -- if (!dp->tdir_ignore) */
6478 } /* -- for-loop -- */
6479
6480 /* Evaluate final IFD data size. */
6481 CalcFinalIFDdatasizeReading(tif, dircount);
6482
6483 /*
6484 * OJPEG hack:
6485 * - If a) compression is OJPEG, and b) photometric tag is missing,
6486 * then we consistently find that photometric should be YCbCr
6487 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
6488 * then we consistently find that the buggy implementation of the
6489 * buggy compression scheme matches photometric YCbCr instead.
6490 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
6491 * then we consistently find bitspersample should be 8.
6492 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
6493 * and c) photometric is RGB or YCbCr, then we consistently find
6494 * samplesperpixel should be 3
6495 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
6496 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
6497 * find samplesperpixel should be 3
6498 */
6500 {
6502 {
6504 tif, module,
6505 "Photometric tag is missing, assuming data is YCbCr");
6507 goto bad;
6508 }
6509 else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
6510 {
6512 TIFFWarningExtR(tif, module,
6513 "Photometric tag value assumed incorrect, "
6514 "assuming data is YCbCr instead of RGB");
6515 }
6517 {
6519 tif, module,
6520 "BitsPerSample tag is missing, assuming 8 bits per sample");
6521 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
6522 goto bad;
6523 }
6525 {
6527 {
6528 TIFFWarningExtR(tif, module,
6529 "SamplesPerPixel tag is missing, "
6530 "assuming correct SamplesPerPixel value is 3");
6532 goto bad;
6533 }
6535 {
6536 TIFFWarningExtR(tif, module,
6537 "SamplesPerPixel tag is missing, "
6538 "applying correct SamplesPerPixel value of 3");
6540 goto bad;
6541 }
6542 else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
6544 {
6545 /*
6546 * SamplesPerPixel tag is missing, but is not required
6547 * by spec. Assume correct SamplesPerPixel value of 1.
6548 */
6550 goto bad;
6551 }
6552 }
6553 }
6554
6555 /*
6556 * Setup appropriate structures (by strip or by tile)
6557 * We do that only after the above OJPEG hack which alters SamplesPerPixel
6558 * and thus influences the number of strips in the separate planarconfig.
6559 */
6561 {
6566 tif->tif_flags &= ~TIFF_ISTILED;
6567 }
6568 else
6569 {
6571 tif->tif_flags |= TIFF_ISTILED;
6572 }
6573 if (!tif->tif_dir.td_nstrips)
6574 {
6575 TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
6576 isTiled(tif) ? "tiles" : "strips");
6577 goto bad;
6578 }
6583 {
6584#ifdef OJPEG_SUPPORT
6586 (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
6587 {
6588 /*
6589 * XXX: OJPEG hack.
6590 * If a) compression is OJPEG, b) it's not a tiled TIFF,
6591 * and c) the number of strips is 1,
6592 * then we tolerate the absence of stripoffsets tag,
6593 * because, presumably, all required data is in the
6594 * JpegInterchangeFormat stream.
6595 */
6597 }
6598 else
6599#endif
6600 {
6601 MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
6602 goto bad;
6603 }
6604 }
6605
6606 if (tif->tif_mode == O_RDWR &&
6615 {
6616 /* Directory typically created with TIFFDeferStrileArrayWriting() */
6617 TIFFSetupStrips(tif);
6618 }
6619 else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
6620 {
6622 {
6624 tif->tif_dir.td_nstrips,
6626 {
6627 goto bad;
6628 }
6629 }
6631 {
6633 tif, &(tif->tif_dir.td_stripbytecount_entry),
6635 {
6636 goto bad;
6637 }
6638 }
6639 }
6640
6641 /*
6642 * Make sure all non-color channels are extrasamples.
6643 * If it's not the case, define them as such.
6644 */
6645 color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
6646 if (color_channels &&
6648 color_channels)
6649 {
6650 uint16_t old_extrasamples;
6651 uint16_t *new_sampleinfo;
6652
6654 tif, module,
6655 "Sum of Photometric type-related "
6656 "color channels and ExtraSamples doesn't match SamplesPerPixel. "
6657 "Defining non-color channels as ExtraSamples.");
6658
6659 old_extrasamples = tif->tif_dir.td_extrasamples;
6661 (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
6662
6663 // sampleinfo should contain information relative to these new extra
6664 // samples
6665 new_sampleinfo = (uint16_t *)_TIFFcallocExt(
6666 tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
6667 if (!new_sampleinfo)
6668 {
6669 TIFFErrorExtR(tif, module,
6670 "Failed to allocate memory for "
6671 "temporary new sampleinfo array "
6672 "(%" PRIu16 " 16 bit elements)",
6674 goto bad;
6675 }
6676
6677 if (old_extrasamples > 0)
6678 memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
6679 old_extrasamples * sizeof(uint16_t));
6680 _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
6682 _TIFFfreeExt(tif, new_sampleinfo);
6683 }
6684
6685 /*
6686 * Verify Palette image has a Colormap.
6687 */
6690 {
6691 if (tif->tif_dir.td_bitspersample >= 8 &&
6692 tif->tif_dir.td_samplesperpixel == 3)
6694 else if (tif->tif_dir.td_bitspersample >= 8)
6696 else
6697 {
6698 MissingRequired(tif, "Colormap");
6699 goto bad;
6700 }
6701 }
6702 /*
6703 * OJPEG hack:
6704 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
6705 * TIFFs
6706 */
6708 {
6709 /*
6710 * Attempt to deal with a missing StripByteCounts tag.
6711 */
6713 {
6714 /*
6715 * Some manufacturers violate the spec by not giving
6716 * the size of the strips. In this case, assume there
6717 * is one uncompressed strip of data.
6718 */
6720 tif->tif_dir.td_nstrips > 1) ||
6722 tif->tif_dir.td_nstrips !=
6723 (uint32_t)tif->tif_dir.td_samplesperpixel))
6724 {
6725 MissingRequired(tif, "StripByteCounts");
6726 goto bad;
6727 }
6729 tif, module,
6730 "TIFF directory is missing required "
6731 "\"StripByteCounts\" field, calculating from imagelength");
6732 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
6733 goto bad;
6734 }
6735 else if (tif->tif_dir.td_nstrips == 1 &&
6736 !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
6737 {
6738 /*
6739 * XXX: Plexus (and others) sometimes give a value of
6740 * zero for a tag when they don't know what the
6741 * correct value is! Try and handle the simple case
6742 * of estimating the size of a one strip image.
6743 */
6744 TIFFWarningExtR(tif, module,
6745 "Bogus \"StripByteCounts\" field, ignoring and "
6746 "calculating from imagelength");
6747 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
6748 goto bad;
6749 }
6750 else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
6752 tif->tif_dir.td_nstrips > 2 &&
6754 TIFFGetStrileByteCount(tif, 0) !=
6755 TIFFGetStrileByteCount(tif, 1) &&
6756 TIFFGetStrileByteCount(tif, 0) != 0 &&
6757 TIFFGetStrileByteCount(tif, 1) != 0)
6758 {
6759 /*
6760 * XXX: Some vendors fill StripByteCount array with
6761 * absolutely wrong values (it can be equal to
6762 * StripOffset array, for example). Catch this case
6763 * here.
6764 *
6765 * We avoid this check if deferring strile loading
6766 * as it would always force us to load the strip/tile
6767 * information.
6768 */
6769 TIFFWarningExtR(tif, module,
6770 "Wrong \"StripByteCounts\" field, ignoring and "
6771 "calculating from imagelength");
6772 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
6773 goto bad;
6774 }
6775 }
6776 if (dir)
6777 {
6778 _TIFFfreeExt(tif, dir);
6779 dir = NULL;
6780 }
6782 {
6783 if (tif->tif_dir.td_bitspersample >= 16)
6784 tif->tif_dir.td_maxsamplevalue = 0xFFFF;
6785 else
6787 (uint16_t)((1 << tif->tif_dir.td_bitspersample) - 1);
6788 }
6789
6790#ifdef STRIPBYTECOUNTSORTED_UNUSED
6791 /*
6792 * XXX: We can optimize checking for the strip bounds using the sorted
6793 * bytecounts array. See also comments for TIFFAppendToStrip()
6794 * function in tif_write.c.
6795 */
6796 if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
6797 {
6798 uint32_t strip;
6799
6800 tif->tif_dir.td_stripbytecountsorted = 1;
6801 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
6802 {
6803 if (TIFFGetStrileOffset(tif, strip - 1) >
6804 TIFFGetStrileOffset(tif, strip))
6805 {
6806 tif->tif_dir.td_stripbytecountsorted = 0;
6807 break;
6808 }
6809 }
6810 }
6811#endif
6812
6813 /*
6814 * An opportunity for compression mode dependent tag fixup
6815 */
6816 (*tif->tif_fixuptags)(tif);
6817
6818 /*
6819 * Some manufacturers make life difficult by writing
6820 * large amounts of uncompressed data as a single strip.
6821 * This is contrary to the recommendations of the spec.
6822 * The following makes an attempt at breaking such images
6823 * into strips closer to the recommended 8k bytes. A
6824 * side effect, however, is that the RowsPerStrip tag
6825 * value may be changed.
6826 */
6828 (tif->tif_dir.td_nstrips == 1) &&
6831 {
6833 }
6834
6835 /* There are also uncompressed striped files with strips larger than */
6836 /* 2 GB, which make them unfriendly with a lot of code. If possible, */
6837 /* try to expose smaller "virtual" strips. */
6841 TIFFStripSize64(tif) > 0x7FFFFFFFUL)
6842 {
6844 }
6845
6846 /*
6847 * Clear the dirty directory flag.
6848 */
6849 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
6850 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
6851
6852 /*
6853 * Reinitialize some further i/o since we are starting on a new directory.
6854 */
6856 if (!tif->tif_dir.td_scanlinesize)
6857 {
6858 TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
6859 return (0);
6860 }
6861
6862 if (isTiled(tif))
6863 {
6864 tif->tif_dir.td_tilesize = TIFFTileSize(tif);
6865 if (!tif->tif_dir.td_tilesize)
6866 {
6867 TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
6868 return (0);
6869 }
6870 }
6871 else
6872 {
6873 if (!TIFFStripSize(tif))
6874 {
6875 TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
6876 return (0);
6877 }
6878 }
6879 return (1);
6880bad:
6881 if (dir)
6882 _TIFFfreeExt(tif, dir);
6883 return (0);
6884} /*-- TIFFReadDirectory() --*/
6885
6899 uint16_t dircount)
6900{
6901 static const char module[] = "TIFFReadDirectoryCheckOrder";
6902 uint32_t m;
6903 uint16_t n;
6904 TIFFDirEntry *o;
6905 m = 0;
6906 for (n = 0, o = dir; n < dircount; n++, o++)
6907 {
6908 if (o->tdir_tag < m)
6909 {
6910 TIFFWarningExtR(tif, module,
6911 "Invalid TIFF directory; tags are not sorted in "
6912 "ascending order");
6913 break;
6914 }
6915 m = o->tdir_tag + 1U;
6916 }
6917}
6918
6934 uint16_t dircount,
6935 uint16_t tagid)
6936{
6937 TIFFDirEntry *m;
6938 uint16_t n;
6939 (void)tif;
6940 for (m = dir, n = 0; n < dircount; m++, n++)
6941 {
6942 if (m->tdir_tag == tagid)
6943 return (m);
6944 }
6945 return (0);
6946}
6947
6963static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
6964 uint32_t *fii)
6965{
6966 int32_t ma, mb, mc;
6967 ma = -1;
6968 mc = (int32_t)tif->tif_nfields;
6969 while (1)
6970 {
6971 if (ma + 1 == mc)
6972 {
6973 *fii = FAILED_FII;
6974 return;
6975 }
6976 mb = (ma + mc) / 2;
6977 if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
6978 break;
6979 if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
6980 ma = mb;
6981 else
6982 mc = mb;
6983 }
6984 while (1)
6985 {
6986 if (mb == 0)
6987 break;
6988 if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
6989 break;
6990 mb--;
6991 }
6992 *fii = (uint32_t)mb;
6993}
6994
6995/*
6996 * Read custom directory from the arbitrary offset.
6997 * The code is very similar to TIFFReadDirectory().
6998 */
7000 const TIFFFieldArray *infoarray)
7001{
7002 static const char module[] = "TIFFReadCustomDirectory";
7003 TIFFDirEntry *dir;
7004 uint16_t dircount;
7005 TIFFDirEntry *dp;
7006 uint16_t di;
7007 const TIFFField *fip;
7008 uint32_t fii;
7009
7010 assert(infoarray != NULL);
7011 dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
7012 if (!dircount)
7013 {
7014 TIFFErrorExtR(tif, module,
7015 "Failed to read custom directory at offset %" PRIu64,
7016 diroff);
7017 return 0;
7018 }
7019 TIFFReadDirectoryCheckOrder(tif, dir, dircount);
7020
7021 /*
7022 * Mark duplicates of any tag to be ignored (bugzilla 1994)
7023 * to avoid certain pathological problems.
7024 */
7025 {
7026 TIFFDirEntry *ma;
7027 uint16_t mb;
7028 for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
7029 {
7030 TIFFDirEntry *na;
7031 uint16_t nb;
7032 for (na = ma + 1, nb = (uint16_t)(mb + 1); nb < dircount;
7033 na++, nb++)
7034 {
7035 if (ma->tdir_tag == na->tdir_tag)
7036 {
7037 na->tdir_ignore = TRUE;
7038 }
7039 }
7040 }
7041 }
7042
7043 /* Free any old stuff and reinit. */
7044 TIFFFreeDirectory(tif);
7045 /* Even if custom directories do not need the default settings of a standard
7046 * IFD, the pointer to the TIFFSetField() and TIFFGetField() (i.e.
7047 * tif->tif_tagmethods.vsetfield and tif->tif_tagmethods.vgetfield) need to
7048 * be initialized, which is done in TIFFDefaultDirectory().
7049 * After that, the field array for the custom tags needs to be setup again.
7050 */
7052 _TIFFSetupFields(tif, infoarray);
7053
7054 /* Allocate arrays for offset values outside IFD entry for IFD data size
7055 * checking. Note: Counter are reset within TIFFFreeDirectory(). */
7058 tif,
7059 (tmsize_t)((size_t)dircount * sizeof(TIFFEntryOffsetAndLength)));
7060 if (tif->tif_dir.td_dirdatasize_offsets == NULL)
7061 {
7063 tif, module,
7064 "Failed to allocate memory for counting IFD data size at reading");
7065 if (dir)
7066 _TIFFfreeExt(tif, dir);
7067 return 0;
7068 }
7069
7070 for (di = 0, dp = dir; di < dircount; di++, dp++)
7071 {
7073 if (fii == FAILED_FII)
7074 {
7075 /* Before creating an anonymous field, check whether this
7076 * tag is defined in the default TIFF field array. Tags
7077 * like GPSIFD or EXIFIFD have known scalar types there
7078 * (e.g. TIFF_SETGET_UINT64 with passcount=FALSE) that
7079 * must be preserved; an anonymous field would change their
7080 * get/set convention to the passcount (count+pointer)
7081 * form, causing va_arg mismatches in callers that expect
7082 * the documented convention.
7083 *
7084 * Only FIELD_CUSTOM descriptors are usable here. The
7085 * other descriptors rely on specialised handling in
7086 * TIFFReadDirectory() and _TIFFVSetField() that custom
7087 * directories do not provide: ColorMap and
7088 * TransferFunction (TIFF_SETGET_OTHER) would trigger an
7089 * assertion abort inside TIFFFetchNormalTag();
7090 * StripOffsets and StripByteCounts
7091 * (TIFF_SETGET_UNDEFINED) would report an error instead
7092 * of being read; FIELD_IGNORE entries would be silently
7093 * discarded; and Compression would call
7094 * TIFFSetCompressionScheme() while reading a custom
7095 * directory. Such tags keep using the anonymous path,
7096 * as before.
7097 *
7098 * Also verify that the on-disk type is compatible with
7099 * the definition. EXIFIFD/GPSIFD are defined as
7100 * TIFF_LONG8 but appear as TIFF_LONG or TIFF_IFD in
7101 * ClassicTIFF files; the TIFFReadDirEntry*() functions
7102 * already promote within the LONG/IFD family, so those
7103 * types are accepted as mutually compatible. */
7104 const TIFFFieldArray *default_fields = _TIFFGetFields();
7105 const TIFFField *fld = NULL;
7106 for (uint32_t k = 0; k < default_fields->count; k++)
7107 {
7108 const TIFFField *candidate = &default_fields->fields[k];
7109 if (candidate->field_tag != (uint32_t)dp->tdir_tag)
7110 continue;
7111 if (candidate->field_bit != FIELD_CUSTOM)
7112 continue;
7113 if (candidate->set_get_field_type == TIFF_SETGET_OTHER ||
7115 continue;
7116 /* Check type compatibility: TIFF_ANY matches
7117 * everything; otherwise the on-disk type must match
7118 * the definition, or both must be in the IFD/LONG
7119 * family (LONG, IFD, LONG8, IFD8). */
7120 if (candidate->field_type != TIFF_ANY &&
7121 candidate->field_type != dp->tdir_type)
7122 {
7123 if ((candidate->field_type == TIFF_LONG ||
7124 candidate->field_type == TIFF_IFD ||
7125 candidate->field_type == TIFF_LONG8 ||
7126 candidate->field_type == TIFF_IFD8) &&
7127 (dp->tdir_type == TIFF_LONG ||
7128 dp->tdir_type == TIFF_IFD ||
7129 dp->tdir_type == TIFF_LONG8 ||
7130 dp->tdir_type == TIFF_IFD8))
7131 {
7132 /* IFD-family types are compatible */
7133 }
7134 else
7135 {
7136 continue;
7137 }
7138 }
7139 fld = candidate;
7140 break;
7141 }
7142 if (fld == NULL)
7143 {
7145 {
7146 TIFFWarningExtR(tif, module,
7147 "Unknown field with tag %" PRIu16
7148 " (0x%" PRIx16 ") encountered",
7149 dp->tdir_tag, dp->tdir_tag);
7150 }
7151 fld = _TIFFCreateAnonField(tif, dp->tdir_tag,
7152 (TIFFDataType)dp->tdir_type);
7153 }
7154 if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
7155 {
7157 {
7159 tif, module,
7160 "Registering anonymous field with tag %" PRIu16
7161 " (0x%" PRIx16 ") failed",
7162 dp->tdir_tag, dp->tdir_tag);
7163 }
7164 dp->tdir_ignore = TRUE;
7165 }
7166 else
7167 {
7169 assert(fii != FAILED_FII);
7170 }
7171 }
7172 if (!dp->tdir_ignore)
7173 {
7174 fip = tif->tif_fields[fii];
7175 if (fip->field_bit == FIELD_IGNORE)
7176 dp->tdir_ignore = TRUE;
7177 else
7178 {
7179 /* check data type - IFD-family types (LONG, IFD,
7180 * LONG8, IFD8) are mutually compatible because the
7181 * TIFFReadDirEntry*() functions promote them. */
7182 while ((fip->field_type != TIFF_ANY) &&
7183 (fip->field_type != dp->tdir_type) &&
7184 !((fip->field_type == TIFF_LONG ||
7185 fip->field_type == TIFF_IFD ||
7186 fip->field_type == TIFF_LONG8 ||
7187 fip->field_type == TIFF_IFD8) &&
7188 (dp->tdir_type == TIFF_LONG ||
7189 dp->tdir_type == TIFF_IFD ||
7190 dp->tdir_type == TIFF_LONG8 ||
7191 dp->tdir_type == TIFF_IFD8)))
7192 {
7193 fii++;
7194 if ((fii == tif->tif_nfields) ||
7195 (tif->tif_fields[fii]->field_tag !=
7196 (uint32_t)dp->tdir_tag))
7197 {
7198 fii = 0xFFFF;
7199 break;
7200 }
7201 fip = tif->tif_fields[fii];
7202 }
7203 if (fii == 0xFFFF)
7204 {
7205 TIFFWarningExtR(tif, module,
7206 "Wrong data type %" PRIu16
7207 " for \"%s\"; tag ignored",
7208 dp->tdir_type, fip->field_name);
7209 dp->tdir_ignore = TRUE;
7210 }
7211 else
7212 {
7213 /* check count if known in advance */
7214 if ((fip->field_readcount != TIFF_VARIABLE) &&
7216 {
7217 uint32_t expected;
7218 if (fip->field_readcount == TIFF_SPP)
7219 expected =
7220 (uint32_t)tif->tif_dir.td_samplesperpixel;
7221 else
7222 expected = (uint32_t)fip->field_readcount;
7223 if (!CheckDirCount(tif, dp, expected))
7224 dp->tdir_ignore = TRUE;
7225 }
7226 }
7227 }
7228 if (!dp->tdir_ignore)
7229 {
7230 switch (dp->tdir_tag)
7231 {
7233 if (!TIFFFieldIsAnonymous(fip))
7234 {
7235 /* should only be called on a Exif directory */
7236 /* when exifFields[] is active */
7237 (void)TIFFFetchSubjectDistance(tif, dp);
7238 }
7239 else
7240 {
7241 (void)TIFFFetchNormalTag(tif, dp, TRUE);
7242 }
7243 break;
7244 default:
7245 (void)TIFFFetchNormalTag(tif, dp, TRUE);
7246 break;
7247 }
7248 } /*-- if (!dp->tdir_ignore) */
7249 }
7250 }
7251 /* Evaluate final IFD data size. */
7252 CalcFinalIFDdatasizeReading(tif, dircount);
7253
7254 /* To be able to return from SubIFD or custom-IFD to main-IFD */
7256 if (dir)
7257 _TIFFfreeExt(tif, dir);
7258 return 1;
7259}
7260
7261/*
7262 * EXIF is important special case of custom IFD, so we have a special
7263 * function to read it.
7264 */
7266{
7267 return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
7268}
7269
7270/*
7271 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
7272 */
7274{
7275 return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
7276}
7277
7300 uint16_t dircount)
7301{
7302 static const char module[] = "EstimateStripByteCounts";
7303
7304 TIFFDirEntry *dp;
7305 TIFFDirectory *td = &tif->tif_dir;
7306 uint32_t strip;
7307
7308 /* Do not try to load stripbytecount as we will compute it */
7309 if (!_TIFFFillStrilesInternal(tif, 0))
7310 return -1;
7311
7312 const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
7313 uint64_t filesize = 0;
7314 if (allocsize > 100 * 1024 * 1024)
7315 {
7316 /* Before allocating a huge amount of memory for corrupted files, check
7317 * if size of requested memory is not greater than file size. */
7318 filesize = TIFFGetFileSize(tif);
7319 if (allocsize > filesize)
7320 {
7322 tif, module,
7323 "Requested memory size for StripByteCounts of %" PRIu64
7324 " is greater than filesize %" PRIu64 ". Memory not allocated",
7325 allocsize, filesize);
7326 return -1;
7327 }
7328 }
7329
7330 if (td->td_stripbytecount_p)
7332 td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
7333 tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
7334 if (td->td_stripbytecount_p == NULL)
7335 return -1;
7336
7338 {
7339 uint64_t space;
7340 uint16_t n;
7341 if (!(tif->tif_flags & TIFF_BIGTIFF))
7342 space = sizeof(TIFFHeaderClassic) + 2 +
7343 (unsigned long)dircount * 12 + 4;
7344 else
7345 space =
7346 sizeof(TIFFHeaderBig) + 8 + (unsigned long)dircount * 20 + 8;
7347 /* calculate amount of space used by indirect values */
7348 for (dp = dir, n = dircount; n > 0; n--, dp++)
7349 {
7350 uint32_t typewidth;
7351 uint64_t datasize;
7352 typewidth = (uint32_t)TIFFDataWidth((TIFFDataType)dp->tdir_type);
7353 if (typewidth == 0)
7354 {
7356 tif, module,
7357 "Cannot determine size of unknown tag type %" PRIu16,
7358 dp->tdir_type);
7359 return -1;
7360 }
7361 if (dp->tdir_count > UINT64_MAX / typewidth)
7362 return -1;
7363 datasize = (uint64_t)typewidth * dp->tdir_count;
7364 if (!(tif->tif_flags & TIFF_BIGTIFF))
7365 {
7366 if (datasize <= 4)
7367 datasize = 0;
7368 }
7369 else
7370 {
7371 if (datasize <= 8)
7372 datasize = 0;
7373 }
7374 if (space > UINT64_MAX - datasize)
7375 return -1;
7376 space += datasize;
7377 }
7378 if (filesize == 0)
7379 filesize = TIFFGetFileSize(tif);
7380 if (filesize < space)
7381 /* we should perhaps return in error ? */
7382 space = filesize;
7383 else
7384 space = filesize - space;
7386 space /= td->td_samplesperpixel;
7387 for (strip = 0; strip < td->td_nstrips; strip++)
7388 td->td_stripbytecount_p[strip] = space;
7389 /*
7390 * This gross hack handles the case were the offset to
7391 * the last strip is past the place where we think the strip
7392 * should begin. Since a strip of data must be contiguous,
7393 * it's safe to assume that we've overestimated the amount
7394 * of data in the strip and trim this number back accordingly.
7395 */
7396 strip--;
7397 if (td->td_stripoffset_p[strip] >
7398 UINT64_MAX - td->td_stripbytecount_p[strip])
7399 return -1;
7400 if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
7401 filesize)
7402 {
7403 if (td->td_stripoffset_p[strip] >= filesize)
7404 {
7405 /* Not sure what we should in that case... */
7406 td->td_stripbytecount_p[strip] = 0;
7407 }
7408 else
7409 {
7410 td->td_stripbytecount_p[strip] =
7411 filesize - td->td_stripoffset_p[strip];
7412 }
7413 }
7414 }
7415 else if (isTiled(tif))
7416 {
7417 uint64_t bytespertile = TIFFTileSize64(tif);
7418
7419 for (strip = 0; strip < td->td_nstrips; strip++)
7420 td->td_stripbytecount_p[strip] = bytespertile;
7421 }
7422 else
7423 {
7424 uint64_t rowbytes = TIFFScanlineSize64(tif);
7425 uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
7426 for (strip = 0; strip < td->td_nstrips; strip++)
7427 {
7428 if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
7429 return -1;
7430 td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
7431 }
7432 }
7436 return 1;
7437}
7438
7449static void MissingRequired(TIFF *tif, const char *tagname)
7450{
7451 static const char module[] = "MissingRequired";
7452
7453 TIFFErrorExtR(tif, module,
7454 "TIFF directory is missing required \"%s\" field", tagname);
7455}
7456
7469static unsigned long hashFuncOffsetToNumber(const void *elt)
7470{
7471 const TIFFOffsetAndDirNumber *offsetAndDirNumber =
7472 (const TIFFOffsetAndDirNumber *)elt;
7473 const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
7474 ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
7475 return hash;
7476}
7477
7491static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
7492{
7493 const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
7494 (const TIFFOffsetAndDirNumber *)elt1;
7495 const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
7496 (const TIFFOffsetAndDirNumber *)elt2;
7497 return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
7498}
7499
7512static unsigned long hashFuncNumberToOffset(const void *elt)
7513{
7514 const TIFFOffsetAndDirNumber *offsetAndDirNumber =
7515 (const TIFFOffsetAndDirNumber *)elt;
7516 return offsetAndDirNumber->dirNumber;
7517}
7518
7532static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
7533{
7534 const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
7535 (const TIFFOffsetAndDirNumber *)elt1;
7536 const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
7537 (const TIFFOffsetAndDirNumber *)elt2;
7538 return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
7539}
7540
7558 TIFFOffsetAndDirNumber *entryPtr)
7559{
7561 {
7562 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7563 "Insertion in tif_map_dir_offset_to_number failed");
7564 return false;
7565 }
7567 {
7568 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7569 "Insertion in tif_map_dir_number_to_offset failed");
7571 {
7572 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7573 "Rollback of directory tracking entry failed");
7574 }
7575 return false;
7576 }
7577 return true;
7578}
7579
7580int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
7581{
7582 if (diroff == 0) /* no more directories */
7583 return 0;
7584
7585 if (tif->tif_map_dir_offset_to_number == NULL)
7586 {
7589 if (tif->tif_map_dir_offset_to_number == NULL)
7590 {
7591 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7592 "Not enough memory");
7593 return 0;
7594 }
7595 }
7596
7597 if (tif->tif_map_dir_number_to_offset == NULL)
7598 {
7599 /* No free callback for this map, as it shares the same items as
7600 * tif->tif_map_dir_offset_to_number. */
7603 if (tif->tif_map_dir_number_to_offset == NULL)
7604 {
7605 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7606 "Not enough memory");
7607 return 0;
7608 }
7609 }
7610
7611 /* Check if offset is already in the list:
7612 * - yes: check, if offset is at the same IFD number - if not, it is an IFD
7613 * loop
7614 * - no: add to list or update offset at that IFD number
7615 */
7617 entry.offset = diroff;
7618 entry.dirNumber = dirn;
7619
7620 TIFFOffsetAndDirNumber *foundEntry =
7622 tif->tif_map_dir_offset_to_number, &entry);
7623 if (foundEntry)
7624 {
7625 if (foundEntry->dirNumber == dirn)
7626 {
7627 return 1;
7628 }
7629 else
7630 {
7631 TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
7632 "TIFF directory %d has IFD looping to directory %u "
7633 "at offset 0x%" PRIx64 " (%" PRIu64 ")",
7634 (int)dirn - 1, foundEntry->dirNumber, diroff,
7635 diroff);
7636 return 0;
7637 }
7638 }
7639
7640 /* Check if offset of an IFD has been changed and update offset of that IFD
7641 * number. */
7643 tif->tif_map_dir_number_to_offset, &entry);
7644 if (foundEntry)
7645 {
7646 if (foundEntry->offset != diroff)
7647 {
7648 TIFFOffsetAndDirNumber entryOld;
7649 entryOld.offset = foundEntry->offset;
7650 entryOld.dirNumber = dirn;
7651 /* We must remove first from tif_map_dir_number_to_offset as the */
7652 /* entry is owned (and thus freed) by */
7653 /* tif_map_dir_offset_to_number */
7654 TIFFOffsetAndDirNumber *foundEntryOld =
7656 tif->tif_map_dir_number_to_offset, &entryOld);
7657 if (foundEntryOld)
7658 {
7660 foundEntryOld);
7661 }
7662 foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
7663 tif->tif_map_dir_offset_to_number, &entryOld);
7664 if (foundEntryOld)
7665 {
7667 foundEntryOld);
7668 }
7669
7670 TIFFOffsetAndDirNumber *entryPtr =
7672 tif, sizeof(TIFFOffsetAndDirNumber));
7673 if (entryPtr == NULL)
7674 {
7675 return 0;
7676 }
7677
7678 /* Add IFD offset and dirn to IFD directory list */
7679 *entryPtr = entry;
7680
7681 if (!insertOffsetAndDirNumber(tif, entryPtr))
7682 {
7683 return 0;
7684 }
7685 }
7686 return 1;
7687 }
7688
7689 /* Arbitrary (hopefully big enough) limit */
7692 {
7693 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7694 "Cannot handle more than %u TIFF directories",
7695 (unsigned)TIFF_MAX_DIR_COUNT);
7696 return 0;
7697 }
7698
7700 tif, sizeof(TIFFOffsetAndDirNumber));
7701 if (entryPtr == NULL)
7702 {
7703 TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
7704 "Cannot allocate directory tracking entry");
7705 return 0;
7706 }
7707
7708 /* Add IFD offset and dirn to IFD directory list */
7709 *entryPtr = entry;
7710
7711 if (!insertOffsetAndDirNumber(tif, entryPtr))
7712 {
7713 return 0;
7714 }
7715
7716 return 1;
7717} /* --- _TIFFCheckDirNumberAndOffset() ---*/
7718
7719int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
7720{
7721 if (diroff == 0) /* no more directories */
7722 return 0;
7723
7724 /* Check if offset is already in the list and return matching directory
7725 * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
7726 * search again in IFD list.
7727 */
7728 if (tif->tif_map_dir_offset_to_number == NULL)
7729 return 0;
7731 entry.offset = diroff;
7732 entry.dirNumber = 0; /* not used */
7733
7734 TIFFOffsetAndDirNumber *foundEntry =
7736 tif->tif_map_dir_offset_to_number, &entry);
7737 if (foundEntry)
7738 {
7739 *dirn = foundEntry->dirNumber;
7740 return 1;
7741 }
7742
7743 /* This updates the directory list for all main-IFDs in the file. */
7745
7747 tif->tif_map_dir_offset_to_number, &entry);
7748 if (foundEntry)
7749 {
7750 *dirn = foundEntry->dirNumber;
7751 return 1;
7752 }
7753
7754 return 0;
7755} /*--- _TIFFGetDirNumberFromOffset() ---*/
7756
7757int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
7758{
7759
7760 if (tif->tif_map_dir_number_to_offset == NULL)
7761 return 0;
7763 entry.offset = 0; /* not used */
7764 entry.dirNumber = dirn;
7765
7766 TIFFOffsetAndDirNumber *foundEntry =
7768 tif->tif_map_dir_number_to_offset, &entry);
7769 if (foundEntry)
7770 {
7771 *diroff = foundEntry->offset;
7772 return 1;
7773 }
7774
7775 return 0;
7776} /*--- _TIFFGetOffsetFromDirNumber() ---*/
7777
7779{
7780 if (tif->tif_map_dir_offset_to_number == NULL)
7781 return 1;
7782
7783 TIFFOffsetAndDirNumber entryOld;
7784 entryOld.offset = diroff;
7785 entryOld.dirNumber = 0;
7786 /* We must remove first from tif_map_dir_number_to_offset as the
7787 * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
7788 * However, we need firstly to find the directory number from offset. */
7789
7790 TIFFOffsetAndDirNumber *foundEntryOldOff =
7792 tif->tif_map_dir_offset_to_number, &entryOld);
7793 if (foundEntryOldOff)
7794 {
7795 entryOld.dirNumber = foundEntryOldOff->dirNumber;
7796 if (tif->tif_map_dir_number_to_offset != NULL)
7797 {
7798 TIFFOffsetAndDirNumber *foundEntryOldDir =
7800 tif->tif_map_dir_number_to_offset, &entryOld);
7801 if (foundEntryOldDir)
7802 {
7804 foundEntryOldDir);
7806 foundEntryOldOff);
7807 return 1;
7808 }
7809 }
7810 else
7811 {
7812 TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
7813 "Unexpectedly tif_map_dir_number_to_offset is "
7814 "missing but tif_map_dir_offset_to_number exists.");
7815 return 0;
7816 }
7817 }
7818 return 1;
7819} /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
7820
7821/*
7822 * Check the count field of a directory entry against a known value. The
7823 * caller is expected to skip/ignore the tag if there is a mismatch.
7824 */
7839static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
7840{
7841 if ((uint64_t)count > dir->tdir_count)
7842 {
7843 const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7844 TIFFWarningExtR(tif, tif->tif_name,
7845 "incorrect count for field \"%s\" (%" PRIu64
7846 ", expecting %" PRIu32 "); tag ignored",
7847 fip ? fip->field_name : "unknown tagname",
7848 dir->tdir_count, count);
7849 return (0);
7850 }
7851 else if ((uint64_t)count < dir->tdir_count)
7852 {
7853 const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7854 TIFFWarningExtR(tif, tif->tif_name,
7855 "incorrect count for field \"%s\" (%" PRIu64
7856 ", expecting %" PRIu32 "); tag trimmed",
7857 fip ? fip->field_name : "unknown tagname",
7858 dir->tdir_count, count);
7859 dir->tdir_count = count;
7860 return (1);
7861 }
7862 return (1);
7863}
7864
7865/*
7866 * Read IFD structure from the specified offset. If the pointer to
7867 * nextdiroff variable has been specified, read it too. Function returns a
7868 * number of fields in the directory or 0 if failed.
7869 */
7895static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
7896 TIFFDirEntry **pdir, uint64_t *nextdiroff)
7897{
7898 static const char module[] = "TIFFFetchDirectory";
7899
7900 void *origdir;
7901 uint16_t dircount16;
7902 uint32_t dirsize;
7903 TIFFDirEntry *dir;
7904 uint8_t *ma;
7905 TIFFDirEntry *mb;
7906 uint16_t n;
7907
7908 assert(pdir);
7909
7910 tif->tif_diroff = diroff;
7911 if (nextdiroff)
7912 *nextdiroff = 0;
7913 if (!isMapped(tif))
7914 {
7915 if (!SeekOK(tif, tif->tif_diroff))
7916 {
7917 TIFFErrorExtR(tif, module,
7918 "%s: Seek error accessing TIFF directory",
7919 tif->tif_name);
7920 return 0;
7921 }
7922 if (!(tif->tif_flags & TIFF_BIGTIFF))
7923 {
7924 if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
7925 {
7926 TIFFErrorExtR(tif, module,
7927 "%s: Can not read TIFF directory count",
7928 tif->tif_name);
7929 return 0;
7930 }
7931 if (tif->tif_flags & TIFF_SWAB)
7932 TIFFSwabShort(&dircount16);
7933 if (dircount16 > 4096)
7934 {
7935 TIFFErrorExtR(tif, module,
7936 "Sanity check on directory count failed, this is "
7937 "probably not a valid IFD offset");
7938 return 0;
7939 }
7940 dirsize = 12;
7941 }
7942 else
7943 {
7944 uint64_t dircount64;
7945 if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
7946 {
7947 TIFFErrorExtR(tif, module,
7948 "%s: Can not read TIFF directory count",
7949 tif->tif_name);
7950 return 0;
7951 }
7952 if (tif->tif_flags & TIFF_SWAB)
7953 TIFFSwabLong8(&dircount64);
7954 if (dircount64 > 4096)
7955 {
7956 TIFFErrorExtR(tif, module,
7957 "Sanity check on directory count failed, this is "
7958 "probably not a valid IFD offset");
7959 return 0;
7960 }
7961 dircount16 = (uint16_t)dircount64;
7962 dirsize = 20;
7963 }
7964 origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
7965 "to read TIFF directory");
7966 if (origdir == NULL)
7967 return 0;
7968 if (!ReadOK(tif, origdir, (tmsize_t)dircount16 * dirsize))
7969 {
7970 TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
7971 tif->tif_name);
7972 _TIFFfreeExt(tif, origdir);
7973 return 0;
7974 }
7975 /*
7976 * Read offset to next directory for sequential scans if
7977 * needed.
7978 */
7979 if (nextdiroff)
7980 {
7981 if (!(tif->tif_flags & TIFF_BIGTIFF))
7982 {
7983 uint32_t nextdiroff32;
7984 if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
7985 nextdiroff32 = 0;
7986 if (tif->tif_flags & TIFF_SWAB)
7987 TIFFSwabLong(&nextdiroff32);
7988 *nextdiroff = nextdiroff32;
7989 }
7990 else
7991 {
7992 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
7993 *nextdiroff = 0;
7994 if (tif->tif_flags & TIFF_SWAB)
7995 TIFFSwabLong8(nextdiroff);
7996 }
7997 }
7998 }
7999 else
8000 {
8001 tmsize_t m;
8002 tmsize_t off;
8003 if (tif->tif_diroff > (uint64_t)INT64_MAX)
8004 {
8005 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
8006 return (0);
8007 }
8008 off = (tmsize_t)tif->tif_diroff;
8009
8010 /*
8011 * Check for integer overflow when validating the dir_off,
8012 * otherwise a very high offset may cause an OOB read and
8013 * crash the client. Make two comparisons instead of
8014 *
8015 * off + sizeof(uint16_t) > tif->tif_size
8016 *
8017 * to avoid overflow.
8018 */
8019 if (!(tif->tif_flags & TIFF_BIGTIFF))
8020 {
8021 m = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
8022 if ((m < off) || ((uint64_t)m < sizeof(uint16_t)) ||
8023 ((uint64_t)m > (uint64_t)tif->tif_size))
8024 {
8025 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
8026 return 0;
8027 }
8028 else
8029 {
8030 _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
8031 }
8032 off = (tmsize_t)((uint64_t)off + sizeof(uint16_t));
8033 if (tif->tif_flags & TIFF_SWAB)
8034 TIFFSwabShort(&dircount16);
8035 if (dircount16 > 4096)
8036 {
8037 TIFFErrorExtR(tif, module,
8038 "Sanity check on directory count failed, this is "
8039 "probably not a valid IFD offset");
8040 return 0;
8041 }
8042 dirsize = 12;
8043 }
8044 else
8045 {
8046 uint64_t dircount64;
8047 m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
8048 if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
8049 ((uint64_t)m > (uint64_t)tif->tif_size))
8050 {
8051 TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
8052 return 0;
8053 }
8054 else
8055 {
8056 _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
8057 }
8058 off = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
8059 if (tif->tif_flags & TIFF_SWAB)
8060 TIFFSwabLong8(&dircount64);
8061 if (dircount64 > 4096)
8062 {
8063 TIFFErrorExtR(tif, module,
8064 "Sanity check on directory count failed, this is "
8065 "probably not a valid IFD offset");
8066 return 0;
8067 }
8068 dircount16 = (uint16_t)dircount64;
8069 dirsize = 20;
8070 }
8071 if (dircount16 == 0)
8072 {
8073 TIFFErrorExtR(tif, module,
8074 "Sanity check on directory count failed, zero tag "
8075 "directories not supported");
8076 return 0;
8077 }
8078 /* Before allocating a huge amount of memory for corrupted files, check
8079 * if size of requested memory is not greater than file size. */
8080 uint64_t filesize = TIFFGetFileSize(tif);
8081 uint64_t allocsize = (uint64_t)dircount16 * dirsize;
8082 if (allocsize > filesize)
8083 {
8085 tif, module,
8086 "Requested memory size for TIFF directory of %" PRIu64
8087 " is greater than filesize %" PRIu64
8088 ". Memory not allocated, TIFF directory not read",
8089 allocsize, filesize);
8090 return 0;
8091 }
8092 origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
8093 "to read TIFF directory");
8094 if (origdir == NULL)
8095 return 0;
8096 m = off + dircount16 * dirsize;
8097 if ((m < off) || (m < (tmsize_t)dircount16 * (tmsize_t)dirsize) ||
8098 (m > tif->tif_size))
8099 {
8100 TIFFErrorExtR(tif, module, "Can not read TIFF directory");
8101 _TIFFfreeExt(tif, origdir);
8102 return 0;
8103 }
8104 else
8105 {
8106 _TIFFmemcpy(origdir, tif->tif_base + off,
8107 (tmsize_t)dircount16 * dirsize);
8108 }
8109 if (nextdiroff)
8110 {
8111 off += dircount16 * dirsize;
8112 if (!(tif->tif_flags & TIFF_BIGTIFF))
8113 {
8114 uint32_t nextdiroff32;
8115 m = (tmsize_t)((uint64_t)off + sizeof(uint32_t));
8116 if ((m < off) || ((uint64_t)m < sizeof(uint32_t)) ||
8117 ((uint64_t)m > (uint64_t)tif->tif_size))
8118 nextdiroff32 = 0;
8119 else
8120 _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
8121 sizeof(uint32_t));
8122 if (tif->tif_flags & TIFF_SWAB)
8123 TIFFSwabLong(&nextdiroff32);
8124 *nextdiroff = nextdiroff32;
8125 }
8126 else
8127 {
8128 m = (tmsize_t)((uint64_t)off + sizeof(uint64_t));
8129 if ((m < off) || ((uint64_t)m < sizeof(uint64_t)) ||
8130 ((uint64_t)m > (uint64_t)tif->tif_size))
8131 *nextdiroff = 0;
8132 else
8133 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
8134 sizeof(uint64_t));
8135 if (tif->tif_flags & TIFF_SWAB)
8136 TIFFSwabLong8(nextdiroff);
8137 }
8138 }
8139 }
8140 /* No check against filesize needed here because "dir" should have same size
8141 * than "origdir" checked above. */
8143 tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
8144 if (dir == 0)
8145 {
8146 _TIFFfreeExt(tif, origdir);
8147 return 0;
8148 }
8149 ma = (uint8_t *)origdir;
8150 mb = dir;
8151 for (n = 0; n < dircount16; n++)
8152 {
8153 mb->tdir_ignore = FALSE;
8154 if (tif->tif_flags & TIFF_SWAB)
8155 TIFFSwabShort((uint16_t *)ma);
8156 mb->tdir_tag = *(uint16_t *)ma;
8157 ma += sizeof(uint16_t);
8158 if (tif->tif_flags & TIFF_SWAB)
8159 TIFFSwabShort((uint16_t *)ma);
8160 mb->tdir_type = *(uint16_t *)ma;
8161 ma += sizeof(uint16_t);
8162 if (!(tif->tif_flags & TIFF_BIGTIFF))
8163 {
8164 if (tif->tif_flags & TIFF_SWAB)
8165 TIFFSwabLong((uint32_t *)ma);
8166 mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
8167 ma += sizeof(uint32_t);
8168 mb->tdir_offset.toff_long8 = 0;
8169 *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
8170 ma += sizeof(uint32_t);
8171 }
8172 else
8173 {
8174 if (tif->tif_flags & TIFF_SWAB)
8175 TIFFSwabLong8((uint64_t *)ma);
8176 mb->tdir_count = TIFFReadUInt64(ma);
8177 ma += sizeof(uint64_t);
8179 ma += sizeof(uint64_t);
8180 }
8181 mb++;
8182 }
8183 _TIFFfreeExt(tif, origdir);
8184 *pdir = dir;
8185 return dircount16;
8186}
8187
8188/*
8189 * Fetch a tag that is not handled by special case code.
8190 */
8217static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
8218{
8219 static const char module[] = "TIFFFetchNormalTag";
8220 enum TIFFReadDirEntryErr err;
8221 uint32_t fii;
8222 const TIFFField *fip = NULL;
8224 if (fii == FAILED_FII)
8225 {
8226 TIFFErrorExtR(tif, "TIFFFetchNormalTag",
8227 "No definition found for tag %" PRIu16, dp->tdir_tag);
8228 return 0;
8229 }
8230 fip = tif->tif_fields[fii];
8231 assert(fip != NULL); /* should not happen */
8232 assert(fip->set_get_field_type !=
8233 TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
8234 this in specialized code */
8235 assert(fip->set_get_field_type !=
8236 TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
8237 the case for pseudo-tags */
8239 switch (fip->set_get_field_type)
8240 {
8243 tif, "TIFFFetchNormalTag",
8244 "Defined set_get_field_type of custom tag %u (%s) is "
8245 "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
8246 fip->field_tag, fip->field_name);
8247 break;
8248 case TIFF_SETGET_ASCII:
8249 {
8250 uint8_t *data;
8251 assert(fip->field_passcount == 0);
8252 err = TIFFReadDirEntryByteArray(tif, dp, &data);
8253 if (err == TIFFReadDirEntryErrOk)
8254 {
8255 size_t mb = 0;
8256 int n;
8257 if (data != NULL)
8258 {
8259 if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
8260 {
8261 /* optimization: if data is known to be 0 terminated, we
8262 * can use strlen() */
8263 mb = strlen((const char *)data);
8264 }
8265 else
8266 {
8267 /* general case. equivalent to non-portable */
8268 /* mb = strnlen((const char*)data,
8269 * (uint32_t)dp->tdir_count); */
8270 uint8_t *ma = data;
8271 while (mb < (uint32_t)dp->tdir_count)
8272 {
8273 if (*ma == 0)
8274 break;
8275 ma++;
8276 mb++;
8277 }
8278 }
8279 }
8280 if (!EvaluateIFDdatasizeReading(tif, dp))
8281 {
8282 if (data != NULL)
8283 _TIFFfreeExt(tif, data);
8284 return (0);
8285 }
8286 if (mb + 1 < (uint32_t)dp->tdir_count)
8288 tif, module,
8289 "ASCII value for tag \"%s\" contains null byte in "
8290 "value; value incorrectly truncated during reading due "
8291 "to implementation limitations",
8292 fip->field_name);
8293 else if (mb + 1 > (uint32_t)dp->tdir_count)
8294 {
8295 TIFFWarningExtR(tif, module,
8296 "ASCII value for tag \"%s\" does not end "
8297 "in null byte. Forcing it to be null",
8298 fip->field_name);
8299 /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
8300 * larger than MAX_SIZE_TAG_DATA */
8301 assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
8302 uint8_t *o = (uint8_t *)_TIFFmallocExt(
8303 tif, (uint32_t)dp->tdir_count + 1);
8304 if (o == NULL)
8305 {
8306 if (data != NULL)
8307 _TIFFfreeExt(tif, data);
8308 return (0);
8309 }
8310 if (dp->tdir_count > 0)
8311 {
8312 _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
8313 }
8314 o[(uint32_t)dp->tdir_count] = 0;
8315 if (data != 0)
8316 _TIFFfreeExt(tif, data);
8317 data = o;
8318 }
8319 n = TIFFSetField(tif, dp->tdir_tag, data);
8320 if (data != 0)
8321 _TIFFfreeExt(tif, data);
8322 if (!n)
8323 return (0);
8324 }
8325 }
8326 break;
8327 case TIFF_SETGET_UINT8:
8328 {
8329 uint8_t data = 0;
8330 assert(fip->field_readcount == 1);
8331 assert(fip->field_passcount == 0);
8332 err = TIFFReadDirEntryByte(tif, dp, &data);
8333 if (err == TIFFReadDirEntryErrOk)
8334 {
8335 if (!TIFFSetField(tif, dp->tdir_tag, data))
8336 return (0);
8337 }
8338 }
8339 break;
8340 case TIFF_SETGET_SINT8:
8341 {
8342 int8_t data = 0;
8343 assert(fip->field_readcount == 1);
8344 assert(fip->field_passcount == 0);
8345 err = TIFFReadDirEntrySbyte(tif, dp, &data);
8346 if (err == TIFFReadDirEntryErrOk)
8347 {
8348 if (!TIFFSetField(tif, dp->tdir_tag, data))
8349 return (0);
8350 }
8351 }
8352 break;
8353 case TIFF_SETGET_UINT16:
8354 {
8355 uint16_t data;
8356 assert(fip->field_readcount == 1);
8357 assert(fip->field_passcount == 0);
8358 err = TIFFReadDirEntryShort(tif, dp, &data);
8359 if (err == TIFFReadDirEntryErrOk)
8360 {
8361 if (!TIFFSetField(tif, dp->tdir_tag, data))
8362 return (0);
8363 }
8364 }
8365 break;
8366 case TIFF_SETGET_SINT16:
8367 {
8368 int16_t data;
8369 assert(fip->field_readcount == 1);
8370 assert(fip->field_passcount == 0);
8371 err = TIFFReadDirEntrySshort(tif, dp, &data);
8372 if (err == TIFFReadDirEntryErrOk)
8373 {
8374 if (!TIFFSetField(tif, dp->tdir_tag, data))
8375 return (0);
8376 }
8377 }
8378 break;
8379 case TIFF_SETGET_UINT32:
8380 {
8381 uint32_t data;
8382 assert(fip->field_readcount == 1);
8383 assert(fip->field_passcount == 0);
8384 err = TIFFReadDirEntryLong(tif, dp, &data);
8385 if (err == TIFFReadDirEntryErrOk)
8386 {
8387 if (!TIFFSetField(tif, dp->tdir_tag, data))
8388 return (0);
8389 }
8390 }
8391 break;
8392 case TIFF_SETGET_SINT32:
8393 {
8394 int32_t data;
8395 assert(fip->field_readcount == 1);
8396 assert(fip->field_passcount == 0);
8397 err = TIFFReadDirEntrySlong(tif, dp, &data);
8398 if (err == TIFFReadDirEntryErrOk)
8399 {
8400 if (!TIFFSetField(tif, dp->tdir_tag, data))
8401 return (0);
8402 }
8403 }
8404 break;
8405 case TIFF_SETGET_UINT64:
8406 {
8407 uint64_t data;
8408 assert(fip->field_readcount == 1);
8409 assert(fip->field_passcount == 0);
8410 err = TIFFReadDirEntryLong8(tif, dp, &data);
8411 if (err == TIFFReadDirEntryErrOk)
8412 {
8413 if (!EvaluateIFDdatasizeReading(tif, dp))
8414 return 0;
8415 if (!TIFFSetField(tif, dp->tdir_tag, data))
8416 return (0);
8417 }
8418 }
8419 break;
8420 case TIFF_SETGET_SINT64:
8421 {
8422 int64_t data;
8423 assert(fip->field_readcount == 1);
8424 assert(fip->field_passcount == 0);
8425 err = TIFFReadDirEntrySlong8(tif, dp, &data);
8426 if (err == TIFFReadDirEntryErrOk)
8427 {
8428 if (!EvaluateIFDdatasizeReading(tif, dp))
8429 return 0;
8430 if (!TIFFSetField(tif, dp->tdir_tag, data))
8431 return (0);
8432 }
8433 }
8434 break;
8435 case TIFF_SETGET_FLOAT:
8436 {
8437 float data;
8438 assert(fip->field_readcount == 1);
8439 assert(fip->field_passcount == 0);
8440 err = TIFFReadDirEntryFloat(tif, dp, &data);
8441 if (err == TIFFReadDirEntryErrOk)
8442 {
8443 if (!EvaluateIFDdatasizeReading(tif, dp))
8444 return 0;
8445 if (!TIFFSetField(tif, dp->tdir_tag, (double)data))
8446 return (0);
8447 }
8448 }
8449 break;
8450 case TIFF_SETGET_DOUBLE:
8451 {
8452 double data;
8453 assert(fip->field_readcount == 1);
8454 assert(fip->field_passcount == 0);
8455 err = TIFFReadDirEntryDouble(tif, dp, &data);
8456 if (err == TIFFReadDirEntryErrOk)
8457 {
8458 if (!EvaluateIFDdatasizeReading(tif, dp))
8459 return 0;
8460 if (!TIFFSetField(tif, dp->tdir_tag, data))
8461 return (0);
8462 }
8463 }
8464 break;
8465 case TIFF_SETGET_IFD8:
8466 {
8467 uint64_t data;
8468 assert(fip->field_readcount == 1);
8469 assert(fip->field_passcount == 0);
8470 err = TIFFReadDirEntryIfd8(tif, dp, &data);
8471 if (err == TIFFReadDirEntryErrOk)
8472 {
8473 if (!EvaluateIFDdatasizeReading(tif, dp))
8474 return 0;
8475 if (!TIFFSetField(tif, dp->tdir_tag, data))
8476 return (0);
8477 }
8478 }
8479 break;
8481 {
8482 uint16_t *data;
8483 assert(fip->field_readcount == 2);
8484 assert(fip->field_passcount == 0);
8485 if (dp->tdir_count != 2)
8486 {
8487 TIFFWarningExtR(tif, module,
8488 "incorrect count for field \"%s\", expected 2, "
8489 "got %" PRIu64,
8490 fip->field_name, dp->tdir_count);
8491 return (0);
8492 }
8493 err = TIFFReadDirEntryShortArray(tif, dp, &data);
8494 if (err == TIFFReadDirEntryErrOk)
8495 {
8496 int m;
8497 assert(data); /* avoid CLang static Analyzer false positive */
8498 m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
8499 _TIFFfreeExt(tif, data);
8500 if (!m)
8501 return (0);
8502 }
8503 }
8504 break;
8506 {
8507 uint8_t *data;
8508 assert(fip->field_readcount >= 1);
8509 assert(fip->field_passcount == 0);
8510 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8511 {
8512 TIFFWarningExtR(tif, module,
8513 "incorrect count for field \"%s\", expected "
8514 "%d, got %" PRIu64,
8515 fip->field_name, (int)fip->field_readcount,
8516 dp->tdir_count);
8517 return (0);
8518 }
8519 else
8520 {
8521 err = TIFFReadDirEntryByteArray(tif, dp, &data);
8522 if (err == TIFFReadDirEntryErrOk)
8523 {
8524 if (!EvaluateIFDdatasizeReading(tif, dp))
8525 {
8526 if (data != 0)
8527 _TIFFfreeExt(tif, data);
8528 return 0;
8529 }
8530 int m;
8531 m = TIFFSetField(tif, dp->tdir_tag, data);
8532 if (data != 0)
8533 _TIFFfreeExt(tif, data);
8534 if (!m)
8535 return (0);
8536 }
8537 }
8538 }
8539 break;
8541 {
8542 int8_t *data;
8543 assert(fip->field_readcount >= 1);
8544 assert(fip->field_passcount == 0);
8545 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8546 {
8547 TIFFWarningExtR(tif, module,
8548 "incorrect count for field \"%s\", expected "
8549 "%d, got %" PRIu64,
8550 fip->field_name, (int)fip->field_readcount,
8551 dp->tdir_count);
8552 return (0);
8553 }
8554 else
8555 {
8556 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
8557 if (err == TIFFReadDirEntryErrOk)
8558 {
8559 if (!EvaluateIFDdatasizeReading(tif, dp))
8560 {
8561 if (data != 0)
8562 _TIFFfreeExt(tif, data);
8563 return 0;
8564 }
8565 int m;
8566 m = TIFFSetField(tif, dp->tdir_tag, data);
8567 if (data != 0)
8568 _TIFFfreeExt(tif, data);
8569 if (!m)
8570 return (0);
8571 }
8572 }
8573 }
8574 break;
8576 {
8577 uint16_t *data;
8578 assert(fip->field_readcount >= 1);
8579 assert(fip->field_passcount == 0);
8580 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8581 {
8582 TIFFWarningExtR(tif, module,
8583 "incorrect count for field \"%s\", expected "
8584 "%d, got %" PRIu64,
8585 fip->field_name, (int)fip->field_readcount,
8586 dp->tdir_count);
8587 return (0);
8588 }
8589 else
8590 {
8591 err = TIFFReadDirEntryShortArray(tif, dp, &data);
8592 if (err == TIFFReadDirEntryErrOk)
8593 {
8594 if (!EvaluateIFDdatasizeReading(tif, dp))
8595 {
8596 if (data != 0)
8597 _TIFFfreeExt(tif, data);
8598 return 0;
8599 }
8600 int m;
8601 m = TIFFSetField(tif, dp->tdir_tag, data);
8602 if (data != 0)
8603 _TIFFfreeExt(tif, data);
8604 if (!m)
8605 return (0);
8606 }
8607 }
8608 }
8609 break;
8611 {
8612 int16_t *data;
8613 assert(fip->field_readcount >= 1);
8614 assert(fip->field_passcount == 0);
8615 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8616 {
8617 TIFFWarningExtR(tif, module,
8618 "incorrect count for field \"%s\", expected "
8619 "%d, got %" PRIu64,
8620 fip->field_name, (int)fip->field_readcount,
8621 dp->tdir_count);
8622 return (0);
8623 }
8624 else
8625 {
8626 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
8627 if (err == TIFFReadDirEntryErrOk)
8628 {
8629 if (!EvaluateIFDdatasizeReading(tif, dp))
8630 {
8631 if (data != 0)
8632 _TIFFfreeExt(tif, data);
8633 return 0;
8634 }
8635 int m;
8636 m = TIFFSetField(tif, dp->tdir_tag, data);
8637 if (data != 0)
8638 _TIFFfreeExt(tif, data);
8639 if (!m)
8640 return (0);
8641 }
8642 }
8643 }
8644 break;
8646 {
8647 uint32_t *data;
8648 assert(fip->field_readcount >= 1);
8649 assert(fip->field_passcount == 0);
8650 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8651 {
8652 TIFFWarningExtR(tif, module,
8653 "incorrect count for field \"%s\", expected "
8654 "%d, got %" PRIu64,
8655 fip->field_name, (int)fip->field_readcount,
8656 dp->tdir_count);
8657 return (0);
8658 }
8659 else
8660 {
8661 err = TIFFReadDirEntryLongArray(tif, dp, &data);
8662 if (err == TIFFReadDirEntryErrOk)
8663 {
8664 if (!EvaluateIFDdatasizeReading(tif, dp))
8665 {
8666 if (data != 0)
8667 _TIFFfreeExt(tif, data);
8668 return 0;
8669 }
8670 int m;
8671 m = TIFFSetField(tif, dp->tdir_tag, data);
8672 if (data != 0)
8673 _TIFFfreeExt(tif, data);
8674 if (!m)
8675 return (0);
8676 }
8677 }
8678 }
8679 break;
8681 {
8682 int32_t *data;
8683 assert(fip->field_readcount >= 1);
8684 assert(fip->field_passcount == 0);
8685 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8686 {
8687 TIFFWarningExtR(tif, module,
8688 "incorrect count for field \"%s\", expected "
8689 "%d, got %" PRIu64,
8690 fip->field_name, (int)fip->field_readcount,
8691 dp->tdir_count);
8692 return (0);
8693 }
8694 else
8695 {
8696 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
8697 if (err == TIFFReadDirEntryErrOk)
8698 {
8699 if (!EvaluateIFDdatasizeReading(tif, dp))
8700 {
8701 if (data != 0)
8702 _TIFFfreeExt(tif, data);
8703 return 0;
8704 }
8705 int m;
8706 m = TIFFSetField(tif, dp->tdir_tag, data);
8707 if (data != 0)
8708 _TIFFfreeExt(tif, data);
8709 if (!m)
8710 return (0);
8711 }
8712 }
8713 }
8714 break;
8716 {
8717 uint64_t *data;
8718 assert(fip->field_readcount >= 1);
8719 assert(fip->field_passcount == 0);
8720 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8721 {
8722 TIFFWarningExtR(tif, module,
8723 "incorrect count for field \"%s\", expected "
8724 "%d, got %" PRIu64,
8725 fip->field_name, (int)fip->field_readcount,
8726 dp->tdir_count);
8727 return (0);
8728 }
8729 else
8730 {
8731 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
8732 if (err == TIFFReadDirEntryErrOk)
8733 {
8734 if (!EvaluateIFDdatasizeReading(tif, dp))
8735 {
8736 if (data != 0)
8737 _TIFFfreeExt(tif, data);
8738 return 0;
8739 }
8740 int m;
8741 m = TIFFSetField(tif, dp->tdir_tag, data);
8742 if (data != 0)
8743 _TIFFfreeExt(tif, data);
8744 if (!m)
8745 return (0);
8746 }
8747 }
8748 }
8749 break;
8751 {
8752 int64_t *data;
8753 assert(fip->field_readcount >= 1);
8754 assert(fip->field_passcount == 0);
8755 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8756 {
8757 TIFFWarningExtR(tif, module,
8758 "incorrect count for field \"%s\", expected "
8759 "%d, got %" PRIu64,
8760 fip->field_name, (int)fip->field_readcount,
8761 dp->tdir_count);
8762 return (0);
8763 }
8764 else
8765 {
8766 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
8767 if (err == TIFFReadDirEntryErrOk)
8768 {
8769 if (!EvaluateIFDdatasizeReading(tif, dp))
8770 {
8771 if (data != 0)
8772 _TIFFfreeExt(tif, data);
8773 return 0;
8774 }
8775 int m;
8776 m = TIFFSetField(tif, dp->tdir_tag, data);
8777 if (data != 0)
8778 _TIFFfreeExt(tif, data);
8779 if (!m)
8780 return (0);
8781 }
8782 }
8783 }
8784 break;
8786 {
8787 float *data;
8788 assert(fip->field_readcount >= 1);
8789 assert(fip->field_passcount == 0);
8790 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8791 {
8792 TIFFWarningExtR(tif, module,
8793 "incorrect count for field \"%s\", expected "
8794 "%d, got %" PRIu64,
8795 fip->field_name, (int)fip->field_readcount,
8796 dp->tdir_count);
8797 return (0);
8798 }
8799 else
8800 {
8801 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
8802 if (err == TIFFReadDirEntryErrOk)
8803 {
8804 if (!EvaluateIFDdatasizeReading(tif, dp))
8805 {
8806 if (data != 0)
8807 _TIFFfreeExt(tif, data);
8808 return 0;
8809 }
8810 int m;
8811 m = TIFFSetField(tif, dp->tdir_tag, data);
8812 if (data != 0)
8813 _TIFFfreeExt(tif, data);
8814 if (!m)
8815 return (0);
8816 }
8817 }
8818 }
8819 break;
8820 /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
8821 * into Double-Arrays. */
8823 {
8824 double *data;
8825 assert(fip->field_readcount >= 1);
8826 assert(fip->field_passcount == 0);
8827 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8828 {
8829 TIFFWarningExtR(tif, module,
8830 "incorrect count for field \"%s\", expected "
8831 "%d, got %" PRIu64,
8832 fip->field_name, (int)fip->field_readcount,
8833 dp->tdir_count);
8834 return (0);
8835 }
8836 else
8837 {
8838 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
8839 if (err == TIFFReadDirEntryErrOk)
8840 {
8841 if (!EvaluateIFDdatasizeReading(tif, dp))
8842 {
8843 if (data != 0)
8844 _TIFFfreeExt(tif, data);
8845 return 0;
8846 }
8847 int m;
8848 m = TIFFSetField(tif, dp->tdir_tag, data);
8849 if (data != 0)
8850 _TIFFfreeExt(tif, data);
8851 if (!m)
8852 return (0);
8853 }
8854 }
8855 }
8856 break;
8858 {
8859 uint64_t *data;
8860 assert(fip->field_readcount >= 1);
8861 assert(fip->field_passcount == 0);
8862 if (dp->tdir_count != (uint64_t)fip->field_readcount)
8863 {
8864 TIFFWarningExtR(tif, module,
8865 "incorrect count for field \"%s\", expected "
8866 "%d, got %" PRIu64,
8867 fip->field_name, (int)fip->field_readcount,
8868 dp->tdir_count);
8869 return (0);
8870 }
8871 else
8872 {
8873 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
8874 if (err == TIFFReadDirEntryErrOk)
8875 {
8876 if (!EvaluateIFDdatasizeReading(tif, dp))
8877 {
8878 if (data != 0)
8879 _TIFFfreeExt(tif, data);
8880 return 0;
8881 }
8882 int m;
8883 m = TIFFSetField(tif, dp->tdir_tag, data);
8884 if (data != 0)
8885 _TIFFfreeExt(tif, data);
8886 if (!m)
8887 return (0);
8888 }
8889 }
8890 }
8891 break;
8893 {
8894 uint8_t *data;
8895 assert(fip->field_readcount == TIFF_VARIABLE);
8896 assert(fip->field_passcount == 1);
8897 if (dp->tdir_count > 0xFFFF)
8899 else
8900 {
8901 err = TIFFReadDirEntryByteArray(tif, dp, &data);
8902 if (err == TIFFReadDirEntryErrOk)
8903 {
8904 if (!EvaluateIFDdatasizeReading(tif, dp))
8905 {
8906 if (data != 0)
8907 _TIFFfreeExt(tif, data);
8908 return 0;
8909 }
8910 int m;
8911 if (data != 0 && dp->tdir_count > 0 &&
8912 data[dp->tdir_count - 1] != '\0')
8913 {
8914 TIFFWarningExtR(tif, module,
8915 "ASCII value for ASCII array tag "
8916 "\"%s\" does not end in null "
8917 "byte. Forcing it to be null",
8918 fip->field_name);
8919 /* Enlarge buffer and add terminating null. */
8920 uint8_t *o = (uint8_t *)_TIFFmallocExt(
8921 tif, (uint32_t)dp->tdir_count + 1);
8922 if (o == NULL)
8923 {
8924 if (data != NULL)
8925 _TIFFfreeExt(tif, data);
8926 return (0);
8927 }
8928 if (dp->tdir_count > 0)
8929 {
8930 _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
8931 }
8932 o[(uint32_t)dp->tdir_count] = 0;
8933 dp->tdir_count++; /* Increment for added null. */
8934 if (data != 0)
8935 _TIFFfreeExt(tif, data);
8936 data = o;
8937 }
8938 m = TIFFSetField(tif, dp->tdir_tag,
8939 (uint16_t)(dp->tdir_count), data);
8940 if (data != 0)
8941 _TIFFfreeExt(tif, data);
8942 if (!m)
8943 return (0);
8944 }
8945 }
8946 }
8947 break;
8949 {
8950 uint8_t *data;
8951 assert(fip->field_readcount == TIFF_VARIABLE);
8952 assert(fip->field_passcount == 1);
8953 if (dp->tdir_count > 0xFFFF)
8955 else
8956 {
8957 err = TIFFReadDirEntryByteArray(tif, dp, &data);
8958 if (err == TIFFReadDirEntryErrOk)
8959 {
8960 if (!EvaluateIFDdatasizeReading(tif, dp))
8961 {
8962 if (data != 0)
8963 _TIFFfreeExt(tif, data);
8964 return 0;
8965 }
8966 int m;
8967 m = TIFFSetField(tif, dp->tdir_tag,
8968 (uint16_t)(dp->tdir_count), data);
8969 if (data != 0)
8970 _TIFFfreeExt(tif, data);
8971 if (!m)
8972 return (0);
8973 }
8974 }
8975 }
8976 break;
8978 {
8979 int8_t *data;
8980 assert(fip->field_readcount == TIFF_VARIABLE);
8981 assert(fip->field_passcount == 1);
8982 if (dp->tdir_count > 0xFFFF)
8984 else
8985 {
8986 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
8987 if (err == TIFFReadDirEntryErrOk)
8988 {
8989 if (!EvaluateIFDdatasizeReading(tif, dp))
8990 {
8991 if (data != 0)
8992 _TIFFfreeExt(tif, data);
8993 return 0;
8994 }
8995 int m;
8996 m = TIFFSetField(tif, dp->tdir_tag,
8997 (uint16_t)(dp->tdir_count), data);
8998 if (data != 0)
8999 _TIFFfreeExt(tif, data);
9000 if (!m)
9001 return (0);
9002 }
9003 }
9004 }
9005 break;
9007 {
9008 uint16_t *data;
9009 assert(fip->field_readcount == TIFF_VARIABLE);
9010 assert(fip->field_passcount == 1);
9011 if (dp->tdir_count > 0xFFFF)
9013 else
9014 {
9015 err = TIFFReadDirEntryShortArray(tif, dp, &data);
9016 if (err == TIFFReadDirEntryErrOk)
9017 {
9018 if (!EvaluateIFDdatasizeReading(tif, dp))
9019 {
9020 if (data != 0)
9021 _TIFFfreeExt(tif, data);
9022 return 0;
9023 }
9024 int m;
9025 m = TIFFSetField(tif, dp->tdir_tag,
9026 (uint16_t)(dp->tdir_count), data);
9027 if (data != 0)
9028 _TIFFfreeExt(tif, data);
9029 if (!m)
9030 return (0);
9031 }
9032 }
9033 }
9034 break;
9036 {
9037 int16_t *data;
9038 assert(fip->field_readcount == TIFF_VARIABLE);
9039 assert(fip->field_passcount == 1);
9040 if (dp->tdir_count > 0xFFFF)
9042 else
9043 {
9044 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
9045 if (err == TIFFReadDirEntryErrOk)
9046 {
9047 if (!EvaluateIFDdatasizeReading(tif, dp))
9048 {
9049 if (data != 0)
9050 _TIFFfreeExt(tif, data);
9051 return 0;
9052 }
9053 int m;
9054 m = TIFFSetField(tif, dp->tdir_tag,
9055 (uint16_t)(dp->tdir_count), data);
9056 if (data != 0)
9057 _TIFFfreeExt(tif, data);
9058 if (!m)
9059 return (0);
9060 }
9061 }
9062 }
9063 break;
9065 {
9066 uint32_t *data;
9067 assert(fip->field_readcount == TIFF_VARIABLE);
9068 assert(fip->field_passcount == 1);
9069 if (dp->tdir_count > 0xFFFF)
9071 else
9072 {
9073 err = TIFFReadDirEntryLongArray(tif, dp, &data);
9074 if (err == TIFFReadDirEntryErrOk)
9075 {
9076 if (!EvaluateIFDdatasizeReading(tif, dp))
9077 {
9078 if (data != 0)
9079 _TIFFfreeExt(tif, data);
9080 return 0;
9081 }
9082 int m;
9083 m = TIFFSetField(tif, dp->tdir_tag,
9084 (uint16_t)(dp->tdir_count), data);
9085 if (data != 0)
9086 _TIFFfreeExt(tif, data);
9087 if (!m)
9088 return (0);
9089 }
9090 }
9091 }
9092 break;
9094 {
9095 int32_t *data;
9096 assert(fip->field_readcount == TIFF_VARIABLE);
9097 assert(fip->field_passcount == 1);
9098 if (dp->tdir_count > 0xFFFF)
9100 else
9101 {
9102 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
9103 if (err == TIFFReadDirEntryErrOk)
9104 {
9105 if (!EvaluateIFDdatasizeReading(tif, dp))
9106 {
9107 if (data != 0)
9108 _TIFFfreeExt(tif, data);
9109 return 0;
9110 }
9111 int m;
9112 m = TIFFSetField(tif, dp->tdir_tag,
9113 (uint16_t)(dp->tdir_count), data);
9114 if (data != 0)
9115 _TIFFfreeExt(tif, data);
9116 if (!m)
9117 return (0);
9118 }
9119 }
9120 }
9121 break;
9123 {
9124 uint64_t *data;
9125 assert(fip->field_readcount == TIFF_VARIABLE);
9126 assert(fip->field_passcount == 1);
9127 if (dp->tdir_count > 0xFFFF)
9129 else
9130 {
9131 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
9132 if (err == TIFFReadDirEntryErrOk)
9133 {
9134 if (!EvaluateIFDdatasizeReading(tif, dp))
9135 {
9136 if (data != 0)
9137 _TIFFfreeExt(tif, data);
9138 return 0;
9139 }
9140 int m;
9141 m = TIFFSetField(tif, dp->tdir_tag,
9142 (uint16_t)(dp->tdir_count), data);
9143 if (data != 0)
9144 _TIFFfreeExt(tif, data);
9145 if (!m)
9146 return (0);
9147 }
9148 }
9149 }
9150 break;
9152 {
9153 int64_t *data;
9154 assert(fip->field_readcount == TIFF_VARIABLE);
9155 assert(fip->field_passcount == 1);
9156 if (dp->tdir_count > 0xFFFF)
9158 else
9159 {
9160 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
9161 if (err == TIFFReadDirEntryErrOk)
9162 {
9163 if (!EvaluateIFDdatasizeReading(tif, dp))
9164 {
9165 if (data != 0)
9166 _TIFFfreeExt(tif, data);
9167 return 0;
9168 }
9169 int m;
9170 m = TIFFSetField(tif, dp->tdir_tag,
9171 (uint16_t)(dp->tdir_count), data);
9172 if (data != 0)
9173 _TIFFfreeExt(tif, data);
9174 if (!m)
9175 return (0);
9176 }
9177 }
9178 }
9179 break;
9181 {
9182 float *data;
9183 assert(fip->field_readcount == TIFF_VARIABLE);
9184 assert(fip->field_passcount == 1);
9185 if (dp->tdir_count > 0xFFFF)
9187 else
9188 {
9189 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
9190 if (err == TIFFReadDirEntryErrOk)
9191 {
9192 if (!EvaluateIFDdatasizeReading(tif, dp))
9193 {
9194 if (data != 0)
9195 _TIFFfreeExt(tif, data);
9196 return 0;
9197 }
9198 int m;
9199 m = TIFFSetField(tif, dp->tdir_tag,
9200 (uint16_t)(dp->tdir_count), data);
9201 if (data != 0)
9202 _TIFFfreeExt(tif, data);
9203 if (!m)
9204 return (0);
9205 }
9206 }
9207 }
9208 break;
9210 {
9211 double *data;
9212 assert(fip->field_readcount == TIFF_VARIABLE);
9213 assert(fip->field_passcount == 1);
9214 if (dp->tdir_count > 0xFFFF)
9216 else
9217 {
9218 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
9219 if (err == TIFFReadDirEntryErrOk)
9220 {
9221 if (!EvaluateIFDdatasizeReading(tif, dp))
9222 {
9223 if (data != 0)
9224 _TIFFfreeExt(tif, data);
9225 return 0;
9226 }
9227 int m;
9228 m = TIFFSetField(tif, dp->tdir_tag,
9229 (uint16_t)(dp->tdir_count), data);
9230 if (data != 0)
9231 _TIFFfreeExt(tif, data);
9232 if (!m)
9233 return (0);
9234 }
9235 }
9236 }
9237 break;
9239 {
9240 uint64_t *data;
9241 assert(fip->field_readcount == TIFF_VARIABLE);
9242 assert(fip->field_passcount == 1);
9243 if (dp->tdir_count > 0xFFFF)
9245 else
9246 {
9247 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
9248 if (err == TIFFReadDirEntryErrOk)
9249 {
9250 if (!EvaluateIFDdatasizeReading(tif, dp))
9251 {
9252 if (data != 0)
9253 _TIFFfreeExt(tif, data);
9254 return 0;
9255 }
9256 int m;
9257 m = TIFFSetField(tif, dp->tdir_tag,
9258 (uint16_t)(dp->tdir_count), data);
9259 if (data != 0)
9260 _TIFFfreeExt(tif, data);
9261 if (!m)
9262 return (0);
9263 }
9264 }
9265 }
9266 break;
9268 {
9269 uint8_t *data;
9270 assert(fip->field_readcount == TIFF_VARIABLE2);
9271 assert(fip->field_passcount == 1);
9272 err = TIFFReadDirEntryByteArray(tif, dp, &data);
9273 if (err == TIFFReadDirEntryErrOk)
9274 {
9275 if (!EvaluateIFDdatasizeReading(tif, dp))
9276 {
9277 if (data != 0)
9278 _TIFFfreeExt(tif, data);
9279 return 0;
9280 }
9281 int m;
9282 if (data != 0 && dp->tdir_count > 0 &&
9283 data[dp->tdir_count - 1] != '\0')
9284 {
9286 tif, module,
9287 "ASCII value for ASCII array tag \"%s\" does not end "
9288 "in null byte. Forcing it to be null",
9289 fip->field_name);
9290 /* Enlarge buffer and add terminating null. */
9291 uint8_t *o = (uint8_t *)_TIFFmallocExt(
9292 tif, (uint32_t)dp->tdir_count + 1);
9293 if (o == NULL)
9294 {
9295 if (data != NULL)
9296 _TIFFfreeExt(tif, data);
9297 return (0);
9298 }
9299 if (dp->tdir_count > 0)
9300 {
9301 _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
9302 }
9303 o[(uint32_t)dp->tdir_count] = 0;
9304 dp->tdir_count++; /* Increment for added null. */
9305 if (data != 0)
9306 _TIFFfreeExt(tif, data);
9307 data = o;
9308 }
9309 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9310 data);
9311 if (data != 0)
9312 _TIFFfreeExt(tif, data);
9313 if (!m)
9314 return (0);
9315 }
9316 }
9317 break;
9319 {
9320 uint8_t *data;
9321 uint32_t count = 0;
9322 assert(fip->field_readcount == TIFF_VARIABLE2);
9323 assert(fip->field_passcount == 1);
9324 if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
9325 dp->tdir_type == TIFF_LONG)
9326 {
9327 /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
9328 * data type LONG instead of UNDEFINED. Work around this
9329 * frequently found issue */
9330 void *origdata;
9331 err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
9332 if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
9333 {
9334 data = NULL;
9335 }
9336 else
9337 {
9338 if (tif->tif_flags & TIFF_SWAB)
9339 TIFFSwabArrayOfLong((uint32_t *)origdata, count);
9340 data = (uint8_t *)origdata;
9341 count = (uint32_t)(count * 4);
9342 }
9343 }
9344 else
9345 {
9346 err = TIFFReadDirEntryByteArray(tif, dp, &data);
9347 count = (uint32_t)(dp->tdir_count);
9348 }
9349 if (err == TIFFReadDirEntryErrOk)
9350 {
9351 if (!EvaluateIFDdatasizeReading(tif, dp))
9352 {
9353 if (data != 0)
9354 _TIFFfreeExt(tif, data);
9355 return 0;
9356 }
9357 int m;
9358 m = TIFFSetField(tif, dp->tdir_tag, count, data);
9359 if (data != 0)
9360 _TIFFfreeExt(tif, data);
9361 if (!m)
9362 return (0);
9363 }
9364 }
9365 break;
9367 {
9368 int8_t *data = NULL;
9369 assert(fip->field_readcount == TIFF_VARIABLE2);
9370 assert(fip->field_passcount == 1);
9371 err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
9372 if (err == TIFFReadDirEntryErrOk)
9373 {
9374 if (!EvaluateIFDdatasizeReading(tif, dp))
9375 {
9376 if (data != 0)
9377 _TIFFfreeExt(tif, data);
9378 return 0;
9379 }
9380 int m;
9381 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9382 data);
9383 if (data != 0)
9384 _TIFFfreeExt(tif, data);
9385 if (!m)
9386 return (0);
9387 }
9388 }
9389 break;
9391 {
9392 uint16_t *data;
9393 assert(fip->field_readcount == TIFF_VARIABLE2);
9394 assert(fip->field_passcount == 1);
9395 err = TIFFReadDirEntryShortArray(tif, dp, &data);
9396 if (err == TIFFReadDirEntryErrOk)
9397 {
9398 if (!EvaluateIFDdatasizeReading(tif, dp))
9399 {
9400 if (data != 0)
9401 _TIFFfreeExt(tif, data);
9402 return 0;
9403 }
9404 int m;
9405 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9406 data);
9407 if (data != 0)
9408 _TIFFfreeExt(tif, data);
9409 if (!m)
9410 return (0);
9411 }
9412 }
9413 break;
9415 {
9416 int16_t *data = NULL;
9417 assert(fip->field_readcount == TIFF_VARIABLE2);
9418 assert(fip->field_passcount == 1);
9419 err = TIFFReadDirEntrySshortArray(tif, dp, &data);
9420 if (err == TIFFReadDirEntryErrOk)
9421 {
9422 if (!EvaluateIFDdatasizeReading(tif, dp))
9423 {
9424 if (data != 0)
9425 _TIFFfreeExt(tif, data);
9426 return 0;
9427 }
9428 int m;
9429 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9430 data);
9431 if (data != 0)
9432 _TIFFfreeExt(tif, data);
9433 if (!m)
9434 return (0);
9435 }
9436 }
9437 break;
9439 {
9440 uint32_t *data;
9441 assert(fip->field_readcount == TIFF_VARIABLE2);
9442 assert(fip->field_passcount == 1);
9443 err = TIFFReadDirEntryLongArray(tif, dp, &data);
9444 if (err == TIFFReadDirEntryErrOk)
9445 {
9446 if (!EvaluateIFDdatasizeReading(tif, dp))
9447 {
9448 if (data != 0)
9449 _TIFFfreeExt(tif, data);
9450 return 0;
9451 }
9452 int m;
9453 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9454 data);
9455 if (data != 0)
9456 _TIFFfreeExt(tif, data);
9457 if (!m)
9458 return (0);
9459 }
9460 }
9461 break;
9463 {
9464 int32_t *data = NULL;
9465 assert(fip->field_readcount == TIFF_VARIABLE2);
9466 assert(fip->field_passcount == 1);
9467 err = TIFFReadDirEntrySlongArray(tif, dp, &data);
9468 if (err == TIFFReadDirEntryErrOk)
9469 {
9470 if (!EvaluateIFDdatasizeReading(tif, dp))
9471 {
9472 if (data != 0)
9473 _TIFFfreeExt(tif, data);
9474 return 0;
9475 }
9476 int m;
9477 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9478 data);
9479 if (data != 0)
9480 _TIFFfreeExt(tif, data);
9481 if (!m)
9482 return (0);
9483 }
9484 }
9485 break;
9487 {
9488 uint64_t *data;
9489 assert(fip->field_readcount == TIFF_VARIABLE2);
9490 assert(fip->field_passcount == 1);
9491 err = TIFFReadDirEntryLong8Array(tif, dp, &data);
9492 if (err == TIFFReadDirEntryErrOk)
9493 {
9494 if (!EvaluateIFDdatasizeReading(tif, dp))
9495 {
9496 if (data != 0)
9497 _TIFFfreeExt(tif, data);
9498 return 0;
9499 }
9500 int m;
9501 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9502 data);
9503 if (data != 0)
9504 _TIFFfreeExt(tif, data);
9505 if (!m)
9506 return (0);
9507 }
9508 }
9509 break;
9511 {
9512 int64_t *data = NULL;
9513 assert(fip->field_readcount == TIFF_VARIABLE2);
9514 assert(fip->field_passcount == 1);
9515 err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
9516 if (err == TIFFReadDirEntryErrOk)
9517 {
9518 if (!EvaluateIFDdatasizeReading(tif, dp))
9519 {
9520 if (data != 0)
9521 _TIFFfreeExt(tif, data);
9522 return 0;
9523 }
9524 int m;
9525 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9526 data);
9527 if (data != 0)
9528 _TIFFfreeExt(tif, data);
9529 if (!m)
9530 return (0);
9531 }
9532 }
9533 break;
9535 {
9536 float *data;
9537 assert(fip->field_readcount == TIFF_VARIABLE2);
9538 assert(fip->field_passcount == 1);
9539 err = TIFFReadDirEntryFloatArray(tif, dp, &data);
9540 if (err == TIFFReadDirEntryErrOk)
9541 {
9542 if (!EvaluateIFDdatasizeReading(tif, dp))
9543 {
9544 if (data != 0)
9545 _TIFFfreeExt(tif, data);
9546 return 0;
9547 }
9548 int m;
9549 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9550 data);
9551 if (data != 0)
9552 _TIFFfreeExt(tif, data);
9553 if (!m)
9554 return (0);
9555 }
9556 }
9557 break;
9559 {
9560 double *data;
9561 assert(fip->field_readcount == TIFF_VARIABLE2);
9562 assert(fip->field_passcount == 1);
9563 err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
9564 if (err == TIFFReadDirEntryErrOk)
9565 {
9566 if (!EvaluateIFDdatasizeReading(tif, dp))
9567 {
9568 if (data != 0)
9569 _TIFFfreeExt(tif, data);
9570 return 0;
9571 }
9572 int m;
9573 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9574 data);
9575 if (data != 0)
9576 _TIFFfreeExt(tif, data);
9577 if (!m)
9578 return (0);
9579 }
9580 }
9581 break;
9583 {
9584 uint64_t *data;
9585 assert(fip->field_readcount == TIFF_VARIABLE2);
9586 assert(fip->field_passcount == 1);
9587 err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
9588 if (err == TIFFReadDirEntryErrOk)
9589 {
9590 if (!EvaluateIFDdatasizeReading(tif, dp))
9591 {
9592 if (data != 0)
9593 _TIFFfreeExt(tif, data);
9594 return 0;
9595 }
9596 int m;
9597 m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
9598 data);
9599 if (data != 0)
9600 _TIFFfreeExt(tif, data);
9601 if (!m)
9602 return (0);
9603 }
9604 }
9605 break;
9606 case TIFF_SETGET_INT:
9608 case TIFF_SETGET_OTHER:
9609 assert(0); /* these should not arrive here */
9610 break;
9611 default:
9612 assert(0); /* we should never get here */
9613 break;
9614 }
9615 if (err != TIFFReadDirEntryErrOk)
9616 {
9617 TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
9618 return (0);
9619 }
9620 return (1);
9621}
9622
9623/*
9624 * Fetch a set of offsets or lengths.
9625 * While this routine says "strips", in fact it's also used for tiles.
9626 */
9651static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
9652 uint64_t **lpp)
9653{
9654 static const char module[] = "TIFFFetchStripThing";
9655 enum TIFFReadDirEntryErr err;
9656 uint64_t *data;
9657 err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
9658 if (err != TIFFReadDirEntryErrOk)
9659 {
9660 const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
9661 TIFFReadDirEntryOutputErr(tif, err, module,
9662 fip ? fip->field_name : "unknown tagname", 0);
9663 return (0);
9664 }
9665 if (dir->tdir_count < (uint64_t)nstrips)
9666 {
9667 uint64_t *resizeddata;
9668 const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
9669 const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
9670 uint32_t max_nstrips = 1000000;
9671 if (pszMax)
9672 max_nstrips = _TIFFParseUInt32(pszMax, 1000000, UINT32_MAX);
9674 fip ? fip->field_name : "unknown tagname",
9675 (nstrips <= max_nstrips));
9676
9677 if (nstrips > max_nstrips)
9678 {
9679 _TIFFfreeExt(tif, data);
9680 return (0);
9681 }
9682
9683 const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
9684 if (allocsize > 100 * 1024 * 1024)
9685 {
9686 /* Before allocating a huge amount of memory for corrupted files,
9687 * check if size of requested memory is not greater than file size.
9688 */
9689 const uint64_t filesize = TIFFGetFileSize(tif);
9690 if (allocsize > filesize)
9691 {
9693 tif, module,
9694 "Requested memory size for StripArray of %" PRIu64
9695 " is greater than filesize %" PRIu64
9696 ". Memory not allocated",
9697 allocsize, filesize);
9698 _TIFFfreeExt(tif, data);
9699 return (0);
9700 }
9701 }
9702 resizeddata = (uint64_t *)_TIFFCheckMalloc(
9703 tif, nstrips, sizeof(uint64_t), "for strip array");
9704 if (resizeddata == 0)
9705 {
9706 _TIFFfreeExt(tif, data);
9707 return (0);
9708 }
9709 if (dir->tdir_count)
9710 _TIFFmemcpy(resizeddata, data,
9711 (tmsize_t)((size_t)dir->tdir_count * sizeof(uint64_t)));
9712 _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
9713 (tmsize_t)((size_t)(nstrips - (uint32_t)dir->tdir_count) *
9714 sizeof(uint64_t)));
9715 _TIFFfreeExt(tif, data);
9716 data = resizeddata;
9717 }
9718 *lpp = data;
9719 return (1);
9720}
9721
9722/*
9723 * Fetch and set the SubjectDistance EXIF tag.
9724 */
9743{
9744 static const char module[] = "TIFFFetchSubjectDistance";
9745 enum TIFFReadDirEntryErr err;
9747 m.l = 0;
9748 assert(sizeof(double) == 8);
9749 assert(sizeof(uint64_t) == 8);
9750 assert(sizeof(uint32_t) == 4);
9751 if (dir->tdir_count != 1)
9753 else if (dir->tdir_type != TIFF_RATIONAL)
9755 else
9756 {
9757 if (!(tif->tif_flags & TIFF_BIGTIFF))
9758 {
9759 uint32_t offset;
9760 offset = *(uint32_t *)(&dir->tdir_offset);
9761 if (tif->tif_flags & TIFF_SWAB)
9762 TIFFSwabLong(&offset);
9763 err = TIFFReadDirEntryData(tif, offset, 8, m.i);
9764 }
9765 else
9766 {
9767 m.l = dir->tdir_offset.toff_long8;
9769 }
9770 }
9771 if (err == TIFFReadDirEntryErrOk)
9772 {
9773 double n;
9774 if (tif->tif_flags & TIFF_SWAB)
9775 TIFFSwabArrayOfLong(m.i, 2);
9776 if (m.i[0] == 0)
9777 n = 0.0;
9778 else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
9779 /*
9780 * XXX: Numerator 0xFFFFFFFF means that we have infinite
9781 * distance. Indicate that with a negative floating point
9782 * SubjectDistance value.
9783 */
9784 n = -1.0;
9785 else
9786 n = (double)m.i[0] / (double)m.i[1];
9787 return (TIFFSetField(tif, dir->tdir_tag, n));
9788 }
9789 else
9790 {
9791 TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
9792 return (0);
9793 }
9794}
9795
9816static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
9817 uint64_t stripbytes,
9818 uint32_t rowsperstrip)
9819{
9820 TIFFDirectory *td = &tif->tif_dir;
9821 uint64_t bytecount;
9822 uint64_t offset;
9823 uint64_t last_offset;
9824 uint64_t last_bytecount;
9825 uint32_t i;
9826 uint64_t *newcounts;
9827 uint64_t *newoffsets;
9828
9829 offset = TIFFGetStrileOffset(tif, 0);
9830 last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
9831 last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
9832 if (last_offset > UINT64_MAX - last_bytecount ||
9833 last_offset + last_bytecount < offset)
9834 {
9835 return;
9836 }
9837 bytecount = last_offset + last_bytecount - offset;
9838
9839 /* Before allocating a huge amount of memory for corrupted files, check if
9840 * size of StripByteCount and StripOffset tags is not greater than
9841 * file size.
9842 */
9843 const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
9844 if (allocsize > 100 * 1024 * 1024)
9845 {
9846 const uint64_t filesize = TIFFGetFileSize(tif);
9847 if (allocsize > filesize)
9848 {
9849 TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
9850 "Requested memory size for StripByteCount and "
9851 "StripOffsets %" PRIu64
9852 " is greater than filesize %" PRIu64
9853 ". Memory not allocated",
9854 allocsize, filesize);
9855 return;
9856 }
9857 }
9858
9859 newcounts =
9860 (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
9861 "for chopped \"StripByteCounts\" array");
9862 newoffsets = (uint64_t *)_TIFFCheckMalloc(
9863 tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
9864 if (newcounts == NULL || newoffsets == NULL)
9865 {
9866 /*
9867 * Unable to allocate new strip information, give up and use
9868 * the original one strip information.
9869 */
9870 if (newcounts != NULL)
9871 _TIFFfreeExt(tif, newcounts);
9872 if (newoffsets != NULL)
9873 _TIFFfreeExt(tif, newoffsets);
9874 return;
9875 }
9876
9877 /*
9878 * Fill the strip information arrays with new bytecounts and offsets
9879 * that reflect the broken-up format.
9880 */
9881 for (i = 0; i < nstrips; i++)
9882 {
9883 if (stripbytes > bytecount)
9884 stripbytes = bytecount;
9885 newcounts[i] = stripbytes;
9886 newoffsets[i] = stripbytes ? offset : 0;
9887 offset += stripbytes;
9888 bytecount -= stripbytes;
9889 }
9890
9891 /*
9892 * Replace old single strip info with multi-strip info.
9893 */
9894 td->td_stripsperimage = td->td_nstrips = nstrips;
9895 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
9896
9899 td->td_stripbytecount_p = newcounts;
9900 td->td_stripoffset_p = newoffsets;
9901#ifdef STRIPBYTECOUNTSORTED_UNUSED
9902 td->td_stripbytecountsorted = 1;
9903#endif
9905}
9906
9907/*
9908 * Replace a single strip (tile) of uncompressed data by multiple strips
9909 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
9910 * dealing with large images or for dealing with machines with a limited
9911 * amount memory.
9912 */
9930{
9931 TIFFDirectory *td = &tif->tif_dir;
9932 uint64_t bytecount;
9933 uint64_t offset;
9934 uint32_t rowblock;
9935 uint64_t rowblockbytes;
9936 uint64_t stripbytes;
9937 uint32_t nstrips;
9938 uint32_t rowsperstrip;
9939
9940 bytecount = TIFFGetStrileByteCount(tif, 0);
9941 /* On a newly created file, just re-opened to be filled, we */
9942 /* don't want strip chop to trigger as it is going to cause issues */
9943 /* later ( StripOffsets and StripByteCounts improperly filled) . */
9944 if (bytecount == 0 && tif->tif_mode != O_RDONLY)
9945 return;
9946 offset = TIFFGetStrileOffset(tif, 0);
9947 assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
9948 if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
9949 rowblock = td->td_ycbcrsubsampling[1];
9950 else
9951 rowblock = 1;
9952 rowblockbytes = TIFFVTileSize64(tif, rowblock);
9953 /*
9954 * Make the rows hold at least one scanline, but fill specified amount
9955 * of data if possible.
9956 */
9957 if (rowblockbytes > STRIP_SIZE_DEFAULT)
9958 {
9959 stripbytes = rowblockbytes;
9960 rowsperstrip = rowblock;
9961 }
9962 else if (rowblockbytes > 0)
9963 {
9964 uint32_t rowblocksperstrip;
9965 rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
9966 rowsperstrip = rowblocksperstrip * rowblock;
9967 stripbytes = rowblocksperstrip * rowblockbytes;
9968 }
9969 else
9970 return;
9971
9972 /*
9973 * never increase the number of rows per strip
9974 */
9975 if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
9976 return;
9977 nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
9978 if (nstrips == 0)
9979 return;
9980
9981 /* If we are going to allocate a lot of memory, make sure that the */
9982 /* file is as big as needed */
9983 if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
9984 (offset >= TIFFGetFileSize(tif) ||
9985 stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
9986 {
9987 return;
9988 }
9989
9990 allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
9991}
9992
9993/*
9994 * Replace a file with contiguous strips > 2 GB of uncompressed data by
9995 * multiple smaller strips. This is useful for
9996 * dealing with large images or for dealing with machines with a limited
9997 * amount memory.
9998 */
10015{
10016 TIFFDirectory *td = &tif->tif_dir;
10017 uint32_t rowblock;
10018 uint64_t rowblockbytes;
10019 uint32_t i;
10020 uint64_t stripsize;
10021 uint32_t rowblocksperstrip;
10022 uint32_t rowsperstrip;
10023 uint64_t stripbytes;
10024 uint32_t nstrips;
10025
10026 stripsize = TIFFStripSize64(tif);
10027
10029 assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
10030 assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
10032 assert(stripsize > 0x7FFFFFFFUL);
10033
10034 /* On a newly created file, just re-opened to be filled, we */
10035 /* don't want strip chop to trigger as it is going to cause issues */
10036 /* later ( StripOffsets and StripByteCounts improperly filled) . */
10037 if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
10038 return;
10039
10040 if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
10041 rowblock = td->td_ycbcrsubsampling[1];
10042 else
10043 rowblock = 1;
10044 rowblockbytes = TIFFVStripSize64(tif, rowblock);
10045 if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
10046 {
10047 /* In case of file with gigantic width */
10048 return;
10049 }
10050
10051 /* Check that the strips are contiguous and of the expected size */
10052 for (i = 0; i < td->td_nstrips; i++)
10053 {
10054 if (i == td->td_nstrips - 1)
10055 {
10056 if (TIFFGetStrileByteCount(tif, i) <
10057 TIFFVStripSize64(tif,
10058 td->td_imagelength - i * td->td_rowsperstrip))
10059 {
10060 return;
10061 }
10062 }
10063 else
10064 {
10065 if (TIFFGetStrileByteCount(tif, i) != stripsize)
10066 {
10067 return;
10068 }
10069 if (i > 0 && TIFFGetStrileOffset(tif, i) !=
10070 TIFFGetStrileOffset(tif, i - 1) +
10071 TIFFGetStrileByteCount(tif, i - 1))
10072 {
10073 return;
10074 }
10075 }
10076 }
10077
10078 /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
10079 rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
10080 if (rowblocksperstrip == 0)
10081 rowblocksperstrip = 1;
10082 rowsperstrip = rowblocksperstrip * rowblock;
10083 stripbytes = rowblocksperstrip * rowblockbytes;
10084 assert(stripbytes <= 0x7FFFFFFFUL);
10085
10086 if (rowsperstrip == 0)
10087 return;
10088 nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
10089 if (nstrips == 0)
10090 return;
10091
10092 /* If we are going to allocate a lot of memory, make sure that the */
10093 /* file is as big as needed */
10094 if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
10095 {
10096 uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
10097 uint64_t filesize = TIFFGetFileSize(tif);
10098 uint64_t last_bytecount =
10099 TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
10100 if (last_offset > filesize || last_bytecount > filesize - last_offset)
10101 {
10102 return;
10103 }
10104 }
10105
10106 allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
10107}
10108
10123static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
10124{
10125 return a + (uint64_t)b;
10126}
10127
10128/* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
10129 * strip/tile of number strile. Also fetch the neighbouring values using a
10130 * 4096 byte page size.
10131 */
10165 int strile, uint64_t *panVals)
10166{
10167 static const char module[] = "_TIFFPartialReadStripArray";
10168#define IO_CACHE_PAGE_SIZE 4096
10169
10170 size_t sizeofval;
10171 const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
10172 int sizeofvalint;
10173 uint64_t nBaseOffset;
10174 uint64_t nOffset;
10175 uint64_t nOffsetStartPage;
10176 uint64_t nOffsetEndPage;
10177 tmsize_t nToRead;
10178 tmsize_t nRead;
10179 uint64_t nLastStripOffset;
10180 int iStartBefore;
10181 int i;
10182 const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
10183 unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
10184
10185 assert(dirent->tdir_count > 4);
10186
10187 if (dirent->tdir_type == TIFF_SHORT)
10188 {
10189 sizeofval = sizeof(uint16_t);
10190 }
10191 else if (dirent->tdir_type == TIFF_LONG)
10192 {
10193 sizeofval = sizeof(uint32_t);
10194 }
10195 else if (dirent->tdir_type == TIFF_LONG8)
10196 {
10197 sizeofval = sizeof(uint64_t);
10198 }
10199 else if (dirent->tdir_type == TIFF_SLONG8)
10200 {
10201 /* Non conformant but used by some images as in */
10202 /* https://github.com/OSGeo/gdal/issues/2165 */
10203 sizeofval = sizeof(int64_t);
10204 }
10205 else
10206 {
10207 TIFFErrorExtR(tif, module,
10208 "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
10209 panVals[strile] = 0;
10210 return 0;
10211 }
10212 sizeofvalint = (int)(sizeofval);
10213
10214 if (tif->tif_flags & TIFF_BIGTIFF)
10215 {
10216 uint64_t offset = dirent->tdir_offset.toff_long8;
10217 if (bSwab)
10218 TIFFSwabLong8(&offset);
10219 nBaseOffset = offset;
10220 }
10221 else
10222 {
10223 uint32_t offset = dirent->tdir_offset.toff_long;
10224 if (bSwab)
10225 TIFFSwabLong(&offset);
10226 nBaseOffset = offset;
10227 }
10228 /* To avoid later unsigned integer overflows */
10229 if (nBaseOffset > (uint64_t)INT64_MAX)
10230 {
10231 TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
10232 strile);
10233 panVals[strile] = 0;
10234 return 0;
10235 }
10236 nOffset = nBaseOffset + (uint64_t)sizeofval * (uint64_t)strile;
10237 nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
10238 nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
10239
10240 if (nOffset + sizeofval > nOffsetEndPage)
10241 nOffsetEndPage += IO_CACHE_PAGE_SIZE;
10242#undef IO_CACHE_PAGE_SIZE
10243
10244 nLastStripOffset = nBaseOffset + (uint64_t)arraySize * sizeofval;
10245 if (nLastStripOffset < nOffsetEndPage)
10246 nOffsetEndPage = nLastStripOffset;
10247 if (nOffsetStartPage >= nOffsetEndPage)
10248 {
10249 TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
10250 strile);
10251 panVals[strile] = 0;
10252 return 0;
10253 }
10254 if (!SeekOK(tif, nOffsetStartPage))
10255 {
10256 panVals[strile] = 0;
10257 return 0;
10258 }
10259
10260 nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
10261 nRead = TIFFReadFile(tif, buffer, nToRead);
10262 if (nRead < nToRead)
10263 {
10264 TIFFErrorExtR(tif, module,
10265 "Cannot read offset/size for strile around ~%d", strile);
10266 return 0;
10267 }
10268 iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
10269 if (strile + iStartBefore < 0)
10270 iStartBefore = -strile;
10271 for (i = iStartBefore;
10272 (uint32_t)(strile + i) < arraySize &&
10273 _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
10274 nOffsetEndPage;
10275 ++i)
10276 {
10277 if (dirent->tdir_type == TIFF_SHORT)
10278 {
10279 uint16_t val;
10280 memcpy(&val,
10281 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
10282 sizeof(val));
10283 if (bSwab)
10284 TIFFSwabShort(&val);
10285 panVals[strile + i] = val;
10286 }
10287 else if (dirent->tdir_type == TIFF_LONG)
10288 {
10289 uint32_t val;
10290 memcpy(&val,
10291 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
10292 sizeof(val));
10293 if (bSwab)
10294 TIFFSwabLong(&val);
10295 panVals[strile + i] = val;
10296 }
10297 else if (dirent->tdir_type == TIFF_LONG8)
10298 {
10299 uint64_t val;
10300 memcpy(&val,
10301 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
10302 sizeof(val));
10303 if (bSwab)
10304 TIFFSwabLong8(&val);
10305 panVals[strile + i] = val;
10306 }
10307 else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
10308 {
10309 /* Non conformant data type */
10310 int64_t val;
10311 memcpy(&val,
10312 buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
10313 sizeof(val));
10314 if (bSwab)
10315 TIFFSwabLong8((uint64_t *)&val);
10316 panVals[strile + i] = (uint64_t)val;
10317 }
10318 }
10319 return 1;
10320}
10321
10375static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
10376 TIFFDirEntry *dirent, uint64_t **parray)
10377{
10378 static const char module[] = "_TIFFFetchStrileValue";
10379 TIFFDirectory *td = &tif->tif_dir;
10380 if (strile >= dirent->tdir_count)
10381 {
10382 return 0;
10383 }
10384 if (strile >= td->td_stripoffsetbyteallocsize)
10385 {
10386 uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
10387 uint32_t nStripArrayAllocNew;
10388 uint64_t nArraySize64;
10389 size_t nArraySize;
10390 uint64_t *offsetArray;
10391 uint64_t *bytecountArray;
10392
10393 if (strile > 1000000)
10394 {
10395 uint64_t filesize = TIFFGetFileSize(tif);
10396 /* Avoid excessive memory allocation attempt */
10397 /* For such a big blockid we need at least a TIFF_LONG per strile */
10398 /* for the offset array. */
10399 if (strile > filesize / sizeof(uint32_t))
10400 {
10401 TIFFErrorExtR(tif, module, "File too short");
10402 return 0;
10403 }
10404 }
10405
10406 if (td->td_stripoffsetbyteallocsize == 0 &&
10407 td->td_nstrips < 1024 * 1024)
10408 {
10409 nStripArrayAllocNew = td->td_nstrips;
10410 }
10411 else
10412 {
10413#define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
10414#define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
10415 nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
10416 if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
10417 nStripArrayAllocNew *= 2;
10418 nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
10419 }
10420 assert(strile < nStripArrayAllocNew);
10421 nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
10422 nArraySize = (size_t)(nArraySize64);
10423#if SIZEOF_SIZE_T == 4
10424 if (nArraySize != nArraySize64)
10425 {
10426 TIFFErrorExtR(tif, module,
10427 "Cannot allocate strip offset and bytecount arrays");
10428 return 0;
10429 }
10430#endif
10431 offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
10432 (tmsize_t)nArraySize));
10433 bytecountArray = (uint64_t *)(_TIFFreallocExt(
10434 tif, td->td_stripbytecount_p, (tmsize_t)nArraySize));
10435 if (offsetArray)
10436 td->td_stripoffset_p = offsetArray;
10437 if (bytecountArray)
10438 td->td_stripbytecount_p = bytecountArray;
10439 if (offsetArray && bytecountArray)
10440 {
10441 td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
10442 /* Initialize new entries to ~0 / -1 */
10443 /* coverity[overrun-buffer-arg] */
10444 memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
10445 (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
10446 sizeof(uint64_t));
10447 /* coverity[overrun-buffer-arg] */
10448 memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
10449 (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
10450 sizeof(uint64_t));
10451 }
10452 else
10453 {
10454 TIFFErrorExtR(tif, module,
10455 "Cannot allocate strip offset and bytecount arrays");
10456 _TIFFfreeExt(tif, td->td_stripoffset_p);
10457 td->td_stripoffset_p = NULL;
10458 _TIFFfreeExt(tif, td->td_stripbytecount_p);
10459 td->td_stripbytecount_p = NULL;
10460 td->td_stripoffsetbyteallocsize = 0;
10461 }
10462 }
10463 if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
10464 return 0;
10465
10466 if (~((*parray)[strile]) == 0)
10467 {
10468 if (!_TIFFPartialReadStripArray(tif, dirent, (int)strile, *parray))
10469 {
10470 (*parray)[strile] = 0;
10471 return 0;
10472 }
10473 }
10474
10475 return 1;
10476}
10477
10501static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
10502 TIFFDirEntry *dirent,
10503 uint64_t **parray,
10504 int *pbErr)
10505{
10506 TIFFDirectory *td = &tif->tif_dir;
10507 if (pbErr)
10508 *pbErr = 0;
10509
10510 if (strile >= td->td_nstrips)
10511 {
10512 if (pbErr)
10513 *pbErr = 1;
10514 return 0;
10515 }
10516
10517 /* Avoid the "dirent->tdir_count <= 4" code path for one of
10518 * StripOffsets/StripByteCounts, and the other code path for the other one,
10519 * which will lead to inconsistencies and potential out-of-bounds reads.
10520 */
10521 if ((td->td_stripoffset_entry.tdir_count <= 4) !=
10523 {
10524 TIFFErrorExtR(tif, "_TIFFGetStrileOffsetOrByteCountValue",
10525 "Inconsistent directory count between StripOffsets and "
10526 "StripByteCounts");
10527 if (pbErr)
10528 *pbErr = 1;
10529 return 0;
10530 }
10531
10532 if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
10534 {
10535 if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) ||
10536 /* If the values may fit in the toff_long/toff_long8 member */
10537 /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
10538 dirent->tdir_count <= 4)
10539 {
10540 if (!_TIFFFillStriles(tif))
10541 {
10542 if (pbErr)
10543 *pbErr = 1;
10544 /* Do not return, as we want this function to always */
10545 /* return the same value if called several times with */
10546 /* the same arguments */
10547 }
10548 }
10549 else
10550 {
10551 if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
10552 {
10553 if (pbErr)
10554 *pbErr = 1;
10555 return 0;
10556 }
10557 }
10558 }
10559 if (*parray == NULL)
10560 {
10561 if (pbErr)
10562 *pbErr = 1;
10563 return 0;
10564 }
10565 return (*parray)[strile];
10566}
10567
10568/* Return the value of the TileOffsets/StripOffsets array for the specified
10569 * tile/strile */
10570uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
10571{
10572 return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
10573}
10574
10575/* Return the value of the TileOffsets/StripOffsets array for the specified
10576 * tile/strile */
10577uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
10578{
10579 TIFFDirectory *td = &tif->tif_dir;
10580 return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
10581 &(td->td_stripoffset_entry),
10582 &(td->td_stripoffset_p), pbErr);
10583}
10584
10585/* Return the value of the TileByteCounts/StripByteCounts array for the
10586 * specified tile/strile */
10587uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
10588{
10589 return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
10590}
10591
10592/* Return the value of the TileByteCounts/StripByteCounts array for the
10593 * specified tile/strile */
10594uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
10595{
10596 TIFFDirectory *td = &tif->tif_dir;
10598 tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
10599 pbErr);
10600}
10601
10603
10625static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
10626{
10627 TIFFDirectory *td = &tif->tif_dir;
10628 int return_value = 1;
10629
10630 /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
10631 if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
10632 (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
10633 return 1;
10634
10635 if ((tif->tif_flags & TIFF_LAZYSTRILELOAD_ASKED) &&
10637 {
10638 /* In case of lazy loading, reload completely the arrays */
10641 td->td_stripoffset_p = NULL;
10642 td->td_stripbytecount_p = NULL;
10645 }
10646
10647 /* If stripoffset array is already loaded, exit with success */
10648 if (td->td_stripoffset_p != NULL)
10649 return 1;
10650
10651 /* If tdir_count was canceled, then we already got there, but in error */
10652 if (td->td_stripoffset_entry.tdir_count == 0)
10653 return 0;
10654
10656 &td->td_stripoffset_p))
10657 {
10658 return_value = 0;
10659 }
10660
10661 if (loadStripByteCount &&
10664 {
10665 return_value = 0;
10666 }
10667
10668 _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
10670
10671#ifdef STRIPBYTECOUNTSORTED_UNUSED
10672 if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
10673 {
10674 uint32_t strip;
10675
10676 tif->tif_dir.td_stripbytecountsorted = 1;
10677 for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
10678 {
10679 if (tif->tif_dir.td_stripoffset_p[strip - 1] >
10680 tif->tif_dir.td_stripoffset_p[strip])
10681 {
10682 tif->tif_dir.td_stripbytecountsorted = 0;
10683 break;
10684 }
10685 }
10686 }
10687#endif
10688
10689 return return_value;
10690}
uint32_t _TIFFParseUInt32(const char *str, uint32_t default_value, uint32_t max_value)
Parse a decimal unsigned integer with a fallback value.
Definition tif_aux.c:58
#define TIFF_MAX_DIR_COUNT
Limit the number of IFDs the library may traverse in a file.
Definition tif_config.h:254
#define STRIP_SIZE_DEFAULT
Define the default target strip size in bytes.
Definition tif_config.h:248
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
Definition tif_config.h:294
int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
Attempt to remove a shared directory-map entry by offset.
int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
Register or validate a chain index/offset pair for loop detection.
static bool insertOffsetAndDirNumber(TIFF *tif, TIFFOffsetAndDirNumber *entryPtr)
Add a directory-tracking record to both directory maps.
int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
Resolve an index from the existing number-to-offset map.
static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
Compare directory-map records by directory index.
int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
Resolve a directory offset using the chain maps.
int TIFFDefaultDirectory(TIFF *tif)
Install the image schema, default values, and core tag methods.
Definition tif_dir.c:2316
static unsigned long hashFuncOffsetToNumber(const void *elt)
Hash a directory-map offset key.
static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
Compare directory-map records by offset.
static unsigned long hashFuncNumberToOffset(const void *elt)
Hash a directory-map directory index key.
#define FIELD_STRIPOFFSETS
Index the strile offsets presence bit; not an on-disk tag number.
Definition tif_dir.h:519
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define FIELD_IMAGEDIMENSIONS
Index the image dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:419
TIFFField * _TIFFCreateAnonField(TIFF *tif, uint32_t tag, TIFFDataType field_type)
Allocate a counted anonymous field descriptor.
#define FIELD_COLORMAP
Index the colormap presence bit; not an on-disk tag number.
Definition tif_dir.h:524
#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 TIFFFieldSet(tif, field)
Test a directory presence bit.
Definition tif_dir.h:662
#define FIELD_MAXSAMPLEVALUE
Index the maximum sample value presence bit; not an on-disk tag number.
Definition tif_dir.h:493
#define FIELD_TILEDIMENSIONS
Index the tile dimensions presence bit; not an on-disk tag number.
Definition tif_dir.h:424
#define FIELD_ROWSPERSTRIP
Index the rows per strip presence bit; not an on-disk tag number.
Definition tif_dir.h:481
#define FIELD_STRIPBYTECOUNTS
Index the strile byte counts presence bit; not an on-disk tag number.
Definition tif_dir.h:514
void _TIFFSetupFields(TIFF *tif, const TIFFFieldArray *fieldarray)
Replace the registered field-pointer table with a schema.
const TIFFFieldArray * _TIFFGetGpsFields(void)
Return the built-in GPS field schema.
int _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
Filter recognised codec-specific tags against current compression.
#define FIELD_BITSPERSAMPLE
Index the bits per sample presence bit; not an on-disk tag number.
Definition tif_dir.h:446
#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.
#define FIELD_PHOTOMETRIC
Index the photometric presence bit; not an on-disk tag number.
Definition tif_dir.h:456
@ TIFF_SETGET_C16_UINT8
Unsigned 8-bit array with 16-bit explicit count.
Definition tif_dir.h:792
@ TIFF_SETGET_C0_SINT64
Signed 64-bit array with implicit count.
Definition tif_dir.h:782
@ TIFF_SETGET_DOUBLE
Double scalar storage.
Definition tif_dir.h:758
@ TIFF_SETGET_SINT64
Signed 64-bit scalar storage.
Definition tif_dir.h:754
@ TIFF_SETGET_C32_SINT32
Signed 32-bit array with 32-bit explicit count.
Definition tif_dir.h:826
@ TIFF_SETGET_C16_FLOAT
Float array with 16-bit explicit count.
Definition tif_dir.h:808
@ TIFF_SETGET_UNDEFINED
Unsupported or unspecified argument representation.
Definition tif_dir.h:736
@ TIFF_SETGET_SINT8
Signed 8-bit scalar storage.
Definition tif_dir.h:742
@ TIFF_SETGET_C32_ASCII
Character array with 32-bit explicit count.
Definition tif_dir.h:814
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
@ TIFF_SETGET_C32_UINT8
Unsigned 8-bit array with 32-bit explicit count.
Definition tif_dir.h:816
@ TIFF_SETGET_IFD8
64-bit directory-offset scalar storage.
Definition tif_dir.h:760
@ TIFF_SETGET_C32_UINT16
Unsigned 16-bit array with 32-bit explicit count.
Definition tif_dir.h:820
@ TIFF_SETGET_C16_DOUBLE
Double array with 16-bit explicit count.
Definition tif_dir.h:810
@ TIFF_SETGET_UINT64
Unsigned 64-bit scalar storage.
Definition tif_dir.h:752
@ TIFF_SETGET_C0_UINT32
Unsigned 32-bit array with implicit count.
Definition tif_dir.h:776
@ TIFF_SETGET_C16_UINT32
Unsigned 32-bit array with 16-bit explicit count.
Definition tif_dir.h:800
@ TIFF_SETGET_UINT16_PAIR
Two separate promoted uint16_t values / uint16_t output slots.
Definition tif_dir.h:764
@ TIFF_SETGET_C32_SINT16
Signed 16-bit array with 32-bit explicit count.
Definition tif_dir.h:822
@ TIFF_SETGET_C16_UINT16
Unsigned 16-bit array with 16-bit explicit count.
Definition tif_dir.h:796
@ TIFF_SETGET_C16_SINT64
Signed 64-bit array with 16-bit explicit count.
Definition tif_dir.h:806
@ TIFF_SETGET_C0_FLOAT
Float array with implicit count.
Definition tif_dir.h:784
@ TIFF_SETGET_C0_SINT32
Signed 32-bit array with implicit count.
Definition tif_dir.h:778
@ TIFF_SETGET_C16_UINT64
Unsigned 64-bit array with 16-bit explicit count.
Definition tif_dir.h:804
@ TIFF_SETGET_C32_UINT32
Unsigned 32-bit array with 32-bit explicit count.
Definition tif_dir.h:824
@ TIFF_SETGET_C0_ASCII
Character array with implicit count.
Definition tif_dir.h:766
@ TIFF_SETGET_C32_SINT8
Signed 8-bit array with 32-bit explicit count.
Definition tif_dir.h:818
@ TIFF_SETGET_UINT16
Unsigned 16-bit scalar storage.
Definition tif_dir.h:744
@ TIFF_SETGET_C16_SINT16
Signed 16-bit array with 16-bit explicit count.
Definition tif_dir.h:798
@ TIFF_SETGET_C0_UINT8
Unsigned 8-bit array with implicit count.
Definition tif_dir.h:768
@ TIFF_SETGET_ASCII
Null-terminated string representation.
Definition tif_dir.h:738
@ TIFF_SETGET_C0_SINT8
Signed 8-bit array with implicit count.
Definition tif_dir.h:770
@ TIFF_SETGET_C16_SINT8
Signed 8-bit array with 16-bit explicit count.
Definition tif_dir.h:794
@ TIFF_SETGET_OTHER
Tag-specific calling convention outside the regular encodings.
Definition tif_dir.h:838
@ TIFF_SETGET_C0_UINT64
Unsigned 64-bit array with implicit count.
Definition tif_dir.h:780
@ TIFF_SETGET_FLOAT
Float scalar storage.
Definition tif_dir.h:756
@ TIFF_SETGET_C16_SINT32
Signed 32-bit array with 16-bit explicit count.
Definition tif_dir.h:802
@ TIFF_SETGET_SINT16
Signed 16-bit scalar storage.
Definition tif_dir.h:746
@ TIFF_SETGET_C0_SINT16
Signed 16-bit array with implicit count.
Definition tif_dir.h:774
@ TIFF_SETGET_C32_IFD8
64-bit directory-offset array with 32-bit explicit count.
Definition tif_dir.h:836
@ TIFF_SETGET_UINT32
Unsigned 32-bit scalar storage.
Definition tif_dir.h:748
@ TIFF_SETGET_C32_SINT64
Signed 64-bit array with 32-bit explicit count.
Definition tif_dir.h:830
@ TIFF_SETGET_C16_ASCII
Character array with 16-bit explicit count.
Definition tif_dir.h:790
@ TIFF_SETGET_C32_DOUBLE
Double array with 32-bit explicit count.
Definition tif_dir.h:834
@ TIFF_SETGET_UINT8
Unsigned 8-bit scalar storage.
Definition tif_dir.h:740
@ TIFF_SETGET_C0_UINT16
Unsigned 16-bit array with implicit count.
Definition tif_dir.h:772
@ TIFF_SETGET_C32_UINT64
Unsigned 64-bit array with 32-bit explicit count.
Definition tif_dir.h:828
@ TIFF_SETGET_C0_IFD8
64-bit directory-offset array with implicit count.
Definition tif_dir.h:788
@ TIFF_SETGET_C16_IFD8
64-bit directory-offset array with 16-bit explicit count.
Definition tif_dir.h:812
@ TIFF_SETGET_SINT32
Signed 32-bit scalar storage.
Definition tif_dir.h:750
@ TIFF_SETGET_C32_FLOAT
Float array with 32-bit explicit count.
Definition tif_dir.h:832
@ TIFF_SETGET_C0_DOUBLE
Double array with implicit count.
Definition tif_dir.h:786
void * TIFFHashSetLookup(TIFFHashSet *set, const void *elt)
Find a borrowed stored element matching a key.
bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
Insert an element or replace an equal element.
bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt)
Remove a matching element, disposing it if configured.
int TIFFHashSetSize(const TIFFHashSet *set)
Return the current live-element count.
TIFFHashSet * TIFFHashSetNew(TIFF *tif, TIFFHashSetHashFunc fnHashFunc, TIFFHashSetEqualFunc fnEqualFunc, TIFFHashSetFreeEltFunc fnFreeEltFunc)
Allocate an empty hash set with optional ownership callbacks.
#define FAILED_FII
Represent a missing field-table index with UINT32_MAX.
Definition tif_dirread.c:54
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
Read a prevalidated uint64_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry, double *value)
Read a prevalidated srational value.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
Read a directory array as int64_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
Check whether uint32_t fits int16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
Convert one directory value to uint16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
Check whether int8_t fits uint64_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
Check whether uint32_t fits uint16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
Check whether int64_t fits uint64_t.
#define INITIAL_THRESHOLD
Start progressive external-tag reads at one MiB on 64-bit hosts.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
Check whether uint32_t fits uint8_t.
static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
Read a prevalidated int32_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
Check whether int16_t fits int8_t.
static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid, uint32_t *fii)
Locate the first registered descriptor index for a tag.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
Check whether int8_t fits uint16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, uint32_t desttypesize, void **value)
Fetch raw directory array bytes with checked size limits.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
Check whether int16_t fits uint16_t.
static void CalcFinalIFDdatasizeReading(TIFF *tif, uint16_t dircount)
Estimate the reusable contiguous span of the loaded IFD.
#define MAX_THRESHOLD
Set the progressive read threshold cutoff to 1000 MiB.
static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
Read a prevalidated uint16_t value.
static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir, uint16_t dircount)
Warn if directory tags are not strictly increasing.
static uint64_t TIFFReadUInt64(const uint8_t *value)
Copy eight unaligned bytes into a native uint64_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value, uint64_t maxcount)
Read a directory array as uint64_t elements.
static int ByteCountLooksBad(TIFF *tif)
Test whether the first strip byte count needs recovery.
static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
Read a directory array as double elements.
static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err, const char *module, const char *tagname, int recover)
Report a directory-entry conversion error at the requested severity.
static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, uint32_t desttypesize, void **value, uint64_t maxcount)
Fetch raw directory array bytes with checked size limits.
static TIFFDirEntry * TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir, uint16_t dircount, uint16_t tagid)
Find the first parsed entry with a given tag.
static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
Convert one directory value to float.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
Check whether uint16_t fits uint8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
Check whether uint16_t fits int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
Check whether int64_t fits int32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
Convert one directory value to int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
Check whether uint32_t fits int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
Convert one directory value to uint8_t.
TIFFReadDirEntryErr
Classify directory-entry decoding failures.
Definition tiffiop.h:3478
static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
Read a prevalidated uint8_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
Check whether int32_t fits int16_t.
static bool EvaluateIFDdatasizeReading(TIFF *tif, TIFFDirEntry *dp)
Record the external byte range of one IFD entry.
static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset, tmsize_t size, void *dest)
Copy a raw external byte range into caller storage.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
Convert one directory value to int64_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
Check whether uint64_t fits uint8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
Check whether int32_t fits uint32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
Check whether int8_t fits uint8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
Check whether uint32_t fits int32_t.
static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
Order external IFD ranges by starting byte offset.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
Check whether int8_t fits uint32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif, uint64_t offset, tmsize_t size, void **pdest)
Grow an owned buffer while reading an external value.
static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff, TIFFDirEntry **pdir, uint64_t *nextdiroff)
Fetch and normalise raw classic TIFF or BigTIFF entries.
static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
Convert one directory value to double.
static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
Read a directory array as uint64_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
Check whether int32_t fits uint8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
Check whether int16_t fits uint32_t.
static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
Read a prevalidated float value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
Convert one directory value to uint32_t.
#define THRESHOLD_MULTIPLIER
Grow progressive read requests by a factor of ten.
static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover)
Decode a parsed value according to its registered set/get representation.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
Read a directory array as int8_t elements.
static void MissingRequired(TIFF *, const char *)
Report an absent required image tag.
static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
Read a directory array as uint16_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
Check whether int32_t fits int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
Check whether uint64_t fits uint16_t.
static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
Read a prevalidated int16_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
Check whether int32_t fits uint16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
Check whether uint64_t fits int32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
Read a directory array as int16_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
Read a directory array as int32_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
Read a directory array as float elements.
static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
Read a prevalidated int8_t value.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
Check whether uint64_t fits int8_t.
static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
Read a prevalidated uint32_t value.
static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir, uint16_t dircount)
Replace missing or suspect strile byte counts with estimates.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
Check whether uint8_t fits int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
Check whether int64_t fits int8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
Check whether int64_t fits int16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
Check whether uint64_t fits uint32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
Read a prevalidated int64_t value.
static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t)
Validate or trim an entry to its expected count.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
Check whether int32_t fits uint64_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
Convert one directory value to uint64_t directory offset.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
Convert one directory value to int32_t.
#define TIFFCvtIEEEDoubleToNative(tif, n, dp)
Recognise already compatible double storage without conversion.
Definition tif_dirread.c:84
enum TIFFReadDirEntryErr _TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry, double *value)
Read a prevalidated rational value.
static int _TIFFGetMaxColorChannels(uint16_t photometric)
Classify the expected maximum colour-channel count.
#define MAX_SIZE_TAG_DATA
Limit source and converted tag arrays to INT32_MAX bytes.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
Check whether uint64_t fits int16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
Check whether int16_t fits uint8_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
Convert one directory value to uint64_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
Read a prevalidated double value.
static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *)
Decode EXIF SubjectDistance with its special sentinel values.
static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
Read a directory array as uint64_t directory offset elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
Check whether int64_t fits uint32_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
Check whether uint16_t fits int16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
Read a directory array as uint32_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
Check whether int64_t fits uint8_t.
union _UInt64Aligned_t UInt64Aligned_t
Provide aligned scalar and byte views of eight raw bytes.
static enum TIFFReadDirEntryErr TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
Convert one directory value to int16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
Check whether int64_t fits uint16_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
Read a directory array as uint8_t elements.
static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
Reduce equal per-sample short values to one scalar.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
Check whether int16_t fits uint64_t.
static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
Check whether uint64_t fits int64_t.
#define TIFFCvtIEEEFloatToNative(tif, n, fp)
Recognise already compatible float storage without conversion.
Definition tif_dirread.c:70
@ TIFFReadDirEntryErrPsdif
Per-sample values differ where uniform values are required.
Definition tiffiop.h:3490
@ TIFFReadDirEntryErrIo
Mapped extent, seek, or read failure.
Definition tiffiop.h:3486
@ TIFFReadDirEntryErrRange
Value cannot be represented by the target integer type.
Definition tiffiop.h:3488
@ TIFFReadDirEntryErrSizesan
Source or target array exceeds the configured sanity ceiling.
Definition tiffiop.h:3492
@ TIFFReadDirEntryErrAlloc
Allocation, policy, or large-request/file-size rejection.
Definition tiffiop.h:3494
@ TIFFReadDirEntryErrType
Unsupported source file data type.
Definition tiffiop.h:3484
@ TIFFReadDirEntryErrCount
Unsupported or inconsistent element count.
Definition tiffiop.h:3482
@ TIFFReadDirEntryErrOk
Successful or accepted empty conversion.
Definition tiffiop.h:3480
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
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
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Allocate a checked array with an allocation-failure diagnostic.
Definition tif_aux.c:274
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array under a handle's allocation policy.
Definition tif_open.c:224
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
#define TIFF_NON_EXISTENT_DIR_NUMBER
Mark an unknown or nonexistent directory index.
Definition tiffiop.h:95
#define TIFFReadFile(tif, buf, size)
Invoke the handle's client read callback.
Definition tiffiop.h:885
#define TIFF_CHOPPEDUPARRAYS
Mark strip arrays replaced by the strip-chopping operation.
Definition tiffiop.h:547
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define isUpSampled(tif)
Test whether the codec reports upsampled output.
Definition tiffiop.h:869
#define TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
Suppress unsigned-overflow instrumentation on supported Clang functions.
Definition tiffiop.h:1484
#define ReadOK(tif, buf, size)
Test whether one client read transfers the entire requested byte count.
Definition tiffiop.h:1000
#define TIFF_PERSAMPLE
Select per-sample arrays for supported sample-valued fields.
Definition tiffiop.h:523
#define TIFF_LAZYSTRILELOAD_ASKED
Record a lazy strip/tile loading request from the O open flag.
Definition tiffiop.h:552
#define TIFF_LAZYSTRILELOAD_DONE
Record a switch from on-demand to full strip/tile-array loading.
Definition tiffiop.h:542
#define SeekOK(tif, off)
Test whether an absolute seek reaches the requested file position.
Definition tiffiop.h:1017
#define TIFF_STRIPCHOP
Enable internal chopping of a large uncompressed strip.
Definition tiffiop.h:486
#define isMapped(tif)
Test whether TIFF_MAPPED is set on a handle.
Definition tiffiop.h:845
#define TIFF_DEFERSTRILELOAD
Defer loading strip/tile offset and byte-count arrays.
Definition tiffiop.h:533
#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 TIFFGetFileSize(tif)
Invoke the handle's client size callback.
Definition tiffiop.h:946
#define TIFFSeekFile(tif, off, whence)
Invoke the handle's client seek callback.
Definition tiffiop.h:920
#define TIFF_BIGTIFF
Mark the BigTIFF file format.
Definition tiffiop.h:508
#define TIFF_SWAB
Request swapping multibyte file values to or from host order.
Definition tiffiop.h:441
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
#define TIFF_ISTILED
Mark tile-based rather than strip-based image storage.
Definition tiffiop.h:456
static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile, TIFFDirEntry *dirent, uint64_t **parray)
Grow paired strile caches and fetch an uncached value.
static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile, TIFFDirEntry *dirent, uint64_t **parray, int *pbErr)
Resolve a cached, fully deferred, or lazy strile metadata value.
int _TIFFFillStriles(TIFF *tif)
Materialise deferred strile arrays when required.
static void TryChopUpUncompressedBigTiff(TIFF *)
Try to subdivide huge contiguous uncompressed strip data.
static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent, int strile, uint64_t *panVals)
Load a strile value and neighbouring page entries into a cache.
#define IO_CACHE_PAGE_SIZE
Set the lazy strile metadata read page size to 4096 bytes.
#define TIFF_MIN(a, b)
Select the smaller of two expressions.
#define TIFF_MAX(a, b)
Select the larger of two expressions.
static void ChopUpSingleUncompressedStrip(TIFF *)
Try to subdivide one contiguous uncompressed strip.
static TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
Add a signed displacement with intentional unsigned wrap.
static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
Materialise deferred strile arrays or recognise an already-loaded state.
static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips, uint64_t stripbytes, uint32_t rowsperstrip)
Replace uncompressed strip metadata with a logical subdivision.
static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, uint64_t **lpp)
Read a strile array and pad permitted missing elements with zero.
void TIFFSwabArrayOfLong8(uint64_t *lp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:144
void TIFFSwabArrayOfShort(uint16_t *wp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:87
void TIFFSwabLong8(uint64_t *lp)
Reverse the byte order of one value.
Definition tif_swab.c:66
void TIFFSwabShort(uint16_t *wp)
Reverse the byte order of one value.
Definition tif_swab.c:39
void TIFFSwabArrayOfLong(uint32_t *lp, tmsize_t n)
Reverse the byte order of each array element.
Definition tif_swab.c:123
void TIFFSwabLong(uint32_t *lp)
Reverse the byte order of one value.
Definition tif_swab.c:51
#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
void TIFFFreeDirectory(TIFF *tif)
Release the current directory's owned values and codec state.
Definition tif_dir.c:2165
#define TIFFTAG_EP_BATTERYLEVEL
TIFF tag: Battery level (rational or ASCII).
Definition tiff.h:1407
#define EXIFTAG_SUBJECTDISTANCE
EXIF tag: Subject distance.
Definition tiff.h:1726
int TIFFFieldIsAnonymous(const TIFFField *fip)
Test whether a field was registered as an unknown tag.
const TIFFField * TIFFFieldWithTag(TIFF *tif, uint32_t tag)
Find a registered field by tag number.
int TIFFDataWidth(TIFFDataType type)
Report the on-disk byte width of a TIFF data type.
int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff, const TIFFFieldArray *infoarray)
Read metadata using an explicit custom field schema.
int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
Read a EXIF GPS metadata directory.
int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
Read a EXIF metadata directory.
int TIFFReadDirectory(TIFF *tif)
Read and install the next image directory.
int TIFFSetupStrips(TIFF *tif)
Replace strile offset/count arrays from current geometry.
Definition tif_write.c:729
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
tmsize_t TIFFStripSize(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:244
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested tile height.
Definition tif_tile.c:287
uint64_t TIFFScanlineSize64(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:297
uint32_t TIFFNumberOfStrips(TIFF *tif)
Compute the strip count from current image geometry.
Definition tif_strip.c:82
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
Definition tif_strip.c:177
uint64_t TIFFTileSize64(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:303
uint64_t TIFFStripSize64(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:236
uint32_t TIFFNumberOfTiles(TIFF *tif)
Compute the tile count across image depth and sample planes.
Definition tif_tile.c:155
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
uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
Query one logical strile file offset.
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
Query one logical strile file offset.
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
Query one logical strile byte count.
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
Query one logical strile byte count.
int TIFFSetField(TIFF *tif, uint32_t tag,...)
Set a tag value in the current in-memory directory.
Definition tif_dir.c:1627
#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 PHOTOMETRIC_CFA
Color filter array.
Definition tiff.h:404
#define OFILETYPE_REDUCEDIMAGE
Reduced size image data.
Definition tiff.h:290
#define OFILETYPE_PAGE
One page of many.
Definition tiff.h:292
#define COMPRESSION_OJPEG
6.0 JPEG (introduced in TIFF 6.0).
Definition tiff.h:316
#define PHOTOMETRIC_MASK
Transparency mask (obsoleted by TIFF 6.0).
Definition tiff.h:392
#define TIFFTAG_SAMPLESPERPIXEL
TIFF tag: Samples per pixel.
Definition tiff.h:456
#define PHOTOMETRIC_LOGL
CIE Log2(L).
Definition tiff.h:406
#define TIFFTAG_DATATYPE
TIFF tag: Legacy sample-type indicator; superseded by TIFFTAG_SAMPLEFORMAT (obsoleted by TIFF 6....
Definition tiff.h:770
#define PHOTOMETRIC_ITULAB
ITU L*a*b*.
Definition tiff.h:402
#define TIFFTAG_COLORMAP
TIFF tag: RGB map for palette image.
Definition tiff.h:560
#define PHOTOMETRIC_CIELAB
1976 CIE L*a*b* (introduced in TIFF 6.0).
Definition tiff.h:398
#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_OSUBFILETYPE
TIFF tag: Old subfile classification; superseded by TIFFTAG_SUBFILETYPE (obsoleted by TIFF 5....
Definition tiff.h:286
#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 PHOTOMETRIC_PALETTE
Color map indexed.
Definition tiff.h:390
#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 PHOTOMETRIC_SEPARATED
Color separations (introduced in TIFF 6.0).
Definition tiff.h:394
#define FILETYPE_REDUCEDIMAGE
Reduced resolution version.
Definition tiff.h:277
#define PHOTOMETRIC_LOGLUV
CIE Log2(L) (u',v').
Definition tiff.h:408
#define TIFFTAG_MINSAMPLEVALUE
TIFF tag: Minimum sample value (obsoleted by TIFF 5.0).
Definition tiff.h:462
#define FILETYPE_PAGE
One page of many.
Definition tiff.h:279
#define PHOTOMETRIC_ICCLAB
ICC L*a*b* [Adobe TIFF Technote 4].
Definition tiff.h:400
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define TIFFTAG_TRANSFERFUNCTION
TIFF tag: Colorimetry info (introduced in TIFF 6.0).
Definition tiff.h:538
#define TIFFTAG_TILEBYTECOUNTS
TIFF tag: Byte counts for tiles (introduced in TIFF 6.0).
Definition tiff.h:570
#define PHOTOMETRIC_MINISWHITE
Min value is white.
Definition tiff.h:384
#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_TILEOFFSETS
TIFF tag: Offsets to data tiles (introduced in TIFF 6.0).
Definition tiff.h:568
#define TIFFTAG_ROWSPERSTRIP
TIFF tag: Rows per strip of data.
Definition tiff.h:458
#define TIFFTAG_SMAXSAMPLEVALUE
TIFF tag: Variable MaxSampleValue (introduced in TIFF 6.0).
Definition tiff.h:624
#define TIFFTAG_SAMPLEFORMAT
TIFF tag: Data sample format (introduced in TIFF 6.0).
Definition tiff.h:608
#define TIFFTAG_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_COMPRESSION
TIFF tag: Data compression technique.
Definition tiff.h:300
#define PHOTOMETRIC_MINISBLACK
Min value is black.
Definition tiff.h:386
#define PHOTOMETRIC_RGB
RGB color model.
Definition tiff.h:388
#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_RICHTIFFIPTC
TIFF tag: RichTIFF IPTC metadata.
Definition tiff.h:844
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
TIFFDataType
Identify the on-disk representation of a TIFF directory field.
Definition tiff.h:229
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
uint32_t tdir_t
Represent a zero-based directory index, rather than a file offset.
Definition tiffio.h:96
@ TIFF_SSHORT
Signed 16-bit integer.
Definition tiff.h:247
@ TIFF_SLONG
Signed 32-bit integer.
Definition tiff.h:249
@ TIFF_BYTE
Unsigned 8-bit integer.
Definition tiff.h:233
@ TIFF_SBYTE
Signed 8-bit integer.
Definition tiff.h:243
@ TIFF_IFD
Unsigned 32-bit IFD byte offset.
Definition tiff.h:257
@ TIFF_SHORT
Unsigned 16-bit integer.
Definition tiff.h:237
@ TIFF_UNDEFINED
Untyped 8-bit data.
Definition tiff.h:245
@ TIFF_LONG
Unsigned 32-bit integer.
Definition tiff.h:239
@ TIFF_SLONG8
BigTIFF signed 64-bit integer.
Definition tiff.h:261
@ TIFF_LONG8
BigTIFF unsigned 64-bit integer.
Definition tiff.h:259
@ TIFF_ASCII
Sequence of 8-bit bytes ending in a null byte.
Definition tiff.h:235
@ TIFF_FLOAT
32-bit IEEE floating-point value.
Definition tiff.h:253
@ TIFF_DOUBLE
64-bit IEEE floating-point value.
Definition tiff.h:255
@ TIFF_IFD8
BigTIFF unsigned 64-bit IFD byte offset.
Definition tiff.h:263
@ TIFF_SRATIONAL
Signed 32-bit numerator and denominator.
Definition tiff.h:251
@ TIFF_RATIONAL
Unsigned 32-bit numerator and denominator.
Definition tiff.h:241
Represent a parsed directory entry before value conversion.
Definition tif_dir.h:82
uint64_t tdir_count
Number of file-type elements, normalised to host order.
Definition tif_dir.h:89
uint16_t toff_short
Raw 16-bit view of inline bytes or offset storage.
Definition tif_dir.h:95
uint16_t tdir_type
On-disk TIFFDataType code.
Definition tif_dir.h:86
uint64_t toff_long8
Raw 64-bit view of inline bytes or offset storage.
Definition tif_dir.h:99
uint16_t tdir_tag
Directory tag identifier.
Definition tif_dir.h:84
union TIFFDirEntry::@0 tdir_offset
Overlay raw directory inline bytes and offset-width views.
uint8_t tdir_ignore
Nonzero to suppress this entry during parsing, e.g. duplicate tags.
Definition tif_dir.h:109
uint32_t toff_long
Raw 32-bit view of inline bytes or offset storage.
Definition tif_dir.h:97
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_tiledepth
Tile depth in slices.
Definition tif_dir.h:164
uint32_t td_dirdatasize_Noffsets
Number of ranges collected in td_dirdatasize_offsets.
Definition tif_dir.h:377
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
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
uint16_t td_extrasamples
Number of extra samples described by td_sampleinfo.
Definition tif_dir.h:232
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
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_maxsamplevalue
Legacy unsigned maximum sample value.
Definition tif_dir.h:188
uint32_t td_imagedepth
Image depth in slices.
Definition tif_dir.h:158
uint32_t td_stripsperimage
Strips or tiles per sample plane, despite the historic name.
Definition tif_dir.h:269
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
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_ycbcrsubsampling[2]
Horizontal and vertical YCbCr subsampling factors.
Definition tif_dir.h:312
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_nstrips
Total strile count across all planes; logical array length.
Definition tif_dir.h:271
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_tilelength
Tile length in rows.
Definition tif_dir.h:162
Describe an external directory-value byte range.
Definition tif_dir.h:120
uint64_t offset
External value offset in file bytes.
Definition tif_dir.h:122
uint64_t length
External value length in bytes.
Definition tif_dir.h:124
Represent a BigTIFF header with a 64-bit directory offset.
Definition tiff.h:195
Represent a classic TIFF header with a 32-bit directory offset.
Definition tiff.h:181
Pair a directory file offset with its main-IFD index.
Definition tiffiop.h:341
tdir_t dirNumber
Zero-based main-directory index associated with offset.
Definition tiffiop.h:345
uint64_t offset
Directory byte offset from the beginning of the file.
Definition tiffiop.h:343
Represent an unsigned TIFF rational as numerator and denominator.
Definition tiffio.h:726
uint32_t uDenom
Unsigned 32-bit denominator; the representation itself performs no validation.
Definition tiffio.h:733
uint32_t uNum
Unsigned 32-bit numerator.
Definition tiffio.h:728
Describe a borrowed schema or owned compatibility batch.
Definition tif_dir.h:962
uint32_t count
Number of valid descriptors in fields.
Definition tif_dir.h:972
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
TIFFDataType field_type
On-disk TIFFDataType, distinct from the argument representation.
Definition tif_dir.h:1009
unsigned char field_passcount
Nonzero for an explicit count argument; tag-specific exceptions still apply.
Definition tif_dir.h:1033
const char * field_name
Borrowed or anonymous-owned null-terminated name; must remain live while used.
Definition tif_dir.h:1038
unsigned short field_bit
FIELD_* presence index; FIELD_CUSTOM shares storage through the custom list.
Definition tif_dir.h:1023
uint32_t field_tag
Tag identifier in this schema namespace.
Definition tif_dir.h:995
short field_readcount
Fixed getter count or TIFF_VARIABLE/TIFF_VARIABLE2/TIFF_SPP rule.
Definition tif_dir.h:1001
TIFFSetGetFieldType set_get_field_type
Internal storage/count representation for tag access and conversion.
Definition tif_dir.h:1018
Associate an anonymous on-disk tag with a friendly print label.
Definition tif_print.c:106
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
int tif_warn_about_unknown_tags
Nonzero to emit warnings for unknown tags.
Definition tiffiop.h:777
int tif_setdirectory_force_absolute
Nonzero to force absolute rather than relative directory stepping.
Definition tiffiop.h:570
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
TIFFHashSet * tif_map_dir_number_to_offset
Handle-managed directory-index-to-offset lookup map.
Definition tiffiop.h:566
size_t tif_nfields
Number of entries in tif_fields.
Definition tiffiop.h:735
uint64_t tif_nextdiroff
Next directory byte offset from the current chain link.
Definition tiffiop.h:560
TIFFField ** tif_fields
Owned sorted array of field pointers; descriptors have mixed ownership.
Definition tiffiop.h:733
TIFFHashSet * tif_map_dir_offset_to_number
Handle-managed directory-offset-to-index lookup map.
Definition tiffiop.h:564
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
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
int tif_mode
Current access mode using the O_* constants.
Definition tiffiop.h:400
uint8_t * tif_base
Mapped-file base provided by tif_mapproc; released through tif_unmapproc by TIFFCleanup() only while ...
Definition tiffiop.h:703
Provide configured integer types and legacy compatibility features.
Declare private library state, callbacks, and implementation helpers.
Provide aligned scalar and byte views of eight raw bytes.
uint8_t c[8]
Eight raw octets in copied order.
uint32_t i[2]
Two native 32-bit component views.
uint16_t s[4]
Four native 16-bit component views.
uint64_t l
Native uint64_t view.
double d
Native double view.