#include <stdio.h>
#include <string.h>

const unsigned char olddata[80] =
   { 0xe8, 0x52, 0xf9, 0xbb, 0x14, 0x00, 0xe8, 0x20,
     0x95, 0xeb, 0x06, 0xdd, 0x3d, 0x1b, 0xff, 0x75,
     0xef, 0xb0, 0x00, 0xa2, 0x4a, 0x00, 0xb0, 0x04,
     0xa2, 0x49, 0x00, 0xe8, 0x83, 0x00, 0xc3, 0xeb,
     0x19, 0xdd, 0x28, 0x49, 0x4e, 0x53, 0x45, 0x52,
     0x54, 0x20, 0x47, 0x41, 0x4c, 0x41, 0x43, 0x54,
     0x49, 0x43, 0x20, 0x44, 0x49, 0x53, 0x4b, 0x29,
     0x8d, 0x00, 0xe8, 0x18, 0xf9, 0xbb, 0x14, 0x00,
     0xe8, 0xe6, 0x94, 0xeb, 0x06, 0xdd, 0x3d, 0x1b,
     0xff, 0x75, 0xef, 0xb0, 0x00, 0xa2, 0x4a, 0x00 };

const unsigned char newdata[80] =
   { 0xe8, 0x52, 0xf9, 0xbb, 0x14, 0x00, 0xe8, 0x20,
     0x95, 0xb0, 0x58, 0xe8, 0x15, 0x00, 0x90, 0x90,
     0x90, 0xb0, 0x00, 0xa2, 0x4a, 0x00, 0xb0, 0x04,
     0xa2, 0x49, 0x00, 0xe8, 0x83, 0x00, 0xc3, 0xeb,
     0x19, 0xdd, 0x28, 0x2e, 0xa2, 0xba, 0x22, 0x2e,
     0xa2, 0xcc, 0x22, 0x2e, 0xa2, 0xf7, 0x22, 0xc3,
     0x00, 0x2d, 0x55, 0x44, 0x49, 0x43, 0x2d, 0x29,
     0x8d, 0x00, 0xe8, 0x18, 0xf9, 0xbb, 0x14, 0x00,
     0xe8, 0xe6, 0x94, 0xb0, 0x47, 0xe8, 0xdb, 0xff,
     0x90, 0x90, 0x90, 0xb0, 0x00, 0xa2, 0x4a, 0x00 };

const long offset = 0x073E8;

unsigned char buffer[80];

void main(void)
{
   FILE *f = fopen("ultimaii.exe","r+b");

   fseek(f,offset,SEEK_SET);

   fread(buffer,1,80,f);
   if (memcmp(buffer,olddata,80)==0)
      {
      printf("Applying the Ultima II Patch.\n");
      fseek(f,offset,SEEK_SET);
      fwrite(newdata,1,80,f);
      }
   else if (memcmp(buffer,newdata,80)==0)
      printf("The Ultima II Patch has already been applied.\n");
   else
      printf("Unexpected data found.  Probably a different version\n"
             "of Ultima II found.  Please contact the Moonstone Dragon\n"
             "so this problem can be fixed.\n");

   fclose(f);
};

