123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- #define __ZIPCLOAK_C
- #ifndef UTIL
- # define UTIL
- #endif
- #include "zip.h"
- #define DEFCPYRT
- #include "revision.h"
- #include "crc32.h"
- #include "crypt.h"
- #include "ttyio.h"
- #include <signal.h>
- #ifndef NO_STDLIB_H
- # include <stdlib.h>
- #endif
- #if CRYPT
- int main OF((int argc, char **argv));
- local void handler OF((int sig));
- local void license OF((void));
- local void help OF((void));
- local void version_info OF((void));
- local FILE *tempzf;
- #if (!defined(USE_ZLIB) || defined(USE_OWN_CRCTAB))
- ZCONST ulg near *crc_32_tab;
- #else
- ZCONST uLongf *crc_32_tab;
- #endif
- int set_filetype(out_path)
- char *out_path;
- {
- #ifdef __BEOS__
-
- setfiletype( out_path, "application/zip" );
- #endif
- #ifdef __ATHEOS__
-
- setfiletype(out_path, "application/x-zip");
- #endif
- #ifdef MACOS
-
- setfiletype(out_path, 'IZip', 'ZIP ');
- #endif
- #ifdef RISCOS
-
- setfiletype(out_path, 0xDDC);
- #endif
- return ZE_OK;
- }
- int rename_split(temp_name, out_path)
- char *temp_name;
- char *out_path;
- {
- int r;
-
- if ((r = replace(out_path, temp_name)) != ZE_OK)
- {
- zipwarn("new zip file left as: ", temp_name);
- free((zvoid *)tempzip);
- tempzip = NULL;
- ZIPERR(r, "was replacing split file");
- }
- if (zip_attributes) {
- setfileattr(out_path, zip_attributes);
- }
- return ZE_OK;
- }
- void zipmessage_nl(a, nl)
- ZCONST char *a;
- int nl;
- {
- if (noisy) {
- fprintf(mesg, "%s", a);
- if (nl) {
- fprintf(mesg, "\n");
- mesg_line_started = 0;
- } else {
- mesg_line_started = 1;
- }
- fflush(mesg);
- }
- }
- void zipmessage(a, b)
- ZCONST char *a, *b;
- {
- if (noisy) {
- if (mesg_line_started)
- fprintf(mesg, "\n");
- fprintf(mesg, "%s%s\n", a, b);
- mesg_line_started = 0;
- fflush(mesg);
- }
- }
- void ziperr(code, msg)
- int code;
- ZCONST char *msg;
- {
- if (PERR(code)) perror("zipcloak error");
- fprintf(mesg, "zipcloak error: %s (%s)\n", ZIPERRORS(code), msg);
- if (tempzf != NULL) fclose(tempzf);
- if (tempzip != NULL) {
- destroy(tempzip);
- free((zvoid *)tempzip);
- }
- if (zipfile != NULL) free((zvoid *)zipfile);
- EXIT(code);
- }
- void zipwarn(msg1, msg2)
- ZCONST char *msg1, *msg2;
- {
- fprintf(mesg, "zipcloak warning: %s%s\n", msg1, msg2);
- }
- local void handler(sig)
- int sig;
- {
- #if (!defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS))
- echon();
- putc('\n', mesg);
- #endif
- ziperr(ZE_ABORT +sig-sig, "aborting");
-
- }
- static ZCONST char *public[] = {
- "The encryption code of this program is not copyrighted and is",
- "put in the public domain. It was originally written in Europe",
- "and can be freely distributed in both source and object forms",
- "from any country, including the USA under License Exception",
- "TSU of the U.S. Export Administration Regulations (section",
- "740.13(e)) of 6 June 2002. (Prior to January 2000, re-export",
- "from the US was a violation of US law.)"
- };
- local void license()
- {
- extent i;
- for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++) {
- puts(swlicense[i]);
- }
- putchar('\n');
- printf("Export notice:\n");
- for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
- puts(public[i]);
- }
- }
- static ZCONST char *help_info[] = {
- "",
- "ZipCloak %s (%s)",
- #ifdef VM_CMS
- "Usage: zipcloak [-dq] [-b fm] zipfile",
- #else
- "Usage: zipcloak [-dq] [-b path] zipfile",
- #endif
- " the default action is to encrypt all unencrypted entries in the zip file",
- "",
- " -d --decrypt decrypt encrypted entries (copy if given wrong password)",
- #ifdef VM_CMS
- " -b --temp-mode use \"fm\" as the filemode for the temporary zip file",
- #else
- " -b --temp-path use \"path\" for the temporary zip file",
- #endif
- " -O --output-file write output to new zip file",
- " -q --quiet quiet operation, suppress some informational messages",
- " -h --help show this help",
- " -v --version show version info",
- " -L --license show software license"
- };
- local void help()
- {
- extent i;
- for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
- printf(help_info[i], VERSION, REVDATE);
- putchar('\n');
- }
- }
- local void version_info()
- {
- extent i;
-
- static ZCONST char *comp_opts[] = {
- #ifdef DEBUG
- "DEBUG",
- #endif
- #if CRYPT && defined(PASSWD_FROM_STDIN)
- "PASSWD_FROM_STDIN",
- #endif
- NULL
- };
- for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
- {
- printf(copyright[i], "zipcloak");
- putchar('\n');
- }
- putchar('\n');
- for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
- {
- printf(versinfolines[i], "ZipCloak", VERSION, REVDATE);
- putchar('\n');
- }
- version_local();
- puts("ZipCloak special compilation options:");
- for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
- {
- printf("\t%s\n",comp_opts[i]);
- }
- printf("\t[encryption, version %d.%d%s of %s]\n",
- CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
- }
- struct option_struct far options[] = {
-
- #ifdef VM_CMS
- {"b", "temp-mode", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'b', "temp file mode"},
- #else
- {"b", "temp-path", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'b', "path for temp file"},
- #endif
- {"d", "decrypt", o_NO_VALUE, o_NOT_NEGATABLE, 'd', "decrypt"},
- {"h", "help", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
- {"L", "license", o_NO_VALUE, o_NOT_NEGATABLE, 'L', "license"},
- {"l", "", o_NO_VALUE, o_NOT_NEGATABLE, 'L', "license"},
- {"O", "output-file", o_REQUIRED_VALUE, o_NOT_NEGATABLE, 'O', "output to new archive"},
- {"v", "version", o_NO_VALUE, o_NOT_NEGATABLE, 'v', "version"},
-
- {NULL, NULL, o_NO_VALUE, o_NOT_NEGATABLE, 0, NULL}
- };
- int main(argc, argv)
- int argc;
- char **argv;
- {
- int attr;
- zoff_t start_offset;
- int decrypt;
- int temp_path;
- char passwd[IZ_PWLEN+1];
- char verify[IZ_PWLEN+1];
- #if 0
- char *q;
- int r;
- #endif
- int res;
- zoff_t length;
- FILE *inzip, *outzip;
- struct zlist far *z;
-
- unsigned long option;
- int argcnt = 0;
- int argnum = 0;
- int optchar = 0;
- char *value = NULL;
- int negated = 0;
- int fna = 0;
- int optnum = 0;
- char **args;
- #ifdef THEOS
- setlocale(LC_CTYPE, "I");
- #endif
- #ifdef UNICODE_SUPPORT
- # ifdef UNIX
-
- {
- char *loc;
-
- loc = setlocale(LC_CTYPE, "en_US.UTF-8");
-
- if (loc != NULL) {
-
- using_utf8 = 1;
-
- } else {
-
- }
- }
- # endif
- #endif
-
- if (argc == 1) {
- help();
- EXIT(ZE_OK);
- }
-
- mesg = stdout;
- init_upper();
- crc_32_tab = get_crc_table();
-
-
- zipfile = tempzip = NULL;
- tempzf = NULL;
- #ifdef SIGINT
- signal(SIGINT, handler);
- #endif
- #ifdef SIGTERM
- signal(SIGTERM, handler);
- #endif
- #ifdef SIGABRT
- signal(SIGABRT, handler);
- #endif
- #ifdef SIGBREAK
- signal(SIGBREAK, handler);
- #endif
- #ifdef SIGBUS
- signal(SIGBUS, handler);
- #endif
- #ifdef SIGILL
- signal(SIGILL, handler);
- #endif
- #ifdef SIGSEGV
- signal(SIGSEGV, handler);
- #endif
- temp_path = decrypt = 0;
- #if 0
-
- for (r = 1; r < argc; r++) {
- if (*argv[r] == '-') {
- if (!argv[r][1]) ziperr(ZE_PARMS, "zip file cannot be stdin");
- for (q = argv[r]+1; *q; q++) {
- switch (*q) {
- case 'b':
- if (temp_path) {
- ziperr(ZE_PARMS, "use -b before zip file name");
- }
- temp_path = 1;
- break;
- case 'd':
- decrypt = 1; break;
- case 'h':
- help();
- EXIT(ZE_OK);
- case 'l': case 'L':
- license();
- EXIT(ZE_OK);
- case 'q':
- noisy = 0; break;
- case 'v':
- version_info();
- EXIT(ZE_OK);
- default:
- ziperr(ZE_PARMS, "unknown option");
- }
- }
- } else if (temp_path == 0) {
- if (zipfile != NULL) {
- ziperr(ZE_PARMS, "can only specify one zip file");
- } else if ((zipfile = ziptyp(argv[r])) == NULL) {
- ziperr(ZE_MEM, "was processing arguments");
- }
- } else {
- tempath = argv[r];
- temp_path = 0;
- }
- }
- #else
-
- zipfile = NULL;
- out_path = NULL;
-
- args = copy_args(argv, 0);
-
-
- argnum = 0;
-
- while ((option = get_option(&args, &argcnt, &argnum,
- &optchar, &value, &negated,
- &fna, &optnum, 0)))
- {
- switch (option)
- {
- case 'b':
- if (temp_path) {
- ziperr(ZE_PARMS, "more than one temp_path");
- }
- temp_path = 1;
- tempath = value;
- break;
- case 'd':
- decrypt = 1; break;
- case 'h':
- help();
- EXIT(ZE_OK);
- case 'l': case 'L':
- license();
- EXIT(ZE_OK);
- case 'O':
- if ((out_path = ziptyp(value)) == NULL) {
- ziperr(ZE_MEM, "was processing arguments");
- }
- free(value);
- break;
- case 'q':
- noisy = 0; break;
- case 'v':
- version_info();
- EXIT(ZE_OK);
- case o_NON_OPTION_ARG:
-
-
-
- if (strcmp(value, "-") == 0) {
- ziperr(ZE_PARMS, "zip file cannot be stdin");
- } else if (zipfile != NULL) {
- ziperr(ZE_PARMS, "can only specify one zip file");
- }
- if ((zipfile = ziptyp(value)) == NULL) {
- ziperr(ZE_MEM, "was processing arguments");
- }
- free(value);
- break;
- default:
- ziperr(ZE_PARMS, "unknown option");
- }
- }
- free_args(args);
- #endif
- if (zipfile == NULL) ziperr(ZE_PARMS, "need to specify zip file");
-
- if ((in_path = malloc(strlen(zipfile) + 1)) == NULL) {
- ziperr(ZE_MEM, "input");
- }
- strcpy(in_path, zipfile);
-
- if (out_path == NULL) {
- if ((out_path = malloc(strlen(zipfile) + 1)) == NULL) {
- ziperr(ZE_MEM, "output");
- }
- strcpy(out_path, zipfile);
- }
-
- if ((res = readzipfile()) != ZE_OK) ziperr(res, zipfile);
- if (zfiles == NULL) ziperr(ZE_NAME, zipfile);
-
- for (z = zfiles; z != NULL; z = z->nxt) {
- if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
- }
- if (z == NULL) {
- ziperr(ZE_NONE, decrypt ? "no encrypted files"
- : "all files encrypted already");
- }
-
- if ((inzip = fopen(zipfile, "a")) == NULL) ziperr(ZE_CREAT, zipfile);
- fclose(inzip);
- attr = getfileattr(zipfile);
-
- #if defined(UNIX) && !defined(NO_MKSTEMP)
- {
- int yd;
- int i;
-
- if (tempath != NULL)
- {
-
- if ((tempzip = malloc(strlen(tempath) + 12)) == NULL) {
- ZIPERR(ZE_MEM, "allocating temp filename");
- }
- strcpy(tempzip, tempath);
- if (lastchar(tempzip) != '/')
- strcat(tempzip, "/");
- }
- else
- {
-
- if ((tempzip = malloc(strlen(zipfile) + 12)) == NULL) {
- ZIPERR(ZE_MEM, "allocating temp filename");
- }
- strcpy(tempzip, zipfile);
- for(i = strlen(tempzip); i > 0; i--) {
- if (tempzip[i - 1] == '/')
- break;
- }
- tempzip[i] = '\0';
- }
- strcat(tempzip, "ziXXXXXX");
- if ((yd = mkstemp(tempzip)) == EOF) {
- ZIPERR(ZE_TEMP, tempzip);
- }
- if ((y = tempzf = outzip = fdopen(yd, FOPW_TMP)) == NULL) {
- ZIPERR(ZE_TEMP, tempzip);
- }
- }
- #else
- if ((y = tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
- ziperr(ZE_TEMP, tempzip);
- }
- #endif
-
- if (getp("Enter password: ", passwd, IZ_PWLEN+1) == NULL)
- ziperr(ZE_PARMS,
- "stderr is not a tty (you may never see this message!)");
- if (decrypt == 0) {
- if (getp("Verify password: ", verify, IZ_PWLEN+1) == NULL)
- ziperr(ZE_PARMS,
- "stderr is not a tty (you may never see this message!)");
- if (strcmp(passwd, verify))
- ziperr(ZE_PARMS, "password verification failed");
- if (*passwd == '\0')
- ziperr(ZE_PARMS, "zero length password not allowed");
- }
-
- if ((in_file = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
- if (zipbeg && (res = bfcopy(zipbeg)) != ZE_OK)
- {
- ziperr(res, res == ZE_TEMP ? tempzip : zipfile);
- }
- tempzn = zipbeg;
-
- for (z = zfiles; z != NULL; z = z->nxt) {
- if (decrypt && (z->flg & 1)) {
- printf("decrypting: %s", z->zname);
- fflush(stdout);
- if ((res = zipbare(z, passwd)) != ZE_OK)
- {
- if (res != ZE_MISS) ziperr(res, "was decrypting an entry");
- printf(" (wrong password--just copying)");
- fflush(stdout);
- }
- putchar('\n');
- } else if ((!decrypt) && !(z->flg & 1)) {
- printf("encrypting: %s\n", z->zname);
- fflush(stdout);
- if ((res = zipcloak(z, passwd)) != ZE_OK)
- {
- ziperr(res, "was encrypting an entry");
- }
- } else {
- printf(" copying: %s\n", z->zname);
- fflush(stdout);
- if ((res = zipcopy(z)) != ZE_OK)
- {
- ziperr(res, "was copying an entry");
- }
- }
- }
- fclose(in_file);
-
-
- if ((start_offset = zftello(outzip)) == (zoff_t)-1)
- ziperr(ZE_TEMP, tempzip);
- for (z = zfiles; z != NULL; z = z->nxt) {
- if ((res = putcentral(z)) != ZE_OK) ziperr(res, tempzip);
- }
-
- if ((length = zftello(outzip)) == (zoff_t)-1)
- ziperr(ZE_TEMP, tempzip);
- length -= start_offset;
- if ((res = putend((zoff_t)zcount, length, start_offset, zcomlen,
- zcomment)) != ZE_OK) {
- ziperr(res, tempzip);
- }
- tempzf = NULL;
- if (fclose(outzip)) ziperr(ZE_TEMP, tempzip);
- if ((res = replace(out_path, tempzip)) != ZE_OK) {
- zipwarn("new zip file left as: ", tempzip);
- free((zvoid *)tempzip);
- tempzip = NULL;
- ziperr(res, "was replacing the original zip file");
- }
- free((zvoid *)tempzip);
- tempzip = NULL;
- setfileattr(zipfile, attr);
- #ifdef RISCOS
-
- setfiletype(zipfile, 0xDDC);
- #endif
- free((zvoid *)in_path);
- free((zvoid *)out_path);
- free((zvoid *)zipfile);
- zipfile = NULL;
-
- RETURN(0);
- }
- #else
- struct option_struct far options[] = {
-
- {"h", "help", o_NO_VALUE, o_NOT_NEGATABLE, 'h', "help"},
-
- {NULL, NULL, o_NO_VALUE, o_NOT_NEGATABLE, 0, NULL}
- };
- int main OF((void));
- void zipwarn(msg1, msg2)
- ZCONST char *msg1, *msg2;
- {
-
- msg1 = msg1; msg2 = msg2;
- }
- void ziperr(c, h)
- int c;
- ZCONST char *h;
- {
-
- c = c; h = h;
- }
- int main()
- {
- fprintf(mesg, "\
- This version of ZipCloak does not support encryption. Get the current Zip\n\
- source distribution and recompile ZipCloak after you have added an option to\n\
- define the symbol USE_CRYPT to the C compiler's command arguments.\n");
- RETURN(1);
- }
- #endif
|