]>
Commit | Line | Data |
---|---|---|
b47c832e RR |
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 Macintosh-specific routines. | |
29 | * | |
30 | * These routines use only Toolbox and high-level File Manager traps. | |
31 | * They make no calls to the THINK C "unix" compatibility library. Also, | |
32 | * malloc is not used directly but it is still referenced internally by | |
33 | * the ANSI library in rare cases. Heap fragmentation by the malloc ring | |
34 | * buffer is therefore minimized. | |
35 | * | |
36 | * O_RDONLY and O_RDWR are treated identically here. The tif_mode flag is | |
37 | * checked in TIFFWriteCheck(). | |
38 | * | |
39 | * Create below fills in a blank creator signature and sets the file type | |
40 | * to 'TIFF'. It is much better for the application to do this by Create'ing | |
41 | * the file first and TIFFOpen'ing it later. | |
42 | */ | |
43 | ||
84e7f94c SC |
44 | #ifdef __MACH__ |
45 | #include <ansi_prefix.mach.h> | |
46 | #include <msl_c_version.h> | |
47 | #include <stdint.h> | |
48 | #undef WCHAR_MAX | |
49 | #include <machine/ansi.h> | |
50 | #endif | |
b47c832e | 51 | #include "tiffiop.h" |
84e7f94c | 52 | #include <MacErrors.h> |
b47c832e RR |
53 | #include <Files.h> |
54 | #include <Memory.h> | |
55 | ||
56 | #if defined(__PPCC__) || defined(__SC__) || defined(__MRC__) || defined(applec) | |
57 | #define CtoPstr c2pstr | |
58 | #endif | |
59 | ||
60 | static tsize_t | |
61 | _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size) | |
62 | { | |
63 | return (FSRead((short) fd, (long*) &size, (char*) buf) == noErr ? | |
64 | size : (tsize_t) -1); | |
65 | } | |
66 | ||
67 | static tsize_t | |
68 | _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size) | |
69 | { | |
70 | return (FSWrite((short) fd, (long*) &size, (char*) buf) == noErr ? | |
71 | size : (tsize_t) -1); | |
72 | } | |
73 | ||
74 | static toff_t | |
75 | _tiffSeekProc(thandle_t fd, toff_t off, int whence) | |
76 | { | |
77 | long fpos, size; | |
78 | ||
79 | if (GetEOF((short) fd, &size) != noErr) | |
80 | return EOF; | |
81 | (void) GetFPos((short) fd, &fpos); | |
82 | ||
83 | switch (whence) { | |
84 | case SEEK_CUR: | |
85 | if (off + fpos > size) | |
86 | SetEOF((short) fd, off + fpos); | |
87 | if (SetFPos((short) fd, fsFromMark, off) != noErr) | |
88 | return EOF; | |
89 | break; | |
90 | case SEEK_END: | |
91 | if (off > 0) | |
92 | SetEOF((short) fd, off + size); | |
93 | if (SetFPos((short) fd, fsFromStart, off + size) != noErr) | |
94 | return EOF; | |
95 | break; | |
96 | case SEEK_SET: | |
97 | if (off > size) | |
98 | SetEOF((short) fd, off); | |
99 | if (SetFPos((short) fd, fsFromStart, off) != noErr) | |
100 | return EOF; | |
101 | break; | |
102 | } | |
103 | ||
104 | return (toff_t)(GetFPos((short) fd, &fpos) == noErr ? fpos : EOF); | |
105 | } | |
106 | ||
107 | static int | |
108 | _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize) | |
109 | { | |
110 | return (0); | |
111 | } | |
112 | ||
113 | static void | |
114 | _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size) | |
115 | { | |
116 | } | |
117 | ||
118 | static int | |
119 | _tiffCloseProc(thandle_t fd) | |
120 | { | |
121 | return (FSClose((short) fd)); | |
122 | } | |
123 | ||
124 | static toff_t | |
125 | _tiffSizeProc(thandle_t fd) | |
126 | { | |
127 | long size; | |
128 | ||
129 | if (GetEOF((short) fd, &size) != noErr) { | |
130 | TIFFError("_tiffSizeProc", "%s: Cannot get file size"); | |
131 | return (-1L); | |
132 | } | |
133 | return ((toff_t) size); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Open a TIFF file descriptor for read/writing. | |
138 | */ | |
139 | TIFF* | |
140 | TIFFFdOpen(int fd, const char* name, const char* mode) | |
141 | { | |
142 | TIFF* tif; | |
143 | ||
144 | tif = TIFFClientOpen(name, mode, (thandle_t) fd, | |
145 | _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc, | |
146 | _tiffSizeProc, _tiffMapProc, _tiffUnmapProc); | |
147 | if (tif) | |
148 | tif->tif_fd = fd; | |
149 | return (tif); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Open a TIFF file for read/writing. | |
154 | */ | |
155 | TIFF* | |
156 | TIFFOpen(const char* name, const char* mode) | |
157 | { | |
158 | static const char module[] = "TIFFOpen"; | |
159 | Str255 pname; | |
160 | FInfo finfo; | |
161 | short fref; | |
162 | OSErr err; | |
163 | ||
164 | strcpy((char*) pname, name); | |
165 | CtoPstr((char*) pname); | |
166 | ||
167 | switch (_TIFFgetMode(mode, module)) { | |
168 | default: | |
169 | return ((TIFF*) 0); | |
170 | case O_RDWR | O_CREAT | O_TRUNC: | |
171 | if (GetFInfo(pname, 0, &finfo) == noErr) | |
172 | FSDelete(pname, 0); | |
173 | /* fall through */ | |
174 | case O_RDWR | O_CREAT: | |
175 | if ((err = GetFInfo(pname, 0, &finfo)) == fnfErr) { | |
176 | if (Create(pname, 0, ' ', 'TIFF') != noErr) | |
177 | goto badCreate; | |
178 | if (FSOpen(pname, 0, &fref) != noErr) | |
179 | goto badOpen; | |
180 | } else if (err == noErr) { | |
181 | if (FSOpen(pname, 0, &fref) != noErr) | |
182 | goto badOpen; | |
183 | } else | |
184 | goto badOpen; | |
185 | break; | |
186 | case O_RDONLY: | |
187 | case O_RDWR: | |
188 | if (FSOpen(pname, 0, &fref) != noErr) | |
189 | goto badOpen; | |
190 | break; | |
191 | } | |
192 | return (TIFFFdOpen((int) fref, name, mode)); | |
193 | badCreate: | |
194 | TIFFError(module, "%s: Cannot create", name); | |
195 | return ((TIFF*) 0); | |
196 | badOpen: | |
197 | TIFFError(module, "%s: Cannot open", name); | |
198 | return ((TIFF*) 0); | |
199 | } | |
200 | ||
201 | void | |
202 | _TIFFmemset(tdata_t p, int v, tsize_t c) | |
203 | { | |
204 | memset(p, v, (size_t) c); | |
205 | } | |
206 | ||
207 | void | |
208 | _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c) | |
209 | { | |
210 | memcpy(d, s, (size_t) c); | |
211 | } | |
212 | ||
213 | int | |
214 | _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c) | |
215 | { | |
216 | return (memcmp(p1, p2, (size_t) c)); | |
217 | } | |
218 | ||
219 | tdata_t | |
220 | _TIFFmalloc(tsize_t s) | |
221 | { | |
222 | return (NewPtr((size_t) s)); | |
223 | } | |
224 | ||
225 | void | |
226 | _TIFFfree(tdata_t p) | |
227 | { | |
228 | DisposePtr(p); | |
229 | } | |
230 | ||
231 | tdata_t | |
232 | _TIFFrealloc(tdata_t p, tsize_t s) | |
233 | { | |
234 | Ptr n = p; | |
235 | ||
236 | SetPtrSize(p, (size_t) s); | |
237 | if (MemError() && (n = NewPtr((size_t) s)) != NULL) { | |
238 | BlockMove(p, n, GetPtrSize(p)); | |
239 | DisposePtr(p); | |
240 | } | |
241 | return ((tdata_t) n); | |
242 | } | |
243 | ||
244 | static void | |
245 | appleWarningHandler(const char* module, const char* fmt, va_list ap) | |
246 | { | |
247 | if (module != NULL) | |
248 | fprintf(stderr, "%s: ", module); | |
249 | fprintf(stderr, "Warning, "); | |
250 | vfprintf(stderr, fmt, ap); | |
251 | fprintf(stderr, ".\n"); | |
252 | } | |
253 | TIFFErrorHandler _TIFFwarningHandler = appleWarningHandler; | |
254 | ||
255 | static void | |
256 | appleErrorHandler(const char* module, const char* fmt, va_list ap) | |
257 | { | |
258 | if (module != NULL) | |
259 | fprintf(stderr, "%s: ", module); | |
260 | vfprintf(stderr, fmt, ap); | |
261 | fprintf(stderr, ".\n"); | |
262 | } | |
263 | TIFFErrorHandler _TIFFerrorHandler = appleErrorHandler; |