NtGdiDdQueryDirectDrawObject do not setup wrong callback to struct members. Check for some NULL parama that can not be NULL.
Modified: trunk/reactos/subsys/win32k/ntddraw/ddraw.c

Modified: trunk/reactos/subsys/win32k/ntddraw/ddraw.c
--- trunk/reactos/subsys/win32k/ntddraw/ddraw.c	2005-12-11 23:45:44 UTC (rev 20093)
+++ trunk/reactos/subsys/win32k/ntddraw/ddraw.c	2005-12-11 23:53:23 UTC (rev 20094)
@@ -13,6 +13,10 @@
 #define NDEBUG
 #include <debug.h>
 
+/* swtich this off to get rid of all dx debug msg */
+#define DX_DEBUG
+
+
 /************************************************************************/
 /* DIRECT DRAW OBJECT                                                   */
 /************************************************************************/
@@ -21,7 +25,9 @@
 DD_Cleanup(PVOID ObjectBody)
 {       
 	PDD_DIRECTDRAW pDirectDraw = (PDD_DIRECTDRAW) ObjectBody;
+#ifdef DX_DEBUG
 	DPRINT1("DD_Cleanup\n");
+#endif
 	
 	if (!pDirectDraw)
 		return FALSE;
@@ -43,7 +49,9 @@
 	DD_CALLBACKS callbacks;
 	DD_SURFACECALLBACKS surface_callbacks;
 	DD_PALETTECALLBACKS palette_callbacks;
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdCreateDirectDrawObject\n");
+#endif
 
 	RtlZeroMemory(&callbacks, sizeof(DD_CALLBACKS));
 	callbacks.dwSize = sizeof(DD_CALLBACKS);
@@ -75,7 +83,9 @@
 
 	if (!success)
 	{
+#ifdef DX_DEBUG
         DPRINT1("DirectDraw creation failed\n"); 
+#endif
 		// DirectDraw creation failed
 		DC_UnlockDc(pDC);
 		return NULL;
@@ -85,7 +95,9 @@
 	if (!hDirectDraw)
 	{
 		/* No more memmory */
+#ifdef DX_DEBUG
 		DPRINT1("No more memmory\n"); 
+#endif
 		DC_UnlockDc(pDC);
 		return NULL;
 	}
@@ -94,7 +106,9 @@
 	if (!pDirectDraw)
 	{
 		/* invalid handle */
+#ifdef DX_DEBUG
 		DPRINT1("invalid handle\n"); 
+#endif
 		DC_UnlockDc(pDC);
 		return NULL;
 	}
@@ -125,13 +139,15 @@
     HANDLE hDirectDrawLocal
 )
 {
+#ifdef DX_DEBUG
     DPRINT1("NtGdiDdDeleteDirectDrawObject\n");
+#endif
 	return GDIOBJ_FreeObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
 }
 
 BOOL STDCALL NtGdiDdQueryDirectDrawObject(
     HANDLE hDirectDrawLocal,
-    DD_HALINFO *pHalInfo,
+    DD_HALINFO  *pHalInfo,
     DWORD *pCallBackFlags,
     LPD3DNTHAL_CALLBACKS puD3dCallbacks,
     LPD3DNTHAL_GLOBALDRIVERDATA puD3dDriverData,
@@ -143,13 +159,51 @@
     DWORD *puFourCC
 )
 {
+#ifdef DX_DEBUG
+    DPRINT1("NtGdiDdQueryDirectDrawObject\n");
+#endif
+
+    /* Check for NULL pointer to prevent any one doing a mistake */
+
+    if (hDirectDrawLocal == NULL)
+    {
+#ifdef DX_DEBUG
+       DPRINT1("warning hDirectDraw handler is NULL, the handler is  DDRAWI_DIRECTDRAW_GBL.hDD\n");
+       DPRINT1("and it is NtGdiDdCreateDirectDrawObject return value\n");
+#endif
+       return FALSE;
+    }
+
+
+    if (pHalInfo == NULL)
+    {
+#ifdef DX_DEBUG
+       DPRINT1("warning pHalInfo buffer is NULL \n");
+#endif
+       return FALSE;
+    }
+
+    if ( pCallBackFlags == NULL)
+    {
+#ifdef DX_DEBUG
+       DPRINT1("warning pCallBackFlags s NULL, the size must be 3*DWORD in follow order \n");
+       DPRINT1("pCallBackFlags[0] = flags in DD_CALLBACKS\n");
+       DPRINT1("pCallBackFlags[1] = flags in DD_SURFACECALLBACKS\n");
+       DPRINT1("pCallBackFlags[2] = flags in DD_PALETTECALLBACKS\n");
+#endif
+       return FALSE;
+    }
+   
+    
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
-	DPRINT1("NtGdiDdQueryDirectDrawObject\n");
 	
+	
 	if (!pDirectDraw)
 	{
         /* Fail to Lock DirectDraw handle */
+#ifdef DX_DEBUG
         DPRINT1(" Fail to Lock DirectDraw handle \n");        
+#endif
 		return FALSE;
     }
 
@@ -163,38 +217,133 @@
 
 	if (!success)
 	{
+#ifdef DX_DEBUG
         DPRINT1(" Fail to get DirectDraw driver info \n");
+#endif
 		GDIOBJ_UnlockObjByPtr(pDirectDraw);
 		return FALSE;
 	}
 
+      
+    /* rest the flag so we do not need do it later */
+    pCallBackFlags[0]=0;
+    pCallBackFlags[1]=0;
+    pCallBackFlags[2]=0;
+
 	if (pHalInfo)
-	{
-       DPRINT1("Found DirectDraw CallBack for 2D Hal\n");
-       RtlMoveMemory(&pDirectDraw->Hal, pHalInfo, sizeof(DD_HALINFO));
+	{       
+      
+          {
+             DDHALINFO* pHalInfo2 = ((DDHALINFO*) pHalInfo);
+#ifdef DX_DEBUG
+             DPRINT1("Found DirectDraw CallBack for 2D and 3D Hal\n");
+#endif
+             RtlMoveMemory(&pDirectDraw->Hal, pHalInfo2, sizeof(DDHALINFO));
 
-	   if (pHalInfo->lpD3DHALCallbacks)
-	   {    
-         DPRINT1("Found DirectDraw CallBack for 3D Hal\n");
-		 RtlMoveMemory(puD3dCallbacks, pHalInfo->lpD3DHALCallbacks, sizeof(D3DNTHAL_CALLBACKS));		
+             if (pHalInfo2->lpDDExeBufCallbacks)
+	         {
+#ifdef DX_DEBUG
+                 DPRINT1("Found DirectDraw CallBack for 3D Hal Bufffer  \n");                                
+#endif
+                 /* msdn DDHAL_D3DBUFCALLBACKS = DD_D3DBUFCALLBACKS */
+                 RtlMoveMemory(puD3dBufferCallbacks, pHalInfo2->lpDDExeBufCallbacks, sizeof(DD_D3DBUFCALLBACKS));
+             }
+              
+#ifdef DX_DEBUG               
+             DPRINT1("Do not support CallBack for 3D Hal\n");
+#endif
+             /* FIXME we need D3DHAL be include 
+
+             if (pHalInfo2->lpD3DHALCallbacks )
+	         {    
+#ifdef DX_DEBUG
+                    DPRINT1("Found DirectDraw CallBack for 3D Hal\n");
+#endif
+		            RtlMoveMemory(puD3dCallbacks, (ULONG *)pHalInfo2->lpD3DHALCallbacks, sizeof( D3DHAL_CALLBACKS ));		
+	         } 
+             */              
+
+
+             /* msdn say D3DHAL_GLOBALDRIVERDATA and D3DNTHAL_GLOBALDRIVERDATA are not same 
+                but if u compare these in msdn it is exacly same */
+
+	         if (pHalInfo->lpD3DGlobalDriverData)
+	         {
+#ifdef DX_DEBUG
+                   DPRINT1("Found DirectDraw CallBack for 3D Hal Private  \n");
+#endif
+		           RtlMoveMemory(puD3dDriverData, (ULONG *)pHalInfo2->lpD3DGlobalDriverData, sizeof(D3DNTHAL_GLOBALDRIVERDATA));
+	         }
+              
+             /* build the flag */
+               
+             if (pHalInfo2->lpDDCallbacks!=NULL)
+             {
+#ifdef DX_DEBUG
+                    DPRINT1("Dectect DirectDraw lpDDCallbacks for 2D Hal flag = %d\n",pHalInfo2->lpDDCallbacks->dwFlags);
+#endif
+                    pCallBackFlags[0] = pHalInfo2->lpDDCallbacks->dwFlags;
+             }
+     
+             if (pHalInfo2->lpDDCallbacks!=NULL)
+             {
+#ifdef DX_DEBUG
+                   DPRINT1("Dectect DirectDraw lpDDSurfaceCallbacks for 2D Hal flag = %d\n",pHalInfo2->lpDDSurfaceCallbacks->dwFlags);
+#endif
+                   pCallBackFlags[1] = pHalInfo2->lpDDSurfaceCallbacks->dwFlags;
+             }
+       
+             if (pHalInfo2->lpDDCallbacks!=NULL)
+             {
+#ifdef DX_DEBUG
+                   DPRINT1("Dectect DirectDraw lpDDCallbacks for 2D Hal flag = %d\n",pHalInfo2->lpDDPaletteCallbacks->dwFlags);
+#endif
+                   pCallBackFlags[2] = pHalInfo2->lpDDPaletteCallbacks->dwFlags;
+             }
+
+          }
+             
+#ifdef DX_DEBUG
+          DPRINT1("Found DirectDraw CallBack for 3D Hal\n");
+#endif
+          RtlMoveMemory(&pDirectDraw->Hal, pHalInfo, sizeof(DD_HALINFO));
+
+          if (pHalInfo->lpD3DBufCallbacks)
+	      {
+#ifdef DX_DEBUG
+                   DPRINT1("Found DirectDraw CallBack for 3D Hal Bufffer  \n");
+#endif
+		           RtlMoveMemory(puD3dBufferCallbacks, pHalInfo->lpD3DBufCallbacks, sizeof(DD_D3DBUFCALLBACKS));
+	      }
+
+          if (pHalInfo->lpD3DHALCallbacks)
+	      {
+#ifdef DX_DEBUG
+                   DPRINT1("Found DirectDraw CallBack for 3D Hal\n");
+#endif
+		           RtlMoveMemory(puD3dCallbacks, pHalInfo->lpD3DHALCallbacks, sizeof(D3DNTHAL_CALLBACKS));		
+	      }
+
+	      if (pHalInfo->lpD3DGlobalDriverData)
+	      {
+#ifdef DX_DEBUG
+                   DPRINT1("Found DirectDraw CallBack for 3D Hal Private  \n");
+#endif
+		           RtlMoveMemory(puD3dDriverData, pHalInfo->lpD3DGlobalDriverData, sizeof(D3DNTHAL_GLOBALDRIVERDATA));
+	      }
+          
+#ifdef DX_DEBUG
+          DPRINT1("Unkown DirectX driver interface\n");
+#endif
+                            	   	          	   	                           	   	
 	   }
 
-	   if (pHalInfo->lpD3DGlobalDriverData)
-	   {
-         DPRINT1("Found DirectDraw CallBack for 3D Hal Private  \n");
-		 RtlMoveMemory(puD3dDriverData, pHalInfo->lpD3DGlobalDriverData, sizeof(D3DNTHAL_GLOBALDRIVERDATA));
-	   }
-	   if (pHalInfo->lpD3DBufCallbacks)
-	   {
-         DPRINT1("Found DirectDraw CallBack for 3D Hal Bufffer  \n");
-		 RtlMoveMemory(puD3dBufferCallbacks, pHalInfo->lpD3DBufCallbacks, sizeof(DD_D3DBUFCALLBACKS));
-	   }
-       	   	                           	   	
-	 }	 
+#ifdef DX_DEBUG
      else
 	 {
 	   DPRINT1("No DirectDraw Hal info have been found, it did not fail, it did gather some other info \n");
     }
+#endif
         
 	GDIOBJ_UnlockObjByPtr(pDirectDraw);
 
@@ -210,11 +359,15 @@
 	DWORD  ddRVal = 0;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdGetDriverInfo\n");
+#endif
 	
 	if (pDirectDraw == NULL) 
 	{
+#ifdef DX_DEBUG
         DPRINT1("Can not lock DirectDraw handle \n");
+#endif
 		return DDHAL_DRIVER_NOTHANDLED;
     }
 
@@ -230,7 +383,9 @@
     /* Now we are doing the call to drv DrvGetDriverInfo */
 	if   (ddRVal == 2)
 	{
+#ifdef DX_DEBUG
          DPRINT1("NtGdiDdGetDriverInfo DDHAL_DRIVER_NOTHANDLED");         
+#endif
 	     ddRVal = DDHAL_DRIVER_NOTHANDLED;
     }
 	else
@@ -259,12 +414,16 @@
 {
 	DWORD  ddRVal = DDHAL_DRIVER_NOTHANDLED;
 	PDD_DIRECTDRAW_GLOBAL lgpl;
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdCreateSurface\n");
+#endif
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
 	if (pDirectDraw == NULL) 
 	{
+#ifdef DX_DEBUG
        	DPRINT1("Can not lock the DirectDraw handle\n");
+#endif
 		return DDHAL_DRIVER_NOTHANDLED;
     }
 	
@@ -277,7 +436,9 @@
 	/* make the call */
 	if (!(pDirectDraw->DD.dwFlags & DDHAL_CB32_CANCREATESURFACE))
 	{
+#ifdef DX_DEBUG
         DPRINT1("DirectDraw HAL does not support Create Surface"); 
+#endif
 		ddRVal = DDHAL_DRIVER_NOTHANDLED;
     }
 	else
@@ -299,7 +460,9 @@
 {
 	DWORD  ddRVal;
 	PDD_DIRECTDRAW_GLOBAL lgpl;
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdWaitForVerticalBlank\n");
+#endif
 
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
@@ -334,7 +497,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;	
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdCanCreateSurface\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -366,7 +531,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdGetScanLine\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -405,7 +572,9 @@
 	DWORD  ddRVal  = DDHAL_DRIVER_NOTHANDLED;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdDestroySurface\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -444,7 +613,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurfaceTarget, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdFlip\n");
+#endif
 	
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
@@ -478,7 +649,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdLock\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -510,7 +683,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdUnlock\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -543,7 +718,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
     PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurfaceDest, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
     DPRINT1("NtGdiDdBlt\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -575,7 +752,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdSetColorKey\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -609,7 +788,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurfaceAttached, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdAddAttachedSurface\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -641,7 +822,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdGetBltStatus\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -673,7 +856,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurface, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdGetFlipStatus\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -706,7 +891,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
     PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurfaceDestination, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
     DPRINT1("NtGdiDdUpdateOverlay\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -739,7 +926,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
     PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hSurfaceDestination, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
     DPRINT1("NtGdiDdSetOverlayPosition\n");
+#endif
 	if (pDirectDraw == NULL) 
 		return DDHAL_DRIVER_NOTHANDLED;
 
@@ -773,7 +962,9 @@
 	/* FIXME: implement 
 	 * PDD_SURFACE pDDSurf = PVOID pDDSurf
 	 */
+#ifdef DX_DEBUG
     DPRINT1("DDSURF_Cleanup\n");
+#endif
 	return TRUE;
 }
 
@@ -787,7 +978,9 @@
 )
 {
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdCreateSurfaceObject\n");
+#endif
 	if (!pDirectDraw)
 		return NULL;
 
@@ -818,7 +1011,9 @@
     HANDLE hSurface
 )
 {
+#ifdef DX_DEBUG
     DPRINT1("NtGdiDdDeleteSurfaceObject\n");
+#endif
     /* FIXME add right GDI_OBJECT_TYPE_ for everthing for now 
        we are using same type */
 	/* return GDIOBJ_FreeObj(hSurface, GDI_OBJECT_TYPE_DD_SURFACE); */
@@ -869,7 +1064,9 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDrawLocal, GDI_OBJECT_TYPE_DIRECTDRAW);
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdGetAvailDriverMemory\n");
+#endif
 
 	/* backup the orignal PDev and info */
 	lgpl = puGetAvailDriverMemoryData->lpDD;
@@ -901,7 +1098,10 @@
 	PDD_DIRECTDRAW_GLOBAL lgpl;
 
 	PDD_DIRECTDRAW pDirectDraw = GDIOBJ_LockObj(hDirectDraw, GDI_OBJECT_TYPE_DIRECTDRAW);
+
+#ifdef DX_DEBUG
 	DPRINT1("NtGdiDdSetExclusiveMode\n");
+#endif
 
 	/* backup the orignal PDev and info */
 	lgpl = puSetExclusiveModeData->lpDD;