~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to zip/zip/zipcloak.c

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  Copyright (c) 1990-1999 Info-ZIP.  All rights reserved.
3
 
 
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
8
 
*/
9
 
/*
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.)
14
 
 */
15
 
#define __ZIPCLOAK_C
16
 
 
17
 
#ifndef UTIL
18
 
#define UTIL
19
 
#endif
20
 
#include "zip.h"
21
 
#define DEFCPYRT        /* main module: enable copyright string defines! */
22
 
#include "revision.h"
23
 
//#include "z_crypt.h"
24
 
#include "z_ttyio.h"
25
 
#include <signal.h>
26
 
#ifndef NO_STDLIB_H
27
 
#  include <stdlib.h>
28
 
#endif
29
 
 
30
 
#if CRYPT       /* defined (as TRUE or FALSE) in crypt.h */
31
 
 
32
 
int main OF((int argc, char **argv));
33
 
 
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));
38
 
 
39
 
/* Temporary zip file name and file pointer */
40
 
local char *tempzip;
41
 
local FILE *tempzf;
42
 
 
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;
46
 
#else
47
 
ZCONST uLongf *crc_32_tab;
48
 
#endif
49
 
 
50
 
/***********************************************************************
51
 
 * Issue a message for the error, clean up files and memory, and exit.
52
 
 */
53
 
void ziperr(code, msg)
54
 
    int code;               /* error code from the ZE_ class */
55
 
    char *msg;              /* message about how it happened */
56
 
{
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) {
61
 
        destroy(tempzip);
62
 
        free((zvoid *)tempzip);
63
 
    }
64
 
    if (zipfile != NULL) free((zvoid *)zipfile);
65
 
    EXIT(code);
66
 
}
67
 
 
68
 
/***********************************************************************
69
 
 * Print a warning message to stderr and return.
70
 
 */
71
 
void zipwarn(msg1, msg2)
72
 
    char *msg1, *msg2;        /* message strings juxtaposed in output */
73
 
{
74
 
    fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
75
 
}
76
 
 
77
 
 
78
 
/***********************************************************************
79
 
 * Upon getting a user interrupt, turn echo back on for tty and abort
80
 
 * cleanly using ziperr().
81
 
 */
82
 
local void handler(sig)
83
 
    int sig;                  /* signal number (ignored) */
84
 
{
85
 
#if (!defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS))
86
 
    echon();
87
 
    putc('\n', stderr);
88
 
#endif
89
 
    ziperr(ZE_ABORT +sig-sig, "aborting");
90
 
    /* dummy usage of sig to avoid compiler warnings */
91
 
}
92
 
 
93
 
 
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."
100
 
};
101
 
 
102
 
/***********************************************************************
103
 
 * Print license information to stdout.
104
 
 */
105
 
local void license()
106
 
{
107
 
    extent i;             /* counter for copyright array */
108
 
 
109
 
    for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
110
 
        puts(public[i]);
111
 
    }
112
 
    for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++) {
113
 
        puts(swlicense[i]);
114
 
    }
115
 
}
116
 
 
117
 
 
118
 
static ZCONST char *help_info[] = {
119
 
"",
120
 
"ZipCloak %s (%s)",
121
 
#ifdef VM_CMS
122
 
"Usage:  zipcloak [-d] [-b fm] zipfile",
123
 
#else
124
 
"Usage:  zipcloak [-d] [-b path] zipfile",
125
 
#endif
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)",
128
 
#ifdef VM_CMS
129
 
"  -b   use \"fm\" as the filemode for the temporary zip file",
130
 
#else
131
 
"  -b   use \"path\" for the temporary zip file",
132
 
#endif
133
 
"  -h   show this help    -v   show version info    -L   show software license"
134
 
  };
135
 
 
136
 
/***********************************************************************
137
 
 * Print help (along with license info) to stdout.
138
 
 */
139
 
local void help()
140
 
{
141
 
    extent i;             /* counter for help array */
142
 
 
143
 
    for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
144
 
        puts(public[i]);
145
 
    }
146
 
    for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
147
 
        printf(help_info[i], VERSION, REVDATE);
148
 
        putchar('\n');
149
 
    }
150
 
}
151
 
 
152
 
 
153
 
local void version_info()
154
 
/* Print verbose info about program version and compile time options
155
 
   to stdout. */
156
 
{
157
 
  extent i;             /* counter in text arrays */
158
 
 
159
 
  /* Options info array */
160
 
  static ZCONST char *comp_opts[] = {
161
 
#ifdef DEBUG
162
 
    "DEBUG",
163
 
#endif
164
 
#if CRYPT && defined(PASSWD_FROM_STDIN)
165
 
    "PASSWD_FROM_STDIN",
166
 
#endif /* CRYPT && PASSWD_FROM_STDIN */
167
 
    NULL
168
 
  };
169
 
 
170
 
  for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
171
 
  {
172
 
    printf(copyright[i], "zipcloak");
173
 
    putchar('\n');
174
 
  }
175
 
 
176
 
  for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
177
 
  {
178
 
    printf(versinfolines[i], "ZipCloak", VERSION, REVDATE);
179
 
    putchar('\n');
180
 
  }
181
 
 
182
 
  version_local();
183
 
 
184
 
  puts("ZipCloak special compilation options:");
185
 
  for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
186
 
  {
187
 
    printf("\t%s\n",comp_opts[i]);
188
 
  }
189
 
  printf("\t[encryption, version %d.%d%s of %s]\n",
190
 
            CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
191
 
}
192
 
 
193
 
 
194
 
/***********************************************************************
195
 
 * Encrypt or decrypt all of the entries in a zip file.  See the command
196
 
 * help in help() above.
197
 
 */
198
 
 
199
 
int main(argc, argv)
200
 
    int argc;                   /* number of tokens in command line */
201
 
    char **argv;                /* command line tokens */
202
 
{
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 */
215
 
 
216
 
#ifdef THEOS
217
 
    setlocale(LC_CTYPE, "I");
218
 
#endif
219
 
 
220
 
    /* If no args, show help */
221
 
    if (argc == 1) {
222
 
        help();
223
 
        EXIT(0);
224
 
    }
225
 
 
226
 
    init_upper();               /* build case map table */
227
 
 
228
 
    crc_32_tab = get_crc_table_boinc();
229
 
                                /* initialize crc table for crypt */
230
 
 
231
 
    /* Go through args */
232
 
    zipfile = tempzip = NULL;
233
 
    tempzf = 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++) {
239
 
                switch (*q) {
240
 
                case 'b':   /* Specify path for temporary file */
241
 
                    if (temp_path) {
242
 
                        ziperr(ZE_PARMS, "use -b before zip file name");
243
 
                    }
244
 
                    temp_path = 1;          /* Next non-option is path */
245
 
                    break;
246
 
                case 'd':
247
 
                    decrypt = 1;  break;
248
 
                case 'h':   /* Show help */
249
 
                    help();
250
 
                    EXIT(0);
251
 
                case 'l': case 'L':  /* Show copyright and disclaimer */
252
 
                    license();
253
 
                    EXIT(0);
254
 
                case 'v':   /* Show version info */
255
 
                    version_info();
256
 
                    EXIT(0);
257
 
                default:
258
 
                    ziperr(ZE_PARMS, "unknown option");
259
 
                } /* switch */
260
 
            } /* for */
261
 
 
262
 
        } else if (temp_path == 0) {
263
 
            if (zipfile != NULL) {
264
 
                ziperr(ZE_PARMS, "can only specify one zip file");
265
 
 
266
 
            } else if ((zipfile = ziptyp(argv[r])) == NULL) {
267
 
                ziperr(ZE_MEM, "was processing arguments");
268
 
            }
269
 
        } else {
270
 
            tempath = argv[r];
271
 
            temp_path = 0;
272
 
        } /* if */
273
 
    } /* for */
274
 
 
275
 
    if (zipfile == NULL) ziperr(ZE_PARMS, "need to specify zip file");
276
 
 
277
 
    /* Read zip file */
278
 
    if ((res = readzipfile()) != ZE_OK) ziperr(res, zipfile);
279
 
    if (zfiles == NULL) ziperr(ZE_NAME, zipfile);
280
 
 
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;
284
 
    }
285
 
    if (z == NULL) {
286
 
        ziperr(ZE_NONE, decrypt ? "no encrypted files"
287
 
                       : "all files encrypted already");
288
 
    }
289
 
 
290
 
    /* Before we get carried away, make sure zip file is writeable */
291
 
    if ((inzip = fopen(zipfile, "a")) == NULL) ziperr(ZE_CREAT, zipfile);
292
 
    fclose(inzip);
293
 
    attr = getfileattr(zipfile);
294
 
 
295
 
    /* Open output zip file for writing */
296
 
    if ((tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
297
 
        ziperr(ZE_TEMP, tempzip);
298
 
    }
299
 
 
300
 
    /* Get password */
301
 
    if (getp("Enter password: ", passwd, IZ_PWLEN+1) == NULL)
302
 
        ziperr(ZE_PARMS,
303
 
               "stderr is not a tty (you may never see this message!)");
304
 
 
305
 
    if (decrypt == 0) {
306
 
        if (getp("Verify password: ", verify, IZ_PWLEN+1) == NULL)
307
 
               ziperr(ZE_PARMS,
308
 
                      "stderr is not a tty (you may never see this message!)");
309
 
 
310
 
        if (strcmp(passwd, verify))
311
 
               ziperr(ZE_PARMS, "password verification failed");
312
 
 
313
 
        if (*passwd == '\0')
314
 
               ziperr(ZE_PARMS, "zero length password not allowed");
315
 
    }
316
 
 
317
 
    /* Open input zip file again, copy preamble if any */
318
 
    if ((inzip = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
319
 
 
320
 
    if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
321
 
        ziperr(res, res == ZE_TEMP ? tempzip : zipfile);
322
 
    }
323
 
    tempzn = zipbeg;
324
 
 
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);
329
 
            fflush(stdout);
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)");
333
 
            }
334
 
            putchar('\n');
335
 
 
336
 
        } else if ((!decrypt) && !(z->flg & 1)) {
337
 
            printf("encrypting: %s\n", z->zname);
338
 
            fflush(stdout);
339
 
            if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
340
 
                ziperr(res, "was encrypting an entry");
341
 
            }
342
 
        } else {
343
 
            printf("   copying: %s\n", z->zname);
344
 
            fflush(stdout);
345
 
            if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
346
 
                ziperr(res, "was copying an entry");
347
 
            }
348
 
        } /* if */
349
 
    } /* for */
350
 
    fclose(inzip);
351
 
 
352
 
    /* Write central directory and end of central directory */
353
 
 
354
 
    /* get start of central */
355
 
    if ((start_offset = (ulg)ftell(outzip)) == (ulg)-1L)
356
 
        ziperr(ZE_TEMP, tempzip);
357
 
 
358
 
    for (z = zfiles; z != NULL; z = z->nxt) {
359
 
        if ((res = putcentral(z, outzip)) != ZE_OK) ziperr(res, tempzip);
360
 
    }
361
 
 
362
 
    /* get end of central */
363
 
    if ((length = (ulg)ftell(outzip)) == (ulg)-1L)
364
 
        ziperr(ZE_TEMP, tempzip);
365
 
 
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);
370
 
    }
371
 
    tempzf = NULL;
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);
376
 
        tempzip = NULL;
377
 
        ziperr(res, "was replacing the original zip file");
378
 
    }
379
 
    free((zvoid *)tempzip);
380
 
    tempzip = NULL;
381
 
    setfileattr(zipfile, attr);
382
 
#ifdef RISCOS
383
 
    /* Set the filetype of the zipfile to &DDC */
384
 
    setfiletype(zipfile, 0xDDC);
385
 
#endif
386
 
    free((zvoid *)zipfile);
387
 
    zipfile = NULL;
388
 
 
389
 
    /* Done! */
390
 
    RETURN(0);
391
 
}
392
 
#else /* !CRYPT */
393
 
 
394
 
int main OF((void));
395
 
 
396
 
void zipwarn(msg1, msg2)
397
 
char  *msg1, *msg2;
398
 
{
399
 
    /* Tell picky compilers to shut up about unused variables */
400
 
    msg1 = msg1; msg2 = msg2;
401
 
}
402
 
 
403
 
void ziperr(c, h)
404
 
int  c;
405
 
char *h;
406
 
{
407
 
    /* Tell picky compilers to shut up about unused variables */
408
 
    c = c; h = h;
409
 
}
410
 
 
411
 
int main()
412
 
{
413
 
    fprintf(stderr, "\
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");
416
 
    RETURN(1);
417
 
}
418
 
 
419
 
#endif /* ?CRYPT */
420
 
 
421
 
const char *BOINC_RCSID_20afbe80ed = "$Id: zipcloak.c 7481 2005-08-25 21:33:28Z davea $";