home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / adobeenc.zoo / encrypt.shar / eexec.c < prev    next >
C/C++ Source or Header  |  1990-10-27  |  2KB  |  106 lines

  1. /*
  2.  * eexec.c -- decrypt an Adobe font file encrypted for eexec
  3.  *
  4.  * Chris B. Sears
  5.  */
  6. #include <stdio.h>
  7. #include <strings.h>
  8.  
  9. #define TRUE                1
  10. #define FALSE                0
  11.  
  12. #define EEXEC_SKIP_BYTES    4
  13.  
  14. unsigned short int er =        55665;
  15. unsigned short int ec1 =    52845;
  16. unsigned short int ec2 =    22719;
  17.  
  18. unsigned char
  19. DeCrypt(ch1, ch2)
  20.     unsigned char ch1, ch2;
  21. {
  22.     unsigned char plain, cipher;
  23.  
  24.     /*
  25.      * Decode hex.
  26.      */
  27.     if ('A' <= ch1 && ch1 <= 'F')
  28.         ch1 -= 'A' - 10;
  29.     else if ('a' <= ch1 && ch1 <= 'f')
  30.         ch1 -= 'a' - 10;
  31.     else
  32.         ch1 -= '0';
  33.  
  34.     if ('A' <= ch2 && ch2 <= 'F')
  35.         ch2 -= 'A' - 10;
  36.     else if ('a' <= ch2 && ch2 <= 'f')
  37.         ch2 -= 'a' - 10;
  38.     else
  39.         ch2 -= '0';
  40.  
  41.     /*
  42.      * Decode cipher.
  43.      */
  44.     cipher = ch1 * 16 + ch2;
  45.  
  46.     plain = (cipher ^ (er >> 8));
  47.     er = (cipher + er) * ec1 + ec2;
  48.  
  49.     return plain;
  50. }
  51.  
  52. void
  53. main(argc, argv)
  54.     int argc;
  55.     char **argv;
  56. {
  57.     FILE *in;
  58.     char in_buff[BUFSIZ], out_buff[BUFSIZ];
  59.     int i, o, in_size;
  60.     int skip_salt = TRUE;
  61.  
  62.     if (argc != 2) {
  63.         fprintf(stderr, "Usage: %s input\n", argv[0]);
  64.         exit(0);
  65.     }
  66.  
  67.     if ((in = fopen(argv[1], "r")) == NULL) {
  68.         fprintf(stderr, "%s: can't open %s\n", argv[0], argv[1]);
  69.         exit(0);
  70.     }
  71.  
  72.     /*
  73.      * Just copy to output until we see an eexec.
  74.      */
  75.     while (fgets(in_buff, BUFSIZ, in) != NULL) {
  76.         if (strcmp(in_buff, "currentfile eexec\n") == 0)
  77.             break;
  78.         fprintf(stdout, "%s", in_buff);
  79.     }
  80.  
  81.     for (;;) {
  82.         if (fgets(in_buff, BUFSIZ, in) == NULL)
  83.             break;
  84.         in_size = strlen(in_buff) - 1;
  85.  
  86.         /*
  87.          * Decrypt a line of hex.
  88.          */
  89.         for (i = o = 0; i < in_size; i += 2)
  90.             out_buff[o++] = DeCrypt(in_buff[i], in_buff[i + 1]);
  91.  
  92.         /*
  93.          * Skip the salt if this is the first cypher line.
  94.          */
  95.         if (skip_salt) {
  96.             fwrite(out_buff + EEXEC_SKIP_BYTES, o - EEXEC_SKIP_BYTES, 1, stdout);
  97.             skip_salt = FALSE;
  98.         } else
  99.             fwrite(out_buff, o, 1, stdout);
  100.     }
  101.  
  102.     fclose(in);
  103.  
  104.     exit(0);
  105. }
  106.