2
0

apr_strmatch.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2002-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_strmatch.h"
  17. #include "apr_lib.h"
  18. #define APR_WANT_STRFUNC
  19. #include "apr_want.h"
  20. #define NUM_CHARS 256
  21. /*
  22. * String searching functions
  23. */
  24. static const char *match_no_op(const apr_strmatch_pattern *this_pattern,
  25. const char *s, apr_size_t slen)
  26. {
  27. return s;
  28. }
  29. static const char *match_boyer_moore_horspool(
  30. const apr_strmatch_pattern *this_pattern,
  31. const char *s, apr_size_t slen)
  32. {
  33. const char *s_end = s + slen;
  34. int *shift = (int *)(this_pattern->context);
  35. const char *s_next = s + this_pattern->length - 1;
  36. const char *p_start = this_pattern->pattern;
  37. const char *p_end = p_start + this_pattern->length - 1;
  38. while (s_next < s_end) {
  39. const char *s_tmp = s_next;
  40. const char *p_tmp = p_end;
  41. while (*s_tmp == *p_tmp) {
  42. p_tmp--;
  43. if (p_tmp < p_start) {
  44. return s_tmp;
  45. }
  46. s_tmp--;
  47. }
  48. s_next += shift[(int)*((const unsigned char *)s_next)];
  49. }
  50. return NULL;
  51. }
  52. static const char *match_boyer_moore_horspool_nocase(
  53. const apr_strmatch_pattern *this_pattern,
  54. const char *s, apr_size_t slen)
  55. {
  56. const char *s_end = s + slen;
  57. int *shift = (int *)(this_pattern->context);
  58. const char *s_next = s + this_pattern->length - 1;
  59. const char *p_start = this_pattern->pattern;
  60. const char *p_end = p_start + this_pattern->length - 1;
  61. while (s_next < s_end) {
  62. const char *s_tmp = s_next;
  63. const char *p_tmp = p_end;
  64. while (apr_tolower(*s_tmp) == apr_tolower(*p_tmp)) {
  65. p_tmp--;
  66. if (p_tmp < p_start) {
  67. return s_tmp;
  68. }
  69. s_tmp--;
  70. }
  71. s_next += shift[apr_tolower(*s_next)];
  72. }
  73. return NULL;
  74. }
  75. APU_DECLARE(const apr_strmatch_pattern *) apr_strmatch_precompile(
  76. apr_pool_t *p, const char *s,
  77. int case_sensitive)
  78. {
  79. apr_strmatch_pattern *pattern;
  80. apr_size_t i;
  81. int *shift;
  82. pattern = apr_palloc(p, sizeof(*pattern));
  83. pattern->pattern = s;
  84. pattern->length = strlen(s);
  85. if (pattern->length == 0) {
  86. pattern->compare = match_no_op;
  87. pattern->context = NULL;
  88. return pattern;
  89. }
  90. shift = (int *)apr_palloc(p, sizeof(int) * NUM_CHARS);
  91. for (i = 0; i < NUM_CHARS; i++) {
  92. shift[i] = pattern->length;
  93. }
  94. if (case_sensitive) {
  95. pattern->compare = match_boyer_moore_horspool;
  96. for (i = 0; i < pattern->length - 1; i++) {
  97. shift[(int)s[i]] = pattern->length - i - 1;
  98. }
  99. }
  100. else {
  101. pattern->compare = match_boyer_moore_horspool_nocase;
  102. for (i = 0; i < pattern->length - 1; i++) {
  103. shift[apr_tolower(s[i])] = pattern->length - i - 1;
  104. }
  105. }
  106. pattern->context = shift;
  107. return pattern;
  108. }