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.

275 lines
5.2 KiB

  1. #include "brian.h"
  2. VOID
  3. DisplayHandle (
  4. IN USHORT Index
  5. );
  6. VOID
  7. InitHandles (
  8. )
  9. {
  10. NtCreateEvent( &HandleEvent, SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
  11. NULL, SynchronizationEvent, TRUE );
  12. RtlZeroMemory( Handles, sizeof( Handles ));
  13. }
  14. VOID
  15. UninitHandles (
  16. )
  17. {
  18. USHORT Index;
  19. //
  20. // Close any handles.
  21. //
  22. for (Index = 0; Index < MAX_HANDLES; Index++) {
  23. if (Handles[Index].Used) {
  24. NtClose( Handles[Index].Handle );
  25. }
  26. }
  27. }
  28. NTSTATUS
  29. ObtainIndex (
  30. OUT PUSHORT NewIndex
  31. )
  32. {
  33. NTSTATUS Status;
  34. USHORT Index;
  35. //
  36. // Wait for the handle event
  37. //
  38. if ((Status = NtWaitForSingleObject( HandleEvent,
  39. FALSE,
  40. NULL )) != STATUS_SUCCESS) {
  41. return Status;
  42. }
  43. //
  44. // Find an available index. Return STATUS_INSUFFICIENT_RESOURCES
  45. // if not found.
  46. //
  47. for (Index = 0; Index < MAX_HANDLES; Index++) {
  48. if (!Handles[Index].Used) {
  49. break;
  50. }
  51. }
  52. if (Index >= MAX_HANDLES) {
  53. Status = STATUS_INSUFFICIENT_RESOURCES;
  54. //
  55. // Otherwise reserve this handle index.
  56. //
  57. } else {
  58. Handles[Index].Used = TRUE;
  59. *NewIndex = Index;
  60. Status = STATUS_SUCCESS;
  61. }
  62. NtSetEvent( HandleEvent, NULL );
  63. return Status;
  64. }
  65. NTSTATUS
  66. FreeIndex (
  67. IN USHORT Index
  68. )
  69. {
  70. NTSTATUS Status = STATUS_SUCCESS;
  71. //
  72. // Return immediately if beyond the end of the valid handles.
  73. //
  74. if (Index >= MAX_HANDLES) {
  75. return Status;
  76. }
  77. //
  78. // Grab the event for the handles.
  79. //
  80. if ((Status = NtWaitForSingleObject( HandleEvent, FALSE, NULL )) != STATUS_SUCCESS) {
  81. return Status;
  82. }
  83. //
  84. // Mark the index as unused and close the file object if present.
  85. //
  86. if (Handles[Index].Handle) {
  87. Status = NtClose( Handles[Index].Handle );
  88. }
  89. if (Handles[Index].Used) {
  90. Handles[Index].Used = FALSE;
  91. }
  92. NtSetEvent( HandleEvent, NULL );
  93. return Status;
  94. }
  95. VOID
  96. InputDisplayHandle (
  97. IN PCHAR ParamBuffer
  98. )
  99. {
  100. ULONG Index;
  101. BOOLEAN LastInput;
  102. BOOLEAN ParmSpecified;
  103. Index = 0;
  104. ParmSpecified = FALSE;
  105. LastInput = TRUE;
  106. //
  107. // While there is more input, analyze the parameter and update the
  108. // query flags.
  109. //
  110. while (TRUE) {
  111. ULONG DummyCount;
  112. //
  113. // Swallow leading white spaces.
  114. //
  115. ParamBuffer = SwallowWhite( ParamBuffer, &DummyCount );
  116. if (*ParamBuffer) {
  117. //
  118. // If the next parameter is legal then check the paramter value.
  119. // Update the parameter value.
  120. //
  121. if ((*ParamBuffer == '-'
  122. || *ParamBuffer == '/')
  123. && (ParamBuffer++, *ParamBuffer != '\0')) {
  124. //
  125. // Switch on the next character.
  126. //
  127. switch( *ParamBuffer ) {
  128. //
  129. // Check buffer index.
  130. //
  131. case 'i' :
  132. case 'I' :
  133. //
  134. // Move to the next character, as long as there
  135. // are no white spaces continue analyzing letters.
  136. // On the first bad letter, skip to the next
  137. // parameter.
  138. //
  139. ParamBuffer++;
  140. Index = AsciiToInteger( ParamBuffer );
  141. ParamBuffer = SwallowNonWhite( ParamBuffer, &DummyCount );
  142. ParmSpecified = TRUE;
  143. break;
  144. default :
  145. //
  146. // Swallow to the next white space and continue the
  147. // loop.
  148. //
  149. ParamBuffer = SwallowNonWhite( ParamBuffer, &DummyCount );
  150. }
  151. }
  152. //
  153. // Else the text is invalid, skip the entire block.
  154. //
  155. //
  156. //
  157. // Else if there is no input then exit.
  158. //
  159. } else if ( LastInput ) {
  160. break;
  161. //
  162. // Else try to read another line for open parameters.
  163. //
  164. } else {
  165. }
  166. }
  167. //
  168. // If no parameters were received then display the syntax message.
  169. //
  170. if (!ParmSpecified) {
  171. printf( "\n Usage: di -i<digits> \n" );
  172. printf( "\n -i<digits> Handle index" );
  173. printf( "\n\n" );
  174. //
  175. // Else call our copy buffer routine.
  176. //
  177. } else {
  178. DisplayHandle( (USHORT) Index );
  179. }
  180. return;
  181. }
  182. VOID
  183. DisplayHandle (
  184. IN USHORT Index
  185. )
  186. {
  187. printf( "\n" );
  188. printf( "\nIndex -> %04x, Handle -> %p, Used -> %04x",
  189. Index,
  190. Handles[Index].Handle,
  191. Handles[Index].Used );
  192. printf( "\n" );
  193. return;
  194. }