#include "base/RegexPattern.h"
#include "ConfigParser.h"
#include "Debug.h"
+#include "sbuf/List.h"
#include "wordlist.h"
ACLRegexData::~ACLRegexData()
}
static bool
-compileRE(std::list<RegexPattern> &curlist, char * RE, int flags)
+compileRE(std::list<RegexPattern> &curlist, const char * RE, int flags)
{
if (RE == NULL || *RE == '\0')
return curlist.empty(); // XXX: old code did this. It looks wrong.
* called only once per ACL.
*/
static int
-compileOptimisedREs(std::list<RegexPattern> &curlist, wordlist * wl)
+compileOptimisedREs(std::list<RegexPattern> &curlist, const SBufList &sl)
{
std::list<RegexPattern> newlist;
int numREs = 0;
char largeRE[BUFSIZ];
*largeRE = 0;
- while (wl != NULL) {
+ for (SBuf i : sl) {
int RElen;
- RElen = strlen( wl->key );
+ RElen = i.length();
- if (strcmp(wl->key, "-i") == 0) {
+ static const SBuf minus_i("-i");
+ static const SBuf plus_i("+i");
+ if (i == minus_i) {
if (flags & REG_ICASE) {
/* optimisation of -i ... -i */
debugs(28, 2, "compileOptimisedREs: optimisation of -i ... -i" );
flags |= REG_ICASE;
largeRE[largeREindex=0] = '\0';
}
- } else if (strcmp(wl->key, "+i") == 0) {
+ } else if (i == plus_i) {
if ((flags & REG_ICASE) == 0) {
/* optimisation of +i ... +i */
debugs(28, 2, "compileOptimisedREs: optimisation of +i ... +i");
largeRE[largeREindex=0] = '\0';
}
} else if (RElen + largeREindex + 3 < BUFSIZ-1) {
- debugs(28, 2, "compileOptimisedREs: adding RE '" << wl->key << "'");
+ debugs(28, 2, "compileOptimisedREs: adding RE '" << i << "'");
if (largeREindex > 0) {
largeRE[largeREindex] = '|';
++largeREindex;
}
largeRE[largeREindex] = '(';
++largeREindex;
- for (char * t = wl->key; *t != '\0'; ++t) {
- largeRE[largeREindex] = *t;
- ++largeREindex;
- }
+ i.copy(largeRE+largeREindex, BUFSIZ-largeREindex);
+ largeREindex += i.length();
largeRE[largeREindex] = ')';
++largeREindex;
largeRE[largeREindex] = '\0';
largeRE[largeREindex=0] = '\0';
continue; /* do the loop again to add the RE to largeRE */
}
- wl = wl->next;
}
if (!compileRE(newlist, largeRE, flags))
}
static void
-compileUnoptimisedREs(std::list<RegexPattern> &curlist, wordlist * wl)
+compileUnoptimisedREs(std::list<RegexPattern> &curlist, const SBufList &sl)
{
int flags = REG_EXTENDED | REG_NOSUB;
- while (wl != NULL) {
- if (strcmp(wl->key, "-i") == 0) {
+ static const SBuf minus_i("-i"), plus_i("+i");
+ for (auto i : sl) {
+ if (i == minus_i) {
flags |= REG_ICASE;
- } else if (strcmp(wl->key, "+i") == 0) {
+ } else if (i == plus_i) {
flags &= ~REG_ICASE;
} else {
- if (!compileRE(curlist, wl->key , flags))
- debugs(28, DBG_CRITICAL, "ERROR: Skipping regular expression. Compile failed: '" << wl->key << "'");
+ if (!compileRE(curlist, i.c_str() , flags))
+ debugs(28, DBG_CRITICAL, "ERROR: Skipping regular expression. Compile failed: '" << i << "'");
}
- wl = wl->next;
}
}
{
debugs(28, 2, "new Regex line or file");
- wordlist *wl = NULL;
+ SBufList sl;
while (char *t = ConfigParser::RegexStrtokFile()) {
const char *clean = removeUnnecessaryWildcards(t);
if (strlen(clean) > BUFSIZ-1) {
debugs(28, DBG_CRITICAL, "ERROR: Skipping regular expression. Larger than " << BUFSIZ-1 << " characters: '" << clean << "'");
} else {
debugs(28, 3, "buffering RE '" << clean << "'");
- wordlistAdd(&wl, clean);
+ sl.push_back(SBuf(clean));
}
}
- if (!compileOptimisedREs(data, wl)) {
+ if (!compileOptimisedREs(data, sl)) {
debugs(28, DBG_IMPORTANT, "WARNING: optimisation of regular expressions failed; using fallback method without optimisation");
- compileUnoptimisedREs(data, wl);
+ compileUnoptimisedREs(data, sl);
}
-
- wordlistDestroy(&wl);
}
bool