Esempio n. 1
0
void compile_study(pcre **re, pcre_extra **re_extra, char *q, const int pcre_opts, const int study_opts) {
    const char *pcre_err = NULL;
    int pcre_err_offset = 0;

    *re = pcre_compile(q, pcre_opts, &pcre_err, &pcre_err_offset, NULL);
    if (*re == NULL) {
        die("pcre_compile failed at position %i. Error: %s", pcre_err_offset, pcre_err);
    }
    *re_extra = pcre_study(*re, study_opts, &pcre_err);
    if (*re_extra == NULL) {
        log_debug("pcre_study returned nothing useful. Error: %s", pcre_err);
    }
}
/* Count all matches with pattern p in src buffer. */
static int fb_countmatches(fbuf_t *src, const char *p)
{
  pcre *re;
  pcre_extra *re_ex;
  const char *re_e;
  int re_eo, m[3], pos, count;
  if (!(re = pcre_compile(p, PCRE_CASELESS, &re_e, &re_eo, NULL))) exit(1);
  re_ex = pcre_study(re, 0, &re_e);
  for (count = 0, pos = 0;
       pcre_exec(re, re_ex, src->buf, src->len, pos, 0, m, 3) >= 0;
       pos = m[1]) count++;
  return count;
}
Esempio n. 3
0
// returns true if the provided identifier matches the barcode regex.
static int oilsAuthIdentIsBarcode(const char* identifier, int org_id) {

    if (org_id < 1)
        org_id = oilsUtilsGetRootOrgId();

    char* bc_regex = oilsUtilsFetchOrgSetting(org_id, "opac.barcode_regex");

    if (!bc_regex) {
        // if no regex is set, assume any identifier starting
        // with a number is a barcode.
        bc_regex = strdup("^\\d"); // dupe for later free'ing
    }

    const char *err_str;
    int err_offset, match_ret;

    pcre *compiled = pcre_compile(
        bc_regex, 0, &err_str, &err_offset, NULL);

    if (compiled == NULL) {
        osrfLogError(OSRF_LOG_MARK,
            "Could not compile '%s': %s", bc_regex, err_str);
        free(bc_regex);
        pcre_free(compiled);
        return 0;
    }

    pcre_extra *extra = pcre_study(compiled, 0, &err_str);

    if(err_str != NULL) {
        osrfLogError(OSRF_LOG_MARK,
            "Could not study regex '%s': %s", bc_regex, err_str);
        free(bc_regex);
        pcre_free(compiled);
        return 0;
    } 

    match_ret = pcre_exec(
        compiled, extra, identifier, strlen(identifier), 0, 0, NULL, 0);       

    free(bc_regex);
    pcre_free(compiled);
    if (extra) pcre_free(extra);

    if (match_ret >= 0) return 1; // regex matched

    if (match_ret != PCRE_ERROR_NOMATCH) 
        osrfLogError(OSRF_LOG_MARK, "Unknown error processing barcode regex");

    return 0; // regex did not match
}
Esempio n. 4
0
static int compile_regex(struct saved_data *data, spec_t *spec, const char **errbuf)
{
	const char *tmperrbuf;
	char *reg_buf, *anchored_regex, *cp;
	stem_t *stem_arr = data->stem_arr;
	size_t len;
	int erroff;

	if (spec->regcomp)
		return 0; /* already done */

	data->ncomp++; /* how many compiled regexes required */

	/* Skip the fixed stem. */
	reg_buf = spec->regex_str;
	if (spec->stem_id >= 0)
		reg_buf += stem_arr[spec->stem_id].len;

	/* Anchor the regular expression. */
	len = strlen(reg_buf);
	cp = anchored_regex = (char *) malloc(len + 3);
	if (!anchored_regex)
		return -1;
	/* Create ^...$ regexp.  */
	*cp++ = '^';
	memcpy(cp, reg_buf, len);
	cp += len;
	*cp++ = '$';
	*cp = '\0';

	/* Compile the regular expression. */
	spec->regex = pcre_compile(anchored_regex, PCRE_DOTALL, &tmperrbuf, &erroff, NULL);
	free(anchored_regex);
	if (!spec->regex) {
		if (errbuf)
			*errbuf=tmperrbuf;
		return -1;
	}

	spec->sd = pcre_study(spec->regex, 0, &tmperrbuf);
	if (!spec->sd && tmperrbuf) {
		if (errbuf)
			*errbuf=tmperrbuf;
		return -1;
	}

	/* Done. */
	spec->regcomp = 1;

	return 0;
}
Esempio n. 5
0
static void
ccze_xferlog_setup (void)
{
  const char *error;
  int errptr;

  /* FIXME: Does not handle spaces in filenames! */
  reg_xferlog = pcre_compile
    ("^(... ... +\\d{1,2} +\\d{1,2}:\\d{1,2}:\\d{1,2} \\d+) (\\d+) ([^ ]+) "
     "(\\d+) (\\S+) (a|b) (C|U|T|_) (o|i) (a|g|r) ([^ ]+) ([^ ]+) " 
     "(0|1) ([^ ]+) (c|i)", 0, &error,
     &errptr, NULL);
  hints_xferlog = pcre_study (reg_xferlog, 0, &error);
}
Esempio n. 6
0
static void
ccze_oops_setup (void)
{
  const char *error;
  int errptr;

  reg_oops = pcre_compile
    ("^((Mon|Tue|Wed|Thu|Fri|Sat|Sun) "
     "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) "
     "\\d+ \\d+:\\d+:\\d+ \\d+)(\\s+)\\[([\\dxa-fA-F]+)\\]"
     "statistics\\(\\): ([\\S]+)(\\s*): (\\d+)(.*)",
     0, &error, &errptr, NULL);
  hints_oops = pcre_study (reg_oops, 0, &error);
}
Esempio n. 7
0
pcre_extra * 
pcre_study_exp(pcre *re, int options)
{
	pcre_extra *extra;
	const char *err;

	if (re == NULL) {
		return NULL;
	}

	extra = pcre_study(re, options, &err);

	return extra;
}
RegularExpression::RegularExpression(const std::string& pattern, int options, bool study): _pcre(0), _extra(0)
{
	const char* error;
	int offs;
	_pcre = pcre_compile(pattern.c_str(), options, &error, &offs, 0);
	if (!_pcre)
	{
		std::ostringstream msg;
		msg << error << " (at offset " << offs << ")";
		throw RegularExpressionException(msg.str());
	}
	if (study)
		_extra = pcre_study(_pcre, 0, &error);
}
Esempio n. 9
0
RegEx::RegEx(const char * regex, int options)
{
   const char * error;
   int          erroffset;

   re = pcre_compile(regex, options, &error, &erroffset, NULL);
   if (re == NULL)
      throw error;
   pe = pcre_study(re, 0, &error);
   pcre_fullinfo(re, pe, PCRE_INFO_CAPTURECOUNT, &substrcount);
   substrcount++;
   ovector = new int[3*substrcount];
   matchlist = NULL;
}
Esempio n. 10
0
void
mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * mc_search,
                                       mc_search_cond_t * mc_search_cond)
{
    GString *tmp = NULL;
#ifdef SEARCH_TYPE_GLIB
    GError *error = NULL;
#else /* SEARCH_TYPE_GLIB */
    const char *error;
    int erroffset;
#endif /* SEARCH_TYPE_GLIB */

    if (!mc_search->is_case_sentitive) {
        tmp = g_string_new_len (mc_search_cond->str->str, mc_search_cond->str->len);
        g_string_free (mc_search_cond->str, TRUE);
        mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp->str, tmp->len);
        g_string_free (tmp, TRUE);
    }
#ifdef SEARCH_TYPE_GLIB
    mc_search_cond->regex_handle =
        g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL, 0,
                     &error);

    if (error != NULL) {
        mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
        mc_search->error_str = str_conv_gerror_message (error, _(" Regular expression error "));
        g_error_free (error);
        return;
    }
#else /* SEARCH_TYPE_GLIB */
    mc_search_cond->regex_handle =
        pcre_compile (mc_search_cond->str->str, PCRE_EXTRA, &error, &erroffset, NULL);
    if (mc_search_cond->regex_handle == NULL) {
        mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
        mc_search->error_str = g_strdup (error);
        return;
    }
    mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
    if (mc_search->regex_match_info == NULL) {
        if (error) {
            mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
            mc_search->error_str = g_strdup (error);
            g_free (mc_search_cond->regex_handle);
            mc_search_cond->regex_handle = NULL;
            return;
        }
    }
#endif /* SEARCH_TYPE_GLIB */
}
Esempio n. 11
0
int regex_comp(const char *str, struct my_regex *regex, int cs, int cap, char **err)
{
    /* copy the original regex format */
    regex->regstr = strdup(str);
    if (!regex->regstr) {
        memprintf(err, "out of memory");
        return 0;
    }

#ifdef USE_PCRE_JIT
    int flags = 0;
    const char *error;
    int erroffset;

    if (!cs)
        flags |= PCRE_CASELESS;
    if (!cap)
        flags |= PCRE_NO_AUTO_CAPTURE;

    regex->reg = pcre_compile(str, flags, &error, &erroffset, NULL);
    if (!regex->reg) {
        free(regex->regstr);
        memprintf(err, "regex '%s' is invalid (error=%s, erroffset=%d)", str, error, erroffset);
        return 0;
    }

    regex->extra = pcre_study(regex->reg, PCRE_STUDY_JIT_COMPILE, &error);
    if (!regex->extra) {
        free(regex->regstr);
        pcre_free(regex->reg);
        memprintf(err, "failed to compile regex '%s' (error=%s)", str, error);
        return 0;
    }
#else
    int flags = REG_EXTENDED;

    if (!cs)
        flags |= REG_ICASE;
    if (!cap)
        flags |= REG_NOSUB;

    if (regcomp(&regex->regex, str, flags) != 0) {
        free(regex->regstr);
        memprintf(err, "regex '%s' is invalid", str);
        return 0;
    }
#endif
    return 1;
}
Esempio n. 12
0
bool RegExp::Study(bool restudy)
{
	if(!cpattern)
		Compile();
	if(study){
		if(restudy)
			pcre_free(study);
		else
			return true;
	}
	study = pcre_study(cpattern, 0, &error_string);
	if(error_string != NULL)
		error_code = -1; // unfortunatelly, pcre_study doesn't return error codes...
	return error_code == 0;
}
Esempio n. 13
0
static void
ccze_squid_setup (void)
{
  const char *error;
  int errptr;

  reg_squid_access = pcre_compile
    ("^(\\d{9,10}\\.\\d{3})(\\s+)(\\d+)\\s(\\S+)\\s(\\w+)\\/(\\d{3})"
     "\\s(\\d+)\\s(\\w+)\\s(\\S+)\\s(\\S+)\\s(\\w+)\\/([\\d\\.]+|-)\\s(.*)",
     0, &error, &errptr, NULL);
  hints_squid_access = pcre_study (reg_squid_access, 0, &error);

  reg_squid_cache = pcre_compile
    ("^(\\d{4}\\/\\d{2}\\/\\d{2}\\s(\\d{2}:){2}\\d{2}\\|)\\s(.*)$", 0,
     &error, &errptr, NULL);
  hints_squid_cache = pcre_study (reg_squid_cache, 0, &error);

  reg_squid_store = pcre_compile
    ("^([\\d\\.]+)\\s(\\w+)\\s(\\-?[\\dA-F]+)\\s+(\\S+)\\s([\\dA-F]+)"
     "(\\s+)(\\d{3}|\\?)(\\s+)(\\-?[\\d\\?]+)(\\s+)(\\-?[\\d\\?]+)(\\s+)"
     "(\\-?[\\d\\?]+)\\s(\\S+)\\s(\\-?[\\d|\\?]+)\\/(\\-?[\\d|\\?]+)\\s"
     "(\\S+)\\s(.*)", 0, &error, &errptr, NULL);
  hints_squid_store = pcre_study (reg_squid_store, 0, &error);
}
Esempio n. 14
0
// Internal triggers
static void AddInternalTrigger (char* regexpstr, unsigned mask, internal_trigger_func func)
{
	pcre_internal_trigger_t *trig;
	const char *error;
	int error_offset;
 
	trig = (pcre_internal_trigger_t *) Z_Malloc (sizeof(pcre_internal_trigger_t));
	trig->next = internal_triggers;
	internal_triggers = trig;
 
	trig->regexp = pcre_compile (regexpstr, 0, &error, &error_offset, NULL);
	trig->regexp_extra = pcre_study (trig->regexp, 0, &error);
	trig->func = func;
	trig->flags = mask;
}
Esempio n. 15
0
File: util.c Progetto: TidyHuang/DLP
void compile_study(pcre **re, pcre_extra **re_extra, char *q, const int pcre_opts, const int study_opts) {
    const char *pcre_err = NULL;
    int pcre_err_offset = 0;

    *re = pcre_compile(q, pcre_opts, &pcre_err, &pcre_err_offset, NULL);
    if (*re == NULL) {
        die("Bad regex! pcre_compile() failed at position %i: %s\nIf you meant to search for a literal string, run ag with -Q",
            pcre_err_offset,
            pcre_err);
    }
    *re_extra = pcre_study(*re, study_opts, &pcre_err);
    if (*re_extra == NULL) {
        log_debug("pcre_study returned nothing useful. Error: %s", pcre_err);
    }
}
Esempio n. 16
0
static int
pcre_compile_do(struct sol_flow_node *node,
    const char *regexp,
    pcre **compiled_re,
    pcre_extra **p_extra,
    int *sub_match_count)
{
    int r;
    int pcre_error_offset;
    const char *pcre_error_str = NULL;

    SOL_NULL_CHECK(compiled_re, -EINVAL);
    SOL_NULL_CHECK(p_extra, -EINVAL);
    SOL_NULL_CHECK(sub_match_count, -EINVAL);

    *compiled_re = NULL;
    *p_extra = NULL;
    *sub_match_count = 0;

    *compiled_re = pcre_compile(regexp, PCRE_UTF8,
        &pcre_error_str, &pcre_error_offset, NULL);
    if (!*compiled_re) {
        sol_flow_send_error_packet(node, EINVAL,
            "Could not compile '%s': %s", regexp, pcre_error_str);
        return -EINVAL;
    }

    r = pcre_fullinfo(*compiled_re, NULL, PCRE_INFO_CAPTURECOUNT,
        sub_match_count);
    if (r < 0) {
        sol_flow_send_error_packet(node, EINVAL, "Could not"
            "extract info from compiled regular expression %s", regexp);
        pcre_free(compiled_re);
        return -EINVAL;
    }

    //A null pcre_extra is fine (no optimization possible), errors are
    //reported on the last argument
    *p_extra = pcre_study(*compiled_re, 0, &pcre_error_str);
    if (pcre_error_str != NULL) {
        sol_flow_send_error_packet(node, EINVAL,
            "Error optimizing '%s': %s", regexp, pcre_error_str);
        pcre_free(compiled_re);
        return -EINVAL;
    }

    return 0;
}
Esempio n. 17
0
File: af.c Progetto: lucy/af
void re_compile(re *r, const char *s, int options)
{
	options |= PCRE_NO_AUTO_CAPTURE;
	options |= PCRE_NO_AUTO_POSSESS;
	int erroff;
	const char *err = NULL;
	r->re = pcre_compile(s, options, &err, &erroff, NULL);
	if (err != NULL)
		die("error at %d: %s", erroff, err);
	r->extra = pcre_study(r->re, PCRE_STUDY_JIT_COMPILE, &err);
	if (err != NULL)
		die("error at %d: %s", erroff, err);
	r->stack = pcre_jit_stack_alloc(32*1024, 1024*1024);
	if (r->stack == NULL)
		die_errno("%s", "compile: pcre_jit_stack_alloc failed");
}
Esempio n. 18
0
static invalidate_t *
copy_invalidate_t(invalidate_t *i)
{
  invalidate_t *iptr;
  const char *errptr;
  int erroffset;

  iptr = (invalidate_t *)TSmalloc(sizeof(invalidate_t));
  iptr->regex_text = TSstrdup(i->regex_text);
  iptr->regex = pcre_compile(iptr->regex_text, 0, &errptr, &erroffset, NULL); // There is no pcre_copy :-(
  iptr->regex_extra = pcre_study(iptr->regex, 0, &errptr);                    // Assuming no errors since this worked before :-/
  iptr->epoch = i->epoch;
  iptr->expiry = i->expiry;
  iptr->next = NULL;
  return iptr;
}
Esempio n. 19
0
template <typename Traits> inline void
branch<Traits>::add_regex_match(char const *pattern) {
	int error_offset = 0;
	char const *error_ptr = 0;
	resource<pcre*, pcre_traits> regex(pcre_compile(pattern, 0, &error_ptr, &error_offset, 0));
	if (!regex) {
		throw error("%s at %d in %s", error_ptr, error_offset, pattern);
	}
	resource<pcre_extra*, pcre_extra_traits> extra(pcre_study(regex.get(), PCRE_STUDY_JIT_COMPILE, &error_ptr));
	if (!extra) {
		throw error("%s in %s", error_ptr, pattern);
	}
	regex_matches_.push_back(std::make_pair(regex.get(), extra.get()));
	extra.release();
	regex.release();
}
Esempio n. 20
0
static EC_OBJ regexp_copy( EC_OBJ obj, EcCopyType type )
{
	pcre       *code;
	pcre_extra *extra;
	EC_OBJ      src, sobj;
	EC_OBJ      res;

	const char *old_str;
	EcInt       old_slen;
	int         old_options;

	const char *errptr  = NULL;
	int         erroffs = 0;

	/* compile a new regexp with the same string and options of the old one */
	src = EC_REGEXPSRC(obj);
	ASSERT( EC_ARRAYP(src) );
	ASSERT( EC_ARRAYLEN(src) == 2 );
	sobj = EcArrayGet( src, 0 );
	res  = EcArrayGet( src, 1 );
	ASSERT( EC_STRINGP(sobj) );
	ASSERT( EC_INUMP(res) );

	old_str     = EC_STRDATA(sobj);
	old_slen    = EC_STRLEN(sobj);
	old_options = EC_INUM(res);

	code = pcre_compile( old_str, old_options,
						 &errptr, &erroffs,
						 /* tableptr */ NULL );
	if (! code) return EcReError( errptr, erroffs );

	extra = NULL;
	if (EC_PCREXTRA(obj))
	{
		extra = pcre_study( code,
							0,									/* currently pcre has no options for study */
							&errptr );
		if (! extra) return EcReError( errptr, -1 );
	}

	return make_regexp( code, extra,
						old_str,
						old_slen,
						old_options );
}
Esempio n. 21
0
static void
ccze_exim_setup (void)
{
  const char *error;
  int errptr;

  reg_exim = pcre_compile
    ("^(\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2})\\s(.*)$", 0,
     &error, &errptr, NULL);
  hints_exim = pcre_study (reg_exim, 0, &error);

  reg_exim_actiontype = pcre_compile
    ("^(\\S{16})\\s([<=\\*][=>\\*])\\s(\\S+.*)$", 0, &error,
     &errptr, NULL);
  reg_exim_uniqn = pcre_compile ("^(\\S{16})\\s(.*)$", 0, &error,
				 &errptr, NULL);
}
Esempio n. 22
0
void ParseSizeInit(void) {
    const char *eb;
    int eo;
    int opts = 0;

    parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
    if (parse_regex == NULL) {
        SCLogError(SC_ERR_PCRE_COMPILE, "Compile of \"%s\" failed at offset "
                   "%" PRId32 ": %s", PARSE_REGEX, eo, eb);
        exit(EXIT_FAILURE);
    }
    parse_regex_study = pcre_study(parse_regex, 0, &eb);
    if (eb != NULL) {
        SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
        exit(EXIT_FAILURE);
    }
}
Esempio n. 23
0
SEXP _gregexpr(SEXP _pattern, SEXP _text, SEXP _subpattern)
{
    SEXP ans;
    pcre *re_pcre;
    pcre_extra *re_pe;
    int erroffset;
    const char *errorptr;
    //int options = 0;
    const unsigned char* tables = pcre_maketables();
    
    const char* spat = as_string(_pattern);
    re_pcre = pcre_compile(spat, 0, &errorptr, &erroffset, tables);
    if (!re_pcre)
    {
        if (errorptr)
        Rprintf("PCRE pattern compilation error\n\t'%s'\n\tat '%s'\n",
            errorptr, spat+erroffset);
        Rprintf("invalid regular expression '%s'\n", spat);
    }
    re_pe = pcre_study(re_pcre, 0, &errorptr);
    
    int n = LENGTH(_text);
    SEXP elt;
    PROTECT(ans = allocVector(VECSXP, n));
    const void *vmax = vmaxget();
    
    for (int i = 0 ; i < n ; i++) {
        if (STRING_ELT(_text, i) == NA_STRING) {
        elt = gregexpr_NAInputAns();
        } else {
            const char* s = as_string(_text,i);
            elt = _pcre(spat, s, re_pcre, re_pe, *REAL(_subpattern));
        }
        SET_VECTOR_ELT(ans, i, elt);
        vmaxset(vmax);
    }
    
    if (re_pe) pcre_free(re_pe);
    pcre_free(re_pcre);
    pcre_free((void *)tables);
    
    UNPROTECT(1);
    return ans;
    
}
Esempio n. 24
0
result_t Routing::append(const char *pattern, Handler_base *hdlr)
{
    int32_t opt = PCRE_JAVASCRIPT_COMPAT | PCRE_EXTRA | PCRE_NEWLINE_ANYCRLF
                  | PCRE_UCP | PCRE_CASELESS;
    const char *error;
    int32_t erroffset;
    pcre *re;
    pcre_extra *extra;

    re = pcre_compile(pattern, opt, &error, &erroffset, NULL);
    if (re == NULL)
    {
        char buf[1024];

        sprintf(buf, "Routing: Compilation failed at offset %d: %s.", erroffset, error);
        return CHECK_ERROR(Runtime::setError(buf));
    }

    extra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &error);
    if (extra == NULL)
    {
        pcre_free(re);
        return CHECK_ERROR(Runtime::setError(error));
    }

    Isolate* isolate = holder();
    v8::Local<v8::String> k = v8::String::NewFromUtf8(isolate->m_isolate, "handler");
    v8::Local<v8::Value> v = wrap()->GetHiddenValue(k);
    v8::Local<v8::Array> a;

    if (IsEmpty(v))
    {
        a = v8::Array::New(isolate->m_isolate);
        wrap()->SetHiddenValue(k, a);
    }
    else
        a = v8::Local<v8::Array>::Cast(v);

    a->Set((int32_t)m_array.size(), hdlr->wrap());

    obj_ptr<rule> r = new rule(re, extra, hdlr);
    m_array.append(r);

    return 0;
}
Esempio n. 25
0
File: grep.c Progetto: 2quala/git
static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt)
{
	const char *error;
	int erroffset;
	int options = PCRE_MULTILINE;

	if (opt->ignore_case)
		options |= PCRE_CASELESS;

	p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset,
			NULL);
	if (!p->pcre_regexp)
		compile_regexp_failed(p, error);

	p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error);
	if (!p->pcre_extra_info && error)
		die("%s", error);
}
Esempio n. 26
0
struct control_udp *control_udp_new(struct poller *p, endpoint_t *ep, struct callmaster *m) {
	struct control_udp *c;
	const char *errptr;
	int erroff;

	if (!p || !m)
		return NULL;

	c = obj_alloc0("control_udp", sizeof(*c), NULL);

	c->callmaster = m;
	c->parse_re = pcre_compile(
			/* cookie cmd flags callid viabranch:5 */
			"^(\\S+)\\s+(?:([ul])(\\S*)\\s+([^;]+)(?:;(\\S+))?\\s+" \
			/* addr4 addr6:7 */
			"(?:([\\d.]+)|([\\da-f:]+(?::ffff:[\\d.]+)?))" \
			/* port fromtag num totag:11 */
			"\\s+(\\d+)\\s+(\\S+?);(\\d+)(?:\\s+(\\S+?);\\d+(?:\\s+.*)?)?\r?\n?$" \
			/* "d/q" flags callid viabranch fromtag totag:17 */
			"|([dq])(\\S*)\\s+([^;\\s]+)(?:;(\\S+))?\\s+(\\S+?)(?:;\\d+)?(?:\\s+(\\S+?)(?:;\\d+)?)?\r?\n?$" \
			/* v flags params:20 */
			"|(v)(\\S*)(?:\\s+(\\S+))?)",
			PCRE_DOLLAR_ENDONLY | PCRE_DOTALL | PCRE_CASELESS, &errptr, &erroff, NULL);
	c->parse_ree = pcre_study(c->parse_re, 0, &errptr);
			              /* cookie       cmd flags callid   addr      port */
	c->fallback_re = pcre_compile("^(\\S+)(?:\\s+(\\S)\\S*\\s+\\S+(\\s+\\S+)(\\s+\\S+))?", PCRE_DOLLAR_ENDONLY | PCRE_DOTALL | PCRE_CASELESS, &errptr, &erroff, NULL);

	if (!c->parse_re || !c->fallback_re)
		goto fail2;

	cookie_cache_init(&c->cookie_cache);

	if (udp_listener_init(&c->udp_listeners[0], p, ep, control_udp_incoming, &c->obj))
		goto fail2;
	if (ipv46_any_convert(ep) && udp_listener_init(&c->udp_listeners[1], p, ep, control_udp_incoming, &c->obj))
		goto fail2;

	return c;

fail2:
	obj_put(c);
	return NULL;

}
Esempio n. 27
0
/**
 * \brief Registration function for template: keyword
 *
 * This function is called once in the 'lifetime' of the engine.
 */
void DetectTemplateRegister(void) {
    /* keyword name: this is how the keyword is used in a rule */
    sigmatch_table[DETECT_TEMPLATE].name = "template";
    /* description: listed in "suricata --list-keywords=all" */
    sigmatch_table[DETECT_TEMPLATE].desc = "give an introduction into how a detection module works";
    /* link to further documentation of the keyword. Normally on the Suricata redmine/wiki */
    sigmatch_table[DETECT_TEMPLATE].url = "https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Suricata_Developers_Guide";
    /* match function is called when the signature is inspected on a packet */
    sigmatch_table[DETECT_TEMPLATE].Match = DetectTemplateMatch;
    /* setup function is called during signature parsing, when the template
     * keyword is encountered in the rule */
    sigmatch_table[DETECT_TEMPLATE].Setup = DetectTemplateSetup;
    /* free function is called when the detect engine is freed. Normally at
     * shutdown, but also during rule reloads. */
    sigmatch_table[DETECT_TEMPLATE].Free = DetectTemplateFree;
    /* registers unittests into the system */
    sigmatch_table[DETECT_TEMPLATE].RegisterTests = DetectTemplateRegisterTests;

    /* set up the PCRE for keyword parsing */
    const char *eb;
    int eo;
    int opts = 0;

    parse_regex = pcre_compile(PARSE_REGEX, opts, &eb, &eo, NULL);
    if (parse_regex == NULL) {
        SCLogError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at "
                "offset %" PRId32 ": %s", PARSE_REGEX, eo, eb);
        goto error;
    }

    parse_regex_study = pcre_study(parse_regex, 0, &eb);
    if (eb != NULL) {
        SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
        goto error;
    }
    return;

error:
    if (parse_regex != NULL)
        SCFree(parse_regex);
    if (parse_regex_study != NULL)
        SCFree(parse_regex_study);
    return;
}
Esempio n. 28
0
G_MODULE_EXPORT int gtk_module_init(gint argc, char *argv[])
{
	const char *errptr = 0;
	int offset = 0;

	regexp_phone = pcre_compile("\\+?[0-9\\-\\.\\(\\)\\s]{0,12}\\d{3}[0-9\\-\\.\\(\\)\\s]{0,12}",
			0, &errptr,&offset,NULL);
	if(!regexp_phone) abort();
	regexp_phone_extra = pcre_study(regexp_phone, 0, &errptr);

#define ATTACH(f) \
	g_type_class_ref(f); g_signal_add_emission_hook(g_signal_lookup("populate-popup", f), \
			0, gtkdial_signal_emission_hook, NULL, NULL)
	ATTACH(GTK_TYPE_ENTRY);
	ATTACH(GTK_TYPE_LABEL);
	ATTACH(GTK_TYPE_TEXT_VIEW);
#undef ATTACH
	return TRUE;
}
Esempio n. 29
0
RegEx::RegEx(const char * regex, int options)
{
   const char*  pcre_error;
   int          erroffset;

   // compile and study the expression
   re = pcre_compile(regex, options, &pcre_error, &erroffset, NULL);
   if (re == NULL)
   {
      UtlString errorMsg("Regular Expression compile error: ");
      errorMsg.append(pcre_error);
      errorMsg.append(" at offset ");
      char offsetStr[10];
      sprintf(offsetStr, "%9d", erroffset);
      errorMsg.append(offsetStr);
      errorMsg.append(" in expression '");
      errorMsg.append(regex);
      errorMsg.append("'");

      throw errorMsg.data();
      assert(FALSE); // regex failed to compile
   }
   pe = pcre_study(re, 0, &pcre_error);
   if ( pcre_error == NULL )
   {
      // save the compilation block sizes for the copy constructor.
      pcre_fullinfo(re, pe, PCRE_INFO_SIZE, &re_size);
      pcre_fullinfo(re, pe, PCRE_INFO_STUDYSIZE, &study_size);
      allocated_study = false;
   }
   else
   {
      re_size = 0;
      study_size = 0;
   }
   
   // allocate space for match results based on how many substrings
   // there are in the expression (+1 for the entire match)
   pcre_fullinfo(re, pe, PCRE_INFO_CAPTURECOUNT, &substrcount);
   substrcount++;
   ovector = new int[3*substrcount];
   matchlist = NULL;
};
Esempio n. 30
0
static int compile_regex (lua_State *L, const TArgComp *argC, TPcre **pud) {
  const char *error;
  int erroffset;
  TPcre *ud;
  const unsigned char *tables = NULL;

  ud = (TPcre*)lua_newuserdata (L, sizeof (TPcre));
  memset (ud, 0, sizeof (TPcre));           /* initialize all members to 0 */
  lua_pushvalue (L, LUA_ENVIRONINDEX);
  lua_setmetatable (L, -2);

  if (argC->locale) {
    char old_locale[256];
    strcpy (old_locale, setlocale (LC_CTYPE, NULL));  /* store the locale */
    if (NULL == setlocale (LC_CTYPE, argC->locale))   /* set new locale */
      return luaL_error (L, "cannot set locale");
    ud->tables = tables = pcre_maketables ();  /* make tables with new locale */
    setlocale (LC_CTYPE, old_locale);          /* restore the old locale */
  }
  else if (argC->tables) {
    tables = argC->tables;
    lua_pushinteger (L, INDEX_CHARTABLES_LINK);
    lua_rawget (L, LUA_ENVIRONINDEX);
    lua_pushvalue (L, -2);
    lua_pushvalue (L, argC->tablespos);
    lua_rawset (L, -3);
    lua_pop (L, 1);
  }

  ud->pr = pcre_compile (argC->pattern, argC->cflags, &error, &erroffset, tables);
  if (!ud->pr)
    return luaL_error (L, "%s (pattern offset: %d)", error, erroffset + 1);

  ud->extra = pcre_study (ud->pr, 0, &error);
  if (error) return luaL_error (L, "%s", error);

  pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt);
  /* need (2 ints per capture, plus one for substring match) * 3/2 */
  ud->match = (int *) Lmalloc (L, (ALG_NSUB(ud) + 1) * 3 * sizeof (int));

  if (pud) *pud = ud;
  return 1;
}