1/* PDFlib GmbH cvsid: $Id: tif_dir.c 14574 2005-10-29 16:27:43Z bonefish $ */
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#include "tiffiop.h"
34
35/*
36 * These are used in the backwards compatibility code...
37 */
38#define DATATYPE_VOID		0       /* !untyped data */
39#define DATATYPE_INT		1       /* !signed integer data */
40#define DATATYPE_UINT		2       /* !unsigned integer data */
41#define DATATYPE_IEEEFP		3       /* !IEEE floating point data */
42
43void _TIFFsetNString(TIFF* tif, char** cpp, char* cp, long n);
44
45void
46_TIFFsetByteArray(TIFF* tif, void** vpp, void* vp, long n)
47{
48	if (*vpp)
49		_TIFFfree(tif, *vpp), *vpp = 0;
50	if (vp && (*vpp = (void*) _TIFFmalloc(tif, n)))
51		_TIFFmemcpy(*vpp, vp, n);
52}
53void _TIFFsetString(TIFF* tif, char** cpp, char* cp)
54    { _TIFFsetByteArray(tif, (void**) cpp, (void*) cp, (long) (strlen(cp)+1)); }
55void _TIFFsetNString(TIFF* tif, char** cpp, char* cp, long n)
56    { _TIFFsetByteArray(tif, (void**) cpp, (void*) cp, n); }
57void _TIFFsetShortArray(TIFF* tif, uint16** wpp, uint16* wp, long n)
58    { _TIFFsetByteArray(tif, (void**) wpp, (void*) wp, n*sizeof (uint16)); }
59void _TIFFsetLongArray(TIFF* tif, uint32** lpp, uint32* lp, long n)
60    { _TIFFsetByteArray(tif, (void**) lpp, (void*) lp, n*sizeof (uint32)); }
61void _TIFFsetFloatArray(TIFF* tif, float** fpp, float* fp, long n)
62    { _TIFFsetByteArray(tif, (void**) fpp, (void*) fp, n*sizeof (float)); }
63void _TIFFsetDoubleArray(TIFF* tif, double** dpp, double* dp, long n)
64    { _TIFFsetByteArray(tif, (void**) dpp, (void*) dp, n*sizeof (double)); }
65
66/*
67 * Install extra samples information.
68 */
69static int
70setExtraSamples(TIFF* tif, TIFFDirectory* td, va_list ap, int* v)
71{
72	uint16* va;
73	int i;
74
75	*v = va_arg(ap, int);
76	if ((uint16) *v > td->td_samplesperpixel)
77		return (0);
78	va = va_arg(ap, uint16*);
79	if (*v > 0 && va == NULL)		/* typically missing param */
80		return (0);
81	for (i = 0; i < *v; i++)
82		if (va[i] > EXTRASAMPLE_UNASSALPHA)
83			return (0);
84	td->td_extrasamples = (uint16) *v;
85	_TIFFsetShortArray(tif, &td->td_sampleinfo, va, td->td_extrasamples);
86	return (1);
87}
88
89#ifdef CMYK_SUPPORT
90static int
91checkInkNamesString(TIFF* tif, int slen, const char* s)
92{
93	TIFFDirectory* td = &tif->tif_dir;
94	int i = td->td_samplesperpixel;
95
96	if (slen > 0) {
97		const char* ep = s+slen;
98		const char* cp = s;
99		for (; i > 0; i--) {
100			for (; *cp != '\0'; cp++)
101				if (cp >= ep)
102					goto bad;
103			cp++;				/* skip \0 */
104		}
105		return (cp-s);
106	}
107bad:
108	TIFFError("TIFFSetField",
109	    "%s: Invalid InkNames value; expecting %d names, found %d",
110	    tif->tif_name,
111	    td->td_samplesperpixel,
112	    td->td_samplesperpixel-i);
113	return (0);
114}
115#endif
116
117static int
118_TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
119{
120	TIFFDirectory* td = &tif->tif_dir;
121	int status = 1;
122	uint32 v32;
123	int i, v;
124	double d;
125	char* s;
126
127	switch (tag) {
128	case TIFFTAG_SUBFILETYPE:
129		td->td_subfiletype = va_arg(ap, uint32);
130		break;
131	case TIFFTAG_IMAGEWIDTH:
132		td->td_imagewidth = va_arg(ap, uint32);
133		break;
134	case TIFFTAG_IMAGELENGTH:
135		td->td_imagelength = va_arg(ap, uint32);
136		break;
137	case TIFFTAG_BITSPERSAMPLE:
138		td->td_bitspersample = (uint16) va_arg(ap, int);
139		/*
140		 * If the data require post-decoding processing
141		 * to byte-swap samples, set it up here.  Note
142		 * that since tags are required to be ordered,
143		 * compression code can override this behaviour
144		 * in the setup method if it wants to roll the
145		 * post decoding work in with its normal work.
146		 */
147		if (tif->tif_flags & TIFF_SWAB) {
148			if (td->td_bitspersample == 16)
149				tif->tif_postdecode = _TIFFSwab16BitData;
150			else if (td->td_bitspersample == 32)
151				tif->tif_postdecode = _TIFFSwab32BitData;
152			else if (td->td_bitspersample == 64)
153				tif->tif_postdecode = _TIFFSwab64BitData;
154		}
155		break;
156	case TIFFTAG_COMPRESSION:
157		v = va_arg(ap, int) & 0xffff;
158		/*
159		 * If we're changing the compression scheme,
160		 * the notify the previous module so that it
161		 * can cleanup any state it's setup.
162		 */
163		if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
164			if (td->td_compression == v)
165				break;
166			(*tif->tif_cleanup)(tif);
167			tif->tif_flags &= ~TIFF_CODERSETUP;
168		}
169		/*
170		 * Setup new compression routine state.
171		 */
172		if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
173                    td->td_compression = v;
174                else
175                    status = 0;
176		break;
177	case TIFFTAG_PHOTOMETRIC:
178		td->td_photometric = (uint16) va_arg(ap, int);
179		break;
180	case TIFFTAG_THRESHHOLDING:
181		td->td_threshholding = (uint16) va_arg(ap, int);
182		break;
183	case TIFFTAG_FILLORDER:
184		v = va_arg(ap, int);
185		if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
186			goto badvalue;
187		td->td_fillorder = (uint16) v;
188		break;
189	case TIFFTAG_DOCUMENTNAME:
190		_TIFFsetString(tif, &td->td_documentname, va_arg(ap, char*));
191		break;
192	case TIFFTAG_ARTIST:
193		_TIFFsetString(tif, &td->td_artist, va_arg(ap, char*));
194		break;
195	case TIFFTAG_DATETIME:
196		_TIFFsetString(tif, &td->td_datetime, va_arg(ap, char*));
197		break;
198	case TIFFTAG_HOSTCOMPUTER:
199		_TIFFsetString(tif, &td->td_hostcomputer, va_arg(ap, char*));
200		break;
201	case TIFFTAG_IMAGEDESCRIPTION:
202		_TIFFsetString(tif, &td->td_imagedescription,va_arg(ap, char*));
203		break;
204	case TIFFTAG_MAKE:
205		_TIFFsetString(tif, &td->td_make, va_arg(ap, char*));
206		break;
207	case TIFFTAG_MODEL:
208		_TIFFsetString(tif, &td->td_model, va_arg(ap, char*));
209		break;
210	case TIFFTAG_SOFTWARE:
211		_TIFFsetString(tif, &td->td_software, va_arg(ap, char*));
212		break;
213	case TIFFTAG_COPYRIGHT:
214		_TIFFsetString(tif, &td->td_copyright, va_arg(ap, char*));
215		break;
216	case TIFFTAG_ORIENTATION:
217		v = va_arg(ap, int);
218		if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
219			TIFFWarning(tif->tif_name,
220			    "Bad value %ld for \"%s\" tag ignored",
221			    v, _TIFFFieldWithTag(tif, tag)->field_name);
222		} else
223			td->td_orientation = (uint16) v;
224		break;
225	case TIFFTAG_SAMPLESPERPIXEL:
226		/* XXX should cross check -- e.g. if pallette, then 1 */
227		v = va_arg(ap, int);
228		if (v == 0)
229			goto badvalue;
230		td->td_samplesperpixel = (uint16) v;
231		break;
232	case TIFFTAG_ROWSPERSTRIP:
233		v32 = va_arg(ap, uint32);
234		if (v32 == 0)
235			goto badvalue32;
236		td->td_rowsperstrip = v32;
237		if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
238			td->td_tilelength = v32;
239			td->td_tilewidth = td->td_imagewidth;
240		}
241		break;
242	case TIFFTAG_MINSAMPLEVALUE:
243		td->td_minsamplevalue = (uint16) va_arg(ap, int);
244		break;
245	case TIFFTAG_MAXSAMPLEVALUE:
246		td->td_maxsamplevalue = (uint16) va_arg(ap, int);
247		break;
248	case TIFFTAG_SMINSAMPLEVALUE:
249		td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t);
250		break;
251	case TIFFTAG_SMAXSAMPLEVALUE:
252		td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t);
253		break;
254	case TIFFTAG_XRESOLUTION:
255		td->td_xresolution = (float) va_arg(ap, dblparam_t);
256		break;
257	case TIFFTAG_YRESOLUTION:
258		td->td_yresolution = (float) va_arg(ap, dblparam_t);
259		break;
260	case TIFFTAG_PLANARCONFIG:
261		v = va_arg(ap, int);
262		if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
263			goto badvalue;
264		td->td_planarconfig = (uint16) v;
265		break;
266	case TIFFTAG_PAGENAME:
267		_TIFFsetString(tif, &td->td_pagename, va_arg(ap, char*));
268		break;
269	case TIFFTAG_XPOSITION:
270		td->td_xposition = (float) va_arg(ap, dblparam_t);
271		break;
272	case TIFFTAG_YPOSITION:
273		td->td_yposition = (float) va_arg(ap, dblparam_t);
274		break;
275	case TIFFTAG_RESOLUTIONUNIT:
276		v = va_arg(ap, int);
277		if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
278			goto badvalue;
279		td->td_resolutionunit = (uint16) v;
280		break;
281	case TIFFTAG_PAGENUMBER:
282		td->td_pagenumber[0] = (uint16) va_arg(ap, int);
283		td->td_pagenumber[1] = (uint16) va_arg(ap, int);
284		break;
285	case TIFFTAG_HALFTONEHINTS:
286		td->td_halftonehints[0] = (uint16) va_arg(ap, int);
287		td->td_halftonehints[1] = (uint16) va_arg(ap, int);
288		break;
289	case TIFFTAG_COLORMAP:
290		v32 = (uint32)(1L<<td->td_bitspersample);
291		_TIFFsetShortArray(tif, &td->td_colormap[0],
292		    va_arg(ap, uint16*),v32);
293		_TIFFsetShortArray(tif, &td->td_colormap[1],
294		    va_arg(ap, uint16*),v32);
295		_TIFFsetShortArray(tif, &td->td_colormap[2],
296		    va_arg(ap, uint16*),v32);
297		break;
298	case TIFFTAG_EXTRASAMPLES:
299		if (!setExtraSamples(tif, td, ap, &v))
300			goto badvalue;
301		break;
302	case TIFFTAG_MATTEING:
303		td->td_extrasamples = (uint16) (va_arg(ap, int) != 0);
304		if (td->td_extrasamples) {
305			uint16 sv = EXTRASAMPLE_ASSOCALPHA;
306			_TIFFsetShortArray(tif, &td->td_sampleinfo, &sv, 1);
307		}
308		break;
309	case TIFFTAG_TILEWIDTH:
310		v32 = va_arg(ap, uint32);
311		if (v32 % 16) {
312			if (tif->tif_mode != O_RDONLY)
313				goto badvalue32;
314			TIFFWarning(tif->tif_name,
315			    "Nonstandard tile width %d, convert file", v32);
316		}
317		td->td_tilewidth = v32;
318		tif->tif_flags |= TIFF_ISTILED;
319		break;
320	case TIFFTAG_TILELENGTH:
321		v32 = va_arg(ap, uint32);
322		if (v32 % 16) {
323			if (tif->tif_mode != O_RDONLY)
324				goto badvalue32;
325			TIFFWarning(tif->tif_name,
326			    "Nonstandard tile length %d, convert file", v32);
327		}
328		td->td_tilelength = v32;
329		tif->tif_flags |= TIFF_ISTILED;
330		break;
331	case TIFFTAG_TILEDEPTH:
332		v32 = va_arg(ap, uint32);
333		if (v32 == 0)
334			goto badvalue32;
335		td->td_tiledepth = v32;
336		break;
337	case TIFFTAG_DATATYPE:
338		v = va_arg(ap, int);
339		switch (v) {
340		case DATATYPE_VOID:	v = SAMPLEFORMAT_VOID;	break;
341		case DATATYPE_INT:	v = SAMPLEFORMAT_INT;	break;
342		case DATATYPE_UINT:	v = SAMPLEFORMAT_UINT;	break;
343		case DATATYPE_IEEEFP:	v = SAMPLEFORMAT_IEEEFP;break;
344		default:		goto badvalue;
345		}
346		td->td_sampleformat = (uint16) v;
347		break;
348	case TIFFTAG_SAMPLEFORMAT:
349		v = va_arg(ap, int);
350		if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
351			goto badvalue;
352		td->td_sampleformat = (uint16) v;
353		break;
354	case TIFFTAG_IMAGEDEPTH:
355		td->td_imagedepth = va_arg(ap, uint32);
356		break;
357	case TIFFTAG_STONITS:
358		d = va_arg(ap, dblparam_t);
359		if (d <= 0.)
360			goto badvaluedbl;
361		td->td_stonits = d;
362		break;
363
364	/* Begin Pixar Tags */
365 	case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
366 		td->td_imagefullwidth = va_arg(ap, uint32);
367 		break;
368 	case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
369 		td->td_imagefulllength = va_arg(ap, uint32);
370 		break;
371 	case TIFFTAG_PIXAR_TEXTUREFORMAT:
372 		_TIFFsetString(tif, &td->td_textureformat, va_arg(ap, char*));
373 		break;
374 	case TIFFTAG_PIXAR_WRAPMODES:
375 		_TIFFsetString(tif, &td->td_wrapmodes, va_arg(ap, char*));
376 		break;
377 	case TIFFTAG_PIXAR_FOVCOT:
378 		td->td_fovcot = (float) va_arg(ap, dblparam_t);
379 		break;
380 	case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
381 		_TIFFsetFloatArray(tif, &td->td_matrixWorldToScreen,
382 			va_arg(ap, float*), 16);
383 		break;
384 	case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
385 		_TIFFsetFloatArray(tif, &td->td_matrixWorldToCamera,
386 			va_arg(ap, float*), 16);
387 		break;
388 	/* End Pixar Tags */
389
390#if SUBIFD_SUPPORT
391	case TIFFTAG_SUBIFD:
392		if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
393			td->td_nsubifd = (uint16) va_arg(ap, int);
394			_TIFFsetLongArray(tif,&td->td_subifd,va_arg(ap,uint32*),
395			    (long) td->td_nsubifd);
396		} else {
397			TIFFError(tif->tif_name, "Sorry, cannot nest SubIFDs");
398			status = 0;
399		}
400		break;
401#endif
402#ifdef YCBCR_SUPPORT
403	case TIFFTAG_YCBCRCOEFFICIENTS:
404		_TIFFsetFloatArray(tif,&td->td_ycbcrcoeffs,va_arg(ap,float*),3);
405		break;
406	case TIFFTAG_YCBCRPOSITIONING:
407		td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
408		break;
409	case TIFFTAG_YCBCRSUBSAMPLING:
410		td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
411		td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
412		break;
413#endif
414#ifdef COLORIMETRY_SUPPORT
415	case TIFFTAG_WHITEPOINT:
416		_TIFFsetFloatArray(tif,&td->td_whitepoint,va_arg(ap,float*),2);
417		break;
418	case TIFFTAG_PRIMARYCHROMATICITIES:
419		_TIFFsetFloatArray(tif,&td->td_primarychromas,
420		    va_arg(ap,float*),6);
421		break;
422	case TIFFTAG_TRANSFERFUNCTION:
423		v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
424		for (i = 0; i < v; i++)
425			_TIFFsetShortArray(tif, &td->td_transferfunction[i],
426			    va_arg(ap, uint16*), 1L<<td->td_bitspersample);
427		break;
428	case TIFFTAG_REFERENCEBLACKWHITE:
429		/* XXX should check for null range */
430		_TIFFsetFloatArray(tif, &td->td_refblackwhite,
431		    va_arg(ap, float*),6);
432		break;
433#endif
434#ifdef CMYK_SUPPORT
435	case TIFFTAG_INKSET:
436		td->td_inkset = (uint16) va_arg(ap, int);
437		break;
438	case TIFFTAG_DOTRANGE:
439		/* XXX should check for null range */
440		td->td_dotrange[0] = (uint16) va_arg(ap, int);
441		td->td_dotrange[1] = (uint16) va_arg(ap, int);
442		break;
443	case TIFFTAG_INKNAMES:
444		i = va_arg(ap, int);
445		s = va_arg(ap, char*);
446		i = checkInkNamesString(tif, i, s);
447                status = i > 0;
448		if( i > 0 ) {
449			_TIFFsetNString(tif, &td->td_inknames, s, i);
450			td->td_inknameslen = i;
451		}
452		break;
453	case TIFFTAG_NUMBEROFINKS:
454		td->td_ninks = (uint16) va_arg(ap, int);
455		break;
456	case TIFFTAG_TARGETPRINTER:
457		_TIFFsetString(tif, &td->td_targetprinter, va_arg(ap, char*));
458		break;
459#endif
460#ifdef ICC_SUPPORT
461	case TIFFTAG_ICCPROFILE:
462		td->td_profileLength = (uint32) va_arg(ap, uint32);
463		_TIFFsetByteArray(tif, &td->td_profileData, va_arg(ap, void*),
464		    td->td_profileLength);
465		break;
466#endif
467#ifdef PHOTOSHOP_SUPPORT
468 	case TIFFTAG_PHOTOSHOP:
469  		td->td_photoshopLength = (uint32) va_arg(ap, uint32);
470  		_TIFFsetByteArray(tif, &td->td_photoshopData, va_arg(ap, void*),
471 			td->td_photoshopLength);
472 		break;
473#endif
474#ifdef IPTC_SUPPORT
475    case TIFFTAG_RICHTIFFIPTC:
476  		td->td_richtiffiptcLength = (uint32) va_arg(ap, uint32);
477#ifdef PHOTOSHOP_SUPPORT
478  		_TIFFsetLongArray (tif, (uint32**)&td->td_richtiffiptcData,
479			va_arg(ap, uint32*),
480 			td->td_richtiffiptcLength);
481#else
482  		_TIFFsetByteArray(tif, &td->td_photoshopData, va_arg(ap, void*),
483 			td->td_photoshopLength);
484#endif
485 		break;
486#endif
487	default:
488		/*
489		 * This can happen if multiple images are open with
490		 * different codecs which have private tags.  The
491		 * global tag information table may then have tags
492		 * that are valid for one file but not the other.
493		 * If the client tries to set a tag that is not valid
494		 * for the image's codec then we'll arrive here.  This
495		 * happens, for example, when tiffcp is used to convert
496		 * between compression schemes and codec-specific tags
497		 * are blindly copied.
498		 */
499		TIFFError("TIFFSetField",
500		    "%s: Invalid %stag \"%s\" (not supported by codec)",
501		    tif->tif_name, isPseudoTag(tag) ? "pseduo-" : "",
502		    _TIFFFieldWithTag(tif, tag)->field_name);
503		status = 0;
504		break;
505	}
506	if (status) {
507		TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
508		tif->tif_flags |= TIFF_DIRTYDIRECT;
509	}
510	va_end(ap);
511	return (status);
512badvalue:
513	TIFFError(tif->tif_name, "%d: Bad value for \"%s\"", v,
514	    _TIFFFieldWithTag(tif, tag)->field_name);
515	va_end(ap);
516	return (0);
517badvalue32:
518	TIFFError(tif->tif_name, "%ld: Bad value for \"%s\"", v32,
519	    _TIFFFieldWithTag(tif, tag)->field_name);
520	va_end(ap);
521	return (0);
522badvaluedbl:
523	TIFFError(tif->tif_name, "%f: Bad value for \"%s\"", d,
524	    _TIFFFieldWithTag(tif, tag)->field_name);
525	va_end(ap);
526	return (0);
527}
528
529/*
530 * Return 1/0 according to whether or not
531 * it is permissible to set the tag's value.
532 * Note that we allow ImageLength to be changed
533 * so that we can append and extend to images.
534 * Any other tag may not be altered once writing
535 * has commenced, unless its value has no effect
536 * on the format of the data that is written.
537 */
538static int
539OkToChangeTag(TIFF* tif, ttag_t tag)
540{
541	const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
542	if (!fip) {			/* unknown tag */
543		TIFFError("TIFFSetField", "%s: Unknown %stag %u",
544		    tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
545		return (0);
546	}
547	if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
548	    !fip->field_oktochange) {
549		/*
550		 * Consult info table to see if tag can be changed
551		 * after we've started writing.  We only allow changes
552		 * to those tags that don't/shouldn't affect the
553		 * compression and/or format of the data.
554		 */
555		TIFFError("TIFFSetField",
556		    "%s: Cannot modify tag \"%s\" while writing",
557		    tif->tif_name, fip->field_name);
558		return (0);
559	}
560	return (1);
561}
562
563/*
564 * Record the value of a field in the
565 * internal directory structure.  The
566 * field will be written to the file
567 * when/if the directory structure is
568 * updated.
569 */
570int
571TIFFSetField(TIFF* tif, ttag_t tag, ...)
572{
573	va_list ap;
574	int status;
575
576	va_start(ap, tag);
577	status = TIFFVSetField(tif, tag, ap);
578	va_end(ap);
579	return (status);
580}
581
582/*
583 * Like TIFFSetField, but taking a varargs
584 * parameter list.  This routine is useful
585 * for building higher-level interfaces on
586 * top of the library.
587 */
588int
589TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
590{
591	return OkToChangeTag(tif, tag) ?
592	    (*tif->tif_vsetfield)(tif, tag, ap) : 0;
593}
594
595static int
596_TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
597{
598	TIFFDirectory* td = &tif->tif_dir;
599
600	switch (tag) {
601	case TIFFTAG_SUBFILETYPE:
602		*va_arg(ap, uint32*) = td->td_subfiletype;
603		break;
604	case TIFFTAG_IMAGEWIDTH:
605		*va_arg(ap, uint32*) = td->td_imagewidth;
606		break;
607	case TIFFTAG_IMAGELENGTH:
608		*va_arg(ap, uint32*) = td->td_imagelength;
609		break;
610	case TIFFTAG_BITSPERSAMPLE:
611		*va_arg(ap, uint16*) = td->td_bitspersample;
612		break;
613	case TIFFTAG_COMPRESSION:
614		*va_arg(ap, uint16*) = td->td_compression;
615		break;
616	case TIFFTAG_PHOTOMETRIC:
617		*va_arg(ap, uint16*) = td->td_photometric;
618		break;
619	case TIFFTAG_THRESHHOLDING:
620		*va_arg(ap, uint16*) = td->td_threshholding;
621		break;
622	case TIFFTAG_FILLORDER:
623		*va_arg(ap, uint16*) = td->td_fillorder;
624		break;
625	case TIFFTAG_DOCUMENTNAME:
626		*va_arg(ap, char**) = td->td_documentname;
627		break;
628	case TIFFTAG_ARTIST:
629		*va_arg(ap, char**) = td->td_artist;
630		break;
631	case TIFFTAG_DATETIME:
632		*va_arg(ap, char**) = td->td_datetime;
633		break;
634	case TIFFTAG_HOSTCOMPUTER:
635		*va_arg(ap, char**) = td->td_hostcomputer;
636		break;
637	case TIFFTAG_IMAGEDESCRIPTION:
638		*va_arg(ap, char**) = td->td_imagedescription;
639		break;
640	case TIFFTAG_MAKE:
641		*va_arg(ap, char**) = td->td_make;
642		break;
643	case TIFFTAG_MODEL:
644		*va_arg(ap, char**) = td->td_model;
645		break;
646	case TIFFTAG_SOFTWARE:
647		*va_arg(ap, char**) = td->td_software;
648		break;
649	case TIFFTAG_COPYRIGHT:
650		*va_arg(ap, char**) = td->td_copyright;
651		break;
652	case TIFFTAG_ORIENTATION:
653		*va_arg(ap, uint16*) = td->td_orientation;
654		break;
655	case TIFFTAG_SAMPLESPERPIXEL:
656		*va_arg(ap, uint16*) = td->td_samplesperpixel;
657		break;
658	case TIFFTAG_ROWSPERSTRIP:
659		*va_arg(ap, uint32*) = td->td_rowsperstrip;
660		break;
661	case TIFFTAG_MINSAMPLEVALUE:
662		*va_arg(ap, uint16*) = td->td_minsamplevalue;
663		break;
664	case TIFFTAG_MAXSAMPLEVALUE:
665		*va_arg(ap, uint16*) = td->td_maxsamplevalue;
666		break;
667	case TIFFTAG_SMINSAMPLEVALUE:
668		*va_arg(ap, double*) = td->td_sminsamplevalue;
669		break;
670	case TIFFTAG_SMAXSAMPLEVALUE:
671		*va_arg(ap, double*) = td->td_smaxsamplevalue;
672		break;
673	case TIFFTAG_XRESOLUTION:
674		*va_arg(ap, float*) = td->td_xresolution;
675		break;
676	case TIFFTAG_YRESOLUTION:
677		*va_arg(ap, float*) = td->td_yresolution;
678		break;
679	case TIFFTAG_PLANARCONFIG:
680		*va_arg(ap, uint16*) = td->td_planarconfig;
681		break;
682	case TIFFTAG_XPOSITION:
683		*va_arg(ap, float*) = td->td_xposition;
684		break;
685	case TIFFTAG_YPOSITION:
686		*va_arg(ap, float*) = td->td_yposition;
687		break;
688	case TIFFTAG_PAGENAME:
689		*va_arg(ap, char**) = td->td_pagename;
690		break;
691	case TIFFTAG_RESOLUTIONUNIT:
692		*va_arg(ap, uint16*) = td->td_resolutionunit;
693		break;
694	case TIFFTAG_PAGENUMBER:
695		*va_arg(ap, uint16*) = td->td_pagenumber[0];
696		*va_arg(ap, uint16*) = td->td_pagenumber[1];
697		break;
698	case TIFFTAG_HALFTONEHINTS:
699		*va_arg(ap, uint16*) = td->td_halftonehints[0];
700		*va_arg(ap, uint16*) = td->td_halftonehints[1];
701		break;
702	case TIFFTAG_COLORMAP:
703		*va_arg(ap, uint16**) = td->td_colormap[0];
704		*va_arg(ap, uint16**) = td->td_colormap[1];
705		*va_arg(ap, uint16**) = td->td_colormap[2];
706		break;
707	case TIFFTAG_STRIPOFFSETS:
708	case TIFFTAG_TILEOFFSETS:
709		*va_arg(ap, uint32**) = td->td_stripoffset;
710		break;
711	case TIFFTAG_STRIPBYTECOUNTS:
712	case TIFFTAG_TILEBYTECOUNTS:
713		*va_arg(ap, uint32**) = td->td_stripbytecount;
714		break;
715	case TIFFTAG_MATTEING:
716		*va_arg(ap, uint16*) =
717		    (td->td_extrasamples == 1 &&
718		     td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
719		break;
720	case TIFFTAG_EXTRASAMPLES:
721		*va_arg(ap, uint16*) = td->td_extrasamples;
722		*va_arg(ap, uint16**) = td->td_sampleinfo;
723		break;
724	case TIFFTAG_TILEWIDTH:
725		*va_arg(ap, uint32*) = td->td_tilewidth;
726		break;
727	case TIFFTAG_TILELENGTH:
728		*va_arg(ap, uint32*) = td->td_tilelength;
729		break;
730	case TIFFTAG_TILEDEPTH:
731		*va_arg(ap, uint32*) = td->td_tiledepth;
732		break;
733	case TIFFTAG_DATATYPE:
734		switch (td->td_sampleformat) {
735		case SAMPLEFORMAT_UINT:
736			*va_arg(ap, uint16*) = DATATYPE_UINT;
737			break;
738		case SAMPLEFORMAT_INT:
739			*va_arg(ap, uint16*) = DATATYPE_INT;
740			break;
741		case SAMPLEFORMAT_IEEEFP:
742			*va_arg(ap, uint16*) = DATATYPE_IEEEFP;
743			break;
744		case SAMPLEFORMAT_VOID:
745			*va_arg(ap, uint16*) = DATATYPE_VOID;
746			break;
747		}
748		break;
749	case TIFFTAG_SAMPLEFORMAT:
750		*va_arg(ap, uint16*) = td->td_sampleformat;
751		break;
752	case TIFFTAG_IMAGEDEPTH:
753		*va_arg(ap, uint32*) = td->td_imagedepth;
754		break;
755	case TIFFTAG_STONITS:
756		*va_arg(ap, double*) = td->td_stonits;
757		break;
758#if SUBIFD_SUPPORT
759	case TIFFTAG_SUBIFD:
760		*va_arg(ap, uint16*) = td->td_nsubifd;
761		*va_arg(ap, uint32**) = td->td_subifd;
762		break;
763#endif
764#ifdef YCBCR_SUPPORT
765	case TIFFTAG_YCBCRCOEFFICIENTS:
766		*va_arg(ap, float**) = td->td_ycbcrcoeffs;
767		break;
768	case TIFFTAG_YCBCRPOSITIONING:
769		*va_arg(ap, uint16*) = td->td_ycbcrpositioning;
770		break;
771	case TIFFTAG_YCBCRSUBSAMPLING:
772		*va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
773		*va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
774		break;
775#endif
776#ifdef COLORIMETRY_SUPPORT
777	case TIFFTAG_WHITEPOINT:
778		*va_arg(ap, float**) = td->td_whitepoint;
779		break;
780	case TIFFTAG_PRIMARYCHROMATICITIES:
781		*va_arg(ap, float**) = td->td_primarychromas;
782		break;
783	case TIFFTAG_TRANSFERFUNCTION:
784		*va_arg(ap, uint16**) = td->td_transferfunction[0];
785		if (td->td_samplesperpixel - td->td_extrasamples > 1) {
786			*va_arg(ap, uint16**) = td->td_transferfunction[1];
787			*va_arg(ap, uint16**) = td->td_transferfunction[2];
788		}
789		break;
790	case TIFFTAG_REFERENCEBLACKWHITE:
791		*va_arg(ap, float**) = td->td_refblackwhite;
792		break;
793#endif
794#ifdef CMYK_SUPPORT
795	case TIFFTAG_INKSET:
796		*va_arg(ap, uint16*) = td->td_inkset;
797		break;
798	case TIFFTAG_DOTRANGE:
799		*va_arg(ap, uint16*) = td->td_dotrange[0];
800		*va_arg(ap, uint16*) = td->td_dotrange[1];
801		break;
802	case TIFFTAG_INKNAMES:
803		*va_arg(ap, char**) = td->td_inknames;
804		break;
805	case TIFFTAG_NUMBEROFINKS:
806		*va_arg(ap, uint16*) = td->td_ninks;
807		break;
808	case TIFFTAG_TARGETPRINTER:
809		*va_arg(ap, char**) = td->td_targetprinter;
810		break;
811#endif
812#ifdef ICC_SUPPORT
813	case TIFFTAG_ICCPROFILE:
814		*va_arg(ap, uint32*) = td->td_profileLength;
815		*va_arg(ap, void**) = td->td_profileData;
816		break;
817#endif
818#ifdef PHOTOSHOP_SUPPORT
819 	case TIFFTAG_PHOTOSHOP:
820 		*va_arg(ap, uint32*) = td->td_photoshopLength;
821 		*va_arg(ap, void**) = td->td_photoshopData;
822 		break;
823#endif
824#ifdef IPTC_SUPPORT
825 	case TIFFTAG_RICHTIFFIPTC:
826 		*va_arg(ap, uint32*) = td->td_richtiffiptcLength;
827 		*va_arg(ap, void**) = td->td_richtiffiptcData;
828 		break;
829#endif
830 	/* Begin Pixar Tags */
831 	case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
832 		*va_arg(ap, uint32*) = td->td_imagefullwidth;
833 		break;
834 	case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
835 		*va_arg(ap, uint32*) = td->td_imagefulllength;
836 		break;
837 	case TIFFTAG_PIXAR_TEXTUREFORMAT:
838 		*va_arg(ap, char**) = td->td_textureformat;
839 		break;
840 	case TIFFTAG_PIXAR_WRAPMODES:
841 		*va_arg(ap, char**) = td->td_wrapmodes;
842 		break;
843 	case TIFFTAG_PIXAR_FOVCOT:
844 		*va_arg(ap, float*) = td->td_fovcot;
845 		break;
846 	case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
847 		*va_arg(ap, float**) = td->td_matrixWorldToScreen;
848 		break;
849 	case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
850 		*va_arg(ap, float**) = td->td_matrixWorldToCamera;
851 		break;
852 	/* End Pixar Tags */
853
854	default:
855		/*
856		 * This can happen if multiple images are open with
857		 * different codecs which have private tags.  The
858		 * global tag information table may then have tags
859		 * that are valid for one file but not the other.
860		 * If the client tries to get a tag that is not valid
861		 * for the image's codec then we'll arrive here.
862		 */
863		TIFFError("TIFFGetField",
864		    "%s: Invalid %stag \"%s\" (not supported by codec)",
865		    tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
866		    _TIFFFieldWithTag(tif, tag)->field_name);
867		break;
868	}
869	return (1);
870}
871
872/*
873 * Return the value of a field in the
874 * internal directory structure.
875 */
876int
877TIFFGetField(TIFF* tif, ttag_t tag, ...)
878{
879	int status;
880	va_list ap;
881
882	va_start(ap, tag);
883	status = TIFFVGetField(tif, tag, ap);
884	va_end(ap);
885	return (status);
886}
887
888/*
889 * Like TIFFGetField, but taking a varargs
890 * parameter list.  This routine is useful
891 * for building higher-level interfaces on
892 * top of the library.
893 */
894int
895TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
896{
897	const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
898	return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
899	    (*tif->tif_vgetfield)(tif, tag, ap) : 0);
900}
901
902#define	CleanupField(member) {		\
903    if (td->member) {			\
904	_TIFFfree(tif, td->member);		\
905	td->member = 0;			\
906    }					\
907}
908
909/*
910 * Release storage associated with a directory.
911 */
912void
913TIFFFreeDirectory(TIFF* tif)
914{
915	register TIFFDirectory *td = &tif->tif_dir;
916
917	CleanupField(td_colormap[0]);
918	CleanupField(td_colormap[1]);
919	CleanupField(td_colormap[2]);
920	CleanupField(td_documentname);
921	CleanupField(td_artist);
922	CleanupField(td_datetime);
923	CleanupField(td_hostcomputer);
924	CleanupField(td_imagedescription);
925	CleanupField(td_make);
926	CleanupField(td_model);
927	CleanupField(td_software);
928	CleanupField(td_copyright);
929	CleanupField(td_pagename);
930	CleanupField(td_sampleinfo);
931#if SUBIFD_SUPPORT
932	CleanupField(td_subifd);
933#endif
934#ifdef YCBCR_SUPPORT
935	CleanupField(td_ycbcrcoeffs);
936#endif
937#ifdef CMYK_SUPPORT
938	CleanupField(td_inknames);
939	CleanupField(td_targetprinter);
940#endif
941#ifdef COLORIMETRY_SUPPORT
942	CleanupField(td_whitepoint);
943	CleanupField(td_primarychromas);
944	CleanupField(td_refblackwhite);
945	CleanupField(td_transferfunction[0]);
946	CleanupField(td_transferfunction[1]);
947	CleanupField(td_transferfunction[2]);
948#endif
949#ifdef ICC_SUPPORT
950	CleanupField(td_profileData);
951#endif
952#ifdef PHOTOSHOP_SUPPORT
953	CleanupField(td_photoshopData);
954#endif
955#ifdef IPTC_SUPPORT
956	CleanupField(td_richtiffiptcData);
957#endif
958	CleanupField(td_stripoffset);
959	CleanupField(td_stripbytecount);
960 	/* Begin Pixar Tags */
961 	CleanupField(td_textureformat);
962 	CleanupField(td_wrapmodes);
963 	CleanupField(td_matrixWorldToScreen);
964 	CleanupField(td_matrixWorldToCamera);
965 	/* End Pixar Tags */
966}
967#undef CleanupField
968
969/*
970 * Client Tag extension support (from Niles Ritter).
971 */
972static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
973
974TIFFExtendProc
975TIFFSetTagExtender(TIFFExtendProc extender)
976{
977	TIFFExtendProc prev = _TIFFextender;
978	_TIFFextender = extender;
979	return (prev);
980}
981
982/* #ifdef PDF_UNUSED */
983/*
984 * Setup for a new directory.  Should we automatically call
985 * TIFFWriteDirectory() if the current one is dirty?
986 *
987 * The newly created directory will not exist on the file till
988 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
989 */
990int
991TIFFCreateDirectory(TIFF* tif)
992{
993    TIFFDefaultDirectory(tif);
994    tif->tif_diroff = 0;
995    tif->tif_nextdiroff = 0;
996    tif->tif_curoff = 0;
997    tif->tif_row = (uint32) -1;
998    tif->tif_curstrip = (tstrip_t) -1;
999
1000    return 0;
1001}
1002/* #endif */
1003
1004/*
1005 * Setup a default directory structure.
1006 */
1007int
1008TIFFDefaultDirectory(TIFF* tif)
1009{
1010	register TIFFDirectory* td = &tif->tif_dir;
1011
1012	_TIFFSetupFieldInfo(tif);
1013	_TIFFmemset(td, 0, sizeof (*td));
1014	td->td_fillorder = FILLORDER_MSB2LSB;
1015	td->td_bitspersample = 1;
1016	td->td_threshholding = THRESHHOLD_BILEVEL;
1017	td->td_orientation = ORIENTATION_TOPLEFT;
1018	td->td_samplesperpixel = 1;
1019	td->td_rowsperstrip = (uint32) -1;
1020	td->td_tilewidth = (uint32) -1;
1021	td->td_tilelength = (uint32) -1;
1022	td->td_tiledepth = 1;
1023	td->td_resolutionunit = RESUNIT_INCH;
1024	td->td_sampleformat = SAMPLEFORMAT_UINT;
1025	td->td_imagedepth = 1;
1026#ifdef YCBCR_SUPPORT
1027	td->td_ycbcrsubsampling[0] = 2;
1028	td->td_ycbcrsubsampling[1] = 2;
1029	td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1030#endif
1031#ifdef CMYK_SUPPORT
1032	td->td_inkset = INKSET_CMYK;
1033	td->td_ninks = 4;
1034#endif
1035	tif->tif_postdecode = _TIFFNoPostDecode;
1036	tif->tif_vsetfield = _TIFFVSetField;
1037	tif->tif_vgetfield = _TIFFVGetField;
1038	tif->tif_printdir = NULL;
1039	/*
1040	 *  Give client code a chance to install their own
1041	 *  tag extensions & methods, prior to compression overloads.
1042	 */
1043	if (_TIFFextender)
1044		(*_TIFFextender)(tif);
1045	(void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1046	/*
1047	 * NB: The directory is marked dirty as a result of setting
1048	 * up the default compression scheme.  However, this really
1049	 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1050	 * if the user does something.  We could just do the setup
1051	 * by hand, but it seems better to use the normal mechanism
1052	 * (i.e. TIFFSetField).
1053	 */
1054	tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1055
1056        /*
1057         * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1058         * we clear the ISTILED flag when setting up a new directory.
1059         * Should we also be clearing stuff like INSUBIFD?
1060         */
1061        tif->tif_flags &= ~TIFF_ISTILED;
1062
1063	return (1);
1064}
1065
1066static int
1067TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
1068{
1069    static const char module[] = "TIFFAdvanceDirectory";
1070    uint16 dircount;
1071    if (isMapped(tif))
1072    {
1073        toff_t poff=*nextdir;
1074        if (poff+sizeof(uint16) > tif->tif_size)
1075        {
1076            TIFFError(module, "%s: Error fetching directory count",
1077                      tif->tif_name);
1078            return (0);
1079        }
1080        _TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
1081        if (tif->tif_flags & TIFF_SWAB)
1082            TIFFSwabShort(&dircount);
1083        poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
1084        if (off != NULL)
1085            *off = poff;
1086        if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
1087        {
1088            TIFFError(module, "%s: Error fetching directory link",
1089                      tif->tif_name);
1090            return (0);
1091        }
1092        _TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
1093        if (tif->tif_flags & TIFF_SWAB)
1094            TIFFSwabLong(nextdir);
1095        return (1);
1096    }
1097    else
1098    {
1099        if (!SeekOK(tif, *nextdir) ||
1100            !ReadOK(tif, &dircount, sizeof (uint16))) {
1101            TIFFError(module, "%s: Error fetching directory count",
1102                      tif->tif_name);
1103            return (0);
1104        }
1105        if (tif->tif_flags & TIFF_SWAB)
1106            TIFFSwabShort(&dircount);
1107        if (off != NULL)
1108            *off = TIFFSeekFile(tif,
1109                                dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1110        else
1111            (void) TIFFSeekFile(tif,
1112                                dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1113        if (!ReadOK(tif, nextdir, sizeof (uint32))) {
1114            TIFFError(module, "%s: Error fetching directory link",
1115                      tif->tif_name);
1116            return (0);
1117        }
1118        if (tif->tif_flags & TIFF_SWAB)
1119            TIFFSwabLong(nextdir);
1120        return (1);
1121    }
1122}
1123
1124#ifdef PDF_UNUSED
1125/*
1126 * Count the number of directories in a file.
1127 */
1128tdir_t
1129TIFFNumberOfDirectories(TIFF* tif)
1130{
1131    toff_t nextdir = tif->tif_header.tiff_diroff;
1132    tdir_t n = 0;
1133
1134    while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1135        n++;
1136    return (n);
1137}
1138#endif
1139
1140/*
1141 * Set the n-th directory as the current directory.
1142 * NB: Directories are numbered starting at 0.
1143 */
1144int
1145TIFFSetDirectory(TIFF* tif, tdir_t dirn)
1146{
1147	toff_t nextdir;
1148	tdir_t n;
1149
1150	nextdir = tif->tif_header.tiff_diroff;
1151	for (n = dirn; n > 0 && nextdir != 0; n--)
1152		if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1153			return (0);
1154	tif->tif_nextdiroff = nextdir;
1155	/*
1156	 * Set curdir to the actual directory index.  The
1157	 * -1 is because TIFFReadDirectory will increment
1158	 * tif_curdir after successfully reading the directory.
1159	 */
1160	tif->tif_curdir = (dirn - n) - 1;
1161	return (TIFFReadDirectory(tif));
1162}
1163
1164#ifdef PDF_UNUSED
1165/*
1166 * Set the current directory to be the directory
1167 * located at the specified file offset.  This interface
1168 * is used mainly to access directories linked with
1169 * the SubIFD tag (e.g. thumbnail images).
1170 */
1171int
1172TIFFSetSubDirectory(TIFF* tif, uint32 diroff)
1173{
1174	tif->tif_nextdiroff = diroff;
1175	return (TIFFReadDirectory(tif));
1176}
1177
1178/*
1179 * Return file offset of the current directory.
1180 */
1181uint32
1182TIFFCurrentDirOffset(TIFF* tif)
1183{
1184	return (tif->tif_diroff);
1185}
1186
1187/*
1188 * Return an indication of whether or not we are
1189 * at the last directory in the file.
1190 */
1191int
1192TIFFLastDirectory(TIFF* tif)
1193{
1194	return (tif->tif_nextdiroff == 0);
1195}
1196
1197/*
1198 * Unlink the specified directory from the directory chain.
1199 */
1200int
1201TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
1202{
1203	static const char module[] = "TIFFUnlinkDirectory";
1204	toff_t nextdir;
1205	toff_t off;
1206	tdir_t n;
1207
1208	if (tif->tif_mode == O_RDONLY) {
1209		TIFFError(module, "Can not unlink directory in read-only file");
1210		return (0);
1211	}
1212	/*
1213	 * Go to the directory before the one we want
1214	 * to unlink and nab the offset of the link
1215	 * field we'll need to patch.
1216	 */
1217	nextdir = tif->tif_header.tiff_diroff;
1218	off = sizeof (uint16) + sizeof (uint16);
1219	for (n = dirn-1; n > 0; n--) {
1220		if (nextdir == 0) {
1221			TIFFError(module, "Directory %d does not exist", dirn);
1222			return (0);
1223		}
1224		if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1225			return (0);
1226	}
1227	/*
1228	 * Advance to the directory to be unlinked and fetch
1229	 * the offset of the directory that follows.
1230	 */
1231	if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1232		return (0);
1233	/*
1234	 * Go back and patch the link field of the preceding
1235	 * directory to point to the offset of the directory
1236	 * that follows.
1237	 */
1238	(void) TIFFSeekFile(tif, off, SEEK_SET);
1239	if (tif->tif_flags & TIFF_SWAB)
1240		TIFFSwabLong(&nextdir);
1241	if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
1242		TIFFError(module, "Error writing directory link");
1243		return (0);
1244	}
1245	/*
1246	 * Leave directory state setup safely.  We don't have
1247	 * facilities for doing inserting and removing directories,
1248	 * so it's safest to just invalidate everything.  This
1249	 * means that the caller can only append to the directory
1250	 * chain.
1251	 */
1252	(*tif->tif_cleanup)(tif);
1253	if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1254		_TIFFfree(tif, tif->tif_rawdata);
1255		tif->tif_rawdata = NULL;
1256		tif->tif_rawcc = 0;
1257	}
1258	tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE);
1259	TIFFFreeDirectory(tif);
1260	TIFFDefaultDirectory(tif);
1261	tif->tif_diroff = 0;			/* force link on next write */
1262	tif->tif_nextdiroff = 0;		/* next write must be at end */
1263	tif->tif_curoff = 0;
1264	tif->tif_row = (uint32) -1;
1265	tif->tif_curstrip = (tstrip_t) -1;
1266	return (1);
1267}
1268#endif
1269
1270/*			[BFC]
1271 *
1272 * Author: Bruce Cameron <cameron@petris.com>
1273 *
1274 * Set a table of tags that are to be replaced during directory process by the
1275 * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that
1276 * 'ReadDirectory' can use the stored information.
1277 */
1278int
1279TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
1280{
1281    static int TIFFignoretags [FIELD_LAST];
1282    static int tagcount = 0 ;
1283    int		i;					/* Loop index */
1284    int		j;					/* Loop index */
1285
1286    switch (task)
1287    {
1288      case TIS_STORE:
1289        if ( tagcount < (FIELD_LAST - 1) )
1290        {
1291            for ( j = 0 ; j < tagcount ; ++j )
1292            {					/* Do not add duplicate tag */
1293                if ( TIFFignoretags [j] == TIFFtagID )
1294                    return (TRUE) ;
1295            }
1296            TIFFignoretags [tagcount++] = TIFFtagID ;
1297            return (TRUE) ;
1298        }
1299        break ;
1300
1301      case TIS_EXTRACT:
1302        for ( i = 0 ; i < tagcount ; ++i )
1303        {
1304            if ( TIFFignoretags [i] == TIFFtagID )
1305                return (TRUE) ;
1306        }
1307        break;
1308
1309      case TIS_EMPTY:
1310        tagcount = 0 ;			/* Clear the list */
1311        return (TRUE) ;
1312
1313      default:
1314        break;
1315    }
1316
1317    return (FALSE);
1318}
1319