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.

246 lines
5.9 KiB

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