Debian Codegraph @

unzip/unzip.cpp

raw symbols SCIP
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/unzip/unzip.cpp $
21 * $Revision: 3 $
22 * $Date: 8/15/99 8:07p $
23 * $Author: Jeff $
24 *
25 * Unzip class routines
26 *
27 * $Log: /DescentIII/Main/unzip/unzip.cpp $
28 *
29 * 3 8/15/99 8:07p Jeff
30 * handle disk write errors when extracting
31 *
32 * 2 8/13/99 8:02p Jeff
33 * initial creation of zip class
34 *
35 * $NoKeywords: $
36 */
37
38 #include <algorithm>
39 #include <cstdlib>
40 #include <cstring>
41 #include <cctype>
42
43 #include <zlib.h>
44
45 #include "unzip.h"cxx . . $ `<file>/unzip/unzip.h`/
46
47 #define INFLATE_INPUT_BUFFER_MAXcxx . . $ `unzip/unzip.cpp:47:9`! 16384
48 #define DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`! 1024
49
50 // ECD Offsets
51 #define ECD_ESIGcxx . . $ `unzip/unzip.cpp:51:9`! 0x00
52 #define ECD_EDSKcxx . . $ `unzip/unzip.cpp:52:9`! 0x04
53 #define ECD_ECENcxx . . $ `unzip/unzip.cpp:53:9`! 0x06
54 #define ECD_ENUMcxx . . $ `unzip/unzip.cpp:54:9`! 0x08
55 #define ECD_ECENNcxx . . $ `unzip/unzip.cpp:55:9`! 0x0a
56 #define ECD_ECSZcxx . . $ `unzip/unzip.cpp:56:9`! 0x0c
57 #define ECD_EOFSTcxx . . $ `unzip/unzip.cpp:57:9`! 0x10
58 #define ECD_ECOMLcxx . . $ `unzip/unzip.cpp:58:9`! 0x14
59 #define ECD_ECOMcxx . . $ `unzip/unzip.cpp:59:9`! 0x16
60
61 // CD Entry Offsets
62 #define ZIPCD_CENSIGcxx . . $ `unzip/unzip.cpp:62:9`! 0x0
63 #define ZIPCD_CVERcxx . . $ `unzip/unzip.cpp:63:9`! 0x4
64 #define ZIPCD_COScxx . . $ `unzip/unzip.cpp:64:9`! 0x5
65 #define ZIPCD_CVXTcxx . . $ `unzip/unzip.cpp:65:9`! 0x6
66 #define ZIPCD_CEXOScxx . . $ `unzip/unzip.cpp:66:9`! 0x7
67 #define ZIPCD_CFLGcxx . . $ `unzip/unzip.cpp:67:9`! 0x8
68 #define ZIPCD_CMTHDcxx . . $ `unzip/unzip.cpp:68:9`! 0xa
69 #define ZIPCD_CTIMEcxx . . $ `unzip/unzip.cpp:69:9`! 0xc
70 #define ZIPCD_CDATEcxx . . $ `unzip/unzip.cpp:70:9`! 0xe
71 #define ZIPCD_CCRCcxx . . $ `unzip/unzip.cpp:71:9`! 0x10
72 #define ZIPCD_CSIZcxx . . $ `unzip/unzip.cpp:72:9`! 0x14
73 #define ZIPCD_CUNCcxx . . $ `unzip/unzip.cpp:73:9`! 0x18
74 #define ZIPCD_CFNLcxx . . $ `unzip/unzip.cpp:74:9`! 0x1c
75 #define ZIPCD_CXTLcxx . . $ `unzip/unzip.cpp:75:9`! 0x1e
76 #define ZIPCD_CCMLcxx . . $ `unzip/unzip.cpp:76:9`! 0x20
77 #define ZIPCD_DSKcxx . . $ `unzip/unzip.cpp:77:9`! 0x22
78 #define ZIPCD_INTcxx . . $ `unzip/unzip.cpp:78:9`! 0x24
79 #define ZIPCD_EXTcxx . . $ `unzip/unzip.cpp:79:9`! 0x26
80 #define ZIPCD_OFSTcxx . . $ `unzip/unzip.cpp:80:9`! 0x2a
81 #define ZIPCD_CFNcxx . . $ `unzip/unzip.cpp:81:9`! 0x2e
82
83 // Local File Header Offsets
84 #define LFH_LOCSIGcxx . . $ `unzip/unzip.cpp:84:9`! 0x00
85 #define LFH_VERcxx . . $ `unzip/unzip.cpp:85:9`! 0x04
86 #define LFH_GENFLGcxx . . $ `unzip/unzip.cpp:86:9`! 0x06
87 #define LFH_MTHDcxx . . $ `unzip/unzip.cpp:87:9`! 0x08
88 #define LFH_TIMEcxx . . $ `unzip/unzip.cpp:88:9`! 0x0a
89 #define LFH_DATEcxx . . $ `unzip/unzip.cpp:89:9`! 0x0c
90 #define LFH_CRCcxx . . $ `unzip/unzip.cpp:90:9`! 0x0e
91 #define LFH_SIZEcxx . . $ `unzip/unzip.cpp:91:9`! 0x12
92 #define LFH_UNCMPcxx . . $ `unzip/unzip.cpp:92:9`! 0x16
93 #define LFH_FNLNcxx . . $ `unzip/unzip.cpp:93:9`! 0x1a
94 #define LFH_XTRALNcxx . . $ `unzip/unzip.cpp:94:9`! 0x1c
95 #define LFH_NAMEcxx . . $ `unzip/unzip.cpp:95:9`! 0x1e
96
97 uint16_tcxx . . $ uint16_t# get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(char *buf) {
98 uint8_tcxx . . $ uint8_t# *ubuf = (uint8_tcxx . . $ uint8_t# *)buf;
99 return ((uint16_tcxx . . $ uint16_t#)ubuf[1] << 8) | (uint16_tcxx . . $ uint16_t#)ubuf[0];
100 }
101 uint32_tcxx . . $ uint32_t# get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(char *buf) {
102 uint8_tcxx . . $ uint8_t# *ubuf = (uint8_tcxx . . $ uint8_t# *)buf;
103 return ((uint32_tcxx . . $ uint32_t#)ubuf[3] << 24) | ((uint32_tcxx . . $ uint32_t#)ubuf[2] << 16) | ((uint32_tcxx . . $ uint32_t#)ubuf[1] << 8) | (uint32_tcxx . . $ uint32_t#)ubuf[0];
104 }
105
106 ZIPcxx . . $ ZIP#::ZIPcxx . . $ ZIP#ZIP(49f6e7a06ebc5aa8).() { m_opencxx . . $ ZIP#m_open. = false; }
107
108 ZIPcxx . . $ ZIP#::~cxx . . $ ZIP#`~ZIP`(ced63f7c635d850d).ZIPcxx . . $ ZIP#() { CloseZipcxx . . $ ZIP#CloseZip(49f6e7a06ebc5aa8).(); }
109
110 // Looks for the ECD in the buffer, if found it returns the offset
111 // returns true on success
112 bool ZIPcxx . . $ ZIP#::FindECDSignaturecxx . . $ ZIP#FindECDSignature(e7ab2d42594eba7).(char *buffer, int buflen, int *offset) {
113 static char sig[] = {'P', 'K', 0x05, 0x06};
114 int i;
115
116 for (i = buflen - 22; i >= 0; i--) {
117 if (memcmp(buffer + i, sig, 4) == 0) {
118 *offset = i;
119 return true;
120 }
121 }
122 return false;
123 }
124
125 int ZIPcxx . . $ ZIP#::ReadECDcxx . . $ ZIP#ReadECD(b126dc7c1de90089).() {
126 char *buf;
127 int buf_length = 1024;
128
129 while (true) {
130 int offset;
131
132 if (buf_length > m_lengthcxx . . $ ZIP#m_length.)
133 buf_length = m_lengthcxx . . $ ZIP#m_length.;
134
135 if (fseek(m_fpcxx . . $ ZIP#m_fp., m_lengthcxx . . $ ZIP#m_length. - buf_length, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) != 0) {
136 return -1;
137 }
138
139 buf = (char *)malloc(buf_length);
140 if (!buf) {
141 return -1;
142 }
143
144 if (fread(buf, buf_length, 1, m_fpcxx . . $ ZIP#m_fp.) != 1) {
145 free(buf);
146 return -1;
147 }
148
149 if (FindECDSignaturecxx . . $ ZIP#FindECDSignature(e7ab2d42594eba7).(buf, buf_length, &offset)) {
150 m_ecd_lengthcxx . . $ ZIP#m_ecd_length. = buf_length - offset;
151 m_ecdcxx . . $ ZIP#m_ecd. = (char *)malloc(m_ecd_lengthcxx . . $ ZIP#m_ecd_length.);
152
153 if (!m_ecdcxx . . $ ZIP#m_ecd.) {
154 free(buf);
155 return -1;
156 }
157
158 memcpy(m_ecdcxx . . $ ZIP#m_ecd., buf + offset, m_ecd_lengthcxx . . $ ZIP#m_ecd_length.);
159 free(buf);
160 return 0;
161 }
162
163 free(buf);
164
165 if (buf_length < m_lengthcxx . . $ ZIP#m_length.) {
166 buf_length = 2 * buf_length;
167 } else {
168 return -1;
169 }
170 }
171
172 return -1;
173 }
174
175 // opens a zip file for reading
176 // returns true on success
177 bool ZIPcxx . . $ ZIP#::OpenZipcxx . . $ ZIP#OpenZip(c5f7da30cbc0f4e9).(const char *zipfile) {
178 if (m_opencxx . . $ ZIP#m_open.)
179 return false;
180
181 // open the file for reading
182 m_fpcxx . . $ ZIP#m_fp. = fopen(zipfile, "rb");
183 if (!m_fpcxx . . $ ZIP#m_fp.) {
184 return false;
185 }
186
187 // seek to the end and get the length of the zip file
188 if (fseek(m_fpcxx . . $ ZIP#m_fp., 0, SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!) != 0) {
189 fclose(m_fpcxx . . $ ZIP#m_fp.);
190 return false;
191 }
192
193 m_lengthcxx . . $ ZIP#m_length. = ftell(m_fpcxx . . $ ZIP#m_fp.);
194 if (m_lengthcxx . . $ ZIP#m_length. < 0) {
195 fclose(m_fpcxx . . $ ZIP#m_fp.);
196 return false;
197 }
198 if (m_lengthcxx . . $ ZIP#m_length. == 0) {
199 fclose(m_fpcxx . . $ ZIP#m_fp.);
200 return false;
201 }
202
203 // read End of Central Directory (ECD) data
204 if (ReadECDcxx . . $ ZIP#ReadECD(b126dc7c1de90089).() != 0) {
205 fclose(m_fpcxx . . $ ZIP#m_fp.);
206 return false;
207 }
208
209 m_end_of_cent_dir_sigcxx . . $ ZIP#m_end_of_cent_dir_sig. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ESIGcxx . . $ `unzip/unzip.cpp:51:9`!);
210 m_number_of_this_diskcxx . . $ ZIP#m_number_of_this_disk. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_EDSKcxx . . $ `unzip/unzip.cpp:52:9`!);
211 m_number_of_disk_start_cent_dircxx . . $ ZIP#m_number_of_disk_start_cent_dir. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ECENcxx . . $ `unzip/unzip.cpp:53:9`!);
212 m_total_entries_cent_dir_this_diskcxx . . $ ZIP#m_total_entries_cent_dir_this_disk. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ENUMcxx . . $ `unzip/unzip.cpp:54:9`!);
213 m_total_entries_cent_dircxx . . $ ZIP#m_total_entries_cent_dir. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ECENNcxx . . $ `unzip/unzip.cpp:55:9`!);
214 m_size_of_cent_dircxx . . $ ZIP#m_size_of_cent_dir. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ECSZcxx . . $ `unzip/unzip.cpp:56:9`!);
215 m_offset_to_start_of_cent_dircxx . . $ ZIP#m_offset_to_start_of_cent_dir. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_EOFSTcxx . . $ `unzip/unzip.cpp:57:9`!);
216 m_zipfile_comment_lengthcxx . . $ ZIP#m_zipfile_comment_length. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_ecdcxx . . $ ZIP#m_ecd. + ECD_ECOMLcxx . . $ `unzip/unzip.cpp:58:9`!);
217 m_zipfile_commentcxx . . $ ZIP#m_zipfile_comment. = m_ecdcxx . . $ ZIP#m_ecd. + ECD_ECOMcxx . . $ `unzip/unzip.cpp:59:9`!;
218
219 // make sure there is no disk spanning
220 if ((m_number_of_this_diskcxx . . $ ZIP#m_number_of_this_disk. != m_number_of_disk_start_cent_dircxx . . $ ZIP#m_number_of_disk_start_cent_dir.) ||
221 (m_total_entries_cent_dir_this_diskcxx . . $ ZIP#m_total_entries_cent_dir_this_disk. != m_total_entries_cent_dircxx . . $ ZIP#m_total_entries_cent_dir.) || (m_total_entries_cent_dircxx . . $ ZIP#m_total_entries_cent_dir. < 1)) {
222 free(m_ecdcxx . . $ ZIP#m_ecd.);
223 fclose(m_fpcxx . . $ ZIP#m_fp.);
224 return false;
225 }
226
227 if (fseek(m_fpcxx . . $ ZIP#m_fp., m_offset_to_start_of_cent_dircxx . . $ ZIP#m_offset_to_start_of_cent_dir., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) != 0) {
228 free(m_ecdcxx . . $ ZIP#m_ecd.);
229 fclose(m_fpcxx . . $ ZIP#m_fp.);
230 return false;
231 }
232
233 // begin reading for the begining of central directory
234 m_cdcxx . . $ ZIP#m_cd. = (char *)malloc(m_size_of_cent_dircxx . . $ ZIP#m_size_of_cent_dir.);
235 if (!m_cdcxx . . $ ZIP#m_cd.) {
236 free(m_ecdcxx . . $ ZIP#m_ecd.);
237 fclose(m_fpcxx . . $ ZIP#m_fp.);
238 return false;
239 }
240
241 if (fread(m_cdcxx . . $ ZIP#m_cd., m_size_of_cent_dircxx . . $ ZIP#m_size_of_cent_dir., 1, m_fpcxx . . $ ZIP#m_fp.) != 1) {
242 free(m_cdcxx . . $ ZIP#m_cd.);
243 free(m_ecdcxx . . $ ZIP#m_ecd.);
244 fclose(m_fpcxx . . $ ZIP#m_fp.);
245 return false;
246 }
247
248 // reset zip entry
249 m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name. = nullptr;
250
251 // rewind to the begining of central dir
252 m_cd_poscxx . . $ ZIP#m_cd_pos. = 0;
253
254 m_zipcxx . . $ ZIP#m_zip. = (char *)malloc(strlen(zipfile) + 1);
255 if (!m_zipcxx . . $ ZIP#m_zip.) {
256 free(m_cdcxx . . $ ZIP#m_cd.);
257 free(m_ecdcxx . . $ ZIP#m_ecd.);
258 fclose(m_fpcxx . . $ ZIP#m_fp.);
259 return false;
260 }
261 strcpy(m_zipcxx . . $ ZIP#m_zip., zipfile);
262 m_opencxx . . $ ZIP#m_open. = true;
263
264 return true;
265 }
266
267 // closes an open zip file
268 void ZIPcxx . . $ ZIP#::CloseZipcxx . . $ ZIP#CloseZip(49f6e7a06ebc5aa8).() {
269 if (!m_opencxx . . $ ZIP#m_open.)
270 return;
271
272 /* release all */
273 if (m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.)
274 free(m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.);
275 if (m_cdcxx . . $ ZIP#m_cd.)
276 free(m_cdcxx . . $ ZIP#m_cd.);
277 if (m_ecdcxx . . $ ZIP#m_ecd.)
278 free(m_ecdcxx . . $ ZIP#m_ecd.);
279 if (m_fpcxx . . $ ZIP#m_fp.)
280 fclose(m_fpcxx . . $ ZIP#m_fp.);
281 if (m_zipcxx . . $ ZIP#m_zip.)
282 free(m_zipcxx . . $ ZIP#m_zip.);
283 m_opencxx . . $ ZIP#m_open. = false;
284 }
285
286 // Reads the current zip entry from the zip file (and moves
287 // to the next entry). Returns NULL if there are no more entries
288 zipentrycxx . . $ zipentry# *ZIPcxx . . $ ZIP#::ReadNextZipEntrycxx . . $ ZIP#ReadNextZipEntry(49bfa059dac8e27d).() {
289 if (!m_opencxx . . $ ZIP#m_open.)
290 return nullptr;
291
292 // make sure we aren't at the end
293 if (m_cd_poscxx . . $ ZIP#m_cd_pos. >= m_size_of_cent_dircxx . . $ ZIP#m_size_of_cent_dir.)
294 return nullptr;
295
296 m_entcxx . . $ ZIP#m_ent..cent_file_header_sigcxx . . $ zipentry#cent_file_header_sig. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CENSIGcxx . . $ `unzip/unzip.cpp:62:9`!);
297 m_entcxx . . $ ZIP#m_ent..version_made_bycxx . . $ zipentry#version_made_by. = *(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CVERcxx . . $ `unzip/unzip.cpp:63:9`!);
298 m_entcxx . . $ ZIP#m_ent..host_oscxx . . $ zipentry#host_os. = *(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_COScxx . . $ `unzip/unzip.cpp:64:9`!);
299 m_entcxx . . $ ZIP#m_ent..version_needed_to_extractcxx . . $ zipentry#version_needed_to_extract. = *(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CVXTcxx . . $ `unzip/unzip.cpp:65:9`!);
300 m_entcxx . . $ ZIP#m_ent..os_needed_to_extractcxx . . $ zipentry#os_needed_to_extract. = *(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CEXOScxx . . $ `unzip/unzip.cpp:66:9`!);
301 m_entcxx . . $ ZIP#m_ent..general_purpose_bit_flagcxx . . $ zipentry#general_purpose_bit_flag. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CFLGcxx . . $ `unzip/unzip.cpp:67:9`!);
302 m_entcxx . . $ ZIP#m_ent..compression_methodcxx . . $ zipentry#compression_method. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CMTHDcxx . . $ `unzip/unzip.cpp:68:9`!);
303 m_entcxx . . $ ZIP#m_ent..last_mod_file_timecxx . . $ zipentry#last_mod_file_time. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CTIMEcxx . . $ `unzip/unzip.cpp:69:9`!);
304 m_entcxx . . $ ZIP#m_ent..last_mod_file_datecxx . . $ zipentry#last_mod_file_date. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CDATEcxx . . $ `unzip/unzip.cpp:70:9`!);
305 m_entcxx . . $ ZIP#m_ent..crc32cxx . . $ zipentry#crc32. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CCRCcxx . . $ `unzip/unzip.cpp:71:9`!);
306 m_entcxx . . $ ZIP#m_ent..compressed_sizecxx . . $ zipentry#compressed_size. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CSIZcxx . . $ `unzip/unzip.cpp:72:9`!);
307 m_entcxx . . $ ZIP#m_ent..uncompressed_sizecxx . . $ zipentry#uncompressed_size. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CUNCcxx . . $ `unzip/unzip.cpp:73:9`!);
308 m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CFNLcxx . . $ `unzip/unzip.cpp:74:9`!);
309 m_entcxx . . $ ZIP#m_ent..extra_field_lengthcxx . . $ zipentry#extra_field_length. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CXTLcxx . . $ `unzip/unzip.cpp:75:9`!);
310 m_entcxx . . $ ZIP#m_ent..file_comment_lengthcxx . . $ zipentry#file_comment_length. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CCMLcxx . . $ `unzip/unzip.cpp:76:9`!);
311 m_entcxx . . $ ZIP#m_ent..disk_number_startcxx . . $ zipentry#disk_number_start. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_DSKcxx . . $ `unzip/unzip.cpp:77:9`!);
312 m_entcxx . . $ ZIP#m_ent..internal_file_attribcxx . . $ zipentry#internal_file_attrib. = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_INTcxx . . $ `unzip/unzip.cpp:78:9`!);
313 m_entcxx . . $ ZIP#m_ent..external_file_attribcxx . . $ zipentry#external_file_attrib. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_EXTcxx . . $ `unzip/unzip.cpp:79:9`!);
314 m_entcxx . . $ ZIP#m_ent..offset_lcl_hdr_frm_frst_diskcxx . . $ zipentry#offset_lcl_hdr_frm_frst_disk. = get_buffer_intcxx . . $ get_buffer_int(33ab057182e9f89d).(m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_OFSTcxx . . $ `unzip/unzip.cpp:80:9`!);
315
316 // Make sure the filename isn't too long
317 if (m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CFNcxx . . $ `unzip/unzip.cpp:81:9`! + m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length. > m_size_of_cent_dircxx . . $ ZIP#m_size_of_cent_dir.) {
318 return nullptr;
319 }
320
321 if (m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.)
322 free(m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.);
323 m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name. = (char *)malloc(m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length. + 1);
324 if (!m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.)
325 return nullptr;
326 memcpy(m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name., m_cdcxx . . $ ZIP#m_cd. + m_cd_poscxx . . $ ZIP#m_cd_pos. + ZIPCD_CFNcxx . . $ `unzip/unzip.cpp:81:9`!, m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length.);
327 m_entcxx . . $ ZIP#m_ent..namecxx . . $ zipentry#name.[m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length.] = '\0';
328
329 // skip to next entry in central dir
330 m_cd_poscxx . . $ ZIP#m_cd_pos. += ZIPCD_CFNcxx . . $ `unzip/unzip.cpp:81:9`! + m_entcxx . . $ ZIP#m_ent..filename_lengthcxx . . $ zipentry#filename_length. + m_entcxx . . $ ZIP#m_ent..extra_field_lengthcxx . . $ zipentry#extra_field_length. + m_entcxx . . $ ZIP#m_ent..file_comment_lengthcxx . . $ zipentry#file_comment_length.;
331
332 return &m_entcxx . . $ ZIP#m_ent.;
333 }
334
335 // Resets a ZIP file to the first entry
336 void ZIPcxx . . $ ZIP#::Rewindcxx . . $ ZIP#Rewind(49f6e7a06ebc5aa8).() {
337 if (!m_opencxx . . $ ZIP#m_open.)
338 return;
339 m_cd_poscxx . . $ ZIP#m_cd_pos. = 0;
340 }
341
342 // Reads a file from the given zip entry into a buffer in memory
343 // -1 : no ZIP file open
344 // -2 : corrupt file
345 // -3 : Version too new
346 // -4 : OS not supported
347 // -5 : No Disk Spanning
348 // -6 : Error inflating
349 // -7 : Compression Type Not Supported
350 int ZIPcxx . . $ ZIP#::ReadFilecxx . . $ ZIP#ReadFile(c690ba302e3ff01d).(zipentrycxx . . $ zipentry# *ent, char *data) {
351 if (!m_opencxx . . $ ZIP#m_open.)
352 return -1;
353
354 if (ent->compression_methodcxx . . $ zipentry#compression_method. == 0x0000) {
355 // this file is STORED, not compressed
356
357 // make sure the sizes are equal, else the file is corrupt
358 if (ent->compressed_sizecxx . . $ zipentry#compressed_size. != ent->uncompressed_sizecxx . . $ zipentry#uncompressed_size.) {
359 return -2;
360 }
361
362 return ReadZipDatacxx . . $ ZIP#ReadZipData(c690ba302e3ff01d).(ent, data);
363 } else if (ent->compression_methodcxx . . $ zipentry#compression_method. == 0x0008) {
364 // compression type of DEFLATE
365
366 if (ent->version_needed_to_extractcxx . . $ zipentry#version_needed_to_extract. > 0x14) {
367 return -3;
368 }
369
370 if (ent->os_needed_to_extractcxx . . $ zipentry#os_needed_to_extract. != 0x00) {
371 return -4;
372 }
373
374 if (ent->disk_number_startcxx . . $ zipentry#disk_number_start. != m_number_of_this_diskcxx . . $ ZIP#m_number_of_this_disk.) {
375 return -5;
376 }
377
378 // seek to compressed data
379 if (SeekToCompressedDatacxx . . $ ZIP#SeekToCompressedData(71e99d3a41c7d32b).(ent) != 0) {
380 return -2;
381 }
382
383 // inflate the file
384 if (InflateFilecxx . . $ ZIP#InflateFile(20bf2652f263df83).(m_fpcxx . . $ ZIP#m_fp., ent->compressed_sizecxx . . $ zipentry#compressed_size., (uint8_tcxx . . $ uint8_t# *)data, ent->uncompressed_sizecxx . . $ zipentry#uncompressed_size.)) {
385 return -6;
386 }
387
388 return 0;
389 }
390
391 return -7;
392 }
393
394 // Extracts a file from the given zip entry into another file
395 // -1 : no ZIP file open
396 // -2 : corrupt file
397 // -3 : Version too new
398 // -4 : OS not supported
399 // -5 : No Disk Spanning
400 // -6 : Error inflating
401 // -7 : Compression Type Not Supported
402 // -8 : Unable to open output
403 // -9 : Error writing to file
404 int ZIPcxx . . $ ZIP#::ExtractFilecxx . . $ ZIP#ExtractFile(6368df9ccc80b6b3).(zipentrycxx . . $ zipentry# *ent, const char *filename) {
405 if (!m_opencxx . . $ ZIP#m_open.)
406 return -1;
407
408 FILE *output;
409 output = fopen(filename, "wb");
410 if (!output)
411 return -8;
412
413 if (ent->compression_methodcxx . . $ zipentry#compression_method. == 0x0000) {
414 // this file is STORED, not compressed
415
416 // make sure the sizes are equal, else the file is corrupt
417 if (ent->compressed_sizecxx . . $ zipentry#compressed_size. != ent->uncompressed_sizecxx . . $ zipentry#uncompressed_size.) {
418 fclose(output);
419 return -2;
420 }
421
422 int ret = ReadZipDataToFilecxx . . $ ZIP#ReadZipDataToFile(d92add29cb964409).(ent, output);
423 fclose(output);
424
425 return ret;
426 } else if (ent->compression_methodcxx . . $ zipentry#compression_method. == 0x0008) {
427 // compression type of DEFLATE
428
429 if (ent->version_needed_to_extractcxx . . $ zipentry#version_needed_to_extract. > 0x14) {
430 fclose(output);
431 return -3;
432 }
433
434 if (ent->os_needed_to_extractcxx . . $ zipentry#os_needed_to_extract. != 0x00) {
435 fclose(output);
436 return -4;
437 }
438
439 if (ent->disk_number_startcxx . . $ zipentry#disk_number_start. != m_number_of_this_diskcxx . . $ ZIP#m_number_of_this_disk.) {
440 fclose(output);
441 return -5;
442 }
443
444 // seek to compressed data
445 if (SeekToCompressedDatacxx . . $ ZIP#SeekToCompressedData(71e99d3a41c7d32b).(ent) != 0) {
446 fclose(output);
447 return -2;
448 }
449
450 // inflate the file
451 int inf_ret;
452 inf_ret = InflateFileToFilecxx . . $ ZIP#InflateFileToFile(a602d8568e72276a).(m_fpcxx . . $ ZIP#m_fp., ent->compressed_sizecxx . . $ zipentry#compressed_size., output, ent->uncompressed_sizecxx . . $ zipentry#uncompressed_size.);
453 if (inf_ret != 0) {
454 fclose(output);
455 return (inf_ret == -2) ? -9 : -6;
456 }
457
458 fclose(output);
459 return 0;
460 }
461
462 fclose(output);
463 return -7;
464 }
465
466 int ZIPcxx . . $ ZIP#::ReadZipDatacxx . . $ ZIP#ReadZipData(c690ba302e3ff01d).(zipentrycxx . . $ zipentry# *ent, char *data) {
467 int err = SeekToCompressedDatacxx . . $ ZIP#SeekToCompressedData(71e99d3a41c7d32b).(ent);
468 if (err != 0)
469 return -2;
470
471 if (fread(data, ent->compressed_sizecxx . . $ zipentry#compressed_size., 1, m_fpcxx . . $ ZIP#m_fp.) != 1) {
472 return -2;
473 }
474
475 return 0;
476 }
477
478 int ZIPcxx . . $ ZIP#::ReadZipDataToFilecxx . . $ ZIP#ReadZipDataToFile(d92add29cb964409).(zipentrycxx . . $ zipentry# *ent, FILE *file) {
479 int err = SeekToCompressedDatacxx . . $ ZIP#SeekToCompressedData(71e99d3a41c7d32b).(ent);
480 if (err != 0)
481 return -2;
482
483 uint8_tcxx . . $ uint8_t# data[DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`!];
484 int size_remaining, amount;
485 size_remaining = ent->compressed_sizecxx . . $ zipentry#compressed_size.;
486
487 while (size_remaining > 0) {
488 amount = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(size_remaining, DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`!);
489
490 if (fread(data, amount, 1, m_fpcxx . . $ ZIP#m_fp.) != 1) {
491 return -2;
492 }
493 if (fwrite(data, amount, 1, file) == 0) {
494 // error writing
495 return -9;
496 }
497 size_remaining -= amount;
498 }
499
500 return 0;
501 }
502
503 int ZIPcxx . . $ ZIP#::SeekToCompressedDatacxx . . $ ZIP#SeekToCompressedData(71e99d3a41c7d32b).(zipentrycxx . . $ zipentry# *ent) {
504 char buf[LFH_NAMEcxx . . $ `unzip/unzip.cpp:95:9`!];
505 int32_t offset;
506
507 if (fseek(m_fpcxx . . $ ZIP#m_fp., ent->offset_lcl_hdr_frm_frst_diskcxx . . $ zipentry#offset_lcl_hdr_frm_frst_disk., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) != 0) {
508 return -1;
509 }
510
511 if (fread(buf, LFH_NAMEcxx . . $ `unzip/unzip.cpp:95:9`!, 1, m_fpcxx . . $ ZIP#m_fp.) != 1) {
512 return -1;
513 }
514
515 uint16_tcxx . . $ uint16_t# filename_length = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(buf + LFH_FNLNcxx . . $ `unzip/unzip.cpp:93:9`!);
516 uint16_tcxx . . $ uint16_t# extra_field_length = get_buffer_shortcxx . . $ get_buffer_short(1c09e5b692edaacb).(buf + LFH_XTRALNcxx . . $ `unzip/unzip.cpp:94:9`!);
517
518 offset = ent->offset_lcl_hdr_frm_frst_diskcxx . . $ zipentry#offset_lcl_hdr_frm_frst_disk. + LFH_NAMEcxx . . $ `unzip/unzip.cpp:95:9`! + filename_length + extra_field_length;
519
520 if (fseek(m_fpcxx . . $ ZIP#m_fp., offset, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) != 0) {
521 return -1;
522 }
523
524 return 0;
525 }
526
527 // uses zlib to inflate the given data
528 int ZIPcxx . . $ ZIP#::InflateFilecxx . . $ ZIP#InflateFile(20bf2652f263df83).(FILE *in_file, unsigned in_size, uint8_tcxx . . $ uint8_t# *out_data, unsigned out_size) {
529 int err;
530 uint8_tcxx . . $ uint8_t# *in_buffer;
531 z_stream d_stream;
532
533 d_stream.zalloc = nullptr;
534 d_stream.zfree = nullptr;
535 d_stream.opaque = nullptr;
536
537 d_stream.next_in = nullptr;
538 d_stream.avail_in = 0;
539 d_stream.next_out = out_data;
540 d_stream.avail_out = out_size;
541
542 err = inflateInit2cxx . . $ `<external>/8929586433573063152/zlib.h:1939:11`!(&d_stream, -MAX_WBITScxx . . $ `<external>/116306602880328527/zconf.h:287:11`!);
543 /* windowBits is passed < 0 to tell that there is no zlib header.
544 * Note that in this case inflate *requires* an extra "dummy" byte
545 * after the compressed stream in order to complete decompression and
546 * return Z_STREAM_END.
547 */
548 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
549 return -1;
550 }
551
552 in_buffer = (uint8_tcxx . . $ uint8_t# *)malloc(INFLATE_INPUT_BUFFER_MAXcxx . . $ `unzip/unzip.cpp:47:9`! + 1);
553 if (!in_buffer)
554 return -1;
555
556 for (;;) {
557 if (in_size <= 0) {
558 free(in_buffer);
559 return -1;
560 }
561 d_stream.next_in = in_buffer;
562 d_stream.avail_in = fread(in_buffer, 1, stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<uIntcxx . . $ uInt#>(in_size, INFLATE_INPUT_BUFFER_MAXcxx . . $ `unzip/unzip.cpp:47:9`!), in_file);
563 in_size -= d_stream.avail_in;
564 if (in_size == 0)
565 d_stream.avail_in++; // add dummy byte at end of compressed data
566
567 err = inflate(&d_stream, Z_NO_FLUSHcxx . . $ `<external>/8929586433573063152/zlib.h:172:9`!);
568 if (err == Z_STREAM_ENDcxx . . $ `<external>/8929586433573063152/zlib.h:182:9`!)
569 break;
570 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
571 free(in_buffer);
572 return -1;
573 }
574 }
575
576 err = inflateEnd(&d_stream);
577 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
578 free(in_buffer);
579 return -1;
580 }
581
582 free(in_buffer);
583
584 if ((d_stream.avail_out > 0) || (in_size > 0)) {
585 return -1;
586 }
587
588 return 0;
589 }
590
591 // uses zlib to inflate the given data
592 int ZIPcxx . . $ ZIP#::InflateFileToFilecxx . . $ ZIP#InflateFileToFile(a602d8568e72276a).(FILE *in_file, unsigned in_size, FILE *file, unsigned out_size) {
593 uint8_tcxx . . $ uint8_t# out_data[DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`!];
594 int err;
595 uint32_tcxx . . $ uint32_t# last_out;
596 uint8_tcxx . . $ uint8_t# *in_buffer;
597 uint8_tcxx . . $ uint8_t# *next_out;
598 z_stream d_stream;
599
600 d_stream.zalloc = nullptr;
601 d_stream.zfree = nullptr;
602 d_stream.opaque = nullptr;
603
604 d_stream.next_in = nullptr;
605 d_stream.avail_in = 0;
606 d_stream.next_out = nullptr;
607 d_stream.avail_out = 0;
608 d_stream.total_out = 0;
609 d_stream.total_in = 0;
610
611 err = inflateInit2cxx . . $ `<external>/8929586433573063152/zlib.h:1939:11`!(&d_stream, -MAX_WBITScxx . . $ `<external>/116306602880328527/zconf.h:287:11`!);
612 /* windowBits is passed < 0 to tell that there is no zlib header.
613 * Note that in this case inflate *requires* an extra "dummy" byte
614 * after the compressed stream in order to complete decompression and
615 * return Z_STREAM_END.
616 */
617 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
618 return -1;
619 }
620
621 in_buffer = (uint8_tcxx . . $ uint8_t# *)malloc(DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`! + 1);
622 if (!in_buffer)
623 return -1;
624
625 for (;;) {
626 // reset the output buffer
627 last_out = d_stream.total_out;
628 next_out = d_stream.next_out;
629
630 if (in_size <= 0 && out_size <= 0) {
631 free(in_buffer);
632 return -1;
633 }
634
635 if (d_stream.avail_out == 0) {
636 d_stream.next_out = out_data;
637 d_stream.avail_out = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<uIntcxx . . $ uInt#>(out_size, DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`!);
638 out_size -= d_stream.avail_out;
639 next_out = d_stream.next_out;
640 }
641
642 if (d_stream.avail_in == 0 && in_size > 0) {
643 d_stream.next_in = in_buffer;
644 d_stream.avail_in = fread(in_buffer, 1, stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).<uIntcxx . . $ uInt#>(in_size, DATA_CHUNK_SIZEcxx . . $ `unzip/unzip.cpp:48:9`!), in_file);
645 in_size -= d_stream.avail_in;
646 if (in_size == 0)
647 d_stream.avail_in++; // add dummy byte at end of compressed data
648 }
649
650 err = inflate(&d_stream, Z_NO_FLUSHcxx . . $ `<external>/8929586433573063152/zlib.h:172:9`!);
651 if (err == Z_STREAM_ENDcxx . . $ `<external>/8929586433573063152/zlib.h:182:9`!) {
652 if (d_stream.total_out > last_out) {
653 int res, to_write = (d_stream.total_out - last_out);
654 res = fwrite(next_out, to_write, 1, file);
655 if (res == 0) {
656 // error writing to file!
657 free(in_buffer);
658 return -2;
659 }
660 break;
661 }
662 }
663 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
664 free(in_buffer);
665 return -1;
666 }
667 if (d_stream.total_out > last_out) {
668 int res, to_write = (d_stream.total_out - last_out);
669 res = fwrite(next_out, to_write, 1, file);
670 if (res == 0) {
671 // error writing to file!
672 free(in_buffer);
673 return -2;
674 }
675 }
676 }
677
678 err = inflateEnd(&d_stream);
679 if (err != Z_OKcxx . . $ `<external>/8929586433573063152/zlib.h:181:9`!) {
680 free(in_buffer);
681 return -1;
682 }
683
684 free(in_buffer);
685
686 if ((d_stream.avail_out > 0) || (in_size > 0)) {
687 return -1;
688 }
689
690 return 0;
691 }
692
693 // Compare two filename without using directory
694 int CompareZipFileNamecxx . . $ CompareZipFileName(5ae29d726ae57f88).(const char *zipfile, const char *file) {
695 const char *s1 = file;
696 const char *s2 = strrchr(zipfile, '/');
697 if (s2)
698 ++s2;
699 else
700 s2 = zipfile;
701 while (*s1 && toupper(*s1) == toupper(*s2)) {
702 ++s1;
703 ++s2;
704 }
705 return !*s1 && !*s2;
706 }