~ubuntu-branches/ubuntu/precise/tiff/precise-security

« back to all changes in this revision

Viewing changes to .pc/CVE-2014-81xx-1.patch/libtiff/tif_dir.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2015-03-30 08:11:18 UTC
  • Revision ID: package-import@ubuntu.com-20150330081118-bvaoaii1act27voq
Tags: 3.9.5-2ubuntu1.7
* SECURITY UPDATE: Fix multiple security issues
  - debian/patches/CVE-2014-81xx-1.patch to CVE-2014-81xx-11.patch
  - debian/patches/CVE-2014-8128-5.patch
  - debian/patches/CVE-2014-9655-1.patch to CVE-2014-9655-3.patch
  - debian/patches/read_overrun.patch
  - debian/patches/CVE-2014-8130.patch
  - CVE-2014-8127 (partially)
  - CVE-2014-8128
  - CVE-2014-8129
  - CVE-2014-8130
  - CVE-2014-9330
  - CVE-2014-9655

Show diffs side-by-side

added added

removed removed

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