home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsd / dataaof / Source / c / Data2AOF
Text File  |  1995-07-04  |  6KB  |  298 lines

  1. /*
  2.     Note:
  3.     This file was created with StrongEd v3.51
  4.     Wrapwidth should be 160, Tabs to 4 column
  5.     If you don't have these settings and it
  6.     looks a mess, that's your problem.
  7. */
  8.  
  9.  
  10. /*
  11.         Data2AOF.c
  12.         Copyright © 1995 A. M. Pereira
  13.  
  14.         History:
  15.             Mon 03rd Jul 95 AMP - created
  16.  
  17. CLI:
  18.  
  19. data2aof [-i] datafile [-o OutputFile] [-l Label]
  20.     defaults:
  21.         -o datafile's ^.datafile
  22.         -l datafile
  23.  
  24. */
  25.  
  26. #include "data2aof.h"
  27. #include "kernel.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31.  
  32. int create_aof (char*, char*, char*);
  33.  
  34. int main (int argc, char **argv)
  35. {
  36.     char         infile[256];
  37.     char         outfile[256];
  38.     char         label[256];
  39.     char        *cptr, *leafptr;
  40.     int             indone =0, outdone =0, labeldone =0;
  41.     int             i;
  42.  
  43.     printf ("Data2AOF Copyright © 1995 A. M. Pereira\nAmpy/Maelstrom Software\n");
  44.     if (argc<2) {
  45.         printf ("data2aof: No input file\n");
  46.         return 1;
  47.     }
  48.  
  49.     for (i = 1; i<argc; i++) {
  50.         if (argv[i][0]=='-') {
  51.             switch (argv[i][1]) {
  52.                 case 'o':                /* -o outputfile */
  53.                     i++;
  54.                     strncpy (outfile, argv[i], 256);
  55.                     outdone = 1;
  56.                     break;
  57.  
  58.                 case 'l':                /* -l label */
  59.                     i++;
  60.                     strncpy (label, argv[i], 256);
  61.                     labeldone = 1;
  62.                     break;
  63.  
  64.                 case 'i':                /* -i input file */
  65.                     i++;
  66.                     strncpy (infile, argv[i], 256);
  67.                     indone = 1;
  68.                     break;
  69.  
  70.                 default:
  71.                     printf ("data2aof: Unrecognised CLI option '%s'", argv[i]);
  72.                     return 1;
  73.                     break;
  74.             }
  75.         }
  76.         else {
  77.             strncpy (infile, argv[i], 256);
  78.             indone = 1;
  79.         }
  80.     }
  81.  
  82.     if (!indone) {
  83.         printf ("data2aof: No input file\n");
  84.         return 1;
  85.     }
  86.  
  87.     if (!labeldone) {
  88.         cptr = leafptr = infile;
  89.         while (*cptr) {
  90.             cptr++;
  91.             if (*cptr == '.' || *cptr == ':') leafptr = cptr+1;
  92.         }
  93.         strcpy (label, leafptr);
  94.     }
  95.  
  96.     if (!outdone) {
  97.         strcpy (outfile, infile);
  98.         cptr = outfile+strlen(outfile)-1;
  99.         while ((*cptr != '.') && (*cptr != ':') && cptr>outfile) {
  100.             cptr--;
  101.         }
  102.         *cptr = 0;
  103.         if (*outfile == 0) {
  104.             strcpy (outfile, "aofdata");
  105.         }
  106.         else {
  107.             cptr = leafptr = infile;
  108.             while (*cptr) {
  109.                 cptr++;
  110.                 if (*cptr == '.' || *cptr == ':') leafptr = cptr+1;
  111.             }
  112.             strcat(outfile, ".o.");
  113.             strcat(outfile, leafptr);
  114.         }
  115.     }
  116.  
  117.     return (create_aof (infile, outfile, label));
  118. }
  119.  
  120. int zstr(void *a, const char *s)
  121. {
  122.     strcpy ((char*)a, s);
  123.     return (strlen(s)+1);
  124. }
  125.  
  126. int create_aof (char *in, char *out, char *label)
  127. {
  128.     _kernel_osfile_block osfile;
  129.     int                 datasize;
  130.     int                 totalsize;
  131.     char            *aof, *ap, *start;
  132.     int                *iptr;
  133.     chunkheader        *head, *area, *idfn, *symt, *strt;
  134.     int                 ampcode, ampdata, mn, codeseg, dataseg, name;
  135.     areaheader        *areahead;
  136.  
  137.     _kernel_osfile (5, in, &osfile);
  138.     datasize = osfile.start;
  139.     if (datasize & 3) datasize=(datasize+4) & 0xfffffffc;
  140.  
  141.     totalsize = AOFSIZE+datasize+8;        /* 8 bytes safety margin */
  142.  
  143.     aof = malloc (totalsize);
  144.     if (!aof) {
  145.         printf ("data2aof: Not enough memory\n");
  146.         return 1;
  147.     }
  148.  
  149.     ap = memset(aof, 0, totalsize);        /* Clear everything to 0 */
  150.  
  151.     iptr = (int*)ap;
  152.     iptr[0] = 0xc3cbc6c5;                /* ChunkFileId */
  153.     iptr[1] = 5;                        /* maxChunks   */
  154.     iptr[2] = 5;                        /* numChunks   */
  155.  
  156. /* Chunk headers */
  157.     ap+=12;
  158.     head = (chunkheader *)ap;
  159.     strcpy (head->id, "OBJ_HEAD");
  160.  
  161.     ap+=sizeof (chunkheader);
  162.     area = (chunkheader *)ap;
  163.     strcpy (area->id, "OBJ_AREA");
  164.  
  165.     ap+=sizeof (chunkheader);
  166.     idfn = (chunkheader *)ap;
  167.     strcpy (idfn->id, "OBJ_IDFN");
  168.  
  169.     ap+=sizeof (chunkheader);
  170.     symt = (chunkheader *)ap;
  171.     strcpy (symt->id, "OBJ_SYMT");
  172.  
  173.     ap+=sizeof (chunkheader);
  174.     strt = (chunkheader *)ap;
  175.     strcpy (strt->id, "OBJ_STRT");
  176.  
  177.     ap+=sizeof (chunkheader);
  178.  
  179. /* OBJ_AREA chunk */
  180.     start = ap;
  181.     area->offset = ap-aof;
  182.     iptr = (int*) ap;
  183.     *iptr = 0xea000000;                /* BL to __main, since no code in this AOF */
  184.     ap+=4;
  185.     osfile.load = (int)ap;
  186.     osfile.exec = osfile.start = osfile.end = 0;
  187.     _kernel_osfile (0xff, in, &osfile);
  188.     ap+=datasize;
  189.     iptr = (int*)ap;
  190.     iptr[0]=0;                        /* offset to relocation */
  191.     iptr[1]=0x00060000;                /* Type 1, PC relative, word, symbol 0 */
  192.  
  193.     iptr[2]=4;                        /* Offset to data in amp$codeseg */
  194.     iptr[3]=0;                        /* offset to relocation */
  195.     iptr[4]=0x000a0001;                /* Type 1, Additive symbol, word, symbol 1 */
  196.     ap+=20;
  197.  
  198.     area->size = ap-start;
  199.  
  200. /* OBJ_IDFN chunk */
  201.     idfn->offset = ap-aof;
  202.     strcpy (ap, "Ampy's Data2AOF v1.00");
  203.     ap+=24;
  204.     idfn->size = 24;
  205.  
  206. /* OBJ_STRT chunk */
  207.     start = ap;
  208.     strt->offset = ap-aof;
  209.  
  210.     iptr = (int *)ap;                /* Size of string table in bytes */
  211.     ap+=4;
  212.     ampcode = ap-start;
  213.     ap+= zstr(ap, "amp$$code");
  214.     ampdata = ap-start;
  215.     ap+= zstr(ap, "amp$$data");
  216.     mn = ap-start;
  217.     ap+= zstr(ap, "__main");
  218.     codeseg = ap-start;
  219.     ap+= zstr(ap, "x$codeseg");
  220.     dataseg = ap-start;
  221.     ap+= zstr(ap, "x$dataseg");
  222.     name = ap-start;
  223.     ap+= zstr(ap, label);
  224.  
  225.     *iptr=ap-start;
  226.     if ((int)ap & 3) ap=(char*)((int)(ap+4) & 0xfffffffc);
  227.     strt->size=ap-start;                /* Word aligned size */
  228.  
  229. /* OBJ_SYMT chunk */
  230.     start = ap;
  231.     symt->offset = ap-aof;
  232.  
  233.     iptr = (int*) ap;
  234.     iptr[0] = mn;        /* __main symbol */
  235.     iptr[1] = 2;        /* defined elsewhere */
  236.     iptr[2] = 0;
  237.     iptr[3] = 0;
  238.  
  239.     iptr[4] = codeseg;    /* x$codeseg symbol */
  240.     iptr[5] = 1;        /* local, defined here */
  241.     iptr[6] = 0;
  242.     iptr[7] = ampcode;
  243.  
  244.     iptr[8] = dataseg;    /* x$dataseg symbol */
  245.     iptr[9] = 1;        /* local, defined here */
  246.     iptr[10] = 0;
  247.     iptr[11] = ampdata;
  248.  
  249.     iptr[12] = name;    /* User's label symbol */
  250.     iptr[13] = 3;        /* global, defined here */
  251.     iptr[14] = 0;
  252.     iptr[15] = ampdata;
  253.  
  254.     ap+=64;
  255.     symt->size = ap-start;
  256.  
  257. /* OBJ_HEAD chunk */
  258.     start = ap;
  259.     head->offset = ap-aof;
  260.  
  261.     iptr = (int *)ap;
  262.     iptr[0] = 0xC5E2D080;            /* relocatable object format */
  263.     iptr[1] = 150;                    /* AOF v1.50 */
  264.     iptr[2] = 2;                    /* number of areas */
  265.     iptr[3] = 4;                    /* number of symbols */
  266.     iptr[4] = 0;                    /* entry point area */
  267.     iptr[5] = 0;                    /* entry point offset */
  268.  
  269.     ap+=24;
  270.     areahead = (areaheader *)ap;
  271.     areahead->name = ampcode;        /* amp$$code area header */
  272.     areahead->al = 2;                /* CODE, READONLY */
  273.     areahead->at = 0x22;
  274.     areahead->size = datasize+4;
  275.     areahead->numreloc = 1;
  276.     areahead++;
  277.  
  278.     areahead->name = ampdata;        /* amp$$data area header */
  279.     areahead->al = 2;                /* DATA, READ, WRITE */
  280.     areahead->at = 0;
  281.     areahead->size = 4;
  282.     areahead->numreloc = 1;
  283.     areahead++;
  284.     ap=(char *)areahead;
  285.  
  286.     head->size = ap-start;
  287.  
  288.     osfile.load = 0xffd;
  289.     osfile.exec = 0;
  290.     osfile.start = (int) aof;
  291.     osfile.end = (int) ap;
  292.     _kernel_osfile (10, out, &osfile);
  293.  
  294.     free (aof);
  295.  
  296.     return 0;
  297. }
  298.