/src/opencv/3rdparty/libtiff/tif_unix.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "tif_config.h" |
31 | | |
32 | | #ifdef HAVE_SYS_TYPES_H |
33 | | #include <sys/types.h> |
34 | | #endif |
35 | | |
36 | | #include <errno.h> |
37 | | |
38 | | #include <stdarg.h> |
39 | | #include <stdlib.h> |
40 | | #include <sys/stat.h> |
41 | | |
42 | | #ifdef HAVE_UNISTD_H |
43 | | #include <unistd.h> |
44 | | #endif |
45 | | |
46 | | #ifdef HAVE_FCNTL_H |
47 | | #include <fcntl.h> |
48 | | #endif |
49 | | |
50 | | #ifdef HAVE_IO_H |
51 | | #include <io.h> |
52 | | #endif |
53 | | |
54 | | #include "tiffiop.h" |
55 | | |
56 | 0 | #define TIFF_IO_MAX 2147483647U |
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 _tiffReadProc(thandle_t fd, void *buf, tmsize_t size) |
65 | 0 | { |
66 | 0 | fd_as_handle_union_t fdh; |
67 | 0 | const size_t bytes_total = (size_t)size; |
68 | 0 | size_t bytes_read; |
69 | 0 | tmsize_t count = -1; |
70 | 0 | if ((tmsize_t)bytes_total != size) |
71 | 0 | { |
72 | 0 | errno = EINVAL; |
73 | 0 | return (tmsize_t)-1; |
74 | 0 | } |
75 | 0 | fdh.h = fd; |
76 | 0 | for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count) |
77 | 0 | { |
78 | 0 | char *buf_offset = (char *)buf + bytes_read; |
79 | 0 | size_t io_size = bytes_total - bytes_read; |
80 | 0 | if (io_size > TIFF_IO_MAX) |
81 | 0 | io_size = TIFF_IO_MAX; |
82 | 0 | count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size); |
83 | 0 | if (count <= 0) |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | if (count < 0) |
87 | 0 | return (tmsize_t)-1; |
88 | 0 | return (tmsize_t)bytes_read; |
89 | 0 | } |
90 | | |
91 | | static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size) |
92 | 0 | { |
93 | 0 | fd_as_handle_union_t fdh; |
94 | 0 | const size_t bytes_total = (size_t)size; |
95 | 0 | size_t bytes_written; |
96 | 0 | tmsize_t count = -1; |
97 | 0 | if ((tmsize_t)bytes_total != size) |
98 | 0 | { |
99 | 0 | errno = EINVAL; |
100 | 0 | return (tmsize_t)-1; |
101 | 0 | } |
102 | 0 | fdh.h = fd; |
103 | 0 | for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count) |
104 | 0 | { |
105 | 0 | const char *buf_offset = (char *)buf + bytes_written; |
106 | 0 | size_t io_size = bytes_total - bytes_written; |
107 | 0 | if (io_size > TIFF_IO_MAX) |
108 | 0 | io_size = TIFF_IO_MAX; |
109 | 0 | count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size); |
110 | 0 | if (count <= 0) |
111 | 0 | break; |
112 | 0 | } |
113 | 0 | if (count < 0) |
114 | 0 | return (tmsize_t)-1; |
115 | 0 | return (tmsize_t)bytes_written; |
116 | | /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */ |
117 | 0 | } |
118 | | |
119 | | static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence) |
120 | 0 | { |
121 | 0 | fd_as_handle_union_t fdh; |
122 | 0 | _TIFF_off_t off_io = (_TIFF_off_t)off; |
123 | 0 | if ((uint64_t)off_io != off) |
124 | 0 | { |
125 | 0 | errno = EINVAL; |
126 | 0 | return (uint64_t)-1; /* this is really gross */ |
127 | 0 | } |
128 | 0 | fdh.h = fd; |
129 | 0 | return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence)); |
130 | 0 | } |
131 | | |
132 | | static int _tiffCloseProc(thandle_t fd) |
133 | 0 | { |
134 | 0 | fd_as_handle_union_t fdh; |
135 | 0 | fdh.h = fd; |
136 | 0 | return (close(fdh.fd)); |
137 | 0 | } |
138 | | |
139 | | static uint64_t _tiffSizeProc(thandle_t fd) |
140 | 0 | { |
141 | 0 | _TIFF_stat_s sb; |
142 | 0 | fd_as_handle_union_t fdh; |
143 | 0 | fdh.h = fd; |
144 | 0 | if (_TIFF_fstat_f(fdh.fd, &sb) < 0) |
145 | 0 | return (0); |
146 | 0 | else |
147 | 0 | return ((uint64_t)sb.st_size); |
148 | 0 | } |
149 | | |
150 | | #ifdef HAVE_MMAP |
151 | | #include <sys/mman.h> |
152 | | |
153 | | static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize) |
154 | 0 | { |
155 | 0 | uint64_t size64 = _tiffSizeProc(fd); |
156 | 0 | tmsize_t sizem = (tmsize_t)size64; |
157 | 0 | if (size64 && (uint64_t)sizem == size64) |
158 | 0 | { |
159 | 0 | fd_as_handle_union_t fdh; |
160 | 0 | fdh.h = fd; |
161 | 0 | *pbase = |
162 | 0 | (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0); |
163 | 0 | if (*pbase != (void *)-1) |
164 | 0 | { |
165 | 0 | *psize = (tmsize_t)sizem; |
166 | 0 | return (1); |
167 | 0 | } |
168 | 0 | } |
169 | 0 | return (0); |
170 | 0 | } |
171 | | |
172 | | static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size) |
173 | 0 | { |
174 | 0 | (void)fd; |
175 | 0 | (void)munmap(base, (off_t)size); |
176 | 0 | } |
177 | | #else /* !HAVE_MMAP */ |
178 | | static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize) |
179 | | { |
180 | | (void)fd; |
181 | | (void)pbase; |
182 | | (void)psize; |
183 | | return (0); |
184 | | } |
185 | | |
186 | | static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size) |
187 | | { |
188 | | (void)fd; |
189 | | (void)base; |
190 | | (void)size; |
191 | | } |
192 | | #endif /* !HAVE_MMAP */ |
193 | | |
194 | | /* |
195 | | * Open a TIFF file descriptor for read/writing. |
196 | | */ |
197 | | TIFF *TIFFFdOpen(int fd, const char *name, const char *mode) |
198 | 0 | { |
199 | 0 | return TIFFFdOpenExt(fd, name, mode, NULL); |
200 | 0 | } |
201 | | |
202 | | TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode, |
203 | | TIFFOpenOptions *opts) |
204 | 0 | { |
205 | 0 | TIFF *tif; |
206 | |
|
207 | 0 | fd_as_handle_union_t fdh; |
208 | 0 | fdh.fd = fd; |
209 | 0 | tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc, |
210 | 0 | _tiffSeekProc, _tiffCloseProc, _tiffSizeProc, |
211 | 0 | _tiffMapProc, _tiffUnmapProc, opts); |
212 | 0 | if (tif) |
213 | 0 | tif->tif_fd = fd; |
214 | 0 | return (tif); |
215 | 0 | } |
216 | | |
217 | | /* |
218 | | * Open a TIFF file for read/writing. |
219 | | */ |
220 | | TIFF *TIFFOpen(const char *name, const char *mode) |
221 | 0 | { |
222 | 0 | return TIFFOpenExt(name, mode, NULL); |
223 | 0 | } |
224 | | |
225 | | TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts) |
226 | 0 | { |
227 | 0 | static const char module[] = "TIFFOpen"; |
228 | 0 | int m, fd; |
229 | 0 | TIFF *tif; |
230 | |
|
231 | 0 | m = _TIFFgetMode(opts, NULL, mode, module); |
232 | 0 | if (m == -1) |
233 | 0 | return ((TIFF *)0); |
234 | | |
235 | | /* for cygwin and mingw */ |
236 | | #ifdef O_BINARY |
237 | | m |= O_BINARY; |
238 | | #endif |
239 | | |
240 | 0 | fd = open(name, m, 0666); |
241 | 0 | if (fd < 0) |
242 | 0 | { |
243 | 0 | if (errno > 0 && strerror(errno) != NULL) |
244 | 0 | { |
245 | 0 | _TIFFErrorEarly(opts, NULL, module, "%s: %s", name, |
246 | 0 | strerror(errno)); |
247 | 0 | } |
248 | 0 | else |
249 | 0 | { |
250 | 0 | _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name); |
251 | 0 | } |
252 | 0 | return ((TIFF *)0); |
253 | 0 | } |
254 | | |
255 | 0 | tif = TIFFFdOpenExt((int)fd, name, mode, opts); |
256 | 0 | if (!tif) |
257 | 0 | close(fd); |
258 | 0 | return tif; |
259 | 0 | } |
260 | | |
261 | | #ifdef __WIN32__ |
262 | | #include <windows.h> |
263 | | /* |
264 | | * Open a TIFF file with a Unicode filename, for read/writing. |
265 | | */ |
266 | | TIFF *TIFFOpenW(const wchar_t *name, const char *mode) |
267 | | { |
268 | | return TIFFOpenWExt(name, mode, NULL); |
269 | | } |
270 | | TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts) |
271 | | { |
272 | | static const char module[] = "TIFFOpenW"; |
273 | | int m, fd; |
274 | | int mbsize; |
275 | | char *mbname; |
276 | | TIFF *tif; |
277 | | |
278 | | m = _TIFFgetMode(opts, NULL, mode, module); |
279 | | if (m == -1) |
280 | | return ((TIFF *)0); |
281 | | |
282 | | /* for cygwin and mingw */ |
283 | | #ifdef O_BINARY |
284 | | m |= O_BINARY; |
285 | | #endif |
286 | | |
287 | | fd = _wopen(name, m, 0666); |
288 | | if (fd < 0) |
289 | | { |
290 | | _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name); |
291 | | return ((TIFF *)0); |
292 | | } |
293 | | |
294 | | mbname = NULL; |
295 | | mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL); |
296 | | if (mbsize > 0) |
297 | | { |
298 | | mbname = _TIFFmalloc(mbsize); |
299 | | if (!mbname) |
300 | | { |
301 | | _TIFFErrorEarly( |
302 | | opts, NULL, module, |
303 | | "Can't allocate space for filename conversion buffer"); |
304 | | return ((TIFF *)0); |
305 | | } |
306 | | |
307 | | WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL); |
308 | | } |
309 | | |
310 | | tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode, |
311 | | opts); |
312 | | |
313 | | _TIFFfree(mbname); |
314 | | |
315 | | if (!tif) |
316 | | close(fd); |
317 | | return tif; |
318 | | } |
319 | | #endif |
320 | | |
321 | | void *_TIFFmalloc(tmsize_t s) |
322 | 7.80k | { |
323 | 7.80k | if (s == 0) |
324 | 0 | return ((void *)NULL); |
325 | | |
326 | 7.80k | return (malloc((size_t)s)); |
327 | 7.80k | } |
328 | | |
329 | | void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz) |
330 | 0 | { |
331 | 0 | if (nmemb == 0 || siz == 0) |
332 | 0 | return ((void *)NULL); |
333 | | |
334 | 0 | return calloc((size_t)nmemb, (size_t)siz); |
335 | 0 | } |
336 | | |
337 | 10.9k | void _TIFFfree(void *p) { free(p); } |
338 | | |
339 | 3.90k | void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); } |
340 | | |
341 | 8.58k | void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); } |
342 | | |
343 | | void _TIFFmemcpy(void *d, const void *s, tmsize_t c) |
344 | 28.8k | { |
345 | 28.8k | memcpy(d, s, (size_t)c); |
346 | 28.8k | } |
347 | | |
348 | | int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c) |
349 | 0 | { |
350 | 0 | return (memcmp(p1, p2, (size_t)c)); |
351 | 0 | } |
352 | | |
353 | | static void unixWarningHandler(const char *module, const char *fmt, va_list ap) |
354 | 0 | { |
355 | 0 | if (module != NULL) |
356 | 0 | fprintf(stderr, "%s: ", module); |
357 | 0 | fprintf(stderr, "Warning, "); |
358 | 0 | vfprintf(stderr, fmt, ap); |
359 | 0 | fprintf(stderr, ".\n"); |
360 | 0 | } |
361 | | TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler; |
362 | | |
363 | | static void unixErrorHandler(const char *module, const char *fmt, va_list ap) |
364 | 0 | { |
365 | 0 | if (module != NULL) |
366 | 0 | fprintf(stderr, "%s: ", module); |
367 | 0 | vfprintf(stderr, fmt, ap); |
368 | 0 | fprintf(stderr, ".\n"); |
369 | 0 | } |
370 | | TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler; |