Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/pds/tif_pdsdirread.c

raw
1 /*
2 * Copyright (c) 1988-1996 Sam Leffler
3 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
4 * Copyright (c( 1996 USAF Phillips Laboratory
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26 /*
27 * TIFF Library.
28 *
29 * These routines written by Conrad J. Poelman on a single late-night of
30 * March 20-21, 1996.
31 *
32 * The entire purpose of this file is to provide a single external function,
33 * TIFFReadPrivateDataSubDirectory(). This function is intended for use in
34 * reading a private subdirectory from a TIFF file into a private structure. The
35 * actual writing of data into the structure is handled by the setFieldFn(),
36 * which is passed to TIFFReadPrivateDataSubDirectory() as a parameter. The idea
37 * is to enable any application wishing to store private subdirectories to do so
38 * easily using this function, without modifying the TIFF library.
39 *
40 * The astute observer will notice that only two functions are at all different
41 * from the original tif_dirread.c file: TIFFReadPrivateDataSubDirectory() and
42 * TIFFFetchNormalSubTag(). All the other stuff that makes this file so huge
43 * is only necessary because all of those functions are declared static in
44 * tif_dirread.c, so we have to totally duplicate them in order to use them.
45 *
46 * Oh, also note the bug fix in TIFFFetchFloat().
47 *
48 */
49
50 #include "tiffiop.h"
51
52 #define IGNORE 0 /* tag placeholder used below */
53
54 #if HAVE_IEEEFP
55 #define TIFFCvtIEEEFloatToNative(tif, n, fp)
56 #define TIFFCvtIEEEDoubleToNative(tif, n, dp)
57 #else
58 extern void TIFFCvtIEEEFloatToNative(TIFF *, uint32_t, float *);
59 extern void TIFFCvtIEEEDoubleToNative(TIFF *, uint32_t, double *);
60 #endif
61
62 static void EstimateStripByteCounts(TIFF *, TIFFDirEntry *, uint16_t);
63 static void MissingRequired(TIFF *, const char *);
64 static int CheckDirCount(TIFF *, TIFFDirEntry *, uint32_t);
65 static tsize_t TIFFFetchData(TIFF *, TIFFDirEntry *, char *);
66 static tsize_t TIFFFetchString(TIFF *, TIFFDirEntry *, char *);
67 static float TIFFFetchRational(TIFF *, TIFFDirEntry *);
68 static int TIFFFetchNormalSubTag(TIFF *, TIFFDirEntry *, const TIFFFieldInfo *,
69 int (*getFieldFn)(TIFF *tif, ttag_t tag, ...));
70 static int TIFFFetchPerSampleShorts(TIFF *, TIFFDirEntry *, int *);
71 static int TIFFFetchPerSampleAnys(TIFF *, TIFFDirEntry *, double *);
72 static int TIFFFetchShortArray(TIFF *, TIFFDirEntry *, uint16_t *);
73 static int TIFFFetchStripThing(TIFF *, TIFFDirEntry *, long, uint32_t **);
74 static int TIFFFetchExtraSamples(TIFF *, TIFFDirEntry *);
75 static int TIFFFetchRefBlackWhite(TIFF *, TIFFDirEntry *);
76 static float TIFFFetchFloat(TIFF *, TIFFDirEntry *);
77 static int TIFFFetchFloatArray(TIFF *, TIFFDirEntry *, float *);
78 static int TIFFFetchDoubleArray(TIFF *, TIFFDirEntry *, double *);
79 static int TIFFFetchAnyArray(TIFF *, TIFFDirEntry *, double *);
80 static int TIFFFetchShortPair(TIFF *, TIFFDirEntry *);
81 #if STRIPCHOP_SUPPORT
82 static void ChopUpSingleUncompressedStrip(TIFF *);
83 #endif
84
85 static char *CheckMalloc(TIFF *tif, tsize_t n, const char *what)
86 {
87 char *cp = (char *)_TIFFmalloc(n);
88 if (cp == NULL)
89 TIFFErrorExtR(tif, tif->tif_name, "No space %s", what);
90 return (cp);
91 }
92
93 /* Just as was done with TIFFWritePrivateDataSubDirectory(), here we implement
94 TIFFReadPrivateDataSubDirectory() which takes an offset into the TIFF file,
95 a TIFFFieldInfo structure specifying the types of the various tags,
96 and a function to use to set individual tags when they are encountered.
97 The data is read from the file, translated using the TIFF library's
98 built-in machine-independent conversion functions, and filled into
99 private subdirectory structure.
100
101 This code was written by copying the original TIFFReadDirectory() function
102 from tif_dirread.c and paring it down to what is needed for this.
103
104 It is the caller's responsibility to allocate and initialize the internal
105 structure that setFieldFn() will be writing into. If this function is being
106 called more than once before closing the file, the caller also must be
107 careful to free data in the structure before re-initializing.
108
109 It is also the caller's responsibility to verify the presence of
110 any required fields after reading the directory in.
111 */
112
113 int TIFFReadPrivateDataSubDirectory(TIFF *tif, toff_t pdir_offset,
114 TIFFFieldInfo *field_info,
115 int (*setFieldFn)(TIFF *tif, ttag_t tag,
116 ...))
117 {
118 register TIFFDirEntry *dp;
119 register int n;
120 register TIFFDirectory *td;
121 TIFFDirEntry *dir;
122 int iv;
123 long v;
124 double dv;
125 const TIFFFieldInfo *fip;
126 int fix;
127 uint16_t dircount;
128 uint32_t nextdiroff;
129 char *cp;
130 int diroutoforderwarning = 0;
131
132 /* Skipped part about checking for directories or compression data. */
133
134 if (!isMapped(tif))
135 {
136 if (!SeekOK(tif, pdir_offset))
137 {
138 TIFFErrorExtR(tif, tif->tif_name,
139 "Seek error accessing TIFF private subdirectory");
140 return (0);
141 }
142 if (!ReadOK(tif, &dircount, sizeof(uint16_t)))
143 {
144 TIFFErrorExtR(tif, tif->tif_name,
145 "Can not read TIFF private subdirectory count");
146 return (0);
147 }
148 if (tif->tif_flags & TIFF_SWAB)
149 TIFFSwabShort(&dircount);
150 dir = (TIFFDirEntry *)CheckMalloc(tif, dircount * sizeof(TIFFDirEntry),
151 "to read TIFF private subdirectory");
152 if (dir == NULL)
153 return (0);
154 if (!ReadOK(tif, dir, dircount * sizeof(TIFFDirEntry)))
155 {
156 TIFFErrorExtR(tif, tif->tif_name,
157 "Can not read TIFF private subdirectory");
158 goto bad;
159 }
160 /*
161 * Read offset to next directory for sequential scans.
162 */
163 (void)ReadOK(tif, &nextdiroff, sizeof(uint32_t));
164 }
165 else
166 {
167 toff_t off = pdir_offset;
168
169 if (off + sizeof(short) > tif->tif_size)
170 {
171 TIFFErrorExtR(tif, tif->tif_name,
172 "Can not read TIFF private subdirectory count");
173 return (0);
174 }
175 else
176 _TIFFmemcpy(&dircount, tif->tif_base + off, sizeof(uint16_t));
177 off += sizeof(uint16_t);
178 if (tif->tif_flags & TIFF_SWAB)
179 TIFFSwabShort(&dircount);
180 dir = (TIFFDirEntry *)CheckMalloc(tif, dircount * sizeof(TIFFDirEntry),
181 "to read TIFF private subdirectory");
182 if (dir == NULL)
183 return (0);
184 if (off + dircount * sizeof(TIFFDirEntry) > tif->tif_size)
185 {
186 TIFFErrorExtR(tif, tif->tif_name,
187 "Can not read TIFF private subdirectory");
188 goto bad;
189 }
190 else
191 _TIFFmemcpy(dir, tif->tif_base + off,
192 dircount * sizeof(TIFFDirEntry));
193 off += dircount * sizeof(TIFFDirEntry);
194 if (off + sizeof(uint32_t) < tif->tif_size)
195 _TIFFmemcpy(&nextdiroff, tif->tif_base + off, sizeof(uint32_t));
196 }
197 if (tif->tif_flags & TIFF_SWAB)
198 TIFFSwabLong(&nextdiroff);
199
200 /*
201 * Setup default value and then make a pass over
202 * the fields to check type and tag information,
203 * and to extract info required to size data
204 * structures. A second pass is made afterwards
205 * to read in everything not taken in the first pass.
206 */
207 td = &tif->tif_dir;
208
209 for (fip = field_info, dp = dir, n = dircount; n > 0; n--, dp++)
210 {
211 if (tif->tif_flags & TIFF_SWAB)
212 {
213 TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
214 TIFFSwabArrayOfLong(&dp->tdir_count, 2);
215 }
216 /*
217 * Find the field information entry for this tag.
218 */
219 /*
220 * Silicon Beach (at least) writes unordered
221 * directory tags (violating the spec). Handle
222 * it here, but be obnoxious (maybe they'll fix it?).
223 */
224 if (dp->tdir_tag < fip->field_tag)
225 {
226 if (!diroutoforderwarning)
227 {
228 TIFFWarning(tif->tif_name,
229 "invalid TIFF private subdirectory; tags are not "
230 "sorted in ascending order");
231 diroutoforderwarning = 1;
232 }
233 fip = field_info; /* O(n^2) */
234 }
235
236 while (fip->field_tag && fip->field_tag < dp->tdir_tag)
237 fip++;
238 if (!fip->field_tag || fip->field_tag != dp->tdir_tag)
239 {
240 TIFFWarning(tif->tif_name,
241 "unknown field with tag %d (0x%x) in private "
242 "subdirectory ignored",
243 dp->tdir_tag, dp->tdir_tag);
244 dp->tdir_tag = IGNORE;
245 fip = field_info; /* restart search */
246 continue;
247 }
248 /*
249 * Null out old tags that we ignore.
250 */
251
252 /* Not implemented yet, since FIELD_IGNORE is specific to
253 the main directories. Could pass this in too... */
254 if (0 /* && fip->field_bit == FIELD_IGNORE */)
255 {
256 ignore:
257 dp->tdir_tag = IGNORE;
258 continue;
259 }
260
261 /*
262 * Check data type.
263 */
264
265 while (dp->tdir_type != (u_short)fip->field_type)
266 {
267 if (fip->field_type == TIFF_ANY) /* wildcard */
268 break;
269 fip++;
270 if (!fip->field_tag || fip->field_tag != dp->tdir_tag)
271 {
272 TIFFWarning(tif->tif_name,
273 "wrong data type %d for \"%s\"; tag ignored",
274 dp->tdir_type, fip[-1].field_name);
275 goto ignore;
276 }
277 }
278 /*
279 * Check count if known in advance.
280 */
281 if (fip->field_readcount != TIFF_VARIABLE)
282 {
283 uint32_t expected = (fip->field_readcount == TIFF_SPP)
284 ? (uint32_t)td->td_samplesperpixel
285 : (uint32_t)fip->field_readcount;
286 if (!CheckDirCount(tif, dp, expected))
287 goto ignore;
288 }
289
290 /* Now read in and process data from field. */
291 if (!TIFFFetchNormalSubTag(tif, dp, fip, setFieldFn))
292 goto bad;
293 }
294
295 if (dir)
296 _TIFFfree(dir);
297 return (1);
298 bad:
299 if (dir)
300 _TIFFfree(dir);
301 return (0);
302 }
303
304 static void EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
305 uint16_t dircount)
306 {
307 register TIFFDirEntry *dp;
308 register TIFFDirectory *td = &tif->tif_dir;
309 uint16_t i;
310
311 if (td->td_stripbytecount)
312 _TIFFfree(td->td_stripbytecount);
313 td->td_stripbytecount =
314 (uint32_t *)CheckMalloc(tif, td->td_nstrips * sizeof(uint32_t),
315 "for \"StripByteCounts\" array");
316 if (td->td_compression != COMPRESSION_NONE)
317 {
318 uint32_t space =
319 (uint32_t)(sizeof(TIFFHeader) + sizeof(uint16_t) +
320 (dircount * sizeof(TIFFDirEntry)) + sizeof(uint32_t));
321 toff_t filesize = TIFFGetFileSize(tif);
322 uint16_t n;
323
324 /* calculate amount of space used by indirect values */
325 for (dp = dir, n = dircount; n > 0; n--, dp++)
326 {
327 uint32_t cc = dp->tdir_count * TIFFDataWidth(dp->tdir_type);
328 if (cc > sizeof(uint32_t))
329 space += cc;
330 }
331 space = (filesize - space) / td->td_samplesperpixel;
332 for (i = 0; i < td->td_nstrips; i++)
333 td->td_stripbytecount[i] = space;
334 /*
335 * This gross hack handles the case were the offset to
336 * the last strip is past the place where we think the strip
337 * should begin. Since a strip of data must be contiguous,
338 * it's safe to assume that we've overestimated the amount
339 * of data in the strip and trim this number back accordingly.
340 */
341 i--;
342 if (td->td_stripoffset[i] + td->td_stripbytecount[i] > filesize)
343 td->td_stripbytecount[i] = filesize - td->td_stripoffset[i];
344 }
345 else
346 {
347 uint32_t rowbytes = TIFFScanlineSize(tif);
348 uint32_t rowsperstrip = td->td_imagelength / td->td_nstrips;
349 for (i = 0; i < td->td_nstrips; i++)
350 td->td_stripbytecount[i] = rowbytes * rowsperstrip;
351 }
352 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
353 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
354 td->td_rowsperstrip = td->td_imagelength;
355 }
356
357 static void MissingRequired(TIFF *tif, const char *tagname)
358 {
359 TIFFErrorExtR(tif, tif->tif_name,
360 "TIFF directory is missing required \"%s\" field", tagname);
361 }
362
363 /*
364 * Check the count field of a directory
365 * entry against a known value. The caller
366 * is expected to skip/ignore the tag if
367 * there is a mismatch.
368 */
369 static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
370 {
371 if (count != dir->tdir_count)
372 {
373 TIFFWarning(tif->tif_name,
374 "incorrect count for field \"%s\" (%lu, expecting %lu); "
375 "tag ignored",
376 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
377 dir->tdir_count, count);
378 return (0);
379 }
380 return (1);
381 }
382
383 /*
384 * Fetch a contiguous directory item.
385 */
386 static tsize_t TIFFFetchData(TIFF *tif, TIFFDirEntry *dir, char *cp)
387 {
388 int w = TIFFDataWidth(dir->tdir_type);
389 tsize_t cc = dir->tdir_count * w;
390
391 if (!isMapped(tif))
392 {
393 if (!SeekOK(tif, dir->tdir_offset))
394 goto bad;
395 if (!ReadOK(tif, cp, cc))
396 goto bad;
397 }
398 else
399 {
400 if (dir->tdir_offset + cc > tif->tif_size)
401 goto bad;
402 _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
403 }
404 if (tif->tif_flags & TIFF_SWAB)
405 {
406 switch (dir->tdir_type)
407 {
408 case TIFF_SHORT:
409 case TIFF_SSHORT:
410 TIFFSwabArrayOfShort((uint16_t *)cp, dir->tdir_count);
411 break;
412 case TIFF_LONG:
413 case TIFF_SLONG:
414 case TIFF_FLOAT:
415 TIFFSwabArrayOfLong((uint32_t *)cp, dir->tdir_count);
416 break;
417 case TIFF_RATIONAL:
418 case TIFF_SRATIONAL:
419 TIFFSwabArrayOfLong((uint32_t *)cp, 2 * dir->tdir_count);
420 break;
421 case TIFF_DOUBLE:
422 TIFFSwabArrayOfDouble((double *)cp, dir->tdir_count);
423 break;
424 }
425 }
426 return (cc);
427 bad:
428 TIFFErrorExtR(tif, tif->tif_name, "Error fetching data for field \"%s\"",
429 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
430 return ((tsize_t)0);
431 }
432
433 /*
434 * Fetch an ASCII item from the file.
435 */
436 static tsize_t TIFFFetchString(TIFF *tif, TIFFDirEntry *dir, char *cp)
437 {
438 if (dir->tdir_count <= 4)
439 {
440 uint32_t l = dir->tdir_offset;
441 if (tif->tif_flags & TIFF_SWAB)
442 TIFFSwabLong(&l);
443 _TIFFmemcpy(cp, &l, dir->tdir_count);
444 return (1);
445 }
446 return (TIFFFetchData(tif, dir, cp));
447 }
448
449 /*
450 * Convert numerator+denominator to float.
451 */
452 static int cvtRational(TIFF *tif, TIFFDirEntry *dir, uint32_t num,
453 uint32_t denom, float *rv)
454 {
455 if (denom == 0)
456 {
457 TIFFErrorExtR(tif, tif->tif_name,
458 "%s: Rational with zero denominator (num = %lu)",
459 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
460 return (0);
461 }
462 else
463 {
464 if (dir->tdir_type == TIFF_RATIONAL)
465 *rv = ((float)num / (float)denom);
466 else
467 *rv = ((float)(int32_t)num / (float)(int32_t)denom);
468 return (1);
469 }
470 }
471
472 /*
473 * Fetch a rational item from the file
474 * at offset off and return the value
475 * as a floating point number.
476 */
477 static float TIFFFetchRational(TIFF *tif, TIFFDirEntry *dir)
478 {
479 uint32_t l[2];
480 float v;
481
482 return (!TIFFFetchData(tif, dir, (char *)l) ||
483 !cvtRational(tif, dir, l[0], l[1], &v)
484 ? 1.0f
485 : v);
486 }
487
488 /*
489 * Fetch a single floating point value
490 * from the offset field and return it
491 * as a native float.
492 */
493 static float TIFFFetchFloat(TIFF *tif, TIFFDirEntry *dir)
494 {
495 /* This appears to be a flagrant bug in the TIFF library, yet I
496 actually don't understand how it could have ever worked the old
497 way. Look at the comments in my new code and you'll understand. */
498 #if (0)
499 float v = (float)TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
500 TIFFCvtIEEEFloatToNative(tif, 1, &v);
501 #else
502 float v;
503 /* This is a little bit tricky - if we just cast the uint32_t to a float,
504 C will perform a numerical conversion, which is not what we want.
505 We want to take the actual bit pattern in the uint32_t and interpret
506 it as a float. Thus we cast a uint32_t * into a float * and then
507 dereference to get v. */
508 uint32_t l =
509 (uint32_t)TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
510 v = *(float *)&l;
511 TIFFCvtIEEEFloatToNative(tif, 1, &v);
512 #endif
513 return (v);
514 }
515
516 /*
517 * Fetch an array of BYTE or SBYTE values.
518 */
519 static int TIFFFetchByteArray(TIFF *tif, TIFFDirEntry *dir, uint16_t *v)
520 {
521 if (dir->tdir_count <= 4)
522 {
523 /*
524 * Extract data from offset field.
525 */
526 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN)
527 {
528 switch (dir->tdir_count)
529 {
530 case 4:
531 v[3] = dir->tdir_offset & 0xff;
532 case 3:
533 v[2] = (dir->tdir_offset >> 8) & 0xff;
534 case 2:
535 v[1] = (dir->tdir_offset >> 16) & 0xff;
536 case 1:
537 v[0] = dir->tdir_offset >> 24;
538 }
539 }
540 else
541 {
542 switch (dir->tdir_count)
543 {
544 case 4:
545 v[3] = dir->tdir_offset >> 24;
546 case 3:
547 v[2] = (dir->tdir_offset >> 16) & 0xff;
548 case 2:
549 v[1] = (dir->tdir_offset >> 8) & 0xff;
550 case 1:
551 v[0] = dir->tdir_offset & 0xff;
552 }
553 }
554 return (1);
555 }
556 else
557 return (TIFFFetchData(tif, dir, (char *)v) != 0); /* XXX */
558 }
559
560 /*
561 * Fetch an array of SHORT or SSHORT values.
562 */
563 static int TIFFFetchShortArray(TIFF *tif, TIFFDirEntry *dir, uint16_t *v)
564 {
565 if (dir->tdir_count <= 2)
566 {
567 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN)
568 {
569 switch (dir->tdir_count)
570 {
571 case 2:
572 v[1] = dir->tdir_offset & 0xffff;
573 case 1:
574 v[0] = dir->tdir_offset >> 16;
575 }
576 }
577 else
578 {
579 switch (dir->tdir_count)
580 {
581 case 2:
582 v[1] = dir->tdir_offset >> 16;
583 case 1:
584 v[0] = dir->tdir_offset & 0xffff;
585 }
586 }
587 return (1);
588 }
589 else
590 return (TIFFFetchData(tif, dir, (char *)v) != 0);
591 }
592
593 /*
594 * Fetch a pair of SHORT or BYTE values.
595 */
596 static int TIFFFetchShortPair(TIFF *tif, TIFFDirEntry *dir)
597 {
598 uint16_t v[2];
599 int ok = 0;
600
601 switch (dir->tdir_type)
602 {
603 case TIFF_SHORT:
604 case TIFF_SSHORT:
605 ok = TIFFFetchShortArray(tif, dir, v);
606 break;
607 case TIFF_BYTE:
608 case TIFF_SBYTE:
609 ok = TIFFFetchByteArray(tif, dir, v);
610 break;
611 }
612 if (ok)
613 TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
614 return (ok);
615 }
616
617 /*
618 * Fetch an array of LONG or SLONG values.
619 */
620 static int TIFFFetchLongArray(TIFF *tif, TIFFDirEntry *dir, uint32_t *v)
621 {
622 if (dir->tdir_count == 1)
623 {
624 v[0] = dir->tdir_offset;
625 return (1);
626 }
627 else
628 return (TIFFFetchData(tif, dir, (char *)v) != 0);
629 }
630
631 /*
632 * Fetch an array of RATIONAL or SRATIONAL values.
633 */
634 static int TIFFFetchRationalArray(TIFF *tif, TIFFDirEntry *dir, float *v)
635 {
636 int ok = 0;
637 uint32_t *l;
638
639 l = (uint32_t *)CheckMalloc(tif,
640 dir->tdir_count * TIFFDataWidth(dir->tdir_type),
641 "to fetch array of rationals");
642 if (l)
643 {
644 if (TIFFFetchData(tif, dir, (char *)l))
645 {
646 uint32_t i;
647 for (i = 0; i < dir->tdir_count; i++)
648 {
649 ok = cvtRational(tif, dir, l[2 * i + 0], l[2 * i + 1], &v[i]);
650 if (!ok)
651 break;
652 }
653 }
654 _TIFFfree((char *)l);
655 }
656 return (ok);
657 }
658
659 /*
660 * Fetch an array of FLOAT values.
661 */
662 static int TIFFFetchFloatArray(TIFF *tif, TIFFDirEntry *dir, float *v)
663 {
664
665 if (dir->tdir_count == 1)
666 {
667 v[0] = *(float *)&dir->tdir_offset;
668 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
669 return (1);
670 }
671 else if (TIFFFetchData(tif, dir, (char *)v))
672 {
673 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
674 return (1);
675 }
676 else
677 return (0);
678 }
679
680 /*
681 * Fetch an array of DOUBLE values.
682 */
683 static int TIFFFetchDoubleArray(TIFF *tif, TIFFDirEntry *dir, double *v)
684 {
685 if (TIFFFetchData(tif, dir, (char *)v))
686 {
687 TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
688 return (1);
689 }
690 else
691 return (0);
692 }
693
694 /*
695 * Fetch an array of ANY values. The actual values are
696 * returned as doubles which should be able hold all the
697 * types. Yes, there really should be an tany_t to avoid
698 * this potential non-portability ... Note in particular
699 * that we assume that the double return value vector is
700 * large enough to read in any fundamental type. We use
701 * that vector as a buffer to read in the base type vector
702 * and then convert it in place to double (from end
703 * to front of course).
704 */
705 static int TIFFFetchAnyArray(TIFF *tif, TIFFDirEntry *dir, double *v)
706 {
707 int i;
708
709 switch (dir->tdir_type)
710 {
711 case TIFF_BYTE:
712 case TIFF_SBYTE:
713 if (!TIFFFetchByteArray(tif, dir, (uint16_t *)v))
714 return (0);
715 if (dir->tdir_type == TIFF_BYTE)
716 {
717 uint16_t *vp = (uint16_t *)v;
718 for (i = dir->tdir_count - 1; i >= 0; i--)
719 v[i] = vp[i];
720 }
721 else
722 {
723 int16_t *vp = (int16_t *)v;
724 for (i = dir->tdir_count - 1; i >= 0; i--)
725 v[i] = vp[i];
726 }
727 break;
728 case TIFF_SHORT:
729 case TIFF_SSHORT:
730 if (!TIFFFetchShortArray(tif, dir, (uint16_t *)v))
731 return (0);
732 if (dir->tdir_type == TIFF_SHORT)
733 {
734 uint16_t *vp = (uint16_t *)v;
735 for (i = dir->tdir_count - 1; i >= 0; i--)
736 v[i] = vp[i];
737 }
738 else
739 {
740 int16_t *vp = (int16_t *)v;
741 for (i = dir->tdir_count - 1; i >= 0; i--)
742 v[i] = vp[i];
743 }
744 break;
745 case TIFF_LONG:
746 case TIFF_SLONG:
747 if (!TIFFFetchLongArray(tif, dir, (uint32_t *)v))
748 return (0);
749 if (dir->tdir_type == TIFF_LONG)
750 {
751 uint32_t *vp = (uint32_t *)v;
752 for (i = dir->tdir_count - 1; i >= 0; i--)
753 v[i] = vp[i];
754 }
755 else
756 {
757 int32_t *vp = (int32_t *)v;
758 for (i = dir->tdir_count - 1; i >= 0; i--)
759 v[i] = vp[i];
760 }
761 break;
762 case TIFF_RATIONAL:
763 case TIFF_SRATIONAL:
764 if (!TIFFFetchRationalArray(tif, dir, (float *)v))
765 return (0);
766 {
767 float *vp = (float *)v;
768 for (i = dir->tdir_count - 1; i >= 0; i--)
769 v[i] = vp[i];
770 }
771 break;
772 case TIFF_FLOAT:
773 if (!TIFFFetchFloatArray(tif, dir, (float *)v))
774 return (0);
775 {
776 float *vp = (float *)v;
777 for (i = dir->tdir_count - 1; i >= 0; i--)
778 v[i] = vp[i];
779 }
780 break;
781 case TIFF_DOUBLE:
782 return (TIFFFetchDoubleArray(tif, dir, (double *)v));
783 default:
784 /* TIFF_NOTYPE */
785 /* TIFF_ASCII */
786 /* TIFF_UNDEFINED */
787 TIFFErrorExtR(tif, tif->tif_name,
788 "Cannot read TIFF_ANY type %d for field \"%s\"",
789 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
790 return (0);
791 }
792 return (1);
793 }
794
795 /*
796 * Fetch a tag that is not handled by special case code.
797 */
798 /* The standard function TIFFFetchNormalTag() could definitely be replaced
799 with a simple call to this function, just adding TIFFSetField() as the
800 last argument. */
801 static int TIFFFetchNormalSubTag(TIFF *tif, TIFFDirEntry *dp,
802 const TIFFFieldInfo *fip,
803 int (*setFieldFn)(TIFF *tif, ttag_t tag, ...))
804 {
805 static char mesg[] = "to fetch tag value";
806 int ok = 0;
807
808 if (dp->tdir_count > 1)
809 { /* array of values */
810 char *cp = NULL;
811
812 switch (dp->tdir_type)
813 {
814 case TIFF_BYTE:
815 case TIFF_SBYTE:
816 /* NB: always expand BYTE values to shorts */
817 cp = CheckMalloc(tif, dp->tdir_count * sizeof(uint16_t), mesg);
818 ok = cp && TIFFFetchByteArray(tif, dp, (uint16_t *)cp);
819 break;
820 case TIFF_SHORT:
821 case TIFF_SSHORT:
822 cp = CheckMalloc(tif, dp->tdir_count * sizeof(uint16_t), mesg);
823 ok = cp && TIFFFetchShortArray(tif, dp, (uint16_t *)cp);
824 break;
825 case TIFF_LONG:
826 case TIFF_SLONG:
827 cp = CheckMalloc(tif, dp->tdir_count * sizeof(uint32_t), mesg);
828 ok = cp && TIFFFetchLongArray(tif, dp, (uint32_t *)cp);
829 break;
830 case TIFF_RATIONAL:
831 case TIFF_SRATIONAL:
832 cp = CheckMalloc(tif, dp->tdir_count * sizeof(float), mesg);
833 ok = cp && TIFFFetchRationalArray(tif, dp, (float *)cp);
834 break;
835 case TIFF_FLOAT:
836 cp = CheckMalloc(tif, dp->tdir_count * sizeof(float), mesg);
837 ok = cp && TIFFFetchFloatArray(tif, dp, (float *)cp);
838 break;
839 case TIFF_DOUBLE:
840 cp = CheckMalloc(tif, dp->tdir_count * sizeof(double), mesg);
841 ok = cp && TIFFFetchDoubleArray(tif, dp, (double *)cp);
842 break;
843 case TIFF_ASCII:
844 case TIFF_UNDEFINED: /* bit of a cheat... */
845 /*
846 * Some vendors write strings w/o the trailing
847 * NULL byte, so always append one just in case.
848 */
849 cp = CheckMalloc(tif, dp->tdir_count + 1, mesg);
850 if (ok = (cp && TIFFFetchString(tif, dp, cp)))
851 cp[dp->tdir_count] = '\0'; /* XXX */
852 break;
853 }
854 if (ok)
855 {
856 ok = (fip->field_passcount
857 ? (*setFieldFn)(tif, dp->tdir_tag, dp->tdir_count, cp)
858 : (*setFieldFn)(tif, dp->tdir_tag, cp));
859 }
860 if (cp != NULL)
861 _TIFFfree(cp);
862 }
863 else if (CheckDirCount(tif, dp, 1))
864 { /* singleton value */
865 switch (dp->tdir_type)
866 {
867 case TIFF_BYTE:
868 case TIFF_SBYTE:
869 case TIFF_SHORT:
870 case TIFF_SSHORT:
871 /*
872 * If the tag is also acceptable as a LONG or SLONG
873 * then (*setFieldFn) will expect an uint32_t parameter
874 * passed to it (through varargs). Thus, for machines
875 * where sizeof (int) != sizeof (uint32_t) we must do
876 * a careful check here. It's hard to say if this
877 * is worth optimizing.
878 *
879 * NB: We use TIFFFieldWithTag here knowing that
880 * it returns us the first entry in the table
881 * for the tag and that that entry is for the
882 * widest potential data type the tag may have.
883 */
884 {
885 TIFFDataType type = fip->field_type;
886 if (type != TIFF_LONG && type != TIFF_SLONG)
887 {
888 uint16_t v = (uint16_t)TIFFExtractData(
889 tif, dp->tdir_type, dp->tdir_offset);
890 ok = (fip->field_passcount
891 ? (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
892 : (*setFieldFn)(tif, dp->tdir_tag, v));
893 break;
894 }
895 }
896 /* fall through... */
897 case TIFF_LONG:
898 case TIFF_SLONG:
899 {
900 uint32_t v32 =
901 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
902 ok = (fip->field_passcount
903 ? (*setFieldFn)(tif, dp->tdir_tag, 1, &v32)
904 : (*setFieldFn)(tif, dp->tdir_tag, v32));
905 }
906 break;
907 case TIFF_RATIONAL:
908 case TIFF_SRATIONAL:
909 case TIFF_FLOAT:
910 {
911 float v =
912 (dp->tdir_type == TIFF_FLOAT ? TIFFFetchFloat(tif, dp)
913 : TIFFFetchRational(tif, dp));
914 ok = (fip->field_passcount
915 ? (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
916 : (*setFieldFn)(tif, dp->tdir_tag, v));
917 }
918 break;
919 case TIFF_DOUBLE:
920 {
921 double v;
922 ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
923 (fip->field_passcount
924 ? (*setFieldFn)(tif, dp->tdir_tag, 1, &v)
925 : (*setFieldFn)(tif, dp->tdir_tag, v)));
926 }
927 break;
928 case TIFF_ASCII:
929 case TIFF_UNDEFINED: /* bit of a cheat... */
930 {
931 char c[2];
932 if (ok = (TIFFFetchString(tif, dp, c) != 0))
933 {
934 c[1] = '\0'; /* XXX paranoid */
935 ok = (*setFieldFn)(tif, dp->tdir_tag, c);
936 }
937 }
938 break;
939 }
940 }
941 return (ok);
942 }
943
944 /* Everything after this is exactly duplicated from the standard tif_dirread.c
945 file, necessitated by the fact that they are declared static there so
946 we can't call them!
947 */
948 #define NITEMS(x) (sizeof(x) / sizeof(x[0]))
949 /*
950 * Fetch samples/pixel short values for
951 * the specified tag and verify that
952 * all values are the same.
953 */
954 static int TIFFFetchPerSampleShorts(TIFF *tif, TIFFDirEntry *dir, int *pl)
955 {
956 int samples = tif->tif_dir.td_samplesperpixel;
957 int status = 0;
958
959 if (CheckDirCount(tif, dir, (uint32_t)samples))
960 {
961 uint16_t buf[10];
962 uint16_t *v = buf;
963
964 if (samples > NITEMS(buf))
965 v = (uint16_t *)_TIFFmalloc(samples * sizeof(uint16_t));
966 if (TIFFFetchShortArray(tif, dir, v))
967 {
968 int i;
969 for (i = 1; i < samples; i++)
970 if (v[i] != v[0])
971 {
972 TIFFErrorExtR(
973 tif, tif->tif_name,
974 "Cannot handle different per-sample values for field "
975 "\"%s\"",
976 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
977 goto bad;
978 }
979 *pl = v[0];
980 status = 1;
981 }
982 bad:
983 if (v != buf)
984 _TIFFfree((char *)v);
985 }
986 return (status);
987 }
988
989 /*
990 * Fetch samples/pixel ANY values for
991 * the specified tag and verify that
992 * all values are the same.
993 */
994 static int TIFFFetchPerSampleAnys(TIFF *tif, TIFFDirEntry *dir, double *pl)
995 {
996 int samples = (int)tif->tif_dir.td_samplesperpixel;
997 int status = 0;
998
999 if (CheckDirCount(tif, dir, (uint32_t)samples))
1000 {
1001 double buf[10];
1002 double *v = buf;
1003
1004 if (samples > NITEMS(buf))
1005 v = (double *)_TIFFmalloc(samples * sizeof(double));
1006 if (TIFFFetchAnyArray(tif, dir, v))
1007 {
1008 int i;
1009 for (i = 1; i < samples; i++)
1010 if (v[i] != v[0])
1011 {
1012 TIFFErrorExtR(
1013 tif, tif->tif_name,
1014 "Cannot handle different per-sample values for field "
1015 "\"%s\"",
1016 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1017 goto bad;
1018 }
1019 *pl = v[0];
1020 status = 1;
1021 }
1022 bad:
1023 if (v != buf)
1024 _TIFFfree(v);
1025 }
1026 return (status);
1027 }
1028 #undef NITEMS
1029
1030 /*
1031 * Fetch a set of offsets or lengths.
1032 * While this routine says "strips",
1033 * in fact it's also used for tiles.
1034 */
1035 static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, long nstrips,
1036 uint32_t **lpp)
1037 {
1038 register uint32_t *lp;
1039 int status;
1040
1041 if (!CheckDirCount(tif, dir, (uint32_t)nstrips))
1042 return (0);
1043 /*
1044 * Allocate space for strip information.
1045 */
1046 if (*lpp == NULL &&
1047 (*lpp = (uint32_t *)CheckMalloc(tif, nstrips * sizeof(uint32_t),
1048 "for strip array")) == NULL)
1049 return (0);
1050 lp = *lpp;
1051 if (dir->tdir_type == (int)TIFF_SHORT)
1052 {
1053 /*
1054 * Handle uint16_t->uint32_t expansion.
1055 */
1056 uint16_t *dp = (uint16_t *)CheckMalloc(
1057 tif, dir->tdir_count * sizeof(uint16_t), "to fetch strip tag");
1058 if (dp == NULL)
1059 return (0);
1060 if (status = TIFFFetchShortArray(tif, dir, dp))
1061 {
1062 register uint16_t *wp = dp;
1063 while (nstrips-- > 0)
1064 *lp++ = *wp++;
1065 }
1066 _TIFFfree((char *)dp);
1067 }
1068 else
1069 status = TIFFFetchLongArray(tif, dir, lp);
1070 return (status);
1071 }
1072
1073 #define NITEMS(x) (sizeof(x) / sizeof(x[0]))
1074 /*
1075 * Fetch and set the ExtraSamples tag.
1076 */
1077 static int TIFFFetchExtraSamples(TIFF *tif, TIFFDirEntry *dir)
1078 {
1079 uint16_t buf[10];
1080 uint16_t *v = buf;
1081 int status;
1082
1083 if (dir->tdir_count > NITEMS(buf))
1084 v = (uint16_t *)_TIFFmalloc(dir->tdir_count * sizeof(uint16_t));
1085 if (dir->tdir_type == TIFF_BYTE)
1086 status = TIFFFetchByteArray(tif, dir, v);
1087 else
1088 status = TIFFFetchShortArray(tif, dir, v);
1089 if (status)
1090 status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v);
1091 if (v != buf)
1092 _TIFFfree((char *)v);
1093 return (status);
1094 }
1095 #undef NITEMS
1096
1097 #ifdef COLORIMETRY_SUPPORT
1098 /*
1099 * Fetch and set the RefBlackWhite tag.
1100 */
1101 static int TIFFFetchRefBlackWhite(TIFF *tif, TIFFDirEntry *dir)
1102 {
1103 static char mesg[] = "for \"ReferenceBlackWhite\" array";
1104 char *cp;
1105 int ok;
1106
1107 if (dir->tdir_type == TIFF_RATIONAL)
1108 return (1/*TIFFFetchNormalTag(tif, dir) just so linker won't complain - this part of the code is never used anyway */);
1109 /*
1110 * Handle LONG's for backward compatibility.
1111 */
1112 cp = CheckMalloc(tif, dir->tdir_count * sizeof(uint32_t), mesg);
1113 if (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32_t *)cp)))
1114 {
1115 float *fp =
1116 (float *)CheckMalloc(tif, dir->tdir_count * sizeof(float), mesg);
1117 if (ok = (fp != NULL))
1118 {
1119 uint32_t i;
1120 for (i = 0; i < dir->tdir_count; i++)
1121 fp[i] = (float)((uint32_t *)cp)[i];
1122 ok = TIFFSetField(tif, dir->tdir_tag, fp);
1123 _TIFFfree((char *)fp);
1124 }
1125 }
1126 if (cp)
1127 _TIFFfree(cp);
1128 return (ok);
1129 }
1130 #endif
1131
1132 #if STRIPCHOP_SUPPORT
1133 /*
1134 * Replace a single strip (tile) of uncompressed data by
1135 * multiple strips (tiles), each approximately 8Kbytes.
1136 * This is useful for dealing with large images or
1137 * for dealing with machines with a limited amount
1138 * memory.
1139 */
1140 static void ChopUpSingleUncompressedStrip(TIFF *tif)
1141 {
1142 register TIFFDirectory *td = &tif->tif_dir;
1143 uint32_t bytecount = td->td_stripbytecount[0];
1144 uint32_t offset = td->td_stripoffset[0];
1145 tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
1146 tstrip_t strip, nstrips, rowsperstrip;
1147 uint32_t *newcounts;
1148 uint32_t *newoffsets;
1149
1150 /*
1151 * Make the rows hold at least one
1152 * scanline, but fill 8k if possible.
1153 */
1154 if (rowbytes > 8192)
1155 {
1156 stripbytes = rowbytes;
1157 rowsperstrip = 1;
1158 }
1159 else
1160 {
1161 rowsperstrip = 8192 / rowbytes;
1162 stripbytes = rowbytes * rowsperstrip;
1163 }
1164 /* never increase the number of strips in an image */
1165 if (rowsperstrip >= td->td_rowsperstrip)
1166 return;
1167 nstrips = (tstrip_t)TIFFhowmany(bytecount, stripbytes);
1168 newcounts =
1169 (uint32_t *)CheckMalloc(tif, nstrips * sizeof(uint32_t),
1170 "for chopped \"StripByteCounts\" array");
1171 newoffsets = (uint32_t *)CheckMalloc(tif, nstrips * sizeof(uint32_t),
1172 "for chopped \"StripOffsets\" array");
1173 if (newcounts == NULL || newoffsets == NULL)
1174 {
1175 /*
1176 * Unable to allocate new strip information, give
1177 * up and use the original one strip information.
1178 */
1179 if (newcounts != NULL)
1180 _TIFFfree(newcounts);
1181 if (newoffsets != NULL)
1182 _TIFFfree(newoffsets);
1183 return;
1184 }
1185 /*
1186 * Fill the strip information arrays with
1187 * new bytecounts and offsets that reflect
1188 * the broken-up format.
1189 */
1190 for (strip = 0; strip < nstrips; strip++)
1191 {
1192 if (stripbytes > bytecount)
1193 stripbytes = bytecount;
1194 newcounts[strip] = stripbytes;
1195 newoffsets[strip] = offset;
1196 offset += stripbytes;
1197 bytecount -= stripbytes;
1198 }
1199 /*
1200 * Replace old single strip info with multi-strip info.
1201 */
1202 td->td_stripsperimage = td->td_nstrips = nstrips;
1203 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
1204
1205 _TIFFfree(td->td_stripbytecount);
1206 _TIFFfree(td->td_stripoffset);
1207 td->td_stripbytecount = newcounts;
1208 td->td_stripoffset = newoffsets;
1209 }
1210 #endif /* STRIPCHOP_SUPPORT */