From 8af652c4e144744130aa0fef481d13ff399b9ef7 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Sat, 11 May 2019 15:49:48 +0200 Subject: [PATCH] Clean up mpqapi_parse_archive_header --- Source/mpqapi.cpp | 70 ++++++++++++++++++++++------------------------- Source/mpqapi.h | 2 +- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/Source/mpqapi.cpp b/Source/mpqapi.cpp index 1834e6c4d..abdc8c89a 100644 --- a/Source/mpqapi.cpp +++ b/Source/mpqapi.cpp @@ -545,47 +545,45 @@ BOOL mpqapi_open_archive(const char *pszArchive, BOOL hidden, int dwChar) // Ope // 65AB14: using guessed type char save_archive_open; // 679660: using guessed type char gbMaxPlayers; -BOOLEAN mpqapi_parse_archive_header(_FILEHEADER *pHdr, int *pdwNextFileStart) // ParseMPQHeader +BOOL mpqapi_parse_archive_header(_FILEHEADER *pHdr, int *pdwNextFileStart) // ParseMPQHeader { - int *v2; // ebp - _FILEHEADER *v3; // esi - DWORD v4; // eax - DWORD v5; // edi - DWORD NumberOfBytesRead; // [esp+10h] [ebp-4h] - - v2 = pdwNextFileStart; - v3 = pHdr; - v4 = GetFileSize(sghArchive, 0); - v5 = v4; - *v2 = v4; - if (v4 == -1 - || v4 < 0x68 - || !ReadFile(sghArchive, v3, 0x68u, &NumberOfBytesRead, NULL) + DWORD size; + DWORD NumberOfBytesRead; + + size = GetFileSize(sghArchive, 0); + *pdwNextFileStart = size; + + if (size == -1 + || size < 0x68 + || !ReadFile(sghArchive, pHdr, 0x68u, &NumberOfBytesRead, NULL) || NumberOfBytesRead != 104 - || v3->signature != '\x1AQPM' - || v3->headersize != 32 - || v3->version > 0u - || v3->sectorsizeid != 3 - || v3->filesize != v5 - || v3->hashoffset != 32872 - || v3->blockoffset != 104 - || v3->hashcount != 2048 - || v3->blockcount != 2048) { - if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1 || !SetEndOfFile(sghArchive)) - return 0; - memset(v3, 0, 0x68u); - v3->signature = '\x1AQPM'; - v3->headersize = 32; - v3->sectorsizeid = 3; - v3->version = 0; - *v2 = 0x10068; + || pHdr->signature != '\x1AQPM' + || pHdr->headersize != 32 + || pHdr->version > 0u + || pHdr->sectorsizeid != 3 + || pHdr->filesize != size + || pHdr->hashoffset != 32872 + || pHdr->blockoffset != 104 + || pHdr->hashcount != 2048 + || pHdr->blockcount != 2048) { + + if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1) + return FALSE; + if (!SetEndOfFile(sghArchive)) + return FALSE; + + memset(pHdr, 0, 0x68u); + pHdr->signature = '\x1AQPM'; + pHdr->headersize = 32; + pHdr->sectorsizeid = 3; + pHdr->version = 0; + *pdwNextFileStart = 0x10068; save_archive_modified = 1; save_archive_open = 1; } - return 1; + + return TRUE; } -// 65AB0C: using guessed type int save_archive_modified; -// 65AB14: using guessed type char save_archive_open; void mpqapi_close_archive(const char *pszArchive, BOOL bFree, int dwChar) // CloseMPQ { @@ -606,8 +604,6 @@ void mpqapi_close_archive(const char *pszArchive, BOOL bFree, int dwChar) // Clo mpqapi_store_creation_time(pszArchive, dwChar); } } -// 65AB0C: using guessed type int save_archive_modified; -// 65AB14: using guessed type char save_archive_open; void mpqapi_store_modified_time(const char *pszArchive, int dwChar) { diff --git a/Source/mpqapi.h b/Source/mpqapi.h index 43c0b747b..aeb0ab5f0 100644 --- a/Source/mpqapi.h +++ b/Source/mpqapi.h @@ -26,7 +26,7 @@ int mpqapi_find_free_block(int size, int *block_size); void mpqapi_rename(char *pszOld, char *pszNew); BOOL mpqapi_has_file(const char *pszName); BOOL mpqapi_open_archive(const char *pszArchive, BOOL hidden, int dwChar); -BOOLEAN mpqapi_parse_archive_header(_FILEHEADER *pHdr, int *pdwNextFileStart); +BOOL mpqapi_parse_archive_header(_FILEHEADER *pHdr, int *pdwNextFileStart); void mpqapi_close_archive(const char *pszArchive, BOOL bFree, int dwChar); void mpqapi_store_modified_time(const char *pszArchive, int dwChar); void mpqapi_flush_and_close(const char *pszArchive, BOOL bFree, int dwChar);