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