wined3d: Set 3D device caps in adapter_gl_get_wined3d_caps().
[wine.git] / dlls / rpcrt4 / ndr_contexthandle.c
bloba5e1ffc253d3cb30cda5c3a517040b68f32db282
1 /*
2 * NDR data marshalling
4 * Copyright 2006 Mike McCormack (for CodeWeavers)
5 * Copyright 2006-2007 Robert Shearman (for CodeWeavers)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "ndr_misc.h"
23 #include "rpc_assoc.h"
24 #include "rpcndr.h"
26 #include "wine/debug.h"
27 #include "wine/list.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ole);
31 #define NDR_CONTEXT_HANDLE_MAGIC 0x4352444e
33 typedef struct ndr_context_handle
35 ULONG attributes;
36 GUID uuid;
37 } ndr_context_handle;
39 struct context_handle_entry
41 struct list entry;
42 DWORD magic;
43 RPC_BINDING_HANDLE handle;
44 ndr_context_handle wire_data;
47 static struct list context_handle_list = LIST_INIT(context_handle_list);
49 static CRITICAL_SECTION ndr_context_cs;
50 static CRITICAL_SECTION_DEBUG ndr_context_debug =
52 0, 0, &ndr_context_cs,
53 { &ndr_context_debug.ProcessLocksList, &ndr_context_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": ndr_context") }
56 static CRITICAL_SECTION ndr_context_cs = { &ndr_context_debug, -1, 0, 0, 0, 0 };
58 static struct context_handle_entry *get_context_entry(NDR_CCONTEXT CContext)
60 struct context_handle_entry *che = CContext;
62 if (che->magic != NDR_CONTEXT_HANDLE_MAGIC)
63 return NULL;
64 return che;
67 static struct context_handle_entry *context_entry_from_guid(LPCGUID uuid)
69 struct context_handle_entry *che;
70 LIST_FOR_EACH_ENTRY(che, &context_handle_list, struct context_handle_entry, entry)
71 if (IsEqualGUID(&che->wire_data.uuid, uuid))
72 return che;
73 return NULL;
76 RPC_BINDING_HANDLE WINAPI NDRCContextBinding(NDR_CCONTEXT CContext)
78 struct context_handle_entry *che;
79 RPC_BINDING_HANDLE handle = NULL;
81 TRACE("%p\n", CContext);
83 EnterCriticalSection(&ndr_context_cs);
84 che = get_context_entry(CContext);
85 if (che)
86 handle = che->handle;
87 LeaveCriticalSection(&ndr_context_cs);
89 if (!handle)
91 ERR("invalid handle %p\n", CContext);
92 RpcRaiseException(RPC_X_SS_CONTEXT_MISMATCH);
94 return handle;
97 void WINAPI NDRCContextMarshall(NDR_CCONTEXT CContext, void *pBuff)
99 struct context_handle_entry *che;
101 TRACE("%p %p\n", CContext, pBuff);
103 if (CContext)
105 EnterCriticalSection(&ndr_context_cs);
106 che = get_context_entry(CContext);
107 memcpy(pBuff, &che->wire_data, sizeof (ndr_context_handle));
108 LeaveCriticalSection(&ndr_context_cs);
110 else
112 ndr_context_handle *wire_data = pBuff;
113 wire_data->attributes = 0;
114 wire_data->uuid = GUID_NULL;
118 /***********************************************************************
119 * RpcSmDestroyClientContext [RPCRT4.@]
121 RPC_STATUS WINAPI RpcSmDestroyClientContext(void **ContextHandle)
123 RPC_STATUS status = RPC_X_SS_CONTEXT_MISMATCH;
124 struct context_handle_entry *che = NULL;
126 TRACE("(%p)\n", ContextHandle);
128 EnterCriticalSection(&ndr_context_cs);
129 che = get_context_entry(*ContextHandle);
130 *ContextHandle = NULL;
131 if (che)
133 status = RPC_S_OK;
134 list_remove(&che->entry);
137 LeaveCriticalSection(&ndr_context_cs);
139 if (che)
141 RpcBindingFree(&che->handle);
142 HeapFree(GetProcessHeap(), 0, che);
145 return status;
148 /***********************************************************************
149 * RpcSsDestroyClientContext [RPCRT4.@]
151 void WINAPI RpcSsDestroyClientContext(void **ContextHandle)
153 RPC_STATUS status = RpcSmDestroyClientContext(ContextHandle);
154 if (status != RPC_S_OK)
155 RpcRaiseException(status);
158 /***********************************************************************
159 * RpcSsDontSerializeContext [RPCRT4.@]
161 void WINAPI RpcSsDontSerializeContext(void)
163 FIXME("stub\n");
166 static RPC_STATUS ndr_update_context_handle(NDR_CCONTEXT *CContext,
167 RPC_BINDING_HANDLE hBinding,
168 const ndr_context_handle *chi)
170 struct context_handle_entry *che = NULL;
172 /* a null UUID means we should free the context handle */
173 if (IsEqualGUID(&chi->uuid, &GUID_NULL))
175 if (*CContext)
177 che = get_context_entry(*CContext);
178 if (!che)
179 return RPC_X_SS_CONTEXT_MISMATCH;
180 list_remove(&che->entry);
181 RpcBindingFree(&che->handle);
182 HeapFree(GetProcessHeap(), 0, che);
183 che = NULL;
186 /* if there's no existing entry matching the GUID, allocate one */
187 else if (!(che = context_entry_from_guid(&chi->uuid)))
189 che = HeapAlloc(GetProcessHeap(), 0, sizeof *che);
190 if (!che)
191 return RPC_X_NO_MEMORY;
192 che->magic = NDR_CONTEXT_HANDLE_MAGIC;
193 RpcBindingCopy(hBinding, &che->handle);
194 list_add_tail(&context_handle_list, &che->entry);
195 che->wire_data = *chi;
198 *CContext = che;
200 return RPC_S_OK;
203 /***********************************************************************
204 * NDRCContextUnmarshall [RPCRT4.@]
206 void WINAPI NDRCContextUnmarshall(NDR_CCONTEXT *CContext,
207 RPC_BINDING_HANDLE hBinding,
208 void *pBuff, ULONG DataRepresentation)
210 RPC_STATUS status;
212 TRACE("*%p=(%p) %p %p %08x\n",
213 CContext, *CContext, hBinding, pBuff, DataRepresentation);
215 EnterCriticalSection(&ndr_context_cs);
216 status = ndr_update_context_handle(CContext, hBinding, pBuff);
217 LeaveCriticalSection(&ndr_context_cs);
218 if (status)
219 RpcRaiseException(status);
222 /***********************************************************************
223 * NDRSContextMarshall [RPCRT4.@]
225 void WINAPI NDRSContextMarshall(NDR_SCONTEXT SContext,
226 void *pBuff,
227 NDR_RUNDOWN userRunDownIn)
229 TRACE("(%p %p %p)\n", SContext, pBuff, userRunDownIn);
230 NDRSContextMarshall2(I_RpcGetCurrentCallHandle(), SContext, pBuff,
231 userRunDownIn, NULL, RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
234 /***********************************************************************
235 * NDRSContextMarshallEx [RPCRT4.@]
237 void WINAPI NDRSContextMarshallEx(RPC_BINDING_HANDLE hBinding,
238 NDR_SCONTEXT SContext,
239 void *pBuff,
240 NDR_RUNDOWN userRunDownIn)
242 TRACE("(%p %p %p %p)\n", hBinding, SContext, pBuff, userRunDownIn);
243 NDRSContextMarshall2(hBinding, SContext, pBuff, userRunDownIn, NULL,
244 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
247 /***********************************************************************
248 * NDRSContextMarshall2 [RPCRT4.@]
250 void WINAPI NDRSContextMarshall2(RPC_BINDING_HANDLE hBinding,
251 NDR_SCONTEXT SContext,
252 void *pBuff,
253 NDR_RUNDOWN userRunDownIn,
254 void *CtxGuard, ULONG Flags)
256 RpcBinding *binding = hBinding;
257 RPC_STATUS status;
258 ndr_context_handle *ndr = pBuff;
260 TRACE("(%p %p %p %p %p %u)\n",
261 hBinding, SContext, pBuff, userRunDownIn, CtxGuard, Flags);
263 if (!binding->server || !binding->Assoc)
264 RpcRaiseException(RPC_S_INVALID_BINDING);
266 if (Flags & RPC_CONTEXT_HANDLE_FLAGS)
267 FIXME("unimplemented flags: 0x%x\n", Flags & RPC_CONTEXT_HANDLE_FLAGS);
269 if (SContext->userContext)
271 status = RpcServerAssoc_UpdateContextHandle(binding->Assoc, SContext, CtxGuard, userRunDownIn);
272 if (status != RPC_S_OK)
273 RpcRaiseException(status);
274 ndr->attributes = 0;
275 RpcContextHandle_GetUuid(SContext, &ndr->uuid);
277 RPCRT4_RemoveThreadContextHandle(SContext);
278 RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, TRUE);
280 else
282 if (!RpcContextHandle_IsGuardCorrect(SContext, CtxGuard))
283 RpcRaiseException(RPC_X_SS_CONTEXT_MISMATCH);
284 memset(ndr, 0, sizeof(*ndr));
286 RPCRT4_RemoveThreadContextHandle(SContext);
287 /* Note: release the context handle twice in this case to release
288 * one ref being kept around for the data and one ref for the
289 * unmarshall/marshall sequence */
290 if (!RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, TRUE))
291 return; /* this is to cope with the case of the data not being valid
292 * before and so not having a further reference */
293 RpcServerAssoc_ReleaseContextHandle(binding->Assoc, SContext, FALSE);
297 /***********************************************************************
298 * NDRSContextUnmarshall [RPCRT4.@]
300 NDR_SCONTEXT WINAPI NDRSContextUnmarshall(void *pBuff,
301 ULONG DataRepresentation)
303 TRACE("(%p %08x)\n", pBuff, DataRepresentation);
304 return NDRSContextUnmarshall2(I_RpcGetCurrentCallHandle(), pBuff,
305 DataRepresentation, NULL,
306 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
309 /***********************************************************************
310 * NDRSContextUnmarshallEx [RPCRT4.@]
312 NDR_SCONTEXT WINAPI NDRSContextUnmarshallEx(RPC_BINDING_HANDLE hBinding,
313 void *pBuff,
314 ULONG DataRepresentation)
316 TRACE("(%p %p %08x)\n", hBinding, pBuff, DataRepresentation);
317 return NDRSContextUnmarshall2(hBinding, pBuff, DataRepresentation, NULL,
318 RPC_CONTEXT_HANDLE_DEFAULT_FLAGS);
321 /***********************************************************************
322 * NDRSContextUnmarshall2 [RPCRT4.@]
324 NDR_SCONTEXT WINAPI NDRSContextUnmarshall2(RPC_BINDING_HANDLE hBinding,
325 void *pBuff,
326 ULONG DataRepresentation,
327 void *CtxGuard, ULONG Flags)
329 RpcBinding *binding = hBinding;
330 NDR_SCONTEXT SContext;
331 RPC_STATUS status;
332 const ndr_context_handle *context_ndr = pBuff;
334 TRACE("(%p %p %08x %p %u)\n",
335 hBinding, pBuff, DataRepresentation, CtxGuard, Flags);
337 if (!binding->server || !binding->Assoc)
338 RpcRaiseException(RPC_S_INVALID_BINDING);
340 if (Flags & RPC_CONTEXT_HANDLE_FLAGS)
341 FIXME("unimplemented flags: 0x%x\n", Flags & RPC_CONTEXT_HANDLE_FLAGS);
343 if (!pBuff || (!context_ndr->attributes &&
344 UuidIsNil((UUID *)&context_ndr->uuid, &status)))
345 status = RpcServerAssoc_AllocateContextHandle(binding->Assoc, CtxGuard,
346 &SContext);
347 else
349 if (context_ndr->attributes)
351 ERR("non-null attributes 0x%x\n", context_ndr->attributes);
352 status = RPC_X_SS_CONTEXT_MISMATCH;
354 else
355 status = RpcServerAssoc_FindContextHandle(binding->Assoc,
356 &context_ndr->uuid,
357 CtxGuard, Flags,
358 &SContext);
361 if (status != RPC_S_OK)
362 RpcRaiseException(status);
364 RPCRT4_PushThreadContextHandle(SContext);
365 return SContext;