From 2ec11ad1fd50098a789a9b477bb9c4240f569e7e Mon Sep 17 00:00:00 2001 From: Ladislav Zezula Date: Sat, 2 Nov 2024 09:18:37 +0100 Subject: Added functions for conversions between MPQ file name <-> Safe file name --- src/StormCommon.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'src/StormCommon.h') diff --git a/src/StormCommon.h b/src/StormCommon.h index c050093..1e67a27 100644 --- a/src/StormCommon.h +++ b/src/StormCommon.h @@ -154,14 +154,15 @@ extern LCID g_lcFileLocale; // Preferred file locale and pla //----------------------------------------------------------------------------- // Conversion to uppercase/lowercase (and "/" to "\") -extern unsigned char AsciiToLowerTable[256]; -extern unsigned char AsciiToUpperTable[256]; +extern const unsigned char AsciiToLowerTable[256]; +extern const unsigned char AsciiToUpperTable[256]; +extern const unsigned char SMemCharToByte[0x80]; //----------------------------------------------------------------------------- // Safe string functions template -XCHAR * IntToString(XCHAR * szBuffer, size_t cchMaxChars, XINT nValue, size_t nDigitCount = 0) +XCHAR * SMemIntToStr(XCHAR * szBuffer, size_t cchMaxChars, XINT nValue, size_t nDigitCount = 0) { XCHAR * szBufferEnd = szBuffer + cchMaxChars - 1; XCHAR szNumberRev[0x20]; @@ -197,6 +198,72 @@ XCHAR * IntToString(XCHAR * szBuffer, size_t cchMaxChars, XINT nValue, size_t nD return szBuffer; } +template +DWORD SMemBinToStr(XCHAR * szBuffer, size_t cchBuffer, const void * pvBinary, size_t cbBinary) +{ + const unsigned char * pbBinary = (const unsigned char *)pvBinary; + const char * SMemIntToHex = "0123456789abcdef"; + + // The size of the string must be enough to hold the binary + EOS + if(cchBuffer < ((cbBinary * 2) + 1)) + return ERROR_INSUFFICIENT_BUFFER; + + // Convert the string to the array of MD5 + // Copy the blob data as text + for(size_t i = 0; i < cbBinary; i++) + { + *szBuffer++ = SMemIntToHex[pbBinary[0] >> 0x04]; + *szBuffer++ = SMemIntToHex[pbBinary[0] & 0x0F]; + pbBinary++; + } + + // Terminate the string + *szBuffer = 0; + return ERROR_SUCCESS; +} + +template +DWORD SMemStrToBin(const XCHAR * szString, void * pvBinary, size_t cbBinary, size_t * PtrBinary = NULL) +{ + LPBYTE pbBinary = (LPBYTE)pvBinary; + LPBYTE pbBinaryEnd = pbBinary + cbBinary; + LPBYTE pbSaveBinary = pbBinary; + + // Verify parameter + if(szString != NULL && szString[0] != 0) + { + // Work as long as we have at least 2 characters ready + while(szString[0] != 0 && szString[1] != 0) + { + // Convert both to unsigned char to get rid of negative indexes produced by szString[x] + BYTE StringByte0 = (BYTE)szString[0]; + BYTE StringByte1 = (BYTE)szString[1]; + + // Each character must be within the range of 0x80 + if(StringByte0 > 0x80 || StringByte1 > 0x80) + return ERROR_INVALID_PARAMETER; + if(SMemCharToByte[StringByte0] == 0xFF || SMemCharToByte[StringByte1] == 0xFF) + return ERROR_INVALID_PARAMETER; + + // Overflow check + if(pbBinary >= pbBinaryEnd) + return ERROR_INSUFFICIENT_BUFFER; + + *pbBinary++ = (SMemCharToByte[StringByte0] << 0x04) | SMemCharToByte[StringByte1]; + szString += 2; + } + + // Odd number of chars? + if(szString[0] != 0 && szString[1] == 0) + return ERROR_INVALID_PARAMETER; + } + + // Give the length + if(PtrBinary != NULL) + PtrBinary[0] = pbBinary - pbSaveBinary; + return ERROR_SUCCESS; +} + char * StringCopy(char * szTarget, size_t cchTarget, const char * szSource); void StringCat(char * szTarget, size_t cchTargetMax, const char * szSource); void StringCreatePseudoFileName(char * szBuffer, size_t cchMaxChars, unsigned int nIndex, const char * szExtension); -- cgit v1.2.3