From e726b380eaf48b6af8b06fe28383a183f35ceace Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Thu, 5 Feb 2009 08:07:28 +0100 Subject: [PATCH 1/1] fileaccess cleanups --- filesystem.c | 18 +++++++++++++++--- rsb-lz.c | 20 +++++++++----------- rsb-lz.h | 2 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/filesystem.c b/filesystem.c index d4b207e..d644679 100644 --- a/filesystem.c +++ b/filesystem.c @@ -93,6 +93,8 @@ void extract_files(unsigned char *fw, int len) write_file(extracted_file(fent->name), fent->start, fent->length); if (*((unsigned int*)fent->start) == LZ_MAGIC) { char *lzname; + unsigned char *outbuf; + unsigned int outlen; if ((lzname = strdup(fent->name)) == NULL) { perror("strdup"); @@ -107,7 +109,10 @@ void extract_files(unsigned char *fw, int len) printf("%s: packed file found, ", lzname); - extract_lz_file(fent->start, (unsigned char*)lzname, 0); + outbuf = extract_lz_file(fent->start, &outlen, 0); + write_file(extracted_file((char*)lzname), outbuf, outlen); + + free(outbuf); free(lzname); } else if (!strcmp(fent->name, "firmware")) { unsigned char *lzpos; @@ -120,8 +125,15 @@ void extract_files(unsigned char *fw, int len) memcpy(lzname + strlen(lzname), lzpos - 4, 4); lzpos += 4; if (*((unsigned int*)(lzpos)) == LZ_MAGIC) { - printf("%s: compressed firmware part found, ", lzname); - extract_lz_file(lzpos, (unsigned char*)lzname, 1); + unsigned char *outbuf; + unsigned int outlen; + + printf("%s: compressed firmware part found", lzname); + outbuf = extract_lz_file(lzpos, &outlen, 1); + printf(", "); + write_file(extracted_file((char*)lzname), outbuf, outlen); + + free(outbuf); } } } else { diff --git a/rsb-lz.c b/rsb-lz.c index c771b89..bb735d6 100644 --- a/rsb-lz.c +++ b/rsb-lz.c @@ -271,9 +271,8 @@ unsigned int crc_check(unsigned char *buf, unsigned int len, unsigned int magic) return 0; } -void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char check_crc) +unsigned char *extract_lz_file(unsigned char *inbuf, unsigned int *outlen , unsigned char check_crc) { - unsigned int len; unsigned char *outbuf; struct data_in_s data_in; struct data_out_s data_out; @@ -281,23 +280,23 @@ void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char ch if (*((unsigned int*)inbuf) != LZ_MAGIC) err_exit(__func__); - len = *((unsigned int*)(inbuf + 4)); - printf(", length: %d", len); + *outlen = *((unsigned int*)(inbuf + 4)); + printf(", length: %d", *outlen); - if ((outbuf = malloc(len)) == NULL) { + if ((outbuf = malloc(*outlen)) == NULL) { perror("malloc"); exit(1); } - bzero(outbuf, len); + bzero(outbuf, *outlen); data_in.start = inbuf + 8; - data_in.stop = inbuf + len; + data_in.stop = inbuf + *outlen; data_in.byte = 0x00; data_in.bitpos = 0x80; data_out.pos = outbuf; - data_out.end = outbuf + len; + data_out.end = outbuf + *outlen; lz_expand(&data_in, &data_out); @@ -312,7 +311,6 @@ void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char ch err_exit(__func__); } } - write_file(extracted_file((char*)name), outbuf, len); - - free(outbuf); + + return outbuf; } diff --git a/rsb-lz.h b/rsb-lz.h index 442ec43..4cf2c82 100644 --- a/rsb-lz.h +++ b/rsb-lz.h @@ -1,4 +1,4 @@ #define LZ_MAGIC 0x6110beef -void extract_lz_file(unsigned char *buf, unsigned char *name, unsigned char check_crc); +unsigned char *extract_lz_file(unsigned char *inbuf, unsigned int *outlen , unsigned char check_crc); unsigned char *compress_lz(unsigned char *inbuf, int inlen, int *outlen); -- 2.39.5