abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
trees.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "deflate.h"

Go to the source code of this file.

Data Structures

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 Buf_size   (8 * 2*sizeof(char))
 
#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

local void tr_static_init OF ((void))
 
local void init_block OF ((deflate_state *s))
 
local void pqdownheap OF ((deflate_state *s, ct_data *tree, int k))
 
local void gen_bitlen OF ((deflate_state *s, tree_desc *desc))
 
local void gen_codes OF ((ct_data *tree, int max_code, ushf *bl_count))
 
local void scan_tree OF ((deflate_state *s, ct_data *tree, int max_code))
 
local void send_all_trees OF ((deflate_state *s, int lcodes, int dcodes, int blcodes))
 
local void compress_block OF ((deflate_state *s, ct_data *ltree, ct_data *dtree))
 
local unsigned bi_reverse OF ((unsigned value, int length))
 
local void copy_block OF ((deflate_state *s, charf *buf, unsigned len, int header))
 
local void tr_static_init ()
 
void ZLIB_INTERNAL _tr_init (deflate_state *s)
 
local void init_block (deflate_state *s)
 
local void pqdownheap (deflate_state *s, ct_data *tree, int k)
 
local void gen_bitlen (deflate_state *s, tree_desc *desc)
 
local void gen_codes (ct_data *tree, int max_code, ushf *bl_count)
 
local void build_tree (deflate_state *s, tree_desc *desc)
 
local void scan_tree (deflate_state *s, ct_data *tree, int max_code)
 
local void send_tree (deflate_state *s, ct_data *tree, int max_code)
 
local int build_bl_tree (deflate_state *s)
 
local 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_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)
 
local void compress_block (deflate_state *s, ct_data *ltree, ct_data *dtree)
 
local int detect_data_type (deflate_state *s)
 
local unsigned bi_reverse (unsigned code, int len)
 
local void bi_flush (deflate_state *s)
 
local void bi_windup (deflate_state *s)
 
local void copy_block (deflate_state *s, charf *buf, unsigned len, int header)
 

Variables

local 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}
 
local 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}
 
local 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}
 
local 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}
 
local ct_data static_ltree [L_CODES+2]
 
local ct_data static_dtree [D_CODES]
 
uch _dist_code [DIST_CODE_LEN]
 
uch _length_code [MAX_MATCH-MIN_MATCH+1]
 
local int base_length [LENGTH_CODES]
 
local int base_dist [D_CODES]
 
local static_tree_desc static_l_desc
 
local static_tree_desc static_d_desc
 
local static_tree_desc static_bl_desc
 

Macro Definition Documentation

#define Buf_size   (8 * 2*sizeof(char))

Definition at line 84 of file trees.c.

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

Definition at line 93 of file trees.c.

#define END_BLOCK   256

Definition at line 57 of file trees.c.

#define MAX_BL_BITS   7

Definition at line 54 of file trees.c.

#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:436
local void pqdownheap(deflate_state *s, ct_data *tree, int k)
Definition: trees.c:465

Definition at line 444 of file trees.c.

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

Definition at line 190 of file trees.c.

#define REP_3_6   16

Definition at line 60 of file trees.c.

#define REPZ_11_138   18

Definition at line 66 of file trees.c.

#define REPZ_3_10   17

Definition at line 63 of file trees.c.

#define send_bits (   s,
  value,
  length 
)
Value:
{ int len = length;\
if (s->bi_valid > (int)Buf_size - len) {\
int val = 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 put_short(s, w)
Definition: trees.c:190
unsigned short ush
Definition: zutil.h:41
if(last==0)
Definition: sparse_int.h:34
#define Buf_size
Definition: trees.c:84
int value

Definition at line 224 of file trees.c.

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

Definition at line 177 of file trees.c.

#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 455 of file trees.c.

#define SMALLEST   1

Definition at line 436 of file trees.c.

Function Documentation

void ZLIB_INTERNAL _tr_align ( deflate_state s)

Definition at line 882 of file trees.c.

883 {
884  send_bits(s, STATIC_TREES<<1, 3);
886 #ifdef DEBUG
887  s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
888 #endif
889  bi_flush(s);
890  /* Of the 10 bits for the empty block, we have already sent
891  * (10 - bi_valid) bits. The lookahead for the last real code (before
892  * the EOB of the previous block) was thus at least one plus the length
893  * of the EOB plus what we have just sent of the empty static block.
894  */
895  if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
896  send_bits(s, STATIC_TREES<<1, 3);
898 #ifdef DEBUG
899  s->compressed_len += 10L;
900 #endif
901  bi_flush(s);
902  }
903  s->last_eob_len = 7;
904 }
#define END_BLOCK
Definition: trees.c:57
int last_eob_len
Definition: deflate.h:249
#define send_bits(s, value, length)
Definition: trees.c:224
#define STATIC_TREES
Definition: zutil.h:69
local ct_data static_ltree[L_CODES+2]
Definition: trees.c:98
#define send_code(s, c, tree)
Definition: trees.c:177
local void bi_flush(deflate_state *s)
Definition: trees.c:1158
void ZLIB_INTERNAL _tr_flush_block ( deflate_state s,
charf buf,
ulg  stored_len,
int  last 
)

Definition at line 910 of file trees.c.

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

Definition at line 394 of file trees.c.

395 {
396  tr_static_init();
397 
398  s->l_desc.dyn_tree = s->dyn_ltree;
400 
401  s->d_desc.dyn_tree = s->dyn_dtree;
403 
404  s->bl_desc.dyn_tree = s->bl_tree;
406 
407  s->bi_buf = 0;
408  s->bi_valid = 0;
409  s->last_eob_len = 8; /* enough lookahead for inflate */
410 #ifdef DEBUG
411  s->compressed_len = 0L;
412  s->bits_sent = 0L;
413 #endif
414 
415  /* Initialize the first block of the first file: */
416  init_block(s);
417 }
local void init_block(deflate_state *s)
Definition: trees.c:422
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
local static_tree_desc static_d_desc
Definition: trees.c:142
ct_data * dyn_tree
Definition: deflate.h:83
static_tree_desc * stat_desc
Definition: deflate.h:85
int last_eob_len
Definition: deflate.h:249
struct tree_desc_s bl_desc
Definition: deflate.h:200
struct tree_desc_s d_desc
Definition: deflate.h:199
local static_tree_desc static_bl_desc
Definition: trees.c:145
local void tr_static_init()
Definition: trees.c:245
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
struct tree_desc_s l_desc
Definition: deflate.h:198
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
local static_tree_desc static_l_desc
Definition: trees.c:139
void ZLIB_INTERNAL _tr_stored_block ( deflate_state s,
charf buf,
ulg  stored_len,
int  last 
)

Definition at line 861 of file trees.c.

862 {
863  send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */
864 #ifdef DEBUG
865  s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
866  s->compressed_len += (stored_len + 4) << 3;
867 #endif
868  copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
869 }
#define STORED_BLOCK
Definition: zutil.h:68
local void copy_block(deflate_state *s, charf *buf, unsigned len, int header)
Definition: trees.c:1192
#define send_bits(s, value, length)
Definition: trees.c:224
unsigned long ulg
Definition: zutil.h:43
int ZLIB_INTERNAL _tr_tally ( deflate_state s,
unsigned  dist,
unsigned  lc 
)

Definition at line 1007 of file trees.c.

1008 {
1009  s->d_buf[s->last_lit] = (ush)dist;
1010  s->l_buf[s->last_lit++] = (uch)lc;
1011  if (dist == 0) {
1012  /* lc is the unmatched char */
1013  s->dyn_ltree[lc].Freq++;
1014  } else {
1015  s->matches++;
1016  /* Here, lc is the match length - MIN_MATCH */
1017  dist--; /* dist = match distance - 1 */
1018  Assert((ush)dist < (ush)MAX_DIST(s) &&
1019  (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1020  (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1021 
1022  s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1023  s->dyn_dtree[d_code(dist)].Freq++;
1024  }
1025 
1026 #ifdef TRUNCATE_BLOCK
1027  /* Try to guess if it is profitable to stop the current block here */
1028  if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1029  /* Compute an upper bound for the compressed length */
1030  ulg out_length = (ulg)s->last_lit*8L;
1031  ulg in_length = (ulg)((long)s->strstart - s->block_start);
1032  int dcode;
1033  for (dcode = 0; dcode < D_CODES; dcode++) {
1034  out_length += (ulg)s->dyn_dtree[dcode].Freq *
1035  (5L+extra_dbits[dcode]);
1036  }
1037  out_length >>= 3;
1038  Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1039  s->last_lit, in_length, out_length,
1040  100L - out_length*100L/in_length));
1041  if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1042  }
1043 #endif
1044  return (s->last_lit == s->lit_bufsize-1);
1045  /* We avoid equality with lit_bufsize because of wraparound at 64K
1046  * on 16 bit machines and because stored blocks are restricted to
1047  * 64K-1 bytes.
1048  */
1049 }
local const int extra_dbits[D_CODES]
Definition: trees.c:73
#define MIN_MATCH
Definition: zutil.h:73
#define Assert(cond, msg)
Definition: zutil.h:268
#define Tracev(x)
Definition: zutil.h:270
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
#define MAX_DIST(s)
Definition: deflate.h:285
#define LITERALS
Definition: deflate.h:35
unsigned short ush
Definition: zutil.h:41
#define MAX_MATCH
Definition: zutil.h:74
uchf * l_buf
Definition: deflate.h:216
uInt lit_bufsize
Definition: deflate.h:218
ushf * d_buf
Definition: deflate.h:240
long block_start
Definition: deflate.h:150
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
#define d_code(dist)
Definition: deflate.h:303
uInt last_lit
Definition: deflate.h:238
unsigned char uch
Definition: zutil.h:39
uch _length_code[MAX_MATCH-MIN_MATCH+1]
Definition: trees.c:116
unsigned long ulg
Definition: zutil.h:43
uInt strstart
Definition: deflate.h:158
uInt matches
Definition: deflate.h:248
#define D_CODES
Definition: deflate.h:41
local void bi_flush ( deflate_state s)

Definition at line 1158 of file trees.c.

1159 {
1160  if (s->bi_valid == 16) {
1161  put_short(s, s->bi_buf);
1162  s->bi_buf = 0;
1163  s->bi_valid = 0;
1164  } else if (s->bi_valid >= 8) {
1165  put_byte(s, (Byte)s->bi_buf);
1166  s->bi_buf >>= 8;
1167  s->bi_valid -= 8;
1168  }
1169 }
unsigned char Byte
Definition: zconf.h:333
#define put_short(s, w)
Definition: trees.c:190
#define put_byte(s, c)
Definition: deflate.h:277
local unsigned bi_reverse ( unsigned  code,
int  len 
)

Definition at line 1145 of file trees.c.

1146 {
1147  register unsigned res = 0;
1148  do {
1149  res |= code & 1;
1150  code >>= 1, res <<= 1;
1151  } while (--len > 0);
1152  return res >> 1;
1153 }
Definition: inftrees.h:26
local void bi_windup ( deflate_state s)

Definition at line 1174 of file trees.c.

1175 {
1176  if (s->bi_valid > 8) {
1177  put_short(s, s->bi_buf);
1178  } else if (s->bi_valid > 0) {
1179  put_byte(s, (Byte)s->bi_buf);
1180  }
1181  s->bi_buf = 0;
1182  s->bi_valid = 0;
1183 #ifdef DEBUG
1184  s->bits_sent = (s->bits_sent+7) & ~7;
1185 #endif
1186 }
unsigned char Byte
Definition: zconf.h:333
#define put_short(s, w)
Definition: trees.c:190
#define put_byte(s, c)
Definition: deflate.h:277
local int build_bl_tree ( deflate_state s)

Definition at line 800 of file trees.c.

801 {
802  int max_blindex; /* index of last bit length code of non zero freq */
803 
804  /* Determine the bit length frequencies for literal and distance trees */
805  scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
806  scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
807 
808  /* Build the bit length tree: */
809  build_tree(s, (tree_desc *)(&(s->bl_desc)));
810  /* opt_len now includes the length of the tree representations, except
811  * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
812  */
813 
814  /* Determine the number of bit length codes to send. The pkzip format
815  * requires that at least 4 bit length codes be sent. (appnote.txt says
816  * 3 but the actual value used is 4.)
817  */
818  for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
819  if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
820  }
821  /* Update opt_len to include the bit length tree and counts */
822  s->opt_len += 3*(max_blindex+1) + 5+5+4;
823  Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
824  s->opt_len, s->static_len));
825 
826  return max_blindex;
827 }
ulg static_len
Definition: deflate.h:247
#define BL_CODES
Definition: deflate.h:44
#define Tracev(x)
Definition: zutil.h:270
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
int max_code
Definition: deflate.h:84
struct tree_desc_s bl_desc
Definition: deflate.h:200
struct tree_desc_s d_desc
Definition: deflate.h:199
local const uch bl_order[BL_CODES]
Definition: trees.c:79
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
struct tree_desc_s l_desc
Definition: deflate.h:198
local void scan_tree(deflate_state *s, ct_data *tree, int max_code)
Definition: trees.c:710
local void build_tree(deflate_state *s, tree_desc *desc)
Definition: trees.c:624
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
local void build_tree ( deflate_state s,
tree_desc desc 
)

Definition at line 624 of file trees.c.

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

Definition at line 1054 of file trees.c.

1055 {
1056  unsigned dist; /* distance of matched string */
1057  int lc; /* match length or unmatched char (if dist == 0) */
1058  unsigned lx = 0; /* running index in l_buf */
1059  unsigned code; /* the code to send */
1060  int extra; /* number of extra bits to send */
1061 
1062  if (s->last_lit != 0) do {
1063  dist = s->d_buf[lx];
1064  lc = s->l_buf[lx++];
1065  if (dist == 0) {
1066  send_code(s, lc, ltree); /* send a literal byte */
1067  Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1068  } else {
1069  /* Here, lc is the match length - MIN_MATCH */
1070  code = _length_code[lc];
1071  send_code(s, code+LITERALS+1, ltree); /* send the length code */
1072  extra = extra_lbits[code];
1073  if (extra != 0) {
1074  lc -= base_length[code];
1075  send_bits(s, lc, extra); /* send the extra length bits */
1076  }
1077  dist--; /* dist is now the match distance - 1 */
1078  code = d_code(dist);
1079  Assert (code < D_CODES, "bad d_code");
1080 
1081  send_code(s, code, dtree); /* send the distance code */
1082  extra = extra_dbits[code];
1083  if (extra != 0) {
1084  dist -= base_dist[code];
1085  send_bits(s, dist, extra); /* send the extra distance bits */
1086  }
1087  } /* literal or match pair ? */
1088 
1089  /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1090  Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
1091  "pendingBuf overflow");
1092 
1093  } while (lx < s->last_lit);
1094 
1095  send_code(s, END_BLOCK, ltree);
1096  s->last_eob_len = ltree[END_BLOCK].Len;
1097 }
local const int extra_dbits[D_CODES]
Definition: trees.c:73
#define Tracecv(c, x)
Definition: zutil.h:273
#define Assert(cond, msg)
Definition: zutil.h:268
#define END_BLOCK
Definition: trees.c:57
local int base_dist[D_CODES]
Definition: trees.c:122
local int base_length[LENGTH_CODES]
Definition: trees.c:119
#define LITERALS
Definition: deflate.h:35
int last_eob_len
Definition: deflate.h:249
#define send_bits(s, value, length)
Definition: trees.c:224
uchf * l_buf
Definition: deflate.h:216
local const int extra_lbits[LENGTH_CODES]
Definition: trees.c:70
uInt lit_bufsize
Definition: deflate.h:218
ushf * d_buf
Definition: deflate.h:240
Definition: inftrees.h:26
#define d_code(dist)
Definition: deflate.h:303
uInt last_lit
Definition: deflate.h:238
uch _length_code[MAX_MATCH-MIN_MATCH+1]
Definition: trees.c:116
uInt pending
Definition: deflate.h:102
#define D_CODES
Definition: deflate.h:41
#define send_code(s, c, tree)
Definition: trees.c:177
unsigned int uInt
Definition: zconf.h:335
local void copy_block ( deflate_state s,
charf buf,
unsigned  len,
int  header 
)

Definition at line 1192 of file trees.c.

1193 {
1194  bi_windup(s); /* align on byte boundary */
1195  s->last_eob_len = 8; /* enough lookahead for inflate */
1196 
1197  if (header) {
1198  put_short(s, (ush)len);
1199  put_short(s, (ush)~len);
1200 #ifdef DEBUG
1201  s->bits_sent += 2*16;
1202 #endif
1203  }
1204 #ifdef DEBUG
1205  s->bits_sent += (ulg)len<<3;
1206 #endif
1207  while (len--) {
1208  put_byte(s, *buf++);
1209  }
1210 }
#define put_short(s, w)
Definition: trees.c:190
int last_eob_len
Definition: deflate.h:249
unsigned short ush
Definition: zutil.h:41
#define put_byte(s, c)
Definition: deflate.h:277
unsigned long ulg
Definition: zutil.h:43
local void bi_windup(deflate_state *s)
Definition: trees.c:1174
local int detect_data_type ( deflate_state s)

Definition at line 1112 of file trees.c.

1113 {
1114  /* black_mask is the bit mask of black-listed bytes
1115  * set bits 0..6, 14..25, and 28..31
1116  * 0xf3ffc07f = binary 11110011111111111100000001111111
1117  */
1118  unsigned long black_mask = 0xf3ffc07fUL;
1119  int n;
1120 
1121  /* Check for non-textual ("black-listed") bytes. */
1122  for (n = 0; n <= 31; n++, black_mask >>= 1)
1123  if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0))
1124  return Z_BINARY;
1125 
1126  /* Check for textual ("white-listed") bytes. */
1127  if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
1128  || s->dyn_ltree[13].Freq != 0)
1129  return Z_TEXT;
1130  for (n = 32; n < LITERALS; n++)
1131  if (s->dyn_ltree[n].Freq != 0)
1132  return Z_TEXT;
1133 
1134  /* There are no "black-listed" or "white-listed" bytes:
1135  * this stream either is empty or has tolerated ("gray-listed") bytes only.
1136  */
1137  return Z_BINARY;
1138 }
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
#define LITERALS
Definition: deflate.h:35
#define Z_BINARY
Definition: zlib.h:207
#define Z_TEXT
Definition: zlib.h:208
local void gen_bitlen ( deflate_state s,
tree_desc desc 
)

Definition at line 497 of file trees.c.

498 {
499  ct_data *tree = desc->dyn_tree;
500  int max_code = desc->max_code;
501  const ct_data *stree = desc->stat_desc->static_tree;
502  const intf *extra = desc->stat_desc->extra_bits;
503  int base = desc->stat_desc->extra_base;
504  int max_length = desc->stat_desc->max_length;
505  int h; /* heap index */
506  int n, m; /* iterate over the tree elements */
507  int bits; /* bit length */
508  int xbits; /* extra bits */
509  ush f; /* frequency */
510  int overflow = 0; /* number of elements with bit length too large */
511 
512  for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
513 
514  /* In a first pass, compute the optimal bit lengths (which may
515  * overflow in the case of the bit length tree).
516  */
517  tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
518 
519  for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
520  n = s->heap[h];
521  bits = tree[tree[n].Dad].Len + 1;
522  if (bits > max_length) bits = max_length, overflow++;
523  tree[n].Len = (ush)bits;
524  /* We overwrite tree[n].Dad which is no longer needed */
525 
526  if (n > max_code) continue; /* not a leaf node */
527 
528  s->bl_count[bits]++;
529  xbits = 0;
530  if (n >= base) xbits = extra[n-base];
531  f = tree[n].Freq;
532  s->opt_len += (ulg)f * (bits + xbits);
533  if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
534  }
535  if (overflow == 0) return;
536 
537  Trace((stderr,"\nbit length overflow\n"));
538  /* This happens for example on obj2 and pic of the Calgary corpus */
539 
540  /* Find the first bit length which could increase: */
541  do {
542  bits = max_length-1;
543  while (s->bl_count[bits] == 0) bits--;
544  s->bl_count[bits]--; /* move one leaf down the tree */
545  s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
546  s->bl_count[max_length]--;
547  /* The brother of the overflow item also moves one step up,
548  * but this does not affect bl_count[max_length]
549  */
550  overflow -= 2;
551  } while (overflow > 0);
552 
553  /* Now recompute all bit lengths, scanning in increasing frequency.
554  * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
555  * lengths instead of fixing only the wrong ones. This idea is taken
556  * from 'ar' written by Haruhiko Okumura.)
557  */
558  for (bits = max_length; bits != 0; bits--) {
559  n = s->bl_count[bits];
560  while (n != 0) {
561  m = s->heap[--h];
562  if (m > max_code) continue;
563  if ((unsigned) tree[m].Len != (unsigned) bits) {
564  Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
565  s->opt_len += ((long)bits - (long)tree[m].Len)
566  *(long)tree[m].Freq;
567  tree[m].Len = (ush)bits;
568  }
569  n--;
570  }
571  }
572 }
ulg static_len
Definition: deflate.h:247
const intf * extra_bits
Definition: trees.c:133
int heap[2 *L_CODES+1]
Definition: deflate.h:205
#define MAX_BITS
Definition: deflate.h:50
ct_data * dyn_tree
Definition: deflate.h:83
static_tree_desc * stat_desc
Definition: deflate.h:85
for(p=first;p->value< newval;p=p->next)
int max_code
Definition: deflate.h:84
unsigned short ush
Definition: zutil.h:41
#define HEAP_SIZE
Definition: deflate.h:47
#define Len
Definition: deflate.h:78
ush bl_count[MAX_BITS+1]
Definition: deflate.h:202
#define Trace(x)
Definition: zutil.h:269
int FAR intf
Definition: zconf.h:345
unsigned long ulg
Definition: zutil.h:43
const ct_data * static_tree
Definition: trees.c:132
static char * bits(int n)
Definition: abcSaucy.c:201
local void gen_codes ( ct_data tree,
int  max_code,
ushf bl_count 
)

Definition at line 582 of file trees.c.

586 {
587  ush next_code[MAX_BITS+1]; /* next code value for each bit length */
588  ush code = 0; /* running code value */
589  int bits; /* bit index */
590  int n; /* code index */
591 
592  /* The distribution counts are first used to generate the code values
593  * without bit reversal.
594  */
595  for (bits = 1; bits <= MAX_BITS; bits++) {
596  next_code[bits] = code = (code + bl_count[bits-1]) << 1;
597  }
598  /* Check that the bit counts in bl_count are consistent. The last code
599  * must be all ones.
600  */
601  Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
602  "inconsistent bit counts");
603  Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
604 
605  for (n = 0; n <= max_code; n++) {
606  int len = tree[n].Len;
607  if (len == 0) continue;
608  /* Now reverse the bits */
609  tree[n].Code = bi_reverse(next_code[len]++, len);
610 
611  Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
612  n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
613  }
614 }
#define Tracecv(c, x)
Definition: zutil.h:273
#define MAX_BITS
Definition: deflate.h:50
#define Assert(cond, msg)
Definition: zutil.h:268
#define Tracev(x)
Definition: zutil.h:270
local unsigned bi_reverse(unsigned code, int len)
Definition: trees.c:1145
#define Code
Definition: deflate.h:76
unsigned short ush
Definition: zutil.h:41
Definition: inftrees.h:26
local ct_data static_ltree[L_CODES+2]
Definition: trees.c:98
static char * bits(int n)
Definition: abcSaucy.c:201
local void init_block ( deflate_state s)

Definition at line 422 of file trees.c.

423 {
424  int n; /* iterates over tree elements */
425 
426  /* Initialize the trees. */
427  for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
428  for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
429  for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
430 
431  s->dyn_ltree[END_BLOCK].Freq = 1;
432  s->opt_len = s->static_len = 0L;
433  s->last_lit = s->matches = 0;
434 }
ulg static_len
Definition: deflate.h:247
#define BL_CODES
Definition: deflate.h:44
#define END_BLOCK
Definition: trees.c:57
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
for(p=first;p->value< newval;p=p->next)
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
uInt last_lit
Definition: deflate.h:238
uInt matches
Definition: deflate.h:248
#define D_CODES
Definition: deflate.h:41
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
#define L_CODES
Definition: deflate.h:38
local void tr_static_init OF ( (void)  )
local void init_block OF ( (deflate_state *s)  )
local void pqdownheap OF ( (deflate_state *s, ct_data *tree, int k)  )
local void gen_bitlen OF ( (deflate_state *s, tree_desc *desc)  )
local void gen_codes OF ( (ct_data *tree, int max_code, ushf *bl_count)  )
local void scan_tree OF ( (deflate_state *s, ct_data *tree, int max_code)  )
local void send_all_trees OF ( (deflate_state *s, int lcodes, int dcodes, int blcodes)  )
local void compress_block OF ( (deflate_state *s, ct_data *ltree, ct_data *dtree)  )
local unsigned bi_reverse OF ( (unsigned value, int length)  )
local void copy_block OF ( (deflate_state *s, charf *buf, unsigned len, int header)  )
local void pqdownheap ( deflate_state s,
ct_data tree,
int  k 
)

Definition at line 465 of file trees.c.

466 {
467  int v = s->heap[k];
468  int j = k << 1; /* left son of k */
469  while (j <= s->heap_len) {
470  /* Set j to the smallest of the two sons: */
471  if (j < s->heap_len &&
472  smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
473  j++;
474  }
475  /* Exit if v is smaller than both sons */
476  if (smaller(tree, v, s->heap[j], s->depth)) break;
477 
478  /* Exchange v with the smallest son */
479  s->heap[k] = s->heap[j]; k = j;
480 
481  /* And continue down the tree, setting j to the left son of k */
482  j <<= 1;
483  }
484  s->heap[k] = v;
485 }
int heap[2 *L_CODES+1]
Definition: deflate.h:205
#define smaller(tree, n, m, depth)
Definition: trees.c:455
uch depth[2 *L_CODES+1]
Definition: deflate.h:212
local void scan_tree ( deflate_state s,
ct_data tree,
int  max_code 
)

Definition at line 710 of file trees.c.

711 {
712  int n; /* iterates over all tree elements */
713  int prevlen = -1; /* last emitted length */
714  int curlen; /* length of current code */
715  int nextlen = tree[0].Len; /* length of next code */
716  int count = 0; /* repeat count of the current code */
717  int max_count = 7; /* max repeat count */
718  int min_count = 4; /* min repeat count */
719 
720  if (nextlen == 0) max_count = 138, min_count = 3;
721  tree[max_code+1].Len = (ush)0xffff; /* guard */
722 
723  for (n = 0; n <= max_code; n++) {
724  curlen = nextlen; nextlen = tree[n+1].Len;
725  if (++count < max_count && curlen == nextlen) {
726  continue;
727  } else if (count < min_count) {
728  s->bl_tree[curlen].Freq += count;
729  } else if (curlen != 0) {
730  if (curlen != prevlen) s->bl_tree[curlen].Freq++;
731  s->bl_tree[REP_3_6].Freq++;
732  } else if (count <= 10) {
733  s->bl_tree[REPZ_3_10].Freq++;
734  } else {
735  s->bl_tree[REPZ_11_138].Freq++;
736  }
737  count = 0; prevlen = curlen;
738  if (nextlen == 0) {
739  max_count = 138, min_count = 3;
740  } else if (curlen == nextlen) {
741  max_count = 6, min_count = 3;
742  } else {
743  max_count = 7, min_count = 4;
744  }
745  }
746 }
#define REP_3_6
Definition: trees.c:60
#define REPZ_11_138
Definition: trees.c:66
unsigned short ush
Definition: zutil.h:41
#define REPZ_3_10
Definition: trees.c:63
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
local void send_all_trees ( deflate_state s,
int  lcodes,
int  dcodes,
int  blcodes 
)

Definition at line 834 of file trees.c.

835 {
836  int rank; /* index in bl_order */
837 
838  Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
839  Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
840  "too many codes");
841  Tracev((stderr, "\nbl counts: "));
842  send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
843  send_bits(s, dcodes-1, 5);
844  send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
845  for (rank = 0; rank < blcodes; rank++) {
846  Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
847  send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
848  }
849  Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
850 
851  send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
852  Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
853 
854  send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
855  Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
856 }
#define BL_CODES
Definition: deflate.h:44
#define Assert(cond, msg)
Definition: zutil.h:268
#define Tracev(x)
Definition: zutil.h:270
struct ct_data_s dyn_ltree[HEAP_SIZE]
Definition: deflate.h:194
#define send_bits(s, value, length)
Definition: trees.c:224
local const uch bl_order[BL_CODES]
Definition: trees.c:79
local void send_tree(deflate_state *s, ct_data *tree, int max_code)
Definition: trees.c:752
struct ct_data_s dyn_dtree[2 *D_CODES+1]
Definition: deflate.h:195
#define D_CODES
Definition: deflate.h:41
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
#define L_CODES
Definition: deflate.h:38
local void send_tree ( deflate_state s,
ct_data tree,
int  max_code 
)

Definition at line 752 of file trees.c.

753 {
754  int n; /* iterates over all tree elements */
755  int prevlen = -1; /* last emitted length */
756  int curlen; /* length of current code */
757  int nextlen = tree[0].Len; /* length of next code */
758  int count = 0; /* repeat count of the current code */
759  int max_count = 7; /* max repeat count */
760  int min_count = 4; /* min repeat count */
761 
762  /* tree[max_code+1].Len = -1; */ /* guard already set */
763  if (nextlen == 0) max_count = 138, min_count = 3;
764 
765  for (n = 0; n <= max_code; n++) {
766  curlen = nextlen; nextlen = tree[n+1].Len;
767  if (++count < max_count && curlen == nextlen) {
768  continue;
769  } else if (count < min_count) {
770  do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
771 
772  } else if (curlen != 0) {
773  if (curlen != prevlen) {
774  send_code(s, curlen, s->bl_tree); count--;
775  }
776  Assert(count >= 3 && count <= 6, " 3_6?");
777  send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
778 
779  } else if (count <= 10) {
780  send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
781 
782  } else {
783  send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
784  }
785  count = 0; prevlen = curlen;
786  if (nextlen == 0) {
787  max_count = 138, min_count = 3;
788  } else if (curlen == nextlen) {
789  max_count = 6, min_count = 3;
790  } else {
791  max_count = 7, min_count = 4;
792  }
793  }
794 }
#define Assert(cond, msg)
Definition: zutil.h:268
#define REP_3_6
Definition: trees.c:60
#define REPZ_11_138
Definition: trees.c:66
#define send_bits(s, value, length)
Definition: trees.c:224
#define REPZ_3_10
Definition: trees.c:63
struct ct_data_s bl_tree[2 *BL_CODES+1]
Definition: deflate.h:196
#define send_code(s, c, tree)
Definition: trees.c:177
local void tr_static_init ( )

Definition at line 245 of file trees.c.

246 {
247 #if defined(GEN_TREES_H) || !defined(STDC)
248  static int static_init_done = 0;
249  int n; /* iterates over tree elements */
250  int bits; /* bit counter */
251  int length; /* length value */
252  int code; /* code value */
253  int dist; /* distance index */
254  ush bl_count[MAX_BITS+1];
255  /* number of codes at each bit length for an optimal tree */
256 
257  if (static_init_done) return;
258 
259  /* For some embedded targets, global variables are not initialized: */
260 #ifdef NO_INIT_GLOBAL_POINTERS
266 #endif
267 
268  /* Initialize the mapping length (0..255) -> length code (0..28) */
269  length = 0;
270  for (code = 0; code < LENGTH_CODES-1; code++) {
271  base_length[code] = length;
272  for (n = 0; n < (1<<extra_lbits[code]); n++) {
273  _length_code[length++] = (uch)code;
274  }
275  }
276  Assert (length == 256, "tr_static_init: length != 256");
277  /* Note that the length 255 (match length 258) can be represented
278  * in two different ways: code 284 + 5 bits or code 285, so we
279  * overwrite length_code[255] to use the best encoding:
280  */
281  _length_code[length-1] = (uch)code;
282 
283  /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
284  dist = 0;
285  for (code = 0 ; code < 16; code++) {
286  base_dist[code] = dist;
287  for (n = 0; n < (1<<extra_dbits[code]); n++) {
288  _dist_code[dist++] = (uch)code;
289  }
290  }
291  Assert (dist == 256, "tr_static_init: dist != 256");
292  dist >>= 7; /* from now on, all distances are divided by 128 */
293  for ( ; code < D_CODES; code++) {
294  base_dist[code] = dist << 7;
295  for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
296  _dist_code[256 + dist++] = (uch)code;
297  }
298  }
299  Assert (dist == 256, "tr_static_init: 256+dist != 512");
300 
301  /* Construct the codes of the static literal tree */
302  for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
303  n = 0;
304  while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
305  while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
306  while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
307  while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
308  /* Codes 286 and 287 do not exist, but we must include them in the
309  * tree construction to get a canonical Huffman tree (longest code
310  * all ones)
311  */
312  gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
313 
314  /* The static distance tree is trivial: */
315  for (n = 0; n < D_CODES; n++) {
316  static_dtree[n].Len = 5;
317  static_dtree[n].Code = bi_reverse((unsigned)n, 5);
318  }
319  static_init_done = 1;
320 
321 # ifdef GEN_TREES_H
322  gen_trees_header();
323 # endif
324 #endif /* defined(GEN_TREES_H) || !defined(STDC) */
325 }
local const int extra_dbits[D_CODES]
Definition: trees.c:73
local ct_data static_dtree[D_CODES]
Definition: trees.c:105
const intf * extra_bits
Definition: trees.c:133
#define MAX_BITS
Definition: deflate.h:50
#define Assert(cond, msg)
Definition: zutil.h:268
local int base_dist[D_CODES]
Definition: trees.c:122
local void gen_codes(ct_data *tree, int max_code, ushf *bl_count)
Definition: trees.c:582
#define LENGTH_CODES
Definition: deflate.h:32
local static_tree_desc static_d_desc
Definition: trees.c:142
local int base_length[LENGTH_CODES]
Definition: trees.c:119
local unsigned bi_reverse(unsigned code, int len)
Definition: trees.c:1145
unsigned short ush
Definition: zutil.h:41
local static_tree_desc static_bl_desc
Definition: trees.c:145
local const int extra_lbits[LENGTH_CODES]
Definition: trees.c:70
Definition: inftrees.h:26
uch _dist_code[DIST_CODE_LEN]
Definition: trees.c:110
unsigned char uch
Definition: zutil.h:39
uch _length_code[MAX_MATCH-MIN_MATCH+1]
Definition: trees.c:116
const ct_data * static_tree
Definition: trees.c:132
local ct_data static_ltree[L_CODES+2]
Definition: trees.c:98
static char * bits(int n)
Definition: abcSaucy.c:201
#define D_CODES
Definition: deflate.h:41
local const int extra_blbits[BL_CODES]
Definition: trees.c:76
local static_tree_desc static_l_desc
Definition: trees.c:139
#define L_CODES
Definition: deflate.h:38

Variable Documentation

uch _dist_code[DIST_CODE_LEN]

Definition at line 110 of file trees.c.

uch _length_code[MAX_MATCH-MIN_MATCH+1]

Definition at line 116 of file trees.c.

local int base_dist[D_CODES]

Definition at line 122 of file trees.c.

local int base_length[LENGTH_CODES]

Definition at line 119 of file trees.c.

local 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 79 of file trees.c.

local 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 76 of file trees.c.

local 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 73 of file trees.c.

local 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 70 of file trees.c.

local static_tree_desc static_bl_desc
Initial value:
=
#define BL_CODES
Definition: deflate.h:44
#define MAX_BL_BITS
Definition: trees.c:54
local const int extra_blbits[BL_CODES]
Definition: trees.c:76

Definition at line 145 of file trees.c.

local static_tree_desc static_d_desc
Initial value:
=
local const int extra_dbits[D_CODES]
Definition: trees.c:73
local ct_data static_dtree[D_CODES]
Definition: trees.c:105
#define MAX_BITS
Definition: deflate.h:50
#define D_CODES
Definition: deflate.h:41

Definition at line 142 of file trees.c.

local ct_data static_dtree[D_CODES]

Definition at line 105 of file trees.c.

local static_tree_desc static_l_desc
Initial value:
=
#define MAX_BITS
Definition: deflate.h:50
#define LITERALS
Definition: deflate.h:35
local const int extra_lbits[LENGTH_CODES]
Definition: trees.c:70
local ct_data static_ltree[L_CODES+2]
Definition: trees.c:98
#define L_CODES
Definition: deflate.h:38

Definition at line 139 of file trees.c.

local ct_data static_ltree[L_CODES+2]

Definition at line 98 of file trees.c.