702b5129fba3f9d6ef7c13af4952c99caf999c1d
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_dir.c
1 /* $Id: tif_dir.c,v 1.127 2016-10-25 21:35:15 erouault Exp $ */
2
3 /*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library.
29 *
30 * Directory Tag Get & Set Routines.
31 * (and also some miscellaneous stuff)
32 */
33
34 #include <precomp.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
45 setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
46 {
47 if (*vpp) {
48 _TIFFfree(*vpp);
49 *vpp = 0;
50 }
51 if (vp) {
52 tmsize_t bytes = (tmsize_t)(nmemb * elem_size);
53 if (elem_size && bytes / elem_size == nmemb)
54 *vpp = (void*) _TIFFmalloc(bytes);
55 if (*vpp)
56 _TIFFmemcpy(*vpp, vp, bytes);
57 }
58 }
59 void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
60 { setByteArray(vpp, vp, n, 1); }
61 void _TIFFsetString(char** cpp, char* cp)
62 { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
63 static void _TIFFsetNString(char** cpp, char* cp, uint32 n)
64 { setByteArray((void**) cpp, (void*) cp, n, 1); }
65 void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
66 { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
67 void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
68 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
69 static void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
70 { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
71 void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
72 { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
73 void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
74 { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
75
76 static void
77 setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
78 {
79 if (*vpp)
80 _TIFFfree(*vpp);
81 *vpp = _TIFFmalloc(nmemb*sizeof(double));
82 if (*vpp)
83 {
84 while (nmemb--)
85 ((double*)*vpp)[nmemb] = value;
86 }
87 }
88
89 /*
90 * Install extra samples information.
91 */
92 static int
93 setExtraSamples(TIFFDirectory* td, va_list ap, uint32* v)
94 {
95 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
96 #define EXTRASAMPLE_COREL_UNASSALPHA 999
97
98 uint16* va;
99 uint32 i;
100
101 *v = (uint16) va_arg(ap, uint16_vap);
102 if ((uint16) *v > td->td_samplesperpixel)
103 return 0;
104 va = va_arg(ap, uint16*);
105 if (*v > 0 && va == NULL) /* typically missing param */
106 return 0;
107 for (i = 0; i < *v; i++) {
108 if (va[i] > EXTRASAMPLE_UNASSALPHA) {
109 /*
110 * XXX: Corel Draw is known to produce incorrect
111 * ExtraSamples tags which must be patched here if we
112 * want to be able to open some of the damaged TIFF
113 * files:
114 */
115 if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
116 va[i] = EXTRASAMPLE_UNASSALPHA;
117 else
118 return 0;
119 }
120 }
121 td->td_extrasamples = (uint16) *v;
122 _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
123 return 1;
124
125 #undef EXTRASAMPLE_COREL_UNASSALPHA
126 }
127
128 /*
129 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
130 * zero if the ink names are not as expected.
131 */
132 static uint32
133 checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
134 {
135 TIFFDirectory* td = &tif->tif_dir;
136 uint16 i = td->td_samplesperpixel;
137
138 if (slen > 0) {
139 const char* ep = s+slen;
140 const char* cp = s;
141 for (; i > 0; i--) {
142 for (; cp < ep && *cp != '\0'; cp++) {}
143 if (cp >= ep)
144 goto bad;
145 cp++; /* skip \0 */
146 }
147 return ((uint32)(cp-s));
148 }
149 bad:
150 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
151 "%s: Invalid InkNames value; expecting %d names, found %d",
152 tif->tif_name,
153 td->td_samplesperpixel,
154 td->td_samplesperpixel-i);
155 return (0);
156 }
157
158 static int
159 _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
160 {
161 static const char module[] = "_TIFFVSetField";
162
163 TIFFDirectory* td = &tif->tif_dir;
164 int status = 1;
165 uint32 v32, i, v;
166 double dblval;
167 char* s;
168 const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
169 uint32 standard_tag = tag;
170 if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
171 return 0;
172 /*
173 * We want to force the custom code to be used for custom
174 * fields even if the tag happens to match a well known
175 * one - important for reinterpreted handling of standard
176 * tag values in custom directories (i.e. EXIF)
177 */
178 if (fip->field_bit == FIELD_CUSTOM) {
179 standard_tag = 0;
180 }
181
182 switch (standard_tag) {
183 case TIFFTAG_SUBFILETYPE:
184 td->td_subfiletype = (uint32) va_arg(ap, uint32);
185 break;
186 case TIFFTAG_IMAGEWIDTH:
187 td->td_imagewidth = (uint32) va_arg(ap, uint32);
188 break;
189 case TIFFTAG_IMAGELENGTH:
190 td->td_imagelength = (uint32) va_arg(ap, uint32);
191 break;
192 case TIFFTAG_BITSPERSAMPLE:
193 td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
194 /*
195 * If the data require post-decoding processing to byte-swap
196 * samples, set it up here. Note that since tags are required
197 * to be ordered, compression code can override this behaviour
198 * in the setup method if it wants to roll the post decoding
199 * work in with its normal work.
200 */
201 if (tif->tif_flags & TIFF_SWAB) {
202 if (td->td_bitspersample == 8)
203 tif->tif_postdecode = _TIFFNoPostDecode;
204 else if (td->td_bitspersample == 16)
205 tif->tif_postdecode = _TIFFSwab16BitData;
206 else if (td->td_bitspersample == 24)
207 tif->tif_postdecode = _TIFFSwab24BitData;
208 else if (td->td_bitspersample == 32)
209 tif->tif_postdecode = _TIFFSwab32BitData;
210 else if (td->td_bitspersample == 64)
211 tif->tif_postdecode = _TIFFSwab64BitData;
212 else if (td->td_bitspersample == 128) /* two 64's */
213 tif->tif_postdecode = _TIFFSwab64BitData;
214 }
215 break;
216 case TIFFTAG_COMPRESSION:
217 v = (uint16) va_arg(ap, uint16_vap);
218 /*
219 * If we're changing the compression scheme, the notify the
220 * previous module so that it can cleanup any state it's
221 * setup.
222 */
223 if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
224 if ((uint32)td->td_compression == v)
225 break;
226 (*tif->tif_cleanup)(tif);
227 tif->tif_flags &= ~TIFF_CODERSETUP;
228 }
229 /*
230 * Setup new compression routine state.
231 */
232 if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
233 td->td_compression = (uint16) v;
234 else
235 status = 0;
236 break;
237 case TIFFTAG_PHOTOMETRIC:
238 td->td_photometric = (uint16) va_arg(ap, uint16_vap);
239 break;
240 case TIFFTAG_THRESHHOLDING:
241 td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
242 break;
243 case TIFFTAG_FILLORDER:
244 v = (uint16) va_arg(ap, uint16_vap);
245 if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
246 goto badvalue;
247 td->td_fillorder = (uint16) v;
248 break;
249 case TIFFTAG_ORIENTATION:
250 v = (uint16) va_arg(ap, uint16_vap);
251 if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
252 goto badvalue;
253 else
254 td->td_orientation = (uint16) v;
255 break;
256 case TIFFTAG_SAMPLESPERPIXEL:
257 v = (uint16) va_arg(ap, uint16_vap);
258 if (v == 0)
259 goto badvalue;
260 if( v != td->td_samplesperpixel )
261 {
262 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
263 if( td->td_sminsamplevalue != NULL )
264 {
265 TIFFWarningExt(tif->tif_clientdata,module,
266 "SamplesPerPixel tag value is changing, "
267 "but SMinSampleValue tag was read with a different value. Cancelling it");
268 TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
269 _TIFFfree(td->td_sminsamplevalue);
270 td->td_sminsamplevalue = NULL;
271 }
272 if( td->td_smaxsamplevalue != NULL )
273 {
274 TIFFWarningExt(tif->tif_clientdata,module,
275 "SamplesPerPixel tag value is changing, "
276 "but SMaxSampleValue tag was read with a different value. Cancelling it");
277 TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
278 _TIFFfree(td->td_smaxsamplevalue);
279 td->td_smaxsamplevalue = NULL;
280 }
281 }
282 td->td_samplesperpixel = (uint16) v;
283 break;
284 case TIFFTAG_ROWSPERSTRIP:
285 v32 = (uint32) va_arg(ap, uint32);
286 if (v32 == 0)
287 goto badvalue32;
288 td->td_rowsperstrip = v32;
289 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
290 td->td_tilelength = v32;
291 td->td_tilewidth = td->td_imagewidth;
292 }
293 break;
294 case TIFFTAG_MINSAMPLEVALUE:
295 td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
296 break;
297 case TIFFTAG_MAXSAMPLEVALUE:
298 td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
299 break;
300 case TIFFTAG_SMINSAMPLEVALUE:
301 if (tif->tif_flags & TIFF_PERSAMPLE)
302 _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
303 else
304 setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
305 break;
306 case TIFFTAG_SMAXSAMPLEVALUE:
307 if (tif->tif_flags & TIFF_PERSAMPLE)
308 _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
309 else
310 setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
311 break;
312 case TIFFTAG_XRESOLUTION:
313 dblval = va_arg(ap, double);
314 if( dblval < 0 )
315 goto badvaluedouble;
316 td->td_xresolution = (float) dblval;
317 break;
318 case TIFFTAG_YRESOLUTION:
319 dblval = va_arg(ap, double);
320 if( dblval < 0 )
321 goto badvaluedouble;
322 td->td_yresolution = (float) dblval;
323 break;
324 case TIFFTAG_PLANARCONFIG:
325 v = (uint16) va_arg(ap, uint16_vap);
326 if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
327 goto badvalue;
328 td->td_planarconfig = (uint16) v;
329 break;
330 case TIFFTAG_XPOSITION:
331 td->td_xposition = (float) va_arg(ap, double);
332 break;
333 case TIFFTAG_YPOSITION:
334 td->td_yposition = (float) va_arg(ap, double);
335 break;
336 case TIFFTAG_RESOLUTIONUNIT:
337 v = (uint16) va_arg(ap, uint16_vap);
338 if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
339 goto badvalue;
340 td->td_resolutionunit = (uint16) v;
341 break;
342 case TIFFTAG_PAGENUMBER:
343 td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
344 td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
345 break;
346 case TIFFTAG_HALFTONEHINTS:
347 td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
348 td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
349 break;
350 case TIFFTAG_COLORMAP:
351 v32 = (uint32)(1L<<td->td_bitspersample);
352 _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
353 _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
354 _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
355 break;
356 case TIFFTAG_EXTRASAMPLES:
357 if (!setExtraSamples(td, ap, &v))
358 goto badvalue;
359 break;
360 case TIFFTAG_MATTEING:
361 td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
362 if (td->td_extrasamples) {
363 uint16 sv = EXTRASAMPLE_ASSOCALPHA;
364 _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
365 }
366 break;
367 case TIFFTAG_TILEWIDTH:
368 v32 = (uint32) va_arg(ap, uint32);
369 if (v32 % 16) {
370 if (tif->tif_mode != O_RDONLY)
371 goto badvalue32;
372 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
373 "Nonstandard tile width %d, convert file", v32);
374 }
375 td->td_tilewidth = v32;
376 tif->tif_flags |= TIFF_ISTILED;
377 break;
378 case TIFFTAG_TILELENGTH:
379 v32 = (uint32) va_arg(ap, uint32);
380 if (v32 % 16) {
381 if (tif->tif_mode != O_RDONLY)
382 goto badvalue32;
383 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
384 "Nonstandard tile length %d, convert file", v32);
385 }
386 td->td_tilelength = v32;
387 tif->tif_flags |= TIFF_ISTILED;
388 break;
389 case TIFFTAG_TILEDEPTH:
390 v32 = (uint32) va_arg(ap, uint32);
391 if (v32 == 0)
392 goto badvalue32;
393 td->td_tiledepth = v32;
394 break;
395 case TIFFTAG_DATATYPE:
396 v = (uint16) va_arg(ap, uint16_vap);
397 switch (v) {
398 case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
399 case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
400 case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
401 case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
402 default: goto badvalue;
403 }
404 td->td_sampleformat = (uint16) v;
405 break;
406 case TIFFTAG_SAMPLEFORMAT:
407 v = (uint16) va_arg(ap, uint16_vap);
408 if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
409 goto badvalue;
410 td->td_sampleformat = (uint16) v;
411
412 /* Try to fix up the SWAB function for complex data. */
413 if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
414 && td->td_bitspersample == 32
415 && tif->tif_postdecode == _TIFFSwab32BitData )
416 tif->tif_postdecode = _TIFFSwab16BitData;
417 else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
418 || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
419 && td->td_bitspersample == 64
420 && tif->tif_postdecode == _TIFFSwab64BitData )
421 tif->tif_postdecode = _TIFFSwab32BitData;
422 break;
423 case TIFFTAG_IMAGEDEPTH:
424 td->td_imagedepth = (uint32) va_arg(ap, uint32);
425 break;
426 case TIFFTAG_SUBIFD:
427 if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
428 td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
429 _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
430 (uint32) td->td_nsubifd);
431 } else {
432 TIFFErrorExt(tif->tif_clientdata, module,
433 "%s: Sorry, cannot nest SubIFDs",
434 tif->tif_name);
435 status = 0;
436 }
437 break;
438 case TIFFTAG_YCBCRPOSITIONING:
439 td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
440 break;
441 case TIFFTAG_YCBCRSUBSAMPLING:
442 td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
443 td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
444 break;
445 case TIFFTAG_TRANSFERFUNCTION:
446 v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
447 for (i = 0; i < v; i++)
448 _TIFFsetShortArray(&td->td_transferfunction[i],
449 va_arg(ap, uint16*), 1U<<td->td_bitspersample);
450 break;
451 case TIFFTAG_REFERENCEBLACKWHITE:
452 /* XXX should check for null range */
453 _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
454 break;
455 case TIFFTAG_INKNAMES:
456 v = (uint16) va_arg(ap, uint16_vap);
457 s = va_arg(ap, char*);
458 v = checkInkNamesString(tif, v, s);
459 status = v > 0;
460 if( v > 0 ) {
461 _TIFFsetNString(&td->td_inknames, s, v);
462 td->td_inknameslen = v;
463 }
464 break;
465 case TIFFTAG_PERSAMPLE:
466 v = (uint16) va_arg(ap, uint16_vap);
467 if( v == PERSAMPLE_MULTI )
468 tif->tif_flags |= TIFF_PERSAMPLE;
469 else
470 tif->tif_flags &= ~TIFF_PERSAMPLE;
471 break;
472 default: {
473 TIFFTagValue *tv;
474 int tv_size, iCustom;
475
476 /*
477 * This can happen if multiple images are open with different
478 * codecs which have private tags. The global tag information
479 * table may then have tags that are valid for one file but not
480 * the other. If the client tries to set a tag that is not valid
481 * for the image's codec then we'll arrive here. This
482 * happens, for example, when tiffcp is used to convert between
483 * compression schemes and codec-specific tags are blindly copied.
484 */
485 if(fip->field_bit != FIELD_CUSTOM) {
486 TIFFErrorExt(tif->tif_clientdata, module,
487 "%s: Invalid %stag \"%s\" (not supported by codec)",
488 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
489 fip->field_name);
490 status = 0;
491 break;
492 }
493
494 /*
495 * Find the existing entry for this custom value.
496 */
497 tv = NULL;
498 for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
499 if (td->td_customValues[iCustom].info->field_tag == tag) {
500 tv = td->td_customValues + iCustom;
501 if (tv->value != NULL) {
502 _TIFFfree(tv->value);
503 tv->value = NULL;
504 }
505 break;
506 }
507 }
508
509 /*
510 * Grow the custom list if the entry was not found.
511 */
512 if(tv == NULL) {
513 TIFFTagValue *new_customValues;
514
515 td->td_customValueCount++;
516 new_customValues = (TIFFTagValue *)
517 _TIFFrealloc(td->td_customValues,
518 sizeof(TIFFTagValue) * td->td_customValueCount);
519 if (!new_customValues) {
520 TIFFErrorExt(tif->tif_clientdata, module,
521 "%s: Failed to allocate space for list of custom values",
522 tif->tif_name);
523 status = 0;
524 goto end;
525 }
526
527 td->td_customValues = new_customValues;
528
529 tv = td->td_customValues + (td->td_customValueCount - 1);
530 tv->info = fip;
531 tv->value = NULL;
532 tv->count = 0;
533 }
534
535 /*
536 * Set custom value ... save a copy of the custom tag value.
537 */
538 tv_size = _TIFFDataSize(fip->field_type);
539 if (tv_size == 0) {
540 status = 0;
541 TIFFErrorExt(tif->tif_clientdata, module,
542 "%s: Bad field type %d for \"%s\"",
543 tif->tif_name, fip->field_type,
544 fip->field_name);
545 goto end;
546 }
547
548 if (fip->field_type == TIFF_ASCII)
549 {
550 uint32 ma;
551 char* mb;
552 if (fip->field_passcount)
553 {
554 assert(fip->field_writecount==TIFF_VARIABLE2);
555 ma=(uint32)va_arg(ap,uint32);
556 mb=(char*)va_arg(ap,char*);
557 }
558 else
559 {
560 mb=(char*)va_arg(ap,char*);
561 ma=(uint32)(strlen(mb)+1);
562 }
563 tv->count=ma;
564 setByteArray(&tv->value,mb,ma,1);
565 }
566 else
567 {
568 if (fip->field_passcount) {
569 if (fip->field_writecount == TIFF_VARIABLE2)
570 tv->count = (uint32) va_arg(ap, uint32);
571 else
572 tv->count = (int) va_arg(ap, int);
573 } else if (fip->field_writecount == TIFF_VARIABLE
574 || fip->field_writecount == TIFF_VARIABLE2)
575 tv->count = 1;
576 else if (fip->field_writecount == TIFF_SPP)
577 tv->count = td->td_samplesperpixel;
578 else
579 tv->count = fip->field_writecount;
580
581 if (tv->count == 0) {
582 status = 0;
583 TIFFErrorExt(tif->tif_clientdata, module,
584 "%s: Null count for \"%s\" (type "
585 "%d, writecount %d, passcount %d)",
586 tif->tif_name,
587 fip->field_name,
588 fip->field_type,
589 fip->field_writecount,
590 fip->field_passcount);
591 goto end;
592 }
593
594 tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
595 "custom tag binary object");
596 if (!tv->value) {
597 status = 0;
598 goto end;
599 }
600
601 if (fip->field_tag == TIFFTAG_DOTRANGE
602 && strcmp(fip->field_name,"DotRange") == 0) {
603 /* TODO: This is an evil exception and should not have been
604 handled this way ... likely best if we move it into
605 the directory structure with an explicit field in
606 libtiff 4.1 and assign it a FIELD_ value */
607 uint16 v2[2];
608 v2[0] = (uint16)va_arg(ap, int);
609 v2[1] = (uint16)va_arg(ap, int);
610 _TIFFmemcpy(tv->value, &v2, 4);
611 }
612
613 else if (fip->field_passcount
614 || fip->field_writecount == TIFF_VARIABLE
615 || fip->field_writecount == TIFF_VARIABLE2
616 || fip->field_writecount == TIFF_SPP
617 || tv->count > 1) {
618 _TIFFmemcpy(tv->value, va_arg(ap, void *),
619 tv->count * tv_size);
620 } else {
621 char *val = (char *)tv->value;
622 assert( tv->count == 1 );
623
624 switch (fip->field_type) {
625 case TIFF_BYTE:
626 case TIFF_UNDEFINED:
627 {
628 uint8 v2 = (uint8)va_arg(ap, int);
629 _TIFFmemcpy(val, &v2, tv_size);
630 }
631 break;
632 case TIFF_SBYTE:
633 {
634 int8 v2 = (int8)va_arg(ap, int);
635 _TIFFmemcpy(val, &v2, tv_size);
636 }
637 break;
638 case TIFF_SHORT:
639 {
640 uint16 v2 = (uint16)va_arg(ap, int);
641 _TIFFmemcpy(val, &v2, tv_size);
642 }
643 break;
644 case TIFF_SSHORT:
645 {
646 int16 v2 = (int16)va_arg(ap, int);
647 _TIFFmemcpy(val, &v2, tv_size);
648 }
649 break;
650 case TIFF_LONG:
651 case TIFF_IFD:
652 {
653 uint32 v2 = va_arg(ap, uint32);
654 _TIFFmemcpy(val, &v2, tv_size);
655 }
656 break;
657 case TIFF_SLONG:
658 {
659 int32 v2 = va_arg(ap, int32);
660 _TIFFmemcpy(val, &v2, tv_size);
661 }
662 break;
663 case TIFF_LONG8:
664 case TIFF_IFD8:
665 {
666 uint64 v2 = va_arg(ap, uint64);
667 _TIFFmemcpy(val, &v2, tv_size);
668 }
669 break;
670 case TIFF_SLONG8:
671 {
672 int64 v2 = va_arg(ap, int64);
673 _TIFFmemcpy(val, &v2, tv_size);
674 }
675 break;
676 case TIFF_RATIONAL:
677 case TIFF_SRATIONAL:
678 case TIFF_FLOAT:
679 {
680 float v2 = (float)va_arg(ap, double);
681 _TIFFmemcpy(val, &v2, tv_size);
682 }
683 break;
684 case TIFF_DOUBLE:
685 {
686 double v2 = va_arg(ap, double);
687 _TIFFmemcpy(val, &v2, tv_size);
688 }
689 break;
690 default:
691 _TIFFmemset(val, 0, tv_size);
692 status = 0;
693 break;
694 }
695 }
696 }
697 }
698 }
699 if (status) {
700 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
701 if (fip2)
702 TIFFSetFieldBit(tif, fip2->field_bit);
703 tif->tif_flags |= TIFF_DIRTYDIRECT;
704 }
705
706 end:
707 va_end(ap);
708 return (status);
709 badvalue:
710 {
711 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
712 TIFFErrorExt(tif->tif_clientdata, module,
713 "%s: Bad value %u for \"%s\" tag",
714 tif->tif_name, v,
715 fip2 ? fip2->field_name : "Unknown");
716 va_end(ap);
717 }
718 return (0);
719 badvalue32:
720 {
721 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
722 TIFFErrorExt(tif->tif_clientdata, module,
723 "%s: Bad value %u for \"%s\" tag",
724 tif->tif_name, v32,
725 fip2 ? fip2->field_name : "Unknown");
726 va_end(ap);
727 }
728 return (0);
729 badvaluedouble:
730 {
731 const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
732 TIFFErrorExt(tif->tif_clientdata, module,
733 "%s: Bad value %f for \"%s\" tag",
734 tif->tif_name, dblval,
735 fip2 ? fip2->field_name : "Unknown");
736 va_end(ap);
737 }
738 return (0);
739 }
740
741 /*
742 * Return 1/0 according to whether or not
743 * it is permissible to set the tag's value.
744 * Note that we allow ImageLength to be changed
745 * so that we can append and extend to images.
746 * Any other tag may not be altered once writing
747 * has commenced, unless its value has no effect
748 * on the format of the data that is written.
749 */
750 static int
751 OkToChangeTag(TIFF* tif, uint32 tag)
752 {
753 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
754 if (!fip) { /* unknown tag */
755 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
756 tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
757 return (0);
758 }
759 if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
760 !fip->field_oktochange) {
761 /*
762 * Consult info table to see if tag can be changed
763 * after we've started writing. We only allow changes
764 * to those tags that don't/shouldn't affect the
765 * compression and/or format of the data.
766 */
767 TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
768 "%s: Cannot modify tag \"%s\" while writing",
769 tif->tif_name, fip->field_name);
770 return (0);
771 }
772 return (1);
773 }
774
775 /*
776 * Record the value of a field in the
777 * internal directory structure. The
778 * field will be written to the file
779 * when/if the directory structure is
780 * updated.
781 */
782 int
783 TIFFSetField(TIFF* tif, uint32 tag, ...)
784 {
785 va_list ap;
786 int status;
787
788 va_start(ap, tag);
789 status = TIFFVSetField(tif, tag, ap);
790 va_end(ap);
791 return (status);
792 }
793
794 /*
795 * Clear the contents of the field in the internal structure.
796 */
797 int
798 TIFFUnsetField(TIFF* tif, uint32 tag)
799 {
800 const TIFFField *fip = TIFFFieldWithTag(tif, tag);
801 TIFFDirectory* td = &tif->tif_dir;
802
803 if( !fip )
804 return 0;
805
806 if( fip->field_bit != FIELD_CUSTOM )
807 TIFFClrFieldBit(tif, fip->field_bit);
808 else
809 {
810 TIFFTagValue *tv = NULL;
811 int i;
812
813 for (i = 0; i < td->td_customValueCount; i++) {
814
815 tv = td->td_customValues + i;
816 if( tv->info->field_tag == tag )
817 break;
818 }
819
820 if( i < td->td_customValueCount )
821 {
822 _TIFFfree(tv->value);
823 for( ; i < td->td_customValueCount-1; i++) {
824 td->td_customValues[i] = td->td_customValues[i+1];
825 }
826 td->td_customValueCount--;
827 }
828 }
829
830 tif->tif_flags |= TIFF_DIRTYDIRECT;
831
832 return (1);
833 }
834
835 /*
836 * Like TIFFSetField, but taking a varargs
837 * parameter list. This routine is useful
838 * for building higher-level interfaces on
839 * top of the library.
840 */
841 int
842 TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
843 {
844 return OkToChangeTag(tif, tag) ?
845 (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
846 }
847
848 static int
849 _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
850 {
851 TIFFDirectory* td = &tif->tif_dir;
852 int ret_val = 1;
853 uint32 standard_tag = tag;
854 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
855 if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
856 return 0;
857
858 /*
859 * We want to force the custom code to be used for custom
860 * fields even if the tag happens to match a well known
861 * one - important for reinterpreted handling of standard
862 * tag values in custom directories (i.e. EXIF)
863 */
864 if (fip->field_bit == FIELD_CUSTOM) {
865 standard_tag = 0;
866 }
867
868 switch (standard_tag) {
869 case TIFFTAG_SUBFILETYPE:
870 *va_arg(ap, uint32*) = td->td_subfiletype;
871 break;
872 case TIFFTAG_IMAGEWIDTH:
873 *va_arg(ap, uint32*) = td->td_imagewidth;
874 break;
875 case TIFFTAG_IMAGELENGTH:
876 *va_arg(ap, uint32*) = td->td_imagelength;
877 break;
878 case TIFFTAG_BITSPERSAMPLE:
879 *va_arg(ap, uint16*) = td->td_bitspersample;
880 break;
881 case TIFFTAG_COMPRESSION:
882 *va_arg(ap, uint16*) = td->td_compression;
883 break;
884 case TIFFTAG_PHOTOMETRIC:
885 *va_arg(ap, uint16*) = td->td_photometric;
886 break;
887 case TIFFTAG_THRESHHOLDING:
888 *va_arg(ap, uint16*) = td->td_threshholding;
889 break;
890 case TIFFTAG_FILLORDER:
891 *va_arg(ap, uint16*) = td->td_fillorder;
892 break;
893 case TIFFTAG_ORIENTATION:
894 *va_arg(ap, uint16*) = td->td_orientation;
895 break;
896 case TIFFTAG_SAMPLESPERPIXEL:
897 *va_arg(ap, uint16*) = td->td_samplesperpixel;
898 break;
899 case TIFFTAG_ROWSPERSTRIP:
900 *va_arg(ap, uint32*) = td->td_rowsperstrip;
901 break;
902 case TIFFTAG_MINSAMPLEVALUE:
903 *va_arg(ap, uint16*) = td->td_minsamplevalue;
904 break;
905 case TIFFTAG_MAXSAMPLEVALUE:
906 *va_arg(ap, uint16*) = td->td_maxsamplevalue;
907 break;
908 case TIFFTAG_SMINSAMPLEVALUE:
909 if (tif->tif_flags & TIFF_PERSAMPLE)
910 *va_arg(ap, double**) = td->td_sminsamplevalue;
911 else
912 {
913 /* libtiff historically treats this as a single value. */
914 uint16 i;
915 double v = td->td_sminsamplevalue[0];
916 for (i=1; i < td->td_samplesperpixel; ++i)
917 if( td->td_sminsamplevalue[i] < v )
918 v = td->td_sminsamplevalue[i];
919 *va_arg(ap, double*) = v;
920 }
921 break;
922 case TIFFTAG_SMAXSAMPLEVALUE:
923 if (tif->tif_flags & TIFF_PERSAMPLE)
924 *va_arg(ap, double**) = td->td_smaxsamplevalue;
925 else
926 {
927 /* libtiff historically treats this as a single value. */
928 uint16 i;
929 double v = td->td_smaxsamplevalue[0];
930 for (i=1; i < td->td_samplesperpixel; ++i)
931 if( td->td_smaxsamplevalue[i] > v )
932 v = td->td_smaxsamplevalue[i];
933 *va_arg(ap, double*) = v;
934 }
935 break;
936 case TIFFTAG_XRESOLUTION:
937 *va_arg(ap, float*) = td->td_xresolution;
938 break;
939 case TIFFTAG_YRESOLUTION:
940 *va_arg(ap, float*) = td->td_yresolution;
941 break;
942 case TIFFTAG_PLANARCONFIG:
943 *va_arg(ap, uint16*) = td->td_planarconfig;
944 break;
945 case TIFFTAG_XPOSITION:
946 *va_arg(ap, float*) = td->td_xposition;
947 break;
948 case TIFFTAG_YPOSITION:
949 *va_arg(ap, float*) = td->td_yposition;
950 break;
951 case TIFFTAG_RESOLUTIONUNIT:
952 *va_arg(ap, uint16*) = td->td_resolutionunit;
953 break;
954 case TIFFTAG_PAGENUMBER:
955 *va_arg(ap, uint16*) = td->td_pagenumber[0];
956 *va_arg(ap, uint16*) = td->td_pagenumber[1];
957 break;
958 case TIFFTAG_HALFTONEHINTS:
959 *va_arg(ap, uint16*) = td->td_halftonehints[0];
960 *va_arg(ap, uint16*) = td->td_halftonehints[1];
961 break;
962 case TIFFTAG_COLORMAP:
963 *va_arg(ap, uint16**) = td->td_colormap[0];
964 *va_arg(ap, uint16**) = td->td_colormap[1];
965 *va_arg(ap, uint16**) = td->td_colormap[2];
966 break;
967 case TIFFTAG_STRIPOFFSETS:
968 case TIFFTAG_TILEOFFSETS:
969 _TIFFFillStriles( tif );
970 *va_arg(ap, uint64**) = td->td_stripoffset;
971 break;
972 case TIFFTAG_STRIPBYTECOUNTS:
973 case TIFFTAG_TILEBYTECOUNTS:
974 _TIFFFillStriles( tif );
975 *va_arg(ap, uint64**) = td->td_stripbytecount;
976 break;
977 case TIFFTAG_MATTEING:
978 *va_arg(ap, uint16*) =
979 (td->td_extrasamples == 1 &&
980 td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
981 break;
982 case TIFFTAG_EXTRASAMPLES:
983 *va_arg(ap, uint16*) = td->td_extrasamples;
984 *va_arg(ap, uint16**) = td->td_sampleinfo;
985 break;
986 case TIFFTAG_TILEWIDTH:
987 *va_arg(ap, uint32*) = td->td_tilewidth;
988 break;
989 case TIFFTAG_TILELENGTH:
990 *va_arg(ap, uint32*) = td->td_tilelength;
991 break;
992 case TIFFTAG_TILEDEPTH:
993 *va_arg(ap, uint32*) = td->td_tiledepth;
994 break;
995 case TIFFTAG_DATATYPE:
996 switch (td->td_sampleformat) {
997 case SAMPLEFORMAT_UINT:
998 *va_arg(ap, uint16*) = DATATYPE_UINT;
999 break;
1000 case SAMPLEFORMAT_INT:
1001 *va_arg(ap, uint16*) = DATATYPE_INT;
1002 break;
1003 case SAMPLEFORMAT_IEEEFP:
1004 *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
1005 break;
1006 case SAMPLEFORMAT_VOID:
1007 *va_arg(ap, uint16*) = DATATYPE_VOID;
1008 break;
1009 }
1010 break;
1011 case TIFFTAG_SAMPLEFORMAT:
1012 *va_arg(ap, uint16*) = td->td_sampleformat;
1013 break;
1014 case TIFFTAG_IMAGEDEPTH:
1015 *va_arg(ap, uint32*) = td->td_imagedepth;
1016 break;
1017 case TIFFTAG_SUBIFD:
1018 *va_arg(ap, uint16*) = td->td_nsubifd;
1019 *va_arg(ap, uint64**) = td->td_subifd;
1020 break;
1021 case TIFFTAG_YCBCRPOSITIONING:
1022 *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
1023 break;
1024 case TIFFTAG_YCBCRSUBSAMPLING:
1025 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
1026 *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
1027 break;
1028 case TIFFTAG_TRANSFERFUNCTION:
1029 *va_arg(ap, uint16**) = td->td_transferfunction[0];
1030 if (td->td_samplesperpixel - td->td_extrasamples > 1) {
1031 *va_arg(ap, uint16**) = td->td_transferfunction[1];
1032 *va_arg(ap, uint16**) = td->td_transferfunction[2];
1033 }
1034 break;
1035 case TIFFTAG_REFERENCEBLACKWHITE:
1036 *va_arg(ap, float**) = td->td_refblackwhite;
1037 break;
1038 case TIFFTAG_INKNAMES:
1039 *va_arg(ap, char**) = td->td_inknames;
1040 break;
1041 default:
1042 {
1043 int i;
1044
1045 /*
1046 * This can happen if multiple images are open
1047 * with different codecs which have private
1048 * tags. The global tag information table may
1049 * then have tags that are valid for one file
1050 * but not the other. If the client tries to
1051 * get a tag that is not valid for the image's
1052 * codec then we'll arrive here.
1053 */
1054 if( fip->field_bit != FIELD_CUSTOM )
1055 {
1056 TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
1057 "%s: Invalid %stag \"%s\" "
1058 "(not supported by codec)",
1059 tif->tif_name,
1060 isPseudoTag(tag) ? "pseudo-" : "",
1061 fip->field_name);
1062 ret_val = 0;
1063 break;
1064 }
1065
1066 /*
1067 * Do we have a custom value?
1068 */
1069 ret_val = 0;
1070 for (i = 0; i < td->td_customValueCount; i++) {
1071 TIFFTagValue *tv = td->td_customValues + i;
1072
1073 if (tv->info->field_tag != tag)
1074 continue;
1075
1076 if (fip->field_passcount) {
1077 if (fip->field_readcount == TIFF_VARIABLE2)
1078 *va_arg(ap, uint32*) = (uint32)tv->count;
1079 else /* Assume TIFF_VARIABLE */
1080 *va_arg(ap, uint16*) = (uint16)tv->count;
1081 *va_arg(ap, void **) = tv->value;
1082 ret_val = 1;
1083 } else if (fip->field_tag == TIFFTAG_DOTRANGE
1084 && strcmp(fip->field_name,"DotRange") == 0) {
1085 /* TODO: This is an evil exception and should not have been
1086 handled this way ... likely best if we move it into
1087 the directory structure with an explicit field in
1088 libtiff 4.1 and assign it a FIELD_ value */
1089 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
1090 *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
1091 ret_val = 1;
1092 } else {
1093 if (fip->field_type == TIFF_ASCII
1094 || fip->field_readcount == TIFF_VARIABLE
1095 || fip->field_readcount == TIFF_VARIABLE2
1096 || fip->field_readcount == TIFF_SPP
1097 || tv->count > 1) {
1098 *va_arg(ap, void **) = tv->value;
1099 ret_val = 1;
1100 } else {
1101 char *val = (char *)tv->value;
1102 assert( tv->count == 1 );
1103 switch (fip->field_type) {
1104 case TIFF_BYTE:
1105 case TIFF_UNDEFINED:
1106 *va_arg(ap, uint8*) =
1107 *(uint8 *)val;
1108 ret_val = 1;
1109 break;
1110 case TIFF_SBYTE:
1111 *va_arg(ap, int8*) =
1112 *(int8 *)val;
1113 ret_val = 1;
1114 break;
1115 case TIFF_SHORT:
1116 *va_arg(ap, uint16*) =
1117 *(uint16 *)val;
1118 ret_val = 1;
1119 break;
1120 case TIFF_SSHORT:
1121 *va_arg(ap, int16*) =
1122 *(int16 *)val;
1123 ret_val = 1;
1124 break;
1125 case TIFF_LONG:
1126 case TIFF_IFD:
1127 *va_arg(ap, uint32*) =
1128 *(uint32 *)val;
1129 ret_val = 1;
1130 break;
1131 case TIFF_SLONG:
1132 *va_arg(ap, int32*) =
1133 *(int32 *)val;
1134 ret_val = 1;
1135 break;
1136 case TIFF_LONG8:
1137 case TIFF_IFD8:
1138 *va_arg(ap, uint64*) =
1139 *(uint64 *)val;
1140 ret_val = 1;
1141 break;
1142 case TIFF_SLONG8:
1143 *va_arg(ap, int64*) =
1144 *(int64 *)val;
1145 ret_val = 1;
1146 break;
1147 case TIFF_RATIONAL:
1148 case TIFF_SRATIONAL:
1149 case TIFF_FLOAT:
1150 *va_arg(ap, float*) =
1151 *(float *)val;
1152 ret_val = 1;
1153 break;
1154 case TIFF_DOUBLE:
1155 *va_arg(ap, double*) =
1156 *(double *)val;
1157 ret_val = 1;
1158 break;
1159 default:
1160 ret_val = 0;
1161 break;
1162 }
1163 }
1164 }
1165 break;
1166 }
1167 }
1168 }
1169 return(ret_val);
1170 }
1171
1172 /*
1173 * Return the value of a field in the
1174 * internal directory structure.
1175 */
1176 int
1177 TIFFGetField(TIFF* tif, uint32 tag, ...)
1178 {
1179 int status;
1180 va_list ap;
1181
1182 va_start(ap, tag);
1183 status = TIFFVGetField(tif, tag, ap);
1184 va_end(ap);
1185 return (status);
1186 }
1187
1188 /*
1189 * Like TIFFGetField, but taking a varargs
1190 * parameter list. This routine is useful
1191 * for building higher-level interfaces on
1192 * top of the library.
1193 */
1194 int
1195 TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
1196 {
1197 const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
1198 return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1199 (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1200 }
1201
1202 #define CleanupField(member) { \
1203 if (td->member) { \
1204 _TIFFfree(td->member); \
1205 td->member = 0; \
1206 } \
1207 }
1208
1209 /*
1210 * Release storage associated with a directory.
1211 */
1212 void
1213 TIFFFreeDirectory(TIFF* tif)
1214 {
1215 TIFFDirectory *td = &tif->tif_dir;
1216 int i;
1217
1218 _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
1219 CleanupField(td_sminsamplevalue);
1220 CleanupField(td_smaxsamplevalue);
1221 CleanupField(td_colormap[0]);
1222 CleanupField(td_colormap[1]);
1223 CleanupField(td_colormap[2]);
1224 CleanupField(td_sampleinfo);
1225 CleanupField(td_subifd);
1226 CleanupField(td_inknames);
1227 CleanupField(td_refblackwhite);
1228 CleanupField(td_transferfunction[0]);
1229 CleanupField(td_transferfunction[1]);
1230 CleanupField(td_transferfunction[2]);
1231 CleanupField(td_stripoffset);
1232 CleanupField(td_stripbytecount);
1233 TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
1234 TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
1235
1236 /* Cleanup custom tag values */
1237 for( i = 0; i < td->td_customValueCount; i++ ) {
1238 if (td->td_customValues[i].value)
1239 _TIFFfree(td->td_customValues[i].value);
1240 }
1241
1242 td->td_customValueCount = 0;
1243 CleanupField(td_customValues);
1244
1245 #if defined(DEFER_STRILE_LOAD)
1246 _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
1247 _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
1248 #endif
1249 }
1250 #undef CleanupField
1251
1252 /*
1253 * Client Tag extension support (from Niles Ritter).
1254 */
1255 static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1256
1257 TIFFExtendProc
1258 TIFFSetTagExtender(TIFFExtendProc extender)
1259 {
1260 TIFFExtendProc prev = _TIFFextender;
1261 _TIFFextender = extender;
1262 return (prev);
1263 }
1264
1265 /*
1266 * Setup for a new directory. Should we automatically call
1267 * TIFFWriteDirectory() if the current one is dirty?
1268 *
1269 * The newly created directory will not exist on the file till
1270 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1271 */
1272 int
1273 TIFFCreateDirectory(TIFF* tif)
1274 {
1275 TIFFDefaultDirectory(tif);
1276 tif->tif_diroff = 0;
1277 tif->tif_nextdiroff = 0;
1278 tif->tif_curoff = 0;
1279 tif->tif_row = (uint32) -1;
1280 tif->tif_curstrip = (uint32) -1;
1281
1282 return 0;
1283 }
1284
1285 int
1286 TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
1287 {
1288 TIFFDefaultDirectory(tif);
1289
1290 /*
1291 * Reset the field definitions to match the application provided list.
1292 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1293 * based on it's assumption this is an image directory.
1294 */
1295 _TIFFSetupFields(tif, infoarray);
1296
1297 tif->tif_diroff = 0;
1298 tif->tif_nextdiroff = 0;
1299 tif->tif_curoff = 0;
1300 tif->tif_row = (uint32) -1;
1301 tif->tif_curstrip = (uint32) -1;
1302
1303 return 0;
1304 }
1305
1306 int
1307 TIFFCreateEXIFDirectory(TIFF* tif)
1308 {
1309 const TIFFFieldArray* exifFieldArray;
1310 exifFieldArray = _TIFFGetExifFields();
1311 return TIFFCreateCustomDirectory(tif, exifFieldArray);
1312 }
1313
1314 /*
1315 * Setup a default directory structure.
1316 */
1317 int
1318 TIFFDefaultDirectory(TIFF* tif)
1319 {
1320 register TIFFDirectory* td = &tif->tif_dir;
1321 const TIFFFieldArray* tiffFieldArray;
1322
1323 tiffFieldArray = _TIFFGetFields();
1324 _TIFFSetupFields(tif, tiffFieldArray);
1325
1326 _TIFFmemset(td, 0, sizeof (*td));
1327 td->td_fillorder = FILLORDER_MSB2LSB;
1328 td->td_bitspersample = 1;
1329 td->td_threshholding = THRESHHOLD_BILEVEL;
1330 td->td_orientation = ORIENTATION_TOPLEFT;
1331 td->td_samplesperpixel = 1;
1332 td->td_rowsperstrip = (uint32) -1;
1333 td->td_tilewidth = 0;
1334 td->td_tilelength = 0;
1335 td->td_tiledepth = 1;
1336 td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
1337 td->td_resolutionunit = RESUNIT_INCH;
1338 td->td_sampleformat = SAMPLEFORMAT_UINT;
1339 td->td_imagedepth = 1;
1340 td->td_ycbcrsubsampling[0] = 2;
1341 td->td_ycbcrsubsampling[1] = 2;
1342 td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1343 tif->tif_postdecode = _TIFFNoPostDecode;
1344 tif->tif_foundfield = NULL;
1345 tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1346 tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1347 tif->tif_tagmethods.printdir = NULL;
1348 /*
1349 * Give client code a chance to install their own
1350 * tag extensions & methods, prior to compression overloads,
1351 * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
1352 */
1353 if (tif->tif_nfieldscompat > 0) {
1354 uint32 i;
1355
1356 for (i = 0; i < tif->tif_nfieldscompat; i++) {
1357 if (tif->tif_fieldscompat[i].allocated_size)
1358 _TIFFfree(tif->tif_fieldscompat[i].fields);
1359 }
1360 _TIFFfree(tif->tif_fieldscompat);
1361 tif->tif_nfieldscompat = 0;
1362 tif->tif_fieldscompat = NULL;
1363 }
1364 if (_TIFFextender)
1365 (*_TIFFextender)(tif);
1366 (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1367 /*
1368 * NB: The directory is marked dirty as a result of setting
1369 * up the default compression scheme. However, this really
1370 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1371 * if the user does something. We could just do the setup
1372 * by hand, but it seems better to use the normal mechanism
1373 * (i.e. TIFFSetField).
1374 */
1375 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1376
1377 /*
1378 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1379 * we clear the ISTILED flag when setting up a new directory.
1380 * Should we also be clearing stuff like INSUBIFD?
1381 */
1382 tif->tif_flags &= ~TIFF_ISTILED;
1383
1384 return (1);
1385 }
1386
1387 static int
1388 TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
1389 {
1390 static const char module[] = "TIFFAdvanceDirectory";
1391 if (isMapped(tif))
1392 {
1393 uint64 poff=*nextdir;
1394 if (!(tif->tif_flags&TIFF_BIGTIFF))
1395 {
1396 tmsize_t poffa,poffb,poffc,poffd;
1397 uint16 dircount;
1398 uint32 nextdir32;
1399 poffa=(tmsize_t)poff;
1400 poffb=poffa+sizeof(uint16);
1401 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
1402 {
1403 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1404 *nextdir=0;
1405 return(0);
1406 }
1407 _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
1408 if (tif->tif_flags&TIFF_SWAB)
1409 TIFFSwabShort(&dircount);
1410 poffc=poffb+dircount*12;
1411 poffd=poffc+sizeof(uint32);
1412 if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
1413 {
1414 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1415 return(0);
1416 }
1417 if (off!=NULL)
1418 *off=(uint64)poffc;
1419 _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
1420 if (tif->tif_flags&TIFF_SWAB)
1421 TIFFSwabLong(&nextdir32);
1422 *nextdir=nextdir32;
1423 }
1424 else
1425 {
1426 tmsize_t poffa,poffb,poffc,poffd;
1427 uint64 dircount64;
1428 uint16 dircount16;
1429 poffa=(tmsize_t)poff;
1430 poffb=poffa+sizeof(uint64);
1431 if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
1432 {
1433 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
1434 return(0);
1435 }
1436 _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
1437 if (tif->tif_flags&TIFF_SWAB)
1438 TIFFSwabLong8(&dircount64);
1439 if (dircount64>0xFFFF)
1440 {
1441 TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
1442 return(0);
1443 }
1444 dircount16=(uint16)dircount64;
1445 poffc=poffb+dircount16*20;
1446 poffd=poffc+sizeof(uint64);
1447 if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
1448 {
1449 TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
1450 return(0);
1451 }
1452 if (off!=NULL)
1453 *off=(uint64)poffc;
1454 _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
1455 if (tif->tif_flags&TIFF_SWAB)
1456 TIFFSwabLong8(nextdir);
1457 }
1458 return(1);
1459 }
1460 else
1461 {
1462 if (!(tif->tif_flags&TIFF_BIGTIFF))
1463 {
1464 uint16 dircount;
1465 uint32 nextdir32;
1466 if (!SeekOK(tif, *nextdir) ||
1467 !ReadOK(tif, &dircount, sizeof (uint16))) {
1468 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1469 tif->tif_name);
1470 return (0);
1471 }
1472 if (tif->tif_flags & TIFF_SWAB)
1473 TIFFSwabShort(&dircount);
1474 if (off != NULL)
1475 *off = TIFFSeekFile(tif,
1476 dircount*12, SEEK_CUR);
1477 else
1478 (void) TIFFSeekFile(tif,
1479 dircount*12, SEEK_CUR);
1480 if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
1481 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
1482 tif->tif_name);
1483 return (0);
1484 }
1485 if (tif->tif_flags & TIFF_SWAB)
1486 TIFFSwabLong(&nextdir32);
1487 *nextdir=nextdir32;
1488 }
1489 else
1490 {
1491 uint64 dircount64;
1492 uint16 dircount16;
1493 if (!SeekOK(tif, *nextdir) ||
1494 !ReadOK(tif, &dircount64, sizeof (uint64))) {
1495 TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
1496 tif->tif_name);
1497 return (0);
1498 }
1499 if (tif->tif_flags & TIFF_SWAB)
1500 TIFFSwabLong8(&dircount64);
1501 if (dircount64>0xFFFF)
1502 {
1503 TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
1504 return(0);
1505 }
1506 dircount16 = (uint16)dircount64;
1507 if (off != NULL)
1508 *off = TIFFSeekFile(tif,
1509 dircount16*20, SEEK_CUR);
1510 else
1511 (void) TIFFSeekFile(tif,
1512 dircount16*20, SEEK_CUR);
1513 if (!ReadOK(tif, nextdir, sizeof (uint64))) {
1514 TIFFErrorExt(tif->tif_clientdata, module,
1515 "%s: Error fetching directory link",
1516 tif->tif_name);
1517 return (0);
1518 }
1519 if (tif->tif_flags & TIFF_SWAB)
1520 TIFFSwabLong8(nextdir);
1521 }
1522 return (1);
1523 }
1524 }
1525
1526 /*
1527 * Count the number of directories in a file.
1528 */
1529 uint16
1530 TIFFNumberOfDirectories(TIFF* tif)
1531 {
1532 static const char module[] = "TIFFNumberOfDirectories";
1533 uint64 nextdir;
1534 uint16 n;
1535 if (!(tif->tif_flags&TIFF_BIGTIFF))
1536 nextdir = tif->tif_header.classic.tiff_diroff;
1537 else
1538 nextdir = tif->tif_header.big.tiff_diroff;
1539 n = 0;
1540 while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1541 {
1542 if (n != 65535) {
1543 ++n;
1544 }
1545 else
1546 {
1547 TIFFErrorExt(tif->tif_clientdata, module,
1548 "Directory count exceeded 65535 limit,"
1549 " giving up on counting.");
1550 return (65535);
1551 }
1552 }
1553 return (n);
1554 }
1555
1556 /*
1557 * Set the n-th directory as the current directory.
1558 * NB: Directories are numbered starting at 0.
1559 */
1560 int
1561 TIFFSetDirectory(TIFF* tif, uint16 dirn)
1562 {
1563 uint64 nextdir;
1564 uint16 n;
1565
1566 if (!(tif->tif_flags&TIFF_BIGTIFF))
1567 nextdir = tif->tif_header.classic.tiff_diroff;
1568 else
1569 nextdir = tif->tif_header.big.tiff_diroff;
1570 for (n = dirn; n > 0 && nextdir != 0; n--)
1571 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1572 return (0);
1573 tif->tif_nextdiroff = nextdir;
1574 /*
1575 * Set curdir to the actual directory index. The
1576 * -1 is because TIFFReadDirectory will increment
1577 * tif_curdir after successfully reading the directory.
1578 */
1579 tif->tif_curdir = (dirn - n) - 1;
1580 /*
1581 * Reset tif_dirnumber counter and start new list of seen directories.
1582 * We need this to prevent IFD loops.
1583 */
1584 tif->tif_dirnumber = 0;
1585 return (TIFFReadDirectory(tif));
1586 }
1587
1588 /*
1589 * Set the current directory to be the directory
1590 * located at the specified file offset. This interface
1591 * is used mainly to access directories linked with
1592 * the SubIFD tag (e.g. thumbnail images).
1593 */
1594 int
1595 TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
1596 {
1597 tif->tif_nextdiroff = diroff;
1598 /*
1599 * Reset tif_dirnumber counter and start new list of seen directories.
1600 * We need this to prevent IFD loops.
1601 */
1602 tif->tif_dirnumber = 0;
1603 return (TIFFReadDirectory(tif));
1604 }
1605
1606 /*
1607 * Return file offset of the current directory.
1608 */
1609 uint64
1610 TIFFCurrentDirOffset(TIFF* tif)
1611 {
1612 return (tif->tif_diroff);
1613 }
1614
1615 /*
1616 * Return an indication of whether or not we are
1617 * at the last directory in the file.
1618 */
1619 int
1620 TIFFLastDirectory(TIFF* tif)
1621 {
1622 return (tif->tif_nextdiroff == 0);
1623 }
1624
1625 /*
1626 * Unlink the specified directory from the directory chain.
1627 */
1628 int
1629 TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
1630 {
1631 static const char module[] = "TIFFUnlinkDirectory";
1632 uint64 nextdir;
1633 uint64 off;
1634 uint16 n;
1635
1636 if (tif->tif_mode == O_RDONLY) {
1637 TIFFErrorExt(tif->tif_clientdata, module,
1638 "Can not unlink directory in read-only file");
1639 return (0);
1640 }
1641 /*
1642 * Go to the directory before the one we want
1643 * to unlink and nab the offset of the link
1644 * field we'll need to patch.
1645 */
1646 if (!(tif->tif_flags&TIFF_BIGTIFF))
1647 {
1648 nextdir = tif->tif_header.classic.tiff_diroff;
1649 off = 4;
1650 }
1651 else
1652 {
1653 nextdir = tif->tif_header.big.tiff_diroff;
1654 off = 8;
1655 }
1656 for (n = dirn-1; n > 0; n--) {
1657 if (nextdir == 0) {
1658 TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
1659 return (0);
1660 }
1661 if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1662 return (0);
1663 }
1664 /*
1665 * Advance to the directory to be unlinked and fetch
1666 * the offset of the directory that follows.
1667 */
1668 if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1669 return (0);
1670 /*
1671 * Go back and patch the link field of the preceding
1672 * directory to point to the offset of the directory
1673 * that follows.
1674 */
1675 (void) TIFFSeekFile(tif, off, SEEK_SET);
1676 if (!(tif->tif_flags&TIFF_BIGTIFF))
1677 {
1678 uint32 nextdir32;
1679 nextdir32=(uint32)nextdir;
1680 assert((uint64)nextdir32==nextdir);
1681 if (tif->tif_flags & TIFF_SWAB)
1682 TIFFSwabLong(&nextdir32);
1683 if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
1684 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1685 return (0);
1686 }
1687 }
1688 else
1689 {
1690 if (tif->tif_flags & TIFF_SWAB)
1691 TIFFSwabLong8(&nextdir);
1692 if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
1693 TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
1694 return (0);
1695 }
1696 }
1697 /*
1698 * Leave directory state setup safely. We don't have
1699 * facilities for doing inserting and removing directories,
1700 * so it's safest to just invalidate everything. This
1701 * means that the caller can only append to the directory
1702 * chain.
1703 */
1704 (*tif->tif_cleanup)(tif);
1705 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1706 _TIFFfree(tif->tif_rawdata);
1707 tif->tif_rawdata = NULL;
1708 tif->tif_rawcc = 0;
1709 tif->tif_rawdataoff = 0;
1710 tif->tif_rawdataloaded = 0;
1711 }
1712 tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
1713 TIFFFreeDirectory(tif);
1714 TIFFDefaultDirectory(tif);
1715 tif->tif_diroff = 0; /* force link on next write */
1716 tif->tif_nextdiroff = 0; /* next write must be at end */
1717 tif->tif_curoff = 0;
1718 tif->tif_row = (uint32) -1;
1719 tif->tif_curstrip = (uint32) -1;
1720 return (1);
1721 }
1722
1723 /* vim: set ts=8 sts=8 sw=8 noet: */
1724 /*
1725 * Local Variables:
1726 * mode: c
1727 * c-basic-offset: 8
1728 * fill-column: 78
1729 * End:
1730 */