1 /* $Id: tif_unix.c,v 1.26 2015-06-16 15:33:17 erouault 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>
58 typedef union fd_as_handle_union
62 } fd_as_handle_union_t;
65 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
67 fd_as_handle_union_t fdh;
68 size_t size_io = (size_t) size;
69 if ((tmsize_t) size_io != size)
75 return ((tmsize_t) read(fdh.fd, buf, size_io));
79 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
81 fd_as_handle_union_t fdh;
82 size_t size_io = (size_t) size;
83 if ((tmsize_t) size_io != size)
89 return ((tmsize_t) write(fdh.fd, buf, size_io));
93 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
95 fd_as_handle_union_t fdh;
96 off_t off_io = (off_t) off;
97 if ((uint64) off_io != off)
100 return (uint64) -1; /* this is really gross */
103 return((uint64)lseek(fdh.fd,off_io,whence));
107 _tiffCloseProc(thandle_t fd)
109 fd_as_handle_union_t fdh;
111 return(close(fdh.fd));
115 _tiffSizeProc(thandle_t fd)
118 fd_as_handle_union_t fdh;
120 if (fstat(fdh.fd,&sb)<0)
123 return((uint64)sb.st_size);
127 #include <sys/mman.h>
130 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
132 uint64 size64 = _tiffSizeProc(fd);
133 tmsize_t sizem = (tmsize_t)size64;
134 if ((uint64)sizem==size64) {
135 fd_as_handle_union_t fdh;
138 mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
139 if (*pbase != (void*) -1) {
140 *psize = (tmsize_t)sizem;
148 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
151 (void) munmap(base, (off_t) size);
153 #else /* !HAVE_MMAP */
155 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
157 (void) fd; (void) pbase; (void) psize;
162 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
164 (void) fd; (void) base; (void) size;
166 #endif /* !HAVE_MMAP */
169 * Open a TIFF file descriptor for read/writing.
172 TIFFFdOpen(int fd, const char* name, const char* mode)
176 fd_as_handle_union_t fdh;
178 tif = TIFFClientOpen(name, mode,
180 _tiffReadProc, _tiffWriteProc,
181 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
182 _tiffMapProc, _tiffUnmapProc);
189 * Open a TIFF file for read/writing.
192 TIFFOpen(const char* name, const char* mode)
194 static const char module[] = "TIFFOpen";
198 m = _TIFFgetMode(mode, module);
202 /* for cygwin and mingw */
207 fd = open(name, m, 0666);
209 if (errno > 0 && strerror(errno) != NULL ) {
210 TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
212 TIFFErrorExt(0, module, "%s: Cannot open", name);
217 tif = TIFFFdOpen((int)fd, name, mode);
226 * Open a TIFF file with a Unicode filename, for read/writing.
229 TIFFOpenW(const wchar_t* name, const char* mode)
231 static const char module[] = "TIFFOpenW";
237 m = _TIFFgetMode(mode, module);
241 /* for cygwin and mingw */
246 fd = _wopen(name, m, 0666);
248 TIFFErrorExt(0, module, "%s: Cannot open", name);
253 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
255 mbname = _TIFFmalloc(mbsize);
257 TIFFErrorExt(0, module,
258 "Can't allocate space for filename conversion buffer");
262 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
266 tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
278 _TIFFmalloc(tmsize_t s)
281 return ((void *) NULL);
283 return (malloc((size_t) s));
293 _TIFFrealloc(void* p, tmsize_t s)
295 return (realloc(p, (size_t) s));
299 _TIFFmemset(void* p, int v, tmsize_t c)
301 memset(p, v, (size_t) c);
305 _TIFFmemcpy(void* d, const void* s, tmsize_t c)
307 memcpy(d, s, (size_t) c);
311 _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
313 return (memcmp(p1, p2, (size_t) c));
317 unixWarningHandler(const char* module, const char* fmt, va_list ap)
320 fprintf(stderr, "%s: ", module);
321 fprintf(stderr, "Warning, ");
322 vfprintf(stderr, fmt, ap);
323 fprintf(stderr, ".\n");
325 TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
328 unixErrorHandler(const char* module, const char* fmt, va_list ap)
331 fprintf(stderr, "%s: ", module);
332 vfprintf(stderr, fmt, ap);
333 fprintf(stderr, ".\n");
335 TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
337 /* vim: set ts=8 sts=8 sw=8 noet: */