opj_thread_pool_setup(): fix infinite waiting if a thread creation failed
[openjpeg.git] / thirdparty / libtiff / tif_win32.c
1 /* $Id: tif_win32.c,v 1.41 2015-08-23 20:12:44 bfriesen Exp $ */
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
32 /*
33   CreateFileA/CreateFileW return type 'HANDLE'.
34
35   thandle_t is declared like
36
37     DECLARE_HANDLE(thandle_t);
38
39   in tiffio.h.
40
41   Windows (from winnt.h) DECLARE_HANDLE logic looks like
42
43   #ifdef STRICT
44     typedef void *HANDLE;
45   #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
46   #else
47     typedef PVOID HANDLE;
48   #define DECLARE_HANDLE(name) typedef HANDLE name
49   #endif
50
51   See http://bugzilla.maptools.org/show_bug.cgi?id=1941 for problems in WIN64
52   builds resulting from this.  Unfortunately, the proposed patch was lost.
53
54 */
55   
56 #include "tiffiop.h"
57
58 #include <windows.h>
59
60 static tmsize_t
61 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
62 {
63         /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
64          * 32bit sizes, so we loop through the data in suitable 32bit sized
65          * chunks */
66         uint8* ma;
67         uint64 mb;
68         DWORD n;
69         DWORD o;
70         tmsize_t p;
71         ma=(uint8*)buf;
72         mb=size;
73         p=0;
74         while (mb>0)
75         {
76                 n=0x80000000UL;
77                 if ((uint64)n>mb)
78                         n=(DWORD)mb;
79                 if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
80                         return(0);
81                 ma+=o;
82                 mb-=o;
83                 p+=o;
84                 if (o!=n)
85                         break;
86         }
87         return(p);
88 }
89
90 static tmsize_t
91 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
92 {
93         /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
94          * 32bit sizes, so we loop through the data in suitable 32bit sized
95          * chunks */
96         uint8* ma;
97         uint64 mb;
98         DWORD n;
99         DWORD o;
100         tmsize_t p;
101         ma=(uint8*)buf;
102         mb=size;
103         p=0;
104         while (mb>0)
105         {
106                 n=0x80000000UL;
107                 if ((uint64)n>mb)
108                         n=(DWORD)mb;
109                 if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
110                         return(0);
111                 ma+=o;
112                 mb-=o;
113                 p+=o;
114                 if (o!=n)
115                         break;
116         }
117         return(p);
118 }
119
120 static uint64
121 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
122 {
123         LARGE_INTEGER offli;
124         DWORD dwMoveMethod;
125         offli.QuadPart = off;
126         switch(whence)
127         {
128                 case SEEK_SET:
129                         dwMoveMethod = FILE_BEGIN;
130                         break;
131                 case SEEK_CUR:
132                         dwMoveMethod = FILE_CURRENT;
133                         break;
134                 case SEEK_END:
135                         dwMoveMethod = FILE_END;
136                         break;
137                 default:
138                         dwMoveMethod = FILE_BEGIN;
139                         break;
140         }
141         offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
142         if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
143                 offli.QuadPart=0;
144         return(offli.QuadPart);
145 }
146
147 static int
148 _tiffCloseProc(thandle_t fd)
149 {
150         return (CloseHandle(fd) ? 0 : -1);
151 }
152
153 static uint64
154 _tiffSizeProc(thandle_t fd)
155 {
156         ULARGE_INTEGER m;
157         m.LowPart=GetFileSize(fd,&m.HighPart);
158         return(m.QuadPart);
159 }
160
161 static int
162 _tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
163 {
164         (void) fd;
165         (void) pbase;
166         (void) psize;
167         return (0);
168 }
169
170 /*
171  * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
172  *
173  * Windows uses both a handle and a pointer for file mapping,
174  * but according to the SDK documentation and Richter's book
175  * "Advanced Windows Programming" it is safe to free the handle
176  * after obtaining the file mapping pointer
177  *
178  * This removes a nasty OS dependency and cures a problem
179  * with Visual C++ 5.0
180  */
181 static int
182 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
183 {
184         uint64 size;
185         tmsize_t sizem;
186         HANDLE hMapFile;
187
188         size = _tiffSizeProc(fd);
189         sizem = (tmsize_t)size;
190         if ((uint64)sizem!=size)
191                 return (0);
192
193         /* By passing in 0 for the maximum file size, it specifies that we
194            create a file mapping object for the full file size. */
195         hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
196         if (hMapFile == NULL)
197                 return (0);
198         *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
199         CloseHandle(hMapFile);
200         if (*pbase == NULL)
201                 return (0);
202         *psize = size;
203         return(1);
204 }
205
206 static void
207 _tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
208 {
209         (void) fd;
210         (void) base;
211         (void) size;
212 }
213
214 static void
215 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
216 {
217         (void) fd;
218         (void) size;
219         UnmapViewOfFile(base);
220 }
221
222 /*
223  * Open a TIFF file descriptor for read/writing.
224  * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
225  * string, which forces the file to be opened unmapped.
226  */
227 TIFF*
228 TIFFFdOpen(int ifd, const char* name, const char* mode)
229 {
230         TIFF* tif;
231         int fSuppressMap;
232         int m;
233         fSuppressMap=0;
234         for (m=0; mode[m]!=0; m++)
235         {
236                 if (mode[m]=='u')
237                 {
238                         fSuppressMap=1;
239                         break;
240                 }
241         }
242         tif = TIFFClientOpen(name, mode, (thandle_t)ifd, /* FIXME: WIN64 cast to pointer warning */
243                         _tiffReadProc, _tiffWriteProc,
244                         _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
245                         fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
246                         fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
247         if (tif)
248                 tif->tif_fd = ifd;
249         return (tif);
250 }
251
252 #ifndef _WIN32_WCE
253
254 /*
255  * Open a TIFF file for read/writing.
256  */
257 TIFF*
258 TIFFOpen(const char* name, const char* mode)
259 {
260         static const char module[] = "TIFFOpen";
261         thandle_t fd;
262         int m;
263         DWORD dwMode;
264         TIFF* tif;
265
266         m = _TIFFgetMode(mode, module);
267
268         switch(m) {
269                 case O_RDONLY:                  dwMode = OPEN_EXISTING; break;
270                 case O_RDWR:                    dwMode = OPEN_ALWAYS;   break;
271                 case O_RDWR|O_CREAT:            dwMode = OPEN_ALWAYS;   break;
272                 case O_RDWR|O_TRUNC:            dwMode = CREATE_ALWAYS; break;
273                 case O_RDWR|O_CREAT|O_TRUNC:    dwMode = CREATE_ALWAYS; break;
274                 default:                        return ((TIFF*)0);
275         }
276         
277         fd = (thandle_t)CreateFileA(name,
278                 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
279                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
280                 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
281                 NULL);
282         if (fd == INVALID_HANDLE_VALUE) {
283                 TIFFErrorExt(0, module, "%s: Cannot open", name);
284                 return ((TIFF *)0);
285         }
286
287         tif = TIFFFdOpen((int)fd, name, mode);   /* FIXME: WIN64 cast from pointer to int warning */
288         if(!tif)
289                 CloseHandle(fd);
290         return tif;
291 }
292
293 /*
294  * Open a TIFF file with a Unicode filename, for read/writing.
295  */
296 TIFF*
297 TIFFOpenW(const wchar_t* name, const char* mode)
298 {
299         static const char module[] = "TIFFOpenW";
300         thandle_t fd;
301         int m;
302         DWORD dwMode;
303         int mbsize;
304         char *mbname;
305         TIFF *tif;
306
307         m = _TIFFgetMode(mode, module);
308
309         switch(m) {
310                 case O_RDONLY:                  dwMode = OPEN_EXISTING; break;
311                 case O_RDWR:                    dwMode = OPEN_ALWAYS;   break;
312                 case O_RDWR|O_CREAT:            dwMode = OPEN_ALWAYS;   break;
313                 case O_RDWR|O_TRUNC:            dwMode = CREATE_ALWAYS; break;
314                 case O_RDWR|O_CREAT|O_TRUNC:    dwMode = CREATE_ALWAYS; break;
315                 default:                        return ((TIFF*)0);
316         }
317
318         fd = (thandle_t)CreateFileW(name,
319                 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
320                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
321                 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
322                 NULL);
323         if (fd == INVALID_HANDLE_VALUE) {
324                 TIFFErrorExt(0, module, "%S: Cannot open", name);
325                 return ((TIFF *)0);
326         }
327
328         mbname = NULL;
329         mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
330         if (mbsize > 0) {
331                 mbname = (char *)_TIFFmalloc(mbsize);
332                 if (!mbname) {
333                         TIFFErrorExt(0, module,
334                         "Can't allocate space for filename conversion buffer");
335                         return ((TIFF*)0);
336                 }
337
338                 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
339                                     NULL, NULL);
340         }
341
342         tif = TIFFFdOpen((int)fd,    /* FIXME: WIN64 cast from pointer to int warning */
343                          (mbname != NULL) ? mbname : "<unknown>", mode);
344         if(!tif)
345                 CloseHandle(fd);
346
347         _TIFFfree(mbname);
348
349         return tif;
350 }
351
352 #endif /* ndef _WIN32_WCE */
353
354 void*
355 _TIFFmalloc(tmsize_t s)
356 {
357         if (s == 0)
358                 return ((void *) NULL);
359
360         return (malloc((size_t) s));
361 }
362
363 void
364 _TIFFfree(void* p)
365 {
366         free(p);
367 }
368
369 void*
370 _TIFFrealloc(void* p, tmsize_t s)
371 {
372         return (realloc(p, (size_t) s));
373 }
374
375 void
376 _TIFFmemset(void* p, int v, tmsize_t c)
377 {
378         memset(p, v, (size_t) c);
379 }
380
381 void
382 _TIFFmemcpy(void* d, const void* s, tmsize_t c)
383 {
384         memcpy(d, s, (size_t) c);
385 }
386
387 int
388 _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
389 {
390         return (memcmp(p1, p2, (size_t) c));
391 }
392
393 #ifndef _WIN32_WCE
394
395 #if (_MSC_VER < 1500)
396 #  define vsnprintf _vsnprintf
397 #endif
398
399 static void
400 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
401 {
402 #ifndef TIF_PLATFORM_CONSOLE
403         LPTSTR szTitle;
404         LPTSTR szTmp;
405         LPCTSTR szTitleText = "%s Warning";
406         LPCTSTR szDefaultModule = "LIBTIFF";
407         LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
408         SIZE_T nBufSize = (strlen(szTmpModule) +
409                         strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
410
411         if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
412                 return;
413         sprintf(szTitle, szTitleText, szTmpModule);
414         szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
415         vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
416         MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
417         LocalFree(szTitle);
418
419         return;
420 #else
421         if (module != NULL)
422                 fprintf(stderr, "%s: ", module);
423         fprintf(stderr, "Warning, ");
424         vfprintf(stderr, fmt, ap);
425         fprintf(stderr, ".\n");
426 #endif        
427 }
428 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
429
430 static void
431 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
432 {
433 #ifndef TIF_PLATFORM_CONSOLE
434         LPTSTR szTitle;
435         LPTSTR szTmp;
436         LPCTSTR szTitleText = "%s Error";
437         LPCTSTR szDefaultModule = "LIBTIFF";
438         LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
439         SIZE_T nBufSize = (strlen(szTmpModule) +
440                         strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
441
442         if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
443                 return;
444         sprintf(szTitle, szTitleText, szTmpModule);
445         szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
446         vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
447         MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
448         LocalFree(szTitle);
449         return;
450 #else
451         if (module != NULL)
452                 fprintf(stderr, "%s: ", module);
453         vfprintf(stderr, fmt, ap);
454         fprintf(stderr, ".\n");
455 #endif        
456 }
457 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
458
459 #endif /* ndef _WIN32_WCE */
460
461 /* vim: set ts=8 sts=8 sw=8 noet: */
462 /*
463  * Local Variables:
464  * mode: c
465  * c-basic-offset: 8
466  * fill-column: 78
467  * End:
468  */