03a63bdd958b7b2c872b99dce85491d9442f1313
[oweals/opkg-lede.git] / libbb / unarchive.c
1 /*
2  *  Copyright (C) 2000 by Glenn McGrath
3  *  Copyright (C) 2001 by Laurence Anderson
4  *      
5  *  Based on previous work by busybox developers and others.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Library General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <utime.h>
28 #include <libgen.h>
29
30 #include "libbb.h"
31
32 #define CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 1
33 #define CONFIG_FEATURE_TAR_GNU_EXTENSIONS
34
35 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
36 static char *longname = NULL;
37 static char *linkname = NULL;
38 #endif
39
40 off_t archive_offset;
41
42 #define SEEK_BUF 4096
43 static ssize_t
44 seek_by_read(FILE* fd, size_t len)
45 {
46         ssize_t cc, total = 0;
47         char buf[SEEK_BUF];
48
49         while (len) {
50                 cc = fread(buf, sizeof(buf[0]), 
51                                 len > SEEK_BUF ? SEEK_BUF : len, 
52                                 fd);
53
54                 total += cc;
55                 len -= cc;
56
57                 if(feof(fd) || ferror(fd))
58                         break;
59         }
60         return total;
61 }
62
63 static void
64 seek_sub_file(FILE *src_stream, const int count)
65 {
66         /* Try to fseek as faster */
67         archive_offset += count;
68         seek_by_read(src_stream, count);
69         return;
70 }
71
72
73 /* Extract the data postioned at src_stream to either filesystem, stdout or 
74  * buffer depending on the value of 'function' which is defined in libbb.h 
75  *
76  * prefix doesnt have to be just a directory, it may prefix the filename as well.
77  *
78  * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath 
79  * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have 
80  * 'dpkg.' as their prefix
81  *
82  * For this reason if prefix does point to a dir then it must end with a
83  * trailing '/' or else the last dir will be assumed to be the file prefix 
84  */
85 static char *
86 extract_archive(FILE *src_stream, FILE *out_stream,
87                 const file_header_t *file_entry, const int function,
88                 const char *prefix,
89                 int *err)
90 {
91         FILE *dst_stream = NULL;
92         char *full_name = NULL;
93         char *full_link_name = NULL;
94         char *buffer = NULL;
95         struct utimbuf t;
96
97         *err = 0;
98
99         /* prefix doesnt have to be a proper path it may prepend 
100          * the filename as well */
101         if (prefix != NULL) {
102                 /* strip leading '/' in filename to extract as prefix may not be dir */
103                 /* Cant use concat_path_file here as prefix might not be a directory */
104                 char *path = file_entry->name;
105                 if (strncmp("./", path, 2) == 0) {
106                         path += 2;
107                         if (strlen(path) == 0)
108                                 /* Do nothing, current dir already exists. */
109                                 return NULL;
110                 }
111                 full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
112                 strcpy(full_name, prefix);
113                 strcat(full_name, path);
114                 if ( file_entry->link_name ){
115                    full_link_name = xmalloc(strlen(prefix) + strlen(file_entry->link_name) + 1);
116                    strcpy(full_link_name, prefix);
117                    strcat(full_link_name, file_entry->link_name);
118                 }
119         } else {
120                 full_name = xstrdup(file_entry->name);
121                 if ( file_entry->link_name )
122                    full_link_name = xstrdup(file_entry->link_name);
123         }
124
125
126         if (function & extract_to_stream) {
127                 if (S_ISREG(file_entry->mode)) {
128                         *err = copy_file_chunk(src_stream, out_stream, file_entry->size);                       
129                         archive_offset += file_entry->size;
130                 }
131         }
132         else if (function & extract_one_to_buffer) { 
133                 if (S_ISREG(file_entry->mode)) {
134                         buffer = (char *) xmalloc(file_entry->size + 1);
135                         fread(buffer, 1, file_entry->size, src_stream);
136                         buffer[file_entry->size] = '\0';
137                         archive_offset += file_entry->size;
138                         goto cleanup;
139                 }
140         }
141         else if (function & extract_all_to_fs) {
142                 struct stat oldfile;
143                 int stat_res;
144                 stat_res = lstat (full_name, &oldfile);
145                 if (stat_res == 0) { /* The file already exists */
146                         if ((function & extract_unconditional) || (oldfile.st_mtime < file_entry->mtime)) {
147                                 if (!S_ISDIR(oldfile.st_mode)) {
148                                         unlink(full_name); /* Directories might not be empty etc */
149                                 }
150                         } else {
151                                 if ((function & extract_quiet) != extract_quiet) {
152                                         *err = -1;
153                                         error_msg("%s not created: newer or same age file exists", file_entry->name);
154                                 }
155                                 seek_sub_file(src_stream, file_entry->size);
156                                 goto cleanup;
157                         }
158                 }
159                 if (function & extract_create_leading_dirs) { /* Create leading directories with default umask */
160                         char *buf, *parent;
161                         buf = xstrdup(full_name);
162                         parent = dirname(buf);
163                         if (make_directory (parent, -1, FILEUTILS_RECUR) != 0) {
164                                 if ((function & extract_quiet) != extract_quiet) {
165                                         *err = -1;
166                                         error_msg("couldn't create leading directories");
167                                 }
168                         }
169                         free (buf);
170                 }
171                 switch(file_entry->mode & S_IFMT) {
172                         case S_IFREG:
173                                 if (file_entry->link_name) { /* Found a cpio hard link */
174                                         if (link(full_link_name, full_name) != 0) {
175                                                 if ((function & extract_quiet) != extract_quiet) {
176                                                         *err = -1;
177                                                         perror_msg("Cannot link from %s to '%s'",
178                                                                 file_entry->name, file_entry->link_name);
179                                                 }
180                                         }
181                                 } else {
182                                         if ((dst_stream = wfopen(full_name, "w")) == NULL) {
183                                                 *err = -1;
184                                                 seek_sub_file(src_stream, file_entry->size);
185                                                 goto cleanup;
186                                         }
187                                         archive_offset += file_entry->size;
188                                         *err = copy_file_chunk(src_stream, dst_stream, file_entry->size);                       
189                                         fclose(dst_stream);
190                                 }
191                                 break;
192                         case S_IFDIR:
193                                 if (stat_res != 0) {
194                                         if (mkdir(full_name, file_entry->mode) < 0) {
195                                                 if ((function & extract_quiet) != extract_quiet) {
196                                                         *err = -1;
197                                                         perror_msg("Cannot make dir %s", full_name);
198                                                 }
199                                         }
200                                 }
201                                 break;
202                         case S_IFLNK:
203                                 if (symlink(file_entry->link_name, full_name) < 0) {
204                                         if ((function & extract_quiet) != extract_quiet) {
205                                                 *err = -1;
206                                                 perror_msg("Cannot create symlink from %s to '%s'", file_entry->name, file_entry->link_name);
207                                         }
208                                         goto cleanup;
209                                 }
210                                 break;
211                         case S_IFSOCK:
212                         case S_IFBLK:
213                         case S_IFCHR:
214                         case S_IFIFO:
215                                 if (mknod(full_name, file_entry->mode, file_entry->device) == -1) {
216                                         if ((function & extract_quiet) != extract_quiet) {
217                                                 *err = -1;
218                                                 perror_msg("Cannot create node %s", file_entry->name);
219                                         }
220                                         goto cleanup;
221                                 }
222                                 break;
223                          default:
224                                 *err = -1;
225                                 perror_msg("Don't know how to handle %s", full_name);
226
227                 }
228
229                 /* Changing a symlink's properties normally changes the properties of the 
230                  * file pointed to, so dont try and change the date or mode, lchown does
231                  * does the right thing, but isnt available in older versions of libc */
232                 if (S_ISLNK(file_entry->mode)) {
233 #if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
234                         lchown(full_name, file_entry->uid, file_entry->gid);
235 #endif
236                 } else {
237                         if (function & extract_preserve_date) {
238                                 t.actime = file_entry->mtime;
239                                 t.modtime = file_entry->mtime;
240                                 utime(full_name, &t);
241                         }
242                         chown(full_name, file_entry->uid, file_entry->gid);
243                         chmod(full_name, file_entry->mode);
244                 }
245         } else {
246                 /* If we arent extracting data we have to skip it, 
247                  * if data size is 0 then then just do it anyway
248                  * (saves testing for it) */
249                 seek_sub_file(src_stream, file_entry->size);
250         }
251
252         /* extract_list and extract_verbose_list can be used in conjunction
253          * with one of the above four extraction functions, so do this seperately */
254         if (function & extract_verbose_list) {
255                 fprintf(out_stream, "%s %d/%d %8d %s ", mode_string(file_entry->mode), 
256                         file_entry->uid, file_entry->gid,
257                         (int) file_entry->size, time_string(file_entry->mtime));
258         }
259         if ((function & extract_list) || (function & extract_verbose_list)){
260                 /* fputs doesnt add a trailing \n, so use fprintf */
261                 fprintf(out_stream, "%s\n", file_entry->name);
262         }
263
264 cleanup:
265         free(full_name);
266         if ( full_link_name )
267             free(full_link_name);
268
269         return buffer;
270 }
271
272 static char *
273 unarchive(FILE *src_stream, FILE *out_stream,
274                 file_header_t *(*get_headers)(FILE *),
275                 void (*free_headers)(file_header_t *),
276                 const int extract_function,
277                 const char *prefix,
278                 const char **extract_names,
279                 int *err)
280 {
281         file_header_t *file_entry;
282         int extract_flag;
283         int i;
284         char *buffer = NULL;
285
286         *err = 0;
287
288         archive_offset = 0;
289         while ((file_entry = get_headers(src_stream)) != NULL) {
290                 extract_flag = TRUE;
291
292                 if (extract_names != NULL) {
293                         int found_flag = FALSE;
294                         for(i = 0; extract_names[i] != 0; i++) {
295                                 if (strcmp(extract_names[i], file_entry->name) == 0) {
296                                         found_flag = TRUE;
297                                         break;
298                                 }
299                         }
300                         if (extract_function & extract_exclude_list) {
301                                 if (found_flag == TRUE) {
302                                         extract_flag = FALSE;
303                                 }
304                         } else {
305                                 /* If its not found in the include list dont extract it */
306                                 if (found_flag == FALSE) {
307                                         extract_flag = FALSE;
308                                 }
309                         }
310                 }
311
312                 if (extract_flag == TRUE) {
313                         buffer = extract_archive(src_stream, out_stream,
314                                         file_entry, extract_function,
315                                         prefix, err);
316                         *err = 0; /* XXX: ignore extraction errors */
317                         if (*err) {
318                                 free_headers(file_entry);
319                                 break;
320                         }
321                 } else {
322                         /* seek past the data entry */
323                         seek_sub_file(src_stream, file_entry->size);
324                 }
325                 free_headers(file_entry);
326         }
327
328         return buffer;
329 }
330
331 static file_header_t *
332 get_header_ar(FILE *src_stream)
333 {
334         file_header_t *typed;
335         union {
336                 char raw[60];
337                 struct {
338                         char name[16];
339                         char date[12];
340                         char uid[6];
341                         char gid[6];
342                         char mode[8];
343                         char size[10];
344                         char magic[2];
345                 } formated;
346         } ar;
347         static char *ar_long_names;
348
349         if (fread(ar.raw, 1, 60, src_stream) != 60) {
350                 return(NULL);
351         }
352         archive_offset += 60;
353         /* align the headers based on the header magic */
354         if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
355                 /* some version of ar, have an extra '\n' after each data entry,
356                  * this puts the next header out by 1 */
357                 if (ar.formated.magic[1] != '`') {
358                         error_msg("Invalid magic");
359                         return(NULL);
360                 }
361                 /* read the next char out of what would be the data section,
362                  * if its a '\n' then it is a valid header offset by 1*/
363                 archive_offset++;
364                 if (fgetc(src_stream) != '\n') {
365                         error_msg("Invalid magic");
366                         return(NULL);
367                 }
368                 /* fix up the header, we started reading 1 byte too early */
369                 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
370                 memmove(ar.raw, &ar.raw[1], 59);
371         }
372                 
373         typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
374
375         typed->size = (size_t) atoi(ar.formated.size);
376         /* long filenames have '/' as the first character */
377         if (ar.formated.name[0] == '/') {
378                 if (ar.formated.name[1] == '/') {
379                         /* If the second char is a '/' then this entries data section
380                          * stores long filename for multiple entries, they are stored
381                          * in static variable long_names for use in future entries */
382                         ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
383                         fread(ar_long_names, 1, typed->size, src_stream);
384                         archive_offset += typed->size;
385                         /* This ar entries data section only contained filenames for other records
386                          * they are stored in the static ar_long_names for future reference */
387                         return (get_header_ar(src_stream)); /* Return next header */
388                 } else if (ar.formated.name[1] == ' ') {
389                         /* This is the index of symbols in the file for compilers */
390                         seek_sub_file(src_stream, typed->size);
391                         return (get_header_ar(src_stream)); /* Return next header */
392                 } else {
393                         /* The number after the '/' indicates the offset in the ar data section
394                         (saved in variable long_name) that conatains the real filename */
395                         if (!ar_long_names) {
396                                 error_msg("Cannot resolve long file name");
397                                 return (NULL);
398                         }
399                         typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
400                 }
401         } else {
402                 /* short filenames */
403                 typed->name = xcalloc(1, 16);
404                 strncpy(typed->name, ar.formated.name, 16);
405         }
406         typed->name[strcspn(typed->name, " /")]='\0';
407
408         /* convert the rest of the now valid char header to its typed struct */ 
409         parse_mode(ar.formated.mode, &typed->mode);
410         typed->mtime = atoi(ar.formated.date);
411         typed->uid = atoi(ar.formated.uid);
412         typed->gid = atoi(ar.formated.gid);
413
414         return(typed);
415 }
416
417 static void
418 free_header_ar(file_header_t *ar_entry)
419 {
420         if (ar_entry == NULL)
421                 return;
422
423         free(ar_entry->name);
424         if (ar_entry->link_name)
425                 free(ar_entry->link_name);
426
427         free(ar_entry);
428 }
429
430
431 static file_header_t *
432 get_header_tar(FILE *tar_stream)
433 {
434         union {
435                 unsigned char raw[512];
436                 struct {
437                         char name[100];         /*   0-99 */
438                         char mode[8];           /* 100-107 */
439                         char uid[8];            /* 108-115 */
440                         char gid[8];            /* 116-123 */
441                         char size[12];          /* 124-135 */
442                         char mtime[12];         /* 136-147 */
443                         char chksum[8];         /* 148-155 */
444                         char typeflag;          /* 156-156 */
445                         char linkname[100];     /* 157-256 */
446                         char magic[6];          /* 257-262 */
447                         char version[2];        /* 263-264 */
448                         char uname[32];         /* 265-296 */
449                         char gname[32];         /* 297-328 */
450                         char devmajor[8];       /* 329-336 */
451                         char devminor[8];       /* 337-344 */
452                         char prefix[155];       /* 345-499 */
453                         char padding[12];       /* 500-512 */
454                 } formated;
455         } tar;
456         file_header_t *tar_entry = NULL;
457         long i;
458         long sum = 0;
459
460         if (archive_offset % 512 != 0) {
461                 seek_sub_file(tar_stream, 512 - (archive_offset % 512));
462         }
463
464         if (fread(tar.raw, 1, 512, tar_stream) != 512) {
465                 /* Unfortunatly its common for tar files to have all sorts of
466                  * trailing garbage, fail silently */
467 //              error_msg("Couldnt read header");
468                 return(NULL);
469         }
470         archive_offset += 512;
471
472         /* Check header has valid magic, unfortunately some tar files
473          * have empty (0'ed) tar entries at the end, which will
474          * cause this to fail, so fail silently for now
475          */
476         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
477 #ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
478                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
479 #endif
480                 return(NULL);
481         }
482
483         /* Do checksum on headers */
484         for (i =  0; i < 148 ; i++) {
485                 sum += tar.raw[i];
486         }
487         sum += ' ' * 8;
488         for (i =  156; i < 512 ; i++) {
489                 sum += tar.raw[i];
490         }
491         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
492                 if ( strtol(tar.formated.chksum,NULL,8) != 0 )
493                         error_msg("Invalid tar header checksum");
494                 return(NULL);
495         }
496
497         /* convert to type'ed variables */
498         tar_entry = xcalloc(1, sizeof(file_header_t));
499
500 #ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
501         if (longname) {
502                 tar_entry->name = longname;
503                 longname = NULL;
504         }
505         else if (linkname) {
506                 tar_entry->name = linkname;
507                 linkname = NULL;
508         } else
509 #endif
510         {
511                 tar_entry->name = xstrndup(tar.formated.name, 100);
512
513                 if (tar.formated.prefix[0]) {
514                         char *temp = tar_entry->name;
515                         char *prefixTemp = xstrndup(tar.formated.prefix, 155);
516                         tar_entry->name = concat_path_file(prefixTemp, temp);
517                         free(temp);
518                         free(prefixTemp);
519                 }
520         }
521
522         // tar_entry->name = xstrdup(tar.formated.name);
523
524 /*
525         parse_mode(tar.formated.mode, &tar_entry->mode);
526 */
527         tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);
528
529         tar_entry->uid   = strtol(tar.formated.uid, NULL, 8);
530         tar_entry->gid   = strtol(tar.formated.gid, NULL, 8);
531         tar_entry->size  = strtol(tar.formated.size, NULL, 8);
532         tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);
533         tar_entry->link_name  = *tar.formated.linkname != '\0' ? xstrndup(tar.formated.linkname, 100) : NULL;
534         tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
535                 strtol(tar.formated.devminor, NULL, 8);
536
537         /* Fix mode, used by the old format */
538         switch (tar.formated.typeflag) {
539         /* hard links are detected as regular files with 0 size and a link name */
540         case '1':
541                 tar_entry->mode |= S_IFREG ;
542                 break;
543         case 0:
544         case '0':
545
546 # ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
547                 if (last_char_is(tar_entry->name, '/')) {
548                         tar_entry->mode |= S_IFDIR;
549                 } else 
550 # endif
551                         tar_entry->mode |= S_IFREG;
552                 break;
553         case '2':
554                 tar_entry->mode |= S_IFLNK;
555                 break;
556         case '3':
557                 tar_entry->mode |= S_IFCHR;
558                 break;
559         case '4':
560                 tar_entry->mode |= S_IFBLK;
561                 break;
562         case '5':
563                 tar_entry->mode |= S_IFDIR;
564                 break;
565         case '6':
566                 tar_entry->mode |= S_IFIFO;
567                 break;
568 # ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
569         case 'L': {
570                         longname = xmalloc(tar_entry->size + 1);
571                         if(fread(longname, tar_entry->size, 1, tar_stream) != 1)
572                                 return NULL;
573                         longname[tar_entry->size] = '\0';
574                         archive_offset += tar_entry->size;
575
576                         return(get_header_tar(tar_stream));
577                 }
578         case 'K': {
579                         linkname = xmalloc(tar_entry->size + 1);
580                         if(fread(linkname, tar_entry->size, 1, tar_stream) != 1)
581                                 return NULL;
582                         linkname[tar_entry->size] = '\0';
583                         archive_offset += tar_entry->size;
584
585                         tar_entry->name = linkname;
586                         return(get_header_tar(tar_stream));
587                 }
588         case 'D':
589         case 'M':
590         case 'N':
591         case 'S':
592         case 'V':
593                 perror_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
594 # endif
595         default:
596                 perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
597                 break;
598
599         }
600
601         return(tar_entry);
602 }
603
604 static void
605 free_header_tar(file_header_t *tar_entry)
606 {
607         if (tar_entry == NULL)
608                 return;
609
610         free(tar_entry->name);
611         if (tar_entry->link_name);
612                 free(tar_entry->link_name);
613
614         free(tar_entry);
615 }
616
617 char *
618 deb_extract(const char *package_filename, FILE *out_stream, 
619         const int extract_function, const char *prefix,
620         const char *filename, int *err)
621 {
622         FILE *deb_stream = NULL;
623         file_header_t *ar_header = NULL;
624         const char **file_list = NULL;
625         char *output_buffer = NULL;
626         char *ared_file = NULL;
627         char ar_magic[8];
628         int gz_err;
629
630         *err = 0;
631
632         if (filename != NULL) {
633                 file_list = xmalloc(sizeof(char *) * 2);
634                 file_list[0] = filename;
635                 file_list[1] = NULL;
636         }
637         
638         if (extract_function & extract_control_tar_gz) {
639                 ared_file = "control.tar.gz";
640         }
641         else if (extract_function & extract_data_tar_gz) {              
642                 ared_file = "data.tar.gz";
643         } else {
644                 opkg_msg(ERROR, "Internal error: extract_function=%x\n",
645                                 extract_function);
646                 *err = -1;
647                 goto cleanup;
648         }
649
650         /* open the debian package to be worked on */
651         deb_stream = wfopen(package_filename, "r");
652         if (deb_stream == NULL) {
653                 *err = -1;
654                 goto cleanup;
655         }
656         /* set the buffer size */
657         setvbuf(deb_stream, NULL, _IOFBF, 0x8000);
658
659         /* check ar magic */
660         fread(ar_magic, 1, 8, deb_stream);
661
662         if (strncmp(ar_magic,"!<arch>",7) == 0) {
663                 archive_offset = 8;
664
665                 while ((ar_header = get_header_ar(deb_stream)) != NULL) {
666                         if (strcmp(ared_file, ar_header->name) == 0) {
667                                 int gunzip_pid = 0;
668                                 FILE *uncompressed_stream;
669                                 /* open a stream of decompressed data */
670                                 uncompressed_stream = gz_open(deb_stream, &gunzip_pid);
671                                 if (uncompressed_stream == NULL) {
672                                         *err = -1;
673                                         goto cleanup;
674                                 }
675
676                                 archive_offset = 0;
677                                 output_buffer = unarchive(uncompressed_stream,
678                                                 out_stream, get_header_tar,
679                                                 free_header_tar,
680                                                 extract_function, prefix,
681                                                 file_list, err);
682                                 fclose(uncompressed_stream);
683                                 gz_err = gz_close(gunzip_pid);
684                                 if (gz_err)
685                                         *err = -1;
686                                 free_header_ar(ar_header);
687                                 break;
688                         }
689                         seek_sub_file(deb_stream, ar_header->size);
690                         free_header_ar(ar_header);
691                 }
692                 goto cleanup;
693         } else if (strncmp(ar_magic, "\037\213", 2) == 0) {
694                 /* it's a gz file, let's assume it's an opkg */
695                 int unzipped_opkg_pid;
696                 FILE *unzipped_opkg_stream;
697                 file_header_t *tar_header;
698                 archive_offset = 0;
699                 fseek(deb_stream, 0, SEEK_SET);
700                 unzipped_opkg_stream = gz_open(deb_stream, &unzipped_opkg_pid);
701                 if (unzipped_opkg_stream == NULL) {
702                         *err = -1;
703                         goto cleanup;
704                 }
705                 
706                 /* walk through outer tar file to find ared_file */
707                 while ((tar_header = get_header_tar(unzipped_opkg_stream)) != NULL) {
708                         int name_offset = 0;
709                         if (strncmp(tar_header->name, "./", 2) == 0)
710                                 name_offset = 2;
711                         if (strcmp(ared_file, tar_header->name+name_offset) == 0) {
712                                 int gunzip_pid = 0;
713                                 FILE *uncompressed_stream;
714                                 /* open a stream of decompressed data */
715                                 uncompressed_stream = gz_open(unzipped_opkg_stream, &gunzip_pid);
716                                 if (uncompressed_stream == NULL) {
717                                         *err = -1;
718                                         goto cleanup;
719                                 }
720                                 archive_offset = 0;
721
722                                 output_buffer = unarchive(uncompressed_stream, 
723                                                           out_stream, 
724                                                           get_header_tar,
725                                                           free_header_tar,
726                                                           extract_function, 
727                                                           prefix, 
728                                                           file_list,
729                                                           err);
730
731                                 free_header_tar(tar_header);
732                                 fclose(uncompressed_stream);
733                                 gz_err = gz_close(gunzip_pid);
734                                 if (gz_err)
735                                         *err = -1;
736                                 free_header_tar(tar_header);
737                                 break;
738                         }
739                         seek_sub_file(unzipped_opkg_stream, tar_header->size);
740                         free_header_tar(tar_header);
741                 }
742                 fclose(unzipped_opkg_stream);
743                 gz_err = gz_close(unzipped_opkg_pid);
744                 if (gz_err)
745                         *err = -1;
746
747                 goto cleanup;
748         } else {
749                 *err = -1;
750                 error_msg("%s: invalid magic", package_filename);
751         }
752
753 cleanup:
754         if (deb_stream)
755                 fclose(deb_stream);
756         if (file_list)
757                 free(file_list);
758
759         return output_buffer;
760 }