]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/fpga_compress.c
db1ef81b6a0fd27cd7ca04d6fb29e5d10c8a2138
[proxmark3-svn] / client / fpga_compress.c
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>
12 #include <stdint.h>
13 #include "zlib.h"
14
15 #define MAX(a,b) ((a)>(b)?(a):(b))
16
17 // zlib configuration
18 #define COMPRESS_LEVEL 9 // use best possible compression
19
20 #define FPGA_CONFIG_SIZE 42175
21 static uint8_t fpga_config[FPGA_CONFIG_SIZE];
22
23 static void usage(char *argv0)
24 {
25 fprintf(stderr, "Usage: %s <infile> <outfile>\n\n", argv0);
26 }
27
28
29 static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
30 {
31 fprintf(stderr, "zlib requested %d bytes\n", items*size);
32 return malloc(items*size);
33 }
34
35
36 static void fpga_deflate_free(voidpf opaque, voidpf address)
37 {
38 fprintf(stderr, "zlib frees memory\n");
39 return free(address);
40 }
41
42
43 int zlib_compress(FILE *infile, FILE *outfile)
44 {
45 int i, ret;
46 z_stream compressed_fpga_stream;
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;
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 }
61
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;
67
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);
74
75 ret = deflateInit(&compressed_fpga_stream, COMPRESS_LEVEL);
76
77 if (ret == Z_OK) {
78 ret = deflate(&compressed_fpga_stream, Z_FINISH);
79 }
80
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 }
91
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);
98 fclose(infile);
99 fclose(outfile);
100
101 return 0;
102
103 }
104
105
106
107 int main(int argc, char **argv)
108 {
109 char *infilename;
110 char *outfilename;
111
112 if (argc != 3) {
113 usage(argv[0]);
114 return -1;
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
133 return zlib_compress(infile, outfile);
134 }
Impressum, Datenschutz