/*

   This program reads an Adobe Font file and unpacks it so that it can be
   loaded into a PostScript printer.  Note that it does not decrypt the file.
   If you want to save some time downloading and you have an eight bit
   connection to the printer, you may not need to convert the font to hex.

   To use it you will either need to compile the program on an IBM PC or
   Macintosh system, convert the fonts and bring them to the system you
   wish to use them on or bring the fonts to the system you wish to use
   them on and unpack them there.  I don't beleive that unpacking
   (as opposed to decrypting) should be a violation of the license 
   agreement, but then again, I am not a lawyer.  In short, as far as the
   legalities go, you are on your own.

   This program was compiled and tested on a Commodore Amiga 2500 with
   Manx C 5.0.  It should build with little trouble using any ANSI C
   compiler.

*/


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

/*
   Define error codes
*/
#define E_IPO 1
#define E_COI 2                        /* Can't open input */
#define E_COO 3                        /* Can't open output */
#define E_BIF 4                       /* Bad input file format */


const char *errmsg[] = {
   "Success",
   "Can't Open Input file %s\n",
   "Can't open Output file %s\n",
   "Bad Input Format"};

void EXIT(code,text)
int code;
char *text;
{
   if (code > 0) printf(errmsg[code],text);
   exit(code);
}

char tohex[] = {"0123456789ABCDEF"};       /* Array to convert to hex */

#define vgetb(buffer,size,stream) fread(buffer,(size_t) 1, \
(size_t) size, stream)

main (argc,argv)
int argc;                             /* Command line argument count */
char *argv[];                          /* Command line argument pointers */
{

   int result;                        /* Command line result */
   unsigned char line[7];                       /* block header */
   int linlen;                        /* length of current line */
   int i;                             /* loop counter */
   FILE *ifcb;                        /* Input file */
   FILE *ofcb;                        /* Output file */
   FILE *fopen();                     /* Open a file */
   unsigned char ch;                            /* a character */
   unsigned long blksiz;               /* size of data block */
   int blktyp;                         /* block type */
   int numred;                         /* number of bytes read */


/* Open the input file and output file */

   if (argc != 3) EXIT(E_IPO,"");
   if ((ifcb = fopen(*++argv,"r")) == NULL) EXIT(E_COI,*argv);
   if ((ofcb = fopen(*++argv,"w")) == NULL) EXIT(E_COO,*argv);

   numred = vgetb(line,2,ifcb);
   if (numred != 2 || line[0] != 128) {
      printf("PostScript font program not in correct format\n");
      EXIT(E_BIF,"");
   };

/*
   load the server dictionary password code here
*/
   fputs("%!",ofcb);
   fputs("serverdict begin 0 exitserver ",ofcb);

   while (line[0] == 128 && line[1] < 3) {
       blktyp = line[1];
       numred = vgetb(line,4,ifcb);
       for (blksiz=0,i=0;i<4;i++)
             blksiz = blksiz | (((unsigned long)line[i])<<(8*i));
       switch (blktyp) {
         case 1:
            for (;blksiz>0;blksiz--) {
                 ch = fgetc(ifcb);
                 if (ch != '\015') fputc(ch,ofcb);
                 else fputs("\n",ofcb);
            }
            break;
         case 2:
            for (linlen=78;blksiz>0;blksiz--,linlen-=2) {
                 if (linlen <= 0) {
                    fputs("",ofcb);
                    linlen=78;
                 };
                 ch = fgetc(ifcb);
                 fputc(tohex[(ch>>4)&15],ofcb);
                 fputc(tohex[ch&15],ofcb);
            };
            fputs("",ofcb);
            break;
        };
   numred = vgetb(line,2,ifcb);
   if (numred != 2 || line[0] != 128) {
      printf("PostScript font program not in correct format\n");
      EXIT(E_BIF,"");
   };
};



/* Close the output file and return success */
   fclose(ifcb);
   fclose(ofcb);
   EXIT(0,"");
}
