/*
 * This program is used to unpack the UniFLEX kermit BOO files.
 */

#include <stdio.h>

#define fixchr(x) ((x) -'0')
#define NULLCHR fixchr('~')

main(argc,argv)
int argc;
char **argv;
{
   decode("ufksup.boo");
   decode("ufkrmt.boo");
}

decode(infile)
char *infile;
{
   char inline[100],
        outline[200];
   char outfile[100];
   int f;
   FILE *ifp, *ofp;

   if ((ifp = fopen(infile,"r")) == NULL)
   {
      printf("%s not found.\n",infile);
      exit(1);
   }

   fgets(outfile,100,ifp);               /* get output file name */
   outfile[strlen(outfile)-1] = '\0';

   if ((ofp = fopen(outfile,"w")) == NULL)
   {
      printf("could not open %s\n",outfile);
      exit(0);
   }
   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
   while(fgets(inline,100,ifp) != NULL)
   {
      int index = 0,
          outindex = 0;

      while (index < strlen(inline) && inline[index] != '\n' &&
           inline[index] != '\r')       /* end of line? */
      if (fixchr(inline[index]) == NULLCHR)
      {                                 /* null compress char... */
         int rptcnt;
         int i;

         rptcnt = fixchr(inline[++index]); /* get repeat count */
         for (i = 0; i < rptcnt; i++)   /* output the nulls */
            putc('\0',ofp);
         index++;                       /* pass the count field */
      }
      else
      {                                 /* a quad to decode... */
         int a, b, c ,d;

         a = fixchr(inline[index++]);
         b = fixchr(inline[index++]);
         c = fixchr(inline[index++]);
         d = fixchr(inline[index++]);
                                        /* output the bytes */
         putc(((a * 4) + (b / 16)) & 255,ofp);
         putc(((b * 16) + (c / 4)) & 255,ofp);
         putc(((c * 64) + d) & 255,ofp);
      }
   }
   fclose(ofp);                         /* Close the files */
   fclose(ifp);
}
