Coverage Report

Created: 2024-06-18 06:05

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