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