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