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