]>
Commit | Line | Data |
---|---|---|
e6153040 | 1 | //----------------------------------------------------------------------------- |
2 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
3 | // at your option, any later version. See the LICENSE.txt file for the text of | |
4 | // the license. | |
5 | //----------------------------------------------------------------------------- | |
6 | // Flasher frontend tool | |
7 | //----------------------------------------------------------------------------- | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
f3919878 | 12 | #include <stdint.h> |
13 | #include "zlib.h" | |
e6153040 | 14 | |
15 | #define MAX(a,b) ((a)>(b)?(a):(b)) | |
16 | ||
f3919878 | 17 | // zlib configuration |
18 | #define COMPRESS_LEVEL 9 // use best possible compression | |
e6153040 | 19 | |
20 | #define FPGA_CONFIG_SIZE 42175 | |
21 | static uint8_t fpga_config[FPGA_CONFIG_SIZE]; | |
e6153040 | 22 | |
23 | static void usage(char *argv0) | |
24 | { | |
f3919878 | 25 | fprintf(stderr, "Usage: %s <infile> <outfile>\n\n", argv0); |
e6153040 | 26 | } |
27 | ||
28 | ||
f3919878 | 29 | static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size) |
e6153040 | 30 | { |
f3919878 | 31 | fprintf(stderr, "zlib requested %d bytes\n", items*size); |
32 | return malloc(items*size); | |
e6153040 | 33 | } |
e6153040 | 34 | |
f3919878 | 35 | |
36 | static void fpga_deflate_free(voidpf opaque, voidpf address) | |
e6153040 | 37 | { |
f3919878 | 38 | fprintf(stderr, "zlib frees memory\n"); |
39 | return free(address); | |
40 | } | |
e6153040 | 41 | |
f3919878 | 42 | |
43 | int zlib_compress(FILE *infile, FILE *outfile) | |
e6153040 | 44 | { |
f3919878 | 45 | int i, ret; |
46 | z_stream compressed_fpga_stream; | |
e6153040 | 47 | |
48 | // read the input file into fpga_config[] and count occurrences of each symbol: | |
49 | i = 0; | |
50 | while(!feof(infile)) { | |
51 | uint8_t c; | |
52 | c = fgetc(infile); | |
53 | fpga_config[i++] = c; | |
e6153040 | 54 | if (i > FPGA_CONFIG_SIZE+1) { |
55 | fprintf(stderr, "Input file too big (> %d bytes). This is probably not a PM3 FPGA config file.", FPGA_CONFIG_SIZE); | |
56 | fclose(infile); | |
57 | fclose(outfile); | |
58 | return -1; | |
59 | } | |
60 | } | |
e6153040 | 61 | |
f3919878 | 62 | // initialize zlib structures |
63 | compressed_fpga_stream.next_in = fpga_config; | |
64 | compressed_fpga_stream.avail_in = i; | |
65 | compressed_fpga_stream.zalloc = fpga_deflate_malloc; | |
66 | compressed_fpga_stream.zfree = fpga_deflate_free; | |
e6153040 | 67 | |
f3919878 | 68 | // estimate the size of the compressed output |
69 | unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in); | |
70 | uint8_t *outbuf = malloc(outsize_max); | |
71 | compressed_fpga_stream.next_out = outbuf; | |
72 | compressed_fpga_stream.avail_out = outsize_max; | |
73 | fprintf(stderr, "Allocated %d bytes for output file (estimated upper bound)\n", outsize_max); | |
e6153040 | 74 | |
f3919878 | 75 | ret = deflateInit(&compressed_fpga_stream, COMPRESS_LEVEL); |
76 | ||
77 | if (ret == Z_OK) { | |
78 | ret = deflate(&compressed_fpga_stream, Z_FINISH); | |
e6153040 | 79 | } |
e6153040 | 80 | |
f3919878 | 81 | fprintf(stderr, "produced %d bytes of output\n", compressed_fpga_stream.total_out); |
82 | ||
83 | if (ret != Z_STREAM_END) { | |
84 | fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg); | |
85 | free(outbuf); | |
86 | deflateEnd(&compressed_fpga_stream); | |
87 | fclose(infile); | |
88 | fclose(outfile); | |
89 | return -1; | |
90 | } | |
e6153040 | 91 | |
f3919878 | 92 | for (i = 0; i < compressed_fpga_stream.total_out; i++) { |
93 | fputc(outbuf[i], outfile); | |
94 | } | |
95 | ||
96 | free(outbuf); | |
97 | deflateEnd(&compressed_fpga_stream); | |
e6153040 | 98 | fclose(infile); |
99 | fclose(outfile); | |
e6153040 | 100 | |
e6153040 | 101 | return 0; |
f3919878 | 102 | |
e6153040 | 103 | } |
104 | ||
105 | ||
f3919878 | 106 | |
e6153040 | 107 | int main(int argc, char **argv) |
108 | { | |
e6153040 | 109 | char *infilename; |
110 | char *outfilename; | |
111 | ||
f3919878 | 112 | if (argc != 3) { |
e6153040 | 113 | usage(argv[0]); |
114 | return -1; | |
e6153040 | 115 | } else { |
116 | infilename = argv[1]; | |
117 | outfilename = argv[2]; | |
118 | } | |
119 | ||
120 | FILE *infile = fopen(infilename, "rb"); | |
121 | if (infile == NULL) { | |
122 | fprintf(stderr, "Error. Cannot open input file %s", infilename); | |
123 | return -1; | |
124 | } | |
125 | ||
126 | FILE *outfile = fopen(outfilename, "wb"); | |
127 | if (outfile == NULL) { | |
128 | fprintf(stderr, "Error. Cannot open output file %s", outfilename); | |
129 | fclose(infile); | |
130 | return -1; | |
131 | } | |
132 | ||
f3919878 | 133 | return zlib_compress(infile, outfile); |
e6153040 | 134 | } |