]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_win32.c
Add missing WXK constants for the control keys
[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
8414a40c
VZ
35static tsize_t
36_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
37{
38 DWORD dwSizeRead;
39 if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
40 return(0);
41 return ((tsize_t) dwSizeRead);
42}
43
44static tsize_t
45_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
46{
47 DWORD dwSizeWritten;
48 if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
49 return(0);
50 return ((tsize_t) dwSizeWritten);
51}
52
53static toff_t
54_tiffSeekProc(thandle_t fd, toff_t off, int whence)
55{
56 DWORD dwMoveMethod, dwMoveHigh;
57
58 /* we use this as a special code, so avoid accepting it */
59 if( off == 0xFFFFFFFF )
60 return 0xFFFFFFFF;
61
62 switch(whence)
63 {
64 case SEEK_SET:
65 dwMoveMethod = FILE_BEGIN;
66 break;
67 case SEEK_CUR:
68 dwMoveMethod = FILE_CURRENT;
69 break;
70 case SEEK_END:
71 dwMoveMethod = FILE_END;
72 break;
73 default:
74 dwMoveMethod = FILE_BEGIN;
75 break;
76 }
77 dwMoveHigh = 0;
78 return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
79 dwMoveMethod));
80}
81
82static int
83_tiffCloseProc(thandle_t fd)
84{
85 return (CloseHandle(fd) ? 0 : -1);
86}
87
88static toff_t
89_tiffSizeProc(thandle_t fd)
90{
91 return ((toff_t)GetFileSize(fd, NULL));
92}
93
94#ifdef __BORLANDC__
95#pragma argsused
96#endif
97static int
98_tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
99{
100 return (0);
101}
102
103/*
104 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
105 *
106 * Windows uses both a handle and a pointer for file mapping,
107 * but according to the SDK documentation and Richter's book
108 * "Advanced Windows Programming" it is safe to free the handle
109 * after obtaining the file mapping pointer
110 *
111 * This removes a nasty OS dependency and cures a problem
112 * with Visual C++ 5.0
113 */
114static int
115_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
116{
117 toff_t size;
118 HANDLE hMapFile;
119
120 if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
121 return (0);
122 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
123 if (hMapFile == NULL)
124 return (0);
125 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
126 CloseHandle(hMapFile);
127 if (*pbase == NULL)
128 return (0);
129 *psize = size;
130 return(1);
131}
132
133#ifdef __BORLANDC__
134#pragma argsused
135#endif
136static void
137_tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
138{
139}
140
141static void
142_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
143{
144 UnmapViewOfFile(base);
145}
146
147/*
148 * Open a TIFF file descriptor for read/writing.
149 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
150 * string, which forces the file to be opened unmapped.
151 */
152TIFF*
153TIFFFdOpen(int ifd, const char* name, const char* mode)
154{
155 TIFF* tif;
156 BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
157
158 tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
159 _tiffReadProc, _tiffWriteProc,
160 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
161 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
162 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
163 if (tif)
164 tif->tif_fd = ifd;
165 return (tif);
166}
167
168/*
169 * Open a TIFF file for read/writing.
170 */
171TIFF*
172TIFFOpen(const char* name, const char* mode)
173{
174 static const char module[] = "TIFFOpen";
175 thandle_t fd;
176 int m;
177 DWORD dwMode;
178 TIFF* tif;
179
180 m = _TIFFgetMode(mode, module);
181
182 switch(m)
183 {
184 case O_RDONLY:
185 dwMode = OPEN_EXISTING;
186 break;
187 case O_RDWR:
188 dwMode = OPEN_ALWAYS;
189 break;
190 case O_RDWR|O_CREAT:
191 dwMode = OPEN_ALWAYS;
192 break;
193 case O_RDWR|O_TRUNC:
194 dwMode = CREATE_ALWAYS;
195 break;
196 case O_RDWR|O_CREAT|O_TRUNC:
197 dwMode = CREATE_ALWAYS;
198 break;
199 default:
200 return ((TIFF*)0);
201 }
202 fd = (thandle_t)CreateFileA(name,
203 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
204 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
205 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
206 NULL);
207 if (fd == INVALID_HANDLE_VALUE) {
208 TIFFErrorExt(0, module, "%s: Cannot open", name);
209 return ((TIFF *)0);
210 }
211
212 tif = TIFFFdOpen((int)fd, name, mode);
213 if(!tif)
214 CloseHandle(fd);
215 return tif;
216}
217
218/*
219 * Open a TIFF file with a Unicode filename, for read/writing.
220 */
221TIFF*
222TIFFOpenW(const wchar_t* name, const char* mode)
223{
224 static const char module[] = "TIFFOpenW";
225 thandle_t fd;
226 int m;
227 DWORD dwMode;
228 int mbsize;
229 char *mbname;
230 TIFF *tif;
231
232 m = _TIFFgetMode(mode, module);
233
234 switch(m) {
235 case O_RDONLY: dwMode = OPEN_EXISTING; break;
236 case O_RDWR: dwMode = OPEN_ALWAYS; break;
237 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
238 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
239 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
240 default: return ((TIFF*)0);
241 }
242
243 fd = (thandle_t)CreateFileW(name,
244 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
245 FILE_SHARE_READ, NULL, dwMode,
246 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
247 NULL);
248 if (fd == INVALID_HANDLE_VALUE) {
249 TIFFErrorExt(0, module, "%S: Cannot open", name);
250 return ((TIFF *)0);
251 }
252
253 mbname = NULL;
254 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
255 if (mbsize > 0) {
256 mbname = (char *)_TIFFmalloc(mbsize);
257 if (!mbname) {
258 TIFFErrorExt(0, module,
259 "Can't allocate space for filename conversion buffer");
260 return ((TIFF*)0);
261 }
262
263 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
264 NULL, NULL);
265 }
266
267 tif = TIFFFdOpen((int)fd,
268 (mbname != NULL) ? mbname : "<unknown>", mode);
269 if(!tif)
270 CloseHandle(fd);
271
272 _TIFFfree(mbname);
273
274 return tif;
275}
276
277tdata_t
278_TIFFmalloc(tsize_t s)
279{
280 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
281}
282
283void
284_TIFFfree(tdata_t p)
285{
286 GlobalFree(p);
287 return;
288}
289
290tdata_t
291_TIFFrealloc(tdata_t p, tsize_t s)
292{
293 void* pvTmp;
294 tsize_t old;
295
296 if(p == NULL)
297 return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
298
299 old = GlobalSize(p);
300
301 if (old>=s) {
302 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
303 CopyMemory(pvTmp, p, s);
304 GlobalFree(p);
305 }
306 } else {
307 if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
308 CopyMemory(pvTmp, p, old);
309 GlobalFree(p);
310 }
311 }
312 return ((tdata_t)pvTmp);
313}
314
315void
316_TIFFmemset(void* p, int v, tsize_t c)
317{
318 FillMemory(p, c, (BYTE)v);
319}
320
321void
322_TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
323{
324 CopyMemory(d, s, c);
325}
326
327int
328_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
329{
330 register const BYTE *pb1 = (const BYTE *) p1;
331 register const BYTE *pb2 = (const BYTE *) p2;
332 register DWORD dwTmp = c;
333 register int iTmp;
334 for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
335 ;
336 return (iTmp);
337}
338
339static void
340Win32WarningHandler(const char* module, const char* fmt, va_list ap)
341{
342#ifndef TIF_PLATFORM_CONSOLE
47493bc0
VZ
343 char *szTitle;
344 char *szTmp;
345 const char *szTitleText = "%s Warning";
346 const char *szDefaultModule = "LIBTIFF";
347 const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
348 if ((szTitle = (char *)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
8414a40c
VZ
349 strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
350 return;
351 sprintf(szTitle, szTitleText, szTmpModule);
352 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
353 vsprintf(szTmp, fmt, ap);
354 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
355 LocalFree(szTitle);
356 return;
357#else
358 if (module != NULL)
359 fprintf(stderr, "%s: ", module);
360 fprintf(stderr, "Warning, ");
361 vfprintf(stderr, fmt, ap);
362 fprintf(stderr, ".\n");
363#endif
364}
365TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
366
367static void
368Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
369{
370#ifndef TIF_PLATFORM_CONSOLE
47493bc0
VZ
371 char *szTitle;
372 char *szTmp;
373 const char *szTitleText = "%s Error";
374 const char *szDefaultModule = "LIBTIFF";
375 const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
376 if ((szTitle = (char *)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
8414a40c
VZ
377 strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
378 return;
379 sprintf(szTitle, szTitleText, szTmpModule);
380 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
381 vsprintf(szTmp, fmt, ap);
382 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
383 LocalFree(szTitle);
384 return;
385#else
386 if (module != NULL)
387 fprintf(stderr, "%s: ", module);
388 vfprintf(stderr, fmt, ap);
389 fprintf(stderr, ".\n");
390#endif
391}
392TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
393
394/* vim: set ts=8 sts=8 sw=8 noet: */