Debian Codegraph @

frmts/gtiff/libtiff/tif_dir.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * TIFF Library.
27 *
28 * Directory Tag Get & Set Routines.
29 * (and also some miscellaneous stuff)
30 */
31 #include "tiffiop.h"
32 #include <float.h> /*--: for Rational2Double */
33 #include <limits.h>
34 #include <math.h>
35
36 /*
37 * These are used in the backwards compatibility code...
38 */
39 #define DATATYPE_VOID 0 /* !untyped data */
40 #define DATATYPE_INT 1 /* !signed integer data */
41 #define DATATYPE_UINT 2 /* !unsigned integer data */
42 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
43
44 static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb,
45 size_t elem_size)
46 {
47 if (*vpp)
48 {
49 _TIFFfreeExt(tif, *vpp);
50 *vpp = 0;
51 }
52 if (vp)
53 {
54 tmsize_t bytes = _TIFFMultiplySSize(NULL, (tmsize_t)nmemb,
55 (tmsize_t)elem_size, NULL);
56 if (bytes)
57 *vpp = (void *)_TIFFmallocExt(tif, bytes);
58 if (*vpp)
59 _TIFFmemcpy(*vpp, vp, bytes);
60 }
61 }
62 void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n)
63 {
64 setByteArray(NULL, vpp, vp, n, 1);
65 }
66 void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
67 {
68 setByteArray(tif, vpp, vp, n, 1);
69 }
70
71 static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n)
72 {
73 setByteArray(tif, (void **)cpp, cp, n, 1);
74 }
75
76 void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n)
77 {
78 setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t));
79 }
80 void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp,
81 uint32_t n)
82 {
83 setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t));
84 }
85
86 void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n)
87 {
88 setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t));
89 }
90 void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp,
91 uint32_t n)
92 {
93 setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t));
94 }
95
96 static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp,
97 uint32_t n)
98 {
99 setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t));
100 }
101
102 void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n)
103 {
104 setByteArray(NULL, (void **)fpp, fp, n, sizeof(float));
105 }
106 void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n)
107 {
108 setByteArray(tif, (void **)fpp, fp, n, sizeof(float));
109 }
110
111 void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n)
112 {
113 setByteArray(NULL, (void **)dpp, dp, n, sizeof(double));
114 }
115 void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp,
116 uint32_t n)
117 {
118 setByteArray(tif, (void **)dpp, dp, n, sizeof(double));
119 }
120
121 static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value,
122 size_t nmemb)
123 {
124 if (*vpp)
125 _TIFFfreeExt(tif, *vpp);
126 *vpp = (double *)_TIFFmallocExt(tif,
127 (tmsize_t)nmemb * (tmsize_t)sizeof(double));
128 if (*vpp)
129 {
130 while (nmemb--)
131 ((double *)*vpp)[nmemb] = value;
132 }
133 }
134
135 /*
136 * Install extra samples information.
137 */
138 static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
139 {
140 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
141 #define EXTRASAMPLE_COREL_UNASSALPHA 999
142
143 uint16_t *va;
144 uint32_t i;
145 TIFFDirectory *td = &tif->tif_dir;
146 static const char module[] = "setExtraSamples";
147
148 *v = (uint16_t)va_arg(ap, uint16_vap);
149 if ((uint16_t)*v > td->td_samplesperpixel)
150 return 0;
151 va = va_arg(ap, uint16_t *);
152 if (*v > 0 && va == NULL) /* typically missing param */
153 return 0;
154 for (i = 0; i < *v; i++)
155 {
156 if (va[i] > EXTRASAMPLE_UNASSALPHA)
157 {
158 /*
159 * XXX: Corel Draw is known to produce incorrect
160 * ExtraSamples tags which must be patched here if we
161 * want to be able to open some of the damaged TIFF
162 * files:
163 */
164 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
165 va[i] = EXTRASAMPLE_UNASSALPHA;
166 else
167 return 0;
168 }
169 }
170
171 if (td->td_transferfunction[0] != NULL &&
172 (td->td_samplesperpixel - *v > 1) &&
173 !(td->td_samplesperpixel - td->td_extrasamples > 1))
174 {
175 TIFFWarningExtR(tif, module,
176 "ExtraSamples tag value is changing, "
177 "but TransferFunction was read with a different value. "
178 "Canceling it");
179 TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
180 _TIFFfreeExt(tif, td->td_transferfunction[0]);
181 td->td_transferfunction[0] = NULL;
182 }
183
184 td->td_extrasamples = (uint16_t)*v;
185 _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples);
186 return 1;
187
188 #undef EXTRASAMPLE_COREL_UNASSALPHA
189 }
190
191 /*
192 * Count ink names separated by \0. Returns
193 * zero if the ink names are not as expected.
194 */
195 static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
196 {
197 uint16_t i = 0;
198
199 if (slen > 0)
200 {
201 const char *ep = s + slen;
202 const char *cp = s;
203 do
204 {
205 for (; cp < ep && *cp != '\0'; cp++)
206 {
207 }
208 if (cp >= ep)
209 goto bad;
210 cp++; /* skip \0 */
211 i++;
212 } while (cp < ep);
213 return (i);
214 }
215 bad:
216 TIFFErrorExtR(tif, "TIFFSetField",
217 "%s: Invalid InkNames value; no null at given buffer end "
218 "location %" PRIu32 ", after %" PRIu16 " ink",
219 tif->tif_name, slen, i);
220 return (0);
221 }
222
223 static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
224 {
225 static const char module[] = "_TIFFVSetField";
226
227 TIFFDirectory *td = &tif->tif_dir;
228 int status = 1;
229 uint32_t v32, v;
230 double dblval;
231 char *s;
232 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
233 uint32_t standard_tag = tag;
234 if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */
235 return 0;
236 /*
237 * We want to force the custom code to be used for custom
238 * fields even if the tag happens to match a well known
239 * one - important for reinterpreted handling of standard
240 * tag values in custom directories (i.e. EXIF)
241 */
242 if (fip->field_bit == FIELD_CUSTOM)
243 {
244 standard_tag = 0;
245 }
246
247 switch (standard_tag)
248 {
249 case TIFFTAG_SUBFILETYPE:
250 td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t);
251 break;
252 case TIFFTAG_IMAGEWIDTH:
253 td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t);
254 break;
255 case TIFFTAG_IMAGELENGTH:
256 td->td_imagelength = (uint32_t)va_arg(ap, uint32_t);
257 break;
258 case TIFFTAG_BITSPERSAMPLE:
259 td->td_bitspersample = (uint16_t)va_arg(ap, uint16_vap);
260 /*
261 * If the data require post-decoding processing to byte-swap
262 * samples, set it up here. Note that since tags are required
263 * to be ordered, compression code can override this behavior
264 * in the setup method if it wants to roll the post decoding
265 * work in with its normal work.
266 */
267 if (tif->tif_flags & TIFF_SWAB)
268 {
269 if (td->td_bitspersample == 8)
270 tif->tif_postdecode = _TIFFNoPostDecode;
271 else if (td->td_bitspersample == 16)
272 tif->tif_postdecode = _TIFFSwab16BitData;
273 else if (td->td_bitspersample == 24)
274 tif->tif_postdecode = _TIFFSwab24BitData;
275 else if (td->td_bitspersample == 32)
276 tif->tif_postdecode = _TIFFSwab32BitData;
277 else if (td->td_bitspersample == 64)
278 tif->tif_postdecode = _TIFFSwab64BitData;
279 else if (td->td_bitspersample == 128) /* two 64's */
280 tif->tif_postdecode = _TIFFSwab64BitData;
281 }
282 break;
283 case TIFFTAG_COMPRESSION:
284 v = (uint16_t)va_arg(ap, uint16_vap);
285 /*
286 * If we're changing the compression scheme, notify the
287 * previous module so that it can cleanup any state it's
288 * setup.
289 */
290 if (TIFFFieldSet(tif, FIELD_COMPRESSION))
291 {
292 if ((uint32_t)td->td_compression == v)
293 break;
294 (*tif->tif_cleanup)(tif);
295 tif->tif_flags &= ~TIFF_CODERSETUP;
296 }
297 /*
298 * Setup new compression routine state.
299 */
300 if ((status = TIFFSetCompressionScheme(tif, (int)v)) != 0)
301 td->td_compression = (uint16_t)v;
302 else
303 status = 0;
304 break;
305 case TIFFTAG_PHOTOMETRIC:
306 td->td_photometric = (uint16_t)va_arg(ap, uint16_vap);
307 break;
308 case TIFFTAG_THRESHHOLDING:
309 td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap);
310 break;
311 case TIFFTAG_FILLORDER:
312 v = (uint16_t)va_arg(ap, uint16_vap);
313 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
314 goto badvalue;
315 td->td_fillorder = (uint16_t)v;
316 break;
317 case TIFFTAG_ORIENTATION:
318 v = (uint16_t)va_arg(ap, uint16_vap);
319 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
320 goto badvalue;
321 else
322 td->td_orientation = (uint16_t)v;
323 break;
324 case TIFFTAG_SAMPLESPERPIXEL:
325 v = (uint16_t)va_arg(ap, uint16_vap);
326 if (v == 0)
327 goto badvalue;
328 if (v != td->td_samplesperpixel)
329 {
330 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
331 if (td->td_sminsamplevalue != NULL)
332 {
333 TIFFWarningExtR(tif, module,
334 "SamplesPerPixel tag value is changing, "
335 "but SMinSampleValue tag was read with a "
336 "different value. Canceling it");
337 TIFFClrFieldBit(tif, FIELD_SMINSAMPLEVALUE);
338 _TIFFfreeExt(tif, td->td_sminsamplevalue);
339 td->td_sminsamplevalue = NULL;
340 }
341 if (td->td_smaxsamplevalue != NULL)
342 {
343 TIFFWarningExtR(tif, module,
344 "SamplesPerPixel tag value is changing, "
345 "but SMaxSampleValue tag was read with a "
346 "different value. Canceling it");
347 TIFFClrFieldBit(tif, FIELD_SMAXSAMPLEVALUE);
348 _TIFFfreeExt(tif, td->td_smaxsamplevalue);
349 td->td_smaxsamplevalue = NULL;
350 }
351 /* Test if 3 transfer functions instead of just one are now
352 needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820
353 */
354 if (td->td_transferfunction[0] != NULL &&
355 (v - td->td_extrasamples > 1) &&
356 !(td->td_samplesperpixel - td->td_extrasamples > 1))
357 {
358 TIFFWarningExtR(tif, module,
359 "SamplesPerPixel tag value is changing, "
360 "but TransferFunction was read with a "
361 "different value. Canceling it");
362 TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
363 _TIFFfreeExt(tif, td->td_transferfunction[0]);
364 td->td_transferfunction[0] = NULL;
365 }
366 }
367 td->td_samplesperpixel = (uint16_t)v;
368 break;
369 case TIFFTAG_ROWSPERSTRIP:
370 v32 = (uint32_t)va_arg(ap, uint32_t);
371 if (v32 == 0)
372 goto badvalue32;
373 td->td_rowsperstrip = v32;
374 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
375 {
376 td->td_tilelength = v32;
377 td->td_tilewidth = td->td_imagewidth;
378 }
379 break;
380 case TIFFTAG_MINSAMPLEVALUE:
381 td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
382 break;
383 case TIFFTAG_MAXSAMPLEVALUE:
384 td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
385 break;
386 case TIFFTAG_SMINSAMPLEVALUE:
387 if (tif->tif_flags & TIFF_PERSAMPLE)
388 _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue,
389 va_arg(ap, double *),
390 td->td_samplesperpixel);
391 else
392 setDoubleArrayOneValue(tif, &td->td_sminsamplevalue,
393 va_arg(ap, double),
394 td->td_samplesperpixel);
395 break;
396 case TIFFTAG_SMAXSAMPLEVALUE:
397 if (tif->tif_flags & TIFF_PERSAMPLE)
398 _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue,
399 va_arg(ap, double *),
400 td->td_samplesperpixel);
401 else
402 setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue,
403 va_arg(ap, double),
404 td->td_samplesperpixel);
405 break;
406 case TIFFTAG_XRESOLUTION:
407 dblval = va_arg(ap, double);
408 if (isnan(dblval) || dblval < 0)
409 goto badvaluedouble;
410 td->td_xresolution = _TIFFClampDoubleToFloat(dblval);
411 break;
412 case TIFFTAG_YRESOLUTION:
413 dblval = va_arg(ap, double);
414 if (isnan(dblval) || dblval < 0)
415 goto badvaluedouble;
416 td->td_yresolution = _TIFFClampDoubleToFloat(dblval);
417 break;
418 case TIFFTAG_PLANARCONFIG:
419 v = (uint16_t)va_arg(ap, uint16_vap);
420 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
421 goto badvalue;
422 td->td_planarconfig = (uint16_t)v;
423 break;
424 case TIFFTAG_XPOSITION:
425 td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
426 break;
427 case TIFFTAG_YPOSITION:
428 td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
429 break;
430 case TIFFTAG_RESOLUTIONUNIT:
431 v = (uint16_t)va_arg(ap, uint16_vap);
432 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
433 goto badvalue;
434 td->td_resolutionunit = (uint16_t)v;
435 break;
436 case TIFFTAG_PAGENUMBER:
437 td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap);
438 td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap);
439 break;
440 case TIFFTAG_HALFTONEHINTS:
441 td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap);
442 td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap);
443 break;
444 case TIFFTAG_COLORMAP:
445 if (td->td_bitspersample >= 32)
446 {
447 v = td->td_bitspersample;
448 goto badvalue;
449 }
450 v32 = 1U << td->td_bitspersample;
451 _TIFFsetShortArrayExt(tif, &td->td_colormap[0],
452 va_arg(ap, uint16_t *), v32);
453 _TIFFsetShortArrayExt(tif, &td->td_colormap[1],
454 va_arg(ap, uint16_t *), v32);
455 _TIFFsetShortArrayExt(tif, &td->td_colormap[2],
456 va_arg(ap, uint16_t *), v32);
457 break;
458 case TIFFTAG_EXTRASAMPLES:
459 if (!setExtraSamples(tif, ap, &v))
460 goto badvalue;
461 break;
462 case TIFFTAG_MATTEING:
463 td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0);
464 if (td->td_extrasamples)
465 {
466 uint16_t sv = EXTRASAMPLE_ASSOCALPHA;
467 _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1);
468 }
469 break;
470 case TIFFTAG_TILEWIDTH:
471 v32 = (uint32_t)va_arg(ap, uint32_t);
472 if (v32 % 16)
473 {
474 if (tif->tif_mode != O_RDONLY)
475 goto badvalue32;
476 TIFFWarningExtR(
477 tif, tif->tif_name,
478 "Nonstandard tile width %" PRIu32 ", convert file", v32);
479 }
480 td->td_tilewidth = v32;
481 tif->tif_flags |= TIFF_ISTILED;
482 break;
483 case TIFFTAG_TILELENGTH:
484 v32 = (uint32_t)va_arg(ap, uint32_t);
485 if (v32 % 16)
486 {
487 if (tif->tif_mode != O_RDONLY)
488 goto badvalue32;
489 TIFFWarningExtR(
490 tif, tif->tif_name,
491 "Nonstandard tile length %" PRIu32 ", convert file", v32);
492 }
493 td->td_tilelength = v32;
494 tif->tif_flags |= TIFF_ISTILED;
495 break;
496 case TIFFTAG_TILEDEPTH:
497 v32 = (uint32_t)va_arg(ap, uint32_t);
498 if (v32 == 0)
499 goto badvalue32;
500 td->td_tiledepth = v32;
501 break;
502 case TIFFTAG_DATATYPE:
503 v = (uint16_t)va_arg(ap, uint16_vap);
504 switch (v)
505 {
506 case DATATYPE_VOID:
507 v = SAMPLEFORMAT_VOID;
508 break;
509 case DATATYPE_INT:
510 v = SAMPLEFORMAT_INT;
511 break;
512 case DATATYPE_UINT:
513 v = SAMPLEFORMAT_UINT;
514 break;
515 case DATATYPE_IEEEFP:
516 v = SAMPLEFORMAT_IEEEFP;
517 break;
518 default:
519 goto badvalue;
520 }
521 td->td_sampleformat = (uint16_t)v;
522 break;
523 case TIFFTAG_SAMPLEFORMAT:
524 v = (uint16_t)va_arg(ap, uint16_vap);
525 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
526 goto badvalue;
527 td->td_sampleformat = (uint16_t)v;
528
529 /* Try to fix up the SWAB function for complex data. */
530 if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT &&
531 td->td_bitspersample == 32 &&
532 tif->tif_postdecode == _TIFFSwab32BitData)
533 tif->tif_postdecode = _TIFFSwab16BitData;
534 else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT ||
535 td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) &&
536 td->td_bitspersample == 64 &&
537 tif->tif_postdecode == _TIFFSwab64BitData)
538 tif->tif_postdecode = _TIFFSwab32BitData;
539 break;
540 case TIFFTAG_IMAGEDEPTH:
541 td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t);
542 break;
543 case TIFFTAG_SUBIFD:
544 if ((tif->tif_flags & TIFF_INSUBIFD) == 0)
545 {
546 td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap);
547 _TIFFsetLong8Array(tif, &td->td_subifd,
548 (uint64_t *)va_arg(ap, uint64_t *),
549 (uint32_t)td->td_nsubifd);
550 }
551 else
552 {
553 TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs",
554 tif->tif_name);
555 status = 0;
556 }
557 break;
558 case TIFFTAG_YCBCRPOSITIONING:
559 td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap);
560 break;
561 case TIFFTAG_YCBCRSUBSAMPLING:
562 td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap);
563 td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap);
564 break;
565 case TIFFTAG_TRANSFERFUNCTION:
566 {
567 uint32_t i;
568 uint32_t count;
569 if (td->td_bitspersample >= 32)
570 {
571 v = td->td_bitspersample;
572 goto badvalue;
573 }
574 count = 1U << td->td_bitspersample;
575 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
576 for (i = 0; i < v; i++)
577 _TIFFsetShortArrayExt(tif, &td->td_transferfunction[i],
578 va_arg(ap, uint16_t *), count);
579 break;
580 }
581 case TIFFTAG_REFERENCEBLACKWHITE:
582 /* XXX should check for null range */
583 _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite,
584 va_arg(ap, float *), 6);
585 break;
586 case TIFFTAG_INKNAMES:
587 {
588 v = (uint16_t)va_arg(ap, uint16_vap);
589 s = va_arg(ap, char *);
590 uint16_t ninksinstring;
591 ninksinstring = countInkNamesString(tif, v, s);
592 status = ninksinstring > 0;
593 if (ninksinstring > 0)
594 {
595 _TIFFsetNString(tif, &td->td_inknames, s, v);
596 td->td_inknameslen = (int)v;
597 /* Set NumberOfInks to the value ninksinstring */
598 if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
599 {
600 if (td->td_numberofinks != ninksinstring)
601 {
602 TIFFErrorExtR(
603 tif, module,
604 "Warning %s; Tag %s:\n Value %" PRIu16
605 " of NumberOfInks is different from the number of "
606 "inks %" PRIu16
607 ".\n -> NumberOfInks value adapted to %" PRIu16 "",
608 tif->tif_name, fip->field_name, td->td_numberofinks,
609 ninksinstring, ninksinstring);
610 td->td_numberofinks = ninksinstring;
611 }
612 }
613 else
614 {
615 td->td_numberofinks = ninksinstring;
616 TIFFSetFieldBit(tif, FIELD_NUMBEROFINKS);
617 }
618 if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
619 {
620 if (td->td_numberofinks != td->td_samplesperpixel)
621 {
622 TIFFErrorExtR(tif, module,
623 "Warning %s; Tag %s:\n Value %" PRIu16
624 " of NumberOfInks is different from the "
625 "SamplesPerPixel value %" PRIu16 "",
626 tif->tif_name, fip->field_name,
627 td->td_numberofinks,
628 td->td_samplesperpixel);
629 }
630 }
631 }
632 }
633 break;
634 case TIFFTAG_NUMBEROFINKS:
635 v = (uint16_t)va_arg(ap, uint16_vap);
636 /* If InkNames already set also NumberOfInks is set accordingly and
637 * should be equal */
638 if (TIFFFieldSet(tif, FIELD_INKNAMES))
639 {
640 if (v != td->td_numberofinks)
641 {
642 TIFFErrorExtR(
643 tif, module,
644 "Error %s; Tag %s:\n It is not possible to set the "
645 "value %" PRIu32
646 " for NumberOfInks\n which is different from the "
647 "number of inks in the InkNames tag (%" PRIu16 ")",
648 tif->tif_name, fip->field_name, v, td->td_numberofinks);
649 /* Do not set / overwrite number of inks already set by
650 * InkNames case accordingly. */
651 status = 0;
652 }
653 }
654 else
655 {
656 td->td_numberofinks = (uint16_t)v;
657 if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
658 {
659 if (td->td_numberofinks != td->td_samplesperpixel)
660 {
661 TIFFErrorExtR(tif, module,
662 "Warning %s; Tag %s:\n Value %" PRIu32
663 " of NumberOfInks is different from the "
664 "SamplesPerPixel value %" PRIu16 "",
665 tif->tif_name, fip->field_name, v,
666 td->td_samplesperpixel);
667 }
668 }
669 }
670 break;
671 case TIFFTAG_PERSAMPLE:
672 v = (uint16_t)va_arg(ap, uint16_vap);
673 if (v == PERSAMPLE_MULTI)
674 tif->tif_flags |= TIFF_PERSAMPLE;
675 else
676 tif->tif_flags &= ~TIFF_PERSAMPLE;
677 break;
678 default:
679 {
680 TIFFTagValue *tv;
681 int tv_size, iCustom;
682
683 /*
684 * This can happen if multiple images are open with different
685 * codecs which have private tags. The global tag information
686 * table may then have tags that are valid for one file but not
687 * the other. If the client tries to set a tag that is not valid
688 * for the image's codec then we'll arrive here. This
689 * happens, for example, when tiffcp is used to convert between
690 * compression schemes and codec-specific tags are blindly copied.
691 *
692 * This also happens when a FIELD_IGNORE tag is written.
693 */
694 if (fip->field_bit == FIELD_IGNORE)
695 {
696 TIFFErrorExtR(
697 tif, module,
698 "%s: Ignored %stag \"%s\" (not supported by libtiff)",
699 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
700 fip->field_name);
701 status = 0;
702 break;
703 }
704 if (fip->field_bit != FIELD_CUSTOM)
705 {
706 TIFFErrorExtR(
707 tif, module,
708 "%s: Invalid %stag \"%s\" (not supported by codec)",
709 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
710 fip->field_name);
711 status = 0;
712 break;
713 }
714
715 /*
716 * Find the existing entry for this custom value.
717 */
718 tv = NULL;
719 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++)
720 {
721 if (td->td_customValues[iCustom].info->field_tag == tag)
722 {
723 tv = td->td_customValues + iCustom;
724 if (tv->value != NULL)
725 {
726 _TIFFfreeExt(tif, tv->value);
727 tv->value = NULL;
728 }
729 break;
730 }
731 }
732
733 /*
734 * Grow the custom list if the entry was not found.
735 */
736 if (tv == NULL)
737 {
738 TIFFTagValue *new_customValues;
739
740 new_customValues = (TIFFTagValue *)_TIFFreallocExt(
741 tif, td->td_customValues,
742 (tmsize_t)(sizeof(TIFFTagValue) *
743 (size_t)(td->td_customValueCount + 1)));
744 if (!new_customValues)
745 {
746 TIFFErrorExtR(tif, module,
747 "%s: Failed to allocate space for list of "
748 "custom values",
749 tif->tif_name);
750 status = 0;
751 goto end;
752 }
753
754 td->td_customValueCount++;
755 td->td_customValues = new_customValues;
756
757 tv = td->td_customValues + (td->td_customValueCount - 1);
758 tv->info = fip;
759 tv->value = NULL;
760 tv->count = 0;
761 }
762
763 /*
764 * Set custom value ... save a copy of the custom tag value.
765 */
766 /*--: Rational2Double: For Rationals evaluate "set_get_field_type"
767 * to determine internal storage size. */
768 tv_size = TIFFFieldSetGetSize(fip);
769 if (tv_size == 0)
770 {
771 status = 0;
772 TIFFErrorExtR(tif, module, "%s: Bad field type %u for \"%s\"",
773 tif->tif_name, fip->field_type, fip->field_name);
774 goto end;
775 }
776
777 if (fip->field_type == TIFF_ASCII)
778 {
779 uint32_t ma;
780 const char *mb;
781 if (fip->field_passcount)
782 {
783 assert(fip->field_writecount == TIFF_VARIABLE2);
784 ma = (uint32_t)va_arg(ap, uint32_t);
785 mb = (const char *)va_arg(ap, const char *);
786 }
787 else
788 {
789 mb = (const char *)va_arg(ap, const char *);
790 size_t len = strlen(mb) + 1;
791 if (len >= 0x80000000U)
792 {
793 status = 0;
794 TIFFErrorExtR(tif, module,
795 "%s: Too long string value for \"%s\". "
796 "Maximum supported is 2147483647 bytes",
797 tif->tif_name, fip->field_name);
798 goto end;
799 }
800 ma = (uint32_t)len;
801 }
802 tv->count = (int)ma;
803 setByteArray(tif, &tv->value, mb, ma, 1);
804 }
805 else
806 {
807 if (fip->field_passcount)
808 {
809 if (fip->field_writecount == TIFF_VARIABLE2)
810 tv->count = (int)va_arg(ap, uint32_t);
811 else
812 tv->count = va_arg(ap, int);
813 }
814 else if (fip->field_writecount == TIFF_VARIABLE ||
815 fip->field_writecount == TIFF_VARIABLE2)
816 tv->count = 1;
817 else if (fip->field_writecount == TIFF_SPP)
818 tv->count = td->td_samplesperpixel;
819 else
820 tv->count = fip->field_writecount;
821
822 if (tv->count == 0)
823 {
824 TIFFWarningExtR(tif, module,
825 "%s: Null count for \"%s\" (type "
826 "%u, writecount %d, passcount %d)",
827 tif->tif_name, fip->field_name,
828 fip->field_type, fip->field_writecount,
829 fip->field_passcount);
830 break;
831 }
832
833 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
834 "custom tag binary object");
835 if (!tv->value)
836 {
837 status = 0;
838 goto end;
839 }
840
841 if (fip->field_tag == TIFFTAG_DOTRANGE &&
842 strcmp(fip->field_name, "DotRange") == 0)
843 {
844 /* TODO: This is an evil exception and should not have been
845 handled this way ... likely best if we move it into
846 the directory structure with an explicit field in
847 libtiff 4.1 and assign it a FIELD_ value */
848 uint16_t v2[2];
849 v2[0] = (uint16_t)va_arg(ap, int);
850 v2[1] = (uint16_t)va_arg(ap, int);
851 _TIFFmemcpy(tv->value, &v2, 4);
852 }
853
854 else if (fip->field_passcount ||
855 fip->field_writecount == TIFF_VARIABLE ||
856 fip->field_writecount == TIFF_VARIABLE2 ||
857 fip->field_writecount == TIFF_SPP || tv->count > 1)
858 {
859 /*--: Rational2Double: For Rationals tv_size is set above to
860 * 4 or 8 according to fip->set_get_field_type! */
861 _TIFFmemcpy(tv->value, va_arg(ap, void *),
862 tv->count * tv_size);
863 /* Test here for too big values for LONG8, IFD8, SLONG8 in
864 * ClassicTIFF and delete custom field from custom list */
865 if (!(tif->tif_flags & TIFF_BIGTIFF))
866 {
867 if (tv->info->field_type == TIFF_LONG8 ||
868 tv->info->field_type == TIFF_IFD8)
869 {
870 uint64_t *pui64 = (uint64_t *)tv->value;
871 for (int i = 0; i < tv->count; i++)
872 {
873 if (pui64[i] > 0xffffffffu)
874 {
875 TIFFErrorExtR(
876 tif, module,
877 "%s: Bad %s value %" PRIu64
878 " at %d. array position for \"%s\" tag "
879 "%u in ClassicTIFF. Tag won't be "
880 "written to file",
881 tif->tif_name,
882 (tv->info->field_type == TIFF_LONG8
883 ? "LONG8"
884 : "IFD8"),
885 pui64[i], i, fip->field_name, tag);
886 goto badvalueifd8long8;
887 }
888 }
889 }
890 else if (tv->info->field_type == TIFF_SLONG8)
891 {
892 int64_t *pi64 = (int64_t *)tv->value;
893 for (int i = 0; i < tv->count; i++)
894 {
895 if (pi64[i] > 2147483647 ||
896 pi64[i] < (-2147483647 - 1))
897 {
898 TIFFErrorExtR(
899 tif, module,
900 "%s: Bad SLONG8 value %" PRIi64
901 " at %d. array position for \"%s\" tag "
902 "%u in ClassicTIFF. Tag won't be "
903 "written to file",
904 tif->tif_name, pi64[i], i,
905 fip->field_name, tag);
906 goto badvalueifd8long8;
907 }
908 }
909 }
910 }
911 }
912 else
913 {
914 char *val = (char *)tv->value;
915 assert(tv->count == 1);
916
917 switch (fip->field_type)
918 {
919 case TIFF_BYTE:
920 case TIFF_UNDEFINED:
921 {
922 uint8_t v2 = (uint8_t)va_arg(ap, int);
923 _TIFFmemcpy(val, &v2, tv_size);
924 }
925 break;
926 case TIFF_SBYTE:
927 {
928 int8_t v2 = (int8_t)va_arg(ap, int);
929 _TIFFmemcpy(val, &v2, tv_size);
930 }
931 break;
932 case TIFF_SHORT:
933 {
934 uint16_t v2 = (uint16_t)va_arg(ap, int);
935 _TIFFmemcpy(val, &v2, tv_size);
936 }
937 break;
938 case TIFF_SSHORT:
939 {
940 int16_t v2 = (int16_t)va_arg(ap, int);
941 _TIFFmemcpy(val, &v2, tv_size);
942 }
943 break;
944 case TIFF_LONG:
945 case TIFF_IFD:
946 {
947 uint32_t v2 = va_arg(ap, uint32_t);
948 _TIFFmemcpy(val, &v2, tv_size);
949 }
950 break;
951 case TIFF_SLONG:
952 {
953 int32_t v2 = va_arg(ap, int32_t);
954 _TIFFmemcpy(val, &v2, tv_size);
955 }
956 break;
957 case TIFF_LONG8:
958 case TIFF_IFD8:
959 {
960 uint64_t v2 = va_arg(ap, uint64_t);
961 _TIFFmemcpy(val, &v2, tv_size);
962 /* Test here for too big values for ClassicTIFF and
963 * delete custom field from custom list */
964 if (!(tif->tif_flags & TIFF_BIGTIFF) &&
965 (v2 > 0xffffffffu))
966 {
967 TIFFErrorExtR(
968 tif, module,
969 "%s: Bad LONG8 or IFD8 value %" PRIu64
970 " for \"%s\" tag %u in ClassicTIFF. Tag "
971 "won't be written to file",
972 tif->tif_name, v2, fip->field_name, tag);
973 goto badvalueifd8long8;
974 }
975 }
976 break;
977 case TIFF_SLONG8:
978 {
979 int64_t v2 = va_arg(ap, int64_t);
980 _TIFFmemcpy(val, &v2, tv_size);
981 /* Test here for too big values for ClassicTIFF and
982 * delete custom field from custom list */
983 if (!(tif->tif_flags & TIFF_BIGTIFF) &&
984 ((v2 > 2147483647) || (v2 < (-2147483647 - 1))))
985 {
986 TIFFErrorExtR(
987 tif, module,
988 "%s: Bad SLONG8 value %" PRIi64
989 " for \"%s\" tag %u in ClassicTIFF. Tag "
990 "won't be written to file",
991 tif->tif_name, v2, fip->field_name, tag);
992 goto badvalueifd8long8;
993 }
994 }
995 break;
996 case TIFF_RATIONAL:
997 case TIFF_SRATIONAL:
998 /*-- Rational2Double: For Rationals tv_size is set
999 * above to 4 or 8 according to
1000 * fip->set_get_field_type!
1001 */
1002 {
1003 if (tv_size == 8)
1004 {
1005 double v2 = va_arg(ap, double);
1006 _TIFFmemcpy(val, &v2, tv_size);
1007 }
1008 else
1009 {
1010 /*-- default should be tv_size == 4 */
1011 float v3 = (float)va_arg(ap, double);
1012 _TIFFmemcpy(val, &v3, tv_size);
1013 /*-- ToDo: After Testing, this should be
1014 * removed and tv_size==4 should be set as
1015 * default. */
1016 if (tv_size != 4)
1017 {
1018 TIFFErrorExtR(tif, module,
1019 "Rational2Double: "
1020 ".set_get_field_type "
1021 "in not 4 but %d",
1022 tv_size);
1023 }
1024 }
1025 }
1026 break;
1027 case TIFF_FLOAT:
1028 {
1029 float v2 =
1030 _TIFFClampDoubleToFloat(va_arg(ap, double));
1031 _TIFFmemcpy(val, &v2, tv_size);
1032 }
1033 break;
1034 case TIFF_DOUBLE:
1035 {
1036 double v2 = va_arg(ap, double);
1037 _TIFFmemcpy(val, &v2, tv_size);
1038 }
1039 break;
1040 case TIFF_NOTYPE:
1041 case TIFF_ASCII:
1042 default:
1043 _TIFFmemset(val, 0, tv_size);
1044 status = 0;
1045 break;
1046 }
1047 }
1048 }
1049 }
1050 }
1051 if (status)
1052 {
1053 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1054 if (fip2)
1055 TIFFSetFieldBit(tif, fip2->field_bit);
1056 tif->tif_flags |= TIFF_DIRTYDIRECT;
1057 }
1058
1059 end:
1060 va_end(ap);
1061 return (status);
1062 badvalue:
1063 {
1064 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1065 TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
1066 tif->tif_name, v, fip2 ? fip2->field_name : "Unknown");
1067 va_end(ap);
1068 }
1069 return (0);
1070 badvalue32:
1071 {
1072 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1073 TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
1074 tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown");
1075 va_end(ap);
1076 }
1077 return (0);
1078 badvaluedouble:
1079 {
1080 const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
1081 TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name,
1082 dblval, fip2 ? fip2->field_name : "Unknown");
1083 va_end(ap);
1084 }
1085 return (0);
1086 badvalueifd8long8:
1087 {
1088 /* Error message issued already above. */
1089 TIFFTagValue *tv2 = NULL;
1090 int iCustom2, iC2;
1091 /* Find the existing entry for this custom value. */
1092 for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++)
1093 {
1094 if (td->td_customValues[iCustom2].info->field_tag == tag)
1095 {
1096 tv2 = td->td_customValues + (iCustom2);
1097 break;
1098 }
1099 }
1100 if (tv2 != NULL)
1101 {
1102 /* Remove custom field from custom list */
1103 if (tv2->value != NULL)
1104 {
1105 _TIFFfreeExt(tif, tv2->value);
1106 tv2->value = NULL;
1107 }
1108 /* Shorten list and close gap in customValues list.
1109 * Re-allocation of td_customValues not necessary here. */
1110 td->td_customValueCount--;
1111 for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++)
1112 {
1113 td->td_customValues[iC2] = td->td_customValues[iC2 + 1];
1114 }
1115 }
1116 else
1117 {
1118 assert(0);
1119 }
1120 va_end(ap);
1121 }
1122 return (0);
1123 } /*-- _TIFFVSetField() --*/
1124
1125 /*
1126 * Return 1/0 according to whether or not
1127 * it is permissible to set the tag's value.
1128 * Note that we allow ImageLength to be changed
1129 * so that we can append and extend to images.
1130 * Any other tag may not be altered once writing
1131 * has commenced, unless its value has no effect
1132 * on the format of the data that is written.
1133 */
1134 static int OkToChangeTag(TIFF *tif, uint32_t tag)
1135 {
1136 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
1137 if (!fip)
1138 { /* unknown tag */
1139 TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32,
1140 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
1141 return (0);
1142 }
1143 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
1144 !fip->field_oktochange)
1145 {
1146 /*
1147 * Consult info table to see if tag can be changed
1148 * after we've started writing. We only allow changes
1149 * to those tags that don't/shouldn't affect the
1150 * compression and/or format of the data.
1151 */
1152 TIFFErrorExtR(tif, "TIFFSetField",
1153 "%s: Cannot modify tag \"%s\" while writing",
1154 tif->tif_name, fip->field_name);
1155 return (0);
1156 }
1157 return (1);
1158 }
1159
1160 /*
1161 * Record the value of a field in the
1162 * internal directory structure. The
1163 * field will be written to the file
1164 * when/if the directory structure is
1165 * updated.
1166 */
1167 int TIFFSetField(TIFF *tif, uint32_t tag, ...)
1168 {
1169 va_list ap;
1170 int status;
1171
1172 va_start(ap, tag);
1173 status = TIFFVSetField(tif, tag, ap);
1174 va_end(ap);
1175 return (status);
1176 }
1177
1178 /*
1179 * Clear the contents of the field in the internal structure.
1180 */
1181 int TIFFUnsetField(TIFF *tif, uint32_t tag)
1182 {
1183 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
1184 TIFFDirectory *td = &tif->tif_dir;
1185
1186 if (!fip)
1187 return 0;
1188
1189 if (fip->field_bit != FIELD_CUSTOM)
1190 TIFFClrFieldBit(tif, fip->field_bit);
1191 else
1192 {
1193 TIFFTagValue *tv = NULL;
1194 int i;
1195
1196 for (i = 0; i < td->td_customValueCount; i++)
1197 {
1198
1199 tv = td->td_customValues + i;
1200 if (tv->info->field_tag == tag)
1201 break;
1202 }
1203
1204 if (i < td->td_customValueCount)
1205 {
1206 _TIFFfreeExt(tif, tv->value);
1207 for (; i < td->td_customValueCount - 1; i++)
1208 {
1209 td->td_customValues[i] = td->td_customValues[i + 1];
1210 }
1211 td->td_customValueCount--;
1212 }
1213 }
1214
1215 tif->tif_flags |= TIFF_DIRTYDIRECT;
1216
1217 return (1);
1218 }
1219
1220 /*
1221 * Like TIFFSetField, but taking a varargs
1222 * parameter list. This routine is useful
1223 * for building higher-level interfaces on
1224 * top of the library.
1225 */
1226 int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
1227 {
1228 return OkToChangeTag(tif, tag)
1229 ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap)
1230 : 0;
1231 }
1232
1233 static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
1234 {
1235 TIFFDirectory *td = &tif->tif_dir;
1236 int ret_val = 1;
1237 uint32_t standard_tag = tag;
1238 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
1239 if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */
1240 return 0;
1241
1242 /*
1243 * We want to force the custom code to be used for custom
1244 * fields even if the tag happens to match a well known
1245 * one - important for reinterpreted handling of standard
1246 * tag values in custom directories (i.e. EXIF)
1247 */
1248 if (fip->field_bit == FIELD_CUSTOM)
1249 {
1250 standard_tag = 0;
1251 }
1252
1253 switch (standard_tag)
1254 {
1255 case TIFFTAG_SUBFILETYPE:
1256 *va_arg(ap, uint32_t *) = td->td_subfiletype;
1257 break;
1258 case TIFFTAG_IMAGEWIDTH:
1259 *va_arg(ap, uint32_t *) = td->td_imagewidth;
1260 break;
1261 case TIFFTAG_IMAGELENGTH:
1262 *va_arg(ap, uint32_t *) = td->td_imagelength;
1263 break;
1264 case TIFFTAG_BITSPERSAMPLE:
1265 *va_arg(ap, uint16_t *) = td->td_bitspersample;
1266 break;
1267 case TIFFTAG_COMPRESSION:
1268 *va_arg(ap, uint16_t *) = td->td_compression;
1269 break;
1270 case TIFFTAG_PHOTOMETRIC:
1271 *va_arg(ap, uint16_t *) = td->td_photometric;
1272 break;
1273 case TIFFTAG_THRESHHOLDING:
1274 *va_arg(ap, uint16_t *) = td->td_threshholding;
1275 break;
1276 case TIFFTAG_FILLORDER:
1277 *va_arg(ap, uint16_t *) = td->td_fillorder;
1278 break;
1279 case TIFFTAG_ORIENTATION:
1280 *va_arg(ap, uint16_t *) = td->td_orientation;
1281 break;
1282 case TIFFTAG_SAMPLESPERPIXEL:
1283 *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
1284 break;
1285 case TIFFTAG_ROWSPERSTRIP:
1286 *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
1287 break;
1288 case TIFFTAG_MINSAMPLEVALUE:
1289 *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
1290 break;
1291 case TIFFTAG_MAXSAMPLEVALUE:
1292 *va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
1293 break;
1294 case TIFFTAG_SMINSAMPLEVALUE:
1295 if (tif->tif_flags & TIFF_PERSAMPLE)
1296 *va_arg(ap, double **) = td->td_sminsamplevalue;
1297 else
1298 {
1299 /* libtiff historically treats this as a single value. */
1300 uint16_t i;
1301 double v = td->td_sminsamplevalue[0];
1302 for (i = 1; i < td->td_samplesperpixel; ++i)
1303 if (td->td_sminsamplevalue[i] < v)
1304 v = td->td_sminsamplevalue[i];
1305 *va_arg(ap, double *) = v;
1306 }
1307 break;
1308 case TIFFTAG_SMAXSAMPLEVALUE:
1309 if (tif->tif_flags & TIFF_PERSAMPLE)
1310 *va_arg(ap, double **) = td->td_smaxsamplevalue;
1311 else
1312 {
1313 /* libtiff historically treats this as a single value. */
1314 uint16_t i;
1315 double v = td->td_smaxsamplevalue[0];
1316 for (i = 1; i < td->td_samplesperpixel; ++i)
1317 if (td->td_smaxsamplevalue[i] > v)
1318 v = td->td_smaxsamplevalue[i];
1319 *va_arg(ap, double *) = v;
1320 }
1321 break;
1322 case TIFFTAG_XRESOLUTION:
1323 *va_arg(ap, float *) = td->td_xresolution;
1324 break;
1325 case TIFFTAG_YRESOLUTION:
1326 *va_arg(ap, float *) = td->td_yresolution;
1327 break;
1328 case TIFFTAG_PLANARCONFIG:
1329 *va_arg(ap, uint16_t *) = td->td_planarconfig;
1330 break;
1331 case TIFFTAG_XPOSITION:
1332 *va_arg(ap, float *) = td->td_xposition;
1333 break;
1334 case TIFFTAG_YPOSITION:
1335 *va_arg(ap, float *) = td->td_yposition;
1336 break;
1337 case TIFFTAG_RESOLUTIONUNIT:
1338 *va_arg(ap, uint16_t *) = td->td_resolutionunit;
1339 break;
1340 case TIFFTAG_PAGENUMBER:
1341 *va_arg(ap, uint16_t *) = td->td_pagenumber[0];
1342 *va_arg(ap, uint16_t *) = td->td_pagenumber[1];
1343 break;
1344 case TIFFTAG_HALFTONEHINTS:
1345 *va_arg(ap, uint16_t *) = td->td_halftonehints[0];
1346 *va_arg(ap, uint16_t *) = td->td_halftonehints[1];
1347 break;
1348 case TIFFTAG_COLORMAP:
1349 *va_arg(ap, const uint16_t **) = td->td_colormap[0];
1350 *va_arg(ap, const uint16_t **) = td->td_colormap[1];
1351 *va_arg(ap, const uint16_t **) = td->td_colormap[2];
1352 break;
1353 case TIFFTAG_STRIPOFFSETS:
1354 case TIFFTAG_TILEOFFSETS:
1355 _TIFFFillStriles(tif);
1356 *va_arg(ap, const uint64_t **) = td->td_stripoffset_p;
1357 if (td->td_stripoffset_p == NULL)
1358 ret_val = 0;
1359 break;
1360 case TIFFTAG_STRIPBYTECOUNTS:
1361 case TIFFTAG_TILEBYTECOUNTS:
1362 _TIFFFillStriles(tif);
1363 *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p;
1364 if (td->td_stripbytecount_p == NULL)
1365 ret_val = 0;
1366 break;
1367 case TIFFTAG_MATTEING:
1368 *va_arg(ap, uint16_t *) =
1369 (td->td_extrasamples == 1 && td->td_sampleinfo &&
1370 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
1371 break;
1372 case TIFFTAG_EXTRASAMPLES:
1373 *va_arg(ap, uint16_t *) = td->td_extrasamples;
1374 *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
1375 break;
1376 case TIFFTAG_TILEWIDTH:
1377 *va_arg(ap, uint32_t *) = td->td_tilewidth;
1378 break;
1379 case TIFFTAG_TILELENGTH:
1380 *va_arg(ap, uint32_t *) = td->td_tilelength;
1381 break;
1382 case TIFFTAG_TILEDEPTH:
1383 *va_arg(ap, uint32_t *) = td->td_tiledepth;
1384 break;
1385 case TIFFTAG_DATATYPE:
1386 switch (td->td_sampleformat)
1387 {
1388 case SAMPLEFORMAT_UINT:
1389 *va_arg(ap, uint16_t *) = DATATYPE_UINT;
1390 break;
1391 case SAMPLEFORMAT_INT:
1392 *va_arg(ap, uint16_t *) = DATATYPE_INT;
1393 break;
1394 case SAMPLEFORMAT_IEEEFP:
1395 *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP;
1396 break;
1397 case SAMPLEFORMAT_VOID:
1398 *va_arg(ap, uint16_t *) = DATATYPE_VOID;
1399 break;
1400 default:
1401 break;
1402 }
1403 break;
1404 case TIFFTAG_SAMPLEFORMAT:
1405 *va_arg(ap, uint16_t *) = td->td_sampleformat;
1406 break;
1407 case TIFFTAG_IMAGEDEPTH:
1408 *va_arg(ap, uint32_t *) = td->td_imagedepth;
1409 break;
1410 case TIFFTAG_SUBIFD:
1411 *va_arg(ap, uint16_t *) = td->td_nsubifd;
1412 *va_arg(ap, const uint64_t **) = td->td_subifd;
1413 break;
1414 case TIFFTAG_YCBCRPOSITIONING:
1415 *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
1416 break;
1417 case TIFFTAG_YCBCRSUBSAMPLING:
1418 *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
1419 *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
1420 break;
1421 case TIFFTAG_TRANSFERFUNCTION:
1422 *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
1423 if (td->td_samplesperpixel - td->td_extrasamples > 1)
1424 {
1425 *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
1426 *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
1427 }
1428 else
1429 {
1430 *va_arg(ap, const uint16_t **) = NULL;
1431 *va_arg(ap, const uint16_t **) = NULL;
1432 }
1433 break;
1434 case TIFFTAG_REFERENCEBLACKWHITE:
1435 *va_arg(ap, const float **) = td->td_refblackwhite;
1436 break;
1437 case TIFFTAG_INKNAMES:
1438 *va_arg(ap, const char **) = td->td_inknames;
1439 break;
1440 case TIFFTAG_NUMBEROFINKS:
1441 *va_arg(ap, uint16_t *) = td->td_numberofinks;
1442 break;
1443 default:
1444 {
1445 int i;
1446
1447 /*
1448 * This can happen if multiple images are open
1449 * with different codecs which have private
1450 * tags. The global tag information table may
1451 * then have tags that are valid for one file
1452 * but not the other. If the client tries to
1453 * get a tag that is not valid for the image's
1454 * codec then we'll arrive here.
1455 */
1456 if (fip->field_bit != FIELD_CUSTOM)
1457 {
1458 TIFFErrorExtR(tif, "_TIFFVGetField",
1459 "%s: Invalid %stag \"%s\" "
1460 "(not supported by codec)",
1461 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
1462 fip->field_name);
1463 ret_val = 0;
1464 break;
1465 }
1466
1467 /*
1468 * Do we have a custom value?
1469 */
1470 ret_val = 0;
1471 for (i = 0; i < td->td_customValueCount; i++)
1472 {
1473 TIFFTagValue *tv = td->td_customValues + i;
1474
1475 if (tv->info->field_tag != tag)
1476 continue;
1477
1478 if (fip->field_passcount)
1479 {
1480 if (fip->field_readcount == TIFF_VARIABLE2)
1481 *va_arg(ap, uint32_t *) = (uint32_t)tv->count;
1482 else /* Assume TIFF_VARIABLE */
1483 *va_arg(ap, uint16_t *) = (uint16_t)tv->count;
1484 *va_arg(ap, const void **) = tv->value;
1485 ret_val = 1;
1486 }
1487 else if (fip->field_tag == TIFFTAG_DOTRANGE &&
1488 strcmp(fip->field_name, "DotRange") == 0)
1489 {
1490 /* TODO: This is an evil exception and should not have been
1491 handled this way ... likely best if we move it into
1492 the directory structure with an explicit field in
1493 libtiff 4.1 and assign it a FIELD_ value */
1494 *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0];
1495 *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1];
1496 ret_val = 1;
1497 }
1498 else
1499 {
1500 if (fip->field_type == TIFF_ASCII ||
1501 fip->field_readcount == TIFF_VARIABLE ||
1502 fip->field_readcount == TIFF_VARIABLE2 ||
1503 fip->field_readcount == TIFF_SPP || tv->count > 1)
1504 {
1505 *va_arg(ap, void **) = tv->value;
1506 ret_val = 1;
1507 }
1508 else
1509 {
1510 char *val = (char *)tv->value;
1511 assert(tv->count == 1);
1512 switch (fip->field_type)
1513 {
1514 case TIFF_BYTE:
1515 case TIFF_UNDEFINED:
1516 *va_arg(ap, uint8_t *) = *(uint8_t *)val;
1517 ret_val = 1;
1518 break;
1519 case TIFF_SBYTE:
1520 *va_arg(ap, int8_t *) = *(int8_t *)val;
1521 ret_val = 1;
1522 break;
1523 case TIFF_SHORT:
1524 *va_arg(ap, uint16_t *) = *(uint16_t *)val;
1525 ret_val = 1;
1526 break;
1527 case TIFF_SSHORT:
1528 *va_arg(ap, int16_t *) = *(int16_t *)val;
1529 ret_val = 1;
1530 break;
1531 case TIFF_LONG:
1532 case TIFF_IFD:
1533 *va_arg(ap, uint32_t *) = *(uint32_t *)val;
1534 ret_val = 1;
1535 break;
1536 case TIFF_SLONG:
1537 *va_arg(ap, int32_t *) = *(int32_t *)val;
1538 ret_val = 1;
1539 break;
1540 case TIFF_LONG8:
1541 case TIFF_IFD8:
1542 *va_arg(ap, uint64_t *) = *(uint64_t *)val;
1543 ret_val = 1;
1544 break;
1545 case TIFF_SLONG8:
1546 *va_arg(ap, int64_t *) = *(int64_t *)val;
1547 ret_val = 1;
1548 break;
1549 case TIFF_RATIONAL:
1550 case TIFF_SRATIONAL:
1551 {
1552 /*-- Rational2Double: For Rationals evaluate
1553 * "set_get_field_type" to determine internal
1554 * storage size and return value size. */
1555 int tv_size = TIFFFieldSetGetSize(fip);
1556 if (tv_size == 8)
1557 {
1558 *va_arg(ap, double *) = *(double *)val;
1559 ret_val = 1;
1560 }
1561 else
1562 {
1563 /*-- default should be tv_size == 4 */
1564 *va_arg(ap, float *) = *(float *)val;
1565 ret_val = 1;
1566 /*-- ToDo: After Testing, this should be
1567 * removed and tv_size==4 should be set as
1568 * default. */
1569 if (tv_size != 4)
1570 {
1571 TIFFErrorExtR(tif, "_TIFFVGetField",
1572 "Rational2Double: "
1573 ".set_get_field_type "
1574 "in not 4 but %d",
1575 tv_size);
1576 }
1577 }
1578 }
1579 break;
1580 case TIFF_FLOAT:
1581 *va_arg(ap, float *) = *(float *)val;
1582 ret_val = 1;
1583 break;
1584 case TIFF_DOUBLE:
1585 *va_arg(ap, double *) = *(double *)val;
1586 ret_val = 1;
1587 break;
1588 case TIFF_NOTYPE:
1589 case TIFF_ASCII:
1590 default:
1591 ret_val = 0;
1592 break;
1593 }
1594 }
1595 }
1596 break;
1597 }
1598 }
1599 }
1600 return (ret_val);
1601 }
1602
1603 /*
1604 * Return the value of a field in the
1605 * internal directory structure.
1606 */
1607 int TIFFGetField(TIFF *tif, uint32_t tag, ...)
1608 {
1609 int status;
1610 va_list ap;
1611
1612 va_start(ap, tag);
1613 status = TIFFVGetField(tif, tag, ap);
1614 va_end(ap);
1615 return (status);
1616 }
1617
1618 /*
1619 * Like TIFFGetField, but taking a varargs
1620 * parameter list. This routine is useful
1621 * for building higher-level interfaces on
1622 * top of the library.
1623 */
1624 int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
1625 {
1626 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
1627 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit))
1628 ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap)
1629 : 0);
1630 }
1631
1632 #define CleanupField(member) \
1633 { \
1634 if (td->member) \
1635 { \
1636 _TIFFfreeExt(tif, td->member); \
1637 td->member = 0; \
1638 } \
1639 }
1640
1641 /*
1642 * Reset tif->tif_dir structure to zero and
1643 * initialize some IFD strile counter and index parameters.
1644 */
1645 void _TIFFResetTifDirAndInitStrileCounters(TIFFDirectory *td)
1646 {
1647 _TIFFmemset(td, 0, sizeof(*td));
1648 td->td_curstrip = NOSTRIP; /* invalid strip = NOSTRIP */
1649 td->td_row = (uint32_t)-1; /* read/write pre-increment */
1650 td->td_col = (uint32_t)-1; /* read/write pre-increment */
1651 td->td_scanlinesize = 0; /* initialize to zero */
1652 td->td_curtile = NOTILE; /* invalid tile = NOTILE */
1653 td->td_tilesize = (tmsize_t)-1; /* invalidate tilezize */
1654 }
1655
1656 /*
1657 * Release storage associated with a directory.
1658 */
1659 void TIFFFreeDirectory(TIFF *tif)
1660 {
1661 TIFFDirectory *td = &tif->tif_dir;
1662 int i;
1663
1664 (*tif->tif_cleanup)(tif);
1665 _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset));
1666 CleanupField(td_sminsamplevalue);
1667 CleanupField(td_smaxsamplevalue);
1668 CleanupField(td_colormap[0]);
1669 CleanupField(td_colormap[1]);
1670 CleanupField(td_colormap[2]);
1671 CleanupField(td_sampleinfo);
1672 CleanupField(td_subifd);
1673 CleanupField(td_inknames);
1674 CleanupField(td_refblackwhite);
1675 CleanupField(td_transferfunction[0]);
1676 CleanupField(td_transferfunction[1]);
1677 CleanupField(td_transferfunction[2]);
1678 CleanupField(td_stripoffset_p);
1679 CleanupField(td_stripbytecount_p);
1680 td->td_stripoffsetbyteallocsize = 0;
1681 TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
1682 TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
1683
1684 /* Cleanup custom tag values */
1685 for (i = 0; i < td->td_customValueCount; i++)
1686 {
1687 if (td->td_customValues[i].value)
1688 _TIFFfreeExt(tif, td->td_customValues[i].value);
1689 }
1690
1691 td->td_customValueCount = 0;
1692 CleanupField(td_customValues);
1693
1694 _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
1695 _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1696
1697 /* Reset some internal parameters for IFD data size checking. */
1698 tif->tif_dir.td_dirdatasize_read = 0;
1699 tif->tif_dir.td_dirdatasize_write = 0;
1700 if (tif->tif_dir.td_dirdatasize_offsets != NULL)
1701 {
1702 _TIFFfreeExt(tif, tif->tif_dir.td_dirdatasize_offsets);
1703 tif->tif_dir.td_dirdatasize_offsets = NULL;
1704 tif->tif_dir.td_dirdatasize_Noffsets = 0;
1705 }
1706 tif->tif_dir.td_iswrittentofile = FALSE;
1707 /* Note: tif->tif_dir structure is set to zero in TIFFDefaultDirectory() */
1708 }
1709 #undef CleanupField
1710
1711 /*
1712 * Client Tag extension support (from Niles Ritter).
1713 */
1714 static TIFFExtendProc _TIFFextender = (TIFFExtendProc)NULL;
1715
1716 TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender)
1717 {
1718 TIFFExtendProc prev = _TIFFextender;
1719 _TIFFextender = extender;
1720 return (prev);
1721 }
1722
1723 /*
1724 * Setup for a new directory. Should we automatically call
1725 * TIFFWriteDirectory() if the current one is dirty?
1726 *
1727 * The newly created directory will not exist on the file till
1728 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1729 */
1730 int TIFFCreateDirectory(TIFF *tif)
1731 {
1732 /* Free previously allocated memory and setup default values. */
1733 TIFFFreeDirectory(tif);
1734 TIFFDefaultDirectory(tif);
1735 tif->tif_diroff = 0;
1736 tif->tif_nextdiroff = 0;
1737 tif->tif_curoff = 0;
1738 tif->tif_dir.td_iswrittentofile = FALSE;
1739 return 0;
1740 }
1741
1742 int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray)
1743 {
1744 /* Free previously allocated memory and setup default values. */
1745 TIFFFreeDirectory(tif);
1746 TIFFDefaultDirectory(tif);
1747
1748 /*
1749 * Reset the field definitions to match the application provided list.
1750 * Hopefully TIFFDefaultDirectory() won't have done anything irreversible
1751 * based on it's assumption this is an image directory.
1752 */
1753 _TIFFSetupFields(tif, infoarray);
1754
1755 tif->tif_diroff = 0;
1756 tif->tif_nextdiroff = 0;
1757 tif->tif_curoff = 0;
1758 /* invalidate directory index */
1759 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
1760 /* invalidate IFD loop lists */
1761 _TIFFCleanupIFDOffsetAndNumberMaps(tif);
1762 /* To be able to return from SubIFD or custom-IFD to main-IFD */
1763 tif->tif_setdirectory_force_absolute = TRUE;
1764 return 0;
1765 }
1766
1767 int TIFFCreateEXIFDirectory(TIFF *tif)
1768 {
1769 const TIFFFieldArray *exifFieldArray;
1770 exifFieldArray = _TIFFGetExifFields();
1771 return TIFFCreateCustomDirectory(tif, exifFieldArray);
1772 }
1773
1774 /*
1775 * Creates the EXIF GPS custom directory
1776 */
1777 int TIFFCreateGPSDirectory(TIFF *tif)
1778 {
1779 const TIFFFieldArray *gpsFieldArray;
1780 gpsFieldArray = _TIFFGetGpsFields();
1781 return TIFFCreateCustomDirectory(tif, gpsFieldArray);
1782 }
1783
1784 /*
1785 * Setup a default directory structure.
1786 */
1787 int TIFFDefaultDirectory(TIFF *tif)
1788 {
1789 TIFFDirectory *td = &tif->tif_dir;
1790 const TIFFFieldArray *tiffFieldArray;
1791
1792 tiffFieldArray = _TIFFGetFields();
1793 _TIFFSetupFields(tif, tiffFieldArray);
1794 /* Reset tif->tif_dir structure to zero and
1795 * initialize some IFD strile counter and index parameters. */
1796 _TIFFResetTifDirAndInitStrileCounters(td);
1797 td->td_fillorder = FILLORDER_MSB2LSB;
1798 td->td_bitspersample = 1;
1799 td->td_threshholding = THRESHHOLD_BILEVEL;
1800 td->td_orientation = ORIENTATION_TOPLEFT;
1801 td->td_samplesperpixel = 1;
1802 td->td_rowsperstrip = (uint32_t)-1;
1803 td->td_tilewidth = 0;
1804 td->td_tilelength = 0;
1805 td->td_tiledepth = 1;
1806 #ifdef STRIPBYTECOUNTSORTED_UNUSED
1807 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
1808 #endif
1809 td->td_resolutionunit = RESUNIT_INCH;
1810 td->td_sampleformat = SAMPLEFORMAT_UINT;
1811 td->td_imagedepth = 1;
1812 td->td_ycbcrsubsampling[0] = 2;
1813 td->td_ycbcrsubsampling[1] = 2;
1814 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1815 tif->tif_postdecode = _TIFFNoPostDecode;
1816 tif->tif_foundfield = NULL;
1817 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1818 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1819 tif->tif_tagmethods.printdir = NULL;
1820 /* additional default values */
1821 td->td_planarconfig = PLANARCONFIG_CONTIG;
1822 td->td_compression = COMPRESSION_NONE;
1823 td->td_subfiletype = 0;
1824 td->td_minsamplevalue = 0;
1825 /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
1826 * Therefore, td_maxsamplevalue has to be re-calculated in
1827 * TIFFGetFieldDefaulted(). */
1828 td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */
1829 td->td_extrasamples = 0;
1830 td->td_sampleinfo = NULL;
1831
1832 /*
1833 * Give client code a chance to install their own
1834 * tag extensions & methods, prior to compression overloads,
1835 * but do some prior cleanup first.
1836 * (http://trac.osgeo.org/gdal/ticket/5054)
1837 */
1838 if (tif->tif_nfieldscompat > 0)
1839 {
1840 uint32_t i;
1841
1842 for (i = 0; i < tif->tif_nfieldscompat; i++)
1843 {
1844 if (tif->tif_fieldscompat[i].allocated_size)
1845 _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
1846 }
1847 _TIFFfreeExt(tif, tif->tif_fieldscompat);
1848 tif->tif_nfieldscompat = 0;
1849 tif->tif_fieldscompat = NULL;
1850 }
1851 if (_TIFFextender)
1852 (*_TIFFextender)(tif);
1853 (void)TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1854 /*
1855 * NB: The directory is marked dirty as a result of setting
1856 * up the default compression scheme. However, this really
1857 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1858 * if the user does something. We could just do the setup
1859 * by hand, but it seems better to use the normal mechanism
1860 * (i.e. TIFFSetField).
1861 */
1862 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1863
1864 /*
1865 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1866 * we clear the ISTILED flag when setting up a new directory.
1867 * Should we also be clearing stuff like INSUBIFD?
1868 */
1869 tif->tif_flags &= ~TIFF_ISTILED;
1870
1871 return (1);
1872 }
1873
1874 static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off,
1875 tdir_t *nextdirnum)
1876 {
1877 static const char module[] = "TIFFAdvanceDirectory";
1878
1879 /* Add this directory to the directory list, if not already in. */
1880 if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
1881 {
1882 TIFFErrorExtR(tif, module,
1883 "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
1884 ") might cause an IFD loop",
1885 *nextdirnum, *nextdiroff, *nextdiroff);
1886 *nextdiroff = 0;
1887 *nextdirnum = 0;
1888 return (0);
1889 }
1890
1891 if (isMapped(tif))
1892 {
1893 uint64_t poff = *nextdiroff;
1894 if (!(tif->tif_flags & TIFF_BIGTIFF))
1895 {
1896 tmsize_t poffa, poffb, poffc, poffd;
1897 uint16_t dircount;
1898 uint32_t nextdir32;
1899 if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint16_t) ||
1900 poff > (uint64_t)tif->tif_size - sizeof(uint16_t))
1901 {
1902 TIFFErrorExtR(tif, module,
1903 "%s:%d: %s: Error fetching directory count",
1904 __FILE__, __LINE__, tif->tif_name);
1905 *nextdiroff = 0;
1906 return (0);
1907 }
1908 poffa = (tmsize_t)poff;
1909 poffb = poffa + (tmsize_t)sizeof(uint16_t);
1910 _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t));
1911 if (tif->tif_flags & TIFF_SWAB)
1912 TIFFSwabShort(&dircount);
1913 if (poffb >
1914 TIFF_TMSIZE_T_MAX - dircount * 12 - (tmsize_t)sizeof(uint32_t))
1915 {
1916 TIFFErrorExtR(tif, module, "Error fetching directory link");
1917 return (0);
1918 }
1919 poffc = poffb + dircount * 12;
1920 poffd = poffc + (tmsize_t)sizeof(uint32_t);
1921 if (poffd > tif->tif_size)
1922 {
1923 TIFFErrorExtR(tif, module, "Error fetching directory link");
1924 return (0);
1925 }
1926 if (off != NULL)
1927 *off = (uint64_t)poffc;
1928 _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t));
1929 if (tif->tif_flags & TIFF_SWAB)
1930 TIFFSwabLong(&nextdir32);
1931 *nextdiroff = nextdir32;
1932 }
1933 else
1934 {
1935 tmsize_t poffa, poffb, poffc, poffd;
1936 uint64_t dircount64;
1937 if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t))
1938 {
1939 TIFFErrorExtR(tif, module,
1940 "%s:%d: %s: Error fetching directory count",
1941 __FILE__, __LINE__, tif->tif_name);
1942 return (0);
1943 }
1944 poffa = (tmsize_t)poff;
1945 poffb = poffa + (tmsize_t)sizeof(uint64_t);
1946 if (poffb > tif->tif_size)
1947 {
1948 TIFFErrorExtR(tif, module,
1949 "%s:%d: %s: Error fetching directory count",
1950 __FILE__, __LINE__, tif->tif_name);
1951 return (0);
1952 }
1953 _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t));
1954 if (tif->tif_flags & TIFF_SWAB)
1955 TIFFSwabLong8(&dircount64);
1956 if (dircount64 > 0xFFFF)
1957 {
1958 TIFFErrorExtR(tif, module,
1959 "Sanity check on directory count failed");
1960 return (0);
1961 }
1962 if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount64 * 20) -
1963 (tmsize_t)sizeof(uint64_t))
1964 {
1965 TIFFErrorExtR(tif, module, "Error fetching directory link");
1966 return (0);
1967 }
1968 poffc = poffb + (tmsize_t)(dircount64 * 20);
1969 poffd = poffc + (tmsize_t)sizeof(uint64_t);
1970 if (poffd > tif->tif_size)
1971 {
1972 TIFFErrorExtR(tif, module, "Error fetching directory link");
1973 return (0);
1974 }
1975 if (off != NULL)
1976 *off = (uint64_t)poffc;
1977 _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t));
1978 if (tif->tif_flags & TIFF_SWAB)
1979 TIFFSwabLong8(nextdiroff);
1980 }
1981 }
1982 else
1983 {
1984 if (!(tif->tif_flags & TIFF_BIGTIFF))
1985 {
1986 uint16_t dircount;
1987 uint32_t nextdir32;
1988 if (!SeekOK(tif, *nextdiroff) ||
1989 !ReadOK(tif, &dircount, sizeof(uint16_t)))
1990 {
1991 TIFFErrorExtR(tif, module,
1992 "%s:%d: %s: Error fetching directory count",
1993 __FILE__, __LINE__, tif->tif_name);
1994 return (0);
1995 }
1996 if (tif->tif_flags & TIFF_SWAB)
1997 TIFFSwabShort(&dircount);
1998 if (off != NULL)
1999 *off = TIFFSeekFile(tif, dircount * 12U, SEEK_CUR);
2000 else
2001 (void)TIFFSeekFile(tif, dircount * 12U, SEEK_CUR);
2002 if (!ReadOK(tif, &nextdir32, sizeof(uint32_t)))
2003 {
2004 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
2005 tif->tif_name);
2006 return (0);
2007 }
2008 if (tif->tif_flags & TIFF_SWAB)
2009 TIFFSwabLong(&nextdir32);
2010 *nextdiroff = nextdir32;
2011 }
2012 else
2013 {
2014 uint64_t dircount64;
2015 if (!SeekOK(tif, *nextdiroff) ||
2016 !ReadOK(tif, &dircount64, sizeof(uint64_t)))
2017 {
2018 TIFFErrorExtR(tif, module,
2019 "%s:%d: %s: Error fetching directory count",
2020 __FILE__, __LINE__, tif->tif_name);
2021 return (0);
2022 }
2023 if (tif->tif_flags & TIFF_SWAB)
2024 TIFFSwabLong8(&dircount64);
2025 if (dircount64 > 0xFFFF)
2026 {
2027 TIFFErrorExtR(tif, module,
2028 "%s:%d: %s: Error fetching directory count",
2029 __FILE__, __LINE__, tif->tif_name);
2030 return (0);
2031 }
2032 if (off != NULL)
2033 *off = TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR);
2034 else
2035 (void)TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR);
2036 if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
2037 {
2038 TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
2039 tif->tif_name);
2040 return (0);
2041 }
2042 if (tif->tif_flags & TIFF_SWAB)
2043 TIFFSwabLong8(nextdiroff);
2044 }
2045 }
2046 if (*nextdiroff != 0)
2047 {
2048 (*nextdirnum)++;
2049 /* Check next directory for IFD looping and if so, set it as last
2050 * directory. */
2051 if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
2052 {
2053 TIFFWarningExtR(
2054 tif, module,
2055 "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64
2056 ") might be an IFD loop. Treating directory %d as "
2057 "last directory",
2058 *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1);
2059 *nextdiroff = 0;
2060 (*nextdirnum)--;
2061 }
2062 }
2063 return (1);
2064 }
2065
2066 /*
2067 * Count the number of directories in a file.
2068 */
2069 tdir_t TIFFNumberOfDirectories(TIFF *tif)
2070 {
2071 uint64_t nextdiroff;
2072 tdir_t nextdirnum;
2073 tdir_t n;
2074 if (!(tif->tif_flags & TIFF_BIGTIFF))
2075 nextdiroff = tif->tif_header.classic.tiff_diroff;
2076 else
2077 nextdiroff = tif->tif_header.big.tiff_diroff;
2078 nextdirnum = 0;
2079 n = 0;
2080 while (nextdiroff != 0 &&
2081 TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
2082 {
2083 ++n;
2084 }
2085 /* Update number of main-IFDs in file. */
2086 tif->tif_curdircount = n;
2087 return (n);
2088 }
2089
2090 /*
2091 * Set the n-th directory as the current directory.
2092 * NB: Directories are numbered starting at 0.
2093 */
2094 int TIFFSetDirectory(TIFF *tif, tdir_t dirn)
2095 {
2096 uint64_t nextdiroff;
2097 tdir_t nextdirnum = 0;
2098 tdir_t n;
2099
2100 if (tif->tif_setdirectory_force_absolute)
2101 {
2102 /* tif_setdirectory_force_absolute=1 will force parsing the main IFD
2103 * chain from the beginning, thus IFD directory list needs to be cleared
2104 * from possible SubIFD offsets.
2105 */
2106 _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
2107 }
2108
2109 /* Even faster path, if offset is available within IFD loop hash list. */
2110 if (!tif->tif_setdirectory_force_absolute &&
2111 _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff))
2112 {
2113 /* Set parameters for following TIFFReadDirectory() below. */
2114 tif->tif_nextdiroff = nextdiroff;
2115 tif->tif_curdir = dirn;
2116 /* Reset to relative stepping */
2117 tif->tif_setdirectory_force_absolute = FALSE;
2118 }
2119 else
2120 {
2121
2122 /* Fast path when we just advance relative to the current directory:
2123 * start at the current dir offset and continue to seek from there.
2124 * Check special cases when relative is not allowed:
2125 * - jump back from SubIFD or custom directory
2126 * - right after TIFFWriteDirectory() jump back to that directory
2127 * using TIFFSetDirectory() */
2128 const int relative = (dirn >= tif->tif_curdir) &&
2129 (tif->tif_diroff != 0) &&
2130 !tif->tif_setdirectory_force_absolute;
2131
2132 if (relative)
2133 {
2134 nextdiroff = tif->tif_diroff;
2135 dirn -= tif->tif_curdir;
2136 nextdirnum = tif->tif_curdir;
2137 }
2138 else if (!(tif->tif_flags & TIFF_BIGTIFF))
2139 nextdiroff = tif->tif_header.classic.tiff_diroff;
2140 else
2141 nextdiroff = tif->tif_header.big.tiff_diroff;
2142
2143 /* Reset to relative stepping */
2144 tif->tif_setdirectory_force_absolute = FALSE;
2145
2146 for (n = dirn; n > 0 && nextdiroff != 0; n--)
2147 if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
2148 return (0);
2149 /* If the n-th directory could not be reached (does not exist),
2150 * return here without touching anything further. */
2151 if (nextdiroff == 0 || n > 0)
2152 return (0);
2153
2154 tif->tif_nextdiroff = nextdiroff;
2155
2156 /* Set curdir to the actual directory index. */
2157 if (relative)
2158 tif->tif_curdir += dirn - n;
2159 else
2160 tif->tif_curdir = dirn - n;
2161 }
2162
2163 /* The -1 decrement is because TIFFReadDirectory will increment
2164 * tif_curdir after successfully reading the directory. */
2165 if (tif->tif_curdir == 0)
2166 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2167 else
2168 tif->tif_curdir--;
2169
2170 tdir_t curdir = tif->tif_curdir;
2171
2172 int retval = TIFFReadDirectory(tif);
2173
2174 if (!retval && tif->tif_curdir == curdir)
2175 {
2176 /* If tif_curdir has not be incremented, TIFFFetchDirectory() in
2177 * TIFFReadDirectory() has failed and tif_curdir shall be set
2178 * specifically. */
2179 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2180 }
2181 return (retval);
2182 }
2183
2184 /*
2185 * Set the current directory to be the directory
2186 * located at the specified file offset. This interface
2187 * is used mainly to access directories linked with
2188 * the SubIFD tag (e.g. thumbnail images).
2189 */
2190 int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
2191 {
2192 /* Match nextdiroff and curdir for consistent IFD-loop checking.
2193 * Only with TIFFSetSubDirectory() the IFD list can be corrupted with
2194 * invalid offsets within the main IFD tree. In the case of several subIFDs
2195 * of a main image, there are two possibilities that are not even mutually
2196 * exclusive. a.) The subIFD tag contains an array with all offsets of the
2197 * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters.
2198 * (refer to
2199 * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.)
2200 */
2201 int retval;
2202 uint32_t curdir = 0;
2203 int8_t probablySubIFD = 0;
2204 if (diroff == 0)
2205 {
2206 /* Special case to set tif_diroff=0, which is done in
2207 * TIFFReadDirectory() below to indicate that the currently read IFD is
2208 * treated as a new, fresh IFD. */
2209 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2210 tif->tif_dir.td_iswrittentofile = FALSE;
2211 }
2212 else
2213 {
2214 if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir))
2215 {
2216 /* Non-existing offsets might point to a SubIFD or invalid IFD.*/
2217 probablySubIFD = 1;
2218 }
2219 /* -1 because TIFFReadDirectory() will increment tif_curdir. */
2220 if (curdir >= 1)
2221 tif->tif_curdir = curdir - 1;
2222 else
2223 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2224 }
2225 curdir = tif->tif_curdir;
2226
2227 tif->tif_nextdiroff = diroff;
2228 retval = TIFFReadDirectory(tif);
2229
2230 /* tif_curdir is incremented in TIFFReadDirectory(), but if it has not been
2231 * incremented, TIFFFetchDirectory() has failed there and tif_curdir shall
2232 * be set specifically. */
2233 if (!retval && diroff != 0 && tif->tif_curdir == curdir)
2234 {
2235 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2236 }
2237
2238 if (probablySubIFD)
2239 {
2240 if (retval)
2241 {
2242 /* Reset IFD list to start new one for SubIFD chain and also start
2243 * SubIFD chain with tif_curdir=0 for IFD loop checking. */
2244 /* invalidate IFD loop lists */
2245 _TIFFCleanupIFDOffsetAndNumberMaps(tif);
2246 tif->tif_curdir = 0; /* first directory of new chain */
2247 /* add this offset to new IFD list */
2248 retval = _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff);
2249 }
2250 /* To be able to return from SubIFD or custom-IFD to main-IFD */
2251 tif->tif_setdirectory_force_absolute = TRUE;
2252 }
2253
2254 return (retval);
2255 }
2256
2257 /*
2258 * Return file offset of the current directory.
2259 */
2260 uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); }
2261
2262 /*
2263 * Return an indication of whether or not we are
2264 * at the last directory in the file.
2265 */
2266 int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); }
2267
2268 /*
2269 * Unlink the specified directory from the directory chain.
2270 * Note: First directory starts with number dirn=1.
2271 * This is different to TIFFSetDirectory() where the first directory starts with
2272 * zero.
2273 */
2274 int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn)
2275 {
2276 static const char module[] = "TIFFUnlinkDirectory";
2277 uint64_t nextdir;
2278 tdir_t nextdirnum;
2279 uint64_t off;
2280 tdir_t n;
2281
2282 if (tif->tif_mode == O_RDONLY)
2283 {
2284 TIFFErrorExtR(tif, module,
2285 "Can not unlink directory in read-only file");
2286 return (0);
2287 }
2288 if (dirn == 0)
2289 {
2290 TIFFErrorExtR(tif, module,
2291 "For TIFFUnlinkDirectory() first directory starts with "
2292 "number 1 and not 0");
2293 return (0);
2294 }
2295 /*
2296 * Go to the directory before the one we want
2297 * to unlink and nab the offset of the link
2298 * field we'll need to patch.
2299 */
2300 if (!(tif->tif_flags & TIFF_BIGTIFF))
2301 {
2302 nextdir = tif->tif_header.classic.tiff_diroff;
2303 off = 4;
2304 }
2305 else
2306 {
2307 nextdir = tif->tif_header.big.tiff_diroff;
2308 off = 8;
2309 }
2310 nextdirnum = 0; /* First directory is dirn=0 */
2311
2312 for (n = dirn - 1; n > 0; n--)
2313 {
2314 if (nextdir == 0)
2315 {
2316 TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn);
2317 return (0);
2318 }
2319 if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum))
2320 return (0);
2321 }
2322 /*
2323 * Advance to the directory to be unlinked and fetch
2324 * the offset of the directory that follows.
2325 */
2326 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum))
2327 return (0);
2328 /*
2329 * Go back and patch the link field of the preceding
2330 * directory to point to the offset of the directory
2331 * that follows.
2332 */
2333 (void)TIFFSeekFile(tif, off, SEEK_SET);
2334 if (!(tif->tif_flags & TIFF_BIGTIFF))
2335 {
2336 uint32_t nextdir32;
2337 nextdir32 = (uint32_t)nextdir;
2338 assert((uint64_t)nextdir32 == nextdir);
2339 if (tif->tif_flags & TIFF_SWAB)
2340 TIFFSwabLong(&nextdir32);
2341 if (!WriteOK(tif, &nextdir32, sizeof(uint32_t)))
2342 {
2343 TIFFErrorExtR(tif, module, "Error writing directory link");
2344 return (0);
2345 }
2346 }
2347 else
2348 {
2349 /* Need local swap because nextdir has to be used unswapped below. */
2350 uint64_t nextdir64 = nextdir;
2351 if (tif->tif_flags & TIFF_SWAB)
2352 TIFFSwabLong8(&nextdir64);
2353 if (!WriteOK(tif, &nextdir64, sizeof(uint64_t)))
2354 {
2355 TIFFErrorExtR(tif, module, "Error writing directory link");
2356 return (0);
2357 }
2358 }
2359
2360 /* For dirn=1 (first directory) also update the libtiff internal
2361 * base offset variables. */
2362 if (dirn == 1)
2363 {
2364 if (!(tif->tif_flags & TIFF_BIGTIFF))
2365 tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir;
2366 else
2367 tif->tif_header.big.tiff_diroff = nextdir;
2368 }
2369
2370 /*
2371 * Leave directory state setup safely. We don't have
2372 * facilities for doing inserting and removing directories,
2373 * so it's safest to just invalidate everything. This
2374 * means that the caller can only append to the directory
2375 * chain.
2376 */
2377 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
2378 {
2379 _TIFFfreeExt(tif, tif->tif_rawdata);
2380 tif->tif_rawdata = NULL;
2381 tif->tif_rawcc = 0;
2382 tif->tif_rawcp = NULL;
2383 tif->tif_rawdataoff = 0;
2384 tif->tif_rawdataloaded = 0;
2385 }
2386 tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP | TIFF_POSTENCODE |
2387 TIFF_BUF4WRITE);
2388 TIFFFreeDirectory(tif);
2389 TIFFDefaultDirectory(tif);
2390 tif->tif_diroff = 0; /* force link on next write */
2391 tif->tif_nextdiroff = 0; /* next write must be at end */
2392 tif->tif_lastdiroff = 0; /* will be updated on next link */
2393 tif->tif_curoff = 0;
2394 tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
2395 if (tif->tif_curdircount > 0)
2396 tif->tif_curdircount--;
2397 else
2398 tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER;
2399 _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
2400 return (1);
2401 }