1/* PDFlib GmbH cvsid: $Id: tif_unix.c 14574 2005-10-29 16:27:43Z bonefish $ */
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 UNIX-specific Routines.
29 */
30#include "tiffiop.h"
31#include "pc_config.h"
32#include <stdlib.h>
33
34static tsize_t
35_tiffReadProc(void* fd, tdata_t buf, tsize_t size)
36{
37	return ((tsize_t) fread(buf, 1, (size_t) size, (FILE *)fd));
38}
39
40static tsize_t
41_tiffWriteProc(void* fd, tdata_t buf, tsize_t size)
42{
43	return ((tsize_t) fwrite(buf, 1, (size_t) size, (FILE *)fd));
44}
45
46static toff_t
47_tiffSeekProc(void* fd, toff_t off, int whence)
48{
49        return ((toff_t) fseek((FILE *)fd, (long) off, whence));
50}
51
52static int
53_tiffCloseProc(void* fd)
54{
55        return (fclose((FILE *)fd));
56}
57
58static toff_t
59_tiffSizeProc(void* fd)
60{
61#if defined(MVS) && defined(I370)
62#define PDF_FTELL_BUSIZE        32000
63
64	char buf[PDF_FTELL_BUSIZE];
65	size_t filelen = 0;
66
67        fseek((FILE *)fd, 0, SEEK_SET);
68
69        while (!feof((FILE *)fd))
70                filelen += fread((void *) buf, 1, PDF_FTELL_BUSIZE, (FILE *)fd);
71
72	return filelen;
73#else
74        fseek((FILE *)fd, 0, SEEK_END);
75        return (toff_t) ftell((FILE *)fd);
76#endif
77}
78
79#ifdef HAVE_MMAP
80#include <sys/mman.h>
81
82static int
83_tiffMapProc(void* fd, tdata_t* pbase, toff_t* psize)
84{
85        toff_t size = _tiffSizeProc((FILE *)fd);
86	if (size != (toff_t) -1) {
87		*pbase = (tdata_t)
88                    mmap(0, size, PROT_READ, MAP_SHARED, (FILE *) fd, 0);
89		if (*pbase != (tdata_t) -1) {
90			*psize = size;
91			return (1);
92		}
93	}
94	return (0);
95}
96
97static void
98_tiffUnmapProc(void* fd, tdata_t base, toff_t size)
99{
100	(void) fd;
101	(void) munmap(base, (off_t) size);
102}
103#else /* !HAVE_MMAP */
104static int
105_tiffMapProc(void* fd, tdata_t* pbase, toff_t* psize)
106{
107	(void) fd; (void) pbase; (void) psize;
108	return (0);
109}
110
111static void
112_tiffUnmapProc(void* fd, tdata_t base, toff_t size)
113{
114	(void) fd; (void) base; (void) size;
115}
116#endif /* !HAVE_MMAP */
117
118/*
119 * Open a TIFF file descriptor for read/writing.
120 */
121TIFF*
122TIFFFdOpen(void* fd, const char* name, const char* mode, void* pdflib_opaque,
123        TIFFmallocHandler malloc_h, TIFFreallocHandler realloc_h,
124        TIFFfreeHandler free_h, TIFFErrorHandler error_h,
125        TIFFErrorHandler warn_h)
126{
127        TIFF* tif;
128
129        tif = TIFFClientOpen(name, mode, fd,
130            _tiffReadProc, _tiffWriteProc,
131            _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
132            _tiffMapProc, _tiffUnmapProc, pdflib_opaque,
133            malloc_h, realloc_h, free_h, error_h, warn_h);
134        if (tif)
135                tif->tif_fd = (FILE *)fd;
136        return (tif);
137}
138
139/*
140 * Open a TIFF file for read/writing.
141 */
142TIFF*
143TIFFOpen(const char* name, const char* mode, void* pdflib_opaque,
144        TIFFmallocHandler malloc_h, TIFFreallocHandler realloc_h,
145        TIFFfreeHandler free_h, TIFFErrorHandler error_h,
146        TIFFErrorHandler warn_h)
147{
148        static const char module[] = "TIFFOpen";
149        FILE *fd;
150
151        if ((fd = fopen(name, READBMODE)) == NULL) {
152                TIFFError(module, "%s: Cannot open", name);
153                return ((TIFF *)0);
154        }
155        return (TIFFFdOpen(fd, name, mode, pdflib_opaque,
156                        malloc_h, realloc_h, free_h, error_h, warn_h));
157}
158
159void*
160_TIFFmalloc(TIFF* tif, tsize_t s)
161{
162        if (tif->pdflib_malloc != NULL)
163            return (tif->pdflib_malloc(tif, (size_t) s));
164        else
165            return (malloc((size_t) s));
166}
167
168void
169_TIFFfree(TIFF* tif, tdata_t p)
170{
171        if (tif->pdflib_free != NULL)
172            tif->pdflib_free(tif, p);
173        else
174            free(p);
175}
176
177void*
178_TIFFrealloc(TIFF* tif, tdata_t p, tsize_t s)
179{
180        if (tif->pdflib_realloc != NULL)
181            return (tif->pdflib_realloc(tif, p, (size_t) s));
182        else
183            return (realloc(p, (size_t) s));
184}
185
186void
187_TIFFmemset(tdata_t p, int v, tsize_t c)
188{
189	memset(p, v, (size_t) c);
190}
191
192void
193_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
194{
195	memcpy(d, s, (size_t) c);
196}
197
198int
199_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
200{
201	return (memcmp(p1, p2, (size_t) c));
202}
203
204static void
205unixWarningHandler(const char* module, const char* fmt, va_list ap)
206{
207	if (module != NULL)
208		fprintf(stderr, "%s: ", module);
209	fprintf(stderr, "Warning, ");
210	vfprintf(stderr, fmt, ap);
211	fprintf(stderr, ".\n");
212}
213TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
214
215static void
216unixErrorHandler(const char* module, const char* fmt, va_list ap)
217{
218	if (module != NULL)
219		fprintf(stderr, "%s: ", module);
220	vfprintf(stderr, fmt, ap);
221	fprintf(stderr, ".\n");
222}
223TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
224