Source code of Windows XP (NT5)
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.

1863 lines
46 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1993-2000 Microsoft Corporation
  3. Module Name :
  4. free.c
  5. Abstract :
  6. This file contains the routines called by MIDL 2.0 stubs and the
  7. interpreter for freeing unmarshalled data on the server.
  8. Author :
  9. David Kays dkays September 1993.
  10. Revision History :
  11. ---------------------------------------------------------------------*/
  12. #include "ndrp.h"
  13. #include "ndrole.h"
  14. #include "attack.h"
  15. #include "pointerq.h"
  16. RPCRTAPI
  17. void
  18. RPC_ENTRY
  19. NdrpNoopFree(
  20. PMIDL_STUB_MESSAGE pStubMsg,
  21. unsigned char * pMemory,
  22. PFORMAT_STRING pFormat
  23. );
  24. //
  25. // Free routine table.
  26. //
  27. extern const
  28. PFREE_ROUTINE FreeRoutinesTable[] =
  29. {
  30. NdrpNoopFree, // 0x00
  31. NdrpNoopFree, // 0x01
  32. NdrpNoopFree, // 0x02
  33. NdrpNoopFree, // 0x03
  34. NdrpNoopFree, // 0x04
  35. NdrpNoopFree, // 0x05
  36. NdrpNoopFree, // 0x06
  37. NdrpNoopFree, // 0x07
  38. NdrpNoopFree, // 0x08
  39. NdrpNoopFree, // 0x09
  40. NdrpNoopFree, // 0x0A
  41. NdrpNoopFree, // 0x0B
  42. NdrpNoopFree, // 0x0C
  43. NdrpNoopFree, // 0x0D
  44. NdrpNoopFree, // 0x0E
  45. NdrpNoopFree, // 0x0F
  46. NdrpNoopFree, // 0x10
  47. NdrPointerFree, // 0x11
  48. NdrPointerFree, // 0x12
  49. NdrPointerFree, // 0x13
  50. NdrPointerFree, // 0x14
  51. NdrpNoopFree, // 0x15 Simple struct
  52. NdrSimpleStructFree, // 0x16
  53. NdrpNoopFree, // 0x17 Conformant struct
  54. NdrConformantStructFree, // 0x18
  55. NdrConformantVaryingStructFree, // 0x19
  56. NdrComplexStructFree, // 0x1A
  57. NdrConformantArrayFree, // 0x1B
  58. NdrConformantVaryingArrayFree, // 0x1C
  59. NdrFixedArrayFree, // 0x1D
  60. NdrFixedArrayFree, // 0x1E
  61. NdrVaryingArrayFree, // 0x1F Small varying array
  62. NdrVaryingArrayFree, // 0x20 Large varying array
  63. NdrComplexArrayFree, // 0x21
  64. NdrpNoopFree, // 0x22 Conformant string
  65. NdrpNoopFree, // 0x23 Conformant string
  66. NdrpNoopFree, // 0x24 Conformant string
  67. NdrpNoopFree, // 0x25 Conformant string
  68. NdrpNoopFree, // 0x26 NonConformant string
  69. NdrpNoopFree, // 0x27 NonConformant string
  70. NdrpNoopFree, // 0x28 NonConformant string
  71. NdrpNoopFree, // 0x29 NonConformant string
  72. NdrEncapsulatedUnionFree, // 0x2A
  73. NdrNonEncapsulatedUnionFree, // 0x2B
  74. NdrByteCountPointerFree, // 0x2C
  75. NdrXmitOrRepAsFree, // 0x2D transmit as
  76. NdrXmitOrRepAsFree, // 0x2E represent as
  77. NdrPointerFree, // 0x2F
  78. NdrpNoopFree, // 0x30 Context handle
  79. // New Post NT 3.5 token serviced from here on.
  80. NdrpNoopFree, // 0x31 NdrHardStructFree,
  81. NdrXmitOrRepAsFree, // 0x32 transmit as ptr
  82. NdrXmitOrRepAsFree, // 0x33 represent as ptr
  83. NdrUserMarshalFree, // 0x34
  84. NdrpNoopFree, // 0x35 FC_PIPE
  85. NdrpNoopFree, // 0x36 FC_BLK_HOLE
  86. NdrpRangeFree, // 0x37
  87. NdrpNoopFree, // 0x38
  88. NdrpNoopFree, // 0x39
  89. NdrpNoopFree, // 0x3A
  90. NdrpNoopFree, // 0x3B
  91. NdrpNoopFree, // 0x3C
  92. NdrpNoopFree, // 0x3D
  93. NdrpNoopFree, // 0x3E
  94. NdrpNoopFree, // 0x3F
  95. };
  96. extern const
  97. PFREE_ROUTINE * pfnFreeRoutines = FreeRoutinesTable;
  98. RPCRTAPI
  99. void RPC_ENTRY
  100. NdrTypeFree(PMIDL_STUB_MESSAGE pStubMsg,
  101. unsigned char __RPC_FAR * pMemory,
  102. PFORMAT_STRING pFormat )
  103. {
  104. if (pfnFreeRoutines[ROUTINE_INDEX(*pFormat)])
  105. {
  106. (*pfnFreeRoutines[ROUTINE_INDEX(*pFormat)])( pStubMsg,
  107. pMemory,
  108. pFormat );
  109. }
  110. }
  111. void RPC_ENTRY
  112. NdrpNoopFree(
  113. PMIDL_STUB_MESSAGE pStubMsg,
  114. uchar * pMemory,
  115. PFORMAT_STRING pFormat )
  116. /*++
  117. Routine Description :
  118. Frees a top level or embedded simple type.
  119. Used for VT_USERDEFINED but in fact simple types,
  120. like TKIND_ENUM and TKIND_ALIAS
  121. Arguments :
  122. pStubMsg - Pointer to the stub message.
  123. pMemory - Pointer to be freed.
  124. pFormat - Pointer's format string description.
  125. Return :
  126. None.
  127. --*/
  128. {
  129. return;
  130. }
  131. inline void
  132. NdrFreeTypeMemory(
  133. PMIDL_STUB_MESSAGE pStubMsg,
  134. uchar * pMemory )
  135. {
  136. if ( !pStubMsg->pPointerQueueState ||
  137. !pStubMsg->pPointerQueueState->GetActiveQueue() )
  138. {
  139. (*pStubMsg->pfnFree)(pMemory);
  140. }
  141. else
  142. {
  143. NDR_PFNFREE_POINTER_QUEUE_ELEMENT*pElement =
  144. new(pStubMsg->pPointerQueueState)
  145. NDR_PFNFREE_POINTER_QUEUE_ELEMENT(pStubMsg->pfnFree,
  146. pMemory );
  147. pStubMsg->pPointerQueueState->GetActiveQueue()->Enque( pElement );
  148. }
  149. }
  150. __forceinline void
  151. NdrPointerFreeInternal(
  152. PMIDL_STUB_MESSAGE pStubMsg,
  153. uchar * pMemory,
  154. PFORMAT_STRING pFormat )
  155. /*++
  156. Routine Description :
  157. Frees a top level or embedded pointer to anything.
  158. Used for FC_RP, FC_UP, FC_FP, FC_OP.
  159. Arguments :
  160. pStubMsg - Pointer to the stub message.
  161. pMemory - Pointer to be freed.
  162. pFormat - Pointer's format string description.
  163. Return :
  164. None.
  165. --*/
  166. {
  167. PFORMAT_STRING pFormatPointee;
  168. uchar * pMemoryPointee;
  169. pMemoryPointee = pMemory;
  170. if ( ! pMemory )
  171. return;
  172. switch( *pFormat )
  173. {
  174. case FC_FP:
  175. //
  176. // Check if we've already freed this full pointer.
  177. //
  178. if ( ! NdrFullPointerFree( pStubMsg->FullPtrXlatTables,
  179. pMemory ) )
  180. return;
  181. break;
  182. case FC_IP:
  183. if(pMemory != 0)
  184. {
  185. ((IUnknown *)pMemory)->Release();
  186. pMemory = 0;
  187. }
  188. return;
  189. default:
  190. break;
  191. }
  192. if ( pFormat[1] == 0 )
  193. goto FreeEmbeddedPointers;
  194. //
  195. // Check if this pointer and any possible embedded pointers should not
  196. // be freed.
  197. //
  198. if ( DONT_FREE(pFormat[1]) )
  199. return;
  200. //
  201. // Just go free a pointer to a simple type.
  202. //
  203. if ( SIMPLE_POINTER(pFormat[1]) )
  204. goto FreeTopPointer;
  205. //
  206. // Check if this is an allocate all nodes pointer.
  207. // IDL symantics say that we only free the top most allocate all nodes
  208. // pointer on the server even in the [out] only case. So jump to the
  209. // check for the pointer free at the end of the routine.
  210. //
  211. if ( ALLOCATE_ALL_NODES(pFormat[1]) )
  212. goto FreeTopPointer;
  213. if ( POINTER_DEREF(pFormat[1]) )
  214. pMemoryPointee = *((uchar **)pMemory);
  215. FreeEmbeddedPointers:
  216. pFormatPointee = pFormat + 2;
  217. pFormatPointee += *((signed short *)pFormatPointee);
  218. //
  219. // Call the correct free routine if one exists for this type.
  220. //
  221. if ( pfnFreeRoutines[ROUTINE_INDEX(*pFormatPointee)] )
  222. {
  223. uchar uFlagsSave = pStubMsg->uFlags;
  224. RESET_CONF_FLAGS_TO_STANDALONE(pStubMsg->uFlags);
  225. (*pfnFreeRoutines[ROUTINE_INDEX(*pFormatPointee)])
  226. ( pStubMsg,
  227. pMemoryPointee,
  228. pFormatPointee );
  229. pStubMsg->uFlags = uFlagsSave;
  230. }
  231. FreeTopPointer:
  232. //
  233. // Now free the pointer. Pointer guaranteed to be non-null here.
  234. //
  235. // We only free the pointer if it lies outside of the message buffer
  236. // that the server stub received from the RPC runtime. Otherwise we
  237. // used the RPC buffer to hold the pointer's data and should not free it.
  238. //
  239. if ( (pMemory < pStubMsg->BufferStart) || (pMemory > pStubMsg->BufferEnd) )
  240. {
  241. //
  242. // Also check to make sure that the pointer was not allocated on the
  243. // server stub's stack (this may happen for ref pointers).
  244. //
  245. // full pointer can't be allocated on stack (it's possible that
  246. // an embedded FP points to the stack slot and got free first, and
  247. // it will try to free the stack memory ). But currently MIDL still
  248. // generate allocated_on_stack flag for FC_FP. The workaround is
  249. // that in unmarshall, we always allocate new, and in free, we should
  250. // free despite the flag.
  251. if ( ! ALLOCED_ON_STACK(pFormat[1]) || ( *pFormat == FC_FP ))
  252. {
  253. NdrFreeTypeMemory(
  254. pStubMsg,
  255. pMemory );
  256. }
  257. }
  258. }
  259. NDR_FREE_POINTER_QUEUE_ELEMENT::NDR_FREE_POINTER_QUEUE_ELEMENT(
  260. MIDL_STUB_MESSAGE *pStubMsg,
  261. uchar * const pMemoryNew,
  262. const PFORMAT_STRING pFormatNew) :
  263. pMemory(pMemoryNew),
  264. pFormat(pFormatNew),
  265. Memory(pStubMsg->Memory),
  266. uFlags(pStubMsg->uFlags)
  267. {
  268. }
  269. void
  270. NDR_FREE_POINTER_QUEUE_ELEMENT::Dispatch(
  271. MIDL_STUB_MESSAGE *pStubMsg)
  272. {
  273. SAVE_CONTEXT<uchar*> MemorySave( pStubMsg->Memory, Memory );
  274. SAVE_CONTEXT<uchar> uFlagsSave( pStubMsg->uFlags, uFlags );
  275. NdrPointerFreeInternal( pStubMsg,
  276. pMemory,
  277. pFormat );
  278. }
  279. #if defined(DBG)
  280. void
  281. NDR_FREE_POINTER_QUEUE_ELEMENT::Print()
  282. {
  283. DbgPrint("NDR_FREE_POINTER_QUEUE_ELEMENT\n");
  284. DbgPrint("pNext: %p\n", pNext );
  285. DbgPrint("pMemory: %p\n", pMemory );
  286. DbgPrint("pFormat: %p\n", pFormat );
  287. DbgPrint("Memory: %p\n", Memory );
  288. DbgPrint("uFlags: %x\n", uFlags );
  289. }
  290. #endif
  291. void
  292. NdrpEnquePointerFree(
  293. PMIDL_STUB_MESSAGE pStubMsg,
  294. uchar * pMemory,
  295. PFORMAT_STRING pFormat )
  296. {
  297. NDR32_POINTER_CONTEXT PointerContext( pStubMsg );
  298. RpcTryFinally
  299. {
  300. NDR_FREE_POINTER_QUEUE_ELEMENT*pElement =
  301. new(PointerContext.GetActiveState())
  302. NDR_FREE_POINTER_QUEUE_ELEMENT(pStubMsg,
  303. pMemory,
  304. pFormat);
  305. PointerContext.Enque( pElement );
  306. PointerContext.DispatchIfRequired();
  307. }
  308. RpcFinally
  309. {
  310. PointerContext.EndContext();
  311. }
  312. RpcEndFinally
  313. }
  314. void RPC_ENTRY
  315. NdrPointerFree(
  316. PMIDL_STUB_MESSAGE pStubMsg,
  317. uchar * pMemory,
  318. PFORMAT_STRING pFormat )
  319. {
  320. if ( !NdrIsLowStack(pStubMsg) )
  321. {
  322. NdrPointerFreeInternal(
  323. pStubMsg,
  324. pMemory,
  325. pFormat );
  326. return;
  327. }
  328. NdrpEnquePointerFree(
  329. pStubMsg,
  330. pMemory,
  331. pFormat );
  332. }
  333. void RPC_ENTRY
  334. NdrpRangeFree(
  335. PMIDL_STUB_MESSAGE pStubMsg,
  336. uchar * pMemory,
  337. PFORMAT_STRING pFormat )
  338. /*++
  339. --*/
  340. {
  341. return;
  342. }
  343. void RPC_ENTRY
  344. NdrSimpleStructFree(
  345. PMIDL_STUB_MESSAGE pStubMsg,
  346. uchar * pMemory,
  347. PFORMAT_STRING pFormat )
  348. /*++
  349. Routine Description :
  350. Frees a simple structure's embedded pointers which were allocated during
  351. a remote call.
  352. Used for FC_STRUCT and FC_PSTRUCT.
  353. Arguments :
  354. pStubMsg - Pointer to the stub message.
  355. pMemory - Pointer to be freed.
  356. pFormat - Pointer's format string description.
  357. Return :
  358. None.
  359. --*/
  360. {
  361. if ( *pFormat == FC_PSTRUCT )
  362. {
  363. NdrpEmbeddedPointerFree( pStubMsg,
  364. pMemory,
  365. pFormat + 4 );
  366. }
  367. }
  368. void RPC_ENTRY
  369. NdrConformantStructFree(
  370. PMIDL_STUB_MESSAGE pStubMsg,
  371. uchar * pMemory,
  372. PFORMAT_STRING pFormat )
  373. /*++
  374. Routine Description :
  375. Frees a conformant structure's embedded pointers which were allocated
  376. during a remote call.
  377. Used for FC_CSTRUCT and FC_CPSTRUCT.
  378. Arguments :
  379. pStubMsg - Pointer to the stub message.
  380. pMemory - Pointer to be freed.
  381. pFormat - Pointer's format string description.
  382. Return :
  383. None.
  384. --*/
  385. {
  386. PFORMAT_STRING pFormatArray;
  387. if ( *pFormat == FC_CSTRUCT )
  388. return;
  389. // Get a pointer to the conformant array's description.
  390. pFormatArray = pFormat + 4;
  391. pFormatArray += *((signed short *)pFormatArray);
  392. //
  393. // Get the conformance count. Pass a memory pointer to the beginning
  394. // of the array.
  395. //
  396. NdrpComputeConformance( pStubMsg,
  397. pMemory + *((ushort *)(pFormat + 2)),
  398. pFormatArray );
  399. // Must pass a format string pointing to the pointer layout.
  400. NdrpEmbeddedPointerFree( pStubMsg,
  401. pMemory,
  402. pFormat + 6 );
  403. // Above, we walk the array pointers as well.
  404. if ( IS_EMBED_CONF_STRUCT( pStubMsg->uFlags ) )
  405. SET_CONF_ARRAY_DONE( pStubMsg->uFlags );
  406. }
  407. void RPC_ENTRY
  408. NdrConformantVaryingStructFree(
  409. PMIDL_STUB_MESSAGE pStubMsg,
  410. uchar * pMemory,
  411. PFORMAT_STRING pFormat )
  412. /*++
  413. Routine Description :
  414. Frees a conformant varying structure's embedded pointers which were
  415. allocated during a remote call.
  416. Used for FC_CVSTRUCT.
  417. Arguments :
  418. pStubMsg - Pointer to the stub message.
  419. pMemory - Pointer to be freed.
  420. pFormat - Pointer's format string description.
  421. Return :
  422. None.
  423. --*/
  424. {
  425. PFORMAT_STRING pFormatArray;
  426. if ( *(pFormat + 6) != FC_PP )
  427. return;
  428. //
  429. // Set the memory pointer to the start of the conformant array/string.
  430. //
  431. // Get the conformant array/string description.
  432. pFormatArray = pFormat + 4;
  433. pFormatArray += *((signed short *)pFormatArray);
  434. NdrpComputeConformance( pStubMsg,
  435. pMemory + *((ushort *)(pFormat + 2)),
  436. pFormatArray );
  437. NdrpComputeVariance( pStubMsg,
  438. pMemory + *((ushort *)(pFormat + 2)),
  439. pFormatArray );
  440. pStubMsg->MaxCount = pStubMsg->ActualCount;
  441. // Must pass a format string pointing to the pointer layout.
  442. NdrpEmbeddedPointerFree( pStubMsg,
  443. pMemory,
  444. pFormat + 6 );
  445. // Above, we walk the array pointers as well.
  446. if ( IS_EMBED_CONF_STRUCT( pStubMsg->uFlags ) )
  447. SET_CONF_ARRAY_DONE( pStubMsg->uFlags );
  448. }
  449. #if 0
  450. void RPC_ENTRY
  451. NdrHardStructFree(
  452. PMIDL_STUB_MESSAGE pStubMsg,
  453. uchar * pMemory,
  454. PFORMAT_STRING pFormat )
  455. /*++
  456. Routine Description :
  457. Frees a hard structure's embedded pointers which were allocated during
  458. a remote call.
  459. Used for FC_HARD_STRUCT.
  460. Arguments :
  461. pStubMsg - Pointer to the stub message.
  462. pMemory - Pointer to be freed.
  463. pFormat - Pointer's format string description.
  464. Return :
  465. None.
  466. --*/
  467. {
  468. if ( *((short *)&pFormat[14]) )
  469. {
  470. pFormat += 12;
  471. pMemory += *((ushort *)pFormat)++;
  472. pFormat += *((short *)pFormat);
  473. (*pfnFreeRoutines[ROUTINE_INDEX(*pFormat)])( pStubMsg,
  474. pMemory,
  475. pFormat );
  476. }
  477. }
  478. #endif
  479. void RPC_ENTRY
  480. NdrComplexStructFree(
  481. PMIDL_STUB_MESSAGE pStubMsg,
  482. uchar * pMemory,
  483. PFORMAT_STRING pFormat )
  484. /*++
  485. Routine Description :
  486. Frees a complex structure's embedded pointers which were allocated during
  487. a remote call.
  488. Used for FC_BOGUS_STRUCT.
  489. Arguments :
  490. pStubMsg - Pointer to the stub message.
  491. pMemory - Pointer to be freed.
  492. pFormat - Pointer's format string description.
  493. Return :
  494. None.
  495. --*/
  496. {
  497. uchar * pMemorySave;
  498. PFORMAT_STRING pFormatPointers;
  499. PFORMAT_STRING pFormatArray;
  500. PFORMAT_STRING pFormatComplex;
  501. long Align8Mod;
  502. uchar fIsEmbeddedStruct = IS_EMBED_CONF_STRUCT( pStubMsg->uFlags );
  503. //
  504. // This is used for support of structs with doubles passed on an
  505. // i386 stack.
  506. //
  507. // A cast to long is what we need.
  508. Align8Mod = 0x7 & PtrToLong( pMemory );
  509. pMemorySave = pStubMsg->Memory;
  510. pStubMsg->Memory = pMemory;
  511. pFormat += 4;
  512. // Get conformant array description.
  513. if ( *((ushort *)pFormat) )
  514. pFormatArray = pFormat + *((signed short *)pFormat);
  515. else
  516. pFormatArray = 0;
  517. pFormat += 2;
  518. // Get pointer layout description.
  519. if ( *((ushort *)pFormat) )
  520. pFormatPointers = pFormat + *((ushort *)pFormat);
  521. else
  522. pFormatPointers = 0;
  523. pFormat += 2;
  524. SET_EMBED_CONF_STRUCT( pStubMsg->uFlags );
  525. //
  526. // Free the structure member by member.
  527. //
  528. for ( ; ; pFormat++ )
  529. {
  530. switch ( *pFormat )
  531. {
  532. //
  533. // simple types
  534. //
  535. case FC_CHAR :
  536. case FC_BYTE :
  537. case FC_SMALL :
  538. case FC_WCHAR :
  539. case FC_SHORT :
  540. case FC_LONG :
  541. #if defined(__RPC_WIN64__)
  542. case FC_INT3264 :
  543. case FC_UINT3264 :
  544. #endif
  545. case FC_FLOAT :
  546. case FC_HYPER :
  547. case FC_DOUBLE :
  548. case FC_ENUM16 :
  549. case FC_ENUM32 :
  550. case FC_IGNORE :
  551. pMemory += SIMPLE_TYPE_MEMSIZE(*pFormat);
  552. break;
  553. case FC_POINTER :
  554. NdrPointerFree( pStubMsg,
  555. *((uchar **)pMemory),
  556. pFormatPointers );
  557. pMemory += PTR_MEM_SIZE;
  558. pFormatPointers += 4;
  559. break;
  560. //
  561. // Embedded complex types.
  562. //
  563. case FC_EMBEDDED_COMPLEX :
  564. // Add padding.
  565. pMemory += pFormat[1];
  566. pFormat += 2;
  567. // Get the type's description.
  568. pFormatComplex = pFormat + *((signed short UNALIGNED *)pFormat);
  569. if ( pfnFreeRoutines[ROUTINE_INDEX(*pFormatComplex)] )
  570. {
  571. (*pfnFreeRoutines[ROUTINE_INDEX(*pFormatComplex)])
  572. ( pStubMsg,
  573. (*pFormatComplex == FC_IP) ? *(uchar **)pMemory : pMemory,
  574. pFormatComplex );
  575. }
  576. pMemory = NdrpMemoryIncrement( pStubMsg,
  577. pMemory,
  578. pFormatComplex );
  579. //
  580. // Increment the main format string one byte. The loop
  581. // will increment it one more byte past the offset field.
  582. //
  583. pFormat++;
  584. break;
  585. case FC_ALIGNM2 :
  586. ALIGN( pMemory, 0x1 );
  587. break;
  588. case FC_ALIGNM4 :
  589. ALIGN( pMemory, 0x3 );
  590. break;
  591. case FC_ALIGNM8 :
  592. //
  593. // We have to play some tricks for the i386 to handle the case
  594. // when an 8 byte aligned structure is passed by value. The
  595. // alignment of the struct on the stack is not guaranteed to be
  596. // on an 8 byte boundary.
  597. //
  598. pMemory -= Align8Mod;
  599. ALIGN( pMemory, 0x7 );
  600. pMemory += Align8Mod;
  601. break;
  602. case FC_STRUCTPAD1 :
  603. case FC_STRUCTPAD2 :
  604. case FC_STRUCTPAD3 :
  605. case FC_STRUCTPAD4 :
  606. case FC_STRUCTPAD5 :
  607. case FC_STRUCTPAD6 :
  608. case FC_STRUCTPAD7 :
  609. pMemory += (*pFormat - FC_STRUCTPAD1) + 1;
  610. break;
  611. case FC_STRUCTPADN :
  612. // FC_STRUCTPADN 0 <unsigned short>
  613. pMemory += *(((unsigned short *)pFormat) + 1);
  614. pFormat += 3;
  615. break;
  616. case FC_PAD :
  617. break;
  618. //
  619. // Done with layout.
  620. //
  621. case FC_END :
  622. goto ComplexFreeEnd;
  623. default :
  624. NDR_ASSERT(0,"NdrComplexStructFree : bad format char");
  625. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  626. return;
  627. } // switch
  628. } // for
  629. ComplexFreeEnd :
  630. // Walk the array at the top level only.
  631. if ( pFormatArray && !fIsEmbeddedStruct &&
  632. ! IS_CONF_ARRAY_DONE( pStubMsg->uFlags ) )
  633. {
  634. PFREE_ROUTINE pfnFree;
  635. switch ( *pFormatArray )
  636. {
  637. case FC_CARRAY :
  638. pfnFree = NdrConformantArrayFree;
  639. break;
  640. case FC_CVARRAY :
  641. pfnFree = NdrConformantVaryingArrayFree;
  642. break;
  643. case FC_BOGUS_ARRAY :
  644. pfnFree = NdrComplexArrayFree;
  645. break;
  646. // case FC_C_CSTRING :
  647. // case FC_C_BSTRING :
  648. // case FC_C_WSTRING :
  649. // case FC_C_SSTRING :
  650. default :
  651. pfnFree = 0;
  652. break;
  653. }
  654. if ( pfnFree )
  655. {
  656. (*pfnFree)( pStubMsg,
  657. pMemory,
  658. pFormatArray );
  659. }
  660. }
  661. if ( fIsEmbeddedStruct )
  662. SET_EMBED_CONF_STRUCT( pStubMsg->uFlags );
  663. else
  664. RESET_CONF_ARRAY_DONE( pStubMsg->uFlags );
  665. pStubMsg->Memory = pMemorySave;
  666. }
  667. void RPC_ENTRY
  668. NdrFixedArrayFree(
  669. PMIDL_STUB_MESSAGE pStubMsg,
  670. uchar * pMemory,
  671. PFORMAT_STRING pFormat )
  672. /*++
  673. Routine Description :
  674. Frees a fixed array's embedded pointers which were allocated during
  675. a remote call.
  676. Used for FC_SMFARRAY and FC_LGFARRAY.
  677. Arguments :
  678. pStubMsg - Pointer to the stub message.
  679. pMemory - Pointer to be freed.
  680. pFormat - Pointer's format string description.
  681. Return :
  682. None.
  683. --*/
  684. {
  685. //
  686. // We have to check this in case we get an exception before actually
  687. // unmarshalling the array.
  688. //
  689. if ( ! pMemory )
  690. return;
  691. if ( *pFormat == FC_SMFARRAY )
  692. pFormat += 4;
  693. else // *pFormat == FC_LGFARRAY
  694. pFormat += 6;
  695. if ( *pFormat == FC_PP )
  696. NdrpEmbeddedPointerFree( pStubMsg,
  697. pMemory,
  698. pFormat );
  699. }
  700. void RPC_ENTRY
  701. NdrConformantArrayFree(
  702. PMIDL_STUB_MESSAGE pStubMsg,
  703. uchar * pMemory,
  704. PFORMAT_STRING pFormat )
  705. /*++
  706. Routine Description :
  707. Frees a one dimensional conformant array's embedded pointers which were
  708. allocated during a remote call. Called for both top level and embedded
  709. conformant arrays.
  710. Used for FC_CARRAY.
  711. Arguments :
  712. pStubMsg - Pointer to the stub message.
  713. pMemory - Pointer to be freed.
  714. pFormat - Pointer's format string description.
  715. Return :
  716. None.
  717. --*/
  718. {
  719. PFORMAT_STRING pFormatPP;
  720. //
  721. // We have to check this in case we get an exception before actually
  722. // unmarshalling the array.
  723. //
  724. if ( ! pMemory )
  725. return;
  726. pFormatPP = pFormat + 8;
  727. CORRELATION_DESC_INCREMENT( pFormatPP );
  728. if ( *pFormatPP == FC_PP )
  729. {
  730. NdrpComputeConformance( pStubMsg,
  731. pMemory,
  732. pFormat );
  733. NdrpEmbeddedPointerFree( pStubMsg,
  734. pMemory,
  735. pFormatPP );
  736. }
  737. }
  738. void RPC_ENTRY
  739. NdrConformantVaryingArrayFree(
  740. PMIDL_STUB_MESSAGE pStubMsg,
  741. uchar * pMemory,
  742. PFORMAT_STRING pFormat )
  743. /*++
  744. Routine Description :
  745. Frees a one dimensional conformant varying array's embedded pointers which
  746. were allocated during a remote call. Called for both top level and
  747. embedded conformant varying arrays.
  748. Used for FC_CVARRAY.
  749. Arguments :
  750. pStubMsg - Pointer to the stub message.
  751. pMemory - Pointer to be freed.
  752. pFormat - Pointer's format string description.
  753. Return :
  754. None.
  755. --*/
  756. {
  757. PFORMAT_STRING pFormatPP;
  758. //
  759. // We have to check this in case we get an exception before actually
  760. // unmarshalling the array.
  761. //
  762. if ( ! pMemory )
  763. return;
  764. pFormatPP = pFormat + 12;
  765. CORRELATION_DESC_INCREMENT( pFormatPP );
  766. CORRELATION_DESC_INCREMENT( pFormatPP );
  767. if ( *(pFormatPP) == FC_PP )
  768. {
  769. NdrpComputeConformance( pStubMsg,
  770. pMemory,
  771. pFormat );
  772. NdrpComputeVariance( pStubMsg,
  773. pMemory,
  774. pFormat );
  775. //
  776. // Set MaxCount equal to the number of shipped elements.
  777. //
  778. pStubMsg->MaxCount = pStubMsg->ActualCount;
  779. NdrpEmbeddedPointerFree( pStubMsg,
  780. pMemory,
  781. pFormatPP );
  782. }
  783. }
  784. void RPC_ENTRY
  785. NdrVaryingArrayFree(
  786. PMIDL_STUB_MESSAGE pStubMsg,
  787. uchar * pMemory,
  788. PFORMAT_STRING pFormat )
  789. /*++
  790. Routine Description :
  791. Frees a varying array's embedded pointers which were allocated
  792. during a remote call. Called for both top level and embedded varying
  793. arrays.
  794. Used for FC_SMVARRAY and FC_LGVARRAY.
  795. Arguments :
  796. pStubMsg - Pointer to the stub message.
  797. pMemory - Pointer to be freed.
  798. pFormat - Pointer's format string description.
  799. Return :
  800. None.
  801. --*/
  802. {
  803. PFORMAT_STRING pFormatPointers;
  804. //
  805. // We have to check this in case we get an exception before actually
  806. // unmarshalling the array.
  807. //
  808. if ( ! pMemory )
  809. return;
  810. if ( *pFormat == FC_SMVARRAY )
  811. pFormatPointers = pFormat + 12;
  812. else // *pFormat == FC_LGVARRAY
  813. pFormatPointers = pFormat + 16;
  814. CORRELATION_DESC_INCREMENT( pFormatPointers );
  815. if ( *pFormatPointers == FC_PP )
  816. {
  817. NdrpComputeVariance( pStubMsg,
  818. pMemory,
  819. pFormat );
  820. //
  821. // Set MaxCount equal to the number of shipped elements.
  822. //
  823. pStubMsg->MaxCount = pStubMsg->ActualCount;
  824. NdrpEmbeddedPointerFree( pStubMsg,
  825. pMemory,
  826. pFormatPointers );
  827. }
  828. }
  829. void RPC_ENTRY
  830. NdrComplexArrayFree(
  831. PMIDL_STUB_MESSAGE pStubMsg,
  832. uchar * pMemory,
  833. PFORMAT_STRING pFormat )
  834. /*++
  835. Routine Description :
  836. Frees a complex array's embedded pointers which were allocated
  837. during a remote call. Called for both top level and embedded complex
  838. arrays.
  839. Used for FC_BOGUS_STRUCT.
  840. Arguments :
  841. pStubMsg - Pointer to the stub message.
  842. pMemory - Pointer to be freed.
  843. pFormat - Pointer's format string description.
  844. Return :
  845. None.
  846. --*/
  847. {
  848. ARRAY_INFO ArrayInfo;
  849. PARRAY_INFO pArrayInfo;
  850. PFREE_ROUTINE pfnFree;
  851. PFORMAT_STRING pFormatStart;
  852. uchar * pMemorySave;
  853. ulong Elements;
  854. ulong Offset, Count;
  855. ulong MemoryElementSize;
  856. long Dimension;
  857. //
  858. // We have to check this in case we get an exception before actually
  859. // unmarshalling the array.
  860. //
  861. if ( ! pMemory )
  862. return;
  863. //
  864. // Lots of setup if we are the outer dimension.
  865. //
  866. if ( ! pStubMsg->pArrayInfo )
  867. {
  868. pStubMsg->pArrayInfo = &ArrayInfo;
  869. ArrayInfo.Dimension = 0;
  870. ArrayInfo.BufferConformanceMark = (unsigned long *)pStubMsg->BufferMark;
  871. ArrayInfo.BufferVarianceMark = 0;
  872. ArrayInfo.MaxCountArray = (unsigned long *) pStubMsg->MaxCount;
  873. ArrayInfo.OffsetArray = (ulong *) UlongToPtr( pStubMsg->Offset );
  874. ArrayInfo.ActualCountArray = (ulong *) UlongToPtr( pStubMsg->ActualCount );
  875. }
  876. pArrayInfo = pStubMsg->pArrayInfo;
  877. Dimension = pArrayInfo->Dimension;
  878. pFormatStart = pFormat;
  879. pFormat += 2;
  880. // Get number of elements (0 if conformance present).
  881. Elements = *((ushort *&)pFormat)++;
  882. //
  883. // Check for conformance description.
  884. //
  885. if ( *((long UNALIGNED *&)pFormat) != 0xffffffff )
  886. {
  887. Elements = (ulong) NdrpComputeConformance( pStubMsg,
  888. pMemory,
  889. pFormatStart );
  890. }
  891. pFormat += 4;
  892. CORRELATION_DESC_INCREMENT( pFormat );
  893. //
  894. // Check for variance description.
  895. //
  896. if ( *((long UNALIGNED *)pFormat) != 0xffffffff )
  897. {
  898. NdrpComputeVariance( pStubMsg,
  899. pMemory,
  900. pFormatStart );
  901. Offset = pStubMsg->Offset;
  902. Count = pStubMsg->ActualCount;
  903. }
  904. else
  905. {
  906. Offset = 0;
  907. Count = Elements;
  908. }
  909. pFormat += 4;
  910. CORRELATION_DESC_INCREMENT( pFormat );
  911. switch ( *pFormat )
  912. {
  913. case FC_EMBEDDED_COMPLEX :
  914. pFormat += 2;
  915. pFormat += *((signed short *)pFormat);
  916. if ( ! (pfnFree = pfnFreeRoutines[ROUTINE_INDEX(*pFormat)]) )
  917. goto ComplexArrayFreeEnd;
  918. pArrayInfo->Dimension = Dimension + 1;
  919. MemoryElementSize = (ulong)
  920. ( NdrpMemoryIncrement( pStubMsg,
  921. pMemory,
  922. pFormat ) - pMemory );
  923. break;
  924. case FC_RP :
  925. case FC_UP :
  926. case FC_FP :
  927. case FC_OP :
  928. pfnFree = NdrPointerFree;
  929. // Need this in case we have a variant offset.
  930. MemoryElementSize = PTR_MEM_SIZE;
  931. break;
  932. case FC_IP :
  933. pfnFree = NdrInterfacePointerFree;
  934. // Need this in case we have a variant offset.
  935. MemoryElementSize = PTR_MEM_SIZE;
  936. break;
  937. default :
  938. // Must be a basetype.
  939. goto ComplexArrayFreeEnd;
  940. }
  941. pMemorySave = pMemory;
  942. //
  943. // If there is variance then increment the memory pointer to the first
  944. // element actually being marshalled.
  945. //
  946. if ( Offset )
  947. pMemory += Offset * MemoryElementSize;
  948. if ( pfnFree == NdrPointerFree )
  949. {
  950. pStubMsg->pArrayInfo = 0;
  951. for(; Count--; )
  952. {
  953. NdrPointerFree( pStubMsg,
  954. *((uchar **)pMemory),
  955. pFormat );
  956. // Increment the memory pointer by the element size.
  957. pMemory += PTR_MEM_SIZE;
  958. }
  959. goto ComplexArrayFreeEnd;
  960. }
  961. if ( ! IS_ARRAY_OR_STRING(*pFormat) )
  962. pStubMsg->pArrayInfo = 0;
  963. for ( ; Count--; )
  964. {
  965. // Keep track of multidimensional array dimension.
  966. if ( IS_ARRAY_OR_STRING(*pFormat) )
  967. pArrayInfo->Dimension = Dimension + 1;
  968. (*pfnFree)( pStubMsg,
  969. pMemory,
  970. pFormat );
  971. // Increment the memory pointer by the element size.
  972. pMemory += MemoryElementSize;
  973. }
  974. ComplexArrayFreeEnd:
  975. // pArrayInfo must be zero when not valid.
  976. pStubMsg->pArrayInfo = (Dimension == 0) ? 0 : pArrayInfo;
  977. }
  978. void RPC_ENTRY
  979. NdrEncapsulatedUnionFree(
  980. PMIDL_STUB_MESSAGE pStubMsg,
  981. uchar * pMemory,
  982. PFORMAT_STRING pFormat )
  983. /*++
  984. Routine Description :
  985. Frees an encapsulated union's embedded pointers which were allocated
  986. during a remote call.
  987. Used for FC_ENCAPSULATED_UNION.
  988. Arguments :
  989. pStubMsg - Pointer to the stub message.
  990. pMemory - Pointer to be freed.
  991. pFormat - Pointer's format string description.
  992. Return :
  993. None.
  994. --*/
  995. {
  996. long SwitchIs;
  997. NO_CORRELATION;
  998. switch ( LOW_NIBBLE(pFormat[1]) )
  999. {
  1000. case FC_SMALL :
  1001. case FC_CHAR :
  1002. SwitchIs = (long) *((char *)pMemory);
  1003. break;
  1004. case FC_USMALL :
  1005. SwitchIs = (long) *((uchar *)pMemory);
  1006. break;
  1007. case FC_ENUM16 :
  1008. case FC_SHORT :
  1009. SwitchIs = (long) *((short *)pMemory);
  1010. break;
  1011. case FC_USHORT :
  1012. case FC_WCHAR :
  1013. SwitchIs = (long) *((ushort *)pMemory);
  1014. break;
  1015. case FC_LONG :
  1016. case FC_ULONG :
  1017. case FC_ENUM32 :
  1018. // FC_INT3264 gets mapped to FC_LONG.
  1019. SwitchIs = *((long *)pMemory);
  1020. break;
  1021. default :
  1022. NDR_ASSERT(0,"NdrEncapsulatedUnionFree : bad switch type");
  1023. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  1024. return;
  1025. }
  1026. // Increment memory pointer to the union.
  1027. pMemory += HIGH_NIBBLE(pFormat[1]);
  1028. NdrpUnionFree( pStubMsg,
  1029. pMemory,
  1030. pFormat + 2,
  1031. SwitchIs );
  1032. }
  1033. void RPC_ENTRY
  1034. NdrNonEncapsulatedUnionFree(
  1035. PMIDL_STUB_MESSAGE pStubMsg,
  1036. uchar * pMemory,
  1037. PFORMAT_STRING pFormat )
  1038. /*++
  1039. Routine Description :
  1040. Frees a non-encapsulated union's embedded pointers which were allocated
  1041. during a remote call.
  1042. Used for FC_NON_ENCAPSULATED_UNION.
  1043. Arguments :
  1044. pStubMsg - Pointer to the stub message.
  1045. pMemory - Pointer to be freed.
  1046. pFormat - Pointer's format string description.
  1047. Return :
  1048. None.
  1049. --*/
  1050. {
  1051. long SwitchIs;
  1052. SwitchIs = (ulong) NdrpComputeSwitchIs( pStubMsg,
  1053. pMemory,
  1054. pFormat );
  1055. //
  1056. // Set the format string to the memory size and arm description.
  1057. //
  1058. pFormat += 6;
  1059. CORRELATION_DESC_INCREMENT( pFormat );
  1060. pFormat += *((signed short *)pFormat);
  1061. NdrpUnionFree( pStubMsg,
  1062. pMemory,
  1063. pFormat,
  1064. SwitchIs );
  1065. }
  1066. void
  1067. NdrpUnionFree(
  1068. PMIDL_STUB_MESSAGE pStubMsg,
  1069. uchar * pMemory,
  1070. PFORMAT_STRING pFormat,
  1071. long SwitchIs )
  1072. /*++
  1073. Routine Description :
  1074. Private routine shared by encapsulated and non-encapsulated unions for
  1075. freeing a union's embedded pointers which were allocated during a remote
  1076. call.
  1077. Arguments :
  1078. pStubMsg - Pointer to the stub message.
  1079. pMemory - Pointer to be freed.
  1080. pFormat - Pointer's format string description.
  1081. SwitchIs - The union's switch is.
  1082. Return :
  1083. None.
  1084. --*/
  1085. {
  1086. long Arms;
  1087. PFREE_ROUTINE pfnFree;
  1088. // Skip the memory size field.
  1089. pFormat += 2;
  1090. Arms = (long) ( *((ushort *&)pFormat)++ & 0x0fff );
  1091. //
  1092. // Search for the arm.
  1093. //
  1094. for ( ; Arms; Arms-- )
  1095. {
  1096. if ( *((long UNALIGNED *&)pFormat)++ == SwitchIs )
  1097. {
  1098. //
  1099. // Found the right arm, break out.
  1100. //
  1101. break;
  1102. }
  1103. // Else increment format string.
  1104. pFormat += 2;
  1105. }
  1106. //
  1107. // Check if we took the default arm and no default arm is specified.
  1108. //
  1109. if ( ! Arms && (*((ushort *)pFormat) == (ushort) 0xffff) )
  1110. {
  1111. return;
  1112. }
  1113. //
  1114. // Return if the arm is empty.
  1115. //
  1116. if ( ! *((ushort *)pFormat) )
  1117. return;
  1118. //
  1119. // Get the arm's description.
  1120. //
  1121. // We need a real solution after beta for simple type arms. This could
  1122. // break if we have a format string larger than about 32K.
  1123. //
  1124. if ( IS_MAGIC_UNION_BYTE(pFormat) )
  1125. return;
  1126. pFormat += *((signed short *)pFormat);
  1127. //
  1128. // If the union arm we take is a pointer, we have to dereference the
  1129. // current memory pointer since we're passed a pointer to the union
  1130. // (regardless of whether the actual parameter was a by-value union
  1131. // or a pointer to a union).
  1132. //
  1133. if ( IS_POINTER_TYPE(*pFormat) )
  1134. pMemory = *((uchar **)pMemory);
  1135. if ( pfnFree = pfnFreeRoutines[ROUTINE_INDEX(*pFormat)] )
  1136. {
  1137. (*pfnFree)( pStubMsg,
  1138. pMemory,
  1139. pFormat );
  1140. }
  1141. }
  1142. void RPC_ENTRY
  1143. NdrByteCountPointerFree(
  1144. PMIDL_STUB_MESSAGE pStubMsg,
  1145. uchar * pMemory,
  1146. PFORMAT_STRING pFormat )
  1147. /*++
  1148. Routine Description :
  1149. Frees a byte count pointer.
  1150. Used for FC_BYTE_COUNT_POINTER.
  1151. Arguments :
  1152. pStubMsg - Pointer to the stub message.
  1153. pMemory - Pointer to be freed.
  1154. pFormat - Pointer's format string description.
  1155. Return :
  1156. None.
  1157. --*/
  1158. {
  1159. if ( ! pMemory )
  1160. return;
  1161. //
  1162. // Free it if we didn't use the rpc buffer for it.
  1163. //
  1164. if ( (pMemory < pStubMsg->BufferStart) || (pMemory > pStubMsg->BufferEnd) )
  1165. (*pStubMsg->pfnFree)(pMemory);
  1166. }
  1167. void RPC_ENTRY
  1168. NdrXmitOrRepAsFree(
  1169. PMIDL_STUB_MESSAGE pStubMsg,
  1170. uchar * pMemory,
  1171. PFORMAT_STRING pFormat )
  1172. /*++
  1173. Routine Description :
  1174. Frees the transmit-as object (actually the presented type instance)
  1175. and steps over the object.
  1176. There is an exceptional situation where the spec forbids stub to free
  1177. the instance. This happens when there is an [in] only parameter with
  1178. a [transmit_as()] on a component of the parameter, and the presented
  1179. typedef is composed of one or more pointers.
  1180. We have a flag in the stub msg that is set when this happens.
  1181. See mrshl.c for the description of the FC layout.
  1182. Arguments :
  1183. pStubMsg - Pointer to the stub message.
  1184. pMemory - Pointer to be freed.
  1185. pFormat - Pointer's format string description.
  1186. Return :
  1187. None.
  1188. --*/
  1189. {
  1190. const XMIT_ROUTINE_QUINTUPLE * pQuintuple;
  1191. unsigned short QIndex;
  1192. // Skip the token itself and Oi flag. Fetch the QuintupleIndex.
  1193. QIndex = *(unsigned short *)(pFormat + 2);
  1194. pQuintuple = pStubMsg->StubDesc->aXmitQuintuple;
  1195. // Free the presented type instance unless forbidden explicitely.
  1196. if ( ! pStubMsg->fDontCallFreeInst )
  1197. {
  1198. pStubMsg->pPresentedType = pMemory;
  1199. pQuintuple[ QIndex ].pfnFreeInst( pStubMsg );
  1200. }
  1201. }
  1202. void RPC_ENTRY
  1203. NdrUserMarshalFree(
  1204. PMIDL_STUB_MESSAGE pStubMsg,
  1205. uchar * pMemory,
  1206. PFORMAT_STRING pFormat )
  1207. /*++
  1208. Routine Description :
  1209. Frees the usr_marshal object and steps over the object.
  1210. See mrshl.c for the description of the layouts.
  1211. Arguments :
  1212. pStubMsg - Pointer to the stub message.
  1213. pMemory - Pointer to be freed.
  1214. pFormat - Pointer's format string description.
  1215. Return :
  1216. None.
  1217. --*/
  1218. {
  1219. const USER_MARSHAL_ROUTINE_QUADRUPLE * pQuadruple;
  1220. unsigned short QIndex;
  1221. USER_MARSHAL_CB UserMarshalCB;
  1222. QIndex = *(unsigned short *)(pFormat + 2);
  1223. pQuadruple = pStubMsg->StubDesc->aUserMarshalQuadruple;
  1224. // Call the user to free his stuff.
  1225. NdrpInitUserMarshalCB( pStubMsg,
  1226. pFormat,
  1227. USER_MARSHAL_CB_FREE,
  1228. & UserMarshalCB);
  1229. // The user shouldn't ever free the top level object as we free it.
  1230. // He should free only pointees of his top level object.
  1231. pQuadruple[ QIndex ].pfnFree( (ulong*) &UserMarshalCB, pMemory );
  1232. // NdrpMemoryIncrement steps over the memory object.
  1233. }
  1234. void RPC_ENTRY
  1235. NdrInterfacePointerFree(
  1236. PMIDL_STUB_MESSAGE pStubMsg,
  1237. uchar * pMemory,
  1238. PFORMAT_STRING pFormat )
  1239. /*++
  1240. Routine Description :
  1241. Frees any memory associated with an interface pointer.
  1242. Arguments :
  1243. pStubMsg - Pointer to the stub message.
  1244. pMemory - Interface pointer.
  1245. pFormat - Interface pointer's format string description.
  1246. Return :
  1247. None.
  1248. --*/
  1249. {
  1250. if(pMemory != 0)
  1251. {
  1252. ((IUnknown *)pMemory)->Release();
  1253. pMemory = 0;
  1254. }
  1255. }
  1256. void
  1257. NdrpEmbeddedPointerFree(
  1258. PMIDL_STUB_MESSAGE pStubMsg,
  1259. uchar * pMemory,
  1260. PFORMAT_STRING pFormat )
  1261. /*++
  1262. Routine Description :
  1263. Frees an array's or a structure's embedded pointers which were allocated
  1264. during a remote call.
  1265. Arguments :
  1266. pStubMsg - Pointer to the stub message.
  1267. pMemory - Pointer to be freed.
  1268. pFormat - Pointer's format string description.
  1269. Return :
  1270. None.
  1271. --*/
  1272. {
  1273. uchar ** ppMemPtr;
  1274. uchar * pMemorySave;
  1275. ULONG_PTR MaxCountSave;
  1276. long OffsetSave;
  1277. MaxCountSave = pStubMsg->MaxCount;
  1278. OffsetSave = pStubMsg->Offset;
  1279. pMemorySave = pStubMsg->Memory;
  1280. pStubMsg->Memory = pMemory;
  1281. // Increment past the FC_PP and pad.
  1282. pFormat += 2;
  1283. for (;;)
  1284. {
  1285. if ( *pFormat == FC_END )
  1286. {
  1287. pStubMsg->Memory = pMemorySave;
  1288. break;
  1289. }
  1290. // Check for FC_FIXED_REPEAT or FC_VARIABLE_REPEAT.
  1291. if ( *pFormat != FC_NO_REPEAT )
  1292. {
  1293. pStubMsg->MaxCount = MaxCountSave;
  1294. pStubMsg->Offset = OffsetSave;
  1295. pFormat = NdrpEmbeddedRepeatPointerFree( pStubMsg,
  1296. pMemory,
  1297. pFormat );
  1298. // Continue loop to next pointer.
  1299. continue;
  1300. }
  1301. // Get the pointer to the pointer to free.
  1302. ppMemPtr = (uchar **)( pMemory + *((signed short *)(pFormat + 2)) );
  1303. // Increment to pointer description.
  1304. pFormat += 6;
  1305. NdrPointerFree(
  1306. pStubMsg,
  1307. *ppMemPtr,
  1308. pFormat );
  1309. // Increment to the next pointer description.
  1310. pFormat += 4;
  1311. }
  1312. }
  1313. PFORMAT_STRING
  1314. NdrpEmbeddedRepeatPointerFree(
  1315. PMIDL_STUB_MESSAGE pStubMsg,
  1316. uchar * pMemory,
  1317. PFORMAT_STRING pFormat )
  1318. /*++
  1319. Routine Description :
  1320. Frees an array's embedded pointers which were allocated during a remote
  1321. call.
  1322. Arguments :
  1323. pStubMsg - Pointer to the stub message.
  1324. pMemory - Pointer to be freed.
  1325. pFormat - Pointer's format string description.
  1326. Return :
  1327. Format string pointer after the array's pointer description.
  1328. --*/
  1329. {
  1330. uchar ** ppMemPtr;
  1331. uchar * pMemorySave;
  1332. PFORMAT_STRING pFormatSave;
  1333. ulong RepeatCount, RepeatIncrement, Pointers, PointersSave;
  1334. pMemorySave = pStubMsg->Memory;
  1335. switch ( *pFormat )
  1336. {
  1337. case FC_FIXED_REPEAT :
  1338. // Increment past the FC_FIXED_REPEAT and FC_PAD.
  1339. pFormat += 2;
  1340. // Get the total number of times to repeat the pointer marshall.
  1341. RepeatCount = *((ushort *&)pFormat)++;
  1342. break;
  1343. case FC_VARIABLE_REPEAT :
  1344. // Get the total number of times to repeat the pointer marshall.
  1345. RepeatCount = (ulong)pStubMsg->MaxCount;
  1346. //
  1347. // Check if this variable repeat instance also has a variable
  1348. // offset (this would be the case for a conformant varying array
  1349. // of pointers). If so then increment the memory pointer to point
  1350. // to the actual first array element which is being marshalled.
  1351. //
  1352. if ( pFormat[1] == FC_VARIABLE_OFFSET )
  1353. pMemory += *((ushort *)(pFormat + 2)) * pStubMsg->Offset;
  1354. // else pFormat[1] == FC_FIXED_OFFSET - do nothing
  1355. // Move the format string to the increment field.
  1356. pFormat += 2;
  1357. break;
  1358. default :
  1359. NDR_ASSERT(0,"NdrEmbeddedRepeatPointerFree : bad format char");
  1360. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  1361. return 0;
  1362. }
  1363. // Get the increment amount between successive pointers.
  1364. // This is actually increment to the next element, same position.
  1365. RepeatIncrement = *((ushort *&)pFormat)++;
  1366. //
  1367. // Add the offset to the beginning of this array to the Memory
  1368. // pointer. This is the offset from the current embedding structure
  1369. // to the array whose pointers we're marshalling.
  1370. //
  1371. pStubMsg->Memory += *((ushort *&)pFormat)++;
  1372. // Get the number of pointers in this repeat instance.
  1373. PointersSave = Pointers = *((ushort *&)pFormat)++;
  1374. pFormatSave = pFormat;
  1375. //
  1376. // Loop over the number of array elements.
  1377. //
  1378. for ( ; RepeatCount--;
  1379. pMemory += RepeatIncrement,
  1380. pStubMsg->Memory += RepeatIncrement )
  1381. {
  1382. pFormat = pFormatSave;
  1383. Pointers = PointersSave;
  1384. //
  1385. // Loop over the number of pointers per array element (this can be
  1386. // zero for an array of structures).
  1387. //
  1388. for ( ; Pointers--; )
  1389. {
  1390. ppMemPtr = (uchar **)(pMemory + *((signed short *)pFormat));
  1391. // Increment to pointer's description.
  1392. pFormat += 4;
  1393. NdrPointerFree(
  1394. pStubMsg,
  1395. *ppMemPtr,
  1396. pFormat );
  1397. // Increment to the next pointer's offset_in_memory.
  1398. pFormat += 4;
  1399. }
  1400. }
  1401. pStubMsg->Memory = pMemory;
  1402. // Return format string pointer past the array's pointer layout.
  1403. return pFormatSave + PointersSave * 8;
  1404. }