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

📄 ztrees.cpp

📁 300种加密解密算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	for (n = 0; n < elems; n++) {
		if (tree[n].Freq != 0) {
			heap[++heap_len] = max_code = n;
			depth[n] = 0;
		} else {
			tree[n].Len = 0;
		}
	}

	/* The pkzip format requires that at least one distance code exists,
	 * and that at least one bit should be sent even if there is only one
	 * possible code. So to avoid special checks later on we force at least
	 * two codes of non zero frequency.
	 */
	while (heap_len < 2) {
		int _new = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
		tree[_new].Freq = 1;
		depth[_new] = 0;
		opt_len--; if (stree) static_len -= stree[_new].Len;
		/* new is 0 or 1 so it does not have extra bits */
	}
	desc->max_code = max_code;

	/* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
	 * establish sub-heaps of increasing lengths:
	 */
	for (n = heap_len/2; n >= 1; n--) pqdownheap(tree, n);

	/* Construct the Huffman tree by repeatedly combining the least two
	 * frequent nodes.
	 */
	do {
		pqremove(tree, n);   /* n = node of least frequency */
		m = heap[SMALLEST];  /* m = node of next least frequency */

		heap[--heap_max] = n; /* keep the nodes sorted by frequency */
		heap[--heap_max] = m;

		/* Create a new node father of n and m */
		tree[node].Freq = tree[n].Freq + tree[m].Freq;
		depth[node] = (byte) (MAX(depth[n], depth[m]) + 1);
		tree[n].Dad = tree[m].Dad = node;
#ifdef DUMP_BL_TREE
		if (tree == bl_tree) {
			fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
					node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
		}
#endif
		/* and insert the new node in the heap */
		heap[SMALLEST] = node++;
		pqdownheap(tree, SMALLEST);

	} while (heap_len >= 2);

	heap[--heap_max] = heap[SMALLEST];

	/* At this point, the fields freq and dad are set. We can now
	 * generate the bit lengths.
	 */
	gen_bitlen(desc);

	/* The field len is now set, we can generate the bit codes */
	gen_codes (tree, max_code);
}

/* ===========================================================================
 * Scan a literal or distance tree to determine the frequencies of the codes
 * in the bit length tree. Updates opt_len to take into account the repeat
 * counts. (The contribution of the bit length codes will be added later
 * during the construction of bl_tree.)
 */
void CodeTree::scan_tree (ct_data *tree, int max_code)
{
	int n;                     /* iterates over all tree elements */
	int prevlen = -1;          /* last emitted length */
	int curlen;                /* length of current code */
	int nextlen = tree[0].Len; /* length of next code */
	int count = 0;             /* repeat count of the current code */
	int max_count = 7;         /* max repeat count */
	int min_count = 4;         /* min repeat count */

	if (nextlen == 0) max_count = 138, min_count = 3;
	tree[max_code+1].Len = (word16)-1; /* guard */

	for (n = 0; n <= max_code; n++) {
		curlen = nextlen; nextlen = tree[n+1].Len;
		if (++count < max_count && curlen == nextlen) {
			continue;
		} else if (count < min_count) {
			bl_tree[curlen].Freq += count;
		} else if (curlen != 0) {
			if (curlen != prevlen) bl_tree[curlen].Freq++;
			bl_tree[REP_3_6].Freq++;
		} else if (count <= 10) {
			bl_tree[REPZ_3_10].Freq++;
		} else {
			bl_tree[REPZ_11_138].Freq++;
		}
		count = 0; prevlen = curlen;
		if (nextlen == 0) {
			max_count = 138, min_count = 3;
		} else if (curlen == nextlen) {
			max_count = 6, min_count = 3;
		} else {
			max_count = 7, min_count = 4;
		}
	}
}

/* Send a literal or distance tree in compressed form,
   using the codes in bl_tree. */
void CodeTree::send_tree (ct_data *tree, int max_code)
{
   int n;                     /* iterates over all tree elements */
   int prevlen = -1;          /* last emitted length */
   int curlen;                /* length of current code */
   int nextlen = tree[0].Len; /* length of next code */
   int count = 0;             /* repeat count of the current code */
   int max_count = 7;         /* max repeat count */
   int min_count = 4;         /* min repeat count */

   /* tree[max_code+1].Len = -1; */  /* guard already set */
   if (nextlen == 0) max_count = 138, min_count = 3;

   for (n = 0; n <= max_code; n++) {
	  curlen = nextlen; nextlen = tree[n+1].Len;
	  if (++count < max_count && curlen == nextlen) {
		 continue;
	  } else if (count < min_count) {
		 do {
			send_code(curlen, bl_tree);
		 } while (--count != 0);
	  } else if (curlen != 0) {
		 if (curlen != prevlen) {
			send_code(curlen, bl_tree);
			count--;
		 }
		 assert(count >= 3 && count <= 6);
		 send_code(REP_3_6, bl_tree);
		 send_bits(count-3, 2);
	  } else if (count <= 10) {
		 send_code(REPZ_3_10, bl_tree);
		 send_bits(count-3, 3);
	  } else {
		 send_code(REPZ_11_138, bl_tree);
		 send_bits(count-11, 7);
	  }
	  count = 0; prevlen = curlen;
	  if (nextlen == 0) {
		 max_count = 138, min_count = 3;
	  } else if (curlen == nextlen) {
		 max_count = 6, min_count = 3;
	  } else {
		 max_count = 7, min_count = 4;
	  }
   }
}

/* Construct the Huffman tree for the bit lengths and return the index in
   bl_order of the last bit length code to send. */
int CodeTree::build_bl_tree()
{
	int max_blindex;  /* index of last bit length code of non zero freq */

	/* Determine the bit length frequencies for literal and distance trees */
	scan_tree(dyn_ltree, l_desc.max_code);
	scan_tree(dyn_dtree, d_desc.max_code);

	/* Build the bit length tree: */
	build_tree(&bl_desc);
	/* opt_len now includes the length of the tree representations, except
	 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
	 */

	/* Determine the number of bit length codes to send. The pkzip format
	 * requires that at least 4 bit length codes be sent. (appnote.txt says
	 * 3 but the actual value used is 4.)
	 */
	for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
		if (bl_tree[bl_order[max_blindex]].Len != 0) break;
	}
	/* Update opt_len to include the bit length tree and counts */
	opt_len += 3*(max_blindex+1) + 5+5+4;
//    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", opt_len, static_len));

	return max_blindex;
}

/* Send the header for a block using dynamic Huffman trees: the counts, the
 * lengths of the bit length codes, the literal tree and the distance tree.
 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. */
void CodeTree::send_all_trees(int lcodes, int dcodes, int blcodes)
{
   int rank;                    /* index in bl_order */

   assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4);
   assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES);
//   Tracev((stderr, "\nbl counts: "));
   send_bits(lcodes-257, 5);
   /* not +255 as stated in appnote.txt 1.93a or -256 in 2.04c */
   send_bits(dcodes-1,   5);
   /* not -3 as stated in appnote.txt */
   send_bits(blcodes-4,  4);
   for (rank = 0; rank < blcodes; rank++) {
//      Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
	  send_bits(bl_tree[bl_order[rank]].Len, 3);
   }
//   Tracev((stderr, "\nbl tree: sent %ld", bits_sent));

   /* send the literal tree */
   send_tree(dyn_ltree, lcodes-1);
//   Tracev((stderr, "\nlit tree: sent %ld", bits_sent));

   /* send the distance tree */
   send_tree(dyn_dtree, dcodes-1);
//   Tracev((stderr, "\ndist tree: sent %ld", bits_sent));
}

/* ===========================================================================
 * Determine the best encoding for the current block: dynamic trees, static
 * trees or store, and output the encoded block to the zip file. This function
 * returns the total compressed length for the file so far.
 */
word32 CodeTree::flush_block(byte *buf, word32 stored_len, int eof)
{
   word32 opt_lenb, static_lenb; /* opt_len and static_len in bytes */
   int max_blindex;  /* index of last bit length code of non zero freq */

   flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */

   /* Construct the literal and distance trees */
   build_tree(&l_desc);
//   Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));

   build_tree(&d_desc);
//   Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len));
   /* At this point, opt_len and static_len are the total bit lengths of
	* the compressed block data, excluding the tree representations.
	*/

   /* Build the bit length tree for the above two trees, and get the index
	* in bl_order of the last bit length code to send.
	*/
   max_blindex = build_bl_tree();

   /* Determine the best encoding. Compute first the block length in bytes */
   opt_lenb = (opt_len+3+7)>>3;
   static_lenb = (static_len+3+7)>>3;
   input_len += stored_len; /* for debugging only */

//   Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
//           opt_lenb, opt_len, static_lenb, static_len, stored_len,
//           last_lit, last_dist));

   if (static_lenb <= opt_lenb) opt_lenb = static_lenb;

#ifdef FORCE_METHOD
   if (level == 2 && buf) /* force stored block */
#else
   if (stored_len+4 <= opt_lenb && buf) /* 4: two words for the lengths */
#endif
   {
	   /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
		* Otherwise we can't have processed more than WSIZE input bytes since
		* the last block flush, because compression would have been
		* successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
		* transform a block into a stored block.
		*/
	   /* send block type */
	   send_bits((STORED_BLOCK<<1)+eof, 3);
	   compressed_len = (compressed_len + 3 + 7) & ~7L;
	   compressed_len += (stored_len + 4) << 3;
	   /* with header */
	   copy_block(buf, (unsigned)stored_len, 1);
   }
#ifdef FORCE_METHOD
   else if (level == 3) /* force static trees */
#else
   else if (static_lenb == opt_lenb)
#endif
   {
	   send_bits((STATIC_TREES<<1)+eof, 3);
	   compress_block(static_ltree,static_dtree);
	   compressed_len += 3 + static_len;
   } else {
	   send_bits((DYN_TREES<<1)+eof, 3);
	   send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
	   compress_block(dyn_ltree,dyn_dtree);
	   compressed_len += 3 + opt_len;
   }
//   assert (compressed_len == bits_sent);
   init_block();

   if (eof) {
//      assert (input_len == isize);
	  bi_windup();
	  compressed_len += 7;  /* align on byte boundary */
   }
//   Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
//          compressed_len-7*eof));

   return compressed_len >> 3;
}

/* Save the match info and tally the frequency counts.
   Return true if the current block must be flushed. */
int CodeTree::ct_tally (int dist, int lc)
{
   l_buf[last_lit++] = (byte)lc;
   if (dist == 0) {
	  /* lc is the unmatched char */
	  dyn_ltree[lc].Freq++;
   } else {
	  /* Here, lc is the match length - MIN_MATCH */
	  dist--;             /* dist = match distance - 1 */
	  assert((word16)dist < (word16)MAX_DIST &&
			 (word16)lc <= (word16)(MAX_MATCH-MIN_MATCH) &&
			 (word16)d_code(dist) < (word16)D_CODES);

	  dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
	  dyn_dtree[d_code(dist)].Freq++;

	  d_buf[last_dist++] = dist;
	  flags |= flag_bit;
   }
   flag_bit <<= 1;

   /* Output the flags if they fill a byte: */
   if ((last_lit & 7) == 0) {
	  flag_buf[last_flags++] = flags;
	  flags = 0, flag_bit = 1;
   }
   /* Try to guess if it is profitable to stop the current block here */
   if (deflate_level > 2 && (last_lit & 0xfff) == 0) {
	  /* Compute an upper bound for the compressed length */
	  word32 out_length = (word32)last_lit*8L;
	  word32 in_length = (word32)strstart-block_start;
	  int dcode;
	  for (dcode = 0; dcode < D_CODES; dcode++) {
		 out_length += (word32)dyn_dtree[dcode].Freq*(5L+extra_dbits[dcode]);
	  }
	  out_length >>= 3;
//      Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
//            last_lit, last_dist, in_length, out_length,
//            100L - out_length*100L/in_length));
	   if (last_dist < last_lit/2 && out_length < in_length/2) return 1;
   }
   return (last_lit == LIT_BUFSIZE-1 || last_dist == (unsigned)DIST_BUFSIZE);
   /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
	* on 16 bit machines and because stored blocks are restricted to
	* 64K-1 bytes. */
}

/* Send the block data compressed using the given Huffman trees */
void CodeTree::compress_block(ct_data *ltree, ct_data *dtree)
{
   unsigned dist;      /* distance of matched string */
   int lc;             /* match length or unmatched char (if dist == 0) */
   unsigned lx = 0;    /* running index in l_buf */
   unsigned dx = 0;    /* running index in d_buf */
   unsigned fx = 0;    /* running index in flag_buf */
   byte flag = 0;       /* current flags */
   unsigned code;      /* the code to send */
   int extra;          /* number of extra bits to send */

   if (last_lit != 0)
	  do {
		 if ((lx & 7) == 0) flag = flag_buf[fx++];
		 lc = l_buf[lx++];
		 if ((flag & 1) == 0) {
			/* send a literal byte */
			send_code(lc, ltree);
//            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
		 } else {
			/* Here, lc is the match length - MIN_MATCH */
			code = length_code[lc];
			/* send the length code */
			send_code(code+LITERALS+1, ltree);
			if ((extra = extra_lbits[code]) != 0) {
			   lc -= base_length[code];
			   /* send the extra length bits */
			   send_bits(lc, extra);
			}
			dist = d_buf[dx++];
			/* Here, dist is the match distance - 1 */
			code = d_code(dist);
			assert(code < D_CODES);

			/* send the distance code */
			send_code(code, dtree);
			if ((extra = extra_dbits[code]) != 0) {
			   dist -= base_dist[code];
			   /* send the extra distance bits */
			   send_bits(dist, extra);
			 }
		 } /* literal or match pair ? */
		 flag >>= 1;
	 } while (lx < last_lit);

   send_code(END_BLOCK, ltree);
}

NAMESPACE_END

⌨️ 快捷键说明

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