LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_win32.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 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26/*
27 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
28 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
29 */
30
31#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
32#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
33#endif
34
40#include "tiffiop.h"
41#include <stdlib.h>
42
43#include <windows.h>
44
45/*
46 CreateFileA/CreateFileW return type 'HANDLE' while TIFFFdOpen() takes 'int',
47 which is formally incompatible and can even seemingly be of different size:
48 HANDLE is 64 bit under Win64, while int is still 32 bits there.
49
50 However, only the lower 32 bits of a HANDLE are significant under Win64 as,
51 for interoperability reasons, they must have the same values in 32- and
52 64-bit programs running on the same system, see
53
54 https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
55
56 Because of this, it is safe to define the following trivial functions for
57 casting between ints and HANDLEs, which are only really needed to avoid
58 compiler warnings (and, perhaps, to make the code slightly more clear).
59 Note that using the intermediate cast to "intptr_t" is crucial for warning
60 avoidance, as this integer type has the same size as HANDLE in all builds.
61*/
62
74static inline thandle_t thandle_from_int(int ifd)
75{
76 return (thandle_t)(intptr_t)ifd;
77}
78
90static inline int thandle_to_int(thandle_t fd) { return (int)(intptr_t)fd; }
91
109static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
110{
111 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
112 * 32bit sizes, so we loop through the data in suitable 32bit sized
113 * chunks */
114 uint8_t *ma;
115 uint64_t mb;
116 DWORD n;
117 DWORD o;
118 tmsize_t p;
119 ma = (uint8_t *)buf;
120 mb = (uint64_t)size;
121 p = 0;
122 while (mb > 0)
123 {
124 n = 0x80000000UL;
125 if ((uint64_t)n > mb)
126 n = (DWORD)mb;
127 if (!ReadFile(fd, (LPVOID)ma, n, &o, NULL))
128 return (0);
129 ma += o;
130 mb -= o;
131 p += o;
132 if (o != n)
133 break;
134 }
135 return (p);
136}
137
155static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
156{
157 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
158 * 32bit sizes, so we loop through the data in suitable 32bit sized
159 * chunks */
160 uint8_t *ma;
161 uint64_t mb;
162 DWORD n;
163 DWORD o;
164 tmsize_t p;
165 ma = (uint8_t *)buf;
166 mb = (uint64_t)size;
167 p = 0;
168 while (mb > 0)
169 {
170 n = 0x80000000UL;
171 if ((uint64_t)n > mb)
172 n = (DWORD)mb;
173 if (!WriteFile(fd, (LPVOID)ma, n, &o, NULL))
174 return (0);
175 ma += o;
176 mb -= o;
177 p += o;
178 if (o != n)
179 break;
180 }
181 return (p);
182}
183
199static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
200{
201 LARGE_INTEGER offli;
202 DWORD dwMoveMethod;
203 offli.QuadPart = (LONGLONG)off;
204 switch (whence)
205 {
206 case SEEK_SET:
207 dwMoveMethod = FILE_BEGIN;
208 break;
209 case SEEK_CUR:
210 dwMoveMethod = FILE_CURRENT;
211 break;
212 case SEEK_END:
213 dwMoveMethod = FILE_END;
214 break;
215 default:
216 dwMoveMethod = FILE_BEGIN;
217 break;
218 }
219 offli.LowPart =
220 SetFilePointer(fd, (LONG)offli.LowPart, &offli.HighPart, dwMoveMethod);
221 if ((offli.LowPart == INVALID_SET_FILE_POINTER) &&
222 (GetLastError() != NO_ERROR))
223 return ((uint64_t)-1);
224 return ((uint64_t)offli.QuadPart);
225}
226
238static int _tiffCloseProc(thandle_t fd) { return (CloseHandle(fd) ? 0 : -1); }
239
251static uint64_t _tiffSizeProc(thandle_t fd)
252{
253 LARGE_INTEGER m;
254 if (GetFileSizeEx(fd, &m))
255 return ((uint64_t)m.QuadPart);
256 else
257 return (0);
258}
259
260/*
261 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
262 *
263 * Windows uses both a handle and a pointer for file mapping,
264 * but according to the SDK documentation and Richter's book
265 * "Advanced Windows Programming" it is safe to free the handle
266 * after obtaining the file mapping pointer
267 *
268 * This removes a nasty OS dependency and cures a problem
269 * with Visual C++ 5.0
270 */
290static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
291{
292 uint64_t size;
293 tmsize_t sizem;
294 HANDLE hMapFile;
295
296 size = _tiffSizeProc(fd);
297 sizem = (tmsize_t)size;
298 if (!size || (uint64_t)sizem != size)
299 return (0);
300
301 /* By passing in 0 for the maximum file size, it specifies that we
302 create a file mapping object for the full file size. */
303 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
304 if (hMapFile == NULL)
305 return (0);
306 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
307 CloseHandle(hMapFile);
308 if (*pbase == NULL)
309 return (0);
310 *psize = size;
311 return (1);
312}
313
326static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
327{
328 (void)fd;
329 (void)size;
330 UnmapViewOfFile(base);
331}
332
333/*
334 * Open a TIFF file descriptor for read/writing.
335 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
336 * string, which forces the file to be opened unmapped.
337 */
338TIFF *TIFFFdOpen(int ifd, const char *name, const char *mode)
339{
340 return TIFFFdOpenExt(ifd, name, mode, NULL);
341}
342
343TIFF *TIFFFdOpenExt(int ifd, const char *name, const char *mode,
344 TIFFOpenOptions *opts)
345{
346 TIFF *tif;
347 int fSuppressMap;
348 int m;
349
350 fSuppressMap = 0;
351 for (m = 0; mode[m] != 0; m++)
352 {
353 if (mode[m] == 'u')
354 {
355 fSuppressMap = 1;
356 break;
357 }
358 }
359
360 tif = TIFFClientOpenExt(
363 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
364 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc, opts);
365 if (tif)
366 tif->tif_fd = ifd;
367 return (tif);
368}
369
370#ifndef _WIN32_WCE
371
372/*
373 * Open a TIFF file for read/writing.
374 */
375TIFF *TIFFOpen(const char *name, const char *mode)
376{
377 return TIFFOpenExt(name, mode, NULL);
378}
379
380TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
381{
382 static const char module[] = "TIFFOpen";
383 thandle_t fd;
384 int m;
385 DWORD dwMode;
386 TIFF *tif;
387
388 m = _TIFFgetMode(opts, NULL, mode, module);
389
390 switch (m)
391 {
392 case O_RDONLY:
393 dwMode = OPEN_EXISTING;
394 break;
395 case O_RDWR:
396 dwMode = OPEN_EXISTING;
397 break;
398 case O_RDWR | O_CREAT:
399 dwMode = OPEN_ALWAYS;
400 break;
401 case O_RDWR | O_TRUNC:
402 dwMode = CREATE_ALWAYS;
403 break;
404 case O_RDWR | O_CREAT | O_TRUNC:
405 dwMode = CREATE_ALWAYS;
406 break;
407 default:
408 return ((TIFF *)0);
409 }
410
411 fd = (thandle_t)CreateFileA(
412 name, (m == O_RDONLY) ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE),
413 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
414 (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL,
415 NULL);
416 if (fd == INVALID_HANDLE_VALUE)
417 {
418 _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
419 return ((TIFF *)0);
420 }
421
422 tif = TIFFFdOpenExt(thandle_to_int(fd), name, mode, opts);
423 if (!tif)
424 CloseHandle(fd);
425 return tif;
426}
427
458TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
459{
460 return TIFFOpenWExt(name, mode, NULL);
461}
462
497TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
498{
499 static const char module[] = "TIFFOpenW";
500 thandle_t fd;
501 int m;
502 DWORD dwMode;
503 int mbsize;
504 char *mbname;
505 TIFF *tif;
506
507 m = _TIFFgetMode(opts, NULL, mode, module);
508
509 switch (m)
510 {
511 case O_RDONLY:
512 dwMode = OPEN_EXISTING;
513 break;
514 case O_RDWR:
515 dwMode = OPEN_EXISTING;
516 break;
517 case O_RDWR | O_CREAT:
518 dwMode = OPEN_ALWAYS;
519 break;
520 case O_RDWR | O_TRUNC:
521 dwMode = CREATE_ALWAYS;
522 break;
523 case O_RDWR | O_CREAT | O_TRUNC:
524 dwMode = CREATE_ALWAYS;
525 break;
526 default:
527 return ((TIFF *)0);
528 }
529
530 fd = (thandle_t)CreateFileW(
531 name, (m == O_RDONLY) ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE),
532 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
533 (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL,
534 NULL);
535 if (fd == INVALID_HANDLE_VALUE)
536 {
537 _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
538 return ((TIFF *)0);
539 }
540
541 mbname = NULL;
542 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
543 if (mbsize > 0)
544 {
545 mbname = (char *)_TIFFmalloc(mbsize);
546 if (!mbname)
547 {
549 opts, NULL, module,
550 "Can't allocate space for filename conversion buffer");
551 return ((TIFF *)0);
552 }
553
554 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
555 }
556
558 (mbname != NULL) ? mbname : "<unknown>", mode, opts);
559 if (!tif)
560 CloseHandle(fd);
561
562 _TIFFfree(mbname);
563
564 return tif;
565}
566
567#endif /* ndef _WIN32_WCE */
568
570{
571 if (s == 0)
572 return ((void *)NULL);
573
574 return (malloc((size_t)s));
575}
576
578{
579 if (nmemb == 0 || siz == 0)
580 return ((void *)NULL);
581
582 return calloc((size_t)nmemb, (size_t)siz);
583}
584
585void _TIFFfree(void *p) { free(p); }
586
587void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
588
589void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
590
591void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
592{
593 memcpy(d, s, (size_t)c);
594}
595
596int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
597{
598 return (memcmp(p1, p2, (size_t)c));
599}
600
601#ifndef _WIN32_WCE
602
617static void Win32WarningHandler(const char *module, const char *fmt, va_list ap)
618{
619 if (module != NULL)
620 fprintf(stderr, "%s: ", module);
621 fprintf(stderr, "Warning, ");
622 vfprintf(stderr, fmt, ap);
623 fprintf(stderr, ".\n");
624}
626
640static void Win32ErrorHandler(const char *module, const char *fmt, va_list ap)
641{
642 if (module != NULL)
643 fprintf(stderr, "%s: ", module);
644 vfprintf(stderr, fmt, ap);
645 fprintf(stderr, ".\n");
646}
648
649#endif /* ndef _WIN32_WCE */
static void Win32WarningHandler(const char *module, const char *fmt, va_list ap)
Print a default warning diagnostic to stderr.
Definition tif_win32.c:617
TIFFErrorHandler _TIFFwarningHandler
Store the global warning callback.
Definition tif_win32.c:625
static void Win32ErrorHandler(const char *module, const char *fmt, va_list ap)
Print a default error diagnostic to stderr.
Definition tif_win32.c:640
TIFFErrorHandler _TIFFerrorHandler
Store the global error callback.
Definition tif_win32.c:647
static int thandle_to_int(thandle_t fd)
Convert a native Windows handle to the backend integer representation.
Definition tif_win32.c:90
static thandle_t thandle_from_int(int ifd)
Convert the native Windows integer handle representation to thandle_t.
Definition tif_win32.c:74
int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize)
Decline mapping when the client supplies no mapping implementation.
Definition tif_open.c:45
void _TIFFErrorEarly(TIFFOpenOptions *opts, thandle_t clientdata, const char *module, const char *fmt,...)
Report an opening error before a complete TIFF handle is available.
Definition tif_error.c:85
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode, const char *module)
Translate the leading open-mode characters into OS access flags.
Definition tif_open.c:60
void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
Provide a no-op unmapper when the client omits one.
Definition tif_open.c:53
void(* TIFFErrorHandler)(const char *module, const char *fmt, va_list ap)
Handle a library-wide error or warning diagnostic.
Definition tiffio.h:835
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)
Initialise a TIFF handle using client-supplied I/O callbacks.
Definition tif_open.c:326
TIFF * TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
Open a TIFF file by pathname.
Definition tif_win32.c:380
TIFF * TIFFFdOpenExt(int ifd, const char *name, const char *mode, TIFFOpenOptions *opts)
Attach a TIFF handle to an already-open platform resource.
Definition tif_win32.c:343
TIFF * TIFFOpen(const char *name, const char *mode)
Open a TIFF file by pathname.
Definition tif_win32.c:375
TIFF * TIFFOpenW(const wchar_t *name, const char *mode)
Open a TIFF file using a wide Windows pathname.
Definition tif_win32.c:458
TIFF * TIFFFdOpen(int ifd, const char *name, const char *mode)
Attach a TIFF handle to an already-open platform resource.
Definition tif_win32.c:338
TIFF * TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
Open a TIFF file using a wide Windows pathname.
Definition tif_win32.c:497
void _TIFFfree(void *p)
Release a block allocated by the public allocation family.
Definition tif_win32.c:585
void * _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array through the public allocator.
Definition tif_win32.c:577
void _TIFFmemset(void *p, int v, tmsize_t c)
Fill a byte range with one repeated byte.
Definition tif_win32.c:589
void * _TIFFmalloc(tmsize_t s)
Allocate an uninitialised block through the public allocator.
Definition tif_win32.c:569
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
Compare two byte ranges lexicographically.
Definition tif_win32.c:596
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_win32.c:591
void * _TIFFrealloc(void *p, tmsize_t s)
Resize a public-allocator block using platform realloc semantics.
Definition tif_win32.c:587
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
void * thandle_t
Carry an opaque client I/O context through library callbacks.
Definition tiffio.h:161
Hold allocation limits and diagnostics used when opening a TIFF handle.
Definition tiffiop.h:791
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
int tif_fd
File-descriptor metadata; client I/O still uses tif_clientdata.
Definition tiffiop.h:398
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
Read bytes from the Windows HANDLE with ReadFile().
Definition tif_win32.c:109
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
Release a read-only backend mapping.
Definition tif_win32.c:326
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
Write bytes to the Windows HANDLE with WriteFile().
Definition tif_win32.c:155
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the platform resource and return its reported position.
Definition tif_win32.c:199
static int _tiffCloseProc(thandle_t fd)
Release the backend OS resource.
Definition tif_win32.c:238
static uint64_t _tiffSizeProc(thandle_t fd)
Query the backend resource length in bytes.
Definition tif_win32.c:251
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
Create a read-only view of the entire backend resource.
Definition tif_win32.c:290
Declare private library state, callbacks, and implementation helpers.