From 3e17cbdb17cd6d4628a04fc6bbeb7db2bf530396 Mon Sep 17 00:00:00 2001 From: mcmartin Date: Sat, 3 May 2003 07:10:12 +0000 Subject: [PATCH] Recoded the DRAWABLE_DESC datatype so that it no longer relies on "intentional buffer overflows". git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@942 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 3 + sc2/src/sc2code/libs/gfxlib.h | 1 - sc2/src/sc2code/libs/graphics/drawable.c | 84 +++++++------------ sc2/src/sc2code/libs/graphics/drawable.h | 15 ++-- sc2/src/sc2code/libs/graphics/sdl/3do_blt.c | 2 +- .../sc2code/libs/graphics/sdl/3do_getbody.c | 2 +- sc2/src/sc2code/libs/graphics/tfb_prim.c | 10 +-- 7 files changed, 47 insertions(+), 70 deletions(-) diff --git a/sc2/ChangeLog b/sc2/ChangeLog index b461710a0..88f4a91e7 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,7 @@ Changes towards version 0.3: +- The DRAWABLE_DESC datatype now uses separately allocated arrays for + animation frames instead of doing pointer arithmetic between it and + FRAME_DESCs --McMartin - Date on the green bar now has floating period between day and the year like in PC version (bug #307 part 5) -Mika - AWARE_OF_SAMATRA flag is now written as well as read (closes #113), diff --git a/sc2/src/sc2code/libs/gfxlib.h b/sc2/src/sc2code/libs/gfxlib.h index 9e4521a8a..375bca842 100644 --- a/sc2/src/sc2code/libs/gfxlib.h +++ b/sc2/src/sc2code/libs/gfxlib.h @@ -256,7 +256,6 @@ extern DRAWABLE CreateDisplay (CREATE_FLAGS CreateFlags, PSIZE pwidth, PSIZE pheight); extern DRAWABLE CreateDrawable (CREATE_FLAGS CreateFlags, SIZE width, SIZE height, COUNT num_frames); -extern DRAWABLE CopyDrawable (DRAWABLE Drawable); extern BOOLEAN DestroyDrawable (DRAWABLE Drawable); extern BOOLEAN GetFrameRect (FRAME Frame, PRECT pRect); diff --git a/sc2/src/sc2code/libs/graphics/drawable.c b/sc2/src/sc2code/libs/graphics/drawable.c index e76417edf..8b11f2859 100644 --- a/sc2/src/sc2code/libs/graphics/drawable.c +++ b/sc2/src/sc2code/libs/graphics/drawable.c @@ -17,6 +17,7 @@ */ #include "gfxintrn.h" +#include "misc.h" FRAMEPTR _CurFramePtr; @@ -77,6 +78,36 @@ CreateDisplay (CREATE_FLAGS CreateFlags, PSIZE pwidth, PSIZE pheight) return (0); } +DRAWABLE +AllocDrawable (COUNT n) +{ + DRAWABLE Drawable; + Drawable = (DRAWABLE)mem_allocate ((MEM_SIZE)(sizeof (DRAWABLE_DESC)), + MEM_ZEROINIT | MEM_GRAPHICS, + DRAWABLE_PRIORITY, MEM_SIMPLE); + if (Drawable) + { + DRAWABLEPTR DrawablePtr; + int i; + DrawablePtr = LockDrawable (Drawable); + DrawablePtr->Frame = (FRAMEPTR)HMalloc ((MEM_SIZE)(sizeof (FRAME_DESC) * n)); + + /* Zero out the newly allocated frames, since HMalloc doesn't have MEM_ZEROINIT. */ + for (i = 0; i < n; i++) { + FRAMEPTR F; + F = &DrawablePtr->Frame[i]; + F->parent = DrawablePtr; + F->TypeIndexAndFlags = 0; + F->image = 0; + F->Bounds = 0; + F->HotSpot.x = F->HotSpot.y = 0; + } + + UnlockDrawable (Drawable); + } + return Drawable; +} + DRAWABLE CreateDrawable (CREATE_FLAGS CreateFlags, SIZE width, SIZE height, COUNT num_frames) @@ -107,58 +138,6 @@ CreateDrawable (CREATE_FLAGS CreateFlags, SIZE width, SIZE height, COUNT return (0); } -DRAWABLE -CopyDrawable (DRAWABLE Drawable) -{ - DRAWABLEPTR DrawablePtr; - - DrawablePtr = LockDrawable (Drawable); - if (DrawablePtr) - { - DRAWABLE CopyDrawable; - DWORD size; - - if (TYPE_GET (DrawablePtr->Frame[0].TypeIndexAndFlags) == SCREEN_DRAWABLE) - CopyDrawable = 0; - else if ((CopyDrawable = AllocDrawable (1, - (size = mem_get_size ((MEM_HANDLE)Drawable)) - - sizeof (DRAWABLE_DESC)))) - { - DRAWABLEPTR CopyDrawablePtr; - - if ((CopyDrawablePtr = LockDrawable (CopyDrawable)) == 0) - { - FreeDrawable (CopyDrawable); - CopyDrawable = 0; - } - else - { - PBYTE lpDst, lpSrc; - - lpDst = (PBYTE)CopyDrawablePtr; - lpSrc = (PBYTE)DrawablePtr; - do - { - COUNT num_bytes; - - num_bytes = size >= 0x7FFF ? 0x7FFF : (COUNT)size; - memcpy (lpDst, lpSrc, num_bytes); - lpDst += num_bytes; - lpSrc += num_bytes; - size -= num_bytes; - } while (size); - CopyDrawablePtr->hDrawable = (MEM_HANDLE)CopyDrawable; - UnlockDrawable (CopyDrawable); - } - } - UnlockDrawable (Drawable); - - return (CopyDrawable); - } - - return (0); -} - BOOLEAN DestroyDrawable (DRAWABLE Drawable) { @@ -170,6 +149,7 @@ DestroyDrawable (DRAWABLE Drawable) DrawablePtr = LockDrawable (Drawable); if (DrawablePtr) { + HFree (DrawablePtr->Frame); UnlockDrawable (Drawable); FreeDrawable (Drawable); diff --git a/sc2/src/sc2code/libs/graphics/drawable.h b/sc2/src/sc2code/libs/graphics/drawable.h index 2800cc7ff..acf576543 100644 --- a/sc2/src/sc2code/libs/graphics/drawable.h +++ b/sc2/src/sc2code/libs/graphics/drawable.h @@ -69,15 +69,16 @@ typedef struct HOT_SPOT HotSpot; DWORD Bounds; TFB_Image *image; + struct _drawable_desc *parent; } FRAME_DESC; typedef FRAME_DESC *PFRAME_DESC; -typedef struct +typedef struct _drawable_desc { MEM_HANDLE hDrawable; UWORD FlagsAndIndex; - FRAME_DESC Frame[1]; + FRAME_DESC *Frame; } DRAWABLE_DESC; typedef DRAWABLE_DESC *PDRAWABLE_DESC; @@ -94,19 +95,13 @@ typedef DRAWABLE_DESC *PDRAWABLE_DESC; #define FRAMEPTR PFRAME_DESC #define COUNTPTR PCOUNT -#define AllocDrawable(n,dc) \ - (DRAWABLE)mem_allocate ((MEM_SIZE)(sizeof (DRAWABLE_DESC) \ - + (sizeof (FRAME_DESC) * ((n) - 1))) + (dc), \ - MEM_ZEROINIT | MEM_GRAPHICS, \ - DRAWABLE_PRIORITY, MEM_SIMPLE) +extern DRAWABLE AllocDrawable (COUNT num_frames); #define LockDrawable(D) ((DRAWABLEPTR)mem_lock (GetDrawableHandle (D))) #define UnlockDrawable(D) mem_unlock (GetDrawableHandle (D)) #define FreeDrawable(D) _ReleaseCelData (GetDrawableHandle (D)) #define GetDrawableHandle(D) ((MEM_HANDLE)LOWORD (D)) #define GetDrawableIndex(D) ((COUNT)HIWORD (D)) -#define GetFrameParentDrawable(F) ((DRAWABLEPTR)((PBYTE)((F) \ - -((int)INDEX_GET((F)->TypeIndexAndFlags)-1)) \ - -sizeof(DRAWABLE_DESC))) +#define GetFrameParentDrawable(F) (F)->parent #define NULL_DRAWABLE (DRAWABLE)NULL_PTR diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c index 31f3f8265..022b6f734 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c @@ -62,7 +62,7 @@ alloc_image (COUNT NumFrames, DRAWABLE_TYPE DrawableType, CREATE_FLAGS (void)flags; (void)width; (void)height; - return AllocDrawable (NumFrames, 0); + return AllocDrawable (NumFrames); } static DISPLAY_INTERFACE DisplayInterface = diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c b/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c index f3f7653cc..d2aaddae9 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c @@ -461,7 +461,7 @@ _GetCelData (FILE *fp, DWORD length) #endif Drawable = 0; - if (cel_ct && (Drawable = AllocDrawable (cel_ct, 0))) + if (cel_ct && (Drawable = AllocDrawable (cel_ct))) { DRAWABLEPTR DrawablePtr; diff --git a/sc2/src/sc2code/libs/graphics/tfb_prim.c b/sc2/src/sc2code/libs/graphics/tfb_prim.c index 6165b6185..ba603f7db 100644 --- a/sc2/src/sc2code/libs/graphics/tfb_prim.c +++ b/sc2/src/sc2code/libs/graphics/tfb_prim.c @@ -36,7 +36,7 @@ TFB_Prim_Point (PPOINT p, TFB_Palette *color) r.corner.y = p->y - _CurFramePtr->HotSpot.y; r.extent.width = r.extent.height = 1; - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) + if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) & SCREEN_DRAWABLE) TFB_DrawScreen_Rect (&r, color->r, color->g, color->b, TFB_SCREEN_MAIN); else TFB_DrawImage_Rect (&r, color->r, color->g, color->b, _CurFramePtr->image); @@ -86,7 +86,7 @@ TFB_Prim_FillRect (PRECT r, TFB_Palette *color) rect.extent.height) >> 1; } - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) + if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) & SCREEN_DRAWABLE) TFB_DrawScreen_Rect (&rect, color->r, color->g, color->b, TFB_SCREEN_MAIN); else TFB_DrawImage_Rect (&rect, color->r, color->g, color->b, _CurFramePtr->image); @@ -102,7 +102,7 @@ TFB_Prim_Line (PLINE line, TFB_Palette *color) x2=line->second.x - _CurFramePtr->HotSpot.x; y2=line->second.y - _CurFramePtr->HotSpot.y; - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) + if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) & SCREEN_DRAWABLE) TFB_DrawScreen_Line (x1, y1, x2, y2, color->r, color->g, color->b, TFB_SCREEN_MAIN); else TFB_DrawImage_Line (x1, y1, x2, y2, color->r, color->g, color->b, _CurFramePtr->image); @@ -155,7 +155,7 @@ TFB_Prim_Stamp (PSTAMP stmp) UnlockMutex (img->mutex); - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) + if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) & SCREEN_DRAWABLE) { TFB_DrawScreen_Image (img, x, y, gscale, (paletted ? palette : NULL), TFB_SCREEN_MAIN); @@ -209,7 +209,7 @@ TFB_Prim_StampFill (PSTAMP stmp, TFB_Palette *color) UnlockMutex (img->mutex); - if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) + if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) & SCREEN_DRAWABLE) { TFB_DrawScreen_FilledImage (img, x, y, gscale, r, g, b, TFB_SCREEN_MAIN);