match.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Wildcard pattern matching
  15. */
  16. #include <assert.h>
  17. #include <string.h>
  18. #include "defines.h"
  19. #include "tool.h"
  20. #include "match.h"
  21. /*
  22. * The pattern matching functions [Matche(), Matche_After_Star()] are based
  23. * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as
  24. * "public domain": <http://c.snippets.org/snip_lister.php?fname=match.c>
  25. */
  26. static int Matche PARAMS(( const char *p, const char *t ));
  27. static int Matche_After_Star PARAMS(( const char *p, const char *t ));
  28. #define MATCH_PATTERN 6 /**< bad pattern */
  29. #define MATCH_LITERAL 5 /**< match failure on literal match */
  30. #define MATCH_RANGE 4 /**< match failure on [..] construct */
  31. #define MATCH_ABORT 3 /**< premature end of text string */
  32. #define MATCH_END 2 /**< premature end of pattern string */
  33. #define MATCH_VALID 1 /**< valid match */
  34. /**
  35. * Match string with pattern.
  36. *
  37. * @param Pattern Pattern to match with
  38. * @param String Input string
  39. * @return true if pattern matches
  40. */
  41. GLOBAL bool
  42. Match( const char *Pattern, const char *String )
  43. {
  44. if (Matche(Pattern, String) == MATCH_VALID)
  45. return true;
  46. else
  47. return false;
  48. } /* Match */
  49. /**
  50. * Match string with pattern case-insensitive.
  51. *
  52. * @param Pattern Pattern to match with
  53. * @param String Input string, at most COMMAND_LEN-1 characters long
  54. * @return true if pattern matches
  55. */
  56. GLOBAL bool
  57. MatchCaseInsensitive(const char *Pattern, const char *String)
  58. {
  59. char needle[COMMAND_LEN], haystack[COMMAND_LEN];
  60. strlcpy(needle, Pattern, sizeof(needle));
  61. strlcpy(haystack, String, sizeof(haystack));
  62. return Match(ngt_LowerStr(needle), ngt_LowerStr(haystack));
  63. } /* MatchCaseInsensitive */
  64. /**
  65. * Match string with pattern case-insensitive.
  66. *
  67. * @param Pattern Pattern to match with
  68. * @param String Input string, at most COMMAND_LEN-1 characters long
  69. * @param Separator Character separating the individual patterns in the list
  70. * @return true if pattern matches
  71. */
  72. GLOBAL bool
  73. MatchCaseInsensitiveList(const char *Pattern, const char *String,
  74. const char *Separator)
  75. {
  76. char tmp_pattern[COMMAND_LEN], *ptr;
  77. strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern));
  78. ptr = strtok(tmp_pattern, Separator);
  79. while (ptr) {
  80. ngt_TrimStr(ptr);
  81. if (MatchCaseInsensitive(ptr, String))
  82. return true;
  83. ptr = strtok(NULL, Separator);
  84. }
  85. return false;
  86. } /* MatchCaseInsensitive */
  87. static int
  88. Matche( const char *p, const char *t )
  89. {
  90. for( ; *p; p++, t++ )
  91. {
  92. /* if this is the end of the text then this is the end of the match */
  93. if( ! *t )
  94. {
  95. return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
  96. }
  97. /* determine and react to pattern type */
  98. switch( *p )
  99. {
  100. case '?': /* single any character match */
  101. break;
  102. case '*': /* multiple any character match */
  103. return Matche_After_Star( p, t );
  104. default: /* must match this character exactly */
  105. if( *p != *t ) return MATCH_LITERAL;
  106. }
  107. }
  108. /* if end of text not reached then the pattern fails */
  109. if( *t ) return MATCH_END;
  110. else return MATCH_VALID;
  111. } /* Matche */
  112. static int
  113. Matche_After_Star( const char *p, const char *t )
  114. {
  115. register int nextp, match = 0;
  116. /* pass over existing ? and * in pattern */
  117. while( *p == '?' || *p == '*' )
  118. {
  119. /* take one char for each ? and + */
  120. if (*p == '?')
  121. {
  122. /* if end of text then no match */
  123. if( ! *t++ ) return MATCH_ABORT;
  124. }
  125. /* move to next char in pattern */
  126. p++;
  127. }
  128. /* if end of pattern we have matched regardless of text left */
  129. if( ! *p ) return MATCH_VALID;
  130. /* get the next character to match which must be a literal or '[' */
  131. nextp = *p;
  132. if( nextp == '\\' )
  133. {
  134. nextp = p[1];
  135. /* if end of text then we have a bad pattern */
  136. if( ! nextp ) return MATCH_PATTERN;
  137. }
  138. /* Continue until we run out of text or definite result seen */
  139. do
  140. {
  141. /* a precondition for matching is that the next character
  142. * in the pattern match the next character in the text or that
  143. * the next pattern char is the beginning of a range. Increment
  144. * text pointer as we go here */
  145. if( nextp == *t || nextp == '[' ) match = Matche( p, t );
  146. /* if the end of text is reached then no match */
  147. if( ! *t++ ) match = MATCH_ABORT;
  148. } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
  149. /* return result */
  150. return match;
  151. } /* Matche_After_Star */
  152. /* -eof- */