Make calculations clearer and move defines to r_picformats.h

This commit is contained in:
Lactozilla 2025-01-28 23:29:11 -03:00
parent d2f3c303aa
commit ff3f240d58
4 changed files with 20 additions and 12 deletions

View file

@ -910,10 +910,6 @@ typedef struct
#pragma pack()
#endif
#define MAX_PATCH_DIMENSIONS 8192
#define VALID_PATCH_LUMP_SIZE(lumplen, width) ((lumplen) >= (sizeof(INT16) * 4) + ((width) * sizeof(INT32)))
// Possible alpha types for a patch.
enum patchalphastyle {AST_COPY, AST_TRANSLUCENT, AST_ADD, AST_SUBTRACT, AST_REVERSESUBTRACT, AST_MODULATE, AST_OVERLAY, AST_FOG};

View file

@ -812,7 +812,7 @@ boolean Picture_IsFlatFormat(pictureformat_t format)
boolean Picture_CheckIfDoomPatch(softwarepatch_t *patch, size_t size)
{
// Does not meet minimum size requirements
if (size < PATCH_MIN_SIZE)
if (size < MIN_PATCH_LUMP_SIZE)
return false;
INT16 width = SHORT(patch->width);
@ -835,7 +835,7 @@ boolean Picture_CheckIfDoomPatch(softwarepatch_t *patch, size_t size)
UINT32 ofs = LONG(patch->columnofs[x]);
// Need one byte for an empty column (but there's patches that don't know that!)
if (ofs < ((sizeof(INT16) * 4) + (width * sizeof(INT32))) || ofs >= (UINT32)size)
if (ofs < FIRST_PATCH_LUMP_COLUMN(width) || (size_t)ofs >= size)
{
return false;
}

View file

@ -55,9 +55,21 @@ enum
PICDEPTH_32BPP = 32
};
// Minimum length of a valid Doom patch
// Maximum allowed dimensions for a patch
#define MAX_PATCH_DIMENSIONS 8192
// Minimum amount of bytes required for a valid patch lump header
#define MIN_PATCH_LUMP_HEADER_SIZE ((sizeof(INT16) * 4) + sizeof(INT32))
// Minimum length of a valid Doom patch lump
// This is the size of a 1x1 patch.
#define PATCH_MIN_SIZE ((sizeof(INT16) * 4) + (sizeof(INT32)) + 1)
#define MIN_PATCH_LUMP_SIZE (MIN_PATCH_LUMP_HEADER_SIZE + 1)
// Gets the offset to the very first column in a patch lump
#define FIRST_PATCH_LUMP_COLUMN(width) ((sizeof(INT16) * 4) + ((width) * sizeof(INT32)))
// Checks if the size of a lump is valid for a patch, given a certain width
#define VALID_PATCH_LUMP_SIZE(lumplen, width) ((lumplen) >= FIRST_PATCH_LUMP_COLUMN(width))
// Minimum size of a PNG file.
// See: https://web.archive.org/web/20230524232139/http://garethrees.org/2007/11/14/pngcrush/

View file

@ -1700,7 +1700,7 @@ lumpnum_t W_GetNumForLongName(const char *name)
// in its entirety.
static boolean W_IsProbablyValidPatch(UINT16 wadnum, UINT16 lumpnum)
{
UINT8 header[PATCH_MIN_SIZE];
UINT8 header[MIN_PATCH_LUMP_HEADER_SIZE];
I_StaticAssert(sizeof(header) >= PNG_HEADER_SIZE);
@ -1708,7 +1708,7 @@ static boolean W_IsProbablyValidPatch(UINT16 wadnum, UINT16 lumpnum)
size_t lumplen = W_LumpLengthPwad(wadnum, lumpnum);
// Cannot be a valid Doom patch
if (lumplen < sizeof(header))
if (lumplen < MIN_PATCH_LUMP_SIZE)
return false;
// Check if it's probably a valid PNG
@ -1726,7 +1726,7 @@ static boolean W_IsProbablyValidPatch(UINT16 wadnum, UINT16 lumpnum)
// Otherwise, we read it as a patch
}
// Read the first 12 bytes, plus one
// Read the first 12 bytes
W_ReadLumpHeaderPwad(wadnum, lumpnum, header, sizeof(header), 0);
softwarepatch_t patch;
@ -1746,7 +1746,7 @@ static boolean W_IsProbablyValidPatch(UINT16 wadnum, UINT16 lumpnum)
UINT32 ofs = LONG(patch.columnofs[0]);
// Need one byte for an empty column (but there's patches that don't know that!)
if (ofs < ((sizeof(INT16) * 4) + (width * sizeof(INT32))) || ofs >= (UINT32)lumplen)
if (ofs < FIRST_PATCH_LUMP_COLUMN(width) || (size_t)ofs >= lumplen)
{
return false;
}