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.
31 #include <tif_config.h>
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
56 _tiffReadProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
58 return ((tsize_t
) read((int) fd
, buf
, (size_t) size
));
62 _tiffWriteProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
64 return ((tsize_t
) write((int) fd
, buf
, (size_t) size
));
68 _tiffSeekProc(thandle_t fd
, toff_t off
, int whence
)
70 return ((toff_t
) lseek((int) fd
, (off_t
) off
, whence
));
74 _tiffCloseProc(thandle_t fd
)
76 return (close((int) fd
));
81 _tiffSizeProc(thandle_t fd
)
85 return ((fsize
= lseek((int) fd
, 0, SEEK_END
)) < 0 ? 0 : fsize
);
88 return (toff_t
) (fstat((int) fd
, &sb
) < 0 ? 0 : sb
.st_size
);
96 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
98 toff_t size
= _tiffSizeProc(fd
);
99 if (size
!= (toff_t
) -1) {
101 mmap(0, size
, PROT_READ
, MAP_SHARED
, (int) fd
, 0);
102 if (*pbase
!= (tdata_t
) -1) {
111 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
114 (void) munmap(base
, (off_t
) size
);
116 #else /* !HAVE_MMAP */
118 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
120 (void) fd
; (void) pbase
; (void) psize
;
125 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
127 (void) fd
; (void) base
; (void) size
;
129 #endif /* !HAVE_MMAP */
132 * Open a TIFF file descriptor for read/writing.
135 TIFFFdOpen(int fd
, const char* name
, const char* mode
)
139 tif
= TIFFClientOpen(name
, mode
,
141 _tiffReadProc
, _tiffWriteProc
,
142 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
143 _tiffMapProc
, _tiffUnmapProc
);
150 * Open a TIFF file for read/writing.
153 TIFFOpen(const char* name
, const char* mode
)
155 static const char module[] = "TIFFOpen";
159 m
= _TIFFgetMode(mode
, module);
163 /* for cygwin and mingw */
171 fd
= open(name
, m
, 0666);
174 TIFFErrorExt(0, module, "%s: Cannot open", name
);
178 tif
= TIFFFdOpen((int)fd
, name
, mode
);
187 * Open a TIFF file with a Unicode filename, for read/writing.
190 TIFFOpenW(const wchar_t* name
, const char* mode
)
192 static const char module[] = "TIFFOpenW";
198 m
= _TIFFgetMode(mode
, module);
202 /* for cygwin and mingw */
207 fd
= _wopen(name
, m
, 0666);
209 TIFFErrorExt(0, module, "%s: Cannot open", name
);
214 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
216 mbname
= _TIFFmalloc(mbsize
);
218 TIFFErrorExt(0, module,
219 "Can't allocate space for filename conversion buffer");
223 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
227 tif
= TIFFFdOpen((int)fd
, (mbname
!= NULL
) ? mbname
: "<unknown>",
239 _TIFFmalloc(tsize_t s
)
241 return (malloc((size_t) s
));
251 _TIFFrealloc(tdata_t p
, tsize_t s
)
253 return (realloc(p
, (size_t) s
));
257 _TIFFmemset(tdata_t p
, int v
, tsize_t c
)
259 memset(p
, v
, (size_t) c
);
263 _TIFFmemcpy(tdata_t d
, const tdata_t s
, tsize_t c
)
265 memcpy(d
, s
, (size_t) c
);
269 _TIFFmemcmp(const tdata_t p1
, const tdata_t p2
, tsize_t c
)
271 return (memcmp(p1
, p2
, (size_t) c
));
275 unixWarningHandler(const char* module, const char* fmt
, va_list ap
)
278 fprintf(stderr
, "%s: ", module);
279 fprintf(stderr
, "Warning, ");
280 vfprintf(stderr
, fmt
, ap
);
281 fprintf(stderr
, ".\n");
283 TIFFErrorHandler _TIFFwarningHandler
= unixWarningHandler
;
286 unixErrorHandler(const char* module, const char* fmt
, va_list ap
)
289 fprintf(stderr
, "%s: ", module);
290 vfprintf(stderr
, fmt
, ap
);
291 fprintf(stderr
, ".\n");
293 TIFFErrorHandler _TIFFerrorHandler
= unixErrorHandler
;