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