diff options
| author | Hans Kristian Rosbach <hk-git@circlestorm.org> | 2019-09-15 18:15:08 +0200 |
|---|---|---|
| committer | Hans Kristian Rosbach <hk-github@circlestorm.org> | 2019-09-20 22:29:21 +0200 |
| commit | 8dd3bb231720c90eb3e7fe8c6168083215013da4 (patch) | |
| tree | ab3194fda713ac33d0f34cc1be03984311f7d7bb /tools | |
| parent | aaa3cc2f0bf90bcb81044096be3c79165a7a900d (diff) | |
| download | Project-Tick-8dd3bb231720c90eb3e7fe8c6168083215013da4.tar.gz Project-Tick-8dd3bb231720c90eb3e7fe8c6168083215013da4.zip | |
Add maketrees test and clean up maketrees.c code
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/maketrees.c | 89 |
1 files changed, 28 insertions, 61 deletions
diff --git a/tools/maketrees.c b/tools/maketrees.c index cabd4a4599..54ab4c09ee 100644 --- a/tools/maketrees.c +++ b/tools/maketrees.c @@ -1,30 +1,26 @@ -/* maketrees.c -- output trees.h header file +/* maketrees.c -- output static huffman trees * Copyright (C) 1995-2017 Jean-loup Gailly * For conditions of distribution and use, see copyright notice in zlib.h */ #include <stdio.h> #include "zbuild.h" -#include "zutil.h" #include "deflate.h" #include "trees_p.h" static ct_data static_ltree[L_CODES+2]; /* The static literal tree. Since the bit lengths are imposed, there is no * need for the L_CODES extra codes used during heap construction. However - * The codes 286 and 287 are needed to build a canonical tree (see zng_tr_init - * below). + * The codes 286 and 287 are needed to build a canonical tree (see zng_tr_init). */ static ct_data static_dtree[D_CODES]; -/* The static distance tree. (Actually a trivial tree since all codes use - * 5 bits.) +/* The static distance tree. (Actually a trivial tree since all codes use 5 bits.) */ static unsigned char dist_code[DIST_CODE_LEN]; -/* Distance codes. The first 256 values correspond to the distances - * 3 .. 258, the last 256 values correspond to the top 8 bits of - * the 15 bit distances. +/* Distance codes. The first 256 values correspond to the distances 3 .. 258, + * the last 256 values correspond to the top 8 bits of the 15 bit distances. */ static unsigned char length_code[MAX_MATCH-MIN_MATCH+1]; @@ -37,12 +33,7 @@ static int base_dist[D_CODES]; /* First normalized distance for each code (0 = distance of 1) */ -static void tr_static_init(void); -static void gen_trees_header (void); - - static void tr_static_init(void) { - static int static_init_done = 0; int n; /* iterates over tree elements */ int bits; /* bit counter */ int length; /* length value */ @@ -51,18 +42,6 @@ static void tr_static_init(void) { uint16_t bl_count[MAX_BITS+1]; /* number of codes at each bit length for an optimal tree */ - if (static_init_done) - return; - - /* For some embedded targets, global variables are not initialized: */ -#ifdef NO_INIT_GLOBAL_POINTERS - static_l_desc.static_tree = static_ltree; - static_l_desc.extra_bits = extra_lbits; - static_d_desc.static_tree = static_dtree; - static_d_desc.extra_bits = extra_dbits; - static_bl_desc.extra_bits = extra_blbits; -#endif - /* Initialize the mapping length (0..255) -> length code (0..28) */ length = 0; for (code = 0; code < LENGTH_CODES-1; code++) { @@ -72,15 +51,14 @@ static void tr_static_init(void) { } } Assert(length == 256, "tr_static_init: length != 256"); - /* Note that the length 255 (match length 258) can be represented - * in two different ways: code 284 + 5 bits or code 285, so we - * overwrite length_code[255] to use the best encoding: + /* Note that the length 255 (match length 258) can be represented in two different + * ways: code 284 + 5 bits or code 285, so we overwrite length_code[255] to use the best encoding: */ length_code[length-1] = (unsigned char)code; /* Initialize the mapping dist (0..32K) -> dist code (0..29) */ dist = 0; - for (code = 0 ; code < 16; code++) { + 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; @@ -104,9 +82,8 @@ static void tr_static_init(void) { 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]++; - /* Codes 286 and 287 do not exist, but we must include them in the - * tree construction to get a canonical Huffman tree (longest code - * all ones) + /* Codes 286 and 287 do not exist, but we must include them in the tree construction + * to get a canonical Huffman tree (longest code all ones) */ gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); @@ -115,66 +92,56 @@ static void tr_static_init(void) { static_dtree[n].Len = 5; static_dtree[n].Code = bi_reverse((unsigned)n, 5); } - static_init_done = 1; - - gen_trees_header(); } -# ifndef ZLIB_DEBUG -# include <stdio.h> -# endif - # define SEPARATOR(i, last, width) \ ((i) == (last)? "\n};\n\n" : \ ((i) % (width) == (width)-1 ? ",\n" : ", ")) - -void gen_trees_header() { - FILE *header = fopen("trees.h", "w"); +static void gen_trees_header() { int i; - Assert(header != NULL, "Can't open trees.h"); - fprintf(header, "#ifndef TREES_H_\n"); - fprintf(header, "#define TREES_H_\n\n"); + printf("#ifndef TREES_H_\n"); + printf("#define TREES_H_\n\n"); - fprintf(header, "/* header created automatically with -DGEN_TREES_H */\n\n"); + printf("/* header created automatically with maketrees.c */\n\n"); - fprintf(header, "ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n"); + printf("ZLIB_INTERNAL const ct_data static_ltree[L_CODES+2] = {\n"); for (i = 0; i < L_CODES+2; i++) { - fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); + printf("{{%3u},{%u}}%s", static_ltree[i].Code, static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); } - fprintf(header, "static const ct_data static_dtree[D_CODES] = {\n"); + printf("static const ct_data static_dtree[D_CODES] = {\n"); for (i = 0; i < D_CODES; i++) { - fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); + printf("{{%2u},{%u}}%s", static_dtree[i].Code, static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); } - fprintf(header, "const unsigned char ZLIB_INTERNAL zng_dist_code[DIST_CODE_LEN] = {\n"); + printf("const unsigned char ZLIB_INTERNAL zng_dist_code[DIST_CODE_LEN] = {\n"); for (i = 0; i < DIST_CODE_LEN; i++) { - fprintf(header, "%2u%s", dist_code[i], SEPARATOR(i, DIST_CODE_LEN-1, 20)); + printf("%2u%s", dist_code[i], SEPARATOR(i, DIST_CODE_LEN-1, 20)); } - fprintf(header, "const unsigned char ZLIB_INTERNAL zng_length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); + printf("const unsigned char ZLIB_INTERNAL zng_length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { - fprintf(header, "%2u%s", length_code[i], SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); + printf("%2u%s", length_code[i], SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); } - fprintf(header, "static const int base_length[LENGTH_CODES] = {\n"); + printf("static const int base_length[LENGTH_CODES] = {\n"); for (i = 0; i < LENGTH_CODES; i++) { - fprintf(header, "%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20)); + printf("%d%s", base_length[i], SEPARATOR(i, LENGTH_CODES-1, 20)); } - fprintf(header, "static const int base_dist[D_CODES] = {\n"); + printf("static const int base_dist[D_CODES] = {\n"); for (i = 0; i < D_CODES; i++) { - fprintf(header, "%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10)); + printf("%5d%s", base_dist[i], SEPARATOR(i, D_CODES-1, 10)); } - fprintf(header, "#endif /* TREES_H_ */\n"); - fclose(header); + printf("#endif /* TREES_H_ */\n"); } // The output of this application can be piped out to recreate trees.h int main(void) { tr_static_init(); + gen_trees_header(); return 0; } |
