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.

1351 lines
36 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. DbcsName.c
  5. Abstract:
  6. The name support package is for manipulating DBCS strings. The routines
  7. allow the caller to dissect and compare strings.
  8. The following routines are provided by this package:
  9. o FsRtlIsFatDbcsLegal - This routine takes an input dbcs
  10. string and determines if it describes a legal name or path.
  11. o FsRtlIsHpfsDbcsLegal - This routine takes an input dbcs
  12. string and determines if it describes a legal name or path.
  13. o FsRtlDissectDbcs - This routine takes a path name string and
  14. breaks into two parts. The first name in the string and the
  15. remainder.
  16. o FsRtlDoesDbcsContainWildCards - This routines tells the caller if
  17. a string contains any wildcard characters.
  18. o FsRtlIsDbcsInExpression - This routine is used to compare a string
  19. against a template (possibly containing wildcards) to sees if the
  20. string is in the language denoted by the template.
  21. Author:
  22. Gary Kimura [GaryKi] 5-Feb-1990
  23. Revision History:
  24. --*/
  25. #include "FsRtlP.h"
  26. //
  27. // Trace level for the module
  28. //
  29. #define Dbg (0x10000000)
  30. //
  31. // Some special debugging code
  32. //
  33. #if DBG
  34. ULONG DaveDebug = 0;
  35. #define DavePrint if (DaveDebug) DbgPrint
  36. #else
  37. #define DavePrint NOTHING
  38. #endif
  39. //
  40. // Define a tag for general pool allocations from this module
  41. //
  42. #undef MODULE_POOL_TAG
  43. #define MODULE_POOL_TAG ('drSF')
  44. #ifdef ALLOC_PRAGMA
  45. #pragma alloc_text(PAGE, FsRtlDissectDbcs)
  46. #pragma alloc_text(PAGE, FsRtlDoesDbcsContainWildCards)
  47. #pragma alloc_text(PAGE, FsRtlIsDbcsInExpression)
  48. #pragma alloc_text(PAGE, FsRtlIsFatDbcsLegal)
  49. #pragma alloc_text(PAGE, FsRtlIsHpfsDbcsLegal)
  50. #endif
  51. BOOLEAN
  52. FsRtlIsFatDbcsLegal (
  53. IN ANSI_STRING DbcsName,
  54. IN BOOLEAN WildCardsPermissible,
  55. IN BOOLEAN PathNamePermissible,
  56. IN BOOLEAN LeadingBackslashPermissible
  57. )
  58. /*++
  59. Routine Description:
  60. This routine simple returns whether the specified file names conforms
  61. to the file system specific rules for legal file names. This routine
  62. will check the single name, or if PathNamePermissible is specified as
  63. TRUE, whether the whole path is a legal name.
  64. For FAT, the following rules apply:
  65. A. A Fat file name may not contain any of the following characters:
  66. 0x00-0x1F " / : | + , ; = [ ]
  67. B. A Fat file name is either of the form N.E or just N, where N is a
  68. string of 1-8 bytes and E is a string of 1-3 bytes conformant to
  69. rule A above. In addition, neither N nor E may contain a period
  70. character or end with a space character.
  71. Incidently, N corresponds to name and E to extension.
  72. Case: Lower case characters are taken as valid, but are up-shifted upon
  73. receipt, ie. Fat only deals with upper case file names.
  74. For example, the files ".foo", "foo.", and "foo .b" are illegal, while
  75. "foo. b" and " bar" are legal.
  76. Arguments:
  77. DbcsName - Supllies the name/path to check.
  78. WildCardsPermissible - Specifies if Nt wild card characters are to be
  79. considered considered legal.
  80. PathNamePermissible - Spcifes if Name may be a path name separated by
  81. backslash characters, or just a simple file name.
  82. LeadingBackSlashPermissible - Specifies if a single leading backslash
  83. is permissible in the file/path name.
  84. Return Value:
  85. BOOLEAN - TRUE if the name is legal, FALSE otherwise.
  86. --*/
  87. {
  88. BOOLEAN ExtensionPresent = FALSE;
  89. ULONG Index;
  90. UCHAR Char;
  91. PAGED_CODE();
  92. //
  93. // Empty names are not valid.
  94. //
  95. if ( DbcsName.Length == 0 ) { return FALSE; }
  96. //
  97. // If Wild Cards are OK, then for directory enumeration to work
  98. // correctly we have to accept . and ..
  99. //
  100. if ( WildCardsPermissible &&
  101. ( ( (DbcsName.Length == 1) &&
  102. ((DbcsName.Buffer[0] == '.') ||
  103. (DbcsName.Buffer[0] == ANSI_DOS_DOT)) )
  104. ||
  105. ( (DbcsName.Length == 2) &&
  106. ( ((DbcsName.Buffer[0] == '.') &&
  107. (DbcsName.Buffer[1] == '.')) ||
  108. ((DbcsName.Buffer[0] == ANSI_DOS_DOT) &&
  109. (DbcsName.Buffer[1] == ANSI_DOS_DOT)) ) ) ) ) {
  110. return TRUE;
  111. }
  112. //
  113. // If a leading \ is OK, skip over it (if there's more)
  114. //
  115. if ( DbcsName.Buffer[0] == '\\' ) {
  116. if ( LeadingBackslashPermissible ) {
  117. if ( (DbcsName.Length > 1) ) {
  118. DbcsName.Buffer += 1;
  119. DbcsName.Length -= 1;
  120. } else { return TRUE; }
  121. } else { return FALSE; }
  122. }
  123. //
  124. // If we got a path name, check each componant.
  125. //
  126. if ( PathNamePermissible ) {
  127. ANSI_STRING FirstName;
  128. ANSI_STRING RemainingName;
  129. RemainingName = DbcsName;
  130. while ( RemainingName.Length != 0 ) {
  131. //
  132. // This will catch the case of an illegal double backslash.
  133. //
  134. if ( RemainingName.Buffer[0] == '\\' ) { return FALSE; }
  135. FsRtlDissectDbcs(RemainingName, &FirstName, &RemainingName);
  136. if ( !FsRtlIsFatDbcsLegal( FirstName,
  137. WildCardsPermissible,
  138. FALSE,
  139. FALSE) ) {
  140. return FALSE;
  141. }
  142. }
  143. //
  144. // All the componants were OK, so the path is OK.
  145. //
  146. return TRUE;
  147. }
  148. //
  149. // If this name contains wild cards, just check for invalid characters.
  150. //
  151. if ( WildCardsPermissible && FsRtlDoesDbcsContainWildCards(&DbcsName) ) {
  152. for ( Index = 0; Index < DbcsName.Length; Index += 1 ) {
  153. Char = DbcsName.Buffer[ Index ];
  154. //
  155. // Skip over any Dbcs chacters
  156. //
  157. if ( FsRtlIsLeadDbcsCharacter( Char ) ) {
  158. ASSERT( Index != (ULONG)(DbcsName.Length - 1) );
  159. Index += 1;
  160. continue;
  161. }
  162. //
  163. // Make sure this character is legal, and if a wild card, that
  164. // wild cards are permissible.
  165. //
  166. if ( !FsRtlIsAnsiCharacterLegalFat(Char, WildCardsPermissible) ) {
  167. return FALSE;
  168. }
  169. }
  170. return TRUE;
  171. }
  172. //
  173. // At this point we should only have a single name, which can't have
  174. // more than 12 characters (including a single period)
  175. //
  176. if ( DbcsName.Length > 12 ) { return FALSE; }
  177. for ( Index = 0; Index < DbcsName.Length; Index += 1 ) {
  178. Char = DbcsName.Buffer[ Index ];
  179. //
  180. // Skip over and Dbcs chacters
  181. //
  182. if ( FsRtlIsLeadDbcsCharacter( Char ) ) {
  183. //
  184. // FsRtlIsFatDbcsLegal(): fat name part and extension part dbcs check
  185. //
  186. // 1) if we're looking at base part ( !ExtensionPresent ) and the 8th byte
  187. // is in the dbcs leading byte range, it's error ( Index == 7 ). If the
  188. // length of base part is more than 8 ( Index > 7 ), it's definitely error.
  189. //
  190. // 2) if the last byte ( Index == DbcsName.Length - 1 ) is in the dbcs leading
  191. // byte range, it's error
  192. //
  193. if ( (!ExtensionPresent && (Index >= 7)) ||
  194. ( Index == (ULONG)(DbcsName.Length - 1) ) ) {
  195. return FALSE;
  196. }
  197. Index += 1;
  198. continue;
  199. }
  200. //
  201. // Make sure this character is legal, and if a wild card, that
  202. // wild cards are permissible.
  203. //
  204. if ( !FsRtlIsAnsiCharacterLegalFat(Char, WildCardsPermissible) ) {
  205. return FALSE;
  206. }
  207. if ( (Char == '.') || (Char == ANSI_DOS_DOT) ) {
  208. //
  209. // We stepped onto a period. We require the following things:
  210. //
  211. // - It can't be the first character
  212. // - There can only be one
  213. // - There can't be more than three characters following
  214. // - The previous character can't be a space.
  215. //
  216. if ( (Index == 0) ||
  217. ExtensionPresent ||
  218. (DbcsName.Length - (Index + 1) > 3) ||
  219. (DbcsName.Buffer[Index - 1] == ' ') ) {
  220. return FALSE;
  221. }
  222. ExtensionPresent = TRUE;
  223. }
  224. //
  225. // The base part of the name can't be more than 8 characters long.
  226. //
  227. if ( (Index >= 8) && !ExtensionPresent ) { return FALSE; }
  228. }
  229. //
  230. // The name cannot end in a space or a period.
  231. //
  232. if ( (Char == ' ') || (Char == '.') || (Char == ANSI_DOS_DOT)) { return FALSE; }
  233. return TRUE;
  234. }
  235. BOOLEAN
  236. FsRtlIsHpfsDbcsLegal (
  237. IN ANSI_STRING DbcsName,
  238. IN BOOLEAN WildCardsPermissible,
  239. IN BOOLEAN PathNamePermissible,
  240. IN BOOLEAN LeadingBackslashPermissible
  241. )
  242. /*++
  243. Routine Description:
  244. This routine simple returns whether the specified file names conforms
  245. to the file system specific rules for legal file names. This routine
  246. will check the single name, or if PathNamePermissible is specified as
  247. TRUE, whether the whole path is a legal name.
  248. For HPFS, the following rules apply:
  249. A. An HPFS file name may not contain any of the following characters:
  250. 0x0000 - 0x001F " / : < > ? | *
  251. B. An HPFS file name may not end in a period or a space.
  252. C. An HPFS file name must contain no more than 255 bytes.
  253. Case: HPFS is case preserving, but not case sensitive. Case is
  254. preserved on creates, but not checked for on file name compares.
  255. For example, the files "foo " and "foo." are illegal, while ".foo",
  256. " foo" and "foo.bar.foo" are legal.
  257. Arguments:
  258. DbcsName - Supllies the name/path to check.
  259. WildCardsPermissible - Specifies if Nt wild card characters are to be
  260. considered considered legal.
  261. PathNamePermissible - Spcifes if Name may be a path name separated by
  262. backslash characters, or just a simple file name.
  263. LeadingBackSlashPermissible - Specifies if a single leading backslash
  264. is permissible in the file/path name.
  265. Return Value:
  266. BOOLEAN - TRUE if the name is legal, FALSE otherwise.
  267. --*/
  268. {
  269. BOOLEAN ExtensionPresent = FALSE;
  270. ULONG Index;
  271. UCHAR Char;
  272. PAGED_CODE();
  273. //
  274. // Empty names are not valid.
  275. //
  276. if ( DbcsName.Length == 0 ) { return FALSE; }
  277. //
  278. // If Wild Cards are OK, then for directory enumeration to work
  279. // correctly we have to accept . and ..
  280. //
  281. if ( WildCardsPermissible &&
  282. ( ( (DbcsName.Length == 1) &&
  283. ((DbcsName.Buffer[0] == '.') ||
  284. (DbcsName.Buffer[0] == ANSI_DOS_DOT)) )
  285. ||
  286. ( (DbcsName.Length == 2) &&
  287. ( ((DbcsName.Buffer[0] == '.') &&
  288. (DbcsName.Buffer[1] == '.')) ||
  289. ((DbcsName.Buffer[0] == ANSI_DOS_DOT) &&
  290. (DbcsName.Buffer[1] == ANSI_DOS_DOT)) ) ) ) ) {
  291. return TRUE;
  292. }
  293. //
  294. // If a leading \ is OK, skip over it (if there's more)
  295. //
  296. if ( DbcsName.Buffer[0] == '\\' ) {
  297. if ( LeadingBackslashPermissible ) {
  298. if ( (DbcsName.Length > 1) ) {
  299. DbcsName.Buffer += 1;
  300. DbcsName.Length -= 1;
  301. } else { return TRUE; }
  302. } else { return FALSE; }
  303. }
  304. //
  305. // If we got a path name, check each componant.
  306. //
  307. if ( PathNamePermissible ) {
  308. ANSI_STRING FirstName;
  309. ANSI_STRING RemainingName;
  310. RemainingName = DbcsName;
  311. while ( RemainingName.Length != 0 ) {
  312. //
  313. // This will catch the case of an illegal double backslash.
  314. //
  315. if ( RemainingName.Buffer[0] == '\\' ) { return FALSE; }
  316. FsRtlDissectDbcs(RemainingName, &FirstName, &RemainingName);
  317. if ( !FsRtlIsHpfsDbcsLegal( FirstName,
  318. WildCardsPermissible,
  319. FALSE,
  320. FALSE) ) {
  321. return FALSE;
  322. }
  323. }
  324. //
  325. // All the componants were OK, so the path is OK.
  326. //
  327. return TRUE;
  328. }
  329. //
  330. // At this point we should only have a single name, which can't have
  331. // more than 255 characters
  332. //
  333. if ( DbcsName.Length > 255 ) { return FALSE; }
  334. for ( Index = 0; Index < DbcsName.Length; Index += 1 ) {
  335. Char = DbcsName.Buffer[ Index ];
  336. //
  337. // Skip over and Dbcs chacters
  338. //
  339. if ( FsRtlIsLeadDbcsCharacter( Char ) ) {
  340. //
  341. // FsRtlIsHpfsDbcsLegal () hpfs dbcs check
  342. //
  343. // If the last byte ( Index == DbcsName.Length - 1 ) is in the
  344. // dbcs leading byte range, it's error.
  345. //
  346. if ( Index == (ULONG)(DbcsName.Length - 1) ) {
  347. return FALSE;
  348. }
  349. Index += 1;
  350. continue;
  351. }
  352. //
  353. // Make sure this character is legal, and if a wild card, that
  354. // wild cards are permissible.
  355. //
  356. if ( !FsRtlIsAnsiCharacterLegalHpfs(Char, WildCardsPermissible) ) {
  357. return FALSE;
  358. }
  359. }
  360. //
  361. // The name cannot end in a space or a period.
  362. //
  363. if ( (Char == ' ') || (Char == '.') || (Char == ANSI_DOS_DOT) ) {
  364. return FALSE;
  365. }
  366. return TRUE;
  367. }
  368. VOID
  369. FsRtlDissectDbcs (
  370. IN ANSI_STRING Path,
  371. OUT PANSI_STRING FirstName,
  372. OUT PANSI_STRING RemainingName
  373. )
  374. /*++
  375. Routine Description:
  376. This routine takes an input Dbcs string and dissects it into two
  377. substrings. The first output string contains the name that appears at
  378. the beginning of the input string, the second output string contains the
  379. remainder of the input string.
  380. In the input string backslashes are used to separate names. The input
  381. string must not start with a backslash. Both output strings will not
  382. begin with a backslash.
  383. If the input string does not contain any names then both output strings
  384. are empty. If the input string contains only one name then the first
  385. output string contains the name and the second string is empty.
  386. Note that both output strings use the same string buffer memory of the
  387. input string.
  388. Example of its results are:
  389. //. . InputString FirstPart RemainingPart
  390. //
  391. //. . empty empty empty
  392. //
  393. //. . A A empty
  394. //
  395. //. . A\B\C\D\E A B\C\D\E
  396. //
  397. //. . *A? *A? empty
  398. //
  399. //. . \A A empty
  400. //
  401. //. . A[,] A[,] empty
  402. //
  403. //. . A\\B+;\C A \B+;\C
  404. Arguments:
  405. InputName - Supplies the input string being dissected
  406. Is8dot3 - Indicates if the first part of the input name must be 8.3
  407. or can be long file name.
  408. FirstPart - Receives the first name in the input string
  409. RemainingPart - Receives the remaining part of the input string
  410. Return Value:
  411. NONE
  412. --*/
  413. {
  414. ULONG i = 0;
  415. ULONG PathLength;
  416. ULONG FirstNameStart;
  417. PAGED_CODE();
  418. //
  419. // Make both output strings empty for now
  420. //
  421. FirstName->Length = 0;
  422. FirstName->MaximumLength = 0;
  423. FirstName->Buffer = NULL;
  424. RemainingName->Length = 0;
  425. RemainingName->MaximumLength = 0;
  426. RemainingName->Buffer = NULL;
  427. PathLength = Path.Length;
  428. //
  429. // Check for an empty input string
  430. //
  431. if (PathLength == 0) {
  432. return;
  433. }
  434. //
  435. // Skip over a starting backslash, and make sure there is more.
  436. //
  437. if ( Path.Buffer[0] == '\\' ) {
  438. i = 1;
  439. }
  440. //
  441. // Now run down the input string until we hit a backslash or the end
  442. // of the string, remembering where we started;
  443. //
  444. for ( FirstNameStart = i;
  445. (i < PathLength) && (Path.Buffer[i] != '\\');
  446. i += 1 ) {
  447. //
  448. // If this is the first byte of a Dbcs character, skip over the
  449. // next byte as well.
  450. //
  451. if ( FsRtlIsLeadDbcsCharacter( Path.Buffer[i] ) ) {
  452. i += 1;
  453. }
  454. }
  455. //
  456. // At this point all characters up to (but not including) i are
  457. // in the first part. So setup the first name
  458. //
  459. FirstName->Length = (USHORT)(i - FirstNameStart);
  460. FirstName->MaximumLength = FirstName->Length;
  461. FirstName->Buffer = &Path.Buffer[FirstNameStart];
  462. //
  463. // Now the remaining part needs a string only if the first part didn't
  464. // exhaust the entire input string. We know that if anything is left
  465. // that is must start with a backslash. Note that if there is only
  466. // a trailing backslash, the length will get correctly set to zero.
  467. //
  468. if (i < PathLength) {
  469. RemainingName->Length = (USHORT)(PathLength - (i + 1));
  470. RemainingName->MaximumLength = RemainingName->Length;
  471. RemainingName->Buffer = &Path.Buffer[i + 1];
  472. }
  473. //
  474. // And return to our caller
  475. //
  476. return;
  477. }
  478. BOOLEAN
  479. FsRtlDoesDbcsContainWildCards (
  480. IN PANSI_STRING Name
  481. )
  482. /*++
  483. Routine Description:
  484. This routine checks if the input Dbcs name contains any wild card
  485. characters (i.e., *, ?, ANSI_DOS_STAR, or ANSI_DOS_QM).
  486. Arguments:
  487. Name - Supplies the name to examine
  488. Return Value:
  489. BOOLEAN - TRUE if the input name contains any wildcard characters and
  490. FALSE otherwise.
  491. --*/
  492. {
  493. CLONG i;
  494. PAGED_CODE();
  495. //
  496. // Check each character in the name to see if it's a wildcard
  497. // character
  498. //
  499. for (i = 0; i < Name->Length; i += 1) {
  500. //
  501. // check for dbcs character because we'll just skip over those
  502. //
  503. if (FsRtlIsLeadDbcsCharacter( Name->Buffer[i] )) {
  504. i += 1;
  505. //
  506. // else check for a wild card character
  507. //
  508. } else if (FsRtlIsAnsiCharacterWild( Name->Buffer[i] )) {
  509. //
  510. // Tell caller that this name contains wild cards
  511. //
  512. return TRUE;
  513. }
  514. }
  515. //
  516. // No wildcard characters were found, so return to our caller
  517. //
  518. return FALSE;
  519. }
  520. #define GetDbcs(BUF,OFFSET,DBCS_CHAR,LENGTH) { \
  521. if (FsRtlIsLeadDbcsCharacter( (BUF)[(OFFSET)] )) { \
  522. *(DBCS_CHAR) = (WCHAR)((BUF)[(OFFSET)] + \
  523. 0x100 * (BUF)[(OFFSET) + 1]); \
  524. *(LENGTH) = 2; \
  525. } else { \
  526. *(DBCS_CHAR) = (WCHAR)(BUF)[(OFFSET)]; \
  527. *(LENGTH) = 1; \
  528. } \
  529. }
  530. #define MATCHES_ARRAY_SIZE 16
  531. BOOLEAN
  532. FsRtlIsDbcsInExpression (
  533. IN PANSI_STRING Expression,
  534. IN PANSI_STRING Name
  535. )
  536. /*++
  537. Routine Description:
  538. This routine compares a Dbcs name and an expression and tells the caller
  539. if the name is in the language defined by the expression. The input name
  540. cannot contain wildcards, while the expression may contain wildcards.
  541. Expression wild cards are evaluated as shown in the nondeterministic
  542. finite automatons below. Note that ~* and ~? are DOS_STAR and DOS_QM.
  543. ~* is DOS_STAR, ~? is DOS_QM, and ~. is DOS_DOT
  544. S
  545. <-----<
  546. X | | e Y
  547. X * Y == (0)----->-(1)->-----(2)-----(3)
  548. S-.
  549. <-----<
  550. X | | e Y
  551. X ~* Y == (0)----->-(1)->-----(2)-----(3)
  552. X S S Y
  553. X ?? Y == (0)---(1)---(2)---(3)---(4)
  554. X . . Y
  555. X ~.~. Y == (0)---(1)----(2)------(3)---(4)
  556. | |________|
  557. | ^ |
  558. |_______________|
  559. ^EOF or .^
  560. X S-. S-. Y
  561. X ~?~? Y == (0)---(1)-----(2)-----(3)---(4)
  562. | |________|
  563. | ^ |
  564. |_______________|
  565. ^EOF or .^
  566. where S is any single character
  567. S-. is any single character except the final .
  568. e is a null character transition
  569. EOF is the end of the name string
  570. In words:
  571. * matches 0 or more characters.
  572. ? matches exactly 1 character.
  573. DOS_STAR matches 0 or more characters until encountering and matching
  574. the final . in the name.
  575. DOS_QM matches any single character, or upon encountering a period or
  576. end of name string, advances the expression to the end of the
  577. set of contiguous DOS_QMs.
  578. DOS_DOT matches either a . or zero characters beyond name string.
  579. Arguments:
  580. Expression - Supplies the input expression to check against
  581. Name - Supplies the input name to check for.
  582. Return Value:
  583. BOOLEAN - TRUE if Name is an element in the set of strings denoted
  584. by the input Expression and FALSE otherwise.
  585. --*/
  586. {
  587. USHORT NameOffset;
  588. USHORT ExprOffset;
  589. USHORT Length;
  590. ULONG SrcCount;
  591. ULONG DestCount;
  592. ULONG PreviousDestCount;
  593. ULONG MatchesCount;
  594. WCHAR NameChar, ExprChar;
  595. USHORT LocalBuffer[MATCHES_ARRAY_SIZE * 2];
  596. USHORT *AuxBuffer = NULL;
  597. USHORT *PreviousMatches;
  598. USHORT *CurrentMatches;
  599. USHORT MaxState;
  600. USHORT CurrentState;
  601. BOOLEAN NameFinished = FALSE;
  602. //
  603. // The idea behind the algorithm is pretty simple. We keep track of
  604. // all possible locations in the regular expression that are matching
  605. // the name. If when the name has been exhausted one of the locations
  606. // in the expression is also just exhausted, the name is in the language
  607. // defined by the regular expression.
  608. //
  609. PAGED_CODE();
  610. DebugTrace(+1, Dbg, "FsRtlIsDbcsInExpression\n", 0);
  611. DebugTrace( 0, Dbg, " Expression = %Z\n", Expression );
  612. DebugTrace( 0, Dbg, " Name = %Z\n", Name );
  613. ASSERT( Name->Length != 0 );
  614. ASSERT( Expression->Length != 0 );
  615. //
  616. // If one string is empty return FALSE. If both are empty return TRUE.
  617. //
  618. if ( (Name->Length == 0) || (Expression->Length == 0) ) {
  619. return (BOOLEAN)(!(Name->Length + Expression->Length));
  620. }
  621. //
  622. // Special case by far the most common wild card search of *
  623. //
  624. if ((Expression->Length == 1) && (Expression->Buffer[0] == '*')) {
  625. return TRUE;
  626. }
  627. ASSERT( FsRtlDoesDbcsContainWildCards( Expression ) );
  628. //
  629. // Also special case expressions of the form *X. With this and the prior
  630. // case we have covered virtually all normal queries.
  631. //
  632. if (Expression->Buffer[0] == '*') {
  633. ANSI_STRING LocalExpression;
  634. LocalExpression = *Expression;
  635. LocalExpression.Buffer += 1;
  636. LocalExpression.Length -= 1;
  637. //
  638. // Only special case an expression with a single *
  639. //
  640. if ( !FsRtlDoesDbcsContainWildCards( &LocalExpression ) ) {
  641. ULONG StartingNameOffset;
  642. if (Name->Length < (USHORT)(Expression->Length - 1)) {
  643. return FALSE;
  644. }
  645. StartingNameOffset = Name->Length - LocalExpression.Length;
  646. //
  647. // FsRtlIsDbcsInExpression(): bug fix "expression[0] == *" case
  648. //
  649. // StatingNameOffset must not bisect DBCS characters.
  650. //
  651. if (NlsMbOemCodePageTag) {
  652. ULONG i = 0;
  653. while ( i < StartingNameOffset ) {
  654. i += FsRtlIsLeadDbcsCharacter( Name->Buffer[i] ) ? 2 : 1;
  655. }
  656. if ( i > StartingNameOffset ) {
  657. return FALSE;
  658. }
  659. }
  660. //
  661. // Do a simple memory compare if case sensitive, otherwise
  662. // we have got to check this one character at a time.
  663. //
  664. return (BOOLEAN) RtlEqualMemory( LocalExpression.Buffer,
  665. Name->Buffer + StartingNameOffset,
  666. LocalExpression.Length );
  667. }
  668. }
  669. //
  670. // Walk through the name string, picking off characters. We go one
  671. // character beyond the end because some wild cards are able to match
  672. // zero characters beyond the end of the string.
  673. //
  674. // With each new name character we determine a new set of states that
  675. // match the name so far. We use two arrays that we swap back and forth
  676. // for this purpose. One array lists the possible expression states for
  677. // all name characters up to but not including the current one, and other
  678. // array is used to build up the list of states considering the current
  679. // name character as well. The arrays are then switched and the process
  680. // repeated.
  681. //
  682. // There is not a one-to-one correspondence between state number and
  683. // offset into the expression. This is evident from the NFAs in the
  684. // initial comment to this function. State numbering is not continuous.
  685. // This allows a simple conversion between state number and expression
  686. // offset. Each character in the expression can represent one or two
  687. // states. * and DOS_STAR generate two states: ExprOffset*2 and
  688. // ExprOffset*2 + 1. All other expreesion characters can produce only
  689. // a single state. Thus ExprOffset = State/2.
  690. //
  691. //
  692. // Here is a short description of the variables involved:
  693. //
  694. // NameOffset - The offset of the current name char being processed.
  695. //
  696. // ExprOffset - The offset of the current expression char being processed.
  697. //
  698. // SrcCount - Prior match being investigated with current name char
  699. //
  700. // DestCount - Next location to put a matching assuming current name char
  701. //
  702. // NameFinished - Allows one more itteration through the Matches array
  703. // after the name is exhusted (to come *s for example)
  704. //
  705. // PreviousDestCount - This is used to prevent entry duplication, see coment
  706. //
  707. // PreviousMatches - Holds the previous set of matches (the Src array)
  708. //
  709. // CurrentMatches - Holds the current set of matches (the Dest array)
  710. //
  711. // AuxBuffer, LocalBuffer - the storage for the Matches arrays
  712. //
  713. //
  714. // Set up the initial variables
  715. //
  716. PreviousMatches = &LocalBuffer[0];
  717. CurrentMatches = &LocalBuffer[MATCHES_ARRAY_SIZE];
  718. PreviousMatches[0] = 0;
  719. MatchesCount = 1;
  720. NameOffset = 0;
  721. MaxState = (USHORT)(Expression->Length * 2);
  722. while ( !NameFinished ) {
  723. if ( NameOffset < Name->Length ) {
  724. GetDbcs( Name->Buffer, NameOffset, &NameChar, &Length );
  725. NameOffset += Length;
  726. } else {
  727. NameFinished = TRUE;
  728. //
  729. // if we have already exhasted the expression, cool. Don't
  730. // continue.
  731. //
  732. if ( PreviousMatches[MatchesCount-1] == MaxState ) {
  733. break;
  734. }
  735. }
  736. //
  737. // Now, for each of the previous stored expression matches, see what
  738. // we can do with this name character.
  739. //
  740. SrcCount = 0;
  741. DestCount = 0;
  742. PreviousDestCount = 0;
  743. while ( SrcCount < MatchesCount ) {
  744. //
  745. // We have to carry on our expression analysis as far as possible
  746. // for each character of name, so we loop here until the
  747. // expression stops matching. A clue here is that expression
  748. // cases that can match zero or more characters end with a
  749. // continue, while those that can accept only a single character
  750. // end with a break.
  751. //
  752. ExprOffset = (USHORT)((PreviousMatches[SrcCount++] + 1) / 2);
  753. Length = 0;
  754. while ( TRUE ) {
  755. if ( ExprOffset == Expression->Length ) {
  756. break;
  757. }
  758. //
  759. // The first time through the loop we don't want
  760. // to increment ExprOffset.
  761. //
  762. ExprOffset += Length;
  763. CurrentState = (USHORT)(ExprOffset * 2);
  764. if ( ExprOffset == Expression->Length ) {
  765. CurrentMatches[DestCount++] = MaxState;
  766. break;
  767. }
  768. GetDbcs(Expression->Buffer, ExprOffset, &ExprChar, &Length);
  769. ASSERT( !((ExprChar >= 'a') && (ExprChar <= 'z')) );
  770. //
  771. // Before we get started, we have to check for something
  772. // really gross. We may be about to exhaust the local
  773. // space for ExpressionMatches[][], so we have to allocate
  774. // some pool if this is the case. Yuk!
  775. //
  776. if ( (DestCount >= MATCHES_ARRAY_SIZE - 2) &&
  777. (AuxBuffer == NULL) ) {
  778. AuxBuffer = FsRtlpAllocatePool( PagedPool,
  779. (Expression->Length+1) *
  780. sizeof(USHORT)*2*2 );
  781. RtlCopyMemory( AuxBuffer,
  782. CurrentMatches,
  783. MATCHES_ARRAY_SIZE * sizeof(USHORT) );
  784. CurrentMatches = AuxBuffer;
  785. RtlCopyMemory( AuxBuffer + (Expression->Length+1)*2,
  786. PreviousMatches,
  787. MATCHES_ARRAY_SIZE * sizeof(USHORT) );
  788. PreviousMatches = AuxBuffer + (Expression->Length+1)*2;
  789. }
  790. //
  791. // * matches any character zero or more times.
  792. //
  793. if (ExprChar == '*') {
  794. CurrentMatches[DestCount++] = CurrentState;
  795. CurrentMatches[DestCount++] = CurrentState + 1;
  796. continue;
  797. }
  798. //
  799. // DOS_STAR matches any character except . zero or more times.
  800. //
  801. if (ExprChar == ANSI_DOS_STAR) {
  802. BOOLEAN ICanEatADot = FALSE;
  803. //
  804. // If we are at a period, determine if we are allowed to
  805. // consume it, ie. make sure it is not the last one.
  806. //
  807. if ( !NameFinished && (NameChar == '.') ) {
  808. WCHAR NameChar;
  809. USHORT Offset;
  810. USHORT Length;
  811. for ( Offset = NameOffset;
  812. Offset < Name->Length;
  813. Offset += Length ) {
  814. GetDbcs( Name->Buffer, Offset, &NameChar, &Length );
  815. if (NameChar == '.') {
  816. ICanEatADot = TRUE;
  817. break;
  818. }
  819. }
  820. }
  821. if (NameFinished || (NameChar != '.') || ICanEatADot) {
  822. CurrentMatches[DestCount++] = CurrentState;
  823. CurrentMatches[DestCount++] = CurrentState + 1;
  824. continue;
  825. } else {
  826. //
  827. // We are at a period. We can only match zero
  828. // characters (ie. the epsilon transition).
  829. //
  830. CurrentMatches[DestCount++] = CurrentState + 1;
  831. continue;
  832. }
  833. }
  834. //
  835. // The following expreesion characters all match by consuming
  836. // a character, thus force the expression, and thus state
  837. // forward.
  838. //
  839. CurrentState += (USHORT)(Length * 2);
  840. //
  841. // DOS_QM is the most complicated. If the name is finished,
  842. // we can match zero characters. If this name is a '.', we
  843. // don't match, but look at the next expression. Otherwise
  844. // we match a single character.
  845. //
  846. if ( ExprChar == ANSI_DOS_QM ) {
  847. if ( NameFinished || (NameChar == '.') ) {
  848. continue;
  849. }
  850. CurrentMatches[DestCount++] = CurrentState;
  851. break;
  852. }
  853. //
  854. // A DOS_DOT can match either a period, or zero characters
  855. // beyond the end of name.
  856. //
  857. if (ExprChar == DOS_DOT) {
  858. if ( NameFinished ) {
  859. continue;
  860. }
  861. if (NameChar == '.') {
  862. CurrentMatches[DestCount++] = CurrentState;
  863. break;
  864. }
  865. }
  866. //
  867. // From this point on a name character is required to even
  868. // continue, let alone make a match.
  869. //
  870. if ( NameFinished ) {
  871. break;
  872. }
  873. //
  874. // If this expression was a '?' we can match it once.
  875. //
  876. if (ExprChar == '?') {
  877. CurrentMatches[DestCount++] = CurrentState;
  878. break;
  879. }
  880. //
  881. // Finally, check if the expression char matches the name char
  882. //
  883. if (ExprChar == NameChar) {
  884. CurrentMatches[DestCount++] = CurrentState;
  885. break;
  886. }
  887. //
  888. // The expression didn't match so go look at the next
  889. // previous match.
  890. //
  891. break;
  892. }
  893. //
  894. // Prevent duplication in the destination array.
  895. //
  896. // Each of the arrays is montonically increasing and non-
  897. // duplicating, thus we skip over any source element in the src
  898. // array if we just added the same element to the destination
  899. // array. This guarentees non-duplication in the dest. array.
  900. //
  901. while ((SrcCount < MatchesCount) &&
  902. (PreviousDestCount < DestCount)) {
  903. while ((SrcCount < MatchesCount) &&
  904. (PreviousMatches[SrcCount] <
  905. CurrentMatches[PreviousDestCount])) {
  906. SrcCount += 1;
  907. }
  908. PreviousDestCount += 1;
  909. }
  910. }
  911. //
  912. // If we found no matches in the just finished itteration, it's time
  913. // to bail.
  914. //
  915. if ( DestCount == 0 ) {
  916. if (AuxBuffer != NULL) { ExFreePool( AuxBuffer ); }
  917. return FALSE;
  918. }
  919. //
  920. // Swap the meaning the two arrays
  921. //
  922. {
  923. USHORT *Tmp;
  924. Tmp = PreviousMatches;
  925. PreviousMatches = CurrentMatches;
  926. CurrentMatches = Tmp;
  927. }
  928. MatchesCount = DestCount;
  929. }
  930. CurrentState = PreviousMatches[MatchesCount-1];
  931. if (AuxBuffer != NULL) { ExFreePool( AuxBuffer ); }
  932. return (BOOLEAN)(CurrentState == MaxState);
  933. }
  934.