1/* $Id: tif_unix.c 276 2010-06-30 12:18:30Z nijtmans $ */ 2 3/* 4 * Copyright (c) 1988-1997 Sam Leffler 5 * Copyright (c) 1991-1997 Silicon Graphics, Inc. 6 * 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. 14 * 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. 18 * 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 24 * OF THIS SOFTWARE. 25 */ 26 27/* 28 * TIFF Library UNIX-specific Routines. These are should also work with the 29 * Windows Common RunTime Library. 30 */ 31#include "tif_config.h" 32 33#ifdef HAVE_SYS_TYPES_H 34# include <sys/types.h> 35#endif 36 37#include <stdarg.h> 38#include <stdlib.h> 39#include <sys/stat.h> 40 41#ifdef HAVE_UNISTD_H 42# include <unistd.h> 43#endif 44 45#ifdef HAVE_FCNTL_H 46# include <fcntl.h> 47#endif 48 49#ifdef HAVE_IO_H 50# include <io.h> 51#endif 52 53#include "tiffiop.h" 54 55static tsize_t 56_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size) 57{ 58 return ((tsize_t) read((int) fd, buf, (size_t) size)); 59} 60 61static tsize_t 62_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size) 63{ 64 return ((tsize_t) write((int) fd, buf, (size_t) size)); 65} 66 67static toff_t 68_tiffSeekProc(thandle_t fd, toff_t off, int whence) 69{ 70 return ((toff_t) lseek((int) fd, (off_t) off, whence)); 71} 72 73static int 74_tiffCloseProc(thandle_t fd) 75{ 76 return (close((int) fd)); 77} 78 79 80static toff_t 81_tiffSizeProc(thandle_t fd) 82{ 83#ifdef _AM29K 84 long fsize; 85 return ((fsize = lseek((int) fd, 0, SEEK_END)) < 0 ? 0 : fsize); 86#else 87 struct stat sb; 88 return (toff_t) (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size); 89#endif 90} 91 92#ifdef HAVE_MMAP 93#include <sys/mman.h> 94 95static int 96_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize) 97{ 98 toff_t size = _tiffSizeProc(fd); 99 if (size != (toff_t) -1) { 100 *pbase = (tdata_t) 101 mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0); 102 if (*pbase != (tdata_t) -1) { 103 *psize = size; 104 return (1); 105 } 106 } 107 return (0); 108} 109 110static void 111_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size) 112{ 113 (void) fd; 114 (void) munmap(base, (off_t) size); 115} 116#else /* !HAVE_MMAP */ 117static int 118_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize) 119{ 120 (void) fd; (void) pbase; (void) psize; 121 return (0); 122} 123 124static void 125_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size) 126{ 127 (void) fd; (void) base; (void) size; 128} 129#endif /* !HAVE_MMAP */ 130 131/* 132 * Open a TIFF file descriptor for read/writing. 133 */ 134TIFF* 135TIFFFdOpen(int fd, const char* name, const char* mode) 136{ 137 TIFF* tif; 138 139 tif = TIFFClientOpen(name, mode, 140 (thandle_t) fd, 141 _tiffReadProc, _tiffWriteProc, 142 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc, 143 _tiffMapProc, _tiffUnmapProc); 144 if (tif) 145 tif->tif_fd = fd; 146 return (tif); 147} 148 149/* 150 * Open a TIFF file for read/writing. 151 */ 152TIFF* 153TIFFOpen(const char* name, const char* mode) 154{ 155 static const char module[] = "TIFFOpen"; 156 int m, fd; 157 TIFF* tif; 158 159 m = _TIFFgetMode(mode, module); 160 if (m == -1) 161 return ((TIFF*)0); 162 163/* for cygwin and mingw */ 164#ifdef O_BINARY 165 m |= O_BINARY; 166#endif 167 168#ifdef _AM29K 169 fd = open(name, m); 170#else 171 fd = open(name, m, 0666); 172#endif 173 if (fd < 0) { 174 TIFFErrorExt(0, module, "%s: Cannot open", name); 175 return ((TIFF *)0); 176 } 177 178 tif = TIFFFdOpen((int)fd, name, mode); 179 if(!tif) 180 close(fd); 181 return tif; 182} 183 184#ifdef __WIN32__ 185#include <windows.h> 186/* 187 * Open a TIFF file with a Unicode filename, for read/writing. 188 */ 189TIFF* 190TIFFOpenW(const wchar_t* name, const char* mode) 191{ 192 static const char module[] = "TIFFOpenW"; 193 int m, fd; 194 int mbsize; 195 char *mbname; 196 TIFF* tif; 197 198 m = _TIFFgetMode(mode, module); 199 if (m == -1) 200 return ((TIFF*)0); 201 202/* for cygwin and mingw */ 203#ifdef O_BINARY 204 m |= O_BINARY; 205#endif 206 207 fd = _wopen(name, m, 0666); 208 if (fd < 0) { 209 TIFFErrorExt(0, module, "%s: Cannot open", name); 210 return ((TIFF *)0); 211 } 212 213 mbname = NULL; 214 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL); 215 if (mbsize > 0) { 216 mbname = _TIFFmalloc(mbsize); 217 if (!mbname) { 218 TIFFErrorExt(0, module, 219 "Can't allocate space for filename conversion buffer"); 220 return ((TIFF*)0); 221 } 222 223 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, 224 NULL, NULL); 225 } 226 227 tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>", 228 mode); 229 230 _TIFFfree(mbname); 231 232 if(!tif) 233 close(fd); 234 return tif; 235} 236#endif 237 238void* 239_TIFFmalloc(tsize_t s) 240{ 241 return (malloc((size_t) s)); 242} 243 244void 245_TIFFfree(tdata_t p) 246{ 247 free(p); 248} 249 250void* 251_TIFFrealloc(tdata_t p, tsize_t s) 252{ 253 return (realloc(p, (size_t) s)); 254} 255 256void 257_TIFFmemset(tdata_t p, int v, tsize_t c) 258{ 259 memset(p, v, (size_t) c); 260} 261 262void 263_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c) 264{ 265 memcpy(d, s, (size_t) c); 266} 267 268int 269_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c) 270{ 271 return (memcmp(p1, p2, (size_t) c)); 272} 273 274static void 275unixWarningHandler(const char* module, const char* fmt, va_list ap) 276{ 277 if (module != NULL) 278 fprintf(stderr, "%s: ", module); 279 fprintf(stderr, "Warning, "); 280 vfprintf(stderr, fmt, ap); 281 fprintf(stderr, ".\n"); 282} 283TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler; 284 285static void 286unixErrorHandler(const char* module, const char* fmt, va_list ap) 287{ 288 if (module != NULL) 289 fprintf(stderr, "%s: ", module); 290 vfprintf(stderr, fmt, ap); 291 fprintf(stderr, ".\n"); 292} 293TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler; 294/* 295 * Local Variables: 296 * mode: c 297 * c-basic-offset: 8 298 * fill-column: 78 299 * End: 300 */ 301