2
Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
4
See the accompanying file LICENSE, version 1999-Oct-05 or later
5
(the contents of which are also included in zip.h) for terms of use.
6
If, for some reason, both of these files are missing, the Info-ZIP license
7
also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
10
This code was originally written in Europe and can be freely distributed
11
from any country except the U.S.A. If this code is imported into the U.S.A,
12
it cannot be re-exported from the U.S.A to another country. (This
13
restriction might seem curious but this is what US law requires.)
21
#define DEFCPYRT /* main module: enable copyright string defines! */
23
//#include "z_crypt.h"
30
#if CRYPT /* defined (as TRUE or FALSE) in crypt.h */
32
int main OF((int argc, char **argv));
34
local void handler OF((int sig));
35
local void license OF((void));
36
local void help OF((void));
37
local void version_info OF((void));
39
/* Temporary zip file name and file pointer */
43
/* Pointer to CRC-32 table (used for decryption/encryption) */
44
#if (!defined(USE_ZLIB) || defined(USE_OWN_CRCTAB))
45
ZCONST ulg near *crc_32_tab;
47
ZCONST uLongf *crc_32_tab;
50
/***********************************************************************
51
* Issue a message for the error, clean up files and memory, and exit.
53
void ziperr(code, msg)
54
int code; /* error code from the ZE_ class */
55
char *msg; /* message about how it happened */
57
if (PERR(code)) perror("zipcloak error");
58
fprintf(stderr, "zipcloak error: %s (%s)\n", errors[code-1], msg);
59
if (tempzf != NULL) fclose(tempzf);
60
if (tempzip != NULL) {
62
free((zvoid *)tempzip);
64
if (zipfile != NULL) free((zvoid *)zipfile);
68
/***********************************************************************
69
* Print a warning message to stderr and return.
71
void zipwarn(msg1, msg2)
72
char *msg1, *msg2; /* message strings juxtaposed in output */
74
fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
78
/***********************************************************************
79
* Upon getting a user interrupt, turn echo back on for tty and abort
80
* cleanly using ziperr().
82
local void handler(sig)
83
int sig; /* signal number (ignored) */
85
#if (!defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS))
89
ziperr(ZE_ABORT +sig-sig, "aborting");
90
/* dummy usage of sig to avoid compiler warnings */
94
static ZCONST char *public[] = {
95
"The encryption code of this program is not copyrighted and is put in the",
96
"public domain. It was originally written in Europe and can be freely",
97
"distributed from any country except the U.S.A. If this program is imported",
98
"into the U.S.A, it cannot be re-exported from the U.S.A to another country.",
99
"The copyright notice of the zip program applies to the rest of the code."
102
/***********************************************************************
103
* Print license information to stdout.
107
extent i; /* counter for copyright array */
109
for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
112
for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++) {
118
static ZCONST char *help_info[] = {
122
"Usage: zipcloak [-d] [-b fm] zipfile",
124
"Usage: zipcloak [-d] [-b path] zipfile",
126
" the default action is to encrypt all unencrypted entries in the zip file",
127
" -d decrypt--decrypt encrypted entries (copy if given wrong password)",
129
" -b use \"fm\" as the filemode for the temporary zip file",
131
" -b use \"path\" for the temporary zip file",
133
" -h show this help -v show version info -L show software license"
136
/***********************************************************************
137
* Print help (along with license info) to stdout.
141
extent i; /* counter for help array */
143
for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
146
for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
147
printf(help_info[i], VERSION, REVDATE);
153
local void version_info()
154
/* Print verbose info about program version and compile time options
157
extent i; /* counter in text arrays */
159
/* Options info array */
160
static ZCONST char *comp_opts[] = {
164
#if CRYPT && defined(PASSWD_FROM_STDIN)
166
#endif /* CRYPT && PASSWD_FROM_STDIN */
170
for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
172
printf(copyright[i], "zipcloak");
176
for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
178
printf(versinfolines[i], "ZipCloak", VERSION, REVDATE);
184
puts("ZipCloak special compilation options:");
185
for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
187
printf("\t%s\n",comp_opts[i]);
189
printf("\t[encryption, version %d.%d%s of %s]\n",
190
CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
194
/***********************************************************************
195
* Encrypt or decrypt all of the entries in a zip file. See the command
196
* help in help() above.
200
int argc; /* number of tokens in command line */
201
char **argv; /* command line tokens */
203
int attr; /* attributes of zip file */
204
ulg start_offset; /* start of central directory */
205
int decrypt; /* decryption flag */
206
int temp_path; /* 1 if next argument is path for temp files */
207
char passwd[IZ_PWLEN+1]; /* password for encryption or decryption */
208
char verify[IZ_PWLEN+1]; /* password for encryption or decryption */
209
char *q; /* steps through option arguments */
210
int r; /* arg counter */
211
int res; /* result code */
212
ulg length; /* length of central directory */
213
FILE *inzip, *outzip; /* input and output zip files */
214
struct zlist far *z; /* steps through zfiles linked list */
217
setlocale(LC_CTYPE, "I");
220
/* If no args, show help */
226
init_upper(); /* build case map table */
228
crc_32_tab = get_crc_table_boinc();
229
/* initialize crc table for crypt */
231
/* Go through args */
232
zipfile = tempzip = NULL;
234
temp_path = decrypt = 0;
235
for (r = 1; r < argc; r++) {
236
if (*argv[r] == '-') {
237
if (!argv[r][1]) ziperr(ZE_PARMS, "zip file cannot be stdin");
238
for (q = argv[r]+1; *q; q++) {
240
case 'b': /* Specify path for temporary file */
242
ziperr(ZE_PARMS, "use -b before zip file name");
244
temp_path = 1; /* Next non-option is path */
248
case 'h': /* Show help */
251
case 'l': case 'L': /* Show copyright and disclaimer */
254
case 'v': /* Show version info */
258
ziperr(ZE_PARMS, "unknown option");
262
} else if (temp_path == 0) {
263
if (zipfile != NULL) {
264
ziperr(ZE_PARMS, "can only specify one zip file");
266
} else if ((zipfile = ziptyp(argv[r])) == NULL) {
267
ziperr(ZE_MEM, "was processing arguments");
275
if (zipfile == NULL) ziperr(ZE_PARMS, "need to specify zip file");
278
if ((res = readzipfile()) != ZE_OK) ziperr(res, zipfile);
279
if (zfiles == NULL) ziperr(ZE_NAME, zipfile);
281
/* Check for something to do */
282
for (z = zfiles; z != NULL; z = z->nxt) {
283
if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
286
ziperr(ZE_NONE, decrypt ? "no encrypted files"
287
: "all files encrypted already");
290
/* Before we get carried away, make sure zip file is writeable */
291
if ((inzip = fopen(zipfile, "a")) == NULL) ziperr(ZE_CREAT, zipfile);
293
attr = getfileattr(zipfile);
295
/* Open output zip file for writing */
296
if ((tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
297
ziperr(ZE_TEMP, tempzip);
301
if (getp("Enter password: ", passwd, IZ_PWLEN+1) == NULL)
303
"stderr is not a tty (you may never see this message!)");
306
if (getp("Verify password: ", verify, IZ_PWLEN+1) == NULL)
308
"stderr is not a tty (you may never see this message!)");
310
if (strcmp(passwd, verify))
311
ziperr(ZE_PARMS, "password verification failed");
314
ziperr(ZE_PARMS, "zero length password not allowed");
317
/* Open input zip file again, copy preamble if any */
318
if ((inzip = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
320
if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
321
ziperr(res, res == ZE_TEMP ? tempzip : zipfile);
325
/* Go through local entries, copying, encrypting, or decrypting */
326
for (z = zfiles; z != NULL; z = z->nxt) {
327
if (decrypt && (z->flg & 1)) {
328
printf("decrypting: %s", z->zname);
330
if ((res = zipbare(z, inzip, outzip, passwd)) != ZE_OK) {
331
if (res != ZE_MISS) ziperr(res, "was decrypting an entry");
332
printf(" (wrong password--just copying)");
336
} else if ((!decrypt) && !(z->flg & 1)) {
337
printf("encrypting: %s\n", z->zname);
339
if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
340
ziperr(res, "was encrypting an entry");
343
printf(" copying: %s\n", z->zname);
345
if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
346
ziperr(res, "was copying an entry");
352
/* Write central directory and end of central directory */
354
/* get start of central */
355
if ((start_offset = (ulg)ftell(outzip)) == (ulg)-1L)
356
ziperr(ZE_TEMP, tempzip);
358
for (z = zfiles; z != NULL; z = z->nxt) {
359
if ((res = putcentral(z, outzip)) != ZE_OK) ziperr(res, tempzip);
362
/* get end of central */
363
if ((length = (ulg)ftell(outzip)) == (ulg)-1L)
364
ziperr(ZE_TEMP, tempzip);
366
length -= start_offset; /* compute length of central */
367
if ((res = putend((int)zcount, length, start_offset, zcomlen,
368
zcomment, outzip)) != ZE_OK) {
369
ziperr(res, tempzip);
372
if (fclose(outzip)) ziperr(ZE_TEMP, tempzip);
373
if ((res = replace(zipfile, tempzip)) != ZE_OK) {
374
zipwarn("new zip file left as: ", tempzip);
375
free((zvoid *)tempzip);
377
ziperr(res, "was replacing the original zip file");
379
free((zvoid *)tempzip);
381
setfileattr(zipfile, attr);
383
/* Set the filetype of the zipfile to &DDC */
384
setfiletype(zipfile, 0xDDC);
386
free((zvoid *)zipfile);
396
void zipwarn(msg1, msg2)
399
/* Tell picky compilers to shut up about unused variables */
400
msg1 = msg1; msg2 = msg2;
407
/* Tell picky compilers to shut up about unused variables */
414
This version of ZipCloak does not support encryption. Get zcrypt27.zip (or\n\
415
a later version) and recompile. The Info-ZIP file `WHERE' lists sites.\n");
421
const char *BOINC_RCSID_20afbe80ed = "$Id: zipcloak.c 7481 2005-08-25 21:33:28Z davea $";