- uint16_t max_codesize = set_codesize(heap_ptr, 0);
-
- fprintf(stderr, "\nStatistics: (symbol: count, codesize)\n");
- uint32_t compressed_size = 0;
- for (i = 0; i < 256; i++) {
- fprintf(stderr, "%3d: %5d, %d\n", leaf_nodes[i].symbol, leaf_nodes[i].count, leaf_nodes[i].code_size);
- compressed_size += leaf_nodes[i].count * leaf_nodes[i].code_size;
- }
- fprintf(stderr, "Compressed size = %ld (%f% of original size)", (compressed_size+7)/8, (float)(compressed_size)/(FPGA_CONFIG_SIZE * 8) * 100);
- fprintf(stderr, "Max Codesize = %d bits", max_codesize);
+ // estimate the size of the compressed output
+ unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
+ uint8_t *outbuf = malloc(outsize_max);
+ compressed_fpga_stream.next_out = outbuf;
+ compressed_fpga_stream.avail_out = outsize_max;
+ fprintf(stderr, "Allocated %d bytes for output file (estimated upper bound)\n", outsize_max);