
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

#define SRCFILE "Boot.com"
#define TGTFILE "Boot.h"

int ConstSearch[3][9] = {
	{0x80, 0xcd, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03},
	{0x80, 0x00, 0xcd, 0x13, 0x00, 0x00, 0x00, 0x00, 0x04},
	{0x00, 0x31, 0x32, 0x33, 0x34, 0x00, 0x00, 0x00, 0x05},
};
#define MAXSEARCH sizeof(ConstSearch) / sizeof(ConstSearch[0])

const char *strString[] = {
	"#define DISKSELECT1\t",
   "#define DISKSELECT2\t",
   "#define BOOTIDTABLE\t"
};

void main(void)
{
   FILE	*rp, *wp;
   int	offset, x, s, c;

   if(!(rp = fopen(SRCFILE, "rb"))) {
   	perror(SRCFILE);
      exit(1);
   } else if(!(wp = fopen(TGTFILE, "wb"))) {
   	perror(TGTFILE);
      exit(1);
   }

   x = offset = 0;
   fprintf(wp, "\n\nconst char BootCode[] = {\n\t");

   while((c = fgetc(rp)) != EOF)
   {
   	// Slow but sure...
      for (s=0;s<MAXSEARCH;s++) {
			// Check if we are still searching...
      	if (ConstSearch[s][6] == 0) {
         	if (c == ConstSearch[s][ConstSearch[s][7]]) {
           		if(++ConstSearch[s][7] == ConstSearch[s][8]) {
               	ConstSearch[s][6] = (offset - ConstSearch[s][8]) + 1;
               }
            } else {
            	ConstSearch[s][6] = 0;
               ConstSearch[s][7] = 0;
            }
         }
      }

   	fprintf(wp, "0x%.2x ,", c);
      if (x++ == 12) {
      	x = 0;
         fprintf(wp, "\n\t");
      }

      // Increment our offset from the beginning of the file
      offset++;
   }
   fprintf(wp, "\n};\n");

   for (s=0;s<MAXSEARCH;s++) {
   	fprintf(wp, "%s 0x%.4x\n", strString[s], ConstSearch[s][6]);
   }

   fclose(wp);
   fclose(rp);
}
