]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/fpga_compress.c
minor lf updates/cleanup
[proxmark3-svn] / client / fpga_compress.c
CommitLineData
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//-----------------------------------------------------------------------------
8e074056 6// Compression tool for FPGA config files. Compress several *.bit files at
7// compile time. Decompression is done at run time (see fpgaloader.c).
8// This uses the zlib library tuned to this specific case. The small file sizes
9// allow to use "insane" parameters for optimum compression ratio.
e6153040 10//-----------------------------------------------------------------------------
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
f3919878 15#include <stdint.h>
fb228974 16#include <stdbool.h>
f3919878 17#include "zlib.h"
e6153040 18
19#define MAX(a,b) ((a)>(b)?(a):(b))
20
f3919878 21// zlib configuration
22#define COMPRESS_LEVEL 9 // use best possible compression
8e074056 23#define COMPRESS_WINDOW_BITS 15 // default = max = 15 for a window of 2^15 = 32KBytes
24#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8.
fb228974 25/* COMPRESS_STRATEGY can be
26 Z_DEFAULT_STRATEGY (the default),
27 Z_FILTERED (more huffmann, less string matching),
28 Z_HUFFMAN_ONLY (huffman only, no string matching)
29 Z_RLE (distances limited to one)
30 Z_FIXED (prevents the use of dynamic Huffman codes)
31*/
32#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
33// zlib tuning parameters:
34#define COMPRESS_GOOD_LENGTH 258
35#define COMPRESS_MAX_LAZY 258
36#define COMPRESS_MAX_NICE_LENGTH 258
37#define COMPRESS_MAX_CHAIN 8192
38
39#define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
40#define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
e6153040 41
8e074056 42static void usage(void)
e6153040 43{
8e074056 44 fprintf(stderr, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
45 fprintf(stderr, " Combine n FPGA bitstream files and compress them into one.\n\n");
46 fprintf(stderr, " fpga_compress -d <infile> <outfile>");
47 fprintf(stderr, " Decompress <infile>. Write result to <outfile>");
e6153040 48}
49
50
f3919878 51static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
e6153040 52{
f3919878 53 return malloc(items*size);
e6153040 54}
e6153040 55
f3919878 56
57static void fpga_deflate_free(voidpf opaque, voidpf address)
e6153040 58{
f3919878 59 return free(address);
60}
e6153040 61
f3919878 62
fb228974 63static bool all_feof(FILE *infile[], uint8_t num_infiles)
64{
65 for (uint16_t i = 0; i < num_infiles; i++) {
66 if (!feof(infile[i])) {
67 return false;
68 }
69 }
70
71 return true;
72}
73
74
75int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
e6153040 76{
fb228974 77 uint8_t *fpga_config;
78 uint32_t i;
79 int ret;
80 uint8_t c;
f3919878 81 z_stream compressed_fpga_stream;
fb228974 82
83 fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
e6153040 84
8e074056 85 // read the input files. Interleave them into fpga_config[]
e6153040 86 i = 0;
fb228974 87 do {
7b242c1c 88
89 if (i >= num_infiles * FPGA_CONFIG_SIZE) {
90 fprintf(stderr, "Input files too big (total > %lu bytes). These are probably not PM3 FPGA config files.\n", num_infiles*FPGA_CONFIG_SIZE);
91 for(uint16_t j = 0; j < num_infiles; j++) {
92 fclose(infile[j]);
93 }
94 return(EXIT_FAILURE);
95 }
96
fb228974 97 for(uint16_t j = 0; j < num_infiles; j++) {
98 for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
99 c = fgetc(infile[j]);
0fa01ec7 100 if (!feof(infile[j])) {
101 fpga_config[i++] = c;
102 } else if (num_infiles > 1) {
103 fpga_config[i++] = '\0';
104 }
fb228974 105 }
106 }
107
fb228974 108 } while (!all_feof(infile, num_infiles));
e6153040 109
f3919878 110 // initialize zlib structures
111 compressed_fpga_stream.next_in = fpga_config;
112 compressed_fpga_stream.avail_in = i;
113 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
114 compressed_fpga_stream.zfree = fpga_deflate_free;
e6153040 115
fb228974 116 ret = deflateInit2(&compressed_fpga_stream,
117 COMPRESS_LEVEL,
118 Z_DEFLATED,
119 COMPRESS_WINDOW_BITS,
120 COMPRESS_MEM_LEVEL,
121 COMPRESS_STRATEGY);
122
f3919878 123 // estimate the size of the compressed output
124 unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
125 uint8_t *outbuf = malloc(outsize_max);
126 compressed_fpga_stream.next_out = outbuf;
127 compressed_fpga_stream.avail_out = outsize_max;
fb228974 128
129 if (ret == Z_OK) {
130 ret = deflateTune(&compressed_fpga_stream,
131 COMPRESS_GOOD_LENGTH,
132 COMPRESS_MAX_LAZY,
133 COMPRESS_MAX_NICE_LENGTH,
134 COMPRESS_MAX_CHAIN);
135 }
136
f3919878 137 if (ret == Z_OK) {
138 ret = deflate(&compressed_fpga_stream, Z_FINISH);
e6153040 139 }
e6153040 140
97a0f5f4 141 fprintf(stderr, "compressed %lu input bytes to %lu output bytes\n", i, compressed_fpga_stream.total_out);
f3919878 142
143 if (ret != Z_STREAM_END) {
144 fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg);
145 free(outbuf);
146 deflateEnd(&compressed_fpga_stream);
fb228974 147 for(uint16_t j = 0; j < num_infiles; j++) {
148 fclose(infile[j]);
149 }
f3919878 150 fclose(outfile);
fb228974 151 free(infile);
152 free(fpga_config);
7b242c1c 153 return(EXIT_FAILURE);
f3919878 154 }
e6153040 155
f3919878 156 for (i = 0; i < compressed_fpga_stream.total_out; i++) {
157 fputc(outbuf[i], outfile);
158 }
159
160 free(outbuf);
161 deflateEnd(&compressed_fpga_stream);
fb228974 162 for(uint16_t j = 0; j < num_infiles; j++) {
163 fclose(infile[j]);
164 }
e6153040 165 fclose(outfile);
fb228974 166 free(infile);
167 free(fpga_config);
168
7b242c1c 169 return(EXIT_SUCCESS);
f3919878 170
e6153040 171}
172
173
4b3f6d79 174int zlib_decompress(FILE *infile, FILE *outfile)
175{
176 #define DECOMPRESS_BUF_SIZE 1024
177 uint8_t outbuf[DECOMPRESS_BUF_SIZE];
178 uint8_t inbuf[DECOMPRESS_BUF_SIZE];
179 int ret;
180
181 z_stream compressed_fpga_stream;
8e074056 182
4b3f6d79 183 // initialize zlib structures
184 compressed_fpga_stream.next_in = inbuf;
185 compressed_fpga_stream.avail_in = 0;
186 compressed_fpga_stream.next_out = outbuf;
187 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
188 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
189 compressed_fpga_stream.zfree = fpga_deflate_free;
190
191 ret = inflateInit2(&compressed_fpga_stream, 0);
192
193 do {
194 if (compressed_fpga_stream.avail_in == 0) {
195 compressed_fpga_stream.next_in = inbuf;
196 uint16_t i = 0;
197 do {
198 uint8_t c = fgetc(infile);
199 if (!feof(infile)) {
200 inbuf[i++] = c;
201 compressed_fpga_stream.avail_in++;
202 } else {
203 break;
204 }
205 } while (i < DECOMPRESS_BUF_SIZE);
206 }
207
208 ret = inflate(&compressed_fpga_stream, Z_SYNC_FLUSH);
209
210 if (ret != Z_OK && ret != Z_STREAM_END) {
211 break;
212 }
213
214 if (compressed_fpga_stream.avail_out == 0) {
215 for (uint16_t i = 0; i < DECOMPRESS_BUF_SIZE; i++) {
216 fputc(outbuf[i], outfile);
217 }
218 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
219 compressed_fpga_stream.next_out = outbuf;
220 }
221 } while (ret == Z_OK);
222
223 if (ret == Z_STREAM_END) { // reached end of input
224 uint16_t i = 0;
225 while (compressed_fpga_stream.avail_out < DECOMPRESS_BUF_SIZE) {
226 fputc(outbuf[i++], outfile);
227 compressed_fpga_stream.avail_out++;
228 }
229 fclose(outfile);
230 fclose(infile);
7b242c1c 231 return(EXIT_SUCCESS);
4b3f6d79 232 } else {
233 fprintf(stderr, "Error. Inflate() returned error %d, %s", ret, compressed_fpga_stream.msg);
234 fclose(outfile);
235 fclose(infile);
7b242c1c 236 return(EXIT_FAILURE);
4b3f6d79 237 }
238
239}
240
f3919878 241
e6153040 242int main(int argc, char **argv)
243{
fb228974 244 FILE **infiles;
245 FILE *outfile;
e6153040 246
fb228974 247 if (argc == 1 || argc == 2) {
8e074056 248 usage();
7b242c1c 249 return(EXIT_FAILURE);
fb228974 250 }
251
8e074056 252 if (!strcmp(argv[1], "-d")) { // Decompress
4b3f6d79 253 infiles = calloc(1, sizeof(FILE*));
254 if (argc != 4) {
8e074056 255 usage();
7b242c1c 256 return(EXIT_FAILURE);
4b3f6d79 257 }
258 infiles[0] = fopen(argv[2], "rb");
259 if (infiles[0] == NULL) {
260 fprintf(stderr, "Error. Cannot open input file %s", argv[2]);
7b242c1c 261 return(EXIT_FAILURE);
4b3f6d79 262 }
263 outfile = fopen(argv[3], "wb");
264 if (outfile == NULL) {
265 fprintf(stderr, "Error. Cannot open output file %s", argv[3]);
7b242c1c 266 return(EXIT_FAILURE);
4b3f6d79 267 }
268 return zlib_decompress(infiles[0], outfile);
4b3f6d79 269
8e074056 270 } else { // Compress
271
272 infiles = calloc(argc-2, sizeof(FILE*));
273 for (uint16_t i = 0; i < argc-2; i++) {
274 infiles[i] = fopen(argv[i+1], "rb");
275 if (infiles[i] == NULL) {
276 fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
7b242c1c 277 return(EXIT_FAILURE);
8e074056 278 }
279 }
280 outfile = fopen(argv[argc-1], "wb");
281 if (outfile == NULL) {
282 fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
7b242c1c 283 return(EXIT_FAILURE);
e6153040 284 }
8e074056 285 return zlib_compress(infiles, argc-2, outfile);
fb228974 286 }
e6153040 287}
Impressum, Datenschutz