Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/libtiff-src/tif_unix.c

raw
1 /*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 /*
26 * TIFF Library UNIX-specific Routines. These are should also work with the
27 * Windows Common RunTime Library.
28 */
29
30 #ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
31 #undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
32 #endif
33
34 #include "tif_config.h"
35
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39
40 #include <errno.h>
41
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <sys/stat.h>
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #ifdef HAVE_FCNTL_H
51 #include <fcntl.h>
52 #endif
53
54 #ifdef HAVE_IO_H
55 #include <io.h>
56 #endif
57
58 #include "tiffiop.h"
59
60 #define TIFF_IO_MAX 2147483647U
61
62 typedef union fd_as_handle_union
63 {
64 int fd;
65 thandle_t h;
66 } fd_as_handle_union_t;
67
68 static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
69 {
70 fd_as_handle_union_t fdh;
71 const size_t bytes_total = (size_t)size;
72 size_t bytes_read;
73 tmsize_t count = -1;
74 if ((tmsize_t)bytes_total != size)
75 {
76 errno = EINVAL;
77 return (tmsize_t)-1;
78 }
79 fdh.h = fd;
80 for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
81 {
82 char *buf_offset = (char *)buf + bytes_read;
83 size_t io_size = bytes_total - bytes_read;
84 if (io_size > TIFF_IO_MAX)
85 io_size = TIFF_IO_MAX;
86 /* Below is an obvious false positive of Coverity Scan */
87 /* coverity[overflow_sink] */
88 count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
89 if (count <= 0)
90 break;
91 }
92 if (count < 0)
93 return (tmsize_t)-1;
94 return (tmsize_t)bytes_read;
95 }
96
97 static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
98 {
99 fd_as_handle_union_t fdh;
100 const size_t bytes_total = (size_t)size;
101 size_t bytes_written;
102 tmsize_t count = -1;
103 if ((tmsize_t)bytes_total != size)
104 {
105 errno = EINVAL;
106 return (tmsize_t)-1;
107 }
108 fdh.h = fd;
109 for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
110 {
111 const char *buf_offset = (char *)buf + bytes_written;
112 size_t io_size = bytes_total - bytes_written;
113 if (io_size > TIFF_IO_MAX)
114 io_size = TIFF_IO_MAX;
115 /* Below is an obvious false positive of Coverity Scan */
116 /* coverity[overflow_sink] */
117 count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
118 if (count <= 0)
119 break;
120 }
121 if (count < 0)
122 return (tmsize_t)-1;
123 return (tmsize_t)bytes_written;
124 /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
125 }
126
127 static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
128 {
129 fd_as_handle_union_t fdh;
130 _TIFF_off_t off_io = (_TIFF_off_t)off;
131 if ((uint64_t)off_io != off)
132 {
133 errno = EINVAL;
134 return (uint64_t)-1; /* this is really gross */
135 }
136 fdh.h = fd;
137 return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
138 }
139
140 static int _tiffCloseProc(thandle_t fd)
141 {
142 fd_as_handle_union_t fdh;
143 fdh.h = fd;
144 return (close(fdh.fd));
145 }
146
147 static uint64_t _tiffSizeProc(thandle_t fd)
148 {
149 _TIFF_stat_s sb;
150 fd_as_handle_union_t fdh;
151 fdh.h = fd;
152 if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
153 return (0);
154 else
155 return ((uint64_t)sb.st_size);
156 }
157
158 #ifdef HAVE_MMAP
159 #include <sys/mman.h>
160
161 static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
162 {
163 uint64_t size64 = _tiffSizeProc(fd);
164 tmsize_t sizem = (tmsize_t)size64;
165 if (size64 && (uint64_t)sizem == size64)
166 {
167 fd_as_handle_union_t fdh;
168 fdh.h = fd;
169 *pbase =
170 (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
171 if (*pbase != (void *)-1)
172 {
173 *psize = (tmsize_t)sizem;
174 return (1);
175 }
176 }
177 return (0);
178 }
179
180 static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
181 {
182 (void)fd;
183 (void)munmap(base, (off_t)size);
184 }
185 #else /* !HAVE_MMAP */
186 static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
187 {
188 (void)fd;
189 (void)pbase;
190 (void)psize;
191 return (0);
192 }
193
194 static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
195 {
196 (void)fd;
197 (void)base;
198 (void)size;
199 }
200 #endif /* !HAVE_MMAP */
201
202 /*
203 * Open a TIFF file descriptor for read/writing.
204 */
205 TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
206 {
207 return TIFFFdOpenExt(fd, name, mode, NULL);
208 }
209
210 TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
211 TIFFOpenOptions *opts)
212 {
213 TIFF *tif;
214
215 fd_as_handle_union_t fdh;
216 fdh.fd = fd;
217 tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
218 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
219 _tiffMapProc, _tiffUnmapProc, opts);
220 if (tif)
221 tif->tif_fd = fd;
222 return (tif);
223 }
224
225 /*
226 * Open a TIFF file for read/writing.
227 */
228 TIFF *TIFFOpen(const char *name, const char *mode)
229 {
230 return TIFFOpenExt(name, mode, NULL);
231 }
232
233 TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
234 {
235 static const char module[] = "TIFFOpen";
236 int m, fd;
237 TIFF *tif;
238
239 m = _TIFFgetMode(opts, NULL, mode, module);
240 if (m == -1)
241 return ((TIFF *)0);
242
243 /* for cygwin and mingw */
244 #ifdef O_BINARY
245 m |= O_BINARY;
246 #endif
247
248 fd = open(name, m, 0666);
249 if (fd < 0)
250 {
251 if (errno > 0 && strerror(errno) != NULL)
252 {
253 _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
254 strerror(errno));
255 }
256 else
257 {
258 _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
259 }
260 return ((TIFF *)0);
261 }
262
263 tif = TIFFFdOpenExt((int)fd, name, mode, opts);
264 if (!tif)
265 close(fd);
266 return tif;
267 }
268
269 #ifdef _WIN32
270 #include <windows.h>
271 /*
272 * Open a TIFF file with a Unicode filename, for read/writing.
273 */
274 TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
275 {
276 return TIFFOpenWExt(name, mode, NULL);
277 }
278 TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
279 {
280 static const char module[] = "TIFFOpenW";
281 int m, fd;
282 int mbsize;
283 char *mbname;
284 TIFF *tif;
285
286 m = _TIFFgetMode(opts, NULL, mode, module);
287 if (m == -1)
288 return ((TIFF *)0);
289
290 /* for cygwin and mingw */
291 #ifdef O_BINARY
292 m |= O_BINARY;
293 #endif
294
295 fd = _wopen(name, m, 0666);
296 if (fd < 0)
297 {
298 _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
299 return ((TIFF *)0);
300 }
301
302 mbname = NULL;
303 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
304 if (mbsize > 0)
305 {
306 mbname = _TIFFmalloc(mbsize);
307 if (!mbname)
308 {
309 _TIFFErrorEarly(
310 opts, NULL, module,
311 "Can't allocate space for filename conversion buffer");
312 return ((TIFF *)0);
313 }
314
315 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
316 }
317
318 tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
319 opts);
320
321 _TIFFfree(mbname);
322
323 if (!tif)
324 close(fd);
325 return tif;
326 }
327 #endif
328
329 void *_TIFFmalloc(tmsize_t s)
330 {
331 if (s == 0)
332 return ((void *)NULL);
333
334 return (malloc((size_t)s));
335 }
336
337 void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
338 {
339 if (nmemb == 0 || siz == 0)
340 return ((void *)NULL);
341
342 return calloc((size_t)nmemb, (size_t)siz);
343 }
344
345 void _TIFFfree(void *p) { free(p); }
346
347 void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
348
349 void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
350
351 void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
352 {
353 memcpy(d, s, (size_t)c);
354 }
355
356 int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
357 {
358 return (memcmp(p1, p2, (size_t)c));
359 }
360
361 static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
362 {
363 if (module != NULL)
364 fprintf(stderr, "%s: ", module);
365 fprintf(stderr, "Warning, ");
366 vfprintf(stderr, fmt, ap);
367 fprintf(stderr, ".\n");
368 }
369 TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
370
371 static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
372 {
373 if (module != NULL)
374 fprintf(stderr, "%s: ", module);
375 vfprintf(stderr, fmt, ap);
376 fprintf(stderr, ".\n");
377 }
378 TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;