home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 April / VPR0304.ISO / OLS / ZCRYD001 / zcryd001.lzh / SRC / CRYPT.C next >
C/C++ Source or Header  |  1998-06-02  |  21KB  |  614 lines

  1. /*
  2.    crypt.c (full version) by Info-ZIP.      Last revised:  [see crypt.h]
  3.  
  4.    This code is not copyrighted and is put in the public domain.  The
  5.    encryption/decryption parts (as opposed to the non-echoing password
  6.    parts) were originally written in Europe; the whole file can there-
  7.    fore be freely distributed from any country except the USA.  If this
  8.    code is imported into the USA, it cannot be re-exported from from
  9.    there to another country.  (This restriction might seem curious, but
  10.    this is what US law requires.)
  11.  */
  12.  
  13. /* This encryption code is a direct transcription of the algorithm from
  14.    Roger Schlafly, described by Phil Katz in the file appnote.txt.  This
  15.    file (appnote.txt) is distributed with the PKZIP program (even in the
  16.    version without encryption capabilities).
  17.  */
  18. /*
  19.     DLL version is made by Yoshioka Tsuneo.(QWF00133@niftyserve.or.jp)
  20.     This code is also not copyrighted and is put in the public domain.
  21. */
  22.  
  23. #define ZCRYPT_INTERNAL
  24. #include "zip.h"
  25. #include "crypt.h"
  26. #include "ttyio.h"
  27. #include <windows.h>
  28. #define ZCrypt32_VERSION 1
  29. int WINAPI ZCrypt32GetVersion(void)
  30. {
  31.     return ZCrypt32_VERSION;
  32. }
  33. /* encode byte c, using temp t.  Warning: c must not have side effects. */
  34. int WINAPI zencode(char c,int *t)
  35. {
  36.     return (*t=decrypt_byte(__G), update_keys(c), *t^(c));
  37. }
  38. #define zencode(c,t)  (t=decrypt_byte(__G), update_keys(c), t^(c))
  39.  
  40. /* decode byte c in place */
  41. #define zdecode(c)   update_keys(__G__ c ^= decrypt_byte(__G))
  42. #ifdef FUNZIP
  43. #  define NEXTBYTE (encrypted? update_keys(__G__ getc(G.in)^decrypt_byte(__G)) : getc(G.in))
  44. #endif
  45.  
  46. #ifndef FALSE
  47. #  define FALSE 0
  48. #endif
  49.  
  50. #ifdef ZIP
  51.    /* For the encoding task used in Zip (and ZipCloak), we want to initialize
  52.       the crypt algorithm with some reasonably unpredictable bytes, see
  53.       the crypthead() function. The standard rand() library function is
  54.       used to supply these `random' bytes, which in turn is initialized by
  55.       a srand() call. The srand() function takes an "unsigned" (at least 16bit)
  56.       seed value as argument to determine the starting point of the rand()
  57.       pseudo-random number generator.
  58.       This seed number is constructed as "Seed = Seed1 .XOR. Seed2" with
  59.       Seed1 supplied by the current time (= "(unsigned)time()") and Seed2
  60.       as some (hopefully) nondeterministic bitmask. On many (most) systems,
  61.       we use some "process specific" number, as the PID or something similar,
  62.       but when nothing unpredictable is available, a fixed number may be
  63.       sufficient.
  64.       NOTE:
  65.       1.) This implementation requires the availability of the following
  66.           standard UNIX C runtime library functions: time(), rand(), srand().
  67.           On systems where some of them are missing, the environment that
  68.           incorporates the crypt routines must supply suitable replacement
  69.           functions.
  70.       2.) It is a very bad idea to use a second call to time() to set the
  71.           "Seed2" number! In this case, both "Seed1" and "Seed2" would be
  72.           (almost) identical, resulting in a (mostly) "zero" constant seed
  73.           number passed to srand().
  74.  
  75.       The implementation environment defined in the "zip.h" header should
  76.       supply a reasonable definition for ZCR_SEED2 (an unsigned number; for
  77.       most implementations of rand() and srand(), only the lower 16 bits are
  78.       significant!). An example that works on many systems would be
  79.            "#define ZCR_SEED2  (unsigned)getpid()".
  80.       The default definition for ZCR_SEED2 supplied below should be regarded
  81.       as a fallback to allow successful compilation in "beta state"
  82.       environments.
  83.     */
  84. #  include <time.h>     /* time() function supplies first part of crypt seed */
  85.    /* "last resort" source for second part of crypt seed pattern */
  86. #  ifndef ZCR_SEED2
  87. #    define ZCR_SEED2 (unsigned)3141592654L     /* use PI as default pattern */
  88. #  endif
  89. #  ifdef GLOBAL         /* used in Amiga system headers, maybe others too */
  90. #    undef GLOBAL
  91. #  endif
  92. #  define GLOBAL(g) g
  93. #else /* !ZIP */
  94. #  define GLOBAL(g) G.g
  95. #endif /* ?ZIP */
  96.  
  97.  
  98. #ifdef UNZIP
  99.    /* char *key = (char *)NULL; moved to globals.h */
  100. #  ifndef FUNZIP
  101.      local int testp OF((__GPRO__ uch *h));
  102.      local int testkey OF((__GPRO__ uch *h, char *key));
  103. #  endif
  104. #endif /* UNZIP */
  105.  
  106. #ifndef UNZIP             /* moved to globals.h for UnZip */
  107.    local ulg keys[3];     /* keys defining the pseudo-random sequence */
  108. #endif /* !UNZIP */
  109.  
  110. #ifndef Trace
  111. #  ifdef CRYPT_DEBUG
  112. #    define Trace(x) fprintf x
  113. #  else
  114. #    define Trace(x)
  115. #  endif
  116. #endif
  117.  
  118. #ifndef CRC_32_TAB
  119. #  define CRC_32_TAB     crc_32_tab
  120. #endif
  121.  
  122. #define CRC32(c, b) (CRC_32_TAB[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  123.  
  124. #ifndef UNZIP                   /* UnZip provides this in globals.h */
  125.    ulg near *crc_32_tab;
  126. void WINAPI zcrypt_set_crc_32_tab(ulg near *_crc_32_tab)
  127. {
  128.     crc_32_tab = _crc_32_tab;
  129. }
  130. #endif /* !UNZIP */
  131. #ifdef ZIP
  132. #  ifndef UTIL
  133.    char *key;
  134. void WINAPI zcrypt_set_key(char *_key)
  135. {
  136.     key = _key;
  137. }
  138. #  endif
  139. #endif /* ZIP */
  140.  
  141. /***********************************************************************
  142.  * Return the next byte in the pseudo-random sequence
  143.  */
  144. int WINAPI decrypt_byte(__G)
  145.     __GDEF
  146. {
  147.     unsigned temp;  /* POTENTIAL BUG:  temp*(temp^1) may overflow in an
  148.                      * unpredictable manner on 16-bit systems; not a problem
  149.                      * with any known compiler so far, though */
  150.  
  151.     temp = ((unsigned)GLOBAL(keys[2]) & 0xffff) | 2;
  152.     return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
  153. }
  154.  
  155. /***********************************************************************
  156.  * Update the encryption keys with the next byte of plain text
  157.  */
  158. int WINAPI update_keys(__G__ c)
  159.     __GDEF
  160.     int c;                  /* byte of plain text */
  161. {
  162.     GLOBAL(keys[0]) = CRC32(GLOBAL(keys[0]), c);
  163.     GLOBAL(keys[1]) += GLOBAL(keys[0]) & 0xff;
  164.     GLOBAL(keys[1]) = GLOBAL(keys[1]) * 134775813L + 1;
  165.     {
  166.       register int keyshift = (int)(GLOBAL(keys[1]) >> 24);
  167.       GLOBAL(keys[2]) = CRC32(GLOBAL(keys[2]), keyshift);
  168.     }
  169.     return c;
  170. }
  171.  
  172.  
  173. /***********************************************************************
  174.  * Initialize the encryption keys and the random header according to
  175.  * the given password.
  176.  */
  177. void WINAPI init_keys(__G__ passwd)
  178.     __GDEF
  179.     char *passwd;             /* password string with which to modify keys */
  180. {
  181.     GLOBAL(keys[0]) = 305419896L;
  182.     GLOBAL(keys[1]) = 591751049L;
  183.     GLOBAL(keys[2]) = 878082192L;
  184.     while (*passwd != '\0') {
  185.         update_keys(__G__ (int)*passwd);
  186.         passwd++;
  187.     }
  188. }
  189.  
  190.  
  191. #ifdef ZIP
  192.  
  193. /***********************************************************************
  194.  * Write encryption header to file zfile using the password passwd
  195.  * and the cyclic redundancy check crc.
  196.  */
  197. void WINAPI crypthead(passwd, crc, zfile)
  198.     char *passwd;                /* password string */
  199.     ulg crc;                     /* crc of file being encrypted */
  200.     FILE *zfile;                 /* where to write header */
  201. {
  202.     int n;                       /* index in random header */
  203.     int t;                       /* temporary */
  204.     int c;                       /* random byte */
  205.     int ztemp;                   /* temporary for zencoded value */
  206.     uch header[RAND_HEAD_LEN-2]; /* random header */
  207.     static unsigned calls = 0;   /* ensure different random header each time */
  208.  
  209.     /* First generate RAND_HEAD_LEN-2 random bytes. We encrypt the
  210.      * output of rand() to get less predictability, since rand() is
  211.      * often poorly implemented.
  212.      */
  213.     if (++calls == 1) {
  214.         srand((unsigned)time(NULL) ^ ZCR_SEED2);
  215.     }
  216.     init_keys(passwd);
  217.     for (n = 0; n < RAND_HEAD_LEN-2; n++) {
  218.         c = (rand() >> 7) & 0xff;
  219.         header[n] = (uch)zencode(c, t);
  220.     }
  221.     /* Encrypt random header (last two bytes is high word of crc) */
  222.     init_keys(passwd);
  223.     for (n = 0; n < RAND_HEAD_LEN-2; n++) {
  224.         ztemp = zencode(header[n], t);
  225.         putc(ztemp, zfile);
  226.     }
  227.     ztemp = zencode((int)(crc >> 16) & 0xff, t);
  228.     putc(ztemp, zfile);
  229.     ztemp = zencode((int)(crc >> 24) & 0xff, t);
  230.     putc(ztemp, zfile);
  231. }
  232.  
  233.  
  234. #ifdef UTIL
  235.  
  236. /***********************************************************************
  237.  * Encrypt the zip entry described by z from file source to file dest
  238.  * using the password passwd.  Return an error code in the ZE_ class.
  239.  */
  240. int WINAPI zipcloak(z, source, dest, passwd)
  241.     struct zlist far *z;    /* zip entry to encrypt */
  242.     FILE *source, *dest;    /* source and destination files */
  243.     char *passwd;           /* password string */
  244. {
  245.     int c;                  /* input byte */
  246.     int res;                /* result code */
  247.     ulg n;                  /* holds offset and counts size */
  248.     ush flag;               /* previous flags */
  249.     int t;                  /* temporary */
  250.     int ztemp;              /* temporary storage for zencode value */
  251.  
  252.     /* Set encrypted bit, clear extended local header bit and write local
  253.        header to output file */
  254.     if ((n = ftell(dest)) == -1L) return ZE_TEMP;
  255.     z->off = n;
  256.     flag = z->flg;
  257.     z->flg |= 1,  z->flg &= ~8;
  258.     z->lflg |= 1, z->lflg &= ~8;
  259.     z->siz += RAND_HEAD_LEN;
  260.     if ((res = putlocal(z, dest)) != ZE_OK) return res;
  261.  
  262.     /* Initialize keys with password and write random header */
  263.     crypthead(passwd, z->crc, dest);
  264.  
  265.     /* Skip local header in input file */
  266.     if (fseek(source, (long)(4 + LOCHEAD + (ulg)z->nam + (ulg)z->ext),
  267.               SEEK_CUR)) {
  268.         return ferror(source) ? ZE_READ : ZE_EOF;
  269.     }
  270.  
  271.     /* Encrypt data */
  272.     for (n = z->siz - RAND_HEAD_LEN; n; n--) {
  273.         if ((c = getc(source)) == EOF) {
  274.             return ferror(source) ? ZE_READ : ZE_EOF;
  275.         }
  276.         ztemp = zencode(c, t);
  277.         putc(ztemp, dest);
  278.     }
  279.     /* Skip extended local header in input file if there is one */
  280.     if ((flag & 8) != 0 && fseek(source, 16L, SEEK_CUR)) {
  281.         return ferror(source) ? ZE_READ : ZE_EOF;
  282.     }
  283.     if (fflush(dest) == EOF) return ZE_TEMP;
  284.     return ZE_OK;
  285. }
  286.  
  287. /***********************************************************************
  288.  * Decrypt the zip entry described by z from file source to file dest
  289.  * using the password passwd.  Return an error code in the ZE_ class.
  290.  */
  291. int WINAPI zipbare(__G__ z, source, dest, passwd)
  292.     __GDEF
  293.     struct zlist far *z;  /* zip entry to encrypt */
  294.     FILE *source, *dest;  /* source and destination files */
  295.     char *passwd;         /* password string */
  296. {
  297.     int c0, c1;           /* last two input bytes */
  298.     ulg offset;           /* used for file offsets */
  299.     ulg size;             /* size of input data */
  300.     int r;                /* size of encryption header */
  301.     int res;              /* return code */
  302.     ush flag;             /* previous flags */
  303.  
  304.     /* Save position and skip local header in input file */
  305.     if ((offset = ftell(source)) == -1L ||
  306.         fseek(source, (long)(4 + LOCHEAD + (ulg)z->nam + (ulg)z->ext),
  307.               SEEK_CUR)) {
  308.         return ferror(source) ? ZE_READ : ZE_EOF;
  309.     }
  310.     /* Initialize keys with password */
  311.     init_keys(passwd);
  312.  
  313.     /* Decrypt encryption header, save last two bytes */
  314.     c1 = 0;
  315.     for (r = RAND_HEAD_LEN; r; r--) {
  316.         c0 = c1;
  317.         if ((c1 = getc(source)) == EOF) {
  318.             return ferror(source) ? ZE_READ : ZE_EOF;
  319.         }
  320.         Trace((stdout, " (%02x)", c1));
  321.         zdecode(c1);
  322.         Trace((stdout, " %02x", c1));
  323.     }
  324.     Trace((stdout, "\n"));
  325.  
  326.     /* If last two bytes of header don't match crc (or file time in the
  327.      * case of an extended local header), back up and just copy. For
  328.      * pkzip 2.0, the check has been reduced to one byte only.
  329.      */
  330. #ifdef ZIP10
  331.     if ((ush)(c0 | (c1<<8)) !=
  332.         (z->flg & 8 ? (ush) z->tim & 0xffff : (ush)(z->crc >> 16))) {
  333. #else
  334.     c0++; /* avoid warning on unused variable */
  335.     if ((ush)c1 != (z->flg & 8 ? (ush) z->tim >> 8 : (ush)(z->crc >> 24))) {
  336. #endif
  337.         if (fseek(source, offset, SEEK_SET)) {
  338.             return ferror(source) ? ZE_READ : ZE_EOF;
  339.         }
  340.         if ((res = zipcopy(z, source, dest)) != ZE_OK) return res;
  341.         return ZE_MISS;
  342.     }
  343.  
  344.     /* Clear encrypted bit and local header bit, and write local header to
  345.        output file */
  346.     if ((offset = ftell(dest)) == -1L) return ZE_TEMP;
  347.     z->off = offset;
  348.     flag = z->flg;
  349.     z->flg &= ~9;
  350.     z->lflg &= ~9;
  351.     z->siz -= RAND_HEAD_LEN;
  352.     if ((res = putlocal(z, dest)) != ZE_OK) return res;
  353.  
  354.     /* Decrypt data */
  355.     for (size = z->siz; size; size--) {
  356.         if ((c1 = getc(source)) == EOF) {
  357.             return ferror(source) ? ZE_READ : ZE_EOF;
  358.         }
  359.         zdecode(c1);
  360.         putc(c1, dest);
  361.     }
  362.     /* Skip extended local header in input file if there is one */
  363.     if ((flag & 8) != 0 && fseek(source, 16L, SEEK_CUR)) {
  364.         return ferror(source) ? ZE_READ : ZE_EOF;
  365.     }
  366.     if (fflush(dest) == EOF) return ZE_TEMP;
  367.  
  368.     return ZE_OK;
  369. }
  370.  
  371.  
  372. #else /* !UTIL */
  373.  
  374. /***********************************************************************
  375.  * If requested, encrypt the data in buf, and in any case call fwrite()
  376.  * with the arguments to zfwrite().  Return what fwrite() returns.
  377.  */
  378. void WINAPI zencode_n(char *ptr,int n)
  379. {
  380.     int i;
  381.     int t;
  382.  
  383.     for(i=0;i<n;i++){
  384.         *ptr = (char)zencode(*ptr,t);
  385.         ptr++;
  386.     }
  387. }
  388. unsigned WINAPI zfwrite(buf, item_size, nb, f)
  389.     zvoid *buf;                /* data buffer */
  390.     extent item_size;          /* size of each item in bytes */
  391.     extent nb;                 /* number of items */
  392.     FILE *f;                   /* file to write to */
  393. {
  394.     int t;                    /* temporary */
  395.  
  396.     if (key != (char *)NULL) { /* key is the global password pointer */
  397.         ulg size;              /* buffer size */
  398.         char *p = (char*)buf;  /* steps through buffer */
  399.  
  400.         /* Encrypt data in buffer */
  401. #if 0
  402.         for (size = item_size*(ulg)nb; size != 0; p++, size--) {
  403.             *p = (char)zencode(*p, t);
  404.         }
  405. #else
  406.         zencode_n(p,item_size*(ulg)nb);
  407. #endif
  408.     }
  409.     /* Write the buffer out */
  410.     return fwrite(buf, item_size, nb, f);
  411. }
  412.  
  413. #endif /* ?UTIL */
  414. #endif /* ZIP */
  415.  
  416.  
  417. #if (defined(UNZIP) && !defined(FUNZIP))
  418.  
  419. /***********************************************************************
  420.  * Get the password and set up keys for current zipfile member.  Return
  421.  * PK_ class error.
  422.  */
  423. int WINAPI decrypt(__G)
  424.     __GDEF
  425. {
  426.     ush b;
  427.     int n, r;
  428.     uch h[RAND_HEAD_LEN];
  429.  
  430.     Trace((stdout, "\n[incnt = %d]: ", GLOBAL(incnt)));
  431.  
  432.     /* get header once (turn off "encrypted" flag temporarily so we don't
  433.      * try to decrypt the same data twice) */
  434.     GLOBAL(pInfo->encrypted) = FALSE;
  435.     defer_leftover_input(__G);
  436.     for (n = 0; n < RAND_HEAD_LEN; n++) {
  437.         b = NEXTBYTE;
  438.         h[n] = (uch)b;
  439.         Trace((stdout, " (%02x)", h[n]));
  440.     }
  441.     undefer_input(__G);
  442.     GLOBAL(pInfo->encrypted) = TRUE;
  443.  
  444.     if (GLOBAL(newzip)) { /* this is first encrypted member in this zipfile */
  445.         GLOBAL(newzip) = FALSE;
  446.         if (GLOBAL(P_flag)) {     /* user gave password on command line */
  447.             if (!GLOBAL(key)) {
  448.                 if ((GLOBAL(key) = (char *)malloc(PWLEN+1)) == (char *)NULL)
  449.                     return PK_MEM2;
  450.                 strncpy(GLOBAL(key), GLOBAL(pwdarg), PWLEN);
  451.                 GLOBAL(nopwd) = TRUE;  /* inhibit password prompting! */
  452.             }
  453.         } else if (GLOBAL(key)) { /* get rid of previous zipfile's key */
  454.             free(GLOBAL(key));
  455.             GLOBAL(key) = (char *)NULL;
  456.         }
  457.     }
  458.  
  459.     /* if have key already, test it; else allocate memory for it */
  460.     if (GLOBAL(key)) {
  461.         if (!testp(__G__ h))
  462.             return PK_COOL;   /* existing password OK (else prompt for new) */
  463.         else if (GLOBAL(nopwd))
  464.             return PK_WARN;   /* user indicated no more prompting */
  465.     } else if ((GLOBAL(key) = (char *)malloc(PWLEN+1)) == (char *)NULL)
  466.         return PK_MEM2;
  467.  
  468.     /* try a few keys */
  469.     n = 0;
  470.     do {
  471.         r = (*G.decr_passwd)((zvoid *)&G, &n, GLOBAL(key), PWLEN+1,
  472.                              GLOBAL(zipfn), GLOBAL(filename));
  473.         if (r == IZ_PW_ERROR) {         /* internal error in fetch of PW */
  474.             free (GLOBAL(key));
  475.             GLOBAL(key) = NULL;
  476.             return PK_MEM2;
  477.         }
  478.         if (r != IZ_PW_ENTERED) {       /* user replied "skip" or "skip all" */
  479.             *GLOBAL(key) = '\0';        /*   We try the NIL password, ... */
  480.             n = 0;                      /*   and cancel fetch for this item. */
  481.         }
  482.         if (!testp(__G__ h))
  483.             return PK_COOL;
  484.         if (r == IZ_PW_CANCELALL)       /* User replied "Skip all" */
  485.             GLOBAL(nopwd) = TRUE;       /*   inhibit any further PW prompt! */
  486.     } while (n > 0);
  487.  
  488.     return PK_WARN;
  489.  
  490. } /* end function decrypt() */
  491.  
  492.  
  493.  
  494. /***********************************************************************
  495.  * Test the password.  Return -1 if bad, 0 if OK.
  496.  */
  497. local WINAPI int testp(__G__ h)
  498.     __GDEF
  499.     uch *h;
  500. {
  501.     int r;
  502.     char *key_translated;
  503.  
  504.     /* On systems with "obscure" native character coding (e.g., EBCDIC),
  505.      * the first test translates the password to the "main standard"
  506.      * character coding. */
  507.  
  508. #ifdef STR_TO_CP1
  509.     /* allocate buffer for translated password */
  510.     if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
  511.         return -1;
  512.     /* first try, test password translated "standard" charset */
  513.     r = testkey(__G__ h, STR_TO_CP1(key_translated, GLOBAL(key)));
  514. #else /* !STR_TO_CP1 */
  515.     /* first try, test password as supplied on the extractor's host */
  516.     r = testkey(__G__ h, GLOBAL(key));
  517. #endif /* ?STR_TO_CP1 */
  518.  
  519. #ifdef STR_TO_CP2
  520.     if (r != 0) {
  521. #ifndef STR_TO_CP1
  522.         /* now prepare for second (and maybe third) test with translated pwd */
  523.         if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
  524.             return -1;
  525. #endif
  526.         /* second try, password translated to alternate ("standard") charset */
  527.         r = testkey(__G__ h, STR_TO_CP2(key_translated, GLOBAL(key)));
  528. #ifdef STR_TO_CP3
  529.         if (r != 0)
  530.             /* third try, password translated to another "standard" charset */
  531.             r = testkey(__G__ h, STR_TO_CP3(key_translated, GLOBAL(key)));
  532. #endif
  533. #ifndef STR_TO_CP1
  534.         free(key_translated);
  535. #endif
  536.     }
  537. #endif /* STR_TO_CP2 */
  538.  
  539. #ifdef STR_TO_CP1
  540.     free(key_translated);
  541.     if (r != 0) {
  542.         /* last resort, test password as supplied on the extractor's host */
  543.         r = testkey(__G__ h, GLOBAL(key));
  544.     }
  545. #endif /* STR_TO_CP1 */
  546.  
  547.     return r;
  548.  
  549. } /* end function testp() */
  550.  
  551.  
  552. local WINAPI int testkey(__G__ h, key)
  553.     __GDEF
  554.     uch *h;             /* decrypted header */
  555.     char *key;          /* decryption password to test */
  556. {
  557.     ush b;
  558. #ifdef ZIP10
  559.     ush c;
  560. #endif
  561.     int n;
  562.     uch *p;
  563.     uch hh[RAND_HEAD_LEN]; /* decrypted header */
  564.  
  565.     /* set keys and save the encrypted header */
  566.     init_keys(__G__ key);
  567.     memcpy(hh, h, RAND_HEAD_LEN);
  568.  
  569.     /* check password */
  570.     for (n = 0; n < RAND_HEAD_LEN; n++) {
  571.         zdecode(hh[n]);
  572.         Trace((stdout, " %02x", hh[n]));
  573.     }
  574.  
  575.     Trace((stdout,
  576.       "\n  lrec.crc= %08lx  crec.crc= %08lx  pInfo->ExtLocHdr= %s\n",
  577.       GLOBAL(lrec.crc32), GLOBAL(pInfo->crc),
  578.       GLOBAL(pInfo->ExtLocHdr) ? "true":"false"));
  579.     Trace((stdout, "  incnt = %d  unzip offset into zipfile = %ld\n",
  580.       GLOBAL(incnt),
  581.       GLOBAL(cur_zipfile_bufstart)+(GLOBAL(inptr)-GLOBAL(inbuf))));
  582.  
  583.     /* same test as in zipbare(): */
  584.  
  585. #ifdef ZIP10 /* check two bytes */
  586.     c = hh[RAND_HEAD_LEN-2], b = hh[RAND_HEAD_LEN-1];
  587.     Trace((stdout,
  588.       "  (c | (b<<8)) = %04x  (crc >> 16) = %04x  lrec.time = %04x\n",
  589.       (ush)(c | (b<<8)), (ush)(GLOBAL(lrec.crc32) >> 16),
  590.       GLOBAL(lrec.last_mod_file_time)));
  591.     if ((ush)(c | (b<<8)) != (GLOBAL(pInfo->ExtLocHdr) ?
  592.                               GLOBAL(lrec.last_mod_file_time) :
  593.                               (ush)(GLOBAL(lrec.crc32) >> 16)))
  594.         return -1;  /* bad */
  595. #else
  596.     b = hh[RAND_HEAD_LEN-1];
  597.     Trace((stdout, "  b = %02x  (crc >> 24) = %02x  (lrec.time >> 8) = %02x\n",
  598.       b, (ush)(GLOBAL(lrec.crc32) >> 24),
  599.       (GLOBAL(lrec.last_mod_file_time) >> 8)));
  600.     if (b != (GLOBAL(pInfo->ExtLocHdr) ? GLOBAL(lrec.last_mod_file_time) >> 8 :
  601.         (ush)(GLOBAL(lrec.crc32) >> 24)))
  602.         return -1;  /* bad */
  603. #endif
  604.     /* password OK:  decrypt current buffer contents before leaving */
  605.     for (n = (long)GLOBAL(incnt) > GLOBAL(csize) ?
  606.              (int)GLOBAL(csize) : GLOBAL(incnt),
  607.          p = GLOBAL(inptr); n--; p++)
  608.         zdecode(*p);
  609.     return 0;       /* OK */
  610.  
  611. } /* end function testkey() */
  612.  
  613. #endif /* UNZIP && !FUNZIP */
  614.