~peter-pearse/ubuntu/natty/unzip/prop001

« back to all changes in this revision

Viewing changes to crypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2009-05-08 20:02:40 UTC
  • mfrom: (1.1.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090508200240-7l4gypruop5863bd
* New upstream release. Closes: #496989.
* Enabled new Unicode support. Closes: #197427. This may or may not work
  for your already created zipfiles, but it's not a bug unless they were
  created using the Unicode feature present in zip 3.0.
* Built using DATE_FORMAT=DF_YMD so that unzip -l show dates in ISO format,
  as that's the only available one which makes sense. Closes: #312886.
* Enabled new bzip2 support. Closes: #426798.
* Exit code for zipgrep should now be the right one. Closes: #441997.
* The reason why a file may not be created is now shown. Closes: #478791.
* Summary of changes in this version not being the debian/* files:
- Manpages in section 1, not 1L.
- Branding patch. UnZip by Debian. Original by Info-ZIP.
- Always #include <unistd.h>. Debian GNU/kFreeBSD needs it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
  Copyright (c) 1990-2000 Info-ZIP.  All rights reserved.
 
2
  Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
3
3
 
4
 
  See the accompanying file LICENSE, version 2000-Apr-09 or later
5
 
  (the contents of which are also included in zip.h) for terms of use.
 
4
  See the accompanying file LICENSE, version 2005-Feb-10 or later
 
5
  (the contents of which are also included in (un)zip.h) for terms of use.
6
6
  If, for some reason, all these files are missing, the Info-ZIP license
7
7
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
8
8
*/
9
9
/*
10
10
  crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
11
11
 
12
 
  This encryption/decryption source code for Info-Zip software was
13
 
  originally written in Europe.  The whole source package can be
14
 
  freely distributed, including from the USA.  (Prior to January 2000,
15
 
  re-export from the US was a violation of US law.)
 
12
  The main encryption/decryption source code for Info-Zip software was
 
13
  originally written in Europe.  To the best of our knowledge, it can
 
14
  be freely distributed in both source and object forms from any country,
 
15
  including the USA under License Exception TSU of the U.S. Export
 
16
  Administration Regulations (section 740.13(e)) of 6 June 2002.
16
17
 
17
18
  NOTE on copyright history:
18
19
  Previous versions of this source package (up to version 2.8) were
96
97
#endif /* UNZIP */
97
98
 
98
99
#ifndef UNZIP             /* moved to globals.h for UnZip */
99
 
   local ulg keys[3];     /* keys defining the pseudo-random sequence */
 
100
#  ifndef Z_UINT4_DEFINED
 
101
#   if !defined(NO_LIMITS_H)
 
102
#    if (defined(UINT_MAX) && (UINT_MAX == 0xffffffffUL))
 
103
       typedef unsigned int     z_uint4;
 
104
#      define Z_UINT4_DEFINED
 
105
#    else
 
106
#    if (defined(ULONG_MAX) && (ULONG_MAX == 0xffffffffUL))
 
107
       typedef unsigned long    z_uint4;
 
108
#      define Z_UINT4_DEFINED
 
109
#    else
 
110
#    if (defined(USHRT_MAX) && (USHRT_MAX == 0xffffffffUL))
 
111
       typedef unsigned short   z_uint4;
 
112
#      define Z_UINT4_DEFINED
 
113
#    endif
 
114
#    endif
 
115
#    endif
 
116
#   endif /* !NO_LIMITS_H */
 
117
#  endif /* !Z_UINT4_DEFINED */
 
118
#  ifndef Z_UINT4_DEFINED
 
119
     typedef ulg                z_uint4;
 
120
#    define Z_UINT4_DEFINED
 
121
#  endif
 
122
   local z_uint4 keys[3];       /* keys defining the pseudo-random sequence */
100
123
#endif /* !UNZIP */
101
124
 
102
125
#ifndef Trace
107
130
#  endif
108
131
#endif
109
132
 
110
 
#ifndef CRC_32_TAB
111
 
#  define CRC_32_TAB     crc_32_tab
112
 
#endif
 
133
#include "crc32.h"
113
134
 
114
 
#define CRC32(c, b) (CRC_32_TAB[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
 
135
#ifdef IZ_CRC_BE_OPTIMIZ
 
136
   local z_uint4 near crycrctab[256];
 
137
   local z_uint4 near *cry_crctb_p = NULL;
 
138
   local z_uint4 near *crytab_init OF((__GPRO));
 
139
#  define CRY_CRC_TAB  cry_crctb_p
 
140
#  undef CRC32
 
141
#  define CRC32(c, b, crctab) (crctab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
 
142
#else
 
143
#  define CRY_CRC_TAB  CRC_32_TAB
 
144
#endif /* ?IZ_CRC_BE_OPTIMIZ */
115
145
 
116
146
/***********************************************************************
117
147
 * Return the next byte in the pseudo-random sequence
134
164
    __GDEF
135
165
    int c;                      /* byte of plain text */
136
166
{
137
 
    GLOBAL(keys[0]) = CRC32(GLOBAL(keys[0]), c);
138
 
    GLOBAL(keys[1]) += GLOBAL(keys[0]) & 0xff;
139
 
    GLOBAL(keys[1]) = GLOBAL(keys[1]) * 134775813L + 1;
 
167
    GLOBAL(keys[0]) = CRC32(GLOBAL(keys[0]), c, CRY_CRC_TAB);
 
168
    GLOBAL(keys[1]) = (GLOBAL(keys[1])
 
169
                       + (GLOBAL(keys[0]) & 0xff))
 
170
                      * 134775813L + 1;
140
171
    {
141
172
      register int keyshift = (int)(GLOBAL(keys[1]) >> 24);
142
 
      GLOBAL(keys[2]) = CRC32(GLOBAL(keys[2]), keyshift);
 
173
      GLOBAL(keys[2]) = CRC32(GLOBAL(keys[2]), keyshift, CRY_CRC_TAB);
143
174
    }
144
175
    return c;
145
176
}
153
184
    __GDEF
154
185
    ZCONST char *passwd;        /* password string with which to modify keys */
155
186
{
 
187
#ifdef IZ_CRC_BE_OPTIMIZ
 
188
    if (cry_crctb_p == NULL) {
 
189
        cry_crctb_p = crytab_init(__G);
 
190
    }
 
191
#endif
156
192
    GLOBAL(keys[0]) = 305419896L;
157
193
    GLOBAL(keys[1]) = 591751049L;
158
194
    GLOBAL(keys[2]) = 878082192L;
163
199
}
164
200
 
165
201
 
 
202
/***********************************************************************
 
203
 * Initialize the local copy of the table of precomputed crc32 values.
 
204
 * Whereas the public crc32-table is optimized for crc32 calculations
 
205
 * on arrays of bytes, the crypt code needs the crc32 values in an
 
206
 * byte-order-independent form as 32-bit unsigned numbers. On systems
 
207
 * with Big-Endian byte order using the optimized crc32 code, this
 
208
 * requires inverting the byte-order of the values in the
 
209
 * crypt-crc32-table.
 
210
 */
 
211
#ifdef IZ_CRC_BE_OPTIMIZ
 
212
local z_uint4 near *crytab_init(__G)
 
213
    __GDEF
 
214
{
 
215
    int i;
 
216
 
 
217
    for (i = 0; i < 256; i++) {
 
218
        crycrctab[i] = REV_BE(CRC_32_TAB[i]);
 
219
    }
 
220
    return crycrctab;
 
221
}
 
222
#endif
 
223
 
 
224
 
166
225
#ifdef ZIP
167
226
 
168
227
/***********************************************************************
177
236
    int n;                       /* index in random header */
178
237
    int t;                       /* temporary */
179
238
    int c;                       /* random byte */
180
 
    int ztemp;                   /* temporary for zencoded value */
181
 
    uch header[RAND_HEAD_LEN-2]; /* random header */
 
239
    uch header[RAND_HEAD_LEN];   /* random header */
182
240
    static unsigned calls = 0;   /* ensure different random header each time */
183
241
 
184
242
    /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
196
254
    /* Encrypt random header (last two bytes is high word of crc) */
197
255
    init_keys(passwd);
198
256
    for (n = 0; n < RAND_HEAD_LEN-2; n++) {
199
 
        ztemp = zencode(header[n], t);
200
 
        putc(ztemp, zfile);
 
257
        header[n] = (uch)zencode(header[n], t);
201
258
    }
202
 
    ztemp = zencode((int)(crc >> 16) & 0xff, t);
203
 
    putc(ztemp, zfile);
204
 
    ztemp = zencode((int)(crc >> 24) & 0xff, t);
205
 
    putc(ztemp, zfile);
 
259
    header[RAND_HEAD_LEN-2] = (uch)zencode((int)(crc >> 16) & 0xff, t);
 
260
    header[RAND_HEAD_LEN-1] = (uch)zencode((int)(crc >> 24) & 0xff, t);
 
261
    fwrite(header, 1, RAND_HEAD_LEN, f);
206
262
}
207
263
 
208
264
 
238
294
    crypthead(passwd, z->crc, dest);
239
295
 
240
296
    /* Skip local header in input file */
241
 
    if (fseek(source, (long)(4 + LOCHEAD + (ulg)z->nam + (ulg)z->ext),
 
297
    if (fseek(source, (long)((4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext),
242
298
              SEEK_CUR)) {
243
299
        return ferror(source) ? ZE_READ : ZE_EOF;
244
300
    }
256
312
        return ferror(source) ? ZE_READ : ZE_EOF;
257
313
    }
258
314
    if (fflush(dest) == EOF) return ZE_TEMP;
 
315
 
 
316
    /* Update number of bytes written to output file */
 
317
    tempzn += (4 + LOCHEAD) + z->nam + z->ext + z->siz;
 
318
 
259
319
    return ZE_OK;
260
320
}
261
321
 
268
328
    FILE *source, *dest;  /* source and destination files */
269
329
    ZCONST char *passwd;  /* password string */
270
330
{
271
 
    int c0, c1;           /* last two input bytes */
 
331
#ifdef ZIP10
 
332
    int c0                /* byte preceding the last input byte */
 
333
#endif
 
334
    int c1;               /* last input byte */
272
335
    ulg offset;           /* used for file offsets */
273
336
    ulg size;             /* size of input data */
274
337
    int r;                /* size of encryption header */
277
340
 
278
341
    /* Save position and skip local header in input file */
279
342
    if ((offset = (ulg)ftell(source)) == (ulg)-1L ||
280
 
        fseek(source, (long)(4 + LOCHEAD + (ulg)z->nam + (ulg)z->ext),
 
343
        fseek(source, (long)((4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext),
281
344
              SEEK_CUR)) {
282
345
        return ferror(source) ? ZE_READ : ZE_EOF;
283
346
    }
287
350
    /* Decrypt encryption header, save last two bytes */
288
351
    c1 = 0;
289
352
    for (r = RAND_HEAD_LEN; r; r--) {
 
353
#ifdef ZIP10
290
354
        c0 = c1;
 
355
#endif
291
356
        if ((c1 = getc(source)) == EOF) {
292
357
            return ferror(source) ? ZE_READ : ZE_EOF;
293
358
        }
305
370
    if ((ush)(c0 | (c1<<8)) !=
306
371
        (z->flg & 8 ? (ush) z->tim & 0xffff : (ush)(z->crc >> 16))) {
307
372
#else
308
 
    c0++; /* avoid warning on unused variable */
309
373
    if ((ush)c1 != (z->flg & 8 ? (ush) z->tim >> 8 : (ush)(z->crc >> 24))) {
310
374
#endif
311
375
        if (fseek(source, offset, SEEK_SET)) {
339
403
    }
340
404
    if (fflush(dest) == EOF) return ZE_TEMP;
341
405
 
 
406
    /* Update number of bytes written to output file */
 
407
    tempzn += (4 + LOCHEAD) + z->nam + z->ext + z->siz;
 
408
 
342
409
    return ZE_OK;
343
410
}
344
411
 
348
415
/***********************************************************************
349
416
 * If requested, encrypt the data in buf, and in any case call fwrite()
350
417
 * with the arguments to zfwrite().  Return what fwrite() returns.
 
418
 *
 
419
 * A bug has been found when encrypting large files.  See trees.c
 
420
 * for details and the fix.
351
421
 */
352
422
unsigned zfwrite(buf, item_size, nb, f)
353
423
    zvoid *buf;                 /* data buffer */