1 #include <stdio.h>
2 #include "zbuild.h"
3 #include "zutil.h"
4 #include "inftrees.h"
5 #include "inflate.h"
6 
7 // Build and return state with length and distance decoding tables and index sizes set to fixed code decoding.
buildfixedtables(struct inflate_state * state)8 void Z_INTERNAL buildfixedtables(struct inflate_state *state) {
9     static code *lenfix, *distfix;
10     static code fixed[544];
11 
12     // build fixed huffman tables
13     unsigned sym, bits;
14     static code *next;
15 
16     // literal/length table
17     sym = 0;
18     while (sym < 144) state->lens[sym++] = 8;
19     while (sym < 256) state->lens[sym++] = 9;
20     while (sym < 280) state->lens[sym++] = 7;
21     while (sym < 288) state->lens[sym++] = 8;
22     next = fixed;
23     lenfix = next;
24     bits = 9;
25     zng_inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
26 
27     // distance table
28     sym = 0;
29     while (sym < 32) state->lens[sym++] = 5;
30     distfix = next;
31     bits = 5;
32     zng_inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
33 
34     state->lencode = lenfix;
35     state->lenbits = 9;
36     state->distcode = distfix;
37     state->distbits = 5;
38 }
39 
40 
41 //  Create fixed tables on the fly and write out a inffixed_tbl.h file that is #include'd above.
42 //  makefixed() writes those tables to stdout, which would be piped to inffixed_tbl.h.
makefixed(void)43 void makefixed(void) {
44     unsigned low, size;
45     struct inflate_state state;
46 
47     memset(&state, 0, sizeof(state));
48     buildfixedtables(&state);
49     puts("    /* inffixed_tbl.h -- table for decoding fixed codes");
50     puts("     * Generated automatically by makefixed().");
51     puts("     */");
52     puts("");
53     puts("    /* WARNING: this file should *not* be used by applications.");
54     puts("       It is part of the implementation of this library and is");
55     puts("       subject to change. Applications should only use zlib.h.");
56     puts("     */");
57     puts("");
58     size = 1U << 9;
59     printf("    static const code lenfix[%u] = {", size);
60     low = 0;
61     for (;;) {
62         if ((low % 7) == 0)
63             printf("\n        ");
64         printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
65             state.lencode[low].bits, state.lencode[low].val);
66         if (++low == size)
67             break;
68         putchar(',');
69     }
70     puts("\n    };");
71     size = 1U << 5;
72     printf("\n    static const code distfix[%u] = {", size);
73     low = 0;
74     for (;;) {
75         if ((low % 6) == 0)
76             printf("\n        ");
77         printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, state.distcode[low].val);
78         if (++low == size)
79             break;
80         putchar(',');
81     }
82     puts("\n    };");
83 }
84 
85 // The output of this application can be piped out to recreate inffixed_tbl.h
main(void)86 int main(void) {
87     makefixed();
88     return 0;
89 }
90