From 879612a1dcb6bb5b891447baffd4d5917ea37ee5 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 10 Apr 2022 15:56:29 +0200 Subject: Switched to C types and memory mutation functions in Archive --- StarsEx/Archive.cpp | 128 ++++++++++++++++++++++++++++------------------------ StarsEx/Archive.h | 38 ++++++++-------- 2 files changed, 86 insertions(+), 80 deletions(-) (limited to 'StarsEx') diff --git a/StarsEx/Archive.cpp b/StarsEx/Archive.cpp index ca73ec5..46a4541 100644 --- a/StarsEx/Archive.cpp +++ b/StarsEx/Archive.cpp @@ -7,18 +7,22 @@ */ -#include "Types.h" #include "Archive.h" -#include "Utils.h" #include -#include -#include #include #include +#include +#include #include -#include "zlib.h" +#include +#include + +#include + +#include "Utils.h" + // +--------------------------------------------------------------------+ @@ -36,7 +40,7 @@ int err; DataArchive::DataArchive(const char* name) { - ZeroMemory(this, sizeof(DataArchive)); + std::memset(this, 0, sizeof(DataArchive)); if (name) LoadDatafile(name); @@ -50,21 +54,23 @@ DataArchive::~DataArchive() // +--------------------------------------------------------------------+ -void DataArchive::WriteEntry(int index, BYTE* buf) +void DataArchive::WriteEntry(int index, std::uint8_t* buf) { int f = _open(datafile, _O_RDWR|_O_CREAT|_O_BINARY, _S_IREAD|_S_IWRITE); if (f != -1) { header.dir_size_comp = DirBlocks() * Archive::BLOCK_SIZE; - dirbuf = new BYTE[header.dir_size_comp]; + dirbuf = new std::uint8_t[header.dir_size_comp]; if (!dirbuf) { err = Z_MEM_ERROR; } else { - err = compress(dirbuf, &header.dir_size_comp, - (BYTE*) directory, header.nfiles * sizeof(DataEntry)); + auto dir_size_comp = static_cast(header.dir_size_comp); + err = compress(dirbuf, &dir_size_comp, + (std::uint8_t*) directory, header.nfiles * sizeof(DataEntry)); + header.dir_size_comp = static_cast(dir_size_comp); CHECK_ERR(err, "compress"); header.dir_blocks = Blocks(header.dir_size_comp) * Archive::BLOCK_SIZE; @@ -90,7 +96,7 @@ void DataArchive::WriteEntry(int index, BYTE* buf) // +--------------------------------------------------------------------+ -DWORD DataArchive::Blocks(DWORD raw_size) +std::uint32_t DataArchive::Blocks(std::uint32_t raw_size) { int full_blocks = raw_size / Archive::BLOCK_SIZE; int part_blocks = (raw_size % Archive::BLOCK_SIZE) > 0; @@ -98,14 +104,14 @@ DWORD DataArchive::Blocks(DWORD raw_size) return full_blocks + part_blocks; } -DWORD DataArchive::DirBlocks() +std::uint32_t DataArchive::DirBlocks() { - DWORD result = Blocks(header.nfiles * sizeof(DataEntry)); + std::uint32_t result = Blocks(header.nfiles * sizeof(DataEntry)); if (result == 0) result = 1; return result; } -DWORD DataArchive::FileBlocks(int index) +std::uint32_t DataArchive::FileBlocks(int index) { if (index >= 0 && index < (int) header.nfiles && directory) return Blocks(directory[index].size_comp); @@ -122,34 +128,34 @@ void DataArchive::CreateBlockMap() if (header.nfiles == 0) return; - DWORD i,j; - DWORD dir_usage = header.dir_offset + DirBlocks() * Archive::BLOCK_SIZE; - DWORD max_usage = dir_usage; + std::uint32_t i,j; + std::uint32_t dir_usage = header.dir_offset + DirBlocks() * Archive::BLOCK_SIZE; + std::uint32_t max_usage = dir_usage; for (i = 0; i < header.nfiles; i++) { - DWORD last_block = directory[i].offset + FileBlocks(i) * Archive::BLOCK_SIZE; + std::uint32_t last_block = directory[i].offset + FileBlocks(i) * Archive::BLOCK_SIZE; if (last_block > max_usage) max_usage = last_block; } nblocks = max_usage/Archive::BLOCK_SIZE; - block_map = new DWORD[nblocks]; + block_map = new std::uint32_t[nblocks]; if (!block_map) { nblocks = 0; } else { - ZeroMemory(block_map, nblocks*sizeof(DWORD)); + std::memset(block_map, 0, nblocks * sizeof(std::uint32_t)); - DWORD first_block = header.dir_offset/Archive::BLOCK_SIZE + + std::uint32_t first_block = header.dir_offset/Archive::BLOCK_SIZE + (header.dir_offset%Archive::BLOCK_SIZE > 0); for (j = 0; j < DirBlocks(); j++) block_map[first_block+j] = 1; for (i = 0; i < header.nfiles; i++) { - DWORD first_block = directory[i].offset/Archive::BLOCK_SIZE + + std::uint32_t first_block = directory[i].offset/Archive::BLOCK_SIZE + (directory[i].offset%Archive::BLOCK_SIZE > 0); for (j = 0; j < FileBlocks(i); j++) @@ -163,7 +169,7 @@ void DataArchive::CreateBlockMap() int DataArchive::FindDataBlocks(int need) { if ((int) (nblocks)-need > 0) { - DWORD start; + std::uint32_t start; int i; for (start = 0; start < nblocks-need; start++) { @@ -187,8 +193,8 @@ void DataArchive::LoadDatafile(const char* name) delete [] directory; delete [] block_map; - ZeroMemory(this, sizeof(DataArchive)); - strncpy_s(datafile, name, Archive::NAMELEN-1); + std::memset(this, 0, sizeof(DataArchive)); + std::strncpy(datafile, name, Archive::NAMELEN-1); FILE* f; fopen_s(&f, datafile, "rb"); @@ -199,14 +205,14 @@ void DataArchive::LoadDatafile(const char* name) Print("ERROR: datafile '%s' invalid version '%d'\n", datafile, header.version); fclose(f); - ZeroMemory(&header, sizeof(header)); + std::memset(&header, 0, sizeof(header)); return; } - DWORD len = DirBlocks() * Archive::BLOCK_SIZE; - DWORD dirsize = header.nfiles + 64; + uLongf len = DirBlocks() * Archive::BLOCK_SIZE; + std::uint32_t dirsize = header.nfiles + 64; - dirbuf = new BYTE[len]; + dirbuf = new std::uint8_t[len]; directory = new DataEntry[dirsize]; if (!dirbuf || !directory) { @@ -214,16 +220,16 @@ void DataArchive::LoadDatafile(const char* name) } else { - ZeroMemory(directory, sizeof(DataEntry) * dirsize); + std::memset(directory, 0, sizeof(DataEntry) * dirsize); fseek(f, sizeof(DataHeader) + header.dir_offset, SEEK_SET); fread(dirbuf, header.dir_size_comp, 1, f); - int err = uncompress((BYTE*) directory, &len, + int err = uncompress((std::uint8_t*) directory, &len, #pragma warning(suppress: 6029) dirbuf, header.dir_size_comp); if (err != Z_OK) - ZeroMemory(directory, sizeof(DataEntry) * dirsize); + std::memset(directory, 0, sizeof(DataEntry) * dirsize); delete [] dirbuf; dirbuf = 0; @@ -257,7 +263,7 @@ int DataArchive::FindEntry(const char* req_name) char path[256]; int len = strlen(req_name); - ZeroMemory(path, sizeof(path)); + std::memset(path, 0, sizeof(path)); for (int c = 0; c < len; c++) { if (req_name[c] == '\\') @@ -266,7 +272,7 @@ int DataArchive::FindEntry(const char* req_name) path[c] = req_name[c]; } - for (DWORD i = 0; i < header.nfiles; i++) { + for (std::uint32_t i = 0; i < header.nfiles; i++) { if (!_stricmp(directory[i].name, path)) return i; } @@ -277,7 +283,7 @@ int DataArchive::FindEntry(const char* req_name) // +--------------------------------------------------------------------+ -BYTE* DataArchive::CompressEntry(int i) +std::uint8_t* DataArchive::CompressEntry(int i) { if (directory && i >= 0 && i < (int) header.nfiles) { char* name = directory[i].name; @@ -287,10 +293,10 @@ BYTE* DataArchive::CompressEntry(int i) if (f) { fseek(f, 0, SEEK_END); - DWORD len = ftell(f); + std::uint32_t len = ftell(f); fseek(f, 0, SEEK_SET); - BYTE* buf = new BYTE[len]; + std::uint8_t* buf = new std::uint8_t[len]; if (!buf) { err = Z_MEM_ERROR; @@ -303,13 +309,15 @@ BYTE* DataArchive::CompressEntry(int i) directory[i].size_orig = len; directory[i].size_comp = (int) (len * 1.1); - BYTE* cbuf = new BYTE[directory[i].size_comp]; + std::uint8_t* cbuf = new std::uint8_t[directory[i].size_comp]; if (!cbuf) { err = Z_MEM_ERROR; } else { - err = compress(cbuf, &directory[i].size_comp, buf, len); + auto size_comp = static_cast(directory[i].size_comp); + err = compress(cbuf, &size_comp, buf, len); + directory[i].size_comp = static_cast(size_comp); CHECK_ERR(err, "compress"); } @@ -324,17 +332,17 @@ BYTE* DataArchive::CompressEntry(int i) // +--------------------------------------------------------------------+ -int DataArchive::ExpandEntry(int i, BYTE*& buf, bool null_terminate) +int DataArchive::ExpandEntry(int i, std::uint8_t*& buf, bool null_terminate) { - DWORD len = 0; + uLongf len = 0; if (directory && i >= 0 && i < (int) header.nfiles) { FILE* f; fopen_s(&f, datafile, "rb"); if (f) { - DWORD clen = directory[i].size_comp; - BYTE* cbuf = new BYTE[clen]; + std::uint32_t clen = directory[i].size_comp; + std::uint8_t* cbuf = new std::uint8_t[clen]; if (!cbuf) { err = Z_MEM_ERROR; @@ -347,12 +355,12 @@ int DataArchive::ExpandEntry(int i, BYTE*& buf, bool null_terminate) len = directory[i].size_orig; if (null_terminate) { - buf = new BYTE[len+1]; + buf = new std::uint8_t[len+1]; if (buf) buf[len] = 0; } else { - buf = new BYTE[len]; + buf = new std::uint8_t[len]; } if (!buf) { @@ -382,11 +390,11 @@ int DataArchive::InsertEntry(const char* name) { if (name && *name) { char path[256]; - DWORD len = strlen(name); + std::uint32_t len = strlen(name); - ZeroMemory(path, sizeof(path)); + std::memset(path, 0, sizeof(path)); - for (DWORD c = 0; c < len; c++) { + for (std::uint32_t c = 0; c < len; c++) { if (name[c] == '\\') path[c] = '/'; else @@ -398,8 +406,8 @@ int DataArchive::InsertEntry(const char* name) if (directory && dirsize) { for (int i = 0; i < dirsize; i++) { if (directory[i].size_orig == 0) { - ZeroMemory(directory[i].name, Archive::NAMELEN); - strncpy_s(directory[i].name, path, Archive::NAMELEN); + std::memset(directory[i].name, 0, Archive::NAMELEN); + std::strncpy(directory[i].name, path, Archive::NAMELEN); directory[i].name[Archive::NAMELEN-1] = '\0'; directory[i].size_orig = 1; @@ -411,8 +419,8 @@ int DataArchive::InsertEntry(const char* name) DataEntry* dir = new DataEntry[dirsize + 64]; if (directory && dirsize) { - ZeroMemory(dir, (dirsize + 64) * sizeof(DataEntry)); - CopyMemory(dir, directory, dirsize * sizeof(DataEntry)); + std::memset(dir, 0, (dirsize + 64) * sizeof(DataEntry)); + std::memcpy(dir, directory, dirsize * sizeof(DataEntry)); } delete [] directory; @@ -420,8 +428,8 @@ int DataArchive::InsertEntry(const char* name) header.nfiles = dirsize + 64; directory = dir; - ZeroMemory(directory[dirsize].name, Archive::NAMELEN); - strncpy_s(directory[dirsize].name, path, Archive::NAMELEN); + std::memset(directory[dirsize].name, 0, Archive::NAMELEN); + std::strncpy(directory[dirsize].name, path, Archive::NAMELEN); directory[dirsize].name[Archive::NAMELEN-1] = '\0'; directory[dirsize].size_orig = 1; @@ -436,15 +444,15 @@ int DataArchive::InsertEntry(const char* name) void DataArchive::RemoveEntry(int index) { if (directory && index >= 0 && index < (int) header.nfiles) - ZeroMemory(&directory[index], sizeof(DataEntry)); + std::memset(&directory[index], 0, sizeof(DataEntry)); } // +--------------------------------------------------------------------+ void DataArchive::Insert(const char* name) { - DWORD old_blocks = 0, old_offset = 0, new_blocks = 0; - DWORD old_dir_blocks = 0, old_dir_offset = 0, new_dir_blocks = 0; + std::uint32_t old_blocks = 0, old_offset = 0, new_blocks = 0; + std::uint32_t old_dir_blocks = 0, old_dir_offset = 0, new_dir_blocks = 0; int added = 0; int index = FindEntry(name); @@ -477,7 +485,7 @@ void DataArchive::Insert(const char* name) if (verbose) Print(" Inserting: %-16s ", e.name); - BYTE* buf = CompressEntry(index); + std::uint8_t* buf = CompressEntry(index); if (!buf) { // this is (almost) unrecoverable, @@ -518,7 +526,7 @@ void DataArchive::Extract(const char* name) return; } - BYTE* buf; + std::uint8_t* buf; ExpandEntry(index, buf); FILE* f; @@ -567,7 +575,7 @@ void DataArchive::List() printf("Index Orig Size Comp Size Ratio Name\n"); printf("----- --------- --------- ----- ----------------\n"); - for (DWORD i = 0; i < header.nfiles; i++) { + for (std::uint32_t i = 0; i < header.nfiles; i++) { DataEntry& e = directory[i]; int ratio = (int) (100.0 * (double) e.size_comp / (double) e.size_orig); diff --git a/StarsEx/Archive.h b/StarsEx/Archive.h index 1cf76e9..825cc1f 100644 --- a/StarsEx/Archive.h +++ b/StarsEx/Archive.h @@ -12,8 +12,6 @@ #include -#include "Types.h" - // +------------------------------------------------------------------+ namespace Archive @@ -29,11 +27,11 @@ struct DataHeader { static const char* TYPENAME() { return "DataHeader"; } - DWORD version; - DWORD nfiles; - DWORD dir_blocks; - DWORD dir_size_comp; - DWORD dir_offset; + std::uint32_t version; + std::uint32_t nfiles; + std::uint32_t dir_blocks; + std::uint32_t dir_size_comp; + std::uint32_t dir_offset; }; struct DataEntry @@ -41,9 +39,9 @@ struct DataEntry static const char* TYPENAME() { return "DataEntry"; } char name[Archive::NAMELEN]; - DWORD size_orig; - DWORD size_comp; - DWORD offset; + std::uint32_t size_orig; + std::uint32_t size_comp; + std::uint32_t offset; }; class DataArchive @@ -62,19 +60,19 @@ public: void Remove(const char* name); void List(); - void WriteEntry(int index, BYTE* buf); + void WriteEntry(int index, std::uint8_t* buf); int FindEntry(const char* req_name); - int ExpandEntry(int index, BYTE*& buf, bool null_terminate=false); - BYTE* CompressEntry(int index); + int ExpandEntry(int index, std::uint8_t*& buf, bool null_terminate=false); + std::uint8_t* CompressEntry(int index); int InsertEntry(const char* name); void RemoveEntry(int index); - DWORD Blocks(DWORD raw_size); - DWORD DirBlocks(); - DWORD FileBlocks(int index); + std::uint32_t Blocks(std::uint32_t raw_size); + std::uint32_t DirBlocks(); + std::uint32_t FileBlocks(int index); int FindDataBlocks(int blocks_needed); void CreateBlockMap(); - DWORD NumFiles() { return header.nfiles; } + std::uint32_t NumFiles() { return header.nfiles; } DataEntry* GetFile(int i) { if (i>=0 && i<(int)header.nfiles) return &directory[i]; return 0; } const char* Name() const { return datafile; } @@ -83,13 +81,13 @@ private: // persistent data members: DataHeader header; DataEntry* directory; - BYTE* dirbuf; + std::uint8_t* dirbuf; // transient members: char datafile[Archive::NAMELEN]; - DWORD* block_map; - DWORD nblocks; + std::uint32_t* block_map; + std::uint32_t nblocks; }; #endif // Archive_h -- cgit v1.1