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