]>
cvs.zerfleddert.de Git - rsbs2/blob - extract.c
15 struct file_entry
* get_next_file(unsigned char *fw
, int len
)
17 static unsigned char *pos
;
18 static unsigned char *end
;
19 static struct file_entry fent
;
22 if (fw
!= NULL
&& len
!= 0) {
25 printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos
));
26 pos
= fw
+ *((unsigned int*)pos
);
33 name_length
= *((unsigned int*)pos
);
36 fent
.length
= *((unsigned int*)pos
);
39 if ((fent
.length
> (end
- pos
)) ||
40 (name_length
> (end
- pos
))) {
41 printf("EOF reached\n");
45 fent
.name
= (char*)pos
;
54 void extract_files(unsigned char *fw
, int len
)
56 struct file_entry
*fent
;
58 fent
= get_next_file(fw
, len
);
61 printf("%s: unknown: 0x%02x, length: %d",
62 fent
->name
, fent
->unknown
, fent
->length
);
64 if (fent
->length
> 0) {
65 write_file(fent
->name
, fent
->start
, fent
->length
);
66 if (*((unsigned int*)fent
->start
) == LZ_MAGIC
) {
69 if ((lzname
= strdup(fent
->name
)) == NULL
) {
74 if (!strcmp(lzname
+ strlen(lzname
) - 4 , ".lz")) {
75 fprintf(stderr
, "Ugh, can't derive filename...\n");
78 lzname
[strlen(lzname
) - 3] = 0x00;
80 printf("%s: packed file found", lzname
);
82 extract_lz_file(fent
->start
, (unsigned char*)lzname
);
84 } else if (!strcmp(fent
->name
, "firmware")) {
88 bzero(lzname
, sizeof(lzname
));
89 strcpy(lzname
, "firmware.");
91 lzpos
= fent
->start
+ *((unsigned int*)(fent
->start
+ 0x20));
92 memcpy(lzname
+ strlen(lzname
), lzpos
- 4, 4);
94 if (*((unsigned int*)(lzpos
)) == LZ_MAGIC
) {
95 printf("%s: compressed firmware part found", lzname
);
96 extract_lz_file(lzpos
, (unsigned char*)lzname
);
100 fent
= get_next_file(NULL
, 0);
104 void mkdir_p(char *dir
)
108 if ((dir
== NULL
) || (!strcmp(dir
, ".")))
111 if ((copy
= strdup(dir
)) == NULL
) {
115 parent
= dirname(copy
);
119 if (mkdir(dir
, 0755) == -1) {
120 if (errno
!= EEXIST
) {
121 fprintf(stderr
, "%s: ", dir
);
129 void write_file(char *fname
, unsigned char *buf
, int len
)
131 char filename
[PATH_MAX
];
132 char *filename_c
, *dirn
;
137 strcpy(filename
, "extracted/");
138 strcat(filename
, fname
);
140 if ((filename_c
= strdup(filename
)) == NULL
) {
144 dirn
= dirname(filename_c
);
148 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
149 fprintf(stderr
, "%s: ", filename
);
157 ret
= write(fd
, buf
+ (len
- remaining
), remaining
);
165 printf(", %s written.\n", filename
);