Thread: [Netadm-devel] gwc/gwclib naufilter.c,NONE,1.1 regexp.c,NONE,1.1
Status: Beta
Brought to you by:
linuxpark
From: linuxpark <lin...@us...> - 2006-04-06 15:11:03
|
Update of /cvsroot/netadm/gwc/gwclib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9615 Added Files: naufilter.c regexp.c Log Message: Initial Check-in of the library for contents filtering. ported C library of dansgardian's oop. --- NEW FILE: naufilter.c --- /* Title : naufilter.c Author : Jeho-Park <lin...@gm...> Created date : 2006. 04. 06. (thu) 23:54:35 KST Description : library of content filter this is the ported code of dansgardian.org's */ #ident "@(#) $Header: /cvsroot/netadm/gwc/gwclib/naufilter.c,v 1.1 2006/04/06 15:10:53 linuxpark Exp $" #include <stdio.h> #include <string.h> #include <stdlib.h> #include "sysutil.h" #include "naufilter.h" static int checkPICSrating (confilter_session_t *sess, char *label); static void checkPICSratingSafeSurf (confilter_session_t *sess, char *r); static void checkPICSratingevaluWEB (confilter_session_t *sess, char *r); static void checkPICSratingCyberNOT (confilter_session_t *sess, char *r); static void checkPICSratingRSAC (confilter_session_t *sess, char *r); static void checkPICSratingICRA (confilter_session_t *sess, char *r); static void checkPICSratingWeburbia (confilter_session_t *sess, char *r); static void checkPICSratingVancouver (confilter_session_t *sess, char *r); static void checkPICSagainstoption (confilter_session_t *sess, char *s, char* l, int opt, char *m); int init_precompileregexps( confilter_session_t *sess) { confilter_out("\n"); /* return : TRUE(1), FALSE(0) */ if (!regexp_comp(&sess->pics1, "pics-label\"[ \t]*content=[\'\"]([^>]*)[\'\"]")) { confilter_out("Error compiling RegExp pics1.\n"); return FALSE; } if (!regexp_comp(&sess->pics2, "[r|{ratings}] *\\(([^\\)]*)\\)")) { confilter_out( "Error compiling RegExp pics2.\n"); return FALSE; } if (!regexp_comp(&sess->isiphost, ".*[a-z|A-Z].*")) { confilter_out("Error compiling RegExp isiphost.\n"); return FALSE; } sess->isnaughty = FALSE; return TRUE; } int free_precompileregexps( confilter_session_t *sess) { confilter_out("\n"); /* return : TRUE(1), FALSE(0) */ if (regexp_destory (&sess->pics1) != TRUE) { confilter_out("Error destory RegExp pics1.\n"); return FALSE; } if (regexp_destory (&sess->pics2) != TRUE) { confilter_out( "Error destory RegExp pics2.\n"); return FALSE; } if (regexp_destory (&sess->isiphost) != TRUE) { confilter_out("Error compiling RegExp isiphost."); return FALSE; } return TRUE; } int checkPICS(confilter_session_t *sess, char* file, int l) { int i; confilter_out("\n"); *(file + l) = '\0'; if (regexp_match (&sess->pics1, file) != TRUE) { return FALSE; } /* if regexp_matched return FALSE, it mean there is no symtom of naughty. */ if (regexp_matched(&sess->pics1) != TRUE) { return TRUE; } confilter_out("-------------->num [%d]\n", regexp_numberOfMatches (&sess->pics1)); for (i = 0; i < regexp_numberOfMatches (&sess->pics1); i++) { confilter_out("-------------->num [%d]\n", i); if (checkPICSrating(sess, regexp_result (&sess->pics1, i)) != TRUE) return FALSE; } return TRUE; } static int checkPICSrating (confilter_session_t *sess, char *label) { char lab [SZMAXSTR]; char *service; char buf [SZMAXSTR]; char *r; char *p; char *o; int i; char *f; confilter_out("\n"); confilter_out("----> label:[%s]\n", label); if (regexp_match (&sess->pics2, label) != TRUE) { return FALSE; } /* TEST was done properly */ if (regexp_matched (&sess->pics2) != TRUE) { return TRUE; } strcpy (lab, label); confilter_out("lab:{%s}\n", lab); confilter_out("-----> pics2: %d\n", regexp_numberOfMatches (&sess->pics2)); confilter_out("\n"); for (i = 0; i < regexp_numberOfMatches (&sess->pics2); i++) { confilter_out("\n"); strcpy (buf, regexp_result (&sess->pics2, i)); confilter_out("\n"); confilter_out("===========>buffer[%s]\n", buf); o = buf; p = strchr (buf, '('); confilter_out("\n"); if (p) { *p = '\0'; o = ++p; } confilter_out("\n"); p = strchr (o, ')'); if (p) { confilter_out("\n"); *p = '\0'; } confilter_out("\n"); r = o; confilter_out("\n"); confilter_out("r:[%s]\n", r); if (i == 0) { confilter_out("------------> i(%d): %d\n", i, regexp_offset (&sess->pics2, i)); /* TODO: ignore ret val of regexp_offset */ service = subString (lab, 0, regexp_offset (&sess->pics2, i) ); confilter_out("service:[%s]\n", service); } else { confilter_out("------------> i(%d): offset(%d):%d, offset(%d):%d\n", i, i-1, regexp_length (&sess->pics2, i -1), i, regexp_length (&sess->pics2, i)); service = subString (lab, regexp_offset (&sess->pics2, i -1) + regexp_length (&sess->pics2, i -1), regexp_offset (&sess->pics2, i) ); confilter_out("service:[%s]\n", service); } if (!service) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "safesurf", &f); if (f) checkPICSratingSafeSurf (sess, r); if (sess->isnaughty != FALSE) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "evaluweb", &f); if (f) checkPICSratingevaluWEB (sess, r); if (sess->isnaughty != FALSE) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "microsys", &f); if (f) checkPICSratingCyberNOT (sess, r); if (sess->isnaughty != FALSE) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "icra", &f); if (f) checkPICSratingICRA (sess, r); if (sess->isnaughty != FALSE) { confilter_out("================> i: %d, r[%s]\n", i, r); return TRUE; } confilter_out("\n"); sys_strfind (service, strlen (service), "rsac", &f); if (f) checkPICSratingRSAC (sess, r); if (sess->isnaughty != FALSE) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "weburbia", &f); if (f) checkPICSratingWeburbia (sess, r); if (sess->isnaughty != FALSE) return TRUE; confilter_out("\n"); sys_strfind (service, strlen (service), "vancouver", &f); if (f) checkPICSratingVancouver (sess, r); } return TRUE; } static void checkPICSratingSafeSurf (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess, r, "000 ", sess->conf->pics_safesurf_agerange, "Safesurf age range"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "001 ", sess->conf->pics_safesurf_profanity, "Safesurf profanity"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "002 ", sess->conf->pics_safesurf_heterosexualthemes, "Safesurf heterosexualthemes"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "003 ", sess->conf->pics_safesurf_homosexualthemes, "Safesurf "); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "004 ", sess->conf->pics_safesurf_nudity, "Safesurf nudity"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "005 ", sess->conf->pics_safesurf_violence, "Safesurf violence"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "006 ", sess->conf->pics_safesurf_sexviolenceandprofanity, "Safesurf sexviolenceandprofanity"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "007 ", sess->conf->pics_safesurf_intolerance, "Safesurf intolerance"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "008 ", sess->conf->pics_safesurf_druguse, "Safesurf druguse"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "009 ", sess->conf->pics_safesurf_otheradultthemes, "Safesurf otheradultthemes"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "00A ", sess->conf->pics_safesurf_gambling, "Safesurf gambling"); } static void checkPICSratingevaluWEB (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess, r, "rating ", sess->conf->pics_evaluweb_rating, "evaluWEB age range"); } static void checkPICSratingCyberNOT (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess, r, "sex ", sess->conf->pics_cybernot_sex, "CyberNOT sex rating"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "other ", sess->conf->pics_cybernot_sex, "CyberNOT other rating"); } static void checkPICSratingRSAC (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess, r, "v ", sess->conf->pics_rsac_violence, "RSAC violence"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "s ", sess->conf->pics_rsac_sex, "RSAC sex"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "n ", sess->conf->pics_rsac_nudity, "RSAC nudity"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess, r, "l ", sess->conf->pics_rsac_language, "RSAC language"); } static void checkPICSratingICRA (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess,r, "la ", sess->conf->pics_icra_languagesexual, "ICRA languagesexual"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ca ", sess->conf->pics_icra_chat, "ICRA chat"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "cb ", sess->conf->pics_icra_moderatedchat, "ICRA moderatedchat"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "lb ", sess->conf->pics_icra_languageprofanity, "ICRA languageprofanity"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "lc ", sess->conf->pics_icra_languagemildexpletives, "ICRA languagemildexpletives"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "na ", sess->conf->pics_icra_nuditygraphic, "ICRA nuditygraphic"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nb ", sess->conf->pics_icra_nuditymalegraphic, "ICRA nuditymalegraphic"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nc ", sess->conf->pics_icra_nudityfemalegraphic, "ICRA nudityfemalegraphic"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nd ", sess->conf->pics_icra_nuditytopless, "ICRA nuditytopless"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ne ", sess->conf->pics_icra_nuditybottoms, "ICRA nuditybottoms"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nf ", sess->conf->pics_icra_nuditysexualacts, "ICRA nuditysexualacts"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ng ", sess->conf->pics_icra_nudityobscuredsexualacts, "ICRA nudityobscuredsexualacts"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nh ", sess->conf->pics_icra_nuditysexualtouching, "ICRA nuditysexualtouching"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ni ", sess->conf->pics_icra_nuditykissing, "ICRA nuditykissing"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nr ", sess->conf->pics_icra_nudityartistic, "ICRA nudityartistic"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ns ", sess->conf->pics_icra_nudityeducational, "ICRA nudityeducational"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "nt ", sess->conf->pics_icra_nuditymedical, "ICRA nuditymedical"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "oa ", sess->conf->pics_icra_drugstobacco, "ICRA drugstobacco"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ob ", sess->conf->pics_icra_drugsalcohol, "ICRA drugsalcohol"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "oc ", sess->conf->pics_icra_drugsuse, "ICRA drugsuse"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "od ", sess->conf->pics_icra_gambling, "ICRA gambling"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "oe ", sess->conf->pics_icra_weaponuse, "ICRA weaponuse"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "of ", sess->conf->pics_icra_intolerance, "ICRA intolerance"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "og ", sess->conf->pics_icra_badexample, "ICRA badexample"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "oh ", sess->conf->pics_icra_pgmaterial, "ICRA pgmaterial"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "va ", sess->conf->pics_icra_violencerape, "ICRA violencerape"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vb ", sess->conf->pics_icra_violencetohumans, "ICRA violencetohumans"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vc ", sess->conf->pics_icra_violencetoanimals, "ICRA violencetoanimals"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vd ", sess->conf->pics_icra_violencetofantasy, "ICRA violencetofantasy"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "ve ", sess->conf->pics_icra_violencekillinghumans, "ICRA violencekillinghumans"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vf ", sess->conf->pics_icra_violencekillinganimals, "ICRA violencekillinganimals"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vg ", sess->conf->pics_icra_violencekillingfantasy, "ICRA violencekillingfantasy"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vh ", sess->conf->pics_icra_violenceinjuryhumans, "ICRA violenceinjuryhumans"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vi ", sess->conf->pics_icra_violenceinjuryanimals, "ICRA violenceinjuryanimals"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vj ", sess->conf->pics_icra_violenceinjuryfantasy, "ICRA violenceinjuryfantasy"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vr ", sess->conf->pics_icra_violenceartisitic, "ICRA violenceartisitic"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vs ", sess->conf->pics_icra_violenceeducational, "ICRA violenceeducational"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vt ", sess->conf->pics_icra_violencemedical, "ICRA violencemedical"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vu ", sess->conf->pics_icra_violencesports, "ICRA violencesports"); if (sess->isnaughty) {return;} checkPICSagainstoption(sess,r, "vk ", sess->conf->pics_icra_violenceobjects, "ICRA violenceobjects"); } static void checkPICSratingWeburbia (confilter_session_t *sess, char *r) { confilter_out("\n"); checkPICSagainstoption(sess, r, "s ", sess->conf->pics_weburbia_rating, "Weburbia rating"); } static void checkPICSratingVancouver (confilter_session_t *sess, char *r) { checkPICSagainstoption(sess, r, "MC ", sess->conf->pics_vancouver_multiculturalism, "Vancouvermulticulturalism"); checkPICSagainstoption(sess, r, "Edu ", sess->conf->pics_vancouver_educationalcontent, "Vancouvereducationalcontent"); checkPICSagainstoption(sess, r, "Env ", sess->conf->pics_vancouver_environmentalawareness, "Vancouverenvironmentalawareness"); checkPICSagainstoption(sess, r, "Tol ", sess->conf->pics_vancouver_tolerance, "Vancouvertolerance"); checkPICSagainstoption(sess, r, "V ", sess->conf->pics_vancouver_violence, "Vancouverviolence"); checkPICSagainstoption(sess, r, "S ", sess->conf->pics_vancouver_sex, "Vancouversex"); checkPICSagainstoption(sess, r, "P ", sess->conf->pics_vancouver_profanity, "Vancouverprofanity"); checkPICSagainstoption(sess, r, "SF ", sess->conf->pics_vancouver_safety, "Vancouversafety"); checkPICSagainstoption(sess, r, "Can ", sess->conf->pics_vancouver_canadiancontent, "Vancouvercanadiancontent"); checkPICSagainstoption(sess, r, "Com ", sess->conf->pics_vancouver_commercialcontent, "Vancouvercommercialcontent"); checkPICSagainstoption(sess, r, "Gam ", sess->conf->pics_vancouver_gambling, "Vancouvergambling"); } static void checkPICSagainstoption (confilter_session_t *sess, char *s, char* l, int opt, char *m) { int i; char *p; char *o; confilter_out("ready rating[%s] syntax[%s] opt [%d] contents[%s]\n", s, l, opt, m); if (strindexof (s, l) != -1 ) { confilter_out("find1\n"); i = 0; /* * s = 1 + strstr (s, l); if ( strindexof (s, " ") != -1 ) { confilter_out("find2\n"); s = strstr (s, " ") -1; confilter_out("find3 s(%s)\n", s); } */ p = strstr (s, l); i = strlen (l); p = p + i; if ( strindexof (p, " ") != -1 ) { o = strstr (p, " "); *o = '\0'; } if ( strlen (p) > 0 ) { i = atoi (p); if (opt < i) { confilter_out("------------------------> got it [opt:%d, i:%d\n", opt, i); sess->isnaughty = TRUE; } } } } --- NEW FILE: regexp.c --- /* Title : regexp.c Author : Jeho-Park <lin...@gm...> Created date : 2006. 04. 06. (thu) 23:54:35 KST Description : regular expression. ported from dansgardian. */ #ident "@(#) $Header: /cvsroot/netadm/gwc/gwclib/regexp.c,v 1.1 2006/04/06 15:10:53 linuxpark Exp $" #include <stdio.h> #include <sys/types.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <regex.h> #include "sysutil.h" #include "naufilter.h" #include "regexp.h" extern int errno; static void free_res (void *p) { confilter_out ("\n"); free (p); } int regexp_init (regexp_t *regx) { confilter_out ("\n"); /* TODO: init list */ regx->results = list_new(); if (!regx->results) return FALSE; else regx->results->del = free_res; regx->offsets = list_new(); if (!regx->offsets) return FALSE; else regx->offsets->del = free_res; regx->lengths = list_new(); if (!regx->offsets) return FALSE; else regx->lengths->del = free_res; regx->imatched = FALSE; regx->wascompiled = FALSE; return TRUE; } int regexp_clean (regexp_t *regx) { confilter_out ("\n"); list_delete_all_node (regx->results); list_delete_all_node (regx->offsets); list_delete_all_node (regx->lengths); if (regx->wascompiled != FALSE) { regfree(®x->reg); } return TRUE; } int regexp_destory (regexp_t *regx) { confilter_out ("\n"); list_delete (regx->results); list_delete (regx->offsets); list_delete (regx->lengths); if (regx->wascompiled != FALSE) { regfree(®x->reg); } return TRUE; } int regexp_copy (regexp_t *dst, regexp_t *src) { struct listnode *n; node_s_t *src_s; node_s_t *dst_s; node_v_t *src_v; node_v_t *dst_v; char errmsg [SZMAXSTR]; int ret; confilter_out ("\n"); list_delete_all_node (dst->results); list_delete_all_node (dst->offsets); list_delete_all_node (dst->lengths); LIST_LOOP (src->results, src_s, n) { if (src_s) { dst_s = (node_s_t *)malloc (sizeof (node_s_t)); if (!dst_s){ confilter_out ("Failed to malloc node for result list\n"); return FALSE; } strcpy (dst_s->s, src_s->s); /* TODO: buffer overflow */ listnode_add (dst->results, dst_s); } }/* end LIST_LOOP */ LIST_LOOP (src->offsets, src_v, n) { if (src_v) { dst_v = (node_v_t *)malloc (sizeof (node_v_t)); if (!dst_v){ confilter_out ("Failed to malloc node for offset list\n"); return FALSE; } dst_v->v = src_v->v; listnode_add (dst->offsets, dst_v); } }/* end LIST_LOOP */ LIST_LOOP (src->lengths, src_v, n) { if (src_v) { dst_v = (node_v_t *)malloc (sizeof (node_v_t)); if (!dst_v){ confilter_out ("Failed to malloc node for length list\n"); return FALSE; } dst_v->v = src_v->v; listnode_add (dst->lengths, dst_v); } }/* end LIST_LOOP */ dst->imatched = src->imatched; dst->wascompiled = src->wascompiled; strcpy (dst->searchstring, src->searchstring); if (dst->wascompiled != FALSE) { if ((ret = regcomp (&dst->reg, dst->searchstring, REG_ICASE | REG_EXTENDED))){ regerror (ret, &dst->reg, errmsg, sizeof (errmsg)); confilter_out ("Failed to regcomp: %s\n", errmsg); regfree (&dst->reg); dst->imatched = FALSE; dst->wascompiled = FALSE; } } return TRUE; } int regexp_comp (regexp_t *regx, const char* exp) { char errmsg [SZMAXSTR]; int ret; confilter_out ("\n"); if (regx->wascompiled != FALSE) { regfree (®x->reg); regx->wascompiled = FALSE; } if (!regx->results && !regx->offsets && !regx->offsets) { /* seems first. so, initialize regexp */ if ( regexp_init (regx) != TRUE ) { confilter_out ("Failed to initialize regexp\n"); return FALSE; } } else { list_delete_all_node (regx->results); list_delete_all_node (regx->offsets); list_delete_all_node (regx->lengths); regx->imatched = FALSE; } if ((ret = regcomp(®x->reg, exp, REG_ICASE | REG_EXTENDED))) { regerror (ret, ®x->reg, errmsg, sizeof (errmsg)); confilter_out ("Failed to regcomp: %s\n", errmsg); regfree(®x->reg); return FALSE; } regx->wascompiled = TRUE; strcpy (regx->searchstring, exp); return TRUE; } int regexp_match (regexp_t *regx, const char* text) { char *pos = (char*)text; int i; regmatch_t *pmatch = NULL; regmatch_t *o_pmatch = NULL; size_t matchlen; char *submatch; unsigned int largestoffset; char errmsg [SZMAXSTR]; int ret; int error = 0; node_s_t *node_s; node_v_t *node_v; confilter_out ("\n"); if (regx->wascompiled != TRUE) { confilter_out ("reg was not compiled\n"); return FALSE; } confilter_out ("\n"); list_delete_all_node (regx->results); list_delete_all_node (regx->offsets); list_delete_all_node (regx->lengths); confilter_out ("\n"); regx->imatched = FALSE; pmatch = (regmatch_t *)malloc (sizeof (regmatch_t) * (regx->reg.re_nsub + 1)); if (!pmatch) { confilter_out ("Failed to malloc of pmatch\n"); regx->imatched = FALSE; return FALSE; } o_pmatch = pmatch; confilter_out ("\n"); if ((ret = regexec(®x->reg, pos, regx->reg.re_nsub + 1, pmatch, 0))) { confilter_out ("\n"); regerror (ret, ®x->reg, errmsg, sizeof (errmsg)); confilter_out ("\n"); confilter_out ("Failed to regexec: %s\n", errmsg); confilter_out ("no match for: %s\n", regx->searchstring); free (o_pmatch); confilter_out ("\n"); regx->imatched = FALSE; return TRUE; } confilter_out ("------------> ! Find match for: %s\n", regx->searchstring); while (!error) { largestoffset = 0; confilter_out ("\n"); for (i = 0; i <= (signed)regx->reg.re_nsub; i++, pmatch++) { confilter_out ("\n"); if (pmatch->rm_so != -1) { confilter_out ("\n"); matchlen = pmatch->rm_eo - pmatch->rm_so; confilter_out ("\n"); submatch = (char *)malloc (matchlen + 1); if (!submatch) { confilter_out ("Failed to malloc for submatch\n"); free (o_pmatch); return FALSE; } confilter_out ("\n"); strncpy(submatch, pos + pmatch->rm_so, matchlen); confilter_out ("\n"); *(submatch + matchlen) = '\0'; confilter_out ("\n"); #if 0 results.push_back(std::string(submatch)); offsets.push_back(pmatch[i].rm_so + (pos - text)); lengths.push_back(matchlen); #endif /* add submatch to result list */ confilter_out ("\n"); node_s = (node_s_t *)malloc (sizeof (node_s_t)); confilter_out ("\n"); if (!node_s){ confilter_out ("Failed to malloc node for result list\n"); free (o_pmatch); free (submatch); return FALSE; } confilter_out ("\n"); strcpy (node_s->s, submatch); confilter_out ("\n"); listnode_add (regx->results, node_s); /* add offset to offsets list */ node_v = (node_v_t *)malloc (sizeof (node_v_t)); if (!node_v){ confilter_out ("Failed to malloc node for offset list\n"); free (o_pmatch); free (submatch); return FALSE; } confilter_out ("\n"); node_v->v = pmatch->rm_so + (pos - text); listnode_add (regx->offsets, node_v); confilter_out ("\n"); /* add matchlen to length list */ node_v = (node_v_t *)malloc (sizeof (node_v_t)); if (!node_v){ confilter_out ("Failed to malloc node for offset list\n"); free (o_pmatch); free (submatch); return FALSE; } confilter_out ("\n"); node_v->v = matchlen; listnode_add (regx->offsets, node_v); confilter_out ("\n"); free (submatch); if ((pmatch->rm_so + matchlen) > largestoffset) { confilter_out ("\n"); largestoffset = pmatch->rm_so + matchlen; } }/* if (pmatch[i].rm_so != -1) */ } confilter_out ("\n"); if (largestoffset > 0) { confilter_out ("\n"); pos += largestoffset; error = regexec(®x->reg, pos, regx->reg.re_nsub + 1, pmatch, REG_NOTBOL); } else { confilter_out ("\n"); error = -1; } } confilter_out ("\n"); regx->imatched = TRUE; free (o_pmatch); confilter_out ("match(s) for:%s\n", regx->searchstring); return TRUE; } char * regexp_result(regexp_t *regx, int i) { #if 0 std::string RegExp::result(int i) { if (i >= (signed)results.size() || i < 0) { // reality check return ""; // maybe exception? } return results[i]; } #endif struct listnode *n; node_s_t *src_s; int cnt = 0; confilter_out ("\n"); if (i >= listcount (regx->results) || i < 0 ) { confilter_out ("index break limit, i(%d), node count of result list(%d)\n", i, listcount (regx->results)); return ""; } confilter_out ("\n"); LIST_LOOP (regx->results, src_s, n) { confilter_out ("\n"); if (src_s && cnt++ == i) { confilter_out ("\n"); return src_s->s; } } confilter_out ("Failed to find i(%d), node count of result list(%d)\n", i, listcount (regx->results)); return ""; } unsigned int regexp_offset(regexp_t *regx, int i) { #if 0 unsigned int RegExp::offset(int i) { if (i >= (signed)offsets.size() || i < 0) { // reality check return 0; // maybe exception? } return offsets[i]; } #endif struct listnode *n; node_v_t *src_v; int cnt = 0; confilter_out ("\n"); if (i >= listcount (regx->offsets) || i < 0 ) { confilter_out ("index break limit, i(%d), node count of offsets(%d)\n", i, listcount (regx->offsets)); return FALSE; } LIST_LOOP (regx->offsets, src_v, n) { if (src_v && cnt++ == i) { return src_v->v; } } confilter_out ("Failed to find i(%d), node count of offsets list(%d)\n", i, listcount (regx->offsets)); return FALSE; /* TODO : offset can be zero == FALSE */ } unsigned int regexp_length(regexp_t *regx, int i) { #if 0 unsigned int RegExp::length(int i) { if (i >= (signed)lengths.size() || i < 0) { // reality check return 0; // maybe exception? } return lengths[i]; } #endif struct listnode *n; node_v_t *src_v; int cnt = 0; confilter_out ("\n"); if (i >= listcount (regx->lengths) || i < 0 ) { confilter_out ("index break limit, i(%d), node count of lengths list(%d)\n", i, listcount (regx->lengths)); return FALSE; } LIST_LOOP (regx->lengths, src_v, n) { if (src_v && cnt++ == i) { return src_v->v; } } confilter_out ("Failed to find i(%d), node count of lengths list(%d)\n", i, listcount (regx->lengths)); return FALSE;/* TODO : length can be zero == FALSE */ } int regexp_numberOfMatches(regexp_t *regx) { #if 0 int RegExp::numberOfMatches() { int i = (signed)results.size(); return i; } #endif confilter_out ("\n"); return listcount (regx->results); } int regexp_matched(regexp_t *regx) { #if 0 bool RegExp::matched() { return imatched; // regexp matches only } #endif confilter_out ("\n"); return regx->imatched; } char* regexp_search(char* file, char* fileend, char* phrase, char* phraseend) { #if 0 char* RegExp::search(char* file, char* fileend, char* phrase, char* phraseend) { int j, l; // counters int p; // to hold precalcuated value for speed bool match; // flag int qsBc[256]; // Quick Search Boyer Moore shift table (256 alphabet) char* k; // pointer used in matching int pl = phraseend - phrase; // phrase length int fl = (int)(fileend - file) - pl; // file length that could match if (fl < pl) return fileend; // reality checking if (pl > 126) return fileend; // reality checking // For speed we append the phrase to the end of the memory block so it // is always found, thus eliminating some checking. This is possible as // we know an extra 127 bytes have been provided by NaughtyFilter.cpp // and also the OptionContainer does not allow phrase lengths greater // than 126 chars for(j = 0; j < pl; j++) { fileend[j] = phrase[j]; } // Next we need to make the Quick Search Boyer Moore shift table p = pl + 1; for (j = 0; j < 256; j++) { // Preprocessing qsBc[j] = p; } for (j = 0; j < pl; j++) { // Preprocessing qsBc[(unsigned char)phrase[j]] = pl - j; } // Now do the searching! for(j = 0;;) { k = file + j; match = true; for (l = 0; l < pl; l++) { // quiv, but faster, memcmp() if (k[l] != phrase[l]) { match = false; break; } } if (match) { return (j + file); // match found at offset j (but could be the // copy put at fileend) } j += qsBc[(unsigned char)file[j + pl]]; // shift } return fileend; // should never get here as it should always match } #endif confilter_out ("\n"); return NULL; } |