+ unsigned int r7;
+ unsigned int r11;
+ unsigned char arr_59b64[2048];
+
+ r5 = 1;
+
+ while (1) {
+ while (1) {
+ r2 = fn_59848(r10_data);
+ if (r2 == 0)
+ break;
+
+ r2 = fn_598b4(r10_data, 8) & 0xff;
+ r4 = r2;
+
+ fn_5980c(r4, r13_mem);
+ arr_59b64[r5] = r4 & 0xff;
+ r2 = r5 + 1;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r5 = r2;
+ }
+
+ r11 = fn_598b4(r10_data, 0x0a);
+ if(r11 == 0)
+ return;
+
+ r2 = fn_598b4(r10_data, 0x04);
+ r7 = r2 + 1;
+ r6 = 0;
+ while (r6 <= r7) {
+ r2 = r6 + r11;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r4 = arr_59b64[r2];
+ fn_5980c(r4, r13_mem);
+ arr_59b64[r5] = r4;
+ r2 = r5 + 1;
+ r2 = r2 << 22;
+ r2 = r2 >> 22;
+ r5 = r2;
+ r6++;
+ }
+ }
+}
+
+unsigned int crc_check_59684(unsigned char *arg1, unsigned int arg2, unsigned int magic)
+{
+ unsigned int r3;
+ unsigned int r4;
+ unsigned int r5;
+
+#if 0
+ if (r0 < 0xc0000000)
+ 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;
+
+ if (arg2 >= r4)
+ r5 = 0;
+ else
+ return 3;
+
+ r5 = ~rsb_crc(~0x00, arg1, r4);
+ r3 = *((unsigned int*)(arg1 + r4));
+ printf("Checksums: 0x%02x <-> 0x%02x\n", r5, r3);
+
+ if (r3 == r5)
+ return 0;
+
+ return 4;
+}
+
+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)
+{
+ 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)
+{
+ unsigned char *r3;
+ unsigned int r5;