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"
46
47 #define INFLATE_INPUT_BUFFER_MAX 16384
48 #define DATA_CHUNK_SIZE 1024
49
50 // ECD Offsets
51 #define ECD_ESIG 0x00
52 #define ECD_EDSK 0x04
53 #define ECD_ECEN 0x06
54 #define ECD_ENUM 0x08
55 #define ECD_ECENN 0x0a
56 #define ECD_ECSZ 0x0c
57 #define ECD_EOFST 0x10
58 #define ECD_ECOML 0x14
59 #define ECD_ECOM 0x16
60
61 // CD Entry Offsets
62 #define ZIPCD_CENSIG 0x0
63 #define ZIPCD_CVER 0x4
64 #define ZIPCD_COS 0x5
65 #define ZIPCD_CVXT 0x6
66 #define ZIPCD_CEXOS 0x7
67 #define ZIPCD_CFLG 0x8
68 #define ZIPCD_CMTHD 0xa
69 #define ZIPCD_CTIME 0xc
70 #define ZIPCD_CDATE 0xe
71 #define ZIPCD_CCRC 0x10
72 #define ZIPCD_CSIZ 0x14
73 #define ZIPCD_CUNC 0x18
74 #define ZIPCD_CFNL 0x1c
75 #define ZIPCD_CXTL 0x1e
76 #define ZIPCD_CCML 0x20
77 #define ZIPCD_DSK 0x22
78 #define ZIPCD_INT 0x24
79 #define ZIPCD_EXT 0x26
80 #define ZIPCD_OFST 0x2a
81 #define ZIPCD_CFN 0x2e
82
83 // Local File Header Offsets
84 #define LFH_LOCSIG 0x00
85 #define LFH_VER 0x04
86 #define LFH_GENFLG 0x06
87 #define LFH_MTHD 0x08
88 #define LFH_TIME 0x0a
89 #define LFH_DATE 0x0c
90 #define LFH_CRC 0x0e
91 #define LFH_SIZE 0x12
92 #define LFH_UNCMP 0x16
93 #define LFH_FNLN 0x1a
94 #define LFH_XTRALN 0x1c
95 #define LFH_NAME 0x1e
96
97 uint16_t get_buffer_short(char *buf) {
98 uint8_t *ubuf = (uint8_t *)buf;
99 return ((uint16_t)ubuf[1] << 8) | (uint16_t)ubuf[0];
100 }
101 uint32_t get_buffer_int(char *buf) {
102 uint8_t *ubuf = (uint8_t *)buf;
103 return ((uint32_t)ubuf[3] << 24) | ((uint32_t)ubuf[2] << 16) | ((uint32_t)ubuf[1] << 8) | (uint32_t)ubuf[0];
104 }
105
106 ZIP::ZIP() { m_open = false; }
107
108 ZIP::~ZIP() { CloseZip(); }
109
110 // Looks for the ECD in the buffer, if found it returns the offset
111 // returns true on success
112 bool ZIP::FindECDSignature(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 ZIP::ReadECD() {
126 char *buf;
127 int buf_length = 1024;
128
129 while (true) {
130 int offset;
131
132 if (buf_length > m_length)
133 buf_length = m_length;
134
135 if (fseek(m_fp, m_length - buf_length, SEEK_SET) != 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_fp) != 1) {
145 free(buf);
146 return -1;
147 }
148
149 if (FindECDSignature(buf, buf_length, &offset)) {
150 m_ecd_length = buf_length - offset;
151 m_ecd = (char *)malloc(m_ecd_length);
152
153 if (!m_ecd) {
154 free(buf);
155 return -1;
156 }
157
158 memcpy(m_ecd, buf + offset, m_ecd_length);
159 free(buf);
160 return 0;
161 }
162
163 free(buf);
164
165 if (buf_length < 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 ZIP::OpenZip(const char *zipfile) {
178 if (m_open)
179 return false;
180
181 // open the file for reading
182 m_fp = fopen(zipfile, "rb");
183 if (!m_fp) {
184 return false;
185 }
186
187 // seek to the end and get the length of the zip file
188 if (fseek(m_fp, 0, SEEK_END) != 0) {
189 fclose(m_fp);
190 return false;
191 }
192
193 m_length = ftell(m_fp);
194 if (m_length < 0) {
195 fclose(m_fp);
196 return false;
197 }
198 if (m_length == 0) {
199 fclose(m_fp);
200 return false;
201 }
202
203 // read End of Central Directory (ECD) data
204 if (ReadECD() != 0) {
205 fclose(m_fp);
206 return false;
207 }
208
209 m_end_of_cent_dir_sig = get_buffer_int(m_ecd + ECD_ESIG);
210 m_number_of_this_disk = get_buffer_short(m_ecd + ECD_EDSK);
211 m_number_of_disk_start_cent_dir = get_buffer_short(m_ecd + ECD_ECEN);
212 m_total_entries_cent_dir_this_disk = get_buffer_short(m_ecd + ECD_ENUM);
213 m_total_entries_cent_dir = get_buffer_short(m_ecd + ECD_ECENN);
214 m_size_of_cent_dir = get_buffer_int(m_ecd + ECD_ECSZ);
215 m_offset_to_start_of_cent_dir = get_buffer_int(m_ecd + ECD_EOFST);
216 m_zipfile_comment_length = get_buffer_short(m_ecd + ECD_ECOML);
217 m_zipfile_comment = m_ecd + ECD_ECOM;
218
219 // make sure there is no disk spanning
220 if ((m_number_of_this_disk != m_number_of_disk_start_cent_dir) ||
221 (m_total_entries_cent_dir_this_disk != m_total_entries_cent_dir) || (m_total_entries_cent_dir < 1)) {
222 free(m_ecd);
223 fclose(m_fp);
224 return false;
225 }
226
227 if (fseek(m_fp, m_offset_to_start_of_cent_dir, SEEK_SET) != 0) {
228 free(m_ecd);
229 fclose(m_fp);
230 return false;
231 }
232
233 // begin reading for the begining of central directory
234 m_cd = (char *)malloc(m_size_of_cent_dir);
235 if (!m_cd) {
236 free(m_ecd);
237 fclose(m_fp);
238 return false;
239 }
240
241 if (fread(m_cd, m_size_of_cent_dir, 1, m_fp) != 1) {
242 free(m_cd);
243 free(m_ecd);
244 fclose(m_fp);
245 return false;
246 }
247
248 // reset zip entry
249 m_ent.name = nullptr;
250
251 // rewind to the begining of central dir
252 m_cd_pos = 0;
253
254 m_zip = (char *)malloc(strlen(zipfile) + 1);
255 if (!m_zip) {
256 free(m_cd);
257 free(m_ecd);
258 fclose(m_fp);
259 return false;
260 }
261 strcpy(m_zip, zipfile);
262 m_open = true;
263
264 return true;
265 }
266
267 // closes an open zip file
268 void ZIP::CloseZip() {
269 if (!m_open)
270 return;
271
272 /* release all */
273 if (m_ent.name)
274 free(m_ent.name);
275 if (m_cd)
276 free(m_cd);
277 if (m_ecd)
278 free(m_ecd);
279 if (m_fp)
280 fclose(m_fp);
281 if (m_zip)
282 free(m_zip);
283 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 zipentry *ZIP::ReadNextZipEntry() {
289 if (!m_open)
290 return nullptr;
291
292 // make sure we aren't at the end
293 if (m_cd_pos >= m_size_of_cent_dir)
294 return nullptr;
295
296 m_ent.cent_file_header_sig = get_buffer_int(m_cd + m_cd_pos + ZIPCD_CENSIG);
297 m_ent.version_made_by = *(m_cd + m_cd_pos + ZIPCD_CVER);
298 m_ent.host_os = *(m_cd + m_cd_pos + ZIPCD_COS);
299 m_ent.version_needed_to_extract = *(m_cd + m_cd_pos + ZIPCD_CVXT);
300 m_ent.os_needed_to_extract = *(m_cd + m_cd_pos + ZIPCD_CEXOS);
301 m_ent.general_purpose_bit_flag = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CFLG);
302 m_ent.compression_method = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CMTHD);
303 m_ent.last_mod_file_time = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CTIME);
304 m_ent.last_mod_file_date = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CDATE);
305 m_ent.crc32 = get_buffer_int(m_cd + m_cd_pos + ZIPCD_CCRC);
306 m_ent.compressed_size = get_buffer_int(m_cd + m_cd_pos + ZIPCD_CSIZ);
307 m_ent.uncompressed_size = get_buffer_int(m_cd + m_cd_pos + ZIPCD_CUNC);
308 m_ent.filename_length = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CFNL);
309 m_ent.extra_field_length = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CXTL);
310 m_ent.file_comment_length = get_buffer_short(m_cd + m_cd_pos + ZIPCD_CCML);
311 m_ent.disk_number_start = get_buffer_short(m_cd + m_cd_pos + ZIPCD_DSK);
312 m_ent.internal_file_attrib = get_buffer_short(m_cd + m_cd_pos + ZIPCD_INT);
313 m_ent.external_file_attrib = get_buffer_int(m_cd + m_cd_pos + ZIPCD_EXT);
314 m_ent.offset_lcl_hdr_frm_frst_disk = get_buffer_int(m_cd + m_cd_pos + ZIPCD_OFST);
315
316 // Make sure the filename isn't too long
317 if (m_cd_pos + ZIPCD_CFN + m_ent.filename_length > m_size_of_cent_dir) {
318 return nullptr;
319 }
320
321 if (m_ent.name)
322 free(m_ent.name);
323 m_ent.name = (char *)malloc(m_ent.filename_length + 1);
324 if (!m_ent.name)
325 return nullptr;
326 memcpy(m_ent.name, m_cd + m_cd_pos + ZIPCD_CFN, m_ent.filename_length);
327 m_ent.name[m_ent.filename_length] = '\0';
328
329 // skip to next entry in central dir
330 m_cd_pos += ZIPCD_CFN + m_ent.filename_length + m_ent.extra_field_length + m_ent.file_comment_length;
331
332 return &m_ent;
333 }
334
335 // Resets a ZIP file to the first entry
336 void ZIP::Rewind() {
337 if (!m_open)
338 return;
339 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 ZIP::ReadFile(zipentry *ent, char *data) {
351 if (!m_open)
352 return -1;
353
354 if (ent->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_size != ent->uncompressed_size) {
359 return -2;
360 }
361
362 return ReadZipData(ent, data);
363 } else if (ent->compression_method == 0x0008) {
364 // compression type of DEFLATE
365
366 if (ent->version_needed_to_extract > 0x14) {
367 return -3;
368 }
369
370 if (ent->os_needed_to_extract != 0x00) {
371 return -4;
372 }
373
374 if (ent->disk_number_start != m_number_of_this_disk) {
375 return -5;
376 }
377
378 // seek to compressed data
379 if (SeekToCompressedData(ent) != 0) {
380 return -2;
381 }
382
383 // inflate the file
384 if (InflateFile(m_fp, ent->compressed_size, (uint8_t *)data, ent->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 ZIP::ExtractFile(zipentry *ent, const char *filename) {
405 if (!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_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_size != ent->uncompressed_size) {
418 fclose(output);
419 return -2;
420 }
421
422 int ret = ReadZipDataToFile(ent, output);
423 fclose(output);
424
425 return ret;
426 } else if (ent->compression_method == 0x0008) {
427 // compression type of DEFLATE
428
429 if (ent->version_needed_to_extract > 0x14) {
430 fclose(output);
431 return -3;
432 }
433
434 if (ent->os_needed_to_extract != 0x00) {
435 fclose(output);
436 return -4;
437 }
438
439 if (ent->disk_number_start != m_number_of_this_disk) {
440 fclose(output);
441 return -5;
442 }
443
444 // seek to compressed data
445 if (SeekToCompressedData(ent) != 0) {
446 fclose(output);
447 return -2;
448 }
449
450 // inflate the file
451 int inf_ret;
452 inf_ret = InflateFileToFile(m_fp, ent->compressed_size, output, ent->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 ZIP::ReadZipData(zipentry *ent, char *data) {
467 int err = SeekToCompressedData(ent);
468 if (err != 0)
469 return -2;
470
471 if (fread(data, ent->compressed_size, 1, m_fp) != 1) {
472 return -2;
473 }
474
475 return 0;
476 }
477
478 int ZIP::ReadZipDataToFile(zipentry *ent, FILE *file) {
479 int err = SeekToCompressedData(ent);
480 if (err != 0)
481 return -2;
482
483 uint8_t data[DATA_CHUNK_SIZE];
484 int size_remaining, amount;
485 size_remaining = ent->compressed_size;
486
487 while (size_remaining > 0) {
488 amount = std::min(size_remaining, DATA_CHUNK_SIZE);
489
490 if (fread(data, amount, 1, 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 ZIP::SeekToCompressedData(zipentry *ent) {
504 char buf[LFH_NAME];
505 int32_t offset;
506
507 if (fseek(m_fp, ent->offset_lcl_hdr_frm_frst_disk, SEEK_SET) != 0) {
508 return -1;
509 }
510
511 if (fread(buf, LFH_NAME, 1, m_fp) != 1) {
512 return -1;
513 }
514
515 uint16_t filename_length = get_buffer_short(buf + LFH_FNLN);
516 uint16_t extra_field_length = get_buffer_short(buf + LFH_XTRALN);
517
518 offset = ent->offset_lcl_hdr_frm_frst_disk + LFH_NAME + filename_length + extra_field_length;
519
520 if (fseek(m_fp, offset, SEEK_SET) != 0) {
521 return -1;
522 }
523
524 return 0;
525 }
526
527 // uses zlib to inflate the given data
528 int ZIP::InflateFile(FILE *in_file, unsigned in_size, uint8_t *out_data, unsigned out_size) {
529 int err;
530 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 = inflateInit2(&d_stream, -MAX_WBITS);
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_OK) {
549 return -1;
550 }
551
552 in_buffer = (uint8_t *)malloc(INFLATE_INPUT_BUFFER_MAX + 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, std::min<uInt>(in_size, INFLATE_INPUT_BUFFER_MAX), 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_FLUSH);
568 if (err == Z_STREAM_END)
569 break;
570 if (err != Z_OK) {
571 free(in_buffer);
572 return -1;
573 }
574 }
575
576 err = inflateEnd(&d_stream);
577 if (err != Z_OK) {
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 ZIP::InflateFileToFile(FILE *in_file, unsigned in_size, FILE *file, unsigned out_size) {
593 uint8_t out_data[DATA_CHUNK_SIZE];
594 int err;
595 uint32_t last_out;
596 uint8_t *in_buffer;
597 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 = inflateInit2(&d_stream, -MAX_WBITS);
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_OK) {
618 return -1;
619 }
620
621 in_buffer = (uint8_t *)malloc(DATA_CHUNK_SIZE + 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 = std::min<uInt>(out_size, DATA_CHUNK_SIZE);
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, std::min<uInt>(in_size, DATA_CHUNK_SIZE), 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_FLUSH);
651 if (err == Z_STREAM_END) {
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_OK) {
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_OK) {
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 CompareZipFileName(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 }