Skip to content

Commit

Permalink
Modified version of fritz_cal_extract.c to handle reversed calibration
Browse files Browse the repository at this point in the history
data.

New options:
-t Number of bytes to truncate (before optional reversal of data).
-r Re-reverse calibration data. This is necessary for some AVM devices
   (e.g. Fritzbox 4040, 7430, ...).
  • Loading branch information
dzzinstant committed May 4, 2024
1 parent ccfadb4 commit 01c1895
Showing 1 changed file with 97 additions and 42 deletions.
139 changes: 97 additions & 42 deletions package/utils/fritz-tools/src/fritz_cal_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* that is Not copyrighted -- provided to the public domain
* Version 1.4 11 December 2005 Mark Adler
*
* Modifications to also handle calibration data in reversed byte order
* and truncation of data (c) 2024 by <dzsoftware@posteo.org>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
Expand All @@ -28,32 +31,41 @@
#include <assert.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <endian.h>
#include <errno.h>
#include "zlib.h"

#define CHUNK 1024
#define DEFAULT_BUFFERSIZE (1024 * 64)

static inline size_t special_min(size_t a, size_t b)
/* Reverse only buffer elements inside range [bottom:top] */
static void buffer_reverse(unsigned char *data, unsigned int bottom, unsigned int top)
{
return a == 0 ? b : (a < b ? a : b);
register unsigned char swapbyte;
unsigned int center = bottom + (top - bottom) / 2;

for (; bottom < center; ++bottom, --top) {
swapbyte = data[bottom];
data[bottom] = data[top];
data[top] = swapbyte;
}
}

/* Decompress from file source to file dest until stream ends or EOF.
inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_DATA_ERROR if the deflate data is
invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
the version of the library linked do not match, or Z_ERRNO if there
is an error reading or writing the files. */
static int inf(FILE *source, FILE *dest, size_t limit, size_t skip)
/* Decompress from file source to data buffer until stream ends or
buffer is full.
inflate_to_buffer() returns Z_OK on success, Z_MEM_ERROR if memory
could not be allocated for processing, Z_DATA_ERROR if the deflate
data is invalid or incomplete, Z_VERSION_ERROR if the version of
zlib.h and the version of the library linked do not match, or
Z_ERRNO if there is an error reading or writing the files. */
static int inflate_to_buffer(FILE *source, unsigned char *buf, size_t *limit)
{
int ret;
size_t have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];


/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
Expand All @@ -64,6 +76,10 @@ static int inf(FILE *source, FILE *dest, size_t limit, size_t skip)
if (ret != Z_OK)
return ret;

/* set data buffer as stream output */
strm.avail_out = *limit;
strm.next_out = buf;

/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, 1, CHUNK, source);
Expand All @@ -75,35 +91,35 @@ static int inf(FILE *source, FILE *dest, size_t limit, size_t skip)
break;
strm.next_in = in;

/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
have = special_min(limit, CHUNK - strm.avail_out) - skip;
if (fwrite(&out[skip], have, 1, dest) != 1 || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
skip = 0;
limit -= have;
} while (strm.avail_out == 0 && limit > 0);
/* run inflate(), fill data buffer with all available output */
ret = inflate(&strm, Z_FINISH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */

switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}

if (strm.avail_out == 0 && ret != Z_STREAM_END) {
fprintf(stderr, "Failed to inflate input data: buffer too small!"
" Use option -l to increase caldata size limit.\n");
(void)inflateEnd(&strm);
return Z_ERRNO;
}
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END && limit > 0);
} while (ret != Z_STREAM_END);

/* set limit to end of retrieved data */
assert(strm.total_out <= *limit);
*limit = strm.total_out;

/* clean up and return */
(void)inflateEnd(&strm);
return (limit == 0 ? Z_OK : (ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR));
return (strm.avail_out == 0 ? Z_OK : (ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR));
}

/* report a zlib or i/o error */
Expand Down Expand Up @@ -140,7 +156,8 @@ static unsigned int get_num(char *str)

static void usage(void)
{
fprintf(stderr, "Usage: fritz_cal_extract [-s seek offset] [-i skip] [-o output file] [-l limit] [infile] -e entry_id\n"
fprintf(stderr, "Usage: fritz_cal_extract [-s seek offset] [-i skip] [-o output file] [-l limit]\n\t"
"[-t truncate n bytes] [-r reverse extracted data] [infile] -e entry_id\n"
"Finds and extracts zlib compressed calibration data in the EVA loader\n");
exit(EXIT_FAILURE);
}
Expand All @@ -154,15 +171,17 @@ struct cal_entry {
int main(int argc, char **argv)
{
struct cal_entry cal = { .len = 0 };
unsigned char *buf = NULL;
FILE *in = stdin;
FILE *out = stdout;
size_t limit = 0, skip = 0;
size_t limit = 0, skip = 0, truncate = 0;
int initial_offset = 0;
int entry = -1;
bool reversed = false;
int ret;
int opt;

while ((opt = getopt(argc, argv, "s:e:o:l:i:")) != -1) {
while ((opt = getopt(argc, argv, "s:e:o:l:i:t:r")) != -1) {
switch (opt) {
case 's':
initial_offset = (int)get_num(optarg);
Expand Down Expand Up @@ -199,6 +218,16 @@ int main(int argc, char **argv)
goto out_bad;
}
break;
case 't':
truncate = (size_t)get_num(optarg);
if (errno) {
perror("Failed to parse truncate");
goto out_bad;
}
break;
case 'r':
reversed = true;
break;
default: /* '?' */
usage();
}
Expand Down Expand Up @@ -243,9 +272,33 @@ int main(int argc, char **argv)
goto out_bad;
}

ret = inf(in, out, limit, skip);
if (ret == Z_OK)
goto out;
/* Create data buffer.
* 'limit' will be one-off the last valid buffer element. */
limit = (limit ? limit : DEFAULT_BUFFERSIZE) + skip;
buf = malloc(limit);
assert(buf != NULL);

ret = inflate_to_buffer(in, buf, &limit);
if (ret != Z_OK) {
zerr(ret);
goto out_bad;
}

limit -= truncate;
if (limit <= skip) {
fprintf(stderr, "Failed: Resulting data range [%ld:%ld] is invalid!\n",
skip, limit - 1);
goto out_bad;
}

if (reversed)
buffer_reverse(buf, skip, limit - 1);

if (fwrite(&buf[skip], (limit - skip), 1, out) != 1 || ferror(out)) {
fprintf(stderr, "Failed to write data buffer to output file");
goto out_bad;
}
goto out;

zerr(ret);

Expand All @@ -257,5 +310,7 @@ int main(int argc, char **argv)
fclose(in);
if (out)
fclose(out);
free(buf);

return ret;
}

0 comments on commit 01c1895

Please sign in to comment.