filters_worker.inc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifdef BINFILTER
  2. struct bfiltervec filters;
  3. #endif // BINFILTER
  4. #ifdef INTFILTER
  5. struct ifiltervec filters;
  6. # ifdef OMITMASK
  7. IFT ifiltermask;
  8. # endif
  9. #endif // INTFILTER
  10. #ifdef PCRE2FILTER
  11. struct pfiltervec filters;
  12. #endif // PCRE2FILTER
  13. void filters_init(void)
  14. {
  15. VEC_INIT(filters);
  16. }
  17. #include "filters_common.inc.h"
  18. #ifdef INTFILTER
  19. # ifndef BINSEARCH
  20. #define MATCHFILTER(it,pk) \
  21. ((*(IFT *)(pk) & VEC_BUF(filters,it).m) == VEC_BUF(filters,it).f)
  22. #define DOFILTER(it,pk,code) \
  23. do { \
  24. for (it = 0;it < VEC_LENGTH(filters);++it) { \
  25. if (unlikely(MATCHFILTER(it,pk))) { \
  26. code; \
  27. break; \
  28. } \
  29. } \
  30. } while (0)
  31. # else // BINSEARCH
  32. # ifdef OMITMASK
  33. #define DOFILTER(it,pk,code) \
  34. do { \
  35. register IFT maskedpk = *(IFT *)(pk) & ifiltermask; \
  36. for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
  37. it = (up + down) / 2; \
  38. if (maskedpk < VEC_BUF(filters,it).f) \
  39. up = it; \
  40. else if (maskedpk > VEC_BUF(filters,it).f) \
  41. down = it + 1; \
  42. else { \
  43. code; \
  44. break; \
  45. } \
  46. } \
  47. } while (0)
  48. # else // OMITMASK
  49. #define DOFILTER(it,pk,code) \
  50. do { \
  51. for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
  52. it = (up + down) / 2; \
  53. IFT maskedpk = *(IFT *)(pk) & VEC_BUF(filters,it).m; \
  54. register int cmp = memcmp(&maskedpk,&VEC_BUF(filters,it).f,sizeof(IFT)); \
  55. if (cmp < 0) \
  56. up = it; \
  57. else if (cmp > 0) \
  58. down = it + 1; \
  59. else { \
  60. code; \
  61. break; \
  62. } \
  63. } \
  64. } while (0)
  65. # endif // OMITMASK
  66. # endif // BINSEARCH
  67. #define PREFILTER
  68. #define POSTFILTER
  69. #endif // INTFILTER
  70. #ifdef BINFILTER
  71. # ifndef BINSEARCH
  72. #define MATCHFILTER(it,pk) ( \
  73. memcmp(pk,VEC_BUF(filters,it).f,VEC_BUF(filters,it).len) == 0 && \
  74. (pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) == VEC_BUF(filters,it).f[VEC_BUF(filters,it).len])
  75. #define DOFILTER(it,pk,code) \
  76. do { \
  77. for (it = 0;it < VEC_LENGTH(filters);++it) { \
  78. if (unlikely(MATCHFILTER(it,pk))) { \
  79. code; \
  80. break; \
  81. } \
  82. } \
  83. } while (0)
  84. # else // BINSEARCH
  85. #define DOFILTER(it,pk,code) \
  86. do { \
  87. for (size_t down = 0,up = VEC_LENGTH(filters);down < up;) { \
  88. it = (up + down) / 2; \
  89. { \
  90. register int filterdiff = memcmp(pk,VEC_BUF(filters,it).f,VEC_BUF(filters,it).len); \
  91. if (filterdiff < 0) { \
  92. up = it; \
  93. continue; \
  94. } \
  95. if (filterdiff > 0) { \
  96. down = it + 1; \
  97. continue; \
  98. } \
  99. } \
  100. if ((pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) < \
  101. VEC_BUF(filters,it).f[VEC_BUF(filters,it).len]) \
  102. { \
  103. up = it; \
  104. continue; \
  105. } \
  106. if ((pk[VEC_BUF(filters,it).len] & VEC_BUF(filters,it).mask) > \
  107. VEC_BUF(filters,it).f[VEC_BUF(filters,it).len]) \
  108. { \
  109. down = it + 1; \
  110. continue; \
  111. } \
  112. { \
  113. code; \
  114. break; \
  115. } \
  116. } \
  117. } while (0)
  118. # endif // BINSEARCH
  119. #define PREFILTER
  120. #define POSTFILTER
  121. #endif // BINFILTER
  122. #ifdef PCRE2FILTER
  123. #define PREFILTER \
  124. char pkconvbuf[BASE32_TO_LEN(PUBLIC_LEN) + 1]; \
  125. pcre2_match_data *pcre2md = pcre2_match_data_create(128,0); \
  126. PCRE2_SIZE *pcre2ovector = 0;
  127. #define POSTFILTER \
  128. pcre2_match_data_free(pcre2md);
  129. #define DOFILTER(it,pk,code) \
  130. do { \
  131. base32_to(pkconvbuf,pk,PUBLIC_LEN); \
  132. size_t __l = VEC_LENGTH(filters); \
  133. for (it = 0;it < __l;++it) { \
  134. int rc = pcre2_match(VEC_BUF(filters,it).re,(PCRE2_SPTR8)pkconvbuf,BASE32_TO_LEN(PUBLIC_LEN),0, \
  135. PCRE2_NO_UTF_CHECK,pcre2md,0); \
  136. if (unlikely(rc >= 0)) { \
  137. pcre2ovector = pcre2_get_ovector_pointer(pcre2md); \
  138. code; \
  139. break; \
  140. } \
  141. } \
  142. } while (0)
  143. #endif // PCRE2FILTER