ReactOS 0.4.16-dev-2613-g9533ad7
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
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#define TIFF_IO_MAX 2147483647U
61
62typedef union fd_as_handle_union
63{
64 int fd;
67
69{
71 const size_t bytes_total = (size_t)size;
72 size_t bytes_read;
73 tmsize_t count = -1;
74 if ((tmsize_t)bytes_total != size)
75 {
76 errno = EINVAL;
77 return (tmsize_t)-1;
78 }
79 fdh.h = fd;
80 for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
81 {
82 char *buf_offset = (char *)buf + bytes_read;
83 size_t io_size = bytes_total - bytes_read;
84 if (io_size > TIFF_IO_MAX)
85 io_size = TIFF_IO_MAX;
86 /* Below is an obvious false positive of Coverity Scan */
87 /* coverity[overflow_sink] */
88 count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
89 if (count <= 0)
90 break;
91 }
92 if (count < 0)
93 return (tmsize_t)-1;
94 /* Silence Coverity Scan warning about unsigned to signed underflow. */
95 /* coverity[return_overflow:SUPPRESS] */
96 return (tmsize_t)bytes_read;
97}
98
100{
102 const size_t bytes_total = (size_t)size;
103 size_t bytes_written;
104 tmsize_t count = -1;
105 if ((tmsize_t)bytes_total != size)
106 {
107 errno = EINVAL;
108 return (tmsize_t)-1;
109 }
110 fdh.h = fd;
111 for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
112 {
113 const char *buf_offset = (char *)buf + bytes_written;
114 size_t io_size = bytes_total - bytes_written;
115 if (io_size > TIFF_IO_MAX)
116 io_size = TIFF_IO_MAX;
117 /* Below is an obvious false positive of Coverity Scan */
118 /* coverity[overflow_sink] */
119 count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
120 if (count <= 0)
121 break;
122 }
123 if (count < 0)
124 return (tmsize_t)-1;
125 /* Silence Coverity Scan warning about unsigned to signed underflow. */
126 /* coverity[return_overflow:SUPPRESS] */
127 return (tmsize_t)bytes_written;
128 /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
129}
130
132{
134 _TIFF_off_t off_io = (_TIFF_off_t)off;
135 if ((uint64_t)off_io != off)
136 {
137 errno = EINVAL;
138 return (uint64_t)-1; /* this is really gross */
139 }
140 fdh.h = fd;
141 return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
142}
143
145{
147 fdh.h = fd;
148 return (close(fdh.fd));
149}
150
152{
155 fdh.h = fd;
156 if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
157 return (0);
158 else
159 return ((uint64_t)sb.st_size);
160}
161
162#ifdef HAVE_MMAP
163#include <sys/mman.h>
164
165static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
166{
167 uint64_t size64 = _tiffSizeProc(fd);
168 tmsize_t sizem = (tmsize_t)size64;
169 if (size64 && (uint64_t)sizem == size64)
170 {
172 fdh.h = fd;
173 *pbase =
174 (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
175 if (*pbase != (void *)-1)
176 {
177 *psize = (tmsize_t)sizem;
178 return (1);
179 }
180 }
181 return (0);
182}
183
184static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
185{
186 (void)fd;
187 (void)munmap(base, (off_t)size);
188}
189#else /* !HAVE_MMAP */
190static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
191{
192 (void)fd;
193 (void)pbase;
194 (void)psize;
195 return (0);
196}
197
199{
200 (void)fd;
201 (void)base;
202 (void)size;
203}
204#endif /* !HAVE_MMAP */
205
206/*
207 * Open a TIFF file descriptor for read/writing.
208 */
209TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
210{
211 return TIFFFdOpenExt(fd, name, mode, NULL);
212}
213
214TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
215 TIFFOpenOptions *opts)
216{
217 TIFF *tif;
218
220 fdh.fd = fd;
224 if (tif)
225 tif->tif_fd = fd;
226 return (tif);
227}
228
229/*
230 * Open a TIFF file for read/writing.
231 */
232TIFF *TIFFOpen(const char *name, const char *mode)
233{
234 return TIFFOpenExt(name, mode, NULL);
235}
236
237TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
238{
239 static const char module[] = "TIFFOpen";
240 int m, fd;
241 TIFF *tif;
242
243 m = _TIFFgetMode(opts, NULL, mode, module);
244 if (m == -1)
245 return ((TIFF *)0);
246
247/* for cygwin and mingw */
248#ifdef O_BINARY
249 m |= O_BINARY;
250#endif
251
252 fd = open(name, m, 0666);
253 if (fd < 0)
254 {
255 if (errno > 0 && strerror(errno) != NULL)
256 {
257 _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
258 strerror(errno));
259 }
260 else
261 {
262 _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
263 }
264 return ((TIFF *)0);
265 }
266
267 tif = TIFFFdOpenExt((int)fd, name, mode, opts);
268 if (!tif)
269 close(fd);
270 return tif;
271}
272
273#ifdef _WIN32
274#include <windows.h>
275/*
276 * Open a TIFF file with a Unicode filename, for read/writing.
277 */
278TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
279{
280 return TIFFOpenWExt(name, mode, NULL);
281}
282TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
283{
284 static const char module[] = "TIFFOpenW";
285 int m, fd;
286 int mbsize;
287 char *mbname;
288 TIFF *tif;
289
290 m = _TIFFgetMode(opts, NULL, mode, module);
291 if (m == -1)
292 return ((TIFF *)0);
293
294/* for cygwin and mingw */
295#ifdef O_BINARY
296 m |= O_BINARY;
297#endif
298
299 fd = _wopen(name, m, 0666);
300 if (fd < 0)
301 {
302 _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
303 return ((TIFF *)0);
304 }
305
306 mbname = NULL;
307 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
308 if (mbsize > 0)
309 {
310 mbname = _TIFFmalloc(mbsize);
311 if (!mbname)
312 {
314 opts, NULL, module,
315 "Can't allocate space for filename conversion buffer");
316 return ((TIFF *)0);
317 }
318
319 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
320 }
321
322 tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
323 opts);
324
325 _TIFFfree(mbname);
326
327 if (!tif)
328 close(fd);
329 return tif;
330}
331#endif
332
334{
335 if (s == 0)
336 return ((void *)NULL);
337
338 return (malloc((size_t)s));
339}
340
342{
343 if (nmemb == 0 || siz == 0)
344 return ((void *)NULL);
345
346 return calloc((size_t)nmemb, (size_t)siz);
347}
348
349void _TIFFfree(void *p) { free(p); }
350
351void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
352
353void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
354
355void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
356{
357 memcpy(d, s, (size_t)c);
358}
359
360int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
361{
362 return (memcmp(p1, p2, (size_t)c));
363}
364
365static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
366{
367 if (module != NULL)
368 fprintf(stderr, "%s: ", module);
369 fprintf(stderr, "Warning, ");
371 fprintf(stderr, ".\n");
372}
374
375static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
376{
377 if (module != NULL)
378 fprintf(stderr, "%s: ", module);
380 fprintf(stderr, ".\n");
381}
#define read
Definition: acwin.h:96
#define close
Definition: acwin.h:98
#define write
Definition: acwin.h:97
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
UINT64 uint64_t
Definition: types.h:77
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
char *CDECL strerror(int err)
Definition: errno.c:273
int WINAPIV _wopen(const wchar_t *path, int flags,...)
Definition: file.c:2674
int WINAPIV fprintf(FILE *file, const char *format,...)
Definition: file.c:5549
int CDECL vfprintf(FILE *file, const char *format, va_list valist)
Definition: file.c:5349
unsigned int size_t
Definition: corecrt.h:203
#define stderr
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
#define EINVAL
Definition: errno.h:44
#define errno
Definition: errno.h:120
#define O_BINARY
Definition: fcntl.h:47
#define open
Definition: io.h:44
char * va_list
Definition: vadefs.h:50
superblock * sb
Definition: btrfs.c:4261
__kernel_off_t off_t
Definition: linux.h:201
const GLdouble * v
Definition: gl.h:2040
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLdouble s
Definition: gl.h:2039
GLsizeiptr size
Definition: glext.h:5919
const GLubyte * c
Definition: glext.h:8905
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLfloat GLfloat p
Definition: glext.h:8902
const GLfloat * m
Definition: glext.h:10848
#define d
Definition: ke_i.h:81
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
_Must_inspect_result_ _Out_ LPSIZE psize
Definition: ntgdi.h:1569
#define calloc
Definition: rosglue.h:14
static int fd
Definition: io.c:51
#define memset(x, y, z)
Definition: compat.h:39
Definition: dsound.c:943
Definition: name.c:39
Definition: tiffiop.h:113
int tif_fd
Definition: tiffiop.h:115
void _TIFFErrorEarly(TIFFOpenOptions *opts, thandle_t clientdata, const char *module, const char *fmt,...)
Definition: tif_error.c:80
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)
Definition: tif_open.c:300
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode, const char *module)
Definition: tif_open.c:55
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
Definition: tif_unix.c:68
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
Definition: tif_unix.c:375
void _TIFFfree(void *p)
Definition: tif_unix.c:349
TIFFErrorHandler _TIFFwarningHandler
Definition: tif_unix.c:373
TIFF * TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
Definition: tif_unix.c:237
TIFF * TIFFFdOpenExt(int fd, const char *name, const char *mode, TIFFOpenOptions *opts)
Definition: tif_unix.c:214
TIFF * TIFFOpen(const char *name, const char *mode)
Definition: tif_unix.c:232
union fd_as_handle_union fd_as_handle_union_t
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
Definition: tif_unix.c:198
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
Definition: tif_unix.c:99
void * _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
Definition: tif_unix.c:341
void _TIFFmemset(void *p, int v, tmsize_t c)
Definition: tif_unix.c:353
void * _TIFFmalloc(tmsize_t s)
Definition: tif_unix.c:333
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
Definition: tif_unix.c:131
TIFF * TIFFFdOpen(int fd, const char *name, const char *mode)
Definition: tif_unix.c:209
static int _tiffCloseProc(thandle_t fd)
Definition: tif_unix.c:144
static uint64_t _tiffSizeProc(thandle_t fd)
Definition: tif_unix.c:151
#define TIFF_IO_MAX
Definition: tif_unix.c:60
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
Definition: tif_unix.c:360
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
Definition: tif_unix.c:365
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
Definition: tif_unix.c:190
TIFFErrorHandler _TIFFerrorHandler
Definition: tif_unix.c:382
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355
void * _TIFFrealloc(void *p, tmsize_t s)
Definition: tif_unix.c:351
TIFF * TIFFOpenW(const wchar_t *name, const char *mode)
Definition: tif_win32.c:301
TIFF * TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
Definition: tif_win32.c:306
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
void(* TIFFErrorHandler)(const char *, const char *, va_list)
Definition: tiffio.h:301
uint64_t toff_t
Definition: tiffio.h:70
#define _TIFF_fstat_f(fildes, stat_buff)
Definition: tiffiop.h:378
#define _TIFF_stat_s
Definition: tiffiop.h:381
#define _TIFF_lseek_f(fildes, offset, whence)
Definition: tiffiop.h:375
size_t TIFFIOSize_t
Definition: tiffiop.h:374
#define _TIFF_off_t
Definition: tiffiop.h:382
thandle_t h
Definition: tif_unix.c:65
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36