2 * A tool for reading the zlib compressed calibration data
3 * found in AVM Fritz!Box based devices).
5 * Copyright (c) 2017 Christian Lamparter <chunkeey@googlemail.com>
7 * Based on zpipe, which is an example of proper use of zlib's inflate().
8 * that is Not copyrighted -- provided to the public domain
9 * Version 1.4 11 December 2005 Mark Adler
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 static inline size_t special_min(size_t a, size_t b)
40 return a == 0 ? b : (a < b ? a : b);
43 /* Decompress from file source to file dest until stream ends or EOF.
44 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
45 allocated for processing, Z_DATA_ERROR if the deflate data is
46 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
47 the version of the library linked do not match, or Z_ERRNO if there
48 is an error reading or writing the files. */
49 static int inf(FILE *source, FILE *dest, size_t limit, size_t skip)
54 unsigned char in[CHUNK];
55 unsigned char out[CHUNK];
57 /* allocate inflate state */
62 strm.next_in = Z_NULL;
63 ret = inflateInit(&strm);
67 /* decompress until deflate stream ends or end of file */
69 strm.avail_in = fread(in, 1, CHUNK, source);
71 (void)inflateEnd(&strm);
74 if (strm.avail_in == 0)
78 /* run inflate() on input until output buffer not full */
80 strm.avail_out = CHUNK;
82 ret = inflate(&strm, Z_NO_FLUSH);
83 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
86 ret = Z_DATA_ERROR; /* and fall through */
89 (void)inflateEnd(&strm);
92 have = special_min(limit, CHUNK - strm.avail_out) - skip;
93 if (fwrite(&out[skip], have, 1, dest) != 1 || ferror(dest)) {
94 (void)inflateEnd(&strm);
99 } while (strm.avail_out == 0 && limit > 0);
101 /* done when inflate() says it's done */
102 } while (ret != Z_STREAM_END && limit > 0);
104 /* clean up and return */
105 (void)inflateEnd(&strm);
106 return (limit == 0 ? Z_OK : (ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR));
109 /* report a zlib or i/o error */
110 static void zerr(int ret)
115 fputs("error reading stdin\n", stderr);
117 fputs("error writing stdout\n", stderr);
120 fputs("invalid compression level\n", stderr);
123 fputs("invalid or incomplete deflate data\n", stderr);
126 fputs("out of memory\n", stderr);
128 case Z_VERSION_ERROR:
129 fputs("zlib version mismatch!\n", stderr);
133 static unsigned int get_num(char *str)
135 if (!strncmp("0x", str, 2))
136 return strtoul(str+2, NULL, 16);
138 return strtoul(str, NULL, 10);
141 static void usage(void)
143 fprintf(stderr, "Usage: fritz_cal_extract [-s seek offset] [-i skip] [-o output file] [-l limit] [infile] -e entry_id\n"
144 "Finds and extracts zlib compressed calibration data in the EVA loader\n");
151 } __attribute__((packed));
153 /* compress or decompress from stdin to stdout */
154 int main(int argc, char **argv)
156 struct cal_entry cal = { .len = 0 };
159 size_t limit = 0, skip = 0;
160 int initial_offset = 0;
165 while ((opt = getopt(argc, argv, "s:e:o:l:i:")) != -1) {
168 initial_offset = (int)get_num(optarg);
170 perror("Failed to parse seek offset");
175 entry = (int) htobe16(get_num(optarg));
177 perror("Failed to entry id");
182 out = fopen(optarg, "w");
184 perror("Failed to create output file");
189 limit = (size_t)get_num(optarg);
191 perror("Failed to parse limit");
196 skip = (size_t)get_num(optarg);
198 perror("Failed to parse skip");
210 if (argc > 1 && optind <= argc) {
211 in = fopen(argv[optind], "r");
213 perror("Failed to create output file");
218 if (initial_offset) {
219 ret = fseek(in, initial_offset, SEEK_CUR);
221 perror("Failed to seek to calibration table");
227 ret = fseek(in, be16toh(cal.len), SEEK_CUR);
229 fprintf(stderr, "Reached end of file, but didn't find the matching entry\n");
231 } else if (ferror(in)) {
232 perror("Failure during seek");
236 ret = fread(&cal, 1, sizeof cal, in);
237 if (ret != sizeof cal)
239 } while (entry != cal.id || cal.id == 0xffff);
241 if (cal.id == 0xffff) {
242 fprintf(stderr, "Reached end of filesystem, but didn't find the matching entry\n");
246 ret = inf(in, out, limit, skip);