Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * get_header_zip for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001 by Laurence Anderson |
| 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 GNU |
| 15 | * 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 <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include "unarchive.h" |
| 26 | #include "libbb.h" |
| 27 | |
| 28 | #define ZIP_FILEHEADER_MAGIC 0x04034b50 |
| 29 | #define ZIP_CDS_MAGIC 0x02014b50 |
| 30 | #define ZIP_CDS_END_MAGIC 0x06054b50 |
| 31 | #define ZIP_DD_MAGIC 0x8074b50 |
| 32 | |
| 33 | file_header_t *get_header_zip(FILE *zip_stream) |
| 34 | { |
| 35 | struct { |
| 36 | short version __attribute__ ((packed)); |
| 37 | short flags __attribute__ ((packed)); |
| 38 | short method __attribute__ ((packed)); |
| 39 | short modtime __attribute__ ((packed)); |
| 40 | short moddate __attribute__ ((packed)); |
| 41 | int crc32 __attribute__ ((packed)); |
| 42 | int cmpsize __attribute__ ((packed)); |
| 43 | int ucmpsize __attribute__ ((packed)); |
| 44 | short filename_len __attribute__ ((packed)); |
| 45 | short extra_len __attribute__ ((packed)); |
| 46 | } zip_header; |
| 47 | file_header_t *zip_entry = NULL; |
| 48 | int magic; |
| 49 | static int dd_ahead = 0; // If this is true, the we didn't know how long the last extraced file was |
| 50 | |
| 51 | fread (&magic, 4, 1, zip_stream); |
| 52 | archive_offset += 4; |
| 53 | |
| 54 | if (feof(zip_stream)) return(NULL); |
| 55 | checkmagic: |
| 56 | switch (magic) { |
| 57 | case ZIP_FILEHEADER_MAGIC: |
| 58 | zip_entry = xcalloc(1, sizeof(file_header_t)); |
| 59 | fread (&zip_header, sizeof(zip_header), 1, zip_stream); |
| 60 | archive_offset += sizeof(zip_header); |
| 61 | if (!(zip_header.method == 8 || zip_header.method == 0)) { printf("Unsupported compression method %d\n", zip_header.method); return(NULL); } |
| 62 | zip_entry->name = calloc(zip_header.filename_len + 1, sizeof(char)); |
| 63 | fread (zip_entry->name, sizeof(char), zip_header.filename_len, zip_stream); |
| 64 | archive_offset += zip_header.filename_len; |
| 65 | seek_sub_file(zip_stream, zip_header.extra_len); |
| 66 | zip_entry->size = zip_header.cmpsize; |
| 67 | if (zip_header.method == 8) zip_entry->extract_func = &inflate; |
| 68 | zip_entry->mode = S_IFREG | 0777; |
| 69 | // Time/Date? |
| 70 | if (*(zip_entry->name + strlen(zip_entry->name) - 1) == '/') { // Files that end in a / are directories |
| 71 | zip_entry->mode ^= S_IFREG; |
| 72 | zip_entry->mode |= S_IFDIR; |
| 73 | *(zip_entry->name + strlen(zip_entry->name) - 1) = '\0'; // Remove trailing / so unarchive doesn't get confused |
| 74 | } |
| 75 | //printf("cmpsize: %d, ucmpsize: %d, method: %d\n", zip_header.cmpsize, zip_header.ucmpsize, zip_header.method); |
| 76 | if (zip_header.flags & 0x8) { // crc32, and sizes are in the data description _after_ the file |
| 77 | if (zip_header.cmpsize == 0) dd_ahead = 1; // As we don't know how long this file it is difficult to skip! but it is compressed, so normally its ok |
| 78 | if (zip_header.ucmpsize != 0) dd_ahead = 2; // Humm... we would otherwise skip this twice - not good! |
| 79 | } |
| 80 | break; |
| 81 | case ZIP_CDS_MAGIC: /* FALLTHRU */ |
| 82 | case ZIP_CDS_END_MAGIC: |
| 83 | return(NULL); |
| 84 | break; |
| 85 | case ZIP_DD_MAGIC: { |
| 86 | int cmpsize; |
| 87 | seek_sub_file(zip_stream, 4); // Skip crc32 |
| 88 | fread(&cmpsize, 4, 1, zip_stream); |
| 89 | archive_offset += 4; |
| 90 | if (dd_ahead == 1) archive_offset += cmpsize; |
| 91 | seek_sub_file(zip_stream, 4); // Skip uncompressed size |
| 92 | dd_ahead = 0; |
| 93 | return (get_header_zip(zip_stream)); |
| 94 | break; } |
| 95 | default: |
| 96 | if (!dd_ahead) error_msg("Invalid magic (%#x): Trying to skip junk", magic); |
| 97 | dd_ahead = 0; |
| 98 | while (!feof(zip_stream)) { |
| 99 | int tmpmagic; |
| 100 | tmpmagic = fgetc(zip_stream); |
| 101 | archive_offset++; |
| 102 | magic = ((magic >> 8) & 0x00ffffff) | ((tmpmagic << 24) & 0xff000000); |
| 103 | if (magic == ZIP_FILEHEADER_MAGIC || magic == ZIP_CDS_MAGIC || magic == ZIP_CDS_END_MAGIC) goto checkmagic; |
| 104 | } |
| 105 | error_msg("End of archive reached: Bad archive"); |
| 106 | return(NULL); |
| 107 | } |
| 108 | //if (archive_offset != ftell(zip_stream)) printf("Archive offset out of sync (%d,%d)\n", (int) archive_offset, (int) ftell(zip_stream)); |
| 109 | return(zip_entry); |
| 110 | } |