Always open the device in the default 22050x8x2 format, and let it
[wine] / dlls / rpcrt4 / cproxy.c
1 /*
2  * COM proxy 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  * TODO: Handle non-i386 architectures
21  *       Get rid of #if 0'ed code.
22  */
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27
28 #include "objbase.h"
29 #include "rpcproxy.h"
30
31 #include "cpsf.h"
32 #include "ndr_misc.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(ole);
36
37 struct StublessThunk;
38
39 /* I don't know what MS's std proxy structure looks like,
40    so this probably doesn't match, but that shouldn't matter */
41 typedef struct {
42   ICOM_VTABLE(IRpcProxyBuffer) *lpVtbl;
43   LPVOID *PVtbl;
44   DWORD RefCount;
45   const MIDL_STUBLESS_PROXY_INFO *stubless;
46   const IID* piid;
47   LPUNKNOWN pUnkOuter;
48   LPPSFACTORYBUFFER pPSFactory;
49   LPRPCCHANNELBUFFER pChannel;
50   struct StublessThunk *thunks;
51 } StdProxyImpl;
52
53 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
54
55 /* How the Windows stubless proxy thunks work is explained at
56  * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
57  * but I'll use a slightly different method, to make life easier */
58
59 #if defined(__i386__)
60
61 struct StublessThunk {
62   BYTE push WINE_PACKED;
63   DWORD index WINE_PACKED;
64   BYTE call WINE_PACKED;
65   LONG handler WINE_PACKED;
66   BYTE ret WINE_PACKED;
67   WORD bytes WINE_PACKED;
68   BYTE pad[3];
69 };
70
71 /* adjust the stack size since we don't use Windows's method */
72 #define STACK_ADJUST sizeof(DWORD)
73
74 #define FILL_STUBLESS(x,idx,stk) \
75  x->push = 0x68; /* pushl [immediate] */ \
76  x->index = (idx); \
77  x->call = 0xe8; /* call [near] */ \
78  x->handler = (char*)ObjectStubless - (char*)&x->ret; \
79  x->ret = 0xc2; /* ret [immediate] */ \
80  x->bytes = stk; \
81  x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
82  x->pad[1] = 0x76; \
83  x->pad[2] = 0x00;
84
85 static HRESULT WINAPI ObjectStubless(DWORD index)
86 {
87   char *args = (char*)(&index + 2);
88   LPVOID iface = *(LPVOID*)args;
89
90   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
91
92   PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
93   unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
94   TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
95
96   return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
97 }
98
99 #else  /* __i386__ */
100
101 /* can't do that on this arch */
102 struct StublessThunk { int dummy; };
103 #define FILL_STUBLESS(x,idx,stk) \
104  ERR("stubless proxies are not supported on this architecture\n");
105 #define STACK_ADJUST 0
106
107 #endif  /* __i386__ */
108
109 HRESULT WINAPI StdProxy_Construct(REFIID riid,
110                                  LPUNKNOWN pUnkOuter,
111                                  CInterfaceProxyVtbl *vtbl,
112                                  CInterfaceStubVtbl *svtbl,
113                                  LPPSFACTORYBUFFER pPSFactory,
114                                  LPRPCPROXYBUFFER *ppProxy,
115                                  LPVOID *ppvObj)
116 {
117   StdProxyImpl *This;
118   const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
119
120   TRACE("(%p,%p,%p,%p,%p)\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj);
121
122   /* I can't find any other way to detect stubless proxies than this hack */
123   if (!IsEqualGUID(vtbl->header.piid, riid)) {
124     stubless = *((const void **)vtbl)++;
125     TRACE("stubless=%p\n", stubless);
126   }
127
128   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
129   TRACE("vtbl=%p\n", vtbl->Vtbl);
130
131   if (!IsEqualGUID(vtbl->header.piid, riid)) {
132     ERR("IID mismatch during proxy creation\n");
133     return RPC_E_UNEXPECTED;
134   }
135
136   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
137   if (!This) return E_OUTOFMEMORY;
138
139   if (stubless) {
140     unsigned i, count = svtbl->header.DispatchTableCount;
141     /* Maybe the original vtbl is just modified directly to point at
142      * ObjectStublessClientXXX thunks in real Windows, but I don't like it
143      */
144     TRACE("stubless thunks: count=%d\n", count);
145     This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
146     This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
147     for (i=0; i<count; i++) {
148       struct StublessThunk *thunk = &This->thunks[i];
149       if (vtbl->Vtbl[i] == (LPVOID)-1) {
150         PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
151         unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
152         TRACE("method %d: stacksize=%d\n", i, bytes);
153         FILL_STUBLESS(thunk, i, bytes)
154         This->PVtbl[i] = thunk;
155       }
156       else {
157         memset(thunk, 0, sizeof(struct StublessThunk));
158         This->PVtbl[i] = vtbl->Vtbl[i];
159       }
160     }
161   }
162   else 
163     This->PVtbl = vtbl->Vtbl;
164
165   This->lpVtbl = &StdProxy_Vtbl;
166   This->RefCount = 1;
167   This->stubless = stubless;
168   This->piid = vtbl->header.piid;
169   This->pUnkOuter = pUnkOuter;
170   This->pPSFactory = pPSFactory;
171   This->pChannel = NULL;
172   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
173   *ppvObj = &This->PVtbl;
174   IPSFactoryBuffer_AddRef(pPSFactory);
175
176   return S_OK;
177 }
178
179 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
180 {
181   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
182
183   IPSFactoryBuffer_Release(This->pPSFactory);
184   if (This->thunks) {
185     HeapFree(GetProcessHeap(),0,This->PVtbl);
186     HeapFree(GetProcessHeap(),0,This->thunks);
187   }
188   HeapFree(GetProcessHeap(),0,This);
189 }
190
191 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
192                                              REFIID riid,
193                                              LPVOID *obj)
194 {
195   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
196   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
197
198   if (IsEqualGUID(&IID_IUnknown,riid) ||
199       IsEqualGUID(This->piid,riid)) {
200     *obj = &This->PVtbl;
201     This->RefCount++;
202     return S_OK;
203   }
204
205   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
206     *obj = &This->lpVtbl;
207     This->RefCount++;
208     return S_OK;
209   }
210
211   return E_NOINTERFACE;
212 }
213
214 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
215 {
216   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
217   TRACE("(%p)->AddRef()\n",This);
218
219   return ++(This->RefCount);
220 }
221
222 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
223 {
224   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
225   TRACE("(%p)->Release()\n",This);
226
227   if (!--(This->RefCount)) {
228     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
229     return 0;
230   }
231   return This->RefCount;
232 }
233
234 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
235                                       LPRPCCHANNELBUFFER pChannel)
236 {
237   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
238   TRACE("(%p)->Connect(%p)\n",This,pChannel);
239
240   This->pChannel = pChannel;
241   return S_OK;
242 }
243
244 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
245 {
246   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
247   TRACE("(%p)->Disconnect()\n",This);
248
249   This->pChannel = NULL;
250 }
251
252 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
253 {
254   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
255   StdProxy_QueryInterface,
256   StdProxy_AddRef,
257   StdProxy_Release,
258   StdProxy_Connect,
259   StdProxy_Disconnect
260 };
261
262 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
263                                   LPRPCCHANNELBUFFER *ppChannel)
264 {
265   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
266   TRACE("(%p)->GetChannel(%p)\n",This,ppChannel);
267
268   *ppChannel = This->pChannel;
269   return S_OK;
270 }
271
272 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
273                               const IID **ppiid)
274 {
275   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
276   TRACE("(%p)->GetIID(%p)\n",This,ppiid);
277
278   *ppiid = This->piid;
279   return S_OK;
280 }
281
282 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
283                                             REFIID riid,
284                                             LPVOID *ppvObj)
285 {
286   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
287   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),ppvObj);
288   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
289 }
290
291 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
292 {
293   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
294   TRACE("(%p)->AddRef()\n",This);
295 #if 0 /* interface refcounting */
296   return ++(This->RefCount);
297 #else /* object refcounting */
298   return IUnknown_AddRef(This->pUnkOuter);
299 #endif
300 }
301
302 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
303 {
304   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
305   TRACE("(%p)->Release()\n",This);
306 #if 0 /* interface refcounting */
307   if (!--(This->RefCount)) {
308     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
309     return 0;
310   }
311   return This->RefCount;
312 #else /* object refcounting */
313   return IUnknown_Release(This->pUnkOuter);
314 #endif
315 }
316