123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690 |
- #define ZCRYPT_INTERNAL
- #include "zip.h"
- #include "crypt.h"
- #include "ttyio.h"
- #if CRYPT
- #ifndef FALSE
- # define FALSE 0
- #endif
- #ifdef ZIP
-
- # include <time.h> /* time() function supplies first part of crypt seed */
-
- # ifndef ZCR_SEED2
- # define ZCR_SEED2 (unsigned)3141592654L
- # endif
- # ifdef GLOBAL
- # undef GLOBAL
- # endif
- # define GLOBAL(g) g
- #else
- # define GLOBAL(g) G.g
- #endif
- #ifdef UNZIP
-
- # ifndef FUNZIP
- local int testp OF((__GPRO__ ZCONST uch *h));
- local int testkey OF((__GPRO__ ZCONST uch *h, ZCONST char *key));
- # endif
- #else
- local z_uint4 keys[3];
- #endif
- #ifndef Trace
- # ifdef CRYPT_DEBUG
- # define Trace(x) fprintf x
- # else
- # define Trace(x)
- # endif
- #endif
- #include "crc32.h"
- #ifdef IZ_CRC_BE_OPTIMIZ
- local z_uint4 near crycrctab[256];
- local z_uint4 near *cry_crctb_p = NULL;
- local z_uint4 near *crytab_init OF((__GPRO));
- # define CRY_CRC_TAB cry_crctb_p
- # undef CRC32
- # define CRC32(c, b, crctab) (crctab[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
- #else
- # define CRY_CRC_TAB CRC_32_TAB
- #endif
- int decrypt_byte(__G)
- __GDEF
- {
- unsigned temp;
- temp = ((unsigned)GLOBAL(keys[2]) & 0xffff) | 2;
- return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
- }
- int update_keys(__G__ c)
- __GDEF
- int c;
- {
- GLOBAL(keys[0]) = CRC32(GLOBAL(keys[0]), c, CRY_CRC_TAB);
- GLOBAL(keys[1]) = (GLOBAL(keys[1])
- + (GLOBAL(keys[0]) & 0xff))
- * 134775813L + 1;
- {
- register int keyshift = (int)(GLOBAL(keys[1]) >> 24);
- GLOBAL(keys[2]) = CRC32(GLOBAL(keys[2]), keyshift, CRY_CRC_TAB);
- }
- return c;
- }
- void init_keys(__G__ passwd)
- __GDEF
- ZCONST char *passwd;
- {
- #ifdef IZ_CRC_BE_OPTIMIZ
- if (cry_crctb_p == NULL) {
- cry_crctb_p = crytab_init(__G);
- }
- #endif
- GLOBAL(keys[0]) = 305419896L;
- GLOBAL(keys[1]) = 591751049L;
- GLOBAL(keys[2]) = 878082192L;
- while (*passwd != '\0') {
- update_keys(__G__ (int)*passwd);
- passwd++;
- }
- }
- #ifdef IZ_CRC_BE_OPTIMIZ
- local z_uint4 near *crytab_init(__G)
- __GDEF
- {
- int i;
- for (i = 0; i < 256; i++) {
- crycrctab[i] = REV_BE(CRC_32_TAB[i]);
- }
- return crycrctab;
- }
- #endif
- #ifdef ZIP
- void crypthead(passwd, crc)
- ZCONST char *passwd;
- ulg crc;
- {
- int n;
- int t;
- int c;
- uch header[RAND_HEAD_LEN];
- static unsigned calls = 0;
-
- if (++calls == 1) {
- srand((unsigned)time(NULL) ^ ZCR_SEED2);
- }
- init_keys(passwd);
- for (n = 0; n < RAND_HEAD_LEN-2; n++) {
- c = (rand() >> 7) & 0xff;
- header[n] = (uch)zencode(c, t);
- }
-
- init_keys(passwd);
- for (n = 0; n < RAND_HEAD_LEN-2; n++) {
- header[n] = (uch)zencode(header[n], t);
- }
- header[RAND_HEAD_LEN-2] = (uch)zencode((int)(crc >> 16) & 0xff, t);
- header[RAND_HEAD_LEN-1] = (uch)zencode((int)(crc >> 24) & 0xff, t);
- bfwrite(header, 1, RAND_HEAD_LEN, BFWRITE_DATA);
- }
- #ifdef UTIL
- int zipcloak(z, passwd)
- struct zlist far *z;
- ZCONST char *passwd;
- {
- int c;
- int res;
- zoff_t n;
- int t;
- struct zlist far *localz;
- uch buf[1024];
- int b;
-
- if ((n = (zoff_t)zftello(y)) == (zoff_t)-1L) return ZE_TEMP;
-
-
- res = readlocal(&localz, z);
-
- z->dsk = 0;
- z->off = n;
-
- z->flg |= 1, z->flg &= ~8;
- localz->lflg |= 1, localz->lflg &= ~8;
-
- localz->siz += RAND_HEAD_LEN;
- z->siz = localz->siz;
-
- if ((res = putlocal(localz, PUTLOCAL_WRITE)) != ZE_OK) return res;
-
- crypthead(passwd, localz->crc);
-
- b = 0;
- for (n = z->siz - RAND_HEAD_LEN; n; n--) {
- if ((c = getc(in_file)) == EOF) {
- return ferror(in_file) ? ZE_READ : ZE_EOF;
- }
- buf[b] = (uch)zencode(c, t);
- b++;
- if (b >= 1024) {
-
- bfwrite(buf, 1, b, BFWRITE_DATA);
- b = 0;
- }
- }
- if (b) {
-
- bfwrite(buf, 1, b, BFWRITE_DATA);
- b = 0;
- }
-
-
-
- tempzn += (4 + LOCHEAD) + localz->nam + localz->ext + localz->siz;
-
- if (localz->ext) free(localz->extra);
- if (localz->nam) free(localz->iname);
- if (localz->nam) free(localz->name);
- #ifdef UNICODE_SUPPORT
- if (localz->uname) free(localz->uname);
- #endif
- free(localz);
- return ZE_OK;
- }
- int zipbare(z, passwd)
- struct zlist far *z;
- ZCONST char *passwd;
- {
- #ifdef ZIP10
- int c0
- #endif
- int c1;
-
- zoff_t size;
- struct zlist far *localz;
- uch buf[1024];
- int b;
- zoff_t n;
- int r;
- int res;
-
- if ((n = (zoff_t)zftello(y)) == (zoff_t)-1L) return ZE_TEMP;
-
- res = readlocal(&localz, z);
-
- z->dsk = 0;
- z->off = n;
-
- init_keys(passwd);
-
- c1 = 0;
- for (r = RAND_HEAD_LEN; r; r--) {
- #ifdef ZIP10
- c0 = c1;
- #endif
- if ((c1 = getc(in_file)) == EOF) {
- return ferror(in_file) ? ZE_READ : ZE_EOF;
- }
- Trace((stdout, " (%02x)", c1));
- zdecode(c1);
- Trace((stdout, " %02x", c1));
- }
- Trace((stdout, "\n"));
-
- #ifdef ZIP10
- if ((ush)(c0 | (c1<<8)) !=
- (z->flg & 8 ? (ush) z->tim & 0xffff : (ush)(z->crc >> 16))) {
- #else
- if ((ush)c1 != (z->flg & 8 ? (ush) z->tim >> 8 : (ush)(z->crc >> 24))) {
- #endif
- if (zfseeko(in_file, n, SEEK_SET)) {
- return ferror(in_file) ? ZE_READ : ZE_EOF;
- }
- if ((res = zipcopy(z)) != ZE_OK) {
- ziperr(res, "was copying an entry");
- }
- return ZE_MISS;
- }
- z->siz -= RAND_HEAD_LEN;
- localz->siz = z->siz;
- localz->flg = z->flg &= ~9;
- z->lflg = localz->lflg &= ~9;
- if ((res = putlocal(localz, PUTLOCAL_WRITE)) != ZE_OK) return res;
-
- b = 0;
- for (size = z->siz; size; size--) {
- if ((c1 = getc(in_file)) == EOF) {
- return ferror(in_file) ? ZE_READ : ZE_EOF;
- }
- zdecode(c1);
- buf[b] = c1;
- b++;
- if (b >= 1024) {
-
- bfwrite(buf, 1, b, BFWRITE_DATA);
- b = 0;
- }
- }
- if (b) {
-
- bfwrite(buf, 1, b, BFWRITE_DATA);
- b = 0;
- }
-
-
- tempzn += (4 + LOCHEAD) + localz->nam + localz->ext + localz->siz;
-
- if (localz->ext) free(localz->extra);
- if (localz->nam) free(localz->iname);
- if (localz->nam) free(localz->name);
- #ifdef UNICODE_SUPPORT
- if (localz->uname) free(localz->uname);
- #endif
- free(localz);
- return ZE_OK;
- }
- #else
- unsigned zfwrite(buf, item_size, nb)
- zvoid *buf;
- extent item_size;
- extent nb;
- #if 0
- FILE *f;
- #endif
- {
- int t;
- if (key != (char *)NULL) {
- ulg size;
- char *p = (char *)buf;
-
- for (size = item_size*(ulg)nb; size != 0; p++, size--) {
- *p = (char)zencode(*p, t);
- }
- }
-
- return bfwrite(buf, item_size, nb, BFWRITE_DATA);
- }
- #endif
- #endif
- #if (defined(UNZIP) && !defined(FUNZIP))
- 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)));
-
- 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)) {
- GLOBAL(newzip) = FALSE;
- if (passwrd != (char *)NULL) {
- if (!GLOBAL(key)) {
- if ((GLOBAL(key) = (char *)malloc(strlen(passwrd)+1)) ==
- (char *)NULL)
- return PK_MEM2;
- strcpy(GLOBAL(key), passwrd);
- GLOBAL(nopwd) = TRUE;
- }
- } else if (GLOBAL(key)) {
- free(GLOBAL(key));
- GLOBAL(key) = (char *)NULL;
- }
- }
-
- if (GLOBAL(key)) {
- if (!testp(__G__ h))
- return PK_COOL;
- else if (GLOBAL(nopwd))
- return PK_WARN;
- } else if ((GLOBAL(key) = (char *)malloc(IZ_PWLEN+1)) == (char *)NULL)
- return PK_MEM2;
-
- n = 0;
- do {
- r = (*G.decr_passwd)((zvoid *)&G, &n, GLOBAL(key), IZ_PWLEN+1,
- GLOBAL(zipfn), GLOBAL(filename));
- if (r == IZ_PW_ERROR) {
- free (GLOBAL(key));
- GLOBAL(key) = NULL;
- return PK_MEM2;
- }
- if (r != IZ_PW_ENTERED) {
- *GLOBAL(key) = '\0';
- n = 0;
- }
- if (!testp(__G__ h))
- return PK_COOL;
- if (r == IZ_PW_CANCELALL)
- GLOBAL(nopwd) = TRUE;
- } while (n > 0);
- return PK_WARN;
- }
- local int testp(__G__ h)
- __GDEF
- ZCONST uch *h;
- {
- int r;
- char *key_translated;
-
- #ifdef STR_TO_CP1
-
- if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
- return -1;
-
- r = testkey(__G__ h, STR_TO_CP1(key_translated, GLOBAL(key)));
- #else
-
- r = testkey(__G__ h, GLOBAL(key));
- #endif
- #ifdef STR_TO_CP2
- if (r != 0) {
- #ifndef STR_TO_CP1
-
- if ((key_translated = malloc(strlen(GLOBAL(key)) + 1)) == (char *)NULL)
- return -1;
- #endif
-
- r = testkey(__G__ h, STR_TO_CP2(key_translated, GLOBAL(key)));
- #ifdef STR_TO_CP3
- if (r != 0)
-
- r = testkey(__G__ h, STR_TO_CP3(key_translated, GLOBAL(key)));
- #endif
- #ifndef STR_TO_CP1
- free(key_translated);
- #endif
- }
- #endif
- #ifdef STR_TO_CP1
- free(key_translated);
- if (r != 0) {
-
- r = testkey(__G__ h, GLOBAL(key));
- }
- #endif
- return r;
- }
- local int testkey(__G__ h, key)
- __GDEF
- ZCONST uch *h;
- ZCONST char *key;
- {
- ush b;
- #ifdef ZIP10
- ush c;
- #endif
- int n;
- uch *p;
- uch hh[RAND_HEAD_LEN];
-
- init_keys(__G__ key);
- memcpy(hh, h, RAND_HEAD_LEN);
-
- 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 = %s\n",
- GLOBAL(incnt),
- fzofft(GLOBAL(cur_zipfile_bufstart)+(GLOBAL(inptr)-GLOBAL(inbuf)),
- NULL, NULL)));
-
- #ifdef ZIP10
- 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;
- #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;
- #endif
-
- for (n = (zoff_t)GLOBAL(incnt) > GLOBAL(csize) ?
- (int)GLOBAL(csize) : GLOBAL(incnt),
- p = GLOBAL(inptr); n--; p++)
- zdecode(*p);
- return 0;
- }
- #endif
- #else
- int zcr_dummy;
- #endif
|