]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_atari.c
guard against errors like in wxStringBase::AllocBeforeWrite code (1123226)
[wxWidgets.git] / src / tiff / tif_atari.c
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 ATARI-specific Routines.
29 */
30 #include "tiffiop.h"
31 #if defined(__TURBOC__)
32 #include <tos.h>
33 #include <stdio.h>
34 #else
35 #include <osbind.h>
36 #include <fcntl.h>
37 #endif
38
39 #ifndef O_ACCMODE
40 #define O_ACCMODE 3
41 #endif
42
43 #include <errno.h>
44
45 #define AEFILNF -33
46
47 static tsize_t
48 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
49 {
50 long r;
51
52 r = Fread((int) fd, size, buf);
53 if (r < 0) {
54 errno = (int)-r;
55 r = -1;
56 }
57 return r;
58 }
59
60 static tsize_t
61 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
62 {
63 long r;
64
65 r = Fwrite((int) fd, size, buf);
66 if (r < 0) {
67 errno = (int)-r;
68 r = -1;
69 }
70 return r;
71 }
72
73 static toff_t
74 _tiffSeekProc(thandle_t fd, off_t off, int whence)
75 {
76 char buf[256];
77 long current_off, expected_off, new_off;
78
79 if (whence == SEEK_END || off <= 0)
80 return Fseek(off, (int) fd, whence);
81 current_off = Fseek(0, (int) fd, SEEK_CUR); /* find out where we are */
82 if (whence == SEEK_SET)
83 expected_off = off;
84 else
85 expected_off = off + current_off;
86 new_off = Fseek(off, (int) fd, whence);
87 if (new_off == expected_off)
88 return new_off;
89 /* otherwise extend file -- zero filling the hole */
90 if (new_off < 0) /* error? */
91 new_off = Fseek(0, (int) fd, SEEK_END); /* go to eof */
92 _TIFFmemset(buf, 0, sizeof(buf));
93 while (expected_off > new_off) {
94 off = expected_off - new_off;
95 if (off > sizeof(buf))
96 off = sizeof(buf);
97 if ((current_off = Fwrite((int) fd, off, buf)) != off)
98 return (current_off > 0) ?
99 new_off + current_off : new_off;
100 new_off += off;
101 }
102 return new_off;
103 }
104
105 static int
106 _tiffCloseProc(thandle_t fd)
107 {
108 long r;
109
110 r = Fclose((int) fd);
111 if (r < 0) {
112 errno = (int)-r;
113 r = -1;
114 }
115 return (int)r;
116 }
117
118 static toff_t
119 _tiffSizeProc(thandle_t fd)
120 {
121 long pos, eof;
122
123 pos = Fseek(0, (int) fd, SEEK_CUR);
124 eof = Fseek(0, (int) fd, SEEK_END);
125 Fseek(pos, (int) fd, SEEK_SET);
126 return eof;
127 }
128
129 static int
130 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
131 {
132 return (0);
133 }
134
135 static void
136 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
137 {
138 }
139
140 /*
141 * Open a TIFF file descriptor for read/writing.
142 */
143 TIFF*
144 TIFFFdOpen(int fd, const char* name, const char* mode)
145 {
146 TIFF* tif;
147
148 tif = TIFFClientOpen(name, mode,
149 (thandle_t) fd,
150 _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
151 _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
152 if (tif)
153 tif->tif_fd = fd;
154 return (tif);
155 }
156
157 /*
158 * Open a TIFF file for read/writing.
159 */
160 TIFF*
161 TIFFOpen(const char* name, const char* mode)
162 {
163 static const char module[] = "TIFFOpen";
164 int m;
165 long fd;
166
167 m = _TIFFgetMode(mode, module);
168 if (m == -1)
169 return ((TIFF*)0);
170 if (m & O_TRUNC) {
171 fd = Fcreate(name, 0);
172 } else {
173 fd = Fopen(name, m & O_ACCMODE);
174 if (fd == AEFILNF && m & O_CREAT)
175 fd = Fcreate(name, 0);
176 }
177 if (fd < 0)
178 errno = (int)fd;
179 if (fd < 0) {
180 TIFFError(module, "%s: Cannot open", name);
181 return ((TIFF*)0);
182 }
183 return (TIFFFdOpen(fd, name, mode));
184 }
185
186 #include <stdlib.h>
187
188 tdata_t
189 _TIFFmalloc(tsize_t s)
190 {
191 return (malloc((size_t) s));
192 }
193
194 void
195 _TIFFfree(tdata_t p)
196 {
197 free(p);
198 }
199
200 tdata_t
201 _TIFFrealloc(tdata_t p, tsize_t s)
202 {
203 return (realloc(p, (size_t) s));
204 }
205
206 void
207 _TIFFmemset(tdata_t p, int v, size_t c)
208 {
209 memset(p, v, (size_t) c);
210 }
211
212 void
213 _TIFFmemcpy(tdata_t d, const tdata_t s, size_t c)
214 {
215 memcpy(d, s, (size_t) c);
216 }
217
218 int
219 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
220 {
221 return (memcmp(p1, p2, (size_t) c));
222 }
223
224 static void
225 atariWarningHandler(const char* module, const char* fmt, va_list ap)
226 {
227 if (module != NULL)
228 fprintf(stderr, "%s: ", module);
229 fprintf(stderr, "Warning, ");
230 vfprintf(stderr, fmt, ap);
231 fprintf(stderr, ".\n");
232 }
233 TIFFErrorHandler _TIFFwarningHandler = atariWarningHandler;
234
235 static void
236 atariErrorHandler(const char* module, const char* fmt, va_list ap)
237 {
238 if (module != NULL)
239 fprintf(stderr, "%s: ", module);
240 vfprintf(stderr, fmt, ap);
241 fprintf(stderr, ".\n");
242 }
243 TIFFErrorHandler _TIFFerrorHandler = atariErrorHandler;