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