]>
git.zerfleddert.de Git - rsbs2/blob - extract.c
784821bf9ff856d8d20e8195cf4370f174654a01
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) {
26 printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos
));
28 pos
= fw
+ *((unsigned int*)pos
);
35 name_length
= *((unsigned int*)pos
);
38 fent
.length
= *((unsigned int*)pos
);
41 if ((fent
.length
> (end
- pos
)) ||
42 (name_length
> (end
- pos
))) {
44 printf("EOF reached\n");
49 fent
.name
= (char*)pos
;
58 void extract_files(unsigned char *fw
, int len
)
60 struct file_entry
*fent
;
62 fent
= get_next_file(fw
, len
);
65 printf("%s: unknown: 0x%02x, length: %d",
66 fent
->name
, fent
->unknown
, fent
->length
);
68 if (fent
->length
> 0) {
69 write_file(fent
->name
, fent
->start
, fent
->length
);
70 if (*((unsigned int*)fent
->start
) == LZ_MAGIC
) {
73 if ((lzname
= strdup(fent
->name
)) == NULL
) {
78 if (!strcmp(lzname
+ strlen(lzname
) - 4 , ".lz")) {
79 fprintf(stderr
, "Ugh, can't derive filename...\n");
82 lzname
[strlen(lzname
) - 3] = 0x00;
84 printf("%s: packed file found", lzname
);
86 extract_lz_file(fent
->start
, (unsigned char*)lzname
);
88 } else if (!strcmp(fent
->name
, "firmware")) {
92 bzero(lzname
, sizeof(lzname
));
93 strcpy(lzname
, "firmware.");
95 lzpos
= fent
->start
+ *((unsigned int*)(fent
->start
+ 0x20));
96 memcpy(lzname
+ strlen(lzname
), lzpos
- 4, 4);
98 if (*((unsigned int*)(lzpos
)) == LZ_MAGIC
) {
99 printf("%s: compressed firmware part found", lzname
);
100 extract_lz_file(lzpos
, (unsigned char*)lzname
);
104 fent
= get_next_file(NULL
, 0);
108 void mkdir_p(char *dir
)
112 if ((dir
== NULL
) || (!strcmp(dir
, ".")))
115 if ((copy
= strdup(dir
)) == NULL
) {
119 parent
= dirname(copy
);
123 if (mkdir(dir
, 0755) == -1) {
124 if (errno
!= EEXIST
) {
125 fprintf(stderr
, "%s: ", dir
);
133 void write_file(char *fname
, unsigned char *buf
, int len
)
135 char filename
[PATH_MAX
];
136 char *filename_c
, *dirn
;
141 strcpy(filename
, "extracted/");
142 strcat(filename
, fname
);
144 if ((filename_c
= strdup(filename
)) == NULL
) {
148 dirn
= dirname(filename_c
);
152 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
, 0644)) == -1) {
153 fprintf(stderr
, "%s: ", filename
);
161 ret
= write(fd
, buf
+ (len
- remaining
), remaining
);
169 printf(", %s written.\n", filename
);