1 /* $Id: tif_unix.c,v 1.28 2017-01-11 19:02:49 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>
59 #define TIFF_IO_MAX 2147483647U
62 typedef union fd_as_handle_union
66 } fd_as_handle_union_t;
69 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
71 fd_as_handle_union_t fdh;
72 const size_t bytes_total = (size_t) size;
75 if ((tmsize_t) bytes_total != size)
81 for (bytes_read=0; bytes_read < bytes_total; bytes_read+=count)
83 char *buf_offset = (char *) buf+bytes_read;
84 size_t io_size = bytes_total-bytes_read;
85 if (io_size > TIFF_IO_MAX)
86 io_size = TIFF_IO_MAX;
87 count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
93 return (tmsize_t) bytes_read;
97 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
99 fd_as_handle_union_t fdh;
100 const size_t bytes_total = (size_t) size;
101 size_t bytes_written;
103 if ((tmsize_t) bytes_total != size)
106 return (tmsize_t) -1;
109 for (bytes_written=0; bytes_written < bytes_total; bytes_written+=count)
111 const char *buf_offset = (char *) buf+bytes_written;
112 size_t io_size = bytes_total-bytes_written;
113 if (io_size > TIFF_IO_MAX)
114 io_size = TIFF_IO_MAX;
115 count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
121 return (tmsize_t) bytes_written;
122 /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
126 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
128 fd_as_handle_union_t fdh;
129 _TIFF_off_t off_io = (_TIFF_off_t) off;
130 if ((uint64) off_io != off)
133 return (uint64) -1; /* this is really gross */
136 return((uint64)_TIFF_lseek_f(fdh.fd,off_io,whence));
140 _tiffCloseProc(thandle_t fd)
142 fd_as_handle_union_t fdh;
144 return(close(fdh.fd));
148 _tiffSizeProc(thandle_t fd)
151 fd_as_handle_union_t fdh;
153 if (_TIFF_fstat_f(fdh.fd,&sb)<0)
156 return((uint64)sb.st_size);
160 #include <sys/mman.h>
163 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
165 uint64 size64 = _tiffSizeProc(fd);
166 tmsize_t sizem = (tmsize_t)size64;
167 if ((uint64)sizem==size64) {
168 fd_as_handle_union_t fdh;
171 mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
172 if (*pbase != (void*) -1) {
173 *psize = (tmsize_t)sizem;
181 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
184 (void) munmap(base, (off_t) size);
186 #else /* !HAVE_MMAP */
188 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
190 (void) fd; (void) pbase; (void) psize;
195 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
197 (void) fd; (void) base; (void) size;
199 #endif /* !HAVE_MMAP */
202 * Open a TIFF file descriptor for read/writing.
205 TIFFFdOpen(int fd, const char* name, const char* mode)
209 fd_as_handle_union_t fdh;
211 tif = TIFFClientOpen(name, mode,
213 _tiffReadProc, _tiffWriteProc,
214 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
215 _tiffMapProc, _tiffUnmapProc);
222 * Open a TIFF file for read/writing.
225 TIFFOpen(const char* name, const char* mode)
227 static const char module[] = "TIFFOpen";
231 m = _TIFFgetMode(mode, module);
235 /* for cygwin and mingw */
240 fd = open(name, m, 0666);
242 if (errno > 0 && strerror(errno) != NULL ) {
243 TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
245 TIFFErrorExt(0, module, "%s: Cannot open", name);
250 tif = TIFFFdOpen((int)fd, name, mode);
259 * Open a TIFF file with a Unicode filename, for read/writing.
262 TIFFOpenW(const wchar_t* name, const char* mode)
264 static const char module[] = "TIFFOpenW";
270 m = _TIFFgetMode(mode, module);
274 /* for cygwin and mingw */
279 fd = _wopen(name, m, 0666);
281 TIFFErrorExt(0, module, "%ls: Cannot open", name);
286 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
288 mbname = _TIFFmalloc(mbsize);
290 TIFFErrorExt(0, module,
291 "Can't allocate space for filename conversion buffer");
295 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
299 tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
311 _TIFFmalloc(tmsize_t s)
314 return ((void *) NULL);
316 return (malloc((size_t) s));
319 void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
321 if( nmemb == 0 || siz == 0 )
322 return ((void *) NULL);
324 return calloc((size_t) nmemb, (size_t)siz);
334 _TIFFrealloc(void* p, tmsize_t s)
336 return (realloc(p, (size_t) s));
340 _TIFFmemset(void* p, int v, tmsize_t c)
342 memset(p, v, (size_t) c);
346 _TIFFmemcpy(void* d, const void* s, tmsize_t c)
348 memcpy(d, s, (size_t) c);
352 _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
354 return (memcmp(p1, p2, (size_t) c));
358 unixWarningHandler(const char* module, const char* fmt, va_list ap)
361 fprintf(stderr, "%s: ", module);
362 fprintf(stderr, "Warning, ");
363 vfprintf(stderr, fmt, ap);
364 fprintf(stderr, ".\n");
366 TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
369 unixErrorHandler(const char* module, const char* fmt, va_list ap)
372 fprintf(stderr, "%s: ", module);
373 vfprintf(stderr, fmt, ap);
374 fprintf(stderr, ".\n");
376 TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
378 /* vim: set ts=8 sts=8 sw=8 noet: */