ReactOS 0.4.15-dev-8119-g4fb2fdb
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#include "tif_config.h"
31
32#ifdef HAVE_SYS_TYPES_H
33# include <sys/types.h>
34#endif
35
36#include <errno.h>
37
38#include <stdarg.h>
39#include <stdlib.h>
40#include <sys/stat.h>
41
42#ifdef HAVE_UNISTD_H
43# include <unistd.h>
44#endif
45
46#ifdef HAVE_FCNTL_H
47# include <fcntl.h>
48#endif
49
50#ifdef HAVE_IO_H
51# include <io.h>
52#endif
53
54#include "tiffiop.h"
55
56
57#define TIFF_IO_MAX 2147483647U
58
59
60typedef union fd_as_handle_union
61{
62 int fd;
65
66static tmsize_t
68{
70 const size_t bytes_total = (size_t) size;
71 size_t bytes_read;
72 tmsize_t count = -1;
73 if ((tmsize_t) bytes_total != size)
74 {
76 return (tmsize_t) -1;
77 }
78 fdh.h = fd;
79 for (bytes_read=0; bytes_read < bytes_total; bytes_read+=count)
80 {
81 char *buf_offset = (char *) buf+bytes_read;
82 size_t io_size = bytes_total-bytes_read;
83 if (io_size > TIFF_IO_MAX)
84 io_size = TIFF_IO_MAX;
85 count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
86 if (count <= 0)
87 break;
88 }
89 if (count < 0)
90 return (tmsize_t)-1;
91 return (tmsize_t) bytes_read;
92}
93
94static tmsize_t
96{
98 const size_t bytes_total = (size_t) size;
99 size_t bytes_written;
100 tmsize_t count = -1;
101 if ((tmsize_t) bytes_total != size)
102 {
104 return (tmsize_t) -1;
105 }
106 fdh.h = fd;
107 for (bytes_written=0; bytes_written < bytes_total; bytes_written+=count)
108 {
109 const char *buf_offset = (char *) buf+bytes_written;
110 size_t io_size = bytes_total-bytes_written;
111 if (io_size > TIFF_IO_MAX)
112 io_size = TIFF_IO_MAX;
113 count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
114 if (count <= 0)
115 break;
116 }
117 if (count < 0)
118 return (tmsize_t)-1;
119 return (tmsize_t) bytes_written;
120 /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
121}
122
123static uint64
125{
127 _TIFF_off_t off_io = (_TIFF_off_t) off;
128 if ((uint64) off_io != off)
129 {
131 return (uint64) -1; /* this is really gross */
132 }
133 fdh.h = fd;
134 return((uint64)_TIFF_lseek_f(fdh.fd,off_io,whence));
135}
136
137static int
139{
141 fdh.h = fd;
142 return(close(fdh.fd));
143}
144
145static uint64
147{
150 fdh.h = fd;
151 if (_TIFF_fstat_f(fdh.fd,&sb)<0)
152 return(0);
153 else
154 return((uint64)sb.st_size);
155}
156
157#ifdef HAVE_MMAP
158#include <sys/mman.h>
159
160static int
161_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
162{
163 uint64 size64 = _tiffSizeProc(fd);
164 tmsize_t sizem = (tmsize_t)size64;
165 if ((uint64)sizem==size64) {
167 fdh.h = fd;
168 *pbase = (void*)
169 mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
170 if (*pbase != (void*) -1) {
171 *psize = (tmsize_t)sizem;
172 return (1);
173 }
174 }
175 return (0);
176}
177
178static void
180{
181 (void) fd;
182 (void) munmap(base, (off_t) size);
183}
184#else /* !HAVE_MMAP */
185static int
187{
188 (void) fd; (void) pbase; (void) psize;
189 return (0);
190}
191
192static void
194{
195 (void) fd; (void) base; (void) size;
196}
197#endif /* !HAVE_MMAP */
198
199/*
200 * Open a TIFF file descriptor for read/writing.
201 */
202TIFF*
203TIFFFdOpen(int fd, const char* name, const char* mode)
204{
205 TIFF* tif;
206
208 fdh.fd = fd;
209 tif = TIFFClientOpen(name, mode,
210 fdh.h,
214 if (tif)
215 tif->tif_fd = fd;
216 return (tif);
217}
218
219/*
220 * Open a TIFF file for read/writing.
221 */
222TIFF*
223TIFFOpen(const char* name, const char* mode)
224{
225 static const char module[] = "TIFFOpen";
226 int m, fd;
227 TIFF* tif;
228
230 if (m == -1)
231 return ((TIFF*)0);
232
233/* for cygwin and mingw */
234#ifdef O_BINARY
235 m |= O_BINARY;
236#endif
237
238 fd = open(name, m, 0666);
239 if (fd < 0) {
240 if (errno > 0 && strerror(errno) != NULL ) {
241 TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
242 } else {
243 TIFFErrorExt(0, module, "%s: Cannot open", name);
244 }
245 return ((TIFF *)0);
246 }
247
248 tif = TIFFFdOpen((int)fd, name, mode);
249 if(!tif)
250 close(fd);
251 return tif;
252}
253
254#ifdef __WIN32__
255#include <windows.h>
256/*
257 * Open a TIFF file with a Unicode filename, for read/writing.
258 */
259TIFF*
260TIFFOpenW(const wchar_t* name, const char* mode)
261{
262 static const char module[] = "TIFFOpenW";
263 int m, fd;
264 int mbsize;
265 char *mbname;
266 TIFF* tif;
267
269 if (m == -1)
270 return ((TIFF*)0);
271
272/* for cygwin and mingw */
273#ifdef O_BINARY
274 m |= O_BINARY;
275#endif
276
277 fd = _wopen(name, m, 0666);
278 if (fd < 0) {
279 TIFFErrorExt(0, module, "%ls: Cannot open", name);
280 return ((TIFF *)0);
281 }
282
283 mbname = NULL;
284 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
285 if (mbsize > 0) {
286 mbname = _TIFFmalloc(mbsize);
287 if (!mbname) {
289 "Can't allocate space for filename conversion buffer");
290 return ((TIFF*)0);
291 }
292
293 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
294 NULL, NULL);
295 }
296
297 tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
298 mode);
299
300 _TIFFfree(mbname);
301
302 if(!tif)
303 close(fd);
304 return tif;
305}
306#endif
307
308void*
310{
311 if (s == 0)
312 return ((void *) NULL);
313
314 return (malloc((size_t) s));
315}
316
318{
319 if( nmemb == 0 || siz == 0 )
320 return ((void *) NULL);
321
322 return calloc((size_t) nmemb, (size_t)siz);
323}
324
325void
327{
328 free(p);
329}
330
331void*
333{
334 return (realloc(p, (size_t) s));
335}
336
337void
339{
340 memset(p, v, (size_t) c);
341}
342
343void
344_TIFFmemcpy(void* d, const void* s, tmsize_t c)
345{
346 memcpy(d, s, (size_t) c);
347}
348
349int
350_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
351{
352 return (memcmp(p1, p2, (size_t) c));
353}
354
355static void
356unixWarningHandler(const char* module, const char* fmt, va_list ap)
357{
358 if (module != NULL)
359 fprintf(stderr, "%s: ", module);
360 fprintf(stderr, "Warning, ");
362 fprintf(stderr, ".\n");
363}
365
366static void
367unixErrorHandler(const char* module, const char* fmt, va_list ap)
368{
369 if (module != NULL)
370 fprintf(stderr, "%s: ", module);
372 fprintf(stderr, ".\n");
373}
375
376/* vim: set ts=8 sts=8 sw=8 noet: */
377
378/*
379 * Local Variables:
380 * mode: c
381 * c-basic-offset: 8
382 * fill-column: 78
383 * End:
384 */
#define EINVAL
Definition: acclib.h:90
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
char * va_list
Definition: acmsvcex.h:78
#define O_BINARY
Definition: acwin.h:109
#define read
Definition: acwin.h:96
#define open
Definition: acwin.h:95
#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
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
superblock * sb
Definition: btrfs.c:4261
unsigned long long uint64
Definition: platform.h:18
__kernel_size_t size_t
Definition: linux.h:237
__kernel_off_t off_t
Definition: linux.h:201
const GLdouble * v
Definition: gl.h:2040
GLdouble s
Definition: gl.h:2039
GLuint GLuint GLsizei count
Definition: gl.h:1545
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 stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define d
Definition: ke_i.h:81
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
const char * strerror(int err)
Definition: compat_str.c:23
_Must_inspect_result_ _Out_ LPSIZE psize
Definition: ntgdi.h:1569
#define calloc
Definition: rosglue.h:14
#define errno
Definition: errno.h:18
_CRTIMP int __cdecl _wopen(const wchar_t *_Filename, int _OpenFlag,...)
Definition: file.c:2020
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:115
int tif_fd
Definition: tiffiop.h:117
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
Definition: tif_error.c:65
TIFF * TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc)
Definition: tif_open.c:71
int _TIFFgetMode(const char *mode, const char *module)
Definition: tif_open.c:47
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
Definition: tif_unix.c:67
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
Definition: tif_unix.c:367
void _TIFFfree(void *p)
Definition: tif_unix.c:326
TIFFErrorHandler _TIFFwarningHandler
Definition: tif_unix.c:364
TIFF * TIFFOpen(const char *name, const char *mode)
Definition: tif_unix.c:223
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:193
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
Definition: tif_unix.c:95
static uint64 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
Definition: tif_unix.c:124
void * _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
Definition: tif_unix.c:317
void _TIFFmemset(void *p, int v, tmsize_t c)
Definition: tif_unix.c:338
void * _TIFFmalloc(tmsize_t s)
Definition: tif_unix.c:309
TIFF * TIFFFdOpen(int fd, const char *name, const char *mode)
Definition: tif_unix.c:203
static int _tiffCloseProc(thandle_t fd)
Definition: tif_unix.c:138
#define TIFF_IO_MAX
Definition: tif_unix.c:57
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
Definition: tif_unix.c:350
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
Definition: tif_unix.c:356
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
Definition: tif_unix.c:186
TIFFErrorHandler _TIFFerrorHandler
Definition: tif_unix.c:374
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:344
static uint64 _tiffSizeProc(thandle_t fd)
Definition: tif_unix.c:146
void * _TIFFrealloc(void *p, tmsize_t s)
Definition: tif_unix.c:332
TIFF * TIFFOpenW(const wchar_t *name, const char *mode)
Definition: tif_win32.c:299
uint64 toff_t
Definition: tiffio.h:66
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:65
void(* TIFFErrorHandler)(const char *, const char *, va_list)
Definition: tiffio.h:271
#define _TIFF_fstat_f(fildes, stat_buff)
Definition: tiffiop.h:325
#define _TIFF_stat_s
Definition: tiffiop.h:328
#define _TIFF_lseek_f(fildes, offset, whence)
Definition: tiffiop.h:322
size_t TIFFIOSize_t
Definition: tiffiop.h:321
#define _TIFF_off_t
Definition: tiffiop.h:329
thandle_t h
Definition: tif_unix.c:63
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36