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.

397 lines
11 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name :
  4. pointer.cxx
  5. Abstract :
  6. This file contains the routines for handling pointers and pointer
  7. layouts.
  8. Author :
  9. Mike Zoran mzoran January 2000.
  10. Revision History :
  11. ---------------------------------------------------------------------*/
  12. #include "precomp.hxx"
  13. #if !defined(DBG)
  14. // Optimize for time to force inlining.
  15. #pragma optimize("gt", on)
  16. #endif
  17. typedef enum
  18. {
  19. NDR64_CORRELATION_NONE=0,
  20. NDR64_CORRELATION_MEMORY=1,
  21. NDR64_CORRELATION_BUFFER=2
  22. } NDR64_CORRELATION_TYPE;
  23. template<class Function>
  24. static __forceinline void
  25. Ndr64pProcessPointerLayout(
  26. PMIDL_STUB_MESSAGE pStubMsg,
  27. PNDR64_FORMAT pLayoutFormat,
  28. NDR64_UINT32 ArrayIterations,
  29. uchar * pMemory,
  30. uchar * pBuffer,
  31. const NDR64_CORRELATION_TYPE CorrType,
  32. Function func )
  33. /*++
  34. Routine Description :
  35. Marshalls an array's or a structure's embedded pointers.
  36. Arguments :
  37. pStubMsg - Pointer to the stub message.
  38. pFormat - The format string pointer layout.
  39. ArrayIterations - Numbers of iterations for variable sized arrays.
  40. pMemory - Pointer to the structure or array whose embedded pointers
  41. are being marshalled.
  42. pBuffer - Pointer to the buffer for the structure or arrays
  43. CorrType - Determines how to set pCorrMemory.
  44. func - Function to call to do the processing
  45. Return :
  46. Format string pointer after the pointer layout.
  47. --*/
  48. {
  49. PFORMAT_STRING pFormat = (PFORMAT_STRING)pLayoutFormat;
  50. for (;;)
  51. {
  52. const NDR64_REPEAT_FORMAT *pRepeatFormat;
  53. NDR64_UINT32 Iterations;
  54. switch( *pFormat )
  55. {
  56. case FC64_NO_REPEAT:
  57. {
  58. const NDR64_NO_REPEAT_FORMAT *pNoRepeatHeader =
  59. (NDR64_NO_REPEAT_FORMAT*)pFormat;
  60. const NDR64_POINTER_INSTANCE_HEADER_FORMAT *pPointerInstance =
  61. (NDR64_POINTER_INSTANCE_HEADER_FORMAT *)(pNoRepeatHeader + 1);
  62. const NDR64_POINTER_FORMAT *pPointerFormat =
  63. (NDR64_POINTER_FORMAT*)(pPointerInstance + 1);
  64. uchar *pMemPtr = pMemory + pPointerInstance->Offset;
  65. uchar *pBufPtr = pBuffer + pPointerInstance->Offset;
  66. func( pStubMsg,
  67. pMemPtr,
  68. pBufPtr ,
  69. pPointerFormat );
  70. pFormat += sizeof(NDR64_NO_REPEAT_FORMAT) +
  71. sizeof(NDR64_POINTER_INSTANCE_HEADER_FORMAT) +
  72. sizeof(NDR64_POINTER_FORMAT);
  73. break;
  74. }
  75. case FC64_FIXED_REPEAT:
  76. Iterations = ((NDR64_FIXED_REPEAT_FORMAT*)pFormat)->Iterations;
  77. pRepeatFormat = (NDR64_REPEAT_FORMAT*)pFormat;
  78. pFormat += sizeof(NDR64_FIXED_REPEAT_FORMAT);
  79. goto RepeatCommon;
  80. case FC64_VARIABLE_REPEAT:
  81. Iterations = ArrayIterations;
  82. pRepeatFormat = (NDR64_REPEAT_FORMAT*)pFormat;
  83. pFormat += sizeof(NDR64_REPEAT_FORMAT);
  84. // Fall through to Repeat Common
  85. RepeatCommon:
  86. {
  87. uchar *pArrayMemory = pMemory + pRepeatFormat->OffsetToArray;
  88. uchar *pArrayBuffer = pBuffer + pRepeatFormat->OffsetToArray;
  89. PFORMAT_STRING pFormatSave = pFormat;
  90. uchar *pCorrMemorySave;
  91. if ( CorrType )
  92. pCorrMemorySave = pStubMsg->pCorrMemory;
  93. {
  94. // Loop over the array elements
  95. for( ; Iterations;
  96. Iterations--,
  97. pArrayMemory += pRepeatFormat->Increment,
  98. pArrayBuffer += pRepeatFormat->Increment)
  99. {
  100. pFormat = pFormatSave;
  101. if ( CorrType )
  102. {
  103. if ( CorrType == NDR64_CORRELATION_MEMORY )
  104. {
  105. if (pRepeatFormat->Flags.SetCorrMark)
  106. pStubMsg->pCorrMemory = pArrayMemory;
  107. }
  108. else
  109. {
  110. if (pRepeatFormat->Flags.SetCorrMark)
  111. pStubMsg->pCorrMemory = pArrayBuffer;
  112. }
  113. }
  114. // Loop over the pointers per element
  115. for ( NDR64_UINT32 Pointers = pRepeatFormat->NumberOfPointers;
  116. Pointers; Pointers-- )
  117. {
  118. const NDR64_POINTER_INSTANCE_HEADER_FORMAT *pPointerInstance =
  119. (NDR64_POINTER_INSTANCE_HEADER_FORMAT *)pFormat;
  120. const NDR64_POINTER_FORMAT *pPointerFormat =
  121. (NDR64_POINTER_FORMAT*)(pPointerInstance + 1);
  122. uchar *pMemPtr = pArrayMemory + pPointerInstance->Offset;
  123. uchar *pBufPtr = pArrayBuffer + pPointerInstance->Offset;
  124. func( pStubMsg,
  125. pMemPtr,
  126. pBufPtr,
  127. pPointerFormat );
  128. pFormat += sizeof(NDR64_POINTER_INSTANCE_HEADER_FORMAT) +
  129. sizeof(NDR64_POINTER_FORMAT);
  130. }
  131. }
  132. }
  133. if ( CorrType )
  134. pStubMsg->pCorrMemory = pCorrMemorySave;
  135. }
  136. case FC64_END:
  137. return;
  138. default :
  139. NDR_ASSERT(0,"Ndr64pProcessPointerLayout : bad format char");
  140. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  141. } // switch
  142. }
  143. }
  144. static __forceinline void
  145. Ndr64pPointerLayoutMarshallCallback(
  146. PMIDL_STUB_MESSAGE pStubMsg,
  147. uchar* pMemPtr,
  148. uchar* pBufPtr,
  149. PNDR64_FORMAT pFormat )
  150. {
  151. Ndr64pPointerMarshall
  152. ( pStubMsg,
  153. (NDR64_PTR_WIRE_TYPE*)pBufPtr,
  154. *(uchar**)pMemPtr,
  155. (PFORMAT_STRING)pFormat );
  156. }
  157. void
  158. Ndr64pPointerLayoutMarshall(
  159. PMIDL_STUB_MESSAGE pStubMsg,
  160. PNDR64_FORMAT pFormat,
  161. NDR64_UINT32 ArrayIterations,
  162. uchar * pMemory,
  163. uchar * pBuffer )
  164. {
  165. POINTER_BUFFER_SWAP_CONTEXT SwapContext( pStubMsg );
  166. Ndr64pProcessPointerLayout(
  167. pStubMsg,
  168. pFormat,
  169. ArrayIterations,
  170. pMemory,
  171. pBuffer,
  172. NDR64_CORRELATION_MEMORY,
  173. Ndr64pPointerLayoutMarshallCallback);
  174. }
  175. static __forceinline void
  176. Ndr64pPointerLayoutUnmarshallCallback(
  177. PMIDL_STUB_MESSAGE pStubMsg,
  178. uchar* pMemPtr,
  179. uchar* pBufPtr,
  180. PNDR64_FORMAT pFormat )
  181. {
  182. Ndr64pPointerUnmarshall(
  183. pStubMsg,
  184. *(NDR64_PTR_WIRE_TYPE*)pBufPtr,
  185. (uchar **)pMemPtr,
  186. *(uchar**)pMemPtr,
  187. pFormat );
  188. // Need to copy the value written to the memory pointer back to
  189. // the buffer pointer since the buffer will be block copied to the memory.
  190. *(uchar **)pBufPtr = *(uchar **)pMemPtr;
  191. }
  192. void
  193. Ndr64pPointerLayoutUnmarshall(
  194. PMIDL_STUB_MESSAGE pStubMsg,
  195. PNDR64_FORMAT pFormat,
  196. NDR64_UINT32 ArrayIterations,
  197. uchar * pMemory,
  198. uchar * pBuffer )
  199. {
  200. POINTER_BUFFER_SWAP_CONTEXT SwapContext( pStubMsg );
  201. uchar *pBufferSave = 0;
  202. // Insert full pointer to ref id translation if needed.
  203. if ( pStubMsg->FullPtrRefId )
  204. FULL_POINTER_INSERT( pStubMsg, pMemory );
  205. Ndr64pProcessPointerLayout(
  206. pStubMsg,
  207. pFormat,
  208. ArrayIterations,
  209. pMemory,
  210. pBuffer,
  211. NDR64_CORRELATION_BUFFER,
  212. Ndr64pPointerLayoutUnmarshallCallback);
  213. }
  214. static __forceinline void
  215. Ndr64pPointerLayoutMemorySizeCallback(
  216. PMIDL_STUB_MESSAGE pStubMsg,
  217. uchar* pMemPtr,
  218. uchar* pBufPtr,
  219. PNDR64_FORMAT pFormat )
  220. {
  221. // Discard the pMemPtr
  222. Ndr64pPointerMemorySize(
  223. pStubMsg,
  224. (NDR64_PTR_WIRE_TYPE*)pBufPtr,
  225. pFormat );
  226. }
  227. void
  228. Ndr64pPointerLayoutMemorySize (
  229. PMIDL_STUB_MESSAGE pStubMsg,
  230. PNDR64_FORMAT pFormat,
  231. NDR64_UINT32 ArrayIterations,
  232. uchar * pBuffer )
  233. {
  234. if ( pStubMsg->IgnoreEmbeddedPointers )
  235. return;
  236. POINTER_MEMSIZE_SWAP_CONTEXT SwapContext( pStubMsg );
  237. Ndr64pProcessPointerLayout(
  238. pStubMsg,
  239. pFormat,
  240. ArrayIterations,
  241. pBuffer,
  242. pBuffer,
  243. NDR64_CORRELATION_NONE,
  244. Ndr64pPointerLayoutMemorySizeCallback );
  245. }
  246. static __forceinline void
  247. Ndr64pPointerLayoutBufferSizeCallback(
  248. PMIDL_STUB_MESSAGE pStubMsg,
  249. uchar* pMemPtr,
  250. uchar* pBufPtr,
  251. PNDR64_FORMAT pFormat )
  252. {
  253. // Discard the BufferPointer
  254. Ndr64pPointerBufferSize(
  255. pStubMsg,
  256. *(uchar**)pMemPtr,
  257. pFormat );
  258. }
  259. void
  260. Ndr64pPointerLayoutBufferSize (
  261. PMIDL_STUB_MESSAGE pStubMsg,
  262. PNDR64_FORMAT pFormat,
  263. NDR64_UINT32 ArrayIterations,
  264. uchar * pMemory )
  265. {
  266. if ( pStubMsg->IgnoreEmbeddedPointers )
  267. return;
  268. POINTER_BUFFERLENGTH_SWAP_CONTEXT SwapContext( pStubMsg );
  269. Ndr64pProcessPointerLayout(
  270. pStubMsg,
  271. pFormat,
  272. ArrayIterations,
  273. pMemory,
  274. pMemory,
  275. NDR64_CORRELATION_MEMORY,
  276. Ndr64pPointerLayoutBufferSizeCallback );
  277. }
  278. static __forceinline void
  279. Ndr64pPointerLayoutFreeCallback(
  280. PMIDL_STUB_MESSAGE pStubMsg,
  281. uchar* pMemPtr,
  282. uchar* pBufPtr,
  283. PNDR64_FORMAT pFormat )
  284. {
  285. Ndr64PointerFree(
  286. pStubMsg,
  287. *(uchar**)pMemPtr,
  288. pFormat );
  289. }
  290. void
  291. Ndr64pPointerLayoutFree(
  292. PMIDL_STUB_MESSAGE pStubMsg,
  293. PNDR64_FORMAT pFormat,
  294. NDR64_UINT32 ArrayIterations,
  295. uchar * pMemory )
  296. {
  297. Ndr64pProcessPointerLayout(
  298. pStubMsg,
  299. pFormat,
  300. ArrayIterations,
  301. pMemory,
  302. pMemory,
  303. NDR64_CORRELATION_MEMORY,
  304. Ndr64pPointerLayoutFreeCallback );
  305. }