30
#include "tif_config.h"
32
#ifdef HAVE_SYS_TYPES_H
56
#define TIFF_IO_MAX 2147483647U
58
typedef union fd_as_handle_union
62
} fd_as_handle_union_t;
64
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
66
fd_as_handle_union_t fdh;
67
const size_t bytes_total = (size_t)size;
70
if ((tmsize_t)bytes_total != size)
76
for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
78
char *buf_offset = (char *)buf + bytes_read;
79
size_t io_size = bytes_total - bytes_read;
80
if (io_size > TIFF_IO_MAX)
81
io_size = TIFF_IO_MAX;
82
count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
88
return (tmsize_t)bytes_read;
91
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
93
fd_as_handle_union_t fdh;
94
const size_t bytes_total = (size_t)size;
97
if ((tmsize_t)bytes_total != size)
103
for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
105
const char *buf_offset = (char *)buf + bytes_written;
106
size_t io_size = bytes_total - bytes_written;
107
if (io_size > TIFF_IO_MAX)
108
io_size = TIFF_IO_MAX;
109
count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
115
return (tmsize_t)bytes_written;
119
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
121
fd_as_handle_union_t fdh;
122
_TIFF_off_t off_io = (_TIFF_off_t)off;
123
if ((uint64_t)off_io != off)
129
return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
132
static int _tiffCloseProc(thandle_t fd)
134
fd_as_handle_union_t fdh;
136
return (close(fdh.fd));
139
static uint64_t _tiffSizeProc(thandle_t fd)
142
fd_as_handle_union_t fdh;
144
if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
147
return ((uint64_t)sb.st_size);
153
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
155
uint64_t size64 = _tiffSizeProc(fd);
156
tmsize_t sizem = (tmsize_t)size64;
157
if (size64 && (uint64_t)sizem == size64)
159
fd_as_handle_union_t fdh;
162
(void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
163
if (*pbase != (void *)-1)
165
*psize = (tmsize_t)sizem;
172
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
175
(void)munmap(base, (off_t)size);
178
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
186
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
197
TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
199
return TIFFFdOpenExt(fd, name, mode, NULL);
202
TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
203
TIFFOpenOptions *opts)
207
fd_as_handle_union_t fdh;
209
tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
210
_tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
211
_tiffMapProc, _tiffUnmapProc, opts);
220
TIFF *TIFFOpen(const char *name, const char *mode)
222
return TIFFOpenExt(name, mode, NULL);
225
TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
227
static const char module[] = "TIFFOpen";
231
m = _TIFFgetMode(opts, NULL, mode, module);
240
fd = open(name, m, 0666);
243
if (errno > 0 && strerror(errno) != NULL)
245
_TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
250
_TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
255
tif = TIFFFdOpenExt((int)fd, name, mode, opts);
266
TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
268
return TIFFOpenWExt(name, mode, NULL);
270
TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
272
static const char module[] = "TIFFOpenW";
278
m = _TIFFgetMode(opts, NULL, mode, module);
287
fd = _wopen(name, m, 0666);
290
_TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
295
mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
298
mbname = _TIFFmalloc(mbsize);
303
"Can't allocate space for filename conversion buffer");
307
WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
310
tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
321
void *_TIFFmalloc(tmsize_t s)
324
return ((void *)NULL);
326
return (malloc((size_t)s));
329
void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
331
if (nmemb == 0 || siz == 0)
332
return ((void *)NULL);
334
return calloc((size_t)nmemb, (size_t)siz);
337
void _TIFFfree(void *p) { free(p); }
339
void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
341
void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
343
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
345
memcpy(d, s, (size_t)c);
348
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
350
return (memcmp(p1, p2, (size_t)c));
353
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
356
fprintf(stderr, "%s: ", module);
357
fprintf(stderr, "Warning, ");
358
vfprintf(stderr, fmt, ap);
359
fprintf(stderr, ".\n");
361
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
363
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
366
fprintf(stderr, "%s: ", module);
367
vfprintf(stderr, fmt, ap);
368
fprintf(stderr, ".\n");
370
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;