msi: Translate the INSTALLSTATE_UNKNOWN index into the INSTALLSTATE_ABSENT image...
[wine] / dlls / rpcrt4 / cstub.c
1 /*
2  * COM stub (CStdStubBuffer) implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "excpt.h"
29
30 #include "objbase.h"
31 #include "rpcproxy.h"
32
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35
36 #include "cpsf.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 #define STUB_HEADER(This) (((CInterfaceStubHeader*)((This)->lpVtbl))[-1])
41
42 static WINE_EXCEPTION_FILTER(stub_filter)
43 {
44     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
45         return EXCEPTION_CONTINUE_SEARCH;
46     return EXCEPTION_EXECUTE_HANDLER;
47 }
48
49 typedef struct
50 {
51     IUnknownVtbl *base_obj;
52     IRpcStubBuffer *base_stub;
53     CStdStubBuffer stub_buffer;
54 } cstdstubbuffer_delegating_t;
55
56 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
57 {
58     return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
59 }
60
61 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
62                                        LPUNKNOWN pUnkServer,
63                                        PCInterfaceName name,
64                                        CInterfaceStubVtbl *vtbl,
65                                        LPPSFACTORYBUFFER pPSFactory,
66                                        LPRPCSTUBBUFFER *ppStub)
67 {
68   CStdStubBuffer *This;
69   IUnknown *pvServer;
70   HRESULT r;
71   TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
72   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
73   TRACE("vtbl=%p\n", &vtbl->Vtbl);
74
75   if (!IsEqualGUID(vtbl->header.piid, riid)) {
76     ERR("IID mismatch during stub creation\n");
77     return RPC_E_UNEXPECTED;
78   }
79
80   r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
81   if(FAILED(r))
82     return r;
83
84   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
85   if (!This) {
86     IUnknown_Release(pvServer);
87     return E_OUTOFMEMORY;
88   }
89
90   This->lpVtbl = &vtbl->Vtbl;
91   This->RefCount = 1;
92   This->pvServerObject = pvServer;
93   This->pPSFactory = pPSFactory;
94   *ppStub = (LPRPCSTUBBUFFER)This;
95
96   IPSFactoryBuffer_AddRef(pPSFactory);
97   return S_OK;
98 }
99
100 static CRITICAL_SECTION delegating_vtbl_section;
101 static CRITICAL_SECTION_DEBUG critsect_debug =
102 {
103     0, 0, &delegating_vtbl_section,
104     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
105       0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
106 };
107 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
108
109 typedef struct
110 {
111     DWORD ref;
112     IUnknownVtbl vtbl;
113 } ref_counted_vtbl;
114
115 static struct
116 {
117     ref_counted_vtbl *table;
118     DWORD size;
119 } current_vtbl;
120
121
122 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
123 {
124     *ppv = (void *)pUnk;
125     return S_OK;
126 }
127
128 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
129 {
130     return 1;
131 }
132
133 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
134 {
135     return 1;
136 }
137
138 #if defined(__i386__)
139
140 /* The idea here is to replace the first param on the stack
141    ie. This (which will point to cstdstubbuffer_delegating_t)
142    with This->stub_buffer.pvServerObject and then jump to the
143    relevant offset in This->stub_buffer.pvServerObject's vtbl.
144 */
145 #include "pshpack1.h"
146 typedef struct {
147     DWORD mov1;    /* mov 0x4(%esp), %eax      8b 44 24 04 */
148     WORD mov2;     /* mov 0x10(%eax), %eax     8b 40 */
149     BYTE sixteen;  /*                          10   */
150     DWORD mov3;    /* mov %eax, 0x4(%esp)      89 44 24 04 */
151     WORD mov4;     /* mov (%eax), %eax         8b 00 */
152     WORD mov5;     /* mov offset(%eax), %eax   8b 80 */
153     DWORD offset;  /*                          xx xx xx xx */
154     WORD jmp;      /* jmp *%eax                ff e0 */
155     BYTE pad[3];   /* lea 0x0(%esi), %esi      8d 76 00 */
156 } vtbl_method_t;
157 #include "poppack.h"
158
159 static void fill_table(IUnknownVtbl *vtbl, DWORD num)
160 {
161     vtbl_method_t *method;
162     void **entry;
163     DWORD i;
164
165     vtbl->QueryInterface = delegating_QueryInterface;
166     vtbl->AddRef = delegating_AddRef;
167     vtbl->Release = delegating_Release;
168
169     method = (vtbl_method_t*)((void **)vtbl + num);
170     entry = (void**)(vtbl + 1);
171
172     for(i = 3; i < num; i++)
173     {
174         *entry = method;
175         method->mov1 = 0x0424448b;
176         method->mov2 = 0x408b;
177         method->sixteen = 0x10;
178         method->mov3 = 0x04244489;
179         method->mov4 = 0x008b;
180         method->mov5 = 0x808b;
181         method->offset = i << 2;
182         method->jmp = 0xe0ff;
183         method->pad[0] = 0x8d;
184         method->pad[1] = 0x76;
185         method->pad[2] = 0x00;
186
187         method++;
188         entry++;
189     }
190 }
191
192 #else  /* __i386__ */
193
194 typedef struct {int dummy;} vtbl_method_t;
195 static void fill_table(IUnknownVtbl *vtbl, DWORD num)
196 {
197     ERR("delegated stubs are not supported on this architecture\n");
198 }
199
200 #endif  /* __i386__ */
201
202 void create_delegating_vtbl(DWORD num_methods)
203 {
204     TRACE("%ld\n", num_methods);
205     if(num_methods <= 3)
206     {
207         ERR("should have more than %ld methods\n", num_methods);
208         return;
209     }
210
211     EnterCriticalSection(&delegating_vtbl_section);
212     if(num_methods > current_vtbl.size)
213     {
214         DWORD size;
215         if(current_vtbl.table && current_vtbl.table->ref == 0)
216         {
217             TRACE("freeing old table\n");
218             HeapFree(GetProcessHeap(), 0, current_vtbl.table);
219         }
220         size = sizeof(DWORD) + num_methods * sizeof(void*) + (num_methods - 3) * sizeof(vtbl_method_t);
221         current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, size);
222         fill_table(&current_vtbl.table->vtbl, num_methods);
223         current_vtbl.table->ref = 0;
224         current_vtbl.size = num_methods;
225     }
226     LeaveCriticalSection(&delegating_vtbl_section);
227 }
228
229 static IUnknownVtbl *get_delegating_vtbl(void)
230 {
231     IUnknownVtbl *ret;
232
233     EnterCriticalSection(&delegating_vtbl_section);
234     current_vtbl.table->ref++;
235     ret = &current_vtbl.table->vtbl;
236     LeaveCriticalSection(&delegating_vtbl_section);
237     return ret;
238 }
239
240 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
241 {
242     ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
243
244     EnterCriticalSection(&delegating_vtbl_section);
245     table->ref--;
246     TRACE("ref now %ld\n", table->ref);
247     if(table->ref == 0 && table != current_vtbl.table)
248     {
249         TRACE("... and we're not current so free'ing\n");
250         HeapFree(GetProcessHeap(), 0, table);
251     }
252     LeaveCriticalSection(&delegating_vtbl_section);
253 }
254
255 HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
256                                                    LPUNKNOWN pUnkServer,
257                                                    PCInterfaceName name,
258                                                    CInterfaceStubVtbl *vtbl,
259                                                    REFIID delegating_iid,
260                                                    LPPSFACTORYBUFFER pPSFactory,
261                                                    LPRPCSTUBBUFFER *ppStub)
262 {
263     cstdstubbuffer_delegating_t *This;
264     IUnknown *pvServer;
265     HRESULT r;
266
267     TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
268     TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
269     TRACE("vtbl=%p\n", &vtbl->Vtbl);
270
271     if (!IsEqualGUID(vtbl->header.piid, riid))
272     {
273         ERR("IID mismatch during stub creation\n");
274         return RPC_E_UNEXPECTED;
275     }
276
277     r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
278     if(FAILED(r)) return r;
279
280     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
281     if (!This)
282     {
283         IUnknown_Release(pvServer);
284         return E_OUTOFMEMORY;
285     }
286
287     This->base_obj = get_delegating_vtbl();
288     r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
289     if(FAILED(r))
290     {
291         release_delegating_vtbl(This->base_obj);
292         HeapFree(GetProcessHeap(), 0, This);
293         IUnknown_Release(pvServer);
294         return r;
295     }
296
297     This->stub_buffer.lpVtbl = &vtbl->Vtbl;
298     This->stub_buffer.RefCount = 1;
299     This->stub_buffer.pvServerObject = pvServer;
300     This->stub_buffer.pPSFactory = pPSFactory;
301     *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
302
303     IPSFactoryBuffer_AddRef(pPSFactory);
304     return S_OK;
305 }
306
307 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
308                                             REFIID riid,
309                                             LPVOID *obj)
310 {
311   CStdStubBuffer *This = (CStdStubBuffer *)iface;
312   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
313
314   if (IsEqualIID(&IID_IUnknown, riid) ||
315       IsEqualIID(&IID_IRpcStubBuffer, riid))
316   {
317     IUnknown_AddRef(iface);
318     *obj = iface;
319     return S_OK;
320   }
321   *obj = NULL;
322   return E_NOINTERFACE;
323 }
324
325 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
326 {
327   CStdStubBuffer *This = (CStdStubBuffer *)iface;
328   TRACE("(%p)->AddRef()\n",This);
329   return InterlockedIncrement(&This->RefCount);
330 }
331
332 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
333                                       LPPSFACTORYBUFFER pPSF)
334 {
335   CStdStubBuffer *This = (CStdStubBuffer *)iface;
336   ULONG refs;
337
338   TRACE("(%p)->Release()\n",This);
339
340   refs = InterlockedDecrement(&This->RefCount);
341   if (!refs)
342   {
343     /* test_Release shows that native doesn't call Disconnect here.
344        We'll leave it in for the time being. */
345     IRpcStubBuffer_Disconnect(iface);
346
347     IPSFactoryBuffer_Release(pPSF);
348     HeapFree(GetProcessHeap(),0,This);
349   }
350   return refs;
351 }
352
353 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
354                                         LPPSFACTORYBUFFER pPSF)
355 {
356     cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
357     ULONG refs;
358
359     TRACE("(%p)->Release()\n", This);
360
361     refs = InterlockedDecrement(&This->stub_buffer.RefCount);
362     if (!refs)
363     {
364         /* Just like NdrCStdStubBuffer_Release, we shouldn't call
365            Disconnect here */
366         IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
367
368         IRpcStubBuffer_Release(This->base_stub);
369         release_delegating_vtbl(This->base_obj);
370
371         IPSFactoryBuffer_Release(pPSF);
372         HeapFree(GetProcessHeap(), 0, This);
373     }
374
375     return refs;
376 }
377
378 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
379                                      LPUNKNOWN lpUnkServer)
380 {
381     CStdStubBuffer *This = (CStdStubBuffer *)iface;
382     HRESULT r;
383     IUnknown *new = NULL;
384
385     TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
386
387     r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
388     new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
389     if(new)
390         IUnknown_Release(new);
391     return r;
392 }
393
394 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
395 {
396     CStdStubBuffer *This = (CStdStubBuffer *)iface;
397     IUnknown *old;
398     TRACE("(%p)->Disconnect()\n",This);
399
400     old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
401
402     if(old)
403         IUnknown_Release(old);
404 }
405
406 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
407                                     PRPCOLEMESSAGE pMsg,
408                                     LPRPCCHANNELBUFFER pChannel)
409 {
410   CStdStubBuffer *This = (CStdStubBuffer *)iface;
411   DWORD dwPhase = STUB_UNMARSHAL;
412   HRESULT hr = S_OK;
413
414   TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
415
416   __TRY
417   {
418     if (STUB_HEADER(This).pDispatchTable)
419       STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
420     else /* pure interpreted */
421       NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
422   }
423   __EXCEPT(stub_filter)
424   {
425     DWORD dwExceptionCode = GetExceptionCode();
426     WARN("a stub call failed with exception 0x%08lx (%ld)\n", dwExceptionCode, dwExceptionCode);
427     if (FAILED(dwExceptionCode))
428       hr = dwExceptionCode;
429     else
430       hr = HRESULT_FROM_WIN32(dwExceptionCode);
431   }
432   __ENDTRY
433
434   return hr;
435 }
436
437 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
438                                                     REFIID riid)
439 {
440   CStdStubBuffer *This = (CStdStubBuffer *)iface;
441   TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
442   return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
443 }
444
445 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
446 {
447   CStdStubBuffer *This = (CStdStubBuffer *)iface;
448   TRACE("(%p)->CountRefs()\n",This);
449   return This->RefCount;
450 }
451
452 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
453                                                        LPVOID *ppv)
454 {
455   CStdStubBuffer *This = (CStdStubBuffer *)iface;
456   TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
457   return S_OK;
458 }
459
460 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
461                                              LPVOID pv)
462 {
463   CStdStubBuffer *This = (CStdStubBuffer *)iface;
464   TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
465 }
466
467 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
468 {
469     CStdStubBuffer_QueryInterface,
470     CStdStubBuffer_AddRef,
471     NULL,
472     CStdStubBuffer_Connect,
473     CStdStubBuffer_Disconnect,
474     CStdStubBuffer_Invoke,
475     CStdStubBuffer_IsIIDSupported,
476     CStdStubBuffer_CountRefs,
477     CStdStubBuffer_DebugServerQueryInterface,
478     CStdStubBuffer_DebugServerRelease
479 };
480
481 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
482                                                         LPUNKNOWN lpUnkServer)
483 {
484     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
485     HRESULT r;
486     TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
487
488     r = CStdStubBuffer_Connect(iface, lpUnkServer);
489     if(SUCCEEDED(r))
490         r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
491
492     return r;
493 }
494
495 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
496 {
497     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
498     TRACE("(%p)->Disconnect()\n", This);
499
500     IRpcStubBuffer_Disconnect(This->base_stub);
501     CStdStubBuffer_Disconnect(iface);
502 }
503
504 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
505 {
506     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
507     ULONG ret;
508     TRACE("(%p)->CountRefs()\n", This);
509
510     ret = CStdStubBuffer_CountRefs(iface);
511     ret += IRpcStubBuffer_CountRefs(This->base_stub);
512
513     return ret;
514 }
515
516 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
517 {
518     CStdStubBuffer_QueryInterface,
519     CStdStubBuffer_AddRef,
520     NULL,
521     CStdStubBuffer_Delegating_Connect,
522     CStdStubBuffer_Delegating_Disconnect,
523     CStdStubBuffer_Invoke,
524     CStdStubBuffer_IsIIDSupported,
525     CStdStubBuffer_Delegating_CountRefs,
526     CStdStubBuffer_DebugServerQueryInterface,
527     CStdStubBuffer_DebugServerRelease
528 };
529
530 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
531 {
532   CStdStubBuffer *This = (CStdStubBuffer *)iface;
533   return STUB_HEADER(This).pServerInfo;
534 }
535
536 /************************************************************************
537  *           NdrStubForwardingFunction [RPCRT4.@]
538  */
539 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
540                                            PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
541 {
542     /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
543
544     cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
545     HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
546     if(FAILED(r)) RpcRaiseException(r);
547     return;
548 }
549
550 /***********************************************************************
551  *           NdrStubInitialize [RPCRT4.@]
552  */
553 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
554                              PMIDL_STUB_MESSAGE pStubMsg,
555                              PMIDL_STUB_DESC pStubDescriptor,
556                              LPRPCCHANNELBUFFER pRpcChannelBuffer)
557 {
558   TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
559   NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
560   pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
561 }
562
563 /***********************************************************************
564  *           NdrStubGetBuffer [RPCRT4.@]
565  */
566 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
567                             LPRPCCHANNELBUFFER pRpcChannelBuffer,
568                             PMIDL_STUB_MESSAGE pStubMsg)
569 {
570   CStdStubBuffer *This = (CStdStubBuffer *)iface;
571   HRESULT hr;
572
573   TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
574
575   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
576   hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
577     (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
578   if (FAILED(hr))
579   {
580     RpcRaiseException(hr);
581     return;
582   }
583
584   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
585   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
586   pStubMsg->Buffer = pStubMsg->BufferStart;
587 }