home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / zipcloak.c < prev    next >
C/C++ Source or Header  |  1996-04-01  |  13KB  |  459 lines

  1. /*
  2.    This code is not copyrighted and is put in the public domain. It
  3.    was originally written in Europe and can be freely distributed from
  4.    any country except the U.S.A. If this code is imported into the U.S.A,
  5.    it cannot be re-exported from the U.S.A to another country. (This
  6.    restriction might seem curious but this is what US law requires.)
  7.  */
  8.  
  9. #define UTIL
  10. #include "zip.h"
  11. #include "revision.h"
  12. #include "crypt.h"
  13. #include "ttyio.h"
  14. #include <signal.h>
  15. #ifndef NO_STDLIB_H
  16. #  include <stdlib.h>
  17. #endif
  18.  
  19.  
  20. #ifdef CRYPT   /* defined (or not) in crypt.h */
  21.  
  22. int main OF((int argc, char **argv));
  23.  
  24. local void handler OF((int sig));
  25. local void license OF((void));
  26. local void help OF((void));
  27. local void version_info OF((void));
  28.  
  29. /* Temporary zip file name and file pointer */
  30. local char *tempzip;
  31. local FILE *tempzf;
  32.  
  33. /* Pointer to CRC-32 table (used for decryption/encryption) */
  34. ulg near *crc_32_tab;
  35.  
  36. /***********************************************************************
  37.  * Issue a message for the error, clean up files and memory, and exit.
  38.  */
  39. void ziperr(code, msg)
  40.     int code;               /* error code from the ZE_ class */
  41.     char *msg;              /* message about how it happened */
  42. {
  43.     if (PERR(code)) perror("zipcloak error");
  44.     fprintf(stderr, "zipcloak error: %s (%s)\n", errors[code-1], msg);
  45.     if (tempzf != NULL) fclose(tempzf);
  46.     if (tempzip != NULL) {
  47.         destroy(tempzip);
  48.         free((zvoid *)tempzip);
  49.     }
  50.     if (zipfile != NULL) free((zvoid *)zipfile);
  51.     EXIT(code);
  52. }
  53.  
  54. /***********************************************************************
  55.  * Print a warning message to stderr and return.
  56.  */
  57. void zipwarn(msg1, msg2)
  58.     char *msg1, *msg2;        /* message strings juxtaposed in output */
  59. {
  60.     fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
  61. }
  62.  
  63.  
  64. /***********************************************************************
  65.  * Upon getting a user interrupt, turn echo back on for tty and abort
  66.  * cleanly using ziperr().
  67.  */
  68. local void handler(sig)
  69.     int sig;                  /* signal number (ignored) */
  70. {
  71. #if (!defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS))
  72.     echon();
  73.     putc('\n', stderr);
  74. #endif
  75.     ziperr(ZE_ABORT +sig-sig, "aborting");
  76.     /* dummy usage of sig to avoid compiler warnings */
  77. }
  78.  
  79.  
  80. static const char *public[] = {
  81. "The encryption code of this program is not copyrighted and is put in the",
  82. "public domain. It was originally written in Europe and can be freely",
  83. "distributed from any country except the U.S.A. If this program is imported",
  84. "into the U.S.A, it cannot be re-exported from the U.S.A to another country.",
  85. "The copyright notice of the zip program applies to the rest of the code."
  86. };
  87.  
  88. /***********************************************************************
  89.  * Print license information to stdout.
  90.  */
  91. local void license()
  92. {
  93.     extent i;             /* counter for copyright array */
  94.  
  95.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  96.         puts(public[i]);
  97.     }
  98.     for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++) {
  99.         puts(disclaimer[i]);
  100.     }
  101. }
  102.  
  103.  
  104. static const char *help_info[] = {
  105. "",
  106. "ZipCloak %s (%s)",
  107. "Usage:  zipcloak [-d] [-b path] zipfile",
  108. "  the default action is to encrypt all unencrypted entries in the zip file",
  109. "  -d   decrypt--decrypt encrypted entries (copy if given wrong password)",
  110. "  -b   use \"path\" for the temporary zip file",
  111. "  -h   show this help    -v   show version info    -L   show software license"
  112.   };
  113.  
  114. /***********************************************************************
  115.  * Print help (along with license info) to stdout.
  116.  */
  117. local void help()
  118. {
  119.     extent i;             /* counter for help array */
  120.  
  121.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  122.         puts(public[i]);
  123.     }
  124.     for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
  125.         printf(help_info[i], VERSION, REVDATE);
  126.         putchar('\n');
  127.     }
  128. }
  129.  
  130.  
  131. static const char CompiledWith[] = "Compiled with %s%s for %s%s%s.\n\n";
  132.                         /* At module level to keep Turbo C++ 1.0 happy !! */
  133.  
  134. local void version_info()
  135. /* Print verbose info about program version and compile time options
  136.    to stdout. */
  137. {
  138.   extent i;             /* counter in text arrays */
  139.  
  140.   /* Options info array */
  141.   static const char *comp_opts[] = {
  142. #ifdef DEBUG
  143.     "DEBUG",
  144. #endif
  145. #if defined(CRYPT) && defined(PASSWD_FROM_STDIN)
  146.     "PASSWD_FROM_STDIN",
  147. #endif /* CRYPT & PASSWD_FROM_STDIN */
  148.     NULL
  149.   };
  150.  
  151.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  152.   {
  153.     printf(copyright[i], "zip");
  154.     putchar('\n');
  155.   }
  156.  
  157.   for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
  158.   {
  159.     printf(versinfolines[i], "ZipCloak", VERSION, REVDATE);
  160.     putchar('\n');
  161.   }
  162.  
  163.   printf(CompiledWith,
  164.  
  165. #ifdef __GNUC__
  166.       "gcc ", __VERSION__,
  167. #else
  168.       "(OS specific) cc ", "",
  169. #endif
  170.  
  171. #ifdef RISCOS
  172.       "RISC OS (Acorn Computers Ltd)",
  173. #else
  174. #ifdef AMIGA
  175.       "AmigaDOS"
  176. #else
  177. #ifdef ATARI
  178. #  ifdef __MINT__
  179.       "Atari TOS/MiNT",
  180. #  else
  181.       "Atari TOS",
  182. #  endif
  183. #else
  184. #ifdef CMS_MVS
  185. #  ifdef VM_CMS
  186.       "VM/CMS",
  187. #  else
  188.       "MVS",
  189. #  endif
  190. #else
  191. #ifdef __human68k__
  192.       "Human68k (X68000)",
  193. #else
  194. #ifdef DOS
  195.       "MS-DOS",
  196. #else
  197. #ifdef OS2
  198.       "OS/2",
  199. #else
  200. #ifdef TOPS20
  201.       "TOPS-20",
  202. #else
  203. #ifdef UNIX
  204.       "Unix",
  205. #else
  206. #ifdef VMS
  207.       "VMS",
  208. #else
  209. #ifdef WIN32
  210.       "\n\tWindows 95 / Windows NT (32-bit)",
  211. #else
  212.       "[unrecognized OS]",
  213. #endif /* WIN32 */
  214. #endif /* VMS */
  215. #endif /* UNIX */
  216. #endif /* TOPS20 */
  217. #endif /* OS2 */
  218. #endif /* DOS */
  219. #endif /* __human68k__ */
  220. #endif /* CMS_MVS */
  221. #endif /* ATARI */
  222. #endif /* AMIGA */
  223. #endif /* RISCOS */
  224.  
  225. #ifdef __DATE__
  226.       " on ", __DATE__
  227. #else
  228.       "", ""
  229. #endif
  230.     );
  231.  
  232.   puts("ZipCloak special compilation options:");
  233.   for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
  234.   {
  235.     printf("\t%s\n",comp_opts[i]);
  236.   }
  237.   printf("\t[encryption, version %d.%d%s of %s]\n",
  238.             CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
  239. }
  240.  
  241.  
  242. /***********************************************************************
  243.  * Encrypt or decrypt all of the entries in a zip file.  See the command
  244.  * help in help() above.
  245.  */
  246.  
  247. int main(argc, argv)
  248.     int argc;             /* number of tokens in command line */
  249.     char **argv;          /* command line tokens */
  250. {
  251.     int attr;             /* attributes of zip file */
  252.     ulg start_offset;     /* start of central directory */
  253.     int decrypt;          /* decryption flag */
  254.     int temp_path;        /* 1 if next argument is path for temp files */
  255.     char passwd[PWLEN+1]; /* password for encryption or decryption */
  256.     char verify[PWLEN+1]; /* password for encryption or decryption */
  257.     char *q;              /* steps through option arguments */
  258.     int r;                /* arg counter */
  259.     int res;              /* result code */
  260.     ulg length;           /* length of central directory */
  261.     FILE *inzip, *outzip; /* input and output zip files */
  262.     struct zlist far *z;  /* steps through zfiles linked list */
  263.  
  264.  
  265.     /* If no args, show help */
  266.     if (argc == 1) {
  267.         help();
  268.         EXIT(0);
  269.     }
  270.  
  271.     init_upper();               /* build case map table */
  272.  
  273.     crc_32_tab = (ulg near *)get_crc_table();
  274.                                 /* initialize crc table for crypt */
  275.  
  276.     /* Go through args */
  277.     zipfile = tempzip = NULL;
  278.     tempzf = NULL;
  279.     signal(SIGINT, handler);
  280. #ifdef SIGTERM                /* Some don't have SIGTERM */
  281.     signal(SIGTERM, handler);
  282. #endif
  283.     temp_path = decrypt = 0;
  284.     for (r = 1; r < argc; r++) {
  285.         if (*argv[r] == '-') {
  286.             if (!argv[r][1]) ziperr(ZE_PARMS, "zip file cannot be stdin");
  287.             for (q = argv[r]+1; *q; q++) {
  288.                 switch(*q) {
  289.                 case 'b':   /* Specify path for temporary file */
  290.                     if (temp_path) {
  291.                         ziperr(ZE_PARMS, "use -b before zip file name");
  292.                     }
  293.                     temp_path = 1;          /* Next non-option is path */
  294.                     break;
  295.                 case 'd':
  296.                     decrypt = 1;  break;
  297.                 case 'h':   /* Show help */
  298.                     help();
  299.                     EXIT(0);
  300.                 case 'l': case 'L':  /* Show copyright and disclaimer */
  301.                     license();
  302.                     EXIT(0);
  303.                 case 'v':   /* Show version info */
  304.                     version_info();
  305.                     EXIT(0);
  306.                 default:
  307.                     ziperr(ZE_PARMS, "unknown option");
  308.                 } /* switch */
  309.             } /* for */
  310.  
  311.         } else if (temp_path == 0) {
  312.             if (zipfile != NULL) {
  313.                 ziperr(ZE_PARMS, "can only specify one zip file");
  314.  
  315.             } else if ((zipfile = ziptyp(argv[r])) == NULL) {
  316.                 ziperr(ZE_MEM, "was processing arguments");
  317.             }
  318.         } else {
  319.             tempath = argv[r];
  320.             temp_path = 0;
  321.         } /* if */
  322.     } /* for */
  323.  
  324.     if (zipfile == NULL) ziperr(ZE_PARMS, "need to specify zip file");
  325.  
  326.     /* Read zip file */
  327.     if ((res = readzipfile()) != ZE_OK) ziperr(res, zipfile);
  328.     if (zfiles == NULL) ziperr(ZE_NAME, zipfile);
  329.  
  330.     /* Check for something to do */
  331.     for (z = zfiles; z != NULL; z = z->nxt) {
  332.         if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
  333.     }
  334.     if (z == NULL) {
  335.         ziperr(ZE_NONE, decrypt ? "no encrypted files"
  336.                        : "all files encrypted already");
  337.     }
  338.  
  339.     /* Before we get carried away, make sure zip file is writeable */
  340.     if ((inzip = fopen(zipfile, "a")) == NULL) ziperr(ZE_CREAT, zipfile);
  341.     fclose(inzip);
  342.     attr = getfileattr(zipfile);
  343.  
  344.     /* Open output zip file for writing */
  345.     if ((tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
  346.         ziperr(ZE_TEMP, tempzip);
  347.     }
  348.  
  349.     /* Get password */
  350.     if (getp("Enter password: ", passwd, PWLEN+1) == NULL)
  351.         ziperr(ZE_PARMS,
  352.                "stderr is not a tty (you may never see this message!)");
  353.  
  354.     if (decrypt == 0) {
  355.         if (getp("Verify password: ", verify, PWLEN+1) == NULL)
  356.                ziperr(ZE_PARMS,
  357.                       "stderr is not a tty (you may never see this message!)");
  358.  
  359.         if (strcmp(passwd, verify))
  360.                ziperr(ZE_PARMS, "password verification failed");
  361.  
  362.         if (*passwd == '\0')
  363.                ziperr(ZE_PARMS, "zero length password not allowed");
  364.     }
  365.  
  366.     /* Open input zip file again, copy preamble if any */
  367.     if ((inzip = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
  368.  
  369.     if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
  370.         ziperr(res, res == ZE_TEMP ? tempzip : zipfile);
  371.     }
  372.     tempzn = zipbeg;
  373.  
  374.     /* Go through local entries, copying, encrypting, or decrypting */
  375.     for (z = zfiles; z != NULL; z = z->nxt) {
  376.         if (decrypt && (z->flg & 1)) {
  377.             printf("decrypting: %s", z->zname);
  378.             fflush(stdout);
  379.             if ((res = zipbare(z, inzip, outzip, passwd)) != ZE_OK) {
  380.                 if (res != ZE_MISS) ziperr(res, "was decrypting an entry");
  381.                 printf(" (wrong password--just copying)");
  382.             }
  383.             putchar('\n');
  384.  
  385.         } else if ((!decrypt) && !(z->flg & 1)) {
  386.             printf("encrypting: %s\n", z->zname);
  387.             fflush(stdout);
  388.             if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
  389.                 ziperr(res, "was encrypting an entry");
  390.             }
  391.         } else {
  392.             printf("   copying: %s\n", z->zname);
  393.             fflush(stdout);
  394.             if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
  395.                 ziperr(res, "was copying an entry");
  396.             }
  397.         } /* if */
  398.     } /* for */
  399.     fclose(inzip);
  400.  
  401.     /* Write central directory and end of central directory */
  402.  
  403.     /* get start of central */
  404.     if ((start_offset = ftell(outzip)) == -1L) ziperr(ZE_TEMP, tempzip);
  405.  
  406.     for (z = zfiles; z != NULL; z = z->nxt) {
  407.         if ((res = putcentral(z, outzip)) != ZE_OK) ziperr(res, tempzip);
  408.     }
  409.  
  410.     /* get end of central */
  411.     if ((length = ftell(outzip)) == -1L) ziperr(ZE_TEMP, tempzip);
  412.  
  413.     length -= start_offset;               /* compute length of central */
  414.     if ((res = putend((int)zcount, length, start_offset, zcomlen,
  415.                        zcomment, outzip)) != ZE_OK) {
  416.         ziperr(res, tempzip);
  417.     }
  418.     tempzf = NULL;
  419.     if (fclose(outzip)) ziperr(ZE_TEMP, tempzip);
  420.     if ((res = replace(zipfile, tempzip)) != ZE_OK) {
  421.         zipwarn("new zip file left as: ", tempzip);
  422.         free((zvoid *)tempzip);
  423.         tempzip = NULL;
  424.         ziperr(res, "was replacing the original zip file");
  425.     }
  426.     free((zvoid *)tempzip);
  427.     tempzip = NULL;
  428.     setfileattr(zipfile, attr);
  429.     free((zvoid *)zipfile);
  430.     zipfile = NULL;
  431.  
  432.     /* Done! */
  433.     RETURN(0);
  434. }
  435. #else /* !CRYPT */
  436.  
  437. int main OF((void));
  438.  
  439. void zipwarn(msg1, msg2)
  440. char  *msg1, *msg2;
  441. {
  442. }
  443.  
  444. void ziperr(c, h)
  445. int  c;
  446. char *h;
  447. {
  448. }
  449.  
  450. int main()
  451. {
  452.     fprintf(stderr, "\
  453. This version of ZipCloak does not support encryption.  Get zcrypt26.zip (or\n\
  454. a later version) and recompile.  The Info-ZIP file `Where' lists sites.\n");
  455.     RETURN(1);
  456. }
  457.  
  458. #endif /* ?CRYPT */
  459.