LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_unix.c
Go to the documentation of this file.
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
39#include "tif_config.h"
40
41#ifdef HAVE_SYS_TYPES_H
42#include <sys/types.h>
43#endif
44
45#include <errno.h>
46
47#include <stdarg.h>
48#include <stdlib.h>
49#include <sys/stat.h>
50
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
54
55#ifdef HAVE_FCNTL_H
56#include <fcntl.h>
57#endif
58
59#ifdef HAVE_IO_H
60#include <io.h>
61#endif
62
63#include "tiffiop.h"
64
68#define TIFF_IO_MAX 2147483647U
69
83
102static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
103{
105 const size_t bytes_total = (size_t)size;
106 size_t bytes_read;
107 tmsize_t count = -1;
108 if ((tmsize_t)bytes_total != size)
109 {
110 errno = EINVAL;
111 return (tmsize_t)-1;
112 }
113 fdh.h = fd;
114 for (bytes_read = 0; bytes_read < bytes_total; bytes_read += (size_t)count)
115 {
116 char *buf_offset = (char *)buf + bytes_read;
117 size_t io_size = (size_t)(bytes_total - bytes_read);
118 if (io_size > TIFF_IO_MAX)
119 io_size = TIFF_IO_MAX;
120 /* Below is an obvious false positive of Coverity Scan */
121 /* coverity[overflow_sink] */
122 count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
123 if (count <= 0)
124 break;
125 }
126 if (count < 0)
127 return (tmsize_t)-1;
128 /* Silence Coverity Scan warning about unsigned to signed underflow. */
129 /* coverity[return_overflow:SUPPRESS] */
130 return (tmsize_t)bytes_read;
131}
132
151static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
152{
154 const size_t bytes_total = (size_t)size;
155 size_t bytes_written;
156 tmsize_t count = -1;
157 if ((tmsize_t)bytes_total != size)
158 {
159 errno = EINVAL;
160 return (tmsize_t)-1;
161 }
162 fdh.h = fd;
163 for (bytes_written = 0; bytes_written < bytes_total;
164 bytes_written += (size_t)count)
165 {
166 const char *buf_offset = (char *)buf + bytes_written;
167 size_t io_size = (size_t)(bytes_total - bytes_written);
168 if (io_size > TIFF_IO_MAX)
169 io_size = TIFF_IO_MAX;
170 /* Below is an obvious false positive of Coverity Scan */
171 /* coverity[overflow_sink] */
172 count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
173 if (count <= 0)
174 break;
175 }
176 if (count < 0)
177 return (tmsize_t)-1;
178 /* Silence Coverity Scan warning about unsigned to signed underflow. */
179 /* coverity[return_overflow:SUPPRESS] */
180 return (tmsize_t)bytes_written;
181 /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
182}
183
199static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
200{
202 _TIFF_off_t off_io = (_TIFF_off_t)off;
203 if ((uint64_t)off_io != off)
204 {
205 errno = EINVAL;
206 return (uint64_t)-1; /* this is really gross */
207 }
208 fdh.h = fd;
209 return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
210}
211
224{
226 fdh.h = fd;
227 return (close(fdh.fd));
228}
229
241static uint64_t _tiffSizeProc(thandle_t fd)
242{
243 _TIFF_stat_s sb;
245 fdh.h = fd;
246 if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
247 return (0);
248 else
249 return ((uint64_t)sb.st_size);
250}
251
252#ifdef HAVE_MMAP
253#include <sys/mman.h>
254
273static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
274{
275 uint64_t size64 = _tiffSizeProc(fd);
276 tmsize_t sizem = (tmsize_t)size64;
277 if (size64 && (uint64_t)sizem == size64)
278 {
280 fdh.h = fd;
281 *pbase =
282 (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
283 if (*pbase != (void *)-1)
284 {
285 *psize = (toff_t)sizem;
286 return (1);
287 }
288 }
289 return (0);
290}
291
304static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
305{
306 (void)fd;
307 (void)munmap(base, (size_t)size);
308}
309#else /* !HAVE_MMAP */
322static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
323{
324 (void)fd;
325 (void)pbase;
326 (void)psize;
327 return (0);
328}
329
340static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
341{
342 (void)fd;
343 (void)base;
344 (void)size;
345}
346#endif /* !HAVE_MMAP */
347
348/*
349 * Open a TIFF file descriptor for read/writing.
350 */
351TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
352{
353 return TIFFFdOpenExt(fd, name, mode, NULL);
354}
355
356TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
357 TIFFOpenOptions *opts)
358{
359 TIFF *tif;
360
362 fdh.fd = fd;
363 tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
366 if (tif)
367 tif->tif_fd = fd;
368 return (tif);
369}
370
371/*
372 * Open a TIFF file for read/writing.
373 */
374TIFF *TIFFOpen(const char *name, const char *mode)
375{
376 return TIFFOpenExt(name, mode, NULL);
377}
378
379TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
380{
381 static const char module[] = "TIFFOpen";
382 int m, fd;
383 TIFF *tif;
384
385 m = _TIFFgetMode(opts, NULL, mode, module);
386 if (m == -1)
387 return ((TIFF *)0);
388
389/* for cygwin and mingw */
390#ifdef O_BINARY
391 m |= O_BINARY;
392#endif
393
394 fd = open(name, m, 0666);
395 if (fd < 0)
396 {
397 if (errno > 0 && strerror(errno) != NULL)
398 {
399 _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
400 strerror(errno));
401 }
402 else
403 {
404 _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
405 }
406 return ((TIFF *)0);
407 }
408
409 tif = TIFFFdOpenExt((int)fd, name, mode, opts);
410 if (!tif)
411 close(fd);
412 return tif;
413}
414
415#ifdef _WIN32
416#include <windows.h>
417/*
418 * Open a TIFF file with a Unicode filename, for read/writing.
419 */
420TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
421{
422 return TIFFOpenWExt(name, mode, NULL);
423}
424TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
425{
426 static const char module[] = "TIFFOpenW";
427 int m, fd;
428 int mbsize;
429 char *mbname;
430 TIFF *tif;
431
432 m = _TIFFgetMode(opts, NULL, mode, module);
433 if (m == -1)
434 return ((TIFF *)0);
435
436/* for cygwin and mingw */
437#ifdef O_BINARY
438 m |= O_BINARY;
439#endif
440
441 fd = _wopen(name, m, 0666);
442 if (fd < 0)
443 {
444 _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
445 return ((TIFF *)0);
446 }
447
448 mbname = NULL;
449 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
450 if (mbsize > 0)
451 {
452 mbname = _TIFFmalloc(mbsize);
453 if (!mbname)
454 {
456 opts, NULL, module,
457 "Can't allocate space for filename conversion buffer");
458 return ((TIFF *)0);
459 }
460
461 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
462 }
463
464 tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
465 opts);
466
467 _TIFFfree(mbname);
468
469 if (!tif)
470 close(fd);
471 return tif;
472}
473#endif
474
476{
477 if (s == 0)
478 return ((void *)NULL);
479
480 return (malloc((size_t)s));
481}
482
484{
485 if (nmemb == 0 || siz == 0)
486 return ((void *)NULL);
487
488 return calloc((size_t)nmemb, (size_t)siz);
489}
490
491void _TIFFfree(void *p) { free(p); }
492
493void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
494
495void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
496
497void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
498{
499 memcpy(d, s, (size_t)c);
500}
501
502int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
503{
504 return (memcmp(p1, p2, (size_t)c));
505}
506
521static void TIFF_ATTRIBUTE((__format__(__printf__, 2, 0)))
522 unixWarningHandler(const char *module, const char *fmt, va_list ap)
523{
524 if (module != NULL)
525 fprintf(stderr, "%s: ", module);
526 fprintf(stderr, "Warning, ");
527 vfprintf(stderr, fmt, ap);
528 fprintf(stderr, ".\n");
529}
531
545static void TIFF_ATTRIBUTE((__format__(__printf__, 2, 0)))
546 unixErrorHandler(const char *module, const char *fmt, va_list ap)
547{
548 if (module != NULL)
549 fprintf(stderr, "%s: ", module);
550 vfprintf(stderr, fmt, ap);
551 fprintf(stderr, ".\n");
552}
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
Print a default error diagnostic to stderr.
Definition tif_unix.c:546
TIFFErrorHandler _TIFFwarningHandler
Store the global warning callback.
Definition tif_unix.c:530
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
Print a default warning diagnostic to stderr.
Definition tif_unix.c:522
TIFFErrorHandler _TIFFerrorHandler
Store the global error callback.
Definition tif_unix.c:553
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
Read bytes from the descriptor with read().
Definition tif_unix.c:102
union fd_as_handle_union fd_as_handle_union_t
Carry descriptor bits through the opaque client-handle representation.
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
Release a read-only backend mapping.
Definition tif_unix.c:304
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
Write bytes to the descriptor with write().
Definition tif_unix.c:151
void _TIFFErrorEarly(TIFFOpenOptions *opts, thandle_t clientdata, const char *module, const char *fmt,...)
Report an opening error before a complete TIFF handle is available.
Definition tif_error.c:85
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode, const char *module)
Translate the leading open-mode characters into OS access flags.
Definition tif_open.c:60
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the platform resource and return its reported position.
Definition tif_unix.c:199
static int _tiffCloseProc(thandle_t fd)
Release the backend OS resource.
Definition tif_unix.c:223
static uint64_t _tiffSizeProc(thandle_t fd)
Query the backend resource length in bytes.
Definition tif_unix.c:241
#define TIFF_IO_MAX
Limit one descriptor read/write request to 2147483647 bytes.
Definition tif_unix.c:68
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
Create a read-only view of the entire backend resource.
Definition tif_unix.c:273
#define _TIFF_fstat_f(fildes, stat_buff)
Query file status through the platform large-file interface.
Definition tiffiop.h:1446
#define _TIFF_stat_s
Select the platform file-status structure type.
Definition tiffiop.h:1453
#define _TIFF_lseek_f(fildes, offset, whence)
Seek a file descriptor through the platform large-file interface.
Definition tiffiop.h:1414
size_t TIFFIOSize_t
Represent a native POSIX I/O byte count.
Definition tiffiop.h:1398
#define _TIFF_off_t
Select the platform large-file offset type.
Definition tiffiop.h:1458
#define TIFF_ATTRIBUTE(x)
Expand a compiler attribute in a public declaration when supported.
Definition tiffio.h:764
void(* TIFFErrorHandler)(const char *module, const char *fmt, va_list ap)
Handle a library-wide error or warning diagnostic.
Definition tiffio.h:835
TIFF * TIFFClientOpenExt(const char *name, const char *mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc, TIFFOpenOptions *opts)
Initialise a TIFF handle using client-supplied I/O callbacks.
Definition tif_open.c:326
TIFF * TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
Open a TIFF file by pathname.
Definition tif_unix.c:379
TIFF * TIFFFdOpenExt(int fd, const char *name, const char *mode, TIFFOpenOptions *opts)
Attach a TIFF handle to an already-open platform resource.
Definition tif_unix.c:356
TIFF * TIFFOpen(const char *name, const char *mode)
Open a TIFF file by pathname.
Definition tif_unix.c:374
TIFF * TIFFOpenW(const wchar_t *name, const char *mode)
Open a TIFF file using a wide Windows pathname.
Definition tif_win32.c:458
TIFF * TIFFFdOpen(int fd, const char *name, const char *mode)
Attach a TIFF handle to an already-open platform resource.
Definition tif_unix.c:351
TIFF * TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
Open a TIFF file using a wide Windows pathname.
Definition tif_win32.c:497
void _TIFFfree(void *p)
Release a block allocated by the public allocation family.
Definition tif_unix.c:491
void * _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array through the public allocator.
Definition tif_unix.c:483
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_unix.c:495
void * _TIFFmalloc(tmsize_t s)
Allocate an uninitialised block through the public allocator.
Definition tif_unix.c:475
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
Compare two byte ranges lexicographically.
Definition tif_unix.c:502
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
void * _TIFFrealloc(void *p, tmsize_t s)
Resize a public-allocator block using platform realloc semantics.
Definition tif_unix.c:493
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
void * thandle_t
Carry an opaque client I/O context through library callbacks.
Definition tiffio.h:161
Hold allocation limits and diagnostics used when opening a TIFF handle.
Definition tiffiop.h:791
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
int tif_fd
File-descriptor metadata; client I/O still uses tif_clientdata.
Definition tiffiop.h:398
Define private build features, platform probes, and formatting helpers.
Declare private library state, callbacks, and implementation helpers.
Carry descriptor bits through the opaque client-handle representation.
Definition tif_unix.c:77
thandle_t h
Opaque callback-context view of the stored bits.
Definition tif_unix.c:81
int fd
Descriptor view of the stored bits.
Definition tif_unix.c:79