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