home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dream 52
/
Amiga_Dream_52.iso
/
RiscOS
/
APP
/
DEVS
/
FORTH
/
BEETLE
/
BEETLE.ZIP
/
Beetle
/
loadobj.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-04-22
|
2KB
|
79 lines
/* LOADOBJ.C
Vrsn Date Comment
----|-------|---------------------------------------------------------------
0.00 24jan95
0.01 24feb95 Made internal routines and globals static, and made the array
magic in load_object one byte smaller, as it can be. A bug in
detecting when a module would not fit in memory was corrected,
and CELL_W used to replace 4 where appropriate.
0.02 01apr95 Made length be read correctly on big-endian machines.
0.03 21may96 Made error -1 be returned always if address = MEMORY.
0.04 23may96 Fixed some unbalanced brackets.
Reuben Thomas
The interface call load_object(file, address) : integer.
*/
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include "beetle.h" /* main header */
static void reverse(CELL *start, UCELL length)
{
UCELL i;
for (i = 0; i < length; i++)
start[i] = (CELL)(((UCELL) start[i] << 24) | ((UCELL)start[i] >> 24) |
(((UCELL)start[i] & 0xff00) << 8) |
(((UCELL)start[i] & 0xff0000) >> 8));
}
static jmp_buf env;
static int get(FILE *fp)
{
int t = getc(fp);
if (t == EOF) longjmp(env, -3);
return t;
}
int load_object(FILE *file, CELL *address)
{
char magic[8];
int endism, reversed, i, err = 0;
UCELL length = 0;
if ((err = setjmp(env)) == 0) {
for (i = 0; i < 7; i++) magic[i] = get(file);
magic[7] = '\0';
if (strcmp(magic, "BEETLE")) { err = -2; goto error; }
endism = get(file);
if (endism != 0 && endism != 1) { err = -2; goto error; }
reversed = endism ^ ENDISM;
for (i = 0; i < CELL_W; i++) length |= get(file) << (8 * i);
if (endism) reverse((CELL *)&length, 1);
if ((((address - (CELL *)M0) + length) * CELL_W > MEMORY) ||
(address - (CELL *)M0) * CELL_W == MEMORY) {
err = -1;
goto error;
}
for (i = 0; i < length * CELL_W; i++) ((BYTE *)address)[i] = get(file);
if (reversed) reverse(address, length);
return 0;
} else {
error: return err;
}
}