4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
28 * TIFF Library RISC OS specific Routines.
29 * Developed out of the Unix version.
30 * Peter Greenham, May 1995
37 Low-level file handling
38 ~~~~~~~~~~~~~~~~~~~~~~~
39 The functions in osfcn.h are unavailable when compiling under C, as it's a
40 C++ header. Therefore they have been implemented here.
42 Now, why have I done it this way?
44 The definitive API library for RISC OS is Jonathan Coxhead's OSLib, which
45 uses heavily optimised ARM assembler or even plain inline SWI calls for
46 maximum performance and minimum runtime size. However, I don't want to make
47 LIBTIFF need that to survive. Therefore I have also emulated the functions
48 using macros to _swi() and _swix() defined in the swis.h header, and
49 borrowing types from kernel.h, which is less efficient but doesn't need any
50 third-party libraries.
62 /* OSLIB EMULATION STARTS */
67 /* From oslib:types.h */
68 typedef unsigned int bits
;
69 typedef unsigned char byte
;
84 typedef _kernel_oserror os_error
;
87 /* From oslib:osfile.h */
91 /* From oslib:osgbpb.h */
95 #define OSGBPB_Write 0x2
97 #define OSGBPB_Read 0x4
99 extern os_error
*xosgbpb_write (os_f file
,
103 extern int osgbpb_write (os_f file
,
107 #define xosgbpb_write(file, data, size, unwritten) \
108 (os_error*) _swix(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_IN(4)|_OUT(3), \
115 #define osgbpb_write(file, data, size) \
116 _swi(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_RETURN(3), \
122 extern os_error
*xosgbpb_read (os_f file
,
126 extern int osgbpb_read (os_f file
,
130 #define xosgbpb_read(file, buffer, size, unread) \
131 (os_error*) _swix(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_OUT(3), \
138 #define osgbpb_read(file, buffer, size) \
139 _swi(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_RETURN(3), \
145 /* From oslib:osfind.h */
149 #define OSFind_Openin 0x40
150 #undef OSFind_Openout
151 #define OSFind_Openout 0x80
153 #define OSFind_Openup 0xC0
155 #define OSFind_Close 0x0
157 #define xosfind_open(reason, file_name, path, file) \
158 (os_error*) _swix(OS_Find, _IN(0)|_IN(1)|_IN(2)|_OUT(0), \
159 reason, file_name, path, file)
161 #define osfind_open(reason, file_name, path) \
162 (os_f) _swi(OS_Find, _IN(0)|_IN(1)|_IN(2)|_RETURN(0), \
163 reason, file_name, path)
165 extern os_error
*xosfind_openin (bits flags
,
169 extern os_f
osfind_openin (bits flags
,
173 #define xosfind_openin(flags, file_name, path, file) \
174 xosfind_open(flags | OSFind_Openin, file_name, path, file)
176 #define osfind_openin(flags, file_name, path) \
177 osfind_open(flags | OSFind_Openin, file_name, path)
179 extern os_error
*xosfind_openout (bits flags
,
183 extern os_f
osfind_openout (bits flags
,
187 #define xosfind_openout(flags, file_name, path, file) \
188 xosfind_open(flags | OSFind_Openout, file_name, path, file)
190 #define osfind_openout(flags, file_name, path) \
191 osfind_open(flags | OSFind_Openout, file_name, path)
193 extern os_error
*xosfind_openup (bits flags
,
197 extern os_f
osfind_openup (bits flags
,
201 #define xosfind_openup(flags, file_name, path, file) \
202 xosfind_open(flags | OSFind_Openup, file_name, path, file)
204 #define osfind_openup(flags, file_name, path) \
205 osfind_open(flags | OSFind_Openup, file_name, path)
207 extern os_error
*xosfind_close (os_f file
);
208 extern void osfind_close (os_f file
);
210 #define xosfind_close(file) \
211 (os_error*) _swix(OS_Find, _IN(0)|_IN(1), \
215 #define osfind_close(file) \
216 (void) _swi(OS_Find, _IN(0)|_IN(1), \
220 /* From oslib:osargs.h */
223 #undef OSArgs_ReadPtr
224 #define OSArgs_ReadPtr 0x0
226 #define OSArgs_SetPtr 0x1
227 #undef OSArgs_ReadExt
228 #define OSArgs_ReadExt 0x2
230 extern os_error
*xosargs_read_ptr (os_f file
,
232 extern int osargs_read_ptr (os_f file
);
234 #define xosargs_read_ptr(file, ptr) \
235 (os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_OUT(2), \
240 #define osargs_read_ptr(file) \
241 _swi(OS_Args, _IN(0)|_IN(1)|_RETURN(2), \
245 extern os_error
*xosargs_set_ptr (os_f file
,
247 extern void osargs_set_ptr (os_f file
,
250 #define xosargs_set_ptr(file, ptr) \
251 (os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_IN(2), \
256 #define osargs_set_ptr(file, ptr) \
257 (void) _swi(OS_Args, _IN(0)|_IN(1)|_IN(2), \
262 extern os_error
*xosargs_read_ext (os_f file
,
264 extern int osargs_read_ext (os_f file
);
266 #define xosargs_read_ext(file, ext) \
267 (os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_OUT(2), \
272 #define osargs_read_ext(file) \
273 _swi(OS_Args, _IN(0)|_IN(1)|_RETURN(2), \
277 /* OSLIB EMULATION ENDS */
282 /* Will be set or not during tiffcomp.h */
283 /* You get this to compile under C++? Please say how! */
285 extern int open(const char* name
, int flags
, int mode
)
287 /* From what I can tell, should return <0 for failure */
288 os_error
* e
= (os_error
*) 1; /* Cheeky way to use a pointer eh? :-) */
289 os_f file
= (os_f
) -1;
297 e
= xosfind_openin(SKIP
, name
, SKIP
, &file
);
302 case O_RDWR
|O_CREAT
|O_TRUNC
:
304 e
= xosfind_openout(SKIP
, name
, SKIP
, &file
);
309 e
= xosfind_openup(SKIP
, name
, SKIP
, &file
);
320 extern int close(int fd
)
322 return ((int) xosfind_close((os_f
) fd
));
325 extern int write(int fd
, const char *buf
, int nbytes
)
327 /* Returns number of bytes written */
328 return (nbytes
- osgbpb_write((os_f
) fd
, (const byte
*) buf
, nbytes
));
331 extern int read(int fd
, char *buf
, int nbytes
)
333 /* Returns number of bytes read */
334 return (nbytes
- osgbpb_read((os_f
) fd
, (byte
*) buf
, nbytes
));
337 extern off_t
lseek(int fd
, off_t offset
, int whence
)
345 absolute
= (int) offset
;
350 absolute
= osargs_read_ptr((os_f
) fd
) + (int) offset
;
355 absolute
= osargs_read_ext((os_f
) fd
) + (int) offset
;
360 osargs_set_ptr((os_f
) fd
, absolute
);
362 return ((off_t
) osargs_read_ptr((os_f
) fd
));
367 _tiffReadProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
369 return ((tsize_t
) read((int) fd
, buf
, (size_t) size
));
373 _tiffWriteProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
375 return ((tsize_t
) write((int) fd
, buf
, (size_t) size
));
379 _tiffSeekProc(thandle_t fd
, toff_t off
, int whence
)
381 return ((toff_t
) lseek((int) fd
, (off_t
) off
, whence
));
385 _tiffCloseProc(thandle_t fd
)
387 return (close((int) fd
));
391 _tiffSizeProc(thandle_t fd
)
393 return (lseek((int) fd
, SEEK_END
, SEEK_SET
));
397 #error "I didn't know Acorn had that!"
402 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
404 (void) fd
; (void) pbase
; (void) psize
;
409 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
411 (void) fd
; (void) base
; (void) size
;
415 * Open a TIFF file descriptor for read/writing.
418 TIFFFdOpen(int fd
, const char* name
, const char* mode
)
422 tif
= TIFFClientOpen(name
, mode
,
424 _tiffReadProc
, _tiffWriteProc
,
425 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
426 _tiffMapProc
, _tiffUnmapProc
);
435 * Open a TIFF file for read/writing.
438 TIFFOpen(const char* name
, const char* mode
)
440 static const char module[] = "TIFFOpen";
443 m
= _TIFFgetMode(mode
, module);
450 fd
= open(name
, 0, m
);
454 TIFFError(module, "%s: Cannot open", name
);
457 return (TIFFFdOpen(fd
, name
, mode
));
461 _TIFFmalloc(tsize_t s
)
463 return (malloc((size_t) s
));
473 _TIFFrealloc(tdata_t p
, tsize_t s
)
475 return (realloc(p
, (size_t) s
));
479 _TIFFmemset(tdata_t p
, int v
, tsize_t c
)
481 memset(p
, v
, (size_t) c
);
485 _TIFFmemcpy(tdata_t d
, const tdata_t s
, tsize_t c
)
487 memcpy(d
, s
, (size_t) c
);
491 _TIFFmemcmp(const tdata_t p1
, const tdata_t p2
, tsize_t c
)
493 return (memcmp(p1
, p2
, (size_t) c
));
497 acornWarningHandler(const char* module, const char* fmt
, va_list ap
)
501 fprintf(stderr
, "%s: ", module);
503 fprintf(stderr
, "Warning, ");
504 vfprintf(stderr
, fmt
, ap
);
505 fprintf(stderr
, ".\n");
507 TIFFErrorHandler _TIFFwarningHandler
= acornWarningHandler
;
510 acornErrorHandler(const char* module, const char* fmt
, va_list ap
)
514 fprintf(stderr
, "%s: ", module);
516 vfprintf(stderr
, fmt
, ap
);
517 fprintf(stderr
, ".\n");
519 TIFFErrorHandler _TIFFerrorHandler
= acornErrorHandler
;