Skip to content

Latest commit

 

History

History
1785 lines (1701 loc) · 50.5 KB

tif_dir.c

File metadata and controls

1785 lines (1701 loc) · 50.5 KB
 
Oct 22, 2017
Oct 22, 2017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
* TIFF Library.
*
* Directory Tag Get & Set Routines.
* (and also some miscellaneous stuff)
*/
#include "tiffiop.h"
/*
* These are used in the backwards compatibility code...
*/
#define DATATYPE_VOID 0 /* !untyped data */
#define DATATYPE_INT 1 /* !signed integer data */
#define DATATYPE_UINT 2 /* !unsigned integer data */
#define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
static void
setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
{
if (*vpp) {
_TIFFfree(*vpp);
*vpp = 0;
}
if (vp) {
Nov 6, 2019
Nov 6, 2019
49
50
tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL);
if (bytes)
Oct 22, 2017
Oct 22, 2017
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
*vpp = (void*) _TIFFmalloc(bytes);
if (*vpp)
_TIFFmemcpy(*vpp, vp, bytes);
}
}
void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
{ setByteArray(vpp, vp, n, 1); }
void _TIFFsetString(char** cpp, char* cp)
{ setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
static void _TIFFsetNString(char** cpp, char* cp, uint32 n)
{ setByteArray((void**) cpp, (void*) cp, n, 1); }
void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
{ setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
{ setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
static void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
{ setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
{ setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
{ setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
static void
setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
{
if (*vpp)
_TIFFfree(*vpp);
*vpp = _TIFFmalloc(nmemb*sizeof(double));
if (*vpp)
{
while (nmemb--)
((double*)*vpp)[nmemb] = value;
}
}
/*
* Install extra samples information.
*/
static int
Nov 6, 2019
Nov 6, 2019
90
setExtraSamples(TIFF* tif, va_list ap, uint32* v)
Oct 22, 2017
Oct 22, 2017
91
92
93
94
95
96
{
/* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
#define EXTRASAMPLE_COREL_UNASSALPHA 999
uint16* va;
uint32 i;
Nov 6, 2019
Nov 6, 2019
97
98
TIFFDirectory* td = &tif->tif_dir;
static const char module[] = "setExtraSamples";
Oct 22, 2017
Oct 22, 2017
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
*v = (uint16) va_arg(ap, uint16_vap);
if ((uint16) *v > td->td_samplesperpixel)
return 0;
va = va_arg(ap, uint16*);
if (*v > 0 && va == NULL) /* typically missing param */
return 0;
for (i = 0; i < *v; i++) {
if (va[i] > EXTRASAMPLE_UNASSALPHA) {
/*
* XXX: Corel Draw is known to produce incorrect
* ExtraSamples tags which must be patched here if we
* want to be able to open some of the damaged TIFF
* files:
*/
if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
va[i] = EXTRASAMPLE_UNASSALPHA;
else
return 0;
}
}
Nov 6, 2019
Nov 6, 2019
120
121
122
123
124
125
126
127
128
129
130
131
if ( td->td_transferfunction[0] != NULL && (td->td_samplesperpixel - *v > 1) &&
!(td->td_samplesperpixel - td->td_extrasamples > 1))
{
TIFFWarningExt(tif->tif_clientdata,module,
"ExtraSamples tag value is changing, "
"but TransferFunction was read with a different value. Cancelling it");
TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
_TIFFfree(td->td_transferfunction[0]);
td->td_transferfunction[0] = NULL;
}
Oct 22, 2017
Oct 22, 2017
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
td->td_extrasamples = (uint16) *v;
_TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
return 1;
#undef EXTRASAMPLE_COREL_UNASSALPHA
}
/*
* Confirm we have "samplesperpixel" ink names separated by \0. Returns
* zero if the ink names are not as expected.
*/
static uint32
checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
{
TIFFDirectory* td = &tif->tif_dir;
uint16 i = td->td_samplesperpixel;
if (slen > 0) {
const char* ep = s+slen;
const char* cp = s;
for (; i > 0; i--) {
for (; cp < ep && *cp != '\0'; cp++) {}
if (cp >= ep)
goto bad;
cp++; /* skip \0 */
}
return ((uint32)(cp-s));
}
bad:
TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
"%s: Invalid InkNames value; expecting %d names, found %d",
tif->tif_name,
td->td_samplesperpixel,
td->td_samplesperpixel-i);
return (0);
}
static int
_TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
{
static const char module[] = "_TIFFVSetField";
TIFFDirectory* td = &tif->tif_dir;
int status = 1;
uint32 v32, i, v;
double dblval;
char* s;
const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
uint32 standard_tag = tag;
if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
return 0;
/*
* We want to force the custom code to be used for custom
* fields even if the tag happens to match a well known
* one - important for reinterpreted handling of standard
* tag values in custom directories (i.e. EXIF)
*/
if (fip->field_bit == FIELD_CUSTOM) {
standard_tag = 0;
}
switch (standard_tag) {
case TIFFTAG_SUBFILETYPE:
td->td_subfiletype = (uint32) va_arg(ap, uint32);
break;
case TIFFTAG_IMAGEWIDTH:
td->td_imagewidth = (uint32) va_arg(ap, uint32);
break;
case TIFFTAG_IMAGELENGTH:
td->td_imagelength = (uint32) va_arg(ap, uint32);
break;
case TIFFTAG_BITSPERSAMPLE:
td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
/*
* If the data require post-decoding processing to byte-swap
* samples, set it up here. Note that since tags are required
* to be ordered, compression code can override this behaviour
* in the setup method if it wants to roll the post decoding
* work in with its normal work.
*/
if (tif->tif_flags & TIFF_SWAB) {
if (td->td_bitspersample == 8)
tif->tif_postdecode = _TIFFNoPostDecode;
else if (td->td_bitspersample == 16)
tif->tif_postdecode = _TIFFSwab16BitData;
else if (td->td_bitspersample == 24)
tif->tif_postdecode = _TIFFSwab24BitData;
else if (td->td_bitspersample == 32)
tif->tif_postdecode = _TIFFSwab32BitData;
else if (td->td_bitspersample == 64)
tif->tif_postdecode = _TIFFSwab64BitData;
else if (td->td_bitspersample == 128) /* two 64's */
tif->tif_postdecode = _TIFFSwab64BitData;
}
break;
case TIFFTAG_COMPRESSION:
v = (uint16) va_arg(ap, uint16_vap);
/*
* If we're changing the compression scheme, the notify the
* previous module so that it can cleanup any state it's
* setup.
*/
if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
if ((uint32)td->td_compression == v)
break;
(*tif->tif_cleanup)(tif);
tif->tif_flags &= ~TIFF_CODERSETUP;
}
/*
* Setup new compression routine state.
*/
if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
td->td_compression = (uint16) v;
else
status = 0;
break;
case TIFFTAG_PHOTOMETRIC:
td->td_photometric = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_THRESHHOLDING:
td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_FILLORDER:
v = (uint16) va_arg(ap, uint16_vap);
if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
goto badvalue;
td->td_fillorder = (uint16) v;
break;
case TIFFTAG_ORIENTATION:
v = (uint16) va_arg(ap, uint16_vap);
if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
goto badvalue;
else
td->td_orientation = (uint16) v;
break;
case TIFFTAG_SAMPLESPERPIXEL:
v = (uint16) va_arg(ap, uint16_vap);
if (v == 0)
goto badvalue;
if( v != td->td_samplesperpixel )
{
/* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
if( td->td_sminsamplevalue != NULL )
{
TIFFWarningExt(tif->tif_clientdata,module,
"SamplesPerPixel tag value is changing, "
"but SMinSampleValue tag was read with a different value. Cancelling it");
TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
_TIFFfree(td->td_sminsamplevalue);
td->td_sminsamplevalue = NULL;
}
if( td->td_smaxsamplevalue != NULL )
{
TIFFWarningExt(tif->tif_clientdata,module,
"SamplesPerPixel tag value is changing, "
"but SMaxSampleValue tag was read with a different value. Cancelling it");
TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
_TIFFfree(td->td_smaxsamplevalue);
td->td_smaxsamplevalue = NULL;
}
Nov 6, 2019
Nov 6, 2019
292
293
294
295
296
297
298
299
300
301
302
303
/* Test if 3 transfer functions instead of just one are now needed
See http://bugzilla.maptools.org/show_bug.cgi?id=2820 */
if( td->td_transferfunction[0] != NULL && (v - td->td_extrasamples > 1) &&
!(td->td_samplesperpixel - td->td_extrasamples > 1))
{
TIFFWarningExt(tif->tif_clientdata,module,
"SamplesPerPixel tag value is changing, "
"but TransferFunction was read with a different value. Cancelling it");
TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
_TIFFfree(td->td_transferfunction[0]);
td->td_transferfunction[0] = NULL;
}
Oct 22, 2017
Oct 22, 2017
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
}
td->td_samplesperpixel = (uint16) v;
break;
case TIFFTAG_ROWSPERSTRIP:
v32 = (uint32) va_arg(ap, uint32);
if (v32 == 0)
goto badvalue32;
td->td_rowsperstrip = v32;
if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
td->td_tilelength = v32;
td->td_tilewidth = td->td_imagewidth;
}
break;
case TIFFTAG_MINSAMPLEVALUE:
td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_MAXSAMPLEVALUE:
td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_SMINSAMPLEVALUE:
if (tif->tif_flags & TIFF_PERSAMPLE)
_TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
else
setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
break;
case TIFFTAG_SMAXSAMPLEVALUE:
if (tif->tif_flags & TIFF_PERSAMPLE)
_TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
else
setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
break;
case TIFFTAG_XRESOLUTION:
dblval = va_arg(ap, double);
if( dblval < 0 )
goto badvaluedouble;
Nov 6, 2019
Nov 6, 2019
339
td->td_xresolution = _TIFFClampDoubleToFloat( dblval );
Oct 22, 2017
Oct 22, 2017
340
341
342
343
344
break;
case TIFFTAG_YRESOLUTION:
dblval = va_arg(ap, double);
if( dblval < 0 )
goto badvaluedouble;
Nov 6, 2019
Nov 6, 2019
345
td->td_yresolution = _TIFFClampDoubleToFloat( dblval );
Oct 22, 2017
Oct 22, 2017
346
347
348
349
350
351
352
353
break;
case TIFFTAG_PLANARCONFIG:
v = (uint16) va_arg(ap, uint16_vap);
if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
goto badvalue;
td->td_planarconfig = (uint16) v;
break;
case TIFFTAG_XPOSITION:
Nov 6, 2019
Nov 6, 2019
354
td->td_xposition = _TIFFClampDoubleToFloat( va_arg(ap, double) );
Oct 22, 2017
Oct 22, 2017
355
356
break;
case TIFFTAG_YPOSITION:
Nov 6, 2019
Nov 6, 2019
357
td->td_yposition = _TIFFClampDoubleToFloat( va_arg(ap, double) );
Oct 22, 2017
Oct 22, 2017
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
break;
case TIFFTAG_RESOLUTIONUNIT:
v = (uint16) va_arg(ap, uint16_vap);
if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
goto badvalue;
td->td_resolutionunit = (uint16) v;
break;
case TIFFTAG_PAGENUMBER:
td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_HALFTONEHINTS:
td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_COLORMAP:
v32 = (uint32)(1L<<td->td_bitspersample);
_TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
_TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
_TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
break;
case TIFFTAG_EXTRASAMPLES:
Nov 6, 2019
Nov 6, 2019
380
if (!setExtraSamples(tif, ap, &v))
Oct 22, 2017
Oct 22, 2017
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
goto badvalue;
break;
case TIFFTAG_MATTEING:
td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
if (td->td_extrasamples) {
uint16 sv = EXTRASAMPLE_ASSOCALPHA;
_TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
}
break;
case TIFFTAG_TILEWIDTH:
v32 = (uint32) va_arg(ap, uint32);
if (v32 % 16) {
if (tif->tif_mode != O_RDONLY)
goto badvalue32;
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
"Nonstandard tile width %d, convert file", v32);
}
td->td_tilewidth = v32;
tif->tif_flags |= TIFF_ISTILED;
break;
case TIFFTAG_TILELENGTH:
v32 = (uint32) va_arg(ap, uint32);
if (v32 % 16) {
if (tif->tif_mode != O_RDONLY)
goto badvalue32;
TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
"Nonstandard tile length %d, convert file", v32);
}
td->td_tilelength = v32;
tif->tif_flags |= TIFF_ISTILED;
break;
case TIFFTAG_TILEDEPTH:
v32 = (uint32) va_arg(ap, uint32);
if (v32 == 0)
goto badvalue32;
td->td_tiledepth = v32;
break;
case TIFFTAG_DATATYPE:
v = (uint16) va_arg(ap, uint16_vap);
switch (v) {
case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
default: goto badvalue;
}
td->td_sampleformat = (uint16) v;
break;
case TIFFTAG_SAMPLEFORMAT:
v = (uint16) va_arg(ap, uint16_vap);
if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
goto badvalue;
td->td_sampleformat = (uint16) v;
/* Try to fix up the SWAB function for complex data. */
if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
&& td->td_bitspersample == 32
&& tif->tif_postdecode == _TIFFSwab32BitData )
tif->tif_postdecode = _TIFFSwab16BitData;
else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
|| td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
&& td->td_bitspersample == 64
&& tif->tif_postdecode == _TIFFSwab64BitData )
tif->tif_postdecode = _TIFFSwab32BitData;
break;
case TIFFTAG_IMAGEDEPTH:
td->td_imagedepth = (uint32) va_arg(ap, uint32);
break;
case TIFFTAG_SUBIFD:
if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
_TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
(uint32) td->td_nsubifd);
} else {
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Sorry, cannot nest SubIFDs",
tif->tif_name);
status = 0;
}
break;
case TIFFTAG_YCBCRPOSITIONING:
td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_YCBCRSUBSAMPLING:
td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
break;
case TIFFTAG_TRANSFERFUNCTION:
v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
for (i = 0; i < v; i++)
_TIFFsetShortArray(&td->td_transferfunction[i],
va_arg(ap, uint16*), 1U<<td->td_bitspersample);
break;
case TIFFTAG_REFERENCEBLACKWHITE:
/* XXX should check for null range */
_TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
break;
case TIFFTAG_INKNAMES:
v = (uint16) va_arg(ap, uint16_vap);
s = va_arg(ap, char*);
v = checkInkNamesString(tif, v, s);
status = v > 0;
if( v > 0 ) {
_TIFFsetNString(&td->td_inknames, s, v);
td->td_inknameslen = v;
}
break;
case TIFFTAG_PERSAMPLE:
v = (uint16) va_arg(ap, uint16_vap);
if( v == PERSAMPLE_MULTI )
tif->tif_flags |= TIFF_PERSAMPLE;
else
tif->tif_flags &= ~TIFF_PERSAMPLE;
break;
default: {
TIFFTagValue *tv;
int tv_size, iCustom;
/*
* This can happen if multiple images are open with different
* codecs which have private tags. The global tag information
* table may then have tags that are valid for one file but not
* the other. If the client tries to set a tag that is not valid
* for the image's codec then we'll arrive here. This
* happens, for example, when tiffcp is used to convert between
* compression schemes and codec-specific tags are blindly copied.
*/
if(fip->field_bit != FIELD_CUSTOM) {
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Invalid %stag \"%s\" (not supported by codec)",
tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
fip->field_name);
status = 0;
break;
}
/*
* Find the existing entry for this custom value.
*/
tv = NULL;
for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
if (td->td_customValues[iCustom].info->field_tag == tag) {
tv = td->td_customValues + iCustom;
if (tv->value != NULL) {
_TIFFfree(tv->value);
tv->value = NULL;
}
break;
}
}
/*
* Grow the custom list if the entry was not found.
*/
if(tv == NULL) {
TIFFTagValue *new_customValues;
td->td_customValueCount++;
new_customValues = (TIFFTagValue *)
_TIFFrealloc(td->td_customValues,
sizeof(TIFFTagValue) * td->td_customValueCount);
if (!new_customValues) {
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Failed to allocate space for list of custom values",
tif->tif_name);
status = 0;
goto end;
}
td->td_customValues = new_customValues;
tv = td->td_customValues + (td->td_customValueCount - 1);
tv->info = fip;
tv->value = NULL;
tv->count = 0;
}
/*
* Set custom value ... save a copy of the custom tag value.
*/
tv_size = _TIFFDataSize(fip->field_type);
if (tv_size == 0) {
status = 0;
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Bad field type %d for \"%s\"",
tif->tif_name, fip->field_type,
fip->field_name);
goto end;
}
if (fip->field_type == TIFF_ASCII)
{
uint32 ma;
char* mb;
if (fip->field_passcount)
{
assert(fip->field_writecount==TIFF_VARIABLE2);
ma=(uint32)va_arg(ap,uint32);
mb=(char*)va_arg(ap,char*);
}
else
{
mb=(char*)va_arg(ap,char*);
ma=(uint32)(strlen(mb)+1);
}
tv->count=ma;
setByteArray(&tv->value,mb,ma,1);
}
else
{
if (fip->field_passcount) {
if (fip->field_writecount == TIFF_VARIABLE2)
tv->count = (uint32) va_arg(ap, uint32);
else
tv->count = (int) va_arg(ap, int);
} else if (fip->field_writecount == TIFF_VARIABLE
|| fip->field_writecount == TIFF_VARIABLE2)
tv->count = 1;
else if (fip->field_writecount == TIFF_SPP)
tv->count = td->td_samplesperpixel;
else
tv->count = fip->field_writecount;
if (tv->count == 0) {
status = 0;
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Null count for \"%s\" (type "
"%d, writecount %d, passcount %d)",
tif->tif_name,
fip->field_name,
fip->field_type,
fip->field_writecount,
fip->field_passcount);
goto end;
}
tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
"custom tag binary object");
if (!tv->value) {
status = 0;
goto end;
}
if (fip->field_tag == TIFFTAG_DOTRANGE
&& strcmp(fip->field_name,"DotRange") == 0) {
/* TODO: This is an evil exception and should not have been
handled this way ... likely best if we move it into
the directory structure with an explicit field in
libtiff 4.1 and assign it a FIELD_ value */
uint16 v2[2];
v2[0] = (uint16)va_arg(ap, int);
v2[1] = (uint16)va_arg(ap, int);
_TIFFmemcpy(tv->value, &v2, 4);
}
else if (fip->field_passcount
|| fip->field_writecount == TIFF_VARIABLE
|| fip->field_writecount == TIFF_VARIABLE2
|| fip->field_writecount == TIFF_SPP
|| tv->count > 1) {
_TIFFmemcpy(tv->value, va_arg(ap, void *),
tv->count * tv_size);
} else {
char *val = (char *)tv->value;
assert( tv->count == 1 );
switch (fip->field_type) {
case TIFF_BYTE:
case TIFF_UNDEFINED:
{
uint8 v2 = (uint8)va_arg(ap, int);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_SBYTE:
{
int8 v2 = (int8)va_arg(ap, int);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_SHORT:
{
uint16 v2 = (uint16)va_arg(ap, int);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_SSHORT:
{
int16 v2 = (int16)va_arg(ap, int);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_LONG:
case TIFF_IFD:
{
uint32 v2 = va_arg(ap, uint32);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_SLONG:
{
int32 v2 = va_arg(ap, int32);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_LONG8:
case TIFF_IFD8:
{
uint64 v2 = va_arg(ap, uint64);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_SLONG8:
{
int64 v2 = va_arg(ap, int64);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_RATIONAL:
case TIFF_SRATIONAL:
case TIFF_FLOAT:
{
Nov 6, 2019
Nov 6, 2019
703
float v2 = _TIFFClampDoubleToFloat(va_arg(ap, double));
Oct 22, 2017
Oct 22, 2017
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
_TIFFmemcpy(val, &v2, tv_size);
}
break;
case TIFF_DOUBLE:
{
double v2 = va_arg(ap, double);
_TIFFmemcpy(val, &v2, tv_size);
}
break;
default:
_TIFFmemset(val, 0, tv_size);
status = 0;
break;
}
}
}
}
}
if (status) {
const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
if (fip2)
TIFFSetFieldBit(tif, fip2->field_bit);
tif->tif_flags |= TIFF_DIRTYDIRECT;
}
end:
va_end(ap);
return (status);
badvalue:
{
const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Bad value %u for \"%s\" tag",
tif->tif_name, v,
fip2 ? fip2->field_name : "Unknown");
va_end(ap);
}
return (0);
badvalue32:
{
const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Bad value %u for \"%s\" tag",
tif->tif_name, v32,
fip2 ? fip2->field_name : "Unknown");
va_end(ap);
}
return (0);
badvaluedouble:
{
const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
TIFFErrorExt(tif->tif_clientdata, module,
"%s: Bad value %f for \"%s\" tag",
tif->tif_name, dblval,
fip2 ? fip2->field_name : "Unknown");
va_end(ap);
}
return (0);
}
/*
* Return 1/0 according to whether or not
* it is permissible to set the tag's value.
* Note that we allow ImageLength to be changed
* so that we can append and extend to images.
* Any other tag may not be altered once writing
* has commenced, unless its value has no effect
* on the format of the data that is written.
*/
static int
OkToChangeTag(TIFF* tif, uint32 tag)
{
const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
if (!fip) { /* unknown tag */
TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
return (0);
}
if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
!fip->field_oktochange) {
/*
* Consult info table to see if tag can be changed
* after we've started writing. We only allow changes
* to those tags that don't/shouldn't affect the
* compression and/or format of the data.
*/
TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
"%s: Cannot modify tag \"%s\" while writing",
tif->tif_name, fip->field_name);
return (0);
}
return (1);
}
/*
* Record the value of a field in the
* internal directory structure. The
* field will be written to the file
* when/if the directory structure is
* updated.
*/
int
TIFFSetField(TIFF* tif, uint32 tag, ...)
{
va_list ap;
int status;
va_start(ap, tag);
status = TIFFVSetField(tif, tag, ap);
va_end(ap);
return (status);
}
/*
* Clear the contents of the field in the internal structure.
*/
int
TIFFUnsetField(TIFF* tif, uint32 tag)
{
const TIFFField *fip = TIFFFieldWithTag(tif, tag);
TIFFDirectory* td = &tif->tif_dir;
if( !fip )
return 0;
if( fip->field_bit != FIELD_CUSTOM )
TIFFClrFieldBit(tif, fip->field_bit);
else
{
TIFFTagValue *tv = NULL;
int i;
for (i = 0; i < td->td_customValueCount; i++) {
tv = td->td_customValues + i;
if( tv->info->field_tag == tag )
break;
}
if( i < td->td_customValueCount )
{
_TIFFfree(tv->value);
for( ; i < td->td_customValueCount-1; i++) {
td->td_customValues[i] = td->td_customValues[i+1];
}
td->td_customValueCount--;
}
}
tif->tif_flags |= TIFF_DIRTYDIRECT;
return (1);
}
/*
* Like TIFFSetField, but taking a varargs
* parameter list. This routine is useful
* for building higher-level interfaces on
* top of the library.
*/
int
TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
{
return OkToChangeTag(tif, tag) ?
(*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
}
static int
_TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
{
TIFFDirectory* td = &tif->tif_dir;
int ret_val = 1;
uint32 standard_tag = tag;
const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
return 0;
Nov 6, 2019
Nov 6, 2019
880
881
882
883
884
885
886
887
888
889
/*
* We want to force the custom code to be used for custom
* fields even if the tag happens to match a well known
* one - important for reinterpreted handling of standard
* tag values in custom directories (i.e. EXIF)
*/
if (fip->field_bit == FIELD_CUSTOM) {
standard_tag = 0;
}
Oct 22, 2017
Oct 22, 2017
890
Nov 6, 2019
Nov 6, 2019
891
if( standard_tag == TIFFTAG_NUMBEROFINKS )
Oct 22, 2017
Oct 22, 2017
892
893
894
895
896
{
int i;
for (i = 0; i < td->td_customValueCount; i++) {
uint16 val;
TIFFTagValue *tv = td->td_customValues + i;
Nov 6, 2019
Nov 6, 2019
897
if (tv->info->field_tag != standard_tag)
Oct 22, 2017
Oct 22, 2017
898
continue;
Nov 4, 2018
Nov 4, 2018
899
900
if( tv->value == NULL )
return 0;
Oct 22, 2017
Oct 22, 2017
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
val = *(uint16 *)tv->value;
/* Truncate to SamplesPerPixel, since the */
/* setting code for INKNAMES assume that there are SamplesPerPixel */
/* inknames. */
/* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
if( val > td->td_samplesperpixel )
{
TIFFWarningExt(tif->tif_clientdata,"_TIFFVGetField",
"Truncating NumberOfInks from %u to %u",
val, td->td_samplesperpixel);
val = td->td_samplesperpixel;
}
*va_arg(ap, uint16*) = val;
return 1;
}
return 0;
}
switch (standard_tag) {
case TIFFTAG_SUBFILETYPE:
*va_arg(ap, uint32*) = td->td_subfiletype;
break;
case TIFFTAG_IMAGEWIDTH:
*va_arg(ap, uint32*) = td->td_imagewidth;
break;
case TIFFTAG_IMAGELENGTH:
*va_arg(ap, uint32*) = td->td_imagelength;
break;
case TIFFTAG_BITSPERSAMPLE:
*va_arg(ap, uint16*) = td->td_bitspersample;
break;
case TIFFTAG_COMPRESSION:
*va_arg(ap, uint16*) = td->td_compression;
break;
case TIFFTAG_PHOTOMETRIC:
*va_arg(ap, uint16*) = td->td_photometric;
break;
case TIFFTAG_THRESHHOLDING:
*va_arg(ap, uint16*) = td->td_threshholding;
break;
case TIFFTAG_FILLORDER:
*va_arg(ap, uint16*) = td->td_fillorder;
break;
case TIFFTAG_ORIENTATION:
*va_arg(ap, uint16*) = td->td_orientation;
break;
case TIFFTAG_SAMPLESPERPIXEL:
*va_arg(ap, uint16*) = td->td_samplesperpixel;
break;
case TIFFTAG_ROWSPERSTRIP:
*va_arg(ap, uint32*) = td->td_rowsperstrip;
break;
case TIFFTAG_MINSAMPLEVALUE:
*va_arg(ap, uint16*) = td->td_minsamplevalue;
break;
case TIFFTAG_MAXSAMPLEVALUE:
*va_arg(ap, uint16*) = td->td_maxsamplevalue;
break;
case TIFFTAG_SMINSAMPLEVALUE:
if (tif->tif_flags & TIFF_PERSAMPLE)
*va_arg(ap, double**) = td->td_sminsamplevalue;
else
{
/* libtiff historically treats this as a single value. */
uint16 i;
double v = td->td_sminsamplevalue[0];
for (i=1; i < td->td_samplesperpixel; ++i)
if( td->td_sminsamplevalue[i] < v )
v = td->td_sminsamplevalue[i];
*va_arg(ap, double*) = v;
}
break;
case TIFFTAG_SMAXSAMPLEVALUE:
if (tif->tif_flags & TIFF_PERSAMPLE)
*va_arg(ap, double**) = td->td_smaxsamplevalue;
else
{
/* libtiff historically treats this as a single value. */
uint16 i;
double v = td->td_smaxsamplevalue[0];
for (i=1; i < td->td_samplesperpixel; ++i)
if( td->td_smaxsamplevalue[i] > v )
v = td->td_smaxsamplevalue[i];
*va_arg(ap, double*) = v;
}
break;
case TIFFTAG_XRESOLUTION:
*va_arg(ap, float*) = td->td_xresolution;
break;
case TIFFTAG_YRESOLUTION:
*va_arg(ap, float*) = td->td_yresolution;
break;
case TIFFTAG_PLANARCONFIG:
*va_arg(ap, uint16*) = td->td_planarconfig;
break;
case TIFFTAG_XPOSITION:
*va_arg(ap, float*) = td->td_xposition;
break;
case TIFFTAG_YPOSITION:
*va_arg(ap, float*) = td->td_yposition;