Coverage Report

Created: 2024-07-27 06:27

/src/libtiff/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
#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
0
#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
0
{
70
0
    fd_as_handle_union_t fdh;
71
0
    const size_t bytes_total = (size_t)size;
72
0
    size_t bytes_read;
73
0
    tmsize_t count = -1;
74
0
    if ((tmsize_t)bytes_total != size)
75
0
    {
76
0
        errno = EINVAL;
77
0
        return (tmsize_t)-1;
78
0
    }
79
0
    fdh.h = fd;
80
0
    for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
81
0
    {
82
0
        char *buf_offset = (char *)buf + bytes_read;
83
0
        size_t io_size = bytes_total - bytes_read;
84
0
        if (io_size > TIFF_IO_MAX)
85
0
            io_size = TIFF_IO_MAX;
86
        /* Below is an obvious false positive of Coverity Scan */
87
        /* coverity[overflow_sink] */
88
0
        count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
89
0
        if (count <= 0)
90
0
            break;
91
0
    }
92
0
    if (count < 0)
93
0
        return (tmsize_t)-1;
94
0
    return (tmsize_t)bytes_read;
95
0
}
96
97
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
98
0
{
99
0
    fd_as_handle_union_t fdh;
100
0
    const size_t bytes_total = (size_t)size;
101
0
    size_t bytes_written;
102
0
    tmsize_t count = -1;
103
0
    if ((tmsize_t)bytes_total != size)
104
0
    {
105
0
        errno = EINVAL;
106
0
        return (tmsize_t)-1;
107
0
    }
108
0
    fdh.h = fd;
109
0
    for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
110
0
    {
111
0
        const char *buf_offset = (char *)buf + bytes_written;
112
0
        size_t io_size = bytes_total - bytes_written;
113
0
        if (io_size > TIFF_IO_MAX)
114
0
            io_size = TIFF_IO_MAX;
115
        /* Below is an obvious false positive of Coverity Scan */
116
        /* coverity[overflow_sink] */
117
0
        count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
118
0
        if (count <= 0)
119
0
            break;
120
0
    }
121
0
    if (count < 0)
122
0
        return (tmsize_t)-1;
123
0
    return (tmsize_t)bytes_written;
124
    /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
125
0
}
126
127
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
128
0
{
129
0
    fd_as_handle_union_t fdh;
130
0
    _TIFF_off_t off_io = (_TIFF_off_t)off;
131
0
    if ((uint64_t)off_io != off)
132
0
    {
133
0
        errno = EINVAL;
134
0
        return (uint64_t)-1; /* this is really gross */
135
0
    }
136
0
    fdh.h = fd;
137
0
    return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
138
0
}
139
140
static int _tiffCloseProc(thandle_t fd)
141
0
{
142
0
    fd_as_handle_union_t fdh;
143
0
    fdh.h = fd;
144
0
    return (close(fdh.fd));
145
0
}
146
147
static uint64_t _tiffSizeProc(thandle_t fd)
148
0
{
149
0
    _TIFF_stat_s sb;
150
0
    fd_as_handle_union_t fdh;
151
0
    fdh.h = fd;
152
0
    if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
153
0
        return (0);
154
0
    else
155
0
        return ((uint64_t)sb.st_size);
156
0
}
157
158
#ifdef HAVE_MMAP
159
#include <sys/mman.h>
160
161
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
162
0
{
163
0
    uint64_t size64 = _tiffSizeProc(fd);
164
0
    tmsize_t sizem = (tmsize_t)size64;
165
0
    if (size64 && (uint64_t)sizem == size64)
166
0
    {
167
0
        fd_as_handle_union_t fdh;
168
0
        fdh.h = fd;
169
0
        *pbase =
170
0
            (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
171
0
        if (*pbase != (void *)-1)
172
0
        {
173
0
            *psize = (tmsize_t)sizem;
174
0
            return (1);
175
0
        }
176
0
    }
177
0
    return (0);
178
0
}
179
180
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
181
0
{
182
0
    (void)fd;
183
0
    (void)munmap(base, (off_t)size);
184
0
}
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
0
{
207
0
    return TIFFFdOpenExt(fd, name, mode, NULL);
208
0
}
209
210
TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
211
                    TIFFOpenOptions *opts)
212
0
{
213
0
    TIFF *tif;
214
215
0
    fd_as_handle_union_t fdh;
216
0
    fdh.fd = fd;
217
0
    tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
218
0
                            _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
219
0
                            _tiffMapProc, _tiffUnmapProc, opts);
220
0
    if (tif)
221
0
        tif->tif_fd = fd;
222
0
    return (tif);
223
0
}
224
225
/*
226
 * Open a TIFF file for read/writing.
227
 */
228
TIFF *TIFFOpen(const char *name, const char *mode)
229
0
{
230
0
    return TIFFOpenExt(name, mode, NULL);
231
0
}
232
233
TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
234
0
{
235
0
    static const char module[] = "TIFFOpen";
236
0
    int m, fd;
237
0
    TIFF *tif;
238
239
0
    m = _TIFFgetMode(opts, NULL, mode, module);
240
0
    if (m == -1)
241
0
        return ((TIFF *)0);
242
243
/* for cygwin and mingw */
244
#ifdef O_BINARY
245
    m |= O_BINARY;
246
#endif
247
248
0
    fd = open(name, m, 0666);
249
0
    if (fd < 0)
250
0
    {
251
0
        if (errno > 0 && strerror(errno) != NULL)
252
0
        {
253
0
            _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
254
0
                            strerror(errno));
255
0
        }
256
0
        else
257
0
        {
258
0
            _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
259
0
        }
260
0
        return ((TIFF *)0);
261
0
    }
262
263
0
    tif = TIFFFdOpenExt((int)fd, name, mode, opts);
264
0
    if (!tif)
265
0
        close(fd);
266
0
    return tif;
267
0
}
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
0
{
331
0
    if (s == 0)
332
0
        return ((void *)NULL);
333
334
0
    return (malloc((size_t)s));
335
0
}
336
337
void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
338
0
{
339
0
    if (nmemb == 0 || siz == 0)
340
0
        return ((void *)NULL);
341
342
0
    return calloc((size_t)nmemb, (size_t)siz);
343
0
}
344
345
0
void _TIFFfree(void *p) { free(p); }
346
347
0
void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
348
349
0
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
0
{
353
0
    memcpy(d, s, (size_t)c);
354
0
}
355
356
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
357
0
{
358
0
    return (memcmp(p1, p2, (size_t)c));
359
0
}
360
361
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
362
0
{
363
0
    if (module != NULL)
364
0
        fprintf(stderr, "%s: ", module);
365
0
    fprintf(stderr, "Warning, ");
366
0
    vfprintf(stderr, fmt, ap);
367
0
    fprintf(stderr, ".\n");
368
0
}
369
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
370
371
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
372
0
{
373
0
    if (module != NULL)
374
0
        fprintf(stderr, "%s: ", module);
375
0
    vfprintf(stderr, fmt, ap);
376
0
    fprintf(stderr, ".\n");
377
0
}
378
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;