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