1 /* $Id: tif_unix.c,v 1.23 2012-06-01 21:40:59 fwarmerdam Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
28 * TIFF Library UNIX-specific Routines. These are should also work with the
29 * Windows Common RunTime Library.
32 #include "tif_config.h"
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
59 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
61 size_t size_io = (size_t) size;
62 if ((tmsize_t) size_io != size)
67 return ((tmsize_t) read((int) fd, buf, size_io));
71 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
73 size_t size_io = (size_t) size;
74 if ((tmsize_t) size_io != size)
79 return ((tmsize_t) write((int) fd, buf, size_io));
83 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
85 off_t off_io = (off_t) off;
86 if ((uint64) off_io != off)
89 return (uint64) -1; /* this is really gross */
91 return((uint64)lseek((int)fd,off_io,whence));
95 _tiffCloseProc(thandle_t fd)
97 return(close((int)fd));
101 _tiffSizeProc(thandle_t fd)
104 if (fstat((int)fd,&sb)<0)
107 return((uint64)sb.st_size);
111 #include <sys/mman.h>
114 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
116 uint64 size64 = _tiffSizeProc(fd);
117 tmsize_t sizem = (tmsize_t)size64;
118 if ((uint64)sizem==size64) {
120 mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, (int) fd, 0);
121 if (*pbase != (void*) -1) {
122 *psize = (tmsize_t)sizem;
130 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
133 (void) munmap(base, (off_t) size);
135 #else /* !HAVE_MMAP */
137 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
139 (void) fd; (void) pbase; (void) psize;
144 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
146 (void) fd; (void) base; (void) size;
148 #endif /* !HAVE_MMAP */
151 * Open a TIFF file descriptor for read/writing.
154 TIFFFdOpen(int fd, const char* name, const char* mode)
158 tif = TIFFClientOpen(name, mode,
160 _tiffReadProc, _tiffWriteProc,
161 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
162 _tiffMapProc, _tiffUnmapProc);
169 * Open a TIFF file for read/writing.
172 TIFFOpen(const char* name, const char* mode)
174 static const char module[] = "TIFFOpen";
178 m = _TIFFgetMode(mode, module);
182 /* for cygwin and mingw */
187 fd = open(name, m, 0666);
189 if (errno > 0 && strerror(errno) != NULL ) {
190 TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
192 TIFFErrorExt(0, module, "%s: Cannot open", name);
197 tif = TIFFFdOpen((int)fd, name, mode);
206 * Open a TIFF file with a Unicode filename, for read/writing.
209 TIFFOpenW(const wchar_t* name, const char* mode)
211 static const char module[] = "TIFFOpenW";
217 m = _TIFFgetMode(mode, module);
221 /* for cygwin and mingw */
226 fd = _wopen(name, m, 0666);
228 TIFFErrorExt(0, module, "%s: Cannot open", name);
233 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
235 mbname = _TIFFmalloc(mbsize);
237 TIFFErrorExt(0, module,
238 "Can't allocate space for filename conversion buffer");
242 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
246 tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
258 _TIFFmalloc(tmsize_t s)
260 return (malloc((size_t) s));
270 _TIFFrealloc(void* p, tmsize_t s)
272 return (realloc(p, (size_t) s));
276 _TIFFmemset(void* p, int v, tmsize_t c)
278 memset(p, v, (size_t) c);
282 _TIFFmemcpy(void* d, const void* s, tmsize_t c)
284 memcpy(d, s, (size_t) c);
288 _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
290 return (memcmp(p1, p2, (size_t) c));
294 unixWarningHandler(const char* module, const char* fmt, va_list ap)
297 fprintf(stderr, "%s: ", module);
298 fprintf(stderr, "Warning, ");
299 vfprintf(stderr, fmt, ap);
300 fprintf(stderr, ".\n");
302 TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
305 unixErrorHandler(const char* module, const char* fmt, va_list ap)
308 fprintf(stderr, "%s: ", module);
309 vfprintf(stderr, fmt, ap);
310 fprintf(stderr, ".\n");
312 TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
314 /* vim: set ts=8 sts=8 sw=8 noet: */