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