Esempio n. 1
0
int cli_pcre_compile(struct cli_pcre_data *pd, long long unsigned match_limit, long long unsigned match_limit_recursion, unsigned int options, int opt_override)
{
    int errornum, erroffset;
    pcre2_general_context *gctx;
    pcre2_compile_context *cctx;

    if (!pd || !pd->expression) {
        cli_errmsg("cli_pcre_compile: NULL pd or NULL pd->expression\n");
        return CL_ENULLARG;
    }

    gctx = pcre2_general_context_create(cli_pcre_malloc, cli_pcre_free, NULL);
    if (!gctx) {
        cli_errmsg("cli_pcre_compile: Unable to allocate memory for general context\n");
        return CL_EMEM;
    }

    cctx = pcre2_compile_context_create(gctx);
    if (!cctx) {
        cli_errmsg("cli_pcre_compile: Unable to allocate memory for compile context\n");
        pcre2_general_context_free(gctx);
        return CL_EMEM;
    }

    /* compile the pcre2 regex last arg is charset, allow for options override */
    if (opt_override)
        pd->re = pcre2_compile(pd->expression, PCRE2_ZERO_TERMINATED, options, &errornum, &erroffset, cctx); /* pd->re handled by pcre2 -> call pcre_free() -> calls free() */
    else
        pd->re = pcre2_compile(pd->expression, PCRE2_ZERO_TERMINATED, pd->options, &errornum, &erroffset, cctx); /* pd->re handled by pcre2 -> call pcre_free() -> calls free() */
    if (pd->re == NULL) {
        PCRE2_UCHAR errmsg[256];
        pcre2_get_error_message(errornum, errmsg, sizeof(errmsg));
        cli_errmsg("cli_pcre_compile: PCRE2 compilation failed at offset %d: %s\n", erroffset, errmsg);
        pcre2_compile_context_free(cctx);
        pcre2_general_context_free(gctx);
        return CL_EMALFDB;
    }

    /* setup matching context and set the match limits */
    pd->mctx = pcre2_match_context_create(gctx);
    if (!pd->mctx) {
        cli_errmsg("cli_pcre_compile: Unable to allocate memory for match context\n");
        pcre2_compile_context_free(cctx);
        pcre2_general_context_free(gctx);
        return CL_EMEM;
    }

    pcre2_set_match_limit(pd->mctx, match_limit);
    pcre2_set_recursion_limit(pd->mctx, match_limit_recursion);

    /* non-dynamic allocated fields set by caller */
    pcre2_compile_context_free(cctx);
    pcre2_general_context_free(gctx);
    return CL_SUCCESS;
}
Esempio n. 2
0
static void free_pcre2_pattern(struct grep_pat *p)
{
	pcre2_compile_context_free(p->pcre2_compile_context);
	pcre2_code_free(p->pcre2_pattern);
	pcre2_match_data_free(p->pcre2_match_data);
	pcre2_jit_stack_free(p->pcre2_jit_stack);
	pcre2_match_context_free(p->pcre2_match_context);
}
Esempio n. 3
0
static void hb_pcre2_exit( void * cargo )
{
   HB_SYMBOL_UNUSED( cargo );

   pcre2_match_context_free( s_re_ctxm );
   pcre2_compile_context_free( s_re_ctxc );
   pcre2_general_context_free( s_re_ctxg );
}