ndr_ole.c 12.6 KB
Newer Older
1 2 3
/*
 * OLE32 callouts, COM interface marshalling
 *
4
 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 *
 * TODO:
21
 *  - fix the wire-protocol to match MS/RPC
22 23 24
 *  - finish RpcStream_Vtbl
 */

25
#include <stdarg.h>
26 27 28
#include <stdio.h>
#include <string.h>

29
#define COBJMACROS
30 31
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
32

33 34 35 36
#include "windef.h"
#include "winbase.h"
#include "winerror.h"

37
#include "objbase.h"
38

39
#include "ndr_misc.h"
40
#include "rpcndr.h"
41
#include "rpcproxy.h"
42
#include "wine/rpcfc.h"
43
#include "cpsf.h"
44 45 46 47 48 49 50

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(ole);

static HMODULE hOLE;

51 52 53 54 55 56 57 58
static HRESULT (WINAPI *COM_GetMarshalSizeMax)(ULONG *,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
static HRESULT (WINAPI *COM_MarshalInterface)(LPSTREAM,REFIID,LPUNKNOWN,DWORD,LPVOID,DWORD);
static HRESULT (WINAPI *COM_UnmarshalInterface)(LPSTREAM,REFIID,LPVOID*);
static HRESULT (WINAPI *COM_ReleaseMarshalData)(LPSTREAM);
static HRESULT (WINAPI *COM_GetClassObject)(REFCLSID,DWORD,COSERVERINFO *,REFIID,LPVOID *);
static HRESULT (WINAPI *COM_GetPSClsid)(REFIID,CLSID *);
static LPVOID (WINAPI *COM_MemAlloc)(ULONG);
static void (WINAPI *COM_MemFree)(LPVOID);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

static HMODULE LoadCOM(void)
{
  if (hOLE) return hOLE;
  hOLE = LoadLibraryA("OLE32.DLL");
  if (!hOLE) return 0;
  COM_GetMarshalSizeMax  = (LPVOID)GetProcAddress(hOLE, "CoGetMarshalSizeMax");
  COM_MarshalInterface   = (LPVOID)GetProcAddress(hOLE, "CoMarshalInterface");
  COM_UnmarshalInterface = (LPVOID)GetProcAddress(hOLE, "CoUnmarshalInterface");
  COM_ReleaseMarshalData = (LPVOID)GetProcAddress(hOLE, "CoReleaseMarshalData");
  COM_GetClassObject     = (LPVOID)GetProcAddress(hOLE, "CoGetClassObject");
  COM_GetPSClsid         = (LPVOID)GetProcAddress(hOLE, "CoGetPSClsid");
  COM_MemAlloc = (LPVOID)GetProcAddress(hOLE, "CoTaskMemAlloc");
  COM_MemFree  = (LPVOID)GetProcAddress(hOLE, "CoTaskMemFree");
  return hOLE;
}

/* CoMarshalInterface/CoUnmarshalInterface works on streams,
 * so implement a simple stream on top of the RPC buffer
 * (which also implements the MInterfacePointer structure) */
typedef struct RpcStreamImpl
{
81
  const IStreamVtbl *lpVtbl;
82
  LONG RefCount;
83 84
  PMIDL_STUB_MESSAGE pMsg;
  LPDWORD size;
85
  unsigned char *data;
86 87 88 89 90 91 92
  DWORD pos;
} RpcStreamImpl;

static HRESULT WINAPI RpcStream_QueryInterface(LPSTREAM iface,
                                              REFIID riid,
                                              LPVOID *obj)
{
93
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
94 95 96 97
  if (IsEqualGUID(&IID_IUnknown, riid) ||
      IsEqualGUID(&IID_ISequentialStream, riid) ||
      IsEqualGUID(&IID_IStream, riid)) {
    *obj = This;
98
    InterlockedIncrement( &This->RefCount );
99 100 101 102 103 104 105
    return S_OK;
  }
  return E_NOINTERFACE;
}

static ULONG WINAPI RpcStream_AddRef(LPSTREAM iface)
{
106
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
107
  return InterlockedIncrement( &This->RefCount );
108 109 110 111
}

static ULONG WINAPI RpcStream_Release(LPSTREAM iface)
{
112
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
113 114
  ULONG ref = InterlockedDecrement( &This->RefCount );
  if (!ref) {
115
    TRACE("size=%d\n", *This->size);
116
    This->pMsg->Buffer = This->data + *This->size;
117 118 119
    HeapFree(GetProcessHeap(),0,This);
    return 0;
  }
120
  return ref;
121 122 123 124 125 126 127
}

static HRESULT WINAPI RpcStream_Read(LPSTREAM iface,
                                    void *pv,
                                    ULONG cb,
                                    ULONG *pcbRead)
{
128
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
129 130 131 132 133 134
  HRESULT hr = S_OK;
  if (This->pos + cb > *This->size)
  {
    cb = *This->size - This->pos;
    hr = S_FALSE;
  }
135 136 137 138 139
  if (cb) {
    memcpy(pv, This->data + This->pos, cb);
    This->pos += cb;
  }
  if (pcbRead) *pcbRead = cb;
140
  return hr;
141 142 143 144 145 146 147
}

static HRESULT WINAPI RpcStream_Write(LPSTREAM iface,
                                     const void *pv,
                                     ULONG cb,
                                     ULONG *pcbWritten)
{
148
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
149
  if (This->data + cb > (unsigned char *)This->pMsg->RpcMsg->Buffer + This->pMsg->BufferLength)
150
    return STG_E_MEDIUMFULL;
151 152 153 154 155 156 157 158 159 160 161 162
  memcpy(This->data + This->pos, pv, cb);
  This->pos += cb;
  if (This->pos > *This->size) *This->size = This->pos;
  if (pcbWritten) *pcbWritten = cb;
  return S_OK;
}

static HRESULT WINAPI RpcStream_Seek(LPSTREAM iface,
                                    LARGE_INTEGER move,
                                    DWORD origin,
                                    ULARGE_INTEGER *newPos)
{
163
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
164 165
  switch (origin) {
  case STREAM_SEEK_SET:
166
    This->pos = move.u.LowPart;
167 168
    break;
  case STREAM_SEEK_CUR:
169
    This->pos = This->pos + move.u.LowPart;
170 171
    break;
  case STREAM_SEEK_END:
172
    This->pos = *This->size + move.u.LowPart;
173 174 175 176 177
    break;
  default:
    return STG_E_INVALIDFUNCTION;
  }
  if (newPos) {
178 179
    newPos->u.LowPart = This->pos;
    newPos->u.HighPart = 0;
180 181 182 183 184 185 186
  }
  return S_OK;
}

static HRESULT WINAPI RpcStream_SetSize(LPSTREAM iface,
                                       ULARGE_INTEGER newSize)
{
187
  RpcStreamImpl *This = (RpcStreamImpl *)iface;
188
  *This->size = newSize.u.LowPart;
189 190 191
  return S_OK;
}

192
static const IStreamVtbl RpcStream_Vtbl =
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
{
  RpcStream_QueryInterface,
  RpcStream_AddRef,
  RpcStream_Release,
  RpcStream_Read,
  RpcStream_Write,
  RpcStream_Seek,
  RpcStream_SetSize,
  NULL, /* CopyTo */
  NULL, /* Commit */
  NULL, /* Revert */
  NULL, /* LockRegion */
  NULL, /* UnlockRegion */
  NULL, /* Stat */
  NULL  /* Clone */
};

static LPSTREAM RpcStream_Create(PMIDL_STUB_MESSAGE pStubMsg, BOOL init)
{
  RpcStreamImpl *This;
  This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RpcStreamImpl));
  if (!This) return NULL;
  This->lpVtbl = &RpcStream_Vtbl;
  This->RefCount = 1;
  This->pMsg = pStubMsg;
  This->size = (LPDWORD)pStubMsg->Buffer;
219
  This->data = (unsigned char*)(This->size + 1);
220 221
  This->pos = 0;
  if (init) *This->size = 0;
222
  TRACE("init size=%d\n", *This->size);
223 224 225
  return (LPSTREAM)This;
}

226
static const IID* get_ip_iid(PMIDL_STUB_MESSAGE pStubMsg, unsigned char *pMemory, PFORMAT_STRING pFormat)
227 228 229 230 231
{
  const IID *riid;
  if (!pFormat) return &IID_IUnknown;
  TRACE("format=%02x %02x\n", pFormat[0], pFormat[1]);
  if (pFormat[0] != RPC_FC_IP) FIXME("format=%d\n", pFormat[0]);
232 233 234 235 236 237
  if (pFormat[1] == RPC_FC_CONSTANT_IID) {
    riid = (const IID *)&pFormat[2];
  } else {
    ComputeConformance(pStubMsg, pMemory, pFormat+2, 0);
    riid = (const IID *)pStubMsg->MaxCount;
  }
238
  if (!riid) riid = &IID_IUnknown;
239
  TRACE("got %s\n", debugstr_guid(riid));
240 241 242
  return riid;
}

243 244 245 246 247 248 249
/***********************************************************************
 *           NdrInterfacePointerMarshall [RPCRT4.@]
 */
unsigned char * WINAPI NdrInterfacePointerMarshall(PMIDL_STUB_MESSAGE pStubMsg,
                                                  unsigned char *pMemory,
                                                  PFORMAT_STRING pFormat)
{
250
  const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
251 252 253 254 255 256
  LPSTREAM stream;
  HRESULT hr;

  TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
  pStubMsg->MaxCount = 0;
  if (!LoadCOM()) return NULL;
257
  if (pStubMsg->Buffer + sizeof(DWORD) <= (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
258 259
    stream = RpcStream_Create(pStubMsg, TRUE);
    if (stream) {
260 261 262 263 264 265 266
      if (pMemory)
        hr = COM_MarshalInterface(stream, riid, (LPUNKNOWN)pMemory,
                                  pStubMsg->dwDestContext, pStubMsg->pvDestContext,
                                  MSHLFLAGS_NORMAL);
      else
        hr = S_OK;

267
      IStream_Release(stream);
268
      if (FAILED(hr))
269
        RpcRaiseException(hr);
270 271
    }
  }
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  return NULL;
}

/***********************************************************************
 *           NdrInterfacePointerUnmarshall [RPCRT4.@]
 */
unsigned char * WINAPI NdrInterfacePointerUnmarshall(PMIDL_STUB_MESSAGE pStubMsg,
                                                    unsigned char **ppMemory,
                                                    PFORMAT_STRING pFormat,
                                                    unsigned char fMustAlloc)
{
  LPSTREAM stream;
  HRESULT hr;

  TRACE("(%p,%p,%p,%d)\n", pStubMsg, ppMemory, pFormat, fMustAlloc);
  if (!LoadCOM()) return NULL;
  *(LPVOID*)ppMemory = NULL;
289
  if (pStubMsg->Buffer + sizeof(DWORD) < (unsigned char *)pStubMsg->RpcMsg->Buffer + pStubMsg->BufferLength) {
290
    stream = RpcStream_Create(pStubMsg, FALSE);
291 292
    if (!stream) RpcRaiseException(E_OUTOFMEMORY);
    if (*((RpcStreamImpl *)stream)->size != 0)
293
      hr = COM_UnmarshalInterface(stream, &IID_NULL, (LPVOID*)ppMemory);
294 295 296 297
    else
      hr = S_OK;
    IStream_Release(stream);
    if (FAILED(hr))
298
        RpcRaiseException(hr);
299
  }
300 301 302 303 304 305 306 307 308 309
  return NULL;
}

/***********************************************************************
 *           NdrInterfacePointerBufferSize [RPCRT4.@]
 */
void WINAPI NdrInterfacePointerBufferSize(PMIDL_STUB_MESSAGE pStubMsg,
                                         unsigned char *pMemory,
                                         PFORMAT_STRING pFormat)
{
310
  const IID *riid = get_ip_iid(pStubMsg, pMemory, pFormat);
311 312 313 314 315 316 317 318
  ULONG size = 0;
  HRESULT hr;

  TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
  if (!LoadCOM()) return;
  hr = COM_GetMarshalSizeMax(&size, riid, (LPUNKNOWN)pMemory,
                            pStubMsg->dwDestContext, pStubMsg->pvDestContext,
                            MSHLFLAGS_NORMAL);
319
  TRACE("size=%d\n", size);
320
  pStubMsg->BufferLength += sizeof(DWORD) + size;
321 322 323 324 325
}

/***********************************************************************
 *           NdrInterfacePointerMemorySize [RPCRT4.@]
 */
326 327
ULONG WINAPI NdrInterfacePointerMemorySize(PMIDL_STUB_MESSAGE pStubMsg,
                                           PFORMAT_STRING pFormat)
328
{
329 330 331 332 333 334 335 336 337 338 339
  ULONG size;

  TRACE("(%p,%p)\n", pStubMsg, pFormat);

  size = *(ULONG *)pStubMsg->Buffer;
  pStubMsg->Buffer += 4;
  pStubMsg->MemorySize += 4;

  pStubMsg->Buffer += size;

  return pStubMsg->MemorySize;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

/***********************************************************************
 *           NdrInterfacePointerFree [RPCRT4.@]
 */
void WINAPI NdrInterfacePointerFree(PMIDL_STUB_MESSAGE pStubMsg,
                                   unsigned char *pMemory,
                                   PFORMAT_STRING pFormat)
{
  LPUNKNOWN pUnk = (LPUNKNOWN)pMemory;
  TRACE("(%p,%p,%p)\n", pStubMsg, pMemory, pFormat);
  if (pUnk) IUnknown_Release(pUnk);
}

/***********************************************************************
 *           NdrOleAllocate [RPCRT4.@]
 */
357
void * WINAPI NdrOleAllocate(SIZE_T Size)
358 359 360 361 362 363 364 365 366 367 368 369 370
{
  if (!LoadCOM()) return NULL;
  return COM_MemAlloc(Size);
}

/***********************************************************************
 *           NdrOleFree [RPCRT4.@]
 */
void WINAPI NdrOleFree(void *NodeToFree)
{
  if (!LoadCOM()) return;
  COM_MemFree(NodeToFree);
}
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/***********************************************************************
 * Helper function to create a proxy.
 * Probably similar to NdrpCreateProxy.
 */
HRESULT create_proxy(REFIID iid, IUnknown *pUnkOuter, IRpcProxyBuffer **pproxy, void **ppv)
{
    CLSID clsid;
    IPSFactoryBuffer *psfac;
    HRESULT r;

    if(!LoadCOM()) return E_FAIL;

    r = COM_GetPSClsid( iid, &clsid );
    if(FAILED(r)) return r;

    r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
    if(FAILED(r)) return r;

    r = IPSFactoryBuffer_CreateProxy(psfac, pUnkOuter, iid, pproxy, ppv);

    IPSFactoryBuffer_Release(psfac);
    return r;
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
/***********************************************************************
 * Helper function to create a stub.
 * This probably looks very much like NdrpCreateStub.
 */
HRESULT create_stub(REFIID iid, IUnknown *pUnk, IRpcStubBuffer **ppstub)
{
    CLSID clsid;
    IPSFactoryBuffer *psfac;
    HRESULT r;

    if(!LoadCOM()) return E_FAIL;

    r = COM_GetPSClsid( iid, &clsid );
    if(FAILED(r)) return r;

    r = COM_GetClassObject( &clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IPSFactoryBuffer, (void**)&psfac );
    if(FAILED(r)) return r;

    r = IPSFactoryBuffer_CreateStub(psfac, iid, pUnk, ppstub);

    IPSFactoryBuffer_Release(psfac);
    return r;
}