From 270541f9422038c829f673de30a0b4e4562d05f5 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 26 Jun 2014 09:43:51 +0200 Subject: [PATCH] fixed compilation with latest LZMA SDK on Windows. LZMA SDK recently added an #include to its headers, meaning it's no longer safe to include its headers globally in platform independent files. The following changes were necessary: - rename DWORD type in zipdir.c - add USE_WINDOWS_DWORD and reorder includes in file_7z.cpp - wrap LZMA decoder stream into a local struct that's declared anonymously in files.h and adjust files.cpp for this change. --- src/files.cpp | 25 +++++++++++++++++----- src/files.h | 5 +++-- src/resourcefiles/file_7z.cpp | 6 ++++-- tools/zipdir/zipdir.c | 40 +++++++++++++++++------------------ 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/files.cpp b/src/files.cpp index d7dfc2cbe..981b351a9 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -33,11 +33,15 @@ ** */ +#define USE_WINDOWS_DWORD +#include "LzmaDec.h" + #include "files.h" #include "i_system.h" #include "templates.h" #include "m_misc.h" + //========================================================================== // // FileReader @@ -370,6 +374,15 @@ extern "C" void bz_internal_error (int errcode) // //========================================================================== +// This is retarded but necessary to work around the inclusion of windows.h in recent +// LZMA versions, meaning it's no longer possible to include the LZMA headers in files.h. +// As a result we cannot declare the CLzmaDec member in the header so we work around +// it my wrapping it into another struct that can be declared anonymously in the header. +struct FileReaderLZMA::StreamPointer +{ + CLzmaDec Stream; +}; + static void *SzAlloc(void *, size_t size) { return malloc(size); } static void SzFree(void *, void *address) { free(address); } ISzAlloc g_Alloc = { SzAlloc, SzFree }; @@ -398,20 +411,22 @@ FileReaderLZMA::FileReaderLZMA (FileReader &file, size_t uncompressed_size, bool FillBuffer(); - LzmaDec_Construct(&Stream); - err = LzmaDec_Allocate(&Stream, header + 4, LZMA_PROPS_SIZE, &g_Alloc); + Streamp = new StreamPointer; + LzmaDec_Construct(&Streamp->Stream); + err = LzmaDec_Allocate(&Streamp->Stream, header + 4, LZMA_PROPS_SIZE, &g_Alloc); if (err != SZ_OK) { I_Error("FileReaderLZMA: LzmaDec_Allocate failed: %d\n", err); } - LzmaDec_Init(&Stream); + LzmaDec_Init(&Streamp->Stream); } FileReaderLZMA::~FileReaderLZMA () { - LzmaDec_Free(&Stream, &g_Alloc); + LzmaDec_Free(&Streamp->Stream, &g_Alloc); + delete Streamp; } long FileReaderLZMA::Read (void *buffer, long len) @@ -426,7 +441,7 @@ long FileReaderLZMA::Read (void *buffer, long len) size_t out_processed = len; size_t in_processed = InSize; - err = LzmaDec_DecodeToBuf(&Stream, next_out, &out_processed, InBuff + InPos, &in_processed, finish_mode, &status); + err = LzmaDec_DecodeToBuf(&Streamp->Stream, next_out, &out_processed, InBuff + InPos, &in_processed, finish_mode, &status); InPos += in_processed; InSize -= in_processed; next_out += out_processed; diff --git a/src/files.h b/src/files.h index ebe9665a4..f7d061c8e 100644 --- a/src/files.h +++ b/src/files.h @@ -4,7 +4,6 @@ #include #include #include "bzlib.h" -#include "LzmaDec.h" #include "doomtype.h" #include "m_swap.h" @@ -257,6 +256,8 @@ private: // Wraps around a FileReader to decompress a lzma stream class FileReaderLZMA : public FileReaderBase { + struct StreamPointer; + public: FileReaderLZMA (FileReader &file, size_t uncompressed_size, bool zip); ~FileReaderLZMA (); @@ -308,7 +309,7 @@ private: FileReader &File; bool SawEOF; - CLzmaDec Stream; + StreamPointer *Streamp; // anonymous pointer to LKZA decoder struct - to avoid including the LZMA headers globally size_t Size; size_t InPos, InSize; size_t OutProcessed; diff --git a/src/resourcefiles/file_7z.cpp b/src/resourcefiles/file_7z.cpp index 53653bb59..c3845d8cc 100644 --- a/src/resourcefiles/file_7z.cpp +++ b/src/resourcefiles/file_7z.cpp @@ -32,6 +32,10 @@ ** ** */ +#define USE_WINDOWS_DWORD + +#include "7z.h" +#include "7zCrc.h" #include "resourcefile.h" #include "cmdlib.h" @@ -41,8 +45,6 @@ #include "i_system.h" #include "w_wad.h" -#include "7z.h" -#include "7zCrc.h" //----------------------------------------------------------------------- diff --git a/tools/zipdir/zipdir.c b/tools/zipdir/zipdir.c index 9b521bedf..1479952e0 100644 --- a/tools/zipdir/zipdir.c +++ b/tools/zipdir/zipdir.c @@ -129,7 +129,7 @@ typedef struct compressor_s int method; } compressor_t; -typedef unsigned int DWORD; +typedef unsigned int UINT32; typedef unsigned short WORD; typedef unsigned char BYTE; @@ -139,49 +139,49 @@ typedef unsigned char BYTE; //#pragma pack(push,1) typedef struct { - DWORD Magic; // 0 + UINT32 Magic; // 0 BYTE VersionToExtract[2]; // 4 WORD Flags; // 6 WORD Method; // 8 WORD ModTime; // 10 WORD ModDate; // 12 - DWORD CRC32; // 14 - DWORD CompressedSize; // 18 - DWORD UncompressedSize; // 22 + UINT32 CRC32; // 14 + UINT32 CompressedSize; // 18 + UINT32 UncompressedSize; // 22 WORD NameLength; // 26 WORD ExtraLength; // 28 } LocalFileHeader; typedef struct { - DWORD Magic; + UINT32 Magic; BYTE VersionMadeBy[2]; BYTE VersionToExtract[2]; WORD Flags; WORD Method; WORD ModTime; WORD ModDate; - DWORD CRC32; - DWORD CompressedSize; - DWORD UncompressedSize; + UINT32 CRC32; + UINT32 CompressedSize; + UINT32 UncompressedSize; WORD NameLength; WORD ExtraLength; WORD CommentLength; WORD StartingDiskNumber; WORD InternalAttributes; - DWORD ExternalAttributes; - DWORD LocalHeaderOffset; + UINT32 ExternalAttributes; + UINT32 LocalHeaderOffset; } CentralDirectoryEntry; typedef struct { - DWORD Magic; + UINT32 Magic; WORD DiskNumber; WORD FirstDisk; WORD NumEntries; WORD NumEntriesOnAllDisks; - DWORD DirectorySize; - DWORD DirectoryOffset; + UINT32 DirectorySize; + UINT32 DirectoryOffset; WORD ZipCommentLength; } EndOfCentralDirectory; //#pragma pack(pop) @@ -1373,7 +1373,7 @@ BYTE *find_central_dir(FILE *fin) back_read = 4; while (back_read < max_back) { - DWORD read_size, read_pos; + UINT32 read_size, read_pos; int i; if (back_read + BUFREADCOMMENT > max_back) back_read = max_back; @@ -1420,12 +1420,12 @@ BYTE *find_central_dir(FILE *fin) free(dir); return NULL; } - if (*(DWORD *)dir != ZIP_CENTRALFILE) + if (*(UINT32 *)dir != ZIP_CENTRALFILE) { free(dir); return NULL; } - *(DWORD *)(dir + LittleLong(eod.DirectorySize)) = ZIP_ENDOFDIR; + *(UINT32 *)(dir + LittleLong(eod.DirectorySize)) = ZIP_ENDOFDIR; return dir; } @@ -1444,7 +1444,7 @@ CentralDirectoryEntry *find_file_in_zip(BYTE *dir, const char *path, unsigned in CentralDirectoryEntry *ent; int flags; - while (*(DWORD *)dir == ZIP_CENTRALFILE) + while (*(UINT32 *)dir == ZIP_CENTRALFILE) { ent = (CentralDirectoryEntry *)dir; if (pathlen == LittleShort(ent->NameLength) && @@ -1455,7 +1455,7 @@ CentralDirectoryEntry *find_file_in_zip(BYTE *dir, const char *path, unsigned in } dir += sizeof(*ent) + LittleShort(ent->NameLength) + LittleShort(ent->ExtraLength) + LittleShort(ent->CommentLength); } - if (*(DWORD *)dir != ZIP_CENTRALFILE) + if (*(UINT32 *)dir != ZIP_CENTRALFILE) { return NULL; } @@ -1495,7 +1495,7 @@ int copy_zip_file(FILE *zip, file_entry_t *file, FILE *ozip, CentralDirectoryEnt { LocalFileHeader lfh; BYTE *buf; - DWORD buf_size; + UINT32 buf_size; if (fseek(ozip, LittleLong(ent->LocalHeaderOffset), SEEK_SET) != 0) {