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