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.

344 lines
6.5 KiB

  1. /*
  2. * hndtrans.c - Handle translation module.
  3. */
  4. /* Headers
  5. **********/
  6. #include "project.h"
  7. #pragma hdrstop
  8. #include "sortsrch.h"
  9. /* Macros
  10. *********/
  11. #define ARRAY_ELEMENT(pht, ai) ((((PHANDLETRANS)(hht))->hpHandlePairs)[(ai)])
  12. /* Types
  13. ********/
  14. /* handle translation unit */
  15. typedef struct _handlepair
  16. {
  17. HGENERIC hgenOld;
  18. HGENERIC hgenNew;
  19. }
  20. HANDLEPAIR;
  21. DECLARE_STANDARD_TYPES(HANDLEPAIR);
  22. /* handle translation structure */
  23. typedef struct _handletrans
  24. {
  25. /* pointer to array of handle translation units */
  26. HANDLEPAIR *hpHandlePairs;
  27. /* number of handle pairs in array */
  28. LONG lcTotalHandlePairs;
  29. /* number of used handle pairs in array */
  30. LONG lcUsedHandlePairs;
  31. }
  32. HANDLETRANS;
  33. DECLARE_STANDARD_TYPES(HANDLETRANS);
  34. /***************************** Private Functions *****************************/
  35. /* Module Prototypes
  36. ********************/
  37. PRIVATE_CODE COMPARISONRESULT CompareHandlePairs(PCVOID, PCVOID);
  38. #ifdef VSTF
  39. PRIVATE_CODE BOOL IsValidPCHANDLETRANS(PCHANDLETRANS);
  40. PRIVATE_CODE BOOL IsValidPCHANDLEPAIR(PCHANDLEPAIR);
  41. #endif
  42. /*
  43. ** CompareHandlePairs()
  44. **
  45. **
  46. **
  47. ** Arguments:
  48. **
  49. ** Returns: TWINRESULT
  50. **
  51. ** Side Effects: none
  52. */
  53. PRIVATE_CODE COMPARISONRESULT CompareHandlePairs(PCVOID pchp1, PCVOID pchp2)
  54. {
  55. COMPARISONRESULT cr;
  56. ASSERT(IS_VALID_STRUCT_PTR(pchp1, CHANDLEPAIR));
  57. ASSERT(IS_VALID_STRUCT_PTR(pchp2, CHANDLEPAIR));
  58. if (((PHANDLEPAIR)pchp1)->hgenOld < ((PHANDLEPAIR)pchp2)->hgenOld)
  59. cr = CR_FIRST_SMALLER;
  60. else if (((PHANDLEPAIR)pchp1)->hgenOld > ((PHANDLEPAIR)pchp2)->hgenOld)
  61. cr = CR_FIRST_LARGER;
  62. else
  63. cr = CR_EQUAL;
  64. return(cr);
  65. }
  66. #ifdef VSTF
  67. /*
  68. ** IsValidPCHANDLETRANS()
  69. **
  70. **
  71. **
  72. ** Arguments:
  73. **
  74. ** Returns:
  75. **
  76. ** Side Effects: none
  77. */
  78. PRIVATE_CODE BOOL IsValidPCHANDLETRANS(PCHANDLETRANS pcht)
  79. {
  80. BOOL bResult;
  81. if (IS_VALID_READ_PTR(pcht, CHANDLETRANS) &&
  82. EVAL(pcht->lcTotalHandlePairs >= 0) &&
  83. (EVAL(pcht->lcUsedHandlePairs >= 0) &&
  84. EVAL(pcht->lcUsedHandlePairs <= pcht->lcTotalHandlePairs)) &&
  85. IS_VALID_READ_BUFFER_PTR(pcht->hpHandlePairs, HANDLEPAIR, (UINT)(pcht->lcTotalHandlePairs)))
  86. bResult = TRUE;
  87. else
  88. bResult = FALSE;
  89. return(bResult);
  90. }
  91. /*
  92. ** IsValidPCHANDLEPAIR()
  93. **
  94. **
  95. **
  96. ** Arguments:
  97. **
  98. ** Returns:
  99. **
  100. ** Side Effects: none
  101. */
  102. PRIVATE_CODE BOOL IsValidPCHANDLEPAIR(PCHANDLEPAIR pchp)
  103. {
  104. return(IS_VALID_READ_PTR(pchp, CHANDLEPAIR));
  105. }
  106. #endif
  107. /****************************** Public Functions *****************************/
  108. /*
  109. ** CreateHandleTranslator()
  110. **
  111. **
  112. **
  113. ** Arguments:
  114. **
  115. ** Returns: TWINRESULT
  116. **
  117. ** Side Effects: none
  118. */
  119. PUBLIC_CODE BOOL CreateHandleTranslator(LONG lcHandles, PHHANDLETRANS phht)
  120. {
  121. PHANDLEPAIR hpHandlePairs;
  122. ASSERT(IS_VALID_WRITE_PTR(phht, HHANDLETRANS));
  123. *phht = NULL;
  124. #ifdef DBLCHECK
  125. ASSERT((double)sizeof(HANDLEPAIR) * (double)lcHandles <= DWORD_MAX);
  126. #endif
  127. if (AllocateMemory(sizeof(HANDLEPAIR) * lcHandles, &hpHandlePairs))
  128. {
  129. PHANDLETRANS phtNew;
  130. if (AllocateMemory(sizeof(*phtNew), &phtNew))
  131. {
  132. /* Success! Fill in HANDLETRANS fields. */
  133. phtNew->hpHandlePairs = hpHandlePairs;
  134. phtNew->lcTotalHandlePairs = lcHandles;
  135. phtNew->lcUsedHandlePairs = 0;
  136. *phht = (HHANDLETRANS)phtNew;
  137. ASSERT(IS_VALID_HANDLE(*phht, HANDLETRANS));
  138. }
  139. else
  140. FreeMemory(hpHandlePairs);
  141. }
  142. return(*phht != NULL);
  143. }
  144. /*
  145. ** DestroyHandleTranslator()
  146. **
  147. **
  148. **
  149. ** Arguments:
  150. **
  151. ** Returns: TWINRESULT
  152. **
  153. ** Side Effects: none
  154. */
  155. PUBLIC_CODE void DestroyHandleTranslator(HHANDLETRANS hht)
  156. {
  157. ASSERT(IS_VALID_HANDLE(hht, HANDLETRANS));
  158. ASSERT(((PHANDLETRANS)hht)->hpHandlePairs);
  159. FreeMemory(((PHANDLETRANS)hht)->hpHandlePairs);
  160. FreeMemory((PHANDLETRANS)hht);
  161. return;
  162. }
  163. /*
  164. ** AddHandleToHandleTranslator()
  165. **
  166. **
  167. **
  168. ** Arguments:
  169. **
  170. ** Returns: TWINRESULT
  171. **
  172. ** Side Effects: none
  173. */
  174. PUBLIC_CODE BOOL AddHandleToHandleTranslator(HHANDLETRANS hht,
  175. HGENERIC hgenOld,
  176. HGENERIC hgenNew)
  177. {
  178. BOOL bRet;
  179. ASSERT(IS_VALID_HANDLE(hht, HANDLETRANS));
  180. if (((PHANDLETRANS)hht)->lcUsedHandlePairs < ((PHANDLETRANS)hht)->lcTotalHandlePairs)
  181. {
  182. ARRAY_ELEMENT((PHANDLETRANS)hht, ((PHANDLETRANS)hht)->lcUsedHandlePairs).hgenOld = hgenOld;
  183. ARRAY_ELEMENT((PHANDLETRANS)hht, ((PHANDLETRANS)hht)->lcUsedHandlePairs).hgenNew = hgenNew;
  184. ((PHANDLETRANS)hht)->lcUsedHandlePairs++;
  185. bRet = TRUE;
  186. }
  187. else
  188. bRet = FALSE;
  189. return(bRet);
  190. }
  191. /*
  192. ** PrepareForHandleTranslation()
  193. **
  194. **
  195. **
  196. ** Arguments:
  197. **
  198. ** Returns: TWINRESULT
  199. **
  200. ** Side Effects: none
  201. */
  202. PUBLIC_CODE void PrepareForHandleTranslation(HHANDLETRANS hht)
  203. {
  204. HANDLEPAIR hpTemp;
  205. ASSERT(IS_VALID_HANDLE(hht, HANDLETRANS));
  206. HeapSort(((PHANDLETRANS)hht)->hpHandlePairs,
  207. ((PHANDLETRANS)hht)->lcUsedHandlePairs,
  208. sizeof((((PHANDLETRANS)hht)->hpHandlePairs)[0]),
  209. &CompareHandlePairs,
  210. &hpTemp);
  211. return;
  212. }
  213. /*
  214. ** TranslateHandle()
  215. **
  216. **
  217. **
  218. ** Arguments:
  219. **
  220. ** Returns: TWINRESULT
  221. **
  222. ** Side Effects: none
  223. */
  224. PUBLIC_CODE BOOL TranslateHandle(HHANDLETRANS hht, HGENERIC hgenOld,
  225. PHGENERIC phgenNew)
  226. {
  227. BOOL bFound;
  228. HANDLEPAIR hpTemp;
  229. LONG liTarget;
  230. ASSERT(IS_VALID_HANDLE(hht, HANDLETRANS));
  231. ASSERT(IS_VALID_WRITE_PTR(phgenNew, HGENERIC));
  232. hpTemp.hgenOld = hgenOld;
  233. bFound = BinarySearch(((PHANDLETRANS)hht)->hpHandlePairs,
  234. ((PHANDLETRANS)hht)->lcUsedHandlePairs,
  235. sizeof((((PHANDLETRANS)hht)->hpHandlePairs)[0]),
  236. &CompareHandlePairs,
  237. &hpTemp,
  238. &liTarget);
  239. if (bFound)
  240. {
  241. ASSERT(liTarget < ((PHANDLETRANS)hht)->lcUsedHandlePairs);
  242. *phgenNew = ARRAY_ELEMENT((PHANDLETRANS)hht, liTarget).hgenNew;
  243. }
  244. return(bFound);
  245. }
  246. #ifdef DEBUG
  247. /*
  248. ** IsValidHHANDLETRANS()
  249. **
  250. **
  251. **
  252. ** Arguments:
  253. **
  254. ** Returns: TWINRESULT
  255. **
  256. ** Side Effects: none
  257. */
  258. PUBLIC_CODE BOOL IsValidHHANDLETRANS(HHANDLETRANS hht)
  259. {
  260. return(IS_VALID_STRUCT_PTR((PHANDLETRANS)hht, CHANDLETRANS));
  261. }
  262. #endif