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

📄 zdeflate.cpp

📁 300种加密解密的算法集
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	   } 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;
#ifdef 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 DEBUG
/* Check that the match at match_start is indeed a match. */
static void check_match(start, match, length)
IPos start, match;
int length;
{
   if (memcmp((char*)window + match, (char*)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

/* Add a block of data into the window. Updates strstart and lookahead.
 * IN assertion: lookahead < MIN_LOOKAHEAD.
 * Note: call with either lookahead == 0 or length == 0 is valid
 */
unsigned Deflator::fill_window(const byte *buffer, unsigned int length)
{
   register unsigned n, m;
   unsigned more = length;

   /* Amount of free space at the end of the window. */
   if (WINDOW_SIZE - lookahead - strstart < more) {
	  more = (unsigned)(WINDOW_SIZE - lookahead - strstart);
   }
   /* If the window is almost full and there is insufficient lookahead,
	* move the upper half to the lower one to make room in the upper half.
	*/
   if (strstart >= (unsigned)WSIZE+MAX_DIST) {
	  memcpy(window, window+WSIZE, WSIZE);
	  match_start -= WSIZE;
	  strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */

	  block_start -= (long) WSIZE;

	  for (n = 0; n < (unsigned)HASH_SIZE; n++) {
		 m = head[n];
		 head[n] = (Pos)(m >= (unsigned)WSIZE ? m-WSIZE : NIL);
	  }
	  for (n = 0; n < (unsigned)WSIZE; n++) {
		 m = prev[n];
		 prev[n] = (Pos)(m >= (unsigned)WSIZE ? m-WSIZE : NIL);
		 /* If n is not on any hash chain, prev[n] is garbage but
			its value will never be used. */
	  }
	  if ((more += WSIZE) > length) more = length;
   }
   if (more) {
	  memcpy((byte*)window+strstart+lookahead, buffer, more);
	  lookahead += more;
   }
   return more;
}

/* 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 ?\
		window+block_start : \
		(byte *)0, (long)strstart - block_start, (eof))

/* Processes a new input block.
 * This function does not perform lazy evaluationof matches and inserts
 * new strings in the dictionary only for unmatched strings or for short
 * matches. It is used only for the fast compression options. */
int Deflator::fast_deflate(const byte *buffer, unsigned int length)
{
   IPos hash_head; /* head of the hash chain */
   int flush;      /* set if current block must be flushed */
   unsigned accepted = 0;

   do {
	  /* 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. */
	  accepted += fill_window(buffer+accepted, length-accepted);
	  if (lookahead <= minlookahead) break;
	  if (!uptodate) {
		 match_length = 0; init_hash(); uptodate = 1;
	  }
	  while (lookahead > minlookahead) {
		 /* 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.
		  * At this point we have always match_length < MIN_MATCH */
		 if (hash_head != NIL && 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;
		 }
		 if (match_length >= MIN_MATCH) {
			check_match(strstart, match_start, match_length);

			flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);

			lookahead -= match_length;

			/* Insert new strings in the hash table only if the match length
			 * is not too large. This saves time but degrades compression.
			 */
			if (match_length <= max_insert_length) {
				match_length--; /* string at strstart already in hash table */
				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 be emitted as literals.
					 */
				} while (--match_length != 0);
				strstart++;
			} else {
				strstart += match_length;
				match_length = 0;
				ins_h = window[strstart];
				UPDATE_HASH(ins_h, window[strstart+1]);
/*
#if MIN_MATCH != 3
				Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
*/
			}
		 } else {
			/* No match, output a literal byte */
//            Tracevv((stderr,"%c",window[strstart]));
			flush = ct_tally (0, window[strstart]);
			lookahead--;
			strstart++;
		 }
		 if (flush) {
			FLUSH_BLOCK(0);
			block_start = strstart;
		 }
	  }
   } while (accepted < length);
   if (!minlookahead) {/* eof achieved */
	  FLUSH_BLOCK(1);
   }
   return accepted;
}

/* 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.  */
int Deflator::lazy_deflate(const byte *buffer, unsigned int length)
{
   IPos hash_head;          /* head of hash chain */
   IPos prev_match;         /* previous match */
   int flush;               /* set if current block must be flushed */
   register unsigned ml = match_length; /* length of best match */
#ifdef DEBUG
   extern word32 isize;        /* byte length of input file, for debug only */
#endif
   unsigned accepted = 0;

   /* Process the input block. */
   do {
	  /* 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. */
	  accepted += fill_window(buffer+accepted, length-accepted);
	  if (lookahead <= minlookahead) break;
	  if (!uptodate) {
		 ml = MIN_MATCH-1; /* length of best match */
		 init_hash();
		 uptodate = 1;
	  }
	  while (lookahead > minlookahead) {
		 INSERT_STRING(strstart, hash_head);

		 /* Find the longest match, discarding those <= prev_length. */
		 prev_length = ml, prev_match = match_start;
		 ml = 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).
			 */
			ml = longest_match (hash_head);
			/* longest_match() sets match_start */
			if (ml > lookahead) ml = lookahead;

			/* Ignore a length 3 match if it is too distant: */
			if (ml == 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. */
			   ml--;
			}
		 }
		 /* 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 && ml <= 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;
			ml = 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--;
		 }
//         assert(strstart <= isize && lookahead <= isize);
	  }
   } while (accepted < length);
   if (!minlookahead) {/* eof achieved */
	  if (match_available) ct_tally (0, window[strstart-1]);
	  FLUSH_BLOCK(1);
   }
   match_length = ml;
   return accepted;
}

NAMESPACE_END

⌨️ 快捷键说明

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