]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_unix.c
Fix for #15520: wxRichTextCtrl: Drawing the selection doesn't respect its container...
[wxWidgets.git] / src / tiff / libtiff / tif_unix.c
CommitLineData
8414a40c
VZ
1
2/*
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
80ed523f 6 * Permission to use, copy, modify, distribute, and sell this software and
8414a40c
VZ
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.
80ed523f
VZ
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 *
8414a40c
VZ
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,
80ed523f
VZ
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
8414a40c
VZ
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 */
80ed523f 30
92ab7bed 31#include <tif_config.h>
8414a40c
VZ
32
33#ifdef HAVE_SYS_TYPES_H
34# include <sys/types.h>
35#endif
36
80ed523f
VZ
37#include <errno.h>
38
8414a40c
VZ
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
80ed523f
VZ
57static tmsize_t
58_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
8414a40c 59{
80ed523f
VZ
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));
8414a40c
VZ
67}
68
80ed523f
VZ
69static tmsize_t
70_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
8414a40c 71{
80ed523f
VZ
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));
8414a40c
VZ
79}
80
80ed523f
VZ
81static uint64
82_tiffSeekProc(thandle_t fd, uint64 off, int whence)
8414a40c 83{
80ed523f
VZ
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));
8414a40c
VZ
91}
92
93static int
94_tiffCloseProc(thandle_t fd)
95{
80ed523f 96 return(close((int)fd));
8414a40c
VZ
97}
98
80ed523f 99static uint64
8414a40c
VZ
100_tiffSizeProc(thandle_t fd)
101{
8414a40c 102 struct stat sb;
80ed523f
VZ
103 if (fstat((int)fd,&sb)<0)
104 return(0);
105 else
106 return((uint64)sb.st_size);
8414a40c
VZ
107}
108
109#ifdef HAVE_MMAP
110#include <sys/mman.h>
111
112static int
80ed523f 113_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
8414a40c 114{
80ed523f
VZ
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;
8414a40c
VZ
122 return (1);
123 }
124 }
125 return (0);
126}
127
128static void
80ed523f 129_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
8414a40c
VZ
130{
131 (void) fd;
132 (void) munmap(base, (off_t) size);
133}
134#else /* !HAVE_MMAP */
135static int
80ed523f 136_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
8414a40c
VZ
137{
138 (void) fd; (void) pbase; (void) psize;
139 return (0);
140}
141
142static void
80ed523f 143_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
8414a40c
VZ
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 */
152TIFF*
153TIFFFdOpen(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 */
170TIFF*
171TIFFOpen(const char* name, const char* mode)
172{
173 static const char module[] = "TIFFOpen";
174 int m, fd;
80ed523f 175 TIFF* tif;
8414a40c
VZ
176
177 m = _TIFFgetMode(mode, module);
178 if (m == -1)
179 return ((TIFF*)0);
180
80ed523f 181/* for cygwin and mingw */
8414a40c 182#ifdef O_BINARY
80ed523f 183 m |= O_BINARY;
8414a40c 184#endif
80ed523f
VZ
185
186 fd = open(name, m, 0666);
8414a40c 187 if (fd < 0) {
80ed523f
VZ
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 }
8414a40c
VZ
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 */
207TIFF*
208TIFFOpenW(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
80ed523f 220/* for cygwin and mingw */
8414a40c 221#ifdef O_BINARY
80ed523f
VZ
222 m |= O_BINARY;
223#endif
224
8414a40c
VZ
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
256void*
80ed523f 257_TIFFmalloc(tmsize_t s)
8414a40c
VZ
258{
259 return (malloc((size_t) s));
260}
261
262void
80ed523f 263_TIFFfree(void* p)
8414a40c
VZ
264{
265 free(p);
266}
267
268void*
80ed523f 269_TIFFrealloc(void* p, tmsize_t s)
8414a40c
VZ
270{
271 return (realloc(p, (size_t) s));
272}
273
274void
80ed523f 275_TIFFmemset(void* p, int v, tmsize_t c)
8414a40c
VZ
276{
277 memset(p, v, (size_t) c);
278}
279
280void
80ed523f 281_TIFFmemcpy(void* d, const void* s, tmsize_t c)
8414a40c
VZ
282{
283 memcpy(d, s, (size_t) c);
284}
285
286int
80ed523f 287_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
8414a40c
VZ
288{
289 return (memcmp(p1, p2, (size_t) c));
290}
291
292static void
293unixWarningHandler(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}
301TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
302
303static void
304unixErrorHandler(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}
311TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
80ed523f
VZ
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 */