Skip to content

Commit

Permalink
make engine.h more robust
Browse files Browse the repository at this point in the history
- use size_t instead of DWORD in the templates
- use standard sized types (uint32_t/uint16_t) instead of DWORD/WORD everywhere else
- do not use std::min
  • Loading branch information
pionere committed Sep 22, 2024
1 parent 2b6b2af commit be2c814
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
41 changes: 22 additions & 19 deletions Source/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ DEVILUTION_BEGIN_NAMESPACE

inline const BYTE* CelGetFrameStart(const BYTE* pCelBuff, int nCel)
{
const DWORD* pFrameTable;
const uint32_t* pFrameTable;

pFrameTable = (const DWORD*)pCelBuff;
pFrameTable = (const uint32_t*)pCelBuff;

return &pCelBuff[SwapLE32(pFrameTable[nCel])];
}

inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize)
{
const DWORD* pFrameTable;
DWORD nCellStart;
const uint32_t* pFrameTable;
uint32_t nCellStart;

pFrameTable = (const DWORD*)&pCelBuff[nCel * 4];
pFrameTable = (const uint32_t*)&pCelBuff[nCel * 4];
nCellStart = SwapLE32(pFrameTable[0]);
*nDataSize = SwapLE32(pFrameTable[1]) - nCellStart;
return &pCelBuff[nCellStart];
}

inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nDataSize)
{
const WORD* pFrameTable;
WORD nDataStart;
const uint16_t* pFrameTable;
uint16_t nDataStart;
const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize);

pFrameTable = (const WORD*)&pRLEBytes[0];
pFrameTable = (const uint16_t*)&pRLEBytes[0];
nDataStart = SwapLE16(pFrameTable[0]);
*nDataSize -= nDataStart;

Expand All @@ -54,11 +54,11 @@ inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nData

inline const BYTE* CelGetFrameClippedAt(const BYTE* pCelBuff, int nCel, int block, int* nDataSize)
{
const WORD* pFrameTable;
WORD nDataStart;
const uint16_t* pFrameTable;
uint16_t nDataStart;
const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize);

pFrameTable = (const WORD*)&pRLEBytes[0];
pFrameTable = (const uint16_t*)&pRLEBytes[0];
nDataStart = SwapLE16(pFrameTable[block]);
// assert(nDataStart != 0);
*nDataSize -= nDataStart;
Expand Down Expand Up @@ -99,14 +99,14 @@ BYTE* LoadFileInMem(const char* pszName, size_t* pdwFileLen = NULL);
void LoadFileWithMem(const char* pszName, BYTE* p);
char** LoadTxtFile(const char* name, int lines);

/* Load .CEL file and overwrite the first (unused) DWORD with nWidth */
inline CelImageBuf* CelLoadImage(const char* name, DWORD nWidth)
/* Load .CEL file and overwrite the first (unused) uint32_t with nWidth */
inline CelImageBuf* CelLoadImage(const char* name, uint32_t nWidth)
{
CelImageBuf* res;

res = (CelImageBuf*)LoadFileInMem(name);
#if DEBUG_MODE
res->ciFrameCnt = SwapLE32(*((DWORD*)res));
res->ciFrameCnt = SwapLE32(*((uint32_t*)res));
#endif
res->ciWidth = nWidth;
return res;
Expand Down Expand Up @@ -157,7 +157,7 @@ inline int RandRangeLow(int minVal, int maxVal)
* Copy string from src to dest.
* The NULL terminated content of src is copied to dest.
*/
template <DWORD N1, DWORD N2>
template <size_t N1, size_t N2>
inline void copy_str(char (&dest)[N1], char (&src)[N2])
{
static_assert(N1 >= N2, "String does not fit the destination.");
Expand All @@ -170,11 +170,13 @@ inline void copy_str(char (&dest)[N1], char (&src)[N2])
* Copy constant string from src to dest.
* The whole (padded) length of the src array is copied.
*/
template <DWORD N1, DWORD N2>
template <size_t N1, size_t N2>
inline void copy_cstr(char (&dest)[N1], const char (&src)[N2])
{
static_assert(N1 >= N2, "String does not fit the destination.");
memcpy(dest, src, std::min(N1, (DWORD)(((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int))));
constexpr size_t src_len = ((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int);
constexpr size_t len = N1 >= src_len ? src_len : N1;
memcpy(dest, src, len);
}

/*
Expand All @@ -201,13 +203,14 @@ inline void copy_pod(T& dest, const T& src)
template <int N>
inline void cat_str(char (&dest)[N], int& pos, const char* fmt, ...)
{
int n;
int n, res;
va_list va;

va_start(va, fmt);

n = N - pos;
pos += std::min(vsnprintf(&dest[pos], n, fmt, va), n - 1);
res = vsnprintf(&dest[pos], n, fmt, va);
pos += (res <= n - 1) ? res : n - 1;

va_end(va);
}
Expand Down
41 changes: 22 additions & 19 deletions tools/patcher/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ DEVILUTION_BEGIN_NAMESPACE

inline const BYTE* CelGetFrameStart(const BYTE* pCelBuff, int nCel)
{
const DWORD* pFrameTable;
const uint32_t* pFrameTable;

pFrameTable = (const DWORD*)pCelBuff;
pFrameTable = (const uint32_t*)pCelBuff;

return &pCelBuff[SwapLE32(pFrameTable[nCel])];
}

inline const BYTE* CelGetFrame(const BYTE* pCelBuff, int nCel, int* nDataSize)
{
const DWORD* pFrameTable;
DWORD nCellStart;
const uint32_t* pFrameTable;
uint32_t nCellStart;

pFrameTable = (const DWORD*)&pCelBuff[nCel * 4];
pFrameTable = (const uint32_t*)&pCelBuff[nCel * 4];
nCellStart = SwapLE32(pFrameTable[0]);
*nDataSize = SwapLE32(pFrameTable[1]) - nCellStart;
return &pCelBuff[nCellStart];
}

inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nDataSize)
{
const WORD* pFrameTable;
WORD nDataStart;
const uint16_t* pFrameTable;
uint16_t nDataStart;
const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize);

pFrameTable = (const WORD*)&pRLEBytes[0];
pFrameTable = (const uint16_t*)&pRLEBytes[0];
nDataStart = SwapLE16(pFrameTable[0]);
*nDataSize -= nDataStart;

Expand All @@ -55,11 +55,11 @@ inline const BYTE* CelGetFrameClipped(const BYTE* pCelBuff, int nCel, int* nData

inline const BYTE* CelGetFrameClippedAt(const BYTE* pCelBuff, int nCel, int block, int* nDataSize)
{
const WORD* pFrameTable;
WORD nDataStart;
const uint16_t* pFrameTable;
uint16_t nDataStart;
const BYTE* pRLEBytes = CelGetFrame(pCelBuff, nCel, nDataSize);

pFrameTable = (const WORD*)&pRLEBytes[0];
pFrameTable = (const uint16_t*)&pRLEBytes[0];
nDataStart = SwapLE16(pFrameTable[block]);
// assert(nDataStart != 0);
*nDataSize -= nDataStart;
Expand Down Expand Up @@ -88,14 +88,14 @@ BYTE* LoadFileInMem(const char* pszName, size_t* pdwFileLen = NULL);
void LoadFileWithMem(const char* pszName, BYTE* p);
char** LoadTxtFile(const char* name, int lines);

/* Load .CEL file and overwrite the first (unused) DWORD with nWidth */
inline CelImageBuf* CelLoadImage(const char* name, DWORD nWidth)
/* Load .CEL file and overwrite the first (unused) uint32_t with nWidth */
inline CelImageBuf* CelLoadImage(const char* name, uint32_t nWidth)
{
CelImageBuf* res;

res = (CelImageBuf*)LoadFileInMem(name);
#if DEBUG_MODE
res->ciFrameCnt = SwapLE32(*((DWORD*)res));
res->ciFrameCnt = SwapLE32(*((uint32_t*)res));
#endif
res->ciWidth = nWidth;
return res;
Expand Down Expand Up @@ -134,7 +134,7 @@ BYTE* CelMerge(BYTE* celA, size_t nDataSizeA, BYTE* celB, size_t nDataSizeB);
* Copy string from src to dest.
* The NULL terminated content of src is copied to dest.
*/
template <DWORD N1, DWORD N2>
template <size_t N1, size_t N2>
inline void copy_str(char (&dest)[N1], char (&src)[N2])
{
static_assert(N1 >= N2, "String does not fit the destination.");
Expand All @@ -147,11 +147,13 @@ inline void copy_str(char (&dest)[N1], char (&src)[N2])
* Copy constant string from src to dest.
* The whole (padded) length of the src array is copied.
*/
template <DWORD N1, DWORD N2>
template <size_t N1, size_t N2>
inline void copy_cstr(char (&dest)[N1], const char (&src)[N2])
{
static_assert(N1 >= N2, "String does not fit the destination.");
memcpy(dest, src, std::min(N1, (DWORD)(((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int))));
constexpr size_t src_len = ((N2 + sizeof(int) - 1) / sizeof(int)) * sizeof(int);
constexpr size_t len = N1 >= src_len ? src_len : N1;
memcpy(dest, src, len);
}

/*
Expand All @@ -178,13 +180,14 @@ inline void copy_pod(T& dest, const T& src)
template <int N>
inline void cat_str(char (&dest)[N], int& pos, const char* fmt, ...)
{
int n;
int n, res;
va_list va;

va_start(va, fmt);

n = N - pos;
pos += std::min(vsnprintf(&dest[pos], n, fmt, va), n - 1);
res = vsnprintf(&dest[pos], n, fmt, va);
pos += (res <= n - 1) ? res : n - 1;

va_end(va);
}
Expand Down

0 comments on commit be2c814

Please sign in to comment.