1 /* Header */
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 Windows 3.x-specific Routines.
29  */
30 #include "tiffiop.h"
31 #if defined(__WATCOMC__) || defined(__BORLANDC__) || defined(_MSC_VER)
32 #include <io.h>   /* for open, close, etc. function prototypes */
33 #endif
34 
35 #include <windows.h>
36 #include <windowsx.h>
37 #include <memory.h>
38 
39 static tsize_t
_tiffReadProc(thandle_t fd,tdata_t buf,tsize_t size)40 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
41 {
42   return (_hread(fd, buf, size));
43 }
44 
45 static tsize_t
_tiffWriteProc(thandle_t fd,tdata_t buf,tsize_t size)46 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
47 {
48   return (_hwrite(fd, buf, size));
49 }
50 
51 static toff_t
_tiffSeekProc(thandle_t fd,toff_t off,int whence)52 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
53 {
54   return (_llseek(fd, (off_t) off, whence));
55 }
56 
57 static int
_tiffCloseProc(thandle_t fd)58 _tiffCloseProc(thandle_t fd)
59 {
60   return (_lclose(fd));
61 }
62 
63 #include <sys/stat.h>
64 
65 static toff_t
_tiffSizeProc(thandle_t fd)66 _tiffSizeProc(thandle_t fd)
67 {
68   struct stat sb;
69   return (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
70 }
71 
72 static int
_tiffMapProc(thandle_t fd,tdata_t * pbase,toff_t * psize)73 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
74 {
75   return (0);
76 }
77 
78 static void
_tiffUnmapProc(thandle_t fd,tdata_t base,toff_t size)79 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
80 {
81 }
82 
83 /*
84  * Open a TIFF file descriptor for read/writing.
85  */
86 TIFF*
TIFFFdOpen(int fd,const char * name,const char * mode)87 TIFFFdOpen(int fd, const char* name, const char* mode)
88 {
89   TIFF* tif;
90 
91   tif = TIFFClientOpen(name, mode,
92       (thandle_t) fd,
93       _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
94       _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
95   if (tif)
96     tif->tif_fd = fd;
97   return (tif);
98 }
99 
100 /*
101  * Open a TIFF file for read/writing.
102  */
103 TIFF*
TIFFOpen(const char * name,const char * mode)104 TIFFOpen(const char* name, const char* mode)
105 {
106   static const char module[] = "TIFFOpen";
107   int m, fd;
108   OFSTRUCT of;
109   int mm = 0;
110 
111   m = _TIFFgetMode(mode, module);
112   if (m == -1)
113     return ((TIFF*)0);
114   if (m & O_CREAT) {
115     if ((m & O_TRUNC) || OpenFile(name, &of, OF_EXIST) != HFILE_ERROR)
116       mm |= OF_CREATE;
117   }
118   if (m & O_WRONLY)
119     mm |= OF_WRITE;
120   if (m & O_RDWR)
121     mm |= OF_READWRITE;
122   fd = OpenFile(name, &of, mm);
123   if (fd < 0) {
124     TIFFErrorExt(0, module, "%s: Cannot open", name);
125     return ((TIFF*)0);
126   }
127   return (TIFFFdOpen(fd, name, mode));
128 }
129 
130 tdata_t
_TIFFmalloc(tsize_t s)131 _TIFFmalloc(tsize_t s)
132 {
133   return (tdata_t) GlobalAllocPtr(GHND, (DWORD) s);
134 }
135 
136 void
_TIFFfree(tdata_t p)137 _TIFFfree(tdata_t p)
138 {
139   GlobalFreePtr(p);
140 }
141 
142 tdata_t
_TIFFrealloc(tdata_t p,tsize_t s)143 _TIFFrealloc(tdata_t p, tsize_t s)
144 {
145   return (tdata_t) GlobalReAllocPtr(p, (DWORD) s, GHND);
146 }
147 
148 void
_TIFFmemset(tdata_t p,int v,tsize_t c)149 _TIFFmemset(tdata_t p, int v, tsize_t c)
150 {
151   char* pp = (char*) p;
152 
153   while (c > 0) {
154     tsize_t chunk = 0x10000 - ((uint32) pp & 0xffff);/* What's left in segment */
155     if (chunk > 0xff00)       /* No more than 0xff00 */
156       chunk = 0xff00;
157     if (chunk > c)          /* No more than needed */
158       chunk = c;
159     memset(pp, v, chunk);
160     pp = (char*) (chunk + (char huge*) pp);
161     c -= chunk;
162   }
163 }
164 
165 void
_TIFFmemcpy(tdata_t d,const tdata_t s,tsize_t c)166 _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
167 {
168   if (c > 0xFFFF)
169     hmemcpy((void _huge*) d, (void _huge*) s, c);
170   else
171     (void) memcpy(d, s, (size_t) c);
172 }
173 
174 int
_TIFFmemcmp(const tdata_t d,const tdata_t s,tsize_t c)175 _TIFFmemcmp(const tdata_t d, const tdata_t s, tsize_t c)
176 {
177   char* dd = (char*) d;
178   char* ss = (char*) s;
179   tsize_t chunks, chunkd, chunk;
180   int result;
181 
182   while (c > 0) {
183     chunks = 0x10000 - ((uint32) ss & 0xffff);  /* What's left in segment */
184     chunkd = 0x10000 - ((uint32) dd & 0xffff);  /* What's left in segment */
185     chunk = c;          /* Get the largest of     */
186     if (chunk > chunks)       /*   c, chunks, chunkd,   */
187       chunk = chunks;       /*   0xff00               */
188     if (chunk > chunkd)
189       chunk = chunkd;
190     if (chunk > 0xff00)
191       chunk = 0xff00;
192     result = memcmp(dd, ss, chunk);
193     if (result != 0)
194       return (result);
195     dd = (char*) (chunk + (char huge*) dd);
196     ss = (char*) (chunk + (char huge*) ss);
197     c -= chunk;
198   }
199   return (0);
200 }
201 
202 static void
win3WarningHandler(const char * module,const char * fmt,va_list ap)203 win3WarningHandler(const char* module, const char* fmt, va_list ap)
204 {
205   char e[512] = { '\0' };
206   if (module != NULL)
207     strcat(strcpy(e, module), ":");
208   vsprintf(e+strlen(e), fmt, ap);
209   strcat(e, ".");
210   MessageBox(GetActiveWindow(), e, "LibTIFF Warning",
211       MB_OK|MB_ICONEXCLAMATION);
212 }
213 TIFFErrorHandler _TIFFwarningHandler = win3WarningHandler;
214 
215 static void
win3ErrorHandler(const char * module,const char * fmt,va_list ap)216 win3ErrorHandler(const char* module, const char* fmt, va_list ap)
217 {
218   char e[512] = { '\0' };
219   if (module != NULL)
220     strcat(strcpy(e, module), ":");
221   vsprintf(e+strlen(e), fmt, ap);
222   strcat(e, ".");
223   MessageBox(GetActiveWindow(), e, "LibTIFF Error", MB_OK|MB_ICONSTOP);
224 }
225 TIFFErrorHandler _TIFFerrorHandler = win3ErrorHandler;
226