+
+static pcre_list *
+compile(const uschar * list, BOOL cacheable, int * cntp)
+{
+int sep = 0, cnt = 0;
+uschar * regex_string;
+pcre_list * re_list_head = NULL, * ri;
+
+/* precompile our regexes */
+while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
+ if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0)
+ {
+ /* compile our regular expression */
+ uschar * errstr;
+ const pcre2_code * re = regex_compile(regex_string,
+ cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
+
+ if (!re)
+ {
+ log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
+ continue;
+ }
+
+ ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
+ ri->re = re;
+ ri->pcre_text = regex_string;
+ ri->next = re_list_head;
+ re_list_head = ri;
+ cnt++;
+ }
+if (cntp) *cntp = cnt;
+return re_list_head;
+}
+
+
+/* Check list of REs against buffer, returning OK for (first) match,
+else FAIL. On match return allocated result strings in regex_vars[].
+
+We use the perm-pool for that, so that our caller can release
+other allocations.
+*/
+static int
+matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
+{
+pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
+
+for (pcre_list * ri = re_list_head; ri; ri = ri->next)
+ {
+ int n;
+
+ /* try matcher on the line */
+ if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
+ {
+ int save_pool = store_pool;
+ store_pool = POOL_PERM;
+
+ regex_match_string = string_copy(ri->pcre_text);
+
+ for (int nn = 1; nn < n; nn++)
+ {
+ PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
+ int off = nn * 2;
+ int len = ovec[off + 1] - ovec[off];
+ regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
+ }
+
+ store_pool = save_pool;
+ return OK;
+ }
+ }
+/* pcre2_match_data_free(md); gen ctx needs no free */
+return FAIL;
+}
+
+
+/* reset expansion variables */
+void
+regex_vars_clear(void)
+{
+regex_match_string = NULL;
+for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
+}
+
+
+