]> git.zerfleddert.de Git - rsbs2/commitdiff
write extracted files to the filesystem.
authorMichael Gernoth <michael@gernoth.net>
Sat, 31 Jan 2009 11:16:57 +0000 (12:16 +0100)
committerMichael Gernoth <michael@gernoth.net>
Sat, 31 Jan 2009 11:16:57 +0000 (12:16 +0100)
checksumming of extracted content is still broken, so it's currently disabled

.gitignore
rsb-lz.c

index e888379a5cc60c6a186bb68da4546815e37de77d..5c01e42824f2043679aca36fc5ae49bae0571619 100644 (file)
@@ -2,3 +2,4 @@ firmware
 firmware.o
 rsb-crc.o
 rsb-lz.o
+extracted
index f5a84fa55832020cead7d92285c8e0502c12d46f..24977029e52f2cb64f00764f27202a732dd96399 100644 (file)
--- a/rsb-lz.c
+++ b/rsb-lz.c
@@ -1,8 +1,14 @@
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <limits.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include <string.h>
+#include <unistd.h>
 #include <errno.h>
+#include <libgen.h>
 #include "rsb-crc.h"
 #include "rsb-lz.h"
 
  * 59b80:      46335053        undefined
  */
 
-static unsigned char *outbuf = NULL;
-
 void fn_59788(const char *fname)
 {
        fprintf(stderr,"%s: error extracting...\n", fname);
-       fprintf(stderr,"outbuf at: %p\n", outbuf);
-       fprintf(stderr,"%s\n", outbuf);
        exit(1);
 }
 
@@ -613,9 +615,11 @@ unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned in
                return 1;
 #endif
 
+       /* ??? */
        r4 = *((unsigned int*)arg1 + 0x20);
        r5 = *((unsigned int*)arg1 + 0x24);
 
+       printf("magic: 0x%08x <-> 0x%08x\n", r5, magic);
        if (r5 != magic)
                return 2;
        
@@ -634,9 +638,70 @@ unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned in
        return 4;
 }
 
-void fn_59508()
+void mkdir_p(char *dir)
+{
+       char *copy, *parent;
+
+       if ((dir == NULL) || (!strcmp(dir, ".")))
+               return;
+
+       if ((copy = strdup(dir)) == NULL) {
+               perror("strdup");
+               exit(1);
+       }
+       parent = dirname(copy);
+       mkdir_p(parent);
+
+       errno = 0;
+       if (mkdir(dir, 0755) == -1) {
+               if (errno != EEXIST) {
+                       fprintf(stderr, "%s: ", dir);
+                       perror("mkdir");
+                       exit(1);
+               }
+       }
+       free(copy);
+}
+
+void write_file(char *fname, unsigned char *buf, int len)
 {
-       fprintf(stderr,"%s\n", __func__);
+       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) {
+               perror("strdup");
+               exit(1);
+       }
+       dirn = dirname(filename_c);
+       mkdir_p(dirn);
+       free(filename_c);
+
+       if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) {
+               fprintf(stderr, "%s: ", filename);
+               perror("open");
+               exit(1);
+       }
+
+       remaining = len;
+
+       while(remaining) {
+               ret = write(fd, buf + (len - remaining), remaining);
+               if (ret < 0) {
+                       perror("write");
+                       exit(1);
+               }
+               remaining -= ret;
+       }
+
+       printf(", %s written.\n", filename);
+
+       close(fd);
 }
 
 void extract_lz_file(unsigned char *buf, unsigned char *name)
@@ -649,17 +714,6 @@ void extract_lz_file(unsigned char *buf, unsigned char *name)
        struct s_59b78 struct1;
        unsigned int arr_59b7c[1024];
 
-       r7 = malloc(4*1024*1024);
-       r10 = r7 + (4*1024*1024);
-
-       if (r7 == NULL) {
-               perror("malloc");
-               exit(1);
-       }
-
-       bzero(r7, 4*1024*1024);
-       outbuf = r7;
-
        if (*((unsigned int*)r11) != LZ_MAGIC)
                fn_59788(__func__);
 
@@ -667,6 +721,13 @@ void extract_lz_file(unsigned char *buf, unsigned char *name)
        r5 = *((unsigned int*)r3);
        printf(", length: %d\n", r5);
 
+       if ((r7 = malloc(r5)) == NULL) {
+               perror("malloc");
+               exit(1);
+       }
+       r10 = r7 + r5;
+       bzero(r7, r5);
+
        r3 = r7 + r5;
        if (r3 > r10)
                fn_59788(__func__);
@@ -681,14 +742,19 @@ void extract_lz_file(unsigned char *buf, unsigned char *name)
 
        fn_5993c(&struct1, arr_59b7c);
 
+#if 0
+       /* This seems to still be completely broken */
        r3 = r7 + 0x20;
        r5 = *((unsigned int*)r3);
 
-       if (crc_check_59684(r7, r5, 0x46335053) != 0)
+       if ((ret = crc_check_59684(r7, r5, 0x46335053)) != 0) {
+               printf("crc_check return: %d\n", ret);
                fn_59788(__func__);
-       
-       fn_59508();
+       }
+#endif
 
+       write_file((char*)name, r7, r5);
+       
        free(r7);
 }
 
@@ -706,15 +772,16 @@ void search_lz_sections(unsigned char *fw, int len)
                        j--;
                        while (j > fw) {
                                if (*j == 0x00) {
-                                       if (strncmp("/web", (char*)(j+1), 4))
+                                       if ( *(j+1) != '/') {
+                                               printf("ignoring...\n");
                                                break;
+                                       }
                                        printf("%s", j+1);
                                        extract_lz_file(fw + i, j+1);
                                        break;
                                }
                                j--;
                        }
-                       printf("\n");
                }
        }
 }
Impressum, Datenschutz