📄 crypt.c
字号:
Trace((stdout, " %02x", c1)); } Trace((stdout, "\n")); /* If last two bytes of header don't match crc (or file time in the * case of an extended local header), back up and just copy. For * pkzip 2.0, the check has been reduced to one byte only. */#ifdef ZIP10 if ((ush)(c0 | (c1<<8)) != (z->flg & 8 ? (ush) z->tim & 0xffff : (ush)(z->crc >> 16))) {#else c0++; /* avoid warning on unused variable */ if ((ush)c1 != (z->flg & 8 ? (ush) z->tim >> 8 : (ush)(z->crc >> 24))) {#endif if (fseek(source, offset, SEEK_SET)) { return ferror(source) ? ZE_READ : ZE_EOF; } if ((res = zipcopy(z, source, dest)) != ZE_OK) return res; return ZE_MISS; } /* Clear encrypted bit and local header bit, and write local header to output file */ if ((offset = (ulg)ftell(dest)) == (ulg)-1L) return ZE_TEMP; z->off = offset; flag = z->flg; z->flg &= ~9; z->lflg &= ~9; z->siz -= RAND_HEAD_LEN; if ((res = putlocal(z, dest)) != ZE_OK) return res; /* Decrypt data */ for (size = z->siz; size; size--) { if ((c1 = getc(source)) == EOF) { return ferror(source) ? ZE_READ : ZE_EOF; } zdecode(c1); putc(c1, dest); } /* Skip extended local header in input file if there is one */ if ((flag & 8) != 0 && fseek(source, 16L, SEEK_CUR)) { return ferror(source) ? ZE_READ : ZE_EOF; } if (fflush(dest) == EOF) return ZE_TEMP; return ZE_OK;}#else /* !UTIL *//*********************************************************************** * If requested, encrypt the data in buf, and in any case call fwrite() * with the arguments to zfwrite(). Return what fwrite() returns. */unsigned zfwrite(buf, item_size, nb, f) zvoid *buf; /* data buffer */ extent item_size; /* size of each item in bytes */ extent nb; /* number of items */ FILE *f; /* file to write to */{ int t; /* temporary */ if (key != (char *)NULL) { /* key is the global password pointer */ ulg size; /* buffer size */ char *p = (char*)buf; /* steps through buffer */ /* Encrypt data in buffer */ for (size = item_size*(ulg)nb; size != 0; p++, size--) { *p = (char)zencode(*p, t); } } /* Write the buffer out */ return fwrite(buf, item_size, nb, f);}#endif /* ?UTIL */#endif /* ZIP */#if (defined(UNZIP) && !defined(FUNZIP))/*********************************************************************** * Get the password and set up keys for current zipfile member. * Return PK_ class error. */int decrypt(__G__ passwrd) __GDEF ZCONST char *passwrd;{ ush b; int n, r; uch h[RAND_HEAD_LEN]; Trace((stdout, "\n[incnt = %d]: ", GLOBAL(incnt))); /* get header once (turn off "encrypted" flag temporarily so we don't * try to decrypt the same data twice) */ GLOBAL(pInfo->encrypted) = FALSE; defer_leftover_input(__G); for (n = 0; n < RAND_HEAD_LEN; n++) { b = NEXTBYTE; h[n] = (uch)b; Trace((stdout, " (%02x)", h[n])); } undefer_input(__G); GLOBAL(pInfo->encrypted) = TRUE; if (GLOBAL(newzip)) { /* this is first encrypted member in this zipfile */ GLOBAL(newzip) = FALSE; if (passwrd != (char *)NULL) { /* user gave password on command line */ if (!GLOBAL(key)) { if ((GLOBAL(key) = (char *)malloc(strlen(passwrd)+1)) == (char *)NULL) return PK_MEM2; strcpy(GLOBAL(key), passwrd); GLOBAL(nopwd) = TRUE; /* inhibit password prompting! */ } } else if (GLOBAL(key)) { /* get rid of previous zipfile's key */ free(GLOBAL(key)); GLOBAL(key) = (char *)NULL; } } /* if have key already, test it; else allocate memory for it */ if (GLOBAL(key)) { if (!testp(__G__ h)) return PK_COOL; /* existing password OK (else prompt for new) */ else if (GLOBAL(nopwd)) return PK_WARN; /* user indicated no more prompting */ } else if ((GLOBAL(key) = (char *)malloc(IZ_PWLEN+1)) == (char *)NULL) return PK_MEM2; /* try a few keys */ n = 0; do { r = (*G.decr_passwd)((zvoid *)&G, &n, GLOBAL(key), IZ_PWLEN+1, GLOBAL(zipfn), GLOBAL(filename)); if (r == IZ_PW_ERROR) { /* internal error in fetch of PW */ free (GLOBAL(key)); GLOBAL(key) = NULL; return PK_MEM2; } if (r != IZ_PW_ENTERED) { /* user replied "skip" or "skip all" */ *GLOBAL(key) = '\0'; /* We try the NIL password, ... */ n = 0; /* and cancel fetch for this item. */ } if (!testp(__G__ h)) return PK_COOL; if (r == IZ_PW_CANCELALL) /* User replied "Skip all" */ GLOBAL(nopwd) = TRUE; /* inhibit any further PW prompt! */ } while (n > 0); return PK_WARN;} /* end function decrypt() *//*********************************************************************** * Test the password. Return -1 if bad, 0 if OK. */local int testp(__G__ h) __GDEF ZCONST uch *h;{ int r; char *key_translated; /* On systems with "obscure" native character coding (e.g., EBCDIC), * the first test translates the password to the "main standard" * character coding. */#ifdef STR_TO_CP1 /* allocate buffer for translated password */ if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL) return -1; /* first try, test password translated "standard" charset */ r = testkey(__G__ h, STR_TO_CP1(key_translated, GLOBAL(key)));#else /* !STR_TO_CP1 */ /* first try, test password as supplied on the extractor's host */ r = testkey(__G__ h, GLOBAL(key));#endif /* ?STR_TO_CP1 */#ifdef STR_TO_CP2 if (r != 0) {#ifndef STR_TO_CP1 /* now prepare for second (and maybe third) test with translated pwd */ if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL) return -1;#endif /* second try, password translated to alternate ("standard") charset */ r = testkey(__G__ h, STR_TO_CP2(key_translated, GLOBAL(key)));#ifdef STR_TO_CP3 if (r != 0) /* third try, password translated to another "standard" charset */ r = testkey(__G__ h, STR_TO_CP3(key_translated, GLOBAL(key)));#endif#ifndef STR_TO_CP1 free(key_translated);#endif }#endif /* STR_TO_CP2 */#ifdef STR_TO_CP1 free(key_translated); if (r != 0) { /* last resort, test password as supplied on the extractor's host */ r = testkey(__G__ h, GLOBAL(key)); }#endif /* STR_TO_CP1 */ return r;} /* end function testp() */local int testkey(__G__ h, key) __GDEF ZCONST uch *h; /* decrypted header */ ZCONST char *key; /* decryption password to test */{ ush b;#ifdef ZIP10 ush c;#endif int n; uch *p; uch hh[RAND_HEAD_LEN]; /* decrypted header */ /* set keys and save the encrypted header */ init_keys(__G__ key); memcpy(hh, h, RAND_HEAD_LEN); /* check password */ for (n = 0; n < RAND_HEAD_LEN; n++) { zdecode(hh[n]); Trace((stdout, " %02x", hh[n])); } Trace((stdout, "\n lrec.crc= %08lx crec.crc= %08lx pInfo->ExtLocHdr= %s\n", GLOBAL(lrec.crc32), GLOBAL(pInfo->crc), GLOBAL(pInfo->ExtLocHdr) ? "true":"false")); Trace((stdout, " incnt = %d unzip offset into zipfile = %ld\n", GLOBAL(incnt), GLOBAL(cur_zipfile_bufstart)+(GLOBAL(inptr)-GLOBAL(inbuf)))); /* same test as in zipbare(): */#ifdef ZIP10 /* check two bytes */ c = hh[RAND_HEAD_LEN-2], b = hh[RAND_HEAD_LEN-1]; Trace((stdout, " (c | (b<<8)) = %04x (crc >> 16) = %04x lrec.time = %04x\n", (ush)(c | (b<<8)), (ush)(GLOBAL(lrec.crc32) >> 16), ((ush)GLOBAL(lrec.last_mod_dos_datetime) & 0xffff)))); if ((ush)(c | (b<<8)) != (GLOBAL(pInfo->ExtLocHdr) ? ((ush)GLOBAL(lrec.last_mod_dos_datetime) & 0xffff) : (ush)(GLOBAL(lrec.crc32) >> 16))) return -1; /* bad */#else b = hh[RAND_HEAD_LEN-1]; Trace((stdout, " b = %02x (crc >> 24) = %02x (lrec.time >> 8) = %02x\n", b, (ush)(GLOBAL(lrec.crc32) >> 24), ((ush)GLOBAL(lrec.last_mod_dos_datetime) >> 8) & 0xff)); if (b != (GLOBAL(pInfo->ExtLocHdr) ? ((ush)GLOBAL(lrec.last_mod_dos_datetime) >> 8) & 0xff : (ush)(GLOBAL(lrec.crc32) >> 24))) return -1; /* bad */#endif /* password OK: decrypt current buffer contents before leaving */ for (n = (long)GLOBAL(incnt) > GLOBAL(csize) ? (int)GLOBAL(csize) : GLOBAL(incnt), p = GLOBAL(inptr); n--; p++) zdecode(*p); return 0; /* OK */} /* end function testkey() */#endif /* UNZIP && !FUNZIP */#else /* !CRYPT *//* something "externally visible" to shut up compiler/linker warnings */int zcr_dummy;#endif /* ?CRYPT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -