| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/Lib/unzip.h $ |
| 21 | * $Revision: 3 $ |
| 22 | * $Date: 8/15/99 8:07p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Unzip class |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/Lib/unzip.h $ |
| 28 | * |
| 29 | * 3 8/15/99 8:07p Jeff |
| 30 | * handle disk write errors when extracting |
| 31 | * |
| 32 | * 2 8/13/99 8:01p Jeff |
| 33 | * initial creation of zip class |
| 34 | * |
| 35 | * $NoKeywords: $ |
| 36 | */ |
| 37 | |
| 38 | #ifndef UNZIP_H |
| 39 | #define UNZIP_H |
| 40 | |
| 41 | #include <cstdint> |
| 42 | #include <cstdio> |
| 43 | |
| 44 | struct zipentry { |
| 45 | uint32_t cent_file_header_sig; |
| 46 | uint8_t version_made_by; |
| 47 | uint8_t host_os; |
| 48 | uint8_t version_needed_to_extract; |
| 49 | uint8_t os_needed_to_extract; |
| 50 | uint16_t general_purpose_bit_flag; |
| 51 | uint16_t compression_method; |
| 52 | uint16_t last_mod_file_time; |
| 53 | uint16_t last_mod_file_date; |
| 54 | uint32_t crc32; |
| 55 | uint32_t compressed_size; |
| 56 | uint32_t uncompressed_size; |
| 57 | uint16_t filename_length; |
| 58 | uint16_t extra_field_length; |
| 59 | uint16_t file_comment_length; |
| 60 | uint16_t disk_number_start; |
| 61 | uint16_t internal_file_attrib; |
| 62 | uint32_t external_file_attrib; |
| 63 | uint32_t offset_lcl_hdr_frm_frst_disk; |
| 64 | char *name; |
| 65 | }; |
| 66 | |
| 67 | class ZIP { |
| 68 | public: |
| 69 | // constructor/destructor |
| 70 | ZIP(); |
| 71 | ~ZIP(); |
| 72 | |
| 73 | public: |
| 74 | // opens a zip file for reading |
| 75 | // returns true on success |
| 76 | bool OpenZip(const char *path); |
| 77 | |
| 78 | // closes an open zip file |
| 79 | void CloseZip(); |
| 80 | |
| 81 | // Reads the current zip entry from the zip file (and moves |
| 82 | // to the next entry). Returns NULL if there are no more entries |
| 83 | zipentry *ReadNextZipEntry(); |
| 84 | |
| 85 | // Resets a ZIP file to the first entry |
| 86 | void Rewind(); |
| 87 | |
| 88 | // Reads a file from the given zip entry into a buffer in memory |
| 89 | // -1 : no ZIP file open |
| 90 | // -2 : corrupt file |
| 91 | // -3 : Version too new |
| 92 | // -4 : OS not supported |
| 93 | // -5 : No Disk Spanning |
| 94 | // -6 : Error inflating |
| 95 | // -7 : Compression Type Not Supported |
| 96 | int ReadFile(zipentry *ent, char *data); |
| 97 | |
| 98 | // Extracts a file from the given zip entry into another file |
| 99 | // -1 : no ZIP file open |
| 100 | // -2 : corrupt file |
| 101 | // -3 : Version too new |
| 102 | // -4 : OS not supported |
| 103 | // -5 : No Disk Spanning |
| 104 | // -6 : Error inflating |
| 105 | // -7 : Compression Type Not Supported |
| 106 | // -8 : Unable to open output |
| 107 | // -9 : Error writing to file |
| 108 | int ExtractFile(zipentry *ent, const char *filename); |
| 109 | |
| 110 | private: |
| 111 | bool FindECDSignature(char *buffer, int buflen, int *offset); |
| 112 | int ReadECD(); |
| 113 | int ReadZipData(zipentry *ent, char *data); |
| 114 | int ReadZipDataToFile(zipentry *ent, FILE *file); |
| 115 | int SeekToCompressedData(zipentry *ent); |
| 116 | int InflateFile(FILE *in_file, unsigned in_size, uint8_t *out_data, unsigned out_size); |
| 117 | int InflateFileToFile(FILE *in_file, unsigned in_size, FILE *file, unsigned out_size); |
| 118 | |
| 119 | private: |
| 120 | bool m_open; |
| 121 | char *m_zip; // zip name |
| 122 | FILE *m_fp; // zip handler |
| 123 | int32_t m_length; // length of zip file |
| 124 | |
| 125 | char *m_ecd; // end_of_cent_dir data |
| 126 | unsigned m_ecd_length; // end_of_cent_dir length |
| 127 | |
| 128 | char *m_cd; // cent_dir data |
| 129 | |
| 130 | unsigned m_cd_pos; // position in cent_dir |
| 131 | |
| 132 | zipentry m_ent; // buffer for readzip |
| 133 | |
| 134 | // end_of_cent_dir |
| 135 | uint32_t m_end_of_cent_dir_sig; |
| 136 | uint16_t m_number_of_this_disk; |
| 137 | uint16_t m_number_of_disk_start_cent_dir; |
| 138 | uint16_t m_total_entries_cent_dir_this_disk; |
| 139 | uint16_t m_total_entries_cent_dir; |
| 140 | uint32_t m_size_of_cent_dir; |
| 141 | uint32_t m_offset_to_start_of_cent_dir; |
| 142 | uint16_t m_zipfile_comment_length; |
| 143 | char *m_zipfile_comment; // pointer in ecd |
| 144 | }; |
| 145 | |
| 146 | // Compare two filename without using directory |
| 147 | int CompareZipFileName(const char *zipfile, const char *file); |
| 148 | |
| 149 | #endif |