• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $Id: tiffiop.h,v 1.87 2015-08-23 17:49:01 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 #ifndef _TIFFIOP_
28 #define	_TIFFIOP_
29 /*
30  * ``Library-private'' definitions.
31  */
32 
33 #include "tiffconf.h"
34 
35 #ifdef HAVE_FCNTL_H
36 # include <fcntl.h>
37 #endif
38 
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42 
43 #ifdef HAVE_STRING_H
44 # include <string.h>
45 #endif
46 
47 #ifdef HAVE_ASSERT_H
48 # include <assert.h>
49 #else
50 # define assert(x)
51 #endif
52 
53 #ifdef HAVE_SEARCH_H
54 # include <search.h>
55 #else
56 extern void *lfind(const void *, const void *, size_t *, size_t,
57 		   int (*)(const void *, const void *));
58 #endif
59 
60 #if !defined(HAVE_SNPRINTF) && !defined(HAVE__SNPRINTF)
61 #undef snprintf
62 #define snprintf FXSYS_snprintf
63 #endif
64 
65 #include "tiffio.h"
66 
67 #include "tif_dir.h"
68 
69 #ifndef STRIP_SIZE_DEFAULT
70 # define STRIP_SIZE_DEFAULT 8192
71 #endif
72 
73 #define    streq(a,b)      (strcmp(a,b) == 0)
74 
75 #ifndef TRUE
76 #define	TRUE	1
77 #define	FALSE	0
78 #endif
79 
80 typedef struct client_info {
81     struct client_info *next;
82     void *data;
83     char *name;
84 } TIFFClientInfoLink;
85 
86 /*
87  * Typedefs for ``method pointers'' used internally.
88  * these are depriciated and provided only for backwards compatibility
89  */
90 typedef unsigned char tidataval_t;    /* internal image data value type */
91 typedef tidataval_t* tidata_t;        /* reference to internal image data */
92 
93 typedef void (*TIFFVoidMethod)(TIFF*);
94 typedef int (*TIFFBoolMethod)(TIFF*);
95 typedef int (*TIFFPreMethod)(TIFF*, uint16);
96 typedef int (*TIFFCodeMethod)(TIFF* tif, uint8* buf, tmsize_t size, uint16 sample);
97 typedef int (*TIFFSeekMethod)(TIFF*, uint32);
98 typedef void (*TIFFPostMethod)(TIFF* tif, uint8* buf, tmsize_t size);
99 typedef uint32 (*TIFFStripMethod)(TIFF*, uint32);
100 typedef void (*TIFFTileMethod)(TIFF*, uint32*, uint32*);
101 
102 struct tiff {
103 	char*                tif_name;         /* name of open file */
104 	int                  tif_fd;           /* open file descriptor */
105 	int                  tif_mode;         /* open mode (O_*) */
106 	uint32               tif_flags;
107 	#define TIFF_FILLORDER   0x00003 /* natural bit fill order for machine */
108 	#define TIFF_DIRTYHEADER 0x00004 /* header must be written on close */
109 	#define TIFF_DIRTYDIRECT 0x00008 /* current directory must be written */
110 	#define TIFF_BUFFERSETUP 0x00010 /* data buffers setup */
111 	#define TIFF_CODERSETUP  0x00020 /* encoder/decoder setup done */
112 	#define TIFF_BEENWRITING 0x00040 /* written 1+ scanlines to file */
113 	#define TIFF_SWAB        0x00080 /* byte swap file information */
114 	#define TIFF_NOBITREV    0x00100 /* inhibit bit reversal logic */
115 	#define TIFF_MYBUFFER    0x00200 /* my raw data buffer; free on close */
116 	#define TIFF_ISTILED     0x00400 /* file is tile, not strip- based */
117 	#define TIFF_MAPPED      0x00800 /* file is mapped into memory */
118 	#define TIFF_POSTENCODE  0x01000 /* need call to postencode routine */
119 	#define TIFF_INSUBIFD    0x02000 /* currently writing a subifd */
120 	#define TIFF_UPSAMPLED   0x04000 /* library is doing data up-sampling */
121 	#define TIFF_STRIPCHOP   0x08000 /* enable strip chopping support */
122 	#define TIFF_HEADERONLY  0x10000 /* read header only, do not process the first directory */
123 	#define TIFF_NOREADRAW   0x20000 /* skip reading of raw uncompressed image data */
124 	#define TIFF_INCUSTOMIFD 0x40000 /* currently writing a custom IFD */
125 	#define TIFF_BIGTIFF     0x80000 /* read/write bigtiff */
126         #define TIFF_BUF4WRITE  0x100000 /* rawcc bytes are for writing */
127         #define TIFF_DIRTYSTRIP 0x200000 /* stripoffsets/stripbytecount dirty*/
128         #define TIFF_PERSAMPLE  0x400000 /* get/set per sample tags as arrays */
129         #define TIFF_BUFFERMMAP 0x800000 /* read buffer (tif_rawdata) points into mmap() memory */
130 	uint64               tif_diroff;       /* file offset of current directory */
131 	uint64               tif_nextdiroff;   /* file offset of following directory */
132 	uint64*              tif_dirlist;      /* list of offsets to already seen directories to prevent IFD looping */
133 	uint16               tif_dirlistsize;  /* number of entires in offset list */
134 	uint16               tif_dirnumber;    /* number of already seen directories */
135 	TIFFDirectory        tif_dir;          /* internal rep of current directory */
136 	TIFFDirectory        tif_customdir;    /* custom IFDs are separated from the main ones */
137 	union {
138 		TIFFHeaderCommon common;
139 		TIFFHeaderClassic classic;
140 		TIFFHeaderBig big;
141 	} tif_header;
142 	uint16               tif_header_size;  /* file's header block and its length */
143 	uint32               tif_row;          /* current scanline */
144 	uint16               tif_curdir;       /* current directory (index) */
145 	uint32               tif_curstrip;     /* current strip for read/write */
146 	uint64               tif_curoff;       /* current offset for read/write */
147 	uint64               tif_dataoff;      /* current offset for writing dir */
148 	/* SubIFD support */
149 	uint16               tif_nsubifd;      /* remaining subifds to write */
150 	uint64               tif_subifdoff;    /* offset for patching SubIFD link */
151 	/* tiling support */
152 	uint32               tif_col;          /* current column (offset by row too) */
153 	uint32               tif_curtile;      /* current tile for read/write */
154 	tmsize_t             tif_tilesize;     /* # of bytes in a tile */
155 	/* compression scheme hooks */
156 	int                  tif_decodestatus;
157 	TIFFBoolMethod       tif_fixuptags;    /* called in TIFFReadDirectory */
158 	TIFFBoolMethod       tif_setupdecode;  /* called once before predecode */
159 	TIFFPreMethod        tif_predecode;    /* pre- row/strip/tile decoding */
160 	TIFFBoolMethod       tif_setupencode;  /* called once before preencode */
161 	int                  tif_encodestatus;
162 	TIFFPreMethod        tif_preencode;    /* pre- row/strip/tile encoding */
163 	TIFFBoolMethod       tif_postencode;   /* post- row/strip/tile encoding */
164 	TIFFCodeMethod       tif_decoderow;    /* scanline decoding routine */
165 	TIFFCodeMethod       tif_encoderow;    /* scanline encoding routine */
166 	TIFFCodeMethod       tif_decodestrip;  /* strip decoding routine */
167 	TIFFCodeMethod       tif_encodestrip;  /* strip encoding routine */
168 	TIFFCodeMethod       tif_decodetile;   /* tile decoding routine */
169 	TIFFCodeMethod       tif_encodetile;   /* tile encoding routine */
170 	TIFFVoidMethod       tif_close;        /* cleanup-on-close routine */
171 	TIFFSeekMethod       tif_seek;         /* position within a strip routine */
172 	TIFFVoidMethod       tif_cleanup;      /* cleanup state routine */
173 	TIFFStripMethod      tif_defstripsize; /* calculate/constrain strip size */
174 	TIFFTileMethod       tif_deftilesize;  /* calculate/constrain tile size */
175 	uint8*               tif_data;         /* compression scheme private data */
176 	/* input/output buffering */
177 	tmsize_t             tif_scanlinesize; /* # of bytes in a scanline */
178 	tmsize_t             tif_scanlineskew; /* scanline skew for reading strips */
179 	uint8*               tif_rawdata;      /* raw data buffer */
180 	tmsize_t             tif_rawdatasize;  /* # of bytes in raw data buffer */
181         tmsize_t             tif_rawdataoff;   /* rawdata offset within strip */
182         tmsize_t             tif_rawdataloaded;/* amount of data in rawdata */
183 	uint8*               tif_rawcp;        /* current spot in raw buffer */
184 	tmsize_t             tif_rawcc;        /* bytes unread from raw buffer */
185 	/* memory-mapped file support */
186 	uint8*               tif_base;         /* base of mapped file */
187 	tmsize_t             tif_size;         /* size of mapped file region (bytes, thus tmsize_t) */
188 	TIFFMapFileProc      tif_mapproc;      /* map file method */
189 	TIFFUnmapFileProc    tif_unmapproc;    /* unmap file method */
190 	/* input/output callback methods */
191 	thandle_t            tif_clientdata;   /* callback parameter */
192 	TIFFReadWriteProc    tif_readproc;     /* read method */
193 	TIFFReadWriteProc    tif_writeproc;    /* write method */
194 	TIFFSeekProc         tif_seekproc;     /* lseek method */
195 	TIFFCloseProc        tif_closeproc;    /* close method */
196 	TIFFSizeProc         tif_sizeproc;     /* filesize method */
197 	/* post-decoding support */
198 	TIFFPostMethod       tif_postdecode;   /* post decoding routine */
199 	/* tag support */
200 	TIFFField**          tif_fields;       /* sorted table of registered tags */
201 	size_t               tif_nfields;      /* # entries in registered tag table */
202 	const TIFFField*     tif_foundfield;   /* cached pointer to already found tag */
203 	TIFFTagMethods       tif_tagmethods;   /* tag get/set/print routines */
204 	TIFFClientInfoLink*  tif_clientinfo;   /* extra client information. */
205 	/* Backward compatibility stuff. We need these two fields for
206 	 * setting up an old tag extension scheme. */
207 	TIFFFieldArray*      tif_fieldscompat;
208 	size_t               tif_nfieldscompat;
209 };
210 
211 #define isPseudoTag(t) (t > 0xffff)            /* is tag value normal or pseudo */
212 
213 #define isTiled(tif) (((tif)->tif_flags & TIFF_ISTILED) != 0)
214 #define isMapped(tif) (((tif)->tif_flags & TIFF_MAPPED) != 0)
215 #define isFillOrder(tif, o) (((tif)->tif_flags & (o)) != 0)
216 #define isUpSampled(tif) (((tif)->tif_flags & TIFF_UPSAMPLED) != 0)
217 #define TIFFReadFile(tif, buf, size) \
218 	((*(tif)->tif_readproc)((tif)->tif_clientdata,(buf),(size)))
219 #define TIFFWriteFile(tif, buf, size) \
220 	((*(tif)->tif_writeproc)((tif)->tif_clientdata,(buf),(size)))
221 #define TIFFSeekFile(tif, off, whence) \
222 	((*(tif)->tif_seekproc)((tif)->tif_clientdata,(off),(whence)))
223 #define TIFFCloseFile(tif) \
224 	((*(tif)->tif_closeproc)((tif)->tif_clientdata))
225 #define TIFFGetFileSize(tif) \
226 	((*(tif)->tif_sizeproc)((tif)->tif_clientdata))
227 #define TIFFMapFileContents(tif, paddr, psize) \
228 	((*(tif)->tif_mapproc)((tif)->tif_clientdata,(paddr),(psize)))
229 #define TIFFUnmapFileContents(tif, addr, size) \
230 	((*(tif)->tif_unmapproc)((tif)->tif_clientdata,(addr),(size)))
231 
232 /*
233  * Default Read/Seek/Write definitions.
234  */
235 #ifndef ReadOK
236 #define ReadOK(tif, buf, size) \
237 	(TIFFReadFile((tif),(buf),(size))==(size))
238 #endif
239 #ifndef SeekOK
240 #define SeekOK(tif, off) \
241 	(TIFFSeekFile((tif),(off),SEEK_SET)==(off))
242 #endif
243 #ifndef WriteOK
244 #define WriteOK(tif, buf, size) \
245 	(TIFFWriteFile((tif),(buf),(size))==(size))
246 #endif
247 
248 /* NB: the uint32 casts are to silence certain ANSI-C compilers */
249 #define TIFFhowmany_32(x, y) (((uint32)x < (0xffffffff - (uint32)(y-1))) ? \
250 			   ((((uint32)(x))+(((uint32)(y))-1))/((uint32)(y))) : \
251 			   0U)
252 #define TIFFhowmany8_32(x) (((x)&0x07)?((uint32)(x)>>3)+1:(uint32)(x)>>3)
253 #define TIFFroundup_32(x, y) (TIFFhowmany_32(x,y)*(y))
254 #define TIFFhowmany_64(x, y) ((((uint64)(x))+(((uint64)(y))-1))/((uint64)(y)))
255 #define TIFFhowmany8_64(x) (((x)&0x07)?((uint64)(x)>>3)+1:(uint64)(x)>>3)
256 #define TIFFroundup_64(x, y) (TIFFhowmany_64(x,y)*(y))
257 
258 /* Safe multiply which returns zero if there is an integer overflow */
259 #define TIFFSafeMultiply(t,v,m) ((((t)(m) != (t)0) && (((t)(((v)*(m))/(m))) == (t)(v))) ? (t)((v)*(m)) : (t)0)
260 
261 #define TIFFmax(A,B) ((A)>(B)?(A):(B))
262 #define TIFFmin(A,B) ((A)<(B)?(A):(B))
263 
264 #define TIFFArrayCount(a) (sizeof (a) / sizeof ((a)[0]))
265 
266 /*
267   Support for large files.
268 
269   Windows read/write APIs support only 'unsigned int' rather than 'size_t'.
270   Windows off_t is only 32-bit, even in 64-bit builds.
271 */
272 #if defined(HAVE_FSEEKO)
273 /*
274   Use fseeko() and ftello() if they are available since they use
275   'off_t' rather than 'long'.  It is wrong to use fseeko() and
276   ftello() only on systems with special LFS support since some systems
277   (e.g. FreeBSD) support a 64-bit off_t by default.
278 
279   For MinGW, __MSVCRT_VERSION__ must be at least 0x800 to expose these
280   interfaces. The MinGW compiler must support the requested version.  MinGW
281   does not distribute the CRT (it is supplied by Microsoft) so the correct CRT
282   must be available on the target computer in order for the program to run.
283 */
284 #if defined(HAVE_FSEEKO)
285 #  define fseek(stream,offset,whence)  fseeko(stream,offset,whence)
286 #  define ftell(stream,offset,whence)  ftello(stream,offset,whence)
287 #endif
288 #endif
289 #if defined(__WIN32__) && \
290         !(defined(_MSC_VER) && _MSC_VER < 1400) && \
291         !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
292 typedef unsigned int TIFFIOSize_t;
293 #define _TIFF_lseek_f(fildes,offset,whence)  _lseeki64(fildes,/* __int64 */ offset,whence)
294 /* #define _TIFF_tell_f(fildes) /\* __int64 *\/ _telli64(fildes) */
295 #define _TIFF_fseek_f(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence)
296 #define _TIFF_fstat_f(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff)
297 /* #define _TIFF_ftell_f(stream) /\* __int64 *\/ _ftelli64(stream) */
298 /* #define _TIFF_stat_f(path,stat_buff) _stati64(path,/\* struct _stati64 *\/ stat_buff) */
299 #define _TIFF_stat_s struct _stati64
300 #define _TIFF_off_t __int64
301 #else
302 typedef size_t TIFFIOSize_t;
303 #define _TIFF_lseek_f(fildes,offset,whence) lseek(fildes,offset,whence)
304 /* #define _TIFF_tell_f(fildes) (_TIFF_lseek_f(fildes,0,SEEK_CUR)) */
305 #define _TIFF_fseek_f(stream,offset,whence) fseek(stream,offset,whence)
306 #define _TIFF_fstat_f(fildes,stat_buff) fstat(fildes,stat_buff)
307 /* #define _TIFF_ftell_f(stream) ftell(stream) */
308 /* #define _TIFF_stat_f(path,stat_buff) stat(path,stat_buff) */
309 #define _TIFF_stat_s struct stat
310 #define _TIFF_off_t off_t
311 #endif
312 
313 #if defined(__cplusplus)
314 extern "C" {
315 #endif
316 extern int _TIFFgetMode(const char* mode, const char* module);
317 extern int _TIFFNoRowEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
318 extern int _TIFFNoStripEncode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
319 extern int _TIFFNoTileEncode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
320 extern int _TIFFNoRowDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
321 extern int _TIFFNoStripDecode(TIFF* tif, uint8* pp, tmsize_t cc, uint16 s);
322 extern int _TIFFNoTileDecode(TIFF*, uint8* pp, tmsize_t cc, uint16 s);
323 extern void _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc);
324 extern int _TIFFNoPreCode(TIFF* tif, uint16 s);
325 extern int _TIFFNoSeek(TIFF* tif, uint32 off);
326 extern void _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc);
327 extern void _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc);
328 extern void _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc);
329 extern void _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc);
330 extern int TIFFFlushData1(TIFF* tif);
331 extern int TIFFDefaultDirectory(TIFF* tif);
332 extern void _TIFFSetDefaultCompressionState(TIFF* tif);
333 extern int _TIFFRewriteField(TIFF *, uint16, TIFFDataType, tmsize_t, void *);
334 extern int TIFFSetCompressionScheme(TIFF* tif, int scheme);
335 extern int TIFFSetDefaultCompressionState(TIFF* tif);
336 extern uint32 _TIFFDefaultStripSize(TIFF* tif, uint32 s);
337 extern void _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th);
338 extern int _TIFFDataSize(TIFFDataType type);
339 
340 extern void _TIFFsetByteArray(void**, void*, uint32);
341 extern void _TIFFsetString(char**, char*);
342 extern void _TIFFsetShortArray(uint16**, uint16*, uint32);
343 extern void _TIFFsetLongArray(uint32**, uint32*, uint32);
344 extern void _TIFFsetFloatArray(float**, float*, uint32);
345 extern void _TIFFsetDoubleArray(double**, double*, uint32);
346 
347 extern void _TIFFprintAscii(FILE*, const char*);
348 extern void _TIFFprintAsciiTag(FILE*, const char*, const char*);
349 
350 extern TIFFErrorHandler _TIFFwarningHandler;
351 extern TIFFErrorHandler _TIFFerrorHandler;
352 extern TIFFErrorHandlerExt _TIFFwarningHandlerExt;
353 extern TIFFErrorHandlerExt _TIFFerrorHandlerExt;
354 
355 extern uint32 _TIFFMultiply32(TIFF*, uint32, uint32, const char*);
356 extern uint64 _TIFFMultiply64(TIFF*, uint64, uint64, const char*);
357 extern void* _TIFFCheckMalloc(TIFF*, tmsize_t, tmsize_t, const char*);
358 extern void* _TIFFCheckRealloc(TIFF*, void*, tmsize_t, tmsize_t, const char*);
359 
360 extern double _TIFFUInt64ToDouble(uint64);
361 extern float _TIFFUInt64ToFloat(uint64);
362 
363 extern int TIFFInitDumpMode(TIFF*, int);
364 #ifdef PACKBITS_SUPPORT
365 extern int TIFFInitPackBits(TIFF*, int);
366 #endif
367 #ifdef CCITT_SUPPORT
368 extern int TIFFInitCCITTRLE(TIFF*, int), TIFFInitCCITTRLEW(TIFF*, int);
369 extern int TIFFInitCCITTFax3(TIFF*, int), TIFFInitCCITTFax4(TIFF*, int);
370 #endif
371 #ifdef THUNDER_SUPPORT
372 extern int TIFFInitThunderScan(TIFF*, int);
373 #endif
374 #ifdef NEXT_SUPPORT
375 extern int TIFFInitNeXT(TIFF*, int);
376 #endif
377 #ifdef LZW_SUPPORT
378 extern int TIFFInitLZW(TIFF*, int);
379 #endif
380 #ifdef OJPEG_SUPPORT
381 extern int TIFFInitOJPEG(TIFF*, int);
382 #endif
383 #ifdef JPEG_SUPPORT
384 extern int TIFFInitJPEG(TIFF*, int);
385 #endif
386 #ifdef JBIG_SUPPORT
387 extern int TIFFInitJBIG(TIFF*, int);
388 #endif
389 #ifdef ZIP_SUPPORT
390 extern int TIFFInitZIP(TIFF*, int);
391 #endif
392 #ifdef PIXARLOG_SUPPORT
393 extern int TIFFInitPixarLog(TIFF*, int);
394 #endif
395 #ifdef LOGLUV_SUPPORT
396 extern int TIFFInitSGILog(TIFF*, int);
397 #endif
398 #ifdef LZMA_SUPPORT
399 extern int TIFFInitLZMA(TIFF*, int);
400 #endif
401 #ifdef VMS
402 extern const TIFFCodec _TIFFBuiltinCODECS[];
403 #else
404 extern TIFFCodec _TIFFBuiltinCODECS[];
405 #endif
406 
407 #if defined(__cplusplus)
408 }
409 #endif
410 #endif /* _TIFFIOP_ */
411 
412 /* vim: set ts=8 sts=8 sw=8 noet: */
413 /*
414  * Local Variables:
415  * mode: c
416  * c-basic-offset: 8
417  * fill-column: 78
418  * End:
419  */
420