home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
utilities
/
utilsd
/
dataaof
/
Source
/
c
/
Data2AOF
Wrap
Text File
|
1995-07-04
|
6KB
|
298 lines
/*
Note:
This file was created with StrongEd v3.51
Wrapwidth should be 160, Tabs to 4 column
If you don't have these settings and it
looks a mess, that's your problem.
*/
/*
Data2AOF.c
Copyright © 1995 A. M. Pereira
History:
Mon 03rd Jul 95 AMP - created
CLI:
data2aof [-i] datafile [-o OutputFile] [-l Label]
defaults:
-o datafile's ^.datafile
-l datafile
*/
#include "data2aof.h"
#include "kernel.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int create_aof (char*, char*, char*);
int main (int argc, char **argv)
{
char infile[256];
char outfile[256];
char label[256];
char *cptr, *leafptr;
int indone =0, outdone =0, labeldone =0;
int i;
printf ("Data2AOF Copyright © 1995 A. M. Pereira\nAmpy/Maelstrom Software\n");
if (argc<2) {
printf ("data2aof: No input file\n");
return 1;
}
for (i = 1; i<argc; i++) {
if (argv[i][0]=='-') {
switch (argv[i][1]) {
case 'o': /* -o outputfile */
i++;
strncpy (outfile, argv[i], 256);
outdone = 1;
break;
case 'l': /* -l label */
i++;
strncpy (label, argv[i], 256);
labeldone = 1;
break;
case 'i': /* -i input file */
i++;
strncpy (infile, argv[i], 256);
indone = 1;
break;
default:
printf ("data2aof: Unrecognised CLI option '%s'", argv[i]);
return 1;
break;
}
}
else {
strncpy (infile, argv[i], 256);
indone = 1;
}
}
if (!indone) {
printf ("data2aof: No input file\n");
return 1;
}
if (!labeldone) {
cptr = leafptr = infile;
while (*cptr) {
cptr++;
if (*cptr == '.' || *cptr == ':') leafptr = cptr+1;
}
strcpy (label, leafptr);
}
if (!outdone) {
strcpy (outfile, infile);
cptr = outfile+strlen(outfile)-1;
while ((*cptr != '.') && (*cptr != ':') && cptr>outfile) {
cptr--;
}
*cptr = 0;
if (*outfile == 0) {
strcpy (outfile, "aofdata");
}
else {
cptr = leafptr = infile;
while (*cptr) {
cptr++;
if (*cptr == '.' || *cptr == ':') leafptr = cptr+1;
}
strcat(outfile, ".o.");
strcat(outfile, leafptr);
}
}
return (create_aof (infile, outfile, label));
}
int zstr(void *a, const char *s)
{
strcpy ((char*)a, s);
return (strlen(s)+1);
}
int create_aof (char *in, char *out, char *label)
{
_kernel_osfile_block osfile;
int datasize;
int totalsize;
char *aof, *ap, *start;
int *iptr;
chunkheader *head, *area, *idfn, *symt, *strt;
int ampcode, ampdata, mn, codeseg, dataseg, name;
areaheader *areahead;
_kernel_osfile (5, in, &osfile);
datasize = osfile.start;
if (datasize & 3) datasize=(datasize+4) & 0xfffffffc;
totalsize = AOFSIZE+datasize+8; /* 8 bytes safety margin */
aof = malloc (totalsize);
if (!aof) {
printf ("data2aof: Not enough memory\n");
return 1;
}
ap = memset(aof, 0, totalsize); /* Clear everything to 0 */
iptr = (int*)ap;
iptr[0] = 0xc3cbc6c5; /* ChunkFileId */
iptr[1] = 5; /* maxChunks */
iptr[2] = 5; /* numChunks */
/* Chunk headers */
ap+=12;
head = (chunkheader *)ap;
strcpy (head->id, "OBJ_HEAD");
ap+=sizeof (chunkheader);
area = (chunkheader *)ap;
strcpy (area->id, "OBJ_AREA");
ap+=sizeof (chunkheader);
idfn = (chunkheader *)ap;
strcpy (idfn->id, "OBJ_IDFN");
ap+=sizeof (chunkheader);
symt = (chunkheader *)ap;
strcpy (symt->id, "OBJ_SYMT");
ap+=sizeof (chunkheader);
strt = (chunkheader *)ap;
strcpy (strt->id, "OBJ_STRT");
ap+=sizeof (chunkheader);
/* OBJ_AREA chunk */
start = ap;
area->offset = ap-aof;
iptr = (int*) ap;
*iptr = 0xea000000; /* BL to __main, since no code in this AOF */
ap+=4;
osfile.load = (int)ap;
osfile.exec = osfile.start = osfile.end = 0;
_kernel_osfile (0xff, in, &osfile);
ap+=datasize;
iptr = (int*)ap;
iptr[0]=0; /* offset to relocation */
iptr[1]=0x00060000; /* Type 1, PC relative, word, symbol 0 */
iptr[2]=4; /* Offset to data in amp$codeseg */
iptr[3]=0; /* offset to relocation */
iptr[4]=0x000a0001; /* Type 1, Additive symbol, word, symbol 1 */
ap+=20;
area->size = ap-start;
/* OBJ_IDFN chunk */
idfn->offset = ap-aof;
strcpy (ap, "Ampy's Data2AOF v1.00");
ap+=24;
idfn->size = 24;
/* OBJ_STRT chunk */
start = ap;
strt->offset = ap-aof;
iptr = (int *)ap; /* Size of string table in bytes */
ap+=4;
ampcode = ap-start;
ap+= zstr(ap, "amp$$code");
ampdata = ap-start;
ap+= zstr(ap, "amp$$data");
mn = ap-start;
ap+= zstr(ap, "__main");
codeseg = ap-start;
ap+= zstr(ap, "x$codeseg");
dataseg = ap-start;
ap+= zstr(ap, "x$dataseg");
name = ap-start;
ap+= zstr(ap, label);
*iptr=ap-start;
if ((int)ap & 3) ap=(char*)((int)(ap+4) & 0xfffffffc);
strt->size=ap-start; /* Word aligned size */
/* OBJ_SYMT chunk */
start = ap;
symt->offset = ap-aof;
iptr = (int*) ap;
iptr[0] = mn; /* __main symbol */
iptr[1] = 2; /* defined elsewhere */
iptr[2] = 0;
iptr[3] = 0;
iptr[4] = codeseg; /* x$codeseg symbol */
iptr[5] = 1; /* local, defined here */
iptr[6] = 0;
iptr[7] = ampcode;
iptr[8] = dataseg; /* x$dataseg symbol */
iptr[9] = 1; /* local, defined here */
iptr[10] = 0;
iptr[11] = ampdata;
iptr[12] = name; /* User's label symbol */
iptr[13] = 3; /* global, defined here */
iptr[14] = 0;
iptr[15] = ampdata;
ap+=64;
symt->size = ap-start;
/* OBJ_HEAD chunk */
start = ap;
head->offset = ap-aof;
iptr = (int *)ap;
iptr[0] = 0xC5E2D080; /* relocatable object format */
iptr[1] = 150; /* AOF v1.50 */
iptr[2] = 2; /* number of areas */
iptr[3] = 4; /* number of symbols */
iptr[4] = 0; /* entry point area */
iptr[5] = 0; /* entry point offset */
ap+=24;
areahead = (areaheader *)ap;
areahead->name = ampcode; /* amp$$code area header */
areahead->al = 2; /* CODE, READONLY */
areahead->at = 0x22;
areahead->size = datasize+4;
areahead->numreloc = 1;
areahead++;
areahead->name = ampdata; /* amp$$data area header */
areahead->al = 2; /* DATA, READ, WRITE */
areahead->at = 0;
areahead->size = 4;
areahead->numreloc = 1;
areahead++;
ap=(char *)areahead;
head->size = ap-start;
osfile.load = 0xffd;
osfile.exec = 0;
osfile.start = (int) aof;
osfile.end = (int) ap;
_kernel_osfile (10, out, &osfile);
free (aof);
return 0;
}