summaryrefslogtreecommitdiff
path: root/tiff/libtiff/tif_acorn.c
blob: 63f9234fb7445d658b71c1536f82f2cfd0d5c3fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/Attic/tif_acorn.c,v 1.2.2.1 2010-06-08 18:50:41 bfriesen Exp $ */

/*
 * Copyright (c) 1988-1997 Sam Leffler
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

/*
 * TIFF Library RISC OS specific Routines.
 * Developed out of the Unix version.
 * Peter Greenham, May 1995
 */
#include "tiffiop.h"
#include <stdio.h>
#include <stdlib.h>

/*
Low-level file handling
~~~~~~~~~~~~~~~~~~~~~~~
The functions in osfcn.h are unavailable when compiling under C, as it's a
C++ header. Therefore they have been implemented here.

Now, why have I done it this way?

The definitive API library for RISC OS is Jonathan Coxhead's OSLib, which
uses heavily optimised ARM assembler or even plain inline SWI calls for
maximum performance and minimum runtime size. However, I don't want to make
LIBTIFF need that to survive. Therefore I have also emulated the functions
using macros to _swi() and _swix() defined in the swis.h header, and
borrowing types from kernel.h, which is less efficient but doesn't need any
third-party libraries.
 */

#ifdef INCLUDE_OSLIB

#include "osfile.h"
#include "osgbpb.h"
#include "osargs.h"
#include "osfind.h"

#else

/* OSLIB EMULATION STARTS */

#include "kernel.h"
#include "swis.h"

/* From oslib:types.h */
typedef unsigned int                            bits;
typedef unsigned char                           byte;
#ifndef TRUE
#define TRUE                                    1
#endif
#ifndef FALSE
#define FALSE                                   0
#endif
#ifndef NULL
#define NULL                                    0
#endif
#ifndef SKIP
#define SKIP                                    0
#endif

/* From oslib:os.h */
typedef _kernel_oserror os_error;
typedef byte os_f;

/* From oslib:osfile.h */
#undef  OS_File
#define OS_File                                 0x8

/* From oslib:osgbpb.h */
#undef  OS_GBPB
#define OS_GBPB                                 0xC
#undef  OSGBPB_Write
#define OSGBPB_Write                            0x2
#undef  OSGBPB_Read
#define OSGBPB_Read                             0x4

extern os_error *xosgbpb_write (os_f file,
      byte *data,
      int size,
      int *unwritten);
extern int osgbpb_write (os_f file,
      byte *data,
      int size);

#define	xosgbpb_write(file, data, size, unwritten) \
	(os_error*) _swix(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_IN(4)|_OUT(3), \
		OSGBPB_WriteAt, \
		file, \
		data, \
		size, \
		unwritten)

#define	osgbpb_write(file, data, size) \
	_swi(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_RETURN(3), \
		OSGBPB_Write, \
		file, \
		data, \
		size)

extern os_error *xosgbpb_read (os_f file,
      byte *buffer,
      int size,
      int *unread);
extern int osgbpb_read (os_f file,
      byte *buffer,
      int size);

#define	xosgbpb_read(file, buffer, size, unread) \
	(os_error*) _swix(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_OUT(3), \
		OSGBPB_Read, \
		file, \
		buffer, \
		size, \
		unread)

#define	osgbpb_read(file, buffer, size) \
	_swi(OS_GBPB, _IN(0)|_IN(1)|_IN(2)|_IN(3)|_RETURN(3), \
		OSGBPB_Read, \
		file, \
		buffer, \
		size)

/* From oslib:osfind.h */
#undef  OS_Find
#define OS_Find                                 0xD
#undef  OSFind_Openin
#define OSFind_Openin                           0x40
#undef  OSFind_Openout
#define OSFind_Openout                          0x80
#undef  OSFind_Openup
#define OSFind_Openup                           0xC0
#undef  OSFind_Close
#define OSFind_Close                            0x0

#define	xosfind_open(reason, file_name, path, file) \
	(os_error*) _swix(OS_Find, _IN(0)|_IN(1)|_IN(2)|_OUT(0), \
		reason, file_name, path, file)

#define	osfind_open(reason, file_name, path) \
	(os_f) _swi(OS_Find, _IN(0)|_IN(1)|_IN(2)|_RETURN(0), \
		reason, file_name, path)

extern os_error *xosfind_openin (bits flags,
      char *file_name,
      char *path,
      os_f *file);
extern os_f osfind_openin (bits flags,
      char *file_name,
      char *path);

#define	xosfind_openin(flags, file_name, path, file) \
	xosfind_open(flags | OSFind_Openin, file_name, path, file)

#define	osfind_openin(flags, file_name, path) \
	osfind_open(flags | OSFind_Openin, file_name, path)

extern os_error *xosfind_openout (bits flags,
      char *file_name,
      char *path,
      os_f *file);
extern os_f osfind_openout (bits flags,
      char *file_name,
      char *path);

#define	xosfind_openout(flags, file_name, path, file) \
	xosfind_open(flags | OSFind_Openout, file_name, path, file)

#define	osfind_openout(flags, file_name, path) \
	osfind_open(flags | OSFind_Openout, file_name, path)

extern os_error *xosfind_openup (bits flags,
      char *file_name,
      char *path,
      os_f *file);
extern os_f osfind_openup (bits flags,
      char *file_name,
      char *path);

#define	xosfind_openup(flags, file_name, path, file) \
	xosfind_open(flags | OSFind_Openup, file_name, path, file)

#define	osfind_openup(flags, file_name, path) \
	osfind_open(flags | OSFind_Openup, file_name, path)

extern os_error *xosfind_close (os_f file);
extern void osfind_close (os_f file);

#define	xosfind_close(file) \
	(os_error*) _swix(OS_Find, _IN(0)|_IN(1), \
		OSFind_Close, \
		file)

#define	osfind_close(file) \
	(void) _swi(OS_Find, _IN(0)|_IN(1), \
		OSFind_Close, \
		file)

/* From oslib:osargs.h */
#undef  OS_Args
#define OS_Args                                 0x9
#undef  OSArgs_ReadPtr
#define OSArgs_ReadPtr                          0x0
#undef  OSArgs_SetPtr
#define OSArgs_SetPtr                           0x1
#undef  OSArgs_ReadExt
#define OSArgs_ReadExt                          0x2

extern os_error *xosargs_read_ptr (os_f file,
      int *ptr);
extern int osargs_read_ptr (os_f file);

#define	xosargs_read_ptr(file, ptr) \
	(os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_OUT(2), \
		OSArgs_ReadPtr, \
		file, \
		ptr)

#define	osargs_read_ptr(file) \
	_swi(OS_Args, _IN(0)|_IN(1)|_RETURN(2), \
		OSArgs_ReadPtr, \
		file)

extern os_error *xosargs_set_ptr (os_f file,
      int ptr);
extern void osargs_set_ptr (os_f file,
      int ptr);

#define	xosargs_set_ptr(file, ptr) \
	(os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_IN(2), \
		OSArgs_SetPtr, \
		file, \
		ptr)

#define	osargs_set_ptr(file, ptr) \
	(void) _swi(OS_Args, _IN(0)|_IN(1)|_IN(2), \
		OSArgs_SetPtr, \
		file, \
		ptr)

extern os_error *xosargs_read_ext (os_f file,
      int *ext);
extern int osargs_read_ext (os_f file);

#define	xosargs_read_ext(file, ext) \
	(os_error*) _swix(OS_Args, _IN(0)|_IN(1)|_OUT(2), \
		OSArgs_ReadExt, \
		file, \
		ext)

#define	osargs_read_ext(file) \
	_swi(OS_Args, _IN(0)|_IN(1)|_RETURN(2), \
		OSArgs_ReadExt, \
		file)

/* OSLIB EMULATION ENDS */

#endif

#ifndef __osfcn_h
/* Will be set or not during tiffcomp.h */
/* You get this to compile under C++? Please say how! */

extern int open(const char* name, int flags, int mode)
{
	/* From what I can tell, should return <0 for failure */
	os_error* e = (os_error*) 1; /* Cheeky way to use a pointer eh? :-) */
	os_f file = (os_f) -1;

	flags = flags;

	switch(mode)
	{
		case O_RDONLY:
		{
			e = xosfind_openin(SKIP, name, SKIP, &file);
			break;
		}
		case O_WRONLY:
		case O_RDWR|O_CREAT:
		case O_RDWR|O_CREAT|O_TRUNC:
		{
			e = xosfind_openout(SKIP, name, SKIP, &file);
			break;
		}
		case O_RDWR:
		{
			e = xosfind_openup(SKIP, name, SKIP, &file);
			break;
		}
	}
	if (e)
	{
		file = (os_f) -1;
	}
	return (file);
}

extern int close(int fd)
{
	return ((int) xosfind_close((os_f) fd));
}

extern int write(int fd, const char *buf, int nbytes)
{
	/* Returns number of bytes written */
	return (nbytes - osgbpb_write((os_f) fd, (const byte*) buf, nbytes));
}

extern int read(int fd, char *buf, int nbytes)
{
	/* Returns number of bytes read */
	return (nbytes - osgbpb_read((os_f) fd, (byte*) buf, nbytes));
}

extern off_t lseek(int fd, off_t offset, int whence)
{
	int absolute = 0;

	switch (whence)
	{
		case SEEK_SET:
		{
			absolute = (int) offset;
			break;
		}
		case SEEK_CUR:
		{
			absolute = osargs_read_ptr((os_f) fd) + (int) offset;
			break;
		}
		case SEEK_END:
		{
			absolute = osargs_read_ext((os_f) fd) + (int) offset;
			break;
		}
	}

	osargs_set_ptr((os_f) fd, absolute);

	return ((off_t) osargs_read_ptr((os_f) fd));
}
#endif

static tsize_t
_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
{
	return ((tsize_t) read((int) fd, buf, (size_t) size));
}

static tsize_t
_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
{
	return ((tsize_t) write((int) fd, buf, (size_t) size));
}

static toff_t
_tiffSeekProc(thandle_t fd, toff_t off, int whence)
{
	return ((toff_t) lseek((int) fd, (off_t) off, whence));
}

static int
_tiffCloseProc(thandle_t fd)
{
	return (close((int) fd));
}

static toff_t
_tiffSizeProc(thandle_t fd)
{
	return (lseek((int) fd, SEEK_END, SEEK_SET));
}

#ifdef HAVE_MMAP
#error "I didn't know Acorn had that!"
#endif

/* !HAVE_MMAP */
static int
_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
{
	(void) fd; (void) pbase; (void) psize;
	return (0);
}

static void
_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
{
	(void) fd; (void) base; (void) size;
}

/*
 * Open a TIFF file descriptor for read/writing.
 */
TIFF*
TIFFFdOpen(int fd, const char* name, const char* mode)
{
	TIFF* tif;

	tif = TIFFClientOpen(name, mode,
		(thandle_t) fd,
		_tiffReadProc, _tiffWriteProc,
		_tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
		_tiffMapProc, _tiffUnmapProc);
	if (tif)
	{
		tif->tif_fd = fd;
	}
	return (tif);
}

/*
 * Open a TIFF file for read/writing.
 */
TIFF*
TIFFOpen(const char* name, const char* mode)
{
	static const char module[] = "TIFFOpen";
	int m, fd;

	m = _TIFFgetMode(mode, module);

	if (m == -1)
	{
		return ((TIFF*) 0);
	}

	fd = open(name, 0, m);

	if (fd < 0)
	{
		TIFFErrorExt(0, module, "%s: Cannot open", name);
		return ((TIFF *)0);
	}
	return (TIFFFdOpen(fd, name, mode));
}

void*
_TIFFmalloc(tsize_t s)
{
	return (malloc((size_t) s));
}

void
_TIFFfree(tdata_t p)
{
	free(p);
}

void*
_TIFFrealloc(tdata_t p, tsize_t s)
{
	return (realloc(p, (size_t) s));
}

void
_TIFFmemset(tdata_t p, int v, tsize_t c)
{
	memset(p, v, (size_t) c);
}

void
_TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
{
	memcpy(d, s, (size_t) c);
}

int
_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
{
	return (memcmp(p1, p2, (size_t) c));
}

static void
acornWarningHandler(const char* module, const char* fmt, va_list ap)
{
	if (module != NULL)
	{
		fprintf(stderr, "%s: ", module);
	}
	fprintf(stderr, "Warning, ");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, ".\n");
}
TIFFErrorHandler _TIFFwarningHandler = acornWarningHandler;

static void
acornErrorHandler(const char* module, const char* fmt, va_list ap)
{
	if (module != NULL)
	{
		fprintf(stderr, "%s: ", module);
	}
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, ".\n");
}
TIFFErrorHandler _TIFFerrorHandler = acornErrorHandler;
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 8
 * fill-column: 78
 * End:
 */