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.

186 lines
4.0 KiB

  1. #include "precomp.hxx"
  2. // Copied from nt\base\win32\client\thread.c
  3. HANDLE
  4. OSCompat_OpenThread(
  5. DWORD dwDesiredAccess,
  6. BOOL bInheritHandle,
  7. DWORD dwThreadId
  8. )
  9. {
  10. NTSTATUS Status;
  11. OBJECT_ATTRIBUTES Obja;
  12. HANDLE Handle;
  13. CLIENT_ID ClientId;
  14. ClientId.UniqueThread = (HANDLE)LongToHandle(dwThreadId);
  15. ClientId.UniqueProcess = (HANDLE)NULL;
  16. InitializeObjectAttributes(
  17. &Obja,
  18. NULL,
  19. (bInheritHandle ? OBJ_INHERIT : 0),
  20. NULL,
  21. NULL
  22. );
  23. Status = NtOpenThread(
  24. &Handle,
  25. (ACCESS_MASK)dwDesiredAccess,
  26. &Obja,
  27. &ClientId
  28. );
  29. if (NT_SUCCESS(Status))
  30. {
  31. return Handle;
  32. }
  33. else
  34. {
  35. SetLastError(Status);
  36. return NULL;
  37. }
  38. }
  39. // Copied from nt\base\ntos\rtl\bitmap.c
  40. static CONST ULONG FillMaskUlong[] = {
  41. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  42. 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
  43. 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
  44. 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
  45. 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
  46. 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
  47. 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
  48. 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
  49. 0xffffffff
  50. };
  51. ULONG
  52. OSCompat_RtlFindLastBackwardRunClear (
  53. IN PRTL_BITMAP BitMapHeader,
  54. IN ULONG FromIndex,
  55. IN PULONG StartingRunIndex
  56. )
  57. {
  58. ULONG Start;
  59. ULONG End;
  60. PULONG PHunk;
  61. ULONG Hunk;
  62. //
  63. // Take care of the boundary case of the null bitmap
  64. //
  65. if (BitMapHeader->SizeOfBitMap == 0) {
  66. *StartingRunIndex = FromIndex;
  67. return 0;
  68. }
  69. //
  70. // Scan backwards for the first clear bit
  71. //
  72. End = FromIndex;
  73. //
  74. // Build pointer to the ULONG word in the bitmap
  75. // containing the End bit, then read in the bitmap
  76. // hunk. Set the rest of the bits in this word, NOT
  77. // inclusive of the FromIndex bit.
  78. //
  79. PHunk = BitMapHeader->Buffer + (End / 32);
  80. Hunk = *PHunk | ~FillMaskUlong[(End % 32) + 1];
  81. //
  82. // If the first subword is set then we can proceed to
  83. // take big steps in the bitmap since we are now ULONG
  84. // aligned in the search
  85. //
  86. if (Hunk == (ULONG)~0) {
  87. //
  88. // Adjust the pointers backwards
  89. //
  90. End -= (End % 32) + 1;
  91. PHunk--;
  92. while ( PHunk > BitMapHeader->Buffer ) {
  93. //
  94. // Stop at first word with set bits
  95. //
  96. if (*PHunk != (ULONG)~0) break;
  97. PHunk--;
  98. End -= 32;
  99. }
  100. }
  101. //
  102. // Bitwise search backward for the clear bit
  103. //
  104. while ((End != MAXULONG) && (RtlCheckBit( BitMapHeader, End ) == 1)) { End -= 1; }
  105. //
  106. // Scan backwards for the first set bit
  107. //
  108. Start = End;
  109. //
  110. // We know that the clear bit was in the last word we looked at,
  111. // so continue from there to find the next set bit, clearing the
  112. // previous bits in the word.
  113. //
  114. Hunk = *PHunk & FillMaskUlong[Start % 32];
  115. //
  116. // If the subword is unset then we can proceed in big steps
  117. //
  118. if (Hunk == (ULONG)0) {
  119. //
  120. // Adjust the pointers backward
  121. //
  122. Start -= (Start % 32) + 1;
  123. PHunk--;
  124. while ( PHunk > BitMapHeader->Buffer ) {
  125. //
  126. // Stop at first word with set bits
  127. //
  128. if (*PHunk != (ULONG)0) break;
  129. PHunk--;
  130. Start -= 32;
  131. }
  132. }
  133. //
  134. // Bitwise search backward for the set bit
  135. //
  136. while ((Start != MAXULONG) && (RtlCheckBit( BitMapHeader, Start ) == 0)) { Start -= 1; }
  137. //
  138. // Compute the index and return the length
  139. //
  140. *StartingRunIndex = Start + 1;
  141. return (End - Start);
  142. }