3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * TIFF Library UNIX-specific Routines. These are should also work with the
28 * Windows Common RunTime Library.
31 #include <tif_config.h>
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
58 _tiffReadProc(thandle_t fd
, void* buf
, tmsize_t size
)
60 size_t size_io
= (size_t) size
;
61 if ((tmsize_t
) size_io
!= size
)
66 return ((tmsize_t
) read((int) fd
, buf
, size_io
));
70 _tiffWriteProc(thandle_t fd
, void* buf
, tmsize_t size
)
72 size_t size_io
= (size_t) size
;
73 if ((tmsize_t
) size_io
!= size
)
78 return ((tmsize_t
) write((int) fd
, buf
, size_io
));
82 _tiffSeekProc(thandle_t fd
, uint64 off
, int whence
)
84 off_t off_io
= (off_t
) off
;
85 if ((uint64
) off_io
!= off
)
88 return (uint64
) -1; /* this is really gross */
90 return((uint64
)lseek((int)fd
,off_io
,whence
));
94 _tiffCloseProc(thandle_t fd
)
96 return(close((int)fd
));
100 _tiffSizeProc(thandle_t fd
)
103 if (fstat((int)fd
,&sb
)<0)
106 return((uint64
)sb
.st_size
);
110 #include <sys/mman.h>
113 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
115 uint64 size64
= _tiffSizeProc(fd
);
116 tmsize_t sizem
= (tmsize_t
)size64
;
117 if ((uint64
)sizem
==size64
) {
119 mmap(0, (size_t)sizem
, PROT_READ
, MAP_SHARED
, (int) fd
, 0);
120 if (*pbase
!= (void*) -1) {
121 *psize
= (tmsize_t
)sizem
;
129 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
132 (void) munmap(base
, (off_t
) size
);
134 #else /* !HAVE_MMAP */
136 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
138 (void) fd
; (void) pbase
; (void) psize
;
143 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
145 (void) fd
; (void) base
; (void) size
;
147 #endif /* !HAVE_MMAP */
150 * Open a TIFF file descriptor for read/writing.
153 TIFFFdOpen(int fd
, const char* name
, const char* mode
)
157 tif
= TIFFClientOpen(name
, mode
,
159 _tiffReadProc
, _tiffWriteProc
,
160 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
161 _tiffMapProc
, _tiffUnmapProc
);
168 * Open a TIFF file for read/writing.
171 TIFFOpen(const char* name
, const char* mode
)
173 static const char module[] = "TIFFOpen";
177 m
= _TIFFgetMode(mode
, module);
181 /* for cygwin and mingw */
186 fd
= open(name
, m
, 0666);
188 if (errno
> 0 && strerror(errno
) != NULL
) {
189 TIFFErrorExt(0, module, "%s: %s", name
, strerror(errno
) );
191 TIFFErrorExt(0, module, "%s: Cannot open", name
);
196 tif
= TIFFFdOpen((int)fd
, name
, mode
);
205 * Open a TIFF file with a Unicode filename, for read/writing.
208 TIFFOpenW(const wchar_t* name
, const char* mode
)
210 static const char module[] = "TIFFOpenW";
216 m
= _TIFFgetMode(mode
, module);
220 /* for cygwin and mingw */
225 fd
= _wopen(name
, m
, 0666);
227 TIFFErrorExt(0, module, "%s: Cannot open", name
);
232 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
234 mbname
= _TIFFmalloc(mbsize
);
236 TIFFErrorExt(0, module,
237 "Can't allocate space for filename conversion buffer");
241 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
245 tif
= TIFFFdOpen((int)fd
, (mbname
!= NULL
) ? mbname
: "<unknown>",
257 _TIFFmalloc(tmsize_t s
)
259 return (malloc((size_t) s
));
269 _TIFFrealloc(void* p
, tmsize_t s
)
271 return (realloc(p
, (size_t) s
));
275 _TIFFmemset(void* p
, int v
, tmsize_t c
)
277 memset(p
, v
, (size_t) c
);
281 _TIFFmemcpy(void* d
, const void* s
, tmsize_t c
)
283 memcpy(d
, s
, (size_t) c
);
287 _TIFFmemcmp(const void* p1
, const void* p2
, tmsize_t c
)
289 return (memcmp(p1
, p2
, (size_t) c
));
293 unixWarningHandler(const char* module, const char* fmt
, va_list ap
)
296 fprintf(stderr
, "%s: ", module);
297 fprintf(stderr
, "Warning, ");
298 vfprintf(stderr
, fmt
, ap
);
299 fprintf(stderr
, ".\n");
301 TIFFErrorHandler _TIFFwarningHandler
= unixWarningHandler
;
304 unixErrorHandler(const char* module, const char* fmt
, va_list ap
)
307 fprintf(stderr
, "%s: ", module);
308 vfprintf(stderr
, fmt
, ap
);
309 fprintf(stderr
, ".\n");
311 TIFFErrorHandler _TIFFerrorHandler
= unixErrorHandler
;
313 /* vim: set ts=8 sts=8 sw=8 noet: */