PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/include/common/regex.h

https://github.com/james-w/haproxy
C Header | 120 lines | 66 code | 13 blank | 41 comment | 0 complexity | a52cdfd723a618faec20cc4e9c6012fc MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. /*
  2. * include/common/regex.h
  3. * This file defines everything related to regular expressions.
  4. *
  5. * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation, version 2.1
  10. * exclusively.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef _COMMON_REGEX_H
  22. #define _COMMON_REGEX_H
  23. #include <stdlib.h>
  24. #include <common/config.h>
  25. #ifdef USE_PCRE
  26. #include <pcre.h>
  27. #include <pcreposix.h>
  28. #else /* no PCRE */
  29. #include <regex.h>
  30. #endif
  31. struct my_regex {
  32. #ifdef USE_PCRE
  33. #ifdef USE_PCRE_JIT
  34. #ifndef PCRE_CONFIG_JIT
  35. #error "The PCRE lib doesn't support JIT. Change your lib, or remove the option USE_PCRE_JIT."
  36. #endif
  37. pcre *reg;
  38. pcre_extra *extra;
  39. #else /* no PCRE_JIT */
  40. regex_t regex;
  41. #endif
  42. #else /* no PCRE */
  43. regex_t regex;
  44. #endif
  45. };
  46. /* what to do when a header matches a regex */
  47. #define ACT_ALLOW 0 /* allow the request */
  48. #define ACT_REPLACE 1 /* replace the matching header */
  49. #define ACT_REMOVE 2 /* remove the matching header */
  50. #define ACT_DENY 3 /* deny the request */
  51. #define ACT_PASS 4 /* pass this header without allowing or denying the request */
  52. #define ACT_TARPIT 5 /* tarpit the connection matching this request */
  53. #define ACT_SETBE 6 /* switch the backend */
  54. struct hdr_exp {
  55. struct hdr_exp *next;
  56. const regex_t *preg; /* expression to look for */
  57. int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
  58. const char *replace; /* expression to set instead */
  59. void *cond; /* a possible condition or NULL */
  60. };
  61. extern regmatch_t pmatch[MAX_MATCH];
  62. /* "str" is the string that contain the regex to compile.
  63. * "regex" is preallocated memory. After the execution of this function, this
  64. * struct contain the compiled regex.
  65. * "cs" is the case sensitive flag. If cs is true, case sensitive is enabled.
  66. * "cap" is capture flag. If cap if true the regex can capture into
  67. * parenthesis strings.
  68. * "err" is the standar error message pointer.
  69. *
  70. * The function return 1 is succes case, else return 0 and err is filled.
  71. */
  72. int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err);
  73. int exp_replace(char *dst, unsigned int dst_size, char *src, const char *str, const regmatch_t *matches);
  74. const char *check_replace_string(const char *str);
  75. const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
  76. int action, const char *replace, void *cond);
  77. /* Note that <subject> MUST be at least <length+1> characters long and must
  78. * be writable because the function will temporarily force a zero past the
  79. * last character.
  80. */
  81. static inline int regex_exec(const struct my_regex *preg, char *subject, int length) {
  82. #ifdef USE_PCRE_JIT
  83. return pcre_exec(preg->reg, preg->extra, subject, length, 0, 0, NULL, 0);
  84. #else
  85. int match;
  86. char old_char = subject[length];
  87. subject[length] = 0;
  88. match = regexec(&preg->regex, subject, 0, NULL, 0);
  89. subject[length] = old_char;
  90. return match;
  91. #endif
  92. }
  93. static inline void regex_free(struct my_regex *preg) {
  94. #ifdef USE_PCRE_JIT
  95. pcre_free_study(preg->extra);
  96. pcre_free(preg->reg);
  97. #else
  98. regfree(&preg->regex);
  99. #endif
  100. }
  101. #endif /* _COMMON_REGEX_H */
  102. /*
  103. * Local variables:
  104. * c-indent-level: 8
  105. * c-basic-offset: 8
  106. * End:
  107. */