Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
trees.c File Reference
#include "deflate.h"

Go to the source code of this file.

Classes

struct  static_tree_desc_s
 

Macros

#define MAX_BL_BITS   7
 
#define END_BLOCK   256
 
#define REP_3_6   16
 
#define REPZ_3_10   17
 
#define REPZ_11_138   18
 
#define DIST_CODE_LEN   512 /* see definition of array dist_code below */
 
#define send_code(s, c, tree)   send_bits(s, tree[c].Code, tree[c].Len)
 
#define put_short(s, w)
 
#define send_bits(s, value, length)
 
#define SMALLEST   1
 
#define pqremove(s, tree, top)
 
#define smaller(tree, n, m, depth)
 

Functions

void tr_static_init OF ((void))
 
void init_block OF ((deflate_state *s))
 
void pqdownheap OF ((deflate_state *s, ct_data *tree, int k))
 
void gen_bitlen OF ((deflate_state *s, tree_desc *desc))
 
void gen_codes OF ((ct_data *tree, int max_code, ushf *bl_count))
 
void scan_tree OF ((deflate_state *s, ct_data *tree, int max_code))
 
void send_all_trees OF ((deflate_state *s, int lcodes, int dcodes, int blcodes))
 
void compress_block OF ((deflate_state *s, const ct_data *ltree, const ct_data *dtree))
 
unsigned bi_reverse OF ((unsigned code, int len))
 
void tr_static_init ()
 
void ZLIB_INTERNAL _tr_init (deflate_state *s)
 
void init_block (deflate_state *s)
 
void pqdownheap (deflate_state *s, ct_data *tree, int k)
 
void gen_bitlen (deflate_state *s, tree_desc *desc)
 
void gen_codes (ct_data *tree, int max_code, ushf *bl_count)
 
void build_tree (deflate_state *s, tree_desc *desc)
 
void scan_tree (deflate_state *s, ct_data *tree, int max_code)
 
void send_tree (deflate_state *s, ct_data *tree, int max_code)
 
int build_bl_tree (deflate_state *s)
 
void send_all_trees (deflate_state *s, int lcodes, int dcodes, int blcodes)
 
void ZLIB_INTERNAL _tr_stored_block (deflate_state *s, charf *buf, ulg stored_len, int last)
 
void ZLIB_INTERNAL _tr_flush_bits (deflate_state *s)
 
void ZLIB_INTERNAL _tr_align (deflate_state *s)
 
void ZLIB_INTERNAL _tr_flush_block (deflate_state *s, charf *buf, ulg stored_len, int last)
 
int ZLIB_INTERNAL _tr_tally (deflate_state *s, unsigned dist, unsigned lc)
 
void compress_block (deflate_state *s, const ct_data *ltree, const ct_data *dtree)
 
int detect_data_type (deflate_state *s)
 
unsigned bi_reverse (unsigned code, int len)
 
void bi_flush (deflate_state *s)
 
void bi_windup (deflate_state *s)
 

Variables

const int extra_lbits [LENGTH_CODES] = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}
 
const int extra_dbits [D_CODES] = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}
 
const int extra_blbits [BL_CODES] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}
 
const uch bl_order [BL_CODES] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}
 
ct_data static_ltree [L_CODES+2]
 
ct_data static_dtree [D_CODES]
 
uch _dist_code [DIST_CODE_LEN]
 
uch _length_code [MAX_MATCH-MIN_MATCH+1]
 
int base_length [LENGTH_CODES]
 
int base_dist [D_CODES]
 
const static_tree_desc static_l_desc
 
const static_tree_desc static_d_desc
 
const static_tree_desc static_bl_desc
 

Macro Definition Documentation

◆ DIST_CODE_LEN

#define DIST_CODE_LEN   512 /* see definition of array dist_code below */

Definition at line 80 of file trees.c.

◆ END_BLOCK

#define END_BLOCK   256

Definition at line 49 of file trees.c.

◆ MAX_BL_BITS

#define MAX_BL_BITS   7

Definition at line 46 of file trees.c.

◆ pqremove

#define pqremove (   s,
  tree,
  top 
)
Value:
{\
top = s->heap[SMALLEST]; \
s->heap[SMALLEST] = s->heap[s->heap_len--]; \
pqdownheap(s, tree, SMALLEST); \
}
#define SMALLEST
Definition: trees.c:421

Definition at line 429 of file trees.c.

◆ put_short

#define put_short (   s,
 
)
Value:
{ \
put_byte(s, (uch)((w) & 0xff)); \
put_byte(s, (uch)((ush)(w) >> 8)); \
}
unsigned short ush
Definition: zutil.h:40
unsigned char uch
Definition: zutil.h:38

Definition at line 173 of file trees.c.

◆ REP_3_6

#define REP_3_6   16

Definition at line 52 of file trees.c.

◆ REPZ_11_138

#define REPZ_11_138   18

Definition at line 58 of file trees.c.

◆ REPZ_3_10

#define REPZ_3_10   17

Definition at line 55 of file trees.c.

◆ send_bits

#define send_bits (   s,
  value,
  length 
)
Value:
{ int len = length;\
if (s->bi_valid > (int)Buf_size - len) {\
int val = (int)value;\
s->bi_buf |= (ush)val << s->bi_valid;\
put_short(s, s->bi_buf);\
s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
s->bi_valid += len - Buf_size;\
} else {\
s->bi_buf |= (ush)(value) << s->bi_valid;\
s->bi_valid += len;\
}\
}
#define Buf_size
Definition: deflate.h:50
#define put_short(s, w)
Definition: trees.c:173

Definition at line 210 of file trees.c.

◆ send_code

#define send_code (   s,
  c,
  tree 
)    send_bits(s, tree[c].Code, tree[c].Len)

Definition at line 160 of file trees.c.

◆ smaller

#define smaller (   tree,
  n,
  m,
  depth 
)
Value:
(tree[n].Freq < tree[m].Freq || \
(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))

Definition at line 440 of file trees.c.

◆ SMALLEST

#define SMALLEST   1

Definition at line 421 of file trees.c.

Function Documentation

◆ _tr_align()

void ZLIB_INTERNAL _tr_align ( deflate_state s)

Definition at line 896 of file trees.c.

898{
899 send_bits(s, STATIC_TREES<<1, 3);
901#ifdef ZLIB_DEBUG
902 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
903#endif
904 bi_flush(s);
905}
void bi_flush(deflate_state *s)
Definition: trees.c:1151
#define END_BLOCK
Definition: trees.c:49
#define send_code(s, c, tree)
Definition: trees.c:160
ct_data static_ltree[L_CODES+2]
Definition: trees.c:85
#define send_bits(s, value, length)
Definition: trees.c:210
#define STATIC_TREES
Definition: zutil.h:79

Referenced by deflate().

◆ _tr_flush_bits()

void ZLIB_INTERNAL _tr_flush_bits ( deflate_state s)

Definition at line 886 of file trees.c.

888{
889 bi_flush(s);
890}

Referenced by deflatePrime(), and flush_pending().

◆ _tr_flush_block()

void ZLIB_INTERNAL _tr_flush_block ( deflate_state s,
charf *  buf,
ulg  stored_len,
int  last 
)

Definition at line 911 of file trees.c.

916{
917 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
918 int max_blindex = 0; /* index of last bit length code of non zero freq */
919
920 /* Build the Huffman trees unless a stored block is forced */
921 if (s->level > 0) {
922
923 /* Check if the file is binary or text */
924 if (s->strm->data_type == Z_UNKNOWN)
925 s->strm->data_type = detect_data_type(s);
926
927 /* Construct the literal and distance trees */
928 build_tree(s, (tree_desc *)(&(s->l_desc)));
929 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
930 s->static_len));
931
932 build_tree(s, (tree_desc *)(&(s->d_desc)));
933 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
934 s->static_len));
935 /* At this point, opt_len and static_len are the total bit lengths of
936 * the compressed block data, excluding the tree representations.
937 */
938
939 /* Build the bit length tree for the above two trees, and get the index
940 * in bl_order of the last bit length code to send.
941 */
942 max_blindex = build_bl_tree(s);
943
944 /* Determine the best encoding. Compute the block lengths in bytes. */
945 opt_lenb = (s->opt_len+3+7)>>3;
946 static_lenb = (s->static_len+3+7)>>3;
947
948 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
949 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
950 s->sym_next / 3));
951
952 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
953
954 } else {
955 Assert(buf != (char*)0, "lost buf");
956 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
957 }
958
959#ifdef FORCE_STORED
960 if (buf != (char*)0) { /* force stored block */
961#else
962 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
963 /* 4: two words for the lengths */
964#endif
965 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
966 * Otherwise we can't have processed more than WSIZE input bytes since
967 * the last block flush, because compression would have been
968 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
969 * transform a block into a stored block.
970 */
971 _tr_stored_block(s, buf, stored_len, last);
972
973#ifdef FORCE_STATIC
974 } else if (static_lenb >= 0) { /* force static trees */
975#else
976 } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
977#endif
978 send_bits(s, (STATIC_TREES<<1)+last, 3);
980 (const ct_data *)static_dtree);
981#ifdef ZLIB_DEBUG
982 s->compressed_len += 3 + s->static_len;
983#endif
984 } else {
985 send_bits(s, (DYN_TREES<<1)+last, 3);
987 max_blindex+1);
988 compress_block(s, (const ct_data *)s->dyn_ltree,
989 (const ct_data *)s->dyn_dtree);
990#ifdef ZLIB_DEBUG
991 s->compressed_len += 3 + s->opt_len;
992#endif
993 }
994 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
995 /* The above check is made mod 2^32, for files larger than 512 MB
996 * and uLong implemented on 32 bits.
997 */
998 init_block(s);
999
1000 if (last) {
1001 bi_windup(s);
1002#ifdef ZLIB_DEBUG
1003 s->compressed_len += 7; /* align on byte boundary */
1004#endif
1005 }
1006 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1007 s->compressed_len-7*last));
1008}
struct tree_desc_s l_desc
Definition: deflate.h:201
uInt sym_next
Definition: deflate.h:241
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:198
ulg static_len
Definition: deflate.h:245
z_streamp strm
Definition: deflate.h:100
struct tree_desc_s d_desc
Definition: deflate.h:202
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:197
int max_code
Definition: deflate.h:87
void send_all_trees(deflate_state *s, int lcodes, int dcodes, int blcodes)
Definition: trees.c:833
void build_tree(deflate_state *s, tree_desc *desc)
Definition: trees.c:614
void init_block(deflate_state *s)
Definition: trees.c:406
int detect_data_type(deflate_state *s)
Definition: trees.c:1102
ct_data static_dtree[D_CODES]
Definition: trees.c:92
void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf, ulg stored_len, int last)
Definition: trees.c:862
void compress_block(deflate_state *s, const ct_data *ltree, const ct_data *dtree)
Definition: trees.c:1042
void bi_windup(deflate_state *s)
Definition: trees.c:1168
int build_bl_tree(deflate_state *s)
Definition: trees.c:798
#define Z_UNKNOWN
Definition: zlib.h:206
#define Z_FIXED
Definition: zlib.h:199
#define DYN_TREES
Definition: zutil.h:80
#define Assert(cond, msg)
Definition: zutil.h:250
#define Tracev(x)
Definition: zutil.h:252
unsigned long ulg
Definition: zutil.h:42

◆ _tr_init()

void ZLIB_INTERNAL _tr_init ( deflate_state s)

Definition at line 378 of file trees.c.

380{
382
383 s->l_desc.dyn_tree = s->dyn_ltree;
385
386 s->d_desc.dyn_tree = s->dyn_dtree;
388
389 s->bl_desc.dyn_tree = s->bl_tree;
391
392 s->bi_buf = 0;
393 s->bi_valid = 0;
394#ifdef ZLIB_DEBUG
395 s->compressed_len = 0L;
396 s->bits_sent = 0L;
397#endif
398
399 /* Initialize the first block of the first file: */
400 init_block(s);
401}
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:199
struct tree_desc_s bl_desc
Definition: deflate.h:203
ct_data * dyn_tree
Definition: deflate.h:86
const static_tree_desc * stat_desc
Definition: deflate.h:88
const static_tree_desc static_d_desc
Definition: trees.c:127
void tr_static_init()
Definition: trees.c:231
const static_tree_desc static_bl_desc
Definition: trees.c:130
const static_tree_desc static_l_desc
Definition: trees.c:124

Referenced by deflateResetKeep().

◆ _tr_stored_block()

void ZLIB_INTERNAL _tr_stored_block ( deflate_state s,
charf *  buf,
ulg  stored_len,
int  last 
)

Definition at line 862 of file trees.c.

867{
868 send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
869 bi_windup(s); /* align on byte boundary */
870 put_short(s, (ush)stored_len);
871 put_short(s, (ush)~stored_len);
872 if (stored_len)
873 zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len);
874 s->pending += stored_len;
875#ifdef ZLIB_DEBUG
876 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
877 s->compressed_len += (stored_len + 4) << 3;
878 s->bits_sent += 2*16;
879 s->bits_sent += stored_len<<3;
880#endif
881}
Bytef * pending_buf
Definition: deflate.h:102
void ZLIB_INTERNAL zmemcpy(Bytef *dest, const Bytef *source, uInt len)
Definition: zutil.c:148
#define STORED_BLOCK
Definition: zutil.h:78

Referenced by _tr_flush_block(), deflate(), and deflate_stored().

◆ _tr_tally()

int ZLIB_INTERNAL _tr_tally ( deflate_state s,
unsigned  dist,
unsigned  lc 
)

Definition at line 1014 of file trees.c.

1018{
1019 s->sym_buf[s->sym_next++] = dist;
1020 s->sym_buf[s->sym_next++] = dist >> 8;
1021 s->sym_buf[s->sym_next++] = lc;
1022 if (dist == 0) {
1023 /* lc is the unmatched char */
1024 s->dyn_ltree[lc].Freq++;
1025 } else {
1026 s->matches++;
1027 /* Here, lc is the match length - MIN_MATCH */
1028 dist--; /* dist = match distance - 1 */
1029 Assert((ush)dist < (ush)MAX_DIST(s) &&
1030 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1031 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1032
1033 s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1034 s->dyn_dtree[d_code(dist)].Freq++;
1035 }
1036 return (s->sym_next == s->sym_end);
1037}
#define MAX_DIST(s)
Definition: deflate.h:283
#define LITERALS
Definition: deflate.h:32
#define d_code(dist)
Definition: deflate.h:302
#define D_CODES
Definition: deflate.h:38
uInt sym_end
Definition: deflate.h:242
uInt matches
Definition: deflate.h:246
uchf * sym_buf
Definition: deflate.h:219
uch _length_code[MAX_MATCH-MIN_MATCH+1]
Definition: trees.c:103
#define MIN_MATCH
Definition: zutil.h:83
#define MAX_MATCH
Definition: zutil.h:84

◆ bi_flush()

void bi_flush ( deflate_state s)

Definition at line 1151 of file trees.c.

1153{
1154 if (s->bi_valid == 16) {
1155 put_short(s, s->bi_buf);
1156 s->bi_buf = 0;
1157 s->bi_valid = 0;
1158 } else if (s->bi_valid >= 8) {
1159 put_byte(s, (Byte)s->bi_buf);
1160 s->bi_buf >>= 8;
1161 s->bi_valid -= 8;
1162 }
1163}
#define put_byte(s, c)
Definition: deflate.h:275

Referenced by _tr_align(), and _tr_flush_bits().

◆ bi_reverse()

unsigned bi_reverse ( unsigned  code,
int  len 
)

Definition at line 1136 of file trees.c.

1139{
1140 register unsigned res = 0;
1141 do {
1142 res |= code & 1;
1143 code >>= 1, res <<= 1;
1144 } while (--len > 0);
1145 return res >> 1;
1146}
Definition: inftrees.h:24

Referenced by gen_codes(), and tr_static_init().

◆ bi_windup()

void bi_windup ( deflate_state s)

Definition at line 1168 of file trees.c.

1170{
1171 if (s->bi_valid > 8) {
1172 put_short(s, s->bi_buf);
1173 } else if (s->bi_valid > 0) {
1174 put_byte(s, (Byte)s->bi_buf);
1175 }
1176 s->bi_buf = 0;
1177 s->bi_valid = 0;
1178#ifdef ZLIB_DEBUG
1179 s->bits_sent = (s->bits_sent+7) & ~7;
1180#endif
1181}

Referenced by _tr_flush_block(), and _tr_stored_block().

◆ build_bl_tree()

int build_bl_tree ( deflate_state s)

Definition at line 798 of file trees.c.

800{
801 int max_blindex; /* index of last bit length code of non zero freq */
802
803 /* Determine the bit length frequencies for literal and distance trees */
806
807 /* Build the bit length tree: */
808 build_tree(s, (tree_desc *)(&(s->bl_desc)));
809 /* opt_len now includes the length of the tree representations, except
810 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
811 */
812
813 /* Determine the number of bit length codes to send. The pkzip format
814 * requires that at least 4 bit length codes be sent. (appnote.txt says
815 * 3 but the actual value used is 4.)
816 */
817 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
818 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
819 }
820 /* Update opt_len to include the bit length tree and counts */
821 s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4;
822 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
823 s->opt_len, s->static_len));
824
825 return max_blindex;
826}
#define BL_CODES
Definition: deflate.h:41
const uch bl_order[BL_CODES]
Definition: trees.c:71
void scan_tree(deflate_state *s, ct_data *tree, int max_code)
Definition: trees.c:702

Referenced by _tr_flush_block().

◆ build_tree()

void build_tree ( deflate_state s,
tree_desc desc 
)

Definition at line 614 of file trees.c.

617{
618 ct_data *tree = desc->dyn_tree;
619 const ct_data *stree = desc->stat_desc->static_tree;
620 int elems = desc->stat_desc->elems;
621 int n, m; /* iterate over heap elements */
622 int max_code = -1; /* largest code with non zero frequency */
623 int node; /* new node being created */
624
625 /* Construct the initial heap, with least frequent element in
626 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
627 * heap[0] is not used.
628 */
629 s->heap_len = 0, s->heap_max = HEAP_SIZE;
630
631 for (n = 0; n < elems; n++) {
632 if (tree[n].Freq != 0) {
633 s->heap[++(s->heap_len)] = max_code = n;
634 s->depth[n] = 0;
635 } else {
636 tree[n].Len = 0;
637 }
638 }
639
640 /* The pkzip format requires that at least one distance code exists,
641 * and that at least one bit should be sent even if there is only one
642 * possible code. So to avoid special checks later on we force at least
643 * two codes of non zero frequency.
644 */
645 while (s->heap_len < 2) {
646 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
647 tree[node].Freq = 1;
648 s->depth[node] = 0;
649 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
650 /* node is 0 or 1 so it does not have extra bits */
651 }
652 desc->max_code = max_code;
653
654 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
655 * establish sub-heaps of increasing lengths:
656 */
657 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
658
659 /* Construct the Huffman tree by repeatedly combining the least two
660 * frequent nodes.
661 */
662 node = elems; /* next internal node of the tree */
663 do {
664 pqremove(s, tree, n); /* n = node of least frequency */
665 m = s->heap[SMALLEST]; /* m = node of next least frequency */
666
667 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
668 s->heap[--(s->heap_max)] = m;
669
670 /* Create a new node father of n and m */
671 tree[node].Freq = tree[n].Freq + tree[m].Freq;
672 s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
673 s->depth[n] : s->depth[m]) + 1);
674 tree[n].Dad = tree[m].Dad = (ush)node;
675#ifdef DUMP_BL_TREE
676 if (tree == s->bl_tree) {
677 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
678 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
679 }
680#endif
681 /* and insert the new node in the heap */
682 s->heap[SMALLEST] = node++;
683 pqdownheap(s, tree, SMALLEST);
684
685 } while (s->heap_len >= 2);
686
687 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
688
689 /* At this point, the fields freq and dad are set. We can now
690 * generate the bit lengths.
691 */
692 gen_bitlen(s, (tree_desc *)desc);
693
694 /* The field len is now set, we can generate the bit codes */
695 gen_codes ((ct_data *)tree, max_code, s->bl_count);
696}
#define HEAP_SIZE
Definition: deflate.h:44
#define Freq
Definition: deflate.h:78
uch depth[2 *L_CODES+1]
Definition: deflate.h:215
ush bl_count[MAX_BITS+1]
Definition: deflate.h:205
int heap[2 *L_CODES+1]
Definition: deflate.h:208
const ct_data * static_tree
Definition: trees.c:117
#define pqremove(s, tree, top)
Definition: trees.c:429
void gen_codes(ct_data *tree, int max_code, ushf *bl_count)
Definition: trees.c:571
void gen_bitlen(deflate_state *s, tree_desc *desc)
Definition: trees.c:485
void pqdownheap(deflate_state *s, ct_data *tree, int k)
Definition: trees.c:450

Referenced by _tr_flush_block(), and build_bl_tree().

◆ compress_block()

void compress_block ( deflate_state s,
const ct_data ltree,
const ct_data dtree 
)

Definition at line 1042 of file trees.c.

1046{
1047 unsigned dist; /* distance of matched string */
1048 int lc; /* match length or unmatched char (if dist == 0) */
1049 unsigned sx = 0; /* running index in sym_buf */
1050 unsigned code; /* the code to send */
1051 int extra; /* number of extra bits to send */
1052
1053 if (s->sym_next != 0) do {
1054 dist = s->sym_buf[sx++] & 0xff;
1055 dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
1056 lc = s->sym_buf[sx++];
1057 if (dist == 0) {
1058 send_code(s, lc, ltree); /* send a literal byte */
1059 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1060 } else {
1061 /* Here, lc is the match length - MIN_MATCH */
1062 code = _length_code[lc];
1063 send_code(s, code+LITERALS+1, ltree); /* send the length code */
1064 extra = extra_lbits[code];
1065 if (extra != 0) {
1066 lc -= base_length[code];
1067 send_bits(s, lc, extra); /* send the extra length bits */
1068 }
1069 dist--; /* dist is now the match distance - 1 */
1070 code = d_code(dist);
1071 Assert (code < D_CODES, "bad d_code");
1072
1073 send_code(s, code, dtree); /* send the distance code */
1074 extra = extra_dbits[code];
1075 if (extra != 0) {
1076 dist -= (unsigned)base_dist[code];
1077 send_bits(s, dist, extra); /* send the extra distance bits */
1078 }
1079 } /* literal or match pair ? */
1080
1081 /* Check that the overlay between pending_buf and sym_buf is ok: */
1082 Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
1083
1084 } while (sx < s->sym_next);
1085
1086 send_code(s, END_BLOCK, ltree);
1087}
uInt lit_bufsize
Definition: deflate.h:221
int base_dist[D_CODES]
Definition: trees.c:109
const int extra_lbits[LENGTH_CODES]
Definition: trees.c:62
const int extra_dbits[D_CODES]
Definition: trees.c:65
int base_length[LENGTH_CODES]
Definition: trees.c:106
#define Tracecv(c, x)
Definition: zutil.h:255

Referenced by _tr_flush_block().

◆ detect_data_type()

int detect_data_type ( deflate_state s)

Definition at line 1102 of file trees.c.

1104{
1105 /* block_mask is the bit mask of block-listed bytes
1106 * set bits 0..6, 14..25, and 28..31
1107 * 0xf3ffc07f = binary 11110011111111111100000001111111
1108 */
1109 unsigned long block_mask = 0xf3ffc07fUL;
1110 int n;
1111
1112 /* Check for non-textual ("block-listed") bytes. */
1113 for (n = 0; n <= 31; n++, block_mask >>= 1)
1114 if ((block_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1115 return Z_BINARY;
1116
1117 /* Check for textual ("allow-listed") bytes. */
1118 if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1119 || s->dyn_ltree[13].Freq != 0)
1120 return Z_TEXT;
1121 for (n = 32; n < LITERALS; n++)
1122 if (s->dyn_ltree[n].Freq != 0)
1123 return Z_TEXT;
1124
1125 /* There are no "block-listed" or "allow-listed" bytes:
1126 * this stream either is empty or has tolerated ("gray-listed") bytes only.
1127 */
1128 return Z_BINARY;
1129}
#define Z_BINARY
Definition: zlib.h:203
#define Z_TEXT
Definition: zlib.h:204

Referenced by _tr_flush_block().

◆ gen_bitlen()

void gen_bitlen ( deflate_state s,
tree_desc desc 
)

Definition at line 485 of file trees.c.

488{
489 ct_data *tree = desc->dyn_tree;
490 int max_code = desc->max_code;
491 const ct_data *stree = desc->stat_desc->static_tree;
492 const intf *extra = desc->stat_desc->extra_bits;
493 int base = desc->stat_desc->extra_base;
494 int max_length = desc->stat_desc->max_length;
495 int h; /* heap index */
496 int n, m; /* iterate over the tree elements */
497 int bits; /* bit length */
498 int xbits; /* extra bits */
499 ush f; /* frequency */
500 int overflow = 0; /* number of elements with bit length too large */
501
502 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
503
504 /* In a first pass, compute the optimal bit lengths (which may
505 * overflow in the case of the bit length tree).
506 */
507 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
508
509 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
510 n = s->heap[h];
511 bits = tree[tree[n].Dad].Len + 1;
512 if (bits > max_length) bits = max_length, overflow++;
513 tree[n].Len = (ush)bits;
514 /* We overwrite tree[n].Dad which is no longer needed */
515
516 if (n > max_code) continue; /* not a leaf node */
517
518 s->bl_count[bits]++;
519 xbits = 0;
520 if (n >= base) xbits = extra[n-base];
521 f = tree[n].Freq;
522 s->opt_len += (ulg)f * (unsigned)(bits + xbits);
523 if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits);
524 }
525 if (overflow == 0) return;
526
527 Tracev((stderr,"\nbit length overflow\n"));
528 /* This happens for example on obj2 and pic of the Calgary corpus */
529
530 /* Find the first bit length which could increase: */
531 do {
532 bits = max_length-1;
533 while (s->bl_count[bits] == 0) bits--;
534 s->bl_count[bits]--; /* move one leaf down the tree */
535 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
536 s->bl_count[max_length]--;
537 /* The brother of the overflow item also moves one step up,
538 * but this does not affect bl_count[max_length]
539 */
540 overflow -= 2;
541 } while (overflow > 0);
542
543 /* Now recompute all bit lengths, scanning in increasing frequency.
544 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
545 * lengths instead of fixing only the wrong ones. This idea is taken
546 * from 'ar' written by Haruhiko Okumura.)
547 */
548 for (bits = max_length; bits != 0; bits--) {
549 n = s->bl_count[bits];
550 while (n != 0) {
551 m = s->heap[--h];
552 if (m > max_code) continue;
553 if ((unsigned) tree[m].Len != (unsigned) bits) {
554 Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
555 s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq;
556 tree[m].Len = (ush)bits;
557 }
558 n--;
559 }
560 }
561}
#define Len
Definition: deflate.h:81
#define MAX_BITS
Definition: deflate.h:47
const intf * extra_bits
Definition: trees.c:118

Referenced by build_tree().

◆ gen_codes()

void gen_codes ( ct_data tree,
int  max_code,
ushf bl_count 
)

Definition at line 571 of file trees.c.

575{
576 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
577 unsigned code = 0; /* running code value */
578 int bits; /* bit index */
579 int n; /* code index */
580
581 /* The distribution counts are first used to generate the code values
582 * without bit reversal.
583 */
584 for (bits = 1; bits <= MAX_BITS; bits++) {
585 code = (code + bl_count[bits-1]) << 1;
586 next_code[bits] = (ush)code;
587 }
588 /* Check that the bit counts in bl_count are consistent. The last code
589 * must be all ones.
590 */
591 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
592 "inconsistent bit counts");
593 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
594
595 for (n = 0; n <= max_code; n++) {
596 int len = tree[n].Len;
597 if (len == 0) continue;
598 /* Now reverse the bits */
599 tree[n].Code = (ush)bi_reverse(next_code[len]++, len);
600
601 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
602 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
603 }
604}
#define Code
Definition: deflate.h:79
unsigned bi_reverse(unsigned code, int len)
Definition: trees.c:1136

Referenced by build_tree(), and tr_static_init().

◆ init_block()

void init_block ( deflate_state s)

Definition at line 406 of file trees.c.

408{
409 int n; /* iterates over tree elements */
410
411 /* Initialize the trees. */
412 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
413 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
414 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
415
416 s->dyn_ltree[END_BLOCK].Freq = 1;
417 s->opt_len = s->static_len = 0L;
418 s->sym_next = s->matches = 0;
419}
#define L_CODES
Definition: deflate.h:35

Referenced by _tr_flush_block(), and _tr_init().

◆ OF() [1/9]

void gen_codes OF ( (ct_data *tree, int max_code, ushf *bl_count)  )

◆ OF() [2/9]

void init_block OF ( (deflate_state *s)  )

◆ OF() [3/9]

void compress_block OF ( (deflate_state *s, const ct_data *ltree, const ct_data *dtree)  )

◆ OF() [4/9]

void pqdownheap OF ( (deflate_state *s, ct_data *tree, int k)  )

◆ OF() [5/9]

void scan_tree OF ( (deflate_state *s, ct_data *tree, int max_code)  )

◆ OF() [6/9]

void send_all_trees OF ( (deflate_state *s, int lcodes, int dcodes, int blcodes)  )

◆ OF() [7/9]

void gen_bitlen OF ( (deflate_state *s, tree_desc *desc)  )

◆ OF() [8/9]

unsigned bi_reverse OF ( (unsigned code, int len)  )

◆ OF() [9/9]

void tr_static_init OF ( (void)  )

◆ pqdownheap()

void pqdownheap ( deflate_state s,
ct_data tree,
int  k 
)

Definition at line 450 of file trees.c.

454{
455 int v = s->heap[k];
456 int j = k << 1; /* left son of k */
457 while (j <= s->heap_len) {
458 /* Set j to the smallest of the two sons: */
459 if (j < s->heap_len &&
460 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
461 j++;
462 }
463 /* Exit if v is smaller than both sons */
464 if (smaller(tree, v, s->heap[j], s->depth)) break;
465
466 /* Exchange v with the smallest son */
467 s->heap[k] = s->heap[j]; k = j;
468
469 /* And continue down the tree, setting j to the left son of k */
470 j <<= 1;
471 }
472 s->heap[k] = v;
473}
#define smaller(tree, n, m, depth)
Definition: trees.c:440

Referenced by build_tree().

◆ scan_tree()

void scan_tree ( deflate_state s,
ct_data tree,
int  max_code 
)

Definition at line 702 of file trees.c.

706{
707 int n; /* iterates over all tree elements */
708 int prevlen = -1; /* last emitted length */
709 int curlen; /* length of current code */
710 int nextlen = tree[0].Len; /* length of next code */
711 int count = 0; /* repeat count of the current code */
712 int max_count = 7; /* max repeat count */
713 int min_count = 4; /* min repeat count */
714
715 if (nextlen == 0) max_count = 138, min_count = 3;
716 tree[max_code+1].Len = (ush)0xffff; /* guard */
717
718 for (n = 0; n <= max_code; n++) {
719 curlen = nextlen; nextlen = tree[n+1].Len;
720 if (++count < max_count && curlen == nextlen) {
721 continue;
722 } else if (count < min_count) {
723 s->bl_tree[curlen].Freq += count;
724 } else if (curlen != 0) {
725 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
726 s->bl_tree[REP_3_6].Freq++;
727 } else if (count <= 10) {
728 s->bl_tree[REPZ_3_10].Freq++;
729 } else {
730 s->bl_tree[REPZ_11_138].Freq++;
731 }
732 count = 0; prevlen = curlen;
733 if (nextlen == 0) {
734 max_count = 138, min_count = 3;
735 } else if (curlen == nextlen) {
736 max_count = 6, min_count = 3;
737 } else {
738 max_count = 7, min_count = 4;
739 }
740 }
741}
#define REPZ_11_138
Definition: trees.c:58
#define REPZ_3_10
Definition: trees.c:55
#define REP_3_6
Definition: trees.c:52

Referenced by build_bl_tree().

◆ send_all_trees()

void send_all_trees ( deflate_state s,
int  lcodes,
int  dcodes,
int  blcodes 
)

Definition at line 833 of file trees.c.

836{
837 int rank; /* index in bl_order */
838
839 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
840 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
841 "too many codes");
842 Tracev((stderr, "\nbl counts: "));
843 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
844 send_bits(s, dcodes-1, 5);
845 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
846 for (rank = 0; rank < blcodes; rank++) {
847 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
848 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
849 }
850 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
851
852 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
853 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
854
855 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
856 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
857}
void send_tree(deflate_state *s, ct_data *tree, int max_code)
Definition: trees.c:747

Referenced by _tr_flush_block().

◆ send_tree()

void send_tree ( deflate_state s,
ct_data tree,
int  max_code 
)

Definition at line 747 of file trees.c.

751{
752 int n; /* iterates over all tree elements */
753 int prevlen = -1; /* last emitted length */
754 int curlen; /* length of current code */
755 int nextlen = tree[0].Len; /* length of next code */
756 int count = 0; /* repeat count of the current code */
757 int max_count = 7; /* max repeat count */
758 int min_count = 4; /* min repeat count */
759
760 /* tree[max_code+1].Len = -1; */ /* guard already set */
761 if (nextlen == 0) max_count = 138, min_count = 3;
762
763 for (n = 0; n <= max_code; n++) {
764 curlen = nextlen; nextlen = tree[n+1].Len;
765 if (++count < max_count && curlen == nextlen) {
766 continue;
767 } else if (count < min_count) {
768 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
769
770 } else if (curlen != 0) {
771 if (curlen != prevlen) {
772 send_code(s, curlen, s->bl_tree); count--;
773 }
774 Assert(count >= 3 && count <= 6, " 3_6?");
775 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
776
777 } else if (count <= 10) {
778 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
779
780 } else {
781 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
782 }
783 count = 0; prevlen = curlen;
784 if (nextlen == 0) {
785 max_count = 138, min_count = 3;
786 } else if (curlen == nextlen) {
787 max_count = 6, min_count = 3;
788 } else {
789 max_count = 7, min_count = 4;
790 }
791 }
792}

Referenced by send_all_trees().

◆ tr_static_init()

void tr_static_init ( )

Definition at line 231 of file trees.c.

232{
233#if defined(GEN_TREES_H) || !defined(STDC)
234 static int static_init_done = 0;
235 int n; /* iterates over tree elements */
236 int bits; /* bit counter */
237 int length; /* length value */
238 int code; /* code value */
239 int dist; /* distance index */
240 ush bl_count[MAX_BITS+1];
241 /* number of codes at each bit length for an optimal tree */
242
243 if (static_init_done) return;
244
245 /* For some embedded targets, global variables are not initialized: */
246#ifdef NO_INIT_GLOBAL_POINTERS
252#endif
253
254 /* Initialize the mapping length (0..255) -> length code (0..28) */
255 length = 0;
256 for (code = 0; code < LENGTH_CODES-1; code++) {
257 base_length[code] = length;
258 for (n = 0; n < (1<<extra_lbits[code]); n++) {
259 _length_code[length++] = (uch)code;
260 }
261 }
262 Assert (length == 256, "tr_static_init: length != 256");
263 /* Note that the length 255 (match length 258) can be represented
264 * in two different ways: code 284 + 5 bits or code 285, so we
265 * overwrite length_code[255] to use the best encoding:
266 */
267 _length_code[length-1] = (uch)code;
268
269 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
270 dist = 0;
271 for (code = 0 ; code < 16; code++) {
272 base_dist[code] = dist;
273 for (n = 0; n < (1<<extra_dbits[code]); n++) {
274 _dist_code[dist++] = (uch)code;
275 }
276 }
277 Assert (dist == 256, "tr_static_init: dist != 256");
278 dist >>= 7; /* from now on, all distances are divided by 128 */
279 for ( ; code < D_CODES; code++) {
280 base_dist[code] = dist << 7;
281 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
282 _dist_code[256 + dist++] = (uch)code;
283 }
284 }
285 Assert (dist == 256, "tr_static_init: 256+dist != 512");
286
287 /* Construct the codes of the static literal tree */
288 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
289 n = 0;
290 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
291 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
292 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
293 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
294 /* Codes 286 and 287 do not exist, but we must include them in the
295 * tree construction to get a canonical Huffman tree (longest code
296 * all ones)
297 */
298 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
299
300 /* The static distance tree is trivial: */
301 for (n = 0; n < D_CODES; n++) {
302 static_dtree[n].Len = 5;
303 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
304 }
305 static_init_done = 1;
306
307# ifdef GEN_TREES_H
308 gen_trees_header();
309# endif
310#endif /* defined(GEN_TREES_H) || !defined(STDC) */
311}
#define LENGTH_CODES
Definition: deflate.h:29
const int extra_blbits[BL_CODES]
Definition: trees.c:68
uch _dist_code[DIST_CODE_LEN]
Definition: trees.c:97

Referenced by _tr_init().

Variable Documentation

◆ _dist_code

uch _dist_code[DIST_CODE_LEN]

Definition at line 97 of file trees.c.

Referenced by tr_static_init().

◆ _length_code

uch _length_code[MAX_MATCH-MIN_MATCH+1]

Definition at line 103 of file trees.c.

Referenced by _tr_tally(), compress_block(), and tr_static_init().

◆ base_dist

int base_dist[D_CODES]

Definition at line 109 of file trees.c.

Referenced by compress_block(), and tr_static_init().

◆ base_length

int base_length[LENGTH_CODES]

Definition at line 106 of file trees.c.

Referenced by compress_block(), and tr_static_init().

◆ bl_order

const uch bl_order[BL_CODES] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}

Definition at line 70 of file trees.c.

Referenced by build_bl_tree(), and send_all_trees().

◆ extra_blbits

const int extra_blbits[BL_CODES] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}

Definition at line 67 of file trees.c.

Referenced by tr_static_init().

◆ extra_dbits

const int extra_dbits[D_CODES] = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}

Definition at line 64 of file trees.c.

Referenced by compress_block(), and tr_static_init().

◆ extra_lbits

const int extra_lbits[LENGTH_CODES] = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}

Definition at line 61 of file trees.c.

Referenced by compress_block(), and tr_static_init().

◆ static_bl_desc

const static_tree_desc static_bl_desc
Initial value:
=
#define MAX_BL_BITS
Definition: trees.c:46

Definition at line 130 of file trees.c.

Referenced by _tr_init(), and tr_static_init().

◆ static_d_desc

const static_tree_desc static_d_desc
Initial value:

Definition at line 127 of file trees.c.

Referenced by _tr_init(), and tr_static_init().

◆ static_dtree

ct_data static_dtree[D_CODES]

Definition at line 92 of file trees.c.

Referenced by _tr_flush_block(), and tr_static_init().

◆ static_l_desc

const static_tree_desc static_l_desc
Initial value:

Definition at line 124 of file trees.c.

Referenced by _tr_init(), and tr_static_init().

◆ static_ltree

ct_data static_ltree[L_CODES+2]

Definition at line 85 of file trees.c.

Referenced by _tr_align(), _tr_flush_block(), gen_codes(), and tr_static_init().