X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/rsbs2/blobdiff_plain/a74204225a1e8d8e725c94ddaa53bbb900155d68..762f3898849f6975286d7d0511f9e340cc2eb073:/filesystem.c?ds=sidebyside diff --git a/filesystem.c b/filesystem.c index 175cbc4..d644679 100644 --- a/filesystem.c +++ b/filesystem.c @@ -86,13 +86,15 @@ void extract_files(unsigned char *fw, int len) fent = get_next_file(fw, len); while (fent) { - printf("%s: unknown: 0x%02x, length: %d", + printf("%s: unknown: 0x%02x, length: %d, ", fent->name, fent->unknown, fent->length); if (fent->length > 0) { - write_file(fent->name, fent->start, fent->length); + 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"); @@ -105,9 +107,12 @@ void extract_files(unsigned char *fw, int len) } lzname[strlen(lzname) - 3] = 0x00; - printf("%s: packed file found", lzname); + 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) { + unsigned char *outbuf; + unsigned int outlen; + printf("%s: compressed firmware part found", lzname); - extract_lz_file(lzpos, (unsigned char*)lzname, 1); + outbuf = extract_lz_file(lzpos, &outlen, 1); + printf(", "); + write_file(extracted_file((char*)lzname), outbuf, outlen); + + free(outbuf); } } } else { @@ -174,16 +186,12 @@ void mkdir_p(char *dir) void write_file(char *fname, unsigned char *buf, int len) { - char filename[PATH_MAX]; char *filename_c, *dirn; int fd; int remaining; int ret; - strcpy(filename, "extracted/"); - strcat(filename, fname); - - if ((filename_c = strdup(filename)) == NULL) { + if ((filename_c = strdup(fname)) == NULL) { perror("strdup"); exit(1); } @@ -191,8 +199,8 @@ void write_file(char *fname, unsigned char *buf, int len) mkdir_p(dirn); free(filename_c); - if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) { - fprintf(stderr, "%s: ", filename); + if ((fd = open(fname, O_WRONLY|O_CREAT, 0644)) == -1) { + fprintf(stderr, "%s: ", fname); perror("open"); exit(1); } @@ -208,7 +216,17 @@ void write_file(char *fname, unsigned char *buf, int len) remaining -= ret; } - printf(", %s written.\n", filename); + printf("%s written.\n", fname); close(fd); } + +char *extracted_file(char *fname) +{ + static char filename[PATH_MAX]; + + strcpy(filename, "extracted/"); + strcat(filename, fname); + + return filename; +}