| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include "zbuild.h" |
| 8 |
#include "deflate.h" |
| 9 |
#include "trees.h" |
| 10 |
|
| 11 |
static ct_data static_ltree[L_CODES+2]; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
static ct_data static_dtree[D_CODES]; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static unsigned char dist_code[DIST_CODE_LEN]; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
static unsigned char length_code[STD_MAX_MATCH-STD_MIN_MATCH+1]; |
| 27 |
|
| 28 |
|
| 29 |
static int base_length[LENGTH_CODES]; |
| 30 |
|
| 31 |
|
| 32 |
static int base_dist[D_CODES]; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static void tr_static_init(void) { |
| 37 |
int n; |
| 38 |
int bits; |
| 39 |
int length; |
| 40 |
int code; |
| 41 |
int dist; |
| 42 |
uint16_t bl_count[MAX_BITS+1]; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
length = 0; |
| 47 |
for (code = 0; code < LENGTH_CODES-1; code++) { |
| 48 |
base_length[code] = length; |
| 49 |
for (n = 0; n < (1 << extra_lbits[code]); n++) { |
| 50 |
length_code[length++] = (unsigned char)code; |
| 51 |
} |
| 52 |
} |
| 53 |
Assert(length == 256, "tr_static_init: length != 256"); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
length_code[length-1] = (unsigned char)code; |
| 58 |
|
| 59 |
|
| 60 |
dist = 0; |
| 61 |
for (code = 0; code < 16; code++) { |
| 62 |
base_dist[code] = dist; |
| 63 |
for (n = 0; n < (1 << extra_dbits[code]); n++) { |
| 64 |
dist_code[dist++] = (unsigned char)code; |
| 65 |
} |
| 66 |
} |
| 67 |
Assert(dist == 256, "tr_static_init: dist != 256"); |
| 68 |
dist >>= 7; |
| 69 |
for ( ; code < D_CODES; code++) { |
| 70 |
base_dist[code] = dist << 7; |
| 71 |
for (n = 0; n < (1 << (extra_dbits[code]-7)); n++) { |
| 72 |
dist_code[256 + dist++] = (unsigned char)code; |
| 73 |
} |
| 74 |
} |
| 75 |
Assert(dist == 256, "tr_static_init: 256+dist != 512"); |
| 76 |
|
| 77 |
|
| 78 |
for (bits = 0; bits <= MAX_BITS; bits++) |
| 79 |
bl_count[bits] = 0; |
| 80 |
n = 0; |
| 81 |
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; |
| 82 |
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; |
| 83 |
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; |
| 84 |
while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); |
| 89 |
|
| 90 |
|
| 91 |
for (n = 0; n < D_CODES; n++) { |
| 92 |
static_dtree[n].Len = 5; |
| 93 |
static_dtree[n].Code = PREFIX(bi_reverse)((unsigned)n, 5); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
# define SEPARATOR(i, last, width) \ |
| 98 |
((i) == (last)? "\n};\n\n" : \ |
| 99 |
((i) % (width) == (width)-1 ? ",\n" : ", ")) |
| 100 |
|
| 101 |
static void gen_trees_header(void) { |
| 102 |
int i; |
| 103 |
|
| 104 |
printf("#ifndef TREES_TBL_H_\n"); |
| 105 |
printf("#define TREES_TBL_H_\n\n"); |
| 106 |
|
| 107 |
printf("/* header created automatically with maketrees.c */\n\n"); |
| 108 |
|
| 109 |
printf("Z_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n"); |
| 110 |
for (i = 0; i < L_CODES+2; i++) { |
| 111 |
printf("{{%3u},{%u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); |
| 112 |
} |
| 113 |
|
| 114 |
printf("Z_INTERNAL const ct_data static_dtree[D_CODES] = {\n"); |
| 115 |
for (i = 0; i < D_CODES; i++) { |
| 116 |
printf("{{%2u},{%u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); |
| 117 |
} |
| 118 |
|
| 119 |
printf("const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN] = {\n"); |
| 120 |
for (i = 0; i < DIST_CODE_LEN; i++) { |
| 121 |
printf("%2u%s", dist_code[i], SEPARATOR(i, DIST_CODE_LEN-1, 20)); |
| 122 |
} |
| 123 |
|
| 124 |
printf("const unsigned char Z_INTERNAL zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1] = {\n"); |
| 125 |
for (i = 0; i < STD_MAX_MATCH-STD_MIN_MATCH+1; i++) { |
| 126 |
printf("%2u%s", length_code[i], SEPARATOR(i, STD_MAX_MATCH-STD_MIN_MATCH, 20)); |
| 127 |
} |
| 128 |
|
| 129 |
printf("Z_INTERNAL const int base_length[LENGTH_CODES] = {\n"); |
| 130 |
for (i = 0; i < LENGTH_CODES; i++) { |
| 131 |
printf("%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20)); |
| 132 |
} |
| 133 |
|
| 134 |
printf("Z_INTERNAL const int base_dist[D_CODES] = {\n"); |
| 135 |
for (i = 0; i < D_CODES; i++) { |
| 136 |
printf("%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10)); |
| 137 |
} |
| 138 |
|
| 139 |
printf("#endif /* TREES_TBL_H_ */\n"); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
int main(void) { |
| 144 |
tr_static_init(); |
| 145 |
gen_trees_header(); |
| 146 |
return 0; |
| 147 |
} |