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.

250 lines
6.1 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. prefxsup.c
  5. Abstract:
  6. This module implements the mailslot prefix support routines
  7. Author:
  8. Manny Weiser (mannyw) 10-Jan-1991
  9. Revision History:
  10. --*/
  11. #include "mailslot.h"
  12. //
  13. // The debug trace level for this module
  14. //
  15. #define Dbg (DEBUG_TRACE_PREFXSUP)
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text( PAGE, MsFindPrefix )
  18. #pragma alloc_text( PAGE, MsFindRelativePrefix )
  19. #endif
  20. PFCB
  21. MsFindPrefix (
  22. IN PVCB Vcb,
  23. IN PUNICODE_STRING String,
  24. IN BOOLEAN CaseInsensitive,
  25. OUT PUNICODE_STRING RemainingPart
  26. )
  27. /*++
  28. Routine Description:
  29. This routine searches the FCBs/DCBs of a volume and locates the
  30. FCB/DCB with longest matching prefix for the given input string. The
  31. search is relative to the root of the volume. So all names must start
  32. with a "\".
  33. Arguments:
  34. Vcb - Supplies the Vcb to search
  35. String - Supplies the input string to search for
  36. CaseInsensitive - Specifies if the search is to be done case sensitive
  37. (FALSE) or insensitive (TRUE)
  38. RemainingPart - Returns the string when the prefix no longer matches.
  39. For example, if the input string is "\alpha\beta" only matches the
  40. root directory then the remaining string is "alpha\beta". If the
  41. same string matches a DCB for "\alpha" then the remaining string is
  42. "beta".
  43. Return Value:
  44. PFCB - Returns a pointer to either an FCB or a DCB whichever is the
  45. longest matching prefix.
  46. --*/
  47. {
  48. PUNICODE_PREFIX_TABLE_ENTRY prefixTableEntry;
  49. PFCB fcb;
  50. PAGED_CODE();
  51. DebugTrace(+1, Dbg, "MsFindPrefix, Vcb = %08lx\n", (ULONG)Vcb);
  52. DebugTrace( 0, Dbg, " String = %wZ\n", (ULONG)String);
  53. //
  54. // Find the longest matching prefix. Make sure we hold the VCB lock here
  55. //
  56. ASSERT (MsIsAcquiredExclusiveVcb(Vcb));
  57. prefixTableEntry = RtlFindUnicodePrefix( &Vcb->PrefixTable,
  58. String,
  59. CaseInsensitive );
  60. //
  61. // If we didn't find one then it's an error.
  62. //
  63. if (prefixTableEntry == NULL) {
  64. DebugDump("Error looking up a prefix", 0, Vcb);
  65. KeBugCheck( MAILSLOT_FILE_SYSTEM );
  66. }
  67. //
  68. // Get a pointer to the FCB containing the prefix table entry.
  69. //
  70. fcb = CONTAINING_RECORD( prefixTableEntry, FCB, PrefixTableEntry );
  71. //
  72. // Tell the caller how many characters we were able to match. We first
  73. // set the remaining part to the original string minus the matched
  74. // prefix, then we check if the remaining part starts with a backslash
  75. // and if it does then we remove the backslash from the remaining string.
  76. //
  77. RemainingPart->Length = String->Length - fcb->FullFileName.Length;
  78. RemainingPart->MaximumLength = RemainingPart->Length;
  79. RemainingPart->Buffer = (PWCH)((PCHAR)String->Buffer + fcb->FullFileName.Length);
  80. if ((RemainingPart->Length > 0) &&
  81. (RemainingPart->Buffer[0] == L'\\')) {
  82. RemainingPart->Length -= sizeof( WCHAR );
  83. RemainingPart->MaximumLength -= sizeof( WCHAR );
  84. RemainingPart->Buffer += sizeof( WCHAR );
  85. }
  86. DebugTrace(0, Dbg, "RemainingPart set to %wZ\n", (ULONG)RemainingPart);
  87. //
  88. // Return to the caller.
  89. //
  90. DebugTrace(-1, Dbg, "MsFindPrefix -> %08lx\n", (ULONG)fcb);
  91. return fcb;
  92. }
  93. NTSTATUS
  94. MsFindRelativePrefix (
  95. IN PDCB Dcb,
  96. IN PUNICODE_STRING String,
  97. IN BOOLEAN CaseInsensitive,
  98. OUT PUNICODE_STRING RemainingPart,
  99. OUT PFCB *ppFcb
  100. )
  101. /*++
  102. Routine Description:
  103. This routine searches the FCBs/DCBs of a volume and locates the
  104. FCB/DCB with longest matching prefix for the given input string. The
  105. search is relative to a input DCB, and must not start with a leading "\"
  106. All searching is done case insensitive.
  107. Arguments:
  108. Dcb - Supplies the Dcb to start searching from
  109. String - Supplies the input string to search for
  110. CaseInsensitive - Specifies if the search is to be done case sensitive
  111. (FALSE) or insensitive (TRUE)
  112. RemainingPart - Returns the index into the string when the prefix no
  113. longer matches. For example, if the input string is "beta\gamma"
  114. and the input Dcb is for "\alpha" and we only match beta then
  115. the remaining string is "gamma".
  116. Return Value:
  117. PFCB - Returns a pointer to either an FCB or a DCB whichever is the
  118. longest matching prefix.
  119. --*/
  120. {
  121. USHORT nameLength;
  122. USHORT MaxLength;
  123. PWCH name;
  124. UNICODE_STRING fullString;
  125. PWCH temp;
  126. PFCB fcb;
  127. PAGED_CODE();
  128. DebugTrace(+1, Dbg, "MsFindRelativePrefix, Dcb = %08lx\n", (ULONG)Dcb);
  129. DebugTrace( 0, Dbg, "String = %08lx\n", (ULONG)String);
  130. ASSERT(NodeType(Dcb) == MSFS_NTC_ROOT_DCB);
  131. //
  132. // We first need to build the complete name and then do a relative
  133. // search from the root.
  134. //
  135. nameLength = String->Length;
  136. name = String->Buffer;
  137. MaxLength = nameLength + 2*sizeof(WCHAR);
  138. if (MaxLength < nameLength) {
  139. return STATUS_INVALID_PARAMETER;
  140. }
  141. temp = MsAllocatePagedPool( MaxLength, 'nFsM' );
  142. if (temp == NULL) {
  143. return STATUS_INSUFFICIENT_RESOURCES;
  144. }
  145. temp[0] = L'\\';
  146. RtlCopyMemory (&temp[1], name, nameLength);
  147. temp[(nameLength / sizeof(WCHAR)) + 1] = L'\0';
  148. fullString.Length = nameLength + sizeof (WCHAR);
  149. fullString.MaximumLength = MaxLength;
  150. fullString.Buffer = temp;
  151. //
  152. // Find the prefix relative to the volume.
  153. //
  154. fcb = MsFindPrefix( Dcb->Vcb,
  155. &fullString,
  156. CaseInsensitive,
  157. RemainingPart );
  158. //
  159. // Now adjust the remaining part to take care of the relative
  160. // volume prefix.
  161. //
  162. MsFreePool (temp);
  163. RemainingPart->Buffer = (PWCH)((PCH)String->Buffer + String->Length -
  164. RemainingPart->Length);
  165. DebugTrace(0, Dbg, "RemainingPart set to %wZ\n", (ULONG)RemainingPart);
  166. //
  167. // Return to the caller.
  168. //
  169. DebugTrace(-1, Dbg, "MsFindRelativePrefix -> %08lx\n", (ULONG)fcb);
  170. *ppFcb = fcb;
  171. return STATUS_SUCCESS;
  172. }