⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgpzdeflate.c

📁 著名的加密软件的应用于电子邮件中
💻 C
📖 第 1 页 / 共 2 页
字号:
		match_start = 0;
		s_match_available = 0;
		s_match_length = 0;
		/* eofile is 1 and lookahead is 0 - no need to wipe */
	#endif

#ifdef DYN_ALLOC
	if (o_window != NULL) {
#ifdef PGP
 memset(o_window, 0, WSIZE);
 memset(o_window+WSIZE, 0, WSIZE+16);
#endif
 fcfree(o_window);
 o_window = window = NULL;
}
if (o_prev != NULL) {
#ifdef PGP
		memset(o_prev, 0, WSIZE/2*sizeof(Pos));
		memset(o_prev+WSIZE/2, 0, WSIZE/2*sizeof(Pos)+16);
		memset(o_head, 0, HASH_SIZE/2*sizeof(Pos));
		memset(o_head+HASH_SIZE/2, 0, HASH_SIZE/2*sizeof(Pos)+16);
	#endif
		fcfree(o_prev);
		fcfree(o_head);
		o_prev = prev = o_head = head = NULL;
	}
#endif /* DYN_ALLOC */
}

/* ===========================================================================
* Set match_start to the longest match starting at the given string and
* return its length. Matches shorter or equal to prev_length are discarded,
* in which case the result is equal to prev_length and match_start is
* garbage.
* IN assertions: cur_match is the head of the hash chain for the current
* string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
*/
#ifndef ASMV
/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
* match.s. The code is functionally equivalent, so you can use the C version
* if desired. A 68000 version is in amiga/match_68.a -- this could be used
* with other 68000 based systems such as Macintosh with a little effort.
*/
int
longest_match(IPos cur_match)
{
unsigned chain_length = max_chain_length; /* max hash chain length */
register byte *scan = window + strstart; /* current string */
register byte *match; /* matched string */
register int len; /* length of current match */
int best_len = prev_length; /* best match length so far */
IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
/* Stop when cur_match becomes <= limit. To simplify the code,
* we prevent matches with the string of window index 0.
*/

/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
* It is easy to get rid of this optimization if necessary.
*/
#if HASH_BITS < 8 || MAX_MATCH != 258
# error Code too clever
#endif

#if UNALIGNED_OK
/* Compare two bytes at a time. Note: this is not always beneficial.
* Try with and without -DUNALIGNED_OK to check.
*/
register byte *strend = window + strstart + MAX_MATCH - 1;
register word16 scan_start = *(word16 *)scan;
register word16 scan_end = *(word16 *)(scan+best_len-1);
#else
register byte *strend = window + strstart + MAX_MATCH;
register byte scan_end1 = scan[best_len-1];
register byte scan_end = scan[best_len];
#endif

		/* Do not waste too much time if we already have a good match: */
		if (prev_length >= good_match)
		chain_length >>= 2;
		ZipAssert(strstart <= window_size-MIN_LOOKAHEAD, "insufficient lookahead");

do {
		ZipAssert(cur_match < strstart, "no future");
		match = window + cur_match;

/* Skip to next match if the match length cannot increase
* or if the match length is less than 2:
*/
#if UNALIGNED_OK && MAX_MATCH == 258
		/* This code assumes sizeof(unsigned short) == 2. Do not use
		* UNALIGNED_OK if your compiler uses a different size.
		*/
		if (*(word16 *)(match+best_len-1) != scan_end ||
		*(word16 *)match != scan_start) continue;

		/* It is not necessary to compare scan[2] and match[2] since they are
		* always equal when the other bytes match, given that the hash keys
		* are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
		* strstart+3, +5, ... up to strstart+257. We check for insufficient
		* lookahead only every 4th comparison; the 128th check will be made
		* at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
		* necessary to put more guard bytes at the end of the window, or
		* to check more often for insufficient lookahead.
		*/
		scan++, match++;
		do {
		} while (*(word16 *)(scan+=2) == *(word16 *)(match+=2) &&
		 *(word16 *)(scan+=2) == *(word16 *)(match+=2) &&
		*(word16 *)(scan+=2) == *(word16 *)(match+=2) &&
		*(word16 *)(scan+=2) == *(word16 *)(match+=2) &&
		scan < strend);
		/* The funny "do {}" generates better code on most compilers */

		/* Here, scan <= window+strstart+257 */
		ZipAssert(scan <= window+(unsigned)(window_size-1), "wild scan");
		if (*scan == *match)
		 scan++;

		len = (MAX_MATCH - 1) - (int)(strend-scan);
		scan = strend - (MAX_MATCH-1);

#else /* UNALIGNED_OK */

		if (match[best_len] != scan_end ||
		match[best_len-1] != scan_end1 ||
		*match != *scan ||
	*++match != scan[1])	continue;

/* The check at best_len-1 can be removed because it will be made
		* again later. (This heuristic is not always a win.)
		* It is not necessary to compare scan[2] and match[2] since they
		* are always equal when the other bytes match, given that
		* the hash keys are equal and that HASH_BITS >= 8.
		*/
		scan += 2, match++;

		/* We check for insufficient lookahead only every 8th comparison;
		* the 256th check will be made at strstart+258.
		*/
		do {
		} while (*++scan == *++match && *++scan == *++match &&
		*++scan == *++match && *++scan == *++match &&
		 *++scan == *++match && *++scan == *++match &&
		*++scan == *++match && *++scan == *++match &&
		scan < strend);

		len = MAX_MATCH - (int)(strend - scan);
		scan = strend - MAX_MATCH;

#endif /* UNALIGNED_OK */

		if (len > best_len) {
		match_start = cur_match;
		best_len = len;
		if (len >= nice_match)
		 break;
#if UNALIGNED_OK
		scan_end = *(word16 *)(scan+best_len-1);
#else
		scan_end1 = scan[best_len-1];
		scan_end = scan[best_len];
	#endif
		}
} while ((cur_match = prev[cur_match & WMASK]) > limit
&& --chain_length != 0);

return best_len;
}
#endif /* ASMV */

#ifdef ZIPDEBUG
/* ===========================================================================
* Check that the match at match_start is indeed a match.
*/
static void
check_match(IPos start, IPos match, int length)
{
/* check that the match is indeed a match */
if (memcmp(window + match, window + start, length) != 0) {
fprintf(stderr, " start %d, match %d, length %d\n",
start, match, length);
error("invalid match");
}
if (verbose > 1) {
fprintf(stderr,"\\[%d,%d]", start-match, length);
do { putc(window[start++], stderr); } while (--length != 0);
}
  }
#else
# define check_match(start, match, length)
#endif

static void
slide_window(void)
	{
		register unsigned n, m;

		memcpy(window, window+WSIZE, (size_t)WSIZE);
		match_start -= WSIZE;
		strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */

		block_start -= (long) WSIZE;

		for (n = 0; n < HASH_SIZE; n++) {
		m = head[n];
		head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
		}
		for (n = 0; n < WSIZE; n++) {
		m = prev[n];
		prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
		/* If n is not on any hash chain, prev[n] is garbage but
		* its value will never be used.
		*/
		}
	}

/*	===========================================================================
* Flush the current block, with given end-of-file flag.
* IN assertion: strstart is set to the end of the current match.
*/
#define FLUSH_BLOCK(eof) \
	flush_block(block_start >= 0L ? (char*)&window[(unsigned)block_start] : \
  (char*)NULL, (word32)strstart - block_start, (eof))

/* ===========================================================================
* Same as above, but achieves better compression. We use a lazy
* evaluation for matches: a match is finally adopted only if there is
* no better match at the next window position.
*/
	static int
	deflate_sub(void)
{
	IPos hash_head;	/* head of hash chain */
	IPos prev_match;	/* previous match */
	int flush;	/* set if current block must be flushed */
int match_available = 0; /* set if previous match exists */
register unsigned match_length = MIN_MATCH-1; /* length of best match */

#if 0
if (level <= 3) return deflate_fast(); /* optimized for speed */
#endif

match_available = s_match_available;
match_length = s_match_length;

/* Process the input block. */
while (lookahead != 0) {
/* Insert the string window[strstart .. strstart+2] in the
* dictionary, and set hash_head to the head of the hash chain:
*/
INSERT_STRING(strstart, hash_head);

/* Find the longest match, discarding those <= prev_length.
*/
prev_length = match_length, prev_match = match_start;
match_length = MIN_MATCH-1;

if (hash_head != NIL && prev_length < max_lazy_match &&
			strstart - hash_head <= MAX_DIST) {
			/* To simplify the code, we prevent matches with the string
			* of window index 0 (in particular we have to avoid a match
			* of the string with itself at the start of the input file).
			*/
match_length = longest_match (hash_head);
/* longest_match() sets match_start */
if (match_length > lookahead) match_length = lookahead;

/* Ignore a length 3 match if it is too distant: */
if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
/* If prev_match is also MIN_MATCH, match_start is garbage
* but we will ignore the current match anyway.
*/
			match_length--;
			}
		}
/* If there was a match at the previous step and the current
* match is not better, output the previous match:
*/
if (prev_length >= MIN_MATCH && match_length <= prev_length) {

check_match(strstart-1, prev_match, prev_length);

flush = ct_tally(strstart-1-prev_match, prev_length - MIN_MATCH);

/* Insert in hash table all strings up to the end of the match.
* strstart-1 and strstart are already inserted.
*/
			lookahead -= prev_length-1;
			prev_length -= 2;
			do {
			strstart++;
			INSERT_STRING(strstart, hash_head);
			/* strstart never exceeds WSIZE-MAX_MATCH, so there are
			* always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
			* these bytes are garbage, but it does not matter since the
			* next lookahead bytes will always be emitted as literals.
			*/
} while (--prev_length != 0);
match_available = 0;
match_length = MIN_MATCH-1;
strstart++;
if (flush) FLUSH_BLOCK(0), block_start = strstart;

} else if (match_available) {
			/* If there was no match at the previous position, output a
			* single literal. If there was a match but the current match
			* is longer, truncate the previous match to a single literal.
			*/
			Tracevv((stderr,"%c",window[strstart-1]));
			if (ct_tally (0, window[strstart-1])) {
			FLUSH_BLOCK(0), block_start = strstart;
			}
			strstart++;
			lookahead--;
} else {
/* There is no previous match to compare with, wait for
* the next step to decide.
*/
			match_available = 1;
			strstart++;
			lookahead--;
		}

		/* Make sure that we always have enough lookahead, except
		* at the end of the input file. We need MAX_MATCH bytes
		* for the next match, plus MIN_MATCH bytes to insert the
		* string following the next match.
		*/
		if (lookahead < MIN_LOOKAHEAD && !eofile) {
			s_match_available = match_available;
			s_match_length = match_length;
			return 0;
			/* fill_window();*/
		}
}
if (match_available) ct_tally (0, window[strstart-1]);

	return FLUSH_BLOCK(1); /* eof */
}


/* Zip a buffer of input */
void
zip_input(char const *buf, unsigned len)
	{
		unsigned more;

		while (len) {
		more = (unsigned)((word32)2*WSIZE - lookahead - strstart);

		if (more <= 1) {
		slide_window();
		more += WSIZE;
		}
		if (more > len)
		more = len;
		memcpy(window+strstart+lookahead, buf, more);
		lookahead += more;

		if (lookahead >= MIN_LOOKAHEAD) {
		if (!strstart) { /* First time */
				register int j;
				ins_h = 0;
				for (j=0; j<MIN_MATCH-1; j++)
				UPDATE_HASH(ins_h, window[j]);
			}
		deflate_sub();
		}
		buf += more;
		len -= more;
}

		return;
	}

/* Zip EOF */
void
zip_finish(void)
	{
		eofile = 1;

		if (!strstart) { /* First time */
		register int j;
		ins_h = 0;
		for (j=0; j<MIN_MATCH-1; j++)
		UPDATE_HASH(ins_h, window[j]);
		}

		deflate_sub();

		return;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -