write_file(extracted_file(fent->name), fent->start, fent->length);
                        if (*((unsigned int*)fent->start) == LZ_MAGIC) {
                                char *lzname;
+                               unsigned char *outbuf;
+                               unsigned int outlen;
 
                                if ((lzname = strdup(fent->name)) == NULL) {
                                        perror("strdup");
 
                                printf("%s: packed file found, ", lzname);
 
-                               extract_lz_file(fent->start, (unsigned char*)lzname, 0);
+                               outbuf = extract_lz_file(fent->start, &outlen, 0);
+                               write_file(extracted_file((char*)lzname), outbuf, outlen);
+
+                               free(outbuf);
                                free(lzname);
                        } else if (!strcmp(fent->name, "firmware")) {
                                unsigned char *lzpos;
                                memcpy(lzname + strlen(lzname), lzpos - 4, 4);
                                lzpos += 4;
                                if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
-                                       printf("%s: compressed firmware part found, ", lzname);
-                                       extract_lz_file(lzpos, (unsigned char*)lzname, 1);
+                                       unsigned char *outbuf;
+                                       unsigned int outlen;
+
+                                       printf("%s: compressed firmware part found", lzname);
+                                       outbuf = extract_lz_file(lzpos, &outlen, 1);
+                                       printf(", ");
+                                       write_file(extracted_file((char*)lzname), outbuf, outlen);
+
+                                       free(outbuf);
                                }
                        }
                } else {
 
        return 0;
 }
 
-void extract_lz_file(unsigned char *inbuf, unsigned char *name, unsigned char check_crc)
+unsigned char *extract_lz_file(unsigned char *inbuf, unsigned int *outlen , unsigned char check_crc)
 {
-       unsigned int len;
        unsigned char *outbuf;
        struct data_in_s data_in;
        struct data_out_s data_out;
        if (*((unsigned int*)inbuf) != LZ_MAGIC)
                err_exit(__func__);
 
-       len = *((unsigned int*)(inbuf + 4));
-       printf(", length: %d", len);
+       *outlen = *((unsigned int*)(inbuf + 4));
+       printf(", length: %d", *outlen);
 
-       if ((outbuf = malloc(len)) == NULL) {
+       if ((outbuf = malloc(*outlen)) == NULL) {
                perror("malloc");
                exit(1);
        }
 
-       bzero(outbuf, len);
+       bzero(outbuf, *outlen);
 
        data_in.start = inbuf + 8;
-       data_in.stop = inbuf + len;
+       data_in.stop = inbuf + *outlen;
        data_in.byte = 0x00;
        data_in.bitpos = 0x80;
 
        data_out.pos = outbuf;
-       data_out.end = outbuf + len;
+       data_out.end = outbuf + *outlen;
 
        lz_expand(&data_in, &data_out);
 
                        err_exit(__func__);
                }
        }
-       write_file(extracted_file((char*)name), outbuf, len);
-       
-       free(outbuf);
+
+       return outbuf;
 }
 
 #define LZ_MAGIC 0x6110beef
 
-void extract_lz_file(unsigned char *buf, unsigned char *name, unsigned char check_crc);
+unsigned char *extract_lz_file(unsigned char *inbuf, unsigned int *outlen , unsigned char check_crc);
 unsigned char *compress_lz(unsigned char *inbuf, int inlen, int *outlen);