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.

190 lines
5.7 KiB

  1. //---------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation, 1997 - 1999
  3. //
  4. // regexp.c
  5. //
  6. // Simple regular expression matching.
  7. //
  8. // Author:
  9. // 06-02-97 Edward Reus Initial version.
  10. //
  11. //---------------------------------------------------------------------------
  12. #include <precomp.hxx>
  13. //-------------------------------------------------------------------------
  14. // MatchRE()
  15. //
  16. // Match the test string (pszString) against the specifed pattern. If they
  17. // match return TRUE, else return FALSE.
  18. //
  19. // In this function patterns are made up from "literal" characters plus
  20. // some control characters, "*", "?", "[" and "]". Asterix (*) is a place
  21. // holder for "zero or more" of any character. Question Mark (?) is a place
  22. // holder for "any single character". The square brackets ([]) contain a
  23. // list of matching characters, in this case "-" is used to denote a range
  24. // of characters (i.e. [a-zA-Z] matches any alpha character).
  25. //
  26. // Note: Currently there is no support for "or" (|) operator.
  27. //
  28. // Note: Ranges are simple, there is no support for dash at the begining
  29. // of a range to denote the dash itself.
  30. //-------------------------------------------------------------------------
  31. BOOL MatchRE( unsigned char *pszString,
  32. unsigned char *pszPattern )
  33. {
  34. unsigned char ch;
  35. unsigned char chPattern;
  36. unsigned char chRangeLow;
  37. while (TRUE)
  38. {
  39. // Walk throuh the pattern, matching it against the string.
  40. switch (chPattern = *pszPattern++)
  41. {
  42. case '*':
  43. // Match zero or more characters.
  44. while (*pszString)
  45. {
  46. if (MatchRE(pszString++,pszPattern))
  47. {
  48. return TRUE;
  49. }
  50. }
  51. return MatchRE(pszString,pszPattern);
  52. case '?':
  53. // Match any single character.
  54. if (*pszString++ == 0)
  55. {
  56. // Not at end of string, so no match.
  57. return FALSE;
  58. }
  59. break;
  60. case '[':
  61. // Match a set of characters.
  62. if ( (ch = *pszString++) == 0)
  63. {
  64. // Syntax error, no matching close bracket "]".
  65. return FALSE;
  66. }
  67. // ch = toupper(ch);
  68. chRangeLow = 0;
  69. while (chPattern = *pszPattern++)
  70. {
  71. if (chPattern == ']')
  72. {
  73. // End of char set, no match found.
  74. return FALSE;
  75. }
  76. if (chPattern == '-')
  77. {
  78. // check a range of chars?
  79. chPattern = *pszPattern; // get high limit of range
  80. if ((chPattern == 0)||(chPattern == ']'))
  81. {
  82. // Syntax error.
  83. return FALSE;
  84. }
  85. if ((ch >= chRangeLow)&&(ch <= chPattern))
  86. {
  87. // In range, go to next character.
  88. break;
  89. }
  90. }
  91. chRangeLow = chPattern;
  92. // See if character matches this pattern element.
  93. if (ch == chPattern)
  94. {
  95. // Character match, go on.
  96. break;
  97. }
  98. }
  99. // Have a match in the character set, skip to the end of the set.
  100. while ((chPattern)&&(chPattern != ']'))
  101. {
  102. chPattern = *pszPattern++;
  103. }
  104. break;
  105. case 0:
  106. // End of pattern, return TRUE if at end of string.
  107. return ((*pszString)? FALSE : TRUE);
  108. default:
  109. ch = *pszString++;
  110. // Check for exact character match.
  111. // Note: CASE matters...
  112. if (ch != chPattern)
  113. {
  114. // No match.
  115. return FALSE;
  116. }
  117. break;
  118. }
  119. }
  120. // Can never exit from here.
  121. }
  122. //-------------------------------------------------------------------------
  123. // MatchREList()
  124. //
  125. // Match a string against a list (array) of RE pattens, return TRUE iff
  126. // the string matches one of the RE patterns. The list of patterns is a
  127. // NULL terminated array of pointers to RE pattern strings.
  128. //-------------------------------------------------------------------------
  129. BOOL MatchREList( unsigned char *pszString,
  130. unsigned char **ppszREList )
  131. {
  132. unsigned char *pszPattern;
  133. if (ppszREList)
  134. {
  135. pszPattern = *ppszREList;
  136. while (pszPattern)
  137. {
  138. if (MatchRE(pszString,pszPattern))
  139. {
  140. return TRUE;
  141. }
  142. pszPattern = *(++ppszREList);
  143. }
  144. }
  145. return FALSE;
  146. }
  147. //-------------------------------------------------------------------------
  148. // MatchExactList()
  149. //
  150. //-------------------------------------------------------------------------
  151. BOOL MatchExactList( unsigned char *pszString,
  152. unsigned char **ppszREList )
  153. {
  154. unsigned char *pszPattern;
  155. if (ppszREList)
  156. {
  157. pszPattern = *ppszREList;
  158. while (pszPattern)
  159. {
  160. if (!lstrcmpA((char *) pszString, (char *) pszPattern))
  161. {
  162. return TRUE;
  163. }
  164. pszPattern = *(++ppszREList);
  165. }
  166. }
  167. return FALSE;
  168. }