]> cvs.zerfleddert.de Git - rsbs2/blob - extract.c
don't ever update the checksum of a corrupt image
[rsbs2] / extract.c
1 #include <sys/stat.h>
2 #include <sys/types.h>
3 #include <limits.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <strings.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <libgen.h>
12 #include "rsb-lz.h"
13 #include "extract.h"
14
15 struct file_entry* get_next_file(unsigned char *fw, int len)
16 {
17 static unsigned char *pos;
18 static unsigned char *end;
19 static struct file_entry fent;
20 int name_length;
21
22 if (fw != NULL && len != 0) {
23 pos = fw + 0x28;
24
25 #if 0
26 printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos));
27 #endif
28 pos = fw + *((unsigned int*)pos);
29 end = fw + len;
30 }
31
32 fent.unknown = *pos;
33 pos++;
34
35 name_length = *((unsigned int*)pos);
36 pos += 4;
37
38 fent.length = *((unsigned int*)pos);
39 pos += 4;
40
41 if ((fent.length > (end - pos)) ||
42 (name_length > (end - pos))) {
43 #if 0
44 printf("EOF reached\n");
45 #endif
46 return NULL;
47 }
48
49 fent.name = (char*)pos;
50 pos += name_length;
51
52 fent.start = pos;
53 pos += fent.length;
54
55 return &fent;
56 }
57
58 void extract_files(unsigned char *fw, int len)
59 {
60 struct file_entry *fent;
61
62 fent = get_next_file(fw, len);
63
64 while (fent) {
65 printf("%s: unknown: 0x%02x, length: %d",
66 fent->name, fent->unknown, fent->length);
67
68 if (fent->length > 0) {
69 write_file(fent->name, fent->start, fent->length);
70 if (*((unsigned int*)fent->start) == LZ_MAGIC) {
71 char *lzname;
72
73 if ((lzname = strdup(fent->name)) == NULL) {
74 perror("strdup");
75 exit(1);
76 }
77
78 if (!strcmp(lzname + strlen(lzname) - 4 , ".lz")) {
79 fprintf(stderr, "Ugh, can't derive filename...\n");
80 exit(1);
81 }
82 lzname[strlen(lzname) - 3] = 0x00;
83
84 printf("%s: packed file found", lzname);
85
86 extract_lz_file(fent->start, (unsigned char*)lzname);
87 free(lzname);
88 } else if (!strcmp(fent->name, "firmware")) {
89 unsigned char *lzpos;
90 char lzname[128];
91
92 bzero(lzname, sizeof(lzname));
93 strcpy(lzname, "firmware.");
94
95 lzpos = fent->start + *((unsigned int*)(fent->start + 0x20));
96 memcpy(lzname + strlen(lzname), lzpos - 4, 4);
97 lzpos += 4;
98 if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
99 printf("%s: compressed firmware part found", lzname);
100 extract_lz_file(lzpos, (unsigned char*)lzname);
101 }
102 }
103 }
104 fent = get_next_file(NULL, 0);
105 }
106 }
107
108 void mkdir_p(char *dir)
109 {
110 char *copy, *parent;
111
112 if ((dir == NULL) || (!strcmp(dir, ".")))
113 return;
114
115 if ((copy = strdup(dir)) == NULL) {
116 perror("strdup");
117 exit(1);
118 }
119 parent = dirname(copy);
120 mkdir_p(parent);
121
122 errno = 0;
123 if (mkdir(dir, 0755) == -1) {
124 if (errno != EEXIST) {
125 fprintf(stderr, "%s: ", dir);
126 perror("mkdir");
127 exit(1);
128 }
129 }
130 free(copy);
131 }
132
133 void write_file(char *fname, unsigned char *buf, int len)
134 {
135 char filename[PATH_MAX];
136 char *filename_c, *dirn;
137 int fd;
138 int remaining;
139 int ret;
140
141 strcpy(filename, "extracted/");
142 strcat(filename, fname);
143
144 if ((filename_c = strdup(filename)) == NULL) {
145 perror("strdup");
146 exit(1);
147 }
148 dirn = dirname(filename_c);
149 mkdir_p(dirn);
150 free(filename_c);
151
152 if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) {
153 fprintf(stderr, "%s: ", filename);
154 perror("open");
155 exit(1);
156 }
157
158 remaining = len;
159
160 while(remaining) {
161 ret = write(fd, buf + (len - remaining), remaining);
162 if (ret < 0) {
163 perror("write");
164 exit(1);
165 }
166 remaining -= ret;
167 }
168
169 printf(", %s written.\n", filename);
170
171 close(fd);
172 }
Impressum, Datenschutz