/src/libtiff/libtiff/tif_dir.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 1988-1997 Sam Leffler |
3 | | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and |
6 | | * its documentation for any purpose is hereby granted without fee, provided |
7 | | * that (i) the above copyright notices and this permission notice appear in |
8 | | * all copies of the software and related documentation, and (ii) the names of |
9 | | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
10 | | * publicity relating to the software without the specific, prior written |
11 | | * permission of Sam Leffler and Silicon Graphics. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
14 | | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
15 | | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
16 | | * |
17 | | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
18 | | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
19 | | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
20 | | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
21 | | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
22 | | * OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | /* |
26 | | * TIFF Library. |
27 | | * |
28 | | * Directory Tag Get & Set Routines. |
29 | | * (and also some miscellaneous stuff) |
30 | | */ |
31 | | #include "tiffiop.h" |
32 | | #include <float.h> /*--: for Rational2Double */ |
33 | | #include <limits.h> |
34 | | #include <math.h> |
35 | | |
36 | | /* |
37 | | * These are used in the backwards compatibility code... |
38 | | */ |
39 | 250 | #define DATATYPE_VOID 0 /* !untyped data */ |
40 | 597 | #define DATATYPE_INT 1 /* !signed integer data */ |
41 | 14 | #define DATATYPE_UINT 2 /* !unsigned integer data */ |
42 | 217 | #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */ |
43 | | |
44 | | static void setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb, |
45 | | size_t elem_size) |
46 | 110k | { |
47 | 110k | if (*vpp) |
48 | 685 | { |
49 | 685 | _TIFFfreeExt(tif, *vpp); |
50 | 685 | *vpp = 0; |
51 | 685 | } |
52 | 110k | if (vp) |
53 | 98.1k | { |
54 | 98.1k | tmsize_t bytes = _TIFFMultiplySSize(NULL, (tmsize_t)nmemb, |
55 | 98.1k | (tmsize_t)elem_size, NULL); |
56 | 98.1k | if (bytes) |
57 | 98.1k | *vpp = (void *)_TIFFmallocExt(tif, bytes); |
58 | 98.1k | if (*vpp) |
59 | 98.1k | _TIFFmemcpy(*vpp, vp, bytes); |
60 | 98.1k | } |
61 | 110k | } |
62 | | void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n) |
63 | 0 | { |
64 | 0 | setByteArray(NULL, vpp, vp, n, 1); |
65 | 0 | } |
66 | | void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n) |
67 | 5.34k | { |
68 | 5.34k | setByteArray(tif, vpp, vp, n, 1); |
69 | 5.34k | } |
70 | | |
71 | | static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n) |
72 | 1.58k | { |
73 | 1.58k | setByteArray(tif, (void **)cpp, cp, n, 1); |
74 | 1.58k | } |
75 | | |
76 | | void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n) |
77 | 0 | { |
78 | 0 | setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t)); |
79 | 0 | } |
80 | | void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp, |
81 | | uint32_t n) |
82 | 32.0k | { |
83 | 32.0k | setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t)); |
84 | 32.0k | } |
85 | | |
86 | | void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n) |
87 | 0 | { |
88 | 0 | setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t)); |
89 | 0 | } |
90 | | void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp, |
91 | | uint32_t n) |
92 | 0 | { |
93 | 0 | setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t)); |
94 | 0 | } |
95 | | |
96 | | static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp, |
97 | | uint32_t n) |
98 | 615 | { |
99 | 615 | setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t)); |
100 | 615 | } |
101 | | |
102 | | void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n) |
103 | 0 | { |
104 | 0 | setByteArray(NULL, (void **)fpp, fp, n, sizeof(float)); |
105 | 0 | } |
106 | | void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n) |
107 | 2.04k | { |
108 | 2.04k | setByteArray(tif, (void **)fpp, fp, n, sizeof(float)); |
109 | 2.04k | } |
110 | | |
111 | | void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n) |
112 | 0 | { |
113 | 0 | setByteArray(NULL, (void **)dpp, dp, n, sizeof(double)); |
114 | 0 | } |
115 | | void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp, |
116 | | uint32_t n) |
117 | 4.61k | { |
118 | 4.61k | setByteArray(tif, (void **)dpp, dp, n, sizeof(double)); |
119 | 4.61k | } |
120 | | |
121 | | static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value, |
122 | | size_t nmemb) |
123 | 2.19k | { |
124 | 2.19k | if (*vpp) |
125 | 0 | _TIFFfreeExt(tif, *vpp); |
126 | 2.19k | *vpp = (double *)_TIFFmallocExt(tif, |
127 | 2.19k | (tmsize_t)nmemb * (tmsize_t)sizeof(double)); |
128 | 2.19k | if (*vpp) |
129 | 2.19k | { |
130 | 8.57k | while (nmemb--) |
131 | 6.38k | ((double *)*vpp)[nmemb] = value; |
132 | 2.19k | } |
133 | 2.19k | } |
134 | | |
135 | | /* |
136 | | * Install extra samples information. |
137 | | */ |
138 | | static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v) |
139 | 16.2k | { |
140 | | /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */ |
141 | 16.2k | #define EXTRASAMPLE_COREL_UNASSALPHA 999 |
142 | | |
143 | 16.2k | uint16_t *va; |
144 | 16.2k | uint32_t i; |
145 | 16.2k | TIFFDirectory *td = &tif->tif_dir; |
146 | 16.2k | static const char module[] = "setExtraSamples"; |
147 | | |
148 | 16.2k | *v = (uint16_t)va_arg(ap, uint16_vap); |
149 | 16.2k | if ((uint16_t)*v > td->td_samplesperpixel) |
150 | 51 | return 0; |
151 | 16.1k | va = va_arg(ap, uint16_t *); |
152 | 16.1k | if (*v > 0 && va == NULL) /* typically missing param */ |
153 | 0 | return 0; |
154 | 31.4k | for (i = 0; i < *v; i++) |
155 | 15.3k | { |
156 | 15.3k | if (va[i] > EXTRASAMPLE_UNASSALPHA) |
157 | 208 | { |
158 | | /* |
159 | | * XXX: Corel Draw is known to produce incorrect |
160 | | * ExtraSamples tags which must be patched here if we |
161 | | * want to be able to open some of the damaged TIFF |
162 | | * files: |
163 | | */ |
164 | 208 | if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA) |
165 | 49 | va[i] = EXTRASAMPLE_UNASSALPHA; |
166 | 159 | else |
167 | 159 | return 0; |
168 | 208 | } |
169 | 15.3k | } |
170 | | |
171 | 16.0k | if (td->td_transferfunction[0] != NULL && |
172 | 0 | (td->td_samplesperpixel - *v > 1) && |
173 | 0 | !(td->td_samplesperpixel - td->td_extrasamples > 1)) |
174 | 0 | { |
175 | 0 | TIFFWarningExtR(tif, module, |
176 | 0 | "ExtraSamples tag value is changing, " |
177 | 0 | "but TransferFunction was read with a different value. " |
178 | 0 | "Canceling it"); |
179 | 0 | TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION); |
180 | 0 | _TIFFfreeExt(tif, td->td_transferfunction[0]); |
181 | 0 | td->td_transferfunction[0] = NULL; |
182 | 0 | } |
183 | | |
184 | 16.0k | td->td_extrasamples = (uint16_t)*v; |
185 | 16.0k | _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples); |
186 | 16.0k | return 1; |
187 | | |
188 | 16.1k | #undef EXTRASAMPLE_COREL_UNASSALPHA |
189 | 16.1k | } |
190 | | |
191 | | /* |
192 | | * Count ink names separated by \0. Returns |
193 | | * zero if the ink names are not as expected. |
194 | | */ |
195 | | static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s) |
196 | 1.77k | { |
197 | 1.77k | uint16_t i = 0; |
198 | | |
199 | 1.77k | if (slen > 0) |
200 | 1.58k | { |
201 | 1.58k | const char *ep = s + slen; |
202 | 1.58k | const char *cp = s; |
203 | 1.58k | do |
204 | 11.4k | { |
205 | 25.0k | for (; cp < ep && *cp != '\0'; cp++) |
206 | 13.5k | { |
207 | 13.5k | } |
208 | 11.4k | if (cp >= ep) |
209 | 0 | goto bad; |
210 | 11.4k | cp++; /* skip \0 */ |
211 | 11.4k | i++; |
212 | 11.4k | } while (cp < ep); |
213 | 1.58k | return (i); |
214 | 1.58k | } |
215 | 192 | bad: |
216 | 192 | TIFFErrorExtR(tif, "TIFFSetField", |
217 | 192 | "%s: Invalid InkNames value; no null at given buffer end " |
218 | 192 | "location %" PRIu32 ", after %" PRIu16 " ink", |
219 | 192 | tif->tif_name, slen, i); |
220 | 192 | return (0); |
221 | 1.77k | } |
222 | | |
223 | | static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap) |
224 | 2.06M | { |
225 | 2.06M | static const char module[] = "_TIFFVSetField"; |
226 | | |
227 | 2.06M | TIFFDirectory *td = &tif->tif_dir; |
228 | 2.06M | int status = 1; |
229 | 2.06M | uint32_t v32, v; |
230 | 2.06M | double dblval; |
231 | 2.06M | char *s; |
232 | 2.06M | const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); |
233 | 2.06M | uint32_t standard_tag = tag; |
234 | 2.06M | if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */ |
235 | 0 | return 0; |
236 | | /* |
237 | | * We want to force the custom code to be used for custom |
238 | | * fields even if the tag happens to match a well known |
239 | | * one - important for reinterpreted handling of standard |
240 | | * tag values in custom directories (i.e. EXIF) |
241 | | */ |
242 | 2.06M | if (fip->field_bit == FIELD_CUSTOM) |
243 | 569k | { |
244 | 569k | standard_tag = 0; |
245 | 569k | } |
246 | | |
247 | 2.06M | switch (standard_tag) |
248 | 2.06M | { |
249 | 3.39k | case TIFFTAG_SUBFILETYPE: |
250 | 3.39k | td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t); |
251 | 3.39k | break; |
252 | 159k | case TIFFTAG_IMAGEWIDTH: |
253 | 159k | td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t); |
254 | 159k | break; |
255 | 150k | case TIFFTAG_IMAGELENGTH: |
256 | 150k | td->td_imagelength = (uint32_t)va_arg(ap, uint32_t); |
257 | 150k | break; |
258 | 88.3k | case TIFFTAG_BITSPERSAMPLE: |
259 | 88.3k | td->td_bitspersample = (uint16_t)va_arg(ap, uint16_vap); |
260 | 88.3k | _TIFFSetDefaultPostDecode(tif); |
261 | 88.3k | break; |
262 | 381k | case TIFFTAG_COMPRESSION: |
263 | 381k | v = (uint16_t)va_arg(ap, uint16_vap); |
264 | | /* |
265 | | * If we're changing the compression scheme, notify the |
266 | | * previous module so that it can cleanup any state it's |
267 | | * setup. |
268 | | */ |
269 | 381k | if (TIFFFieldSet(tif, FIELD_COMPRESSION)) |
270 | 175k | { |
271 | 175k | if ((uint32_t)td->td_compression == v) |
272 | 94.1k | break; |
273 | 80.9k | (*tif->tif_cleanup)(tif); |
274 | 80.9k | tif->tif_flags &= ~TIFF_CODERSETUP; |
275 | 80.9k | } |
276 | | /* |
277 | | * Setup new compression routine state. |
278 | | */ |
279 | 287k | if ((status = TIFFSetCompressionScheme(tif, (int)v)) != 0) |
280 | 287k | td->td_compression = (uint16_t)v; |
281 | 0 | else |
282 | 0 | status = 0; |
283 | 287k | break; |
284 | 139k | case TIFFTAG_PHOTOMETRIC: |
285 | 139k | td->td_photometric = (uint16_t)va_arg(ap, uint16_vap); |
286 | 139k | break; |
287 | 654 | case TIFFTAG_THRESHHOLDING: |
288 | 654 | td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap); |
289 | 654 | break; |
290 | 10.9k | case TIFFTAG_FILLORDER: |
291 | 10.9k | v = (uint16_t)va_arg(ap, uint16_vap); |
292 | 10.9k | if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB) |
293 | 400 | goto badvalue; |
294 | 10.5k | td->td_fillorder = (uint16_t)v; |
295 | 10.5k | break; |
296 | 23.6k | case TIFFTAG_ORIENTATION: |
297 | 23.6k | v = (uint16_t)va_arg(ap, uint16_vap); |
298 | 23.6k | if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) |
299 | 619 | goto badvalue; |
300 | 23.0k | else |
301 | 23.0k | td->td_orientation = (uint16_t)v; |
302 | 23.0k | break; |
303 | 92.0k | case TIFFTAG_SAMPLESPERPIXEL: |
304 | 92.0k | v = (uint16_t)va_arg(ap, uint16_vap); |
305 | 92.0k | if (v == 0) |
306 | 9 | goto badvalue; |
307 | 91.9k | if (v != td->td_samplesperpixel) |
308 | 78.2k | { |
309 | | /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */ |
310 | 78.2k | if (td->td_sminsamplevalue != NULL) |
311 | 662 | { |
312 | 662 | TIFFWarningExtR(tif, module, |
313 | 662 | "SamplesPerPixel tag value is changing, " |
314 | 662 | "but SMinSampleValue tag was read with a " |
315 | 662 | "different value. Canceling it"); |
316 | 662 | TIFFClrFieldBit(tif, FIELD_SMINSAMPLEVALUE); |
317 | 662 | _TIFFfreeExt(tif, td->td_sminsamplevalue); |
318 | 662 | td->td_sminsamplevalue = NULL; |
319 | 662 | } |
320 | 78.2k | if (td->td_smaxsamplevalue != NULL) |
321 | 502 | { |
322 | 502 | TIFFWarningExtR(tif, module, |
323 | 502 | "SamplesPerPixel tag value is changing, " |
324 | 502 | "but SMaxSampleValue tag was read with a " |
325 | 502 | "different value. Canceling it"); |
326 | 502 | TIFFClrFieldBit(tif, FIELD_SMAXSAMPLEVALUE); |
327 | 502 | _TIFFfreeExt(tif, td->td_smaxsamplevalue); |
328 | 502 | td->td_smaxsamplevalue = NULL; |
329 | 502 | } |
330 | | /* Test if 3 transfer functions instead of just one are now |
331 | | needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820 |
332 | | */ |
333 | 78.2k | if (td->td_transferfunction[0] != NULL && |
334 | 17 | (v - td->td_extrasamples > 1) && |
335 | 17 | !(td->td_samplesperpixel - td->td_extrasamples > 1)) |
336 | 17 | { |
337 | 17 | TIFFWarningExtR(tif, module, |
338 | 17 | "SamplesPerPixel tag value is changing, " |
339 | 17 | "but TransferFunction was read with a " |
340 | 17 | "different value. Canceling it"); |
341 | 17 | TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION); |
342 | 17 | _TIFFfreeExt(tif, td->td_transferfunction[0]); |
343 | 17 | td->td_transferfunction[0] = NULL; |
344 | 17 | } |
345 | 78.2k | } |
346 | 91.9k | td->td_samplesperpixel = (uint16_t)v; |
347 | 91.9k | break; |
348 | 76.6k | case TIFFTAG_ROWSPERSTRIP: |
349 | 76.6k | v32 = (uint32_t)va_arg(ap, uint32_t); |
350 | 76.6k | if (v32 == 0) |
351 | 393 | goto badvalue32; |
352 | 76.2k | td->td_rowsperstrip = v32; |
353 | 76.2k | if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) |
354 | 75.9k | { |
355 | 75.9k | td->td_tilelength = v32; |
356 | 75.9k | td->td_tilewidth = td->td_imagewidth; |
357 | 75.9k | } |
358 | 76.2k | break; |
359 | 1.93k | case TIFFTAG_MINSAMPLEVALUE: |
360 | 1.93k | td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap); |
361 | 1.93k | break; |
362 | 1.88k | case TIFFTAG_MAXSAMPLEVALUE: |
363 | 1.88k | td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap); |
364 | 1.88k | break; |
365 | 3.92k | case TIFFTAG_SMINSAMPLEVALUE: |
366 | 3.92k | if (tif->tif_flags & TIFF_PERSAMPLE) |
367 | 2.82k | _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue, |
368 | 2.82k | va_arg(ap, double *), |
369 | 2.82k | td->td_samplesperpixel); |
370 | 1.09k | else |
371 | 1.09k | setDoubleArrayOneValue(tif, &td->td_sminsamplevalue, |
372 | 1.09k | va_arg(ap, double), |
373 | 1.09k | td->td_samplesperpixel); |
374 | 3.92k | break; |
375 | 2.88k | case TIFFTAG_SMAXSAMPLEVALUE: |
376 | 2.88k | if (tif->tif_flags & TIFF_PERSAMPLE) |
377 | 1.78k | _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue, |
378 | 1.78k | va_arg(ap, double *), |
379 | 1.78k | td->td_samplesperpixel); |
380 | 1.09k | else |
381 | 1.09k | setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue, |
382 | 1.09k | va_arg(ap, double), |
383 | 1.09k | td->td_samplesperpixel); |
384 | 2.88k | break; |
385 | 7.00k | case TIFFTAG_XRESOLUTION: |
386 | 7.00k | dblval = va_arg(ap, double); |
387 | 7.00k | if (isnan(dblval) || dblval < 0) |
388 | 261 | goto badvaluedouble; |
389 | 6.74k | td->td_xresolution = _TIFFClampDoubleToFloat(dblval); |
390 | 6.74k | break; |
391 | 7.39k | case TIFFTAG_YRESOLUTION: |
392 | 7.39k | dblval = va_arg(ap, double); |
393 | 7.39k | if (isnan(dblval) || dblval < 0) |
394 | 271 | goto badvaluedouble; |
395 | 7.12k | td->td_yresolution = _TIFFClampDoubleToFloat(dblval); |
396 | 7.12k | break; |
397 | 207k | case TIFFTAG_PLANARCONFIG: |
398 | 207k | v = (uint16_t)va_arg(ap, uint16_vap); |
399 | 207k | if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE) |
400 | 70 | goto badvalue; |
401 | 206k | td->td_planarconfig = (uint16_t)v; |
402 | 206k | break; |
403 | 3.81k | case TIFFTAG_XPOSITION: |
404 | 3.81k | td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double)); |
405 | 3.81k | break; |
406 | 2.88k | case TIFFTAG_YPOSITION: |
407 | 2.88k | td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double)); |
408 | 2.88k | break; |
409 | 4.45k | case TIFFTAG_RESOLUTIONUNIT: |
410 | 4.45k | v = (uint16_t)va_arg(ap, uint16_vap); |
411 | 4.45k | if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v) |
412 | 453 | goto badvalue; |
413 | 4.00k | td->td_resolutionunit = (uint16_t)v; |
414 | 4.00k | break; |
415 | 17.8k | case TIFFTAG_PAGENUMBER: |
416 | 17.8k | td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap); |
417 | 17.8k | td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap); |
418 | 17.8k | break; |
419 | 235 | case TIFFTAG_HALFTONEHINTS: |
420 | 235 | td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap); |
421 | 235 | td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap); |
422 | 235 | break; |
423 | 2.31k | case TIFFTAG_COLORMAP: |
424 | 2.31k | v32 = (uint32_t)(1UL << td->td_bitspersample); |
425 | 2.31k | _TIFFsetShortArrayExt(tif, &td->td_colormap[0], |
426 | 2.31k | va_arg(ap, uint16_t *), v32); |
427 | 2.31k | _TIFFsetShortArrayExt(tif, &td->td_colormap[1], |
428 | 2.31k | va_arg(ap, uint16_t *), v32); |
429 | 2.31k | _TIFFsetShortArrayExt(tif, &td->td_colormap[2], |
430 | 2.31k | va_arg(ap, uint16_t *), v32); |
431 | 2.31k | break; |
432 | 16.2k | case TIFFTAG_EXTRASAMPLES: |
433 | 16.2k | if (!setExtraSamples(tif, ap, &v)) |
434 | 210 | goto badvalue; |
435 | 16.0k | break; |
436 | 16.0k | case TIFFTAG_MATTEING: |
437 | 363 | td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0); |
438 | 363 | if (td->td_extrasamples) |
439 | 228 | { |
440 | 228 | uint16_t sv = EXTRASAMPLE_ASSOCALPHA; |
441 | 228 | _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1); |
442 | 228 | } |
443 | 363 | break; |
444 | 13.4k | case TIFFTAG_TILEWIDTH: |
445 | 13.4k | v32 = (uint32_t)va_arg(ap, uint32_t); |
446 | 13.4k | if (v32 % 16) |
447 | 7.53k | { |
448 | 7.53k | if (tif->tif_mode != O_RDONLY) |
449 | 0 | goto badvalue32; |
450 | 7.53k | TIFFWarningExtR( |
451 | 7.53k | tif, tif->tif_name, |
452 | 7.53k | "Nonstandard tile width %" PRIu32 ", convert file", v32); |
453 | 7.53k | } |
454 | 13.4k | td->td_tilewidth = v32; |
455 | 13.4k | tif->tif_flags |= TIFF_ISTILED; |
456 | 13.4k | break; |
457 | 12.9k | case TIFFTAG_TILELENGTH: |
458 | 12.9k | v32 = (uint32_t)va_arg(ap, uint32_t); |
459 | 12.9k | if (v32 % 16) |
460 | 7.37k | { |
461 | 7.37k | if (tif->tif_mode != O_RDONLY) |
462 | 0 | goto badvalue32; |
463 | 7.37k | TIFFWarningExtR( |
464 | 7.37k | tif, tif->tif_name, |
465 | 7.37k | "Nonstandard tile length %" PRIu32 ", convert file", v32); |
466 | 7.37k | } |
467 | 12.9k | td->td_tilelength = v32; |
468 | 12.9k | tif->tif_flags |= TIFF_ISTILED; |
469 | 12.9k | break; |
470 | 649 | case TIFFTAG_TILEDEPTH: |
471 | 649 | v32 = (uint32_t)va_arg(ap, uint32_t); |
472 | 649 | if (v32 == 0) |
473 | 7 | goto badvalue32; |
474 | 642 | td->td_tiledepth = v32; |
475 | 642 | break; |
476 | 1.08k | case TIFFTAG_DATATYPE: |
477 | 1.08k | v = (uint16_t)va_arg(ap, uint16_vap); |
478 | 1.08k | switch (v) |
479 | 1.08k | { |
480 | 250 | case DATATYPE_VOID: |
481 | 250 | v = SAMPLEFORMAT_VOID; |
482 | 250 | break; |
483 | 597 | case DATATYPE_INT: |
484 | 597 | v = SAMPLEFORMAT_INT; |
485 | 597 | break; |
486 | 14 | case DATATYPE_UINT: |
487 | 14 | v = SAMPLEFORMAT_UINT; |
488 | 14 | break; |
489 | 217 | case DATATYPE_IEEEFP: |
490 | 217 | v = SAMPLEFORMAT_IEEEFP; |
491 | 217 | break; |
492 | 10 | default: |
493 | 10 | goto badvalue; |
494 | 1.08k | } |
495 | 1.07k | td->td_sampleformat = (uint16_t)v; |
496 | 1.07k | break; |
497 | 22.4k | case TIFFTAG_SAMPLEFORMAT: |
498 | 22.4k | v = (uint16_t)va_arg(ap, uint16_vap); |
499 | 22.4k | if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v) |
500 | 61 | goto badvalue; |
501 | 22.4k | td->td_sampleformat = (uint16_t)v; |
502 | | |
503 | | /* Try to fix up the SWAB function for complex data. */ |
504 | 22.4k | if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT && |
505 | 516 | td->td_bitspersample == 32 && |
506 | 144 | tif->tif_postdecode == _TIFFSwab32BitData) |
507 | 51 | tif->tif_postdecode = _TIFFSwab16BitData; |
508 | 22.3k | else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT || |
509 | 21.8k | td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) && |
510 | 621 | td->td_bitspersample == 64 && |
511 | 13 | tif->tif_postdecode == _TIFFSwab64BitData) |
512 | 8 | tif->tif_postdecode = _TIFFSwab32BitData; |
513 | 22.4k | break; |
514 | 639 | case TIFFTAG_IMAGEDEPTH: |
515 | 639 | td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t); |
516 | 639 | break; |
517 | 615 | case TIFFTAG_SUBIFD: |
518 | 615 | if ((tif->tif_flags & TIFF_INSUBIFD) == 0) |
519 | 615 | { |
520 | 615 | td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap); |
521 | 615 | _TIFFsetLong8Array(tif, &td->td_subifd, |
522 | 615 | (uint64_t *)va_arg(ap, uint64_t *), |
523 | 615 | (uint32_t)td->td_nsubifd); |
524 | 615 | } |
525 | 0 | else |
526 | 0 | { |
527 | 0 | TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs", |
528 | 0 | tif->tif_name); |
529 | 0 | status = 0; |
530 | 0 | } |
531 | 615 | break; |
532 | 541 | case TIFFTAG_YCBCRPOSITIONING: |
533 | 541 | td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap); |
534 | 541 | break; |
535 | 30.0k | case TIFFTAG_YCBCRSUBSAMPLING: |
536 | 30.0k | td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap); |
537 | 30.0k | td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap); |
538 | 30.0k | break; |
539 | 124 | case TIFFTAG_TRANSFERFUNCTION: |
540 | 124 | { |
541 | 124 | uint32_t i; |
542 | 124 | v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1; |
543 | 408 | for (i = 0; i < v; i++) |
544 | 284 | _TIFFsetShortArrayExt(tif, &td->td_transferfunction[i], |
545 | 284 | va_arg(ap, uint16_t *), |
546 | 284 | 1U << td->td_bitspersample); |
547 | 124 | break; |
548 | 22.4k | } |
549 | 2.04k | case TIFFTAG_REFERENCEBLACKWHITE: |
550 | | /* XXX should check for null range */ |
551 | 2.04k | _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite, |
552 | 2.04k | va_arg(ap, float *), 6); |
553 | 2.04k | break; |
554 | 1.77k | case TIFFTAG_INKNAMES: |
555 | 1.77k | { |
556 | 1.77k | v = (uint16_t)va_arg(ap, uint16_vap); |
557 | 1.77k | s = va_arg(ap, char *); |
558 | 1.77k | uint16_t ninksinstring; |
559 | 1.77k | ninksinstring = countInkNamesString(tif, v, s); |
560 | 1.77k | status = ninksinstring > 0; |
561 | 1.77k | if (ninksinstring > 0) |
562 | 1.58k | { |
563 | 1.58k | _TIFFsetNString(tif, &td->td_inknames, s, v); |
564 | 1.58k | td->td_inknameslen = (int)v; |
565 | | /* Set NumberOfInks to the value ninksinstring */ |
566 | 1.58k | if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS)) |
567 | 133 | { |
568 | 133 | if (td->td_numberofinks != ninksinstring) |
569 | 71 | { |
570 | 71 | TIFFErrorExtR( |
571 | 71 | tif, module, |
572 | 71 | "Warning %s; Tag %s:\n Value %" PRIu16 |
573 | 71 | " of NumberOfInks is different from the number of " |
574 | 71 | "inks %" PRIu16 |
575 | 71 | ".\n -> NumberOfInks value adapted to %" PRIu16 "", |
576 | 71 | tif->tif_name, fip->field_name, td->td_numberofinks, |
577 | 71 | ninksinstring, ninksinstring); |
578 | 71 | td->td_numberofinks = ninksinstring; |
579 | 71 | } |
580 | 133 | } |
581 | 1.45k | else |
582 | 1.45k | { |
583 | 1.45k | td->td_numberofinks = ninksinstring; |
584 | 1.45k | TIFFSetFieldBit(tif, FIELD_NUMBEROFINKS); |
585 | 1.45k | } |
586 | 1.58k | if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) |
587 | 355 | { |
588 | 355 | if (td->td_numberofinks != td->td_samplesperpixel) |
589 | 163 | { |
590 | 163 | TIFFErrorExtR(tif, module, |
591 | 163 | "Warning %s; Tag %s:\n Value %" PRIu16 |
592 | 163 | " of NumberOfInks is different from the " |
593 | 163 | "SamplesPerPixel value %" PRIu16 "", |
594 | 163 | tif->tif_name, fip->field_name, |
595 | 163 | td->td_numberofinks, |
596 | 163 | td->td_samplesperpixel); |
597 | 163 | } |
598 | 355 | } |
599 | 1.58k | } |
600 | 1.77k | } |
601 | 1.77k | break; |
602 | 730 | case TIFFTAG_NUMBEROFINKS: |
603 | 730 | v = (uint16_t)va_arg(ap, uint16_vap); |
604 | | /* If InkNames already set also NumberOfInks is set accordingly and |
605 | | * should be equal */ |
606 | 730 | if (TIFFFieldSet(tif, FIELD_INKNAMES)) |
607 | 136 | { |
608 | 136 | if (v != td->td_numberofinks) |
609 | 79 | { |
610 | 79 | TIFFErrorExtR( |
611 | 79 | tif, module, |
612 | 79 | "Error %s; Tag %s:\n It is not possible to set the " |
613 | 79 | "value %" PRIu32 |
614 | 79 | " for NumberOfInks\n which is different from the " |
615 | 79 | "number of inks in the InkNames tag (%" PRIu16 ")", |
616 | 79 | tif->tif_name, fip->field_name, v, td->td_numberofinks); |
617 | | /* Do not set / overwrite number of inks already set by |
618 | | * InkNames case accordingly. */ |
619 | 79 | status = 0; |
620 | 79 | } |
621 | 136 | } |
622 | 594 | else |
623 | 594 | { |
624 | 594 | td->td_numberofinks = (uint16_t)v; |
625 | 594 | if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) |
626 | 205 | { |
627 | 205 | if (td->td_numberofinks != td->td_samplesperpixel) |
628 | 123 | { |
629 | 123 | TIFFErrorExtR(tif, module, |
630 | 123 | "Warning %s; Tag %s:\n Value %" PRIu32 |
631 | 123 | " of NumberOfInks is different from the " |
632 | 123 | "SamplesPerPixel value %" PRIu16 "", |
633 | 123 | tif->tif_name, fip->field_name, v, |
634 | 123 | td->td_samplesperpixel); |
635 | 123 | } |
636 | 205 | } |
637 | 594 | } |
638 | 730 | break; |
639 | 0 | case TIFFTAG_PERSAMPLE: |
640 | 0 | v = (uint16_t)va_arg(ap, uint16_vap); |
641 | 0 | if (v == PERSAMPLE_MULTI) |
642 | 0 | tif->tif_flags |= TIFF_PERSAMPLE; |
643 | 0 | else |
644 | 0 | tif->tif_flags &= ~TIFF_PERSAMPLE; |
645 | 0 | break; |
646 | 569k | default: |
647 | 569k | { |
648 | 569k | TIFFTagValue *tv; |
649 | 569k | int tv_size, iCustom; |
650 | | |
651 | | /* |
652 | | * This can happen if multiple images are open with different |
653 | | * codecs which have private tags. The global tag information |
654 | | * table may then have tags that are valid for one file but not |
655 | | * the other. If the client tries to set a tag that is not valid |
656 | | * for the image's codec then we'll arrive here. This |
657 | | * happens, for example, when tiffcp is used to convert between |
658 | | * compression schemes and codec-specific tags are blindly copied. |
659 | | * |
660 | | * This also happens when a FIELD_IGNORE tag is written. |
661 | | */ |
662 | 569k | if (fip->field_bit == FIELD_IGNORE) |
663 | 0 | { |
664 | 0 | TIFFErrorExtR( |
665 | 0 | tif, module, |
666 | 0 | "%s: Ignored %stag \"%s\" (not supported by libtiff)", |
667 | 0 | tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", |
668 | 0 | fip->field_name); |
669 | 0 | status = 0; |
670 | 0 | break; |
671 | 0 | } |
672 | 569k | if (fip->field_bit != FIELD_CUSTOM) |
673 | 0 | { |
674 | 0 | TIFFErrorExtR( |
675 | 0 | tif, module, |
676 | 0 | "%s: Invalid %stag \"%s\" (not supported by codec)", |
677 | 0 | tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", |
678 | 0 | fip->field_name); |
679 | 0 | status = 0; |
680 | 0 | break; |
681 | 0 | } |
682 | | |
683 | | /* |
684 | | * Find the existing entry for this custom value. |
685 | | */ |
686 | 569k | tv = NULL; |
687 | 3.13M | for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) |
688 | 2.56M | { |
689 | 2.56M | if (td->td_customValues[iCustom].info->field_tag == tag) |
690 | 0 | { |
691 | 0 | tv = td->td_customValues + iCustom; |
692 | 0 | if (tv->value != NULL) |
693 | 0 | { |
694 | 0 | _TIFFfreeExt(tif, tv->value); |
695 | 0 | tv->value = NULL; |
696 | 0 | } |
697 | 0 | break; |
698 | 0 | } |
699 | 2.56M | } |
700 | | |
701 | | /* |
702 | | * Grow the custom list if the entry was not found. |
703 | | */ |
704 | 569k | if (tv == NULL) |
705 | 569k | { |
706 | 569k | TIFFTagValue *new_customValues; |
707 | | |
708 | 569k | new_customValues = (TIFFTagValue *)_TIFFreallocExt( |
709 | 569k | tif, td->td_customValues, |
710 | 569k | (tmsize_t)(sizeof(TIFFTagValue) * |
711 | 569k | (size_t)(td->td_customValueCount + 1))); |
712 | 569k | if (!new_customValues) |
713 | 0 | { |
714 | 0 | TIFFErrorExtR(tif, module, |
715 | 0 | "%s: Failed to allocate space for list of " |
716 | 0 | "custom values", |
717 | 0 | tif->tif_name); |
718 | 0 | status = 0; |
719 | 0 | goto end; |
720 | 0 | } |
721 | | |
722 | 569k | td->td_customValueCount++; |
723 | 569k | td->td_customValues = new_customValues; |
724 | | |
725 | 569k | tv = td->td_customValues + (td->td_customValueCount - 1); |
726 | 569k | tv->info = fip; |
727 | 569k | tv->value = NULL; |
728 | 569k | tv->count = 0; |
729 | 569k | } |
730 | | |
731 | | /* |
732 | | * Set custom value ... save a copy of the custom tag value. |
733 | | */ |
734 | | /*--: Rational2Double: For Rationals evaluate "set_get_field_type" |
735 | | * to determine internal storage size. */ |
736 | 569k | tv_size = TIFFFieldSetGetSize(fip); |
737 | 569k | if (tv_size == 0) |
738 | 0 | { |
739 | 0 | status = 0; |
740 | 0 | TIFFErrorExtR(tif, module, "%s: Bad field type %u for \"%s\"", |
741 | 0 | tif->tif_name, fip->field_type, fip->field_name); |
742 | 0 | goto end; |
743 | 0 | } |
744 | | |
745 | 569k | if (fip->field_type == TIFF_ASCII) |
746 | 64.6k | { |
747 | 64.6k | uint32_t ma; |
748 | 64.6k | const char *mb; |
749 | 64.6k | if (fip->field_passcount) |
750 | 39.8k | { |
751 | 39.8k | assert(fip->field_writecount == TIFF_VARIABLE2); |
752 | 39.8k | ma = (uint32_t)va_arg(ap, uint32_t); |
753 | 39.8k | mb = (const char *)va_arg(ap, const char *); |
754 | 39.8k | } |
755 | 24.8k | else |
756 | 24.8k | { |
757 | 24.8k | mb = (const char *)va_arg(ap, const char *); |
758 | 24.8k | size_t len = strlen(mb) + 1; |
759 | 24.8k | if (len >= 0x80000000U) |
760 | 0 | { |
761 | 0 | status = 0; |
762 | 0 | TIFFErrorExtR(tif, module, |
763 | 0 | "%s: Too long string value for \"%s\". " |
764 | 0 | "Maximum supported is 2147483647 bytes", |
765 | 0 | tif->tif_name, fip->field_name); |
766 | 0 | goto end; |
767 | 0 | } |
768 | 24.8k | ma = (uint32_t)len; |
769 | 24.8k | } |
770 | 64.6k | tv->count = (int)ma; |
771 | 64.6k | setByteArray(tif, &tv->value, mb, ma, 1); |
772 | 64.6k | } |
773 | 505k | else |
774 | 505k | { |
775 | 505k | if (fip->field_passcount) |
776 | 379k | { |
777 | 379k | if (fip->field_writecount == TIFF_VARIABLE2) |
778 | 369k | tv->count = (int)va_arg(ap, uint32_t); |
779 | 10.5k | else |
780 | 10.5k | tv->count = va_arg(ap, int); |
781 | 379k | } |
782 | 125k | else if (fip->field_writecount == TIFF_VARIABLE || |
783 | 125k | fip->field_writecount == TIFF_VARIABLE2) |
784 | 0 | tv->count = 1; |
785 | 125k | else if (fip->field_writecount == TIFF_SPP) |
786 | 0 | tv->count = td->td_samplesperpixel; |
787 | 125k | else |
788 | 125k | tv->count = fip->field_writecount; |
789 | | |
790 | 505k | if (tv->count == 0) |
791 | 65.5k | { |
792 | 65.5k | TIFFWarningExtR(tif, module, |
793 | 65.5k | "%s: Null count for \"%s\" (type " |
794 | 65.5k | "%u, writecount %d, passcount %d)", |
795 | 65.5k | tif->tif_name, fip->field_name, |
796 | 65.5k | fip->field_type, fip->field_writecount, |
797 | 65.5k | fip->field_passcount); |
798 | 65.5k | break; |
799 | 65.5k | } |
800 | | |
801 | 439k | tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size, |
802 | 439k | "custom tag binary object"); |
803 | 439k | if (!tv->value) |
804 | 0 | { |
805 | 0 | status = 0; |
806 | 0 | goto end; |
807 | 0 | } |
808 | | |
809 | 439k | if (fip->field_tag == TIFFTAG_DOTRANGE && |
810 | 1.07k | strcmp(fip->field_name, "DotRange") == 0) |
811 | 550 | { |
812 | | /* TODO: This is an evil exception and should not have been |
813 | | handled this way ... likely best if we move it into |
814 | | the directory structure with an explicit field in |
815 | | libtiff 4.1 and assign it a FIELD_ value */ |
816 | 550 | uint16_t v2[2]; |
817 | 550 | v2[0] = (uint16_t)va_arg(ap, int); |
818 | 550 | v2[1] = (uint16_t)va_arg(ap, int); |
819 | 550 | _TIFFmemcpy(tv->value, &v2, 4); |
820 | 550 | } |
821 | | |
822 | 439k | else if (fip->field_passcount || |
823 | 124k | fip->field_writecount == TIFF_VARIABLE || |
824 | 124k | fip->field_writecount == TIFF_VARIABLE2 || |
825 | 124k | fip->field_writecount == TIFF_SPP || tv->count > 1) |
826 | 334k | { |
827 | | /*--: Rational2Double: For Rationals tv_size is set above to |
828 | | * 4 or 8 according to fip->set_get_field_type! */ |
829 | 334k | _TIFFmemcpy(tv->value, va_arg(ap, void *), |
830 | 334k | tv->count * tv_size); |
831 | | /* Test here for too big values for LONG8, IFD8, SLONG8 in |
832 | | * ClassicTIFF and delete custom field from custom list */ |
833 | 334k | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
834 | 330k | { |
835 | 330k | if (tv->info->field_type == TIFF_LONG8 || |
836 | 326k | tv->info->field_type == TIFF_IFD8) |
837 | 6.79k | { |
838 | 6.79k | uint64_t *pui64 = (uint64_t *)tv->value; |
839 | 13.9k | for (int i = 0; i < tv->count; i++) |
840 | 13.1k | { |
841 | 13.1k | if (pui64[i] > 0xffffffffu) |
842 | 6.04k | { |
843 | 6.04k | TIFFErrorExtR( |
844 | 6.04k | tif, module, |
845 | 6.04k | "%s: Bad %s value %" PRIu64 |
846 | 6.04k | " at %d. array position for \"%s\" tag " |
847 | 6.04k | "%u in ClassicTIFF. Tag won't be " |
848 | 6.04k | "written to file", |
849 | 6.04k | tif->tif_name, |
850 | 6.04k | (tv->info->field_type == TIFF_LONG8 |
851 | 6.04k | ? "LONG8" |
852 | 6.04k | : "IFD8"), |
853 | 6.04k | pui64[i], i, fip->field_name, tag); |
854 | 6.04k | goto badvalueifd8long8; |
855 | 6.04k | } |
856 | 13.1k | } |
857 | 6.79k | } |
858 | 323k | else if (tv->info->field_type == TIFF_SLONG8) |
859 | 4.71k | { |
860 | 4.71k | int64_t *pi64 = (int64_t *)tv->value; |
861 | 10.8k | for (int i = 0; i < tv->count; i++) |
862 | 10.0k | { |
863 | 10.0k | if (pi64[i] > 2147483647 || |
864 | 7.24k | pi64[i] < (-2147483647 - 1)) |
865 | 3.86k | { |
866 | 3.86k | TIFFErrorExtR( |
867 | 3.86k | tif, module, |
868 | 3.86k | "%s: Bad SLONG8 value %" PRIi64 |
869 | 3.86k | " at %d. array position for \"%s\" tag " |
870 | 3.86k | "%u in ClassicTIFF. Tag won't be " |
871 | 3.86k | "written to file", |
872 | 3.86k | tif->tif_name, pi64[i], i, |
873 | 3.86k | fip->field_name, tag); |
874 | 3.86k | goto badvalueifd8long8; |
875 | 3.86k | } |
876 | 10.0k | } |
877 | 4.71k | } |
878 | 330k | } |
879 | 334k | } |
880 | 105k | else |
881 | 105k | { |
882 | 105k | char *val = (char *)tv->value; |
883 | 105k | assert(tv->count == 1); |
884 | | |
885 | 105k | switch (fip->field_type) |
886 | 105k | { |
887 | 2.23k | case TIFF_BYTE: |
888 | 2.28k | case TIFF_UNDEFINED: |
889 | 2.28k | { |
890 | 2.28k | uint8_t v2 = (uint8_t)va_arg(ap, int); |
891 | 2.28k | _TIFFmemcpy(val, &v2, tv_size); |
892 | 2.28k | } |
893 | 2.28k | break; |
894 | 0 | case TIFF_SBYTE: |
895 | 0 | { |
896 | 0 | int8_t v2 = (int8_t)va_arg(ap, int); |
897 | 0 | _TIFFmemcpy(val, &v2, tv_size); |
898 | 0 | } |
899 | 0 | break; |
900 | 6.76k | case TIFF_SHORT: |
901 | 6.76k | { |
902 | 6.76k | uint16_t v2 = (uint16_t)va_arg(ap, int); |
903 | 6.76k | _TIFFmemcpy(val, &v2, tv_size); |
904 | 6.76k | } |
905 | 6.76k | break; |
906 | 0 | case TIFF_SSHORT: |
907 | 0 | { |
908 | 0 | int16_t v2 = (int16_t)va_arg(ap, int); |
909 | 0 | _TIFFmemcpy(val, &v2, tv_size); |
910 | 0 | } |
911 | 0 | break; |
912 | 883 | case TIFF_LONG: |
913 | 883 | case TIFF_IFD: |
914 | 883 | { |
915 | 883 | uint32_t v2 = va_arg(ap, uint32_t); |
916 | 883 | _TIFFmemcpy(val, &v2, tv_size); |
917 | 883 | } |
918 | 883 | break; |
919 | 0 | case TIFF_SLONG: |
920 | 0 | { |
921 | 0 | int32_t v2 = va_arg(ap, int32_t); |
922 | 0 | _TIFFmemcpy(val, &v2, tv_size); |
923 | 0 | } |
924 | 0 | break; |
925 | 88.4k | case TIFF_LONG8: |
926 | 88.9k | case TIFF_IFD8: |
927 | 88.9k | { |
928 | 88.9k | uint64_t v2 = va_arg(ap, uint64_t); |
929 | 88.9k | _TIFFmemcpy(val, &v2, tv_size); |
930 | | /* Test here for too big values for ClassicTIFF and |
931 | | * delete custom field from custom list */ |
932 | 88.9k | if (!(tif->tif_flags & TIFF_BIGTIFF) && |
933 | 88.9k | (v2 > 0xffffffffu)) |
934 | 406 | { |
935 | 406 | TIFFErrorExtR( |
936 | 406 | tif, module, |
937 | 406 | "%s: Bad LONG8 or IFD8 value %" PRIu64 |
938 | 406 | " for \"%s\" tag %u in ClassicTIFF. Tag " |
939 | 406 | "won't be written to file", |
940 | 406 | tif->tif_name, v2, fip->field_name, tag); |
941 | 406 | goto badvalueifd8long8; |
942 | 406 | } |
943 | 88.9k | } |
944 | 88.5k | break; |
945 | 88.5k | case TIFF_SLONG8: |
946 | 0 | { |
947 | 0 | int64_t v2 = va_arg(ap, int64_t); |
948 | 0 | _TIFFmemcpy(val, &v2, tv_size); |
949 | | /* Test here for too big values for ClassicTIFF and |
950 | | * delete custom field from custom list */ |
951 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF) && |
952 | 0 | ((v2 > 2147483647) || (v2 < (-2147483647 - 1)))) |
953 | 0 | { |
954 | 0 | TIFFErrorExtR( |
955 | 0 | tif, module, |
956 | 0 | "%s: Bad SLONG8 value %" PRIi64 |
957 | 0 | " for \"%s\" tag %u in ClassicTIFF. Tag " |
958 | 0 | "won't be written to file", |
959 | 0 | tif->tif_name, v2, fip->field_name, tag); |
960 | 0 | goto badvalueifd8long8; |
961 | 0 | } |
962 | 0 | } |
963 | 0 | break; |
964 | 3.72k | case TIFF_RATIONAL: |
965 | 4.98k | case TIFF_SRATIONAL: |
966 | | /*-- Rational2Double: For Rationals tv_size is set |
967 | | * above to 4 or 8 according to |
968 | | * fip->set_get_field_type! |
969 | | */ |
970 | 4.98k | { |
971 | 4.98k | if (tv_size == 8) |
972 | 276 | { |
973 | 276 | double v2 = va_arg(ap, double); |
974 | 276 | _TIFFmemcpy(val, &v2, tv_size); |
975 | 276 | } |
976 | 4.70k | else |
977 | 4.70k | { |
978 | | /*-- default should be tv_size == 4 */ |
979 | 4.70k | float v3 = (float)va_arg(ap, double); |
980 | 4.70k | _TIFFmemcpy(val, &v3, tv_size); |
981 | | /*-- ToDo: After Testing, this should be |
982 | | * removed and tv_size==4 should be set as |
983 | | * default. */ |
984 | 4.70k | if (tv_size != 4) |
985 | 0 | { |
986 | 0 | TIFFErrorExtR(tif, module, |
987 | 0 | "Rational2Double: " |
988 | 0 | ".set_get_field_type " |
989 | 0 | "in not 4 but %d", |
990 | 0 | tv_size); |
991 | 0 | } |
992 | 4.70k | } |
993 | 4.98k | } |
994 | 4.98k | break; |
995 | 122 | case TIFF_FLOAT: |
996 | 122 | { |
997 | 122 | float v2 = |
998 | 122 | _TIFFClampDoubleToFloat(va_arg(ap, double)); |
999 | 122 | _TIFFmemcpy(val, &v2, tv_size); |
1000 | 122 | } |
1001 | 122 | break; |
1002 | 1.09k | case TIFF_DOUBLE: |
1003 | 1.09k | { |
1004 | 1.09k | double v2 = va_arg(ap, double); |
1005 | 1.09k | _TIFFmemcpy(val, &v2, tv_size); |
1006 | 1.09k | } |
1007 | 1.09k | break; |
1008 | 0 | case TIFF_NOTYPE: |
1009 | 0 | case TIFF_ASCII: |
1010 | 0 | default: |
1011 | 0 | _TIFFmemset(val, 0, tv_size); |
1012 | 0 | status = 0; |
1013 | 0 | break; |
1014 | 105k | } |
1015 | 105k | } |
1016 | 439k | } |
1017 | 569k | } |
1018 | 2.06M | } |
1019 | 2.05M | if (status) |
1020 | 2.05M | { |
1021 | 2.05M | const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); |
1022 | 2.05M | if (fip2) |
1023 | 2.05M | TIFFSetFieldBit(tif, fip2->field_bit); |
1024 | 2.05M | tif->tif_flags |= TIFF_DIRTYDIRECT; |
1025 | 2.05M | } |
1026 | | |
1027 | 2.05M | end: |
1028 | 2.05M | va_end(ap); |
1029 | 2.05M | return (status); |
1030 | 1.83k | badvalue: |
1031 | 1.83k | { |
1032 | 1.83k | const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); |
1033 | 1.83k | TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag", |
1034 | 1.83k | tif->tif_name, v, fip2 ? fip2->field_name : "Unknown"); |
1035 | 1.83k | va_end(ap); |
1036 | 1.83k | } |
1037 | 1.83k | return (0); |
1038 | 400 | badvalue32: |
1039 | 400 | { |
1040 | 400 | const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); |
1041 | 400 | TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag", |
1042 | 400 | tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown"); |
1043 | 400 | va_end(ap); |
1044 | 400 | } |
1045 | 400 | return (0); |
1046 | 532 | badvaluedouble: |
1047 | 532 | { |
1048 | 532 | const TIFFField *fip2 = TIFFFieldWithTag(tif, tag); |
1049 | 532 | TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name, |
1050 | 532 | dblval, fip2 ? fip2->field_name : "Unknown"); |
1051 | 532 | va_end(ap); |
1052 | 532 | } |
1053 | 532 | return (0); |
1054 | 10.3k | badvalueifd8long8: |
1055 | 10.3k | { |
1056 | | /* Error message issued already above. */ |
1057 | 10.3k | TIFFTagValue *tv2 = NULL; |
1058 | 10.3k | int iCustom2, iC2; |
1059 | | /* Find the existing entry for this custom value. */ |
1060 | 76.1k | for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++) |
1061 | 76.1k | { |
1062 | 76.1k | if (td->td_customValues[iCustom2].info->field_tag == tag) |
1063 | 10.3k | { |
1064 | 10.3k | tv2 = td->td_customValues + (iCustom2); |
1065 | 10.3k | break; |
1066 | 10.3k | } |
1067 | 76.1k | } |
1068 | 10.3k | if (tv2 != NULL) |
1069 | 10.3k | { |
1070 | | /* Remove custom field from custom list */ |
1071 | 10.3k | if (tv2->value != NULL) |
1072 | 10.3k | { |
1073 | 10.3k | _TIFFfreeExt(tif, tv2->value); |
1074 | 10.3k | tv2->value = NULL; |
1075 | 10.3k | } |
1076 | | /* Shorten list and close gap in customValues list. |
1077 | | * Re-allocation of td_customValues not necessary here. */ |
1078 | 10.3k | td->td_customValueCount--; |
1079 | 10.3k | for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++) |
1080 | 0 | { |
1081 | 0 | td->td_customValues[iC2] = td->td_customValues[iC2 + 1]; |
1082 | 0 | } |
1083 | 10.3k | } |
1084 | 0 | else |
1085 | 0 | { |
1086 | 0 | assert(0); |
1087 | 0 | } |
1088 | 10.3k | va_end(ap); |
1089 | 10.3k | } |
1090 | 10.3k | return (0); |
1091 | 10.3k | } /*-- _TIFFVSetField() --*/ |
1092 | | |
1093 | | /* |
1094 | | * Return 1/0 according to whether or not |
1095 | | * it is permissible to set the tag's value. |
1096 | | * Note that we allow ImageLength to be changed |
1097 | | * so that we can append and extend to images. |
1098 | | * Any other tag may not be altered once writing |
1099 | | * has commenced, unless its value has no effect |
1100 | | * on the format of the data that is written. |
1101 | | */ |
1102 | | static int OkToChangeTag(TIFF *tif, uint32_t tag) |
1103 | 2.14M | { |
1104 | 2.14M | const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); |
1105 | 2.14M | if (!fip) |
1106 | 0 | { /* unknown tag */ |
1107 | 0 | TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32, |
1108 | 0 | tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag); |
1109 | 0 | return (0); |
1110 | 0 | } |
1111 | 2.14M | if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) && |
1112 | 0 | !fip->field_oktochange) |
1113 | 0 | { |
1114 | | /* |
1115 | | * Consult info table to see if tag can be changed |
1116 | | * after we've started writing. We only allow changes |
1117 | | * to those tags that don't/shouldn't affect the |
1118 | | * compression and/or format of the data. |
1119 | | */ |
1120 | 0 | TIFFErrorExtR(tif, "TIFFSetField", |
1121 | 0 | "%s: Cannot modify tag \"%s\" while writing", |
1122 | 0 | tif->tif_name, fip->field_name); |
1123 | 0 | return (0); |
1124 | 0 | } |
1125 | 2.14M | return (1); |
1126 | 2.14M | } |
1127 | | |
1128 | | /* |
1129 | | * Record the value of a field in the |
1130 | | * internal directory structure. The |
1131 | | * field will be written to the file |
1132 | | * when/if the directory structure is |
1133 | | * updated. |
1134 | | */ |
1135 | | int TIFFSetField(TIFF *tif, uint32_t tag, ...) |
1136 | 2.14M | { |
1137 | 2.14M | va_list ap; |
1138 | 2.14M | int status; |
1139 | | |
1140 | 2.14M | va_start(ap, tag); |
1141 | 2.14M | status = TIFFVSetField(tif, tag, ap); |
1142 | 2.14M | va_end(ap); |
1143 | 2.14M | return (status); |
1144 | 2.14M | } |
1145 | | |
1146 | | /* |
1147 | | * Clear the contents of the field in the internal structure. |
1148 | | */ |
1149 | | int TIFFUnsetField(TIFF *tif, uint32_t tag) |
1150 | 0 | { |
1151 | 0 | const TIFFField *fip = TIFFFieldWithTag(tif, tag); |
1152 | 0 | TIFFDirectory *td = &tif->tif_dir; |
1153 | |
|
1154 | 0 | if (!fip) |
1155 | 0 | return 0; |
1156 | | |
1157 | 0 | if (fip->field_bit != FIELD_CUSTOM) |
1158 | 0 | TIFFClrFieldBit(tif, fip->field_bit); |
1159 | 0 | else |
1160 | 0 | { |
1161 | 0 | TIFFTagValue *tv = NULL; |
1162 | 0 | int i; |
1163 | |
|
1164 | 0 | for (i = 0; i < td->td_customValueCount; i++) |
1165 | 0 | { |
1166 | |
|
1167 | 0 | tv = td->td_customValues + i; |
1168 | 0 | if (tv->info->field_tag == tag) |
1169 | 0 | break; |
1170 | 0 | } |
1171 | |
|
1172 | 0 | if (i < td->td_customValueCount) |
1173 | 0 | { |
1174 | 0 | _TIFFfreeExt(tif, tv->value); |
1175 | 0 | for (; i < td->td_customValueCount - 1; i++) |
1176 | 0 | { |
1177 | 0 | td->td_customValues[i] = td->td_customValues[i + 1]; |
1178 | 0 | } |
1179 | 0 | td->td_customValueCount--; |
1180 | 0 | } |
1181 | 0 | } |
1182 | |
|
1183 | 0 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
1184 | |
|
1185 | 0 | return (1); |
1186 | 0 | } |
1187 | | |
1188 | | /* |
1189 | | * Like TIFFSetField, but taking a varargs |
1190 | | * parameter list. This routine is useful |
1191 | | * for building higher-level interfaces on |
1192 | | * top of the library. |
1193 | | */ |
1194 | | int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap) |
1195 | 2.14M | { |
1196 | 2.14M | return OkToChangeTag(tif, tag) |
1197 | 2.14M | ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) |
1198 | 2.14M | : 0; |
1199 | 2.14M | } |
1200 | | |
1201 | | static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap) |
1202 | 2.35M | { |
1203 | 2.35M | TIFFDirectory *td = &tif->tif_dir; |
1204 | 2.35M | int ret_val = 1; |
1205 | 2.35M | uint32_t standard_tag = tag; |
1206 | 2.35M | const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); |
1207 | 2.35M | if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */ |
1208 | 0 | return 0; |
1209 | | |
1210 | | /* |
1211 | | * We want to force the custom code to be used for custom |
1212 | | * fields even if the tag happens to match a well known |
1213 | | * one - important for reinterpreted handling of standard |
1214 | | * tag values in custom directories (i.e. EXIF) |
1215 | | */ |
1216 | 2.35M | if (fip->field_bit == FIELD_CUSTOM) |
1217 | 1.36M | { |
1218 | 1.36M | standard_tag = 0; |
1219 | 1.36M | } |
1220 | | |
1221 | 2.35M | switch (standard_tag) |
1222 | 2.35M | { |
1223 | 1.78k | case TIFFTAG_SUBFILETYPE: |
1224 | 1.78k | *va_arg(ap, uint32_t *) = td->td_subfiletype; |
1225 | 1.78k | break; |
1226 | 111k | case TIFFTAG_IMAGEWIDTH: |
1227 | 111k | *va_arg(ap, uint32_t *) = td->td_imagewidth; |
1228 | 111k | break; |
1229 | 118k | case TIFFTAG_IMAGELENGTH: |
1230 | 118k | *va_arg(ap, uint32_t *) = td->td_imagelength; |
1231 | 118k | break; |
1232 | 68.5k | case TIFFTAG_BITSPERSAMPLE: |
1233 | 68.5k | *va_arg(ap, uint16_t *) = td->td_bitspersample; |
1234 | 68.5k | break; |
1235 | 109k | case TIFFTAG_COMPRESSION: |
1236 | 109k | *va_arg(ap, uint16_t *) = td->td_compression; |
1237 | 109k | break; |
1238 | 246k | case TIFFTAG_PHOTOMETRIC: |
1239 | 246k | *va_arg(ap, uint16_t *) = td->td_photometric; |
1240 | 246k | break; |
1241 | 0 | case TIFFTAG_THRESHHOLDING: |
1242 | 0 | *va_arg(ap, uint16_t *) = td->td_threshholding; |
1243 | 0 | break; |
1244 | 1.25k | case TIFFTAG_FILLORDER: |
1245 | 1.25k | *va_arg(ap, uint16_t *) = td->td_fillorder; |
1246 | 1.25k | break; |
1247 | 10.7k | case TIFFTAG_ORIENTATION: |
1248 | 10.7k | *va_arg(ap, uint16_t *) = td->td_orientation; |
1249 | 10.7k | break; |
1250 | 68.0k | case TIFFTAG_SAMPLESPERPIXEL: |
1251 | 68.0k | *va_arg(ap, uint16_t *) = td->td_samplesperpixel; |
1252 | 68.0k | break; |
1253 | 35.5k | case TIFFTAG_ROWSPERSTRIP: |
1254 | 35.5k | *va_arg(ap, uint32_t *) = td->td_rowsperstrip; |
1255 | 35.5k | break; |
1256 | 1.14k | case TIFFTAG_MINSAMPLEVALUE: |
1257 | 1.14k | *va_arg(ap, uint16_t *) = td->td_minsamplevalue; |
1258 | 1.14k | break; |
1259 | 822 | case TIFFTAG_MAXSAMPLEVALUE: |
1260 | 822 | *va_arg(ap, uint16_t *) = td->td_maxsamplevalue; |
1261 | 822 | break; |
1262 | 0 | case TIFFTAG_SMINSAMPLEVALUE: |
1263 | 0 | if (tif->tif_flags & TIFF_PERSAMPLE) |
1264 | 0 | *va_arg(ap, double **) = td->td_sminsamplevalue; |
1265 | 0 | else |
1266 | 0 | { |
1267 | | /* libtiff historically treats this as a single value. */ |
1268 | 0 | uint16_t i; |
1269 | 0 | double v = td->td_sminsamplevalue[0]; |
1270 | 0 | for (i = 1; i < td->td_samplesperpixel; ++i) |
1271 | 0 | if (td->td_sminsamplevalue[i] < v) |
1272 | 0 | v = td->td_sminsamplevalue[i]; |
1273 | 0 | *va_arg(ap, double *) = v; |
1274 | 0 | } |
1275 | 0 | break; |
1276 | 0 | case TIFFTAG_SMAXSAMPLEVALUE: |
1277 | 0 | if (tif->tif_flags & TIFF_PERSAMPLE) |
1278 | 0 | *va_arg(ap, double **) = td->td_smaxsamplevalue; |
1279 | 0 | else |
1280 | 0 | { |
1281 | | /* libtiff historically treats this as a single value. */ |
1282 | 0 | uint16_t i; |
1283 | 0 | double v = td->td_smaxsamplevalue[0]; |
1284 | 0 | for (i = 1; i < td->td_samplesperpixel; ++i) |
1285 | 0 | if (td->td_smaxsamplevalue[i] > v) |
1286 | 0 | v = td->td_smaxsamplevalue[i]; |
1287 | 0 | *va_arg(ap, double *) = v; |
1288 | 0 | } |
1289 | 0 | break; |
1290 | 5.09k | case TIFFTAG_XRESOLUTION: |
1291 | 5.09k | *va_arg(ap, float *) = td->td_xresolution; |
1292 | 5.09k | break; |
1293 | 5.09k | case TIFFTAG_YRESOLUTION: |
1294 | 5.09k | *va_arg(ap, float *) = td->td_yresolution; |
1295 | 5.09k | break; |
1296 | 109k | case TIFFTAG_PLANARCONFIG: |
1297 | 109k | *va_arg(ap, uint16_t *) = td->td_planarconfig; |
1298 | 109k | break; |
1299 | 2.03k | case TIFFTAG_XPOSITION: |
1300 | 2.03k | *va_arg(ap, float *) = td->td_xposition; |
1301 | 2.03k | break; |
1302 | 2.03k | case TIFFTAG_YPOSITION: |
1303 | 2.03k | *va_arg(ap, float *) = td->td_yposition; |
1304 | 2.03k | break; |
1305 | 1.95k | case TIFFTAG_RESOLUTIONUNIT: |
1306 | 1.95k | *va_arg(ap, uint16_t *) = td->td_resolutionunit; |
1307 | 1.95k | break; |
1308 | 827 | case TIFFTAG_PAGENUMBER: |
1309 | 827 | *va_arg(ap, uint16_t *) = td->td_pagenumber[0]; |
1310 | 827 | *va_arg(ap, uint16_t *) = td->td_pagenumber[1]; |
1311 | 827 | break; |
1312 | 0 | case TIFFTAG_HALFTONEHINTS: |
1313 | 0 | *va_arg(ap, uint16_t *) = td->td_halftonehints[0]; |
1314 | 0 | *va_arg(ap, uint16_t *) = td->td_halftonehints[1]; |
1315 | 0 | break; |
1316 | 2.55k | case TIFFTAG_COLORMAP: |
1317 | 2.55k | *va_arg(ap, const uint16_t **) = td->td_colormap[0]; |
1318 | 2.55k | *va_arg(ap, const uint16_t **) = td->td_colormap[1]; |
1319 | 2.55k | *va_arg(ap, const uint16_t **) = td->td_colormap[2]; |
1320 | 2.55k | break; |
1321 | 0 | case TIFFTAG_STRIPOFFSETS: |
1322 | 0 | case TIFFTAG_TILEOFFSETS: |
1323 | 0 | _TIFFFillStriles(tif); |
1324 | 0 | *va_arg(ap, const uint64_t **) = td->td_stripoffset_p; |
1325 | 0 | if (td->td_stripoffset_p == NULL) |
1326 | 0 | ret_val = 0; |
1327 | 0 | break; |
1328 | 1.26k | case TIFFTAG_STRIPBYTECOUNTS: |
1329 | 1.26k | case TIFFTAG_TILEBYTECOUNTS: |
1330 | 1.26k | _TIFFFillStriles(tif); |
1331 | 1.26k | *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p; |
1332 | 1.26k | if (td->td_stripbytecount_p == NULL) |
1333 | 0 | ret_val = 0; |
1334 | 1.26k | break; |
1335 | 0 | case TIFFTAG_MATTEING: |
1336 | 0 | *va_arg(ap, uint16_t *) = |
1337 | 0 | (td->td_extrasamples == 1 && td->td_sampleinfo && |
1338 | 0 | td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); |
1339 | 0 | break; |
1340 | 17.5k | case TIFFTAG_EXTRASAMPLES: |
1341 | 17.5k | *va_arg(ap, uint16_t *) = td->td_extrasamples; |
1342 | 17.5k | *va_arg(ap, const uint16_t **) = td->td_sampleinfo; |
1343 | 17.5k | break; |
1344 | 20.2k | case TIFFTAG_TILEWIDTH: |
1345 | 20.2k | *va_arg(ap, uint32_t *) = td->td_tilewidth; |
1346 | 20.2k | break; |
1347 | 20.2k | case TIFFTAG_TILELENGTH: |
1348 | 20.2k | *va_arg(ap, uint32_t *) = td->td_tilelength; |
1349 | 20.2k | break; |
1350 | 0 | case TIFFTAG_TILEDEPTH: |
1351 | 0 | *va_arg(ap, uint32_t *) = td->td_tiledepth; |
1352 | 0 | break; |
1353 | 0 | case TIFFTAG_DATATYPE: |
1354 | 0 | switch (td->td_sampleformat) |
1355 | 0 | { |
1356 | 0 | case SAMPLEFORMAT_UINT: |
1357 | 0 | *va_arg(ap, uint16_t *) = DATATYPE_UINT; |
1358 | 0 | break; |
1359 | 0 | case SAMPLEFORMAT_INT: |
1360 | 0 | *va_arg(ap, uint16_t *) = DATATYPE_INT; |
1361 | 0 | break; |
1362 | 0 | case SAMPLEFORMAT_IEEEFP: |
1363 | 0 | *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP; |
1364 | 0 | break; |
1365 | 0 | case SAMPLEFORMAT_VOID: |
1366 | 0 | *va_arg(ap, uint16_t *) = DATATYPE_VOID; |
1367 | 0 | break; |
1368 | 0 | default: |
1369 | 0 | break; |
1370 | 0 | } |
1371 | 0 | break; |
1372 | 17.9k | case TIFFTAG_SAMPLEFORMAT: |
1373 | 17.9k | *va_arg(ap, uint16_t *) = td->td_sampleformat; |
1374 | 17.9k | break; |
1375 | 0 | case TIFFTAG_IMAGEDEPTH: |
1376 | 0 | *va_arg(ap, uint32_t *) = td->td_imagedepth; |
1377 | 0 | break; |
1378 | 0 | case TIFFTAG_SUBIFD: |
1379 | 0 | *va_arg(ap, uint16_t *) = td->td_nsubifd; |
1380 | 0 | *va_arg(ap, const uint64_t **) = td->td_subifd; |
1381 | 0 | break; |
1382 | 0 | case TIFFTAG_YCBCRPOSITIONING: |
1383 | 0 | *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning; |
1384 | 0 | break; |
1385 | 9.04k | case TIFFTAG_YCBCRSUBSAMPLING: |
1386 | 9.04k | *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0]; |
1387 | 9.04k | *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1]; |
1388 | 9.04k | break; |
1389 | 0 | case TIFFTAG_TRANSFERFUNCTION: |
1390 | 0 | *va_arg(ap, const uint16_t **) = td->td_transferfunction[0]; |
1391 | 0 | if (td->td_samplesperpixel - td->td_extrasamples > 1) |
1392 | 0 | { |
1393 | 0 | *va_arg(ap, const uint16_t **) = td->td_transferfunction[1]; |
1394 | 0 | *va_arg(ap, const uint16_t **) = td->td_transferfunction[2]; |
1395 | 0 | } |
1396 | 0 | else |
1397 | 0 | { |
1398 | 0 | *va_arg(ap, const uint16_t **) = NULL; |
1399 | 0 | *va_arg(ap, const uint16_t **) = NULL; |
1400 | 0 | } |
1401 | 0 | break; |
1402 | 263 | case TIFFTAG_REFERENCEBLACKWHITE: |
1403 | 263 | *va_arg(ap, const float **) = td->td_refblackwhite; |
1404 | 263 | break; |
1405 | 0 | case TIFFTAG_INKNAMES: |
1406 | 0 | *va_arg(ap, const char **) = td->td_inknames; |
1407 | 0 | break; |
1408 | 0 | case TIFFTAG_NUMBEROFINKS: |
1409 | 0 | *va_arg(ap, uint16_t *) = td->td_numberofinks; |
1410 | 0 | break; |
1411 | 1.36M | default: |
1412 | 1.36M | { |
1413 | 1.36M | int i; |
1414 | | |
1415 | | /* |
1416 | | * This can happen if multiple images are open |
1417 | | * with different codecs which have private |
1418 | | * tags. The global tag information table may |
1419 | | * then have tags that are valid for one file |
1420 | | * but not the other. If the client tries to |
1421 | | * get a tag that is not valid for the image's |
1422 | | * codec then we'll arrive here. |
1423 | | */ |
1424 | 1.36M | if (fip->field_bit != FIELD_CUSTOM) |
1425 | 0 | { |
1426 | 0 | TIFFErrorExtR(tif, "_TIFFVGetField", |
1427 | 0 | "%s: Invalid %stag \"%s\" " |
1428 | 0 | "(not supported by codec)", |
1429 | 0 | tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", |
1430 | 0 | fip->field_name); |
1431 | 0 | ret_val = 0; |
1432 | 0 | break; |
1433 | 0 | } |
1434 | | |
1435 | | /* |
1436 | | * Do we have a custom value? |
1437 | | */ |
1438 | 1.36M | ret_val = 0; |
1439 | 6.24M | for (i = 0; i < td->td_customValueCount; i++) |
1440 | 5.09M | { |
1441 | 5.09M | TIFFTagValue *tv = td->td_customValues + i; |
1442 | | |
1443 | 5.09M | if (tv->info->field_tag != tag) |
1444 | 4.88M | continue; |
1445 | | |
1446 | 217k | if (fip->field_passcount) |
1447 | 164k | { |
1448 | 164k | if (fip->field_readcount == TIFF_VARIABLE2) |
1449 | 164k | *va_arg(ap, uint32_t *) = (uint32_t)tv->count; |
1450 | 201 | else /* Assume TIFF_VARIABLE */ |
1451 | 201 | *va_arg(ap, uint16_t *) = (uint16_t)tv->count; |
1452 | 164k | *va_arg(ap, const void **) = tv->value; |
1453 | 164k | ret_val = 1; |
1454 | 164k | } |
1455 | 53.0k | else if (fip->field_tag == TIFFTAG_DOTRANGE && |
1456 | 0 | strcmp(fip->field_name, "DotRange") == 0) |
1457 | 0 | { |
1458 | | /* TODO: This is an evil exception and should not have been |
1459 | | handled this way ... likely best if we move it into |
1460 | | the directory structure with an explicit field in |
1461 | | libtiff 4.1 and assign it a FIELD_ value */ |
1462 | 0 | *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0]; |
1463 | 0 | *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1]; |
1464 | 0 | ret_val = 1; |
1465 | 0 | } |
1466 | 53.0k | else |
1467 | 53.0k | { |
1468 | 53.0k | if (fip->field_type == TIFF_ASCII || |
1469 | 42.4k | fip->field_readcount == TIFF_VARIABLE || |
1470 | 42.4k | fip->field_readcount == TIFF_VARIABLE2 || |
1471 | 42.4k | fip->field_readcount == TIFF_SPP || tv->count > 1) |
1472 | 15.3k | { |
1473 | 15.3k | *va_arg(ap, void **) = tv->value; |
1474 | 15.3k | ret_val = 1; |
1475 | 15.3k | } |
1476 | 37.7k | else |
1477 | 37.7k | { |
1478 | 37.7k | char *val = (char *)tv->value; |
1479 | 37.7k | assert(tv->count == 1); |
1480 | 37.7k | switch (fip->field_type) |
1481 | 37.7k | { |
1482 | 930 | case TIFF_BYTE: |
1483 | 974 | case TIFF_UNDEFINED: |
1484 | 974 | *va_arg(ap, uint8_t *) = *(uint8_t *)val; |
1485 | 974 | ret_val = 1; |
1486 | 974 | break; |
1487 | 0 | case TIFF_SBYTE: |
1488 | 0 | *va_arg(ap, int8_t *) = *(int8_t *)val; |
1489 | 0 | ret_val = 1; |
1490 | 0 | break; |
1491 | 1.94k | case TIFF_SHORT: |
1492 | 1.94k | *va_arg(ap, uint16_t *) = *(uint16_t *)val; |
1493 | 1.94k | ret_val = 1; |
1494 | 1.94k | break; |
1495 | 0 | case TIFF_SSHORT: |
1496 | 0 | *va_arg(ap, int16_t *) = *(int16_t *)val; |
1497 | 0 | ret_val = 1; |
1498 | 0 | break; |
1499 | 161 | case TIFF_LONG: |
1500 | 161 | case TIFF_IFD: |
1501 | 161 | *va_arg(ap, uint32_t *) = *(uint32_t *)val; |
1502 | 161 | ret_val = 1; |
1503 | 161 | break; |
1504 | 0 | case TIFF_SLONG: |
1505 | 0 | *va_arg(ap, int32_t *) = *(int32_t *)val; |
1506 | 0 | ret_val = 1; |
1507 | 0 | break; |
1508 | 33.0k | case TIFF_LONG8: |
1509 | 33.0k | case TIFF_IFD8: |
1510 | 33.0k | *va_arg(ap, uint64_t *) = *(uint64_t *)val; |
1511 | 33.0k | ret_val = 1; |
1512 | 33.0k | break; |
1513 | 0 | case TIFF_SLONG8: |
1514 | 0 | *va_arg(ap, int64_t *) = *(int64_t *)val; |
1515 | 0 | ret_val = 1; |
1516 | 0 | break; |
1517 | 1.18k | case TIFF_RATIONAL: |
1518 | 1.55k | case TIFF_SRATIONAL: |
1519 | 1.55k | { |
1520 | | /*-- Rational2Double: For Rationals evaluate |
1521 | | * "set_get_field_type" to determine internal |
1522 | | * storage size and return value size. */ |
1523 | 1.55k | int tv_size = TIFFFieldSetGetSize(fip); |
1524 | 1.55k | if (tv_size == 8) |
1525 | 276 | { |
1526 | 276 | *va_arg(ap, double *) = *(double *)val; |
1527 | 276 | ret_val = 1; |
1528 | 276 | } |
1529 | 1.27k | else |
1530 | 1.27k | { |
1531 | | /*-- default should be tv_size == 4 */ |
1532 | 1.27k | *va_arg(ap, float *) = *(float *)val; |
1533 | 1.27k | ret_val = 1; |
1534 | | /*-- ToDo: After Testing, this should be |
1535 | | * removed and tv_size==4 should be set as |
1536 | | * default. */ |
1537 | 1.27k | if (tv_size != 4) |
1538 | 0 | { |
1539 | 0 | TIFFErrorExtR(tif, "_TIFFVGetField", |
1540 | 0 | "Rational2Double: " |
1541 | 0 | ".set_get_field_type " |
1542 | 0 | "in not 4 but %d", |
1543 | 0 | tv_size); |
1544 | 0 | } |
1545 | 1.27k | } |
1546 | 1.55k | } |
1547 | 1.55k | break; |
1548 | 0 | case TIFF_FLOAT: |
1549 | 0 | *va_arg(ap, float *) = *(float *)val; |
1550 | 0 | ret_val = 1; |
1551 | 0 | break; |
1552 | 0 | case TIFF_DOUBLE: |
1553 | 0 | *va_arg(ap, double *) = *(double *)val; |
1554 | 0 | ret_val = 1; |
1555 | 0 | break; |
1556 | 0 | case TIFF_NOTYPE: |
1557 | 0 | case TIFF_ASCII: |
1558 | 0 | default: |
1559 | 0 | ret_val = 0; |
1560 | 0 | break; |
1561 | 37.7k | } |
1562 | 37.7k | } |
1563 | 53.0k | } |
1564 | 217k | break; |
1565 | 217k | } |
1566 | 1.36M | } |
1567 | 2.35M | } |
1568 | 2.35M | return (ret_val); |
1569 | 2.35M | } |
1570 | | |
1571 | | /* |
1572 | | * Return the value of a field in the |
1573 | | * internal directory structure. |
1574 | | */ |
1575 | | int TIFFGetField(TIFF *tif, uint32_t tag, ...) |
1576 | 2.78M | { |
1577 | 2.78M | int status; |
1578 | 2.78M | va_list ap; |
1579 | | |
1580 | 2.78M | va_start(ap, tag); |
1581 | 2.78M | status = TIFFVGetField(tif, tag, ap); |
1582 | 2.78M | va_end(ap); |
1583 | 2.78M | return (status); |
1584 | 2.78M | } |
1585 | | |
1586 | | /* |
1587 | | * Like TIFFGetField, but taking a varargs |
1588 | | * parameter list. This routine is useful |
1589 | | * for building higher-level interfaces on |
1590 | | * top of the library. |
1591 | | */ |
1592 | | int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap) |
1593 | 4.35M | { |
1594 | 4.35M | const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY); |
1595 | 4.35M | return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) |
1596 | 4.35M | ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) |
1597 | 4.35M | : 0); |
1598 | 4.35M | } |
1599 | | |
1600 | | #define CleanupField(member) \ |
1601 | 5.12M | { \ |
1602 | 5.12M | if (td->member) \ |
1603 | 5.12M | { \ |
1604 | 426k | _TIFFfreeExt(tif, td->member); \ |
1605 | 426k | td->member = 0; \ |
1606 | 426k | } \ |
1607 | 5.12M | } |
1608 | | |
1609 | | /* |
1610 | | * Reset tif->tif_dir structure to zero and |
1611 | | * initialize some IFD strile counter and index parameters. |
1612 | | */ |
1613 | | void _TIFFResetTifDirAndInitStrileCounters(TIFFDirectory *td) |
1614 | 341k | { |
1615 | 341k | _TIFFmemset(td, 0, sizeof(*td)); |
1616 | 341k | td->td_curstrip = NOSTRIP; /* invalid strip = NOSTRIP */ |
1617 | 341k | td->td_row = (uint32_t)-1; /* read/write pre-increment */ |
1618 | 341k | td->td_col = (uint32_t)-1; /* read/write pre-increment */ |
1619 | 341k | td->td_scanlinesize = 0; /* initialize to zero */ |
1620 | 341k | td->td_curtile = NOTILE; /* invalid tile = NOTILE */ |
1621 | 341k | td->td_tilesize = (tmsize_t)-1; /* invalidate tilezize */ |
1622 | 341k | } |
1623 | | |
1624 | | /* |
1625 | | * Release storage associated with a directory. |
1626 | | */ |
1627 | | void TIFFFreeDirectory(TIFF *tif) |
1628 | 341k | { |
1629 | 341k | TIFFDirectory *td = &tif->tif_dir; |
1630 | 341k | int i; |
1631 | | |
1632 | 341k | (*tif->tif_cleanup)(tif); |
1633 | 341k | _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset)); |
1634 | 341k | CleanupField(td_sminsamplevalue); |
1635 | 341k | CleanupField(td_smaxsamplevalue); |
1636 | 341k | CleanupField(td_colormap[0]); |
1637 | 341k | CleanupField(td_colormap[1]); |
1638 | 341k | CleanupField(td_colormap[2]); |
1639 | 341k | CleanupField(td_sampleinfo); |
1640 | 341k | CleanupField(td_subifd); |
1641 | 341k | CleanupField(td_inknames); |
1642 | 341k | CleanupField(td_refblackwhite); |
1643 | 341k | CleanupField(td_transferfunction[0]); |
1644 | 341k | CleanupField(td_transferfunction[1]); |
1645 | 341k | CleanupField(td_transferfunction[2]); |
1646 | 341k | CleanupField(td_stripoffset_p); |
1647 | 341k | CleanupField(td_stripbytecount_p); |
1648 | 341k | td->td_stripoffsetbyteallocsize = 0; |
1649 | 341k | TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING); |
1650 | 341k | TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING); |
1651 | | |
1652 | | /* Cleanup custom tag values */ |
1653 | 901k | for (i = 0; i < td->td_customValueCount; i++) |
1654 | 559k | { |
1655 | 559k | if (td->td_customValues[i].value) |
1656 | 483k | _TIFFfreeExt(tif, td->td_customValues[i].value); |
1657 | 559k | } |
1658 | | |
1659 | 341k | td->td_customValueCount = 0; |
1660 | 341k | CleanupField(td_customValues); |
1661 | | |
1662 | 341k | _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry)); |
1663 | 341k | _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry)); |
1664 | | |
1665 | | /* Reset some internal parameters for IFD data size checking. */ |
1666 | 341k | tif->tif_dir.td_dirdatasize_read = 0; |
1667 | 341k | tif->tif_dir.td_dirdatasize_write = 0; |
1668 | 341k | if (tif->tif_dir.td_dirdatasize_offsets != NULL) |
1669 | 186k | { |
1670 | 186k | _TIFFfreeExt(tif, tif->tif_dir.td_dirdatasize_offsets); |
1671 | 186k | tif->tif_dir.td_dirdatasize_offsets = NULL; |
1672 | 186k | tif->tif_dir.td_dirdatasize_Noffsets = 0; |
1673 | 186k | } |
1674 | 341k | tif->tif_dir.td_iswrittentofile = FALSE; |
1675 | | /* Note: tif->tif_dir structure is set to zero in TIFFDefaultDirectory() */ |
1676 | 341k | } |
1677 | | #undef CleanupField |
1678 | | |
1679 | | /* |
1680 | | * Client Tag extension support (from Niles Ritter). |
1681 | | */ |
1682 | | static TIFFExtendProc _TIFFextender = (TIFFExtendProc)NULL; |
1683 | | |
1684 | | TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender) |
1685 | 16 | { |
1686 | 16 | TIFFExtendProc prev = _TIFFextender; |
1687 | 16 | _TIFFextender = extender; |
1688 | 16 | return (prev); |
1689 | 16 | } |
1690 | | |
1691 | | /* |
1692 | | * Setup for a new directory. Should we automatically call |
1693 | | * TIFFWriteDirectory() if the current one is dirty? |
1694 | | * |
1695 | | * The newly created directory will not exist on the file till |
1696 | | * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called. |
1697 | | */ |
1698 | | int TIFFCreateDirectory(TIFF *tif) |
1699 | 10.3k | { |
1700 | | /* Free previously allocated memory and setup default values. */ |
1701 | 10.3k | TIFFFreeDirectory(tif); |
1702 | 10.3k | TIFFDefaultDirectory(tif); |
1703 | 10.3k | tif->tif_diroff = 0; |
1704 | 10.3k | tif->tif_nextdiroff = 0; |
1705 | 10.3k | tif->tif_curoff = 0; |
1706 | 10.3k | tif->tif_dir.td_iswrittentofile = FALSE; |
1707 | 10.3k | return 0; |
1708 | 10.3k | } |
1709 | | |
1710 | | int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray) |
1711 | 0 | { |
1712 | | /* Free previously allocated memory and setup default values. */ |
1713 | 0 | TIFFFreeDirectory(tif); |
1714 | 0 | TIFFDefaultDirectory(tif); |
1715 | | |
1716 | | /* |
1717 | | * Reset the field definitions to match the application provided list. |
1718 | | * Hopefully TIFFDefaultDirectory() won't have done anything irreversible |
1719 | | * based on it's assumption this is an image directory. |
1720 | | */ |
1721 | 0 | _TIFFSetupFields(tif, infoarray); |
1722 | |
|
1723 | 0 | tif->tif_diroff = 0; |
1724 | 0 | tif->tif_nextdiroff = 0; |
1725 | 0 | tif->tif_curoff = 0; |
1726 | | /* invalidate directory index */ |
1727 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
1728 | | /* invalidate IFD loop lists */ |
1729 | 0 | _TIFFCleanupIFDOffsetAndNumberMaps(tif); |
1730 | | /* To be able to return from SubIFD or custom-IFD to main-IFD */ |
1731 | 0 | tif->tif_setdirectory_force_absolute = TRUE; |
1732 | 0 | return 0; |
1733 | 0 | } |
1734 | | |
1735 | | int TIFFCreateEXIFDirectory(TIFF *tif) |
1736 | 0 | { |
1737 | 0 | const TIFFFieldArray *exifFieldArray; |
1738 | 0 | exifFieldArray = _TIFFGetExifFields(); |
1739 | 0 | return TIFFCreateCustomDirectory(tif, exifFieldArray); |
1740 | 0 | } |
1741 | | |
1742 | | /* |
1743 | | * Creates the EXIF GPS custom directory |
1744 | | */ |
1745 | | int TIFFCreateGPSDirectory(TIFF *tif) |
1746 | 0 | { |
1747 | 0 | const TIFFFieldArray *gpsFieldArray; |
1748 | 0 | gpsFieldArray = _TIFFGetGpsFields(); |
1749 | 0 | return TIFFCreateCustomDirectory(tif, gpsFieldArray); |
1750 | 0 | } |
1751 | | |
1752 | | /* |
1753 | | * Setup a default directory structure. |
1754 | | */ |
1755 | | int TIFFDefaultDirectory(TIFF *tif) |
1756 | 206k | { |
1757 | 206k | TIFFDirectory *td = &tif->tif_dir; |
1758 | 206k | const TIFFFieldArray *tiffFieldArray; |
1759 | | |
1760 | 206k | tiffFieldArray = _TIFFGetFields(); |
1761 | 206k | _TIFFSetupFields(tif, tiffFieldArray); |
1762 | | /* Reset tif->tif_dir structure to zero and |
1763 | | * initialize some IFD strile counter and index parameters. */ |
1764 | 206k | _TIFFResetTifDirAndInitStrileCounters(td); |
1765 | 206k | td->td_fillorder = FILLORDER_MSB2LSB; |
1766 | 206k | td->td_bitspersample = 1; |
1767 | 206k | td->td_threshholding = THRESHHOLD_BILEVEL; |
1768 | 206k | td->td_orientation = ORIENTATION_TOPLEFT; |
1769 | 206k | td->td_samplesperpixel = 1; |
1770 | 206k | td->td_rowsperstrip = (uint32_t)-1; |
1771 | 206k | td->td_tilewidth = 0; |
1772 | 206k | td->td_tilelength = 0; |
1773 | 206k | td->td_tiledepth = 1; |
1774 | | #ifdef STRIPBYTECOUNTSORTED_UNUSED |
1775 | | td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */ |
1776 | | #endif |
1777 | 206k | td->td_resolutionunit = RESUNIT_INCH; |
1778 | 206k | td->td_sampleformat = SAMPLEFORMAT_UINT; |
1779 | 206k | td->td_imagedepth = 1; |
1780 | 206k | td->td_ycbcrsubsampling[0] = 2; |
1781 | 206k | td->td_ycbcrsubsampling[1] = 2; |
1782 | 206k | td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED; |
1783 | 206k | tif->tif_postdecode = _TIFFNoPostDecode; |
1784 | 206k | tif->tif_foundfield = NULL; |
1785 | 206k | tif->tif_tagmethods.vsetfield = _TIFFVSetField; |
1786 | 206k | tif->tif_tagmethods.vgetfield = _TIFFVGetField; |
1787 | 206k | tif->tif_tagmethods.printdir = NULL; |
1788 | | /* additional default values */ |
1789 | 206k | td->td_planarconfig = PLANARCONFIG_CONTIG; |
1790 | 206k | td->td_compression = COMPRESSION_NONE; |
1791 | 206k | td->td_subfiletype = 0; |
1792 | 206k | td->td_minsamplevalue = 0; |
1793 | | /* td_bitspersample=1 is always set in TIFFDefaultDirectory(). |
1794 | | * Therefore, td_maxsamplevalue has to be re-calculated in |
1795 | | * TIFFGetFieldDefaulted(). */ |
1796 | 206k | td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */ |
1797 | 206k | td->td_extrasamples = 0; |
1798 | 206k | td->td_sampleinfo = NULL; |
1799 | | |
1800 | | /* |
1801 | | * Give client code a chance to install their own |
1802 | | * tag extensions & methods, prior to compression overloads, |
1803 | | * but do some prior cleanup first. |
1804 | | * (http://trac.osgeo.org/gdal/ticket/5054) |
1805 | | */ |
1806 | 206k | if (tif->tif_nfieldscompat > 0) |
1807 | 75.9k | { |
1808 | 75.9k | uint32_t i; |
1809 | | |
1810 | 151k | for (i = 0; i < tif->tif_nfieldscompat; i++) |
1811 | 75.9k | { |
1812 | 75.9k | if (tif->tif_fieldscompat[i].allocated_size) |
1813 | 75.9k | _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields); |
1814 | 75.9k | } |
1815 | 75.9k | _TIFFfreeExt(tif, tif->tif_fieldscompat); |
1816 | 75.9k | tif->tif_nfieldscompat = 0; |
1817 | 75.9k | tif->tif_fieldscompat = NULL; |
1818 | 75.9k | } |
1819 | 206k | if (_TIFFextender) |
1820 | 206k | (*_TIFFextender)(tif); |
1821 | 206k | (void)TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
1822 | | /* |
1823 | | * NB: The directory is marked dirty as a result of setting |
1824 | | * up the default compression scheme. However, this really |
1825 | | * isn't correct -- we want TIFF_DIRTYDIRECT to be set only |
1826 | | * if the user does something. We could just do the setup |
1827 | | * by hand, but it seems better to use the normal mechanism |
1828 | | * (i.e. TIFFSetField). |
1829 | | */ |
1830 | 206k | tif->tif_flags &= ~TIFF_DIRTYDIRECT; |
1831 | | |
1832 | | /* |
1833 | | * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19 |
1834 | | * we clear the ISTILED flag when setting up a new directory. |
1835 | | * Should we also be clearing stuff like INSUBIFD? |
1836 | | */ |
1837 | 206k | tif->tif_flags &= ~TIFF_ISTILED; |
1838 | | |
1839 | 206k | return (1); |
1840 | 206k | } |
1841 | | |
1842 | | static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off, |
1843 | | tdir_t *nextdirnum) |
1844 | 47.5k | { |
1845 | 47.5k | static const char module[] = "TIFFAdvanceDirectory"; |
1846 | | |
1847 | | /* Add this directory to the directory list, if not already in. */ |
1848 | 47.5k | if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff)) |
1849 | 0 | { |
1850 | 0 | TIFFErrorExtR(tif, module, |
1851 | 0 | "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64 |
1852 | 0 | ") might cause an IFD loop", |
1853 | 0 | *nextdirnum, *nextdiroff, *nextdiroff); |
1854 | 0 | *nextdiroff = 0; |
1855 | 0 | *nextdirnum = 0; |
1856 | 0 | return (0); |
1857 | 0 | } |
1858 | | |
1859 | 47.5k | if (isMapped(tif)) |
1860 | 47.5k | { |
1861 | 47.5k | uint64_t poff = *nextdiroff; |
1862 | 47.5k | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
1863 | 47.5k | { |
1864 | 47.5k | tmsize_t poffa, poffb, poffc, poffd; |
1865 | 47.5k | uint16_t dircount; |
1866 | 47.5k | uint32_t nextdir32; |
1867 | 47.5k | if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint16_t) || |
1868 | 47.5k | poff > (uint64_t)tif->tif_size - sizeof(uint16_t)) |
1869 | 3.00k | { |
1870 | 3.00k | TIFFErrorExtR(tif, module, |
1871 | 3.00k | "%s:%d: %s: Error fetching directory count", |
1872 | 3.00k | __FILE__, __LINE__, tif->tif_name); |
1873 | 3.00k | *nextdiroff = 0; |
1874 | 3.00k | return (0); |
1875 | 3.00k | } |
1876 | 44.5k | poffa = (tmsize_t)poff; |
1877 | 44.5k | poffb = poffa + (tmsize_t)sizeof(uint16_t); |
1878 | 44.5k | _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t)); |
1879 | 44.5k | if (tif->tif_flags & TIFF_SWAB) |
1880 | 30.8k | TIFFSwabShort(&dircount); |
1881 | 44.5k | if (poffb > |
1882 | 44.5k | TIFF_TMSIZE_T_MAX - dircount * 12 - (tmsize_t)sizeof(uint32_t)) |
1883 | 0 | { |
1884 | 0 | TIFFErrorExtR(tif, module, "Error fetching directory link"); |
1885 | 0 | return (0); |
1886 | 0 | } |
1887 | 44.5k | poffc = poffb + dircount * 12; |
1888 | 44.5k | poffd = poffc + (tmsize_t)sizeof(uint32_t); |
1889 | 44.5k | if (poffd > tif->tif_size) |
1890 | 852 | { |
1891 | 852 | TIFFErrorExtR(tif, module, "Error fetching directory link"); |
1892 | 852 | return (0); |
1893 | 852 | } |
1894 | 43.6k | if (off != NULL) |
1895 | 0 | *off = (uint64_t)poffc; |
1896 | 43.6k | _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t)); |
1897 | 43.6k | if (tif->tif_flags & TIFF_SWAB) |
1898 | 30.1k | TIFFSwabLong(&nextdir32); |
1899 | 43.6k | *nextdiroff = nextdir32; |
1900 | 43.6k | } |
1901 | 0 | else |
1902 | 0 | { |
1903 | 0 | tmsize_t poffa, poffb, poffc, poffd; |
1904 | 0 | uint64_t dircount64; |
1905 | 0 | if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t)) |
1906 | 0 | { |
1907 | 0 | TIFFErrorExtR(tif, module, |
1908 | 0 | "%s:%d: %s: Error fetching directory count", |
1909 | 0 | __FILE__, __LINE__, tif->tif_name); |
1910 | 0 | return (0); |
1911 | 0 | } |
1912 | 0 | poffa = (tmsize_t)poff; |
1913 | 0 | poffb = poffa + (tmsize_t)sizeof(uint64_t); |
1914 | 0 | if (poffb > tif->tif_size) |
1915 | 0 | { |
1916 | 0 | TIFFErrorExtR(tif, module, |
1917 | 0 | "%s:%d: %s: Error fetching directory count", |
1918 | 0 | __FILE__, __LINE__, tif->tif_name); |
1919 | 0 | return (0); |
1920 | 0 | } |
1921 | 0 | _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t)); |
1922 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1923 | 0 | TIFFSwabLong8(&dircount64); |
1924 | 0 | if (dircount64 > 0xFFFF) |
1925 | 0 | { |
1926 | 0 | TIFFErrorExtR(tif, module, |
1927 | 0 | "Sanity check on directory count failed"); |
1928 | 0 | return (0); |
1929 | 0 | } |
1930 | 0 | if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount64 * 20) - |
1931 | 0 | (tmsize_t)sizeof(uint64_t)) |
1932 | 0 | { |
1933 | 0 | TIFFErrorExtR(tif, module, "Error fetching directory link"); |
1934 | 0 | return (0); |
1935 | 0 | } |
1936 | 0 | poffc = poffb + (tmsize_t)(dircount64 * 20); |
1937 | 0 | poffd = poffc + (tmsize_t)sizeof(uint64_t); |
1938 | 0 | if (poffd > tif->tif_size) |
1939 | 0 | { |
1940 | 0 | TIFFErrorExtR(tif, module, "Error fetching directory link"); |
1941 | 0 | return (0); |
1942 | 0 | } |
1943 | 0 | if (off != NULL) |
1944 | 0 | *off = (uint64_t)poffc; |
1945 | 0 | _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t)); |
1946 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1947 | 0 | TIFFSwabLong8(nextdiroff); |
1948 | 0 | } |
1949 | 47.5k | } |
1950 | 0 | else |
1951 | 0 | { |
1952 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
1953 | 0 | { |
1954 | 0 | uint16_t dircount; |
1955 | 0 | uint32_t nextdir32; |
1956 | 0 | if (!SeekOK(tif, *nextdiroff) || |
1957 | 0 | !ReadOK(tif, &dircount, sizeof(uint16_t))) |
1958 | 0 | { |
1959 | 0 | TIFFErrorExtR(tif, module, |
1960 | 0 | "%s:%d: %s: Error fetching directory count", |
1961 | 0 | __FILE__, __LINE__, tif->tif_name); |
1962 | 0 | return (0); |
1963 | 0 | } |
1964 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1965 | 0 | TIFFSwabShort(&dircount); |
1966 | 0 | if (off != NULL) |
1967 | 0 | *off = TIFFSeekFile(tif, dircount * 12U, SEEK_CUR); |
1968 | 0 | else |
1969 | 0 | (void)TIFFSeekFile(tif, dircount * 12U, SEEK_CUR); |
1970 | 0 | if (!ReadOK(tif, &nextdir32, sizeof(uint32_t))) |
1971 | 0 | { |
1972 | 0 | TIFFErrorExtR(tif, module, "%s: Error fetching directory link", |
1973 | 0 | tif->tif_name); |
1974 | 0 | return (0); |
1975 | 0 | } |
1976 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1977 | 0 | TIFFSwabLong(&nextdir32); |
1978 | 0 | *nextdiroff = nextdir32; |
1979 | 0 | } |
1980 | 0 | else |
1981 | 0 | { |
1982 | 0 | uint64_t dircount64; |
1983 | 0 | if (!SeekOK(tif, *nextdiroff) || |
1984 | 0 | !ReadOK(tif, &dircount64, sizeof(uint64_t))) |
1985 | 0 | { |
1986 | 0 | TIFFErrorExtR(tif, module, |
1987 | 0 | "%s:%d: %s: Error fetching directory count", |
1988 | 0 | __FILE__, __LINE__, tif->tif_name); |
1989 | 0 | return (0); |
1990 | 0 | } |
1991 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1992 | 0 | TIFFSwabLong8(&dircount64); |
1993 | 0 | if (dircount64 > 0xFFFF) |
1994 | 0 | { |
1995 | 0 | TIFFErrorExtR(tif, module, |
1996 | 0 | "%s:%d: %s: Error fetching directory count", |
1997 | 0 | __FILE__, __LINE__, tif->tif_name); |
1998 | 0 | return (0); |
1999 | 0 | } |
2000 | 0 | if (off != NULL) |
2001 | 0 | *off = TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR); |
2002 | 0 | else |
2003 | 0 | (void)TIFFSeekFile(tif, dircount64 * 20, SEEK_CUR); |
2004 | 0 | if (!ReadOK(tif, nextdiroff, sizeof(uint64_t))) |
2005 | 0 | { |
2006 | 0 | TIFFErrorExtR(tif, module, "%s: Error fetching directory link", |
2007 | 0 | tif->tif_name); |
2008 | 0 | return (0); |
2009 | 0 | } |
2010 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2011 | 0 | TIFFSwabLong8(nextdiroff); |
2012 | 0 | } |
2013 | 0 | } |
2014 | 43.6k | if (*nextdiroff != 0) |
2015 | 27.6k | { |
2016 | 27.6k | (*nextdirnum)++; |
2017 | | /* Check next directory for IFD looping and if so, set it as last |
2018 | | * directory. */ |
2019 | 27.6k | if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff)) |
2020 | 325 | { |
2021 | 325 | TIFFWarningExtR( |
2022 | 325 | tif, module, |
2023 | 325 | "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64 |
2024 | 325 | ") might be an IFD loop. Treating directory %d as " |
2025 | 325 | "last directory", |
2026 | 325 | *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1); |
2027 | 325 | *nextdiroff = 0; |
2028 | 325 | (*nextdirnum)--; |
2029 | 325 | } |
2030 | 27.6k | } |
2031 | 43.6k | return (1); |
2032 | 47.5k | } |
2033 | | |
2034 | | /* |
2035 | | * Count the number of directories in a file. |
2036 | | */ |
2037 | | tdir_t TIFFNumberOfDirectories(TIFF *tif) |
2038 | 20.1k | { |
2039 | 20.1k | uint64_t nextdiroff; |
2040 | 20.1k | tdir_t nextdirnum; |
2041 | 20.1k | tdir_t n; |
2042 | 20.1k | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
2043 | 20.1k | nextdiroff = tif->tif_header.classic.tiff_diroff; |
2044 | 0 | else |
2045 | 0 | nextdiroff = tif->tif_header.big.tiff_diroff; |
2046 | 20.1k | nextdirnum = 0; |
2047 | 20.1k | n = 0; |
2048 | 40.0k | while (nextdiroff != 0 && |
2049 | 23.7k | TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum)) |
2050 | 19.9k | { |
2051 | 19.9k | ++n; |
2052 | 19.9k | } |
2053 | | /* Update number of main-IFDs in file. */ |
2054 | 20.1k | tif->tif_curdircount = n; |
2055 | 20.1k | return (n); |
2056 | 20.1k | } |
2057 | | |
2058 | | /* |
2059 | | * Set the n-th directory as the current directory. |
2060 | | * NB: Directories are numbered starting at 0. |
2061 | | */ |
2062 | | int TIFFSetDirectory(TIFF *tif, tdir_t dirn) |
2063 | 33.0k | { |
2064 | 33.0k | uint64_t nextdiroff; |
2065 | 33.0k | tdir_t nextdirnum = 0; |
2066 | 33.0k | tdir_t n; |
2067 | | |
2068 | 33.0k | if (tif->tif_setdirectory_force_absolute) |
2069 | 17.4k | { |
2070 | | /* tif_setdirectory_force_absolute=1 will force parsing the main IFD |
2071 | | * chain from the beginning, thus IFD directory list needs to be cleared |
2072 | | * from possible SubIFD offsets. |
2073 | | */ |
2074 | 17.4k | _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */ |
2075 | 17.4k | } |
2076 | | |
2077 | | /* Even faster path, if offset is available within IFD loop hash list. */ |
2078 | 33.0k | if (!tif->tif_setdirectory_force_absolute && |
2079 | 15.6k | _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff)) |
2080 | 15.6k | { |
2081 | | /* Set parameters for following TIFFReadDirectory() below. */ |
2082 | 15.6k | tif->tif_nextdiroff = nextdiroff; |
2083 | 15.6k | tif->tif_curdir = dirn; |
2084 | | /* Reset to relative stepping */ |
2085 | 15.6k | tif->tif_setdirectory_force_absolute = FALSE; |
2086 | 15.6k | } |
2087 | 17.4k | else |
2088 | 17.4k | { |
2089 | | |
2090 | | /* Fast path when we just advance relative to the current directory: |
2091 | | * start at the current dir offset and continue to seek from there. |
2092 | | * Check special cases when relative is not allowed: |
2093 | | * - jump back from SubIFD or custom directory |
2094 | | * - right after TIFFWriteDirectory() jump back to that directory |
2095 | | * using TIFFSetDirectory() */ |
2096 | 17.4k | const int relative = (dirn >= tif->tif_curdir) && |
2097 | 17.4k | (tif->tif_diroff != 0) && |
2098 | 17.4k | !tif->tif_setdirectory_force_absolute; |
2099 | | |
2100 | 17.4k | if (relative) |
2101 | 0 | { |
2102 | 0 | nextdiroff = tif->tif_diroff; |
2103 | 0 | dirn -= tif->tif_curdir; |
2104 | 0 | nextdirnum = tif->tif_curdir; |
2105 | 0 | } |
2106 | 17.4k | else if (!(tif->tif_flags & TIFF_BIGTIFF)) |
2107 | 17.4k | nextdiroff = tif->tif_header.classic.tiff_diroff; |
2108 | 0 | else |
2109 | 0 | nextdiroff = tif->tif_header.big.tiff_diroff; |
2110 | | |
2111 | | /* Reset to relative stepping */ |
2112 | 17.4k | tif->tif_setdirectory_force_absolute = FALSE; |
2113 | | |
2114 | 41.1k | for (n = dirn; n > 0 && nextdiroff != 0; n--) |
2115 | 23.7k | if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum)) |
2116 | 0 | return (0); |
2117 | | /* If the n-th directory could not be reached (does not exist), |
2118 | | * return here without touching anything further. */ |
2119 | 17.4k | if (nextdiroff == 0 || n > 0) |
2120 | 0 | return (0); |
2121 | | |
2122 | 17.4k | tif->tif_nextdiroff = nextdiroff; |
2123 | | |
2124 | | /* Set curdir to the actual directory index. */ |
2125 | 17.4k | if (relative) |
2126 | 0 | tif->tif_curdir += dirn - n; |
2127 | 17.4k | else |
2128 | 17.4k | tif->tif_curdir = dirn - n; |
2129 | 17.4k | } |
2130 | | |
2131 | | /* The -1 decrement is because TIFFReadDirectory will increment |
2132 | | * tif_curdir after successfully reading the directory. */ |
2133 | 33.0k | if (tif->tif_curdir == 0) |
2134 | 15.4k | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2135 | 17.5k | else |
2136 | 17.5k | tif->tif_curdir--; |
2137 | | |
2138 | 33.0k | tdir_t curdir = tif->tif_curdir; |
2139 | | |
2140 | 33.0k | int retval = TIFFReadDirectory(tif); |
2141 | | |
2142 | 33.0k | if (!retval && tif->tif_curdir == curdir) |
2143 | 0 | { |
2144 | | /* If tif_curdir has not be incremented, TIFFFetchDirectory() in |
2145 | | * TIFFReadDirectory() has failed and tif_curdir shall be set |
2146 | | * specifically. */ |
2147 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2148 | 0 | } |
2149 | 33.0k | return (retval); |
2150 | 33.0k | } |
2151 | | |
2152 | | /* |
2153 | | * Set the current directory to be the directory |
2154 | | * located at the specified file offset. This interface |
2155 | | * is used mainly to access directories linked with |
2156 | | * the SubIFD tag (e.g. thumbnail images). |
2157 | | */ |
2158 | | int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff) |
2159 | 0 | { |
2160 | | /* Match nextdiroff and curdir for consistent IFD-loop checking. |
2161 | | * Only with TIFFSetSubDirectory() the IFD list can be corrupted with |
2162 | | * invalid offsets within the main IFD tree. In the case of several subIFDs |
2163 | | * of a main image, there are two possibilities that are not even mutually |
2164 | | * exclusive. a.) The subIFD tag contains an array with all offsets of the |
2165 | | * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters. |
2166 | | * (refer to |
2167 | | * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.) |
2168 | | */ |
2169 | 0 | int retval; |
2170 | 0 | uint32_t curdir = 0; |
2171 | 0 | int8_t probablySubIFD = 0; |
2172 | 0 | if (diroff == 0) |
2173 | 0 | { |
2174 | | /* Special case to set tif_diroff=0, which is done in |
2175 | | * TIFFReadDirectory() below to indicate that the currently read IFD is |
2176 | | * treated as a new, fresh IFD. */ |
2177 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2178 | 0 | tif->tif_dir.td_iswrittentofile = FALSE; |
2179 | 0 | } |
2180 | 0 | else |
2181 | 0 | { |
2182 | 0 | if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir)) |
2183 | 0 | { |
2184 | | /* Non-existing offsets might point to a SubIFD or invalid IFD.*/ |
2185 | 0 | probablySubIFD = 1; |
2186 | 0 | } |
2187 | | /* -1 because TIFFReadDirectory() will increment tif_curdir. */ |
2188 | 0 | if (curdir >= 1) |
2189 | 0 | tif->tif_curdir = curdir - 1; |
2190 | 0 | else |
2191 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2192 | 0 | } |
2193 | 0 | curdir = tif->tif_curdir; |
2194 | |
|
2195 | 0 | tif->tif_nextdiroff = diroff; |
2196 | 0 | retval = TIFFReadDirectory(tif); |
2197 | | |
2198 | | /* tif_curdir is incremented in TIFFReadDirectory(), but if it has not been |
2199 | | * incremented, TIFFFetchDirectory() has failed there and tif_curdir shall |
2200 | | * be set specifically. */ |
2201 | 0 | if (!retval && diroff != 0 && tif->tif_curdir == curdir) |
2202 | 0 | { |
2203 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2204 | 0 | } |
2205 | |
|
2206 | 0 | if (probablySubIFD) |
2207 | 0 | { |
2208 | 0 | if (retval) |
2209 | 0 | { |
2210 | | /* Reset IFD list to start new one for SubIFD chain and also start |
2211 | | * SubIFD chain with tif_curdir=0 for IFD loop checking. */ |
2212 | | /* invalidate IFD loop lists */ |
2213 | 0 | _TIFFCleanupIFDOffsetAndNumberMaps(tif); |
2214 | 0 | tif->tif_curdir = 0; /* first directory of new chain */ |
2215 | | /* add this offset to new IFD list */ |
2216 | 0 | retval = _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff); |
2217 | 0 | } |
2218 | | /* To be able to return from SubIFD or custom-IFD to main-IFD */ |
2219 | 0 | tif->tif_setdirectory_force_absolute = TRUE; |
2220 | 0 | } |
2221 | |
|
2222 | 0 | return (retval); |
2223 | 0 | } |
2224 | | |
2225 | | /* |
2226 | | * Return file offset of the current directory. |
2227 | | */ |
2228 | 0 | uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); } |
2229 | | |
2230 | | /* |
2231 | | * Return an indication of whether or not we are |
2232 | | * at the last directory in the file. |
2233 | | */ |
2234 | 0 | int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); } |
2235 | | |
2236 | | /* |
2237 | | * Unlink the specified directory from the directory chain. |
2238 | | * Note: First directory starts with number dirn=1. |
2239 | | * This is different to TIFFSetDirectory() where the first directory starts with |
2240 | | * zero. |
2241 | | */ |
2242 | | int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn) |
2243 | 0 | { |
2244 | 0 | static const char module[] = "TIFFUnlinkDirectory"; |
2245 | 0 | uint64_t nextdir; |
2246 | 0 | tdir_t nextdirnum; |
2247 | 0 | uint64_t off; |
2248 | 0 | tdir_t n; |
2249 | |
|
2250 | 0 | if (tif->tif_mode == O_RDONLY) |
2251 | 0 | { |
2252 | 0 | TIFFErrorExtR(tif, module, |
2253 | 0 | "Can not unlink directory in read-only file"); |
2254 | 0 | return (0); |
2255 | 0 | } |
2256 | 0 | if (dirn == 0) |
2257 | 0 | { |
2258 | 0 | TIFFErrorExtR(tif, module, |
2259 | 0 | "For TIFFUnlinkDirectory() first directory starts with " |
2260 | 0 | "number 1 and not 0"); |
2261 | 0 | return (0); |
2262 | 0 | } |
2263 | | /* |
2264 | | * Go to the directory before the one we want |
2265 | | * to unlink and nab the offset of the link |
2266 | | * field we'll need to patch. |
2267 | | */ |
2268 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
2269 | 0 | { |
2270 | 0 | nextdir = tif->tif_header.classic.tiff_diroff; |
2271 | 0 | off = 4; |
2272 | 0 | } |
2273 | 0 | else |
2274 | 0 | { |
2275 | 0 | nextdir = tif->tif_header.big.tiff_diroff; |
2276 | 0 | off = 8; |
2277 | 0 | } |
2278 | 0 | nextdirnum = 0; /* First directory is dirn=0 */ |
2279 | |
|
2280 | 0 | for (n = dirn - 1; n > 0; n--) |
2281 | 0 | { |
2282 | 0 | if (nextdir == 0) |
2283 | 0 | { |
2284 | 0 | TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn); |
2285 | 0 | return (0); |
2286 | 0 | } |
2287 | 0 | if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum)) |
2288 | 0 | return (0); |
2289 | 0 | } |
2290 | | /* |
2291 | | * Advance to the directory to be unlinked and fetch |
2292 | | * the offset of the directory that follows. |
2293 | | */ |
2294 | 0 | if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum)) |
2295 | 0 | return (0); |
2296 | | /* |
2297 | | * Go back and patch the link field of the preceding |
2298 | | * directory to point to the offset of the directory |
2299 | | * that follows. |
2300 | | */ |
2301 | 0 | (void)TIFFSeekFile(tif, off, SEEK_SET); |
2302 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
2303 | 0 | { |
2304 | 0 | uint32_t nextdir32; |
2305 | 0 | nextdir32 = (uint32_t)nextdir; |
2306 | 0 | assert((uint64_t)nextdir32 == nextdir); |
2307 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2308 | 0 | TIFFSwabLong(&nextdir32); |
2309 | 0 | if (!WriteOK(tif, &nextdir32, sizeof(uint32_t))) |
2310 | 0 | { |
2311 | 0 | TIFFErrorExtR(tif, module, "Error writing directory link"); |
2312 | 0 | return (0); |
2313 | 0 | } |
2314 | 0 | } |
2315 | 0 | else |
2316 | 0 | { |
2317 | | /* Need local swap because nextdir has to be used unswapped below. */ |
2318 | 0 | uint64_t nextdir64 = nextdir; |
2319 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2320 | 0 | TIFFSwabLong8(&nextdir64); |
2321 | 0 | if (!WriteOK(tif, &nextdir64, sizeof(uint64_t))) |
2322 | 0 | { |
2323 | 0 | TIFFErrorExtR(tif, module, "Error writing directory link"); |
2324 | 0 | return (0); |
2325 | 0 | } |
2326 | 0 | } |
2327 | | |
2328 | | /* For dirn=1 (first directory) also update the libtiff internal |
2329 | | * base offset variables. */ |
2330 | 0 | if (dirn == 1) |
2331 | 0 | { |
2332 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
2333 | 0 | tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir; |
2334 | 0 | else |
2335 | 0 | tif->tif_header.big.tiff_diroff = nextdir; |
2336 | 0 | } |
2337 | | |
2338 | | /* |
2339 | | * Leave directory state setup safely. We don't have |
2340 | | * facilities for doing inserting and removing directories, |
2341 | | * so it's safest to just invalidate everything. This |
2342 | | * means that the caller can only append to the directory |
2343 | | * chain. |
2344 | | */ |
2345 | 0 | if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) |
2346 | 0 | { |
2347 | 0 | _TIFFfreeExt(tif, tif->tif_rawdata); |
2348 | 0 | tif->tif_rawdata = NULL; |
2349 | 0 | tif->tif_rawcc = 0; |
2350 | 0 | tif->tif_rawcp = NULL; |
2351 | 0 | tif->tif_rawdataoff = 0; |
2352 | 0 | tif->tif_rawdataloaded = 0; |
2353 | 0 | } |
2354 | 0 | tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP | TIFF_POSTENCODE | |
2355 | 0 | TIFF_BUF4WRITE); |
2356 | 0 | TIFFFreeDirectory(tif); |
2357 | 0 | TIFFDefaultDirectory(tif); |
2358 | 0 | tif->tif_diroff = 0; /* force link on next write */ |
2359 | 0 | tif->tif_nextdiroff = 0; /* next write must be at end */ |
2360 | 0 | tif->tif_lastdiroff = 0; /* will be updated on next link */ |
2361 | 0 | tif->tif_curoff = 0; |
2362 | 0 | tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
2363 | 0 | if (tif->tif_curdircount > 0) |
2364 | 0 | tif->tif_curdircount--; |
2365 | 0 | else |
2366 | 0 | tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER; |
2367 | 0 | _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */ |
2368 | 0 | return (1); |
2369 | 0 | } |