⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstring.c

📁 字符串匹配算法
💻 C
📖 第 1 页 / 共 2 页
字号:
            if(m_cnt == (p_len - 1))            {		DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,					"\n%ld compares for match\n", loopcnt););                return 1;            }            m_cnt++;            b_idx++;            p_idx++;        }        else        {            if(m_cnt == 0)            {                b_idx++;            }            else            {                b_idx = b_idx - (m_cnt - 1);            }            p_idx = pat;            m_cnt = 0;        }    } while(b_idx < b_end);    /* if we make it here we didn't find what we were looking for */    return 0;}/**************************************************************** * *  Function: make_skip(char *, int) * *  Purpose: Create a Boyer-Moore skip table for a given pattern * *  Parameters: *      ptrn => pattern *      plen => length of the data in the pattern buffer * *  Returns: *      int * - the skip table * ****************************************************************/int *make_skip(char *ptrn, int plen){    int *skip = (int *) malloc(256 * sizeof(int));    int *sptr = &skip[256];    if (skip == NULL)        FatalPrintError("malloc");    while(sptr-- != skip)        *sptr = plen + 1;    while(plen != 0)        skip[(unsigned char) *ptrn++] = plen--;    return skip;}/**************************************************************** * *  Function: make_shift(char *, int) * *  Purpose: Create a Boyer-Moore shift table for a given pattern * *  Parameters: *      ptrn => pattern *      plen => length of the data in the pattern buffer * *  Returns: *      int * - the shift table * ****************************************************************/int *make_shift(char *ptrn, int plen){    int *shift = (int *) malloc(plen * sizeof(int));    int *sptr = shift + plen - 1;    char *pptr = ptrn + plen - 1;    char c;    if (shift == NULL)        FatalPrintError("malloc");     c = ptrn[plen - 1];    *sptr = 1;    while(sptr-- != shift)    {        char *p1 = ptrn + plen - 2, *p2, *p3;        do        {            while(p1 >= ptrn && *p1-- != c);            p2 = ptrn + plen - 2;            p3 = p1;            while(p3 >= ptrn && *p3-- == *p2-- && p2 >= pptr);        }        while(p3 >= ptrn && p2 >= pptr);        *sptr = shift + plen - sptr + p2 - p3;        pptr--;    }    return shift;}/**************************************************************** * *  Function: mSearch(char *, int, char *, int) * *  Purpose: Determines if a string contains a (non-regex) *           substring. * *  Parameters: *      buf => data buffer we want to find the data in *      blen => data buffer length *      ptrn => pattern to find *      plen => length of the data in the pattern buffer *      skip => the B-M skip array *      shift => the B-M shift array * *  Returns: *      Integer value, 1 on success (str constains substr), 0 on *      failure (substr not in str) * ****************************************************************/int mSearch(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift){    int b_idx = plen;#ifdef DEBUG    char *hexbuf;    int cmpcnt = 0;#endif    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,"buf: %p  blen: %d  ptrn: %p  "                "plen: %d\n", buf, blen, ptrn, plen););#ifdef DEBUG    hexbuf = fasthex(buf, blen);    DebugMessage(DEBUG_PATTERN_MATCH,"buf: %s\n", hexbuf);    free(hexbuf);    hexbuf = fasthex(ptrn, plen);    DebugMessage(DEBUG_PATTERN_MATCH,"ptrn: %s\n", hexbuf);    free(hexbuf);    DebugMessage(DEBUG_PATTERN_MATCH,"buf: %p  blen: %d  ptrn: %p  "                 "plen: %d\n", buf, blen, ptrn, plen);#endif /* DEBUG */    if(plen == 0)        return 1;    while(b_idx <= blen)    {        int p_idx = plen, skip_stride, shift_stride;        while(buf[--b_idx] == ptrn[--p_idx])        {#ifdef DEBUG            cmpcnt++;#endif            if(b_idx < 0)                return 0;            if(p_idx == 0)            {                DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,                             "match: compares = %d.\n", cmpcnt););                doe_ptr = &(buf[b_idx]) + plen;                return 1;            }        }        skip_stride = skip[(unsigned char) buf[b_idx]];        shift_stride = shift[p_idx];        b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;    }    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,                "no match: compares = %d.\n", cmpcnt););    return 0;}/**************************************************************** * *  Function: mSearchCI(char *, int, char *, int) * *  Purpose: Determines if a string contains a (non-regex) *           substring matching is case insensitive * *  Parameters: *      buf => data buffer we want to find the data in *      blen => data buffer length *      ptrn => pattern to find *      plen => length of the data in the pattern buffer *      skip => the B-M skip array *      shift => the B-M shift array * *  Returns: *      Integer value, 1 on success (str constains substr), 0 on *      failure (substr not in str) * ****************************************************************/int mSearchCI(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift){    int b_idx = plen;#ifdef DEBUG    int cmpcnt = 0;#endif    if(plen == 0)        return 1;    while(b_idx <= blen)    {        int p_idx = plen, skip_stride, shift_stride;        while((unsigned char) ptrn[--p_idx] ==                 toupper((unsigned char) buf[--b_idx]))        {#ifdef DEBUG            cmpcnt++;#endif            if(p_idx == 0)            {                DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,                             "match: compares = %d.\n",                             cmpcnt););                doe_ptr = &(buf[b_idx]) + plen;                return 1;            }        }        skip_stride = skip[toupper((unsigned char) buf[b_idx])];        shift_stride = shift[p_idx];        b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;    }    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "no match: compares = %d.\n", cmpcnt););    return 0;}/**************************************************************** * *  Function: mSearchREG(char *, int, char *, int) * *  Purpose: Determines if a string contains a (regex) *           substring. * *  Parameters: *      buf => data buffer we want to find the data in *      blen => data buffer length *      ptrn => pattern to find *      plen => length of the data in the pattern buffer *      skip => the B-M skip array *      shift => the B-M shift array * *  Returns: *      Integer value, 1 on success (str constains substr), 0 on *      failure (substr not in str) * ****************************************************************/int mSearchREG(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift){    int b_idx = plen;    int literal = 0;    int regexcomp = 0;#ifdef DEBUG    int cmpcnt = 0;#endif /*DEBUG*/        DEBUG_WRAP(	       DebugMessage(DEBUG_PATTERN_MATCH, "buf: %p  blen: %d  ptrn: %p "			    " plen: %d b_idx: %d\n", buf, blen, ptrn, plen, b_idx);	       DebugMessage(DEBUG_PATTERN_MATCH, "packet data: \"%s\"\n", buf);	       DebugMessage(DEBUG_PATTERN_MATCH, "matching for \"%s\"\n", ptrn);	       );	           if(plen == 0)        return 1;    while(b_idx <= blen)    {        int p_idx = plen, skip_stride, shift_stride;	DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "Looping... "				"([%d]0x%X (%c) -> [%d]0x%X(%c))\n",				b_idx, buf[b_idx-1], 				buf[b_idx-1], 				p_idx, ptrn[p_idx-1], ptrn[p_idx-1]););        while(buf[--b_idx] == ptrn[--p_idx]              || (ptrn[p_idx] == '?' && !literal)              || (ptrn[p_idx] == '*' && !literal)              || (ptrn[p_idx] == '\\' && !literal))        {	    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "comparing: b:%c -> p:%c\n", 				    buf[b_idx], ptrn[p_idx]););#ifdef DEBUG            cmpcnt++;#endif            if(literal)                literal = 0;            if(!literal && ptrn[p_idx] == '\\')                literal = 1;            if(ptrn[p_idx] == '*')            {		DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,"Checking wildcard matching...\n"););                while(p_idx != 0 && ptrn[--p_idx] == '*'); /* fool-proof */                while(buf[--b_idx] != ptrn[p_idx])                {		    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "comparing: b[%d]:%c -> p[%d]:%c\n",					    b_idx, buf[b_idx], p_idx, ptrn[p_idx]););                   regexcomp++;                    if(b_idx == 0)                    {			DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,						"b_idx went to 0, returning 0\n");)                        return 0;                    }                }		DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "got wildcard final char match! (b[%d]: %c -> p[%d]: %c\n", b_idx, buf[b_idx], p_idx, ptrn[p_idx]););            }            if(p_idx == 0)            {		DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "match: compares = %d.\n", 					cmpcnt););                return 1;            }            if(b_idx == 0)                break;        }	DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "skip-shifting...\n"););	skip_stride = skip[(unsigned char) buf[b_idx]];	shift_stride = shift[p_idx];		b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;	DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "b_idx skip-shifted to %d\n", b_idx););	b_idx += regexcomp;	DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH,				"b_idx regex compensated %d steps, to %d\n", regexcomp, b_idx););	regexcomp = 0;    }    DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "no match: compares = %d, b_idx = %d, "			    "blen = %d\n", cmpcnt, b_idx, blen););    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -