]> cvs.zerfleddert.de Git - rsbs2/blobdiff - extract.c
add functions to reliably read files in the filesystem, to be used by the
[rsbs2] / extract.c
index 6c30bd22eace6c727bc0e6a154b28be2a6aecb1c..86491d1cfcf47541f5dc15a255832e6c8d030f77 100644 (file)
--- a/extract.c
+++ b/extract.c
 #include "rsb-lz.h"
 #include "extract.h"
 
+struct file_entry* get_next_file(unsigned char *fw, int len)
+{
+       static unsigned char *pos;
+       static unsigned char *end;
+       static struct file_entry fent;
+       int name_length;
+
+       if (fw != NULL && len != 0) {
+               pos = fw + 0x28;
+
+               printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos));
+               pos = fw + *((unsigned int*)pos);
+               end = fw + len;
+       }
+
+       fent.unknown = *pos;
+       pos++;
+
+       name_length = *((unsigned int*)pos);
+       pos += 4;
+
+       fent.length = *((unsigned int*)pos);
+       pos += 4;
+
+       if ((fent.length > (end - pos)) ||
+           (name_length > (end - pos))) {
+               printf("EOF reached\n");
+               return NULL;
+       }
+
+       fent.name = (char*)pos;
+       pos += name_length;
+
+       fent.start = pos;
+       pos += fent.length;
+
+       return &fent;
+}
+
 void extract_files(unsigned char *fw, int len)
 {
-       unsigned char *pos;
-       unsigned int length;
-       unsigned char unknown[5];
-       char *name;
-
-       pos = fw + 0x28;
-       printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos));
-
-       pos = fw + *((unsigned int*)pos);
-
-       while (pos < (fw + len)) {
-               unknown[0] = *pos; pos++;
-               unknown[1] = *pos; pos++;
-               unknown[2] = *pos; pos++;
-               unknown[3] = *pos; pos++;
-               unknown[4] = *pos; pos++;
-               length = *((unsigned int*)pos);
-               pos += 4;
-               name = (char*)pos;
-               pos += strlen(name) + 1;
-
-               if ((pos + length) > (fw + len)) {
-                       printf("EOF reached\n");
-                       break;
-               }
-               printf("%s: unknown: %02x %02x %02x %02x %02x, length: %d",
-                       name, unknown[0], unknown[1], unknown[2], unknown[3],
-                       unknown[4], length);
+       struct file_entry *fent;
+
+       fent = get_next_file(fw, len);
+       
+       while (fent) {
+               printf("%s: unknown: 0x%02x, length: %d",
+                       fent->name, fent->unknown, fent->length);
 
-               if (length > 0) {
-                       write_file(name, pos, length);
-                       if (*((unsigned int*)pos) == LZ_MAGIC) {
+               if (fent->length > 0) {
+                       write_file(fent->name, fent->start, fent->length);
+                       if (*((unsigned int*)fent->start) == LZ_MAGIC) {
                                char *lzname;
 
-                               if ((lzname = strdup(name)) == NULL) {
+                               if ((lzname = strdup(fent->name)) == NULL) {
                                        perror("strdup");
                                        exit(1);
                                }
@@ -61,16 +79,16 @@ void extract_files(unsigned char *fw, int len)
 
                                printf("%s: packed file found", lzname);
 
-                               extract_lz_file(pos, (unsigned char*)lzname);
+                               extract_lz_file(fent->start, (unsigned char*)lzname);
                                free(lzname);
-                       } else if (!strcmp(name, "firmware")) {
+                       } else if (!strcmp(fent->name, "firmware")) {
                                unsigned char *lzpos;
                                char lzname[128];
 
                                bzero(lzname, sizeof(lzname));
                                strcpy(lzname, "firmware.");
 
-                               lzpos = pos + *((unsigned int*)(pos + 0x20));
+                               lzpos = fent->start + *((unsigned int*)(fent->start + 0x20));
                                memcpy(lzname + strlen(lzname), lzpos - 4, 4);
                                lzpos += 4;
                                if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
@@ -79,9 +97,7 @@ void extract_files(unsigned char *fw, int len)
                                }
                        }
                }
-
-               pos += length;
-
+               fent = get_next_file(NULL, 0);
        }
 }
 
Impressum, Datenschutz