From f6f654ee53f7cc1d5844d5a884ab49f1b43a6bd1 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Mon, 12 Feb 2024 05:04:11 +0100 Subject: [PATCH] minizip: re-add doom3-specific changes --- Changelog.md | 1 + neo/framework/minizip/MiniZip64_info.txt | 9 ++- neo/framework/minizip/ioapi.c | 2 +- neo/framework/minizip/ioapi.h | 8 ++- neo/framework/minizip/unzip.cpp | 70 +++++++++++++++++++++++- neo/framework/minizip/unzip.h | 22 +++++++- 6 files changed, 103 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 57c02144..22eecc03 100644 --- a/Changelog.md +++ b/Changelog.md @@ -26,6 +26,7 @@ Note: Numbers starting with a "#" like #330 refer to the bugreport with that num * Fix several compiler warnings * Added build instructions for Linux (and similar systems) to README.md * Updated stb_image and stb_vorbis +* Updated minizip (from zlib/contrib) to latest upstream code 1.5.2 (2022-06-13) diff --git a/neo/framework/minizip/MiniZip64_info.txt b/neo/framework/minizip/MiniZip64_info.txt index 62dec524..cd6e450e 100644 --- a/neo/framework/minizip/MiniZip64_info.txt +++ b/neo/framework/minizip/MiniZip64_info.txt @@ -1,11 +1,14 @@ NOTE from Daniel Gibson: -These are parts of MiniZip (the unzip-parts), taken from zlib-1.2.7/contrib/minizip/ -and adjusted for doom3 by making small adjustments foor doom3 (doom3-specific allocator) +These are parts of MiniZip (the unzip-parts), taken from zlib's contrib/minizip/, +the now-current development branch (commit f56ad0aafa868303ce324ae8d45675570c66b050 +"Note termination of returned strings in contrib/minizip/unzip.h." from 2024-01-23) +and adjusted for doom3 by making small adjustments for doom3 (doom3-specific allocator, +which included reintroducing the TRYFREE macro that was removed upstream) and adding a function (unzReOpen()) from the original Doom3 GPL release by id Software that used MiniZip 0.15beta. It was adjusted by Daniel Gibson for MiniZip 1.1 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. - Copyright (C) 2012 Daniel Gibson + Copyright (C) 2012,2024 Daniel Gibson ########### Original Content of this File below ############# diff --git a/neo/framework/minizip/ioapi.c b/neo/framework/minizip/ioapi.c index 782d3246..d8e1e5f7 100644 --- a/neo/framework/minizip/ioapi.c +++ b/neo/framework/minizip/ioapi.c @@ -14,7 +14,7 @@ #define _CRT_SECURE_NO_WARNINGS #endif -#if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) +#if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) || defined(__OpenBSD__) // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions #define FOPEN_FUNC(filename, mode) fopen(filename, mode) #define FTELLO_FUNC(stream) ftello(stream) diff --git a/neo/framework/minizip/ioapi.h b/neo/framework/minizip/ioapi.h index a2d2e6e6..8ba1f6f4 100644 --- a/neo/framework/minizip/ioapi.h +++ b/neo/framework/minizip/ioapi.h @@ -21,6 +21,12 @@ #ifndef _ZLIBIOAPI64_H #define _ZLIBIOAPI64_H +// fixes Gentoo zlib problems, see +// https://bugs.gentoo.org/show_bug.cgi?id=383179 +#ifndef OF +#define OF(x) x +#endif + #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) // Linux needs this to support file operation on files larger then 4+GB @@ -43,7 +49,7 @@ #include #include -#include "zlib.h" +#include #if defined(USE_FILE32API) #define fopen64 fopen diff --git a/neo/framework/minizip/unzip.cpp b/neo/framework/minizip/unzip.cpp index b8a0d127..a5a1ce20 100644 --- a/neo/framework/minizip/unzip.cpp +++ b/neo/framework/minizip/unzip.cpp @@ -61,9 +61,18 @@ Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson + #################################################################################### + +unzReOpen() was added by id Software (originally in framework/Unzip.cpp) for doom3 +and adjusted for MiniZip 1.1 by Daniel Gibson +The file was renamed from unzip.c to unzip.cpp so we can use C++ functions from doom3 + + Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. + Copyright (C) 2012 Daniel Gibson + */ - +#if 0 // we don't really want system includes here #include #include #include @@ -80,6 +89,18 @@ # include #endif +#else // 0 +// use doom3 headers +#include "sys/platform.h" +#include "idlib/Heap.h" +#include "idlib/Lib.h" + +// we don't need crypt support +#define NOUNCRYPT 1 + +#include "unzip.h" +#endif // 0 + #ifndef local # define local static @@ -102,12 +123,17 @@ #define UNZ_MAXFILENAMEINZIP (256) #endif +#if 0 // don't use system malloc but doom3's allocator #ifndef ALLOC # define ALLOC(size) (malloc(size)) #endif #ifndef TRYFREE # define TRYFREE(p) { free(p);} #endif +#else // 0 +#define ALLOC(size) (Mem_Alloc(size)) +#define TRYFREE(p) {Mem_Free(p);} // Mem_Free - as well as free() - check for NULL themselves, no need to do it here +#endif // 0 #define SIZECENTRALDIRITEM (0x2e) #define SIZEZIPLOCALHEADER (0x1e) @@ -1593,6 +1619,9 @@ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) { uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed; if (uReadThis == 0) return UNZ_EOF; + // TODO: the following line was added by id Software to the original src - seems to work without it, + // but if problems occur look here.. + // if(s->cur_file_info.compressed_size == pfile_in_zip_read_info->rest_read_compressed) if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, pfile_in_zip_read_info->pos_in_zipfile + @@ -1982,3 +2011,42 @@ extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos) { extern int ZEXPORT unzSetOffset (unzFile file, uLong pos) { return unzSetOffset64(file,pos); } + + +// #################################################################### +// the following function was added for doom3 by id Software and +// adjusted for MiniZip 1.1 by Daniel Gibson + + +extern unzFile unzReOpen (const char* path, unzFile file) +{ + unz64_s* s; + unz64_s* zFile = (unz64_s*)file; + + if(zFile == NULL) + return NULL; + + // create unz64_s* "s" as clone of "file" + s=(unz64_s*)ALLOC(sizeof(unz64_s)); + if(s == NULL) + return NULL; + + memcpy(s, zFile, sizeof(unz64_s)); + + // create new filestream for path + voidp fin = ZOPEN64(s->z_filefunc, + path, + ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING); + + if( fin == NULL ) { + TRYFREE(s); + return NULL; + } + + // set that filestream in s + s->filestream = fin; + + unzOpenCurrentFile( s ); + + return (unzFile)s; +} diff --git a/neo/framework/minizip/unzip.h b/neo/framework/minizip/unzip.h index ceb614e7..1ccc0a60 100644 --- a/neo/framework/minizip/unzip.h +++ b/neo/framework/minizip/unzip.h @@ -48,12 +48,12 @@ extern "C" { #endif #ifndef _ZLIB_H -#include "zlib.h" +#include #endif -#ifndef _ZLIBIOAPI_H +// TODO #ifndef _ZLIBIOAPI_H #include "ioapi.h" -#endif +// TODO #endif #ifdef HAVE_BZIP2 #include "bzlib.h" @@ -433,6 +433,22 @@ extern int ZEXPORT unzSetOffset64 (unzFile file, ZPOS64_T pos); extern int ZEXPORT unzSetOffset (unzFile file, uLong pos); +// #################################################################### + +// the following function was added for doom3 by id Software + +extern unzFile unzReOpen( const char* path, unzFile file ); + +/* + Re-Open a Zip file, i.e. clone an existing one and give it a new file descriptor. + path contain the full pathname (by example, + on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer + "zlib/zlib111.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ #ifdef __cplusplus }