/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_dirread.c
Line | Count | Source (jump to first uncovered line) |
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 Read Support Routines. |
29 | | */ |
30 | | |
31 | | /* Suggested pending improvements: |
32 | | * - add a field 'field_info' to the TIFFDirEntry structure, and set that with |
33 | | * the pointer to the appropriate TIFFField structure early on in |
34 | | * TIFFReadDirectory, so as to eliminate current possibly repetitive lookup. |
35 | | */ |
36 | | |
37 | | #include "tiffconf.h" |
38 | | #include "tiffiop.h" |
39 | | #include <float.h> |
40 | | #include <limits.h> |
41 | | #include <stdlib.h> |
42 | | #include <string.h> |
43 | | |
44 | 0 | #define FAILED_FII ((uint32_t)-1) |
45 | | |
46 | | #ifdef HAVE_IEEEFP |
47 | | #define TIFFCvtIEEEFloatToNative(tif, n, fp) |
48 | | #define TIFFCvtIEEEDoubleToNative(tif, n, dp) |
49 | | #else |
50 | | extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *); |
51 | | extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *); |
52 | | #endif |
53 | | |
54 | | enum TIFFReadDirEntryErr |
55 | | { |
56 | | TIFFReadDirEntryErrOk = 0, |
57 | | TIFFReadDirEntryErrCount = 1, |
58 | | TIFFReadDirEntryErrType = 2, |
59 | | TIFFReadDirEntryErrIo = 3, |
60 | | TIFFReadDirEntryErrRange = 4, |
61 | | TIFFReadDirEntryErrPsdif = 5, |
62 | | TIFFReadDirEntryErrSizesan = 6, |
63 | | TIFFReadDirEntryErrAlloc = 7, |
64 | | }; |
65 | | |
66 | | static enum TIFFReadDirEntryErr |
67 | | TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value); |
68 | | static enum TIFFReadDirEntryErr |
69 | | TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value); |
70 | | static enum TIFFReadDirEntryErr |
71 | | TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value); |
72 | | static enum TIFFReadDirEntryErr |
73 | | TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value); |
74 | | static enum TIFFReadDirEntryErr |
75 | | TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value); |
76 | | static enum TIFFReadDirEntryErr |
77 | | TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value); |
78 | | static enum TIFFReadDirEntryErr |
79 | | TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value); |
80 | | static enum TIFFReadDirEntryErr |
81 | | TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value); |
82 | | static enum TIFFReadDirEntryErr |
83 | | TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value); |
84 | | static enum TIFFReadDirEntryErr |
85 | | TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value); |
86 | | static enum TIFFReadDirEntryErr |
87 | | TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value); |
88 | | |
89 | | static enum TIFFReadDirEntryErr |
90 | | TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, |
91 | | uint32_t desttypesize, void **value); |
92 | | static enum TIFFReadDirEntryErr |
93 | | TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value); |
94 | | static enum TIFFReadDirEntryErr |
95 | | TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value); |
96 | | static enum TIFFReadDirEntryErr |
97 | | TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value); |
98 | | static enum TIFFReadDirEntryErr |
99 | | TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value); |
100 | | static enum TIFFReadDirEntryErr |
101 | | TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value); |
102 | | static enum TIFFReadDirEntryErr |
103 | | TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value); |
104 | | static enum TIFFReadDirEntryErr |
105 | | TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value); |
106 | | static enum TIFFReadDirEntryErr |
107 | | TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value); |
108 | | static enum TIFFReadDirEntryErr |
109 | | TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value); |
110 | | static enum TIFFReadDirEntryErr |
111 | | TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value); |
112 | | static enum TIFFReadDirEntryErr |
113 | | TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value); |
114 | | |
115 | | static enum TIFFReadDirEntryErr |
116 | | TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry, |
117 | | uint16_t *value); |
118 | | |
119 | | static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry, |
120 | | uint8_t *value); |
121 | | static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry, |
122 | | int8_t *value); |
123 | | static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry, |
124 | | uint16_t *value); |
125 | | static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry, |
126 | | int16_t *value); |
127 | | static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry, |
128 | | uint32_t *value); |
129 | | static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry, |
130 | | int32_t *value); |
131 | | static enum TIFFReadDirEntryErr |
132 | | TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, |
133 | | uint64_t *value); |
134 | | static enum TIFFReadDirEntryErr |
135 | | TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, |
136 | | int64_t *value); |
137 | | static enum TIFFReadDirEntryErr |
138 | | TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry, |
139 | | double *value); |
140 | | static enum TIFFReadDirEntryErr |
141 | | TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry, |
142 | | double *value); |
143 | | static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry, |
144 | | float *value); |
145 | | static enum TIFFReadDirEntryErr |
146 | | TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value); |
147 | | static enum TIFFReadDirEntryErr |
148 | | TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry, |
149 | | TIFFRational_t *value); |
150 | | |
151 | | static enum TIFFReadDirEntryErr |
152 | | TIFFReadDirEntryCheckRangeByteSbyte(int8_t value); |
153 | | static enum TIFFReadDirEntryErr |
154 | | TIFFReadDirEntryCheckRangeByteShort(uint16_t value); |
155 | | static enum TIFFReadDirEntryErr |
156 | | TIFFReadDirEntryCheckRangeByteSshort(int16_t value); |
157 | | static enum TIFFReadDirEntryErr |
158 | | TIFFReadDirEntryCheckRangeByteLong(uint32_t value); |
159 | | static enum TIFFReadDirEntryErr |
160 | | TIFFReadDirEntryCheckRangeByteSlong(int32_t value); |
161 | | static enum TIFFReadDirEntryErr |
162 | | TIFFReadDirEntryCheckRangeByteLong8(uint64_t value); |
163 | | static enum TIFFReadDirEntryErr |
164 | | TIFFReadDirEntryCheckRangeByteSlong8(int64_t value); |
165 | | |
166 | | static enum TIFFReadDirEntryErr |
167 | | TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value); |
168 | | static enum TIFFReadDirEntryErr |
169 | | TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value); |
170 | | static enum TIFFReadDirEntryErr |
171 | | TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value); |
172 | | static enum TIFFReadDirEntryErr |
173 | | TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value); |
174 | | static enum TIFFReadDirEntryErr |
175 | | TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value); |
176 | | static enum TIFFReadDirEntryErr |
177 | | TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value); |
178 | | static enum TIFFReadDirEntryErr |
179 | | TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value); |
180 | | |
181 | | static enum TIFFReadDirEntryErr |
182 | | TIFFReadDirEntryCheckRangeShortSbyte(int8_t value); |
183 | | static enum TIFFReadDirEntryErr |
184 | | TIFFReadDirEntryCheckRangeShortSshort(int16_t value); |
185 | | static enum TIFFReadDirEntryErr |
186 | | TIFFReadDirEntryCheckRangeShortLong(uint32_t value); |
187 | | static enum TIFFReadDirEntryErr |
188 | | TIFFReadDirEntryCheckRangeShortSlong(int32_t value); |
189 | | static enum TIFFReadDirEntryErr |
190 | | TIFFReadDirEntryCheckRangeShortLong8(uint64_t value); |
191 | | static enum TIFFReadDirEntryErr |
192 | | TIFFReadDirEntryCheckRangeShortSlong8(int64_t value); |
193 | | |
194 | | static enum TIFFReadDirEntryErr |
195 | | TIFFReadDirEntryCheckRangeSshortShort(uint16_t value); |
196 | | static enum TIFFReadDirEntryErr |
197 | | TIFFReadDirEntryCheckRangeSshortLong(uint32_t value); |
198 | | static enum TIFFReadDirEntryErr |
199 | | TIFFReadDirEntryCheckRangeSshortSlong(int32_t value); |
200 | | static enum TIFFReadDirEntryErr |
201 | | TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value); |
202 | | static enum TIFFReadDirEntryErr |
203 | | TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value); |
204 | | |
205 | | static enum TIFFReadDirEntryErr |
206 | | TIFFReadDirEntryCheckRangeLongSbyte(int8_t value); |
207 | | static enum TIFFReadDirEntryErr |
208 | | TIFFReadDirEntryCheckRangeLongSshort(int16_t value); |
209 | | static enum TIFFReadDirEntryErr |
210 | | TIFFReadDirEntryCheckRangeLongSlong(int32_t value); |
211 | | static enum TIFFReadDirEntryErr |
212 | | TIFFReadDirEntryCheckRangeLongLong8(uint64_t value); |
213 | | static enum TIFFReadDirEntryErr |
214 | | TIFFReadDirEntryCheckRangeLongSlong8(int64_t value); |
215 | | |
216 | | static enum TIFFReadDirEntryErr |
217 | | TIFFReadDirEntryCheckRangeSlongLong(uint32_t value); |
218 | | static enum TIFFReadDirEntryErr |
219 | | TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value); |
220 | | static enum TIFFReadDirEntryErr |
221 | | TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value); |
222 | | |
223 | | static enum TIFFReadDirEntryErr |
224 | | TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value); |
225 | | static enum TIFFReadDirEntryErr |
226 | | TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value); |
227 | | static enum TIFFReadDirEntryErr |
228 | | TIFFReadDirEntryCheckRangeLong8Slong(int32_t value); |
229 | | static enum TIFFReadDirEntryErr |
230 | | TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value); |
231 | | |
232 | | static enum TIFFReadDirEntryErr |
233 | | TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value); |
234 | | |
235 | | static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset, |
236 | | tmsize_t size, void *dest); |
237 | | static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err, |
238 | | const char *module, const char *tagname, |
239 | | int recover); |
240 | | |
241 | | static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir, |
242 | | uint16_t dircount); |
243 | | static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir, |
244 | | uint16_t dircount, |
245 | | uint16_t tagid); |
246 | | static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid, |
247 | | uint32_t *fii); |
248 | | |
249 | | static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir, |
250 | | uint16_t dircount); |
251 | | static void MissingRequired(TIFF *, const char *); |
252 | | static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t); |
253 | | static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff, |
254 | | TIFFDirEntry **pdir, uint64_t *nextdiroff); |
255 | | static int TIFFFetchNormalTag(TIFF *, TIFFDirEntry *, int recover); |
256 | | static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, |
257 | | uint64_t **lpp); |
258 | | static int TIFFFetchSubjectDistance(TIFF *, TIFFDirEntry *); |
259 | | static void ChopUpSingleUncompressedStrip(TIFF *); |
260 | | static void TryChopUpUncompressedBigTiff(TIFF *); |
261 | | static uint64_t TIFFReadUInt64(const uint8_t *value); |
262 | | static int _TIFFGetMaxColorChannels(uint16_t photometric); |
263 | | |
264 | | static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount); |
265 | | |
266 | | typedef union _UInt64Aligned_t |
267 | | { |
268 | | double d; |
269 | | uint64_t l; |
270 | | uint32_t i[2]; |
271 | | uint16_t s[4]; |
272 | | uint8_t c[8]; |
273 | | } UInt64Aligned_t; |
274 | | |
275 | | /* |
276 | | Unaligned safe copy of a uint64_t value from an octet array. |
277 | | */ |
278 | | static uint64_t TIFFReadUInt64(const uint8_t *value) |
279 | 0 | { |
280 | 0 | UInt64Aligned_t result; |
281 | |
|
282 | 0 | result.c[0] = value[0]; |
283 | 0 | result.c[1] = value[1]; |
284 | 0 | result.c[2] = value[2]; |
285 | 0 | result.c[3] = value[3]; |
286 | 0 | result.c[4] = value[4]; |
287 | 0 | result.c[5] = value[5]; |
288 | 0 | result.c[6] = value[6]; |
289 | 0 | result.c[7] = value[7]; |
290 | |
|
291 | 0 | return result.l; |
292 | 0 | } |
293 | | |
294 | | static enum TIFFReadDirEntryErr |
295 | | TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value) |
296 | 0 | { |
297 | 0 | enum TIFFReadDirEntryErr err; |
298 | 0 | if (direntry->tdir_count != 1) |
299 | 0 | return (TIFFReadDirEntryErrCount); |
300 | 0 | switch (direntry->tdir_type) |
301 | 0 | { |
302 | 0 | case TIFF_BYTE: |
303 | 0 | case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with |
304 | | field_readcount==1 */ |
305 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, value); |
306 | 0 | return (TIFFReadDirEntryErrOk); |
307 | 0 | case TIFF_SBYTE: |
308 | 0 | { |
309 | 0 | int8_t m; |
310 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
311 | 0 | err = TIFFReadDirEntryCheckRangeByteSbyte(m); |
312 | 0 | if (err != TIFFReadDirEntryErrOk) |
313 | 0 | return (err); |
314 | 0 | *value = (uint8_t)m; |
315 | 0 | return (TIFFReadDirEntryErrOk); |
316 | 0 | } |
317 | 0 | case TIFF_SHORT: |
318 | 0 | { |
319 | 0 | uint16_t m; |
320 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
321 | 0 | err = TIFFReadDirEntryCheckRangeByteShort(m); |
322 | 0 | if (err != TIFFReadDirEntryErrOk) |
323 | 0 | return (err); |
324 | 0 | *value = (uint8_t)m; |
325 | 0 | return (TIFFReadDirEntryErrOk); |
326 | 0 | } |
327 | 0 | case TIFF_SSHORT: |
328 | 0 | { |
329 | 0 | int16_t m; |
330 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
331 | 0 | err = TIFFReadDirEntryCheckRangeByteSshort(m); |
332 | 0 | if (err != TIFFReadDirEntryErrOk) |
333 | 0 | return (err); |
334 | 0 | *value = (uint8_t)m; |
335 | 0 | return (TIFFReadDirEntryErrOk); |
336 | 0 | } |
337 | 0 | case TIFF_LONG: |
338 | 0 | { |
339 | 0 | uint32_t m; |
340 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
341 | 0 | err = TIFFReadDirEntryCheckRangeByteLong(m); |
342 | 0 | if (err != TIFFReadDirEntryErrOk) |
343 | 0 | return (err); |
344 | 0 | *value = (uint8_t)m; |
345 | 0 | return (TIFFReadDirEntryErrOk); |
346 | 0 | } |
347 | 0 | case TIFF_SLONG: |
348 | 0 | { |
349 | 0 | int32_t m; |
350 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
351 | 0 | err = TIFFReadDirEntryCheckRangeByteSlong(m); |
352 | 0 | if (err != TIFFReadDirEntryErrOk) |
353 | 0 | return (err); |
354 | 0 | *value = (uint8_t)m; |
355 | 0 | return (TIFFReadDirEntryErrOk); |
356 | 0 | } |
357 | 0 | case TIFF_LONG8: |
358 | 0 | { |
359 | 0 | uint64_t m; |
360 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
361 | 0 | if (err != TIFFReadDirEntryErrOk) |
362 | 0 | return (err); |
363 | 0 | err = TIFFReadDirEntryCheckRangeByteLong8(m); |
364 | 0 | if (err != TIFFReadDirEntryErrOk) |
365 | 0 | return (err); |
366 | 0 | *value = (uint8_t)m; |
367 | 0 | return (TIFFReadDirEntryErrOk); |
368 | 0 | } |
369 | 0 | case TIFF_SLONG8: |
370 | 0 | { |
371 | 0 | int64_t m; |
372 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
373 | 0 | if (err != TIFFReadDirEntryErrOk) |
374 | 0 | return (err); |
375 | 0 | err = TIFFReadDirEntryCheckRangeByteSlong8(m); |
376 | 0 | if (err != TIFFReadDirEntryErrOk) |
377 | 0 | return (err); |
378 | 0 | *value = (uint8_t)m; |
379 | 0 | return (TIFFReadDirEntryErrOk); |
380 | 0 | } |
381 | 0 | default: |
382 | 0 | return (TIFFReadDirEntryErrType); |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | | static enum TIFFReadDirEntryErr |
387 | | TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value) |
388 | 0 | { |
389 | 0 | enum TIFFReadDirEntryErr err; |
390 | 0 | if (direntry->tdir_count != 1) |
391 | 0 | return (TIFFReadDirEntryErrCount); |
392 | 0 | switch (direntry->tdir_type) |
393 | 0 | { |
394 | 0 | case TIFF_BYTE: |
395 | 0 | case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with |
396 | | field_readcount==1 */ |
397 | 0 | { |
398 | 0 | uint8_t m; |
399 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
400 | 0 | err = TIFFReadDirEntryCheckRangeSbyteByte(m); |
401 | 0 | if (err != TIFFReadDirEntryErrOk) |
402 | 0 | return (err); |
403 | 0 | *value = (int8_t)m; |
404 | 0 | return (TIFFReadDirEntryErrOk); |
405 | 0 | } |
406 | 0 | case TIFF_SBYTE: |
407 | 0 | { |
408 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, value); |
409 | 0 | return (TIFFReadDirEntryErrOk); |
410 | 0 | } |
411 | 0 | case TIFF_SHORT: |
412 | 0 | { |
413 | 0 | uint16_t m; |
414 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
415 | 0 | err = TIFFReadDirEntryCheckRangeSbyteShort(m); |
416 | 0 | if (err != TIFFReadDirEntryErrOk) |
417 | 0 | return (err); |
418 | 0 | *value = (int8_t)m; |
419 | 0 | return (TIFFReadDirEntryErrOk); |
420 | 0 | } |
421 | 0 | case TIFF_SSHORT: |
422 | 0 | { |
423 | 0 | int16_t m; |
424 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
425 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSshort(m); |
426 | 0 | if (err != TIFFReadDirEntryErrOk) |
427 | 0 | return (err); |
428 | 0 | *value = (int8_t)m; |
429 | 0 | return (TIFFReadDirEntryErrOk); |
430 | 0 | } |
431 | 0 | case TIFF_LONG: |
432 | 0 | { |
433 | 0 | uint32_t m; |
434 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
435 | 0 | err = TIFFReadDirEntryCheckRangeSbyteLong(m); |
436 | 0 | if (err != TIFFReadDirEntryErrOk) |
437 | 0 | return (err); |
438 | 0 | *value = (int8_t)m; |
439 | 0 | return (TIFFReadDirEntryErrOk); |
440 | 0 | } |
441 | 0 | case TIFF_SLONG: |
442 | 0 | { |
443 | 0 | int32_t m; |
444 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
445 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSlong(m); |
446 | 0 | if (err != TIFFReadDirEntryErrOk) |
447 | 0 | return (err); |
448 | 0 | *value = (int8_t)m; |
449 | 0 | return (TIFFReadDirEntryErrOk); |
450 | 0 | } |
451 | 0 | case TIFF_LONG8: |
452 | 0 | { |
453 | 0 | uint64_t m; |
454 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
455 | 0 | if (err != TIFFReadDirEntryErrOk) |
456 | 0 | return (err); |
457 | 0 | err = TIFFReadDirEntryCheckRangeSbyteLong8(m); |
458 | 0 | if (err != TIFFReadDirEntryErrOk) |
459 | 0 | return (err); |
460 | 0 | *value = (int8_t)m; |
461 | 0 | return (TIFFReadDirEntryErrOk); |
462 | 0 | } |
463 | 0 | case TIFF_SLONG8: |
464 | 0 | { |
465 | 0 | int64_t m; |
466 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
467 | 0 | if (err != TIFFReadDirEntryErrOk) |
468 | 0 | return (err); |
469 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSlong8(m); |
470 | 0 | if (err != TIFFReadDirEntryErrOk) |
471 | 0 | return (err); |
472 | 0 | *value = (int8_t)m; |
473 | 0 | return (TIFFReadDirEntryErrOk); |
474 | 0 | } |
475 | 0 | default: |
476 | 0 | return (TIFFReadDirEntryErrType); |
477 | 0 | } |
478 | 0 | } /*-- TIFFReadDirEntrySbyte() --*/ |
479 | | |
480 | | static enum TIFFReadDirEntryErr |
481 | | TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value) |
482 | 0 | { |
483 | 0 | enum TIFFReadDirEntryErr err; |
484 | 0 | if (direntry->tdir_count != 1) |
485 | 0 | return (TIFFReadDirEntryErrCount); |
486 | 0 | switch (direntry->tdir_type) |
487 | 0 | { |
488 | 0 | case TIFF_BYTE: |
489 | 0 | { |
490 | 0 | uint8_t m; |
491 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
492 | 0 | *value = (uint16_t)m; |
493 | 0 | return (TIFFReadDirEntryErrOk); |
494 | 0 | } |
495 | 0 | case TIFF_SBYTE: |
496 | 0 | { |
497 | 0 | int8_t m; |
498 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
499 | 0 | err = TIFFReadDirEntryCheckRangeShortSbyte(m); |
500 | 0 | if (err != TIFFReadDirEntryErrOk) |
501 | 0 | return (err); |
502 | 0 | *value = (uint16_t)m; |
503 | 0 | return (TIFFReadDirEntryErrOk); |
504 | 0 | } |
505 | 0 | case TIFF_SHORT: |
506 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, value); |
507 | 0 | return (TIFFReadDirEntryErrOk); |
508 | 0 | case TIFF_SSHORT: |
509 | 0 | { |
510 | 0 | int16_t m; |
511 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
512 | 0 | err = TIFFReadDirEntryCheckRangeShortSshort(m); |
513 | 0 | if (err != TIFFReadDirEntryErrOk) |
514 | 0 | return (err); |
515 | 0 | *value = (uint16_t)m; |
516 | 0 | return (TIFFReadDirEntryErrOk); |
517 | 0 | } |
518 | 0 | case TIFF_LONG: |
519 | 0 | { |
520 | 0 | uint32_t m; |
521 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
522 | 0 | err = TIFFReadDirEntryCheckRangeShortLong(m); |
523 | 0 | if (err != TIFFReadDirEntryErrOk) |
524 | 0 | return (err); |
525 | 0 | *value = (uint16_t)m; |
526 | 0 | return (TIFFReadDirEntryErrOk); |
527 | 0 | } |
528 | 0 | case TIFF_SLONG: |
529 | 0 | { |
530 | 0 | int32_t m; |
531 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
532 | 0 | err = TIFFReadDirEntryCheckRangeShortSlong(m); |
533 | 0 | if (err != TIFFReadDirEntryErrOk) |
534 | 0 | return (err); |
535 | 0 | *value = (uint16_t)m; |
536 | 0 | return (TIFFReadDirEntryErrOk); |
537 | 0 | } |
538 | 0 | case TIFF_LONG8: |
539 | 0 | { |
540 | 0 | uint64_t m; |
541 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
542 | 0 | if (err != TIFFReadDirEntryErrOk) |
543 | 0 | return (err); |
544 | 0 | err = TIFFReadDirEntryCheckRangeShortLong8(m); |
545 | 0 | if (err != TIFFReadDirEntryErrOk) |
546 | 0 | return (err); |
547 | 0 | *value = (uint16_t)m; |
548 | 0 | return (TIFFReadDirEntryErrOk); |
549 | 0 | } |
550 | 0 | case TIFF_SLONG8: |
551 | 0 | { |
552 | 0 | int64_t m; |
553 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
554 | 0 | if (err != TIFFReadDirEntryErrOk) |
555 | 0 | return (err); |
556 | 0 | err = TIFFReadDirEntryCheckRangeShortSlong8(m); |
557 | 0 | if (err != TIFFReadDirEntryErrOk) |
558 | 0 | return (err); |
559 | 0 | *value = (uint16_t)m; |
560 | 0 | return (TIFFReadDirEntryErrOk); |
561 | 0 | } |
562 | 0 | default: |
563 | 0 | return (TIFFReadDirEntryErrType); |
564 | 0 | } |
565 | 0 | } /*-- TIFFReadDirEntryShort() --*/ |
566 | | |
567 | | static enum TIFFReadDirEntryErr |
568 | | TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value) |
569 | 0 | { |
570 | 0 | enum TIFFReadDirEntryErr err; |
571 | 0 | if (direntry->tdir_count != 1) |
572 | 0 | return (TIFFReadDirEntryErrCount); |
573 | 0 | switch (direntry->tdir_type) |
574 | 0 | { |
575 | 0 | case TIFF_BYTE: |
576 | 0 | { |
577 | 0 | uint8_t m; |
578 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
579 | 0 | *value = (int16_t)m; |
580 | 0 | return (TIFFReadDirEntryErrOk); |
581 | 0 | } |
582 | 0 | case TIFF_SBYTE: |
583 | 0 | { |
584 | 0 | int8_t m; |
585 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
586 | 0 | *value = (int16_t)m; |
587 | 0 | return (TIFFReadDirEntryErrOk); |
588 | 0 | } |
589 | 0 | case TIFF_SHORT: |
590 | 0 | { |
591 | 0 | uint16_t m; |
592 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
593 | 0 | err = TIFFReadDirEntryCheckRangeSshortShort(m); |
594 | 0 | if (err != TIFFReadDirEntryErrOk) |
595 | 0 | return (err); |
596 | 0 | *value = (uint16_t)m; |
597 | 0 | return (TIFFReadDirEntryErrOk); |
598 | 0 | } |
599 | 0 | case TIFF_SSHORT: |
600 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, value); |
601 | 0 | return (TIFFReadDirEntryErrOk); |
602 | 0 | case TIFF_LONG: |
603 | 0 | { |
604 | 0 | uint32_t m; |
605 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
606 | 0 | err = TIFFReadDirEntryCheckRangeSshortLong(m); |
607 | 0 | if (err != TIFFReadDirEntryErrOk) |
608 | 0 | return (err); |
609 | 0 | *value = (int16_t)m; |
610 | 0 | return (TIFFReadDirEntryErrOk); |
611 | 0 | } |
612 | 0 | case TIFF_SLONG: |
613 | 0 | { |
614 | 0 | int32_t m; |
615 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
616 | 0 | err = TIFFReadDirEntryCheckRangeSshortSlong(m); |
617 | 0 | if (err != TIFFReadDirEntryErrOk) |
618 | 0 | return (err); |
619 | 0 | *value = (int16_t)m; |
620 | 0 | return (TIFFReadDirEntryErrOk); |
621 | 0 | } |
622 | 0 | case TIFF_LONG8: |
623 | 0 | { |
624 | 0 | uint64_t m; |
625 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
626 | 0 | if (err != TIFFReadDirEntryErrOk) |
627 | 0 | return (err); |
628 | 0 | err = TIFFReadDirEntryCheckRangeSshortLong8(m); |
629 | 0 | if (err != TIFFReadDirEntryErrOk) |
630 | 0 | return (err); |
631 | 0 | *value = (int16_t)m; |
632 | 0 | return (TIFFReadDirEntryErrOk); |
633 | 0 | } |
634 | 0 | case TIFF_SLONG8: |
635 | 0 | { |
636 | 0 | int64_t m; |
637 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
638 | 0 | if (err != TIFFReadDirEntryErrOk) |
639 | 0 | return (err); |
640 | 0 | err = TIFFReadDirEntryCheckRangeSshortSlong8(m); |
641 | 0 | if (err != TIFFReadDirEntryErrOk) |
642 | 0 | return (err); |
643 | 0 | *value = (int16_t)m; |
644 | 0 | return (TIFFReadDirEntryErrOk); |
645 | 0 | } |
646 | 0 | default: |
647 | 0 | return (TIFFReadDirEntryErrType); |
648 | 0 | } |
649 | 0 | } /*-- TIFFReadDirEntrySshort() --*/ |
650 | | |
651 | | static enum TIFFReadDirEntryErr |
652 | | TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value) |
653 | 0 | { |
654 | 0 | enum TIFFReadDirEntryErr err; |
655 | 0 | if (direntry->tdir_count != 1) |
656 | 0 | return (TIFFReadDirEntryErrCount); |
657 | 0 | switch (direntry->tdir_type) |
658 | 0 | { |
659 | 0 | case TIFF_BYTE: |
660 | 0 | { |
661 | 0 | uint8_t m; |
662 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
663 | 0 | *value = (uint32_t)m; |
664 | 0 | return (TIFFReadDirEntryErrOk); |
665 | 0 | } |
666 | 0 | case TIFF_SBYTE: |
667 | 0 | { |
668 | 0 | int8_t m; |
669 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
670 | 0 | err = TIFFReadDirEntryCheckRangeLongSbyte(m); |
671 | 0 | if (err != TIFFReadDirEntryErrOk) |
672 | 0 | return (err); |
673 | 0 | *value = (uint32_t)m; |
674 | 0 | return (TIFFReadDirEntryErrOk); |
675 | 0 | } |
676 | 0 | case TIFF_SHORT: |
677 | 0 | { |
678 | 0 | uint16_t m; |
679 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
680 | 0 | *value = (uint32_t)m; |
681 | 0 | return (TIFFReadDirEntryErrOk); |
682 | 0 | } |
683 | 0 | case TIFF_SSHORT: |
684 | 0 | { |
685 | 0 | int16_t m; |
686 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
687 | 0 | err = TIFFReadDirEntryCheckRangeLongSshort(m); |
688 | 0 | if (err != TIFFReadDirEntryErrOk) |
689 | 0 | return (err); |
690 | 0 | *value = (uint32_t)m; |
691 | 0 | return (TIFFReadDirEntryErrOk); |
692 | 0 | } |
693 | 0 | case TIFF_LONG: |
694 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, value); |
695 | 0 | return (TIFFReadDirEntryErrOk); |
696 | 0 | case TIFF_SLONG: |
697 | 0 | { |
698 | 0 | int32_t m; |
699 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
700 | 0 | err = TIFFReadDirEntryCheckRangeLongSlong(m); |
701 | 0 | if (err != TIFFReadDirEntryErrOk) |
702 | 0 | return (err); |
703 | 0 | *value = (uint32_t)m; |
704 | 0 | return (TIFFReadDirEntryErrOk); |
705 | 0 | } |
706 | 0 | case TIFF_LONG8: |
707 | 0 | { |
708 | 0 | uint64_t m; |
709 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
710 | 0 | if (err != TIFFReadDirEntryErrOk) |
711 | 0 | return (err); |
712 | 0 | err = TIFFReadDirEntryCheckRangeLongLong8(m); |
713 | 0 | if (err != TIFFReadDirEntryErrOk) |
714 | 0 | return (err); |
715 | 0 | *value = (uint32_t)m; |
716 | 0 | return (TIFFReadDirEntryErrOk); |
717 | 0 | } |
718 | 0 | case TIFF_SLONG8: |
719 | 0 | { |
720 | 0 | int64_t m; |
721 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
722 | 0 | if (err != TIFFReadDirEntryErrOk) |
723 | 0 | return (err); |
724 | 0 | err = TIFFReadDirEntryCheckRangeLongSlong8(m); |
725 | 0 | if (err != TIFFReadDirEntryErrOk) |
726 | 0 | return (err); |
727 | 0 | *value = (uint32_t)m; |
728 | 0 | return (TIFFReadDirEntryErrOk); |
729 | 0 | } |
730 | 0 | default: |
731 | 0 | return (TIFFReadDirEntryErrType); |
732 | 0 | } |
733 | 0 | } /*-- TIFFReadDirEntryLong() --*/ |
734 | | |
735 | | static enum TIFFReadDirEntryErr |
736 | | TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value) |
737 | 0 | { |
738 | 0 | enum TIFFReadDirEntryErr err; |
739 | 0 | if (direntry->tdir_count != 1) |
740 | 0 | return (TIFFReadDirEntryErrCount); |
741 | 0 | switch (direntry->tdir_type) |
742 | 0 | { |
743 | 0 | case TIFF_BYTE: |
744 | 0 | { |
745 | 0 | uint8_t m; |
746 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
747 | 0 | *value = (int32_t)m; |
748 | 0 | return (TIFFReadDirEntryErrOk); |
749 | 0 | } |
750 | 0 | case TIFF_SBYTE: |
751 | 0 | { |
752 | 0 | int8_t m; |
753 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
754 | 0 | *value = (int32_t)m; |
755 | 0 | return (TIFFReadDirEntryErrOk); |
756 | 0 | } |
757 | 0 | case TIFF_SHORT: |
758 | 0 | { |
759 | 0 | uint16_t m; |
760 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
761 | 0 | *value = (int32_t)m; |
762 | 0 | return (TIFFReadDirEntryErrOk); |
763 | 0 | } |
764 | 0 | case TIFF_SSHORT: |
765 | 0 | { |
766 | 0 | int16_t m; |
767 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
768 | 0 | *value = (int32_t)m; |
769 | 0 | return (TIFFReadDirEntryErrOk); |
770 | 0 | } |
771 | 0 | case TIFF_LONG: |
772 | 0 | { |
773 | 0 | uint32_t m; |
774 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
775 | 0 | err = TIFFReadDirEntryCheckRangeSlongLong(m); |
776 | 0 | if (err != TIFFReadDirEntryErrOk) |
777 | 0 | return (err); |
778 | 0 | *value = (int32_t)m; |
779 | 0 | return (TIFFReadDirEntryErrOk); |
780 | 0 | } |
781 | 0 | case TIFF_SLONG: |
782 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, value); |
783 | 0 | return (TIFFReadDirEntryErrOk); |
784 | 0 | case TIFF_LONG8: |
785 | 0 | { |
786 | 0 | uint64_t m; |
787 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
788 | 0 | if (err != TIFFReadDirEntryErrOk) |
789 | 0 | return (err); |
790 | 0 | err = TIFFReadDirEntryCheckRangeSlongLong8(m); |
791 | 0 | if (err != TIFFReadDirEntryErrOk) |
792 | 0 | return (err); |
793 | 0 | *value = (int32_t)m; |
794 | 0 | return (TIFFReadDirEntryErrOk); |
795 | 0 | } |
796 | 0 | case TIFF_SLONG8: |
797 | 0 | { |
798 | 0 | int64_t m; |
799 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
800 | 0 | if (err != TIFFReadDirEntryErrOk) |
801 | 0 | return (err); |
802 | 0 | err = TIFFReadDirEntryCheckRangeSlongSlong8(m); |
803 | 0 | if (err != TIFFReadDirEntryErrOk) |
804 | 0 | return (err); |
805 | 0 | *value = (int32_t)m; |
806 | 0 | return (TIFFReadDirEntryErrOk); |
807 | 0 | } |
808 | 0 | default: |
809 | 0 | return (TIFFReadDirEntryErrType); |
810 | 0 | } |
811 | 0 | } /*-- TIFFReadDirEntrySlong() --*/ |
812 | | |
813 | | static enum TIFFReadDirEntryErr |
814 | | TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) |
815 | 0 | { |
816 | 0 | enum TIFFReadDirEntryErr err; |
817 | 0 | if (direntry->tdir_count != 1) |
818 | 0 | return (TIFFReadDirEntryErrCount); |
819 | 0 | switch (direntry->tdir_type) |
820 | 0 | { |
821 | 0 | case TIFF_BYTE: |
822 | 0 | { |
823 | 0 | uint8_t m; |
824 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
825 | 0 | *value = (uint64_t)m; |
826 | 0 | return (TIFFReadDirEntryErrOk); |
827 | 0 | } |
828 | 0 | case TIFF_SBYTE: |
829 | 0 | { |
830 | 0 | int8_t m; |
831 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
832 | 0 | err = TIFFReadDirEntryCheckRangeLong8Sbyte(m); |
833 | 0 | if (err != TIFFReadDirEntryErrOk) |
834 | 0 | return (err); |
835 | 0 | *value = (uint64_t)m; |
836 | 0 | return (TIFFReadDirEntryErrOk); |
837 | 0 | } |
838 | 0 | case TIFF_SHORT: |
839 | 0 | { |
840 | 0 | uint16_t m; |
841 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
842 | 0 | *value = (uint64_t)m; |
843 | 0 | return (TIFFReadDirEntryErrOk); |
844 | 0 | } |
845 | 0 | case TIFF_SSHORT: |
846 | 0 | { |
847 | 0 | int16_t m; |
848 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
849 | 0 | err = TIFFReadDirEntryCheckRangeLong8Sshort(m); |
850 | 0 | if (err != TIFFReadDirEntryErrOk) |
851 | 0 | return (err); |
852 | 0 | *value = (uint64_t)m; |
853 | 0 | return (TIFFReadDirEntryErrOk); |
854 | 0 | } |
855 | 0 | case TIFF_LONG: |
856 | 0 | { |
857 | 0 | uint32_t m; |
858 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
859 | 0 | *value = (uint64_t)m; |
860 | 0 | return (TIFFReadDirEntryErrOk); |
861 | 0 | } |
862 | 0 | case TIFF_SLONG: |
863 | 0 | { |
864 | 0 | int32_t m; |
865 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
866 | 0 | err = TIFFReadDirEntryCheckRangeLong8Slong(m); |
867 | 0 | if (err != TIFFReadDirEntryErrOk) |
868 | 0 | return (err); |
869 | 0 | *value = (uint64_t)m; |
870 | 0 | return (TIFFReadDirEntryErrOk); |
871 | 0 | } |
872 | 0 | case TIFF_LONG8: |
873 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, value); |
874 | 0 | return (err); |
875 | 0 | case TIFF_SLONG8: |
876 | 0 | { |
877 | 0 | int64_t m; |
878 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
879 | 0 | if (err != TIFFReadDirEntryErrOk) |
880 | 0 | return (err); |
881 | 0 | err = TIFFReadDirEntryCheckRangeLong8Slong8(m); |
882 | 0 | if (err != TIFFReadDirEntryErrOk) |
883 | 0 | return (err); |
884 | 0 | *value = (uint64_t)m; |
885 | 0 | return (TIFFReadDirEntryErrOk); |
886 | 0 | } |
887 | 0 | default: |
888 | 0 | return (TIFFReadDirEntryErrType); |
889 | 0 | } |
890 | 0 | } /*-- TIFFReadDirEntryLong8() --*/ |
891 | | |
892 | | static enum TIFFReadDirEntryErr |
893 | | TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value) |
894 | 0 | { |
895 | 0 | enum TIFFReadDirEntryErr err; |
896 | 0 | if (direntry->tdir_count != 1) |
897 | 0 | return (TIFFReadDirEntryErrCount); |
898 | 0 | switch (direntry->tdir_type) |
899 | 0 | { |
900 | 0 | case TIFF_BYTE: |
901 | 0 | { |
902 | 0 | uint8_t m; |
903 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
904 | 0 | *value = (int64_t)m; |
905 | 0 | return (TIFFReadDirEntryErrOk); |
906 | 0 | } |
907 | 0 | case TIFF_SBYTE: |
908 | 0 | { |
909 | 0 | int8_t m; |
910 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
911 | 0 | *value = (int64_t)m; |
912 | 0 | return (TIFFReadDirEntryErrOk); |
913 | 0 | } |
914 | 0 | case TIFF_SHORT: |
915 | 0 | { |
916 | 0 | uint16_t m; |
917 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
918 | 0 | *value = (int64_t)m; |
919 | 0 | return (TIFFReadDirEntryErrOk); |
920 | 0 | } |
921 | 0 | case TIFF_SSHORT: |
922 | 0 | { |
923 | 0 | int16_t m; |
924 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
925 | 0 | *value = (int64_t)m; |
926 | 0 | return (TIFFReadDirEntryErrOk); |
927 | 0 | } |
928 | 0 | case TIFF_LONG: |
929 | 0 | { |
930 | 0 | uint32_t m; |
931 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
932 | 0 | *value = (int64_t)m; |
933 | 0 | return (TIFFReadDirEntryErrOk); |
934 | 0 | } |
935 | 0 | case TIFF_SLONG: |
936 | 0 | { |
937 | 0 | int32_t m; |
938 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
939 | 0 | *value = (int64_t)m; |
940 | 0 | return (TIFFReadDirEntryErrOk); |
941 | 0 | } |
942 | 0 | case TIFF_LONG8: |
943 | 0 | { |
944 | 0 | uint64_t m; |
945 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
946 | 0 | if (err != TIFFReadDirEntryErrOk) |
947 | 0 | return (err); |
948 | 0 | err = TIFFReadDirEntryCheckRangeSlong8Long8(m); |
949 | 0 | if (err != TIFFReadDirEntryErrOk) |
950 | 0 | return (err); |
951 | 0 | *value = (int64_t)m; |
952 | 0 | return (TIFFReadDirEntryErrOk); |
953 | 0 | } |
954 | 0 | case TIFF_SLONG8: |
955 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value); |
956 | 0 | return (err); |
957 | 0 | default: |
958 | 0 | return (TIFFReadDirEntryErrType); |
959 | 0 | } |
960 | 0 | } /*-- TIFFReadDirEntrySlong8() --*/ |
961 | | |
962 | | static enum TIFFReadDirEntryErr |
963 | | TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value) |
964 | 0 | { |
965 | 0 | enum TIFFReadDirEntryErr err; |
966 | 0 | if (direntry->tdir_count != 1) |
967 | 0 | return (TIFFReadDirEntryErrCount); |
968 | 0 | switch (direntry->tdir_type) |
969 | 0 | { |
970 | 0 | case TIFF_BYTE: |
971 | 0 | { |
972 | 0 | uint8_t m; |
973 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
974 | 0 | *value = (float)m; |
975 | 0 | return (TIFFReadDirEntryErrOk); |
976 | 0 | } |
977 | 0 | case TIFF_SBYTE: |
978 | 0 | { |
979 | 0 | int8_t m; |
980 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
981 | 0 | *value = (float)m; |
982 | 0 | return (TIFFReadDirEntryErrOk); |
983 | 0 | } |
984 | 0 | case TIFF_SHORT: |
985 | 0 | { |
986 | 0 | uint16_t m; |
987 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
988 | 0 | *value = (float)m; |
989 | 0 | return (TIFFReadDirEntryErrOk); |
990 | 0 | } |
991 | 0 | case TIFF_SSHORT: |
992 | 0 | { |
993 | 0 | int16_t m; |
994 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
995 | 0 | *value = (float)m; |
996 | 0 | return (TIFFReadDirEntryErrOk); |
997 | 0 | } |
998 | 0 | case TIFF_LONG: |
999 | 0 | { |
1000 | 0 | uint32_t m; |
1001 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
1002 | 0 | *value = (float)m; |
1003 | 0 | return (TIFFReadDirEntryErrOk); |
1004 | 0 | } |
1005 | 0 | case TIFF_SLONG: |
1006 | 0 | { |
1007 | 0 | int32_t m; |
1008 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
1009 | 0 | *value = (float)m; |
1010 | 0 | return (TIFFReadDirEntryErrOk); |
1011 | 0 | } |
1012 | 0 | case TIFF_LONG8: |
1013 | 0 | { |
1014 | 0 | uint64_t m; |
1015 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
1016 | 0 | if (err != TIFFReadDirEntryErrOk) |
1017 | 0 | return (err); |
1018 | | #if defined(__WIN32__) && (_MSC_VER < 1500) |
1019 | | /* |
1020 | | * XXX: MSVC 6.0 does not support conversion |
1021 | | * of 64-bit integers into floating point |
1022 | | * values. |
1023 | | */ |
1024 | | *value = _TIFFUInt64ToFloat(m); |
1025 | | #else |
1026 | 0 | *value = (float)m; |
1027 | 0 | #endif |
1028 | 0 | return (TIFFReadDirEntryErrOk); |
1029 | 0 | } |
1030 | 0 | case TIFF_SLONG8: |
1031 | 0 | { |
1032 | 0 | int64_t m; |
1033 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
1034 | 0 | if (err != TIFFReadDirEntryErrOk) |
1035 | 0 | return (err); |
1036 | 0 | *value = (float)m; |
1037 | 0 | return (TIFFReadDirEntryErrOk); |
1038 | 0 | } |
1039 | 0 | case TIFF_RATIONAL: |
1040 | 0 | { |
1041 | 0 | double m; |
1042 | 0 | err = TIFFReadDirEntryCheckedRational(tif, direntry, &m); |
1043 | 0 | if (err != TIFFReadDirEntryErrOk) |
1044 | 0 | return (err); |
1045 | 0 | *value = (float)m; |
1046 | 0 | return (TIFFReadDirEntryErrOk); |
1047 | 0 | } |
1048 | 0 | case TIFF_SRATIONAL: |
1049 | 0 | { |
1050 | 0 | double m; |
1051 | 0 | err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m); |
1052 | 0 | if (err != TIFFReadDirEntryErrOk) |
1053 | 0 | return (err); |
1054 | 0 | *value = (float)m; |
1055 | 0 | return (TIFFReadDirEntryErrOk); |
1056 | 0 | } |
1057 | 0 | case TIFF_FLOAT: |
1058 | 0 | TIFFReadDirEntryCheckedFloat(tif, direntry, value); |
1059 | 0 | return (TIFFReadDirEntryErrOk); |
1060 | 0 | case TIFF_DOUBLE: |
1061 | 0 | { |
1062 | 0 | double m; |
1063 | 0 | err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m); |
1064 | 0 | if (err != TIFFReadDirEntryErrOk) |
1065 | 0 | return (err); |
1066 | 0 | if ((m > FLT_MAX) || (m < -FLT_MAX)) |
1067 | 0 | return (TIFFReadDirEntryErrRange); |
1068 | 0 | *value = (float)m; |
1069 | 0 | return (TIFFReadDirEntryErrOk); |
1070 | 0 | } |
1071 | 0 | default: |
1072 | 0 | return (TIFFReadDirEntryErrType); |
1073 | 0 | } |
1074 | 0 | } |
1075 | | |
1076 | | static enum TIFFReadDirEntryErr |
1077 | | TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value) |
1078 | 0 | { |
1079 | 0 | enum TIFFReadDirEntryErr err; |
1080 | 0 | if (direntry->tdir_count != 1) |
1081 | 0 | return (TIFFReadDirEntryErrCount); |
1082 | 0 | switch (direntry->tdir_type) |
1083 | 0 | { |
1084 | 0 | case TIFF_BYTE: |
1085 | 0 | { |
1086 | 0 | uint8_t m; |
1087 | 0 | TIFFReadDirEntryCheckedByte(tif, direntry, &m); |
1088 | 0 | *value = (double)m; |
1089 | 0 | return (TIFFReadDirEntryErrOk); |
1090 | 0 | } |
1091 | 0 | case TIFF_SBYTE: |
1092 | 0 | { |
1093 | 0 | int8_t m; |
1094 | 0 | TIFFReadDirEntryCheckedSbyte(tif, direntry, &m); |
1095 | 0 | *value = (double)m; |
1096 | 0 | return (TIFFReadDirEntryErrOk); |
1097 | 0 | } |
1098 | 0 | case TIFF_SHORT: |
1099 | 0 | { |
1100 | 0 | uint16_t m; |
1101 | 0 | TIFFReadDirEntryCheckedShort(tif, direntry, &m); |
1102 | 0 | *value = (double)m; |
1103 | 0 | return (TIFFReadDirEntryErrOk); |
1104 | 0 | } |
1105 | 0 | case TIFF_SSHORT: |
1106 | 0 | { |
1107 | 0 | int16_t m; |
1108 | 0 | TIFFReadDirEntryCheckedSshort(tif, direntry, &m); |
1109 | 0 | *value = (double)m; |
1110 | 0 | return (TIFFReadDirEntryErrOk); |
1111 | 0 | } |
1112 | 0 | case TIFF_LONG: |
1113 | 0 | { |
1114 | 0 | uint32_t m; |
1115 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
1116 | 0 | *value = (double)m; |
1117 | 0 | return (TIFFReadDirEntryErrOk); |
1118 | 0 | } |
1119 | 0 | case TIFF_SLONG: |
1120 | 0 | { |
1121 | 0 | int32_t m; |
1122 | 0 | TIFFReadDirEntryCheckedSlong(tif, direntry, &m); |
1123 | 0 | *value = (double)m; |
1124 | 0 | return (TIFFReadDirEntryErrOk); |
1125 | 0 | } |
1126 | 0 | case TIFF_LONG8: |
1127 | 0 | { |
1128 | 0 | uint64_t m; |
1129 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m); |
1130 | 0 | if (err != TIFFReadDirEntryErrOk) |
1131 | 0 | return (err); |
1132 | | #if defined(__WIN32__) && (_MSC_VER < 1500) |
1133 | | /* |
1134 | | * XXX: MSVC 6.0 does not support conversion |
1135 | | * of 64-bit integers into floating point |
1136 | | * values. |
1137 | | */ |
1138 | | *value = _TIFFUInt64ToDouble(m); |
1139 | | #else |
1140 | 0 | *value = (double)m; |
1141 | 0 | #endif |
1142 | 0 | return (TIFFReadDirEntryErrOk); |
1143 | 0 | } |
1144 | 0 | case TIFF_SLONG8: |
1145 | 0 | { |
1146 | 0 | int64_t m; |
1147 | 0 | err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m); |
1148 | 0 | if (err != TIFFReadDirEntryErrOk) |
1149 | 0 | return (err); |
1150 | 0 | *value = (double)m; |
1151 | 0 | return (TIFFReadDirEntryErrOk); |
1152 | 0 | } |
1153 | 0 | case TIFF_RATIONAL: |
1154 | 0 | err = TIFFReadDirEntryCheckedRational(tif, direntry, value); |
1155 | 0 | return (err); |
1156 | 0 | case TIFF_SRATIONAL: |
1157 | 0 | err = TIFFReadDirEntryCheckedSrational(tif, direntry, value); |
1158 | 0 | return (err); |
1159 | 0 | case TIFF_FLOAT: |
1160 | 0 | { |
1161 | 0 | float m; |
1162 | 0 | TIFFReadDirEntryCheckedFloat(tif, direntry, &m); |
1163 | 0 | *value = (double)m; |
1164 | 0 | return (TIFFReadDirEntryErrOk); |
1165 | 0 | } |
1166 | 0 | case TIFF_DOUBLE: |
1167 | 0 | err = TIFFReadDirEntryCheckedDouble(tif, direntry, value); |
1168 | 0 | return (err); |
1169 | 0 | default: |
1170 | 0 | return (TIFFReadDirEntryErrType); |
1171 | 0 | } |
1172 | 0 | } |
1173 | | |
1174 | | static enum TIFFReadDirEntryErr |
1175 | | TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) |
1176 | 0 | { |
1177 | 0 | enum TIFFReadDirEntryErr err; |
1178 | 0 | if (direntry->tdir_count != 1) |
1179 | 0 | return (TIFFReadDirEntryErrCount); |
1180 | 0 | switch (direntry->tdir_type) |
1181 | 0 | { |
1182 | 0 | case TIFF_LONG: |
1183 | 0 | case TIFF_IFD: |
1184 | 0 | { |
1185 | 0 | uint32_t m; |
1186 | 0 | TIFFReadDirEntryCheckedLong(tif, direntry, &m); |
1187 | 0 | *value = (uint64_t)m; |
1188 | 0 | return (TIFFReadDirEntryErrOk); |
1189 | 0 | } |
1190 | 0 | case TIFF_LONG8: |
1191 | 0 | case TIFF_IFD8: |
1192 | 0 | err = TIFFReadDirEntryCheckedLong8(tif, direntry, value); |
1193 | 0 | return (err); |
1194 | 0 | default: |
1195 | 0 | return (TIFFReadDirEntryErrType); |
1196 | 0 | } |
1197 | 0 | } |
1198 | | |
1199 | 0 | #define INITIAL_THRESHOLD (1024 * 1024) |
1200 | 0 | #define THRESHOLD_MULTIPLIER 10 |
1201 | | #define MAX_THRESHOLD \ |
1202 | 0 | (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \ |
1203 | 0 | INITIAL_THRESHOLD) |
1204 | | |
1205 | | static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif, |
1206 | | uint64_t offset, |
1207 | | tmsize_t size, |
1208 | | void **pdest) |
1209 | 0 | { |
1210 | 0 | #if SIZEOF_SIZE_T == 8 |
1211 | 0 | tmsize_t threshold = INITIAL_THRESHOLD; |
1212 | 0 | #endif |
1213 | 0 | tmsize_t already_read = 0; |
1214 | |
|
1215 | 0 | assert(!isMapped(tif)); |
1216 | | |
1217 | 0 | if (!SeekOK(tif, offset)) |
1218 | 0 | return (TIFFReadDirEntryErrIo); |
1219 | | |
1220 | | /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */ |
1221 | | /* so as to avoid allocating too much memory in case the file is too */ |
1222 | | /* short. We could ask for the file size, but this might be */ |
1223 | | /* expensive with some I/O layers (think of reading a gzipped file) */ |
1224 | | /* Restrict to 64 bit processes, so as to avoid reallocs() */ |
1225 | | /* on 32 bit processes where virtual memory is scarce. */ |
1226 | 0 | while (already_read < size) |
1227 | 0 | { |
1228 | 0 | void *new_dest; |
1229 | 0 | tmsize_t bytes_read; |
1230 | 0 | tmsize_t to_read = size - already_read; |
1231 | 0 | #if SIZEOF_SIZE_T == 8 |
1232 | 0 | if (to_read >= threshold && threshold < MAX_THRESHOLD) |
1233 | 0 | { |
1234 | 0 | to_read = threshold; |
1235 | 0 | threshold *= THRESHOLD_MULTIPLIER; |
1236 | 0 | } |
1237 | 0 | #endif |
1238 | |
|
1239 | 0 | new_dest = |
1240 | 0 | (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read); |
1241 | 0 | if (new_dest == NULL) |
1242 | 0 | { |
1243 | 0 | TIFFErrorExtR(tif, tif->tif_name, |
1244 | 0 | "Failed to allocate memory for %s " |
1245 | 0 | "(%" TIFF_SSIZE_FORMAT |
1246 | 0 | " elements of %" TIFF_SSIZE_FORMAT " bytes each)", |
1247 | 0 | "TIFFReadDirEntryArray", (tmsize_t)1, |
1248 | 0 | already_read + to_read); |
1249 | 0 | return TIFFReadDirEntryErrAlloc; |
1250 | 0 | } |
1251 | 0 | *pdest = new_dest; |
1252 | |
|
1253 | 0 | bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read); |
1254 | 0 | already_read += bytes_read; |
1255 | 0 | if (bytes_read != to_read) |
1256 | 0 | { |
1257 | 0 | return TIFFReadDirEntryErrIo; |
1258 | 0 | } |
1259 | 0 | } |
1260 | 0 | return TIFFReadDirEntryErrOk; |
1261 | 0 | } |
1262 | | |
1263 | | /* Caution: if raising that value, make sure int32 / uint32 overflows can't |
1264 | | * occur elsewhere */ |
1265 | 0 | #define MAX_SIZE_TAG_DATA 2147483647U |
1266 | | |
1267 | | static enum TIFFReadDirEntryErr |
1268 | | TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, |
1269 | | uint32_t *count, uint32_t desttypesize, |
1270 | | void **value, uint64_t maxcount) |
1271 | 0 | { |
1272 | 0 | int typesize; |
1273 | 0 | uint32_t datasize; |
1274 | 0 | void *data; |
1275 | 0 | uint64_t target_count64; |
1276 | 0 | int original_datasize_clamped; |
1277 | 0 | typesize = TIFFDataWidth(direntry->tdir_type); |
1278 | |
|
1279 | 0 | target_count64 = |
1280 | 0 | (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count; |
1281 | |
|
1282 | 0 | if ((target_count64 == 0) || (typesize == 0)) |
1283 | 0 | { |
1284 | 0 | *value = 0; |
1285 | 0 | return (TIFFReadDirEntryErrOk); |
1286 | 0 | } |
1287 | 0 | (void)desttypesize; |
1288 | | |
1289 | | /* We just want to know if the original tag size is more than 4 bytes |
1290 | | * (classic TIFF) or 8 bytes (BigTIFF) |
1291 | | */ |
1292 | 0 | original_datasize_clamped = |
1293 | 0 | ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) * |
1294 | 0 | typesize; |
1295 | | |
1296 | | /* |
1297 | | * As a sanity check, make sure we have no more than a 2GB tag array |
1298 | | * in either the current data type or the dest data type. This also |
1299 | | * avoids problems with overflow of tmsize_t on 32bit systems. |
1300 | | */ |
1301 | 0 | if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64) |
1302 | 0 | return (TIFFReadDirEntryErrSizesan); |
1303 | 0 | if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64) |
1304 | 0 | return (TIFFReadDirEntryErrSizesan); |
1305 | | |
1306 | 0 | *count = (uint32_t)target_count64; |
1307 | 0 | datasize = (*count) * typesize; |
1308 | 0 | assert((tmsize_t)datasize > 0); |
1309 | | |
1310 | 0 | if (isMapped(tif) && datasize > (uint64_t)tif->tif_size) |
1311 | 0 | return TIFFReadDirEntryErrIo; |
1312 | | |
1313 | 0 | if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) || |
1314 | 0 | (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4))) |
1315 | 0 | { |
1316 | 0 | data = NULL; |
1317 | 0 | } |
1318 | 0 | else |
1319 | 0 | { |
1320 | 0 | data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray"); |
1321 | 0 | if (data == 0) |
1322 | 0 | return (TIFFReadDirEntryErrAlloc); |
1323 | 0 | } |
1324 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
1325 | 0 | { |
1326 | | /* Only the condition on original_datasize_clamped. The second |
1327 | | * one is implied, but Coverity Scan cannot see it. */ |
1328 | 0 | if (original_datasize_clamped <= 4 && datasize <= 4) |
1329 | 0 | _TIFFmemcpy(data, &direntry->tdir_offset, datasize); |
1330 | 0 | else |
1331 | 0 | { |
1332 | 0 | enum TIFFReadDirEntryErr err; |
1333 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
1334 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1335 | 0 | TIFFSwabLong(&offset); |
1336 | 0 | if (isMapped(tif)) |
1337 | 0 | err = TIFFReadDirEntryData(tif, (uint64_t)offset, |
1338 | 0 | (tmsize_t)datasize, data); |
1339 | 0 | else |
1340 | 0 | err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, |
1341 | 0 | (tmsize_t)datasize, &data); |
1342 | 0 | if (err != TIFFReadDirEntryErrOk) |
1343 | 0 | { |
1344 | 0 | _TIFFfreeExt(tif, data); |
1345 | 0 | return (err); |
1346 | 0 | } |
1347 | 0 | } |
1348 | 0 | } |
1349 | 0 | else |
1350 | 0 | { |
1351 | | /* See above comment for the Classic TIFF case */ |
1352 | 0 | if (original_datasize_clamped <= 8 && datasize <= 8) |
1353 | 0 | _TIFFmemcpy(data, &direntry->tdir_offset, datasize); |
1354 | 0 | else |
1355 | 0 | { |
1356 | 0 | enum TIFFReadDirEntryErr err; |
1357 | 0 | uint64_t offset = direntry->tdir_offset.toff_long8; |
1358 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1359 | 0 | TIFFSwabLong8(&offset); |
1360 | 0 | if (isMapped(tif)) |
1361 | 0 | err = TIFFReadDirEntryData(tif, (uint64_t)offset, |
1362 | 0 | (tmsize_t)datasize, data); |
1363 | 0 | else |
1364 | 0 | err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, |
1365 | 0 | (tmsize_t)datasize, &data); |
1366 | 0 | if (err != TIFFReadDirEntryErrOk) |
1367 | 0 | { |
1368 | 0 | _TIFFfreeExt(tif, data); |
1369 | 0 | return (err); |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | } |
1373 | 0 | *value = data; |
1374 | 0 | return (TIFFReadDirEntryErrOk); |
1375 | 0 | } |
1376 | | |
1377 | | static enum TIFFReadDirEntryErr |
1378 | | TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count, |
1379 | | uint32_t desttypesize, void **value) |
1380 | 0 | { |
1381 | 0 | return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize, |
1382 | 0 | value, ~((uint64_t)0)); |
1383 | 0 | } |
1384 | | |
1385 | | static enum TIFFReadDirEntryErr |
1386 | | TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value) |
1387 | 0 | { |
1388 | 0 | enum TIFFReadDirEntryErr err; |
1389 | 0 | uint32_t count; |
1390 | 0 | void *origdata; |
1391 | 0 | uint8_t *data; |
1392 | 0 | switch (direntry->tdir_type) |
1393 | 0 | { |
1394 | 0 | case TIFF_ASCII: |
1395 | 0 | case TIFF_UNDEFINED: |
1396 | 0 | case TIFF_BYTE: |
1397 | 0 | case TIFF_SBYTE: |
1398 | 0 | case TIFF_SHORT: |
1399 | 0 | case TIFF_SSHORT: |
1400 | 0 | case TIFF_LONG: |
1401 | 0 | case TIFF_SLONG: |
1402 | 0 | case TIFF_LONG8: |
1403 | 0 | case TIFF_SLONG8: |
1404 | 0 | break; |
1405 | 0 | default: |
1406 | 0 | return (TIFFReadDirEntryErrType); |
1407 | 0 | } |
1408 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata); |
1409 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
1410 | 0 | { |
1411 | 0 | *value = 0; |
1412 | 0 | return (err); |
1413 | 0 | } |
1414 | 0 | switch (direntry->tdir_type) |
1415 | 0 | { |
1416 | 0 | case TIFF_ASCII: |
1417 | 0 | case TIFF_UNDEFINED: |
1418 | 0 | case TIFF_BYTE: |
1419 | 0 | *value = (uint8_t *)origdata; |
1420 | 0 | return (TIFFReadDirEntryErrOk); |
1421 | 0 | case TIFF_SBYTE: |
1422 | 0 | { |
1423 | 0 | int8_t *m; |
1424 | 0 | uint32_t n; |
1425 | 0 | m = (int8_t *)origdata; |
1426 | 0 | for (n = 0; n < count; n++) |
1427 | 0 | { |
1428 | 0 | err = TIFFReadDirEntryCheckRangeByteSbyte(*m); |
1429 | 0 | if (err != TIFFReadDirEntryErrOk) |
1430 | 0 | { |
1431 | 0 | _TIFFfreeExt(tif, origdata); |
1432 | 0 | return (err); |
1433 | 0 | } |
1434 | 0 | m++; |
1435 | 0 | } |
1436 | 0 | *value = (uint8_t *)origdata; |
1437 | 0 | return (TIFFReadDirEntryErrOk); |
1438 | 0 | } |
1439 | 0 | } |
1440 | 0 | data = (uint8_t *)_TIFFmallocExt(tif, count); |
1441 | 0 | if (data == 0) |
1442 | 0 | { |
1443 | 0 | _TIFFfreeExt(tif, origdata); |
1444 | 0 | return (TIFFReadDirEntryErrAlloc); |
1445 | 0 | } |
1446 | 0 | switch (direntry->tdir_type) |
1447 | 0 | { |
1448 | 0 | case TIFF_SHORT: |
1449 | 0 | { |
1450 | 0 | uint16_t *ma; |
1451 | 0 | uint8_t *mb; |
1452 | 0 | uint32_t n; |
1453 | 0 | ma = (uint16_t *)origdata; |
1454 | 0 | mb = data; |
1455 | 0 | for (n = 0; n < count; n++) |
1456 | 0 | { |
1457 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1458 | 0 | TIFFSwabShort(ma); |
1459 | 0 | err = TIFFReadDirEntryCheckRangeByteShort(*ma); |
1460 | 0 | if (err != TIFFReadDirEntryErrOk) |
1461 | 0 | break; |
1462 | 0 | *mb++ = (uint8_t)(*ma++); |
1463 | 0 | } |
1464 | 0 | } |
1465 | 0 | break; |
1466 | 0 | case TIFF_SSHORT: |
1467 | 0 | { |
1468 | 0 | int16_t *ma; |
1469 | 0 | uint8_t *mb; |
1470 | 0 | uint32_t n; |
1471 | 0 | ma = (int16_t *)origdata; |
1472 | 0 | mb = data; |
1473 | 0 | for (n = 0; n < count; n++) |
1474 | 0 | { |
1475 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1476 | 0 | TIFFSwabShort((uint16_t *)ma); |
1477 | 0 | err = TIFFReadDirEntryCheckRangeByteSshort(*ma); |
1478 | 0 | if (err != TIFFReadDirEntryErrOk) |
1479 | 0 | break; |
1480 | 0 | *mb++ = (uint8_t)(*ma++); |
1481 | 0 | } |
1482 | 0 | } |
1483 | 0 | break; |
1484 | 0 | case TIFF_LONG: |
1485 | 0 | { |
1486 | 0 | uint32_t *ma; |
1487 | 0 | uint8_t *mb; |
1488 | 0 | uint32_t n; |
1489 | 0 | ma = (uint32_t *)origdata; |
1490 | 0 | mb = data; |
1491 | 0 | for (n = 0; n < count; n++) |
1492 | 0 | { |
1493 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1494 | 0 | TIFFSwabLong(ma); |
1495 | 0 | err = TIFFReadDirEntryCheckRangeByteLong(*ma); |
1496 | 0 | if (err != TIFFReadDirEntryErrOk) |
1497 | 0 | break; |
1498 | 0 | *mb++ = (uint8_t)(*ma++); |
1499 | 0 | } |
1500 | 0 | } |
1501 | 0 | break; |
1502 | 0 | case TIFF_SLONG: |
1503 | 0 | { |
1504 | 0 | int32_t *ma; |
1505 | 0 | uint8_t *mb; |
1506 | 0 | uint32_t n; |
1507 | 0 | ma = (int32_t *)origdata; |
1508 | 0 | mb = data; |
1509 | 0 | for (n = 0; n < count; n++) |
1510 | 0 | { |
1511 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1512 | 0 | TIFFSwabLong((uint32_t *)ma); |
1513 | 0 | err = TIFFReadDirEntryCheckRangeByteSlong(*ma); |
1514 | 0 | if (err != TIFFReadDirEntryErrOk) |
1515 | 0 | break; |
1516 | 0 | *mb++ = (uint8_t)(*ma++); |
1517 | 0 | } |
1518 | 0 | } |
1519 | 0 | break; |
1520 | 0 | case TIFF_LONG8: |
1521 | 0 | { |
1522 | 0 | uint64_t *ma; |
1523 | 0 | uint8_t *mb; |
1524 | 0 | uint32_t n; |
1525 | 0 | ma = (uint64_t *)origdata; |
1526 | 0 | mb = data; |
1527 | 0 | for (n = 0; n < count; n++) |
1528 | 0 | { |
1529 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1530 | 0 | TIFFSwabLong8(ma); |
1531 | 0 | err = TIFFReadDirEntryCheckRangeByteLong8(*ma); |
1532 | 0 | if (err != TIFFReadDirEntryErrOk) |
1533 | 0 | break; |
1534 | 0 | *mb++ = (uint8_t)(*ma++); |
1535 | 0 | } |
1536 | 0 | } |
1537 | 0 | break; |
1538 | 0 | case TIFF_SLONG8: |
1539 | 0 | { |
1540 | 0 | int64_t *ma; |
1541 | 0 | uint8_t *mb; |
1542 | 0 | uint32_t n; |
1543 | 0 | ma = (int64_t *)origdata; |
1544 | 0 | mb = data; |
1545 | 0 | for (n = 0; n < count; n++) |
1546 | 0 | { |
1547 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1548 | 0 | TIFFSwabLong8((uint64_t *)ma); |
1549 | 0 | err = TIFFReadDirEntryCheckRangeByteSlong8(*ma); |
1550 | 0 | if (err != TIFFReadDirEntryErrOk) |
1551 | 0 | break; |
1552 | 0 | *mb++ = (uint8_t)(*ma++); |
1553 | 0 | } |
1554 | 0 | } |
1555 | 0 | break; |
1556 | 0 | } |
1557 | 0 | _TIFFfreeExt(tif, origdata); |
1558 | 0 | if (err != TIFFReadDirEntryErrOk) |
1559 | 0 | { |
1560 | 0 | _TIFFfreeExt(tif, data); |
1561 | 0 | return (err); |
1562 | 0 | } |
1563 | 0 | *value = data; |
1564 | 0 | return (TIFFReadDirEntryErrOk); |
1565 | 0 | } |
1566 | | |
1567 | | static enum TIFFReadDirEntryErr |
1568 | | TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value) |
1569 | 0 | { |
1570 | 0 | enum TIFFReadDirEntryErr err; |
1571 | 0 | uint32_t count; |
1572 | 0 | void *origdata; |
1573 | 0 | int8_t *data; |
1574 | 0 | switch (direntry->tdir_type) |
1575 | 0 | { |
1576 | 0 | case TIFF_UNDEFINED: |
1577 | 0 | case TIFF_BYTE: |
1578 | 0 | case TIFF_SBYTE: |
1579 | 0 | case TIFF_SHORT: |
1580 | 0 | case TIFF_SSHORT: |
1581 | 0 | case TIFF_LONG: |
1582 | 0 | case TIFF_SLONG: |
1583 | 0 | case TIFF_LONG8: |
1584 | 0 | case TIFF_SLONG8: |
1585 | 0 | break; |
1586 | 0 | default: |
1587 | 0 | return (TIFFReadDirEntryErrType); |
1588 | 0 | } |
1589 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata); |
1590 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
1591 | 0 | { |
1592 | 0 | *value = 0; |
1593 | 0 | return (err); |
1594 | 0 | } |
1595 | 0 | switch (direntry->tdir_type) |
1596 | 0 | { |
1597 | 0 | case TIFF_UNDEFINED: |
1598 | 0 | case TIFF_BYTE: |
1599 | 0 | { |
1600 | 0 | uint8_t *m; |
1601 | 0 | uint32_t n; |
1602 | 0 | m = (uint8_t *)origdata; |
1603 | 0 | for (n = 0; n < count; n++) |
1604 | 0 | { |
1605 | 0 | err = TIFFReadDirEntryCheckRangeSbyteByte(*m); |
1606 | 0 | if (err != TIFFReadDirEntryErrOk) |
1607 | 0 | { |
1608 | 0 | _TIFFfreeExt(tif, origdata); |
1609 | 0 | return (err); |
1610 | 0 | } |
1611 | 0 | m++; |
1612 | 0 | } |
1613 | 0 | *value = (int8_t *)origdata; |
1614 | 0 | return (TIFFReadDirEntryErrOk); |
1615 | 0 | } |
1616 | 0 | case TIFF_SBYTE: |
1617 | 0 | *value = (int8_t *)origdata; |
1618 | 0 | return (TIFFReadDirEntryErrOk); |
1619 | 0 | } |
1620 | 0 | data = (int8_t *)_TIFFmallocExt(tif, count); |
1621 | 0 | if (data == 0) |
1622 | 0 | { |
1623 | 0 | _TIFFfreeExt(tif, origdata); |
1624 | 0 | return (TIFFReadDirEntryErrAlloc); |
1625 | 0 | } |
1626 | 0 | switch (direntry->tdir_type) |
1627 | 0 | { |
1628 | 0 | case TIFF_SHORT: |
1629 | 0 | { |
1630 | 0 | uint16_t *ma; |
1631 | 0 | int8_t *mb; |
1632 | 0 | uint32_t n; |
1633 | 0 | ma = (uint16_t *)origdata; |
1634 | 0 | mb = data; |
1635 | 0 | for (n = 0; n < count; n++) |
1636 | 0 | { |
1637 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1638 | 0 | TIFFSwabShort(ma); |
1639 | 0 | err = TIFFReadDirEntryCheckRangeSbyteShort(*ma); |
1640 | 0 | if (err != TIFFReadDirEntryErrOk) |
1641 | 0 | break; |
1642 | 0 | *mb++ = (int8_t)(*ma++); |
1643 | 0 | } |
1644 | 0 | } |
1645 | 0 | break; |
1646 | 0 | case TIFF_SSHORT: |
1647 | 0 | { |
1648 | 0 | int16_t *ma; |
1649 | 0 | int8_t *mb; |
1650 | 0 | uint32_t n; |
1651 | 0 | ma = (int16_t *)origdata; |
1652 | 0 | mb = data; |
1653 | 0 | for (n = 0; n < count; n++) |
1654 | 0 | { |
1655 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1656 | 0 | TIFFSwabShort((uint16_t *)ma); |
1657 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma); |
1658 | 0 | if (err != TIFFReadDirEntryErrOk) |
1659 | 0 | break; |
1660 | 0 | *mb++ = (int8_t)(*ma++); |
1661 | 0 | } |
1662 | 0 | } |
1663 | 0 | break; |
1664 | 0 | case TIFF_LONG: |
1665 | 0 | { |
1666 | 0 | uint32_t *ma; |
1667 | 0 | int8_t *mb; |
1668 | 0 | uint32_t n; |
1669 | 0 | ma = (uint32_t *)origdata; |
1670 | 0 | mb = data; |
1671 | 0 | for (n = 0; n < count; n++) |
1672 | 0 | { |
1673 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1674 | 0 | TIFFSwabLong(ma); |
1675 | 0 | err = TIFFReadDirEntryCheckRangeSbyteLong(*ma); |
1676 | 0 | if (err != TIFFReadDirEntryErrOk) |
1677 | 0 | break; |
1678 | 0 | *mb++ = (int8_t)(*ma++); |
1679 | 0 | } |
1680 | 0 | } |
1681 | 0 | break; |
1682 | 0 | case TIFF_SLONG: |
1683 | 0 | { |
1684 | 0 | int32_t *ma; |
1685 | 0 | int8_t *mb; |
1686 | 0 | uint32_t n; |
1687 | 0 | ma = (int32_t *)origdata; |
1688 | 0 | mb = data; |
1689 | 0 | for (n = 0; n < count; n++) |
1690 | 0 | { |
1691 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1692 | 0 | TIFFSwabLong((uint32_t *)ma); |
1693 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma); |
1694 | 0 | if (err != TIFFReadDirEntryErrOk) |
1695 | 0 | break; |
1696 | 0 | *mb++ = (int8_t)(*ma++); |
1697 | 0 | } |
1698 | 0 | } |
1699 | 0 | break; |
1700 | 0 | case TIFF_LONG8: |
1701 | 0 | { |
1702 | 0 | uint64_t *ma; |
1703 | 0 | int8_t *mb; |
1704 | 0 | uint32_t n; |
1705 | 0 | ma = (uint64_t *)origdata; |
1706 | 0 | mb = data; |
1707 | 0 | for (n = 0; n < count; n++) |
1708 | 0 | { |
1709 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1710 | 0 | TIFFSwabLong8(ma); |
1711 | 0 | err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma); |
1712 | 0 | if (err != TIFFReadDirEntryErrOk) |
1713 | 0 | break; |
1714 | 0 | *mb++ = (int8_t)(*ma++); |
1715 | 0 | } |
1716 | 0 | } |
1717 | 0 | break; |
1718 | 0 | case TIFF_SLONG8: |
1719 | 0 | { |
1720 | 0 | int64_t *ma; |
1721 | 0 | int8_t *mb; |
1722 | 0 | uint32_t n; |
1723 | 0 | ma = (int64_t *)origdata; |
1724 | 0 | mb = data; |
1725 | 0 | for (n = 0; n < count; n++) |
1726 | 0 | { |
1727 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1728 | 0 | TIFFSwabLong8((uint64_t *)ma); |
1729 | 0 | err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma); |
1730 | 0 | if (err != TIFFReadDirEntryErrOk) |
1731 | 0 | break; |
1732 | 0 | *mb++ = (int8_t)(*ma++); |
1733 | 0 | } |
1734 | 0 | } |
1735 | 0 | break; |
1736 | 0 | } |
1737 | 0 | _TIFFfreeExt(tif, origdata); |
1738 | 0 | if (err != TIFFReadDirEntryErrOk) |
1739 | 0 | { |
1740 | 0 | _TIFFfreeExt(tif, data); |
1741 | 0 | return (err); |
1742 | 0 | } |
1743 | 0 | *value = data; |
1744 | 0 | return (TIFFReadDirEntryErrOk); |
1745 | 0 | } |
1746 | | |
1747 | | static enum TIFFReadDirEntryErr |
1748 | | TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value) |
1749 | 0 | { |
1750 | 0 | enum TIFFReadDirEntryErr err; |
1751 | 0 | uint32_t count; |
1752 | 0 | void *origdata; |
1753 | 0 | uint16_t *data; |
1754 | 0 | switch (direntry->tdir_type) |
1755 | 0 | { |
1756 | 0 | case TIFF_BYTE: |
1757 | 0 | case TIFF_SBYTE: |
1758 | 0 | case TIFF_SHORT: |
1759 | 0 | case TIFF_SSHORT: |
1760 | 0 | case TIFF_LONG: |
1761 | 0 | case TIFF_SLONG: |
1762 | 0 | case TIFF_LONG8: |
1763 | 0 | case TIFF_SLONG8: |
1764 | 0 | break; |
1765 | 0 | default: |
1766 | 0 | return (TIFFReadDirEntryErrType); |
1767 | 0 | } |
1768 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata); |
1769 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
1770 | 0 | { |
1771 | 0 | *value = 0; |
1772 | 0 | return (err); |
1773 | 0 | } |
1774 | 0 | switch (direntry->tdir_type) |
1775 | 0 | { |
1776 | 0 | case TIFF_SHORT: |
1777 | 0 | *value = (uint16_t *)origdata; |
1778 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1779 | 0 | TIFFSwabArrayOfShort(*value, count); |
1780 | 0 | return (TIFFReadDirEntryErrOk); |
1781 | 0 | case TIFF_SSHORT: |
1782 | 0 | { |
1783 | 0 | int16_t *m; |
1784 | 0 | uint32_t n; |
1785 | 0 | m = (int16_t *)origdata; |
1786 | 0 | for (n = 0; n < count; n++) |
1787 | 0 | { |
1788 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1789 | 0 | TIFFSwabShort((uint16_t *)m); |
1790 | 0 | err = TIFFReadDirEntryCheckRangeShortSshort(*m); |
1791 | 0 | if (err != TIFFReadDirEntryErrOk) |
1792 | 0 | { |
1793 | 0 | _TIFFfreeExt(tif, origdata); |
1794 | 0 | return (err); |
1795 | 0 | } |
1796 | 0 | m++; |
1797 | 0 | } |
1798 | 0 | *value = (uint16_t *)origdata; |
1799 | 0 | return (TIFFReadDirEntryErrOk); |
1800 | 0 | } |
1801 | 0 | } |
1802 | 0 | data = (uint16_t *)_TIFFmallocExt(tif, count * 2); |
1803 | 0 | if (data == 0) |
1804 | 0 | { |
1805 | 0 | _TIFFfreeExt(tif, origdata); |
1806 | 0 | return (TIFFReadDirEntryErrAlloc); |
1807 | 0 | } |
1808 | 0 | switch (direntry->tdir_type) |
1809 | 0 | { |
1810 | 0 | case TIFF_BYTE: |
1811 | 0 | { |
1812 | 0 | uint8_t *ma; |
1813 | 0 | uint16_t *mb; |
1814 | 0 | uint32_t n; |
1815 | 0 | ma = (uint8_t *)origdata; |
1816 | 0 | mb = data; |
1817 | 0 | for (n = 0; n < count; n++) |
1818 | 0 | *mb++ = (uint16_t)(*ma++); |
1819 | 0 | } |
1820 | 0 | break; |
1821 | 0 | case TIFF_SBYTE: |
1822 | 0 | { |
1823 | 0 | int8_t *ma; |
1824 | 0 | uint16_t *mb; |
1825 | 0 | uint32_t n; |
1826 | 0 | ma = (int8_t *)origdata; |
1827 | 0 | mb = data; |
1828 | 0 | for (n = 0; n < count; n++) |
1829 | 0 | { |
1830 | 0 | err = TIFFReadDirEntryCheckRangeShortSbyte(*ma); |
1831 | 0 | if (err != TIFFReadDirEntryErrOk) |
1832 | 0 | break; |
1833 | 0 | *mb++ = (uint16_t)(*ma++); |
1834 | 0 | } |
1835 | 0 | } |
1836 | 0 | break; |
1837 | 0 | case TIFF_LONG: |
1838 | 0 | { |
1839 | 0 | uint32_t *ma; |
1840 | 0 | uint16_t *mb; |
1841 | 0 | uint32_t n; |
1842 | 0 | ma = (uint32_t *)origdata; |
1843 | 0 | mb = data; |
1844 | 0 | for (n = 0; n < count; n++) |
1845 | 0 | { |
1846 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1847 | 0 | TIFFSwabLong(ma); |
1848 | 0 | err = TIFFReadDirEntryCheckRangeShortLong(*ma); |
1849 | 0 | if (err != TIFFReadDirEntryErrOk) |
1850 | 0 | break; |
1851 | 0 | *mb++ = (uint16_t)(*ma++); |
1852 | 0 | } |
1853 | 0 | } |
1854 | 0 | break; |
1855 | 0 | case TIFF_SLONG: |
1856 | 0 | { |
1857 | 0 | int32_t *ma; |
1858 | 0 | uint16_t *mb; |
1859 | 0 | uint32_t n; |
1860 | 0 | ma = (int32_t *)origdata; |
1861 | 0 | mb = data; |
1862 | 0 | for (n = 0; n < count; n++) |
1863 | 0 | { |
1864 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1865 | 0 | TIFFSwabLong((uint32_t *)ma); |
1866 | 0 | err = TIFFReadDirEntryCheckRangeShortSlong(*ma); |
1867 | 0 | if (err != TIFFReadDirEntryErrOk) |
1868 | 0 | break; |
1869 | 0 | *mb++ = (uint16_t)(*ma++); |
1870 | 0 | } |
1871 | 0 | } |
1872 | 0 | break; |
1873 | 0 | case TIFF_LONG8: |
1874 | 0 | { |
1875 | 0 | uint64_t *ma; |
1876 | 0 | uint16_t *mb; |
1877 | 0 | uint32_t n; |
1878 | 0 | ma = (uint64_t *)origdata; |
1879 | 0 | mb = data; |
1880 | 0 | for (n = 0; n < count; n++) |
1881 | 0 | { |
1882 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1883 | 0 | TIFFSwabLong8(ma); |
1884 | 0 | err = TIFFReadDirEntryCheckRangeShortLong8(*ma); |
1885 | 0 | if (err != TIFFReadDirEntryErrOk) |
1886 | 0 | break; |
1887 | 0 | *mb++ = (uint16_t)(*ma++); |
1888 | 0 | } |
1889 | 0 | } |
1890 | 0 | break; |
1891 | 0 | case TIFF_SLONG8: |
1892 | 0 | { |
1893 | 0 | int64_t *ma; |
1894 | 0 | uint16_t *mb; |
1895 | 0 | uint32_t n; |
1896 | 0 | ma = (int64_t *)origdata; |
1897 | 0 | mb = data; |
1898 | 0 | for (n = 0; n < count; n++) |
1899 | 0 | { |
1900 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1901 | 0 | TIFFSwabLong8((uint64_t *)ma); |
1902 | 0 | err = TIFFReadDirEntryCheckRangeShortSlong8(*ma); |
1903 | 0 | if (err != TIFFReadDirEntryErrOk) |
1904 | 0 | break; |
1905 | 0 | *mb++ = (uint16_t)(*ma++); |
1906 | 0 | } |
1907 | 0 | } |
1908 | 0 | break; |
1909 | 0 | } |
1910 | 0 | _TIFFfreeExt(tif, origdata); |
1911 | 0 | if (err != TIFFReadDirEntryErrOk) |
1912 | 0 | { |
1913 | 0 | _TIFFfreeExt(tif, data); |
1914 | 0 | return (err); |
1915 | 0 | } |
1916 | 0 | *value = data; |
1917 | 0 | return (TIFFReadDirEntryErrOk); |
1918 | 0 | } |
1919 | | |
1920 | | static enum TIFFReadDirEntryErr |
1921 | | TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value) |
1922 | 0 | { |
1923 | 0 | enum TIFFReadDirEntryErr err; |
1924 | 0 | uint32_t count; |
1925 | 0 | void *origdata; |
1926 | 0 | int16_t *data; |
1927 | 0 | switch (direntry->tdir_type) |
1928 | 0 | { |
1929 | 0 | case TIFF_BYTE: |
1930 | 0 | case TIFF_SBYTE: |
1931 | 0 | case TIFF_SHORT: |
1932 | 0 | case TIFF_SSHORT: |
1933 | 0 | case TIFF_LONG: |
1934 | 0 | case TIFF_SLONG: |
1935 | 0 | case TIFF_LONG8: |
1936 | 0 | case TIFF_SLONG8: |
1937 | 0 | break; |
1938 | 0 | default: |
1939 | 0 | return (TIFFReadDirEntryErrType); |
1940 | 0 | } |
1941 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata); |
1942 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
1943 | 0 | { |
1944 | 0 | *value = 0; |
1945 | 0 | return (err); |
1946 | 0 | } |
1947 | 0 | switch (direntry->tdir_type) |
1948 | 0 | { |
1949 | 0 | case TIFF_SHORT: |
1950 | 0 | { |
1951 | 0 | uint16_t *m; |
1952 | 0 | uint32_t n; |
1953 | 0 | m = (uint16_t *)origdata; |
1954 | 0 | for (n = 0; n < count; n++) |
1955 | 0 | { |
1956 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1957 | 0 | TIFFSwabShort(m); |
1958 | 0 | err = TIFFReadDirEntryCheckRangeSshortShort(*m); |
1959 | 0 | if (err != TIFFReadDirEntryErrOk) |
1960 | 0 | { |
1961 | 0 | _TIFFfreeExt(tif, origdata); |
1962 | 0 | return (err); |
1963 | 0 | } |
1964 | 0 | m++; |
1965 | 0 | } |
1966 | 0 | *value = (int16_t *)origdata; |
1967 | 0 | return (TIFFReadDirEntryErrOk); |
1968 | 0 | } |
1969 | 0 | case TIFF_SSHORT: |
1970 | 0 | *value = (int16_t *)origdata; |
1971 | 0 | if (tif->tif_flags & TIFF_SWAB) |
1972 | 0 | TIFFSwabArrayOfShort((uint16_t *)(*value), count); |
1973 | 0 | return (TIFFReadDirEntryErrOk); |
1974 | 0 | } |
1975 | 0 | data = (int16_t *)_TIFFmallocExt(tif, count * 2); |
1976 | 0 | if (data == 0) |
1977 | 0 | { |
1978 | 0 | _TIFFfreeExt(tif, origdata); |
1979 | 0 | return (TIFFReadDirEntryErrAlloc); |
1980 | 0 | } |
1981 | 0 | switch (direntry->tdir_type) |
1982 | 0 | { |
1983 | 0 | case TIFF_BYTE: |
1984 | 0 | { |
1985 | 0 | uint8_t *ma; |
1986 | 0 | int16_t *mb; |
1987 | 0 | uint32_t n; |
1988 | 0 | ma = (uint8_t *)origdata; |
1989 | 0 | mb = data; |
1990 | 0 | for (n = 0; n < count; n++) |
1991 | 0 | *mb++ = (int16_t)(*ma++); |
1992 | 0 | } |
1993 | 0 | break; |
1994 | 0 | case TIFF_SBYTE: |
1995 | 0 | { |
1996 | 0 | int8_t *ma; |
1997 | 0 | int16_t *mb; |
1998 | 0 | uint32_t n; |
1999 | 0 | ma = (int8_t *)origdata; |
2000 | 0 | mb = data; |
2001 | 0 | for (n = 0; n < count; n++) |
2002 | 0 | *mb++ = (int16_t)(*ma++); |
2003 | 0 | } |
2004 | 0 | break; |
2005 | 0 | case TIFF_LONG: |
2006 | 0 | { |
2007 | 0 | uint32_t *ma; |
2008 | 0 | int16_t *mb; |
2009 | 0 | uint32_t n; |
2010 | 0 | ma = (uint32_t *)origdata; |
2011 | 0 | mb = data; |
2012 | 0 | for (n = 0; n < count; n++) |
2013 | 0 | { |
2014 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2015 | 0 | TIFFSwabLong(ma); |
2016 | 0 | err = TIFFReadDirEntryCheckRangeSshortLong(*ma); |
2017 | 0 | if (err != TIFFReadDirEntryErrOk) |
2018 | 0 | break; |
2019 | 0 | *mb++ = (int16_t)(*ma++); |
2020 | 0 | } |
2021 | 0 | } |
2022 | 0 | break; |
2023 | 0 | case TIFF_SLONG: |
2024 | 0 | { |
2025 | 0 | int32_t *ma; |
2026 | 0 | int16_t *mb; |
2027 | 0 | uint32_t n; |
2028 | 0 | ma = (int32_t *)origdata; |
2029 | 0 | mb = data; |
2030 | 0 | for (n = 0; n < count; n++) |
2031 | 0 | { |
2032 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2033 | 0 | TIFFSwabLong((uint32_t *)ma); |
2034 | 0 | err = TIFFReadDirEntryCheckRangeSshortSlong(*ma); |
2035 | 0 | if (err != TIFFReadDirEntryErrOk) |
2036 | 0 | break; |
2037 | 0 | *mb++ = (int16_t)(*ma++); |
2038 | 0 | } |
2039 | 0 | } |
2040 | 0 | break; |
2041 | 0 | case TIFF_LONG8: |
2042 | 0 | { |
2043 | 0 | uint64_t *ma; |
2044 | 0 | int16_t *mb; |
2045 | 0 | uint32_t n; |
2046 | 0 | ma = (uint64_t *)origdata; |
2047 | 0 | mb = data; |
2048 | 0 | for (n = 0; n < count; n++) |
2049 | 0 | { |
2050 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2051 | 0 | TIFFSwabLong8(ma); |
2052 | 0 | err = TIFFReadDirEntryCheckRangeSshortLong8(*ma); |
2053 | 0 | if (err != TIFFReadDirEntryErrOk) |
2054 | 0 | break; |
2055 | 0 | *mb++ = (int16_t)(*ma++); |
2056 | 0 | } |
2057 | 0 | } |
2058 | 0 | break; |
2059 | 0 | case TIFF_SLONG8: |
2060 | 0 | { |
2061 | 0 | int64_t *ma; |
2062 | 0 | int16_t *mb; |
2063 | 0 | uint32_t n; |
2064 | 0 | ma = (int64_t *)origdata; |
2065 | 0 | mb = data; |
2066 | 0 | for (n = 0; n < count; n++) |
2067 | 0 | { |
2068 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2069 | 0 | TIFFSwabLong8((uint64_t *)ma); |
2070 | 0 | err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma); |
2071 | 0 | if (err != TIFFReadDirEntryErrOk) |
2072 | 0 | break; |
2073 | 0 | *mb++ = (int16_t)(*ma++); |
2074 | 0 | } |
2075 | 0 | } |
2076 | 0 | break; |
2077 | 0 | } |
2078 | 0 | _TIFFfreeExt(tif, origdata); |
2079 | 0 | if (err != TIFFReadDirEntryErrOk) |
2080 | 0 | { |
2081 | 0 | _TIFFfreeExt(tif, data); |
2082 | 0 | return (err); |
2083 | 0 | } |
2084 | 0 | *value = data; |
2085 | 0 | return (TIFFReadDirEntryErrOk); |
2086 | 0 | } |
2087 | | |
2088 | | static enum TIFFReadDirEntryErr |
2089 | | TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value) |
2090 | 0 | { |
2091 | 0 | enum TIFFReadDirEntryErr err; |
2092 | 0 | uint32_t count; |
2093 | 0 | void *origdata; |
2094 | 0 | uint32_t *data; |
2095 | 0 | switch (direntry->tdir_type) |
2096 | 0 | { |
2097 | 0 | case TIFF_BYTE: |
2098 | 0 | case TIFF_SBYTE: |
2099 | 0 | case TIFF_SHORT: |
2100 | 0 | case TIFF_SSHORT: |
2101 | 0 | case TIFF_LONG: |
2102 | 0 | case TIFF_SLONG: |
2103 | 0 | case TIFF_LONG8: |
2104 | 0 | case TIFF_SLONG8: |
2105 | 0 | break; |
2106 | 0 | default: |
2107 | 0 | return (TIFFReadDirEntryErrType); |
2108 | 0 | } |
2109 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); |
2110 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
2111 | 0 | { |
2112 | 0 | *value = 0; |
2113 | 0 | return (err); |
2114 | 0 | } |
2115 | 0 | switch (direntry->tdir_type) |
2116 | 0 | { |
2117 | 0 | case TIFF_LONG: |
2118 | 0 | *value = (uint32_t *)origdata; |
2119 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2120 | 0 | TIFFSwabArrayOfLong(*value, count); |
2121 | 0 | return (TIFFReadDirEntryErrOk); |
2122 | 0 | case TIFF_SLONG: |
2123 | 0 | { |
2124 | 0 | int32_t *m; |
2125 | 0 | uint32_t n; |
2126 | 0 | m = (int32_t *)origdata; |
2127 | 0 | for (n = 0; n < count; n++) |
2128 | 0 | { |
2129 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2130 | 0 | TIFFSwabLong((uint32_t *)m); |
2131 | 0 | err = TIFFReadDirEntryCheckRangeLongSlong(*m); |
2132 | 0 | if (err != TIFFReadDirEntryErrOk) |
2133 | 0 | { |
2134 | 0 | _TIFFfreeExt(tif, origdata); |
2135 | 0 | return (err); |
2136 | 0 | } |
2137 | 0 | m++; |
2138 | 0 | } |
2139 | 0 | *value = (uint32_t *)origdata; |
2140 | 0 | return (TIFFReadDirEntryErrOk); |
2141 | 0 | } |
2142 | 0 | } |
2143 | 0 | data = (uint32_t *)_TIFFmallocExt(tif, count * 4); |
2144 | 0 | if (data == 0) |
2145 | 0 | { |
2146 | 0 | _TIFFfreeExt(tif, origdata); |
2147 | 0 | return (TIFFReadDirEntryErrAlloc); |
2148 | 0 | } |
2149 | 0 | switch (direntry->tdir_type) |
2150 | 0 | { |
2151 | 0 | case TIFF_BYTE: |
2152 | 0 | { |
2153 | 0 | uint8_t *ma; |
2154 | 0 | uint32_t *mb; |
2155 | 0 | uint32_t n; |
2156 | 0 | ma = (uint8_t *)origdata; |
2157 | 0 | mb = data; |
2158 | 0 | for (n = 0; n < count; n++) |
2159 | 0 | *mb++ = (uint32_t)(*ma++); |
2160 | 0 | } |
2161 | 0 | break; |
2162 | 0 | case TIFF_SBYTE: |
2163 | 0 | { |
2164 | 0 | int8_t *ma; |
2165 | 0 | uint32_t *mb; |
2166 | 0 | uint32_t n; |
2167 | 0 | ma = (int8_t *)origdata; |
2168 | 0 | mb = data; |
2169 | 0 | for (n = 0; n < count; n++) |
2170 | 0 | { |
2171 | 0 | err = TIFFReadDirEntryCheckRangeLongSbyte(*ma); |
2172 | 0 | if (err != TIFFReadDirEntryErrOk) |
2173 | 0 | break; |
2174 | 0 | *mb++ = (uint32_t)(*ma++); |
2175 | 0 | } |
2176 | 0 | } |
2177 | 0 | break; |
2178 | 0 | case TIFF_SHORT: |
2179 | 0 | { |
2180 | 0 | uint16_t *ma; |
2181 | 0 | uint32_t *mb; |
2182 | 0 | uint32_t n; |
2183 | 0 | ma = (uint16_t *)origdata; |
2184 | 0 | mb = data; |
2185 | 0 | for (n = 0; n < count; n++) |
2186 | 0 | { |
2187 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2188 | 0 | TIFFSwabShort(ma); |
2189 | 0 | *mb++ = (uint32_t)(*ma++); |
2190 | 0 | } |
2191 | 0 | } |
2192 | 0 | break; |
2193 | 0 | case TIFF_SSHORT: |
2194 | 0 | { |
2195 | 0 | int16_t *ma; |
2196 | 0 | uint32_t *mb; |
2197 | 0 | uint32_t n; |
2198 | 0 | ma = (int16_t *)origdata; |
2199 | 0 | mb = data; |
2200 | 0 | for (n = 0; n < count; n++) |
2201 | 0 | { |
2202 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2203 | 0 | TIFFSwabShort((uint16_t *)ma); |
2204 | 0 | err = TIFFReadDirEntryCheckRangeLongSshort(*ma); |
2205 | 0 | if (err != TIFFReadDirEntryErrOk) |
2206 | 0 | break; |
2207 | 0 | *mb++ = (uint32_t)(*ma++); |
2208 | 0 | } |
2209 | 0 | } |
2210 | 0 | break; |
2211 | 0 | case TIFF_LONG8: |
2212 | 0 | { |
2213 | 0 | uint64_t *ma; |
2214 | 0 | uint32_t *mb; |
2215 | 0 | uint32_t n; |
2216 | 0 | ma = (uint64_t *)origdata; |
2217 | 0 | mb = data; |
2218 | 0 | for (n = 0; n < count; n++) |
2219 | 0 | { |
2220 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2221 | 0 | TIFFSwabLong8(ma); |
2222 | 0 | err = TIFFReadDirEntryCheckRangeLongLong8(*ma); |
2223 | 0 | if (err != TIFFReadDirEntryErrOk) |
2224 | 0 | break; |
2225 | 0 | *mb++ = (uint32_t)(*ma++); |
2226 | 0 | } |
2227 | 0 | } |
2228 | 0 | break; |
2229 | 0 | case TIFF_SLONG8: |
2230 | 0 | { |
2231 | 0 | int64_t *ma; |
2232 | 0 | uint32_t *mb; |
2233 | 0 | uint32_t n; |
2234 | 0 | ma = (int64_t *)origdata; |
2235 | 0 | mb = data; |
2236 | 0 | for (n = 0; n < count; n++) |
2237 | 0 | { |
2238 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2239 | 0 | TIFFSwabLong8((uint64_t *)ma); |
2240 | 0 | err = TIFFReadDirEntryCheckRangeLongSlong8(*ma); |
2241 | 0 | if (err != TIFFReadDirEntryErrOk) |
2242 | 0 | break; |
2243 | 0 | *mb++ = (uint32_t)(*ma++); |
2244 | 0 | } |
2245 | 0 | } |
2246 | 0 | break; |
2247 | 0 | } |
2248 | 0 | _TIFFfreeExt(tif, origdata); |
2249 | 0 | if (err != TIFFReadDirEntryErrOk) |
2250 | 0 | { |
2251 | 0 | _TIFFfreeExt(tif, data); |
2252 | 0 | return (err); |
2253 | 0 | } |
2254 | 0 | *value = data; |
2255 | 0 | return (TIFFReadDirEntryErrOk); |
2256 | 0 | } |
2257 | | |
2258 | | static enum TIFFReadDirEntryErr |
2259 | | TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value) |
2260 | 0 | { |
2261 | 0 | enum TIFFReadDirEntryErr err; |
2262 | 0 | uint32_t count; |
2263 | 0 | void *origdata; |
2264 | 0 | int32_t *data; |
2265 | 0 | switch (direntry->tdir_type) |
2266 | 0 | { |
2267 | 0 | case TIFF_BYTE: |
2268 | 0 | case TIFF_SBYTE: |
2269 | 0 | case TIFF_SHORT: |
2270 | 0 | case TIFF_SSHORT: |
2271 | 0 | case TIFF_LONG: |
2272 | 0 | case TIFF_SLONG: |
2273 | 0 | case TIFF_LONG8: |
2274 | 0 | case TIFF_SLONG8: |
2275 | 0 | break; |
2276 | 0 | default: |
2277 | 0 | return (TIFFReadDirEntryErrType); |
2278 | 0 | } |
2279 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); |
2280 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
2281 | 0 | { |
2282 | 0 | *value = 0; |
2283 | 0 | return (err); |
2284 | 0 | } |
2285 | 0 | switch (direntry->tdir_type) |
2286 | 0 | { |
2287 | 0 | case TIFF_LONG: |
2288 | 0 | { |
2289 | 0 | uint32_t *m; |
2290 | 0 | uint32_t n; |
2291 | 0 | m = (uint32_t *)origdata; |
2292 | 0 | for (n = 0; n < count; n++) |
2293 | 0 | { |
2294 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2295 | 0 | TIFFSwabLong((uint32_t *)m); |
2296 | 0 | err = TIFFReadDirEntryCheckRangeSlongLong(*m); |
2297 | 0 | if (err != TIFFReadDirEntryErrOk) |
2298 | 0 | { |
2299 | 0 | _TIFFfreeExt(tif, origdata); |
2300 | 0 | return (err); |
2301 | 0 | } |
2302 | 0 | m++; |
2303 | 0 | } |
2304 | 0 | *value = (int32_t *)origdata; |
2305 | 0 | return (TIFFReadDirEntryErrOk); |
2306 | 0 | } |
2307 | 0 | case TIFF_SLONG: |
2308 | 0 | *value = (int32_t *)origdata; |
2309 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2310 | 0 | TIFFSwabArrayOfLong((uint32_t *)(*value), count); |
2311 | 0 | return (TIFFReadDirEntryErrOk); |
2312 | 0 | } |
2313 | 0 | data = (int32_t *)_TIFFmallocExt(tif, count * 4); |
2314 | 0 | if (data == 0) |
2315 | 0 | { |
2316 | 0 | _TIFFfreeExt(tif, origdata); |
2317 | 0 | return (TIFFReadDirEntryErrAlloc); |
2318 | 0 | } |
2319 | 0 | switch (direntry->tdir_type) |
2320 | 0 | { |
2321 | 0 | case TIFF_BYTE: |
2322 | 0 | { |
2323 | 0 | uint8_t *ma; |
2324 | 0 | int32_t *mb; |
2325 | 0 | uint32_t n; |
2326 | 0 | ma = (uint8_t *)origdata; |
2327 | 0 | mb = data; |
2328 | 0 | for (n = 0; n < count; n++) |
2329 | 0 | *mb++ = (int32_t)(*ma++); |
2330 | 0 | } |
2331 | 0 | break; |
2332 | 0 | case TIFF_SBYTE: |
2333 | 0 | { |
2334 | 0 | int8_t *ma; |
2335 | 0 | int32_t *mb; |
2336 | 0 | uint32_t n; |
2337 | 0 | ma = (int8_t *)origdata; |
2338 | 0 | mb = data; |
2339 | 0 | for (n = 0; n < count; n++) |
2340 | 0 | *mb++ = (int32_t)(*ma++); |
2341 | 0 | } |
2342 | 0 | break; |
2343 | 0 | case TIFF_SHORT: |
2344 | 0 | { |
2345 | 0 | uint16_t *ma; |
2346 | 0 | int32_t *mb; |
2347 | 0 | uint32_t n; |
2348 | 0 | ma = (uint16_t *)origdata; |
2349 | 0 | mb = data; |
2350 | 0 | for (n = 0; n < count; n++) |
2351 | 0 | { |
2352 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2353 | 0 | TIFFSwabShort(ma); |
2354 | 0 | *mb++ = (int32_t)(*ma++); |
2355 | 0 | } |
2356 | 0 | } |
2357 | 0 | break; |
2358 | 0 | case TIFF_SSHORT: |
2359 | 0 | { |
2360 | 0 | int16_t *ma; |
2361 | 0 | int32_t *mb; |
2362 | 0 | uint32_t n; |
2363 | 0 | ma = (int16_t *)origdata; |
2364 | 0 | mb = data; |
2365 | 0 | for (n = 0; n < count; n++) |
2366 | 0 | { |
2367 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2368 | 0 | TIFFSwabShort((uint16_t *)ma); |
2369 | 0 | *mb++ = (int32_t)(*ma++); |
2370 | 0 | } |
2371 | 0 | } |
2372 | 0 | break; |
2373 | 0 | case TIFF_LONG8: |
2374 | 0 | { |
2375 | 0 | uint64_t *ma; |
2376 | 0 | int32_t *mb; |
2377 | 0 | uint32_t n; |
2378 | 0 | ma = (uint64_t *)origdata; |
2379 | 0 | mb = data; |
2380 | 0 | for (n = 0; n < count; n++) |
2381 | 0 | { |
2382 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2383 | 0 | TIFFSwabLong8(ma); |
2384 | 0 | err = TIFFReadDirEntryCheckRangeSlongLong8(*ma); |
2385 | 0 | if (err != TIFFReadDirEntryErrOk) |
2386 | 0 | break; |
2387 | 0 | *mb++ = (int32_t)(*ma++); |
2388 | 0 | } |
2389 | 0 | } |
2390 | 0 | break; |
2391 | 0 | case TIFF_SLONG8: |
2392 | 0 | { |
2393 | 0 | int64_t *ma; |
2394 | 0 | int32_t *mb; |
2395 | 0 | uint32_t n; |
2396 | 0 | ma = (int64_t *)origdata; |
2397 | 0 | mb = data; |
2398 | 0 | for (n = 0; n < count; n++) |
2399 | 0 | { |
2400 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2401 | 0 | TIFFSwabLong8((uint64_t *)ma); |
2402 | 0 | err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma); |
2403 | 0 | if (err != TIFFReadDirEntryErrOk) |
2404 | 0 | break; |
2405 | 0 | *mb++ = (int32_t)(*ma++); |
2406 | 0 | } |
2407 | 0 | } |
2408 | 0 | break; |
2409 | 0 | } |
2410 | 0 | _TIFFfreeExt(tif, origdata); |
2411 | 0 | if (err != TIFFReadDirEntryErrOk) |
2412 | 0 | { |
2413 | 0 | _TIFFfreeExt(tif, data); |
2414 | 0 | return (err); |
2415 | 0 | } |
2416 | 0 | *value = data; |
2417 | 0 | return (TIFFReadDirEntryErrOk); |
2418 | 0 | } |
2419 | | |
2420 | | static enum TIFFReadDirEntryErr |
2421 | | TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry, |
2422 | | uint64_t **value, uint64_t maxcount) |
2423 | 0 | { |
2424 | 0 | enum TIFFReadDirEntryErr err; |
2425 | 0 | uint32_t count; |
2426 | 0 | void *origdata; |
2427 | 0 | uint64_t *data; |
2428 | 0 | switch (direntry->tdir_type) |
2429 | 0 | { |
2430 | 0 | case TIFF_BYTE: |
2431 | 0 | case TIFF_SBYTE: |
2432 | 0 | case TIFF_SHORT: |
2433 | 0 | case TIFF_SSHORT: |
2434 | 0 | case TIFF_LONG: |
2435 | 0 | case TIFF_SLONG: |
2436 | 0 | case TIFF_LONG8: |
2437 | 0 | case TIFF_SLONG8: |
2438 | 0 | break; |
2439 | 0 | default: |
2440 | 0 | return (TIFFReadDirEntryErrType); |
2441 | 0 | } |
2442 | 0 | err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata, |
2443 | 0 | maxcount); |
2444 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
2445 | 0 | { |
2446 | 0 | *value = 0; |
2447 | 0 | return (err); |
2448 | 0 | } |
2449 | 0 | switch (direntry->tdir_type) |
2450 | 0 | { |
2451 | 0 | case TIFF_LONG8: |
2452 | 0 | *value = (uint64_t *)origdata; |
2453 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2454 | 0 | TIFFSwabArrayOfLong8(*value, count); |
2455 | 0 | return (TIFFReadDirEntryErrOk); |
2456 | 0 | case TIFF_SLONG8: |
2457 | 0 | { |
2458 | 0 | int64_t *m; |
2459 | 0 | uint32_t n; |
2460 | 0 | m = (int64_t *)origdata; |
2461 | 0 | for (n = 0; n < count; n++) |
2462 | 0 | { |
2463 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2464 | 0 | TIFFSwabLong8((uint64_t *)m); |
2465 | 0 | err = TIFFReadDirEntryCheckRangeLong8Slong8(*m); |
2466 | 0 | if (err != TIFFReadDirEntryErrOk) |
2467 | 0 | { |
2468 | 0 | _TIFFfreeExt(tif, origdata); |
2469 | 0 | return (err); |
2470 | 0 | } |
2471 | 0 | m++; |
2472 | 0 | } |
2473 | 0 | *value = (uint64_t *)origdata; |
2474 | 0 | return (TIFFReadDirEntryErrOk); |
2475 | 0 | } |
2476 | 0 | } |
2477 | 0 | data = (uint64_t *)_TIFFmallocExt(tif, count * 8); |
2478 | 0 | if (data == 0) |
2479 | 0 | { |
2480 | 0 | _TIFFfreeExt(tif, origdata); |
2481 | 0 | return (TIFFReadDirEntryErrAlloc); |
2482 | 0 | } |
2483 | 0 | switch (direntry->tdir_type) |
2484 | 0 | { |
2485 | 0 | case TIFF_BYTE: |
2486 | 0 | { |
2487 | 0 | uint8_t *ma; |
2488 | 0 | uint64_t *mb; |
2489 | 0 | uint32_t n; |
2490 | 0 | ma = (uint8_t *)origdata; |
2491 | 0 | mb = data; |
2492 | 0 | for (n = 0; n < count; n++) |
2493 | 0 | *mb++ = (uint64_t)(*ma++); |
2494 | 0 | } |
2495 | 0 | break; |
2496 | 0 | case TIFF_SBYTE: |
2497 | 0 | { |
2498 | 0 | int8_t *ma; |
2499 | 0 | uint64_t *mb; |
2500 | 0 | uint32_t n; |
2501 | 0 | ma = (int8_t *)origdata; |
2502 | 0 | mb = data; |
2503 | 0 | for (n = 0; n < count; n++) |
2504 | 0 | { |
2505 | 0 | err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma); |
2506 | 0 | if (err != TIFFReadDirEntryErrOk) |
2507 | 0 | break; |
2508 | 0 | *mb++ = (uint64_t)(*ma++); |
2509 | 0 | } |
2510 | 0 | } |
2511 | 0 | break; |
2512 | 0 | case TIFF_SHORT: |
2513 | 0 | { |
2514 | 0 | uint16_t *ma; |
2515 | 0 | uint64_t *mb; |
2516 | 0 | uint32_t n; |
2517 | 0 | ma = (uint16_t *)origdata; |
2518 | 0 | mb = data; |
2519 | 0 | for (n = 0; n < count; n++) |
2520 | 0 | { |
2521 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2522 | 0 | TIFFSwabShort(ma); |
2523 | 0 | *mb++ = (uint64_t)(*ma++); |
2524 | 0 | } |
2525 | 0 | } |
2526 | 0 | break; |
2527 | 0 | case TIFF_SSHORT: |
2528 | 0 | { |
2529 | 0 | int16_t *ma; |
2530 | 0 | uint64_t *mb; |
2531 | 0 | uint32_t n; |
2532 | 0 | ma = (int16_t *)origdata; |
2533 | 0 | mb = data; |
2534 | 0 | for (n = 0; n < count; n++) |
2535 | 0 | { |
2536 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2537 | 0 | TIFFSwabShort((uint16_t *)ma); |
2538 | 0 | err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma); |
2539 | 0 | if (err != TIFFReadDirEntryErrOk) |
2540 | 0 | break; |
2541 | 0 | *mb++ = (uint64_t)(*ma++); |
2542 | 0 | } |
2543 | 0 | } |
2544 | 0 | break; |
2545 | 0 | case TIFF_LONG: |
2546 | 0 | { |
2547 | 0 | uint32_t *ma; |
2548 | 0 | uint64_t *mb; |
2549 | 0 | uint32_t n; |
2550 | 0 | ma = (uint32_t *)origdata; |
2551 | 0 | mb = data; |
2552 | 0 | for (n = 0; n < count; n++) |
2553 | 0 | { |
2554 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2555 | 0 | TIFFSwabLong(ma); |
2556 | 0 | *mb++ = (uint64_t)(*ma++); |
2557 | 0 | } |
2558 | 0 | } |
2559 | 0 | break; |
2560 | 0 | case TIFF_SLONG: |
2561 | 0 | { |
2562 | 0 | int32_t *ma; |
2563 | 0 | uint64_t *mb; |
2564 | 0 | uint32_t n; |
2565 | 0 | ma = (int32_t *)origdata; |
2566 | 0 | mb = data; |
2567 | 0 | for (n = 0; n < count; n++) |
2568 | 0 | { |
2569 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2570 | 0 | TIFFSwabLong((uint32_t *)ma); |
2571 | 0 | err = TIFFReadDirEntryCheckRangeLong8Slong(*ma); |
2572 | 0 | if (err != TIFFReadDirEntryErrOk) |
2573 | 0 | break; |
2574 | 0 | *mb++ = (uint64_t)(*ma++); |
2575 | 0 | } |
2576 | 0 | } |
2577 | 0 | break; |
2578 | 0 | } |
2579 | 0 | _TIFFfreeExt(tif, origdata); |
2580 | 0 | if (err != TIFFReadDirEntryErrOk) |
2581 | 0 | { |
2582 | 0 | _TIFFfreeExt(tif, data); |
2583 | 0 | return (err); |
2584 | 0 | } |
2585 | 0 | *value = data; |
2586 | 0 | return (TIFFReadDirEntryErrOk); |
2587 | 0 | } |
2588 | | |
2589 | | static enum TIFFReadDirEntryErr |
2590 | | TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value) |
2591 | 0 | { |
2592 | 0 | return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value, |
2593 | 0 | ~((uint64_t)0)); |
2594 | 0 | } |
2595 | | |
2596 | | static enum TIFFReadDirEntryErr |
2597 | | TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value) |
2598 | 0 | { |
2599 | 0 | enum TIFFReadDirEntryErr err; |
2600 | 0 | uint32_t count; |
2601 | 0 | void *origdata; |
2602 | 0 | int64_t *data; |
2603 | 0 | switch (direntry->tdir_type) |
2604 | 0 | { |
2605 | 0 | case TIFF_BYTE: |
2606 | 0 | case TIFF_SBYTE: |
2607 | 0 | case TIFF_SHORT: |
2608 | 0 | case TIFF_SSHORT: |
2609 | 0 | case TIFF_LONG: |
2610 | 0 | case TIFF_SLONG: |
2611 | 0 | case TIFF_LONG8: |
2612 | 0 | case TIFF_SLONG8: |
2613 | 0 | break; |
2614 | 0 | default: |
2615 | 0 | return (TIFFReadDirEntryErrType); |
2616 | 0 | } |
2617 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); |
2618 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
2619 | 0 | { |
2620 | 0 | *value = 0; |
2621 | 0 | return (err); |
2622 | 0 | } |
2623 | 0 | switch (direntry->tdir_type) |
2624 | 0 | { |
2625 | 0 | case TIFF_LONG8: |
2626 | 0 | { |
2627 | 0 | uint64_t *m; |
2628 | 0 | uint32_t n; |
2629 | 0 | m = (uint64_t *)origdata; |
2630 | 0 | for (n = 0; n < count; n++) |
2631 | 0 | { |
2632 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2633 | 0 | TIFFSwabLong8(m); |
2634 | 0 | err = TIFFReadDirEntryCheckRangeSlong8Long8(*m); |
2635 | 0 | if (err != TIFFReadDirEntryErrOk) |
2636 | 0 | { |
2637 | 0 | _TIFFfreeExt(tif, origdata); |
2638 | 0 | return (err); |
2639 | 0 | } |
2640 | 0 | m++; |
2641 | 0 | } |
2642 | 0 | *value = (int64_t *)origdata; |
2643 | 0 | return (TIFFReadDirEntryErrOk); |
2644 | 0 | } |
2645 | 0 | case TIFF_SLONG8: |
2646 | 0 | *value = (int64_t *)origdata; |
2647 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2648 | 0 | TIFFSwabArrayOfLong8((uint64_t *)(*value), count); |
2649 | 0 | return (TIFFReadDirEntryErrOk); |
2650 | 0 | } |
2651 | 0 | data = (int64_t *)_TIFFmallocExt(tif, count * 8); |
2652 | 0 | if (data == 0) |
2653 | 0 | { |
2654 | 0 | _TIFFfreeExt(tif, origdata); |
2655 | 0 | return (TIFFReadDirEntryErrAlloc); |
2656 | 0 | } |
2657 | 0 | switch (direntry->tdir_type) |
2658 | 0 | { |
2659 | 0 | case TIFF_BYTE: |
2660 | 0 | { |
2661 | 0 | uint8_t *ma; |
2662 | 0 | int64_t *mb; |
2663 | 0 | uint32_t n; |
2664 | 0 | ma = (uint8_t *)origdata; |
2665 | 0 | mb = data; |
2666 | 0 | for (n = 0; n < count; n++) |
2667 | 0 | *mb++ = (int64_t)(*ma++); |
2668 | 0 | } |
2669 | 0 | break; |
2670 | 0 | case TIFF_SBYTE: |
2671 | 0 | { |
2672 | 0 | int8_t *ma; |
2673 | 0 | int64_t *mb; |
2674 | 0 | uint32_t n; |
2675 | 0 | ma = (int8_t *)origdata; |
2676 | 0 | mb = data; |
2677 | 0 | for (n = 0; n < count; n++) |
2678 | 0 | *mb++ = (int64_t)(*ma++); |
2679 | 0 | } |
2680 | 0 | break; |
2681 | 0 | case TIFF_SHORT: |
2682 | 0 | { |
2683 | 0 | uint16_t *ma; |
2684 | 0 | int64_t *mb; |
2685 | 0 | uint32_t n; |
2686 | 0 | ma = (uint16_t *)origdata; |
2687 | 0 | mb = data; |
2688 | 0 | for (n = 0; n < count; n++) |
2689 | 0 | { |
2690 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2691 | 0 | TIFFSwabShort(ma); |
2692 | 0 | *mb++ = (int64_t)(*ma++); |
2693 | 0 | } |
2694 | 0 | } |
2695 | 0 | break; |
2696 | 0 | case TIFF_SSHORT: |
2697 | 0 | { |
2698 | 0 | int16_t *ma; |
2699 | 0 | int64_t *mb; |
2700 | 0 | uint32_t n; |
2701 | 0 | ma = (int16_t *)origdata; |
2702 | 0 | mb = data; |
2703 | 0 | for (n = 0; n < count; n++) |
2704 | 0 | { |
2705 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2706 | 0 | TIFFSwabShort((uint16_t *)ma); |
2707 | 0 | *mb++ = (int64_t)(*ma++); |
2708 | 0 | } |
2709 | 0 | } |
2710 | 0 | break; |
2711 | 0 | case TIFF_LONG: |
2712 | 0 | { |
2713 | 0 | uint32_t *ma; |
2714 | 0 | int64_t *mb; |
2715 | 0 | uint32_t n; |
2716 | 0 | ma = (uint32_t *)origdata; |
2717 | 0 | mb = data; |
2718 | 0 | for (n = 0; n < count; n++) |
2719 | 0 | { |
2720 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2721 | 0 | TIFFSwabLong(ma); |
2722 | 0 | *mb++ = (int64_t)(*ma++); |
2723 | 0 | } |
2724 | 0 | } |
2725 | 0 | break; |
2726 | 0 | case TIFF_SLONG: |
2727 | 0 | { |
2728 | 0 | int32_t *ma; |
2729 | 0 | int64_t *mb; |
2730 | 0 | uint32_t n; |
2731 | 0 | ma = (int32_t *)origdata; |
2732 | 0 | mb = data; |
2733 | 0 | for (n = 0; n < count; n++) |
2734 | 0 | { |
2735 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2736 | 0 | TIFFSwabLong((uint32_t *)ma); |
2737 | 0 | *mb++ = (int64_t)(*ma++); |
2738 | 0 | } |
2739 | 0 | } |
2740 | 0 | break; |
2741 | 0 | } |
2742 | 0 | _TIFFfreeExt(tif, origdata); |
2743 | 0 | *value = data; |
2744 | 0 | return (TIFFReadDirEntryErrOk); |
2745 | 0 | } |
2746 | | |
2747 | | static enum TIFFReadDirEntryErr |
2748 | | TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value) |
2749 | 0 | { |
2750 | 0 | enum TIFFReadDirEntryErr err; |
2751 | 0 | uint32_t count; |
2752 | 0 | void *origdata; |
2753 | 0 | float *data; |
2754 | 0 | switch (direntry->tdir_type) |
2755 | 0 | { |
2756 | 0 | case TIFF_BYTE: |
2757 | 0 | case TIFF_SBYTE: |
2758 | 0 | case TIFF_SHORT: |
2759 | 0 | case TIFF_SSHORT: |
2760 | 0 | case TIFF_LONG: |
2761 | 0 | case TIFF_SLONG: |
2762 | 0 | case TIFF_LONG8: |
2763 | 0 | case TIFF_SLONG8: |
2764 | 0 | case TIFF_RATIONAL: |
2765 | 0 | case TIFF_SRATIONAL: |
2766 | 0 | case TIFF_FLOAT: |
2767 | 0 | case TIFF_DOUBLE: |
2768 | 0 | break; |
2769 | 0 | default: |
2770 | 0 | return (TIFFReadDirEntryErrType); |
2771 | 0 | } |
2772 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata); |
2773 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
2774 | 0 | { |
2775 | 0 | *value = 0; |
2776 | 0 | return (err); |
2777 | 0 | } |
2778 | 0 | switch (direntry->tdir_type) |
2779 | 0 | { |
2780 | 0 | case TIFF_FLOAT: |
2781 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2782 | 0 | TIFFSwabArrayOfLong((uint32_t *)origdata, count); |
2783 | 0 | TIFFCvtIEEEDoubleToNative(tif, count, (float *)origdata); |
2784 | 0 | *value = (float *)origdata; |
2785 | 0 | return (TIFFReadDirEntryErrOk); |
2786 | 0 | } |
2787 | 0 | data = (float *)_TIFFmallocExt(tif, count * sizeof(float)); |
2788 | 0 | if (data == 0) |
2789 | 0 | { |
2790 | 0 | _TIFFfreeExt(tif, origdata); |
2791 | 0 | return (TIFFReadDirEntryErrAlloc); |
2792 | 0 | } |
2793 | 0 | switch (direntry->tdir_type) |
2794 | 0 | { |
2795 | 0 | case TIFF_BYTE: |
2796 | 0 | { |
2797 | 0 | uint8_t *ma; |
2798 | 0 | float *mb; |
2799 | 0 | uint32_t n; |
2800 | 0 | ma = (uint8_t *)origdata; |
2801 | 0 | mb = data; |
2802 | 0 | for (n = 0; n < count; n++) |
2803 | 0 | *mb++ = (float)(*ma++); |
2804 | 0 | } |
2805 | 0 | break; |
2806 | 0 | case TIFF_SBYTE: |
2807 | 0 | { |
2808 | 0 | int8_t *ma; |
2809 | 0 | float *mb; |
2810 | 0 | uint32_t n; |
2811 | 0 | ma = (int8_t *)origdata; |
2812 | 0 | mb = data; |
2813 | 0 | for (n = 0; n < count; n++) |
2814 | 0 | *mb++ = (float)(*ma++); |
2815 | 0 | } |
2816 | 0 | break; |
2817 | 0 | case TIFF_SHORT: |
2818 | 0 | { |
2819 | 0 | uint16_t *ma; |
2820 | 0 | float *mb; |
2821 | 0 | uint32_t n; |
2822 | 0 | ma = (uint16_t *)origdata; |
2823 | 0 | mb = data; |
2824 | 0 | for (n = 0; n < count; n++) |
2825 | 0 | { |
2826 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2827 | 0 | TIFFSwabShort(ma); |
2828 | 0 | *mb++ = (float)(*ma++); |
2829 | 0 | } |
2830 | 0 | } |
2831 | 0 | break; |
2832 | 0 | case TIFF_SSHORT: |
2833 | 0 | { |
2834 | 0 | int16_t *ma; |
2835 | 0 | float *mb; |
2836 | 0 | uint32_t n; |
2837 | 0 | ma = (int16_t *)origdata; |
2838 | 0 | mb = data; |
2839 | 0 | for (n = 0; n < count; n++) |
2840 | 0 | { |
2841 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2842 | 0 | TIFFSwabShort((uint16_t *)ma); |
2843 | 0 | *mb++ = (float)(*ma++); |
2844 | 0 | } |
2845 | 0 | } |
2846 | 0 | break; |
2847 | 0 | case TIFF_LONG: |
2848 | 0 | { |
2849 | 0 | uint32_t *ma; |
2850 | 0 | float *mb; |
2851 | 0 | uint32_t n; |
2852 | 0 | ma = (uint32_t *)origdata; |
2853 | 0 | mb = data; |
2854 | 0 | for (n = 0; n < count; n++) |
2855 | 0 | { |
2856 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2857 | 0 | TIFFSwabLong(ma); |
2858 | 0 | *mb++ = (float)(*ma++); |
2859 | 0 | } |
2860 | 0 | } |
2861 | 0 | break; |
2862 | 0 | case TIFF_SLONG: |
2863 | 0 | { |
2864 | 0 | int32_t *ma; |
2865 | 0 | float *mb; |
2866 | 0 | uint32_t n; |
2867 | 0 | ma = (int32_t *)origdata; |
2868 | 0 | mb = data; |
2869 | 0 | for (n = 0; n < count; n++) |
2870 | 0 | { |
2871 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2872 | 0 | TIFFSwabLong((uint32_t *)ma); |
2873 | 0 | *mb++ = (float)(*ma++); |
2874 | 0 | } |
2875 | 0 | } |
2876 | 0 | break; |
2877 | 0 | case TIFF_LONG8: |
2878 | 0 | { |
2879 | 0 | uint64_t *ma; |
2880 | 0 | float *mb; |
2881 | 0 | uint32_t n; |
2882 | 0 | ma = (uint64_t *)origdata; |
2883 | 0 | mb = data; |
2884 | 0 | for (n = 0; n < count; n++) |
2885 | 0 | { |
2886 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2887 | 0 | TIFFSwabLong8(ma); |
2888 | | #if defined(__WIN32__) && (_MSC_VER < 1500) |
2889 | | /* |
2890 | | * XXX: MSVC 6.0 does not support |
2891 | | * conversion of 64-bit integers into |
2892 | | * floating point values. |
2893 | | */ |
2894 | | *mb++ = _TIFFUInt64ToFloat(*ma++); |
2895 | | #else |
2896 | 0 | *mb++ = (float)(*ma++); |
2897 | 0 | #endif |
2898 | 0 | } |
2899 | 0 | } |
2900 | 0 | break; |
2901 | 0 | case TIFF_SLONG8: |
2902 | 0 | { |
2903 | 0 | int64_t *ma; |
2904 | 0 | float *mb; |
2905 | 0 | uint32_t n; |
2906 | 0 | ma = (int64_t *)origdata; |
2907 | 0 | mb = data; |
2908 | 0 | for (n = 0; n < count; n++) |
2909 | 0 | { |
2910 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2911 | 0 | TIFFSwabLong8((uint64_t *)ma); |
2912 | 0 | *mb++ = (float)(*ma++); |
2913 | 0 | } |
2914 | 0 | } |
2915 | 0 | break; |
2916 | 0 | case TIFF_RATIONAL: |
2917 | 0 | { |
2918 | 0 | uint32_t *ma; |
2919 | 0 | uint32_t maa; |
2920 | 0 | uint32_t mab; |
2921 | 0 | float *mb; |
2922 | 0 | uint32_t n; |
2923 | 0 | ma = (uint32_t *)origdata; |
2924 | 0 | mb = data; |
2925 | 0 | for (n = 0; n < count; n++) |
2926 | 0 | { |
2927 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2928 | 0 | TIFFSwabLong(ma); |
2929 | 0 | maa = *ma++; |
2930 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2931 | 0 | TIFFSwabLong(ma); |
2932 | 0 | mab = *ma++; |
2933 | 0 | if (mab == 0) |
2934 | 0 | *mb++ = 0.0; |
2935 | 0 | else |
2936 | 0 | *mb++ = (float)maa / (float)mab; |
2937 | 0 | } |
2938 | 0 | } |
2939 | 0 | break; |
2940 | 0 | case TIFF_SRATIONAL: |
2941 | 0 | { |
2942 | 0 | uint32_t *ma; |
2943 | 0 | int32_t maa; |
2944 | 0 | uint32_t mab; |
2945 | 0 | float *mb; |
2946 | 0 | uint32_t n; |
2947 | 0 | ma = (uint32_t *)origdata; |
2948 | 0 | mb = data; |
2949 | 0 | for (n = 0; n < count; n++) |
2950 | 0 | { |
2951 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2952 | 0 | TIFFSwabLong(ma); |
2953 | 0 | maa = *(int32_t *)ma; |
2954 | 0 | ma++; |
2955 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2956 | 0 | TIFFSwabLong(ma); |
2957 | 0 | mab = *ma++; |
2958 | 0 | if (mab == 0) |
2959 | 0 | *mb++ = 0.0; |
2960 | 0 | else |
2961 | 0 | *mb++ = (float)maa / (float)mab; |
2962 | 0 | } |
2963 | 0 | } |
2964 | 0 | break; |
2965 | 0 | case TIFF_DOUBLE: |
2966 | 0 | { |
2967 | 0 | double *ma; |
2968 | 0 | float *mb; |
2969 | 0 | uint32_t n; |
2970 | 0 | if (tif->tif_flags & TIFF_SWAB) |
2971 | 0 | TIFFSwabArrayOfLong8((uint64_t *)origdata, count); |
2972 | 0 | TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata); |
2973 | 0 | ma = (double *)origdata; |
2974 | 0 | mb = data; |
2975 | 0 | for (n = 0; n < count; n++) |
2976 | 0 | { |
2977 | 0 | double val = *ma++; |
2978 | 0 | if (val > FLT_MAX) |
2979 | 0 | val = FLT_MAX; |
2980 | 0 | else if (val < -FLT_MAX) |
2981 | 0 | val = -FLT_MAX; |
2982 | 0 | *mb++ = (float)val; |
2983 | 0 | } |
2984 | 0 | } |
2985 | 0 | break; |
2986 | 0 | } |
2987 | 0 | _TIFFfreeExt(tif, origdata); |
2988 | 0 | *value = data; |
2989 | 0 | return (TIFFReadDirEntryErrOk); |
2990 | 0 | } |
2991 | | |
2992 | | static enum TIFFReadDirEntryErr |
2993 | | TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value) |
2994 | 0 | { |
2995 | 0 | enum TIFFReadDirEntryErr err; |
2996 | 0 | uint32_t count; |
2997 | 0 | void *origdata; |
2998 | 0 | double *data; |
2999 | 0 | switch (direntry->tdir_type) |
3000 | 0 | { |
3001 | 0 | case TIFF_BYTE: |
3002 | 0 | case TIFF_SBYTE: |
3003 | 0 | case TIFF_SHORT: |
3004 | 0 | case TIFF_SSHORT: |
3005 | 0 | case TIFF_LONG: |
3006 | 0 | case TIFF_SLONG: |
3007 | 0 | case TIFF_LONG8: |
3008 | 0 | case TIFF_SLONG8: |
3009 | 0 | case TIFF_RATIONAL: |
3010 | 0 | case TIFF_SRATIONAL: |
3011 | 0 | case TIFF_FLOAT: |
3012 | 0 | case TIFF_DOUBLE: |
3013 | 0 | break; |
3014 | 0 | default: |
3015 | 0 | return (TIFFReadDirEntryErrType); |
3016 | 0 | } |
3017 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); |
3018 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
3019 | 0 | { |
3020 | 0 | *value = 0; |
3021 | 0 | return (err); |
3022 | 0 | } |
3023 | 0 | switch (direntry->tdir_type) |
3024 | 0 | { |
3025 | 0 | case TIFF_DOUBLE: |
3026 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3027 | 0 | TIFFSwabArrayOfLong8((uint64_t *)origdata, count); |
3028 | 0 | TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata); |
3029 | 0 | *value = (double *)origdata; |
3030 | 0 | return (TIFFReadDirEntryErrOk); |
3031 | 0 | } |
3032 | 0 | data = (double *)_TIFFmallocExt(tif, count * sizeof(double)); |
3033 | 0 | if (data == 0) |
3034 | 0 | { |
3035 | 0 | _TIFFfreeExt(tif, origdata); |
3036 | 0 | return (TIFFReadDirEntryErrAlloc); |
3037 | 0 | } |
3038 | 0 | switch (direntry->tdir_type) |
3039 | 0 | { |
3040 | 0 | case TIFF_BYTE: |
3041 | 0 | { |
3042 | 0 | uint8_t *ma; |
3043 | 0 | double *mb; |
3044 | 0 | uint32_t n; |
3045 | 0 | ma = (uint8_t *)origdata; |
3046 | 0 | mb = data; |
3047 | 0 | for (n = 0; n < count; n++) |
3048 | 0 | *mb++ = (double)(*ma++); |
3049 | 0 | } |
3050 | 0 | break; |
3051 | 0 | case TIFF_SBYTE: |
3052 | 0 | { |
3053 | 0 | int8_t *ma; |
3054 | 0 | double *mb; |
3055 | 0 | uint32_t n; |
3056 | 0 | ma = (int8_t *)origdata; |
3057 | 0 | mb = data; |
3058 | 0 | for (n = 0; n < count; n++) |
3059 | 0 | *mb++ = (double)(*ma++); |
3060 | 0 | } |
3061 | 0 | break; |
3062 | 0 | case TIFF_SHORT: |
3063 | 0 | { |
3064 | 0 | uint16_t *ma; |
3065 | 0 | double *mb; |
3066 | 0 | uint32_t n; |
3067 | 0 | ma = (uint16_t *)origdata; |
3068 | 0 | mb = data; |
3069 | 0 | for (n = 0; n < count; n++) |
3070 | 0 | { |
3071 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3072 | 0 | TIFFSwabShort(ma); |
3073 | 0 | *mb++ = (double)(*ma++); |
3074 | 0 | } |
3075 | 0 | } |
3076 | 0 | break; |
3077 | 0 | case TIFF_SSHORT: |
3078 | 0 | { |
3079 | 0 | int16_t *ma; |
3080 | 0 | double *mb; |
3081 | 0 | uint32_t n; |
3082 | 0 | ma = (int16_t *)origdata; |
3083 | 0 | mb = data; |
3084 | 0 | for (n = 0; n < count; n++) |
3085 | 0 | { |
3086 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3087 | 0 | TIFFSwabShort((uint16_t *)ma); |
3088 | 0 | *mb++ = (double)(*ma++); |
3089 | 0 | } |
3090 | 0 | } |
3091 | 0 | break; |
3092 | 0 | case TIFF_LONG: |
3093 | 0 | { |
3094 | 0 | uint32_t *ma; |
3095 | 0 | double *mb; |
3096 | 0 | uint32_t n; |
3097 | 0 | ma = (uint32_t *)origdata; |
3098 | 0 | mb = data; |
3099 | 0 | for (n = 0; n < count; n++) |
3100 | 0 | { |
3101 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3102 | 0 | TIFFSwabLong(ma); |
3103 | 0 | *mb++ = (double)(*ma++); |
3104 | 0 | } |
3105 | 0 | } |
3106 | 0 | break; |
3107 | 0 | case TIFF_SLONG: |
3108 | 0 | { |
3109 | 0 | int32_t *ma; |
3110 | 0 | double *mb; |
3111 | 0 | uint32_t n; |
3112 | 0 | ma = (int32_t *)origdata; |
3113 | 0 | mb = data; |
3114 | 0 | for (n = 0; n < count; n++) |
3115 | 0 | { |
3116 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3117 | 0 | TIFFSwabLong((uint32_t *)ma); |
3118 | 0 | *mb++ = (double)(*ma++); |
3119 | 0 | } |
3120 | 0 | } |
3121 | 0 | break; |
3122 | 0 | case TIFF_LONG8: |
3123 | 0 | { |
3124 | 0 | uint64_t *ma; |
3125 | 0 | double *mb; |
3126 | 0 | uint32_t n; |
3127 | 0 | ma = (uint64_t *)origdata; |
3128 | 0 | mb = data; |
3129 | 0 | for (n = 0; n < count; n++) |
3130 | 0 | { |
3131 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3132 | 0 | TIFFSwabLong8(ma); |
3133 | | #if defined(__WIN32__) && (_MSC_VER < 1500) |
3134 | | /* |
3135 | | * XXX: MSVC 6.0 does not support |
3136 | | * conversion of 64-bit integers into |
3137 | | * floating point values. |
3138 | | */ |
3139 | | *mb++ = _TIFFUInt64ToDouble(*ma++); |
3140 | | #else |
3141 | 0 | *mb++ = (double)(*ma++); |
3142 | 0 | #endif |
3143 | 0 | } |
3144 | 0 | } |
3145 | 0 | break; |
3146 | 0 | case TIFF_SLONG8: |
3147 | 0 | { |
3148 | 0 | int64_t *ma; |
3149 | 0 | double *mb; |
3150 | 0 | uint32_t n; |
3151 | 0 | ma = (int64_t *)origdata; |
3152 | 0 | mb = data; |
3153 | 0 | for (n = 0; n < count; n++) |
3154 | 0 | { |
3155 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3156 | 0 | TIFFSwabLong8((uint64_t *)ma); |
3157 | 0 | *mb++ = (double)(*ma++); |
3158 | 0 | } |
3159 | 0 | } |
3160 | 0 | break; |
3161 | 0 | case TIFF_RATIONAL: |
3162 | 0 | { |
3163 | 0 | uint32_t *ma; |
3164 | 0 | uint32_t maa; |
3165 | 0 | uint32_t mab; |
3166 | 0 | double *mb; |
3167 | 0 | uint32_t n; |
3168 | 0 | ma = (uint32_t *)origdata; |
3169 | 0 | mb = data; |
3170 | 0 | for (n = 0; n < count; n++) |
3171 | 0 | { |
3172 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3173 | 0 | TIFFSwabLong(ma); |
3174 | 0 | maa = *ma++; |
3175 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3176 | 0 | TIFFSwabLong(ma); |
3177 | 0 | mab = *ma++; |
3178 | 0 | if (mab == 0) |
3179 | 0 | *mb++ = 0.0; |
3180 | 0 | else |
3181 | 0 | *mb++ = (double)maa / (double)mab; |
3182 | 0 | } |
3183 | 0 | } |
3184 | 0 | break; |
3185 | 0 | case TIFF_SRATIONAL: |
3186 | 0 | { |
3187 | 0 | uint32_t *ma; |
3188 | 0 | int32_t maa; |
3189 | 0 | uint32_t mab; |
3190 | 0 | double *mb; |
3191 | 0 | uint32_t n; |
3192 | 0 | ma = (uint32_t *)origdata; |
3193 | 0 | mb = data; |
3194 | 0 | for (n = 0; n < count; n++) |
3195 | 0 | { |
3196 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3197 | 0 | TIFFSwabLong(ma); |
3198 | 0 | maa = *(int32_t *)ma; |
3199 | 0 | ma++; |
3200 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3201 | 0 | TIFFSwabLong(ma); |
3202 | 0 | mab = *ma++; |
3203 | 0 | if (mab == 0) |
3204 | 0 | *mb++ = 0.0; |
3205 | 0 | else |
3206 | 0 | *mb++ = (double)maa / (double)mab; |
3207 | 0 | } |
3208 | 0 | } |
3209 | 0 | break; |
3210 | 0 | case TIFF_FLOAT: |
3211 | 0 | { |
3212 | 0 | float *ma; |
3213 | 0 | double *mb; |
3214 | 0 | uint32_t n; |
3215 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3216 | 0 | TIFFSwabArrayOfLong((uint32_t *)origdata, count); |
3217 | 0 | TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata); |
3218 | 0 | ma = (float *)origdata; |
3219 | 0 | mb = data; |
3220 | 0 | for (n = 0; n < count; n++) |
3221 | 0 | *mb++ = (double)(*ma++); |
3222 | 0 | } |
3223 | 0 | break; |
3224 | 0 | } |
3225 | 0 | _TIFFfreeExt(tif, origdata); |
3226 | 0 | *value = data; |
3227 | 0 | return (TIFFReadDirEntryErrOk); |
3228 | 0 | } |
3229 | | |
3230 | | static enum TIFFReadDirEntryErr |
3231 | | TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value) |
3232 | 0 | { |
3233 | 0 | enum TIFFReadDirEntryErr err; |
3234 | 0 | uint32_t count; |
3235 | 0 | void *origdata; |
3236 | 0 | uint64_t *data; |
3237 | 0 | switch (direntry->tdir_type) |
3238 | 0 | { |
3239 | 0 | case TIFF_LONG: |
3240 | 0 | case TIFF_LONG8: |
3241 | 0 | case TIFF_IFD: |
3242 | 0 | case TIFF_IFD8: |
3243 | 0 | break; |
3244 | 0 | default: |
3245 | 0 | return (TIFFReadDirEntryErrType); |
3246 | 0 | } |
3247 | 0 | err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata); |
3248 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
3249 | 0 | { |
3250 | 0 | *value = 0; |
3251 | 0 | return (err); |
3252 | 0 | } |
3253 | 0 | switch (direntry->tdir_type) |
3254 | 0 | { |
3255 | 0 | case TIFF_LONG8: |
3256 | 0 | case TIFF_IFD8: |
3257 | 0 | *value = (uint64_t *)origdata; |
3258 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3259 | 0 | TIFFSwabArrayOfLong8(*value, count); |
3260 | 0 | return (TIFFReadDirEntryErrOk); |
3261 | 0 | } |
3262 | 0 | data = (uint64_t *)_TIFFmallocExt(tif, count * 8); |
3263 | 0 | if (data == 0) |
3264 | 0 | { |
3265 | 0 | _TIFFfreeExt(tif, origdata); |
3266 | 0 | return (TIFFReadDirEntryErrAlloc); |
3267 | 0 | } |
3268 | 0 | switch (direntry->tdir_type) |
3269 | 0 | { |
3270 | 0 | case TIFF_LONG: |
3271 | 0 | case TIFF_IFD: |
3272 | 0 | { |
3273 | 0 | uint32_t *ma; |
3274 | 0 | uint64_t *mb; |
3275 | 0 | uint32_t n; |
3276 | 0 | ma = (uint32_t *)origdata; |
3277 | 0 | mb = data; |
3278 | 0 | for (n = 0; n < count; n++) |
3279 | 0 | { |
3280 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3281 | 0 | TIFFSwabLong(ma); |
3282 | 0 | *mb++ = (uint64_t)(*ma++); |
3283 | 0 | } |
3284 | 0 | } |
3285 | 0 | break; |
3286 | 0 | } |
3287 | 0 | _TIFFfreeExt(tif, origdata); |
3288 | 0 | *value = data; |
3289 | 0 | return (TIFFReadDirEntryErrOk); |
3290 | 0 | } |
3291 | | |
3292 | | static enum TIFFReadDirEntryErr |
3293 | | TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry, |
3294 | | uint16_t *value) |
3295 | 0 | { |
3296 | 0 | enum TIFFReadDirEntryErr err; |
3297 | 0 | uint16_t *m; |
3298 | 0 | uint16_t *na; |
3299 | 0 | uint16_t nb; |
3300 | 0 | if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel) |
3301 | 0 | return (TIFFReadDirEntryErrCount); |
3302 | 0 | err = TIFFReadDirEntryShortArray(tif, direntry, &m); |
3303 | 0 | if (err != TIFFReadDirEntryErrOk || m == NULL) |
3304 | 0 | return (err); |
3305 | 0 | na = m; |
3306 | 0 | nb = tif->tif_dir.td_samplesperpixel; |
3307 | 0 | *value = *na++; |
3308 | 0 | nb--; |
3309 | 0 | while (nb > 0) |
3310 | 0 | { |
3311 | 0 | if (*na++ != *value) |
3312 | 0 | { |
3313 | 0 | err = TIFFReadDirEntryErrPsdif; |
3314 | 0 | break; |
3315 | 0 | } |
3316 | 0 | nb--; |
3317 | 0 | } |
3318 | 0 | _TIFFfreeExt(tif, m); |
3319 | 0 | return (err); |
3320 | 0 | } |
3321 | | |
3322 | | static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry, |
3323 | | uint8_t *value) |
3324 | 0 | { |
3325 | 0 | (void)tif; |
3326 | 0 | *value = *(uint8_t *)(&direntry->tdir_offset); |
3327 | 0 | } |
3328 | | |
3329 | | static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry, |
3330 | | int8_t *value) |
3331 | 0 | { |
3332 | 0 | (void)tif; |
3333 | 0 | *value = *(int8_t *)(&direntry->tdir_offset); |
3334 | 0 | } |
3335 | | |
3336 | | static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry, |
3337 | | uint16_t *value) |
3338 | 0 | { |
3339 | 0 | *value = direntry->tdir_offset.toff_short; |
3340 | | /* *value=*(uint16_t*)(&direntry->tdir_offset); */ |
3341 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3342 | 0 | TIFFSwabShort(value); |
3343 | 0 | } |
3344 | | |
3345 | | static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry, |
3346 | | int16_t *value) |
3347 | 0 | { |
3348 | 0 | *value = *(int16_t *)(&direntry->tdir_offset); |
3349 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3350 | 0 | TIFFSwabShort((uint16_t *)value); |
3351 | 0 | } |
3352 | | |
3353 | | static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry, |
3354 | | uint32_t *value) |
3355 | 0 | { |
3356 | 0 | *value = *(uint32_t *)(&direntry->tdir_offset); |
3357 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3358 | 0 | TIFFSwabLong(value); |
3359 | 0 | } |
3360 | | |
3361 | | static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry, |
3362 | | int32_t *value) |
3363 | 0 | { |
3364 | 0 | *value = *(int32_t *)(&direntry->tdir_offset); |
3365 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3366 | 0 | TIFFSwabLong((uint32_t *)value); |
3367 | 0 | } |
3368 | | |
3369 | | static enum TIFFReadDirEntryErr |
3370 | | TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value) |
3371 | 0 | { |
3372 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3373 | 0 | { |
3374 | 0 | enum TIFFReadDirEntryErr err; |
3375 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3376 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3377 | 0 | TIFFSwabLong(&offset); |
3378 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, value); |
3379 | 0 | if (err != TIFFReadDirEntryErrOk) |
3380 | 0 | return (err); |
3381 | 0 | } |
3382 | 0 | else |
3383 | 0 | *value = direntry->tdir_offset.toff_long8; |
3384 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3385 | 0 | TIFFSwabLong8(value); |
3386 | 0 | return (TIFFReadDirEntryErrOk); |
3387 | 0 | } |
3388 | | |
3389 | | static enum TIFFReadDirEntryErr |
3390 | | TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value) |
3391 | 0 | { |
3392 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3393 | 0 | { |
3394 | 0 | enum TIFFReadDirEntryErr err; |
3395 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3396 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3397 | 0 | TIFFSwabLong(&offset); |
3398 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, value); |
3399 | 0 | if (err != TIFFReadDirEntryErrOk) |
3400 | 0 | return (err); |
3401 | 0 | } |
3402 | 0 | else |
3403 | 0 | *value = *(int64_t *)(&direntry->tdir_offset); |
3404 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3405 | 0 | TIFFSwabLong8((uint64_t *)value); |
3406 | 0 | return (TIFFReadDirEntryErrOk); |
3407 | 0 | } |
3408 | | |
3409 | | static enum TIFFReadDirEntryErr |
3410 | | TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry, |
3411 | | double *value) |
3412 | 0 | { |
3413 | 0 | UInt64Aligned_t m; |
3414 | |
|
3415 | 0 | assert(sizeof(double) == 8); |
3416 | 0 | assert(sizeof(uint64_t) == 8); |
3417 | 0 | assert(sizeof(uint32_t) == 4); |
3418 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3419 | 0 | { |
3420 | 0 | enum TIFFReadDirEntryErr err; |
3421 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3422 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3423 | 0 | TIFFSwabLong(&offset); |
3424 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, m.i); |
3425 | 0 | if (err != TIFFReadDirEntryErrOk) |
3426 | 0 | return (err); |
3427 | 0 | } |
3428 | 0 | else |
3429 | 0 | m.l = direntry->tdir_offset.toff_long8; |
3430 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3431 | 0 | TIFFSwabArrayOfLong(m.i, 2); |
3432 | | /* Not completely sure what we should do when m.i[1]==0, but some */ |
3433 | | /* sanitizers do not like division by 0.0: */ |
3434 | | /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */ |
3435 | 0 | if (m.i[0] == 0 || m.i[1] == 0) |
3436 | 0 | *value = 0.0; |
3437 | 0 | else |
3438 | 0 | *value = (double)m.i[0] / (double)m.i[1]; |
3439 | 0 | return (TIFFReadDirEntryErrOk); |
3440 | 0 | } |
3441 | | |
3442 | | static enum TIFFReadDirEntryErr |
3443 | | TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry, |
3444 | | double *value) |
3445 | 0 | { |
3446 | 0 | UInt64Aligned_t m; |
3447 | 0 | assert(sizeof(double) == 8); |
3448 | 0 | assert(sizeof(uint64_t) == 8); |
3449 | 0 | assert(sizeof(int32_t) == 4); |
3450 | 0 | assert(sizeof(uint32_t) == 4); |
3451 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3452 | 0 | { |
3453 | 0 | enum TIFFReadDirEntryErr err; |
3454 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3455 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3456 | 0 | TIFFSwabLong(&offset); |
3457 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, m.i); |
3458 | 0 | if (err != TIFFReadDirEntryErrOk) |
3459 | 0 | return (err); |
3460 | 0 | } |
3461 | 0 | else |
3462 | 0 | m.l = direntry->tdir_offset.toff_long8; |
3463 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3464 | 0 | TIFFSwabArrayOfLong(m.i, 2); |
3465 | | /* Not completely sure what we should do when m.i[1]==0, but some */ |
3466 | | /* sanitizers do not like division by 0.0: */ |
3467 | | /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */ |
3468 | 0 | if ((int32_t)m.i[0] == 0 || m.i[1] == 0) |
3469 | 0 | *value = 0.0; |
3470 | 0 | else |
3471 | 0 | *value = (double)((int32_t)m.i[0]) / (double)m.i[1]; |
3472 | 0 | return (TIFFReadDirEntryErrOk); |
3473 | 0 | } |
3474 | | |
3475 | | static enum TIFFReadDirEntryErr |
3476 | | TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry, |
3477 | | TIFFRational_t *value) |
3478 | 0 | { /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals) |
3479 | | directly --*/ |
3480 | 0 | UInt64Aligned_t m; |
3481 | |
|
3482 | 0 | assert(sizeof(double) == 8); |
3483 | 0 | assert(sizeof(uint64_t) == 8); |
3484 | 0 | assert(sizeof(uint32_t) == 4); |
3485 | | |
3486 | 0 | if (direntry->tdir_count != 1) |
3487 | 0 | return (TIFFReadDirEntryErrCount); |
3488 | | |
3489 | 0 | if (direntry->tdir_type != TIFF_RATIONAL && |
3490 | 0 | direntry->tdir_type != TIFF_SRATIONAL) |
3491 | 0 | return (TIFFReadDirEntryErrType); |
3492 | | |
3493 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3494 | 0 | { |
3495 | 0 | enum TIFFReadDirEntryErr err; |
3496 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3497 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3498 | 0 | TIFFSwabLong(&offset); |
3499 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, m.i); |
3500 | 0 | if (err != TIFFReadDirEntryErrOk) |
3501 | 0 | return (err); |
3502 | 0 | } |
3503 | 0 | else |
3504 | 0 | { |
3505 | 0 | m.l = direntry->tdir_offset.toff_long8; |
3506 | 0 | } |
3507 | | |
3508 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3509 | 0 | TIFFSwabArrayOfLong(m.i, 2); |
3510 | |
|
3511 | 0 | value->uNum = m.i[0]; |
3512 | 0 | value->uDenom = m.i[1]; |
3513 | 0 | return (TIFFReadDirEntryErrOk); |
3514 | 0 | } /*-- TIFFReadDirEntryCheckedRationalDirect() --*/ |
3515 | | |
3516 | | static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry, |
3517 | | float *value) |
3518 | 0 | { |
3519 | 0 | union |
3520 | 0 | { |
3521 | 0 | float f; |
3522 | 0 | uint32_t i; |
3523 | 0 | } float_union; |
3524 | 0 | assert(sizeof(float) == 4); |
3525 | 0 | assert(sizeof(uint32_t) == 4); |
3526 | 0 | assert(sizeof(float_union) == 4); |
3527 | 0 | float_union.i = *(uint32_t *)(&direntry->tdir_offset); |
3528 | 0 | *value = float_union.f; |
3529 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3530 | 0 | TIFFSwabLong((uint32_t *)value); |
3531 | 0 | } |
3532 | | |
3533 | | static enum TIFFReadDirEntryErr |
3534 | | TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value) |
3535 | 0 | { |
3536 | 0 | assert(sizeof(double) == 8); |
3537 | 0 | assert(sizeof(uint64_t) == 8); |
3538 | 0 | assert(sizeof(UInt64Aligned_t) == 8); |
3539 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
3540 | 0 | { |
3541 | 0 | enum TIFFReadDirEntryErr err; |
3542 | 0 | uint32_t offset = direntry->tdir_offset.toff_long; |
3543 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3544 | 0 | TIFFSwabLong(&offset); |
3545 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, value); |
3546 | 0 | if (err != TIFFReadDirEntryErrOk) |
3547 | 0 | return (err); |
3548 | 0 | } |
3549 | 0 | else |
3550 | 0 | { |
3551 | 0 | UInt64Aligned_t uint64_union; |
3552 | 0 | uint64_union.l = direntry->tdir_offset.toff_long8; |
3553 | 0 | *value = uint64_union.d; |
3554 | 0 | } |
3555 | 0 | if (tif->tif_flags & TIFF_SWAB) |
3556 | 0 | TIFFSwabLong8((uint64_t *)value); |
3557 | 0 | return (TIFFReadDirEntryErrOk); |
3558 | 0 | } |
3559 | | |
3560 | | static enum TIFFReadDirEntryErr |
3561 | | TIFFReadDirEntryCheckRangeByteSbyte(int8_t value) |
3562 | 0 | { |
3563 | 0 | if (value < 0) |
3564 | 0 | return (TIFFReadDirEntryErrRange); |
3565 | 0 | else |
3566 | 0 | return (TIFFReadDirEntryErrOk); |
3567 | 0 | } |
3568 | | |
3569 | | static enum TIFFReadDirEntryErr |
3570 | | TIFFReadDirEntryCheckRangeByteShort(uint16_t value) |
3571 | 0 | { |
3572 | 0 | if (value > 0xFF) |
3573 | 0 | return (TIFFReadDirEntryErrRange); |
3574 | 0 | else |
3575 | 0 | return (TIFFReadDirEntryErrOk); |
3576 | 0 | } |
3577 | | |
3578 | | static enum TIFFReadDirEntryErr |
3579 | | TIFFReadDirEntryCheckRangeByteSshort(int16_t value) |
3580 | 0 | { |
3581 | 0 | if ((value < 0) || (value > 0xFF)) |
3582 | 0 | return (TIFFReadDirEntryErrRange); |
3583 | 0 | else |
3584 | 0 | return (TIFFReadDirEntryErrOk); |
3585 | 0 | } |
3586 | | |
3587 | | static enum TIFFReadDirEntryErr |
3588 | | TIFFReadDirEntryCheckRangeByteLong(uint32_t value) |
3589 | 0 | { |
3590 | 0 | if (value > 0xFF) |
3591 | 0 | return (TIFFReadDirEntryErrRange); |
3592 | 0 | else |
3593 | 0 | return (TIFFReadDirEntryErrOk); |
3594 | 0 | } |
3595 | | |
3596 | | static enum TIFFReadDirEntryErr |
3597 | | TIFFReadDirEntryCheckRangeByteSlong(int32_t value) |
3598 | 0 | { |
3599 | 0 | if ((value < 0) || (value > 0xFF)) |
3600 | 0 | return (TIFFReadDirEntryErrRange); |
3601 | 0 | else |
3602 | 0 | return (TIFFReadDirEntryErrOk); |
3603 | 0 | } |
3604 | | |
3605 | | static enum TIFFReadDirEntryErr |
3606 | | TIFFReadDirEntryCheckRangeByteLong8(uint64_t value) |
3607 | 0 | { |
3608 | 0 | if (value > 0xFF) |
3609 | 0 | return (TIFFReadDirEntryErrRange); |
3610 | 0 | else |
3611 | 0 | return (TIFFReadDirEntryErrOk); |
3612 | 0 | } |
3613 | | |
3614 | | static enum TIFFReadDirEntryErr |
3615 | | TIFFReadDirEntryCheckRangeByteSlong8(int64_t value) |
3616 | 0 | { |
3617 | 0 | if ((value < 0) || (value > 0xFF)) |
3618 | 0 | return (TIFFReadDirEntryErrRange); |
3619 | 0 | else |
3620 | 0 | return (TIFFReadDirEntryErrOk); |
3621 | 0 | } |
3622 | | |
3623 | | static enum TIFFReadDirEntryErr |
3624 | | TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value) |
3625 | 0 | { |
3626 | 0 | if (value > 0x7F) |
3627 | 0 | return (TIFFReadDirEntryErrRange); |
3628 | 0 | else |
3629 | 0 | return (TIFFReadDirEntryErrOk); |
3630 | 0 | } |
3631 | | |
3632 | | static enum TIFFReadDirEntryErr |
3633 | | TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value) |
3634 | 0 | { |
3635 | 0 | if (value > 0x7F) |
3636 | 0 | return (TIFFReadDirEntryErrRange); |
3637 | 0 | else |
3638 | 0 | return (TIFFReadDirEntryErrOk); |
3639 | 0 | } |
3640 | | |
3641 | | static enum TIFFReadDirEntryErr |
3642 | | TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value) |
3643 | 0 | { |
3644 | 0 | if ((value < -0x80) || (value > 0x7F)) |
3645 | 0 | return (TIFFReadDirEntryErrRange); |
3646 | 0 | else |
3647 | 0 | return (TIFFReadDirEntryErrOk); |
3648 | 0 | } |
3649 | | |
3650 | | static enum TIFFReadDirEntryErr |
3651 | | TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value) |
3652 | 0 | { |
3653 | 0 | if (value > 0x7F) |
3654 | 0 | return (TIFFReadDirEntryErrRange); |
3655 | 0 | else |
3656 | 0 | return (TIFFReadDirEntryErrOk); |
3657 | 0 | } |
3658 | | |
3659 | | static enum TIFFReadDirEntryErr |
3660 | | TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value) |
3661 | 0 | { |
3662 | 0 | if ((value < -0x80) || (value > 0x7F)) |
3663 | 0 | return (TIFFReadDirEntryErrRange); |
3664 | 0 | else |
3665 | 0 | return (TIFFReadDirEntryErrOk); |
3666 | 0 | } |
3667 | | |
3668 | | static enum TIFFReadDirEntryErr |
3669 | | TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value) |
3670 | 0 | { |
3671 | 0 | if (value > 0x7F) |
3672 | 0 | return (TIFFReadDirEntryErrRange); |
3673 | 0 | else |
3674 | 0 | return (TIFFReadDirEntryErrOk); |
3675 | 0 | } |
3676 | | |
3677 | | static enum TIFFReadDirEntryErr |
3678 | | TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value) |
3679 | 0 | { |
3680 | 0 | if ((value < -0x80) || (value > 0x7F)) |
3681 | 0 | return (TIFFReadDirEntryErrRange); |
3682 | 0 | else |
3683 | 0 | return (TIFFReadDirEntryErrOk); |
3684 | 0 | } |
3685 | | |
3686 | | static enum TIFFReadDirEntryErr |
3687 | | TIFFReadDirEntryCheckRangeShortSbyte(int8_t value) |
3688 | 0 | { |
3689 | 0 | if (value < 0) |
3690 | 0 | return (TIFFReadDirEntryErrRange); |
3691 | 0 | else |
3692 | 0 | return (TIFFReadDirEntryErrOk); |
3693 | 0 | } |
3694 | | |
3695 | | static enum TIFFReadDirEntryErr |
3696 | | TIFFReadDirEntryCheckRangeShortSshort(int16_t value) |
3697 | 0 | { |
3698 | 0 | if (value < 0) |
3699 | 0 | return (TIFFReadDirEntryErrRange); |
3700 | 0 | else |
3701 | 0 | return (TIFFReadDirEntryErrOk); |
3702 | 0 | } |
3703 | | |
3704 | | static enum TIFFReadDirEntryErr |
3705 | | TIFFReadDirEntryCheckRangeShortLong(uint32_t value) |
3706 | 0 | { |
3707 | 0 | if (value > 0xFFFF) |
3708 | 0 | return (TIFFReadDirEntryErrRange); |
3709 | 0 | else |
3710 | 0 | return (TIFFReadDirEntryErrOk); |
3711 | 0 | } |
3712 | | |
3713 | | static enum TIFFReadDirEntryErr |
3714 | | TIFFReadDirEntryCheckRangeShortSlong(int32_t value) |
3715 | 0 | { |
3716 | 0 | if ((value < 0) || (value > 0xFFFF)) |
3717 | 0 | return (TIFFReadDirEntryErrRange); |
3718 | 0 | else |
3719 | 0 | return (TIFFReadDirEntryErrOk); |
3720 | 0 | } |
3721 | | |
3722 | | static enum TIFFReadDirEntryErr |
3723 | | TIFFReadDirEntryCheckRangeShortLong8(uint64_t value) |
3724 | 0 | { |
3725 | 0 | if (value > 0xFFFF) |
3726 | 0 | return (TIFFReadDirEntryErrRange); |
3727 | 0 | else |
3728 | 0 | return (TIFFReadDirEntryErrOk); |
3729 | 0 | } |
3730 | | |
3731 | | static enum TIFFReadDirEntryErr |
3732 | | TIFFReadDirEntryCheckRangeShortSlong8(int64_t value) |
3733 | 0 | { |
3734 | 0 | if ((value < 0) || (value > 0xFFFF)) |
3735 | 0 | return (TIFFReadDirEntryErrRange); |
3736 | 0 | else |
3737 | 0 | return (TIFFReadDirEntryErrOk); |
3738 | 0 | } |
3739 | | |
3740 | | static enum TIFFReadDirEntryErr |
3741 | | TIFFReadDirEntryCheckRangeSshortShort(uint16_t value) |
3742 | 0 | { |
3743 | 0 | if (value > 0x7FFF) |
3744 | 0 | return (TIFFReadDirEntryErrRange); |
3745 | 0 | else |
3746 | 0 | return (TIFFReadDirEntryErrOk); |
3747 | 0 | } |
3748 | | |
3749 | | static enum TIFFReadDirEntryErr |
3750 | | TIFFReadDirEntryCheckRangeSshortLong(uint32_t value) |
3751 | 0 | { |
3752 | 0 | if (value > 0x7FFF) |
3753 | 0 | return (TIFFReadDirEntryErrRange); |
3754 | 0 | else |
3755 | 0 | return (TIFFReadDirEntryErrOk); |
3756 | 0 | } |
3757 | | |
3758 | | static enum TIFFReadDirEntryErr |
3759 | | TIFFReadDirEntryCheckRangeSshortSlong(int32_t value) |
3760 | 0 | { |
3761 | 0 | if ((value < -0x8000) || (value > 0x7FFF)) |
3762 | 0 | return (TIFFReadDirEntryErrRange); |
3763 | 0 | else |
3764 | 0 | return (TIFFReadDirEntryErrOk); |
3765 | 0 | } |
3766 | | |
3767 | | static enum TIFFReadDirEntryErr |
3768 | | TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value) |
3769 | 0 | { |
3770 | 0 | if (value > 0x7FFF) |
3771 | 0 | return (TIFFReadDirEntryErrRange); |
3772 | 0 | else |
3773 | 0 | return (TIFFReadDirEntryErrOk); |
3774 | 0 | } |
3775 | | |
3776 | | static enum TIFFReadDirEntryErr |
3777 | | TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value) |
3778 | 0 | { |
3779 | 0 | if ((value < -0x8000) || (value > 0x7FFF)) |
3780 | 0 | return (TIFFReadDirEntryErrRange); |
3781 | 0 | else |
3782 | 0 | return (TIFFReadDirEntryErrOk); |
3783 | 0 | } |
3784 | | |
3785 | | static enum TIFFReadDirEntryErr |
3786 | | TIFFReadDirEntryCheckRangeLongSbyte(int8_t value) |
3787 | 0 | { |
3788 | 0 | if (value < 0) |
3789 | 0 | return (TIFFReadDirEntryErrRange); |
3790 | 0 | else |
3791 | 0 | return (TIFFReadDirEntryErrOk); |
3792 | 0 | } |
3793 | | |
3794 | | static enum TIFFReadDirEntryErr |
3795 | | TIFFReadDirEntryCheckRangeLongSshort(int16_t value) |
3796 | 0 | { |
3797 | 0 | if (value < 0) |
3798 | 0 | return (TIFFReadDirEntryErrRange); |
3799 | 0 | else |
3800 | 0 | return (TIFFReadDirEntryErrOk); |
3801 | 0 | } |
3802 | | |
3803 | | static enum TIFFReadDirEntryErr |
3804 | | TIFFReadDirEntryCheckRangeLongSlong(int32_t value) |
3805 | 0 | { |
3806 | 0 | if (value < 0) |
3807 | 0 | return (TIFFReadDirEntryErrRange); |
3808 | 0 | else |
3809 | 0 | return (TIFFReadDirEntryErrOk); |
3810 | 0 | } |
3811 | | |
3812 | | static enum TIFFReadDirEntryErr |
3813 | | TIFFReadDirEntryCheckRangeLongLong8(uint64_t value) |
3814 | 0 | { |
3815 | 0 | if (value > UINT32_MAX) |
3816 | 0 | return (TIFFReadDirEntryErrRange); |
3817 | 0 | else |
3818 | 0 | return (TIFFReadDirEntryErrOk); |
3819 | 0 | } |
3820 | | |
3821 | | static enum TIFFReadDirEntryErr |
3822 | | TIFFReadDirEntryCheckRangeLongSlong8(int64_t value) |
3823 | 0 | { |
3824 | 0 | if ((value < 0) || (value > (int64_t)UINT32_MAX)) |
3825 | 0 | return (TIFFReadDirEntryErrRange); |
3826 | 0 | else |
3827 | 0 | return (TIFFReadDirEntryErrOk); |
3828 | 0 | } |
3829 | | |
3830 | | static enum TIFFReadDirEntryErr |
3831 | | TIFFReadDirEntryCheckRangeSlongLong(uint32_t value) |
3832 | 0 | { |
3833 | 0 | if (value > 0x7FFFFFFFUL) |
3834 | 0 | return (TIFFReadDirEntryErrRange); |
3835 | 0 | else |
3836 | 0 | return (TIFFReadDirEntryErrOk); |
3837 | 0 | } |
3838 | | |
3839 | | /* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */ |
3840 | | static enum TIFFReadDirEntryErr |
3841 | | TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value) |
3842 | 0 | { |
3843 | 0 | if (value > 0x7FFFFFFF) |
3844 | 0 | return (TIFFReadDirEntryErrRange); |
3845 | 0 | else |
3846 | 0 | return (TIFFReadDirEntryErrOk); |
3847 | 0 | } |
3848 | | |
3849 | | /* Check that the 8-byte signed value can fit in a 4-byte signed range */ |
3850 | | static enum TIFFReadDirEntryErr |
3851 | | TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value) |
3852 | 0 | { |
3853 | 0 | if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF)) |
3854 | 0 | return (TIFFReadDirEntryErrRange); |
3855 | 0 | else |
3856 | 0 | return (TIFFReadDirEntryErrOk); |
3857 | 0 | } |
3858 | | |
3859 | | static enum TIFFReadDirEntryErr |
3860 | | TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value) |
3861 | 0 | { |
3862 | 0 | if (value < 0) |
3863 | 0 | return (TIFFReadDirEntryErrRange); |
3864 | 0 | else |
3865 | 0 | return (TIFFReadDirEntryErrOk); |
3866 | 0 | } |
3867 | | |
3868 | | static enum TIFFReadDirEntryErr |
3869 | | TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value) |
3870 | 0 | { |
3871 | 0 | if (value < 0) |
3872 | 0 | return (TIFFReadDirEntryErrRange); |
3873 | 0 | else |
3874 | 0 | return (TIFFReadDirEntryErrOk); |
3875 | 0 | } |
3876 | | |
3877 | | static enum TIFFReadDirEntryErr |
3878 | | TIFFReadDirEntryCheckRangeLong8Slong(int32_t value) |
3879 | 0 | { |
3880 | 0 | if (value < 0) |
3881 | 0 | return (TIFFReadDirEntryErrRange); |
3882 | 0 | else |
3883 | 0 | return (TIFFReadDirEntryErrOk); |
3884 | 0 | } |
3885 | | |
3886 | | static enum TIFFReadDirEntryErr |
3887 | | TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value) |
3888 | 0 | { |
3889 | 0 | if (value < 0) |
3890 | 0 | return (TIFFReadDirEntryErrRange); |
3891 | 0 | else |
3892 | 0 | return (TIFFReadDirEntryErrOk); |
3893 | 0 | } |
3894 | | |
3895 | | static enum TIFFReadDirEntryErr |
3896 | | TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value) |
3897 | 0 | { |
3898 | 0 | if (value > INT64_MAX) |
3899 | 0 | return (TIFFReadDirEntryErrRange); |
3900 | 0 | else |
3901 | 0 | return (TIFFReadDirEntryErrOk); |
3902 | 0 | } |
3903 | | |
3904 | | static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset, |
3905 | | tmsize_t size, void *dest) |
3906 | 0 | { |
3907 | 0 | assert(size > 0); |
3908 | 0 | if (!isMapped(tif)) |
3909 | 0 | { |
3910 | 0 | if (!SeekOK(tif, offset)) |
3911 | 0 | return (TIFFReadDirEntryErrIo); |
3912 | 0 | if (!ReadOK(tif, dest, size)) |
3913 | 0 | return (TIFFReadDirEntryErrIo); |
3914 | 0 | } |
3915 | 0 | else |
3916 | 0 | { |
3917 | 0 | size_t ma, mb; |
3918 | 0 | ma = (size_t)offset; |
3919 | 0 | if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size) |
3920 | 0 | { |
3921 | 0 | return TIFFReadDirEntryErrIo; |
3922 | 0 | } |
3923 | 0 | mb = ma + size; |
3924 | 0 | if (mb > (uint64_t)tif->tif_size) |
3925 | 0 | return (TIFFReadDirEntryErrIo); |
3926 | 0 | _TIFFmemcpy(dest, tif->tif_base + ma, size); |
3927 | 0 | } |
3928 | 0 | return (TIFFReadDirEntryErrOk); |
3929 | 0 | } |
3930 | | |
3931 | | static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err, |
3932 | | const char *module, const char *tagname, |
3933 | | int recover) |
3934 | 0 | { |
3935 | 0 | if (!recover) |
3936 | 0 | { |
3937 | 0 | switch (err) |
3938 | 0 | { |
3939 | 0 | case TIFFReadDirEntryErrCount: |
3940 | 0 | TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"", |
3941 | 0 | tagname); |
3942 | 0 | break; |
3943 | 0 | case TIFFReadDirEntryErrType: |
3944 | 0 | TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"", |
3945 | 0 | tagname); |
3946 | 0 | break; |
3947 | 0 | case TIFFReadDirEntryErrIo: |
3948 | 0 | TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"", |
3949 | 0 | tagname); |
3950 | 0 | break; |
3951 | 0 | case TIFFReadDirEntryErrRange: |
3952 | 0 | TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"", |
3953 | 0 | tagname); |
3954 | 0 | break; |
3955 | 0 | case TIFFReadDirEntryErrPsdif: |
3956 | 0 | TIFFErrorExtR( |
3957 | 0 | tif, module, |
3958 | 0 | "Cannot handle different values per sample for \"%s\"", |
3959 | 0 | tagname); |
3960 | 0 | break; |
3961 | 0 | case TIFFReadDirEntryErrSizesan: |
3962 | 0 | TIFFErrorExtR(tif, module, |
3963 | 0 | "Sanity check on size of \"%s\" value failed", |
3964 | 0 | tagname); |
3965 | 0 | break; |
3966 | 0 | case TIFFReadDirEntryErrAlloc: |
3967 | 0 | TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"", |
3968 | 0 | tagname); |
3969 | 0 | break; |
3970 | 0 | default: |
3971 | 0 | assert(0); /* we should never get here */ |
3972 | 0 | break; |
3973 | 0 | } |
3974 | 0 | } |
3975 | 0 | else |
3976 | 0 | { |
3977 | 0 | switch (err) |
3978 | 0 | { |
3979 | 0 | case TIFFReadDirEntryErrCount: |
3980 | 0 | TIFFWarningExtR(tif, module, |
3981 | 0 | "Incorrect count for \"%s\"; tag ignored", |
3982 | 0 | tagname); |
3983 | 0 | break; |
3984 | 0 | case TIFFReadDirEntryErrType: |
3985 | 0 | TIFFWarningExtR(tif, module, |
3986 | 0 | "Incompatible type for \"%s\"; tag ignored", |
3987 | 0 | tagname); |
3988 | 0 | break; |
3989 | 0 | case TIFFReadDirEntryErrIo: |
3990 | 0 | TIFFWarningExtR( |
3991 | 0 | tif, module, |
3992 | 0 | "IO error during reading of \"%s\"; tag ignored", tagname); |
3993 | 0 | break; |
3994 | 0 | case TIFFReadDirEntryErrRange: |
3995 | 0 | TIFFWarningExtR(tif, module, |
3996 | 0 | "Incorrect value for \"%s\"; tag ignored", |
3997 | 0 | tagname); |
3998 | 0 | break; |
3999 | 0 | case TIFFReadDirEntryErrPsdif: |
4000 | 0 | TIFFWarningExtR(tif, module, |
4001 | 0 | "Cannot handle different values per sample for " |
4002 | 0 | "\"%s\"; tag ignored", |
4003 | 0 | tagname); |
4004 | 0 | break; |
4005 | 0 | case TIFFReadDirEntryErrSizesan: |
4006 | 0 | TIFFWarningExtR( |
4007 | 0 | tif, module, |
4008 | 0 | "Sanity check on size of \"%s\" value failed; tag ignored", |
4009 | 0 | tagname); |
4010 | 0 | break; |
4011 | 0 | case TIFFReadDirEntryErrAlloc: |
4012 | 0 | TIFFWarningExtR(tif, module, |
4013 | 0 | "Out of memory reading of \"%s\"; tag ignored", |
4014 | 0 | tagname); |
4015 | 0 | break; |
4016 | 0 | default: |
4017 | 0 | assert(0); /* we should never get here */ |
4018 | 0 | break; |
4019 | 0 | } |
4020 | 0 | } |
4021 | 0 | } |
4022 | | |
4023 | | /* |
4024 | | * Return the maximum number of color channels specified for a given photometric |
4025 | | * type. 0 is returned if photometric type isn't supported or no default value |
4026 | | * is defined by the specification. |
4027 | | */ |
4028 | | static int _TIFFGetMaxColorChannels(uint16_t photometric) |
4029 | 0 | { |
4030 | 0 | switch (photometric) |
4031 | 0 | { |
4032 | 0 | case PHOTOMETRIC_PALETTE: |
4033 | 0 | case PHOTOMETRIC_MINISWHITE: |
4034 | 0 | case PHOTOMETRIC_MINISBLACK: |
4035 | 0 | return 1; |
4036 | 0 | case PHOTOMETRIC_YCBCR: |
4037 | 0 | case PHOTOMETRIC_RGB: |
4038 | 0 | case PHOTOMETRIC_CIELAB: |
4039 | 0 | case PHOTOMETRIC_LOGLUV: |
4040 | 0 | case PHOTOMETRIC_ITULAB: |
4041 | 0 | case PHOTOMETRIC_ICCLAB: |
4042 | 0 | return 3; |
4043 | 0 | case PHOTOMETRIC_SEPARATED: |
4044 | 0 | case PHOTOMETRIC_MASK: |
4045 | 0 | return 4; |
4046 | 0 | case PHOTOMETRIC_LOGL: |
4047 | 0 | case PHOTOMETRIC_CFA: |
4048 | 0 | default: |
4049 | 0 | return 0; |
4050 | 0 | } |
4051 | 0 | } |
4052 | | |
4053 | | static int ByteCountLooksBad(TIFF *tif) |
4054 | 0 | { |
4055 | | /* |
4056 | | * Assume we have wrong StripByteCount value (in case |
4057 | | * of single strip) in following cases: |
4058 | | * - it is equal to zero along with StripOffset; |
4059 | | * - it is larger than file itself (in case of uncompressed |
4060 | | * image); |
4061 | | * - it is smaller than the size of the bytes per row |
4062 | | * multiplied on the number of rows. The last case should |
4063 | | * not be checked in the case of writing new image, |
4064 | | * because we may do not know the exact strip size |
4065 | | * until the whole image will be written and directory |
4066 | | * dumped out. |
4067 | | */ |
4068 | 0 | uint64_t bytecount = TIFFGetStrileByteCount(tif, 0); |
4069 | 0 | uint64_t offset = TIFFGetStrileOffset(tif, 0); |
4070 | 0 | uint64_t filesize; |
4071 | |
|
4072 | 0 | if (offset == 0) |
4073 | 0 | return 0; |
4074 | 0 | if (bytecount == 0) |
4075 | 0 | return 1; |
4076 | 0 | if (tif->tif_dir.td_compression != COMPRESSION_NONE) |
4077 | 0 | return 0; |
4078 | 0 | filesize = TIFFGetFileSize(tif); |
4079 | 0 | if (offset <= filesize && bytecount > filesize - offset) |
4080 | 0 | return 1; |
4081 | 0 | if (tif->tif_mode == O_RDONLY) |
4082 | 0 | { |
4083 | 0 | uint64_t scanlinesize = TIFFScanlineSize64(tif); |
4084 | 0 | if (tif->tif_dir.td_imagelength > 0 && |
4085 | 0 | scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength) |
4086 | 0 | { |
4087 | 0 | return 1; |
4088 | 0 | } |
4089 | 0 | if (bytecount < scanlinesize * tif->tif_dir.td_imagelength) |
4090 | 0 | return 1; |
4091 | 0 | } |
4092 | 0 | return 0; |
4093 | 0 | } |
4094 | | |
4095 | | /* |
4096 | | * Read the next TIFF directory from a file and convert it to the internal |
4097 | | * format. We read directories sequentially. |
4098 | | */ |
4099 | | int TIFFReadDirectory(TIFF *tif) |
4100 | 0 | { |
4101 | 0 | static const char module[] = "TIFFReadDirectory"; |
4102 | 0 | TIFFDirEntry *dir; |
4103 | 0 | uint16_t dircount; |
4104 | 0 | TIFFDirEntry *dp; |
4105 | 0 | uint16_t di; |
4106 | 0 | const TIFFField *fip; |
4107 | 0 | uint32_t fii = FAILED_FII; |
4108 | 0 | toff_t nextdiroff; |
4109 | 0 | int bitspersample_read = FALSE; |
4110 | 0 | int color_channels; |
4111 | |
|
4112 | 0 | if (tif->tif_nextdiroff == 0) |
4113 | 0 | { |
4114 | | /* In this special case, tif_diroff needs also to be set to 0. |
4115 | | * This is behind the last IFD, thus no checking or reading necessary. |
4116 | | */ |
4117 | 0 | tif->tif_diroff = tif->tif_nextdiroff; |
4118 | 0 | return 0; |
4119 | 0 | } |
4120 | | |
4121 | 0 | nextdiroff = tif->tif_nextdiroff; |
4122 | | /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL |
4123 | | * reading of the directory. Otherwise, invalid IFD offsets could corrupt |
4124 | | * the IFD list. */ |
4125 | 0 | if (!_TIFFCheckDirNumberAndOffset(tif, |
4126 | 0 | tif->tif_curdir == |
4127 | 0 | TIFF_NON_EXISTENT_DIR_NUMBER |
4128 | 0 | ? 0 |
4129 | 0 | : tif->tif_curdir + 1, |
4130 | 0 | nextdiroff)) |
4131 | 0 | { |
4132 | 0 | return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT |
4133 | | IFDs) */ |
4134 | 0 | } |
4135 | 0 | dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff); |
4136 | 0 | if (!dircount) |
4137 | 0 | { |
4138 | 0 | TIFFErrorExtR(tif, module, |
4139 | 0 | "Failed to read directory at offset %" PRIu64, |
4140 | 0 | nextdiroff); |
4141 | 0 | return 0; |
4142 | 0 | } |
4143 | | /* Set global values after a valid directory has been fetched. |
4144 | | * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the |
4145 | | * beginning. */ |
4146 | 0 | if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER) |
4147 | 0 | tif->tif_curdir = 0; |
4148 | 0 | else |
4149 | 0 | tif->tif_curdir++; |
4150 | 0 | (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */ |
4151 | |
|
4152 | 0 | TIFFReadDirectoryCheckOrder(tif, dir, dircount); |
4153 | | |
4154 | | /* |
4155 | | * Mark duplicates of any tag to be ignored (bugzilla 1994) |
4156 | | * to avoid certain pathological problems. |
4157 | | */ |
4158 | 0 | { |
4159 | 0 | TIFFDirEntry *ma; |
4160 | 0 | uint16_t mb; |
4161 | 0 | for (ma = dir, mb = 0; mb < dircount; ma++, mb++) |
4162 | 0 | { |
4163 | 0 | TIFFDirEntry *na; |
4164 | 0 | uint16_t nb; |
4165 | 0 | for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++) |
4166 | 0 | { |
4167 | 0 | if (ma->tdir_tag == na->tdir_tag) |
4168 | 0 | { |
4169 | 0 | na->tdir_ignore = TRUE; |
4170 | 0 | } |
4171 | 0 | } |
4172 | 0 | } |
4173 | 0 | } |
4174 | |
|
4175 | 0 | tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */ |
4176 | 0 | tif->tif_flags &= ~TIFF_BUF4WRITE; /* reset before new dir */ |
4177 | 0 | tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS; |
4178 | | |
4179 | | /* free any old stuff and reinit */ |
4180 | 0 | TIFFFreeDirectory(tif); |
4181 | 0 | TIFFDefaultDirectory(tif); |
4182 | | /* |
4183 | | * Electronic Arts writes gray-scale TIFF files |
4184 | | * without a PlanarConfiguration directory entry. |
4185 | | * Thus we setup a default value here, even though |
4186 | | * the TIFF spec says there is no default value. |
4187 | | * After PlanarConfiguration is preset in TIFFDefaultDirectory() |
4188 | | * the following setting is not needed, but does not harm either. |
4189 | | */ |
4190 | 0 | TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
4191 | | /* |
4192 | | * Setup default value and then make a pass over |
4193 | | * the fields to check type and tag information, |
4194 | | * and to extract info required to size data |
4195 | | * structures. A second pass is made afterwards |
4196 | | * to read in everything not taken in the first pass. |
4197 | | * But we must process the Compression tag first |
4198 | | * in order to merge in codec-private tag definitions (otherwise |
4199 | | * we may get complaints about unknown tags). However, the |
4200 | | * Compression tag may be dependent on the SamplesPerPixel |
4201 | | * tag value because older TIFF specs permitted Compression |
4202 | | * to be written as a SamplesPerPixel-count tag entry. |
4203 | | * Thus if we don't first figure out the correct SamplesPerPixel |
4204 | | * tag value then we may end up ignoring the Compression tag |
4205 | | * value because it has an incorrect count value (if the |
4206 | | * true value of SamplesPerPixel is not 1). |
4207 | | */ |
4208 | 0 | dp = |
4209 | 0 | TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL); |
4210 | 0 | if (dp) |
4211 | 0 | { |
4212 | 0 | if (!TIFFFetchNormalTag(tif, dp, 0)) |
4213 | 0 | goto bad; |
4214 | 0 | dp->tdir_ignore = TRUE; |
4215 | 0 | } |
4216 | 0 | dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION); |
4217 | 0 | if (dp) |
4218 | 0 | { |
4219 | | /* |
4220 | | * The 5.0 spec says the Compression tag has one value, while |
4221 | | * earlier specs say it has one value per sample. Because of |
4222 | | * this, we accept the tag if one value is supplied with either |
4223 | | * count. |
4224 | | */ |
4225 | 0 | uint16_t value; |
4226 | 0 | enum TIFFReadDirEntryErr err; |
4227 | 0 | err = TIFFReadDirEntryShort(tif, dp, &value); |
4228 | 0 | if (err == TIFFReadDirEntryErrCount) |
4229 | 0 | err = TIFFReadDirEntryPersampleShort(tif, dp, &value); |
4230 | 0 | if (err != TIFFReadDirEntryErrOk) |
4231 | 0 | { |
4232 | 0 | TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0); |
4233 | 0 | goto bad; |
4234 | 0 | } |
4235 | 0 | if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value)) |
4236 | 0 | goto bad; |
4237 | 0 | dp->tdir_ignore = TRUE; |
4238 | 0 | } |
4239 | 0 | else |
4240 | 0 | { |
4241 | 0 | if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE)) |
4242 | 0 | goto bad; |
4243 | 0 | } |
4244 | | /* |
4245 | | * First real pass over the directory. |
4246 | | */ |
4247 | 0 | for (di = 0, dp = dir; di < dircount; di++, dp++) |
4248 | 0 | { |
4249 | 0 | if (!dp->tdir_ignore) |
4250 | 0 | { |
4251 | 0 | TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); |
4252 | 0 | if (fii == FAILED_FII) |
4253 | 0 | { |
4254 | 0 | TIFFWarningExtR(tif, module, |
4255 | 0 | "Unknown field with tag %" PRIu16 " (0x%" PRIx16 |
4256 | 0 | ") encountered", |
4257 | 0 | dp->tdir_tag, dp->tdir_tag); |
4258 | | /* the following knowingly leaks the |
4259 | | anonymous field structure */ |
4260 | 0 | if (!_TIFFMergeFields( |
4261 | 0 | tif, |
4262 | 0 | _TIFFCreateAnonField(tif, dp->tdir_tag, |
4263 | 0 | (TIFFDataType)dp->tdir_type), |
4264 | 0 | 1)) |
4265 | 0 | { |
4266 | 0 | TIFFWarningExtR( |
4267 | 0 | tif, module, |
4268 | 0 | "Registering anonymous field with tag %" PRIu16 |
4269 | 0 | " (0x%" PRIx16 ") failed", |
4270 | 0 | dp->tdir_tag, dp->tdir_tag); |
4271 | 0 | dp->tdir_ignore = TRUE; |
4272 | 0 | } |
4273 | 0 | else |
4274 | 0 | { |
4275 | 0 | TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); |
4276 | 0 | assert(fii != FAILED_FII); |
4277 | 0 | } |
4278 | 0 | } |
4279 | 0 | } |
4280 | 0 | if (!dp->tdir_ignore) |
4281 | 0 | { |
4282 | 0 | fip = tif->tif_fields[fii]; |
4283 | 0 | if (fip->field_bit == FIELD_IGNORE) |
4284 | 0 | dp->tdir_ignore = TRUE; |
4285 | 0 | else |
4286 | 0 | { |
4287 | 0 | switch (dp->tdir_tag) |
4288 | 0 | { |
4289 | 0 | case TIFFTAG_STRIPOFFSETS: |
4290 | 0 | case TIFFTAG_STRIPBYTECOUNTS: |
4291 | 0 | case TIFFTAG_TILEOFFSETS: |
4292 | 0 | case TIFFTAG_TILEBYTECOUNTS: |
4293 | 0 | TIFFSetFieldBit(tif, fip->field_bit); |
4294 | 0 | break; |
4295 | 0 | case TIFFTAG_IMAGEWIDTH: |
4296 | 0 | case TIFFTAG_IMAGELENGTH: |
4297 | 0 | case TIFFTAG_IMAGEDEPTH: |
4298 | 0 | case TIFFTAG_TILELENGTH: |
4299 | 0 | case TIFFTAG_TILEWIDTH: |
4300 | 0 | case TIFFTAG_TILEDEPTH: |
4301 | 0 | case TIFFTAG_PLANARCONFIG: |
4302 | 0 | case TIFFTAG_ROWSPERSTRIP: |
4303 | 0 | case TIFFTAG_EXTRASAMPLES: |
4304 | 0 | if (!TIFFFetchNormalTag(tif, dp, 0)) |
4305 | 0 | goto bad; |
4306 | 0 | dp->tdir_ignore = TRUE; |
4307 | 0 | break; |
4308 | 0 | default: |
4309 | 0 | if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag)) |
4310 | 0 | dp->tdir_ignore = TRUE; |
4311 | 0 | break; |
4312 | 0 | } |
4313 | 0 | } |
4314 | 0 | } |
4315 | 0 | } |
4316 | | /* |
4317 | | * XXX: OJPEG hack. |
4318 | | * If a) compression is OJPEG, b) planarconfig tag says it's separate, |
4319 | | * c) strip offsets/bytecounts tag are both present and |
4320 | | * d) both contain exactly one value, then we consistently find |
4321 | | * that the buggy implementation of the buggy compression scheme |
4322 | | * matches contig planarconfig best. So we 'fix-up' the tag here |
4323 | | */ |
4324 | 0 | if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) && |
4325 | 0 | (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)) |
4326 | 0 | { |
4327 | 0 | if (!_TIFFFillStriles(tif)) |
4328 | 0 | goto bad; |
4329 | 0 | dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, |
4330 | 0 | TIFFTAG_STRIPOFFSETS); |
4331 | 0 | if ((dp != 0) && (dp->tdir_count == 1)) |
4332 | 0 | { |
4333 | 0 | dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, |
4334 | 0 | TIFFTAG_STRIPBYTECOUNTS); |
4335 | 0 | if ((dp != 0) && (dp->tdir_count == 1)) |
4336 | 0 | { |
4337 | 0 | tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG; |
4338 | 0 | TIFFWarningExtR(tif, module, |
4339 | 0 | "Planarconfig tag value assumed incorrect, " |
4340 | 0 | "assuming data is contig instead of chunky"); |
4341 | 0 | } |
4342 | 0 | } |
4343 | 0 | } |
4344 | | /* |
4345 | | * Allocate directory structure and setup defaults. |
4346 | | */ |
4347 | 0 | if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) |
4348 | 0 | { |
4349 | 0 | MissingRequired(tif, "ImageLength"); |
4350 | 0 | goto bad; |
4351 | 0 | } |
4352 | | |
4353 | | /* |
4354 | | * Second pass: extract other information. |
4355 | | */ |
4356 | 0 | for (di = 0, dp = dir; di < dircount; di++, dp++) |
4357 | 0 | { |
4358 | 0 | if (!dp->tdir_ignore) |
4359 | 0 | { |
4360 | 0 | switch (dp->tdir_tag) |
4361 | 0 | { |
4362 | 0 | case TIFFTAG_MINSAMPLEVALUE: |
4363 | 0 | case TIFFTAG_MAXSAMPLEVALUE: |
4364 | 0 | case TIFFTAG_BITSPERSAMPLE: |
4365 | 0 | case TIFFTAG_DATATYPE: |
4366 | 0 | case TIFFTAG_SAMPLEFORMAT: |
4367 | | /* |
4368 | | * The MinSampleValue, MaxSampleValue, BitsPerSample |
4369 | | * DataType and SampleFormat tags are supposed to be |
4370 | | * written as one value/sample, but some vendors |
4371 | | * incorrectly write one value only -- so we accept |
4372 | | * that as well (yuck). Other vendors write correct |
4373 | | * value for NumberOfSamples, but incorrect one for |
4374 | | * BitsPerSample and friends, and we will read this |
4375 | | * too. |
4376 | | */ |
4377 | 0 | { |
4378 | 0 | uint16_t value; |
4379 | 0 | enum TIFFReadDirEntryErr err; |
4380 | 0 | err = TIFFReadDirEntryShort(tif, dp, &value); |
4381 | 0 | if (err == TIFFReadDirEntryErrCount) |
4382 | 0 | err = |
4383 | 0 | TIFFReadDirEntryPersampleShort(tif, dp, &value); |
4384 | 0 | if (err != TIFFReadDirEntryErrOk) |
4385 | 0 | { |
4386 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4387 | 0 | TIFFReadDirEntryOutputErr( |
4388 | 0 | tif, err, module, |
4389 | 0 | fip ? fip->field_name : "unknown tagname", 0); |
4390 | 0 | goto bad; |
4391 | 0 | } |
4392 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, value)) |
4393 | 0 | goto bad; |
4394 | 0 | if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE) |
4395 | 0 | bitspersample_read = TRUE; |
4396 | 0 | } |
4397 | 0 | break; |
4398 | 0 | case TIFFTAG_SMINSAMPLEVALUE: |
4399 | 0 | case TIFFTAG_SMAXSAMPLEVALUE: |
4400 | 0 | { |
4401 | |
|
4402 | 0 | double *data = NULL; |
4403 | 0 | enum TIFFReadDirEntryErr err; |
4404 | 0 | uint32_t saved_flags; |
4405 | 0 | int m; |
4406 | 0 | if (dp->tdir_count != |
4407 | 0 | (uint64_t)tif->tif_dir.td_samplesperpixel) |
4408 | 0 | err = TIFFReadDirEntryErrCount; |
4409 | 0 | else |
4410 | 0 | err = TIFFReadDirEntryDoubleArray(tif, dp, &data); |
4411 | 0 | if (err != TIFFReadDirEntryErrOk) |
4412 | 0 | { |
4413 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4414 | 0 | TIFFReadDirEntryOutputErr( |
4415 | 0 | tif, err, module, |
4416 | 0 | fip ? fip->field_name : "unknown tagname", 0); |
4417 | 0 | goto bad; |
4418 | 0 | } |
4419 | 0 | saved_flags = tif->tif_flags; |
4420 | 0 | tif->tif_flags |= TIFF_PERSAMPLE; |
4421 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
4422 | 0 | tif->tif_flags = saved_flags; |
4423 | 0 | _TIFFfreeExt(tif, data); |
4424 | 0 | if (!m) |
4425 | 0 | goto bad; |
4426 | 0 | } |
4427 | 0 | break; |
4428 | 0 | case TIFFTAG_STRIPOFFSETS: |
4429 | 0 | case TIFFTAG_TILEOFFSETS: |
4430 | 0 | switch (dp->tdir_type) |
4431 | 0 | { |
4432 | 0 | case TIFF_SHORT: |
4433 | 0 | case TIFF_LONG: |
4434 | 0 | case TIFF_LONG8: |
4435 | 0 | break; |
4436 | 0 | default: |
4437 | | /* Warn except if directory typically created with |
4438 | | * TIFFDeferStrileArrayWriting() */ |
4439 | 0 | if (!(tif->tif_mode == O_RDWR && |
4440 | 0 | dp->tdir_count == 0 && dp->tdir_type == 0 && |
4441 | 0 | dp->tdir_offset.toff_long8 == 0)) |
4442 | 0 | { |
4443 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4444 | 0 | TIFFWarningExtR( |
4445 | 0 | tif, module, "Invalid data type for tag %s", |
4446 | 0 | fip ? fip->field_name : "unknown tagname"); |
4447 | 0 | } |
4448 | 0 | break; |
4449 | 0 | } |
4450 | 0 | _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp, |
4451 | 0 | sizeof(TIFFDirEntry)); |
4452 | 0 | break; |
4453 | 0 | case TIFFTAG_STRIPBYTECOUNTS: |
4454 | 0 | case TIFFTAG_TILEBYTECOUNTS: |
4455 | 0 | switch (dp->tdir_type) |
4456 | 0 | { |
4457 | 0 | case TIFF_SHORT: |
4458 | 0 | case TIFF_LONG: |
4459 | 0 | case TIFF_LONG8: |
4460 | 0 | break; |
4461 | 0 | default: |
4462 | | /* Warn except if directory typically created with |
4463 | | * TIFFDeferStrileArrayWriting() */ |
4464 | 0 | if (!(tif->tif_mode == O_RDWR && |
4465 | 0 | dp->tdir_count == 0 && dp->tdir_type == 0 && |
4466 | 0 | dp->tdir_offset.toff_long8 == 0)) |
4467 | 0 | { |
4468 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4469 | 0 | TIFFWarningExtR( |
4470 | 0 | tif, module, "Invalid data type for tag %s", |
4471 | 0 | fip ? fip->field_name : "unknown tagname"); |
4472 | 0 | } |
4473 | 0 | break; |
4474 | 0 | } |
4475 | 0 | _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp, |
4476 | 0 | sizeof(TIFFDirEntry)); |
4477 | 0 | break; |
4478 | 0 | case TIFFTAG_COLORMAP: |
4479 | 0 | case TIFFTAG_TRANSFERFUNCTION: |
4480 | 0 | { |
4481 | 0 | enum TIFFReadDirEntryErr err; |
4482 | 0 | uint32_t countpersample; |
4483 | 0 | uint32_t countrequired; |
4484 | 0 | uint32_t incrementpersample; |
4485 | 0 | uint16_t *value = NULL; |
4486 | | /* It would be dangerous to instantiate those tag values */ |
4487 | | /* since if td_bitspersample has not yet been read (due to |
4488 | | */ |
4489 | | /* unordered tags), it could be read afterwards with a */ |
4490 | | /* values greater than the default one (1), which may cause |
4491 | | */ |
4492 | | /* crashes in user code */ |
4493 | 0 | if (!bitspersample_read) |
4494 | 0 | { |
4495 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4496 | 0 | TIFFWarningExtR( |
4497 | 0 | tif, module, |
4498 | 0 | "Ignoring %s since BitsPerSample tag not found", |
4499 | 0 | fip ? fip->field_name : "unknown tagname"); |
4500 | 0 | continue; |
4501 | 0 | } |
4502 | | /* ColorMap or TransferFunction for high bit */ |
4503 | | /* depths do not make much sense and could be */ |
4504 | | /* used as a denial of service vector */ |
4505 | 0 | if (tif->tif_dir.td_bitspersample > 24) |
4506 | 0 | { |
4507 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4508 | 0 | TIFFWarningExtR( |
4509 | 0 | tif, module, |
4510 | 0 | "Ignoring %s because BitsPerSample=%" PRIu16 ">24", |
4511 | 0 | fip ? fip->field_name : "unknown tagname", |
4512 | 0 | tif->tif_dir.td_bitspersample); |
4513 | 0 | continue; |
4514 | 0 | } |
4515 | 0 | countpersample = (1U << tif->tif_dir.td_bitspersample); |
4516 | 0 | if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) && |
4517 | 0 | (dp->tdir_count == (uint64_t)countpersample)) |
4518 | 0 | { |
4519 | 0 | countrequired = countpersample; |
4520 | 0 | incrementpersample = 0; |
4521 | 0 | } |
4522 | 0 | else |
4523 | 0 | { |
4524 | 0 | countrequired = 3 * countpersample; |
4525 | 0 | incrementpersample = countpersample; |
4526 | 0 | } |
4527 | 0 | if (dp->tdir_count != (uint64_t)countrequired) |
4528 | 0 | err = TIFFReadDirEntryErrCount; |
4529 | 0 | else |
4530 | 0 | err = TIFFReadDirEntryShortArray(tif, dp, &value); |
4531 | 0 | if (err != TIFFReadDirEntryErrOk) |
4532 | 0 | { |
4533 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4534 | 0 | TIFFReadDirEntryOutputErr( |
4535 | 0 | tif, err, module, |
4536 | 0 | fip ? fip->field_name : "unknown tagname", 1); |
4537 | 0 | } |
4538 | 0 | else |
4539 | 0 | { |
4540 | 0 | TIFFSetField(tif, dp->tdir_tag, value, |
4541 | 0 | value + incrementpersample, |
4542 | 0 | value + 2 * incrementpersample); |
4543 | 0 | _TIFFfreeExt(tif, value); |
4544 | 0 | } |
4545 | 0 | } |
4546 | 0 | break; |
4547 | | /* BEGIN REV 4.0 COMPATIBILITY */ |
4548 | 0 | case TIFFTAG_OSUBFILETYPE: |
4549 | 0 | { |
4550 | 0 | uint16_t valueo; |
4551 | 0 | uint32_t value; |
4552 | 0 | if (TIFFReadDirEntryShort(tif, dp, &valueo) == |
4553 | 0 | TIFFReadDirEntryErrOk) |
4554 | 0 | { |
4555 | 0 | switch (valueo) |
4556 | 0 | { |
4557 | 0 | case OFILETYPE_REDUCEDIMAGE: |
4558 | 0 | value = FILETYPE_REDUCEDIMAGE; |
4559 | 0 | break; |
4560 | 0 | case OFILETYPE_PAGE: |
4561 | 0 | value = FILETYPE_PAGE; |
4562 | 0 | break; |
4563 | 0 | default: |
4564 | 0 | value = 0; |
4565 | 0 | break; |
4566 | 0 | } |
4567 | 0 | if (value != 0) |
4568 | 0 | TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value); |
4569 | 0 | } |
4570 | 0 | } |
4571 | 0 | break; |
4572 | | /* END REV 4.0 COMPATIBILITY */ |
4573 | 0 | case TIFFTAG_EP_BATTERYLEVEL: |
4574 | | /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII. |
4575 | | * LibTiff defines it as ASCII and converts RATIONAL to an |
4576 | | * ASCII string. */ |
4577 | 0 | switch (dp->tdir_type) |
4578 | 0 | { |
4579 | 0 | case TIFF_RATIONAL: |
4580 | 0 | { |
4581 | | /* Read rational and convert to ASCII*/ |
4582 | 0 | enum TIFFReadDirEntryErr err; |
4583 | 0 | TIFFRational_t rValue; |
4584 | 0 | err = TIFFReadDirEntryCheckedRationalDirect( |
4585 | 0 | tif, dp, &rValue); |
4586 | 0 | if (err != TIFFReadDirEntryErrOk) |
4587 | 0 | { |
4588 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4589 | 0 | TIFFReadDirEntryOutputErr( |
4590 | 0 | tif, err, module, |
4591 | 0 | fip ? fip->field_name : "unknown tagname", |
4592 | 0 | 1); |
4593 | 0 | } |
4594 | 0 | else |
4595 | 0 | { |
4596 | 0 | char szAux[32]; |
4597 | 0 | snprintf(szAux, sizeof(szAux) - 1, "%d/%d", |
4598 | 0 | rValue.uNum, rValue.uDenom); |
4599 | 0 | TIFFSetField(tif, dp->tdir_tag, szAux); |
4600 | 0 | } |
4601 | 0 | } |
4602 | 0 | break; |
4603 | 0 | case TIFF_ASCII: |
4604 | 0 | (void)TIFFFetchNormalTag(tif, dp, TRUE); |
4605 | 0 | break; |
4606 | 0 | default: |
4607 | 0 | fip = TIFFFieldWithTag(tif, dp->tdir_tag); |
4608 | 0 | TIFFWarningExtR(tif, module, |
4609 | 0 | "Invalid data type for tag %s. " |
4610 | 0 | "ASCII or RATIONAL expected", |
4611 | 0 | fip ? fip->field_name |
4612 | 0 | : "unknown tagname"); |
4613 | 0 | break; |
4614 | 0 | } |
4615 | 0 | break; |
4616 | 0 | default: |
4617 | 0 | (void)TIFFFetchNormalTag(tif, dp, TRUE); |
4618 | 0 | break; |
4619 | 0 | } |
4620 | 0 | } /* -- if (!dp->tdir_ignore) */ |
4621 | 0 | } /* -- for-loop -- */ |
4622 | | |
4623 | | /* |
4624 | | * OJPEG hack: |
4625 | | * - If a) compression is OJPEG, and b) photometric tag is missing, |
4626 | | * then we consistently find that photometric should be YCbCr |
4627 | | * - If a) compression is OJPEG, and b) photometric tag says it's RGB, |
4628 | | * then we consistently find that the buggy implementation of the |
4629 | | * buggy compression scheme matches photometric YCbCr instead. |
4630 | | * - If a) compression is OJPEG, and b) bitspersample tag is missing, |
4631 | | * then we consistently find bitspersample should be 8. |
4632 | | * - If a) compression is OJPEG, b) samplesperpixel tag is missing, |
4633 | | * and c) photometric is RGB or YCbCr, then we consistently find |
4634 | | * samplesperpixel should be 3 |
4635 | | * - If a) compression is OJPEG, b) samplesperpixel tag is missing, |
4636 | | * and c) photometric is MINISWHITE or MINISBLACK, then we consistently |
4637 | | * find samplesperpixel should be 3 |
4638 | | */ |
4639 | 0 | if (tif->tif_dir.td_compression == COMPRESSION_OJPEG) |
4640 | 0 | { |
4641 | 0 | if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC)) |
4642 | 0 | { |
4643 | 0 | TIFFWarningExtR( |
4644 | 0 | tif, module, |
4645 | 0 | "Photometric tag is missing, assuming data is YCbCr"); |
4646 | 0 | if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR)) |
4647 | 0 | goto bad; |
4648 | 0 | } |
4649 | 0 | else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB) |
4650 | 0 | { |
4651 | 0 | tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR; |
4652 | 0 | TIFFWarningExtR(tif, module, |
4653 | 0 | "Photometric tag value assumed incorrect, " |
4654 | 0 | "assuming data is YCbCr instead of RGB"); |
4655 | 0 | } |
4656 | 0 | if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE)) |
4657 | 0 | { |
4658 | 0 | TIFFWarningExtR( |
4659 | 0 | tif, module, |
4660 | 0 | "BitsPerSample tag is missing, assuming 8 bits per sample"); |
4661 | 0 | if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) |
4662 | 0 | goto bad; |
4663 | 0 | } |
4664 | 0 | if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL)) |
4665 | 0 | { |
4666 | 0 | if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB) |
4667 | 0 | { |
4668 | 0 | TIFFWarningExtR(tif, module, |
4669 | 0 | "SamplesPerPixel tag is missing, " |
4670 | 0 | "assuming correct SamplesPerPixel value is 3"); |
4671 | 0 | if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) |
4672 | 0 | goto bad; |
4673 | 0 | } |
4674 | 0 | if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR) |
4675 | 0 | { |
4676 | 0 | TIFFWarningExtR(tif, module, |
4677 | 0 | "SamplesPerPixel tag is missing, " |
4678 | 0 | "applying correct SamplesPerPixel value of 3"); |
4679 | 0 | if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) |
4680 | 0 | goto bad; |
4681 | 0 | } |
4682 | 0 | else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) || |
4683 | 0 | (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK)) |
4684 | 0 | { |
4685 | | /* |
4686 | | * SamplesPerPixel tag is missing, but is not required |
4687 | | * by spec. Assume correct SamplesPerPixel value of 1. |
4688 | | */ |
4689 | 0 | if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1)) |
4690 | 0 | goto bad; |
4691 | 0 | } |
4692 | 0 | } |
4693 | 0 | } |
4694 | | |
4695 | | /* |
4696 | | * Setup appropriate structures (by strip or by tile) |
4697 | | * We do that only after the above OJPEG hack which alters SamplesPerPixel |
4698 | | * and thus influences the number of strips in the separate planarconfig. |
4699 | | */ |
4700 | 0 | if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) |
4701 | 0 | { |
4702 | 0 | tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif); |
4703 | 0 | tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth; |
4704 | 0 | tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip; |
4705 | 0 | tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth; |
4706 | 0 | tif->tif_flags &= ~TIFF_ISTILED; |
4707 | 0 | } |
4708 | 0 | else |
4709 | 0 | { |
4710 | 0 | tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif); |
4711 | 0 | tif->tif_flags |= TIFF_ISTILED; |
4712 | 0 | } |
4713 | 0 | if (!tif->tif_dir.td_nstrips) |
4714 | 0 | { |
4715 | 0 | TIFFErrorExtR(tif, module, "Cannot handle zero number of %s", |
4716 | 0 | isTiled(tif) ? "tiles" : "strips"); |
4717 | 0 | goto bad; |
4718 | 0 | } |
4719 | 0 | tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips; |
4720 | 0 | if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE) |
4721 | 0 | tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel; |
4722 | 0 | if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) |
4723 | 0 | { |
4724 | 0 | #ifdef OJPEG_SUPPORT |
4725 | 0 | if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) && |
4726 | 0 | (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1)) |
4727 | 0 | { |
4728 | | /* |
4729 | | * XXX: OJPEG hack. |
4730 | | * If a) compression is OJPEG, b) it's not a tiled TIFF, |
4731 | | * and c) the number of strips is 1, |
4732 | | * then we tolerate the absence of stripoffsets tag, |
4733 | | * because, presumably, all required data is in the |
4734 | | * JpegInterchangeFormat stream. |
4735 | | */ |
4736 | 0 | TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS); |
4737 | 0 | } |
4738 | 0 | else |
4739 | 0 | #endif |
4740 | 0 | { |
4741 | 0 | MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets"); |
4742 | 0 | goto bad; |
4743 | 0 | } |
4744 | 0 | } |
4745 | | |
4746 | 0 | if (tif->tif_mode == O_RDWR && |
4747 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 && |
4748 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_count == 0 && |
4749 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_type == 0 && |
4750 | 0 | tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 && |
4751 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 && |
4752 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 && |
4753 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 && |
4754 | 0 | tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) |
4755 | 0 | { |
4756 | | /* Directory typically created with TIFFDeferStrileArrayWriting() */ |
4757 | 0 | TIFFSetupStrips(tif); |
4758 | 0 | } |
4759 | 0 | else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD)) |
4760 | 0 | { |
4761 | 0 | if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0) |
4762 | 0 | { |
4763 | 0 | if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry), |
4764 | 0 | tif->tif_dir.td_nstrips, |
4765 | 0 | &tif->tif_dir.td_stripoffset_p)) |
4766 | 0 | { |
4767 | 0 | goto bad; |
4768 | 0 | } |
4769 | 0 | } |
4770 | 0 | if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0) |
4771 | 0 | { |
4772 | 0 | if (!TIFFFetchStripThing( |
4773 | 0 | tif, &(tif->tif_dir.td_stripbytecount_entry), |
4774 | 0 | tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p)) |
4775 | 0 | { |
4776 | 0 | goto bad; |
4777 | 0 | } |
4778 | 0 | } |
4779 | 0 | } |
4780 | | |
4781 | | /* |
4782 | | * Make sure all non-color channels are extrasamples. |
4783 | | * If it's not the case, define them as such. |
4784 | | */ |
4785 | 0 | color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric); |
4786 | 0 | if (color_channels && |
4787 | 0 | tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > |
4788 | 0 | color_channels) |
4789 | 0 | { |
4790 | 0 | uint16_t old_extrasamples; |
4791 | 0 | uint16_t *new_sampleinfo; |
4792 | |
|
4793 | 0 | TIFFWarningExtR( |
4794 | 0 | tif, module, |
4795 | 0 | "Sum of Photometric type-related " |
4796 | 0 | "color channels and ExtraSamples doesn't match SamplesPerPixel. " |
4797 | 0 | "Defining non-color channels as ExtraSamples."); |
4798 | |
|
4799 | 0 | old_extrasamples = tif->tif_dir.td_extrasamples; |
4800 | 0 | tif->tif_dir.td_extrasamples = |
4801 | 0 | (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels); |
4802 | | |
4803 | | // sampleinfo should contain information relative to these new extra |
4804 | | // samples |
4805 | 0 | new_sampleinfo = (uint16_t *)_TIFFcallocExt( |
4806 | 0 | tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t)); |
4807 | 0 | if (!new_sampleinfo) |
4808 | 0 | { |
4809 | 0 | TIFFErrorExtR(tif, module, |
4810 | 0 | "Failed to allocate memory for " |
4811 | 0 | "temporary new sampleinfo array " |
4812 | 0 | "(%" PRIu16 " 16 bit elements)", |
4813 | 0 | tif->tif_dir.td_extrasamples); |
4814 | 0 | goto bad; |
4815 | 0 | } |
4816 | | |
4817 | 0 | if (old_extrasamples > 0) |
4818 | 0 | memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, |
4819 | 0 | old_extrasamples * sizeof(uint16_t)); |
4820 | 0 | _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo, |
4821 | 0 | tif->tif_dir.td_extrasamples); |
4822 | 0 | _TIFFfreeExt(tif, new_sampleinfo); |
4823 | 0 | } |
4824 | | |
4825 | | /* |
4826 | | * Verify Palette image has a Colormap. |
4827 | | */ |
4828 | 0 | if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE && |
4829 | 0 | !TIFFFieldSet(tif, FIELD_COLORMAP)) |
4830 | 0 | { |
4831 | 0 | if (tif->tif_dir.td_bitspersample >= 8 && |
4832 | 0 | tif->tif_dir.td_samplesperpixel == 3) |
4833 | 0 | tif->tif_dir.td_photometric = PHOTOMETRIC_RGB; |
4834 | 0 | else if (tif->tif_dir.td_bitspersample >= 8) |
4835 | 0 | tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK; |
4836 | 0 | else |
4837 | 0 | { |
4838 | 0 | MissingRequired(tif, "Colormap"); |
4839 | 0 | goto bad; |
4840 | 0 | } |
4841 | 0 | } |
4842 | | /* |
4843 | | * OJPEG hack: |
4844 | | * We do no further messing with strip/tile offsets/bytecounts in OJPEG |
4845 | | * TIFFs |
4846 | | */ |
4847 | 0 | if (tif->tif_dir.td_compression != COMPRESSION_OJPEG) |
4848 | 0 | { |
4849 | | /* |
4850 | | * Attempt to deal with a missing StripByteCounts tag. |
4851 | | */ |
4852 | 0 | if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) |
4853 | 0 | { |
4854 | | /* |
4855 | | * Some manufacturers violate the spec by not giving |
4856 | | * the size of the strips. In this case, assume there |
4857 | | * is one uncompressed strip of data. |
4858 | | */ |
4859 | 0 | if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && |
4860 | 0 | tif->tif_dir.td_nstrips > 1) || |
4861 | 0 | (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE && |
4862 | 0 | tif->tif_dir.td_nstrips != |
4863 | 0 | (uint32_t)tif->tif_dir.td_samplesperpixel)) |
4864 | 0 | { |
4865 | 0 | MissingRequired(tif, "StripByteCounts"); |
4866 | 0 | goto bad; |
4867 | 0 | } |
4868 | 0 | TIFFWarningExtR( |
4869 | 0 | tif, module, |
4870 | 0 | "TIFF directory is missing required " |
4871 | 0 | "\"StripByteCounts\" field, calculating from imagelength"); |
4872 | 0 | if (EstimateStripByteCounts(tif, dir, dircount) < 0) |
4873 | 0 | goto bad; |
4874 | 0 | } |
4875 | 0 | else if (tif->tif_dir.td_nstrips == 1 && |
4876 | 0 | !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif)) |
4877 | 0 | { |
4878 | | /* |
4879 | | * XXX: Plexus (and others) sometimes give a value of |
4880 | | * zero for a tag when they don't know what the |
4881 | | * correct value is! Try and handle the simple case |
4882 | | * of estimating the size of a one strip image. |
4883 | | */ |
4884 | 0 | TIFFWarningExtR(tif, module, |
4885 | 0 | "Bogus \"StripByteCounts\" field, ignoring and " |
4886 | 0 | "calculating from imagelength"); |
4887 | 0 | if (EstimateStripByteCounts(tif, dir, dircount) < 0) |
4888 | 0 | goto bad; |
4889 | 0 | } |
4890 | 0 | else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && |
4891 | 0 | tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && |
4892 | 0 | tif->tif_dir.td_nstrips > 2 && |
4893 | 0 | tif->tif_dir.td_compression == COMPRESSION_NONE && |
4894 | 0 | TIFFGetStrileByteCount(tif, 0) != |
4895 | 0 | TIFFGetStrileByteCount(tif, 1) && |
4896 | 0 | TIFFGetStrileByteCount(tif, 0) != 0 && |
4897 | 0 | TIFFGetStrileByteCount(tif, 1) != 0) |
4898 | 0 | { |
4899 | | /* |
4900 | | * XXX: Some vendors fill StripByteCount array with |
4901 | | * absolutely wrong values (it can be equal to |
4902 | | * StripOffset array, for example). Catch this case |
4903 | | * here. |
4904 | | * |
4905 | | * We avoid this check if deferring strile loading |
4906 | | * as it would always force us to load the strip/tile |
4907 | | * information. |
4908 | | */ |
4909 | 0 | TIFFWarningExtR(tif, module, |
4910 | 0 | "Wrong \"StripByteCounts\" field, ignoring and " |
4911 | 0 | "calculating from imagelength"); |
4912 | 0 | if (EstimateStripByteCounts(tif, dir, dircount) < 0) |
4913 | 0 | goto bad; |
4914 | 0 | } |
4915 | 0 | } |
4916 | 0 | if (dir) |
4917 | 0 | { |
4918 | 0 | _TIFFfreeExt(tif, dir); |
4919 | 0 | dir = NULL; |
4920 | 0 | } |
4921 | 0 | if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE)) |
4922 | 0 | { |
4923 | 0 | if (tif->tif_dir.td_bitspersample >= 16) |
4924 | 0 | tif->tif_dir.td_maxsamplevalue = 0xFFFF; |
4925 | 0 | else |
4926 | 0 | tif->tif_dir.td_maxsamplevalue = |
4927 | 0 | (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1); |
4928 | 0 | } |
4929 | |
|
4930 | | #ifdef STRIPBYTECOUNTSORTED_UNUSED |
4931 | | /* |
4932 | | * XXX: We can optimize checking for the strip bounds using the sorted |
4933 | | * bytecounts array. See also comments for TIFFAppendToStrip() |
4934 | | * function in tif_write.c. |
4935 | | */ |
4936 | | if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) |
4937 | | { |
4938 | | uint32_t strip; |
4939 | | |
4940 | | tif->tif_dir.td_stripbytecountsorted = 1; |
4941 | | for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) |
4942 | | { |
4943 | | if (TIFFGetStrileOffset(tif, strip - 1) > |
4944 | | TIFFGetStrileOffset(tif, strip)) |
4945 | | { |
4946 | | tif->tif_dir.td_stripbytecountsorted = 0; |
4947 | | break; |
4948 | | } |
4949 | | } |
4950 | | } |
4951 | | #endif |
4952 | | |
4953 | | /* |
4954 | | * An opportunity for compression mode dependent tag fixup |
4955 | | */ |
4956 | 0 | (*tif->tif_fixuptags)(tif); |
4957 | | |
4958 | | /* |
4959 | | * Some manufacturers make life difficult by writing |
4960 | | * large amounts of uncompressed data as a single strip. |
4961 | | * This is contrary to the recommendations of the spec. |
4962 | | * The following makes an attempt at breaking such images |
4963 | | * into strips closer to the recommended 8k bytes. A |
4964 | | * side effect, however, is that the RowsPerStrip tag |
4965 | | * value may be changed. |
4966 | | */ |
4967 | 0 | if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) && |
4968 | 0 | (tif->tif_dir.td_nstrips == 1) && |
4969 | 0 | (tif->tif_dir.td_compression == COMPRESSION_NONE) && |
4970 | 0 | ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP)) |
4971 | 0 | { |
4972 | 0 | ChopUpSingleUncompressedStrip(tif); |
4973 | 0 | } |
4974 | | |
4975 | | /* There are also uncompressed striped files with strips larger than */ |
4976 | | /* 2 GB, which make them unfriendly with a lot of code. If possible, */ |
4977 | | /* try to expose smaller "virtual" strips. */ |
4978 | 0 | if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG && |
4979 | 0 | tif->tif_dir.td_compression == COMPRESSION_NONE && |
4980 | 0 | (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP && |
4981 | 0 | TIFFStripSize64(tif) > 0x7FFFFFFFUL) |
4982 | 0 | { |
4983 | 0 | TryChopUpUncompressedBigTiff(tif); |
4984 | 0 | } |
4985 | | |
4986 | | /* |
4987 | | * Clear the dirty directory flag. |
4988 | | */ |
4989 | 0 | tif->tif_flags &= ~TIFF_DIRTYDIRECT; |
4990 | 0 | tif->tif_flags &= ~TIFF_DIRTYSTRIP; |
4991 | | |
4992 | | /* |
4993 | | * Reinitialize i/o since we are starting on a new directory. |
4994 | | */ |
4995 | 0 | tif->tif_row = (uint32_t)-1; |
4996 | 0 | tif->tif_curstrip = (uint32_t)-1; |
4997 | 0 | tif->tif_col = (uint32_t)-1; |
4998 | 0 | tif->tif_curtile = (uint32_t)-1; |
4999 | 0 | tif->tif_tilesize = (tmsize_t)-1; |
5000 | |
|
5001 | 0 | tif->tif_scanlinesize = TIFFScanlineSize(tif); |
5002 | 0 | if (!tif->tif_scanlinesize) |
5003 | 0 | { |
5004 | 0 | TIFFErrorExtR(tif, module, "Cannot handle zero scanline size"); |
5005 | 0 | return (0); |
5006 | 0 | } |
5007 | | |
5008 | 0 | if (isTiled(tif)) |
5009 | 0 | { |
5010 | 0 | tif->tif_tilesize = TIFFTileSize(tif); |
5011 | 0 | if (!tif->tif_tilesize) |
5012 | 0 | { |
5013 | 0 | TIFFErrorExtR(tif, module, "Cannot handle zero tile size"); |
5014 | 0 | return (0); |
5015 | 0 | } |
5016 | 0 | } |
5017 | 0 | else |
5018 | 0 | { |
5019 | 0 | if (!TIFFStripSize(tif)) |
5020 | 0 | { |
5021 | 0 | TIFFErrorExtR(tif, module, "Cannot handle zero strip size"); |
5022 | 0 | return (0); |
5023 | 0 | } |
5024 | 0 | } |
5025 | 0 | return (1); |
5026 | 0 | bad: |
5027 | 0 | if (dir) |
5028 | 0 | _TIFFfreeExt(tif, dir); |
5029 | 0 | return (0); |
5030 | 0 | } |
5031 | | |
5032 | | static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir, |
5033 | | uint16_t dircount) |
5034 | 0 | { |
5035 | 0 | static const char module[] = "TIFFReadDirectoryCheckOrder"; |
5036 | 0 | uint16_t m; |
5037 | 0 | uint16_t n; |
5038 | 0 | TIFFDirEntry *o; |
5039 | 0 | m = 0; |
5040 | 0 | for (n = 0, o = dir; n < dircount; n++, o++) |
5041 | 0 | { |
5042 | 0 | if (o->tdir_tag < m) |
5043 | 0 | { |
5044 | 0 | TIFFWarningExtR(tif, module, |
5045 | 0 | "Invalid TIFF directory; tags are not sorted in " |
5046 | 0 | "ascending order"); |
5047 | 0 | break; |
5048 | 0 | } |
5049 | 0 | m = o->tdir_tag + 1; |
5050 | 0 | } |
5051 | 0 | } |
5052 | | |
5053 | | static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir, |
5054 | | uint16_t dircount, |
5055 | | uint16_t tagid) |
5056 | 0 | { |
5057 | 0 | TIFFDirEntry *m; |
5058 | 0 | uint16_t n; |
5059 | 0 | (void)tif; |
5060 | 0 | for (m = dir, n = 0; n < dircount; m++, n++) |
5061 | 0 | { |
5062 | 0 | if (m->tdir_tag == tagid) |
5063 | 0 | return (m); |
5064 | 0 | } |
5065 | 0 | return (0); |
5066 | 0 | } |
5067 | | |
5068 | | static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid, |
5069 | | uint32_t *fii) |
5070 | 0 | { |
5071 | 0 | int32_t ma, mb, mc; |
5072 | 0 | ma = -1; |
5073 | 0 | mc = (int32_t)tif->tif_nfields; |
5074 | 0 | while (1) |
5075 | 0 | { |
5076 | 0 | if (ma + 1 == mc) |
5077 | 0 | { |
5078 | 0 | *fii = FAILED_FII; |
5079 | 0 | return; |
5080 | 0 | } |
5081 | 0 | mb = (ma + mc) / 2; |
5082 | 0 | if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid) |
5083 | 0 | break; |
5084 | 0 | if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid) |
5085 | 0 | ma = mb; |
5086 | 0 | else |
5087 | 0 | mc = mb; |
5088 | 0 | } |
5089 | 0 | while (1) |
5090 | 0 | { |
5091 | 0 | if (mb == 0) |
5092 | 0 | break; |
5093 | 0 | if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid) |
5094 | 0 | break; |
5095 | 0 | mb--; |
5096 | 0 | } |
5097 | 0 | *fii = mb; |
5098 | 0 | } |
5099 | | |
5100 | | /* |
5101 | | * Read custom directory from the arbitrary offset. |
5102 | | * The code is very similar to TIFFReadDirectory(). |
5103 | | */ |
5104 | | int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff, |
5105 | | const TIFFFieldArray *infoarray) |
5106 | 0 | { |
5107 | 0 | static const char module[] = "TIFFReadCustomDirectory"; |
5108 | 0 | TIFFDirEntry *dir; |
5109 | 0 | uint16_t dircount; |
5110 | 0 | TIFFDirEntry *dp; |
5111 | 0 | uint16_t di; |
5112 | 0 | const TIFFField *fip; |
5113 | 0 | uint32_t fii; |
5114 | 0 | (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */ |
5115 | 0 | _TIFFSetupFields(tif, infoarray); |
5116 | 0 | dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL); |
5117 | 0 | if (!dircount) |
5118 | 0 | { |
5119 | 0 | TIFFErrorExtR(tif, module, |
5120 | 0 | "Failed to read custom directory at offset %" PRIu64, |
5121 | 0 | diroff); |
5122 | 0 | return 0; |
5123 | 0 | } |
5124 | 0 | TIFFFreeDirectory(tif); |
5125 | 0 | _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory)); |
5126 | 0 | TIFFReadDirectoryCheckOrder(tif, dir, dircount); |
5127 | 0 | for (di = 0, dp = dir; di < dircount; di++, dp++) |
5128 | 0 | { |
5129 | 0 | TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); |
5130 | 0 | if (fii == FAILED_FII) |
5131 | 0 | { |
5132 | 0 | TIFFWarningExtR(tif, module, |
5133 | 0 | "Unknown field with tag %" PRIu16 " (0x%" PRIx16 |
5134 | 0 | ") encountered", |
5135 | 0 | dp->tdir_tag, dp->tdir_tag); |
5136 | 0 | if (!_TIFFMergeFields( |
5137 | 0 | tif, |
5138 | 0 | _TIFFCreateAnonField(tif, dp->tdir_tag, |
5139 | 0 | (TIFFDataType)dp->tdir_type), |
5140 | 0 | 1)) |
5141 | 0 | { |
5142 | 0 | TIFFWarningExtR(tif, module, |
5143 | 0 | "Registering anonymous field with tag %" PRIu16 |
5144 | 0 | " (0x%" PRIx16 ") failed", |
5145 | 0 | dp->tdir_tag, dp->tdir_tag); |
5146 | 0 | dp->tdir_ignore = TRUE; |
5147 | 0 | } |
5148 | 0 | else |
5149 | 0 | { |
5150 | 0 | TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); |
5151 | 0 | assert(fii != FAILED_FII); |
5152 | 0 | } |
5153 | 0 | } |
5154 | 0 | if (!dp->tdir_ignore) |
5155 | 0 | { |
5156 | 0 | fip = tif->tif_fields[fii]; |
5157 | 0 | if (fip->field_bit == FIELD_IGNORE) |
5158 | 0 | dp->tdir_ignore = TRUE; |
5159 | 0 | else |
5160 | 0 | { |
5161 | | /* check data type */ |
5162 | 0 | while ((fip->field_type != TIFF_ANY) && |
5163 | 0 | (fip->field_type != dp->tdir_type)) |
5164 | 0 | { |
5165 | 0 | fii++; |
5166 | 0 | if ((fii == tif->tif_nfields) || |
5167 | 0 | (tif->tif_fields[fii]->field_tag != |
5168 | 0 | (uint32_t)dp->tdir_tag)) |
5169 | 0 | { |
5170 | 0 | fii = 0xFFFF; |
5171 | 0 | break; |
5172 | 0 | } |
5173 | 0 | fip = tif->tif_fields[fii]; |
5174 | 0 | } |
5175 | 0 | if (fii == 0xFFFF) |
5176 | 0 | { |
5177 | 0 | TIFFWarningExtR(tif, module, |
5178 | 0 | "Wrong data type %" PRIu16 |
5179 | 0 | " for \"%s\"; tag ignored", |
5180 | 0 | dp->tdir_type, fip->field_name); |
5181 | 0 | dp->tdir_ignore = TRUE; |
5182 | 0 | } |
5183 | 0 | else |
5184 | 0 | { |
5185 | | /* check count if known in advance */ |
5186 | 0 | if ((fip->field_readcount != TIFF_VARIABLE) && |
5187 | 0 | (fip->field_readcount != TIFF_VARIABLE2)) |
5188 | 0 | { |
5189 | 0 | uint32_t expected; |
5190 | 0 | if (fip->field_readcount == TIFF_SPP) |
5191 | 0 | expected = |
5192 | 0 | (uint32_t)tif->tif_dir.td_samplesperpixel; |
5193 | 0 | else |
5194 | 0 | expected = (uint32_t)fip->field_readcount; |
5195 | 0 | if (!CheckDirCount(tif, dp, expected)) |
5196 | 0 | dp->tdir_ignore = TRUE; |
5197 | 0 | } |
5198 | 0 | } |
5199 | 0 | } |
5200 | 0 | if (!dp->tdir_ignore) |
5201 | 0 | { |
5202 | 0 | switch (dp->tdir_tag) |
5203 | 0 | { |
5204 | 0 | case EXIFTAG_SUBJECTDISTANCE: |
5205 | 0 | if (!TIFFFieldIsAnonymous(fip)) |
5206 | 0 | { |
5207 | | /* should only be called on a Exif directory */ |
5208 | | /* when exifFields[] is active */ |
5209 | 0 | (void)TIFFFetchSubjectDistance(tif, dp); |
5210 | 0 | } |
5211 | 0 | else |
5212 | 0 | { |
5213 | 0 | (void)TIFFFetchNormalTag(tif, dp, TRUE); |
5214 | 0 | } |
5215 | 0 | break; |
5216 | 0 | default: |
5217 | 0 | (void)TIFFFetchNormalTag(tif, dp, TRUE); |
5218 | 0 | break; |
5219 | 0 | } |
5220 | 0 | } /*-- if (!dp->tdir_ignore) */ |
5221 | 0 | } |
5222 | 0 | } |
5223 | | /* To be able to return from SubIFD or custom-IFD to main-IFD */ |
5224 | 0 | tif->tif_setdirectory_force_absolute = TRUE; |
5225 | 0 | if (dir) |
5226 | 0 | _TIFFfreeExt(tif, dir); |
5227 | 0 | return 1; |
5228 | 0 | } |
5229 | | |
5230 | | /* |
5231 | | * EXIF is important special case of custom IFD, so we have a special |
5232 | | * function to read it. |
5233 | | */ |
5234 | | int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff) |
5235 | 0 | { |
5236 | 0 | const TIFFFieldArray *exifFieldArray; |
5237 | 0 | exifFieldArray = _TIFFGetExifFields(); |
5238 | 0 | return TIFFReadCustomDirectory(tif, diroff, exifFieldArray); |
5239 | 0 | } |
5240 | | |
5241 | | /* |
5242 | | *--: EXIF-GPS custom directory reading as another special case of custom IFD. |
5243 | | */ |
5244 | | int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff) |
5245 | 0 | { |
5246 | 0 | const TIFFFieldArray *gpsFieldArray; |
5247 | 0 | gpsFieldArray = _TIFFGetGpsFields(); |
5248 | 0 | return TIFFReadCustomDirectory(tif, diroff, gpsFieldArray); |
5249 | 0 | } |
5250 | | |
5251 | | static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir, |
5252 | | uint16_t dircount) |
5253 | 0 | { |
5254 | 0 | static const char module[] = "EstimateStripByteCounts"; |
5255 | |
|
5256 | 0 | TIFFDirEntry *dp; |
5257 | 0 | TIFFDirectory *td = &tif->tif_dir; |
5258 | 0 | uint32_t strip; |
5259 | | |
5260 | | /* Do not try to load stripbytecount as we will compute it */ |
5261 | 0 | if (!_TIFFFillStrilesInternal(tif, 0)) |
5262 | 0 | return -1; |
5263 | | |
5264 | 0 | if (td->td_stripbytecount_p) |
5265 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
5266 | 0 | td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc( |
5267 | 0 | tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array"); |
5268 | 0 | if (td->td_stripbytecount_p == NULL) |
5269 | 0 | return -1; |
5270 | | |
5271 | 0 | if (td->td_compression != COMPRESSION_NONE) |
5272 | 0 | { |
5273 | 0 | uint64_t space; |
5274 | 0 | uint64_t filesize; |
5275 | 0 | uint16_t n; |
5276 | 0 | filesize = TIFFGetFileSize(tif); |
5277 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5278 | 0 | space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4; |
5279 | 0 | else |
5280 | 0 | space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8; |
5281 | | /* calculate amount of space used by indirect values */ |
5282 | 0 | for (dp = dir, n = dircount; n > 0; n--, dp++) |
5283 | 0 | { |
5284 | 0 | uint32_t typewidth; |
5285 | 0 | uint64_t datasize; |
5286 | 0 | typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type); |
5287 | 0 | if (typewidth == 0) |
5288 | 0 | { |
5289 | 0 | TIFFErrorExtR( |
5290 | 0 | tif, module, |
5291 | 0 | "Cannot determine size of unknown tag type %" PRIu16, |
5292 | 0 | dp->tdir_type); |
5293 | 0 | return -1; |
5294 | 0 | } |
5295 | 0 | if (dp->tdir_count > UINT64_MAX / typewidth) |
5296 | 0 | return -1; |
5297 | 0 | datasize = (uint64_t)typewidth * dp->tdir_count; |
5298 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5299 | 0 | { |
5300 | 0 | if (datasize <= 4) |
5301 | 0 | datasize = 0; |
5302 | 0 | } |
5303 | 0 | else |
5304 | 0 | { |
5305 | 0 | if (datasize <= 8) |
5306 | 0 | datasize = 0; |
5307 | 0 | } |
5308 | 0 | if (space > UINT64_MAX - datasize) |
5309 | 0 | return -1; |
5310 | 0 | space += datasize; |
5311 | 0 | } |
5312 | 0 | if (filesize < space) |
5313 | | /* we should perhaps return in error ? */ |
5314 | 0 | space = filesize; |
5315 | 0 | else |
5316 | 0 | space = filesize - space; |
5317 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
5318 | 0 | space /= td->td_samplesperpixel; |
5319 | 0 | for (strip = 0; strip < td->td_nstrips; strip++) |
5320 | 0 | td->td_stripbytecount_p[strip] = space; |
5321 | | /* |
5322 | | * This gross hack handles the case were the offset to |
5323 | | * the last strip is past the place where we think the strip |
5324 | | * should begin. Since a strip of data must be contiguous, |
5325 | | * it's safe to assume that we've overestimated the amount |
5326 | | * of data in the strip and trim this number back accordingly. |
5327 | | */ |
5328 | 0 | strip--; |
5329 | 0 | if (td->td_stripoffset_p[strip] > |
5330 | 0 | UINT64_MAX - td->td_stripbytecount_p[strip]) |
5331 | 0 | return -1; |
5332 | 0 | if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] > |
5333 | 0 | filesize) |
5334 | 0 | { |
5335 | 0 | if (td->td_stripoffset_p[strip] >= filesize) |
5336 | 0 | { |
5337 | | /* Not sure what we should in that case... */ |
5338 | 0 | td->td_stripbytecount_p[strip] = 0; |
5339 | 0 | } |
5340 | 0 | else |
5341 | 0 | { |
5342 | 0 | td->td_stripbytecount_p[strip] = |
5343 | 0 | filesize - td->td_stripoffset_p[strip]; |
5344 | 0 | } |
5345 | 0 | } |
5346 | 0 | } |
5347 | 0 | else if (isTiled(tif)) |
5348 | 0 | { |
5349 | 0 | uint64_t bytespertile = TIFFTileSize64(tif); |
5350 | |
|
5351 | 0 | for (strip = 0; strip < td->td_nstrips; strip++) |
5352 | 0 | td->td_stripbytecount_p[strip] = bytespertile; |
5353 | 0 | } |
5354 | 0 | else |
5355 | 0 | { |
5356 | 0 | uint64_t rowbytes = TIFFScanlineSize64(tif); |
5357 | 0 | uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage; |
5358 | 0 | for (strip = 0; strip < td->td_nstrips; strip++) |
5359 | 0 | { |
5360 | 0 | if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes) |
5361 | 0 | return -1; |
5362 | 0 | td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip; |
5363 | 0 | } |
5364 | 0 | } |
5365 | 0 | TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); |
5366 | 0 | if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) |
5367 | 0 | td->td_rowsperstrip = td->td_imagelength; |
5368 | 0 | return 1; |
5369 | 0 | } |
5370 | | |
5371 | | static void MissingRequired(TIFF *tif, const char *tagname) |
5372 | 0 | { |
5373 | 0 | static const char module[] = "MissingRequired"; |
5374 | |
|
5375 | 0 | TIFFErrorExtR(tif, module, |
5376 | 0 | "TIFF directory is missing required \"%s\" field", tagname); |
5377 | 0 | } |
5378 | | |
5379 | | static unsigned long hashFuncOffsetToNumber(const void *elt) |
5380 | 0 | { |
5381 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber = |
5382 | 0 | (const TIFFOffsetAndDirNumber *)elt; |
5383 | 0 | const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^ |
5384 | 0 | ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU); |
5385 | 0 | return hash; |
5386 | 0 | } |
5387 | | |
5388 | | static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2) |
5389 | 0 | { |
5390 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber1 = |
5391 | 0 | (const TIFFOffsetAndDirNumber *)elt1; |
5392 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber2 = |
5393 | 0 | (const TIFFOffsetAndDirNumber *)elt2; |
5394 | 0 | return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset; |
5395 | 0 | } |
5396 | | |
5397 | | static unsigned long hashFuncNumberToOffset(const void *elt) |
5398 | 0 | { |
5399 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber = |
5400 | 0 | (const TIFFOffsetAndDirNumber *)elt; |
5401 | 0 | return offsetAndDirNumber->dirNumber; |
5402 | 0 | } |
5403 | | |
5404 | | static bool equalFuncNumberToOffset(const void *elt1, const void *elt2) |
5405 | 0 | { |
5406 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber1 = |
5407 | 0 | (const TIFFOffsetAndDirNumber *)elt1; |
5408 | 0 | const TIFFOffsetAndDirNumber *offsetAndDirNumber2 = |
5409 | 0 | (const TIFFOffsetAndDirNumber *)elt2; |
5410 | 0 | return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber; |
5411 | 0 | } |
5412 | | |
5413 | | /* |
5414 | | * Check the directory number and offset against the list of already seen |
5415 | | * directory numbers and offsets. This is a trick to prevent IFD looping. |
5416 | | * The one can create TIFF file with looped directory pointers. We will |
5417 | | * maintain a list of already seen directories and check every IFD offset |
5418 | | * and its IFD number against that list. However, the offset of an IFD number |
5419 | | * can change - e.g. when writing updates to file. |
5420 | | * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered, |
5421 | | * or an error has occurred. |
5422 | | */ |
5423 | | int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff) |
5424 | 0 | { |
5425 | 0 | if (diroff == 0) /* no more directories */ |
5426 | 0 | return 0; |
5427 | | |
5428 | 0 | if (tif->tif_map_dir_offset_to_number == NULL) |
5429 | 0 | { |
5430 | 0 | tif->tif_map_dir_offset_to_number = TIFFHashSetNew( |
5431 | 0 | hashFuncOffsetToNumber, equalFuncOffsetToNumber, free); |
5432 | 0 | if (tif->tif_map_dir_offset_to_number == NULL) |
5433 | 0 | { |
5434 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5435 | 0 | "Not enough memory"); |
5436 | 0 | return 1; |
5437 | 0 | } |
5438 | 0 | } |
5439 | | |
5440 | 0 | if (tif->tif_map_dir_number_to_offset == NULL) |
5441 | 0 | { |
5442 | | /* No free callback for this map, as it shares the same items as |
5443 | | * tif->tif_map_dir_offset_to_number. */ |
5444 | 0 | tif->tif_map_dir_number_to_offset = TIFFHashSetNew( |
5445 | 0 | hashFuncNumberToOffset, equalFuncNumberToOffset, NULL); |
5446 | 0 | if (tif->tif_map_dir_number_to_offset == NULL) |
5447 | 0 | { |
5448 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5449 | 0 | "Not enough memory"); |
5450 | 0 | return 1; |
5451 | 0 | } |
5452 | 0 | } |
5453 | | |
5454 | | /* Check if offset is already in the list: |
5455 | | * - yes: check, if offset is at the same IFD number - if not, it is an IFD |
5456 | | * loop |
5457 | | * - no: add to list or update offset at that IFD number |
5458 | | */ |
5459 | 0 | TIFFOffsetAndDirNumber entry; |
5460 | 0 | entry.offset = diroff; |
5461 | 0 | entry.dirNumber = dirn; |
5462 | |
|
5463 | 0 | TIFFOffsetAndDirNumber *foundEntry = |
5464 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5465 | 0 | tif->tif_map_dir_offset_to_number, &entry); |
5466 | 0 | if (foundEntry) |
5467 | 0 | { |
5468 | 0 | if (foundEntry->dirNumber == dirn) |
5469 | 0 | { |
5470 | 0 | return 1; |
5471 | 0 | } |
5472 | 0 | else |
5473 | 0 | { |
5474 | 0 | TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5475 | 0 | "TIFF directory %d has IFD looping to directory %u " |
5476 | 0 | "at offset 0x%" PRIx64 " (%" PRIu64 ")", |
5477 | 0 | (int)dirn - 1, foundEntry->dirNumber, diroff, |
5478 | 0 | diroff); |
5479 | 0 | return 0; |
5480 | 0 | } |
5481 | 0 | } |
5482 | | |
5483 | | /* Check if offset of an IFD has been changed and update offset of that IFD |
5484 | | * number. */ |
5485 | 0 | foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5486 | 0 | tif->tif_map_dir_number_to_offset, &entry); |
5487 | 0 | if (foundEntry) |
5488 | 0 | { |
5489 | 0 | if (foundEntry->offset != diroff) |
5490 | 0 | { |
5491 | 0 | TIFFOffsetAndDirNumber entryOld; |
5492 | 0 | entryOld.offset = foundEntry->offset; |
5493 | 0 | entryOld.dirNumber = dirn; |
5494 | | /* We must remove first from tif_map_dir_number_to_offset as the */ |
5495 | | /* entry is owned (and thus freed) by */ |
5496 | | /* tif_map_dir_offset_to_number */ |
5497 | 0 | TIFFOffsetAndDirNumber *foundEntryOld = |
5498 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5499 | 0 | tif->tif_map_dir_number_to_offset, &entryOld); |
5500 | 0 | if (foundEntryOld) |
5501 | 0 | { |
5502 | 0 | TIFFHashSetRemove(tif->tif_map_dir_number_to_offset, |
5503 | 0 | foundEntryOld); |
5504 | 0 | } |
5505 | 0 | foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5506 | 0 | tif->tif_map_dir_offset_to_number, &entryOld); |
5507 | 0 | if (foundEntryOld) |
5508 | 0 | { |
5509 | 0 | TIFFHashSetRemove(tif->tif_map_dir_offset_to_number, |
5510 | 0 | foundEntryOld); |
5511 | 0 | } |
5512 | |
|
5513 | 0 | TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc( |
5514 | 0 | sizeof(TIFFOffsetAndDirNumber)); |
5515 | 0 | if (entryPtr == NULL) |
5516 | 0 | { |
5517 | 0 | return 0; |
5518 | 0 | } |
5519 | | |
5520 | | /* Add IFD offset and dirn to IFD directory list */ |
5521 | 0 | *entryPtr = entry; |
5522 | |
|
5523 | 0 | if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr)) |
5524 | 0 | { |
5525 | 0 | TIFFErrorExtR( |
5526 | 0 | tif, "_TIFFCheckDirNumberAndOffset", |
5527 | 0 | "Insertion in tif_map_dir_offset_to_number failed"); |
5528 | 0 | return 0; |
5529 | 0 | } |
5530 | 0 | if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr)) |
5531 | 0 | { |
5532 | 0 | TIFFErrorExtR( |
5533 | 0 | tif, "_TIFFCheckDirNumberAndOffset", |
5534 | 0 | "Insertion in tif_map_dir_number_to_offset failed"); |
5535 | 0 | return 0; |
5536 | 0 | } |
5537 | 0 | } |
5538 | 0 | return 1; |
5539 | 0 | } |
5540 | | |
5541 | | /* Arbitrary (hopefully big enough) limit */ |
5542 | 0 | if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >= |
5543 | 0 | TIFF_MAX_DIR_COUNT) |
5544 | 0 | { |
5545 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5546 | 0 | "Cannot handle more than %u TIFF directories", |
5547 | 0 | TIFF_MAX_DIR_COUNT); |
5548 | 0 | return 0; |
5549 | 0 | } |
5550 | | |
5551 | 0 | TIFFOffsetAndDirNumber *entryPtr = |
5552 | 0 | (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber)); |
5553 | 0 | if (entryPtr == NULL) |
5554 | 0 | { |
5555 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5556 | 0 | "malloc(sizeof(TIFFOffsetAndDirNumber)) failed"); |
5557 | 0 | return 0; |
5558 | 0 | } |
5559 | | |
5560 | | /* Add IFD offset and dirn to IFD directory list */ |
5561 | 0 | *entryPtr = entry; |
5562 | |
|
5563 | 0 | if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr)) |
5564 | 0 | { |
5565 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5566 | 0 | "Insertion in tif_map_dir_offset_to_number failed"); |
5567 | 0 | return 0; |
5568 | 0 | } |
5569 | 0 | if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr)) |
5570 | 0 | { |
5571 | 0 | TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset", |
5572 | 0 | "Insertion in tif_map_dir_number_to_offset failed"); |
5573 | 0 | return 0; |
5574 | 0 | } |
5575 | | |
5576 | 0 | return 1; |
5577 | 0 | } /* --- _TIFFCheckDirNumberAndOffset() ---*/ |
5578 | | |
5579 | | /* |
5580 | | * Retrieve the matching IFD directory number of a given IFD offset |
5581 | | * from the list of directories already seen. |
5582 | | * Returns 1 if the offset was in the list and the directory number |
5583 | | * can be returned. |
5584 | | * Otherwise returns 0 or if an error occurred. |
5585 | | */ |
5586 | | int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn) |
5587 | 0 | { |
5588 | 0 | if (diroff == 0) /* no more directories */ |
5589 | 0 | return 0; |
5590 | | |
5591 | | /* Check if offset is already in the list and return matching directory |
5592 | | * number. Otherwise update IFD list using TIFFNumberOfDirectories() and |
5593 | | * search again in IFD list. |
5594 | | */ |
5595 | 0 | if (tif->tif_map_dir_offset_to_number == NULL) |
5596 | 0 | return 0; |
5597 | 0 | TIFFOffsetAndDirNumber entry; |
5598 | 0 | entry.offset = diroff; |
5599 | 0 | entry.dirNumber = 0; /* not used */ |
5600 | |
|
5601 | 0 | TIFFOffsetAndDirNumber *foundEntry = |
5602 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5603 | 0 | tif->tif_map_dir_offset_to_number, &entry); |
5604 | 0 | if (foundEntry) |
5605 | 0 | { |
5606 | 0 | *dirn = foundEntry->dirNumber; |
5607 | 0 | return 1; |
5608 | 0 | } |
5609 | | |
5610 | | /* This updates the directory list for all main-IFDs in the file. */ |
5611 | 0 | TIFFNumberOfDirectories(tif); |
5612 | |
|
5613 | 0 | foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5614 | 0 | tif->tif_map_dir_offset_to_number, &entry); |
5615 | 0 | if (foundEntry) |
5616 | 0 | { |
5617 | 0 | *dirn = foundEntry->dirNumber; |
5618 | 0 | return 1; |
5619 | 0 | } |
5620 | | |
5621 | 0 | return 0; |
5622 | 0 | } /*--- _TIFFGetDirNumberFromOffset() ---*/ |
5623 | | |
5624 | | /* |
5625 | | * Retrieve the matching IFD directory offset of a given IFD number |
5626 | | * from the list of directories already seen. |
5627 | | * Returns 1 if the offset was in the list of already seen IFDs and the |
5628 | | * directory offset can be returned. The directory list is not updated. |
5629 | | * Otherwise returns 0 or if an error occurred. |
5630 | | */ |
5631 | | int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff) |
5632 | 0 | { |
5633 | |
|
5634 | 0 | if (tif->tif_map_dir_number_to_offset == NULL) |
5635 | 0 | return 0; |
5636 | 0 | TIFFOffsetAndDirNumber entry; |
5637 | 0 | entry.offset = 0; /* not used */ |
5638 | 0 | entry.dirNumber = dirn; |
5639 | |
|
5640 | 0 | TIFFOffsetAndDirNumber *foundEntry = |
5641 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5642 | 0 | tif->tif_map_dir_number_to_offset, &entry); |
5643 | 0 | if (foundEntry) |
5644 | 0 | { |
5645 | 0 | *diroff = foundEntry->offset; |
5646 | 0 | return 1; |
5647 | 0 | } |
5648 | | |
5649 | 0 | return 0; |
5650 | 0 | } /*--- _TIFFGetOffsetFromDirNumber() ---*/ |
5651 | | |
5652 | | /* |
5653 | | * Remove an entry from the directory list of already seen directories |
5654 | | * by directory offset. |
5655 | | * If an entry is to be removed from the list, it is also okay if the entry |
5656 | | * is not in the list or the list does not exist. |
5657 | | */ |
5658 | | int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff) |
5659 | 0 | { |
5660 | 0 | if (tif->tif_map_dir_offset_to_number == NULL) |
5661 | 0 | return 1; |
5662 | | |
5663 | 0 | TIFFOffsetAndDirNumber entryOld; |
5664 | 0 | entryOld.offset = diroff; |
5665 | 0 | entryOld.dirNumber = 0; |
5666 | | /* We must remove first from tif_map_dir_number_to_offset as the |
5667 | | * entry is owned (and thus freed) by tif_map_dir_offset_to_number. |
5668 | | * However, we need firstly to find the directory number from offset. */ |
5669 | |
|
5670 | 0 | TIFFOffsetAndDirNumber *foundEntryOldOff = |
5671 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5672 | 0 | tif->tif_map_dir_offset_to_number, &entryOld); |
5673 | 0 | if (foundEntryOldOff) |
5674 | 0 | { |
5675 | 0 | entryOld.dirNumber = foundEntryOldOff->dirNumber; |
5676 | 0 | if (tif->tif_map_dir_number_to_offset != NULL) |
5677 | 0 | { |
5678 | 0 | TIFFOffsetAndDirNumber *foundEntryOldDir = |
5679 | 0 | (TIFFOffsetAndDirNumber *)TIFFHashSetLookup( |
5680 | 0 | tif->tif_map_dir_number_to_offset, &entryOld); |
5681 | 0 | if (foundEntryOldDir) |
5682 | 0 | { |
5683 | 0 | TIFFHashSetRemove(tif->tif_map_dir_number_to_offset, |
5684 | 0 | foundEntryOldDir); |
5685 | 0 | TIFFHashSetRemove(tif->tif_map_dir_offset_to_number, |
5686 | 0 | foundEntryOldOff); |
5687 | 0 | return 1; |
5688 | 0 | } |
5689 | 0 | } |
5690 | 0 | else |
5691 | 0 | { |
5692 | 0 | TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset", |
5693 | 0 | "Unexpectedly tif_map_dir_number_to_offset is " |
5694 | 0 | "missing but tif_map_dir_offset_to_number exists."); |
5695 | 0 | return 0; |
5696 | 0 | } |
5697 | 0 | } |
5698 | 0 | return 1; |
5699 | 0 | } /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/ |
5700 | | |
5701 | | /* |
5702 | | * Check the count field of a directory entry against a known value. The |
5703 | | * caller is expected to skip/ignore the tag if there is a mismatch. |
5704 | | */ |
5705 | | static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count) |
5706 | 0 | { |
5707 | 0 | if ((uint64_t)count > dir->tdir_count) |
5708 | 0 | { |
5709 | 0 | const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); |
5710 | 0 | TIFFWarningExtR(tif, tif->tif_name, |
5711 | 0 | "incorrect count for field \"%s\" (%" PRIu64 |
5712 | 0 | ", expecting %" PRIu32 "); tag ignored", |
5713 | 0 | fip ? fip->field_name : "unknown tagname", |
5714 | 0 | dir->tdir_count, count); |
5715 | 0 | return (0); |
5716 | 0 | } |
5717 | 0 | else if ((uint64_t)count < dir->tdir_count) |
5718 | 0 | { |
5719 | 0 | const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); |
5720 | 0 | TIFFWarningExtR(tif, tif->tif_name, |
5721 | 0 | "incorrect count for field \"%s\" (%" PRIu64 |
5722 | 0 | ", expecting %" PRIu32 "); tag trimmed", |
5723 | 0 | fip ? fip->field_name : "unknown tagname", |
5724 | 0 | dir->tdir_count, count); |
5725 | 0 | dir->tdir_count = count; |
5726 | 0 | return (1); |
5727 | 0 | } |
5728 | 0 | return (1); |
5729 | 0 | } |
5730 | | |
5731 | | /* |
5732 | | * Read IFD structure from the specified offset. If the pointer to |
5733 | | * nextdiroff variable has been specified, read it too. Function returns a |
5734 | | * number of fields in the directory or 0 if failed. |
5735 | | */ |
5736 | | static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff, |
5737 | | TIFFDirEntry **pdir, uint64_t *nextdiroff) |
5738 | 0 | { |
5739 | 0 | static const char module[] = "TIFFFetchDirectory"; |
5740 | |
|
5741 | 0 | void *origdir; |
5742 | 0 | uint16_t dircount16; |
5743 | 0 | uint32_t dirsize; |
5744 | 0 | TIFFDirEntry *dir; |
5745 | 0 | uint8_t *ma; |
5746 | 0 | TIFFDirEntry *mb; |
5747 | 0 | uint16_t n; |
5748 | |
|
5749 | 0 | assert(pdir); |
5750 | | |
5751 | 0 | tif->tif_diroff = diroff; |
5752 | 0 | if (nextdiroff) |
5753 | 0 | *nextdiroff = 0; |
5754 | 0 | if (!isMapped(tif)) |
5755 | 0 | { |
5756 | 0 | if (!SeekOK(tif, tif->tif_diroff)) |
5757 | 0 | { |
5758 | 0 | TIFFErrorExtR(tif, module, |
5759 | 0 | "%s: Seek error accessing TIFF directory", |
5760 | 0 | tif->tif_name); |
5761 | 0 | return 0; |
5762 | 0 | } |
5763 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5764 | 0 | { |
5765 | 0 | if (!ReadOK(tif, &dircount16, sizeof(uint16_t))) |
5766 | 0 | { |
5767 | 0 | TIFFErrorExtR(tif, module, |
5768 | 0 | "%s: Can not read TIFF directory count", |
5769 | 0 | tif->tif_name); |
5770 | 0 | return 0; |
5771 | 0 | } |
5772 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5773 | 0 | TIFFSwabShort(&dircount16); |
5774 | 0 | if (dircount16 > 4096) |
5775 | 0 | { |
5776 | 0 | TIFFErrorExtR(tif, module, |
5777 | 0 | "Sanity check on directory count failed, this is " |
5778 | 0 | "probably not a valid IFD offset"); |
5779 | 0 | return 0; |
5780 | 0 | } |
5781 | 0 | dirsize = 12; |
5782 | 0 | } |
5783 | 0 | else |
5784 | 0 | { |
5785 | 0 | uint64_t dircount64; |
5786 | 0 | if (!ReadOK(tif, &dircount64, sizeof(uint64_t))) |
5787 | 0 | { |
5788 | 0 | TIFFErrorExtR(tif, module, |
5789 | 0 | "%s: Can not read TIFF directory count", |
5790 | 0 | tif->tif_name); |
5791 | 0 | return 0; |
5792 | 0 | } |
5793 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5794 | 0 | TIFFSwabLong8(&dircount64); |
5795 | 0 | if (dircount64 > 4096) |
5796 | 0 | { |
5797 | 0 | TIFFErrorExtR(tif, module, |
5798 | 0 | "Sanity check on directory count failed, this is " |
5799 | 0 | "probably not a valid IFD offset"); |
5800 | 0 | return 0; |
5801 | 0 | } |
5802 | 0 | dircount16 = (uint16_t)dircount64; |
5803 | 0 | dirsize = 20; |
5804 | 0 | } |
5805 | 0 | origdir = _TIFFCheckMalloc(tif, dircount16, dirsize, |
5806 | 0 | "to read TIFF directory"); |
5807 | 0 | if (origdir == NULL) |
5808 | 0 | return 0; |
5809 | 0 | if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize))) |
5810 | 0 | { |
5811 | 0 | TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory", |
5812 | 0 | tif->tif_name); |
5813 | 0 | _TIFFfreeExt(tif, origdir); |
5814 | 0 | return 0; |
5815 | 0 | } |
5816 | | /* |
5817 | | * Read offset to next directory for sequential scans if |
5818 | | * needed. |
5819 | | */ |
5820 | 0 | if (nextdiroff) |
5821 | 0 | { |
5822 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5823 | 0 | { |
5824 | 0 | uint32_t nextdiroff32; |
5825 | 0 | if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t))) |
5826 | 0 | nextdiroff32 = 0; |
5827 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5828 | 0 | TIFFSwabLong(&nextdiroff32); |
5829 | 0 | *nextdiroff = nextdiroff32; |
5830 | 0 | } |
5831 | 0 | else |
5832 | 0 | { |
5833 | 0 | if (!ReadOK(tif, nextdiroff, sizeof(uint64_t))) |
5834 | 0 | *nextdiroff = 0; |
5835 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5836 | 0 | TIFFSwabLong8(nextdiroff); |
5837 | 0 | } |
5838 | 0 | } |
5839 | 0 | } |
5840 | 0 | else |
5841 | 0 | { |
5842 | 0 | tmsize_t m; |
5843 | 0 | tmsize_t off; |
5844 | 0 | if (tif->tif_diroff > (uint64_t)INT64_MAX) |
5845 | 0 | { |
5846 | 0 | TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); |
5847 | 0 | return (0); |
5848 | 0 | } |
5849 | 0 | off = (tmsize_t)tif->tif_diroff; |
5850 | | |
5851 | | /* |
5852 | | * Check for integer overflow when validating the dir_off, |
5853 | | * otherwise a very high offset may cause an OOB read and |
5854 | | * crash the client. Make two comparisons instead of |
5855 | | * |
5856 | | * off + sizeof(uint16_t) > tif->tif_size |
5857 | | * |
5858 | | * to avoid overflow. |
5859 | | */ |
5860 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5861 | 0 | { |
5862 | 0 | m = off + sizeof(uint16_t); |
5863 | 0 | if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) || |
5864 | 0 | (m > tif->tif_size)) |
5865 | 0 | { |
5866 | 0 | TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); |
5867 | 0 | return 0; |
5868 | 0 | } |
5869 | 0 | else |
5870 | 0 | { |
5871 | 0 | _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t)); |
5872 | 0 | } |
5873 | 0 | off += sizeof(uint16_t); |
5874 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5875 | 0 | TIFFSwabShort(&dircount16); |
5876 | 0 | if (dircount16 > 4096) |
5877 | 0 | { |
5878 | 0 | TIFFErrorExtR(tif, module, |
5879 | 0 | "Sanity check on directory count failed, this is " |
5880 | 0 | "probably not a valid IFD offset"); |
5881 | 0 | return 0; |
5882 | 0 | } |
5883 | 0 | dirsize = 12; |
5884 | 0 | } |
5885 | 0 | else |
5886 | 0 | { |
5887 | 0 | uint64_t dircount64; |
5888 | 0 | m = off + sizeof(uint64_t); |
5889 | 0 | if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) || |
5890 | 0 | (m > tif->tif_size)) |
5891 | 0 | { |
5892 | 0 | TIFFErrorExtR(tif, module, "Can not read TIFF directory count"); |
5893 | 0 | return 0; |
5894 | 0 | } |
5895 | 0 | else |
5896 | 0 | { |
5897 | 0 | _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t)); |
5898 | 0 | } |
5899 | 0 | off += sizeof(uint64_t); |
5900 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5901 | 0 | TIFFSwabLong8(&dircount64); |
5902 | 0 | if (dircount64 > 4096) |
5903 | 0 | { |
5904 | 0 | TIFFErrorExtR(tif, module, |
5905 | 0 | "Sanity check on directory count failed, this is " |
5906 | 0 | "probably not a valid IFD offset"); |
5907 | 0 | return 0; |
5908 | 0 | } |
5909 | 0 | dircount16 = (uint16_t)dircount64; |
5910 | 0 | dirsize = 20; |
5911 | 0 | } |
5912 | 0 | if (dircount16 == 0) |
5913 | 0 | { |
5914 | 0 | TIFFErrorExtR(tif, module, |
5915 | 0 | "Sanity check on directory count failed, zero tag " |
5916 | 0 | "directories not supported"); |
5917 | 0 | return 0; |
5918 | 0 | } |
5919 | 0 | origdir = _TIFFCheckMalloc(tif, dircount16, dirsize, |
5920 | 0 | "to read TIFF directory"); |
5921 | 0 | if (origdir == NULL) |
5922 | 0 | return 0; |
5923 | 0 | m = off + dircount16 * dirsize; |
5924 | 0 | if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) || |
5925 | 0 | (m > tif->tif_size)) |
5926 | 0 | { |
5927 | 0 | TIFFErrorExtR(tif, module, "Can not read TIFF directory"); |
5928 | 0 | _TIFFfreeExt(tif, origdir); |
5929 | 0 | return 0; |
5930 | 0 | } |
5931 | 0 | else |
5932 | 0 | { |
5933 | 0 | _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize); |
5934 | 0 | } |
5935 | 0 | if (nextdiroff) |
5936 | 0 | { |
5937 | 0 | off += dircount16 * dirsize; |
5938 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5939 | 0 | { |
5940 | 0 | uint32_t nextdiroff32; |
5941 | 0 | m = off + sizeof(uint32_t); |
5942 | 0 | if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) || |
5943 | 0 | (m > tif->tif_size)) |
5944 | 0 | nextdiroff32 = 0; |
5945 | 0 | else |
5946 | 0 | _TIFFmemcpy(&nextdiroff32, tif->tif_base + off, |
5947 | 0 | sizeof(uint32_t)); |
5948 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5949 | 0 | TIFFSwabLong(&nextdiroff32); |
5950 | 0 | *nextdiroff = nextdiroff32; |
5951 | 0 | } |
5952 | 0 | else |
5953 | 0 | { |
5954 | 0 | m = off + sizeof(uint64_t); |
5955 | 0 | if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) || |
5956 | 0 | (m > tif->tif_size)) |
5957 | 0 | *nextdiroff = 0; |
5958 | 0 | else |
5959 | 0 | _TIFFmemcpy(nextdiroff, tif->tif_base + off, |
5960 | 0 | sizeof(uint64_t)); |
5961 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5962 | 0 | TIFFSwabLong8(nextdiroff); |
5963 | 0 | } |
5964 | 0 | } |
5965 | 0 | } |
5966 | 0 | dir = (TIFFDirEntry *)_TIFFCheckMalloc( |
5967 | 0 | tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory"); |
5968 | 0 | if (dir == 0) |
5969 | 0 | { |
5970 | 0 | _TIFFfreeExt(tif, origdir); |
5971 | 0 | return 0; |
5972 | 0 | } |
5973 | 0 | ma = (uint8_t *)origdir; |
5974 | 0 | mb = dir; |
5975 | 0 | for (n = 0; n < dircount16; n++) |
5976 | 0 | { |
5977 | 0 | mb->tdir_ignore = FALSE; |
5978 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5979 | 0 | TIFFSwabShort((uint16_t *)ma); |
5980 | 0 | mb->tdir_tag = *(uint16_t *)ma; |
5981 | 0 | ma += sizeof(uint16_t); |
5982 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5983 | 0 | TIFFSwabShort((uint16_t *)ma); |
5984 | 0 | mb->tdir_type = *(uint16_t *)ma; |
5985 | 0 | ma += sizeof(uint16_t); |
5986 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
5987 | 0 | { |
5988 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5989 | 0 | TIFFSwabLong((uint32_t *)ma); |
5990 | 0 | mb->tdir_count = (uint64_t)(*(uint32_t *)ma); |
5991 | 0 | ma += sizeof(uint32_t); |
5992 | 0 | mb->tdir_offset.toff_long8 = 0; |
5993 | 0 | *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma; |
5994 | 0 | ma += sizeof(uint32_t); |
5995 | 0 | } |
5996 | 0 | else |
5997 | 0 | { |
5998 | 0 | if (tif->tif_flags & TIFF_SWAB) |
5999 | 0 | TIFFSwabLong8((uint64_t *)ma); |
6000 | 0 | mb->tdir_count = TIFFReadUInt64(ma); |
6001 | 0 | ma += sizeof(uint64_t); |
6002 | 0 | mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma); |
6003 | 0 | ma += sizeof(uint64_t); |
6004 | 0 | } |
6005 | 0 | mb++; |
6006 | 0 | } |
6007 | 0 | _TIFFfreeExt(tif, origdir); |
6008 | 0 | *pdir = dir; |
6009 | 0 | return dircount16; |
6010 | 0 | } |
6011 | | |
6012 | | /* |
6013 | | * Fetch a tag that is not handled by special case code. |
6014 | | */ |
6015 | | static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover) |
6016 | 0 | { |
6017 | 0 | static const char module[] = "TIFFFetchNormalTag"; |
6018 | 0 | enum TIFFReadDirEntryErr err; |
6019 | 0 | uint32_t fii; |
6020 | 0 | const TIFFField *fip = NULL; |
6021 | 0 | TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii); |
6022 | 0 | if (fii == FAILED_FII) |
6023 | 0 | { |
6024 | 0 | TIFFErrorExtR(tif, "TIFFFetchNormalTag", |
6025 | 0 | "No definition found for tag %" PRIu16, dp->tdir_tag); |
6026 | 0 | return 0; |
6027 | 0 | } |
6028 | 0 | fip = tif->tif_fields[fii]; |
6029 | 0 | assert(fip != NULL); /* should not happen */ |
6030 | 0 | assert(fip->set_field_type != |
6031 | 0 | TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with |
6032 | | this in specialized code */ |
6033 | 0 | assert(fip->set_field_type != |
6034 | 0 | TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only |
6035 | | the case for pseudo-tags */ |
6036 | 0 | err = TIFFReadDirEntryErrOk; |
6037 | 0 | switch (fip->set_field_type) |
6038 | 0 | { |
6039 | 0 | case TIFF_SETGET_UNDEFINED: |
6040 | 0 | TIFFErrorExtR( |
6041 | 0 | tif, "TIFFFetchNormalTag", |
6042 | 0 | "Defined set_field_type of custom tag %u (%s) is " |
6043 | 0 | "TIFF_SETGET_UNDEFINED and thus tag is not read from file", |
6044 | 0 | fip->field_tag, fip->field_name); |
6045 | 0 | break; |
6046 | 0 | case TIFF_SETGET_ASCII: |
6047 | 0 | { |
6048 | 0 | uint8_t *data; |
6049 | 0 | assert(fip->field_passcount == 0); |
6050 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6051 | 0 | if (err == TIFFReadDirEntryErrOk) |
6052 | 0 | { |
6053 | 0 | size_t mb = 0; |
6054 | 0 | int n; |
6055 | 0 | if (data != NULL) |
6056 | 0 | { |
6057 | 0 | if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0) |
6058 | 0 | { |
6059 | | /* optimization: if data is known to be 0 terminated, we |
6060 | | * can use strlen() */ |
6061 | 0 | mb = strlen((const char *)data); |
6062 | 0 | } |
6063 | 0 | else |
6064 | 0 | { |
6065 | | /* general case. equivalent to non-portable */ |
6066 | | /* mb = strnlen((const char*)data, |
6067 | | * (uint32_t)dp->tdir_count); */ |
6068 | 0 | uint8_t *ma = data; |
6069 | 0 | while (mb < (uint32_t)dp->tdir_count) |
6070 | 0 | { |
6071 | 0 | if (*ma == 0) |
6072 | 0 | break; |
6073 | 0 | ma++; |
6074 | 0 | mb++; |
6075 | 0 | } |
6076 | 0 | } |
6077 | 0 | } |
6078 | 0 | if (mb + 1 < (uint32_t)dp->tdir_count) |
6079 | 0 | TIFFWarningExtR( |
6080 | 0 | tif, module, |
6081 | 0 | "ASCII value for tag \"%s\" contains null byte in " |
6082 | 0 | "value; value incorrectly truncated during reading due " |
6083 | 0 | "to implementation limitations", |
6084 | 0 | fip->field_name); |
6085 | 0 | else if (mb + 1 > (uint32_t)dp->tdir_count) |
6086 | 0 | { |
6087 | 0 | uint8_t *o; |
6088 | 0 | TIFFWarningExtR( |
6089 | 0 | tif, module, |
6090 | 0 | "ASCII value for tag \"%s\" does not end in null byte", |
6091 | 0 | fip->field_name); |
6092 | | /* TIFFReadDirEntryArrayWithLimit() ensures this can't be |
6093 | | * larger than MAX_SIZE_TAG_DATA */ |
6094 | 0 | assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1); |
6095 | 0 | o = _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1); |
6096 | 0 | if (o == NULL) |
6097 | 0 | { |
6098 | 0 | if (data != NULL) |
6099 | 0 | _TIFFfreeExt(tif, data); |
6100 | 0 | return (0); |
6101 | 0 | } |
6102 | 0 | if (dp->tdir_count > 0) |
6103 | 0 | { |
6104 | 0 | _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count); |
6105 | 0 | } |
6106 | 0 | o[(uint32_t)dp->tdir_count] = 0; |
6107 | 0 | if (data != 0) |
6108 | 0 | _TIFFfreeExt(tif, data); |
6109 | 0 | data = o; |
6110 | 0 | } |
6111 | 0 | n = TIFFSetField(tif, dp->tdir_tag, data); |
6112 | 0 | if (data != 0) |
6113 | 0 | _TIFFfreeExt(tif, data); |
6114 | 0 | if (!n) |
6115 | 0 | return (0); |
6116 | 0 | } |
6117 | 0 | } |
6118 | 0 | break; |
6119 | 0 | case TIFF_SETGET_UINT8: |
6120 | 0 | { |
6121 | 0 | uint8_t data = 0; |
6122 | 0 | assert(fip->field_readcount == 1); |
6123 | 0 | assert(fip->field_passcount == 0); |
6124 | 0 | err = TIFFReadDirEntryByte(tif, dp, &data); |
6125 | 0 | if (err == TIFFReadDirEntryErrOk) |
6126 | 0 | { |
6127 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6128 | 0 | return (0); |
6129 | 0 | } |
6130 | 0 | } |
6131 | 0 | break; |
6132 | 0 | case TIFF_SETGET_SINT8: |
6133 | 0 | { |
6134 | 0 | int8_t data = 0; |
6135 | 0 | assert(fip->field_readcount == 1); |
6136 | 0 | assert(fip->field_passcount == 0); |
6137 | 0 | err = TIFFReadDirEntrySbyte(tif, dp, &data); |
6138 | 0 | if (err == TIFFReadDirEntryErrOk) |
6139 | 0 | { |
6140 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6141 | 0 | return (0); |
6142 | 0 | } |
6143 | 0 | } |
6144 | 0 | break; |
6145 | 0 | case TIFF_SETGET_UINT16: |
6146 | 0 | { |
6147 | 0 | uint16_t data; |
6148 | 0 | assert(fip->field_readcount == 1); |
6149 | 0 | assert(fip->field_passcount == 0); |
6150 | 0 | err = TIFFReadDirEntryShort(tif, dp, &data); |
6151 | 0 | if (err == TIFFReadDirEntryErrOk) |
6152 | 0 | { |
6153 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6154 | 0 | return (0); |
6155 | 0 | } |
6156 | 0 | } |
6157 | 0 | break; |
6158 | 0 | case TIFF_SETGET_SINT16: |
6159 | 0 | { |
6160 | 0 | int16_t data; |
6161 | 0 | assert(fip->field_readcount == 1); |
6162 | 0 | assert(fip->field_passcount == 0); |
6163 | 0 | err = TIFFReadDirEntrySshort(tif, dp, &data); |
6164 | 0 | if (err == TIFFReadDirEntryErrOk) |
6165 | 0 | { |
6166 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6167 | 0 | return (0); |
6168 | 0 | } |
6169 | 0 | } |
6170 | 0 | break; |
6171 | 0 | case TIFF_SETGET_UINT32: |
6172 | 0 | { |
6173 | 0 | uint32_t data; |
6174 | 0 | assert(fip->field_readcount == 1); |
6175 | 0 | assert(fip->field_passcount == 0); |
6176 | 0 | err = TIFFReadDirEntryLong(tif, dp, &data); |
6177 | 0 | if (err == TIFFReadDirEntryErrOk) |
6178 | 0 | { |
6179 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6180 | 0 | return (0); |
6181 | 0 | } |
6182 | 0 | } |
6183 | 0 | break; |
6184 | 0 | case TIFF_SETGET_SINT32: |
6185 | 0 | { |
6186 | 0 | int32_t data; |
6187 | 0 | assert(fip->field_readcount == 1); |
6188 | 0 | assert(fip->field_passcount == 0); |
6189 | 0 | err = TIFFReadDirEntrySlong(tif, dp, &data); |
6190 | 0 | if (err == TIFFReadDirEntryErrOk) |
6191 | 0 | { |
6192 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6193 | 0 | return (0); |
6194 | 0 | } |
6195 | 0 | } |
6196 | 0 | break; |
6197 | 0 | case TIFF_SETGET_UINT64: |
6198 | 0 | { |
6199 | 0 | uint64_t data; |
6200 | 0 | assert(fip->field_readcount == 1); |
6201 | 0 | assert(fip->field_passcount == 0); |
6202 | 0 | err = TIFFReadDirEntryLong8(tif, dp, &data); |
6203 | 0 | if (err == TIFFReadDirEntryErrOk) |
6204 | 0 | { |
6205 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6206 | 0 | return (0); |
6207 | 0 | } |
6208 | 0 | } |
6209 | 0 | break; |
6210 | 0 | case TIFF_SETGET_SINT64: |
6211 | 0 | { |
6212 | 0 | int64_t data; |
6213 | 0 | assert(fip->field_readcount == 1); |
6214 | 0 | assert(fip->field_passcount == 0); |
6215 | 0 | err = TIFFReadDirEntrySlong8(tif, dp, &data); |
6216 | 0 | if (err == TIFFReadDirEntryErrOk) |
6217 | 0 | { |
6218 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6219 | 0 | return (0); |
6220 | 0 | } |
6221 | 0 | } |
6222 | 0 | break; |
6223 | 0 | case TIFF_SETGET_FLOAT: |
6224 | 0 | { |
6225 | 0 | float data; |
6226 | 0 | assert(fip->field_readcount == 1); |
6227 | 0 | assert(fip->field_passcount == 0); |
6228 | 0 | err = TIFFReadDirEntryFloat(tif, dp, &data); |
6229 | 0 | if (err == TIFFReadDirEntryErrOk) |
6230 | 0 | { |
6231 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6232 | 0 | return (0); |
6233 | 0 | } |
6234 | 0 | } |
6235 | 0 | break; |
6236 | 0 | case TIFF_SETGET_DOUBLE: |
6237 | 0 | { |
6238 | 0 | double data; |
6239 | 0 | assert(fip->field_readcount == 1); |
6240 | 0 | assert(fip->field_passcount == 0); |
6241 | 0 | err = TIFFReadDirEntryDouble(tif, dp, &data); |
6242 | 0 | if (err == TIFFReadDirEntryErrOk) |
6243 | 0 | { |
6244 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6245 | 0 | return (0); |
6246 | 0 | } |
6247 | 0 | } |
6248 | 0 | break; |
6249 | 0 | case TIFF_SETGET_IFD8: |
6250 | 0 | { |
6251 | 0 | uint64_t data; |
6252 | 0 | assert(fip->field_readcount == 1); |
6253 | 0 | assert(fip->field_passcount == 0); |
6254 | 0 | err = TIFFReadDirEntryIfd8(tif, dp, &data); |
6255 | 0 | if (err == TIFFReadDirEntryErrOk) |
6256 | 0 | { |
6257 | 0 | if (!TIFFSetField(tif, dp->tdir_tag, data)) |
6258 | 0 | return (0); |
6259 | 0 | } |
6260 | 0 | } |
6261 | 0 | break; |
6262 | 0 | case TIFF_SETGET_UINT16_PAIR: |
6263 | 0 | { |
6264 | 0 | uint16_t *data; |
6265 | 0 | assert(fip->field_readcount == 2); |
6266 | 0 | assert(fip->field_passcount == 0); |
6267 | 0 | if (dp->tdir_count != 2) |
6268 | 0 | { |
6269 | 0 | TIFFWarningExtR(tif, module, |
6270 | 0 | "incorrect count for field \"%s\", expected 2, " |
6271 | 0 | "got %" PRIu64, |
6272 | 0 | fip->field_name, dp->tdir_count); |
6273 | 0 | return (0); |
6274 | 0 | } |
6275 | 0 | err = TIFFReadDirEntryShortArray(tif, dp, &data); |
6276 | 0 | if (err == TIFFReadDirEntryErrOk) |
6277 | 0 | { |
6278 | 0 | int m; |
6279 | 0 | assert(data); /* avoid CLang static Analyzer false positive */ |
6280 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]); |
6281 | 0 | _TIFFfreeExt(tif, data); |
6282 | 0 | if (!m) |
6283 | 0 | return (0); |
6284 | 0 | } |
6285 | 0 | } |
6286 | 0 | break; |
6287 | 0 | case TIFF_SETGET_C0_UINT8: |
6288 | 0 | { |
6289 | 0 | uint8_t *data; |
6290 | 0 | assert(fip->field_readcount >= 1); |
6291 | 0 | assert(fip->field_passcount == 0); |
6292 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6293 | 0 | { |
6294 | 0 | TIFFWarningExtR(tif, module, |
6295 | 0 | "incorrect count for field \"%s\", expected " |
6296 | 0 | "%d, got %" PRIu64, |
6297 | 0 | fip->field_name, (int)fip->field_readcount, |
6298 | 0 | dp->tdir_count); |
6299 | 0 | return (0); |
6300 | 0 | } |
6301 | 0 | else |
6302 | 0 | { |
6303 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6304 | 0 | if (err == TIFFReadDirEntryErrOk) |
6305 | 0 | { |
6306 | 0 | int m; |
6307 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6308 | 0 | if (data != 0) |
6309 | 0 | _TIFFfreeExt(tif, data); |
6310 | 0 | if (!m) |
6311 | 0 | return (0); |
6312 | 0 | } |
6313 | 0 | } |
6314 | 0 | } |
6315 | 0 | break; |
6316 | 0 | case TIFF_SETGET_C0_SINT8: |
6317 | 0 | { |
6318 | 0 | int8_t *data; |
6319 | 0 | assert(fip->field_readcount >= 1); |
6320 | 0 | assert(fip->field_passcount == 0); |
6321 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6322 | 0 | { |
6323 | 0 | TIFFWarningExtR(tif, module, |
6324 | 0 | "incorrect count for field \"%s\", expected " |
6325 | 0 | "%d, got %" PRIu64, |
6326 | 0 | fip->field_name, (int)fip->field_readcount, |
6327 | 0 | dp->tdir_count); |
6328 | 0 | return (0); |
6329 | 0 | } |
6330 | 0 | else |
6331 | 0 | { |
6332 | 0 | err = TIFFReadDirEntrySbyteArray(tif, dp, &data); |
6333 | 0 | if (err == TIFFReadDirEntryErrOk) |
6334 | 0 | { |
6335 | 0 | int m; |
6336 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6337 | 0 | if (data != 0) |
6338 | 0 | _TIFFfreeExt(tif, data); |
6339 | 0 | if (!m) |
6340 | 0 | return (0); |
6341 | 0 | } |
6342 | 0 | } |
6343 | 0 | } |
6344 | 0 | break; |
6345 | 0 | case TIFF_SETGET_C0_UINT16: |
6346 | 0 | { |
6347 | 0 | uint16_t *data; |
6348 | 0 | assert(fip->field_readcount >= 1); |
6349 | 0 | assert(fip->field_passcount == 0); |
6350 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6351 | 0 | { |
6352 | 0 | TIFFWarningExtR(tif, module, |
6353 | 0 | "incorrect count for field \"%s\", expected " |
6354 | 0 | "%d, got %" PRIu64, |
6355 | 0 | fip->field_name, (int)fip->field_readcount, |
6356 | 0 | dp->tdir_count); |
6357 | 0 | return (0); |
6358 | 0 | } |
6359 | 0 | else |
6360 | 0 | { |
6361 | 0 | err = TIFFReadDirEntryShortArray(tif, dp, &data); |
6362 | 0 | if (err == TIFFReadDirEntryErrOk) |
6363 | 0 | { |
6364 | 0 | int m; |
6365 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6366 | 0 | if (data != 0) |
6367 | 0 | _TIFFfreeExt(tif, data); |
6368 | 0 | if (!m) |
6369 | 0 | return (0); |
6370 | 0 | } |
6371 | 0 | } |
6372 | 0 | } |
6373 | 0 | break; |
6374 | 0 | case TIFF_SETGET_C0_SINT16: |
6375 | 0 | { |
6376 | 0 | int16_t *data; |
6377 | 0 | assert(fip->field_readcount >= 1); |
6378 | 0 | assert(fip->field_passcount == 0); |
6379 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6380 | 0 | { |
6381 | 0 | TIFFWarningExtR(tif, module, |
6382 | 0 | "incorrect count for field \"%s\", expected " |
6383 | 0 | "%d, got %" PRIu64, |
6384 | 0 | fip->field_name, (int)fip->field_readcount, |
6385 | 0 | dp->tdir_count); |
6386 | 0 | return (0); |
6387 | 0 | } |
6388 | 0 | else |
6389 | 0 | { |
6390 | 0 | err = TIFFReadDirEntrySshortArray(tif, dp, &data); |
6391 | 0 | if (err == TIFFReadDirEntryErrOk) |
6392 | 0 | { |
6393 | 0 | int m; |
6394 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6395 | 0 | if (data != 0) |
6396 | 0 | _TIFFfreeExt(tif, data); |
6397 | 0 | if (!m) |
6398 | 0 | return (0); |
6399 | 0 | } |
6400 | 0 | } |
6401 | 0 | } |
6402 | 0 | break; |
6403 | 0 | case TIFF_SETGET_C0_UINT32: |
6404 | 0 | { |
6405 | 0 | uint32_t *data; |
6406 | 0 | assert(fip->field_readcount >= 1); |
6407 | 0 | assert(fip->field_passcount == 0); |
6408 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6409 | 0 | { |
6410 | 0 | TIFFWarningExtR(tif, module, |
6411 | 0 | "incorrect count for field \"%s\", expected " |
6412 | 0 | "%d, got %" PRIu64, |
6413 | 0 | fip->field_name, (int)fip->field_readcount, |
6414 | 0 | dp->tdir_count); |
6415 | 0 | return (0); |
6416 | 0 | } |
6417 | 0 | else |
6418 | 0 | { |
6419 | 0 | err = TIFFReadDirEntryLongArray(tif, dp, &data); |
6420 | 0 | if (err == TIFFReadDirEntryErrOk) |
6421 | 0 | { |
6422 | 0 | int m; |
6423 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6424 | 0 | if (data != 0) |
6425 | 0 | _TIFFfreeExt(tif, data); |
6426 | 0 | if (!m) |
6427 | 0 | return (0); |
6428 | 0 | } |
6429 | 0 | } |
6430 | 0 | } |
6431 | 0 | break; |
6432 | 0 | case TIFF_SETGET_C0_SINT32: |
6433 | 0 | { |
6434 | 0 | int32_t *data; |
6435 | 0 | assert(fip->field_readcount >= 1); |
6436 | 0 | assert(fip->field_passcount == 0); |
6437 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6438 | 0 | { |
6439 | 0 | TIFFWarningExtR(tif, module, |
6440 | 0 | "incorrect count for field \"%s\", expected " |
6441 | 0 | "%d, got %" PRIu64, |
6442 | 0 | fip->field_name, (int)fip->field_readcount, |
6443 | 0 | dp->tdir_count); |
6444 | 0 | return (0); |
6445 | 0 | } |
6446 | 0 | else |
6447 | 0 | { |
6448 | 0 | err = TIFFReadDirEntrySlongArray(tif, dp, &data); |
6449 | 0 | if (err == TIFFReadDirEntryErrOk) |
6450 | 0 | { |
6451 | 0 | int m; |
6452 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6453 | 0 | if (data != 0) |
6454 | 0 | _TIFFfreeExt(tif, data); |
6455 | 0 | if (!m) |
6456 | 0 | return (0); |
6457 | 0 | } |
6458 | 0 | } |
6459 | 0 | } |
6460 | 0 | break; |
6461 | 0 | case TIFF_SETGET_C0_UINT64: |
6462 | 0 | { |
6463 | 0 | uint64_t *data; |
6464 | 0 | assert(fip->field_readcount >= 1); |
6465 | 0 | assert(fip->field_passcount == 0); |
6466 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6467 | 0 | { |
6468 | 0 | TIFFWarningExtR(tif, module, |
6469 | 0 | "incorrect count for field \"%s\", expected " |
6470 | 0 | "%d, got %" PRIu64, |
6471 | 0 | fip->field_name, (int)fip->field_readcount, |
6472 | 0 | dp->tdir_count); |
6473 | 0 | return (0); |
6474 | 0 | } |
6475 | 0 | else |
6476 | 0 | { |
6477 | 0 | err = TIFFReadDirEntryLong8Array(tif, dp, &data); |
6478 | 0 | if (err == TIFFReadDirEntryErrOk) |
6479 | 0 | { |
6480 | 0 | int m; |
6481 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6482 | 0 | if (data != 0) |
6483 | 0 | _TIFFfreeExt(tif, data); |
6484 | 0 | if (!m) |
6485 | 0 | return (0); |
6486 | 0 | } |
6487 | 0 | } |
6488 | 0 | } |
6489 | 0 | break; |
6490 | 0 | case TIFF_SETGET_C0_SINT64: |
6491 | 0 | { |
6492 | 0 | int64_t *data; |
6493 | 0 | assert(fip->field_readcount >= 1); |
6494 | 0 | assert(fip->field_passcount == 0); |
6495 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6496 | 0 | { |
6497 | 0 | TIFFWarningExtR(tif, module, |
6498 | 0 | "incorrect count for field \"%s\", expected " |
6499 | 0 | "%d, got %" PRIu64, |
6500 | 0 | fip->field_name, (int)fip->field_readcount, |
6501 | 0 | dp->tdir_count); |
6502 | 0 | return (0); |
6503 | 0 | } |
6504 | 0 | else |
6505 | 0 | { |
6506 | 0 | err = TIFFReadDirEntrySlong8Array(tif, dp, &data); |
6507 | 0 | if (err == TIFFReadDirEntryErrOk) |
6508 | 0 | { |
6509 | 0 | int m; |
6510 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6511 | 0 | if (data != 0) |
6512 | 0 | _TIFFfreeExt(tif, data); |
6513 | 0 | if (!m) |
6514 | 0 | return (0); |
6515 | 0 | } |
6516 | 0 | } |
6517 | 0 | } |
6518 | 0 | break; |
6519 | 0 | case TIFF_SETGET_C0_FLOAT: |
6520 | 0 | { |
6521 | 0 | float *data; |
6522 | 0 | assert(fip->field_readcount >= 1); |
6523 | 0 | assert(fip->field_passcount == 0); |
6524 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6525 | 0 | { |
6526 | 0 | TIFFWarningExtR(tif, module, |
6527 | 0 | "incorrect count for field \"%s\", expected " |
6528 | 0 | "%d, got %" PRIu64, |
6529 | 0 | fip->field_name, (int)fip->field_readcount, |
6530 | 0 | dp->tdir_count); |
6531 | 0 | return (0); |
6532 | 0 | } |
6533 | 0 | else |
6534 | 0 | { |
6535 | 0 | err = TIFFReadDirEntryFloatArray(tif, dp, &data); |
6536 | 0 | if (err == TIFFReadDirEntryErrOk) |
6537 | 0 | { |
6538 | 0 | int m; |
6539 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6540 | 0 | if (data != 0) |
6541 | 0 | _TIFFfreeExt(tif, data); |
6542 | 0 | if (!m) |
6543 | 0 | return (0); |
6544 | 0 | } |
6545 | 0 | } |
6546 | 0 | } |
6547 | 0 | break; |
6548 | | /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read |
6549 | | * into Double-Arrays. */ |
6550 | 0 | case TIFF_SETGET_C0_DOUBLE: |
6551 | 0 | { |
6552 | 0 | double *data; |
6553 | 0 | assert(fip->field_readcount >= 1); |
6554 | 0 | assert(fip->field_passcount == 0); |
6555 | 0 | if (dp->tdir_count != (uint64_t)fip->field_readcount) |
6556 | 0 | { |
6557 | 0 | TIFFWarningExtR(tif, module, |
6558 | 0 | "incorrect count for field \"%s\", expected " |
6559 | 0 | "%d, got %" PRIu64, |
6560 | 0 | fip->field_name, (int)fip->field_readcount, |
6561 | 0 | dp->tdir_count); |
6562 | 0 | return (0); |
6563 | 0 | } |
6564 | 0 | else |
6565 | 0 | { |
6566 | 0 | err = TIFFReadDirEntryDoubleArray(tif, dp, &data); |
6567 | 0 | if (err == TIFFReadDirEntryErrOk) |
6568 | 0 | { |
6569 | 0 | int m; |
6570 | 0 | m = TIFFSetField(tif, dp->tdir_tag, data); |
6571 | 0 | if (data != 0) |
6572 | 0 | _TIFFfreeExt(tif, data); |
6573 | 0 | if (!m) |
6574 | 0 | return (0); |
6575 | 0 | } |
6576 | 0 | } |
6577 | 0 | } |
6578 | 0 | break; |
6579 | 0 | case TIFF_SETGET_C16_ASCII: |
6580 | 0 | { |
6581 | 0 | uint8_t *data; |
6582 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6583 | 0 | assert(fip->field_passcount == 1); |
6584 | 0 | if (dp->tdir_count > 0xFFFF) |
6585 | 0 | err = TIFFReadDirEntryErrCount; |
6586 | 0 | else |
6587 | 0 | { |
6588 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6589 | 0 | if (err == TIFFReadDirEntryErrOk) |
6590 | 0 | { |
6591 | 0 | int m; |
6592 | 0 | if (data != 0 && dp->tdir_count > 0 && |
6593 | 0 | data[dp->tdir_count - 1] != '\0') |
6594 | 0 | { |
6595 | 0 | TIFFWarningExtR( |
6596 | 0 | tif, module, |
6597 | 0 | "ASCII value for tag \"%s\" does not end in null " |
6598 | 0 | "byte. Forcing it to be null", |
6599 | 0 | fip->field_name); |
6600 | 0 | data[dp->tdir_count - 1] = '\0'; |
6601 | 0 | } |
6602 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6603 | 0 | (uint16_t)(dp->tdir_count), data); |
6604 | 0 | if (data != 0) |
6605 | 0 | _TIFFfreeExt(tif, data); |
6606 | 0 | if (!m) |
6607 | 0 | return (0); |
6608 | 0 | } |
6609 | 0 | } |
6610 | 0 | } |
6611 | 0 | break; |
6612 | 0 | case TIFF_SETGET_C16_UINT8: |
6613 | 0 | { |
6614 | 0 | uint8_t *data; |
6615 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6616 | 0 | assert(fip->field_passcount == 1); |
6617 | 0 | if (dp->tdir_count > 0xFFFF) |
6618 | 0 | err = TIFFReadDirEntryErrCount; |
6619 | 0 | else |
6620 | 0 | { |
6621 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6622 | 0 | if (err == TIFFReadDirEntryErrOk) |
6623 | 0 | { |
6624 | 0 | int m; |
6625 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6626 | 0 | (uint16_t)(dp->tdir_count), data); |
6627 | 0 | if (data != 0) |
6628 | 0 | _TIFFfreeExt(tif, data); |
6629 | 0 | if (!m) |
6630 | 0 | return (0); |
6631 | 0 | } |
6632 | 0 | } |
6633 | 0 | } |
6634 | 0 | break; |
6635 | 0 | case TIFF_SETGET_C16_SINT8: |
6636 | 0 | { |
6637 | 0 | int8_t *data; |
6638 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6639 | 0 | assert(fip->field_passcount == 1); |
6640 | 0 | if (dp->tdir_count > 0xFFFF) |
6641 | 0 | err = TIFFReadDirEntryErrCount; |
6642 | 0 | else |
6643 | 0 | { |
6644 | 0 | err = TIFFReadDirEntrySbyteArray(tif, dp, &data); |
6645 | 0 | if (err == TIFFReadDirEntryErrOk) |
6646 | 0 | { |
6647 | 0 | int m; |
6648 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6649 | 0 | (uint16_t)(dp->tdir_count), data); |
6650 | 0 | if (data != 0) |
6651 | 0 | _TIFFfreeExt(tif, data); |
6652 | 0 | if (!m) |
6653 | 0 | return (0); |
6654 | 0 | } |
6655 | 0 | } |
6656 | 0 | } |
6657 | 0 | break; |
6658 | 0 | case TIFF_SETGET_C16_UINT16: |
6659 | 0 | { |
6660 | 0 | uint16_t *data; |
6661 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6662 | 0 | assert(fip->field_passcount == 1); |
6663 | 0 | if (dp->tdir_count > 0xFFFF) |
6664 | 0 | err = TIFFReadDirEntryErrCount; |
6665 | 0 | else |
6666 | 0 | { |
6667 | 0 | err = TIFFReadDirEntryShortArray(tif, dp, &data); |
6668 | 0 | if (err == TIFFReadDirEntryErrOk) |
6669 | 0 | { |
6670 | 0 | int m; |
6671 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6672 | 0 | (uint16_t)(dp->tdir_count), data); |
6673 | 0 | if (data != 0) |
6674 | 0 | _TIFFfreeExt(tif, data); |
6675 | 0 | if (!m) |
6676 | 0 | return (0); |
6677 | 0 | } |
6678 | 0 | } |
6679 | 0 | } |
6680 | 0 | break; |
6681 | 0 | case TIFF_SETGET_C16_SINT16: |
6682 | 0 | { |
6683 | 0 | int16_t *data; |
6684 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6685 | 0 | assert(fip->field_passcount == 1); |
6686 | 0 | if (dp->tdir_count > 0xFFFF) |
6687 | 0 | err = TIFFReadDirEntryErrCount; |
6688 | 0 | else |
6689 | 0 | { |
6690 | 0 | err = TIFFReadDirEntrySshortArray(tif, dp, &data); |
6691 | 0 | if (err == TIFFReadDirEntryErrOk) |
6692 | 0 | { |
6693 | 0 | int m; |
6694 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6695 | 0 | (uint16_t)(dp->tdir_count), data); |
6696 | 0 | if (data != 0) |
6697 | 0 | _TIFFfreeExt(tif, data); |
6698 | 0 | if (!m) |
6699 | 0 | return (0); |
6700 | 0 | } |
6701 | 0 | } |
6702 | 0 | } |
6703 | 0 | break; |
6704 | 0 | case TIFF_SETGET_C16_UINT32: |
6705 | 0 | { |
6706 | 0 | uint32_t *data; |
6707 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6708 | 0 | assert(fip->field_passcount == 1); |
6709 | 0 | if (dp->tdir_count > 0xFFFF) |
6710 | 0 | err = TIFFReadDirEntryErrCount; |
6711 | 0 | else |
6712 | 0 | { |
6713 | 0 | err = TIFFReadDirEntryLongArray(tif, dp, &data); |
6714 | 0 | if (err == TIFFReadDirEntryErrOk) |
6715 | 0 | { |
6716 | 0 | int m; |
6717 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6718 | 0 | (uint16_t)(dp->tdir_count), data); |
6719 | 0 | if (data != 0) |
6720 | 0 | _TIFFfreeExt(tif, data); |
6721 | 0 | if (!m) |
6722 | 0 | return (0); |
6723 | 0 | } |
6724 | 0 | } |
6725 | 0 | } |
6726 | 0 | break; |
6727 | 0 | case TIFF_SETGET_C16_SINT32: |
6728 | 0 | { |
6729 | 0 | int32_t *data; |
6730 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6731 | 0 | assert(fip->field_passcount == 1); |
6732 | 0 | if (dp->tdir_count > 0xFFFF) |
6733 | 0 | err = TIFFReadDirEntryErrCount; |
6734 | 0 | else |
6735 | 0 | { |
6736 | 0 | err = TIFFReadDirEntrySlongArray(tif, dp, &data); |
6737 | 0 | if (err == TIFFReadDirEntryErrOk) |
6738 | 0 | { |
6739 | 0 | int m; |
6740 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6741 | 0 | (uint16_t)(dp->tdir_count), data); |
6742 | 0 | if (data != 0) |
6743 | 0 | _TIFFfreeExt(tif, data); |
6744 | 0 | if (!m) |
6745 | 0 | return (0); |
6746 | 0 | } |
6747 | 0 | } |
6748 | 0 | } |
6749 | 0 | break; |
6750 | 0 | case TIFF_SETGET_C16_UINT64: |
6751 | 0 | { |
6752 | 0 | uint64_t *data; |
6753 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6754 | 0 | assert(fip->field_passcount == 1); |
6755 | 0 | if (dp->tdir_count > 0xFFFF) |
6756 | 0 | err = TIFFReadDirEntryErrCount; |
6757 | 0 | else |
6758 | 0 | { |
6759 | 0 | err = TIFFReadDirEntryLong8Array(tif, dp, &data); |
6760 | 0 | if (err == TIFFReadDirEntryErrOk) |
6761 | 0 | { |
6762 | 0 | int m; |
6763 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6764 | 0 | (uint16_t)(dp->tdir_count), data); |
6765 | 0 | if (data != 0) |
6766 | 0 | _TIFFfreeExt(tif, data); |
6767 | 0 | if (!m) |
6768 | 0 | return (0); |
6769 | 0 | } |
6770 | 0 | } |
6771 | 0 | } |
6772 | 0 | break; |
6773 | 0 | case TIFF_SETGET_C16_SINT64: |
6774 | 0 | { |
6775 | 0 | int64_t *data; |
6776 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6777 | 0 | assert(fip->field_passcount == 1); |
6778 | 0 | if (dp->tdir_count > 0xFFFF) |
6779 | 0 | err = TIFFReadDirEntryErrCount; |
6780 | 0 | else |
6781 | 0 | { |
6782 | 0 | err = TIFFReadDirEntrySlong8Array(tif, dp, &data); |
6783 | 0 | if (err == TIFFReadDirEntryErrOk) |
6784 | 0 | { |
6785 | 0 | int m; |
6786 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6787 | 0 | (uint16_t)(dp->tdir_count), data); |
6788 | 0 | if (data != 0) |
6789 | 0 | _TIFFfreeExt(tif, data); |
6790 | 0 | if (!m) |
6791 | 0 | return (0); |
6792 | 0 | } |
6793 | 0 | } |
6794 | 0 | } |
6795 | 0 | break; |
6796 | 0 | case TIFF_SETGET_C16_FLOAT: |
6797 | 0 | { |
6798 | 0 | float *data; |
6799 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6800 | 0 | assert(fip->field_passcount == 1); |
6801 | 0 | if (dp->tdir_count > 0xFFFF) |
6802 | 0 | err = TIFFReadDirEntryErrCount; |
6803 | 0 | else |
6804 | 0 | { |
6805 | 0 | err = TIFFReadDirEntryFloatArray(tif, dp, &data); |
6806 | 0 | if (err == TIFFReadDirEntryErrOk) |
6807 | 0 | { |
6808 | 0 | int m; |
6809 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6810 | 0 | (uint16_t)(dp->tdir_count), data); |
6811 | 0 | if (data != 0) |
6812 | 0 | _TIFFfreeExt(tif, data); |
6813 | 0 | if (!m) |
6814 | 0 | return (0); |
6815 | 0 | } |
6816 | 0 | } |
6817 | 0 | } |
6818 | 0 | break; |
6819 | 0 | case TIFF_SETGET_C16_DOUBLE: |
6820 | 0 | { |
6821 | 0 | double *data; |
6822 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6823 | 0 | assert(fip->field_passcount == 1); |
6824 | 0 | if (dp->tdir_count > 0xFFFF) |
6825 | 0 | err = TIFFReadDirEntryErrCount; |
6826 | 0 | else |
6827 | 0 | { |
6828 | 0 | err = TIFFReadDirEntryDoubleArray(tif, dp, &data); |
6829 | 0 | if (err == TIFFReadDirEntryErrOk) |
6830 | 0 | { |
6831 | 0 | int m; |
6832 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6833 | 0 | (uint16_t)(dp->tdir_count), data); |
6834 | 0 | if (data != 0) |
6835 | 0 | _TIFFfreeExt(tif, data); |
6836 | 0 | if (!m) |
6837 | 0 | return (0); |
6838 | 0 | } |
6839 | 0 | } |
6840 | 0 | } |
6841 | 0 | break; |
6842 | 0 | case TIFF_SETGET_C16_IFD8: |
6843 | 0 | { |
6844 | 0 | uint64_t *data; |
6845 | 0 | assert(fip->field_readcount == TIFF_VARIABLE); |
6846 | 0 | assert(fip->field_passcount == 1); |
6847 | 0 | if (dp->tdir_count > 0xFFFF) |
6848 | 0 | err = TIFFReadDirEntryErrCount; |
6849 | 0 | else |
6850 | 0 | { |
6851 | 0 | err = TIFFReadDirEntryIfd8Array(tif, dp, &data); |
6852 | 0 | if (err == TIFFReadDirEntryErrOk) |
6853 | 0 | { |
6854 | 0 | int m; |
6855 | 0 | m = TIFFSetField(tif, dp->tdir_tag, |
6856 | 0 | (uint16_t)(dp->tdir_count), data); |
6857 | 0 | if (data != 0) |
6858 | 0 | _TIFFfreeExt(tif, data); |
6859 | 0 | if (!m) |
6860 | 0 | return (0); |
6861 | 0 | } |
6862 | 0 | } |
6863 | 0 | } |
6864 | 0 | break; |
6865 | 0 | case TIFF_SETGET_C32_ASCII: |
6866 | 0 | { |
6867 | 0 | uint8_t *data; |
6868 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6869 | 0 | assert(fip->field_passcount == 1); |
6870 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6871 | 0 | if (err == TIFFReadDirEntryErrOk) |
6872 | 0 | { |
6873 | 0 | int m; |
6874 | 0 | if (data != 0 && dp->tdir_count > 0 && |
6875 | 0 | data[dp->tdir_count - 1] != '\0') |
6876 | 0 | { |
6877 | 0 | TIFFWarningExtR(tif, module, |
6878 | 0 | "ASCII value for tag \"%s\" does not end " |
6879 | 0 | "in null byte. Forcing it to be null", |
6880 | 0 | fip->field_name); |
6881 | 0 | data[dp->tdir_count - 1] = '\0'; |
6882 | 0 | } |
6883 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
6884 | 0 | data); |
6885 | 0 | if (data != 0) |
6886 | 0 | _TIFFfreeExt(tif, data); |
6887 | 0 | if (!m) |
6888 | 0 | return (0); |
6889 | 0 | } |
6890 | 0 | } |
6891 | 0 | break; |
6892 | 0 | case TIFF_SETGET_C32_UINT8: |
6893 | 0 | { |
6894 | 0 | uint8_t *data; |
6895 | 0 | uint32_t count = 0; |
6896 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6897 | 0 | assert(fip->field_passcount == 1); |
6898 | 0 | if (fip->field_tag == TIFFTAG_RICHTIFFIPTC && |
6899 | 0 | dp->tdir_type == TIFF_LONG) |
6900 | 0 | { |
6901 | | /* Adobe's software (wrongly) writes RichTIFFIPTC tag with |
6902 | | * data type LONG instead of UNDEFINED. Work around this |
6903 | | * frequently found issue */ |
6904 | 0 | void *origdata; |
6905 | 0 | err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata); |
6906 | 0 | if ((err != TIFFReadDirEntryErrOk) || (origdata == 0)) |
6907 | 0 | { |
6908 | 0 | data = NULL; |
6909 | 0 | } |
6910 | 0 | else |
6911 | 0 | { |
6912 | 0 | if (tif->tif_flags & TIFF_SWAB) |
6913 | 0 | TIFFSwabArrayOfLong((uint32_t *)origdata, count); |
6914 | 0 | data = (uint8_t *)origdata; |
6915 | 0 | count = (uint32_t)(count * 4); |
6916 | 0 | } |
6917 | 0 | } |
6918 | 0 | else |
6919 | 0 | { |
6920 | 0 | err = TIFFReadDirEntryByteArray(tif, dp, &data); |
6921 | 0 | count = (uint32_t)(dp->tdir_count); |
6922 | 0 | } |
6923 | 0 | if (err == TIFFReadDirEntryErrOk) |
6924 | 0 | { |
6925 | 0 | int m; |
6926 | 0 | m = TIFFSetField(tif, dp->tdir_tag, count, data); |
6927 | 0 | if (data != 0) |
6928 | 0 | _TIFFfreeExt(tif, data); |
6929 | 0 | if (!m) |
6930 | 0 | return (0); |
6931 | 0 | } |
6932 | 0 | } |
6933 | 0 | break; |
6934 | 0 | case TIFF_SETGET_C32_SINT8: |
6935 | 0 | { |
6936 | 0 | int8_t *data = NULL; |
6937 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6938 | 0 | assert(fip->field_passcount == 1); |
6939 | 0 | err = TIFFReadDirEntrySbyteArray(tif, dp, &data); |
6940 | 0 | if (err == TIFFReadDirEntryErrOk) |
6941 | 0 | { |
6942 | 0 | int m; |
6943 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
6944 | 0 | data); |
6945 | 0 | if (data != 0) |
6946 | 0 | _TIFFfreeExt(tif, data); |
6947 | 0 | if (!m) |
6948 | 0 | return (0); |
6949 | 0 | } |
6950 | 0 | } |
6951 | 0 | break; |
6952 | 0 | case TIFF_SETGET_C32_UINT16: |
6953 | 0 | { |
6954 | 0 | uint16_t *data; |
6955 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6956 | 0 | assert(fip->field_passcount == 1); |
6957 | 0 | err = TIFFReadDirEntryShortArray(tif, dp, &data); |
6958 | 0 | if (err == TIFFReadDirEntryErrOk) |
6959 | 0 | { |
6960 | 0 | int m; |
6961 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
6962 | 0 | data); |
6963 | 0 | if (data != 0) |
6964 | 0 | _TIFFfreeExt(tif, data); |
6965 | 0 | if (!m) |
6966 | 0 | return (0); |
6967 | 0 | } |
6968 | 0 | } |
6969 | 0 | break; |
6970 | 0 | case TIFF_SETGET_C32_SINT16: |
6971 | 0 | { |
6972 | 0 | int16_t *data = NULL; |
6973 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6974 | 0 | assert(fip->field_passcount == 1); |
6975 | 0 | err = TIFFReadDirEntrySshortArray(tif, dp, &data); |
6976 | 0 | if (err == TIFFReadDirEntryErrOk) |
6977 | 0 | { |
6978 | 0 | int m; |
6979 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
6980 | 0 | data); |
6981 | 0 | if (data != 0) |
6982 | 0 | _TIFFfreeExt(tif, data); |
6983 | 0 | if (!m) |
6984 | 0 | return (0); |
6985 | 0 | } |
6986 | 0 | } |
6987 | 0 | break; |
6988 | 0 | case TIFF_SETGET_C32_UINT32: |
6989 | 0 | { |
6990 | 0 | uint32_t *data; |
6991 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
6992 | 0 | assert(fip->field_passcount == 1); |
6993 | 0 | err = TIFFReadDirEntryLongArray(tif, dp, &data); |
6994 | 0 | if (err == TIFFReadDirEntryErrOk) |
6995 | 0 | { |
6996 | 0 | int m; |
6997 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
6998 | 0 | data); |
6999 | 0 | if (data != 0) |
7000 | 0 | _TIFFfreeExt(tif, data); |
7001 | 0 | if (!m) |
7002 | 0 | return (0); |
7003 | 0 | } |
7004 | 0 | } |
7005 | 0 | break; |
7006 | 0 | case TIFF_SETGET_C32_SINT32: |
7007 | 0 | { |
7008 | 0 | int32_t *data = NULL; |
7009 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7010 | 0 | assert(fip->field_passcount == 1); |
7011 | 0 | err = TIFFReadDirEntrySlongArray(tif, dp, &data); |
7012 | 0 | if (err == TIFFReadDirEntryErrOk) |
7013 | 0 | { |
7014 | 0 | int m; |
7015 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7016 | 0 | data); |
7017 | 0 | if (data != 0) |
7018 | 0 | _TIFFfreeExt(tif, data); |
7019 | 0 | if (!m) |
7020 | 0 | return (0); |
7021 | 0 | } |
7022 | 0 | } |
7023 | 0 | break; |
7024 | 0 | case TIFF_SETGET_C32_UINT64: |
7025 | 0 | { |
7026 | 0 | uint64_t *data; |
7027 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7028 | 0 | assert(fip->field_passcount == 1); |
7029 | 0 | err = TIFFReadDirEntryLong8Array(tif, dp, &data); |
7030 | 0 | if (err == TIFFReadDirEntryErrOk) |
7031 | 0 | { |
7032 | 0 | int m; |
7033 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7034 | 0 | data); |
7035 | 0 | if (data != 0) |
7036 | 0 | _TIFFfreeExt(tif, data); |
7037 | 0 | if (!m) |
7038 | 0 | return (0); |
7039 | 0 | } |
7040 | 0 | } |
7041 | 0 | break; |
7042 | 0 | case TIFF_SETGET_C32_SINT64: |
7043 | 0 | { |
7044 | 0 | int64_t *data = NULL; |
7045 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7046 | 0 | assert(fip->field_passcount == 1); |
7047 | 0 | err = TIFFReadDirEntrySlong8Array(tif, dp, &data); |
7048 | 0 | if (err == TIFFReadDirEntryErrOk) |
7049 | 0 | { |
7050 | 0 | int m; |
7051 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7052 | 0 | data); |
7053 | 0 | if (data != 0) |
7054 | 0 | _TIFFfreeExt(tif, data); |
7055 | 0 | if (!m) |
7056 | 0 | return (0); |
7057 | 0 | } |
7058 | 0 | } |
7059 | 0 | break; |
7060 | 0 | case TIFF_SETGET_C32_FLOAT: |
7061 | 0 | { |
7062 | 0 | float *data; |
7063 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7064 | 0 | assert(fip->field_passcount == 1); |
7065 | 0 | err = TIFFReadDirEntryFloatArray(tif, dp, &data); |
7066 | 0 | if (err == TIFFReadDirEntryErrOk) |
7067 | 0 | { |
7068 | 0 | int m; |
7069 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7070 | 0 | data); |
7071 | 0 | if (data != 0) |
7072 | 0 | _TIFFfreeExt(tif, data); |
7073 | 0 | if (!m) |
7074 | 0 | return (0); |
7075 | 0 | } |
7076 | 0 | } |
7077 | 0 | break; |
7078 | 0 | case TIFF_SETGET_C32_DOUBLE: |
7079 | 0 | { |
7080 | 0 | double *data; |
7081 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7082 | 0 | assert(fip->field_passcount == 1); |
7083 | 0 | err = TIFFReadDirEntryDoubleArray(tif, dp, &data); |
7084 | 0 | if (err == TIFFReadDirEntryErrOk) |
7085 | 0 | { |
7086 | 0 | int m; |
7087 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7088 | 0 | data); |
7089 | 0 | if (data != 0) |
7090 | 0 | _TIFFfreeExt(tif, data); |
7091 | 0 | if (!m) |
7092 | 0 | return (0); |
7093 | 0 | } |
7094 | 0 | } |
7095 | 0 | break; |
7096 | 0 | case TIFF_SETGET_C32_IFD8: |
7097 | 0 | { |
7098 | 0 | uint64_t *data; |
7099 | 0 | assert(fip->field_readcount == TIFF_VARIABLE2); |
7100 | 0 | assert(fip->field_passcount == 1); |
7101 | 0 | err = TIFFReadDirEntryIfd8Array(tif, dp, &data); |
7102 | 0 | if (err == TIFFReadDirEntryErrOk) |
7103 | 0 | { |
7104 | 0 | int m; |
7105 | 0 | m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count), |
7106 | 0 | data); |
7107 | 0 | if (data != 0) |
7108 | 0 | _TIFFfreeExt(tif, data); |
7109 | 0 | if (!m) |
7110 | 0 | return (0); |
7111 | 0 | } |
7112 | 0 | } |
7113 | 0 | break; |
7114 | 0 | default: |
7115 | 0 | assert(0); /* we should never get here */ |
7116 | 0 | break; |
7117 | 0 | } |
7118 | 0 | if (err != TIFFReadDirEntryErrOk) |
7119 | 0 | { |
7120 | 0 | TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover); |
7121 | 0 | return (0); |
7122 | 0 | } |
7123 | 0 | return (1); |
7124 | 0 | } |
7125 | | |
7126 | | /* |
7127 | | * Fetch a set of offsets or lengths. |
7128 | | * While this routine says "strips", in fact it's also used for tiles. |
7129 | | */ |
7130 | | static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips, |
7131 | | uint64_t **lpp) |
7132 | 0 | { |
7133 | 0 | static const char module[] = "TIFFFetchStripThing"; |
7134 | 0 | enum TIFFReadDirEntryErr err; |
7135 | 0 | uint64_t *data; |
7136 | 0 | err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips); |
7137 | 0 | if (err != TIFFReadDirEntryErrOk) |
7138 | 0 | { |
7139 | 0 | const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); |
7140 | 0 | TIFFReadDirEntryOutputErr(tif, err, module, |
7141 | 0 | fip ? fip->field_name : "unknown tagname", 0); |
7142 | 0 | return (0); |
7143 | 0 | } |
7144 | 0 | if (dir->tdir_count < (uint64_t)nstrips) |
7145 | 0 | { |
7146 | 0 | uint64_t *resizeddata; |
7147 | 0 | const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag); |
7148 | 0 | const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT"); |
7149 | 0 | uint32_t max_nstrips = 1000000; |
7150 | 0 | if (pszMax) |
7151 | 0 | max_nstrips = (uint32_t)atoi(pszMax); |
7152 | 0 | TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module, |
7153 | 0 | fip ? fip->field_name : "unknown tagname", |
7154 | 0 | (nstrips <= max_nstrips)); |
7155 | |
|
7156 | 0 | if (nstrips > max_nstrips) |
7157 | 0 | { |
7158 | 0 | _TIFFfreeExt(tif, data); |
7159 | 0 | return (0); |
7160 | 0 | } |
7161 | | |
7162 | 0 | resizeddata = (uint64_t *)_TIFFCheckMalloc( |
7163 | 0 | tif, nstrips, sizeof(uint64_t), "for strip array"); |
7164 | 0 | if (resizeddata == 0) |
7165 | 0 | { |
7166 | 0 | _TIFFfreeExt(tif, data); |
7167 | 0 | return (0); |
7168 | 0 | } |
7169 | 0 | if (dir->tdir_count) |
7170 | 0 | _TIFFmemcpy(resizeddata, data, |
7171 | 0 | (uint32_t)dir->tdir_count * sizeof(uint64_t)); |
7172 | 0 | _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0, |
7173 | 0 | (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t)); |
7174 | 0 | _TIFFfreeExt(tif, data); |
7175 | 0 | data = resizeddata; |
7176 | 0 | } |
7177 | 0 | *lpp = data; |
7178 | 0 | return (1); |
7179 | 0 | } |
7180 | | |
7181 | | /* |
7182 | | * Fetch and set the SubjectDistance EXIF tag. |
7183 | | */ |
7184 | | static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir) |
7185 | 0 | { |
7186 | 0 | static const char module[] = "TIFFFetchSubjectDistance"; |
7187 | 0 | enum TIFFReadDirEntryErr err; |
7188 | 0 | UInt64Aligned_t m; |
7189 | 0 | m.l = 0; |
7190 | 0 | assert(sizeof(double) == 8); |
7191 | 0 | assert(sizeof(uint64_t) == 8); |
7192 | 0 | assert(sizeof(uint32_t) == 4); |
7193 | 0 | if (dir->tdir_count != 1) |
7194 | 0 | err = TIFFReadDirEntryErrCount; |
7195 | 0 | else if (dir->tdir_type != TIFF_RATIONAL) |
7196 | 0 | err = TIFFReadDirEntryErrType; |
7197 | 0 | else |
7198 | 0 | { |
7199 | 0 | if (!(tif->tif_flags & TIFF_BIGTIFF)) |
7200 | 0 | { |
7201 | 0 | uint32_t offset; |
7202 | 0 | offset = *(uint32_t *)(&dir->tdir_offset); |
7203 | 0 | if (tif->tif_flags & TIFF_SWAB) |
7204 | 0 | TIFFSwabLong(&offset); |
7205 | 0 | err = TIFFReadDirEntryData(tif, offset, 8, m.i); |
7206 | 0 | } |
7207 | 0 | else |
7208 | 0 | { |
7209 | 0 | m.l = dir->tdir_offset.toff_long8; |
7210 | 0 | err = TIFFReadDirEntryErrOk; |
7211 | 0 | } |
7212 | 0 | } |
7213 | 0 | if (err == TIFFReadDirEntryErrOk) |
7214 | 0 | { |
7215 | 0 | double n; |
7216 | 0 | if (tif->tif_flags & TIFF_SWAB) |
7217 | 0 | TIFFSwabArrayOfLong(m.i, 2); |
7218 | 0 | if (m.i[0] == 0) |
7219 | 0 | n = 0.0; |
7220 | 0 | else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0) |
7221 | | /* |
7222 | | * XXX: Numerator 0xFFFFFFFF means that we have infinite |
7223 | | * distance. Indicate that with a negative floating point |
7224 | | * SubjectDistance value. |
7225 | | */ |
7226 | 0 | n = -1.0; |
7227 | 0 | else |
7228 | 0 | n = (double)m.i[0] / (double)m.i[1]; |
7229 | 0 | return (TIFFSetField(tif, dir->tdir_tag, n)); |
7230 | 0 | } |
7231 | 0 | else |
7232 | 0 | { |
7233 | 0 | TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE); |
7234 | 0 | return (0); |
7235 | 0 | } |
7236 | 0 | } |
7237 | | |
7238 | | static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips, |
7239 | | uint64_t stripbytes, |
7240 | | uint32_t rowsperstrip) |
7241 | 0 | { |
7242 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7243 | 0 | uint64_t bytecount; |
7244 | 0 | uint64_t offset; |
7245 | 0 | uint64_t last_offset; |
7246 | 0 | uint64_t last_bytecount; |
7247 | 0 | uint32_t i; |
7248 | 0 | uint64_t *newcounts; |
7249 | 0 | uint64_t *newoffsets; |
7250 | |
|
7251 | 0 | offset = TIFFGetStrileOffset(tif, 0); |
7252 | 0 | last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1); |
7253 | 0 | last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1); |
7254 | 0 | if (last_offset > UINT64_MAX - last_bytecount || |
7255 | 0 | last_offset + last_bytecount < offset) |
7256 | 0 | { |
7257 | 0 | return; |
7258 | 0 | } |
7259 | 0 | bytecount = last_offset + last_bytecount - offset; |
7260 | |
|
7261 | 0 | newcounts = |
7262 | 0 | (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t), |
7263 | 0 | "for chopped \"StripByteCounts\" array"); |
7264 | 0 | newoffsets = (uint64_t *)_TIFFCheckMalloc( |
7265 | 0 | tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array"); |
7266 | 0 | if (newcounts == NULL || newoffsets == NULL) |
7267 | 0 | { |
7268 | | /* |
7269 | | * Unable to allocate new strip information, give up and use |
7270 | | * the original one strip information. |
7271 | | */ |
7272 | 0 | if (newcounts != NULL) |
7273 | 0 | _TIFFfreeExt(tif, newcounts); |
7274 | 0 | if (newoffsets != NULL) |
7275 | 0 | _TIFFfreeExt(tif, newoffsets); |
7276 | 0 | return; |
7277 | 0 | } |
7278 | | |
7279 | | /* |
7280 | | * Fill the strip information arrays with new bytecounts and offsets |
7281 | | * that reflect the broken-up format. |
7282 | | */ |
7283 | 0 | for (i = 0; i < nstrips; i++) |
7284 | 0 | { |
7285 | 0 | if (stripbytes > bytecount) |
7286 | 0 | stripbytes = bytecount; |
7287 | 0 | newcounts[i] = stripbytes; |
7288 | 0 | newoffsets[i] = stripbytes ? offset : 0; |
7289 | 0 | offset += stripbytes; |
7290 | 0 | bytecount -= stripbytes; |
7291 | 0 | } |
7292 | | |
7293 | | /* |
7294 | | * Replace old single strip info with multi-strip info. |
7295 | | */ |
7296 | 0 | td->td_stripsperimage = td->td_nstrips = nstrips; |
7297 | 0 | TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
7298 | |
|
7299 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
7300 | 0 | _TIFFfreeExt(tif, td->td_stripoffset_p); |
7301 | 0 | td->td_stripbytecount_p = newcounts; |
7302 | 0 | td->td_stripoffset_p = newoffsets; |
7303 | | #ifdef STRIPBYTECOUNTSORTED_UNUSED |
7304 | | td->td_stripbytecountsorted = 1; |
7305 | | #endif |
7306 | 0 | tif->tif_flags |= TIFF_CHOPPEDUPARRAYS; |
7307 | 0 | } |
7308 | | |
7309 | | /* |
7310 | | * Replace a single strip (tile) of uncompressed data by multiple strips |
7311 | | * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for |
7312 | | * dealing with large images or for dealing with machines with a limited |
7313 | | * amount memory. |
7314 | | */ |
7315 | | static void ChopUpSingleUncompressedStrip(TIFF *tif) |
7316 | 0 | { |
7317 | 0 | register TIFFDirectory *td = &tif->tif_dir; |
7318 | 0 | uint64_t bytecount; |
7319 | 0 | uint64_t offset; |
7320 | 0 | uint32_t rowblock; |
7321 | 0 | uint64_t rowblockbytes; |
7322 | 0 | uint64_t stripbytes; |
7323 | 0 | uint32_t nstrips; |
7324 | 0 | uint32_t rowsperstrip; |
7325 | |
|
7326 | 0 | bytecount = TIFFGetStrileByteCount(tif, 0); |
7327 | | /* On a newly created file, just re-opened to be filled, we */ |
7328 | | /* don't want strip chop to trigger as it is going to cause issues */ |
7329 | | /* later ( StripOffsets and StripByteCounts improperly filled) . */ |
7330 | 0 | if (bytecount == 0 && tif->tif_mode != O_RDONLY) |
7331 | 0 | return; |
7332 | 0 | offset = TIFFGetStrileByteCount(tif, 0); |
7333 | 0 | assert(td->td_planarconfig == PLANARCONFIG_CONTIG); |
7334 | 0 | if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) |
7335 | 0 | rowblock = td->td_ycbcrsubsampling[1]; |
7336 | 0 | else |
7337 | 0 | rowblock = 1; |
7338 | 0 | rowblockbytes = TIFFVTileSize64(tif, rowblock); |
7339 | | /* |
7340 | | * Make the rows hold at least one scanline, but fill specified amount |
7341 | | * of data if possible. |
7342 | | */ |
7343 | 0 | if (rowblockbytes > STRIP_SIZE_DEFAULT) |
7344 | 0 | { |
7345 | 0 | stripbytes = rowblockbytes; |
7346 | 0 | rowsperstrip = rowblock; |
7347 | 0 | } |
7348 | 0 | else if (rowblockbytes > 0) |
7349 | 0 | { |
7350 | 0 | uint32_t rowblocksperstrip; |
7351 | 0 | rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes); |
7352 | 0 | rowsperstrip = rowblocksperstrip * rowblock; |
7353 | 0 | stripbytes = rowblocksperstrip * rowblockbytes; |
7354 | 0 | } |
7355 | 0 | else |
7356 | 0 | return; |
7357 | | |
7358 | | /* |
7359 | | * never increase the number of rows per strip |
7360 | | */ |
7361 | 0 | if (rowsperstrip >= td->td_rowsperstrip) |
7362 | 0 | return; |
7363 | 0 | nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip); |
7364 | 0 | if (nstrips == 0) |
7365 | 0 | return; |
7366 | | |
7367 | | /* If we are going to allocate a lot of memory, make sure that the */ |
7368 | | /* file is as big as needed */ |
7369 | 0 | if (tif->tif_mode == O_RDONLY && nstrips > 1000000 && |
7370 | 0 | (offset >= TIFFGetFileSize(tif) || |
7371 | 0 | stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1))) |
7372 | 0 | { |
7373 | 0 | return; |
7374 | 0 | } |
7375 | | |
7376 | 0 | allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip); |
7377 | 0 | } |
7378 | | |
7379 | | /* |
7380 | | * Replace a file with contiguous strips > 2 GB of uncompressed data by |
7381 | | * multiple smaller strips. This is useful for |
7382 | | * dealing with large images or for dealing with machines with a limited |
7383 | | * amount memory. |
7384 | | */ |
7385 | | static void TryChopUpUncompressedBigTiff(TIFF *tif) |
7386 | 0 | { |
7387 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7388 | 0 | uint32_t rowblock; |
7389 | 0 | uint64_t rowblockbytes; |
7390 | 0 | uint32_t i; |
7391 | 0 | uint64_t stripsize; |
7392 | 0 | uint32_t rowblocksperstrip; |
7393 | 0 | uint32_t rowsperstrip; |
7394 | 0 | uint64_t stripbytes; |
7395 | 0 | uint32_t nstrips; |
7396 | |
|
7397 | 0 | stripsize = TIFFStripSize64(tif); |
7398 | |
|
7399 | 0 | assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG); |
7400 | 0 | assert(tif->tif_dir.td_compression == COMPRESSION_NONE); |
7401 | 0 | assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == |
7402 | 0 | TIFF_STRIPCHOP); |
7403 | 0 | assert(stripsize > 0x7FFFFFFFUL); |
7404 | | |
7405 | | /* On a newly created file, just re-opened to be filled, we */ |
7406 | | /* don't want strip chop to trigger as it is going to cause issues */ |
7407 | | /* later ( StripOffsets and StripByteCounts improperly filled) . */ |
7408 | 0 | if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY) |
7409 | 0 | return; |
7410 | | |
7411 | 0 | if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) |
7412 | 0 | rowblock = td->td_ycbcrsubsampling[1]; |
7413 | 0 | else |
7414 | 0 | rowblock = 1; |
7415 | 0 | rowblockbytes = TIFFVStripSize64(tif, rowblock); |
7416 | 0 | if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL) |
7417 | 0 | { |
7418 | | /* In case of file with gigantic width */ |
7419 | 0 | return; |
7420 | 0 | } |
7421 | | |
7422 | | /* Check that the strips are contiguous and of the expected size */ |
7423 | 0 | for (i = 0; i < td->td_nstrips; i++) |
7424 | 0 | { |
7425 | 0 | if (i == td->td_nstrips - 1) |
7426 | 0 | { |
7427 | 0 | if (TIFFGetStrileByteCount(tif, i) < |
7428 | 0 | TIFFVStripSize64(tif, |
7429 | 0 | td->td_imagelength - i * td->td_rowsperstrip)) |
7430 | 0 | { |
7431 | 0 | return; |
7432 | 0 | } |
7433 | 0 | } |
7434 | 0 | else |
7435 | 0 | { |
7436 | 0 | if (TIFFGetStrileByteCount(tif, i) != stripsize) |
7437 | 0 | { |
7438 | 0 | return; |
7439 | 0 | } |
7440 | 0 | if (i > 0 && TIFFGetStrileOffset(tif, i) != |
7441 | 0 | TIFFGetStrileOffset(tif, i - 1) + |
7442 | 0 | TIFFGetStrileByteCount(tif, i - 1)) |
7443 | 0 | { |
7444 | 0 | return; |
7445 | 0 | } |
7446 | 0 | } |
7447 | 0 | } |
7448 | | |
7449 | | /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */ |
7450 | 0 | rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes); |
7451 | 0 | if (rowblocksperstrip == 0) |
7452 | 0 | rowblocksperstrip = 1; |
7453 | 0 | rowsperstrip = rowblocksperstrip * rowblock; |
7454 | 0 | stripbytes = rowblocksperstrip * rowblockbytes; |
7455 | 0 | assert(stripbytes <= 0x7FFFFFFFUL); |
7456 | | |
7457 | 0 | nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip); |
7458 | 0 | if (nstrips == 0) |
7459 | 0 | return; |
7460 | | |
7461 | | /* If we are going to allocate a lot of memory, make sure that the */ |
7462 | | /* file is as big as needed */ |
7463 | 0 | if (tif->tif_mode == O_RDONLY && nstrips > 1000000) |
7464 | 0 | { |
7465 | 0 | uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1); |
7466 | 0 | uint64_t filesize = TIFFGetFileSize(tif); |
7467 | 0 | uint64_t last_bytecount = |
7468 | 0 | TIFFGetStrileByteCount(tif, td->td_nstrips - 1); |
7469 | 0 | if (last_offset > filesize || last_bytecount > filesize - last_offset) |
7470 | 0 | { |
7471 | 0 | return; |
7472 | 0 | } |
7473 | 0 | } |
7474 | | |
7475 | 0 | allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip); |
7476 | 0 | } |
7477 | | |
7478 | | TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW |
7479 | | static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b) |
7480 | 0 | { |
7481 | 0 | return a + b; |
7482 | 0 | } |
7483 | | |
7484 | | /* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around |
7485 | | * strip/tile of number strile. Also fetch the neighbouring values using a |
7486 | | * 4096 byte page size. |
7487 | | */ |
7488 | | static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent, |
7489 | | int strile, uint64_t *panVals) |
7490 | 0 | { |
7491 | 0 | static const char module[] = "_TIFFPartialReadStripArray"; |
7492 | 0 | #define IO_CACHE_PAGE_SIZE 4096 |
7493 | |
|
7494 | 0 | size_t sizeofval; |
7495 | 0 | const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0; |
7496 | 0 | int sizeofvalint; |
7497 | 0 | uint64_t nBaseOffset; |
7498 | 0 | uint64_t nOffset; |
7499 | 0 | uint64_t nOffsetStartPage; |
7500 | 0 | uint64_t nOffsetEndPage; |
7501 | 0 | tmsize_t nToRead; |
7502 | 0 | tmsize_t nRead; |
7503 | 0 | uint64_t nLastStripOffset; |
7504 | 0 | int iStartBefore; |
7505 | 0 | int i; |
7506 | 0 | const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize; |
7507 | 0 | unsigned char buffer[2 * IO_CACHE_PAGE_SIZE]; |
7508 | |
|
7509 | 0 | assert(dirent->tdir_count > 4); |
7510 | | |
7511 | 0 | if (dirent->tdir_type == TIFF_SHORT) |
7512 | 0 | { |
7513 | 0 | sizeofval = sizeof(uint16_t); |
7514 | 0 | } |
7515 | 0 | else if (dirent->tdir_type == TIFF_LONG) |
7516 | 0 | { |
7517 | 0 | sizeofval = sizeof(uint32_t); |
7518 | 0 | } |
7519 | 0 | else if (dirent->tdir_type == TIFF_LONG8) |
7520 | 0 | { |
7521 | 0 | sizeofval = sizeof(uint64_t); |
7522 | 0 | } |
7523 | 0 | else if (dirent->tdir_type == TIFF_SLONG8) |
7524 | 0 | { |
7525 | | /* Non conformant but used by some images as in */ |
7526 | | /* https://github.com/OSGeo/gdal/issues/2165 */ |
7527 | 0 | sizeofval = sizeof(int64_t); |
7528 | 0 | } |
7529 | 0 | else |
7530 | 0 | { |
7531 | 0 | TIFFErrorExtR(tif, module, |
7532 | 0 | "Invalid type for [Strip|Tile][Offset/ByteCount] tag"); |
7533 | 0 | panVals[strile] = 0; |
7534 | 0 | return 0; |
7535 | 0 | } |
7536 | 0 | sizeofvalint = (int)(sizeofval); |
7537 | |
|
7538 | 0 | if (tif->tif_flags & TIFF_BIGTIFF) |
7539 | 0 | { |
7540 | 0 | uint64_t offset = dirent->tdir_offset.toff_long8; |
7541 | 0 | if (bSwab) |
7542 | 0 | TIFFSwabLong8(&offset); |
7543 | 0 | nBaseOffset = offset; |
7544 | 0 | } |
7545 | 0 | else |
7546 | 0 | { |
7547 | 0 | uint32_t offset = dirent->tdir_offset.toff_long; |
7548 | 0 | if (bSwab) |
7549 | 0 | TIFFSwabLong(&offset); |
7550 | 0 | nBaseOffset = offset; |
7551 | 0 | } |
7552 | | /* To avoid later unsigned integer overflows */ |
7553 | 0 | if (nBaseOffset > (uint64_t)INT64_MAX) |
7554 | 0 | { |
7555 | 0 | TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d", |
7556 | 0 | strile); |
7557 | 0 | panVals[strile] = 0; |
7558 | 0 | return 0; |
7559 | 0 | } |
7560 | 0 | nOffset = nBaseOffset + sizeofval * strile; |
7561 | 0 | nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE; |
7562 | 0 | nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE; |
7563 | |
|
7564 | 0 | if (nOffset + sizeofval > nOffsetEndPage) |
7565 | 0 | nOffsetEndPage += IO_CACHE_PAGE_SIZE; |
7566 | 0 | #undef IO_CACHE_PAGE_SIZE |
7567 | |
|
7568 | 0 | nLastStripOffset = nBaseOffset + arraySize * sizeofval; |
7569 | 0 | if (nLastStripOffset < nOffsetEndPage) |
7570 | 0 | nOffsetEndPage = nLastStripOffset; |
7571 | 0 | if (nOffsetStartPage >= nOffsetEndPage) |
7572 | 0 | { |
7573 | 0 | TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d", |
7574 | 0 | strile); |
7575 | 0 | panVals[strile] = 0; |
7576 | 0 | return 0; |
7577 | 0 | } |
7578 | 0 | if (!SeekOK(tif, nOffsetStartPage)) |
7579 | 0 | { |
7580 | 0 | panVals[strile] = 0; |
7581 | 0 | return 0; |
7582 | 0 | } |
7583 | | |
7584 | 0 | nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage); |
7585 | 0 | nRead = TIFFReadFile(tif, buffer, nToRead); |
7586 | 0 | if (nRead < nToRead) |
7587 | 0 | { |
7588 | 0 | TIFFErrorExtR(tif, module, |
7589 | 0 | "Cannot read offset/size for strile around ~%d", strile); |
7590 | 0 | return 0; |
7591 | 0 | } |
7592 | 0 | iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval); |
7593 | 0 | if (strile + iStartBefore < 0) |
7594 | 0 | iStartBefore = -strile; |
7595 | 0 | for (i = iStartBefore; |
7596 | 0 | (uint32_t)(strile + i) < arraySize && |
7597 | 0 | _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <= |
7598 | 0 | nOffsetEndPage; |
7599 | 0 | ++i) |
7600 | 0 | { |
7601 | 0 | if (dirent->tdir_type == TIFF_SHORT) |
7602 | 0 | { |
7603 | 0 | uint16_t val; |
7604 | 0 | memcpy(&val, |
7605 | 0 | buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, |
7606 | 0 | sizeof(val)); |
7607 | 0 | if (bSwab) |
7608 | 0 | TIFFSwabShort(&val); |
7609 | 0 | panVals[strile + i] = val; |
7610 | 0 | } |
7611 | 0 | else if (dirent->tdir_type == TIFF_LONG) |
7612 | 0 | { |
7613 | 0 | uint32_t val; |
7614 | 0 | memcpy(&val, |
7615 | 0 | buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, |
7616 | 0 | sizeof(val)); |
7617 | 0 | if (bSwab) |
7618 | 0 | TIFFSwabLong(&val); |
7619 | 0 | panVals[strile + i] = val; |
7620 | 0 | } |
7621 | 0 | else if (dirent->tdir_type == TIFF_LONG8) |
7622 | 0 | { |
7623 | 0 | uint64_t val; |
7624 | 0 | memcpy(&val, |
7625 | 0 | buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, |
7626 | 0 | sizeof(val)); |
7627 | 0 | if (bSwab) |
7628 | 0 | TIFFSwabLong8(&val); |
7629 | 0 | panVals[strile + i] = val; |
7630 | 0 | } |
7631 | 0 | else /* if( dirent->tdir_type == TIFF_SLONG8 ) */ |
7632 | 0 | { |
7633 | | /* Non conformant data type */ |
7634 | 0 | int64_t val; |
7635 | 0 | memcpy(&val, |
7636 | 0 | buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint, |
7637 | 0 | sizeof(val)); |
7638 | 0 | if (bSwab) |
7639 | 0 | TIFFSwabLong8((uint64_t *)&val); |
7640 | 0 | panVals[strile + i] = (uint64_t)val; |
7641 | 0 | } |
7642 | 0 | } |
7643 | 0 | return 1; |
7644 | 0 | } |
7645 | | |
7646 | | static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile, |
7647 | | TIFFDirEntry *dirent, uint64_t **parray) |
7648 | 0 | { |
7649 | 0 | static const char module[] = "_TIFFFetchStrileValue"; |
7650 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7651 | 0 | if (strile >= dirent->tdir_count) |
7652 | 0 | { |
7653 | 0 | return 0; |
7654 | 0 | } |
7655 | 0 | if (strile >= td->td_stripoffsetbyteallocsize) |
7656 | 0 | { |
7657 | 0 | uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize; |
7658 | 0 | uint32_t nStripArrayAllocNew; |
7659 | 0 | uint64_t nArraySize64; |
7660 | 0 | size_t nArraySize; |
7661 | 0 | uint64_t *offsetArray; |
7662 | 0 | uint64_t *bytecountArray; |
7663 | |
|
7664 | 0 | if (strile > 1000000) |
7665 | 0 | { |
7666 | 0 | uint64_t filesize = TIFFGetFileSize(tif); |
7667 | | /* Avoid excessive memory allocation attempt */ |
7668 | | /* For such a big blockid we need at least a TIFF_LONG per strile */ |
7669 | | /* for the offset array. */ |
7670 | 0 | if (strile > filesize / sizeof(uint32_t)) |
7671 | 0 | { |
7672 | 0 | TIFFErrorExtR(tif, module, "File too short"); |
7673 | 0 | return 0; |
7674 | 0 | } |
7675 | 0 | } |
7676 | | |
7677 | 0 | if (td->td_stripoffsetbyteallocsize == 0 && |
7678 | 0 | td->td_nstrips < 1024 * 1024) |
7679 | 0 | { |
7680 | 0 | nStripArrayAllocNew = td->td_nstrips; |
7681 | 0 | } |
7682 | 0 | else |
7683 | 0 | { |
7684 | 0 | #define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b)) |
7685 | 0 | #define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b)) |
7686 | 0 | nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U); |
7687 | 0 | if (nStripArrayAllocNew < 0xFFFFFFFFU / 2) |
7688 | 0 | nStripArrayAllocNew *= 2; |
7689 | 0 | nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips); |
7690 | 0 | } |
7691 | 0 | assert(strile < nStripArrayAllocNew); |
7692 | 0 | nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew; |
7693 | 0 | nArraySize = (size_t)(nArraySize64); |
7694 | | #if SIZEOF_SIZE_T == 4 |
7695 | | if (nArraySize != nArraySize64) |
7696 | | { |
7697 | | TIFFErrorExtR(tif, module, |
7698 | | "Cannot allocate strip offset and bytecount arrays"); |
7699 | | return 0; |
7700 | | } |
7701 | | #endif |
7702 | 0 | offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p, |
7703 | 0 | nArraySize)); |
7704 | 0 | bytecountArray = (uint64_t *)(_TIFFreallocExt( |
7705 | 0 | tif, td->td_stripbytecount_p, nArraySize)); |
7706 | 0 | if (offsetArray) |
7707 | 0 | td->td_stripoffset_p = offsetArray; |
7708 | 0 | if (bytecountArray) |
7709 | 0 | td->td_stripbytecount_p = bytecountArray; |
7710 | 0 | if (offsetArray && bytecountArray) |
7711 | 0 | { |
7712 | 0 | td->td_stripoffsetbyteallocsize = nStripArrayAllocNew; |
7713 | | /* Initialize new entries to ~0 / -1 */ |
7714 | | /* coverity[overrun-buffer-arg] */ |
7715 | 0 | memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF, |
7716 | 0 | (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * |
7717 | 0 | sizeof(uint64_t)); |
7718 | | /* coverity[overrun-buffer-arg] */ |
7719 | 0 | memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF, |
7720 | 0 | (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) * |
7721 | 0 | sizeof(uint64_t)); |
7722 | 0 | } |
7723 | 0 | else |
7724 | 0 | { |
7725 | 0 | TIFFErrorExtR(tif, module, |
7726 | 0 | "Cannot allocate strip offset and bytecount arrays"); |
7727 | 0 | _TIFFfreeExt(tif, td->td_stripoffset_p); |
7728 | 0 | td->td_stripoffset_p = NULL; |
7729 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
7730 | 0 | td->td_stripbytecount_p = NULL; |
7731 | 0 | td->td_stripoffsetbyteallocsize = 0; |
7732 | 0 | } |
7733 | 0 | } |
7734 | 0 | if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize) |
7735 | 0 | return 0; |
7736 | | |
7737 | 0 | if (~((*parray)[strile]) == 0) |
7738 | 0 | { |
7739 | 0 | if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray)) |
7740 | 0 | { |
7741 | 0 | (*parray)[strile] = 0; |
7742 | 0 | return 0; |
7743 | 0 | } |
7744 | 0 | } |
7745 | | |
7746 | 0 | return 1; |
7747 | 0 | } |
7748 | | |
7749 | | static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile, |
7750 | | TIFFDirEntry *dirent, |
7751 | | uint64_t **parray, |
7752 | | int *pbErr) |
7753 | 0 | { |
7754 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7755 | 0 | if (pbErr) |
7756 | 0 | *pbErr = 0; |
7757 | 0 | if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) && |
7758 | 0 | !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS)) |
7759 | 0 | { |
7760 | 0 | if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) || |
7761 | | /* If the values may fit in the toff_long/toff_long8 member */ |
7762 | | /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */ |
7763 | 0 | dirent->tdir_count <= 4) |
7764 | 0 | { |
7765 | 0 | if (!_TIFFFillStriles(tif)) |
7766 | 0 | { |
7767 | 0 | if (pbErr) |
7768 | 0 | *pbErr = 1; |
7769 | | /* Do not return, as we want this function to always */ |
7770 | | /* return the same value if called several times with */ |
7771 | | /* the same arguments */ |
7772 | 0 | } |
7773 | 0 | } |
7774 | 0 | else |
7775 | 0 | { |
7776 | 0 | if (!_TIFFFetchStrileValue(tif, strile, dirent, parray)) |
7777 | 0 | { |
7778 | 0 | if (pbErr) |
7779 | 0 | *pbErr = 1; |
7780 | 0 | return 0; |
7781 | 0 | } |
7782 | 0 | } |
7783 | 0 | } |
7784 | 0 | if (*parray == NULL || strile >= td->td_nstrips) |
7785 | 0 | { |
7786 | 0 | if (pbErr) |
7787 | 0 | *pbErr = 1; |
7788 | 0 | return 0; |
7789 | 0 | } |
7790 | 0 | return (*parray)[strile]; |
7791 | 0 | } |
7792 | | |
7793 | | /* Return the value of the TileOffsets/StripOffsets array for the specified |
7794 | | * tile/strile */ |
7795 | | uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile) |
7796 | 0 | { |
7797 | 0 | return TIFFGetStrileOffsetWithErr(tif, strile, NULL); |
7798 | 0 | } |
7799 | | |
7800 | | /* Return the value of the TileOffsets/StripOffsets array for the specified |
7801 | | * tile/strile */ |
7802 | | uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr) |
7803 | 0 | { |
7804 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7805 | 0 | return _TIFFGetStrileOffsetOrByteCountValue(tif, strile, |
7806 | 0 | &(td->td_stripoffset_entry), |
7807 | 0 | &(td->td_stripoffset_p), pbErr); |
7808 | 0 | } |
7809 | | |
7810 | | /* Return the value of the TileByteCounts/StripByteCounts array for the |
7811 | | * specified tile/strile */ |
7812 | | uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile) |
7813 | 0 | { |
7814 | 0 | return TIFFGetStrileByteCountWithErr(tif, strile, NULL); |
7815 | 0 | } |
7816 | | |
7817 | | /* Return the value of the TileByteCounts/StripByteCounts array for the |
7818 | | * specified tile/strile */ |
7819 | | uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr) |
7820 | 0 | { |
7821 | 0 | TIFFDirectory *td = &tif->tif_dir; |
7822 | 0 | return _TIFFGetStrileOffsetOrByteCountValue( |
7823 | 0 | tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p), |
7824 | 0 | pbErr); |
7825 | 0 | } |
7826 | | |
7827 | 0 | int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); } |
7828 | | |
7829 | | static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount) |
7830 | 0 | { |
7831 | 0 | register TIFFDirectory *td = &tif->tif_dir; |
7832 | 0 | int return_value = 1; |
7833 | | |
7834 | | /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */ |
7835 | 0 | if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) || |
7836 | 0 | (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0) |
7837 | 0 | return 1; |
7838 | | |
7839 | 0 | if (tif->tif_flags & TIFF_LAZYSTRILELOAD) |
7840 | 0 | { |
7841 | | /* In case of lazy loading, reload completely the arrays */ |
7842 | 0 | _TIFFfreeExt(tif, td->td_stripoffset_p); |
7843 | 0 | _TIFFfreeExt(tif, td->td_stripbytecount_p); |
7844 | 0 | td->td_stripoffset_p = NULL; |
7845 | 0 | td->td_stripbytecount_p = NULL; |
7846 | 0 | td->td_stripoffsetbyteallocsize = 0; |
7847 | 0 | tif->tif_flags &= ~TIFF_LAZYSTRILELOAD; |
7848 | 0 | } |
7849 | | |
7850 | | /* If stripoffset array is already loaded, exit with success */ |
7851 | 0 | if (td->td_stripoffset_p != NULL) |
7852 | 0 | return 1; |
7853 | | |
7854 | | /* If tdir_count was canceled, then we already got there, but in error */ |
7855 | 0 | if (td->td_stripoffset_entry.tdir_count == 0) |
7856 | 0 | return 0; |
7857 | | |
7858 | 0 | if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips, |
7859 | 0 | &td->td_stripoffset_p)) |
7860 | 0 | { |
7861 | 0 | return_value = 0; |
7862 | 0 | } |
7863 | |
|
7864 | 0 | if (loadStripByteCount && |
7865 | 0 | !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry), |
7866 | 0 | td->td_nstrips, &td->td_stripbytecount_p)) |
7867 | 0 | { |
7868 | 0 | return_value = 0; |
7869 | 0 | } |
7870 | |
|
7871 | 0 | _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry)); |
7872 | 0 | _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry)); |
7873 | |
|
7874 | | #ifdef STRIPBYTECOUNTSORTED_UNUSED |
7875 | | if (tif->tif_dir.td_nstrips > 1 && return_value == 1) |
7876 | | { |
7877 | | uint32_t strip; |
7878 | | |
7879 | | tif->tif_dir.td_stripbytecountsorted = 1; |
7880 | | for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) |
7881 | | { |
7882 | | if (tif->tif_dir.td_stripoffset_p[strip - 1] > |
7883 | | tif->tif_dir.td_stripoffset_p[strip]) |
7884 | | { |
7885 | | tif->tif_dir.td_stripbytecountsorted = 0; |
7886 | | break; |
7887 | | } |
7888 | | } |
7889 | | } |
7890 | | #endif |
7891 | |
|
7892 | 0 | return return_value; |
7893 | 0 | } |