opencv

Форк
0
/
tif_unix.c 
370 строк · 9.0 Кб
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
#define TIFF_IO_MAX 2147483647U
57

58
typedef union fd_as_handle_union
59
{
60
    int fd;
61
    thandle_t h;
62
} fd_as_handle_union_t;
63

64
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
65
{
66
    fd_as_handle_union_t fdh;
67
    const size_t bytes_total = (size_t)size;
68
    size_t bytes_read;
69
    tmsize_t count = -1;
70
    if ((tmsize_t)bytes_total != size)
71
    {
72
        errno = EINVAL;
73
        return (tmsize_t)-1;
74
    }
75
    fdh.h = fd;
76
    for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
77
    {
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);
83
        if (count <= 0)
84
            break;
85
    }
86
    if (count < 0)
87
        return (tmsize_t)-1;
88
    return (tmsize_t)bytes_read;
89
}
90

91
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
92
{
93
    fd_as_handle_union_t fdh;
94
    const size_t bytes_total = (size_t)size;
95
    size_t bytes_written;
96
    tmsize_t count = -1;
97
    if ((tmsize_t)bytes_total != size)
98
    {
99
        errno = EINVAL;
100
        return (tmsize_t)-1;
101
    }
102
    fdh.h = fd;
103
    for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
104
    {
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);
110
        if (count <= 0)
111
            break;
112
    }
113
    if (count < 0)
114
        return (tmsize_t)-1;
115
    return (tmsize_t)bytes_written;
116
    /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
117
}
118

119
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
120
{
121
    fd_as_handle_union_t fdh;
122
    _TIFF_off_t off_io = (_TIFF_off_t)off;
123
    if ((uint64_t)off_io != off)
124
    {
125
        errno = EINVAL;
126
        return (uint64_t)-1; /* this is really gross */
127
    }
128
    fdh.h = fd;
129
    return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
130
}
131

132
static int _tiffCloseProc(thandle_t fd)
133
{
134
    fd_as_handle_union_t fdh;
135
    fdh.h = fd;
136
    return (close(fdh.fd));
137
}
138

139
static uint64_t _tiffSizeProc(thandle_t fd)
140
{
141
    _TIFF_stat_s sb;
142
    fd_as_handle_union_t fdh;
143
    fdh.h = fd;
144
    if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
145
        return (0);
146
    else
147
        return ((uint64_t)sb.st_size);
148
}
149

150
#ifdef HAVE_MMAP
151
#include <sys/mman.h>
152

153
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
154
{
155
    uint64_t size64 = _tiffSizeProc(fd);
156
    tmsize_t sizem = (tmsize_t)size64;
157
    if (size64 && (uint64_t)sizem == size64)
158
    {
159
        fd_as_handle_union_t fdh;
160
        fdh.h = fd;
161
        *pbase =
162
            (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
163
        if (*pbase != (void *)-1)
164
        {
165
            *psize = (tmsize_t)sizem;
166
            return (1);
167
        }
168
    }
169
    return (0);
170
}
171

172
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
173
{
174
    (void)fd;
175
    (void)munmap(base, (off_t)size);
176
}
177
#else  /* !HAVE_MMAP */
178
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
179
{
180
    (void)fd;
181
    (void)pbase;
182
    (void)psize;
183
    return (0);
184
}
185

186
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
187
{
188
    (void)fd;
189
    (void)base;
190
    (void)size;
191
}
192
#endif /* !HAVE_MMAP */
193

194
/*
195
 * Open a TIFF file descriptor for read/writing.
196
 */
197
TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
198
{
199
    return TIFFFdOpenExt(fd, name, mode, NULL);
200
}
201

202
TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
203
                    TIFFOpenOptions *opts)
204
{
205
    TIFF *tif;
206

207
    fd_as_handle_union_t fdh;
208
    fdh.fd = fd;
209
    tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
210
                            _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
211
                            _tiffMapProc, _tiffUnmapProc, opts);
212
    if (tif)
213
        tif->tif_fd = fd;
214
    return (tif);
215
}
216

217
/*
218
 * Open a TIFF file for read/writing.
219
 */
220
TIFF *TIFFOpen(const char *name, const char *mode)
221
{
222
    return TIFFOpenExt(name, mode, NULL);
223
}
224

225
TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
226
{
227
    static const char module[] = "TIFFOpen";
228
    int m, fd;
229
    TIFF *tif;
230

231
    m = _TIFFgetMode(opts, NULL, mode, module);
232
    if (m == -1)
233
        return ((TIFF *)0);
234

235
/* for cygwin and mingw */
236
#ifdef O_BINARY
237
    m |= O_BINARY;
238
#endif
239

240
    fd = open(name, m, 0666);
241
    if (fd < 0)
242
    {
243
        if (errno > 0 && strerror(errno) != NULL)
244
        {
245
            _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
246
                            strerror(errno));
247
        }
248
        else
249
        {
250
            _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
251
        }
252
        return ((TIFF *)0);
253
    }
254

255
    tif = TIFFFdOpenExt((int)fd, name, mode, opts);
256
    if (!tif)
257
        close(fd);
258
    return tif;
259
}
260

261
#ifdef __WIN32__
262
#include <windows.h>
263
/*
264
 * Open a TIFF file with a Unicode filename, for read/writing.
265
 */
266
TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
267
{
268
    return TIFFOpenWExt(name, mode, NULL);
269
}
270
TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
271
{
272
    static const char module[] = "TIFFOpenW";
273
    int m, fd;
274
    int mbsize;
275
    char *mbname;
276
    TIFF *tif;
277

278
    m = _TIFFgetMode(opts, NULL, mode, module);
279
    if (m == -1)
280
        return ((TIFF *)0);
281

282
/* for cygwin and mingw */
283
#ifdef O_BINARY
284
    m |= O_BINARY;
285
#endif
286

287
    fd = _wopen(name, m, 0666);
288
    if (fd < 0)
289
    {
290
        _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
291
        return ((TIFF *)0);
292
    }
293

294
    mbname = NULL;
295
    mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
296
    if (mbsize > 0)
297
    {
298
        mbname = _TIFFmalloc(mbsize);
299
        if (!mbname)
300
        {
301
            _TIFFErrorEarly(
302
                opts, NULL, module,
303
                "Can't allocate space for filename conversion buffer");
304
            return ((TIFF *)0);
305
        }
306

307
        WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
308
    }
309

310
    tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
311
                        opts);
312

313
    _TIFFfree(mbname);
314

315
    if (!tif)
316
        close(fd);
317
    return tif;
318
}
319
#endif
320

321
void *_TIFFmalloc(tmsize_t s)
322
{
323
    if (s == 0)
324
        return ((void *)NULL);
325

326
    return (malloc((size_t)s));
327
}
328

329
void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
330
{
331
    if (nmemb == 0 || siz == 0)
332
        return ((void *)NULL);
333

334
    return calloc((size_t)nmemb, (size_t)siz);
335
}
336

337
void _TIFFfree(void *p) { free(p); }
338

339
void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
340

341
void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
342

343
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
344
{
345
    memcpy(d, s, (size_t)c);
346
}
347

348
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
349
{
350
    return (memcmp(p1, p2, (size_t)c));
351
}
352

353
static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
354
{
355
    if (module != NULL)
356
        fprintf(stderr, "%s: ", module);
357
    fprintf(stderr, "Warning, ");
358
    vfprintf(stderr, fmt, ap);
359
    fprintf(stderr, ".\n");
360
}
361
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
362

363
static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
364
{
365
    if (module != NULL)
366
        fprintf(stderr, "%s: ", module);
367
    vfprintf(stderr, fmt, ap);
368
    fprintf(stderr, ".\n");
369
}
370
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
371

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.