]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_win32.c
Fit the generic preferences editor dialog to its pages size initially.
[wxWidgets.git] / src / tiff / libtiff / tif_win32.c
CommitLineData
8414a40c
VZ
1/* $Id$ */
2
3/*
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27/*
28 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
29 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
30 */
31#include "tiffiop.h"
32
3442c078
VZ
33#include <windows.h>
34
8c2bd115
VZ
35/* This define is missing from VC6 headers. */
36#ifndef INVALID_SET_FILE_POINTER
37#define INVALID_SET_FILE_POINTER ((DWORD)-1)
38#endif
39
80ed523f
VZ
40static tmsize_t
41_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
8414a40c 42{
80ed523f
VZ
43 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
44 * 32bit sizes, so we loop through the data in suitable 32bit sized
45 * chunks */
46 uint8* ma;
47 uint64 mb;
48 DWORD n;
49 DWORD o;
50 tmsize_t p;
51 ma=(uint8*)buf;
52 mb=size;
53 p=0;
54 while (mb>0)
55 {
56 n=0x80000000UL;
57 if ((uint64)n>mb)
58 n=(DWORD)mb;
59 if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
60 return(0);
61 ma+=o;
62 mb-=o;
63 p+=o;
64 if (o!=n)
65 break;
66 }
67 return(p);
8414a40c
VZ
68}
69
80ed523f
VZ
70static tmsize_t
71_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
8414a40c 72{
80ed523f
VZ
73 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
74 * 32bit sizes, so we loop through the data in suitable 32bit sized
75 * chunks */
76 uint8* ma;
77 uint64 mb;
78 DWORD n;
79 DWORD o;
80 tmsize_t p;
81 ma=(uint8*)buf;
82 mb=size;
83 p=0;
84 while (mb>0)
85 {
86 n=0x80000000UL;
87 if ((uint64)n>mb)
88 n=(DWORD)mb;
89 if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
90 return(0);
91 ma+=o;
92 mb-=o;
93 p+=o;
94 if (o!=n)
95 break;
96 }
97 return(p);
8414a40c
VZ
98}
99
80ed523f
VZ
100static uint64
101_tiffSeekProc(thandle_t fd, uint64 off, int whence)
8414a40c 102{
80ed523f
VZ
103 LARGE_INTEGER offli;
104 DWORD dwMoveMethod;
105 offli.QuadPart = off;
8414a40c
VZ
106 switch(whence)
107 {
80ed523f
VZ
108 case SEEK_SET:
109 dwMoveMethod = FILE_BEGIN;
110 break;
111 case SEEK_CUR:
112 dwMoveMethod = FILE_CURRENT;
113 break;
114 case SEEK_END:
115 dwMoveMethod = FILE_END;
116 break;
117 default:
118 dwMoveMethod = FILE_BEGIN;
119 break;
8414a40c 120 }
80ed523f
VZ
121 offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
122 if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
123 offli.QuadPart=0;
124 return(offli.QuadPart);
8414a40c
VZ
125}
126
127static int
128_tiffCloseProc(thandle_t fd)
129{
130 return (CloseHandle(fd) ? 0 : -1);
131}
132
80ed523f 133static uint64
8414a40c
VZ
134_tiffSizeProc(thandle_t fd)
135{
80ed523f
VZ
136 ULARGE_INTEGER m;
137 m.LowPart=GetFileSize(fd,&m.HighPart);
138 return(m.QuadPart);
8414a40c
VZ
139}
140
8414a40c 141static int
80ed523f 142_tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
8414a40c 143{
80ed523f
VZ
144 (void) fd;
145 (void) pbase;
146 (void) psize;
8414a40c
VZ
147 return (0);
148}
149
150/*
151 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
152 *
153 * Windows uses both a handle and a pointer for file mapping,
154 * but according to the SDK documentation and Richter's book
155 * "Advanced Windows Programming" it is safe to free the handle
156 * after obtaining the file mapping pointer
157 *
158 * This removes a nasty OS dependency and cures a problem
159 * with Visual C++ 5.0
160 */
161static int
80ed523f 162_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
8414a40c 163{
80ed523f
VZ
164 uint64 size;
165 tmsize_t sizem;
8414a40c
VZ
166 HANDLE hMapFile;
167
80ed523f
VZ
168 size = _tiffSizeProc(fd);
169 sizem = (tmsize_t)size;
170 if ((uint64)sizem!=size)
8414a40c 171 return (0);
80ed523f
VZ
172
173 /* By passing in 0 for the maximum file size, it specifies that we
174 create a file mapping object for the full file size. */
175 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
8414a40c
VZ
176 if (hMapFile == NULL)
177 return (0);
178 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
179 CloseHandle(hMapFile);
180 if (*pbase == NULL)
181 return (0);
182 *psize = size;
183 return(1);
184}
185
8414a40c 186static void
80ed523f 187_tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
8414a40c 188{
80ed523f
VZ
189 (void) fd;
190 (void) base;
191 (void) size;
8414a40c
VZ
192}
193
194static void
80ed523f 195_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
8414a40c 196{
80ed523f
VZ
197 (void) fd;
198 (void) size;
8414a40c
VZ
199 UnmapViewOfFile(base);
200}
201
202/*
203 * Open a TIFF file descriptor for read/writing.
204 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
205 * string, which forces the file to be opened unmapped.
206 */
207TIFF*
208TIFFFdOpen(int ifd, const char* name, const char* mode)
209{
210 TIFF* tif;
80ed523f
VZ
211 int fSuppressMap;
212 int m;
213 fSuppressMap=0;
214 for (m=0; mode[m]!=0; m++)
215 {
216 if (mode[m]=='u')
217 {
218 fSuppressMap=1;
219 break;
220 }
221 }
8414a40c
VZ
222 tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
223 _tiffReadProc, _tiffWriteProc,
224 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
225 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
226 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
227 if (tif)
228 tif->tif_fd = ifd;
229 return (tif);
230}
231
80ed523f
VZ
232#ifndef _WIN32_WCE
233
8414a40c
VZ
234/*
235 * Open a TIFF file for read/writing.
236 */
237TIFF*
238TIFFOpen(const char* name, const char* mode)
239{
240 static const char module[] = "TIFFOpen";
241 thandle_t fd;
242 int m;
243 DWORD dwMode;
244 TIFF* tif;
245
246 m = _TIFFgetMode(mode, module);
247
80ed523f
VZ
248 switch(m) {
249 case O_RDONLY: dwMode = OPEN_EXISTING; break;
250 case O_RDWR: dwMode = OPEN_ALWAYS; break;
251 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
252 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
253 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
254 default: return ((TIFF*)0);
8414a40c 255 }
80ed523f 256
8414a40c
VZ
257 fd = (thandle_t)CreateFileA(name,
258 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
259 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
260 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
261 NULL);
262 if (fd == INVALID_HANDLE_VALUE) {
263 TIFFErrorExt(0, module, "%s: Cannot open", name);
264 return ((TIFF *)0);
265 }
266
267 tif = TIFFFdOpen((int)fd, name, mode);
268 if(!tif)
269 CloseHandle(fd);
270 return tif;
271}
272
273/*
274 * Open a TIFF file with a Unicode filename, for read/writing.
275 */
276TIFF*
277TIFFOpenW(const wchar_t* name, const char* mode)
278{
279 static const char module[] = "TIFFOpenW";
280 thandle_t fd;
281 int m;
282 DWORD dwMode;
283 int mbsize;
284 char *mbname;
285 TIFF *tif;
286
287 m = _TIFFgetMode(mode, module);
288
289 switch(m) {
290 case O_RDONLY: dwMode = OPEN_EXISTING; break;
291 case O_RDWR: dwMode = OPEN_ALWAYS; break;
292 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
293 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
294 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
295 default: return ((TIFF*)0);
296 }
297
298 fd = (thandle_t)CreateFileW(name,
299 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
80ed523f 300 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
8414a40c
VZ
301 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
302 NULL);
303 if (fd == INVALID_HANDLE_VALUE) {
304 TIFFErrorExt(0, module, "%S: Cannot open", name);
305 return ((TIFF *)0);
306 }
307
308 mbname = NULL;
309 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
310 if (mbsize > 0) {
311 mbname = (char *)_TIFFmalloc(mbsize);
312 if (!mbname) {
313 TIFFErrorExt(0, module,
314 "Can't allocate space for filename conversion buffer");
315 return ((TIFF*)0);
316 }
317
318 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
319 NULL, NULL);
320 }
321
322 tif = TIFFFdOpen((int)fd,
323 (mbname != NULL) ? mbname : "<unknown>", mode);
324 if(!tif)
325 CloseHandle(fd);
326
327 _TIFFfree(mbname);
328
329 return tif;
330}
331
80ed523f
VZ
332#endif /* ndef _WIN32_WCE */
333
334void*
335_TIFFmalloc(tmsize_t s)
8414a40c 336{
80ed523f 337 return (malloc((size_t) s));
8414a40c
VZ
338}
339
340void
80ed523f 341_TIFFfree(void* p)
8414a40c 342{
80ed523f 343 free(p);
8414a40c
VZ
344}
345
80ed523f
VZ
346void*
347_TIFFrealloc(void* p, tmsize_t s)
8414a40c 348{
80ed523f 349 return (realloc(p, (size_t) s));
8414a40c
VZ
350}
351
352void
80ed523f 353_TIFFmemset(void* p, int v, tmsize_t c)
8414a40c 354{
80ed523f 355 memset(p, v, (size_t) c);
8414a40c
VZ
356}
357
358void
80ed523f 359_TIFFmemcpy(void* d, const void* s, tmsize_t c)
8414a40c 360{
80ed523f 361 memcpy(d, s, (size_t) c);
8414a40c
VZ
362}
363
364int
80ed523f 365_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
8414a40c 366{
80ed523f 367 return (memcmp(p1, p2, (size_t) c));
8414a40c
VZ
368}
369
80ed523f
VZ
370#ifndef _WIN32_WCE
371
9ab1e153 372#if defined(_MSC_VER) && (_MSC_VER < 1500)
80ed523f
VZ
373# define vsnprintf _vsnprintf
374#endif
375
8414a40c
VZ
376static void
377Win32WarningHandler(const char* module, const char* fmt, va_list ap)
378{
379#ifndef TIF_PLATFORM_CONSOLE
47493bc0
VZ
380 char *szTitle;
381 char *szTmp;
382 const char *szTitleText = "%s Warning";
383 const char *szDefaultModule = "LIBTIFF";
384 const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
80ed523f
VZ
385 SIZE_T nBufSize = (strlen(szTmpModule) +
386 strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
387
388 if ((szTitle = (char*)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
8414a40c
VZ
389 return;
390 sprintf(szTitle, szTitleText, szTmpModule);
391 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
80ed523f 392 vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
8414a40c
VZ
393 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
394 LocalFree(szTitle);
80ed523f 395
8414a40c
VZ
396 return;
397#else
398 if (module != NULL)
399 fprintf(stderr, "%s: ", module);
400 fprintf(stderr, "Warning, ");
401 vfprintf(stderr, fmt, ap);
402 fprintf(stderr, ".\n");
403#endif
404}
405TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
406
407static void
80ed523f 408Win32ErrorHandler(const char *module, const char *fmt, va_list ap)
8414a40c
VZ
409{
410#ifndef TIF_PLATFORM_CONSOLE
47493bc0
VZ
411 char *szTitle;
412 char *szTmp;
413 const char *szTitleText = "%s Error";
414 const char *szDefaultModule = "LIBTIFF";
415 const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
80ed523f
VZ
416 SIZE_T nBufSize = (strlen(szTmpModule) +
417 strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
418
419 if ((szTitle = (char*)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
8414a40c
VZ
420 return;
421 sprintf(szTitle, szTitleText, szTmpModule);
422 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
80ed523f 423 vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
8414a40c
VZ
424 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
425 LocalFree(szTitle);
426 return;
427#else
428 if (module != NULL)
429 fprintf(stderr, "%s: ", module);
430 vfprintf(stderr, fmt, ap);
431 fprintf(stderr, ".\n");
432#endif
433}
434TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
435
80ed523f
VZ
436#endif /* ndef _WIN32_WCE */
437
8414a40c 438/* vim: set ts=8 sts=8 sw=8 noet: */
80ed523f
VZ
439/*
440 * Local Variables:
441 * mode: c
442 * c-basic-offset: 8
443 * fill-column: 78
444 * End:
445 */