static int setf_fixup(void** param, int param_no) { unsigned short nr; regex_t *filter; char *s; s = (char*)*param; if (param_no==1) { /* compile the filter */ if (regexp_compile( s, &filter)<0) { LM_ERR("cannot init filter <%s>\n", s); return E_BAD_RE; } pkg_free(*param); *param = (void*)filter; } else if (param_no==2) { if (s==0 || s[0]==0) { nr = 0; } else if (strcasecmp(s,"reset_all")==0) { nr = RESET_ADDED|RESET_DEFAULT; } else if (strcasecmp(s,"reset_default")==0) { nr = RESET_DEFAULT; } else if (strcasecmp(s,"reset_added")==0) { nr = RESET_ADDED; } else { LM_ERR("unknown reset type <%s>\n",s); return E_UNSPEC; } pkg_free(*param); *param = (void*)(long)nr; } return 0; }
int regexp_match(struct regexp *r, const char *string, const int size, const int start, struct re_registers *regs) { if (r->re == NULL) { if (regexp_compile(r) == -1) return -3; } return re_match(r->re, string, size, start, regs); }
static int redirect_init(void) { regex_t *filter; /* load the TM API */ if (load_tm_api(&rd_tmb)!=0) { LOG(L_ERR, "ERROR:uac_redirect:init: can't load TM API\n"); goto error; } /* init filter */ init_filters(); /* what's the default rule? */ if (def_filter_s) { if ( !strcasecmp(def_filter_s,ACCEPT_RULE_STR) ) { set_default_rule( ACCEPT_RULE ); } else if ( !strcasecmp(def_filter_s,DENY_RULE_STR) ) { set_default_rule( DENY_RULE ); } else { LOG(L_ERR,"ERROR:uac_redirect:init: unknown default " "filter <%s>\n",def_filter_s); } } /* if accept filter specify, compile it */ if (regexp_compile(accept_filter_s, &filter)<0) { LOG(L_ERR,"ERROR:uac_redirect:init: cannot init accept filter\n"); goto error; } add_default_filter( ACCEPT_FILTER, filter); /* if deny filter specify, compile it */ if (regexp_compile(deny_filter_s, &filter)<0) { LOG(L_ERR,"ERROR:uac_redirect:init: cannot init deny filter\n"); goto error; } add_default_filter( DENY_FILTER, filter); return 0; error: return -1; }
/** * Make sub-string replacing by regular expression matching * * @param __re - compiled regular expression * @param __s - source string * @param __mask - mask of replacement * @return replaced string on success, NULL otherwise * @sideeffect allocate memory for return value */ char* preg_replace (const char *__regexp, const char *__s, const char *__mask) { regexp_t *re; char *result; re = regexp_compile (__regexp); if (!re) { return NULL; } result = regexp_replace (re, __s, __mask); regexp_free (re); return result; }
/** * Check is string matches to regular expression * * @param __regexp - regular expression to use * @param __string - string to check * @return non-zero if string matches to regular expression, zero otherwise */ BOOL preg_match (const char *__regexp, const char *__str) { int dummy; regexp_t *re; /* Compile regexp */ re = regexp_compile (__regexp); if (!re) { return FALSE; } dummy = regexp_match (re, __str); /* Free memory */ regexp_free (re); return dummy; }
static grn_bool string_match_regexp(grn_ctx *ctx, const char *target, unsigned int target_len, const char *pattern, unsigned int pattern_len) { #ifdef GRN_SUPPORT_REGEXP OnigRegex regex; grn_bool matched; regex = regexp_compile(ctx, pattern, pattern_len, ONIG_SYNTAX_RUBY); if (!regex) { return GRN_FALSE; } matched = regexp_is_match(ctx, regex, target, target_len); onig_free(regex); return matched; #else /* GRN_SUPPORT_REGEXP */ return GRN_FALSE; #endif /* GRN_SUPPORT_REGEXP */ }
int regexp_nsub(struct regexp *r) { if (r->re == NULL) if (regexp_compile(r) == -1) return -1; return r->re->re_nsub; }
pcre *tintin_regexp_compile(struct session * ses, struct listnode * node, char *exp, int option) { char out[BUFFER_SIZE], *pti, *pto; pti = exp; pto = out; if (*pti == '~') { pti++; SET_BIT(node->flags, NODE_FLAG_META); } while (*pti == '^') { *pto++ = *pti++; } while (*pti) { if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0) { *pto++ = *pti++; *pto++ = *pti++; continue; } switch (pti[0]) { case '\\': *pto++ = *pti++; *pto++ = *pti++; break; case '{': *pto++ = '('; pti = get_arg_in_braces(ses, pti, pto, TRUE); while (*pto) { if (pto[0] == '$' || pto[0] == '@') { if (pto[1]) { return NULL; } } pto++; } *pto++ = ')'; break; case '&': if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1]) || pti[1] == '&') { return NULL; } *pto++ = *pti++; break; case '@': if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1]) || pti[1] == '@') { return NULL; } *pto++ = *pti++; break; case '$': if (pti[1] == DEFAULT_OPEN || isalnum((int)pti[1])) { return NULL; } { int i = 0; while (pti[++i] == '$') { continue; } if (pti[i]) { *pto++ = '\\'; } } *pto++ = *pti++; break; case '[': case ']': case '(': case ')': case '|': case '.': case '?': case '+': case '*': case '^': *pto++ = '\\'; *pto++ = *pti++; break; case '%': switch (pti[1]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': pti += isdigit((int)pti[2]) ? 3 : 2; strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)"); pto += strlen(pto); break; case 'd': pti += 2; strcpy(pto, *pti == 0 ? "([0-9]*)" : "([0-9]*?)"); pto += strlen(pto); break; case 'D': pti += 2; strcpy(pto, *pti == 0 ? "([^0-9]*)" : "([^0-9]*?)"); pto += strlen(pto); break; case 'i': pti += 2; strcpy(pto, "(?i)"); pto += strlen(pto); break; case 'I': pti += 2; strcpy(pto, "(?-i)"); pto += strlen(pto); break; case 's': pti += 2; strcpy(pto, *pti == 0 ? "(\\s*)" : "(\\s*?)"); pto += strlen(pto); break; case 'S': pti += 2; strcpy(pto, *pti == 0 ? "(\\S*)" : "(\\S*?)"); pto += strlen(pto); break; case 'w': pti += 2; strcpy(pto, *pti == 0 ? "([a-zA-Z]*)" : "([a-zA-Z]*?)"); pto += strlen(pto); break; case 'W': pti += 2; strcpy(pto, *pti == 0 ? "([^a-zA-Z]*)" : "([^a-zA-Z]*?)"); pto += strlen(pto); break; case '?': pti += 2; strcpy(pto, *pti == 0 ? "(.?)" : "(.?" "?)"); pto += strlen(pto); break; case '*': pti += 2; strcpy(pto, *pti == 0 ? "(.*)" : "(.*?)"); pto += strlen(pto); break; case '+': pti += 2; strcpy(pto, *pti == 0 ? "(.+)" : "(.+?)"); pto += strlen(pto); break; case '.': pti += 2; strcpy(pto, "(.)"); pto += strlen(pto); break; case '%': *pto++ = *pti++; pti++; break; default: *pto++ = *pti++; break; } break; default: *pto++ = *pti++; break; } } *pto = 0; return regexp_compile(out, option); }
/* * Read a waveform data file. * If the format name is non-NULL, only tries reading in specified format. * If format not specified, tries to guess based on filename, and if * that fails, tries all of the readers until one sucedes. * Returns NULL on failure after printing an error message. * * TODO: use some kind of callback or exception so that client * can put the error messages in a GUI or somthing. */ WaveFile *wf_read(char *name, char *format) { FILE *fp; SpiceStream *ss; int i; unsigned int tried = 0; /* bitmask of formats. */ g_assert(NFormats <= 8*sizeof(tried)); fp = fopen64(name, "r"); if(fp == NULL) { perror(name); return NULL; } if(format == NULL) { for(i = 0; i < NFormats; i++) { if(!format_tab[i].creg) { format_tab[i].creg = regexp_compile(format_tab[i].fnrexp); } if(regexp_test(format_tab[i].creg, name)) { tried |= 1<<i; ss = ss_open_internal(fp, name, format_tab[i].name); if(ss) { ss_msg(INFO, "wf_read", "%s: read with format \"%s\"", name, format_tab[i].name); return wf_finish_read(ss); } if(fseek(fp, 0L, SEEK_SET) < 0) { perror(name); return NULL; } } } if(tried == 0) ss_msg(INFO, "wf_read", "%s: couldn't guess a format from filename suffix.", name); /* no success with formats whose regexp matched filename, * try the others. */ for(i = 0; i < NFormats; i++) { if((tried & (1<<i)) == 0) { ss = ss_open_internal(fp, name, format_tab[i].name); if(ss) return wf_finish_read(ss); tried |= 1<<i; if(fseek(fp, 0L, SEEK_SET) < 0) { perror(name); return NULL; } } } ss_msg(ERR, "wf_read", "%s: couldn't read with any format\n", name); return NULL; } else { /* use specified format only */ ss = ss_open_internal(fp, name, format); if(ss) return wf_finish_read(ss); else return NULL; } }
regexp_t * TclCompileRegexp( Tcl_Interp *interp /* For use in error reporting. */ , unsigned char *string /* String for which to produce * compiled regular expression. */ ) { register Interp *iPtr = (Interp *) interp; int i, length, size; regexp_t *result; length = strlen(string); for (i = 0; i < NUM_REGEXPS; i++) { if ((length == iPtr->patLengths[i]) && (strcmp(string, iPtr->patterns[i]) == 0)) { /* * Move the matched pattern to the first slot in the * cache and shift the other patterns down one position. */ if (i != 0) { int j; unsigned char *cachedString; cachedString = iPtr->patterns[i]; result = iPtr->regexps[i]; for (j = i-1; j >= 0; j--) { iPtr->patterns[j+1] = iPtr->patterns[j]; iPtr->patLengths[j+1] = iPtr->patLengths[j]; iPtr->regexps[j+1] = iPtr->regexps[j]; } iPtr->patterns[0] = cachedString; iPtr->patLengths[0] = length; iPtr->regexps[0] = result; } return iPtr->regexps[0]; } } /* * No match in the cache. Compile the string and add it to the * cache. */ size = regexp_size (string); if (size <= 0) { Tcl_AppendResult(interp, "invalid regular expression pattern", 0); return 0; } result = (regexp_t*) mem_alloc (interp->pool, size); if (! regexp_compile (result, string)) { Tcl_AppendResult(interp, "couldn't compile regular expression pattern", 0); return 0; } if (iPtr->patterns[NUM_REGEXPS-1] != 0) { mem_free (iPtr->patterns[NUM_REGEXPS-1]); mem_free (iPtr->regexps[NUM_REGEXPS-1]); } for (i = NUM_REGEXPS - 2; i >= 0; i--) { iPtr->patterns[i+1] = iPtr->patterns[i]; iPtr->patLengths[i+1] = iPtr->patLengths[i]; iPtr->regexps[i+1] = iPtr->regexps[i]; } iPtr->patterns[0] = (unsigned char *)mem_alloc (interp->pool, length+1); strcpy(iPtr->patterns[0], string); iPtr->patLengths[0] = length; iPtr->regexps[0] = result; return result; }
static int redirect_init(void) { regex_t *filter; void *p; cmd_function fct; /* load the TM API */ if (load_tm_api(&rd_tmb)!=0) { LM_ERR("failed to load TM API\n"); goto error; } p = (void*)acc_db_table; /* fixup table name */ if(fixup_var_pve_str_12(&p, 1)<0) { LM_ERR("failed to fixup acc db table\n"); goto error; } acc_db_table = p; /* init filter */ init_filters(); /* what's the default rule? */ if (def_filter_s) { if ( !strcasecmp(def_filter_s,ACCEPT_RULE_STR) ) { set_default_rule( ACCEPT_RULE ); } else if ( !strcasecmp(def_filter_s,DENY_RULE_STR) ) { set_default_rule( DENY_RULE ); } else { LM_ERR("unknown default filter <%s>\n",def_filter_s); } } /* if accept filter specify, compile it */ if (regexp_compile(accept_filter_s, &filter)<0) { LM_ERR("failed to init accept filter\n"); goto error; } add_default_filter( ACCEPT_FILTER, filter); /* if deny filter specify, compile it */ if (regexp_compile(deny_filter_s, &filter)<0) { LM_ERR("failed to init deny filter\n"); goto error; } add_default_filter( DENY_FILTER, filter); if(sruid_init(&_redirect_sruid, '-', "rdir", SRUID_INC)<0) return -1; if(rd_acc_fct == 0) { /* import the acc stuff */ if(acc_fct_s != 0 && acc_fct_s[0] == '\0') { fct = find_export(acc_fct_s, 2, REQUEST_ROUTE); if(fct == 0) fct = find_export(acc_fct_s, 1, REQUEST_ROUTE); if(fct == 0) { LM_ERR("cannot import %s function; is acc loaded and" " configured\n", acc_fct_s); return E_UNSPEC; } rd_acc_fct = fct; } } return 0; error: return -1; }
static grn_bool string_have_sub_text(grn_ctx *ctx, const char *text, unsigned int text_len, const char *sub_text, unsigned int sub_text_len) { if (sub_text_len == 0) { return GRN_FALSE; } if (sub_text_len > text_len) { return GRN_FALSE; } #ifdef GRN_SUPPORT_REGEXP { OnigRegex regex; grn_bool matched; regex = regexp_compile(ctx, sub_text, sub_text_len, ONIG_SYNTAX_ASIS); if (!regex) { return GRN_FALSE; } matched = regexp_is_match(ctx, regex, text, text_len); onig_free(regex); return matched; } #else /* GRN_SUPPORT_REGEXP */ { const char *text_current = text; const char *text_end = text + text_len; const char *sub_text_current = sub_text; const char *sub_text_end = sub_text + sub_text_len; int sub_text_start_char_len; int sub_text_char_len; sub_text_start_char_len = grn_charlen(ctx, sub_text, sub_text_end); if (sub_text_start_char_len == 0) { return GRN_FALSE; } sub_text_char_len = sub_text_start_char_len; while (text_current < text_end) { int text_char_len; text_char_len = grn_charlen(ctx, text_current, text_end); if (text_char_len == 0) { return GRN_FALSE; } if (text_char_len == sub_text_char_len && memcmp(text_current, sub_text_current, text_char_len) == 0) { sub_text_current += sub_text_char_len; if (sub_text_current == sub_text_end) { return GRN_TRUE; } sub_text_char_len = grn_charlen(ctx, sub_text_current, sub_text_end); if (sub_text_char_len == 0) { return GRN_FALSE; } } else { if (sub_text_current != sub_text) { sub_text_current = sub_text; sub_text_char_len = sub_text_start_char_len; continue; } } text_current += text_char_len; } return GRN_FALSE; } #endif /* GRN_SUPPORT_REGEXP */ }
static grn_bool exec_regexp_vector_bulk(grn_ctx *ctx, grn_obj *vector, grn_obj *pattern) { #ifdef GRN_SUPPORT_REGEXP grn_obj *normalizer = NULL; grn_bool matched = GRN_FALSE; unsigned int i, size; OnigRegex regex; size = grn_vector_size(ctx, vector); if (size == 0) { return GRN_FALSE; } regex = regexp_compile(ctx, GRN_TEXT_VALUE(pattern), GRN_TEXT_LEN(pattern), ONIG_SYNTAX_RUBY); if (!regex) { return GRN_FALSE; } normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); for (i = 0; i < size; i++) { const char *content; unsigned int content_size; grn_id domain_id; grn_obj *norm_content; const char *norm_content_raw; unsigned int norm_content_raw_length_in_bytes; content_size = grn_vector_get_element(ctx, vector, i, &content, NULL, &domain_id); if (content_size == 0) { continue; } norm_content = grn_string_open(ctx, content, content_size, normalizer, 0); grn_string_get_normalized(ctx, norm_content, &norm_content_raw, &norm_content_raw_length_in_bytes, NULL); matched = regexp_is_match(ctx, regex, norm_content_raw, norm_content_raw_length_in_bytes); grn_obj_unlink(ctx, norm_content); if (matched) { break; } } grn_obj_unlink(ctx, normalizer); onig_free(regex); return matched; #else /* GRN_SUPPORT_REGEXP */ return GRN_FALSE; #endif /* GRN_SUPPORT_REGEXP */ }
static grn_bool exec_regexp_uvector_bulk(grn_ctx *ctx, grn_obj *uvector, grn_obj *pattern) { #ifdef GRN_SUPPORT_REGEXP grn_bool matched = GRN_FALSE; unsigned int i, size; OnigRegex regex; grn_obj *domain; grn_obj *normalizer; grn_obj *normalizer_auto = NULL; size = grn_uvector_size(ctx, uvector); if (size == 0) { return GRN_FALSE; } regex = regexp_compile(ctx, GRN_TEXT_VALUE(pattern), GRN_TEXT_LEN(pattern), ONIG_SYNTAX_RUBY); if (!regex) { return GRN_FALSE; } domain = grn_ctx_at(ctx, uvector->header.domain); if (!domain) { onig_free(regex); return GRN_FALSE; } grn_table_get_info(ctx, domain, NULL, NULL, NULL, &normalizer, NULL); if (!normalizer) { normalizer_auto = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); } for (i = 0; i < size; i++) { grn_id record_id; char key[GRN_TABLE_MAX_KEY_SIZE]; int key_size; record_id = grn_uvector_get_element(ctx, uvector, i, NULL); key_size = grn_table_get_key(ctx, domain, record_id, key, GRN_TABLE_MAX_KEY_SIZE); if (key_size == 0) { continue; } if (normalizer) { matched = regexp_is_match(ctx, regex, key, key_size); } else { grn_obj *norm_key; const char *norm_key_raw; unsigned int norm_key_raw_length_in_bytes; norm_key = grn_string_open(ctx, key, key_size, normalizer_auto, 0); grn_string_get_normalized(ctx, norm_key, &norm_key_raw, &norm_key_raw_length_in_bytes, NULL); matched = regexp_is_match(ctx, regex, norm_key_raw, norm_key_raw_length_in_bytes); grn_obj_unlink(ctx, norm_key); } if (matched) { break; } } if (normalizer_auto) { grn_obj_unlink(ctx, normalizer_auto); } grn_obj_unlink(ctx, domain); onig_free(regex); return matched; #else /* GRN_SUPPORT_REGEXP */ return GRN_FALSE; #endif /* GRN_SUPPORT_REGEXP */ }
/* * IMAPFilter: an IMAP mail filtering utility. */ int main(int argc, char *argv[]) { int c; char *cafile = NULL, *capath = NULL; setlocale(LC_CTYPE, ""); opts.verbose = 0; opts.interactive = 0; opts.log = NULL; opts.config = NULL; opts.oneline = NULL; opts.debug = NULL; opts.truststore = NULL; if (exists_dir("/etc/ssl/certs")) opts.truststore = "/etc/ssl/certs"; else if (exists_file("/etc/ssl/cert.pem")) opts.truststore = "/etc/ssl/cert.pem"; env.home = NULL; env.pathmax = -1; while ((c = getopt(argc, argv, "Vc:d:e:il:t:v?")) != -1) { switch (c) { case 'V': version(); /* NOTREACHED */ break; case 'c': opts.config = optarg; break; case 'd': opts.debug = optarg; break; case 'e': opts.oneline = optarg; break; case 'i': opts.interactive = 1; break; case 'l': opts.log = optarg; break; case 't': opts.truststore = optarg; break; case 'v': opts.verbose = 1; break; case '?': default: usage(); /* NOTREACHED */ break; } } get_pathmax(); open_debug(); create_homedir(); catch_signals(); open_log(); if (opts.config == NULL) opts.config = get_filepath("config.lua"); buffer_init(&ibuf, INPUT_BUF); buffer_init(&obuf, OUTPUT_BUF); buffer_init(&nbuf, NAMESPACE_BUF); buffer_init(&cbuf, CONVERSION_BUF); regexp_compile(responses); SSL_library_init(); SSL_load_error_strings(); ssl3ctx = SSL_CTX_new(SSLv3_client_method()); ssl23ctx = SSL_CTX_new(SSLv23_client_method()); tls1ctx = SSL_CTX_new(TLSv1_client_method()); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL tls11ctx = SSL_CTX_new(TLSv1_1_client_method()); tls12ctx = SSL_CTX_new(TLSv1_2_client_method()); #endif if (exists_dir(opts.truststore)) capath = opts.truststore; else if (exists_file(opts.truststore)) cafile = opts.truststore; SSL_CTX_load_verify_locations(ssl3ctx, cafile, capath); SSL_CTX_load_verify_locations(ssl23ctx, cafile, capath); SSL_CTX_load_verify_locations(tls1ctx, cafile, capath); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL SSL_CTX_load_verify_locations(tls11ctx, cafile, capath); SSL_CTX_load_verify_locations(tls12ctx, cafile, capath); #endif start_lua(); #if LUA_VERSION_NUM < 502 { list *l; session *s; l = sessions; while (l != NULL) { s = l->data; l = l->next; request_logout(s); } } #endif stop_lua(); SSL_CTX_free(ssl3ctx); SSL_CTX_free(ssl23ctx); SSL_CTX_free(tls1ctx); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL SSL_CTX_free(tls11ctx); SSL_CTX_free(tls12ctx); #endif ERR_free_strings(); regexp_free(responses); buffer_free(&ibuf); buffer_free(&obuf); buffer_free(&nbuf); buffer_free(&cbuf); xfree(env.home); close_log(); close_debug(); exit(0); }
/* * IMAPFilter: an IMAP mail filtering utility. */ int main(int argc, char *argv[]) { int c; setlocale(LC_CTYPE, ""); opts.verbose = 0; opts.interactive = 0; opts.log = NULL; opts.config = NULL; opts.oneline = NULL; opts.debug = NULL; env.home = NULL; env.pathmax = -1; while ((c = getopt(argc, argv, "Vc:d:e:il:v?")) != -1) { switch (c) { case 'V': version(); /* NOTREACHED */ break; case 'c': opts.config = optarg; break; case 'd': opts.debug = optarg; break; case 'e': opts.oneline = optarg; break; case 'i': opts.interactive = 1; break; case 'l': opts.log = optarg; break; case 'v': opts.verbose = 1; break; case '?': default: usage(); /* NOTREACHED */ break; } } get_pathmax(); open_debug(); create_homedir(); catch_signals(); open_log(); if (opts.config == NULL) opts.config = get_filepath("config.lua"); buffer_init(&ibuf, INPUT_BUF); buffer_init(&obuf, OUTPUT_BUF); buffer_init(&nbuf, NAMESPACE_BUF); buffer_init(&cbuf, CONVERSION_BUF); regexp_compile(responses); SSL_library_init(); SSL_load_error_strings(); start_lua(); #if LUA_VERSION_NUM < 502 { list *l; session *s; l = sessions; while (l != NULL) { s = l->data; l = l->next; request_logout(s); } } #endif stop_lua(); ERR_free_strings(); regexp_free(responses); buffer_free(&ibuf); buffer_free(&obuf); buffer_free(&nbuf); buffer_free(&cbuf); xfree(env.home); close_log(); close_debug(); exit(0); }