Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / core / src / fxcodec / fx_tiff / tiff_v403 / tif_dirread.c
1 /* $Id: tif_dirread.c,v 1.178 2012-08-19 16:56:34 bfriesen Exp $ */
2
3 /*
4  * Copyright (c) 1988-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26
27 /*
28  * TIFF Library.
29  *
30  * Directory Read Support Routines.
31  */
32
33 /* Suggested pending improvements:
34  * - add a field 'ignore' to the TIFFDirEntry structure, to flag status,
35  *   eliminating current use of the IGNORE value, and therefore eliminating
36  *   current irrational behaviour on tags with tag id code 0
37  * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
38  *   the pointer to the appropriate TIFFField structure early on in
39  *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
40  */
41  
42 #if (!defined(_FPDFAPI_MINI_) || defined(_TIFF_DECODER_)) && !defined(_USE_ADDIN_) && !defined _FX_NO_ANSIC_ && !defined(_FX_EMB_NOUSE_DECODER_)
43 #include "tiffiop.h"
44
45 #define IGNORE 0          /* tag placeholder used below */
46 #define FAILED_FII    ((uint32) -1)
47
48 #ifdef HAVE_IEEEFP
49 # define TIFFCvtIEEEFloatToNative(tif, n, fp)
50 # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
51 #else
52 extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
53 extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
54 #endif
55
56 enum TIFFReadDirEntryErr {
57         TIFFReadDirEntryErrOk = 0,
58         TIFFReadDirEntryErrCount = 1,
59         TIFFReadDirEntryErrType = 2,
60         TIFFReadDirEntryErrIo = 3,
61         TIFFReadDirEntryErrRange = 4,
62         TIFFReadDirEntryErrPsdif = 5,
63         TIFFReadDirEntryErrSizesan = 6,
64         TIFFReadDirEntryErrAlloc = 7,
65 };
66
67 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
68 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
69 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
70 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
71 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
72 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
73 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
74
75 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value);
76 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value);
77 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value);
78 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value);
79 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value);
80 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value);
81 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value);
82 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
83 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value);
84 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value);
85 static enum TIFFReadDirEntryErr TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value);
86 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value);
87
88 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
89 #if 0
90 static enum TIFFReadDirEntryErr TIFFReadDirEntryPersampleDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
91 #endif
92
93 static void TIFFReadDirEntryCheckedByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value);
94 static void TIFFReadDirEntryCheckedSbyte(TIFF* tif, TIFFDirEntry* direntry, int8* value);
95 static void TIFFReadDirEntryCheckedShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value);
96 static void TIFFReadDirEntryCheckedSshort(TIFF* tif, TIFFDirEntry* direntry, int16* value);
97 static void TIFFReadDirEntryCheckedLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value);
98 static void TIFFReadDirEntryCheckedSlong(TIFF* tif, TIFFDirEntry* direntry, int32* value);
99 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value);
100 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSlong8(TIFF* tif, TIFFDirEntry* direntry, int64* value);
101 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedRational(TIFF* tif, TIFFDirEntry* direntry, double* value);
102 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedSrational(TIFF* tif, TIFFDirEntry* direntry, double* value);
103 static void TIFFReadDirEntryCheckedFloat(TIFF* tif, TIFFDirEntry* direntry, float* value);
104 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckedDouble(TIFF* tif, TIFFDirEntry* direntry, double* value);
105
106 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSbyte(int8 value);
107 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteShort(uint16 value);
108 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSshort(int16 value);
109 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong(uint32 value);
110 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong(int32 value);
111 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteLong8(uint64 value);
112 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeByteSlong8(int64 value);
113
114 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteByte(uint8 value);
115 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteShort(uint16 value);
116 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSshort(int16 value);
117 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong(uint32 value);
118 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong(int32 value);
119 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteLong8(uint64 value);
120 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSbyteSlong8(int64 value);
121
122 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSbyte(int8 value);
123 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSshort(int16 value);
124 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong(uint32 value);
125 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong(int32 value);
126 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortLong8(uint64 value);
127 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeShortSlong8(int64 value);
128
129 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortShort(uint16 value);
130 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong(uint32 value);
131 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong(int32 value);
132 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortLong8(uint64 value);
133 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSshortSlong8(int64 value);
134
135 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSbyte(int8 value);
136 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSshort(int16 value);
137 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong(int32 value);
138 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongLong8(uint64 value);
139 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLongSlong8(int64 value);
140
141 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong(uint32 value);
142 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongLong8(uint64 value);
143 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlongSlong8(int64 value);
144
145 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sbyte(int8 value);
146 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Sshort(int16 value);
147 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong(int32 value);
148 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeLong8Slong8(int64 value);
149
150 static enum TIFFReadDirEntryErr TIFFReadDirEntryCheckRangeSlong8Long8(uint64 value);
151
152 static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF* tif, uint64 offset, tmsize_t size, void* dest);
153 static void TIFFReadDirEntryOutputErr(TIFF* tif, enum TIFFReadDirEntryErr err, const char* module, const char* tagname, int recover);
154
155 static void TIFFReadDirectoryCheckOrder(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
156 static TIFFDirEntry* TIFFReadDirectoryFindEntry(TIFF* tif, TIFFDirEntry* dir, uint16 dircount, uint16 tagid);
157 static void TIFFReadDirectoryFindFieldInfo(TIFF* tif, uint16 tagid, uint32* fii);
158
159 static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount);
160 static void MissingRequired(TIFF*, const char*);
161 static int TIFFCheckDirOffset(TIFF* tif, uint64 diroff);
162 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
163 static uint16 TIFFFetchDirectory(TIFF* tif, uint64 diroff, TIFFDirEntry** pdir, uint64* nextdiroff);
164 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*, int recover);
165 static int TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, uint32 nstrips, uint64** lpp);
166 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
167 static void ChopUpSingleUncompressedStrip(TIFF*);
168 static uint64 TIFFReadUInt64(const uint8 *value);
169
170 typedef union _UInt64Aligned_t
171 {
172         double d;
173         uint64 l;
174         uint32 i[2];
175         uint16 s[4];
176         uint8  c[8];
177 } UInt64Aligned_t;
178
179 /*
180   Unaligned safe copy of a uint64 value from an octet array.
181 */
182 static uint64 TIFFReadUInt64(const uint8 *value)
183 {
184         UInt64Aligned_t result;
185
186         result.c[0]=value[0];
187         result.c[1]=value[1];
188         result.c[2]=value[2];
189         result.c[3]=value[3];
190         result.c[4]=value[4];
191         result.c[5]=value[5];
192         result.c[6]=value[6];
193         result.c[7]=value[7];
194
195         return result.l;
196 }
197
198 static enum TIFFReadDirEntryErr TIFFReadDirEntryByte(TIFF* tif, TIFFDirEntry* direntry, uint8* value)
199 {
200         enum TIFFReadDirEntryErr err;
201         if (direntry->tdir_count!=1)
202                 return(TIFFReadDirEntryErrCount);
203         switch (direntry->tdir_type)
204         {
205                 case TIFF_BYTE:
206                         TIFFReadDirEntryCheckedByte(tif,direntry,value);
207                         return(TIFFReadDirEntryErrOk);
208                 case TIFF_SBYTE:
209                         {
210                                 int8 m;
211                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
212                                 err=TIFFReadDirEntryCheckRangeByteSbyte(m);
213                                 if (err!=TIFFReadDirEntryErrOk)
214                                         return(err);
215                                 *value=(uint8)m;
216                                 return(TIFFReadDirEntryErrOk);
217                         }
218                 case TIFF_SHORT:
219                         {
220                                 uint16 m;
221                                 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
222                                 err=TIFFReadDirEntryCheckRangeByteShort(m);
223                                 if (err!=TIFFReadDirEntryErrOk)
224                                         return(err);
225                                 *value=(uint8)m;
226                                 return(TIFFReadDirEntryErrOk);
227                         }
228                 case TIFF_SSHORT:
229                         {
230                                 int16 m;
231                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
232                                 err=TIFFReadDirEntryCheckRangeByteSshort(m);
233                                 if (err!=TIFFReadDirEntryErrOk)
234                                         return(err);
235                                 *value=(uint8)m;
236                                 return(TIFFReadDirEntryErrOk);
237                         }
238                 case TIFF_LONG:
239                         {
240                                 uint32 m;
241                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
242                                 err=TIFFReadDirEntryCheckRangeByteLong(m);
243                                 if (err!=TIFFReadDirEntryErrOk)
244                                         return(err);
245                                 *value=(uint8)m;
246                                 return(TIFFReadDirEntryErrOk);
247                         }
248                 case TIFF_SLONG:
249                         {
250                                 int32 m;
251                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
252                                 err=TIFFReadDirEntryCheckRangeByteSlong(m);
253                                 if (err!=TIFFReadDirEntryErrOk)
254                                         return(err);
255                                 *value=(uint8)m;
256                                 return(TIFFReadDirEntryErrOk);
257                         }
258                 case TIFF_LONG8:
259                         {
260                                 uint64 m;
261                                 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
262                                 if (err!=TIFFReadDirEntryErrOk)
263                                         return(err);
264                                 err=TIFFReadDirEntryCheckRangeByteLong8(m);
265                                 if (err!=TIFFReadDirEntryErrOk)
266                                         return(err);
267                                 *value=(uint8)m;
268                                 return(TIFFReadDirEntryErrOk);
269                         }
270                 case TIFF_SLONG8:
271                         {
272                                 int64 m;
273                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
274                                 if (err!=TIFFReadDirEntryErrOk)
275                                         return(err);
276                                 err=TIFFReadDirEntryCheckRangeByteSlong8(m);
277                                 if (err!=TIFFReadDirEntryErrOk)
278                                         return(err);
279                                 *value=(uint8)m;
280                                 return(TIFFReadDirEntryErrOk);
281                         }
282                 default:
283                         return(TIFFReadDirEntryErrType);
284         }
285 }
286
287 static enum TIFFReadDirEntryErr TIFFReadDirEntryShort(TIFF* tif, TIFFDirEntry* direntry, uint16* value)
288 {
289         enum TIFFReadDirEntryErr err;
290         if (direntry->tdir_count!=1)
291                 return(TIFFReadDirEntryErrCount);
292         switch (direntry->tdir_type)
293         {
294                 case TIFF_BYTE:
295                         {
296                                 uint8 m;
297                                 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
298                                 *value=(uint16)m;
299                                 return(TIFFReadDirEntryErrOk);
300                         }
301                 case TIFF_SBYTE:
302                         {
303                                 int8 m;
304                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
305                                 err=TIFFReadDirEntryCheckRangeShortSbyte(m);
306                                 if (err!=TIFFReadDirEntryErrOk)
307                                         return(err);
308                                 *value=(uint16)m;
309                                 return(TIFFReadDirEntryErrOk);
310                         }
311                 case TIFF_SHORT:
312                         TIFFReadDirEntryCheckedShort(tif,direntry,value);
313                         return(TIFFReadDirEntryErrOk);
314                 case TIFF_SSHORT:
315                         {
316                                 int16 m;
317                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
318                                 err=TIFFReadDirEntryCheckRangeShortSshort(m);
319                                 if (err!=TIFFReadDirEntryErrOk)
320                                         return(err);
321                                 *value=(uint16)m;
322                                 return(TIFFReadDirEntryErrOk);
323                         }
324                 case TIFF_LONG:
325                         {
326                                 uint32 m;
327                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
328                                 err=TIFFReadDirEntryCheckRangeShortLong(m);
329                                 if (err!=TIFFReadDirEntryErrOk)
330                                         return(err);
331                                 *value=(uint16)m;
332                                 return(TIFFReadDirEntryErrOk);
333                         }
334                 case TIFF_SLONG:
335                         {
336                                 int32 m;
337                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
338                                 err=TIFFReadDirEntryCheckRangeShortSlong(m);
339                                 if (err!=TIFFReadDirEntryErrOk)
340                                         return(err);
341                                 *value=(uint16)m;
342                                 return(TIFFReadDirEntryErrOk);
343                         }
344                 case TIFF_LONG8:
345                         {
346                                 uint64 m;
347                                 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
348                                 if (err!=TIFFReadDirEntryErrOk)
349                                         return(err);
350                                 err=TIFFReadDirEntryCheckRangeShortLong8(m);
351                                 if (err!=TIFFReadDirEntryErrOk)
352                                         return(err);
353                                 *value=(uint16)m;
354                                 return(TIFFReadDirEntryErrOk);
355                         }
356                 case TIFF_SLONG8:
357                         {
358                                 int64 m;
359                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
360                                 if (err!=TIFFReadDirEntryErrOk)
361                                         return(err);
362                                 err=TIFFReadDirEntryCheckRangeShortSlong8(m);
363                                 if (err!=TIFFReadDirEntryErrOk)
364                                         return(err);
365                                 *value=(uint16)m;
366                                 return(TIFFReadDirEntryErrOk);
367                         }
368                 default:
369                         return(TIFFReadDirEntryErrType);
370         }
371 }
372
373 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong(TIFF* tif, TIFFDirEntry* direntry, uint32* value)
374 {
375         enum TIFFReadDirEntryErr err;
376         if (direntry->tdir_count!=1)
377                 return(TIFFReadDirEntryErrCount);
378         switch (direntry->tdir_type)
379         {
380                 case TIFF_BYTE:
381                         {
382                                 uint8 m;
383                                 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
384                                 *value=(uint32)m;
385                                 return(TIFFReadDirEntryErrOk);
386                         }
387                 case TIFF_SBYTE:
388                         {
389                                 int8 m;
390                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
391                                 err=TIFFReadDirEntryCheckRangeLongSbyte(m);
392                                 if (err!=TIFFReadDirEntryErrOk)
393                                         return(err);
394                                 *value=(uint32)m;
395                                 return(TIFFReadDirEntryErrOk);
396                         }
397                 case TIFF_SHORT:
398                         {
399                                 uint16 m;
400                                 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
401                                 *value=(uint32)m;
402                                 return(TIFFReadDirEntryErrOk);
403                         }
404                 case TIFF_SSHORT:
405                         {
406                                 int16 m;
407                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
408                                 err=TIFFReadDirEntryCheckRangeLongSshort(m);
409                                 if (err!=TIFFReadDirEntryErrOk)
410                                         return(err);
411                                 *value=(uint32)m;
412                                 return(TIFFReadDirEntryErrOk);
413                         }
414                 case TIFF_LONG:
415                         TIFFReadDirEntryCheckedLong(tif,direntry,value);
416                         return(TIFFReadDirEntryErrOk);
417                 case TIFF_SLONG:
418                         {
419                                 int32 m;
420                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
421                                 err=TIFFReadDirEntryCheckRangeLongSlong(m);
422                                 if (err!=TIFFReadDirEntryErrOk)
423                                         return(err);
424                                 *value=(uint32)m;
425                                 return(TIFFReadDirEntryErrOk);
426                         }
427                 case TIFF_LONG8:
428                         {
429                                 uint64 m;
430                                 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
431                                 if (err!=TIFFReadDirEntryErrOk)
432                                         return(err);
433                                 err=TIFFReadDirEntryCheckRangeLongLong8(m);
434                                 if (err!=TIFFReadDirEntryErrOk)
435                                         return(err);
436                                 *value=(uint32)m;
437                                 return(TIFFReadDirEntryErrOk);
438                         }
439                 case TIFF_SLONG8:
440                         {
441                                 int64 m;
442                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
443                                 if (err!=TIFFReadDirEntryErrOk)
444                                         return(err);
445                                 err=TIFFReadDirEntryCheckRangeLongSlong8(m);
446                                 if (err!=TIFFReadDirEntryErrOk)
447                                         return(err);
448                                 *value=(uint32)m;
449                                 return(TIFFReadDirEntryErrOk);
450                         }
451                 default:
452                         return(TIFFReadDirEntryErrType);
453         }
454 }
455
456 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
457 {
458         enum TIFFReadDirEntryErr err;
459         if (direntry->tdir_count!=1)
460                 return(TIFFReadDirEntryErrCount);
461         switch (direntry->tdir_type)
462         {
463                 case TIFF_BYTE:
464                         {
465                                 uint8 m;
466                                 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
467                                 *value=(uint64)m;
468                                 return(TIFFReadDirEntryErrOk);
469                         }
470                 case TIFF_SBYTE:
471                         {
472                                 int8 m;
473                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
474                                 err=TIFFReadDirEntryCheckRangeLong8Sbyte(m);
475                                 if (err!=TIFFReadDirEntryErrOk)
476                                         return(err);
477                                 *value=(uint64)m;
478                                 return(TIFFReadDirEntryErrOk);
479                         }
480                 case TIFF_SHORT:
481                         {
482                                 uint16 m;
483                                 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
484                                 *value=(uint64)m;
485                                 return(TIFFReadDirEntryErrOk);
486                         }
487                 case TIFF_SSHORT:
488                         {
489                                 int16 m;
490                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
491                                 err=TIFFReadDirEntryCheckRangeLong8Sshort(m);
492                                 if (err!=TIFFReadDirEntryErrOk)
493                                         return(err);
494                                 *value=(uint64)m;
495                                 return(TIFFReadDirEntryErrOk);
496                         }
497                 case TIFF_LONG:
498                         {
499                                 uint32 m;
500                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
501                                 *value=(uint64)m;
502                                 return(TIFFReadDirEntryErrOk);
503                         }
504                 case TIFF_SLONG:
505                         {
506                                 int32 m;
507                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
508                                 err=TIFFReadDirEntryCheckRangeLong8Slong(m);
509                                 if (err!=TIFFReadDirEntryErrOk)
510                                         return(err);
511                                 *value=(uint64)m;
512                                 return(TIFFReadDirEntryErrOk);
513                         }
514                 case TIFF_LONG8:
515                         err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
516                         return(err);
517                 case TIFF_SLONG8:
518                         {
519                                 int64 m;
520                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
521                                 if (err!=TIFFReadDirEntryErrOk)
522                                         return(err);
523                                 err=TIFFReadDirEntryCheckRangeLong8Slong8(m);
524                                 if (err!=TIFFReadDirEntryErrOk)
525                                         return(err);
526                                 *value=(uint64)m;
527                                 return(TIFFReadDirEntryErrOk);
528                         }
529                 default:
530                         return(TIFFReadDirEntryErrType);
531         }
532 }
533
534 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloat(TIFF* tif, TIFFDirEntry* direntry, float* value)
535 {
536         enum TIFFReadDirEntryErr err;
537         if (direntry->tdir_count!=1)
538                 return(TIFFReadDirEntryErrCount);
539         switch (direntry->tdir_type)
540         {
541                 case TIFF_BYTE:
542                         {
543                                 uint8 m;
544                                 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
545                                 *value=(float)m;
546                                 return(TIFFReadDirEntryErrOk);
547                         }
548                 case TIFF_SBYTE:
549                         {
550                                 int8 m;
551                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
552                                 *value=(float)m;
553                                 return(TIFFReadDirEntryErrOk);
554                         }
555                 case TIFF_SHORT:
556                         {
557                                 uint16 m;
558                                 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
559                                 *value=(float)m;
560                                 return(TIFFReadDirEntryErrOk);
561                         }
562                 case TIFF_SSHORT:
563                         {
564                                 int16 m;
565                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
566                                 *value=(float)m;
567                                 return(TIFFReadDirEntryErrOk);
568                         }
569                 case TIFF_LONG:
570                         {
571                                 uint32 m;
572                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
573                                 *value=(float)m;
574                                 return(TIFFReadDirEntryErrOk);
575                         }
576                 case TIFF_SLONG:
577                         {
578                                 int32 m;
579                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
580                                 *value=(float)m;
581                                 return(TIFFReadDirEntryErrOk);
582                         }
583                 case TIFF_LONG8:
584                         {
585                                 uint64 m;
586                                 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
587                                 if (err!=TIFFReadDirEntryErrOk)
588                                         return(err);
589 #if defined(__WIN32__) && (_MSC_VER < 1500)
590                                 /*
591                                  * XXX: MSVC 6.0 does not support conversion
592                                  * of 64-bit integers into floating point
593                                  * values.
594                                  */
595                                 *value = _TIFFUInt64ToFloat(m);
596 #else
597                                 *value=(float)m;
598 #endif
599                                 return(TIFFReadDirEntryErrOk);
600                         }
601                 case TIFF_SLONG8:
602                         {
603                                 int64 m;
604                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
605                                 if (err!=TIFFReadDirEntryErrOk)
606                                         return(err);
607                                 *value=(float)m;
608                                 return(TIFFReadDirEntryErrOk);
609                         }
610                 case TIFF_RATIONAL:
611                         {
612                                 double m;
613                                 err=TIFFReadDirEntryCheckedRational(tif,direntry,&m);
614                                 if (err!=TIFFReadDirEntryErrOk)
615                                         return(err);
616                                 *value=(float)m;
617                                 return(TIFFReadDirEntryErrOk);
618                         }
619                 case TIFF_SRATIONAL:
620                         {
621                                 double m;
622                                 err=TIFFReadDirEntryCheckedSrational(tif,direntry,&m);
623                                 if (err!=TIFFReadDirEntryErrOk)
624                                         return(err);
625                                 *value=(float)m;
626                                 return(TIFFReadDirEntryErrOk);
627                         }
628                 case TIFF_FLOAT:
629                         TIFFReadDirEntryCheckedFloat(tif,direntry,value);
630                         return(TIFFReadDirEntryErrOk);
631                 case TIFF_DOUBLE:
632                         {
633                                 double m;
634                                 err=TIFFReadDirEntryCheckedDouble(tif,direntry,&m);
635                                 if (err!=TIFFReadDirEntryErrOk)
636                                         return(err);
637                                 *value=(float)m;
638                                 return(TIFFReadDirEntryErrOk);
639                         }
640                 default:
641                         return(TIFFReadDirEntryErrType);
642         }
643 }
644
645 static enum TIFFReadDirEntryErr TIFFReadDirEntryDouble(TIFF* tif, TIFFDirEntry* direntry, double* value)
646 {
647         enum TIFFReadDirEntryErr err;
648         if (direntry->tdir_count!=1)
649                 return(TIFFReadDirEntryErrCount);
650         switch (direntry->tdir_type)
651         {
652                 case TIFF_BYTE:
653                         {
654                                 uint8 m;
655                                 TIFFReadDirEntryCheckedByte(tif,direntry,&m);
656                                 *value=(double)m;
657                                 return(TIFFReadDirEntryErrOk);
658                         }
659                 case TIFF_SBYTE:
660                         {
661                                 int8 m;
662                                 TIFFReadDirEntryCheckedSbyte(tif,direntry,&m);
663                                 *value=(double)m;
664                                 return(TIFFReadDirEntryErrOk);
665                         }
666                 case TIFF_SHORT:
667                         {
668                                 uint16 m;
669                                 TIFFReadDirEntryCheckedShort(tif,direntry,&m);
670                                 *value=(double)m;
671                                 return(TIFFReadDirEntryErrOk);
672                         }
673                 case TIFF_SSHORT:
674                         {
675                                 int16 m;
676                                 TIFFReadDirEntryCheckedSshort(tif,direntry,&m);
677                                 *value=(double)m;
678                                 return(TIFFReadDirEntryErrOk);
679                         }
680                 case TIFF_LONG:
681                         {
682                                 uint32 m;
683                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
684                                 *value=(double)m;
685                                 return(TIFFReadDirEntryErrOk);
686                         }
687                 case TIFF_SLONG:
688                         {
689                                 int32 m;
690                                 TIFFReadDirEntryCheckedSlong(tif,direntry,&m);
691                                 *value=(double)m;
692                                 return(TIFFReadDirEntryErrOk);
693                         }
694                 case TIFF_LONG8:
695                         {
696                                 uint64 m;
697                                 err=TIFFReadDirEntryCheckedLong8(tif,direntry,&m);
698                                 if (err!=TIFFReadDirEntryErrOk)
699                                         return(err);
700 #if defined(__WIN32__) && (_MSC_VER < 1500)
701                                 /*
702                                  * XXX: MSVC 6.0 does not support conversion
703                                  * of 64-bit integers into floating point
704                                  * values.
705                                  */
706                                 *value = _TIFFUInt64ToDouble(m);
707 #else
708                                 *value = (double)m;
709 #endif
710                                 return(TIFFReadDirEntryErrOk);
711                         }
712                 case TIFF_SLONG8:
713                         {
714                                 int64 m;
715                                 err=TIFFReadDirEntryCheckedSlong8(tif,direntry,&m);
716                                 if (err!=TIFFReadDirEntryErrOk)
717                                         return(err);
718                                 *value=(double)m;
719                                 return(TIFFReadDirEntryErrOk);
720                         }
721                 case TIFF_RATIONAL:
722                         err=TIFFReadDirEntryCheckedRational(tif,direntry,value);
723                         return(err);
724                 case TIFF_SRATIONAL:
725                         err=TIFFReadDirEntryCheckedSrational(tif,direntry,value);
726                         return(err);
727                 case TIFF_FLOAT:
728                         {
729                                 float m;
730                                 TIFFReadDirEntryCheckedFloat(tif,direntry,&m);
731                                 *value=(double)m;
732                                 return(TIFFReadDirEntryErrOk);
733                         }
734                 case TIFF_DOUBLE:
735                         err=TIFFReadDirEntryCheckedDouble(tif,direntry,value);
736                         return(err);
737                 default:
738                         return(TIFFReadDirEntryErrType);
739         }
740 }
741
742 static enum TIFFReadDirEntryErr TIFFReadDirEntryIfd8(TIFF* tif, TIFFDirEntry* direntry, uint64* value)
743 {
744         enum TIFFReadDirEntryErr err;
745         if (direntry->tdir_count!=1)
746                 return(TIFFReadDirEntryErrCount);
747         switch (direntry->tdir_type)
748         {
749                 case TIFF_LONG:
750                 case TIFF_IFD:
751                         {
752                                 uint32 m;
753                                 TIFFReadDirEntryCheckedLong(tif,direntry,&m);
754                                 *value=(uint64)m;
755                                 return(TIFFReadDirEntryErrOk);
756                         }
757                 case TIFF_LONG8:
758                 case TIFF_IFD8:
759                         err=TIFFReadDirEntryCheckedLong8(tif,direntry,value);
760                         return(err);
761                 default:
762                         return(TIFFReadDirEntryErrType);
763         }
764 }
765
766 static enum TIFFReadDirEntryErr TIFFReadDirEntryArray(TIFF* tif, TIFFDirEntry* direntry, uint32* count, uint32 desttypesize, void** value)
767 {
768         int typesize;
769         uint32 datasize;
770         void* data;
771         typesize=TIFFDataWidth(direntry->tdir_type);
772         if ((direntry->tdir_count==0)||(typesize==0))
773         {
774                 *value=0;
775                 return(TIFFReadDirEntryErrOk);
776         }
777         (void) desttypesize;
778
779         /* 
780          * As a sanity check, make sure we have no more than a 2GB tag array 
781          * in either the current data type or the dest data type.  This also
782          * avoids problems with overflow of tmsize_t on 32bit systems.
783          */
784         if ((uint64)(2147483647/typesize)<direntry->tdir_count)
785                 return(TIFFReadDirEntryErrSizesan);
786         if ((uint64)(2147483647/desttypesize)<direntry->tdir_count)
787                 return(TIFFReadDirEntryErrSizesan);
788
789         *count=(uint32)direntry->tdir_count;
790         datasize=(*count)*typesize;
791         assert((tmsize_t)datasize>0);
792         data=_TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
793         if (data==0)
794                 return(TIFFReadDirEntryErrAlloc);
795         if (!(tif->tif_flags&TIFF_BIGTIFF))
796         {
797                 if (datasize<=4)
798                         _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
799                 else
800                 {
801                         enum TIFFReadDirEntryErr err;
802                         uint32 offset = direntry->tdir_offset.toff_long;
803                         if (tif->tif_flags&TIFF_SWAB)
804                                 TIFFSwabLong(&offset);
805                         err=TIFFReadDirEntryData(tif,(uint64)offset,(tmsize_t)datasize,data);
806                         if (err!=TIFFReadDirEntryErrOk)
807                         {
808                                 _TIFFfree(data);
809                                 return(err);
810                         }
811                 }
812         }
813         else
814         {
815                 if (datasize<=8)
816                         _TIFFmemcpy(data,&direntry->tdir_offset,datasize);
817                 else
818                 {
819                         enum TIFFReadDirEntryErr err;
820                         uint64 offset = direntry->tdir_offset.toff_long8;
821                         if (tif->tif_flags&TIFF_SWAB)
822                                 TIFFSwabLong8(&offset);
823                         err=TIFFReadDirEntryData(tif,offset,(tmsize_t)datasize,data);
824                         if (err!=TIFFReadDirEntryErrOk)
825                         {
826                                 _TIFFfree(data);
827                                 return(err);
828                         }
829                 }
830         }
831         *value=data;
832         return(TIFFReadDirEntryErrOk);
833 }
834
835 static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntry* direntry, uint8** value)
836 {
837         enum TIFFReadDirEntryErr err;
838         uint32 count;
839         void* origdata;
840         uint8* data;
841         switch (direntry->tdir_type)
842         {
843                 case TIFF_ASCII:
844                 case TIFF_UNDEFINED:
845                 case TIFF_BYTE:
846                 case TIFF_SBYTE:
847                 case TIFF_SHORT:
848                 case TIFF_SSHORT:
849                 case TIFF_LONG:
850                 case TIFF_SLONG:
851                 case TIFF_LONG8:
852                 case TIFF_SLONG8:
853                         break;
854                 default:
855                         return(TIFFReadDirEntryErrType);
856         }
857         err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
858         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
859         {
860                 *value=0;
861                 return(err);
862         }
863         switch (direntry->tdir_type)
864         {
865                 case TIFF_ASCII:
866                 case TIFF_UNDEFINED:
867                 case TIFF_BYTE:
868                         *value=(uint8*)origdata;
869                         return(TIFFReadDirEntryErrOk);
870                 case TIFF_SBYTE:
871                         {
872                                 int8* m;
873                                 uint32 n;
874                                 m=(int8*)origdata;
875                                 for (n=0; n<count; n++)
876                                 {
877                                         err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
878                                         if (err!=TIFFReadDirEntryErrOk)
879                                         {
880                                                 _TIFFfree(origdata);
881                                                 return(err);
882                                         }
883                                         m++;
884                                 }
885                                 *value=(uint8*)origdata;
886                                 return(TIFFReadDirEntryErrOk);
887                         }
888         }
889         data=(uint8*)_TIFFmalloc(count);
890         if (data==0)
891         {
892                 _TIFFfree(origdata);
893                 return(TIFFReadDirEntryErrAlloc);
894         }
895         switch (direntry->tdir_type)
896         {
897                 case TIFF_SHORT:
898                         {
899                                 uint16* ma;
900                                 uint8* mb;
901                                 uint32 n;
902                                 ma=(uint16*)origdata;
903                                 mb=data;
904                                 for (n=0; n<count; n++)
905                                 {
906                                         if (tif->tif_flags&TIFF_SWAB)
907                                                 TIFFSwabShort(ma);
908                                         err=TIFFReadDirEntryCheckRangeByteShort(*ma);
909                                         if (err!=TIFFReadDirEntryErrOk)
910                                                 break;
911                                         *mb++=(uint8)(*ma++);
912                                 }
913                         }
914                         break;
915                 case TIFF_SSHORT:
916                         {
917                                 int16* ma;
918                                 uint8* mb;
919                                 uint32 n;
920                                 ma=(int16*)origdata;
921                                 mb=data;
922                                 for (n=0; n<count; n++)
923                                 {
924                                         if (tif->tif_flags&TIFF_SWAB)
925                                                 TIFFSwabShort((uint16*)ma);
926                                         err=TIFFReadDirEntryCheckRangeByteSshort(*ma);
927                                         if (err!=TIFFReadDirEntryErrOk)
928                                                 break;
929                                         *mb++=(uint8)(*ma++);
930                                 }
931                         }
932                         break;
933                 case TIFF_LONG:
934                         {
935                                 uint32* ma;
936                                 uint8* mb;
937                                 uint32 n;
938                                 ma=(uint32*)origdata;
939                                 mb=data;
940                                 for (n=0; n<count; n++)
941                                 {
942                                         if (tif->tif_flags&TIFF_SWAB)
943                                                 TIFFSwabLong(ma);
944                                         err=TIFFReadDirEntryCheckRangeByteLong(*ma);
945                                         if (err!=TIFFReadDirEntryErrOk)
946                                                 break;
947                                         *mb++=(uint8)(*ma++);
948                                 }
949                         }
950                         break;
951                 case TIFF_SLONG:
952                         {
953                                 int32* ma;
954                                 uint8* mb;
955                                 uint32 n;
956                                 ma=(int32*)origdata;
957                                 mb=data;
958                                 for (n=0; n<count; n++)
959                                 {
960                                         if (tif->tif_flags&TIFF_SWAB)
961                                                 TIFFSwabLong((uint32*)ma);
962                                         err=TIFFReadDirEntryCheckRangeByteSlong(*ma);
963                                         if (err!=TIFFReadDirEntryErrOk)
964                                                 break;
965                                         *mb++=(uint8)(*ma++);
966                                 }
967                         }
968                         break;
969                 case TIFF_LONG8:
970                         {
971                                 uint64* ma;
972                                 uint8* mb;
973                                 uint32 n;
974                                 ma=(uint64*)origdata;
975                                 mb=data;
976                                 for (n=0; n<count; n++)
977                                 {
978                                         if (tif->tif_flags&TIFF_SWAB)
979                                                 TIFFSwabLong8(ma);
980                                         err=TIFFReadDirEntryCheckRangeByteLong8(*ma);
981                                         if (err!=TIFFReadDirEntryErrOk)
982                                                 break;
983                                         *mb++=(uint8)(*ma++);
984                                 }
985                         }
986                         break;
987                 case TIFF_SLONG8:
988                         {
989                                 int64* ma;
990                                 uint8* mb;
991                                 uint32 n;
992                                 ma=(int64*)origdata;
993                                 mb=data;
994                                 for (n=0; n<count; n++)
995                                 {
996                                         if (tif->tif_flags&TIFF_SWAB)
997                                                 TIFFSwabLong8((uint64*)ma);
998                                         err=TIFFReadDirEntryCheckRangeByteSlong8(*ma);
999                                         if (err!=TIFFReadDirEntryErrOk)
1000                                                 break;
1001                                         *mb++=(uint8)(*ma++);
1002                                 }
1003                         }
1004                         break;
1005         }
1006         _TIFFfree(origdata);
1007         if (err!=TIFFReadDirEntryErrOk)
1008         {
1009                 _TIFFfree(data);
1010                 return(err);
1011         }
1012         *value=data;
1013         return(TIFFReadDirEntryErrOk);
1014 }
1015
1016 static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEntry* direntry, int8** value)
1017 {
1018         enum TIFFReadDirEntryErr err;
1019         uint32 count;
1020         void* origdata;
1021         int8* data;
1022         switch (direntry->tdir_type)
1023         {
1024                 case TIFF_UNDEFINED:
1025                 case TIFF_BYTE:
1026                 case TIFF_SBYTE:
1027                 case TIFF_SHORT:
1028                 case TIFF_SSHORT:
1029                 case TIFF_LONG:
1030                 case TIFF_SLONG:
1031                 case TIFF_LONG8:
1032                 case TIFF_SLONG8:
1033                         break;
1034                 default:
1035                         return(TIFFReadDirEntryErrType);
1036         }
1037         err=TIFFReadDirEntryArray(tif,direntry,&count,1,&origdata);
1038         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1039         {
1040                 *value=0;
1041                 return(err);
1042         }
1043         switch (direntry->tdir_type)
1044         {
1045                 case TIFF_UNDEFINED:
1046                 case TIFF_BYTE:
1047                         {
1048                                 uint8* m;
1049                                 uint32 n;
1050                                 m=(uint8*)origdata;
1051                                 for (n=0; n<count; n++)
1052                                 {
1053                                         err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
1054                                         if (err!=TIFFReadDirEntryErrOk)
1055                                         {
1056                                                 _TIFFfree(origdata);
1057                                                 return(err);
1058                                         }
1059                                         m++;
1060                                 }
1061                                 *value=(int8*)origdata;
1062                                 return(TIFFReadDirEntryErrOk);
1063                         }
1064                 case TIFF_SBYTE:
1065                         *value=(int8*)origdata;
1066                         return(TIFFReadDirEntryErrOk);
1067         }
1068         data=(int8*)_TIFFmalloc(count);
1069         if (data==0)
1070         {
1071                 _TIFFfree(origdata);
1072                 return(TIFFReadDirEntryErrAlloc);
1073         }
1074         switch (direntry->tdir_type)
1075         {
1076                 case TIFF_SHORT:
1077                         {
1078                                 uint16* ma;
1079                                 int8* mb;
1080                                 uint32 n;
1081                                 ma=(uint16*)origdata;
1082                                 mb=data;
1083                                 for (n=0; n<count; n++)
1084                                 {
1085                                         if (tif->tif_flags&TIFF_SWAB)
1086                                                 TIFFSwabShort(ma);
1087                                         err=TIFFReadDirEntryCheckRangeSbyteShort(*ma);
1088                                         if (err!=TIFFReadDirEntryErrOk)
1089                                                 break;
1090                                         *mb++=(int8)(*ma++);
1091                                 }
1092                         }
1093                         break;
1094                 case TIFF_SSHORT:
1095                         {
1096                                 int16* ma;
1097                                 int8* mb;
1098                                 uint32 n;
1099                                 ma=(int16*)origdata;
1100                                 mb=data;
1101                                 for (n=0; n<count; n++)
1102                                 {
1103                                         if (tif->tif_flags&TIFF_SWAB)
1104                                                 TIFFSwabShort((uint16*)ma);
1105                                         err=TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
1106                                         if (err!=TIFFReadDirEntryErrOk)
1107                                                 break;
1108                                         *mb++=(int8)(*ma++);
1109                                 }
1110                         }
1111                         break;
1112                 case TIFF_LONG:
1113                         {
1114                                 uint32* ma;
1115                                 int8* mb;
1116                                 uint32 n;
1117                                 ma=(uint32*)origdata;
1118                                 mb=data;
1119                                 for (n=0; n<count; n++)
1120                                 {
1121                                         if (tif->tif_flags&TIFF_SWAB)
1122                                                 TIFFSwabLong(ma);
1123                                         err=TIFFReadDirEntryCheckRangeSbyteLong(*ma);
1124                                         if (err!=TIFFReadDirEntryErrOk)
1125                                                 break;
1126                                         *mb++=(int8)(*ma++);
1127                                 }
1128                         }
1129                         break;
1130                 case TIFF_SLONG:
1131                         {
1132                                 int32* ma;
1133                                 int8* mb;
1134                                 uint32 n;
1135                                 ma=(int32*)origdata;
1136                                 mb=data;
1137                                 for (n=0; n<count; n++)
1138                                 {
1139                                         if (tif->tif_flags&TIFF_SWAB)
1140                                                 TIFFSwabLong((uint32*)ma);
1141                                         err=TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
1142                                         if (err!=TIFFReadDirEntryErrOk)
1143                                                 break;
1144                                         *mb++=(int8)(*ma++);
1145                                 }
1146                         }
1147                         break;
1148                 case TIFF_LONG8:
1149                         {
1150                                 uint64* ma;
1151                                 int8* mb;
1152                                 uint32 n;
1153                                 ma=(uint64*)origdata;
1154                                 mb=data;
1155                                 for (n=0; n<count; n++)
1156                                 {
1157                                         if (tif->tif_flags&TIFF_SWAB)
1158                                                 TIFFSwabLong8(ma);
1159                                         err=TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
1160                                         if (err!=TIFFReadDirEntryErrOk)
1161                                                 break;
1162                                         *mb++=(int8)(*ma++);
1163                                 }
1164                         }
1165                         break;
1166                 case TIFF_SLONG8:
1167                         {
1168                                 int64* ma;
1169                                 int8* mb;
1170                                 uint32 n;
1171                                 ma=(int64*)origdata;
1172                                 mb=data;
1173                                 for (n=0; n<count; n++)
1174                                 {
1175                                         if (tif->tif_flags&TIFF_SWAB)
1176                                                 TIFFSwabLong8((uint64*)ma);
1177                                         err=TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
1178                                         if (err!=TIFFReadDirEntryErrOk)
1179                                                 break;
1180                                         *mb++=(int8)(*ma++);
1181                                 }
1182                         }
1183                         break;
1184         }
1185         _TIFFfree(origdata);
1186         if (err!=TIFFReadDirEntryErrOk)
1187         {
1188                 _TIFFfree(data);
1189                 return(err);
1190         }
1191         *value=data;
1192         return(TIFFReadDirEntryErrOk);
1193 }
1194
1195 static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEntry* direntry, uint16** value)
1196 {
1197         enum TIFFReadDirEntryErr err;
1198         uint32 count;
1199         void* origdata;
1200         uint16* data;
1201         switch (direntry->tdir_type)
1202         {
1203                 case TIFF_BYTE:
1204                 case TIFF_SBYTE:
1205                 case TIFF_SHORT:
1206                 case TIFF_SSHORT:
1207                 case TIFF_LONG:
1208                 case TIFF_SLONG:
1209                 case TIFF_LONG8:
1210                 case TIFF_SLONG8:
1211                         break;
1212                 default:
1213                         return(TIFFReadDirEntryErrType);
1214         }
1215         err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1216         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1217         {
1218                 *value=0;
1219                 return(err);
1220         }
1221         switch (direntry->tdir_type)
1222         {
1223                 case TIFF_SHORT:
1224                         *value=(uint16*)origdata;
1225                         if (tif->tif_flags&TIFF_SWAB)
1226                                 TIFFSwabArrayOfShort(*value,count);  
1227                         return(TIFFReadDirEntryErrOk);
1228                 case TIFF_SSHORT:
1229                         {
1230                                 int16* m;
1231                                 uint32 n;
1232                                 m=(int16*)origdata;
1233                                 for (n=0; n<count; n++)
1234                                 {
1235                                         if (tif->tif_flags&TIFF_SWAB)
1236                                                 TIFFSwabShort((uint16*)m);
1237                                         err=TIFFReadDirEntryCheckRangeShortSshort(*m);
1238                                         if (err!=TIFFReadDirEntryErrOk)
1239                                         {
1240                                                 _TIFFfree(origdata);
1241                                                 return(err);
1242                                         }
1243                                         m++;
1244                                 }
1245                                 *value=(uint16*)origdata;
1246                                 return(TIFFReadDirEntryErrOk);
1247                         }
1248         }
1249         data=(uint16*)_TIFFmalloc(count*2);
1250         if (data==0)
1251         {
1252                 _TIFFfree(origdata);
1253                 return(TIFFReadDirEntryErrAlloc);
1254         }
1255         switch (direntry->tdir_type)
1256         {
1257                 case TIFF_BYTE:
1258                         {
1259                                 uint8* ma;
1260                                 uint16* mb;
1261                                 uint32 n;
1262                                 ma=(uint8*)origdata;
1263                                 mb=data;
1264                                 for (n=0; n<count; n++)
1265                                         *mb++=(uint16)(*ma++);
1266                         }
1267                         break;
1268                 case TIFF_SBYTE:
1269                         {
1270                                 int8* ma;
1271                                 uint16* mb;
1272                                 uint32 n;
1273                                 ma=(int8*)origdata;
1274                                 mb=data;
1275                                 for (n=0; n<count; n++)
1276                                 {
1277                                         err=TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1278                                         if (err!=TIFFReadDirEntryErrOk)
1279                                                 break;
1280                                         *mb++=(uint16)(*ma++);
1281                                 }
1282                         }
1283                         break;
1284                 case TIFF_LONG:
1285                         {
1286                                 uint32* ma;
1287                                 uint16* mb;
1288                                 uint32 n;
1289                                 ma=(uint32*)origdata;
1290                                 mb=data;
1291                                 for (n=0; n<count; n++)
1292                                 {
1293                                         if (tif->tif_flags&TIFF_SWAB)
1294                                                 TIFFSwabLong(ma);
1295                                         err=TIFFReadDirEntryCheckRangeShortLong(*ma);
1296                                         if (err!=TIFFReadDirEntryErrOk)
1297                                                 break;
1298                                         *mb++=(uint16)(*ma++);
1299                                 }
1300                         }
1301                         break;
1302                 case TIFF_SLONG:
1303                         {
1304                                 int32* ma;
1305                                 uint16* mb;
1306                                 uint32 n;
1307                                 ma=(int32*)origdata;
1308                                 mb=data;
1309                                 for (n=0; n<count; n++)
1310                                 {
1311                                         if (tif->tif_flags&TIFF_SWAB)
1312                                                 TIFFSwabLong((uint32*)ma);
1313                                         err=TIFFReadDirEntryCheckRangeShortSlong(*ma);
1314                                         if (err!=TIFFReadDirEntryErrOk)
1315                                                 break;
1316                                         *mb++=(uint16)(*ma++);
1317                                 }
1318                         }
1319                         break;
1320                 case TIFF_LONG8:
1321                         {
1322                                 uint64* ma;
1323                                 uint16* mb;
1324                                 uint32 n;
1325                                 ma=(uint64*)origdata;
1326                                 mb=data;
1327                                 for (n=0; n<count; n++)
1328                                 {
1329                                         if (tif->tif_flags&TIFF_SWAB)
1330                                                 TIFFSwabLong8(ma);
1331                                         err=TIFFReadDirEntryCheckRangeShortLong8(*ma);
1332                                         if (err!=TIFFReadDirEntryErrOk)
1333                                                 break;
1334                                         *mb++=(uint16)(*ma++);
1335                                 }
1336                         }
1337                         break;
1338                 case TIFF_SLONG8:
1339                         {
1340                                 int64* ma;
1341                                 uint16* mb;
1342                                 uint32 n;
1343                                 ma=(int64*)origdata;
1344                                 mb=data;
1345                                 for (n=0; n<count; n++)
1346                                 {
1347                                         if (tif->tif_flags&TIFF_SWAB)
1348                                                 TIFFSwabLong8((uint64*)ma);
1349                                         err=TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1350                                         if (err!=TIFFReadDirEntryErrOk)
1351                                                 break;
1352                                         *mb++=(uint16)(*ma++);
1353                                 }
1354                         }
1355                         break;
1356         }
1357         _TIFFfree(origdata);
1358         if (err!=TIFFReadDirEntryErrOk)
1359         {
1360                 _TIFFfree(data);
1361                 return(err);
1362         }
1363         *value=data;
1364         return(TIFFReadDirEntryErrOk);
1365 }
1366
1367 static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEntry* direntry, int16** value)
1368 {
1369         enum TIFFReadDirEntryErr err;
1370         uint32 count;
1371         void* origdata;
1372         int16* data;
1373         switch (direntry->tdir_type)
1374         {
1375                 case TIFF_BYTE:
1376                 case TIFF_SBYTE:
1377                 case TIFF_SHORT:
1378                 case TIFF_SSHORT:
1379                 case TIFF_LONG:
1380                 case TIFF_SLONG:
1381                 case TIFF_LONG8:
1382                 case TIFF_SLONG8:
1383                         break;
1384                 default:
1385                         return(TIFFReadDirEntryErrType);
1386         }
1387         err=TIFFReadDirEntryArray(tif,direntry,&count,2,&origdata);
1388         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1389         {
1390                 *value=0;
1391                 return(err);
1392         }
1393         switch (direntry->tdir_type)
1394         {
1395                 case TIFF_SHORT:
1396                         {
1397                                 uint16* m;
1398                                 uint32 n;
1399                                 m=(uint16*)origdata;
1400                                 for (n=0; n<count; n++)
1401                                 {
1402                                         if (tif->tif_flags&TIFF_SWAB)
1403                                                 TIFFSwabShort(m);
1404                                         err=TIFFReadDirEntryCheckRangeSshortShort(*m);
1405                                         if (err!=TIFFReadDirEntryErrOk)
1406                                         {
1407                                                 _TIFFfree(origdata);
1408                                                 return(err);
1409                                         }
1410                                         m++;
1411                                 }
1412                                 *value=(int16*)origdata;
1413                                 return(TIFFReadDirEntryErrOk);
1414                         }
1415                 case TIFF_SSHORT:
1416                         *value=(int16*)origdata;
1417                         if (tif->tif_flags&TIFF_SWAB)
1418                                 TIFFSwabArrayOfShort((uint16*)(*value),count);
1419                         return(TIFFReadDirEntryErrOk);
1420         }
1421         data=(int16*)_TIFFmalloc(count*2);
1422         if (data==0)
1423         {
1424                 _TIFFfree(origdata);
1425                 return(TIFFReadDirEntryErrAlloc);
1426         }
1427         switch (direntry->tdir_type)
1428         {
1429                 case TIFF_BYTE:
1430                         {
1431                                 uint8* ma;
1432                                 int16* mb;
1433                                 uint32 n;
1434                                 ma=(uint8*)origdata;
1435                                 mb=data;
1436                                 for (n=0; n<count; n++)
1437                                         *mb++=(int16)(*ma++);
1438                         }
1439                         break;
1440                 case TIFF_SBYTE:
1441                         {
1442                                 int8* ma;
1443                                 int16* mb;
1444                                 uint32 n;
1445                                 ma=(int8*)origdata;
1446                                 mb=data;
1447                                 for (n=0; n<count; n++)
1448                                         *mb++=(int16)(*ma++);
1449                         }
1450                         break;
1451                 case TIFF_LONG:
1452                         {
1453                                 uint32* ma;
1454                                 int16* mb;
1455                                 uint32 n;
1456                                 ma=(uint32*)origdata;
1457                                 mb=data;
1458                                 for (n=0; n<count; n++)
1459                                 {
1460                                         if (tif->tif_flags&TIFF_SWAB)
1461                                                 TIFFSwabLong(ma);
1462                                         err=TIFFReadDirEntryCheckRangeSshortLong(*ma);
1463                                         if (err!=TIFFReadDirEntryErrOk)
1464                                                 break;
1465                                         *mb++=(int16)(*ma++);
1466                                 }
1467                         }
1468                         break;
1469                 case TIFF_SLONG:
1470                         {
1471                                 int32* ma;
1472                                 int16* mb;
1473                                 uint32 n;
1474                                 ma=(int32*)origdata;
1475                                 mb=data;
1476                                 for (n=0; n<count; n++)
1477                                 {
1478                                         if (tif->tif_flags&TIFF_SWAB)
1479                                                 TIFFSwabLong((uint32*)ma);
1480                                         err=TIFFReadDirEntryCheckRangeSshortSlong(*ma);
1481                                         if (err!=TIFFReadDirEntryErrOk)
1482                                                 break;
1483                                         *mb++=(int16)(*ma++);
1484                                 }
1485                         }
1486                         break;
1487                 case TIFF_LONG8:
1488                         {
1489                                 uint64* ma;
1490                                 int16* mb;
1491                                 uint32 n;
1492                                 ma=(uint64*)origdata;
1493                                 mb=data;
1494                                 for (n=0; n<count; n++)
1495                                 {
1496                                         if (tif->tif_flags&TIFF_SWAB)
1497                                                 TIFFSwabLong8(ma);
1498                                         err=TIFFReadDirEntryCheckRangeSshortLong8(*ma);
1499                                         if (err!=TIFFReadDirEntryErrOk)
1500                                                 break;
1501                                         *mb++=(int16)(*ma++);
1502                                 }
1503                         }
1504                         break;
1505                 case TIFF_SLONG8:
1506                         {
1507                                 int64* ma;
1508                                 int16* mb;
1509                                 uint32 n;
1510                                 ma=(int64*)origdata;
1511                                 mb=data;
1512                                 for (n=0; n<count; n++)
1513                                 {
1514                                         if (tif->tif_flags&TIFF_SWAB)
1515                                                 TIFFSwabLong8((uint64*)ma);
1516                                         err=TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
1517                                         if (err!=TIFFReadDirEntryErrOk)
1518                                                 break;
1519                                         *mb++=(int16)(*ma++);
1520                                 }
1521                         }
1522                         break;
1523         }
1524         _TIFFfree(origdata);
1525         if (err!=TIFFReadDirEntryErrOk)
1526         {
1527                 _TIFFfree(data);
1528                 return(err);
1529         }
1530         *value=data;
1531         return(TIFFReadDirEntryErrOk);
1532 }
1533
1534 static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntry* direntry, uint32** value)
1535 {
1536         enum TIFFReadDirEntryErr err;
1537         uint32 count;
1538         void* origdata;
1539         uint32* data;
1540         switch (direntry->tdir_type)
1541         {
1542                 case TIFF_BYTE:
1543                 case TIFF_SBYTE:
1544                 case TIFF_SHORT:
1545                 case TIFF_SSHORT:
1546                 case TIFF_LONG:
1547                 case TIFF_SLONG:
1548                 case TIFF_LONG8:
1549                 case TIFF_SLONG8:
1550                         break;
1551                 default:
1552                         return(TIFFReadDirEntryErrType);
1553         }
1554         err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1555         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1556         {
1557                 *value=0;
1558                 return(err);
1559         }
1560         switch (direntry->tdir_type)
1561         {
1562                 case TIFF_LONG:
1563                         *value=(uint32*)origdata;
1564                         if (tif->tif_flags&TIFF_SWAB)
1565                                 TIFFSwabArrayOfLong(*value,count);
1566                         return(TIFFReadDirEntryErrOk);
1567                 case TIFF_SLONG:
1568                         {
1569                                 int32* m;
1570                                 uint32 n;
1571                                 m=(int32*)origdata;
1572                                 for (n=0; n<count; n++)
1573                                 {
1574                                         if (tif->tif_flags&TIFF_SWAB)
1575                                                 TIFFSwabLong((uint32*)m);
1576                                         err=TIFFReadDirEntryCheckRangeLongSlong(*m);
1577                                         if (err!=TIFFReadDirEntryErrOk)
1578                                         {
1579                                                 _TIFFfree(origdata);
1580                                                 return(err);
1581                                         }
1582                                         m++;
1583                                 }
1584                                 *value=(uint32*)origdata;
1585                                 return(TIFFReadDirEntryErrOk);
1586                         }
1587         }
1588         data=(uint32*)_TIFFmalloc(count*4);
1589         if (data==0)
1590         {
1591                 _TIFFfree(origdata);
1592                 return(TIFFReadDirEntryErrAlloc);
1593         }
1594         switch (direntry->tdir_type)
1595         {
1596                 case TIFF_BYTE:
1597                         {
1598                                 uint8* ma;
1599                                 uint32* mb;
1600                                 uint32 n;
1601                                 ma=(uint8*)origdata;
1602                                 mb=data;
1603                                 for (n=0; n<count; n++)
1604                                         *mb++=(uint32)(*ma++);
1605                         }
1606                         break;
1607                 case TIFF_SBYTE:
1608                         {
1609                                 int8* ma;
1610                                 uint32* mb;
1611                                 uint32 n;
1612                                 ma=(int8*)origdata;
1613                                 mb=data;
1614                                 for (n=0; n<count; n++)
1615                                 {
1616                                         err=TIFFReadDirEntryCheckRangeLongSbyte(*ma);
1617                                         if (err!=TIFFReadDirEntryErrOk)
1618                                                 break;
1619                                         *mb++=(uint32)(*ma++);
1620                                 }
1621                         }
1622                         break;
1623                 case TIFF_SHORT:
1624                         {
1625                                 uint16* ma;
1626                                 uint32* mb;
1627                                 uint32 n;
1628                                 ma=(uint16*)origdata;
1629                                 mb=data;
1630                                 for (n=0; n<count; n++)
1631                                 {
1632                                         if (tif->tif_flags&TIFF_SWAB)
1633                                                 TIFFSwabShort(ma);
1634                                         *mb++=(uint32)(*ma++);
1635                                 }
1636                         }
1637                         break;
1638                 case TIFF_SSHORT:
1639                         {
1640                                 int16* ma;
1641                                 uint32* mb;
1642                                 uint32 n;
1643                                 ma=(int16*)origdata;
1644                                 mb=data;
1645                                 for (n=0; n<count; n++)
1646                                 {
1647                                         if (tif->tif_flags&TIFF_SWAB)
1648                                                 TIFFSwabShort((uint16*)ma);
1649                                         err=TIFFReadDirEntryCheckRangeLongSshort(*ma);
1650                                         if (err!=TIFFReadDirEntryErrOk)
1651                                                 break;
1652                                         *mb++=(uint32)(*ma++);
1653                                 }
1654                         }
1655                         break;
1656                 case TIFF_LONG8:
1657                         {
1658                                 uint64* ma;
1659                                 uint32* mb;
1660                                 uint32 n;
1661                                 ma=(uint64*)origdata;
1662                                 mb=data;
1663                                 for (n=0; n<count; n++)
1664                                 {
1665                                         if (tif->tif_flags&TIFF_SWAB)
1666                                                 TIFFSwabLong8(ma);
1667                                         err=TIFFReadDirEntryCheckRangeLongLong8(*ma);
1668                                         if (err!=TIFFReadDirEntryErrOk)
1669                                                 break;
1670                                         *mb++=(uint32)(*ma++);
1671                                 }
1672                         }
1673                         break;
1674                 case TIFF_SLONG8:
1675                         {
1676                                 int64* ma;
1677                                 uint32* mb;
1678                                 uint32 n;
1679                                 ma=(int64*)origdata;
1680                                 mb=data;
1681                                 for (n=0; n<count; n++)
1682                                 {
1683                                         if (tif->tif_flags&TIFF_SWAB)
1684                                                 TIFFSwabLong8((uint64*)ma);
1685                                         err=TIFFReadDirEntryCheckRangeLongSlong8(*ma);
1686                                         if (err!=TIFFReadDirEntryErrOk)
1687                                                 break;
1688                                         *mb++=(uint32)(*ma++);
1689                                 }
1690                         }
1691                         break;
1692         }
1693         _TIFFfree(origdata);
1694         if (err!=TIFFReadDirEntryErrOk)
1695         {
1696                 _TIFFfree(data);
1697                 return(err);
1698         }
1699         *value=data;
1700         return(TIFFReadDirEntryErrOk);
1701 }
1702
1703 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEntry* direntry, int32** value)
1704 {
1705         enum TIFFReadDirEntryErr err;
1706         uint32 count;
1707         void* origdata;
1708         int32* data;
1709         switch (direntry->tdir_type)
1710         {
1711                 case TIFF_BYTE:
1712                 case TIFF_SBYTE:
1713                 case TIFF_SHORT:
1714                 case TIFF_SSHORT:
1715                 case TIFF_LONG:
1716                 case TIFF_SLONG:
1717                 case TIFF_LONG8:
1718                 case TIFF_SLONG8:
1719                         break;
1720                 default:
1721                         return(TIFFReadDirEntryErrType);
1722         }
1723         err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
1724         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1725         {
1726                 *value=0;
1727                 return(err);
1728         }
1729         switch (direntry->tdir_type)
1730         {
1731                 case TIFF_LONG:
1732                         {
1733                                 uint32* m;
1734                                 uint32 n;
1735                                 m=(uint32*)origdata;
1736                                 for (n=0; n<count; n++)
1737                                 {
1738                                         if (tif->tif_flags&TIFF_SWAB)
1739                                                 TIFFSwabLong((uint32*)m);
1740                                         err=TIFFReadDirEntryCheckRangeSlongLong(*m);
1741                                         if (err!=TIFFReadDirEntryErrOk)
1742                                         {
1743                                                 _TIFFfree(origdata);
1744                                                 return(err);
1745                                         }
1746                                         m++;
1747                                 }
1748                                 *value=(int32*)origdata;
1749                                 return(TIFFReadDirEntryErrOk);
1750                         }
1751                 case TIFF_SLONG:
1752                         *value=(int32*)origdata;
1753                         if (tif->tif_flags&TIFF_SWAB)
1754                                 TIFFSwabArrayOfLong((uint32*)(*value),count);
1755                         return(TIFFReadDirEntryErrOk);
1756         }
1757         data=(int32*)_TIFFmalloc(count*4);
1758         if (data==0)
1759         {
1760                 _TIFFfree(origdata);
1761                 return(TIFFReadDirEntryErrAlloc);
1762         }
1763         switch (direntry->tdir_type)
1764         {
1765                 case TIFF_BYTE:
1766                         {
1767                                 uint8* ma;
1768                                 int32* mb;
1769                                 uint32 n;
1770                                 ma=(uint8*)origdata;
1771                                 mb=data;
1772                                 for (n=0; n<count; n++)
1773                                         *mb++=(int32)(*ma++);
1774                         }
1775                         break;
1776                 case TIFF_SBYTE:
1777                         {
1778                                 int8* ma;
1779                                 int32* mb;
1780                                 uint32 n;
1781                                 ma=(int8*)origdata;
1782                                 mb=data;
1783                                 for (n=0; n<count; n++)
1784                                         *mb++=(int32)(*ma++);
1785                         }
1786                         break;
1787                 case TIFF_SHORT:
1788                         {
1789                                 uint16* ma;
1790                                 int32* mb;
1791                                 uint32 n;
1792                                 ma=(uint16*)origdata;
1793                                 mb=data;
1794                                 for (n=0; n<count; n++)
1795                                 {
1796                                         if (tif->tif_flags&TIFF_SWAB)
1797                                                 TIFFSwabShort(ma);
1798                                         *mb++=(int32)(*ma++);
1799                                 }
1800                         }
1801                         break;
1802                 case TIFF_SSHORT:
1803                         {
1804                                 int16* ma;
1805                                 int32* mb;
1806                                 uint32 n;
1807                                 ma=(int16*)origdata;
1808                                 mb=data;
1809                                 for (n=0; n<count; n++)
1810                                 {
1811                                         if (tif->tif_flags&TIFF_SWAB)
1812                                                 TIFFSwabShort((uint16*)ma);
1813                                         *mb++=(int32)(*ma++);
1814                                 }
1815                         }
1816                         break;
1817                 case TIFF_LONG8:
1818                         {
1819                                 uint64* ma;
1820                                 int32* mb;
1821                                 uint32 n;
1822                                 ma=(uint64*)origdata;
1823                                 mb=data;
1824                                 for (n=0; n<count; n++)
1825                                 {
1826                                         if (tif->tif_flags&TIFF_SWAB)
1827                                                 TIFFSwabLong8(ma);
1828                                         err=TIFFReadDirEntryCheckRangeSlongLong8(*ma);
1829                                         if (err!=TIFFReadDirEntryErrOk)
1830                                                 break;
1831                                         *mb++=(int32)(*ma++);
1832                                 }
1833                         }
1834                         break;
1835                 case TIFF_SLONG8:
1836                         {
1837                                 int64* ma;
1838                                 int32* mb;
1839                                 uint32 n;
1840                                 ma=(int64*)origdata;
1841                                 mb=data;
1842                                 for (n=0; n<count; n++)
1843                                 {
1844                                         if (tif->tif_flags&TIFF_SWAB)
1845                                                 TIFFSwabLong8((uint64*)ma);
1846                                         err=TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
1847                                         if (err!=TIFFReadDirEntryErrOk)
1848                                                 break;
1849                                         *mb++=(int32)(*ma++);
1850                                 }
1851                         }
1852                         break;
1853         }
1854         _TIFFfree(origdata);
1855         if (err!=TIFFReadDirEntryErrOk)
1856         {
1857                 _TIFFfree(data);
1858                 return(err);
1859         }
1860         *value=data;
1861         return(TIFFReadDirEntryErrOk);
1862 }
1863
1864 static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8Array(TIFF* tif, TIFFDirEntry* direntry, uint64** value)
1865 {
1866         enum TIFFReadDirEntryErr err;
1867         uint32 count;
1868         void* origdata;
1869         uint64* data;
1870         switch (direntry->tdir_type)
1871         {
1872                 case TIFF_BYTE:
1873                 case TIFF_SBYTE:
1874                 case TIFF_SHORT:
1875                 case TIFF_SSHORT:
1876                 case TIFF_LONG:
1877                 case TIFF_SLONG:
1878                 case TIFF_LONG8:
1879                 case TIFF_SLONG8:
1880                         break;
1881                 default:
1882                         return(TIFFReadDirEntryErrType);
1883         }
1884         err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
1885         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
1886         {
1887                 *value=0;
1888                 return(err);
1889         }
1890         switch (direntry->tdir_type)
1891         {
1892                 case TIFF_LONG8:
1893                         *value=(uint64*)origdata;
1894                         if (tif->tif_flags&TIFF_SWAB)
1895                                 TIFFSwabArrayOfLong8(*value,count);
1896                         return(TIFFReadDirEntryErrOk);
1897                 case TIFF_SLONG8:
1898                         {
1899                                 int64* m;
1900                                 uint32 n;
1901                                 m=(int64*)origdata;
1902                                 for (n=0; n<count; n++)
1903                                 {
1904                                         if (tif->tif_flags&TIFF_SWAB)
1905                                                 TIFFSwabLong8((uint64*)m);
1906                                         err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1907                                         if (err!=TIFFReadDirEntryErrOk)
1908                                         {
1909                                                 _TIFFfree(origdata);
1910                                                 return(err);
1911                                         }
1912                                         m++;
1913                                 }
1914                                 *value=(uint64*)origdata;
1915                                 return(TIFFReadDirEntryErrOk);
1916                         }
1917         }
1918         data=(uint64*)_TIFFmalloc(count*8);
1919         if (data==0)
1920         {
1921                 _TIFFfree(origdata);
1922                 return(TIFFReadDirEntryErrAlloc);
1923         }
1924         switch (direntry->tdir_type)
1925         {
1926                 case TIFF_BYTE:
1927                         {
1928                                 uint8* ma;
1929                                 uint64* mb;
1930                                 uint32 n;
1931                                 ma=(uint8*)origdata;
1932                                 mb=data;
1933                                 for (n=0; n<count; n++)
1934                                         *mb++=(uint64)(*ma++);
1935                         }
1936                         break;
1937                 case TIFF_SBYTE:
1938                         {
1939                                 int8* ma;
1940                                 uint64* mb;
1941                                 uint32 n;
1942                                 ma=(int8*)origdata;
1943                                 mb=data;
1944                                 for (n=0; n<count; n++)
1945                                 {
1946                                         err=TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
1947                                         if (err!=TIFFReadDirEntryErrOk)
1948                                                 break;
1949                                         *mb++=(uint64)(*ma++);
1950                                 }
1951                         }
1952                         break;
1953                 case TIFF_SHORT:
1954                         {
1955                                 uint16* ma;
1956                                 uint64* mb;
1957                                 uint32 n;
1958                                 ma=(uint16*)origdata;
1959                                 mb=data;
1960                                 for (n=0; n<count; n++)
1961                                 {
1962                                         if (tif->tif_flags&TIFF_SWAB)
1963                                                 TIFFSwabShort(ma);
1964                                         *mb++=(uint64)(*ma++);
1965                                 }
1966                         }
1967                         break;
1968                 case TIFF_SSHORT:
1969                         {
1970                                 int16* ma;
1971                                 uint64* mb;
1972                                 uint32 n;
1973                                 ma=(int16*)origdata;
1974                                 mb=data;
1975                                 for (n=0; n<count; n++)
1976                                 {
1977                                         if (tif->tif_flags&TIFF_SWAB)
1978                                                 TIFFSwabShort((uint16*)ma);
1979                                         err=TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
1980                                         if (err!=TIFFReadDirEntryErrOk)
1981                                                 break;
1982                                         *mb++=(uint64)(*ma++);
1983                                 }
1984                         }
1985                         break;
1986                 case TIFF_LONG:
1987                         {
1988                                 uint32* ma;
1989                                 uint64* mb;
1990                                 uint32 n;
1991                                 ma=(uint32*)origdata;
1992                                 mb=data;
1993                                 for (n=0; n<count; n++)
1994                                 {
1995                                         if (tif->tif_flags&TIFF_SWAB)
1996                                                 TIFFSwabLong(ma);
1997                                         *mb++=(uint64)(*ma++);
1998                                 }
1999                         }
2000                         break;
2001                 case TIFF_SLONG:
2002                         {
2003                                 int32* ma;
2004                                 uint64* mb;
2005                                 uint32 n;
2006                                 ma=(int32*)origdata;
2007                                 mb=data;
2008                                 for (n=0; n<count; n++)
2009                                 {
2010                                         if (tif->tif_flags&TIFF_SWAB)
2011                                                 TIFFSwabLong((uint32*)ma);
2012                                         err=TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2013                                         if (err!=TIFFReadDirEntryErrOk)
2014                                                 break;
2015                                         *mb++=(uint64)(*ma++);
2016                                 }
2017                         }
2018                         break;
2019         }
2020         _TIFFfree(origdata);
2021         if (err!=TIFFReadDirEntryErrOk)
2022         {
2023                 _TIFFfree(data);
2024                 return(err);
2025         }
2026         *value=data;
2027         return(TIFFReadDirEntryErrOk);
2028 }
2029
2030 static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEntry* direntry, int64** value)
2031 {
2032         enum TIFFReadDirEntryErr err;
2033         uint32 count;
2034         void* origdata;
2035         int64* data;
2036         switch (direntry->tdir_type)
2037         {
2038                 case TIFF_BYTE:
2039                 case TIFF_SBYTE:
2040                 case TIFF_SHORT:
2041                 case TIFF_SSHORT:
2042                 case TIFF_LONG:
2043                 case TIFF_SLONG:
2044                 case TIFF_LONG8:
2045                 case TIFF_SLONG8:
2046                         break;
2047                 default:
2048                         return(TIFFReadDirEntryErrType);
2049         }
2050         err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2051         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2052         {
2053                 *value=0;
2054                 return(err);
2055         }
2056         switch (direntry->tdir_type)
2057         {
2058                 case TIFF_LONG8:
2059                         {
2060                                 uint64* m;
2061                                 uint32 n;
2062                                 m=(uint64*)origdata;
2063                                 for (n=0; n<count; n++)
2064                                 {
2065                                         if (tif->tif_flags&TIFF_SWAB)
2066                                                 TIFFSwabLong8(m);
2067                                         err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
2068                                         if (err!=TIFFReadDirEntryErrOk)
2069                                         {
2070                                                 _TIFFfree(origdata);
2071                                                 return(err);
2072                                         }
2073                                         m++;
2074                                 }
2075                                 *value=(int64*)origdata;
2076                                 return(TIFFReadDirEntryErrOk);
2077                         }
2078                 case TIFF_SLONG8:
2079                         *value=(int64*)origdata;
2080                         if (tif->tif_flags&TIFF_SWAB)
2081                                 TIFFSwabArrayOfLong8((uint64*)(*value),count);
2082                         return(TIFFReadDirEntryErrOk);
2083         }
2084         data=(int64*)_TIFFmalloc(count*8);
2085         if (data==0)
2086         {
2087                 _TIFFfree(origdata);
2088                 return(TIFFReadDirEntryErrAlloc);
2089         }
2090         switch (direntry->tdir_type)
2091         {
2092                 case TIFF_BYTE:
2093                         {
2094                                 uint8* ma;
2095                                 int64* mb;
2096                                 uint32 n;
2097                                 ma=(uint8*)origdata;
2098                                 mb=data;
2099                                 for (n=0; n<count; n++)
2100                                         *mb++=(int64)(*ma++);
2101                         }
2102                         break;
2103                 case TIFF_SBYTE:
2104                         {
2105                                 int8* ma;
2106                                 int64* mb;
2107                                 uint32 n;
2108                                 ma=(int8*)origdata;
2109                                 mb=data;
2110                                 for (n=0; n<count; n++)
2111                                         *mb++=(int64)(*ma++);
2112                         }
2113                         break;
2114                 case TIFF_SHORT:
2115                         {
2116                                 uint16* ma;
2117                                 int64* mb;
2118                                 uint32 n;
2119                                 ma=(uint16*)origdata;
2120                                 mb=data;
2121                                 for (n=0; n<count; n++)
2122                                 {
2123                                         if (tif->tif_flags&TIFF_SWAB)
2124                                                 TIFFSwabShort(ma);
2125                                         *mb++=(int64)(*ma++);
2126                                 }
2127                         }
2128                         break;
2129                 case TIFF_SSHORT:
2130                         {
2131                                 int16* ma;
2132                                 int64* mb;
2133                                 uint32 n;
2134                                 ma=(int16*)origdata;
2135                                 mb=data;
2136                                 for (n=0; n<count; n++)
2137                                 {
2138                                         if (tif->tif_flags&TIFF_SWAB)
2139                                                 TIFFSwabShort((uint16*)ma);
2140                                         *mb++=(int64)(*ma++);
2141                                 }
2142                         }
2143                         break;
2144                 case TIFF_LONG:
2145                         {
2146                                 uint32* ma;
2147                                 int64* mb;
2148                                 uint32 n;
2149                                 ma=(uint32*)origdata;
2150                                 mb=data;
2151                                 for (n=0; n<count; n++)
2152                                 {
2153                                         if (tif->tif_flags&TIFF_SWAB)
2154                                                 TIFFSwabLong(ma);
2155                                         *mb++=(int64)(*ma++);
2156                                 }
2157                         }
2158                         break;
2159                 case TIFF_SLONG:
2160                         {
2161                                 int32* ma;
2162                                 int64* mb;
2163                                 uint32 n;
2164                                 ma=(int32*)origdata;
2165                                 mb=data;
2166                                 for (n=0; n<count; n++)
2167                                 {
2168                                         if (tif->tif_flags&TIFF_SWAB)
2169                                                 TIFFSwabLong((uint32*)ma);
2170                                         *mb++=(int64)(*ma++);
2171                                 }
2172                         }
2173                         break;
2174         }
2175         _TIFFfree(origdata);
2176         if (err!=TIFFReadDirEntryErrOk)
2177         {
2178                 _TIFFfree(data);
2179                 return(err);
2180         }
2181         *value=data;
2182         return(TIFFReadDirEntryErrOk);
2183 }
2184
2185 static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEntry* direntry, float** value)
2186 {
2187         enum TIFFReadDirEntryErr err;
2188         uint32 count;
2189         void* origdata;
2190         float* data;
2191         switch (direntry->tdir_type)
2192         {
2193                 case TIFF_BYTE:
2194                 case TIFF_SBYTE:
2195                 case TIFF_SHORT:
2196                 case TIFF_SSHORT:
2197                 case TIFF_LONG:
2198                 case TIFF_SLONG:
2199                 case TIFF_LONG8:
2200                 case TIFF_SLONG8:
2201                 case TIFF_RATIONAL:
2202                 case TIFF_SRATIONAL:
2203                 case TIFF_FLOAT:
2204                 case TIFF_DOUBLE:
2205                         break;
2206                 default:
2207                         return(TIFFReadDirEntryErrType);
2208         }
2209         err=TIFFReadDirEntryArray(tif,direntry,&count,4,&origdata);
2210         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2211         {
2212                 *value=0;
2213                 return(err);
2214         }
2215         switch (direntry->tdir_type)
2216         {
2217                 case TIFF_FLOAT:
2218                         if (tif->tif_flags&TIFF_SWAB)
2219                                 TIFFSwabArrayOfLong((uint32*)origdata,count);  
2220                         TIFFCvtIEEEDoubleToNative(tif,count,(float*)origdata);
2221                         *value=(float*)origdata;
2222                         return(TIFFReadDirEntryErrOk);
2223         }
2224         data=(float*)_TIFFmalloc(count*sizeof(float));
2225         if (data==0)
2226         {
2227                 _TIFFfree(origdata);
2228                 return(TIFFReadDirEntryErrAlloc);
2229         }
2230         switch (direntry->tdir_type)
2231         {
2232                 case TIFF_BYTE:
2233                         {
2234                                 uint8* ma;
2235                                 float* mb;
2236                                 uint32 n;
2237                                 ma=(uint8*)origdata;
2238                                 mb=data;
2239                                 for (n=0; n<count; n++)
2240                                         *mb++=(float)(*ma++);
2241                         }
2242                         break;
2243                 case TIFF_SBYTE:
2244                         {
2245                                 int8* ma;
2246                                 float* mb;
2247                                 uint32 n;
2248                                 ma=(int8*)origdata;
2249                                 mb=data;
2250                                 for (n=0; n<count; n++)
2251                                         *mb++=(float)(*ma++);
2252                         }
2253                         break;
2254                 case TIFF_SHORT:
2255                         {
2256                                 uint16* ma;
2257                                 float* mb;
2258                                 uint32 n;
2259                                 ma=(uint16*)origdata;
2260                                 mb=data;
2261                                 for (n=0; n<count; n++)
2262                                 {
2263                                         if (tif->tif_flags&TIFF_SWAB)
2264                                                 TIFFSwabShort(ma);
2265                                         *mb++=(float)(*ma++);
2266                                 }
2267                         }
2268                         break;
2269                 case TIFF_SSHORT:
2270                         {
2271                                 int16* ma;
2272                                 float* mb;
2273                                 uint32 n;
2274                                 ma=(int16*)origdata;
2275                                 mb=data;
2276                                 for (n=0; n<count; n++)
2277                                 {
2278                                         if (tif->tif_flags&TIFF_SWAB)
2279                                                 TIFFSwabShort((uint16*)ma);
2280                                         *mb++=(float)(*ma++);
2281                                 }
2282                         }
2283                         break;
2284                 case TIFF_LONG:
2285                         {
2286                                 uint32* ma;
2287                                 float* mb;
2288                                 uint32 n;
2289                                 ma=(uint32*)origdata;
2290                                 mb=data;
2291                                 for (n=0; n<count; n++)
2292                                 {
2293                                         if (tif->tif_flags&TIFF_SWAB)
2294                                                 TIFFSwabLong(ma);
2295                                         *mb++=(float)(*ma++);
2296                                 }
2297                         }
2298                         break;
2299                 case TIFF_SLONG:
2300                         {
2301                                 int32* ma;
2302                                 float* mb;
2303                                 uint32 n;
2304                                 ma=(int32*)origdata;
2305                                 mb=data;
2306                                 for (n=0; n<count; n++)
2307                                 {
2308                                         if (tif->tif_flags&TIFF_SWAB)
2309                                                 TIFFSwabLong((uint32*)ma);
2310                                         *mb++=(float)(*ma++);
2311                                 }
2312                         }
2313                         break;
2314                 case TIFF_LONG8:
2315                         {
2316                                 uint64* ma;
2317                                 float* mb;
2318                                 uint32 n;
2319                                 ma=(uint64*)origdata;
2320                                 mb=data;
2321                                 for (n=0; n<count; n++)
2322                                 {
2323                                         if (tif->tif_flags&TIFF_SWAB)
2324                                                 TIFFSwabLong8(ma);
2325 #if defined(__WIN32__) && (_MSC_VER < 1500)
2326                                         /*
2327                                          * XXX: MSVC 6.0 does not support
2328                                          * conversion of 64-bit integers into
2329                                          * floating point values.
2330                                          */
2331                                         *mb++ = _TIFFUInt64ToFloat(*ma++);
2332 #else
2333                                         *mb++ = (float)(*ma++);
2334 #endif
2335                                 }
2336                         }
2337                         break;
2338                 case TIFF_SLONG8:
2339                         {
2340                                 int64* ma;
2341                                 float* mb;
2342                                 uint32 n;
2343                                 ma=(int64*)origdata;
2344                                 mb=data;
2345                                 for (n=0; n<count; n++)
2346                                 {
2347                                         if (tif->tif_flags&TIFF_SWAB)
2348                                                 TIFFSwabLong8((uint64*)ma);
2349                                         *mb++=(float)(*ma++);
2350                                 }
2351                         }
2352                         break;
2353                 case TIFF_RATIONAL:
2354                         {
2355                                 uint32* ma;
2356                                 uint32 maa;
2357                                 uint32 mab;
2358                                 float* mb;
2359                                 uint32 n;
2360                                 ma=(uint32*)origdata;
2361                                 mb=data;
2362                                 for (n=0; n<count; n++)
2363                                 {
2364                                         if (tif->tif_flags&TIFF_SWAB)
2365                                                 TIFFSwabLong(ma);
2366                                         maa=*ma++;
2367                                         if (tif->tif_flags&TIFF_SWAB)
2368                                                 TIFFSwabLong(ma);
2369                                         mab=*ma++;
2370                                         if (mab==0)
2371                                                 *mb++=0.0;
2372                                         else
2373                                                 *mb++=(float)maa/(float)mab;
2374                                 }
2375                         }
2376                         break;
2377                 case TIFF_SRATIONAL:
2378                         {
2379                                 uint32* ma;
2380                                 int32 maa;
2381                                 uint32 mab;
2382                                 float* mb;
2383                                 uint32 n;
2384                                 ma=(uint32*)origdata;
2385                                 mb=data;
2386                                 for (n=0; n<count; n++)
2387                                 {
2388                                         if (tif->tif_flags&TIFF_SWAB)
2389                                                 TIFFSwabLong(ma);
2390                                         maa=*(int32*)ma;
2391                                         ma++;
2392                                         if (tif->tif_flags&TIFF_SWAB)
2393                                                 TIFFSwabLong(ma);
2394                                         mab=*ma++;
2395                                         if (mab==0)
2396                                                 *mb++=0.0;
2397                                         else
2398                                                 *mb++=(float)maa/(float)mab;
2399                                 }
2400                         }
2401                         break;
2402                 case TIFF_DOUBLE:
2403                         {
2404                                 double* ma;
2405                                 float* mb;
2406                                 uint32 n;
2407                                 if (tif->tif_flags&TIFF_SWAB)
2408                                         TIFFSwabArrayOfLong8((uint64*)origdata,count);
2409                                 TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2410                                 ma=(double*)origdata;
2411                                 mb=data;
2412                                 for (n=0; n<count; n++)
2413                                         *mb++=(float)(*ma++);
2414                         }
2415                         break;
2416         }
2417         _TIFFfree(origdata);
2418         if (err!=TIFFReadDirEntryErrOk)
2419         {
2420                 _TIFFfree(data);
2421                 return(err);
2422         }
2423         *value=data;
2424         return(TIFFReadDirEntryErrOk);
2425 }
2426
2427 static enum TIFFReadDirEntryErr
2428 TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
2429 {
2430         enum TIFFReadDirEntryErr err;
2431         uint32 count;
2432         void* origdata;
2433         double* data;
2434         switch (direntry->tdir_type)
2435         {
2436                 case TIFF_BYTE:
2437                 case TIFF_SBYTE:
2438                 case TIFF_SHORT:
2439                 case TIFF_SSHORT:
2440                 case TIFF_LONG:
2441                 case TIFF_SLONG:
2442                 case TIFF_LONG8:
2443                 case TIFF_SLONG8:
2444                 case TIFF_RATIONAL:
2445                 case TIFF_SRATIONAL:
2446                 case TIFF_FLOAT:
2447                 case TIFF_DOUBLE:
2448                         break;
2449                 default:
2450                         return(TIFFReadDirEntryErrType);
2451         }
2452         err=TIFFReadDirEntryArray(tif,direntry,&count,8,&origdata);
2453         if ((err!=TIFFReadDirEntryErrOk)||(origdata==0))
2454         {
2455                 *value=0;
2456                 return(err);
2457         }
2458         switch (direntry->tdir_type)
2459         {
2460                 case TIFF_DOUBLE:
2461                         if (tif->tif_flags&TIFF_SWAB)
2462                                 TIFFSwabArrayOfLong8((uint64*)origdata,count);
2463                         TIFFCvtIEEEDoubleToNative(tif,count,(double*)origdata);
2464                         *value=(double*)origdata;
2465                         return(TIFFReadDirEntryErrOk);
2466         }
2467         data=(double*)_TIFFmalloc(count*sizeof(double));
2468         if (data==0)
2469         {
2470                 _TIFFfree(origdata);
2471                 return(TIFFReadDirEntryErrAlloc);
2472         }
2473         switch (direntry->tdir_type)
2474         {
2475                 case TIFF_BYTE:
2476                         {
2477                                 uint8* ma;
2478                                 double* mb;
2479                                 uint32 n;
2480                                 ma=(uint8*)origdata;
2481                                 mb=data;
2482                                 for (n=0; n<count; n++)
2483                                         *mb++=(double)(*ma++);
2484                         }
2485                         break;
2486                 case TIFF_SBYTE:
2487                         {
2488                                 int8* ma;
2489                                 double* mb;
2490                                 uint32 n;
2491                                 ma=(int8*)origdata;
2492                                 mb=data;
2493                                 for (n=0; n<count; n++)
2494                                         *mb++=(double)(*ma++);
2495                         }
2496                         break;
2497                 case TIFF_SHORT:
2498                         {
2499                                 uint16* ma;
2500                                 double* mb;
2501                                 uint32 n;
2502                                 ma=(uint16*)origdata;
2503                                 mb=data;
2504                                 for (n=0; n<count; n++)
2505                                 {
2506                                         if (tif->tif_flags&TIFF_SWAB)
2507                                                 TIFFSwabShort(ma);
2508                                         *mb++=(double)(*ma++);
2509                                 }
2510                         }
2511                         break;
2512                 case TIFF_SSHORT:
2513                         {
2514                                 int16* ma;
2515                                 double* mb;
2516                                 uint32 n;
2517                                 ma=(int16*)origdata;
2518                                 mb=data;
2519                                 for (n=0; n<count; n++)
2520                                 {
2521                                         if (tif->tif_flags&TIFF_SWAB)
2522                                                 TIFFSwabShort((uint16*)ma);
2523                                         *mb++=(double)(*ma++);
2524                                 }
2525                         }
2526                         break;
2527                 case TIFF_LONG:
2528                         {
2529                                 uint32* ma;
2530                                 double* mb;
2531                                 uint32 n;
2532                                 ma=(uint32*)origdata;
2533                                 mb=data;
2534                                 for (n=0; n<count; n++)
2535                                 {
2536                                         if (tif->tif_flags&TIFF_SWAB)
2537                                                 TIFFSwabLong(ma);
2538                                         *mb++=(double)(*ma++);
2539                                 }
2540                         }
2541                         break;
2542                 case TIFF_SLONG:
2543                         {
2544                                 int32* ma;
2545                                 double* mb;
2546                                 uint32 n;
2547                                 ma=(int32*)origdata;
2548                                 mb=data;
2549                                 for (n=0; n<count; n++)
2550                                 {
2551                                         if (tif->tif_flags&TIFF_SWAB)
2552                                                 TIFFSwabLong((uint32*)ma);
2553                                         *mb++=(double)(*ma++);
2554                                 }
2555                         }
2556                         break;
2557                 case TIFF_LONG8:
2558                         {
2559                                 uint64* ma;
2560                                 double* mb;
2561                                 uint32 n;
2562                                 ma=(uint64*)origdata;
2563                                 mb=data;
2564                                 for (n=0; n<count; n++)
2565                                 {
2566                                         if (tif->tif_flags&TIFF_SWAB)
2567                                                 TIFFSwabLong8(ma);
2568 #if defined(__WIN32__) && (_MSC_VER < 1500)
2569                                         /*
2570                                          * XXX: MSVC 6.0 does not support
2571                                          * conversion of 64-bit integers into
2572                                          * floating point values.
2573                                          */
2574                                         *mb++ = _TIFFUInt64ToDouble(*ma++);
2575 #else
2576                                         *mb++ = (double)(*ma++);
2577 #endif