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;
}
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);
free(lzname);
memcpy(lzname + strlen(lzname), lzpos - 4, 4);
lzpos += 4;
if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
- printf("%s: compressed firmware part found", lzname);
+ printf("%s: compressed firmware part found, ", lzname);
extract_lz_file(lzpos, (unsigned char*)lzname, 1);
}
}
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);
}
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);
}
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;
+}
strcpy(newfile, file);
strcat(newfile, ".patched");
- printf("Writing %s\n", newfile);
- if ((fd = open(newfile, O_WRONLY|O_CREAT, 0644)) == -1) {
- fprintf(stderr,"%s: ", newfile);
- perror("open");
- exit(1);
- }
-
- remaining = statbuf.st_size;
-
- while(remaining) {
- if ((ret = write(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
- perror("write");
- exit(1);
- }
- remaining -= ret;
- }
- close(fd);
+ printf("Firmware ");
+ write_file(newfile, fw, statbuf.st_size);
} else {
fprintf(stderr,"Can't set correct checksum, aborting...\n");
}