1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS ReactX
4 * FILE: dll/directx/d3d9/d3d9_helpers.c
5 * PURPOSE: d3d9.dll helper functions
6 * PROGRAMERS: Gregor Brunmar <gregor (dot) brunmar (at) home (dot) se>
7 */
8
9 #include <d3d9.h>
10 #include "d3d9_helpers.h"
11 #include <stdio.h>
12 #include <ddraw.h>
13 #include <debug.h>
14
15 #define MEM_ALIGNMENT 0x20
16
17 static LPCSTR D3D9_DebugRegPath = "Software\\Microsoft\\Direct3D";
18
ReadRegistryValue(IN DWORD ValueType,IN LPCSTR ValueName,OUT LPBYTE DataBuffer,IN OUT LPDWORD DataBufferSize)19 BOOL ReadRegistryValue(IN DWORD ValueType, IN LPCSTR ValueName, OUT LPBYTE DataBuffer, IN OUT LPDWORD DataBufferSize)
20 {
21 HKEY hKey;
22 DWORD Type;
23 LONG Ret;
24
25 if (ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, D3D9_DebugRegPath, 0, KEY_QUERY_VALUE, &hKey))
26 return FALSE;
27
28 Ret = RegQueryValueExA(hKey, ValueName, 0, &Type, DataBuffer, DataBufferSize);
29
30 RegCloseKey(hKey);
31
32 if (ERROR_SUCCESS != Ret)
33 return FALSE;
34
35 if (Type != ValueType)
36 return FALSE;
37
38 return TRUE;
39 }
40
SafeFormatString(OUT LPSTR Buffer,IN DWORD BufferSize,IN LPCSTR FormatString,...)41 HRESULT SafeFormatString(OUT LPSTR Buffer, IN DWORD BufferSize, IN LPCSTR FormatString, ... )
42 {
43 DWORD BytesWritten;
44 va_list vargs;
45
46 if (BufferSize == 0)
47 return DDERR_INVALIDPARAMS;
48
49 va_start(vargs, FormatString);
50 BytesWritten = _vsnprintf(Buffer, BufferSize-1, FormatString, vargs);
51 va_end(vargs);
52
53 if (BytesWritten < BufferSize)
54 return DDERR_GENERIC;
55
56 Buffer[BufferSize-1] = '\0';
57
58 return ERROR_SUCCESS;
59 }
60
SafeCopyString(OUT LPSTR Dst,IN DWORD DstSize,IN LPCSTR Src)61 HRESULT SafeCopyString(OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src)
62 {
63 HRESULT hr = ERROR_SUCCESS;
64
65 if (Dst == NULL || DstSize == 0 || Src == NULL)
66 return DDERR_INVALIDPARAMS;
67
68 while (*Src != '\0' && DstSize > 0)
69 {
70 *Dst++ = *Src++;
71 --DstSize;
72 }
73
74 if (DstSize == 0)
75 {
76 --Dst;
77 hr = DDERR_GENERIC;
78 }
79
80 return hr;
81 }
82
SafeAppendString(IN OUT LPSTR Dst,IN DWORD DstSize,IN LPCSTR Src)83 HRESULT SafeAppendString(IN OUT LPSTR Dst, IN DWORD DstSize, IN LPCSTR Src)
84 {
85 size_t CurrentDstLength;
86
87 if (Dst == NULL || DstSize == 0)
88 return DDERR_INVALIDPARAMS;
89
90 CurrentDstLength = strlen(Dst);
91
92 return SafeCopyString(Dst + CurrentDstLength, DstSize - CurrentDstLength, Src);
93 }
94
AlignedAlloc(IN OUT LPVOID * ppObject,IN SIZE_T dwSize)95 HRESULT AlignedAlloc(IN OUT LPVOID *ppObject, IN SIZE_T dwSize)
96 {
97 ULONG_PTR AddressOffset;
98 ULONG AlignedMask = MEM_ALIGNMENT - 1;
99 CHAR *AlignedPtr;
100 ULONG_PTR *AlignedOffsetPtr;
101
102 if (ppObject == 0)
103 return DDERR_INVALIDPARAMS;
104
105 if (dwSize == 0)
106 {
107 *ppObject = NULL;
108 return S_OK;
109 }
110
111 dwSize += MEM_ALIGNMENT;
112
113 AlignedPtr = (CHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
114
115 if (AlignedPtr == 0)
116 return DDERR_OUTOFMEMORY;
117
118 AddressOffset = MEM_ALIGNMENT - ((ULONG_PTR)AlignedPtr & AlignedMask);
119
120 AlignedPtr += AddressOffset;
121
122 AlignedOffsetPtr = (ULONG_PTR *)(AlignedPtr - sizeof(ULONG));
123 *AlignedOffsetPtr = AddressOffset;
124
125 *ppObject = (ULONG_PTR *)AlignedPtr;
126
127 return S_OK;
128 }
129
AlignedFree(IN OUT LPVOID pObject)130 VOID AlignedFree(IN OUT LPVOID pObject)
131 {
132 CHAR *NonAlignedPtr = pObject;
133 ULONG_PTR *AlignedPtr = pObject;
134
135 if (pObject == 0)
136 return;
137
138 NonAlignedPtr -= *(AlignedPtr - 1);
139
140 HeapFree(GetProcessHeap(), 0, NonAlignedPtr);
141 }
142