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