#include <stdio.h>
#include "zbuild.h"
#include "deflate.h"
#include "trees.h"
static ct_data static_ltree[L_CODES+2];
static ct_data static_dtree[D_CODES];
static unsigned char dist_code[DIST_CODE_LEN];
static unsigned char length_code[STD_MAX_MATCH-STD_MIN_MATCH+1];
static int base_length[LENGTH_CODES];
static int base_dist[D_CODES];
static void tr_static_init(void) {
int n;
int bits;
int length;
int code;
int dist;
uint16_t bl_count[MAX_BITS+1];
length = 0;
for (code = 0; code < LENGTH_CODES-1; code++) {
base_length[code] = length;
for (n = 0; n < (1 << extra_lbits[code]); n++) {
length_code[length++] = (unsigned char)code;
}
}
Assert(length == 256, "tr_static_init: length != 256");
length_code[length-1] = (unsigned char)code;
dist = 0;
for (code = 0; code < 16; code++) {
base_dist[code] = dist;
for (n = 0; n < (1 << extra_dbits[code]); n++) {
dist_code[dist++] = (unsigned char)code;
}
}
Assert(dist == 256, "tr_static_init: dist != 256");
dist >>= 7;
for ( ; code < D_CODES; code++) {
base_dist[code] = dist << 7;
for (n = 0; n < (1 << (extra_dbits[code]-7)); n++) {
dist_code[256 + dist++] = (unsigned char)code;
}
}
Assert(dist == 256, "tr_static_init: 256+dist != 512");
for (bits = 0; bits <= MAX_BITS; bits++)
bl_count[bits] = 0;
n = 0;
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
for (n = 0; n < D_CODES; n++) {
static_dtree[n].Len = 5;
static_dtree[n].Code = bi_reverse((unsigned)n, 5);
}
}
# define SEPARATOR(i, last, width) \
((i) == (last)? "\n};\n\n" : \
((i) % (width) == (width)-1 ? ",\n" : ", "))
static void gen_trees_header() {
int i;
printf("#ifndef TREES_TBL_H_\n");
printf("#define TREES_TBL_H_\n\n");
printf("/* header created automatically with maketrees.c */\n\n");
printf("Z_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n");
for (i = 0; i < L_CODES+2; i++) {
printf("{{%3u},{%u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
}
printf("Z_INTERNAL const ct_data static_dtree[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
printf("{{%2u},{%u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
}
printf("const unsigned char Z_INTERNAL zng_dist_code[DIST_CODE_LEN] = {\n");
for (i = 0; i < DIST_CODE_LEN; i++) {
printf("%2u%s", dist_code[i], SEPARATOR(i, DIST_CODE_LEN-1, 20));
}
printf("const unsigned char Z_INTERNAL zng_length_code[STD_MAX_MATCH-STD_MIN_MATCH+1] = {\n");
for (i = 0; i < STD_MAX_MATCH-STD_MIN_MATCH+1; i++) {
printf("%2u%s", length_code[i], SEPARATOR(i, STD_MAX_MATCH-STD_MIN_MATCH, 20));
}
printf("Z_INTERNAL const int base_length[LENGTH_CODES] = {\n");
for (i = 0; i < LENGTH_CODES; i++) {
printf("%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20));
}
printf("Z_INTERNAL const int base_dist[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
printf("%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10));
}
printf("#endif /* TREES_TBL_H_ */\n");
}
int main(void) {
tr_static_init();
gen_trees_header();
return 0;
}