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.

565 lines
16 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name :
  4. misc.c
  5. Abstract :
  6. Contains miscelaneous helper routines.
  7. Author :
  8. David Kays dkays December 1993.
  9. Revision History :
  10. ---------------------------------------------------------------------*/
  11. #include "precomp.hxx"
  12. uchar *
  13. Ndr64pMemoryIncrement(
  14. PMIDL_STUB_MESSAGE pStubMsg,
  15. uchar * pMemory,
  16. PNDR64_FORMAT pFormat,
  17. BOOL fUseBufferConformance
  18. )
  19. /*++
  20. Routine Description :
  21. Returns a memory pointer incremeted past a complex data type. This routine
  22. is also overloaded to compute the size of a complex data type by passing
  23. a 0 memory pointer.
  24. Arguments :
  25. pStubMsg - Pointer to the stub message.
  26. pMemory - Pointer to the complex type, or 0 if a size is being computed.
  27. pFormat - Format string description.
  28. fUseBufferConformance - Use conformance from buffer(During unmarshalling).
  29. Return :
  30. A memory pointer incremented past the complex type. If a 0 memory pointer
  31. was passed in then the returned value is the size of the complex type.
  32. --*/
  33. {
  34. long Elements;
  35. long ElementSize;
  36. switch ( *(PFORMAT_STRING)pFormat )
  37. {
  38. //
  39. // simple types
  40. //
  41. case FC64_CHAR :
  42. case FC64_WCHAR :
  43. case FC64_INT8:
  44. case FC64_UINT8:
  45. case FC64_INT16:
  46. case FC64_UINT16:
  47. case FC64_INT32:
  48. case FC64_UINT32:
  49. case FC64_INT64:
  50. case FC64_UINT64:
  51. case FC64_FLOAT32 :
  52. case FC64_FLOAT64 :
  53. case FC64_ERROR_STATUS_T:
  54. case FC64_IGNORE :
  55. pMemory += NDR64_SIMPLE_TYPE_MEMSIZE(*(PFORMAT_STRING)pFormat);
  56. break;
  57. //
  58. // Structs
  59. //
  60. case FC64_STRUCT :
  61. case FC64_PSTRUCT :
  62. pMemory += ((const NDR64_STRUCTURE_HEADER_FORMAT*)pFormat)->MemorySize;
  63. break;
  64. case FC64_CONF_STRUCT :
  65. case FC64_CONF_PSTRUCT :
  66. {
  67. const NDR64_CONF_STRUCTURE_HEADER_FORMAT *pStructFormat =
  68. (const NDR64_CONF_STRUCTURE_HEADER_FORMAT*) pFormat;
  69. pMemory += pStructFormat->MemorySize;
  70. pMemory = Ndr64pMemoryIncrement( pStubMsg,
  71. pMemory,
  72. pStructFormat->ArrayDescription,
  73. fUseBufferConformance );
  74. }
  75. break;
  76. case FC64_BOGUS_STRUCT :
  77. case FC64_FORCED_BOGUS_STRUCT:
  78. pMemory += ((const NDR64_BOGUS_STRUCTURE_HEADER_FORMAT*)pFormat)->MemorySize;
  79. break;
  80. case FC64_CONF_BOGUS_STRUCT:
  81. case FC64_FORCED_CONF_BOGUS_STRUCT:
  82. {
  83. const NDR64_CONF_BOGUS_STRUCTURE_HEADER_FORMAT *pStructFormat =
  84. (const NDR64_CONF_BOGUS_STRUCTURE_HEADER_FORMAT*) pFormat;
  85. CORRELATION_CONTEXT CorrCtxt( pStubMsg, pMemory );
  86. pMemory += pStructFormat->MemorySize;
  87. pMemory = Ndr64pMemoryIncrement( pStubMsg,
  88. pMemory,
  89. pStructFormat->ConfArrayDescription,
  90. fUseBufferConformance );
  91. }
  92. break;
  93. //
  94. // Unions
  95. //
  96. case FC64_ENCAPSULATED_UNION :
  97. pMemory += ((const NDR64_ENCAPSULATED_UNION*)pFormat)->MemorySize;
  98. break;
  99. case FC64_NON_ENCAPSULATED_UNION :
  100. pMemory += ((const NDR64_NON_ENCAPSULATED_UNION*)pFormat)->MemorySize;
  101. break;
  102. //
  103. // Arrays
  104. //
  105. case FC64_FIX_ARRAY :
  106. pMemory += ((const NDR64_FIX_ARRAY_HEADER_FORMAT*)pFormat)->TotalSize;
  107. break;
  108. case FC64_CONF_ARRAY:
  109. {
  110. const NDR64_CONF_ARRAY_HEADER_FORMAT *pArrayFormat =
  111. (const NDR64_CONF_ARRAY_HEADER_FORMAT *)pFormat;
  112. SAVE_CONTEXT<uchar*> ConformanceMarkSave( pStubMsg->ConformanceMark );
  113. NDR64_WIRE_COUNT_TYPE Elements;
  114. if ( fUseBufferConformance )
  115. {
  116. Elements = *(NDR64_WIRE_COUNT_TYPE*)pStubMsg->ConformanceMark;
  117. pStubMsg->ConformanceMark += sizeof(NDR64_WIRE_COUNT_TYPE);
  118. }
  119. else
  120. {
  121. Elements =
  122. Ndr64EvaluateExpr( pStubMsg,
  123. pArrayFormat->ConfDescriptor,
  124. EXPR_MAXCOUNT );
  125. }
  126. pMemory += Ndr64pConvertTo2GB( (NDR64_UINT64)pArrayFormat->ElementSize *
  127. Elements );
  128. }
  129. break;
  130. case FC64_CONFVAR_ARRAY:
  131. {
  132. const NDR64_CONF_VAR_ARRAY_HEADER_FORMAT *pArrayFormat =
  133. (const NDR64_CONF_VAR_ARRAY_HEADER_FORMAT *)pFormat;
  134. SAVE_CONTEXT<uchar*> ConformanceMarkSave( pStubMsg->ConformanceMark );
  135. NDR64_WIRE_COUNT_TYPE Elements;
  136. if ( fUseBufferConformance )
  137. {
  138. Elements = *(NDR64_WIRE_COUNT_TYPE*)pStubMsg->ConformanceMark;
  139. pStubMsg->ConformanceMark += sizeof(NDR64_WIRE_COUNT_TYPE);
  140. }
  141. else
  142. {
  143. Elements =
  144. Ndr64EvaluateExpr( pStubMsg,
  145. pArrayFormat->ConfDescriptor,
  146. EXPR_MAXCOUNT );
  147. }
  148. pMemory += Ndr64pConvertTo2GB( (NDR64_UINT64)pArrayFormat->ElementSize *
  149. Elements );
  150. }
  151. break;
  152. case FC64_VAR_ARRAY:
  153. {
  154. const NDR64_VAR_ARRAY_HEADER_FORMAT *pArrayFormat =
  155. (NDR64_VAR_ARRAY_HEADER_FORMAT*)pFormat;
  156. pMemory += pArrayFormat->TotalSize;
  157. }
  158. break;
  159. case FC64_FIX_BOGUS_ARRAY :
  160. case FC64_FIX_FORCED_BOGUS_ARRAY:
  161. case FC64_BOGUS_ARRAY:
  162. case FC64_FORCED_BOGUS_ARRAY:
  163. {
  164. const NDR64_BOGUS_ARRAY_HEADER_FORMAT *pArrayFormat =
  165. (NDR64_BOGUS_ARRAY_HEADER_FORMAT*)pFormat;
  166. NDR64_WIRE_COUNT_TYPE Elements = pArrayFormat->NumberElements;
  167. BOOL IsFixed = ( pArrayFormat->FormatCode == FC64_FIX_BOGUS_ARRAY ) ||
  168. ( pArrayFormat->FormatCode == FC64_FIX_FORCED_BOGUS_ARRAY );
  169. SAVE_CONTEXT<uchar*> ConformanceMarkSave( pStubMsg->ConformanceMark );
  170. if ( !IsFixed )
  171. {
  172. const NDR64_CONF_VAR_BOGUS_ARRAY_HEADER_FORMAT* pConfVarFormat=
  173. (NDR64_CONF_VAR_BOGUS_ARRAY_HEADER_FORMAT*)pFormat;
  174. if ( pConfVarFormat->ConfDescription )
  175. {
  176. if ( fUseBufferConformance )
  177. {
  178. Elements = *(NDR64_WIRE_COUNT_TYPE*)pStubMsg->ConformanceMark;
  179. pStubMsg->ConformanceMark += sizeof(NDR64_WIRE_COUNT_TYPE);
  180. }
  181. else
  182. {
  183. Elements = (NDR64_UINT32)
  184. Ndr64EvaluateExpr(pStubMsg,
  185. pConfVarFormat->ConfDescription,
  186. EXPR_MAXCOUNT);
  187. }
  188. }
  189. }
  190. NDR64_UINT32 ElementMemorySize =
  191. (NDR64_UINT32)( Ndr64pMemoryIncrement( pStubMsg,
  192. pMemory,
  193. pArrayFormat->Element,
  194. fUseBufferConformance ) - pMemory );
  195. pMemory += Ndr64pConvertTo2GB( (NDR64_UINT64)ElementMemorySize *
  196. Elements );
  197. }
  198. break;
  199. //
  200. // String arrays (a.k.a. non-conformant strings).
  201. //
  202. case FC64_CHAR_STRING:
  203. case FC64_WCHAR_STRING:
  204. case FC64_STRUCT_STRING:
  205. {
  206. const NDR64_NON_CONFORMANT_STRING_FORMAT *pStringFormat =
  207. (const NDR64_NON_CONFORMANT_STRING_FORMAT*) pFormat;
  208. pMemory += pStringFormat->TotalSize;
  209. break;
  210. }
  211. //
  212. // Sized conformant strings.
  213. //
  214. case FC64_CONF_CHAR_STRING:
  215. case FC64_CONF_WCHAR_STRING:
  216. case FC64_CONF_STRUCT_STRING:
  217. {
  218. const NDR64_SIZED_CONFORMANT_STRING_FORMAT *pStringFormat =
  219. (const NDR64_SIZED_CONFORMANT_STRING_FORMAT *) pFormat;
  220. NDR64_WIRE_COUNT_TYPE Elements;
  221. SAVE_CONTEXT<uchar*> ConformanceMarkSave( pStubMsg->ConformanceMark );
  222. NDR_ASSERT( pStringFormat->Header.Flags.IsSized,,
  223. "Ndr64pMemoryIncrement : called for non-sized string");
  224. if ( fUseBufferConformance )
  225. {
  226. Elements = *(NDR64_WIRE_COUNT_TYPE*)pStubMsg->ConformanceMark;
  227. pStubMsg->ConformanceMark += sizeof(NDR64_WIRE_COUNT_TYPE);
  228. }
  229. else
  230. {
  231. Elements = Ndr64EvaluateExpr( pStubMsg,
  232. pStringFormat->SizeDescription,
  233. EXPR_MAXCOUNT );
  234. }
  235. pMemory += Ndr64pConvertTo2GB( Elements *
  236. (NDR64_UINT64)pStringFormat->Header.ElementSize );
  237. }
  238. break;
  239. case FC64_RP :
  240. case FC64_UP :
  241. case FC64_OP :
  242. case FC64_IP :
  243. case FC64_FP :
  244. pMemory += PTR_MEM_SIZE;
  245. break;
  246. case FC64_RANGE:
  247. pMemory += NDR64_SIMPLE_TYPE_MEMSIZE( ((NDR64_RANGE_FORMAT *)pFormat)->RangeType );
  248. break;
  249. //
  250. // Transmit as, represent as, user marshal
  251. //
  252. case FC64_TRANSMIT_AS :
  253. case FC64_REPRESENT_AS :
  254. pMemory += ( ( NDR64_TRANSMIT_AS_FORMAT * )pFormat )->PresentedTypeMemorySize;
  255. break;
  256. case FC64_USER_MARSHAL :
  257. // Get the presented type size.
  258. pMemory += ( ( NDR64_USER_MARSHAL_FORMAT * )pFormat )->UserTypeMemorySize;
  259. break;
  260. default :
  261. NDR_ASSERT(0,"Ndr64pMemoryIncrement : bad format char");
  262. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  263. return 0;
  264. }
  265. return pMemory;
  266. }
  267. BOOL
  268. Ndr64pIsStructStringTerminator(
  269. NDR64_UINT8* pMemory,
  270. NDR64_UINT32 ElementSize
  271. )
  272. /*--
  273. RoutineDescription :
  274. Determines is pMemory is a struct string terminator.
  275. Arguments :
  276. pMemory - Pointer to struct string character.
  277. ElementSize - Number of bytes of each string character.
  278. Return :
  279. Length of string.
  280. --*/
  281. {
  282. while( ElementSize-- )
  283. {
  284. if ( *pMemory++ != 0)
  285. return FALSE;
  286. }
  287. return TRUE;
  288. }
  289. NDR64_UINT32
  290. Ndr64pStructStringLen(
  291. NDR64_UINT8* pMemory,
  292. NDR64_UINT32 ElementSize
  293. )
  294. /*--
  295. RoutineDescription :
  296. Determines a stringable struct's length.
  297. Arguments :
  298. pMemory - Pointer to stringable struct.
  299. ElementSize - Number of bytes of each string element.
  300. Return :
  301. Length of string.
  302. --*/
  303. {
  304. NDR64_UINT32 StringSize = 0;
  305. while( !Ndr64pIsStructStringTerminator( pMemory, ElementSize ) )
  306. {
  307. StringSize++;
  308. pMemory += ElementSize;
  309. }
  310. return StringSize;
  311. }
  312. NDR64_UINT32
  313. Ndr64pCommonStringSize(
  314. PMIDL_STUB_MESSAGE pStubMsg,
  315. uchar * pMemory,
  316. const NDR64_STRING_HEADER_FORMAT *pStringFormat
  317. )
  318. {
  319. NDR64_UINT64 CopySize64;
  320. // Compute the element count of the string and the total copy size.
  321. switch ( pStringFormat->FormatCode )
  322. {
  323. case FC64_CHAR_STRING:
  324. case FC64_CONF_CHAR_STRING:
  325. CopySize64 = pStubMsg->ActualCount = strlen((char *)pMemory) + 1;
  326. break;
  327. case FC64_WCHAR_STRING:
  328. case FC64_CONF_WCHAR_STRING:
  329. pStubMsg->ActualCount = wcslen((wchar_t *)pMemory) + 1;
  330. CopySize64 = (NDR64_UINT64)pStubMsg->ActualCount * (NDR64_UINT64)sizeof(wchar_t);
  331. break;
  332. case FC64_STRUCT_STRING:
  333. case FC64_CONF_STRUCT_STRING:
  334. pStubMsg->ActualCount = Ndr64pStructStringLen( (NDR64_UINT8*)pMemory,
  335. pStringFormat->ElementSize ) + 1;
  336. CopySize64 = (NDR64_UINT64)pStubMsg->ActualCount *
  337. (NDR64_UINT64)pStringFormat->ElementSize;
  338. break;
  339. default :
  340. NDR_ASSERT(0,"Ndr64pConformantStringMarshall : bad format char");
  341. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  342. return 0;
  343. }
  344. pStubMsg->Offset = 0;
  345. return Ndr64pConvertTo2GB( CopySize64 );
  346. }
  347. PNDR64_FORMAT
  348. Ndr64pFindUnionArm(
  349. PMIDL_STUB_MESSAGE pStubMsg,
  350. const NDR64_UNION_ARM_SELECTOR* pArmSelector,
  351. EXPR_VALUE Value
  352. )
  353. {
  354. const NDR64_UNION_ARM *pUnionArm = (NDR64_UNION_ARM*)(pArmSelector + 1);
  355. NDR64_UINT32 Arms = pArmSelector->Arms;
  356. while(1)
  357. {
  358. if (0 == Arms--)
  359. {
  360. PNDR64_FORMAT DefaultType = *(PNDR64_FORMAT*)pUnionArm;
  361. if (DefaultType != (PNDR64_FORMAT)-1)
  362. {
  363. return DefaultType ? *(PNDR64_FORMAT*)pUnionArm :
  364. NULL;
  365. }
  366. else
  367. {
  368. RpcRaiseException( RPC_S_INVALID_TAG );
  369. return 0;
  370. }
  371. }
  372. if ( (EXPR_VALUE)pUnionArm->CaseValue == Value )
  373. {
  374. return pUnionArm->Type ? pUnionArm->Type : NULL;
  375. }
  376. pUnionArm++;
  377. }
  378. }
  379. EXPR_VALUE
  380. Ndr64pSimpleTypeToExprValue(
  381. NDR64_FORMAT_CHAR FormatChar,
  382. uchar *pSimple)
  383. {
  384. switch( FormatChar )
  385. {
  386. case FC64_UINT8:
  387. return (EXPR_VALUE)*(unsigned char *)pSimple;
  388. case FC64_CHAR:
  389. case FC64_INT8:
  390. return (EXPR_VALUE)*(signed char *)pSimple;
  391. case FC64_WCHAR:
  392. case FC64_UINT16:
  393. return (EXPR_VALUE)*(unsigned short *)pSimple;
  394. case FC64_INT16:
  395. return (EXPR_VALUE)*(signed short *)pSimple;
  396. case FC64_UINT32:
  397. return (EXPR_VALUE)*(unsigned long *)pSimple;
  398. case FC64_INT32:
  399. case FC64_ERROR_STATUS_T:
  400. return (EXPR_VALUE)*(signed long *)pSimple;
  401. case FC64_INT64:
  402. return (EXPR_VALUE)*(NDR64_INT64 *)pSimple;
  403. case FC64_UINT64:
  404. return (EXPR_VALUE)*(NDR64_UINT64 *)pSimple;
  405. case FC64_POINTER:
  406. return (EXPR_VALUE)*(void **)pSimple;
  407. default :
  408. NDR_ASSERT(0,"Ndr64pSimpleTypeToExprValue : bad swith type");
  409. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  410. return 0;
  411. }
  412. }
  413. EXPR_VALUE
  414. Ndr64pCastExprValueToExprValue(
  415. NDR64_FORMAT_CHAR FormatChar,
  416. EXPR_VALUE Value)
  417. {
  418. switch ( FormatChar )
  419. {
  420. case FC64_UINT8:
  421. return (EXPR_VALUE)(unsigned char)Value;
  422. case FC64_INT8:
  423. case FC64_CHAR:
  424. return (EXPR_VALUE)(signed char)Value;
  425. case FC64_UINT16:
  426. case FC64_WCHAR:
  427. return (EXPR_VALUE)(unsigned short)Value;
  428. case FC64_INT16:
  429. return (EXPR_VALUE)(signed short)Value;
  430. case FC64_UINT32:
  431. return (EXPR_VALUE)(unsigned long)Value;
  432. case FC64_INT32:
  433. return (EXPR_VALUE)(signed long)Value;
  434. case FC64_UINT64:
  435. return (EXPR_VALUE)(NDR64_UINT64)Value;
  436. case FC64_INT64:
  437. return (EXPR_VALUE)(NDR64_INT64)Value;
  438. case FC64_POINTER:
  439. return (EXPR_VALUE)(void *)Value;
  440. default:
  441. NDR_ASSERT(0,"Ndr64pCastExprValueToExprValue : Illegal type.");
  442. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  443. return 0;
  444. }
  445. }