Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4663 lines
132 KiB

  1. //************************************************************************
  2. //
  3. // Copyright (C) Microsoft Corporation, 1996-2000.
  4. //
  5. // File: typeinfo.cxx
  6. //
  7. // Contents: Generates -Oi2 proxies and stubs from an ITypeInfo.
  8. //
  9. // Functions: CacheRegister
  10. // CacheRelease
  11. // CacheLookup
  12. //
  13. // History: 26-Apr-96 ShannonC Created
  14. // June 1997 YongQu Add UDT support
  15. // Oct 1998 YongQu arbitrary length vtbl
  16. //
  17. //----------------------------------------------------------------------------
  18. #include <typeinfo.h>
  19. #include <interp.h>
  20. #include <stddef.h>
  21. #include <ndrtypes.h>
  22. #include <tiutil.h>
  23. #ifdef DOSWIN32RPC
  24. #include <critsec.hxx>
  25. #endif
  26. #include <sysinc.h>
  27. #include <limits.h>
  28. #include "fmtstr.h"
  29. #define GetWireSize(x) (x->WireSize)
  30. #define GetMemorySize(x) (x->MemorySize)
  31. #define GetMemoryAlignment(x) (x->MemoryAlignment)
  32. #define GetWireAlignment(x) (x->WireAlignment)
  33. extern const IRpcProxyBufferVtbl CStdProxyBuffer3Vtbl = {
  34. CStdProxyBuffer_QueryInterface,
  35. CStdProxyBuffer_AddRef,
  36. CStdProxyBuffer3_Release,
  37. CStdProxyBuffer2_Connect,
  38. CStdProxyBuffer2_Disconnect };
  39. HRESULT NdrpInitializeStublessVtbl(ULONG numMethods);
  40. void GetTemplateVtbl(void *** pVtbl);
  41. void ReleaseTemplateVtbl(void ** pVtbl);
  42. void GetTemplateForwardVtbl(void *** pVtbl);
  43. void ReleaseTemplateForwardVtbl(void ** pVtbl);
  44. extern const IReleaseMarshalBuffersVtbl CStdProxyBuffer_ReleaseMarshalBuffersVtbl;
  45. extern const IReleaseMarshalBuffersVtbl CStdStubBuffer_ReleaseMarshalBuffersVtbl;
  46. static I_RPC_MUTEX TypeInfoMutex = 0;
  47. //#define CACHE_BLOCK 32
  48. //#define INIT_HIGH_MARK 20
  49. //#define THRASHING_TIME 1000*30 // 30 sec
  50. //The constants can be tunable instead of constants.
  51. static LONG CACHE_BLOCK=32;
  52. static ULONG INIT_HIGH_MARK = 20;
  53. static ULONG IDLE_TIME= 1000 * 60 *5; // 5 minutes.
  54. #define THRASHING_TIME 1000*30
  55. #define DELTA_MARK 4 // shrink 25% each time.
  56. HINSTANCE hOleAut32 = 0;
  57. // local to this file only
  58. static TypeInfoCache* g_pCache = NULL;//array of cache entries.
  59. static LONG g_lActiveCacheRef = 0;
  60. static LONG g_lCacheSize = 0;
  61. static LONG g_lTotalCacheRef = 0;
  62. #ifdef DEBUGRPC
  63. static LONG g_lCount = 0;
  64. #endif
  65. #if defined(_IA64_)
  66. #define ARGS_IN_REGISTERS 8
  67. #elif defined(_AMD64_)
  68. #define ARGS_IN_REGISTERS 4
  69. #endif
  70. //+---------------------------------------------------------------------------
  71. //
  72. // Function: CreateProxyFromTypeInfo
  73. //
  74. // Synopsis: Creates an interface proxy using the type information supplied
  75. // in pTypeInfo.
  76. //
  77. // Arguments:
  78. // pTypeInfo - Supplies the ITypeInfo * describing the interface.
  79. // punkOuter - Specifies the controlling unknown.
  80. // riid - Specifies the interface ID.
  81. // ppProxy - Returns a pointer to the IRpcProxyBuffer interface.
  82. // ppv - Returns a pointer to the specified interface.
  83. //
  84. // Returns:
  85. // S_OK
  86. // E_NOINTERFACE
  87. // E_OUTOFMEMORY
  88. //
  89. //----------------------------------------------------------------------------
  90. HRESULT STDAPICALLTYPE
  91. CreateProxyFromTypeInfo
  92. (
  93. IN ITypeInfo * pTypeInfo,
  94. IN IUnknown * punkOuter,
  95. IN REFIID riid,
  96. OUT IRpcProxyBuffer ** ppProxy,
  97. OUT void ** ppv
  98. )
  99. {
  100. HRESULT hr = E_FAIL;
  101. BOOL fIsDual;
  102. void * pVtbl;
  103. *ppProxy = NULL;
  104. *ppv = NULL;
  105. //Get the proxy vtable.
  106. hr = GetProxyVtblFromTypeInfo(pTypeInfo, riid, &fIsDual, &pVtbl);
  107. if(SUCCEEDED(hr))
  108. {
  109. //Create the proxy.
  110. CStdProxyBuffer2 *pProxyBuffer;
  111. pProxyBuffer = new CStdProxyBuffer2;
  112. if(pProxyBuffer != NULL)
  113. {
  114. memset(pProxyBuffer, 0, sizeof(CStdProxyBuffer2));
  115. pProxyBuffer->lpVtbl = &CStdProxyBuffer3Vtbl;
  116. pProxyBuffer->RefCount = 1;
  117. pProxyBuffer->punkOuter = punkOuter ?
  118. punkOuter : (IUnknown *) pProxyBuffer;
  119. pProxyBuffer->pProxyVtbl = pVtbl;
  120. pProxyBuffer->pRMBVtbl = &CStdProxyBuffer_ReleaseMarshalBuffersVtbl;
  121. if(fIsDual)
  122. {
  123. pProxyBuffer->iidBase = IID_IDispatch;
  124. //Create the proxy for the base interface.
  125. hr = NdrpCreateProxy(IID_IDispatch,
  126. (IUnknown *) pProxyBuffer,
  127. &pProxyBuffer->pBaseProxyBuffer,
  128. (void **)&pProxyBuffer->pBaseProxy);
  129. }
  130. else
  131. {
  132. hr = S_OK;
  133. }
  134. if(SUCCEEDED(hr))
  135. {
  136. *ppProxy = (IRpcProxyBuffer *) pProxyBuffer;
  137. pProxyBuffer->punkOuter->lpVtbl->AddRef(pProxyBuffer->punkOuter);
  138. *ppv = &pProxyBuffer->pProxyVtbl;
  139. }
  140. else
  141. {
  142. delete pProxyBuffer;
  143. }
  144. }
  145. else
  146. {
  147. hr = E_OUTOFMEMORY;
  148. }
  149. if(FAILED(hr))
  150. {
  151. ReleaseProxyVtbl(pVtbl);
  152. }
  153. }
  154. return hr;
  155. }
  156. //+---------------------------------------------------------------------------
  157. //
  158. // Function: GetProxyVtblFromTypeInfo
  159. //
  160. // Synopsis: Get a pointer to the proxy vtbl. The proxy vtbl should be
  161. // released via ReleaseProxyVtbl.
  162. //
  163. // Arguments:
  164. //
  165. // Returns:
  166. // S_OK
  167. //
  168. //----------------------------------------------------------------------------
  169. HRESULT GetProxyVtblFromTypeInfo
  170. (
  171. IN ITypeInfo * pTypeInfo,
  172. IN REFIID riid,
  173. OUT BOOL * pfIsDual,
  174. OUT void ** ppVtbl
  175. )
  176. {
  177. HRESULT hr = E_FAIL;
  178. TypeInfoVtbl *pInfo;
  179. //Get the vtbl.
  180. hr = GetVtbl(pTypeInfo, riid, &pInfo);
  181. if(SUCCEEDED(hr))
  182. {
  183. *pfIsDual = pInfo->fIsDual;
  184. *ppVtbl = &pInfo->proxyVtbl.Vtbl;
  185. }
  186. else
  187. {
  188. *ppVtbl = NULL;
  189. }
  190. return hr;
  191. }
  192. //+---------------------------------------------------------------------------
  193. //
  194. // Function: CreateStubFromTypeInfo
  195. //
  196. // Synopsis: Create an interface stub from the type information
  197. // supplied in pTypeInfo.
  198. //
  199. // Arguments:
  200. //
  201. // Returns:
  202. // S_OK
  203. //
  204. //----------------------------------------------------------------------------
  205. HRESULT STDAPICALLTYPE
  206. CreateStubFromTypeInfo
  207. (
  208. IN ITypeInfo * pTypeInfo,
  209. IN REFIID riid,
  210. IN IUnknown * punkServer,
  211. OUT IRpcStubBuffer ** ppStub
  212. )
  213. {
  214. HRESULT hr = E_FAIL;
  215. BOOL fIsDual;
  216. IRpcStubBufferVtbl *pVtbl;
  217. void ** pForwardingVtbl;
  218. *ppStub = NULL;
  219. //Get the stub vtable.
  220. hr = GetStubVtblFromTypeInfo(pTypeInfo, riid, &fIsDual, &pVtbl);
  221. if(SUCCEEDED(hr))
  222. {
  223. //Create the stub
  224. IUnknown * punkForward;
  225. CStdStubBuffer2 *pStubBuffer = new CStdStubBuffer2;
  226. if(pStubBuffer != NULL)
  227. {
  228. GetTemplateForwardVtbl(&pForwardingVtbl);
  229. //Initialize the new stub buffer.
  230. pStubBuffer->lpForwardingVtbl = pForwardingVtbl;
  231. pStubBuffer->pBaseStubBuffer = 0;
  232. pStubBuffer->lpVtbl = pVtbl;
  233. pStubBuffer->RefCount= 1;
  234. pStubBuffer->pvServerObject = 0;
  235. pStubBuffer->pRMBVtbl = &CStdStubBuffer_ReleaseMarshalBuffersVtbl;
  236. *ppStub = (IRpcStubBuffer *) &pStubBuffer->lpVtbl;
  237. //Connect the stub to the server object.
  238. if(punkServer != 0)
  239. {
  240. hr = punkServer->lpVtbl->QueryInterface(
  241. punkServer,
  242. riid,
  243. (void **) &pStubBuffer->pvServerObject);
  244. }
  245. else
  246. {
  247. hr = S_OK;
  248. }
  249. if(SUCCEEDED(hr))
  250. {
  251. if(punkServer != 0)
  252. punkForward = (IUnknown *) &pStubBuffer->lpForwardingVtbl;
  253. else
  254. punkForward = 0;
  255. if(fIsDual)
  256. {
  257. //Create a stub for the base interface
  258. hr = NdrpCreateStub(IID_IDispatch,
  259. punkForward,
  260. &pStubBuffer->pBaseStubBuffer);
  261. }
  262. if(FAILED(hr))
  263. {
  264. if(pStubBuffer->pvServerObject)
  265. pStubBuffer->pvServerObject->lpVtbl->Release(pStubBuffer->pvServerObject);
  266. NdrOleFree(pStubBuffer);
  267. }
  268. }
  269. else
  270. {
  271. hr = E_OUTOFMEMORY;
  272. }
  273. }
  274. if(FAILED(hr))
  275. {
  276. ReleaseTemplateForwardVtbl(pForwardingVtbl);
  277. ReleaseStubVtbl(pVtbl);
  278. }
  279. }
  280. return hr;
  281. }
  282. //+---------------------------------------------------------------------------
  283. //
  284. // Function: GetStubVtblFromTypeInfo
  285. //
  286. // Synopsis: Get a pointer to the stub vtbl. The stub vtbl should be
  287. // released via ReleaseStubVtbl.
  288. //
  289. // Arguments:
  290. //
  291. // Returns:
  292. // S_OK
  293. //
  294. //----------------------------------------------------------------------------
  295. HRESULT GetStubVtblFromTypeInfo(
  296. IN ITypeInfo * pTypeInfo,
  297. IN REFIID riid,
  298. OUT BOOL * pfIsDual,
  299. OUT IRpcStubBufferVtbl ** ppVtbl)
  300. {
  301. HRESULT hr = E_FAIL;
  302. TypeInfoVtbl *pInfo;
  303. //Get the vtbl.
  304. hr = GetVtbl(pTypeInfo, riid, &pInfo);
  305. if(SUCCEEDED(hr))
  306. {
  307. *pfIsDual = pInfo->fIsDual;
  308. *ppVtbl = &pInfo->stubVtbl.Vtbl;
  309. }
  310. return hr;
  311. }
  312. HRESULT CheckTypeInfo(
  313. IN ITypeInfo *pTypeInfo,
  314. OUT ITypeInfo **pptinfoProxy,
  315. OUT USHORT *pcMethods,
  316. OUT BOOL *pfIsDual)
  317. {
  318. HRESULT hr;
  319. TYPEATTR * pTypeAttr;
  320. HREFTYPE hRefType;
  321. UINT cbSizeVft = 0;
  322. ITypeInfo *ptinfoProxy = NULL;
  323. USHORT cMethods;
  324. *pfIsDual = FALSE;
  325. hr = pTypeInfo->lpVtbl->GetTypeAttr(pTypeInfo, &pTypeAttr);
  326. if(SUCCEEDED(hr))
  327. {
  328. if(pTypeAttr->wTypeFlags & TYPEFLAG_FDUAL)
  329. {
  330. *pfIsDual = TRUE;
  331. if(TKIND_DISPATCH == pTypeAttr->typekind)
  332. {
  333. //Get the TKIND_INTERFACE type info.
  334. hr = pTypeInfo->lpVtbl->GetRefTypeOfImplType(pTypeInfo, (UINT) -1, &hRefType);
  335. if(SUCCEEDED(hr))
  336. {
  337. hr = pTypeInfo->lpVtbl->GetRefTypeInfo(pTypeInfo, hRefType, &ptinfoProxy);
  338. if(SUCCEEDED(hr))
  339. {
  340. TYPEATTR * ptattrProxy;
  341. hr = ptinfoProxy->lpVtbl->GetTypeAttr(ptinfoProxy, &ptattrProxy);
  342. if(SUCCEEDED(hr))
  343. {
  344. cbSizeVft = ptattrProxy->cbSizeVft;
  345. ptinfoProxy->lpVtbl->ReleaseTypeAttr(ptinfoProxy, ptattrProxy);
  346. }
  347. }
  348. }
  349. }
  350. else if (TKIND_INTERFACE == pTypeAttr->typekind)
  351. {
  352. pTypeInfo->lpVtbl->AddRef(pTypeInfo);
  353. ptinfoProxy = pTypeInfo;
  354. cbSizeVft = pTypeAttr->cbSizeVft;
  355. }
  356. else
  357. {
  358. hr = E_FAIL;
  359. }
  360. }
  361. else if((pTypeAttr->wTypeFlags & TYPEFLAG_FOLEAUTOMATION) &&
  362. (TKIND_INTERFACE == pTypeAttr->typekind))
  363. {
  364. pTypeInfo->lpVtbl->AddRef(pTypeInfo);
  365. ptinfoProxy = pTypeInfo;
  366. cbSizeVft = pTypeAttr->cbSizeVft;
  367. }
  368. else
  369. {
  370. hr = E_FAIL;
  371. }
  372. pTypeInfo->lpVtbl->ReleaseTypeAttr(pTypeInfo, pTypeAttr);
  373. }
  374. cMethods = (USHORT) ( (cbSizeVft - VTABLE_BASE) / sizeof(void *) );
  375. if(SUCCEEDED(hr))
  376. {
  377. *pptinfoProxy = ptinfoProxy;
  378. //Calculate the number of methods in the vtable.
  379. *pcMethods = cMethods;
  380. }
  381. else
  382. {
  383. *pptinfoProxy = NULL;
  384. if(ptinfoProxy != NULL)
  385. {
  386. ptinfoProxy->lpVtbl->Release(ptinfoProxy);
  387. }
  388. }
  389. return hr;
  390. }
  391. //+---------------------------------------------------------------------------
  392. //
  393. // Function: GetVtbl
  394. //
  395. // Synopsis: Get a pointer to the vtbl structure.
  396. //
  397. // Returns:
  398. // S_OK
  399. //
  400. //----------------------------------------------------------------------------
  401. HRESULT GetVtbl(
  402. IN ITypeInfo * pTypeInfo,
  403. IN REFIID riid,
  404. OUT TypeInfoVtbl ** ppVtbl)
  405. {
  406. HRESULT hr;
  407. RPC_STATUS rc;
  408. USHORT numMethods;
  409. MethodInfo * aMethodInfo;
  410. BOOL fIsDual = FALSE;
  411. ITypeInfo * ptinfoProxy = NULL;
  412. *ppVtbl = NULL;
  413. rc = NdrpPerformRpcInitialization();
  414. if (RPC_S_OK != rc)
  415. return HRESULT_FROM_WIN32(rc);
  416. // the other two mutexes will be initialized in NdrpInitializeStublessVtbl
  417. if ( TypeInfoMutex == NULL )
  418. {
  419. hr = NdrpInitializeMutex( &TypeInfoMutex );
  420. if ( FAILED( hr ) )
  421. return hr;
  422. }
  423. //Check the cache.
  424. I_RpcRequestMutex(&TypeInfoMutex);
  425. hr = CacheLookup(riid, ppVtbl);
  426. I_RpcClearMutex(TypeInfoMutex);
  427. if(FAILED(hr))
  428. {
  429. //We didn't find the interface in the cache.
  430. //Create a vtbl from the ITypeInfo.
  431. hr = CheckTypeInfo(pTypeInfo, &ptinfoProxy, &numMethods, &fIsDual);
  432. if(SUCCEEDED(hr))
  433. {
  434. //allocate space for per-method data.
  435. aMethodInfo = (MethodInfo *) alloca(numMethods * sizeof(MethodInfo));
  436. if(aMethodInfo != NULL)
  437. {
  438. memset(aMethodInfo, 0, numMethods * sizeof(MethodInfo));
  439. //Get the per-method data.
  440. hr = GetFuncDescs(ptinfoProxy, aMethodInfo);
  441. if(SUCCEEDED(hr))
  442. {
  443. hr = CreateVtblFromTypeInfo(riid, fIsDual, numMethods, aMethodInfo, ppVtbl);
  444. if(SUCCEEDED(hr))
  445. {
  446. hr = CacheRegister(riid,ppVtbl);
  447. }
  448. }
  449. ReleaseFuncDescs(numMethods, aMethodInfo);
  450. }
  451. else
  452. {
  453. hr = E_OUTOFMEMORY;
  454. }
  455. ptinfoProxy->lpVtbl->Release(ptinfoProxy);
  456. }
  457. }
  458. return hr;
  459. }
  460. //+---------------------------------------------------------------------------
  461. //
  462. // Function: CreateVtblFromTypeInfo
  463. //
  464. // Synopsis: Create a vtbl structure from the type information.
  465. //
  466. // Arguments:
  467. //
  468. // Returns:
  469. // S_OK
  470. //
  471. //----------------------------------------------------------------------------
  472. HRESULT CreateVtblFromTypeInfo(
  473. IN REFIID riid,
  474. IN BOOL fIsDual,
  475. IN USHORT numMethods,
  476. IN MethodInfo * pMethodInfo,
  477. OUT TypeInfoVtbl ** ppVtbl)
  478. {
  479. HRESULT hr = S_OK;
  480. USHORT iMethod;
  481. ULONG cbVtbl;
  482. ULONG cbOffsetTable;
  483. USHORT cbProcFormatString = 0;
  484. ULONG cbSize;
  485. TypeInfoVtbl *pInfo = NULL;
  486. byte *pTemp;
  487. PFORMAT_STRING pTypeFormatString = NULL;
  488. PFORMAT_STRING pProcFormatString;
  489. unsigned short *pFormatStringOffsetTable;
  490. CTypeGen typeGen;
  491. CProcGen procGen;
  492. USHORT cbFormat;
  493. USHORT offset = 0;
  494. ULONG cbDelegationTable;
  495. void **pDispatchTable = NULL;
  496. void **pStublessClientVtbl = NULL;
  497. void **pForwardingVtbl = NULL;
  498. *ppVtbl = NULL;
  499. #ifdef DEBUGRPC
  500. InterlockedIncrement(&g_lCount);
  501. #endif
  502. //Compute the size of the vtbl structure;
  503. cbVtbl = numMethods * sizeof(void *);
  504. if(fIsDual)
  505. cbDelegationTable = cbVtbl;
  506. else
  507. cbDelegationTable = 0;
  508. cbOffsetTable = numMethods * sizeof(USHORT);
  509. //Compute the size of the proc format string.
  510. for(iMethod = 3;
  511. iMethod < numMethods;
  512. iMethod++)
  513. {
  514. if(pMethodInfo[iMethod].pFuncDesc != NULL)
  515. {
  516. #if !defined(__RPC_WIN64__)
  517. cbProcFormatString += 22;
  518. #else
  519. // proc format string in 64bit is longer
  520. cbProcFormatString += 22 + sizeof(NDR_PROC_HEADER_EXTS64);
  521. #endif
  522. cbProcFormatString += pMethodInfo[iMethod].pFuncDesc->cParams * 6;
  523. }
  524. }
  525. cbSize = sizeof(TypeInfoVtbl) + cbVtbl + cbDelegationTable + cbOffsetTable + cbProcFormatString;
  526. //Allocate the structure
  527. pInfo = (TypeInfoVtbl *) I_RpcAllocate(cbSize);
  528. if(pInfo != NULL)
  529. {
  530. memset(pInfo, 0, cbSize);
  531. pTemp = (byte *) pInfo->proxyVtbl.Vtbl + cbVtbl;
  532. if(cbDelegationTable != 0)
  533. {
  534. pDispatchTable = (void **) pTemp;
  535. pInfo->stubVtbl.header.pDispatchTable = (const PRPC_STUB_FUNCTION *) pDispatchTable;
  536. pTemp += cbDelegationTable;
  537. }
  538. pFormatStringOffsetTable = (unsigned short *) pTemp;
  539. pTemp += cbOffsetTable;
  540. pProcFormatString = (PFORMAT_STRING) pTemp;
  541. pInfo->proxyVtbl.Vtbl[0] = IUnknown_QueryInterface_Proxy;
  542. pInfo->proxyVtbl.Vtbl[1] = IUnknown_AddRef_Proxy;
  543. pInfo->proxyVtbl.Vtbl[2] = IUnknown_Release_Proxy;
  544. hr = NdrpInitializeStublessVtbl((ULONG)numMethods);
  545. if (FAILED(hr))
  546. {
  547. if (pInfo)
  548. I_RpcFree(pInfo);
  549. return hr;
  550. }
  551. //Get the format strings.
  552. //Generate -Oi2 proc format string from the ITypeInfo.
  553. GetTemplateVtbl(&pStublessClientVtbl);
  554. GetTemplateForwardVtbl(&pForwardingVtbl);
  555. for(iMethod = 3;
  556. SUCCEEDED(hr) && iMethod < numMethods;
  557. iMethod++)
  558. {
  559. if(pMethodInfo[iMethod].pFuncDesc != NULL)
  560. {
  561. pFormatStringOffsetTable[iMethod] = offset;
  562. hr = procGen.GetProcFormat(&typeGen,
  563. pMethodInfo[iMethod].pTypeInfo,
  564. pMethodInfo[iMethod].pFuncDesc,
  565. iMethod,
  566. (PFORMAT_STRING)pTemp,
  567. &cbFormat);
  568. if (FAILED(hr))
  569. {
  570. ReleaseTemplateVtbl(pStublessClientVtbl);
  571. ReleaseTemplateForwardVtbl(pForwardingVtbl);
  572. if (pInfo)
  573. I_RpcFree(pInfo);
  574. return hr;
  575. }
  576. pTemp += cbFormat;
  577. offset += cbFormat;
  578. //Stubless client function.
  579. pInfo->proxyVtbl.Vtbl[iMethod] = pStublessClientVtbl[iMethod];
  580. if(pDispatchTable != NULL)
  581. {
  582. //Interpreted server function.
  583. pDispatchTable[iMethod] = NdrStubCall2;
  584. }
  585. }
  586. else
  587. {
  588. pFormatStringOffsetTable[iMethod] = (USHORT) -1;
  589. //Proxy delegation forwarding function.
  590. pInfo->proxyVtbl.Vtbl[iMethod] = pForwardingVtbl[iMethod];
  591. if(pDispatchTable != NULL)
  592. {
  593. //Stub delegation forwarding function.
  594. pDispatchTable[iMethod] = NdrStubForwardingFunction;
  595. }
  596. }
  597. }
  598. ReleaseTemplateForwardVtbl(pForwardingVtbl);
  599. ReleaseTemplateVtbl(pStublessClientVtbl);
  600. if(SUCCEEDED(hr))
  601. {
  602. USHORT length;
  603. hr = typeGen.GetTypeFormatString(&pTypeFormatString, &length);
  604. }
  605. if(SUCCEEDED(hr))
  606. {
  607. //Initialize the vtbl.
  608. pInfo->cRefs = 1;
  609. //Initialize the iid.
  610. pInfo->iid = riid;
  611. pInfo->fIsDual = fIsDual;
  612. //Initialize the MIDL_STUB_DESC.
  613. pInfo->stubDesc.pfnAllocate = NdrOleAllocate;
  614. pInfo->stubDesc.pfnFree = NdrOleFree;
  615. //pInfo->stubDesc.apfnExprEval = ExprEvalRoutines;
  616. pInfo->stubDesc.pFormatTypes = pTypeFormatString;
  617. #if !defined(__RPC_WIN64__)
  618. pInfo->stubDesc.Version = 0x20000; /* Ndr library version */
  619. pInfo->stubDesc.MIDLVersion = MIDL_VERSION_3_0_44;
  620. #else
  621. pInfo->stubDesc.Version = 0x50002; /* Ndr library version */
  622. pInfo->stubDesc.MIDLVersion = MIDL_VERSION_5_2_202;
  623. #endif
  624. pInfo->stubDesc.aUserMarshalQuadruple = UserMarshalRoutines;
  625. //Initialize the MIDL_SERVER_INFO.
  626. pInfo->stubInfo.pStubDesc = &pInfo->stubDesc;
  627. pInfo->stubInfo.ProcString = pProcFormatString;
  628. pInfo->stubInfo.FmtStringOffset = pFormatStringOffsetTable;
  629. //Initialize the stub vtbl.
  630. pInfo->stubVtbl.header.piid = &pInfo->iid;
  631. pInfo->stubVtbl.header.pServerInfo = &pInfo->stubInfo;
  632. pInfo->stubVtbl.header.DispatchTableCount = numMethods;
  633. //Initialize stub methods.
  634. memcpy(&pInfo->stubVtbl.Vtbl, &CStdStubBuffer2Vtbl, sizeof(CStdStubBuffer2Vtbl));
  635. pInfo->stubVtbl.Vtbl.Release = CStdStubBuffer3_Release;
  636. //Initialize the proxy info.
  637. pInfo->proxyInfo.pStubDesc = &pInfo->stubDesc;
  638. pInfo->proxyInfo.ProcFormatString = pProcFormatString;
  639. pInfo->proxyInfo.FormatStringOffset = pFormatStringOffsetTable;
  640. //Initialize the proxy vtbl.
  641. pInfo->proxyVtbl.header.pStublessProxyInfo = &pInfo->proxyInfo;
  642. pInfo->proxyVtbl.header.piid = &pInfo->iid;
  643. *ppVtbl = pInfo;
  644. }
  645. else
  646. {
  647. //Free the memory.
  648. I_RpcFree(pInfo);
  649. }
  650. }
  651. else
  652. {
  653. hr = E_OUTOFMEMORY;
  654. }
  655. return hr;
  656. }
  657. //+---------------------------------------------------------------------------
  658. //
  659. // Function: GetFuncDescs
  660. //
  661. // Synopsis: Get the funcdesc for each method.
  662. //
  663. // Returns:
  664. // S_OK
  665. //
  666. //----------------------------------------------------------------------------
  667. HRESULT GetFuncDescs(
  668. IN ITypeInfo *pTypeInfo,
  669. OUT MethodInfo *pMethodInfo)
  670. {
  671. HRESULT hr;
  672. TYPEATTR *pTypeAttr;
  673. HREFTYPE hRefType;
  674. ITypeInfo *pRefTypeInfo;
  675. hr = pTypeInfo->lpVtbl->GetTypeAttr(pTypeInfo, &pTypeAttr);
  676. if(SUCCEEDED(hr))
  677. {
  678. if(IsEqualIID(IID_IUnknown, pTypeAttr->guid))
  679. {
  680. hr = S_OK;
  681. }
  682. else if(IsEqualIID(IID_IDispatch, pTypeAttr->guid))
  683. {
  684. hr = S_OK;
  685. }
  686. else
  687. {
  688. //This is an oleautomation interface.
  689. ULONG i, iMethod;
  690. FUNCDESC *pFuncDesc;
  691. if(pTypeAttr->cImplTypes)
  692. {
  693. //Recursively get the inherited member functions.
  694. hr = pTypeInfo->lpVtbl->GetRefTypeOfImplType(pTypeInfo, 0, &hRefType);
  695. if(SUCCEEDED(hr))
  696. {
  697. hr = pTypeInfo->lpVtbl->GetRefTypeInfo(pTypeInfo, hRefType, &pRefTypeInfo);
  698. if(SUCCEEDED(hr))
  699. {
  700. hr = GetFuncDescs(pRefTypeInfo, pMethodInfo);
  701. pRefTypeInfo->lpVtbl->Release(pRefTypeInfo);
  702. }
  703. }
  704. }
  705. //Get the member functions.
  706. for(i = 0; SUCCEEDED(hr) && i < pTypeAttr->cFuncs; i++)
  707. {
  708. hr = pTypeInfo->lpVtbl->GetFuncDesc(pTypeInfo, i, &pFuncDesc);
  709. if(SUCCEEDED(hr))
  710. {
  711. iMethod = (pFuncDesc->oVft - VTABLE_BASE) / sizeof(void *);
  712. pMethodInfo[iMethod].pFuncDesc = pFuncDesc;
  713. pTypeInfo->lpVtbl->AddRef(pTypeInfo);
  714. pMethodInfo[iMethod].pTypeInfo = pTypeInfo;
  715. }
  716. }
  717. }
  718. pTypeInfo->lpVtbl->ReleaseTypeAttr(pTypeInfo, pTypeAttr);
  719. }
  720. return hr;
  721. }
  722. //+---------------------------------------------------------------------------
  723. //
  724. // Function: ReleaseFuncDescs
  725. //
  726. // Synopsis: Release the funcdescs.
  727. //
  728. // Returns:
  729. // S_OK
  730. //
  731. //----------------------------------------------------------------------------
  732. HRESULT ReleaseFuncDescs(USHORT numMethods, MethodInfo *pMethodInfo)
  733. {
  734. USHORT iMethod;
  735. //Release the funcdescs.
  736. if(pMethodInfo != NULL)
  737. {
  738. for(iMethod = 0;
  739. iMethod < numMethods;
  740. iMethod++)
  741. {
  742. if(pMethodInfo[iMethod].pFuncDesc != NULL)
  743. {
  744. //Release the funcdesc.
  745. pMethodInfo[iMethod].pTypeInfo->lpVtbl->ReleaseFuncDesc(
  746. pMethodInfo[iMethod].pTypeInfo,
  747. pMethodInfo[iMethod].pFuncDesc);
  748. pMethodInfo[iMethod].pFuncDesc = NULL;
  749. //release the type info
  750. pMethodInfo[iMethod].pTypeInfo->lpVtbl->Release(
  751. pMethodInfo[iMethod].pTypeInfo);
  752. pMethodInfo[iMethod].pTypeInfo = NULL;
  753. }
  754. }
  755. }
  756. return S_OK;
  757. }
  758. //+---------------------------------------------------------------------------
  759. //
  760. // Function: ReleaseProxyVtbl
  761. //
  762. // Synopsis: Releases the proxy vtbl.
  763. //
  764. // Arguments:
  765. //
  766. // Returns:
  767. // S_OK
  768. //
  769. //----------------------------------------------------------------------------
  770. HRESULT ReleaseProxyVtbl(void * pVtbl)
  771. {
  772. HRESULT hr = S_OK;
  773. byte *pTemp;
  774. TypeInfoVtbl *pInfo;
  775. pTemp = (byte *)pVtbl - offsetof(TypeInfoVtbl, proxyVtbl.Vtbl);
  776. pInfo = (TypeInfoVtbl *) pTemp;
  777. hr = ReleaseVtbl(pInfo);
  778. return hr;
  779. }
  780. //+---------------------------------------------------------------------------
  781. //
  782. // Function: ReleaseStubVtbl
  783. //
  784. // Synopsis: Releases the stub vtbl.
  785. //
  786. // Arguments:
  787. //
  788. // Returns:
  789. // S_OK
  790. //
  791. //----------------------------------------------------------------------------
  792. HRESULT ReleaseStubVtbl(void * pVtbl)
  793. {
  794. HRESULT hr = S_OK;
  795. byte *pTemp;
  796. TypeInfoVtbl *pInfo;
  797. pTemp = (byte *)pVtbl - offsetof(TypeInfoVtbl, stubVtbl.Vtbl);
  798. pInfo = (TypeInfoVtbl *) pTemp;
  799. hr = ReleaseVtbl(pInfo);
  800. return hr;
  801. }
  802. //+---------------------------------------------------------------------------
  803. //
  804. // Function: ReleaseTypeFormatString
  805. //
  806. // Synopsis: Frees the type format string.
  807. //
  808. //----------------------------------------------------------------------------
  809. HRESULT ReleaseTypeFormatString(
  810. PFORMAT_STRING pTypeFormat)
  811. {
  812. if((pTypeFormat != 0) &&
  813. (pTypeFormat != __MIDL_TypeFormatString.Format))
  814. {
  815. I_RpcFree((void *)pTypeFormat);
  816. }
  817. return S_OK;
  818. }
  819. //+---------------------------------------------------------------------------
  820. //
  821. // Function: CStdProxyBuffer3_Release
  822. //
  823. // Synopsis: Decrement the proxy's reference count
  824. //
  825. // Returns: Reference count.
  826. //
  827. //----------------------------------------------------------------------------
  828. ULONG STDMETHODCALLTYPE
  829. CStdProxyBuffer3_Release(
  830. IN IRpcProxyBuffer * This)
  831. {
  832. ULONG count;
  833. IRpcProxyBuffer * pBaseProxyBuffer;
  834. count = (ULONG) ((CStdProxyBuffer2 *)This)->RefCount - 1;
  835. if(InterlockedDecrement(&((CStdProxyBuffer2 *)This)->RefCount) == 0)
  836. {
  837. count = 0;
  838. ReleaseProxyVtbl((void *) ((CStdProxyBuffer2 *)This)->pProxyVtbl);
  839. //Delegation support.
  840. pBaseProxyBuffer = ((CStdProxyBuffer2 *)This)->pBaseProxyBuffer;
  841. if( pBaseProxyBuffer != 0)
  842. {
  843. pBaseProxyBuffer->lpVtbl->Release(pBaseProxyBuffer);
  844. }
  845. //Free the memory
  846. delete (CStdProxyBuffer2 *)This;
  847. }
  848. return count;
  849. };
  850. //+---------------------------------------------------------------------------
  851. //
  852. // Function: CStdStubBuffer3_Release
  853. //
  854. // Synopsis: Decrement the proxy's reference count
  855. //
  856. // Returns: Reference count.
  857. //
  858. //----------------------------------------------------------------------------
  859. ULONG STDMETHODCALLTYPE
  860. CStdStubBuffer3_Release(
  861. IN IRpcStubBuffer * This)
  862. {
  863. ULONG count;
  864. unsigned char *pTemp;
  865. CStdStubBuffer2 * pStubBuffer;
  866. IRpcStubBuffer *pBaseStubBuffer;
  867. pTemp = (unsigned char *)This;
  868. pTemp -= offsetof(CStdStubBuffer2, lpVtbl);
  869. pStubBuffer = (CStdStubBuffer2 *) pTemp;
  870. count = (ULONG) pStubBuffer->RefCount - 1;
  871. if(InterlockedDecrement(&pStubBuffer->RefCount) == 0)
  872. {
  873. count = 0;
  874. ReleaseStubVtbl((void *) This->lpVtbl);
  875. pBaseStubBuffer = pStubBuffer->pBaseStubBuffer;
  876. if(pBaseStubBuffer != 0)
  877. pBaseStubBuffer->lpVtbl->Release(pBaseStubBuffer);
  878. //Free the stub buffer
  879. if (pStubBuffer->lpForwardingVtbl)
  880. ReleaseTemplateForwardVtbl((void **)pStubBuffer->lpForwardingVtbl);
  881. delete pStubBuffer;
  882. }
  883. return count;
  884. }
  885. //+---------------------------------------------------------------------------
  886. //
  887. // Function: GrowCacheIfNecessary
  888. //
  889. // Synopsis: increase the size of cache array if it's too small.
  890. //
  891. // Arguments:
  892. //
  893. // Returns:
  894. //
  895. //----------------------------------------------------------------------------
  896. HRESULT GrowCacheIfNecessary()
  897. {
  898. TypeInfoCache *pTemp = NULL;
  899. DWORD *pIndex = NULL;
  900. if (NULL == g_pCache)
  901. {
  902. g_pCache = (TypeInfoCache *)I_RpcAllocate(CACHE_BLOCK * sizeof(TypeInfoCache));
  903. if (g_pCache )
  904. {
  905. memset(g_pCache,0,CACHE_BLOCK * sizeof(TypeInfoCache));
  906. g_lCacheSize = CACHE_BLOCK;
  907. return S_OK;
  908. }
  909. else
  910. return E_OUTOFMEMORY;
  911. }
  912. if (g_lCacheSize <= g_lTotalCacheRef)
  913. {
  914. pTemp = (TypeInfoCache *)I_RpcAllocate((g_lCacheSize + CACHE_BLOCK)* sizeof(TypeInfoCache));
  915. if (NULL == pTemp)
  916. return E_OUTOFMEMORY;
  917. memset(pTemp,0,(g_lCacheSize + CACHE_BLOCK)* sizeof(TypeInfoCache));
  918. memcpy(pTemp,g_pCache,g_lCacheSize*sizeof(TypeInfoCache));
  919. I_RpcFree(g_pCache);
  920. g_pCache = pTemp;
  921. g_lCacheSize += CACHE_BLOCK;
  922. }
  923. return S_OK;
  924. }
  925. void swapCache(ULONG src, ULONG dest)
  926. {
  927. if (src == dest)
  928. return;
  929. TypeInfoCache temp;
  930. memcpy(&temp,&g_pCache[src],sizeof(TypeInfoCache));
  931. memcpy(&g_pCache[src],&g_pCache[dest],sizeof(TypeInfoCache));
  932. memcpy(&g_pCache[dest],&temp,sizeof(TypeInfoCache));
  933. }
  934. //+---------------------------------------------------------------------------
  935. //
  936. // Function: CacheRegister
  937. //
  938. // Synopsis: add a new instance of interface into the cache list. before
  939. // inserting the newly generated vtbl, make sure no other thread
  940. // has generated it first. If existing, discard the one this
  941. // thread generated and use the one already in the list. Otherwise
  942. // put it at the end of the array and adjust cache length.
  943. //
  944. // Arguments: riid,
  945. // pVtbl
  946. //
  947. // Returns:
  948. //
  949. //----------------------------------------------------------------------------
  950. HRESULT CacheRegister(
  951. IID riid,
  952. TypeInfoVtbl ** ppVtbl)
  953. {
  954. HRESULT hr = E_FAIL;
  955. TypeInfoVtbl *pVtbl = *ppVtbl;
  956. // this exact same item has been registered by others while we
  957. // are busying building our own: use the existing one instead.
  958. I_RpcRequestMutex(&TypeInfoMutex);
  959. if (CacheLookup(riid,ppVtbl) == S_OK)
  960. {
  961. if(pVtbl->stubDesc.pFormatTypes != __MIDL_TypeFormatString.Format)
  962. {
  963. I_RpcFree((void *) pVtbl->stubDesc.pFormatTypes);
  964. }
  965. I_RpcFree(pVtbl);
  966. I_RpcClearMutex(TypeInfoMutex);
  967. return S_OK;
  968. }
  969. hr = GrowCacheIfNecessary();
  970. if (SUCCEEDED(hr))
  971. {
  972. g_pCache[g_lTotalCacheRef].iid = (*ppVtbl)->iid;
  973. g_pCache[g_lTotalCacheRef].dwTickCount = 0;
  974. g_pCache[g_lTotalCacheRef].pVtbl = *ppVtbl;
  975. swapCache(g_lTotalCacheRef,g_lActiveCacheRef);
  976. g_lTotalCacheRef++;
  977. g_lActiveCacheRef++;
  978. }
  979. I_RpcClearMutex(TypeInfoMutex);
  980. return hr;
  981. }
  982. void swap(ULONG *dwDelta,
  983. ULONG *dwIndex,
  984. ULONG src,
  985. ULONG dest)
  986. {
  987. if (src == dest)
  988. return;
  989. ULONG temp;
  990. temp = dwDelta[src];
  991. dwDelta[src] = dwDelta[dest];
  992. dwDelta[dest] = temp;
  993. temp = dwIndex[src];
  994. dwIndex[src] = dwIndex[dest];
  995. dwIndex[dest] = temp;
  996. }
  997. //+---------------------------------------------------------------------------
  998. //
  999. // Function: FindEntrysToShrink
  1000. //
  1001. // Synopsis: find the largest ulPivot number of entrys in the array.
  1002. // a modification of algorithm due to C.A.R. Hoare in Programming
  1003. // Pearls in Novermber 1985 Communications of ACM.
  1004. //
  1005. // Arguments:
  1006. //
  1007. // Returns:
  1008. //
  1009. //----------------------------------------------------------------------------
  1010. void FindEntrysToShrink(ULONG *dwDelta,
  1011. ULONG *dwIndex,
  1012. ULONG ulPivot)
  1013. {
  1014. ULONG ulLow = 0 , ulUp , ulSeq;
  1015. ULONG ulMid ,ulIndex;
  1016. ulUp = g_lTotalCacheRef - g_lActiveCacheRef -1 ;
  1017. while (ulLow < ulUp)
  1018. {
  1019. ulSeq = ulLow;
  1020. // pick a random number and assume it's the kth largest.
  1021. ulIndex = ulLow + ((GetTickCount() & 0xff)*(ulUp-ulLow)/0xff);
  1022. ulMid = dwDelta[ulIndex];
  1023. swap(dwDelta,dwIndex,ulIndex,ulLow);
  1024. for (ULONG i = ulLow + 1; i <= ulUp; i++)
  1025. {
  1026. if (dwDelta[i] >= ulMid)
  1027. swap(dwDelta,dwIndex,++ulSeq,i);
  1028. }
  1029. // ulSeq is the ulSeq'th largest.
  1030. swap(dwDelta,dwIndex,ulSeq,ulLow);
  1031. if (ulSeq == ulPivot)
  1032. break; // done
  1033. if (ulSeq < ulPivot)
  1034. ulLow = ulSeq + 1 ;
  1035. else
  1036. ulUp = ulSeq - 1;
  1037. }
  1038. }
  1039. //+---------------------------------------------------------------------------
  1040. //
  1041. // Function: ShrinkReleasedCacheIfNecessary
  1042. //
  1043. // Synopsis: adjust the released cache size if necessary.
  1044. // the algorithm:
  1045. // find the eldest DELTA_MARK released interfaces. to avoid thrashing,
  1046. // increase the released cache size if eldest one is released within
  1047. // 30 sec. fill the empty spots with active entries.
  1048. //
  1049. // Arguments:
  1050. //
  1051. // Returns: S_OK. leave the HRESULT return for possible future change.
  1052. //
  1053. //----------------------------------------------------------------------------
  1054. HRESULT ShrinkReleasedCacheIfNecessary(TypeInfoVtbl*** pppVtbl, DWORD *dwLength)
  1055. {
  1056. static ULONG dwHigh = INIT_HIGH_MARK;
  1057. HRESULT hr = E_FAIL;
  1058. ULONG dwShrink ;
  1059. ULONG *dwDelta = NULL;
  1060. ULONG *dwIndex = NULL;
  1061. TypeInfoVtbl **ppvtbl = NULL;
  1062. ULONG dwTime = GetTickCount(), dwMax = 0;
  1063. ULONG i,j, dwMin=0xffffffff;
  1064. ULONG dwReleasedCache = g_lTotalCacheRef - g_lActiveCacheRef;
  1065. // doesn't need to shrink
  1066. if (dwReleasedCache <= dwHigh)
  1067. return S_FALSE;
  1068. dwShrink = (ULONG)(dwHigh / DELTA_MARK); // number to shrink
  1069. dwDelta = (ULONG *)I_RpcAllocate(dwReleasedCache * sizeof(ULONG));
  1070. dwIndex = (ULONG *)I_RpcAllocate(dwReleasedCache * sizeof(ULONG));
  1071. ppvtbl = (TypeInfoVtbl **)I_RpcAllocate(dwShrink * sizeof(TypeInfoVtbl *));
  1072. if ( ( NULL == ppvtbl ) || ( NULL == dwDelta ) || ( NULL == dwIndex ) )
  1073. {
  1074. if ( NULL != dwDelta ) I_RpcFree( dwDelta );
  1075. if ( NULL != dwIndex ) I_RpcFree( dwIndex );
  1076. if ( NULL != ppvtbl ) I_RpcFree( ppvtbl );
  1077. return E_OUTOFMEMORY;
  1078. }
  1079. for ( i = 0; i < dwReleasedCache; i++)
  1080. {
  1081. dwDelta[i] = dwTime - g_pCache[g_lActiveCacheRef+i].dwTickCount;
  1082. dwIndex[i] = g_lActiveCacheRef+i;
  1083. // basic book keeping to find the eldest and latest release.
  1084. if (dwDelta[i] > dwMax)
  1085. dwMax = dwDelta[i];
  1086. if (dwDelta[i] < dwMin)
  1087. dwMin = dwDelta[i];
  1088. }
  1089. // don't reclaim those released entries if it's thrashing.
  1090. if (dwMax <= THRASHING_TIME)
  1091. {
  1092. dwHigh += (ULONG)(dwHigh / DELTA_MARK);
  1093. return S_FALSE;
  1094. }
  1095. FindEntrysToShrink(dwDelta,dwIndex,dwShrink);
  1096. // cleanup the entries to be removed.
  1097. for (i = 0; i < dwShrink; i++)
  1098. {
  1099. ppvtbl[i] = g_pCache[dwIndex[i]].pVtbl;
  1100. ASSERT(ppvtbl[i]->cRefs == 0);
  1101. g_pCache[dwIndex[i]].pVtbl = 0;
  1102. }
  1103. // fill in the empty spots.
  1104. j = g_lTotalCacheRef -1;
  1105. for (i = 0; i < dwShrink; i++)
  1106. {
  1107. while (j > 0 && (g_pCache[j].pVtbl == 0)) j--;
  1108. if (j > dwIndex[i])
  1109. {
  1110. memcpy(&g_pCache[dwIndex[i]],&g_pCache[j],sizeof(TypeInfoCache));
  1111. memset(&g_pCache[j],0,sizeof(TypeInfoCache));
  1112. j--;
  1113. }
  1114. else
  1115. memset(&g_pCache[dwIndex[i]],0,sizeof(TypeInfoCache));
  1116. }
  1117. g_lTotalCacheRef -= dwShrink;
  1118. #ifdef DEBUGRPC
  1119. for (i = 0; (LONG)i < g_lTotalCacheRef; i++)
  1120. ASSERT(g_pCache[i].pVtbl != 0);
  1121. #endif
  1122. if (dwMin > IDLE_TIME)
  1123. {
  1124. dwHigh -= (ULONG)(dwHigh / DELTA_MARK);
  1125. if (dwHigh < INIT_HIGH_MARK)
  1126. dwHigh = INIT_HIGH_MARK;
  1127. }
  1128. // don't free those vtbls: do it out of CS
  1129. *pppVtbl = ppvtbl;
  1130. *dwLength = dwShrink;
  1131. return S_OK;
  1132. }
  1133. //+---------------------------------------------------------------------------
  1134. //
  1135. // Function: ReleaseVtbl
  1136. //
  1137. // Synopsis: Releases the vtbl.
  1138. //
  1139. // Arguments:
  1140. //
  1141. // Returns:
  1142. // S_OK
  1143. //
  1144. //----------------------------------------------------------------------------
  1145. HRESULT ReleaseVtbl(TypeInfoVtbl *pInfo)
  1146. {
  1147. TypeInfoVtbl **ppvtbl = NULL;
  1148. DWORD dwLength = 0;
  1149. LONG i;
  1150. HRESULT hr = E_FAIL;
  1151. I_RpcRequestMutex(&TypeInfoMutex);
  1152. if (0 == --pInfo->cRefs)
  1153. {
  1154. for (i = 0 ; i < g_lTotalCacheRef; i++)
  1155. {
  1156. if (IsEqualIID(pInfo->iid,g_pCache[i].iid))
  1157. {
  1158. g_pCache[i].dwTickCount = GetTickCount();
  1159. g_lActiveCacheRef--;
  1160. swapCache(i,g_lActiveCacheRef);
  1161. hr = ShrinkReleasedCacheIfNecessary(&ppvtbl,&dwLength);
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. else
  1167. {
  1168. I_RpcClearMutex(TypeInfoMutex);
  1169. return S_OK;
  1170. }
  1171. I_RpcClearMutex(TypeInfoMutex);
  1172. // free the vtbl outof mutex.
  1173. if (S_OK == hr)
  1174. {
  1175. for (i = 0; i < (LONG)dwLength; i++)
  1176. {
  1177. if((ppvtbl[i])->stubDesc.pFormatTypes != __MIDL_TypeFormatString.Format)
  1178. {
  1179. I_RpcFree((void *) (ppvtbl[i])->stubDesc.pFormatTypes);
  1180. }
  1181. I_RpcFree((void *) ppvtbl[i]);
  1182. }
  1183. I_RpcFree(ppvtbl);
  1184. }
  1185. return hr;
  1186. }
  1187. //+---------------------------------------------------------------------------
  1188. //
  1189. // Function: CacheLookup
  1190. //
  1191. // Synopsis: look up a TypeInfoVtbl from cache array using IID.
  1192. // adjust the cache state if the entry is released.
  1193. //
  1194. // Arguments: riid
  1195. // ppInfo
  1196. //
  1197. // Returns: S_OK if an entry is found in cache. E_NOINTERFACE is not.
  1198. //
  1199. //----------------------------------------------------------------------------
  1200. HRESULT CacheLookup(
  1201. REFIID riid,
  1202. TypeInfoVtbl **ppInfo)
  1203. {
  1204. HRESULT hr = E_NOINTERFACE;
  1205. LONG i;
  1206. if (NULL == g_pCache)
  1207. goto Exit;
  1208. for ( i = 0; i < g_lTotalCacheRef; i++)
  1209. {
  1210. if (IsEqualIID(riid,g_pCache[i].iid))
  1211. {
  1212. *ppInfo = g_pCache[i].pVtbl;
  1213. if (0 == (*ppInfo)->cRefs++)
  1214. {
  1215. g_pCache[i].dwTickCount = 0;
  1216. swapCache(i,g_lActiveCacheRef++);
  1217. }
  1218. hr = S_OK;
  1219. goto Exit;
  1220. }
  1221. }
  1222. Exit:
  1223. return hr;
  1224. }
  1225. //+---------------------------------------------------------------------------
  1226. //
  1227. // Function: NdrpGetTypeGenCookie
  1228. //
  1229. // Synopsis: Allocate a cookie that can be used in subsequent calls to
  1230. // NdrpGetProcFormatString, NdrpGetTypeFormatString, and
  1231. // NdrpReleaseTypeGenCookie.
  1232. //
  1233. // Parameters:
  1234. //
  1235. // Returns: ppvTypeGenCookie: A type gen cookie
  1236. //
  1237. //----------------------------------------------------------------------------
  1238. EXTERN_C
  1239. HRESULT NdrpGetTypeGenCookie(void **ppvTypeGenCookie)
  1240. {
  1241. HRESULT hr = S_OK;
  1242. CTypeGen *pTypeGen;
  1243. if (ppvTypeGenCookie == NULL)
  1244. return E_POINTER;
  1245. // Ensure that everything is initialized.
  1246. RPC_STATUS sc = NdrpPerformRpcInitialization();
  1247. if (sc != RPC_S_OK)
  1248. return HRESULT_FROM_WIN32(sc);
  1249. pTypeGen = new CTypeGen;
  1250. if (pTypeGen)
  1251. *ppvTypeGenCookie = pTypeGen;
  1252. else
  1253. hr = E_OUTOFMEMORY;
  1254. return hr;
  1255. }
  1256. //+---------------------------------------------------------------------------
  1257. //
  1258. // Function: NdrpReleaseTypeGenCookie
  1259. //
  1260. // Synopsis: Free memory associeated with type gen cookie allocated with
  1261. // NdrpGetTypeGenCookie
  1262. //
  1263. // Parameters: pvTypeGenCookie: cookie returned from NdrpGetTypeGenCookie
  1264. //
  1265. // Returns:
  1266. //
  1267. //----------------------------------------------------------------------------
  1268. EXTERN_C
  1269. HRESULT NdrpReleaseTypeGenCookie(void *pvTypeGenCookie)
  1270. {
  1271. CTypeGen *pTypeGen = (CTypeGen *)pvTypeGenCookie;
  1272. delete pTypeGen;
  1273. return S_OK;
  1274. }
  1275. //+---------------------------------------------------------------------------
  1276. //
  1277. // Function: NdrpGetProcFormatString
  1278. //
  1279. // Synopsis: Generate proc format string and type format string for specified
  1280. // function.
  1281. //
  1282. // Parameters: pTypeGenCookie: Type format generator object
  1283. // pTypeInfo: ITypeInfo interface.
  1284. // pFuncDesc: Function descriptor.
  1285. // iMethod: # of methods.
  1286. //
  1287. // Returns: pProcFormatString: Address for proc format string.
  1288. // pcbFormat: Size of format string.
  1289. //
  1290. //----------------------------------------------------------------------------
  1291. EXTERN_C
  1292. HRESULT NdrpGetProcFormatString(IN void *pvTypeGenCookie,
  1293. IN ITypeInfo *pTypeInfo,
  1294. IN FUNCDESC *pFuncDesc,
  1295. IN USHORT iMethod,
  1296. OUT PFORMAT_STRING pProcFormatString,
  1297. OUT USHORT *pcbFormat)
  1298. {
  1299. CProcGen proc;
  1300. CTypeGen *pTypeGen = (CTypeGen *)pvTypeGenCookie;
  1301. if (pTypeGen == NULL)
  1302. return E_INVALIDARG;
  1303. else
  1304. return proc.GetProcFormat(pTypeGen, pTypeInfo, pFuncDesc, iMethod,
  1305. pProcFormatString, pcbFormat);
  1306. }
  1307. //+---------------------------------------------------------------------------
  1308. //
  1309. // Function: NdrpGetTypeFormatString
  1310. //
  1311. // Synopsis: Get the MIDL_TYPE_FORMAT_STRING.
  1312. //
  1313. // Arguments: pvTypeGenCookie - cookie allocated with NdrpGetTypeGenCookie
  1314. // and used in subsequent NdrpGetProcFormatString
  1315. // calls.
  1316. //
  1317. // ppTypeFormatString - Returns a pointer to the type format
  1318. // string.
  1319. //
  1320. // pLength - Returns the length of the format string.
  1321. //
  1322. //----------------------------------------------------------------------------
  1323. EXTERN_C
  1324. HRESULT NdrpGetTypeFormatString(IN void *pvTypeGenCookie,
  1325. OUT PFORMAT_STRING * pTypeFormatString,
  1326. OUT USHORT * pLength)
  1327. {
  1328. CTypeGen *pTypeGen = (CTypeGen *)pvTypeGenCookie;
  1329. if (pTypeGen == NULL)
  1330. return E_INVALIDARG;
  1331. else
  1332. return pTypeGen->GetTypeFormatString(pTypeFormatString, pLength);
  1333. }
  1334. //+---------------------------------------------------------------------------
  1335. //
  1336. // Function: NdrpReleaseTypeFormatString
  1337. //
  1338. // Synopsis: Free the memory returned from NdrpGetTypeFormatString function.
  1339. //
  1340. // Parameters: pTypeFormatString: Address of format string returned from
  1341. // NdrpGetTypeFormatString.
  1342. //
  1343. //----------------------------------------------------------------------------
  1344. EXTERN_C
  1345. HRESULT NdrpReleaseTypeFormatString(PFORMAT_STRING pTypeFormatString)
  1346. {
  1347. if (pTypeFormatString)
  1348. {
  1349. if(pTypeFormatString != __MIDL_TypeFormatString.Format)
  1350. {
  1351. I_RpcFree((void *)pTypeFormatString);
  1352. }
  1353. }
  1354. return S_OK;
  1355. }
  1356. //+---------------------------------------------------------------------------
  1357. //
  1358. // Function: GetProcFormat
  1359. //
  1360. // Synopsis: Generate proc format string and type format string for specified
  1361. // function.
  1362. //
  1363. // Parameters: pTeypGen: Type format generator object
  1364. // pTypeInfo: ITypeInfo interface.
  1365. // pFuncDesc: Function descriptor.
  1366. // iMethod: # of methods.
  1367. //
  1368. // Returns: pProcFormatString: Address for proc format string.
  1369. //
  1370. //----------------------------------------------------------------------------
  1371. HRESULT CProcGen::GetProcFormat(
  1372. IN CTypeGen * pTypeGen,
  1373. IN ITypeInfo * pTypeInfo,
  1374. IN FUNCDESC * pFuncDesc,
  1375. IN USHORT iMethod,
  1376. OUT PFORMAT_STRING pProcFormatString,
  1377. OUT USHORT * pcbFormat)
  1378. {
  1379. HRESULT hr = S_OK;
  1380. USHORT iParam;
  1381. INTERPRETER_FLAGS OiFlags ;
  1382. INTERPRETER_OPT_FLAGS Oi2Flags ;
  1383. INTERPRETER_OPT_FLAGS2 Oi2Flags2;
  1384. PARAMINFO *aParamInfo;
  1385. BOOLEAN fChangeSize,fNeedChange = FALSE;
  1386. USHORT offset;
  1387. aParamInfo = new PARAMINFO[pFuncDesc->cParams];
  1388. if(0 == aParamInfo)
  1389. {
  1390. return E_OUTOFMEMORY;
  1391. }
  1392. for(iParam = 0;
  1393. iParam < pFuncDesc->cParams;
  1394. iParam++)
  1395. {
  1396. hr = VarVtOfTypeDesc(pTypeInfo,
  1397. &pFuncDesc->lprgelemdescParam[iParam].tdesc,
  1398. &aParamInfo[iParam]);
  1399. if(SUCCEEDED(hr))
  1400. {
  1401. // PARAMFlags should give us enough information.
  1402. DWORD wIDLFlags = pFuncDesc->lprgelemdescParam[iParam].idldesc.wIDLFlags;
  1403. if(wIDLFlags & IDLFLAG_FRETVAL)
  1404. {
  1405. wIDLFlags |= IDLFLAG_FOUT;
  1406. }
  1407. if(!(wIDLFlags & (IDLFLAG_FIN | IDLFLAG_FOUT)))
  1408. {
  1409. //Set the direction flags.
  1410. if(aParamInfo[iParam].vt & (VT_BYREF | VT_ARRAY))
  1411. {
  1412. wIDLFlags |= IDLFLAG_FIN | IDLFLAG_FOUT;
  1413. }
  1414. else
  1415. {
  1416. wIDLFlags |= IDLFLAG_FIN;
  1417. }
  1418. }
  1419. aParamInfo[iParam].wIDLFlags = wIDLFlags;
  1420. }
  1421. else
  1422. {
  1423. goto Error;
  1424. }
  1425. }
  1426. _pTypeGen = pTypeGen;
  1427. _offset = 0;
  1428. _pProcFormatString = pProcFormatString;
  1429. _fClientMustSize = FALSE;
  1430. _fServerMustSize = FALSE;
  1431. _fClientCorrCheck = FALSE;
  1432. _fServerCorrCheck = FALSE;
  1433. _clientBufferSize = 0;
  1434. _serverBufferSize = 0;
  1435. _usFloatArgMask = 0;
  1436. _usFloatSlots = 0;
  1437. // The "this" pointer uses 8 bytes of stack on Alpha and 64bit platforms
  1438. // and 4 bytes of stack on other platforms.
  1439. _stackSize = sizeof(REGISTER_TYPE);
  1440. //Compute the size of the parameters. leave out structures. We can only determine
  1441. // the size later.
  1442. // Also, we calculate the Oi2 extension flags / FloatMask for 64bit
  1443. for(iParam = 0;
  1444. SUCCEEDED(hr) && iParam < pFuncDesc->cParams;
  1445. iParam++)
  1446. {
  1447. hr = CalcSize(aParamInfo[iParam].vt,
  1448. aParamInfo[iParam].wIDLFlags,
  1449. iParam);
  1450. }
  1451. if(SUCCEEDED(hr))
  1452. {
  1453. //Compute the size of the HRESULT return value.
  1454. _stackSize += sizeof(REGISTER_TYPE);
  1455. LENGTH_ALIGN(_serverBufferSize, 3);
  1456. _serverBufferSize += 8; // HRESULT is simple type, overestimate it also
  1457. //Handle type
  1458. PushByte(FC_AUTO_HANDLE);
  1459. //Oi interpreter flags
  1460. OiFlags.FullPtrUsed = FALSE;
  1461. OiFlags.RpcSsAllocUsed = FALSE;
  1462. OiFlags.ObjectProc = TRUE;
  1463. OiFlags.HasRpcFlags = TRUE;
  1464. OiFlags.IgnoreObjectException = FALSE;
  1465. OiFlags.HasCommOrFault = TRUE;
  1466. OiFlags.UseNewInitRoutines = TRUE;
  1467. OiFlags.Unused = FALSE;
  1468. PushByte(*((byte *) &OiFlags));
  1469. PushLong(0); // RpcFlags
  1470. //Method number
  1471. PushShort(iMethod);
  1472. offset = _offset; // _stackSize,_clientBufferSize and _serverBufferSize could be
  1473. //Stack size
  1474. PushShort(_stackSize);
  1475. //Size of client RPC message buffer.
  1476. // place holder if there is a UDT parameter
  1477. if(_clientBufferSize <= 65535)
  1478. PushShort((USHORT) _clientBufferSize);
  1479. else
  1480. {
  1481. hr = E_FAIL;
  1482. goto Error;
  1483. }
  1484. //Size of server RPC message buffer.
  1485. if(_serverBufferSize <= 65535)
  1486. PushShort((USHORT) _serverBufferSize);
  1487. else
  1488. {
  1489. hr = E_FAIL;
  1490. goto Error;
  1491. }
  1492. //Oi2 interpreter flags
  1493. *(byte*)&Oi2Flags = 0;
  1494. Oi2Flags.ServerMustSize = _fServerMustSize;
  1495. Oi2Flags.ClientMustSize = _fClientMustSize;
  1496. Oi2Flags.HasReturn = TRUE;
  1497. Oi2Flags.HasPipes = FALSE;
  1498. Oi2Flags.Unused = 0;
  1499. #if defined(__RPC_WIN64__)
  1500. // robust is only availble in 64bit tlb
  1501. Oi2Flags.HasExtensions = TRUE;
  1502. #endif
  1503. PushByte(*((byte *) &Oi2Flags));
  1504. //Number of parameters + return value.
  1505. PushByte(pFuncDesc->cParams + 1);
  1506. #if defined(__RPC_WIN64__)
  1507. // robust is only availble in 64bit tlb
  1508. if ( Oi2Flags.HasExtensions )
  1509. {
  1510. *(byte*)&Oi2Flags2 = 0;
  1511. Oi2Flags2.HasNewCorrDesc = _fClientCorrCheck | _fServerCorrCheck ;
  1512. Oi2Flags2.ClientCorrCheck = _fClientCorrCheck;
  1513. Oi2Flags2.ServerCorrCheck = _fServerCorrCheck;
  1514. PushByte( sizeof(NDR_PROC_HEADER_EXTS64) ); // header extension size
  1515. PushByte( *( (byte *) &Oi2Flags2 ) ); // extension flags
  1516. PushShort(0); // client correlation count
  1517. PushShort(0); // server collrelation count
  1518. PushShort(0); // notify index
  1519. PushShort(0); // placeholder for WIN64 float mask
  1520. }
  1521. #endif
  1522. // only a place holder for now if there are struct parameters.
  1523. //Generate the parameter info.
  1524. //The "this" pointer uses 8 bytes of stack on Alpha
  1525. //and 4 bytes of stack on other platforms.
  1526. _stackSize = sizeof(REGISTER_TYPE);
  1527. for(iParam = 0;
  1528. SUCCEEDED(hr) && iParam < pFuncDesc->cParams;
  1529. iParam++)
  1530. {
  1531. hr = GenParamDescriptor(&aParamInfo[iParam],&fChangeSize);
  1532. if (fChangeSize) // there are structs as parameters.
  1533. // _stackSize etc. might need to be changed.
  1534. {
  1535. fNeedChange = TRUE;
  1536. }
  1537. }
  1538. if(SUCCEEDED(hr))
  1539. {
  1540. //Generate the HRESULT return value.
  1541. PushShort( 0x70); //IsOut, IsReturn, IsBaseType
  1542. PushShort(_stackSize);
  1543. PushByte(FC_LONG);
  1544. PushByte(0);
  1545. *pcbFormat = _offset;
  1546. }
  1547. if (fNeedChange)
  1548. {
  1549. //Compute the size of the HRESULT return value.
  1550. LENGTH_ALIGN(_serverBufferSize, 3);
  1551. _serverBufferSize += 4;
  1552. _stackSize += sizeof(REGISTER_TYPE);
  1553. SetShort(offset,_stackSize);
  1554. SetShort(offset + sizeof(SHORT),(USHORT)_clientBufferSize);
  1555. SetShort(offset + 2*sizeof(SHORT),(USHORT)_serverBufferSize);
  1556. if (_clientBufferSize > _UI16_MAX || _serverBufferSize > _UI16_MAX)
  1557. {
  1558. hr = E_FAIL;
  1559. goto Error;
  1560. }
  1561. }
  1562. #if defined(__RPC_WIN64__)
  1563. // Set the WIN64 floatarg mask
  1564. SetShort(24, _usFloatArgMask);
  1565. #endif
  1566. }
  1567. Error:
  1568. delete [] aParamInfo;
  1569. return hr;
  1570. }
  1571. //+---------------------------------------------------------------------------
  1572. //
  1573. // Function: CProcGen::CalcSize
  1574. //
  1575. // Synopsis: calculate the stack size of current parameter in a method, also
  1576. // calculate Oi2 extension flags for 64bit
  1577. //
  1578. //
  1579. // Arguments:
  1580. // IN VARTYPE vt - vt type of current parameter
  1581. // IN DWORD wIDLFlags - IDL flags of this paramter
  1582. // IN ULONG nParam - parameter number
  1583. //
  1584. // Returns:
  1585. // S_OK
  1586. // DISP_E_BADVARTYPE
  1587. //
  1588. //----------------------------------------------------------------------------
  1589. HRESULT CProcGen::CalcSize(
  1590. IN VARTYPE vt,
  1591. IN DWORD wIDLFlags,
  1592. IN ULONG nParam)
  1593. {
  1594. HRESULT hr = S_OK;
  1595. switch(vt & (~VT_BYREF))
  1596. {
  1597. case VT_I1:
  1598. case VT_UI1:
  1599. _stackSize += sizeof(REGISTER_TYPE);
  1600. if(wIDLFlags & IDLFLAG_FIN)
  1601. {
  1602. _clientBufferSize += 1;
  1603. }
  1604. if(wIDLFlags & IDLFLAG_FOUT)
  1605. {
  1606. _serverBufferSize += 1;
  1607. }
  1608. break;
  1609. case VT_I2:
  1610. case VT_UI2:
  1611. case VT_BOOL:
  1612. _stackSize += sizeof(REGISTER_TYPE);
  1613. if(wIDLFlags & IDLFLAG_FIN)
  1614. {
  1615. _clientBufferSize = (_clientBufferSize + 1) & ~1;
  1616. _clientBufferSize += 4;
  1617. }
  1618. if(wIDLFlags & IDLFLAG_FOUT)
  1619. {
  1620. _serverBufferSize = (_serverBufferSize + 1) & ~1;
  1621. _serverBufferSize += 4;
  1622. }
  1623. break;
  1624. case VT_R4:
  1625. #if defined(_WIN64)
  1626. // setup float mask for WIN64
  1627. if ( !( vt & VT_BYREF ) && ( _stackSize/sizeof(REGISTER_TYPE) < ARGS_IN_REGISTERS ) )
  1628. _usFloatArgMask |= 1 << ( 2 * _stackSize/sizeof(REGISTER_TYPE) );
  1629. #endif
  1630. case VT_I4:
  1631. case VT_UI4:
  1632. case VT_INT:
  1633. case VT_UINT:
  1634. case VT_ERROR:
  1635. case VT_HRESULT:
  1636. _stackSize += sizeof(REGISTER_TYPE);
  1637. if(wIDLFlags & IDLFLAG_FIN)
  1638. {
  1639. _clientBufferSize = (_clientBufferSize + 3) & ~3;
  1640. _clientBufferSize += 8;
  1641. }
  1642. if(wIDLFlags & IDLFLAG_FOUT)
  1643. {
  1644. _serverBufferSize = (_serverBufferSize + 3) & ~3;
  1645. _serverBufferSize += 8;
  1646. }
  1647. break;
  1648. case VT_R8:
  1649. case VT_DATE:
  1650. #if defined(_WIN64)
  1651. // setup float mask for WIN64
  1652. if ( !( vt & VT_BYREF ) && ( _stackSize/sizeof(REGISTER_TYPE) < ARGS_IN_REGISTERS ) )
  1653. _usFloatArgMask |= 1 << ( 2 * _stackSize/sizeof(REGISTER_TYPE) + 1 );
  1654. #endif
  1655. case VT_CY:
  1656. case VT_I8:
  1657. case VT_UI8:
  1658. if(vt & VT_BYREF)
  1659. {
  1660. _stackSize += sizeof(REGISTER_TYPE);
  1661. }
  1662. else
  1663. {
  1664. _stackSize += 8;
  1665. }
  1666. if(wIDLFlags & IDLFLAG_FIN)
  1667. {
  1668. _clientBufferSize = (_clientBufferSize + 7) & ~7;
  1669. _clientBufferSize += 16;
  1670. }
  1671. if(wIDLFlags & IDLFLAG_FOUT)
  1672. {
  1673. _serverBufferSize = (_serverBufferSize + 7) & ~7;
  1674. _serverBufferSize += 16;
  1675. }
  1676. break;
  1677. case VT_DECIMAL:
  1678. if(vt & VT_BYREF)
  1679. {
  1680. _stackSize += sizeof(REGISTER_TYPE);
  1681. }
  1682. else
  1683. {
  1684. // On WIN64 no need to align when size is up to 8 bytes
  1685. #if defined(_AMD64_)
  1686. _stackSize += sizeof(REGISTER_TYPE);
  1687. #else
  1688. _stackSize += sizeof(DECIMAL);
  1689. #endif
  1690. }
  1691. // overestimate the alignment to avoid buffer underrun during marshalling
  1692. if(wIDLFlags & IDLFLAG_FIN)
  1693. {
  1694. _clientBufferSize = (_clientBufferSize + 7) & ~7;
  1695. _clientBufferSize += sizeof(DECIMAL) + 8;
  1696. }
  1697. if(wIDLFlags & IDLFLAG_FOUT)
  1698. {
  1699. _serverBufferSize = (_serverBufferSize + 7) & ~7;
  1700. _serverBufferSize += sizeof(DECIMAL) + 8;
  1701. }
  1702. break;
  1703. case VT_VARIANT:
  1704. if(vt & VT_BYREF)
  1705. {
  1706. _stackSize += sizeof(REGISTER_TYPE);
  1707. }
  1708. else
  1709. {
  1710. // in new spec, VARIANT is aligned to 8 again.
  1711. #if defined(_WIN64)
  1712. LENGTH_ALIGN(_stackSize, 7);
  1713. #endif
  1714. #if defined(_AMD64_)
  1715. _stackSize += sizeof(REGISTER_TYPE );
  1716. #else
  1717. _stackSize += sizeof(VARIANT);
  1718. #endif
  1719. }
  1720. if(wIDLFlags & IDLFLAG_FIN)
  1721. {
  1722. _fClientMustSize = TRUE;
  1723. _fServerCorrCheck= TRUE;
  1724. }
  1725. if(wIDLFlags & IDLFLAG_FOUT)
  1726. {
  1727. _fServerMustSize = TRUE;
  1728. _fClientCorrCheck= TRUE;
  1729. }
  1730. break;
  1731. case VT_INTERFACE:
  1732. case VT_UNKNOWN:
  1733. case VT_DISPATCH:
  1734. case VT_STREAM:
  1735. case VT_STORAGE:
  1736. // structure
  1737. case VT_USERDEFINED:
  1738. case VT_BSTR:
  1739. case VT_MULTIINDIRECTIONS:
  1740. case VT_CARRAY:
  1741. // set robust check for interfaces
  1742. if(wIDLFlags & IDLFLAG_FIN)
  1743. {
  1744. _fServerCorrCheck= TRUE;
  1745. }
  1746. if(wIDLFlags & IDLFLAG_FOUT)
  1747. {
  1748. _fClientCorrCheck= TRUE;
  1749. }
  1750. // fall through for sizing
  1751. case VT_LPSTR:
  1752. case VT_LPWSTR:
  1753. _stackSize += sizeof(REGISTER_TYPE);
  1754. if(wIDLFlags & IDLFLAG_FIN)
  1755. {
  1756. _fClientMustSize = TRUE;
  1757. }
  1758. if(wIDLFlags & IDLFLAG_FOUT)
  1759. {
  1760. _fServerMustSize = TRUE;
  1761. }
  1762. break;
  1763. case VT_FILETIME:
  1764. if(vt & VT_BYREF)
  1765. {
  1766. _stackSize += sizeof(REGISTER_TYPE);
  1767. }
  1768. else
  1769. {
  1770. _stackSize += sizeof(FILETIME);
  1771. }
  1772. if(wIDLFlags & IDLFLAG_FIN)
  1773. {
  1774. _clientBufferSize = (_clientBufferSize + 3) & ~3;
  1775. _clientBufferSize += sizeof(FILETIME) + 4;
  1776. }
  1777. if(wIDLFlags & IDLFLAG_FOUT)
  1778. {
  1779. _serverBufferSize = (_serverBufferSize + 3) & ~3;
  1780. _serverBufferSize += sizeof(FILETIME) + 4;
  1781. }
  1782. break;
  1783. default:
  1784. if(vt & VT_ARRAY)
  1785. {
  1786. _stackSize += sizeof(REGISTER_TYPE);
  1787. if(wIDLFlags & IDLFLAG_FIN)
  1788. {
  1789. _fClientMustSize = TRUE;
  1790. _fServerCorrCheck= TRUE;
  1791. }
  1792. if(wIDLFlags & IDLFLAG_FOUT)
  1793. {
  1794. _fServerMustSize = TRUE;
  1795. _fClientCorrCheck= TRUE;
  1796. }
  1797. }
  1798. else
  1799. {
  1800. hr = DISP_E_BADVARTYPE;
  1801. }
  1802. break;
  1803. }
  1804. return hr;
  1805. }
  1806. //+---------------------------------------------------------------------------
  1807. //
  1808. // Function: GenParamDescriptor
  1809. //
  1810. // Synopsis: generate proc string and format string for one parameter.
  1811. //
  1812. // Parameter: parainfo. the parameter information. vt for VARTYPE,
  1813. // IID if VARTYPE is interface,
  1814. // ITypeInfo if VARTYPE is UDT
  1815. //
  1816. // Returns: fChangeSize. TRUE if the parameter is a UDT. member variable of
  1817. // CProcGen (_stackSize,_clientBufferSize and _serverBufferSize need to
  1818. // be written back)
  1819. //
  1820. //----------------------------------------------------------------------------
  1821. HRESULT
  1822. CProcGen::GenParamDescriptor(
  1823. IN PARAMINFO *parainfo,
  1824. OUT BOOLEAN * fChangeSize)
  1825. {
  1826. HRESULT hr = S_OK;
  1827. PARAM_ATTRIBUTES attr;
  1828. USHORT offset;
  1829. *fChangeSize = FALSE;
  1830. VARTYPE vt = parainfo->vt;
  1831. VARTYPE vtnoref = vt & (~VT_BYREF) ;
  1832. MemoryInfo ProcMemInfo;
  1833. USHORT ParamOffset = _stackSize;
  1834. memset( &attr, 0, sizeof(attr) );
  1835. memset( &ProcMemInfo, 0, sizeof( ProcMemInfo ) );
  1836. attr.IsIn = (parainfo->wIDLFlags & IDLFLAG_FIN) ? 1 : 0;
  1837. attr.IsOut = (parainfo->wIDLFlags & IDLFLAG_FOUT) ? 1 : 0;
  1838. switch(vtnoref)
  1839. {
  1840. case VT_I1:
  1841. case VT_UI1:
  1842. case VT_I2:
  1843. case VT_BOOL:
  1844. case VT_UI2:
  1845. case VT_I4:
  1846. case VT_INT:
  1847. case VT_ERROR:
  1848. case VT_HRESULT:
  1849. case VT_UINT:
  1850. case VT_UI4:
  1851. case VT_R4:
  1852. attr.IsBasetype = TRUE;
  1853. if(vt & VT_BYREF)
  1854. {
  1855. attr.IsSimpleRef = TRUE;
  1856. if(!attr.IsIn)
  1857. {
  1858. attr.ServerAllocSize = 1;
  1859. }
  1860. }
  1861. PushShort(*((short *) &attr));
  1862. PushShort(_stackSize);
  1863. _stackSize += sizeof(REGISTER_TYPE);
  1864. PushShort(VT_FC_MAP[vtnoref] );
  1865. break;
  1866. case VT_I8:
  1867. case VT_UI8:
  1868. case VT_CY:
  1869. case VT_R8:
  1870. case VT_DATE:
  1871. attr.IsBasetype = TRUE;
  1872. if(vt & VT_BYREF)
  1873. {
  1874. attr.IsSimpleRef = TRUE;
  1875. if(!attr.IsIn)
  1876. {
  1877. attr.ServerAllocSize = 1;
  1878. }
  1879. }
  1880. PushShort(*((short *) &attr));
  1881. if(vt & VT_BYREF)
  1882. {
  1883. PushShort(_stackSize);
  1884. _stackSize += sizeof(REGISTER_TYPE);
  1885. }
  1886. else
  1887. {
  1888. PushShort(_stackSize);
  1889. _stackSize += 8;
  1890. }
  1891. PushShort(VT_FC_MAP[vtnoref] );
  1892. break;
  1893. case VT_DISPATCH:
  1894. case VT_UNKNOWN:
  1895. case VT_INTERFACE:
  1896. case VT_STREAM:
  1897. case VT_STORAGE:
  1898. attr.MustSize = TRUE;
  1899. attr.MustFree = TRUE;
  1900. PushShort(*((short *) &attr));
  1901. PushShort(_stackSize);
  1902. _stackSize += sizeof(REGISTER_TYPE);
  1903. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  1904. PushShort(offset);
  1905. break;
  1906. case VT_BSTR:
  1907. attr.MustSize = TRUE;
  1908. attr.MustFree = TRUE;
  1909. if(vt & VT_BYREF)
  1910. {
  1911. attr.IsSimpleRef = TRUE;
  1912. if(!attr.IsIn)
  1913. {
  1914. attr.ServerAllocSize = 1;
  1915. }
  1916. }
  1917. else
  1918. {
  1919. attr.IsByValue = TRUE;
  1920. }
  1921. PushShort(*((short *) &attr));
  1922. PushShort(_stackSize);
  1923. _stackSize += sizeof(REGISTER_TYPE);
  1924. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  1925. PushShort(offset);
  1926. break;
  1927. case VT_VARIANT:
  1928. attr.MustSize = TRUE;
  1929. attr.MustFree = TRUE;
  1930. #if defined(_AMD64_)
  1931. // in amd64, variant is always passed by reference.
  1932. vt |= VT_BYREF;
  1933. parainfo->vt |= VT_BYREF;
  1934. #endif
  1935. if(vt & VT_BYREF)
  1936. {
  1937. attr.IsSimpleRef = TRUE;
  1938. if(!attr.IsIn)
  1939. {
  1940. // size of VARIANT in 64bit is larger.
  1941. attr.ServerAllocSize = (sizeof(VARIANT) +7 ) >> 3;
  1942. }
  1943. }
  1944. else
  1945. {
  1946. attr.IsByValue = TRUE;
  1947. }
  1948. PushShort(*((short *) &attr));
  1949. if(vt & VT_BYREF)
  1950. {
  1951. PushShort(_stackSize);
  1952. _stackSize += sizeof(REGISTER_TYPE);
  1953. }
  1954. else
  1955. {
  1956. #if defined(_WIN64)
  1957. LENGTH_ALIGN(_stackSize, 7);
  1958. #endif
  1959. PushShort(_stackSize);
  1960. _stackSize += sizeof(VARIANT);
  1961. }
  1962. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  1963. PushShort(offset);
  1964. break;
  1965. case VT_LPSTR:
  1966. case VT_LPWSTR:
  1967. attr.MustSize = TRUE;
  1968. attr.MustFree = TRUE;
  1969. if(vt & VT_BYREF)
  1970. {
  1971. if(!attr.IsIn)
  1972. {
  1973. attr.ServerAllocSize = 1;
  1974. }
  1975. }
  1976. else
  1977. {
  1978. attr.IsSimpleRef = TRUE;
  1979. }
  1980. PushShort(*((short *) &attr));
  1981. PushShort(_stackSize);
  1982. _stackSize += sizeof(REGISTER_TYPE);
  1983. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  1984. PushShort(offset);
  1985. break;
  1986. case VT_DECIMAL:
  1987. attr.MustFree = TRUE;
  1988. #if defined(_AMD64_)
  1989. // in amd64, decimal is always passed by reference.
  1990. vt |= VT_BYREF;
  1991. parainfo->vt |= VT_BYREF;
  1992. #endif
  1993. if(vt & VT_BYREF)
  1994. {
  1995. attr.IsSimpleRef = TRUE;
  1996. if(!attr.IsIn)
  1997. {
  1998. attr.ServerAllocSize = (sizeof( DECIMAL ) + 7 ) >> 3;
  1999. }
  2000. }
  2001. else
  2002. {
  2003. attr.IsByValue = TRUE;
  2004. }
  2005. PushShort(*((short *) &attr));
  2006. if(vt & VT_BYREF)
  2007. {
  2008. PushShort(_stackSize);
  2009. _stackSize += sizeof(REGISTER_TYPE);
  2010. }
  2011. else
  2012. {
  2013. // On WIN64 no need to align when size is up to 8 bytes
  2014. PushShort(_stackSize);
  2015. _stackSize += sizeof(DECIMAL);
  2016. }
  2017. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  2018. PushShort(offset);
  2019. break;
  2020. case VT_FILETIME:
  2021. attr.MustFree = TRUE;
  2022. if(vt & VT_BYREF)
  2023. {
  2024. attr.IsSimpleRef = TRUE;
  2025. if(!attr.IsIn)
  2026. {
  2027. attr.ServerAllocSize = 1;
  2028. }
  2029. }
  2030. else
  2031. {
  2032. attr.IsByValue = TRUE;
  2033. }
  2034. PushShort(*((short *) &attr));
  2035. if(vt & VT_BYREF)
  2036. {
  2037. PushShort(_stackSize);
  2038. _stackSize += sizeof(REGISTER_TYPE);
  2039. }
  2040. else
  2041. {
  2042. // On WIN64 no need to align when size is up to 8 bytes
  2043. PushShort(_stackSize);
  2044. _stackSize += sizeof(FILETIME);
  2045. }
  2046. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  2047. PushShort(offset);
  2048. break;
  2049. case VT_CARRAY:
  2050. case VT_USERDEFINED:
  2051. {
  2052. SHORT WireAlignment,length;
  2053. BOOL IsByRef = FALSE;
  2054. #if defined(_AMD64_)
  2055. IsByRef = vt & VT_BYREF;
  2056. if ( vt & VT_USERDEFINED )
  2057. {
  2058. parainfo->vt |= VT_BYREF;
  2059. }
  2060. #endif
  2061. hr = _pTypeGen->RegisterType(parainfo, &offset,&ProcMemInfo);
  2062. if (FAILED(hr))
  2063. break;
  2064. WireAlignment = ProcMemInfo.WireAlignment;
  2065. length = ProcMemInfo.MemorySize;
  2066. #if defined(_AMD64_)
  2067. // we need to roll back if the struct size is 1,2,4,8
  2068. if ( !IsByRef && length <= 8 && !(length & (length-1) ) )
  2069. {
  2070. parainfo->vt &= ~VT_BYREF;
  2071. hr = _pTypeGen->GetOffset(offset + 2 * sizeof (BYTE), &offset);
  2072. if(FAILED(hr))
  2073. break;
  2074. }
  2075. else // otherwise, it's effecitvely pass by reference
  2076. vt |= VT_BYREF;
  2077. #endif
  2078. attr.MustSize = TRUE;
  2079. attr.MustFree = TRUE;
  2080. if ((vt & (~VT_BYREF)) != VT_CARRAY)
  2081. {
  2082. // this is code for server alloc field in proc format string.
  2083. // optimization to reduce the allocation for small top level struct
  2084. if(vt & VT_BYREF)
  2085. {
  2086. USHORT serverSize = length + 7;
  2087. serverSize = serverSize >> 3;
  2088. if (serverSize < 8 && !attr.IsIn)
  2089. attr.ServerAllocSize = serverSize;
  2090. attr.IsSimpleRef = TRUE;
  2091. hr = _pTypeGen->GetOffset(offset + 2 * sizeof (BYTE), &offset);
  2092. if(FAILED(hr))
  2093. break;
  2094. }
  2095. else
  2096. {
  2097. attr.IsByValue = TRUE;
  2098. }
  2099. }
  2100. PushShort(*((short *) &attr));
  2101. PushShort(_stackSize);
  2102. PushShort(offset);
  2103. if(vt & VT_BYREF)
  2104. {
  2105. _stackSize += sizeof(REGISTER_TYPE);
  2106. }
  2107. else
  2108. {
  2109. if (vt == VT_CARRAY)
  2110. _stackSize += sizeof(REGISTER_TYPE);
  2111. else
  2112. {
  2113. USHORT ualign = sizeof(REGISTER_TYPE) -1;
  2114. #if defined(_AMD64_)
  2115. _stackSize += sizeof(REGISTER_TYPE);
  2116. #else
  2117. _stackSize += length;
  2118. #endif
  2119. _stackSize = (_stackSize + ualign) & ~ualign;
  2120. #if defined(__RPC_WIN64__)
  2121. // only check for floatmask if it UDT. top level float/double is handled already
  2122. AnalyzeFloatTypes(ParamOffset, offset);
  2123. #endif
  2124. }
  2125. }
  2126. // over estimate the buffersize to avoid buffer underrun during marshalling
  2127. if (parainfo->wIDLFlags & IDLFLAG_FIN)
  2128. {
  2129. _clientBufferSize += WireAlignment+ 1;
  2130. }
  2131. if (parainfo->wIDLFlags & IDLFLAG_FOUT)
  2132. {
  2133. _serverBufferSize += WireAlignment + 1;
  2134. }
  2135. *fChangeSize = TRUE;
  2136. }
  2137. break;
  2138. case VT_MULTIINDIRECTIONS:
  2139. attr.MustSize = TRUE;
  2140. attr.MustFree = TRUE;
  2141. PushShort(*((short *) &attr));
  2142. PushShort(_stackSize);
  2143. _stackSize += sizeof(REGISTER_TYPE);
  2144. // top level double pointers should be FC_RP FC_ALLOCED_ON_STACK|DEREF
  2145. // we have to change the type format string here because RegisterType
  2146. // doesn't know if the ** is in top level or embedded
  2147. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo);
  2148. if (SUCCEEDED(hr))
  2149. {
  2150. hr = _pTypeGen->AdjustTopLevelRef(offset);
  2151. }
  2152. if (SUCCEEDED(hr))
  2153. {
  2154. PushShort(offset);
  2155. }
  2156. break;
  2157. default:
  2158. {
  2159. if(vt & VT_ARRAY)
  2160. {
  2161. attr.MustSize = TRUE;
  2162. attr.MustFree = TRUE;
  2163. if(vt & VT_BYREF)
  2164. {
  2165. attr.IsSimpleRef = TRUE;
  2166. if(!attr.IsIn)
  2167. {
  2168. attr.ServerAllocSize = 1;
  2169. }
  2170. }
  2171. else
  2172. {
  2173. attr.IsByValue = TRUE;
  2174. }
  2175. PushShort(*((short *) &attr));
  2176. PushShort(_stackSize);
  2177. _stackSize += sizeof(REGISTER_TYPE);
  2178. hr = _pTypeGen->RegisterType(parainfo, &offset, &ProcMemInfo );
  2179. PushShort(offset);
  2180. }
  2181. else
  2182. {
  2183. if (vt & VT_VECTOR)
  2184. {
  2185. NDR_ASSERT(0, "VT_VECTOR is not supported");
  2186. }
  2187. hr = DISP_E_BADVARTYPE;
  2188. }
  2189. break;
  2190. }// end default
  2191. }
  2192. return hr;
  2193. }
  2194. //+---------------------------------------------------------------------------
  2195. //
  2196. // Method: CProcGen::IsHomogeneous
  2197. //
  2198. // Synopsis: Determine if a type is a homogeneous floating point type
  2199. //
  2200. // Parameter: pFormat -- The type info string
  2201. // fc -- FC_FLOAT or FC_DOUBLE
  2202. //
  2203. // Returns: true if the type is a homogeneous type of the specified
  2204. // FC_FLOAT or FC_DOUBLE
  2205. //
  2206. // Notes: Base types are homogeneous. Structs are homogeneous if each
  2207. // member is homogeneous.
  2208. //
  2209. //----------------------------------------------------------------------------
  2210. #if defined(__RPC_WIN64__)
  2211. bool CProcGen::IsHomogeneous(PFORMAT_STRING pFormat, FORMAT_CHARACTER fc)
  2212. {
  2213. switch (*pFormat)
  2214. {
  2215. case FC_FLOAT:
  2216. case FC_DOUBLE:
  2217. return ( *pFormat == fc );
  2218. case FC_STRUCT:
  2219. return IsHomogeneousMemberLayout(pFormat + 4, fc);
  2220. case FC_BOGUS_STRUCT:
  2221. return IsHomogeneousMemberLayout(pFormat + 8, fc);
  2222. }
  2223. return false;
  2224. }
  2225. #endif // __RPC_WIN64__
  2226. //+---------------------------------------------------------------------------
  2227. //
  2228. // Method: CProcGen::IsHomogeneousMemberLayout
  2229. //
  2230. // Synopsis: Determine if a member layout is homogeneous
  2231. //
  2232. // Parameter: pFormat -- The type info string
  2233. // fc -- FC_FLOAT or FC_DOUBLE
  2234. //
  2235. // Returns: true if the type is a homogeneous layout of the specified
  2236. // FC_FLOAT or FC_DOUBLE
  2237. //
  2238. // Notes: Base types are homogeneous. Structs are homogeneous if each
  2239. // member is homogeneous.
  2240. //
  2241. //----------------------------------------------------------------------------
  2242. #if defined(__RPC_WIN64__)
  2243. bool CProcGen::IsHomogeneousMemberLayout(PFORMAT_STRING pFormat, FORMAT_CHARACTER fc)
  2244. {
  2245. while (*pFormat != FC_END)
  2246. {
  2247. switch (*pFormat)
  2248. {
  2249. case FC_FLOAT:
  2250. case FC_DOUBLE:
  2251. if ( *pFormat != fc )
  2252. return false;
  2253. ++pFormat;
  2254. break;
  2255. case FC_EMBEDDED_COMPLEX:
  2256. pFormat += 2;
  2257. if ( !IsHomogeneous( pFormat + *(short UNALIGNED *)pFormat, fc ) )
  2258. return false;
  2259. pFormat += 2;
  2260. break;
  2261. case FC_PAD:
  2262. pFormat += 1;
  2263. break;
  2264. default:
  2265. return false;
  2266. }
  2267. }
  2268. return true;
  2269. }
  2270. #endif __RPC_WIN64__
  2271. //+---------------------------------------------------------------------------
  2272. //
  2273. // Method: CProcGen::AnalyzeFloatTypes
  2274. //
  2275. // Synopsis: Analyse a type to see if it is a homogeneous floating point
  2276. // type and adjust the floating point mask accordingly.
  2277. //
  2278. // Parameter: ParamOffset -- Offset of the param from the stack top
  2279. // offset -- The offset into the type string of the type
  2280. //
  2281. //----------------------------------------------------------------------------
  2282. #if defined(__RPC_WIN64__)
  2283. void CProcGen::AnalyzeFloatTypes(USHORT ParamOffset, USHORT offset)
  2284. {
  2285. enum FloatType
  2286. {
  2287. NonFloat = 0,
  2288. Single = 1,
  2289. Double = 2,
  2290. DualSingle = 3
  2291. }
  2292. type;
  2293. PFORMAT_STRING pFormat = _pTypeGen->GetFormatString() + offset;
  2294. bool issingle = IsHomogeneous(pFormat, FC_FLOAT);
  2295. bool isdouble = IsHomogeneous(pFormat, FC_DOUBLE);
  2296. if ( issingle || isdouble )
  2297. {
  2298. ULONG paramSlot = ParamOffset /= sizeof(REGISTER_TYPE);
  2299. long members;
  2300. if ( FC_FLOAT == *pFormat || FC_DOUBLE == *pFormat )
  2301. members = 1;
  2302. else
  2303. members = _pTypeGen->GetStructSize() / ( isdouble ? 8 : 4 );
  2304. while (members > 0 && _usFloatSlots < 8 && paramSlot < 8)
  2305. {
  2306. if ( isdouble )
  2307. type = Double;
  2308. else if ( members > 1 && _usFloatSlots < 7 )
  2309. type = DualSingle;
  2310. else
  2311. type = Single;
  2312. _usFloatArgMask |= type << (paramSlot * 2);
  2313. paramSlot += 1;
  2314. members -= 1 + (DualSingle == type);
  2315. _usFloatSlots += 1 + (DualSingle == type);
  2316. }
  2317. }
  2318. }
  2319. #endif // __RPC_WIN64__
  2320. HRESULT CProcGen::PushByte(
  2321. IN byte b)
  2322. {
  2323. BYTE *pb = (BYTE *) &_pProcFormatString[_offset];
  2324. *pb = b;
  2325. _offset += sizeof(b);
  2326. return S_OK;
  2327. }
  2328. HRESULT CProcGen::PushShort(
  2329. IN USHORT s)
  2330. {
  2331. short UNALIGNED *ps = (UNALIGNED short*)&_pProcFormatString[_offset];
  2332. *ps = s;
  2333. _offset += sizeof(s);
  2334. return S_OK;
  2335. }
  2336. HRESULT CProcGen::PushLong(
  2337. IN ULONG s)
  2338. {
  2339. long UNALIGNED *ps = (UNALIGNED long*)&_pProcFormatString[_offset];
  2340. *ps = s;
  2341. _offset += sizeof(s);
  2342. return S_OK;
  2343. }
  2344. HRESULT CProcGen::SetShort(
  2345. IN USHORT offset,
  2346. IN USHORT data)
  2347. {
  2348. if (offset >= _offset)
  2349. return E_INVALIDARG;
  2350. *((UNALIGNED short *) &_pProcFormatString[offset]) = data;
  2351. return S_OK;
  2352. }
  2353. //+---------------------------------------------------------------------------
  2354. //
  2355. // Method: CTypeGen::CTypeGen
  2356. //
  2357. // Synopsis: Constructor for type generator.
  2358. //
  2359. //----------------------------------------------------------------------------
  2360. CTypeGen::CTypeGen()
  2361. {
  2362. Init();
  2363. }
  2364. //+---------------------------------------------------------------------------
  2365. //
  2366. // Method: CTypeGen::Init
  2367. //
  2368. // Synopsis: Initialize the type generator.
  2369. //
  2370. //----------------------------------------------------------------------------
  2371. void CTypeGen::Init()
  2372. {
  2373. _pTypeFormat = __MIDL_TypeFormatString.Format;
  2374. _cbTypeFormat = TYPE_FORMAT_STRING_SIZE;
  2375. //The _offset must be aligned on a 4 byte boundary.
  2376. //Note that this may result in _offset > _cbTypeFormat.
  2377. _offset = (TYPE_FORMAT_STRING_SIZE + 3) & ~3;
  2378. }
  2379. //+---------------------------------------------------------------------------
  2380. //
  2381. // Method: CTypeGen::~CTypeGen
  2382. //
  2383. // Synopsis: Destructor for type generator.
  2384. //
  2385. //----------------------------------------------------------------------------
  2386. CTypeGen::~CTypeGen()
  2387. {
  2388. ReleaseTypeFormatString(_pTypeFormat);
  2389. }
  2390. //+---------------------------------------------------------------------------
  2391. //
  2392. // Method: CTypeGen::GetTypeFormatString
  2393. //
  2394. // Synopsis: Get the MIDL_TYPE_FORMAT_STRING.
  2395. //
  2396. // Arguments: ppTypeFormatString - Returns a pointer to the type format
  2397. // string.
  2398. //
  2399. // pLength - Returns the length of the format string.
  2400. //
  2401. //----------------------------------------------------------------------------
  2402. HRESULT CTypeGen::GetTypeFormatString(
  2403. OUT PFORMAT_STRING * ppTypeFormatString,
  2404. OUT USHORT * pLength)
  2405. {
  2406. HRESULT hr = S_OK;
  2407. *ppTypeFormatString = _pTypeFormat;
  2408. if(_offset < _cbTypeFormat)
  2409. {
  2410. *pLength = _offset;
  2411. }
  2412. else
  2413. {
  2414. *pLength = _cbTypeFormat;
  2415. }
  2416. //Clear the type format string.
  2417. Init();
  2418. return hr;
  2419. }
  2420. //+---------------------------------------------------------------------------
  2421. //
  2422. // Method: CTypeGen::UpdateStructInfo
  2423. //
  2424. // Synopsis: Update the memory information of the specified vt type.
  2425. //
  2426. // Arguments: uPackingLevel: packing level specified by tlb. Same as /Zp setting in MIDL
  2427. // packing level is the maximum alignment in memory. It doesn't affect wire format but
  2428. // it could force a structure to be bogus.
  2429. //
  2430. //
  2431. //----------------------------------------------------------------------------
  2432. void
  2433. CTypeGen::UpdateStructInfo( MemoryInfo *pStructInfo,
  2434. VARTYPE vtnoref,
  2435. long IsRef,
  2436. ushort uPackingLevel )
  2437. {
  2438. MemoryInfo const*pInfo;
  2439. if ( IsRef )
  2440. pInfo = &VarMemInfo[VT_PTR];
  2441. else
  2442. pInfo = &VarMemInfo[vtnoref];
  2443. pStructInfo->MemorySize = pInfo->MemorySize;
  2444. if ( pInfo->MemoryAlignment > uPackingLevel )
  2445. pStructInfo->MemoryAlignment = uPackingLevel;
  2446. else
  2447. pStructInfo->MemoryAlignment = pInfo->MemoryAlignment;
  2448. pStructInfo->WireAlignment = pInfo->WireAlignment;
  2449. pStructInfo->WireSize = pInfo->WireSize;
  2450. }
  2451. //+---------------------------------------------------------------------------
  2452. //
  2453. // Method: CTypeGen::RegisterType
  2454. //
  2455. // Synopsis: Registers a top-level type in the type format string.
  2456. //
  2457. // Arguments: pTypeDesc - Supplies the type descriptor.
  2458. // pOffset - Returns the offset in the type format string.
  2459. //
  2460. //----------------------------------------------------------------------------
  2461. HRESULT CTypeGen::RegisterType(
  2462. IN PARAMINFO * parainfo,
  2463. OUT USHORT * pOffset,
  2464. OUT MemoryInfo * pStructInfo)
  2465. {
  2466. HRESULT hr = S_OK;
  2467. VARTYPE vt = parainfo->vt;
  2468. VARTYPE vtnoref = vt & (~VT_BYREF);
  2469. long IsRef = (vt & VT_BYREF) ? 1 : 0;
  2470. IID *piid = &(parainfo->iid);
  2471. USHORT uPackingLevel = parainfo->cbAlignment;
  2472. // we don't support vector; vt_array is safearray, treated separately
  2473. if ( vt & (VT_ARRAY | VT_VECTOR | VT_RESERVED ) )
  2474. {
  2475. if(vt & VT_ARRAY)
  2476. {
  2477. hr = RegisterSafeArray(parainfo, pOffset);
  2478. }
  2479. else
  2480. {
  2481. hr = DISP_E_BADVARTYPE;
  2482. }
  2483. vtnoref = VT_SAFEARRAY;
  2484. if ( SUCCEEDED(hr ) )
  2485. {
  2486. // safearray is pointer.
  2487. UpdateStructInfo( pStructInfo, VT_SAFEARRAY, FALSE , uPackingLevel );
  2488. }
  2489. return hr;
  2490. }
  2491. // it's at the end and we can't folk this into the offset array.
  2492. if ( vt == VT_MULTIINDIRECTIONS )
  2493. {
  2494. // realvt is already VT_BYREF | something.
  2495. // in multiple indirections case, we generate multiple
  2496. // FC_UP FC_POINTER_DEREF. It'll be fixed up if it's
  2497. // top level parameter.
  2498. parainfo->vt = parainfo->realvt;
  2499. ASSERT(parainfo->vt != VT_MULTIINDIRECTIONS);
  2500. hr = RegisterType( parainfo,pOffset, pStructInfo );
  2501. parainfo->vt = VT_MULTIINDIRECTIONS;
  2502. vtnoref = parainfo->realvt;
  2503. if (SUCCEEDED(hr))
  2504. {
  2505. USHORT tmpOffset = *pOffset;
  2506. USHORT prevOffset;
  2507. ASSERT(parainfo->lLevelCount > 0);
  2508. for (LONG i = 0; i < parainfo->lLevelCount; i++ )
  2509. {
  2510. prevOffset = _offset;
  2511. PushByte(FC_UP);
  2512. PushByte(FC_POINTER_DEREF);
  2513. PushOffset(tmpOffset);
  2514. tmpOffset = prevOffset;
  2515. }
  2516. *pOffset = prevOffset;
  2517. // this is multiple indirection case, it's a pointer.
  2518. UpdateStructInfo( pStructInfo, VT_PTR, TRUE, uPackingLevel );
  2519. }
  2520. }
  2521. else
  2522. {
  2523. *pOffset = OffsetArray[vtnoref][IsRef];
  2524. UpdateStructInfo( pStructInfo, vtnoref, IsRef, uPackingLevel );
  2525. // take care of special cases and invalid cases.
  2526. if ( *pOffset == 0xffff )
  2527. {
  2528. switch ( vtnoref )
  2529. {
  2530. case VT_INTERFACE:
  2531. hr = RegisterInterfacePointer(parainfo, pOffset);
  2532. break;
  2533. case VT_USERDEFINED:
  2534. hr = RegisterUDT(parainfo, pOffset, pStructInfo);
  2535. break;
  2536. case VT_I8:
  2537. case VT_UI8:
  2538. case VT_CY:
  2539. if ( IsRef )
  2540. {
  2541. *pOffset = _offset;
  2542. PushByte(FC_UP);
  2543. PushByte(FC_SIMPLE_POINTER);
  2544. PushByte(FC_HYPER);
  2545. PushByte(FC_PAD);
  2546. }
  2547. break;
  2548. case VT_CARRAY:
  2549. hr = RegisterCArray(parainfo,pOffset,pStructInfo);
  2550. break;
  2551. default:
  2552. NDR_ASSERT(0, "invalid vartype");
  2553. hr = DISP_E_BADVARTYPE;
  2554. }
  2555. }
  2556. }
  2557. return hr;
  2558. }
  2559. //+---------------------------------------------------------------------------
  2560. //
  2561. // Method: CTypeGen::RegisterInterfacePointer
  2562. //
  2563. // Synopsis: Register an interface pointer in the type format string.
  2564. //
  2565. // Arguments: riid - Supplies the IID of the interface.
  2566. // pOffset - Returns the type offset of the interface pointer.
  2567. //
  2568. //----------------------------------------------------------------------------
  2569. HRESULT CTypeGen::RegisterInterfacePointer(
  2570. IN PARAMINFO* parainfo,
  2571. OUT USHORT *pOffset)
  2572. {
  2573. HRESULT hr = E_FAIL;
  2574. USHORT offset;
  2575. VARTYPE vt = parainfo->vt;
  2576. IID* piid = &(parainfo->iid);
  2577. offset = _offset;
  2578. hr = PushByte(FC_IP);
  2579. if(FAILED(hr))
  2580. return hr;
  2581. hr = PushByte(FC_CONSTANT_IID);
  2582. if(FAILED(hr))
  2583. return hr;
  2584. hr = PushIID(*piid);
  2585. if(FAILED(hr))
  2586. return hr;
  2587. if(vt & VT_BYREF)
  2588. {
  2589. *pOffset = _offset;
  2590. hr = PushByte(FC_RP);
  2591. if(FAILED(hr))
  2592. return hr;
  2593. hr = PushByte(FC_POINTER_DEREF);
  2594. if(FAILED(hr))
  2595. return hr;
  2596. hr = PushOffset(offset);
  2597. }
  2598. else
  2599. {
  2600. *pOffset = offset;
  2601. }
  2602. return hr;
  2603. }
  2604. //+---------------------------------------------------------------------------
  2605. //
  2606. // Method: CTypeGen::RegisterCArray
  2607. //
  2608. // Synopsis: generate format string for a fixed size array.
  2609. //
  2610. // Arguments: pTypeDesc - Supplies the type descriptor.
  2611. // pOffset - Returns the offset in the type format string.
  2612. //
  2613. //----------------------------------------------------------------------------
  2614. HRESULT CTypeGen::RegisterCArray(
  2615. IN PARAMINFO* parainfo,
  2616. OUT USHORT *pOffset,
  2617. OUT MemoryInfo *pStructInfo)
  2618. {
  2619. HRESULT hr = S_OK;
  2620. ARRAYDESC *padesc = parainfo->pArray;
  2621. VARTYPE vt = padesc->tdescElem.vt;
  2622. ULONG ulCount=1;
  2623. USHORT WireAlignment = 0,ussize = 0,offset;
  2624. BYTE fcElem,fcStruct;
  2625. PARAMINFO iteminfo;
  2626. USHORT uPackingLevel = parainfo->cbAlignment;
  2627. MemoryInfo MemInfo;
  2628. for ( int i = 0; i < padesc->cDims; i++)
  2629. ulCount *= padesc->rgbounds[i].cElements ;
  2630. memset( &MemInfo, 0, sizeof( MemoryInfo ) );
  2631. if ( vt > VT_VERSIONED_STREAM )
  2632. {
  2633. NDR_ASSERT(0, "invalid c-array type");
  2634. hr = DISP_E_BADVARTYPE;
  2635. goto Error;
  2636. }
  2637. fcElem = VT_FC_MAP[vt];
  2638. MemInfo = VarMemInfo[vt];
  2639. // for simple types, wire size is the same as memory size;
  2640. // for complex data, it's bogus array and the value here is irrelavant.
  2641. // take care of special case here.
  2642. if ( fcElem == FC_ZERO )
  2643. {
  2644. switch (vt)
  2645. {
  2646. case VT_USERDEFINED:
  2647. case VT_INTERFACE:
  2648. case VT_PTR:
  2649. case VT_CARRAY:
  2650. // we need some more information other than vt type.
  2651. {
  2652. ITypeInfo* pTempInfo = parainfo->pTypeInfo;
  2653. fcElem = FC_EMBEDDED_COMPLEX;
  2654. hr = VarVtOfTypeDesc(parainfo->pTypeInfo,&(padesc->tdescElem),&iteminfo);
  2655. if (SUCCEEDED(hr))
  2656. {
  2657. hr = RegisterType(&iteminfo,&offset,&MemInfo);
  2658. // it's a bogus array if wire size is diffrent from memorysize
  2659. }
  2660. if (FAILED(hr))
  2661. goto Error;
  2662. }
  2663. if ( offset == 0 )
  2664. {
  2665. fcElem = VT_FC_MAP[iteminfo.vt];
  2666. }
  2667. break;
  2668. default:
  2669. hr = DISP_E_BADVARTYPE;
  2670. goto Error;
  2671. }
  2672. }
  2673. else
  2674. if ( fcElem == FC_EMBEDDED_COMPLEX )
  2675. {
  2676. iteminfo.vt = vt;
  2677. hr = RegisterType(&iteminfo,&offset, &MemInfo);
  2678. if (FAILED(hr))
  2679. goto Error;
  2680. }
  2681. ussize = MemInfo.MemorySize;
  2682. WireAlignment = MemInfo.WireAlignment;
  2683. *pOffset = _offset;
  2684. // simple array. ulCount is the total size(same for wire & mem)
  2685. if (fcElem != FC_EMBEDDED_COMPLEX)
  2686. {
  2687. ulCount *= ussize;
  2688. if (ulCount <= _UI16_MAX)
  2689. {
  2690. PushByte(FC_SMFARRAY);
  2691. PushByte((BYTE)WireAlignment);
  2692. PushShort((SHORT)ulCount);
  2693. PushByte(fcElem);
  2694. }
  2695. else
  2696. {
  2697. PushByte(FC_LGFARRAY);
  2698. PushByte((BYTE)WireAlignment);
  2699. PushLong(ulCount);
  2700. PushByte(fcElem);
  2701. }
  2702. }
  2703. else
  2704. {
  2705. hr = GetByte(offset,&fcStruct);
  2706. if (FAILED(hr))
  2707. goto Error;
  2708. switch (fcStruct)
  2709. {
  2710. case FC_STRUCT:
  2711. ulCount *= ussize;
  2712. if ( ulCount <= _UI16_MAX )
  2713. {
  2714. PushByte(FC_SMFARRAY);
  2715. PushByte((BYTE)WireAlignment);
  2716. PushShort((SHORT)ulCount); // total size
  2717. PushByte(FC_EMBEDDED_COMPLEX);
  2718. PushByte(0);
  2719. PushOffset(offset);
  2720. PushByte(FC_PAD);
  2721. }
  2722. else
  2723. {
  2724. PushByte(FC_LGFARRAY);
  2725. PushByte((BYTE)WireAlignment);
  2726. PushLong(ulCount); // total size
  2727. PushByte(FC_EMBEDDED_COMPLEX);
  2728. PushByte(0);
  2729. PushOffset(offset);
  2730. PushByte(FC_PAD);
  2731. }
  2732. break;
  2733. // FC_EMBEDDED_COMPLEX-FC_UP within a complex array
  2734. // should be the same as FC_BOGUSY_ARRAY-FC_UP directly
  2735. case FC_UP:
  2736. case FC_RP:
  2737. {
  2738. byte fctmp,bflag;
  2739. USHORT tmpoffset;
  2740. PushByte(FC_BOGUS_ARRAY);
  2741. PushByte((BYTE)WireAlignment);
  2742. PushShort((SHORT)ulCount); // this is the count
  2743. PushLong(0xffffffff); // no conformance description
  2744. #if defined(__RPC_WIN64__)
  2745. PushShort(0); // 6 bytes description in /robust
  2746. #endif
  2747. PushLong(0xffffffff); // no variance description
  2748. #if defined(__RPC_WIN64__)
  2749. PushShort(0);
  2750. #endif
  2751. GetByte(offset, &fctmp);
  2752. PushByte(fctmp);
  2753. GetByte(offset+1, &bflag);
  2754. PushByte(bflag);
  2755. GetOffset(offset+2, &tmpoffset);
  2756. PushOffset(tmpoffset);
  2757. PushByte(FC_PAD);
  2758. ulCount *= PTR_MEM_SIZE;
  2759. break;
  2760. }
  2761. case FC_BOGUS_STRUCT:
  2762. case FC_USER_MARSHAL:
  2763. case FC_IP:
  2764. PushByte(FC_BOGUS_ARRAY);
  2765. PushByte((BYTE)WireAlignment);
  2766. PushShort((SHORT)ulCount); // this is the count
  2767. PushLong(0xffffffff); // no conformance description
  2768. #if defined(__RPC_WIN64__)
  2769. PushShort(0);
  2770. #endif
  2771. PushLong(0xffffffff); // no variance description
  2772. #if defined(__RPC_WIN64__)
  2773. PushShort(0); // correlation description
  2774. #endif
  2775. PushByte(FC_EMBEDDED_COMPLEX);
  2776. PushByte(0); // the first element
  2777. PushOffset(offset);
  2778. PushByte(FC_PAD);
  2779. ulCount *= ussize;
  2780. break;
  2781. default:
  2782. hr = DISP_E_BADVARTYPE;
  2783. }
  2784. }
  2785. PushByte(FC_END);
  2786. if (parainfo->vt & VT_BYREF)
  2787. {
  2788. USHORT uTemp = _offset;
  2789. PushByte(FC_UP);
  2790. PushByte(0);
  2791. PushOffset(*pOffset);
  2792. *pOffset = uTemp;
  2793. UpdateStructInfo(pStructInfo, vt, TRUE, uPackingLevel); // isbyref
  2794. }
  2795. else
  2796. {
  2797. UpdateStructInfo( pStructInfo, vt, FALSE, uPackingLevel); // not byref
  2798. // special case for larger array. make sure structures
  2799. // embedding a big array would be rejected
  2800. if (ulCount > _UI16_MAX)
  2801. ulCount = _UI16_MAX;
  2802. pStructInfo->MemorySize = (USHORT)ulCount;
  2803. pStructInfo->WireSize = (USHORT)ulCount;
  2804. }
  2805. Error:
  2806. return hr;
  2807. }
  2808. //+---------------------------------------------------------------------------
  2809. //
  2810. // Method: CTypeGen::RegisterUDT
  2811. //
  2812. // Synopsis: Register a user defined type in the type format string.
  2813. //
  2814. // Arguments: pParamInfo - Supplies the user defined type.
  2815. // pOffset - Returns the type offset of the struct.
  2816. // pStructInfo - HIWORD is alignment, LOWORD is size.
  2817. //
  2818. //----------------------------------------------------------------------------
  2819. HRESULT CTypeGen::RegisterUDT(
  2820. IN PARAMINFO * pParamInfo,
  2821. OUT USHORT * pOffset,
  2822. OUT MemoryInfo * pStructInfo)
  2823. {
  2824. HRESULT hr;
  2825. TYPEATTR * pTypeAttr;
  2826. PARAMINFO paramInfo;
  2827. ITypeInfo * pTypeInfo = pParamInfo->pTypeInfo;
  2828. *pOffset = 0;
  2829. hr = pTypeInfo->lpVtbl->GetTypeAttr(pTypeInfo,
  2830. &pTypeAttr);
  2831. if(SUCCEEDED(hr))
  2832. {
  2833. pParamInfo->cbAlignment = pTypeAttr->cbAlignment -1;
  2834. switch(pTypeAttr->typekind)
  2835. {
  2836. case TKIND_RECORD:
  2837. hr = RegisterStruct(pParamInfo, pOffset, pStructInfo);
  2838. NDR_ASSERT( pStructInfo->MemorySize != 0, "struct size can't be 0");
  2839. break;
  2840. case TKIND_ALIAS:
  2841. hr = VarVtOfTypeDesc(pTypeInfo, &pTypeAttr->tdescAlias, &paramInfo);
  2842. if(SUCCEEDED(hr))
  2843. {
  2844. hr = RegisterType(&paramInfo, pOffset, pStructInfo);
  2845. NDR_ASSERT( pStructInfo->MemorySize != 0, "data size can't be 0");
  2846. }
  2847. if (FAILED(hr))
  2848. break;
  2849. if (0 == *pOffset)
  2850. // this is an aliases to a simple type. We should just pass the vt type back
  2851. {
  2852. pParamInfo->vt = paramInfo.vt;
  2853. }
  2854. break;
  2855. case TKIND_DISPATCH:
  2856. case TKIND_INTERFACE:
  2857. hr = VarVtOfIface(pTypeInfo, pTypeAttr, &paramInfo);
  2858. if(SUCCEEDED(hr))
  2859. {
  2860. hr = RegisterType(&paramInfo, pOffset, pStructInfo);
  2861. }
  2862. break;
  2863. case TKIND_ENUM:
  2864. pParamInfo->vt = VT_I4;
  2865. *pOffset = 0;
  2866. UpdateStructInfo( pStructInfo, VT_I4, FALSE , pParamInfo->cbAlignment);
  2867. break;
  2868. case TKIND_MODULE:
  2869. case TKIND_COCLASS:
  2870. case TKIND_UNION:
  2871. default:
  2872. hr = DISP_E_BADVARTYPE;
  2873. break;
  2874. }
  2875. }
  2876. pTypeInfo->lpVtbl->ReleaseTypeAttr(pTypeInfo,pTypeAttr);
  2877. return hr;
  2878. }
  2879. //+---------------------------------------------------------------------------
  2880. //
  2881. // Method: CTypeGen::RegisterStruct
  2882. //
  2883. // Synopsis: Register an user defined struct in the type format string.
  2884. //
  2885. // Arguments: riid - Supplies the ITypeInfo for that struct.
  2886. // pOffset - Returns the type offset of the struct.
  2887. // pStructInfo: HIWORD is alignment, LOWORD is size.
  2888. //
  2889. //----------------------------------------------------------------------------
  2890. HRESULT CTypeGen::RegisterStruct(
  2891. IN PARAMINFO * parainfo,
  2892. OUT USHORT * pOffset,
  2893. OUT MemoryInfo * pStructInfo)
  2894. {
  2895. HRESULT hr = S_OK;
  2896. TYPEATTR *pTypeAttr;
  2897. VARDESC **ppVarDesc;
  2898. ITypeInfo* pInfo = parainfo->pTypeInfo;
  2899. VARTYPE vt = parainfo->vt;
  2900. FORMAT_CHARACTER fcStruct = FC_STRUCT;
  2901. BYTE fcTemp;
  2902. USHORT *poffsets;
  2903. MemoryInfo *pFieldInfos;
  2904. USHORT uPacklingLevel;
  2905. int i = 0;
  2906. hr = pInfo->lpVtbl->GetTypeAttr(pInfo,&pTypeAttr);
  2907. if (FAILED(hr))
  2908. return hr;
  2909. *pOffset = 0;
  2910. uPacklingLevel = parainfo->cbAlignment;
  2911. poffsets = (USHORT *)alloca(pTypeAttr->cVars * sizeof(USHORT));
  2912. memset(poffsets,0,pTypeAttr->cVars * sizeof(USHORT));
  2913. pFieldInfos = (MemoryInfo *)alloca(pTypeAttr->cVars * sizeof(MemoryInfo));
  2914. memset(pFieldInfos,0,pTypeAttr->cVars * sizeof(MemoryInfo));
  2915. ppVarDesc = (VARDESC **)alloca(pTypeAttr->cVars * sizeof(VARDESC *));
  2916. memset(ppVarDesc,0,pTypeAttr->cVars * sizeof(VARDESC *));
  2917. for(i = 0; SUCCEEDED(hr) && i < pTypeAttr->cVars; i++)
  2918. {
  2919. hr = pInfo->lpVtbl->GetVarDesc(pInfo,i, &ppVarDesc[i]);
  2920. if(SUCCEEDED(hr))
  2921. {
  2922. VARKIND varkind = ppVarDesc[i]->varkind;
  2923. PARAMINFO iteminfo;
  2924. iteminfo.cbAlignment = pTypeAttr->cbAlignment - 1;
  2925. BOOL IsRef;
  2926. switch (varkind)
  2927. {
  2928. case VAR_PERINSTANCE:
  2929. iteminfo.wIDLFlags = parainfo->wIDLFlags;
  2930. iteminfo.vt = ppVarDesc[i]->elemdescVar.tdesc.vt;
  2931. IsRef = iteminfo.vt & VT_BYREF;
  2932. switch (iteminfo.vt & ~VT_BYREF)
  2933. {
  2934. case VT_USERDEFINED:
  2935. ITypeInfo *pTempTI;
  2936. hr = pInfo->lpVtbl->GetRefTypeInfo(pInfo,ppVarDesc[i]->elemdescVar.tdesc.hreftype, &pTempTI);
  2937. if (FAILED(hr))
  2938. goto Error;
  2939. iteminfo.pTypeInfo = pTempTI;
  2940. hr = RegisterUDT(&iteminfo,&poffsets[i],&pFieldInfos[i]);
  2941. if (FAILED(hr))
  2942. goto Error;
  2943. if (0 == poffsets[i])
  2944. // the UDT in fact is a simple type (alias or TKIND_ENUM). pass the type back
  2945. // this is a hack. If the member is typedef->typedef<>...tkind_enum, it's effectively a
  2946. // simple type, and we bring the real type up here to be used in Parse & PushStruct.
  2947. {
  2948. ppVarDesc[i]->elemdescVar.tdesc.vt = iteminfo.vt;
  2949. UpdateStructInfo( &pFieldInfos[i], iteminfo.vt, IsRef, uPacklingLevel);
  2950. }
  2951. break;
  2952. // special case: the top level parameter case is treated differently
  2953. case VT_DECIMAL:
  2954. if ( IsRef )
  2955. {
  2956. poffsets[i] = BYREF_DECIMAL_TYPE_FS_OFFSET ;
  2957. }
  2958. else
  2959. {
  2960. poffsets[i] = DECIMAL_TYPE_FS_OFFSET;
  2961. }
  2962. UpdateStructInfo(&pFieldInfos[i], VT_DECIMAL, IsRef, uPacklingLevel);
  2963. break;
  2964. case VT_LPSTR:
  2965. if (IsRef)
  2966. {
  2967. poffsets[i] = BYREF_LPSTR_TYPE_FS_OFFSET ;
  2968. }
  2969. else
  2970. {
  2971. poffsets[i] = EMBEDDED_LPSTR_TYPE_FS_OFFSET ;
  2972. }
  2973. UpdateStructInfo(&pFieldInfos[i], VT_LPSTR, IsRef, uPacklingLevel);
  2974. break;
  2975. case VT_LPWSTR:
  2976. if ( IsRef )
  2977. {
  2978. poffsets[i] = BYREF_LPWSTR_TYPE_FS_OFFSET ;
  2979. }
  2980. else
  2981. {
  2982. poffsets[i] = EMBEDDED_LPWSTR_TYPE_FS_OFFSET ;
  2983. }
  2984. UpdateStructInfo(&pFieldInfos[i], VT_LPWSTR, IsRef, uPacklingLevel);
  2985. break;
  2986. // doesn't need special case
  2987. case VT_FILETIME:
  2988. // all the following are user marshalls.
  2989. case VT_DISPATCH:
  2990. case VT_UNKNOWN:
  2991. case VT_INTERFACE:
  2992. case VT_STREAM:
  2993. case VT_STORAGE:
  2994. case VT_BSTR:
  2995. case VT_VARIANT:
  2996. hr = RegisterType(&iteminfo,&(poffsets[i]),&(pFieldInfos[i]));
  2997. if (FAILED(hr))
  2998. goto Error;
  2999. break;
  3000. case VT_PTR:
  3001. case VT_SAFEARRAY: // 13129
  3002. {
  3003. PARAMINFO ptrInfo;
  3004. hr = VarVtOfTypeDesc(pInfo,&(ppVarDesc[i]->elemdescVar.tdesc),&ptrInfo);
  3005. if (SUCCEEDED(hr))
  3006. hr = RegisterType(&ptrInfo,&(poffsets[i]),&(pFieldInfos[i]));
  3007. if (FAILED(hr))
  3008. goto Error;
  3009. // these are really pointers.
  3010. UpdateStructInfo(&pFieldInfos[i], iteminfo.vt, IsRef, uPacklingLevel);
  3011. }
  3012. break;
  3013. case VT_CARRAY:
  3014. iteminfo.pArray = ppVarDesc[i]->elemdescVar.tdesc.lpadesc;
  3015. iteminfo.pTypeInfo = parainfo->pTypeInfo;
  3016. iteminfo.pTypeInfo->lpVtbl->AddRef(iteminfo.pTypeInfo);
  3017. iteminfo.vt = ppVarDesc[i]->elemdescVar.tdesc.vt;
  3018. hr = RegisterCArray(&iteminfo,&poffsets[i],&pFieldInfos[i]);
  3019. if (FAILED(hr))
  3020. goto Error;
  3021. break;
  3022. }
  3023. // this member is not a simple type.
  3024. // we could have some optimization here but for now we just
  3025. // generate complex struct.
  3026. if (poffsets[i])
  3027. {
  3028. hr = GetByte(poffsets[i],&fcTemp);
  3029. if (FAILED(hr))
  3030. return hr;
  3031. switch (fcTemp)
  3032. {
  3033. case FC_PSTRUCT:
  3034. case FC_CSTRUCT:
  3035. case FC_CVSTRUCT:
  3036. hr = DISP_E_BADVARTYPE;
  3037. break;
  3038. case FC_STRUCT:
  3039. case FC_BOGUS_STRUCT:
  3040. case FC_USER_MARSHAL:
  3041. case FC_LGFARRAY:
  3042. case FC_SMFARRAY:
  3043. case FC_BOGUS_ARRAY:
  3044. case FC_IP:
  3045. case FC_UP:
  3046. case FC_OP:
  3047. case FC_RP:
  3048. fcStruct = FC_BOGUS_STRUCT;
  3049. break;
  3050. default:
  3051. NDR_ASSERT(0, "invalid struct type");
  3052. fcStruct = FC_BOGUS_STRUCT;
  3053. break;
  3054. }
  3055. }
  3056. break;
  3057. default: // all other types shouldn't happen .
  3058. hr = DISP_E_BADVARTYPE;
  3059. break;
  3060. }
  3061. }
  3062. else
  3063. goto Error;
  3064. }
  3065. if (FAILED(hr))
  3066. goto Error;
  3067. hr = PushStruct(parainfo,fcStruct,ppVarDesc,poffsets,pFieldInfos,pTypeAttr->cVars,pOffset,pStructInfo);
  3068. Error:
  3069. for (int j = 0; j < pTypeAttr->cVars ; j++)
  3070. if (ppVarDesc[j])
  3071. pInfo->lpVtbl->ReleaseVarDesc(pInfo,ppVarDesc[j]);
  3072. pInfo->lpVtbl->ReleaseTypeAttr(pInfo,pTypeAttr);
  3073. return hr;
  3074. }
  3075. //+---------------------------------------------------------------------------
  3076. //
  3077. // Method: CTypeGen::PushStruct
  3078. //
  3079. // Synopsis: This should be part of RegisterStruct, seperate them just because
  3080. // the function is too long.
  3081. //
  3082. // Arguments: parainfo - parameter information.
  3083. // fcStruct - type of struct.
  3084. // ppVarDesc - variable description, if applicable.
  3085. // poffsets - offset of embedded complex members.
  3086. // pdwStructInfo - size/pad of embedded complex members.
  3087. // uNumElement - number of members in the struct.
  3088. // pOffset - Returns the type offset of the struct.
  3089. //
  3090. //----------------------------------------------------------------------------
  3091. HRESULT CTypeGen::PushStruct(
  3092. IN PARAMINFO *parainfo,
  3093. IN FORMAT_CHARACTER fcStruct,
  3094. IN VARDESC **ppVarDesc,
  3095. IN USHORT *poffsets,
  3096. IN MemoryInfo * pFieldInfos,
  3097. IN USHORT uNumElements,
  3098. OUT USHORT *pOffset,
  3099. OUT MemoryInfo *pStructInfo)
  3100. {
  3101. HRESULT hr = S_OK;
  3102. USHORT uStartoffset;
  3103. USHORT uMemorySize = 0,maxMemoryAlignment=0;
  3104. USHORT FieldMemoryAlignment = 0;
  3105. USHORT structpad;
  3106. int i;
  3107. VARTYPE vt = parainfo->vt;
  3108. boolean fHasPointer = FALSE;
  3109. USHORT uPackingLevel = parainfo->cbAlignment;
  3110. _uStructSize = 0;
  3111. // sizing pass, find out the size of the struct and check if we need to convert struct into bogus struct.
  3112. hr = ParseStructMembers(parainfo,&fcStruct,ppVarDesc,poffsets,pFieldInfos,uNumElements,pStructInfo);
  3113. if (FAILED(hr))
  3114. return hr;
  3115. uStartoffset = _offset; // the starting point of this struct.
  3116. _uStructSize = 0;
  3117. PushByte(fcStruct);
  3118. PushByte((BYTE)pStructInfo->WireAlignment);
  3119. PushShort(pStructInfo->MemorySize);
  3120. switch (fcStruct)
  3121. {
  3122. case FC_STRUCT:
  3123. break;
  3124. case FC_CSTRUCT:
  3125. case FC_PSTRUCT:
  3126. case FC_CVSTRUCT:
  3127. hr = E_NOTIMPL;
  3128. break;
  3129. case FC_BOGUS_STRUCT:
  3130. PushShort(0); // offset to array. E_NOTIMPLE;
  3131. PushShort(0); // offset to pointer layout.
  3132. break;
  3133. default:
  3134. hr = DISP_E_BADVARTYPE;
  3135. break;
  3136. }
  3137. if (FAILED(hr))
  3138. return hr;
  3139. for (i = 0; i < uNumElements; i++)
  3140. {
  3141. if (poffsets[i] > 0)
  3142. {
  3143. // The struct member is an embedded complex, the descsriptor of which
  3144. // is already generated.
  3145. NDR_ASSERT( pFieldInfos[i].MemorySize > 0, "MemoryInfo should have been initialized");
  3146. USHORT uPrevSize;
  3147. BYTE fcTemp;
  3148. // we need to find the offset to know if we have pointers.
  3149. // we could do that through the typelib directly though.
  3150. hr = GetByte(poffsets[i],&fcTemp);
  3151. if (FAILED(hr))
  3152. return hr;
  3153. if ( pFieldInfos[i].MemoryAlignment > uPackingLevel )
  3154. FieldMemoryAlignment = uPackingLevel;
  3155. else
  3156. FieldMemoryAlignment = pFieldInfos[i].MemoryAlignment;
  3157. uMemorySize = pFieldInfos[i].MemorySize;
  3158. if (fcTemp == FC_UP || fcTemp == FC_RP || fcTemp == FC_OP )
  3159. {
  3160. FieldMemoryAlignment = Alignment( PTR_MEM_ALIGN, uPackingLevel );
  3161. PushByte(FC_POINTER);
  3162. fHasPointer = TRUE;
  3163. _uStructSize += PTR_MEM_SIZE;
  3164. NDR_ASSERT( uMemorySize == PTR_MEM_SIZE, "invalid pointer size");
  3165. }
  3166. else
  3167. {
  3168. PushByte(FC_EMBEDDED_COMPLEX);
  3169. // push the padding required the previous field and
  3170. // following FC_EMBEDDED_COMPLEX
  3171. uPrevSize = (SHORT )_uStructSize;
  3172. LENGTH_ALIGN( _uStructSize, FieldMemoryAlignment);
  3173. PushByte( (BYTE)( _uStructSize - uPrevSize ) );
  3174. PushOffset(poffsets[i]);
  3175. _uStructSize += uMemorySize; // size of the struct
  3176. }
  3177. }
  3178. else
  3179. {
  3180. hr = GenStructSimpleTypesFormatString(parainfo,ppVarDesc[i],&FieldMemoryAlignment);
  3181. if (FAILED(hr))
  3182. return hr;
  3183. }
  3184. if ( FieldMemoryAlignment > maxMemoryAlignment )
  3185. maxMemoryAlignment = FieldMemoryAlignment;
  3186. }
  3187. structpad = (USHORT)_uStructSize & maxMemoryAlignment;
  3188. if (structpad )
  3189. {
  3190. structpad = maxMemoryAlignment - structpad;
  3191. hr = PushByte((BYTE)FC_STRUCTPAD1 + structpad) ;
  3192. if (FAILED(hr))
  3193. return hr;
  3194. }
  3195. if (!((_offset - uStartoffset) & 1))
  3196. {
  3197. hr = PushByte(FC_PAD);
  3198. if (FAILED(hr))
  3199. return hr;
  3200. }
  3201. PushByte(FC_END);
  3202. // one level of indirection if it's a pointer
  3203. if (fHasPointer)
  3204. {
  3205. USHORT tempOffset, tempAddr;
  3206. BYTE fcTemp,fcType;
  3207. tempOffset = uStartoffset + 2*sizeof(BYTE) + 2*sizeof(SHORT);
  3208. SetShort(tempOffset,_offset-tempOffset);
  3209. for (i = 0 ; i < uNumElements; i++)
  3210. {
  3211. if (poffsets[i] > 0)
  3212. {
  3213. hr = GetByte(poffsets[i],&fcTemp);
  3214. hr = GetByte(poffsets[i] + 1, &fcType);
  3215. if (FAILED(hr))
  3216. return hr;
  3217. if (fcTemp == FC_UP || fcTemp == FC_RP || fcTemp == FC_OP )
  3218. {
  3219. // should really be FC_OP here!!!
  3220. // MIDL generate FC_OP for [out] and [in,out] param.
  3221. // We can always generate FC_OP here: the engine behave differently
  3222. // only on unmarshaling in client side, and force no buffer reuse on
  3223. // server side, but even we set the flag on [in] only parameter,
  3224. // unmarshall routine is not called on the client side so it doesn't matter.
  3225. PushByte(FC_UP);
  3226. PushByte(fcType);
  3227. if (FC_SIMPLE_POINTER == fcType)
  3228. {
  3229. GetByte(poffsets[i] + 2 * sizeof(BYTE), &fcTemp);
  3230. PushByte(fcTemp);
  3231. PushByte(FC_PAD);
  3232. }
  3233. else
  3234. {
  3235. GetOffset(poffsets[i] + 2*sizeof(BYTE) , &tempAddr);
  3236. PushOffset(tempAddr);
  3237. }
  3238. }
  3239. }
  3240. }
  3241. }
  3242. if (vt & VT_BYREF)
  3243. {
  3244. *pOffset = _offset;
  3245. PushByte(FC_UP);
  3246. PushByte(0);
  3247. PushOffset(uStartoffset);
  3248. }
  3249. else
  3250. *pOffset = uStartoffset;
  3251. if (_offset & 2) // align the starting point of next struct to 4 byte boundary.
  3252. PushShort(0);
  3253. return hr;
  3254. }
  3255. //+---------------------------------------------------------------------------
  3256. //
  3257. // Method: CTypeGen::ParseStructMembers
  3258. //
  3259. // Synopsis: basically sizing pass of the struct, check if we need to convert
  3260. // struct into bogus struct when we can't memcpy the buffer.
  3261. //
  3262. // Arguments: parainfo - parameter information.
  3263. // pfcStruct - type of struct.
  3264. // ppVarDesc - variable description, if applicable.
  3265. // poffsets - offset of embedded complex members.
  3266. // pdwStructInfo - size/pad of embedded complex members.
  3267. // uNumElement - number of members in the struct.
  3268. // pStructInfo - return the struct info.
  3269. //
  3270. //----------------------------------------------------------------------------
  3271. HRESULT CTypeGen::ParseStructMembers(
  3272. IN PARAMINFO *parainfo,
  3273. IN OUT FORMAT_CHARACTER *pfcStruct,
  3274. IN VARDESC **ppVarDesc,
  3275. IN USHORT *poffsets,
  3276. IN MemoryInfo *pFieldInfos,
  3277. IN USHORT uNumElements,
  3278. OUT MemoryInfo *pStructInfo)
  3279. {
  3280. USHORT uStartoffset = 0;
  3281. BOOL fChangeToBogus = FALSE;
  3282. int i;
  3283. USHORT FieldMemoryAlignment = 0, MaxWireAlignment = 0, MaxMemoryAlignment = 0;
  3284. HRESULT hr = S_OK ;
  3285. USHORT uPacklingLevel = parainfo->cbAlignment;
  3286. memset( pStructInfo, 0, sizeof( MemoryInfo ) );
  3287. for (i = 0; i < uNumElements; i++)
  3288. {
  3289. // this is simple type. Let's fill in the memory/size info.
  3290. if ( pFieldInfos[i].MemorySize == 0 )
  3291. {
  3292. NDR_ASSERT( poffsets[i] == 0 , "invalid simple type format string offset");
  3293. hr = GetMemoryInfoForSimpleType(ppVarDesc[i]->elemdescVar.tdesc.vt,
  3294. &pFieldInfos[i]);
  3295. if (FAILED(hr))
  3296. return hr;
  3297. }
  3298. NDR_ASSERT( pFieldInfos[i].MemorySize != 0, "invalid field information");
  3299. if ( pFieldInfos[i].MemoryAlignment > uPacklingLevel )
  3300. {
  3301. // need to change to bogus struct if the struct is not naturally aligned.
  3302. FieldMemoryAlignment = uPacklingLevel;
  3303. fChangeToBogus = TRUE;
  3304. }
  3305. else
  3306. FieldMemoryAlignment = pFieldInfos[i].MemoryAlignment;
  3307. // wire alignment is put on the format string only.
  3308. if ( pFieldInfos[i].WireAlignment > MaxWireAlignment )
  3309. MaxWireAlignment = pFieldInfos[i].WireAlignment ;
  3310. if ( FieldMemoryAlignment > MaxMemoryAlignment )
  3311. MaxMemoryAlignment = FieldMemoryAlignment;
  3312. LENGTH_ALIGN( _uStructSize, FieldMemoryAlignment );
  3313. _uStructSize += pFieldInfos[i].MemorySize; // size of the struct
  3314. }
  3315. if ((USHORT)_uStructSize & MaxMemoryAlignment )
  3316. {
  3317. // it's a bogus struct if we have trailing padding.
  3318. fChangeToBogus = TRUE;
  3319. *pfcStruct = FC_BOGUS_STRUCT;
  3320. }
  3321. LENGTH_ALIGN( _uStructSize, MaxMemoryAlignment );
  3322. // we are returning the real size of struct up. In the type format generating code,
  3323. // embedded pointer size is always PTR_MEM_SIZE and the sizing info returned from here
  3324. // is ignored.
  3325. pStructInfo->MemorySize = (USHORT)_uStructSize;
  3326. pStructInfo->MemoryAlignment = MaxMemoryAlignment;
  3327. pStructInfo->WireAlignment = MaxWireAlignment;
  3328. NDR_ASSERT( _uStructSize != 0, "invalid struct size");
  3329. if (_uStructSize > _UI16_MAX)
  3330. {
  3331. hr = DISP_E_BADVARTYPE;
  3332. }
  3333. return hr;
  3334. }
  3335. //+---------------------------------------------------------------------------
  3336. //
  3337. // Method: CTypeGen::GetMemoryInfoForSimpleType
  3338. //
  3339. // Synopsis: Fill in the memory and wire size information for simple types
  3340. //
  3341. // Arguments: vt - what's the simple type.
  3342. //
  3343. //----------------------------------------------------------------------------
  3344. HRESULT CTypeGen::GetMemoryInfoForSimpleType(VARTYPE vt,
  3345. MemoryInfo *pSimpleTypeInfo)
  3346. {
  3347. HRESULT hr = S_OK;
  3348. VARTYPE vtnoref = vt & ~VT_BYREF;
  3349. if ( vt > VT_VERSIONED_STREAM )
  3350. {
  3351. NDR_ASSERT( 0, "invalid vartype" );
  3352. hr = DISP_E_BADVARTYPE;
  3353. }
  3354. *pSimpleTypeInfo = VarMemInfo[vtnoref];
  3355. if ( pSimpleTypeInfo->WireSize == 0 )
  3356. {
  3357. NDR_ASSERT(0, "invalid vartype" );
  3358. hr = DISP_E_BADVARTYPE;
  3359. }
  3360. return hr;
  3361. }
  3362. //+---------------------------------------------------------------------------
  3363. //
  3364. // Method: CTypeGen::RegisterSafeArray
  3365. //
  3366. // Synopsis: Register a safe array in the type format string.
  3367. //
  3368. // Arguments: riid - Supplies the IID of the interface.
  3369. // pOffset - Returns the type offset of the safe array.
  3370. //
  3371. //----------------------------------------------------------------------------
  3372. HRESULT CTypeGen::RegisterSafeArray(
  3373. IN PARAMINFO* pParainfo,
  3374. OUT USHORT *pOffset)
  3375. {
  3376. HRESULT hr = S_OK;
  3377. USHORT offset;
  3378. VARTYPE vt = pParainfo->vt;
  3379. IID *piid = &(pParainfo->iid);
  3380. switch(vt & ~(VT_ARRAY | VT_BYREF))
  3381. {
  3382. case VT_I2:
  3383. case VT_I4:
  3384. case VT_R4:
  3385. case VT_R8:
  3386. case VT_CY:
  3387. case VT_DATE:
  3388. case VT_BSTR:
  3389. case VT_DISPATCH:
  3390. case VT_ERROR:
  3391. case VT_BOOL:
  3392. case VT_VARIANT:
  3393. case VT_UNKNOWN:
  3394. case VT_DECIMAL:
  3395. case VT_I1:
  3396. case VT_UI1:
  3397. case VT_UI2:
  3398. case VT_UI4:
  3399. case VT_I8:
  3400. case VT_UI8:
  3401. case VT_INT:
  3402. case VT_UINT:
  3403. case VT_USERDEFINED: // 13129
  3404. case VT_CARRAY:
  3405. //This is actually an LPSAFEARRAY pSafeArray.
  3406. if(vt & VT_BYREF)
  3407. {
  3408. *pOffset = BYREF_SAFEARRAY_TYPE_FS_OFFSET ;
  3409. }
  3410. else
  3411. {
  3412. *pOffset = SAFEARRAY_TYPE_FS_OFFSET ;
  3413. }
  3414. break;
  3415. case VT_INTERFACE:
  3416. offset = _offset;
  3417. hr = PushByte(FC_USER_MARSHAL);
  3418. if(FAILED(hr))
  3419. return hr;
  3420. hr = PushByte(USER_MARSHAL_UNIQUE | USER_MARSHAL_IID | 3);
  3421. if(FAILED(hr))
  3422. return hr;
  3423. hr = PushShort(2);
  3424. if(FAILED(hr))
  3425. return hr;
  3426. hr = PushShort(4);
  3427. if(FAILED(hr))
  3428. return hr;
  3429. hr = PushShort(0);
  3430. if(FAILED(hr))
  3431. return hr;
  3432. // yongqu: this offset doesn't really matter. In NdrpInitUserMarshalCB,
  3433. // pReserve is set to offset+10, pointing to the iid directly. In safearrayunmarshal,
  3434. // the iid is read from usermarshalinfo->pReserve. So this short value is never read
  3435. hr = PushOffset(SAFEARRAY_TYPE_FS_OFFSET); //LPSAFEARRAY * type offset
  3436. if(FAILED(hr))
  3437. return hr;
  3438. hr = PushIID(*piid);
  3439. if(FAILED(hr))
  3440. return hr;
  3441. *pOffset = offset;
  3442. break;
  3443. default:
  3444. hr = DISP_E_BADVARTYPE;
  3445. break;
  3446. }
  3447. return hr;
  3448. }
  3449. //+---------------------------------------------------------------------------
  3450. //
  3451. // Method: CTypeGen::GenStructFormatString
  3452. //
  3453. // Synopsis: generate type format string for a simple type member of a struct.
  3454. //
  3455. // Arguments: parainfo - parameter information
  3456. // pVarDesc - variable description.
  3457. // pOffset - Returns the type offset of the safe array.
  3458. //
  3459. //----------------------------------------------------------------------------
  3460. HRESULT CTypeGen::GenStructSimpleTypesFormatString(
  3461. IN PARAMINFO *parainfo,
  3462. IN VARDESC *pVarDesc,
  3463. OUT USHORT *TypeMemoryAlignment)
  3464. {
  3465. VARTYPE vtnoref = pVarDesc->elemdescVar.tdesc.vt & ~VT_BYREF;
  3466. HRESULT hr = S_OK;
  3467. NDR_ASSERT( vtnoref <= VT_VERSIONED_STREAM, "invalid vt");
  3468. if ( vtnoref > VT_VERSIONED_STREAM )
  3469. return DISP_E_BADVARTYPE;
  3470. BYTE FcElement = VT_FC_MAP[vtnoref];
  3471. *TypeMemoryAlignment = Alignment( VarMemInfo[vtnoref].MemoryAlignment, parainfo->cbAlignment );
  3472. PushByte( FcElement);
  3473. _uStructSize += VarMemInfo[vtnoref].MemorySize;
  3474. NDR_ASSERT( VarMemInfo[vtnoref].WireSize != 0, "invalid simple vartype");
  3475. if ( VarMemInfo[vtnoref].WireSize == 0 )
  3476. hr = DISP_E_BADVARTYPE;
  3477. return hr;
  3478. }
  3479. //+---------------------------------------------------------------------------
  3480. //
  3481. // Method: CTypeGen::AdjustTopLevelRef
  3482. //
  3483. // Synopsis: Adjust the top level multiple level pointer to
  3484. // FC_RP FC_ALLOCED_ON_STACK|FC_POINTER_DEREF
  3485. //
  3486. // Arguments: dwReq - basic alignment requirement of the member.
  3487. // dwMax - the alignment requirement of the struct.
  3488. //
  3489. //----------------------------------------------------------------------------
  3490. HRESULT CTypeGen::AdjustTopLevelRef(USHORT offset)
  3491. {
  3492. HRESULT hr;
  3493. hr = SetByte(offset,FC_RP);
  3494. if (SUCCEEDED(hr))
  3495. hr = SetByte(offset+1, FC_ALLOCED_ON_STACK|FC_POINTER_DEREF);
  3496. return hr;
  3497. }
  3498. //+---------------------------------------------------------------------------
  3499. //
  3500. // Method: CTypeGen::Alignment
  3501. //
  3502. // Synopsis: get the alignment info of a type and push FC_ALIGN when required.
  3503. //
  3504. // Arguments: dwReq - basic alignment requirement of the member.
  3505. // dwMax - the alignment requirement of the struct.
  3506. //
  3507. //----------------------------------------------------------------------------
  3508. USHORT CTypeGen::Alignment(DWORD dwReq,DWORD dwMax)
  3509. {
  3510. USHORT TypeAlignment = (USHORT)(dwReq > dwMax ? dwMax : dwReq);
  3511. if (_uStructSize & TypeAlignment)
  3512. {
  3513. LENGTH_ALIGN( _uStructSize, TypeAlignment );
  3514. switch (TypeAlignment)
  3515. {
  3516. case 1:
  3517. PushByte(FC_ALIGNM2);
  3518. break;
  3519. case 3:
  3520. PushByte(FC_ALIGNM4);
  3521. break;
  3522. case 7:
  3523. PushByte(FC_ALIGNM8);
  3524. break;
  3525. }
  3526. }
  3527. return TypeAlignment;
  3528. }
  3529. HRESULT CTypeGen::GrowTypeFormat(
  3530. USHORT cb)
  3531. {
  3532. HRESULT hr = S_OK;
  3533. //Check if we need to grow the type format string.
  3534. if((_offset + cb) >= _cbTypeFormat)
  3535. {
  3536. void *pTemp;
  3537. USHORT cbTemp;
  3538. cbTemp = _cbTypeFormat * 2;
  3539. pTemp = I_RpcAllocate(cbTemp);
  3540. if(pTemp != NULL)
  3541. {
  3542. //copy the memory
  3543. memcpy(pTemp, _pTypeFormat, _cbTypeFormat);
  3544. //free the old memory
  3545. if(_pTypeFormat != __MIDL_TypeFormatString.Format)
  3546. {
  3547. I_RpcFree((void *) _pTypeFormat);
  3548. }
  3549. _pTypeFormat = (PFORMAT_STRING) pTemp;
  3550. _cbTypeFormat = cbTemp;
  3551. }
  3552. else
  3553. {
  3554. hr = E_OUTOFMEMORY;
  3555. }
  3556. }
  3557. return hr;
  3558. }
  3559. HRESULT CTypeGen::PushByte(
  3560. IN byte b)
  3561. {
  3562. HRESULT hr;
  3563. hr = GrowTypeFormat(sizeof(b));
  3564. if(SUCCEEDED(hr))
  3565. {
  3566. *((BYTE *) &_pTypeFormat[_offset]) = b;
  3567. _offset += sizeof(b);
  3568. }
  3569. return hr;
  3570. }
  3571. HRESULT CTypeGen::PushShort(
  3572. IN USHORT s)
  3573. {
  3574. HRESULT hr;
  3575. hr = GrowTypeFormat(sizeof(s));
  3576. if(SUCCEEDED(hr))
  3577. {
  3578. *((UNALIGNED short *) &_pTypeFormat[_offset]) = s;
  3579. _offset += sizeof(s);
  3580. }
  3581. return hr;
  3582. }
  3583. HRESULT CTypeGen::PushLong(
  3584. IN ULONG s)
  3585. {
  3586. HRESULT hr;
  3587. hr = GrowTypeFormat(sizeof(s));
  3588. if(SUCCEEDED(hr))
  3589. {
  3590. *((UNALIGNED long *) &_pTypeFormat[_offset]) = s;
  3591. _offset += sizeof(s);
  3592. }
  3593. return hr;
  3594. }
  3595. HRESULT CTypeGen::PushOffset(
  3596. IN USHORT offset)
  3597. {
  3598. HRESULT hr;
  3599. hr = PushShort(offset - _offset);
  3600. return hr;
  3601. }
  3602. HRESULT CTypeGen::PushIID(
  3603. IN IID iid)
  3604. {
  3605. HRESULT hr;
  3606. hr = GrowTypeFormat(sizeof(IID));
  3607. if(SUCCEEDED(hr))
  3608. {
  3609. memcpy((void *)&_pTypeFormat[_offset], &iid, sizeof(IID));
  3610. _offset += sizeof(IID);
  3611. }
  3612. return hr;
  3613. }
  3614. HRESULT CTypeGen::SetShort(
  3615. IN USHORT offset,
  3616. IN USHORT data)
  3617. {
  3618. if (offset >= _offset)
  3619. return E_INVALIDARG;
  3620. *((UNALIGNED short *) &_pTypeFormat[offset]) = data;
  3621. return S_OK;
  3622. }
  3623. HRESULT CTypeGen::SetByte(
  3624. IN USHORT offset,
  3625. IN BYTE data)
  3626. {
  3627. if (offset >= _offset)
  3628. return E_INVALIDARG;
  3629. *((BYTE *) &_pTypeFormat[offset]) = data;
  3630. return S_OK;
  3631. }
  3632. HRESULT CTypeGen::GetShort(
  3633. IN USHORT offset,
  3634. OUT USHORT* data)
  3635. {
  3636. if (offset >= _offset)
  3637. return E_INVALIDARG;
  3638. *data = *((UNALIGNED short*)&_pTypeFormat[offset]);
  3639. return S_OK;
  3640. }
  3641. HRESULT CTypeGen::GetOffset(
  3642. IN USHORT addr,
  3643. OUT USHORT* poffset)
  3644. {
  3645. USHORT delta;
  3646. HRESULT hr;
  3647. hr = GetShort(addr,&delta);
  3648. if (FAILED(hr))
  3649. return hr;
  3650. *poffset = addr + (SHORT) delta;
  3651. // hr = GetShort(addr + (SHORT)delta,poffset);
  3652. return hr;
  3653. }
  3654. HRESULT CTypeGen::GetByte(
  3655. IN USHORT offset,
  3656. OUT BYTE* data)
  3657. {
  3658. if (offset >= _offset)
  3659. return E_INVALIDARG;
  3660. *data = *((BYTE *) &_pTypeFormat[offset]);
  3661. return S_OK;
  3662. }
  3663. ULONG __RPC_USER
  3664. BSTR_UserSize(ULONG * pFlags, ULONG Offset, BSTR * pBstr)
  3665. {
  3666. HRESULT hr;
  3667. hr = NdrLoadOleAutomationRoutines();
  3668. if(FAILED(hr))
  3669. RpcRaiseException(hr);
  3670. return (UserMarshalRoutines[0].pfnBufferSize)(pFlags, Offset, pBstr);
  3671. }
  3672. BYTE * __RPC_USER
  3673. BSTR_UserMarshal (ULONG * pFlags, BYTE * pBuffer, BSTR * pBstr)
  3674. {
  3675. HRESULT hr;
  3676. hr = NdrLoadOleAutomationRoutines();
  3677. if(FAILED(hr))
  3678. RpcRaiseException(hr);
  3679. return (UserMarshalRoutines[0].pfnMarshall)(pFlags, pBuffer, pBstr);
  3680. }
  3681. BYTE * __RPC_USER
  3682. BSTR_UserUnmarshal(ULONG * pFlags, BYTE * pBuffer, BSTR * pBstr)
  3683. {
  3684. HRESULT hr;
  3685. hr = NdrLoadOleAutomationRoutines();
  3686. if(FAILED(hr))
  3687. RpcRaiseException(hr);
  3688. return (UserMarshalRoutines[0].pfnUnmarshall)(pFlags, pBuffer, pBstr);
  3689. }
  3690. void __RPC_USER
  3691. BSTR_UserFree(ULONG * pFlags, BSTR * pBstr)
  3692. {
  3693. HRESULT hr;
  3694. hr = NdrLoadOleAutomationRoutines();
  3695. if(FAILED(hr))
  3696. RpcRaiseException(hr);
  3697. (UserMarshalRoutines[0].pfnFree)(pFlags, pBstr);
  3698. }
  3699. ULONG __RPC_USER
  3700. VARIANT_UserSize(ULONG * pFlags, ULONG Offset, VARIANT * pVariant)
  3701. {
  3702. HRESULT hr;
  3703. hr = NdrLoadOleAutomationRoutines();
  3704. if(FAILED(hr))
  3705. RpcRaiseException(hr);
  3706. return (UserMarshalRoutines[1].pfnBufferSize)(pFlags, Offset, pVariant);
  3707. }
  3708. BYTE * __RPC_USER
  3709. VARIANT_UserMarshal (ULONG * pFlags, BYTE * pBuffer, VARIANT * pVariant)
  3710. {
  3711. HRESULT hr;
  3712. hr = NdrLoadOleAutomationRoutines();
  3713. if(FAILED(hr))
  3714. RpcRaiseException(hr);
  3715. return (UserMarshalRoutines[1].pfnMarshall)(pFlags, pBuffer, pVariant);
  3716. }
  3717. BYTE * __RPC_USER
  3718. VARIANT_UserUnmarshal(ULONG * pFlags, BYTE * pBuffer, VARIANT * pVariant)
  3719. {
  3720. HRESULT hr;
  3721. hr = NdrLoadOleAutomationRoutines();
  3722. if(FAILED(hr))
  3723. RpcRaiseException(hr);
  3724. return (UserMarshalRoutines[1].pfnUnmarshall)(pFlags, pBuffer, pVariant);
  3725. }
  3726. void __RPC_USER
  3727. VARIANT_UserFree(ULONG * pFlags, VARIANT * pVariant)
  3728. {
  3729. HRESULT hr;
  3730. hr = NdrLoadOleAutomationRoutines();
  3731. if(FAILED(hr))
  3732. RpcRaiseException(hr);
  3733. (UserMarshalRoutines[1].pfnFree)(pFlags, pVariant);
  3734. }
  3735. ULONG __RPC_USER
  3736. LPSAFEARRAY_UserSize(ULONG * pFlags, ULONG Offset, LPSAFEARRAY * ppSafeArray)
  3737. {
  3738. HRESULT hr;
  3739. hr = NdrLoadOleAutomationRoutines();
  3740. if(FAILED(hr))
  3741. RpcRaiseException(hr);
  3742. return (pfnLPSAFEARRAY_UserSize)(pFlags, Offset, ppSafeArray);
  3743. }
  3744. BYTE * __RPC_USER
  3745. LPSAFEARRAY_UserMarshal (ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray)
  3746. {
  3747. HRESULT hr;
  3748. hr = NdrLoadOleAutomationRoutines();
  3749. if(FAILED(hr))
  3750. RpcRaiseException(hr);
  3751. return (pfnLPSAFEARRAY_UserMarshal)(pFlags, pBuffer, ppSafeArray);
  3752. }
  3753. BYTE * __RPC_USER
  3754. LPSAFEARRAY_UserUnmarshal(ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray)
  3755. {
  3756. HRESULT hr;
  3757. hr = NdrLoadOleAutomationRoutines();
  3758. if(FAILED(hr))
  3759. RpcRaiseException(hr);
  3760. return (pfnLPSAFEARRAY_UserUnmarshal)(pFlags, pBuffer, ppSafeArray);
  3761. }
  3762. void __RPC_USER
  3763. LPSAFEARRAY_UserFree(ULONG * pFlags, LPSAFEARRAY * ppSafeArray)
  3764. {
  3765. HRESULT hr;
  3766. hr = NdrLoadOleAutomationRoutines();
  3767. if(FAILED(hr))
  3768. RpcRaiseException(hr);
  3769. (UserMarshalRoutines[2].pfnFree)(pFlags, ppSafeArray);
  3770. }
  3771. ULONG __RPC_USER
  3772. LPSAFEARRAY_Size(ULONG * pFlags, ULONG Offset, LPSAFEARRAY * ppSafeArray, const IID *piid)
  3773. {
  3774. HINSTANCE h;
  3775. void * pfnTemp;
  3776. //Load oleaut32.dll
  3777. if(0 == hOleAut32)
  3778. {
  3779. h = LoadLibraryA("OLEAUT32");
  3780. if(h != 0)
  3781. {
  3782. hOleAut32 = h;
  3783. }
  3784. else
  3785. {
  3786. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3787. }
  3788. }
  3789. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_Size");
  3790. if(pfnTemp != 0)
  3791. {
  3792. pfnLPSAFEARRAY_Size = (PFNSAFEARRAY_SIZE) pfnTemp;
  3793. }
  3794. else
  3795. {
  3796. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3797. }
  3798. return (pfnLPSAFEARRAY_Size)(pFlags, Offset, ppSafeArray, piid);
  3799. }
  3800. BYTE * __RPC_USER
  3801. LPSAFEARRAY_Marshal (ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray, const IID *piid)
  3802. {
  3803. HINSTANCE h;
  3804. void * pfnTemp;
  3805. //Load oleaut32.dll
  3806. if(0 == hOleAut32)
  3807. {
  3808. h = LoadLibraryA("OLEAUT32");
  3809. if(h != 0)
  3810. {
  3811. hOleAut32 = h;
  3812. }
  3813. else
  3814. {
  3815. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3816. }
  3817. }
  3818. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_Marshal");
  3819. if(pfnTemp != 0)
  3820. {
  3821. pfnLPSAFEARRAY_Marshal = (PFNSAFEARRAY_MARSHAL) pfnTemp;
  3822. }
  3823. else
  3824. {
  3825. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3826. }
  3827. return (pfnLPSAFEARRAY_Marshal)(pFlags, pBuffer, ppSafeArray, piid);
  3828. }
  3829. BYTE * __RPC_USER
  3830. LPSAFEARRAY_Unmarshal(ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray, const IID *piid)
  3831. {
  3832. HINSTANCE h;
  3833. void * pfnTemp;
  3834. //Load oleaut32.dll
  3835. if(0 == hOleAut32)
  3836. {
  3837. h = LoadLibraryA("OLEAUT32");
  3838. if(h != 0)
  3839. {
  3840. hOleAut32 = h;
  3841. }
  3842. else
  3843. {
  3844. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3845. }
  3846. }
  3847. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_Unmarshal");
  3848. if(pfnTemp != 0)
  3849. {
  3850. pfnLPSAFEARRAY_Unmarshal = (PFNSAFEARRAY_UNMARSHAL) pfnTemp;
  3851. }
  3852. else
  3853. {
  3854. RpcRaiseException(HRESULT_FROM_WIN32(GetLastError()));
  3855. }
  3856. return (pfnLPSAFEARRAY_Unmarshal)(pFlags, pBuffer, ppSafeArray, piid);
  3857. }
  3858. PFNSAFEARRAY_SIZE pfnLPSAFEARRAY_Size = LPSAFEARRAY_Size;
  3859. PFNSAFEARRAY_MARSHAL pfnLPSAFEARRAY_Marshal = LPSAFEARRAY_Marshal;
  3860. PFNSAFEARRAY_UNMARSHAL pfnLPSAFEARRAY_Unmarshal = LPSAFEARRAY_Unmarshal;
  3861. ULONG __RPC_USER
  3862. SafeArraySize(ULONG * pFlags, ULONG Offset, LPSAFEARRAY * ppSafeArray)
  3863. {
  3864. USER_MARSHAL_CB *pUserMarshal = (USER_MARSHAL_CB *) pFlags;
  3865. if(pUserMarshal->pReserve != 0)
  3866. {
  3867. IID iid;
  3868. memcpy(&iid, pUserMarshal->pReserve, sizeof(IID));
  3869. return (pfnLPSAFEARRAY_Size)(pFlags, Offset, ppSafeArray, &iid);
  3870. }
  3871. else
  3872. {
  3873. return (pfnLPSAFEARRAY_UserSize)(pFlags, Offset, ppSafeArray);
  3874. }
  3875. }
  3876. BYTE * __RPC_USER
  3877. SafeArrayMarshal (ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray)
  3878. {
  3879. USER_MARSHAL_CB *pUserMarshal = (USER_MARSHAL_CB *) pFlags;
  3880. if(pUserMarshal->pReserve != 0)
  3881. {
  3882. IID iid;
  3883. memcpy(&iid, pUserMarshal->pReserve, sizeof(IID));
  3884. return (pfnLPSAFEARRAY_Marshal)(pFlags, pBuffer, ppSafeArray, &iid);
  3885. }
  3886. else
  3887. {
  3888. return (pfnLPSAFEARRAY_UserMarshal)(pFlags, pBuffer, ppSafeArray);
  3889. }
  3890. }
  3891. BYTE * __RPC_USER
  3892. SafeArrayUnmarshal(ULONG * pFlags, BYTE * pBuffer, LPSAFEARRAY * ppSafeArray)
  3893. {
  3894. USER_MARSHAL_CB *pUserMarshal = (USER_MARSHAL_CB *) pFlags;
  3895. if(pUserMarshal->pReserve != 0)
  3896. {
  3897. IID iid;
  3898. memcpy(&iid, pUserMarshal->pReserve, sizeof(IID));
  3899. return (pfnLPSAFEARRAY_Unmarshal)(pFlags, pBuffer, ppSafeArray, &iid);
  3900. }
  3901. else
  3902. {
  3903. return (pfnLPSAFEARRAY_UserUnmarshal)(pFlags, pBuffer, ppSafeArray);
  3904. }
  3905. }
  3906. USER_MARSHAL_SIZING_ROUTINE
  3907. pfnLPSAFEARRAY_UserSize = (USER_MARSHAL_SIZING_ROUTINE) LPSAFEARRAY_UserSize;
  3908. USER_MARSHAL_MARSHALLING_ROUTINE
  3909. pfnLPSAFEARRAY_UserMarshal = (USER_MARSHAL_MARSHALLING_ROUTINE) LPSAFEARRAY_UserMarshal;
  3910. USER_MARSHAL_UNMARSHALLING_ROUTINE
  3911. pfnLPSAFEARRAY_UserUnmarshal = (USER_MARSHAL_UNMARSHALLING_ROUTINE) LPSAFEARRAY_UserUnmarshal;
  3912. USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[3] =
  3913. {
  3914. {
  3915. (USER_MARSHAL_SIZING_ROUTINE) BSTR_UserSize,
  3916. (USER_MARSHAL_MARSHALLING_ROUTINE) BSTR_UserMarshal,
  3917. (USER_MARSHAL_UNMARSHALLING_ROUTINE) BSTR_UserUnmarshal,
  3918. (USER_MARSHAL_FREEING_ROUTINE) BSTR_UserFree
  3919. },
  3920. {
  3921. (USER_MARSHAL_SIZING_ROUTINE) VARIANT_UserSize,
  3922. (USER_MARSHAL_MARSHALLING_ROUTINE) VARIANT_UserMarshal,
  3923. (USER_MARSHAL_UNMARSHALLING_ROUTINE) VARIANT_UserUnmarshal,
  3924. (USER_MARSHAL_FREEING_ROUTINE) VARIANT_UserFree
  3925. },
  3926. {
  3927. (USER_MARSHAL_SIZING_ROUTINE) SafeArraySize,
  3928. (USER_MARSHAL_MARSHALLING_ROUTINE) SafeArrayMarshal,
  3929. (USER_MARSHAL_UNMARSHALLING_ROUTINE) SafeArrayUnmarshal,
  3930. (USER_MARSHAL_FREEING_ROUTINE) LPSAFEARRAY_UserFree
  3931. }
  3932. };
  3933. HRESULT NdrLoadOleAutomationRoutines()
  3934. {
  3935. void * pfnTemp;
  3936. //Load oleaut32.dll
  3937. if(hOleAut32 == 0)
  3938. {
  3939. hOleAut32 = LoadLibraryA("OLEAUT32");
  3940. if(0 == hOleAut32)
  3941. return HRESULT_FROM_WIN32(GetLastError());
  3942. }
  3943. pfnTemp = GetProcAddress(hOleAut32, "BSTR_UserSize");
  3944. if(pfnTemp != 0)
  3945. UserMarshalRoutines[0].pfnBufferSize = (USER_MARSHAL_SIZING_ROUTINE) pfnTemp;
  3946. else
  3947. return HRESULT_FROM_WIN32(GetLastError());
  3948. pfnTemp = GetProcAddress(hOleAut32, "BSTR_UserMarshal");
  3949. if(pfnTemp != 0)
  3950. UserMarshalRoutines[0].pfnMarshall = (USER_MARSHAL_MARSHALLING_ROUTINE) pfnTemp;
  3951. else
  3952. return HRESULT_FROM_WIN32(GetLastError());
  3953. pfnTemp = GetProcAddress(hOleAut32, "BSTR_UserUnmarshal");
  3954. if(pfnTemp != 0)
  3955. UserMarshalRoutines[0].pfnUnmarshall = (USER_MARSHAL_UNMARSHALLING_ROUTINE) pfnTemp;
  3956. else
  3957. return HRESULT_FROM_WIN32(GetLastError());
  3958. pfnTemp = GetProcAddress(hOleAut32, "BSTR_UserFree");
  3959. if(pfnTemp != 0)
  3960. UserMarshalRoutines[0].pfnFree = (USER_MARSHAL_FREEING_ROUTINE) pfnTemp;
  3961. else
  3962. return HRESULT_FROM_WIN32(GetLastError());
  3963. pfnTemp = GetProcAddress(hOleAut32, "VARIANT_UserSize");
  3964. if(pfnTemp != 0)
  3965. UserMarshalRoutines[1].pfnBufferSize = (USER_MARSHAL_SIZING_ROUTINE) pfnTemp;
  3966. else
  3967. return HRESULT_FROM_WIN32(GetLastError());
  3968. pfnTemp = GetProcAddress(hOleAut32, "VARIANT_UserMarshal");
  3969. if(pfnTemp != 0)
  3970. UserMarshalRoutines[1].pfnMarshall = (USER_MARSHAL_MARSHALLING_ROUTINE) pfnTemp;
  3971. else
  3972. return HRESULT_FROM_WIN32(GetLastError());
  3973. pfnTemp = GetProcAddress(hOleAut32, "VARIANT_UserUnmarshal");
  3974. if(pfnTemp != 0)
  3975. UserMarshalRoutines[1].pfnUnmarshall = (USER_MARSHAL_UNMARSHALLING_ROUTINE) pfnTemp;
  3976. else
  3977. return HRESULT_FROM_WIN32(GetLastError());
  3978. pfnTemp = GetProcAddress(hOleAut32, "VARIANT_UserFree");
  3979. if(pfnTemp != 0)
  3980. UserMarshalRoutines[1].pfnFree = (USER_MARSHAL_FREEING_ROUTINE) pfnTemp;
  3981. else
  3982. return HRESULT_FROM_WIN32(GetLastError());
  3983. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_UserSize");
  3984. if(pfnTemp != 0)
  3985. pfnLPSAFEARRAY_UserSize = (USER_MARSHAL_SIZING_ROUTINE) pfnTemp;
  3986. else
  3987. return HRESULT_FROM_WIN32(GetLastError());
  3988. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_UserMarshal");
  3989. if(pfnTemp != 0)
  3990. pfnLPSAFEARRAY_UserMarshal = (USER_MARSHAL_MARSHALLING_ROUTINE) pfnTemp;
  3991. else
  3992. return HRESULT_FROM_WIN32(GetLastError());
  3993. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_UserUnmarshal");
  3994. if(pfnTemp != 0)
  3995. pfnLPSAFEARRAY_UserUnmarshal = (USER_MARSHAL_UNMARSHALLING_ROUTINE) pfnTemp;
  3996. else
  3997. return HRESULT_FROM_WIN32(GetLastError());
  3998. pfnTemp = GetProcAddress(hOleAut32, "LPSAFEARRAY_UserFree");
  3999. if(pfnTemp != 0)
  4000. UserMarshalRoutines[2].pfnFree = (USER_MARSHAL_FREEING_ROUTINE) pfnTemp;
  4001. else
  4002. return HRESULT_FROM_WIN32(GetLastError());
  4003. return S_OK;
  4004. }