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.

566 lines
17 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. // MIDL generates 0xffffffff instead of -1 here, and pointer is 64bit.
  362. if ( (NDR64_UINT32)((ULONG_PTR)DefaultType) != 0xffffffff)
  363. {
  364. return DefaultType ? *(PNDR64_FORMAT*)pUnionArm :
  365. NULL;
  366. }
  367. else
  368. {
  369. RpcRaiseException( RPC_S_INVALID_TAG );
  370. return 0;
  371. }
  372. }
  373. if ( (EXPR_VALUE)pUnionArm->CaseValue == Value )
  374. {
  375. return pUnionArm->Type ? pUnionArm->Type : NULL;
  376. }
  377. pUnionArm++;
  378. }
  379. }
  380. EXPR_VALUE
  381. Ndr64pSimpleTypeToExprValue(
  382. NDR64_FORMAT_CHAR FormatChar,
  383. uchar *pSimple)
  384. {
  385. switch( FormatChar )
  386. {
  387. case FC64_UINT8:
  388. return (EXPR_VALUE)*(unsigned char *)pSimple;
  389. case FC64_CHAR:
  390. case FC64_INT8:
  391. return (EXPR_VALUE)*(signed char *)pSimple;
  392. case FC64_WCHAR:
  393. case FC64_UINT16:
  394. return (EXPR_VALUE)*(unsigned short *)pSimple;
  395. case FC64_INT16:
  396. return (EXPR_VALUE)*(signed short *)pSimple;
  397. case FC64_UINT32:
  398. return (EXPR_VALUE)*(unsigned long *)pSimple;
  399. case FC64_INT32:
  400. case FC64_ERROR_STATUS_T:
  401. return (EXPR_VALUE)*(signed long *)pSimple;
  402. case FC64_INT64:
  403. return (EXPR_VALUE)*(NDR64_INT64 *)pSimple;
  404. case FC64_UINT64:
  405. return (EXPR_VALUE)*(NDR64_UINT64 *)pSimple;
  406. case FC64_POINTER:
  407. return (EXPR_VALUE)*(void **)pSimple;
  408. default :
  409. NDR_ASSERT(0,"Ndr64pSimpleTypeToExprValue : bad swith type");
  410. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  411. return 0;
  412. }
  413. }
  414. EXPR_VALUE
  415. Ndr64pCastExprValueToExprValue(
  416. NDR64_FORMAT_CHAR FormatChar,
  417. EXPR_VALUE Value)
  418. {
  419. switch ( FormatChar )
  420. {
  421. case FC64_UINT8:
  422. return (EXPR_VALUE)(unsigned char)Value;
  423. case FC64_INT8:
  424. case FC64_CHAR:
  425. return (EXPR_VALUE)(signed char)Value;
  426. case FC64_UINT16:
  427. case FC64_WCHAR:
  428. return (EXPR_VALUE)(unsigned short)Value;
  429. case FC64_INT16:
  430. return (EXPR_VALUE)(signed short)Value;
  431. case FC64_UINT32:
  432. return (EXPR_VALUE)(unsigned long)Value;
  433. case FC64_INT32:
  434. return (EXPR_VALUE)(signed long)Value;
  435. case FC64_UINT64:
  436. return (EXPR_VALUE)(NDR64_UINT64)Value;
  437. case FC64_INT64:
  438. return (EXPR_VALUE)(NDR64_INT64)Value;
  439. case FC64_POINTER:
  440. return (EXPR_VALUE)(void *)Value;
  441. default:
  442. NDR_ASSERT(0,"Ndr64pCastExprValueToExprValue : Illegal type.");
  443. RpcRaiseException( RPC_S_INTERNAL_ERROR );
  444. return 0;
  445. }
  446. }