int KMP(const char *x, int m, getblock_function *getter, void *cookie) { int i, j, k, kmpNext[m]; const char *text; int len; /* Preprocessing */ preKmp(x, m, kmpNext); /* Searching */ i = j = 0; for (;;) { len = getter(&text, cookie); if (len == 0) break; for (k = 0; k < len; k++) { while (i > -1 && x[i] != text[k]) { i = kmpNext[i]; } i++; j++; if (i >= m) { /* We return the position just after the match. * This is where our getter input is left */ return j; } } } /* Not found */ return -1; }
void* preSkmp(char* pat, int m) { if(m == 0) m = strlen(pat); structSkmp* s= (structSkmp*)malloc(sizeof(structSkmp));; preKmp(pat,m,s->kmpNext); return (void*) s; }
FLONG FindInBinaryPOS(char *x, int m, char *y, FULONG n) { FLONG i, j; int kmpNext[ m ]; // Preprocessing preKmp(x, m, kmpNext); // Searching i = j = 0; while (j < (FLONG)n) { //printf("find %d\n", j ); while (i > -1 && x[i] != y[j]) { i = kmpNext[ i ]; } i++; j++; if (i >= m) { return j-i; } } return -1; }
char *FindInBinary(char *x, int m, char *y, int n) { int i, j, kmpNext[ m ]; if( y == NULL ) { return NULL; } // Preprocessing preKmp(x, m, kmpNext); // Searching i = j = 0; while (j < n) { while (i > -1 && x[i] != y[j]) { i = kmpNext[i]; } i++; j++; if (i >= m) { //OUTPUT(j - i); //printf("%d", j-i ); return &(y[j-i]); //i = kmpNext[i]; } } return NULL; }
knuth_morris_pratt ( patIter first, patIter last ) : pat_first ( first ), pat_last ( last ), k_pattern_length ( std::distance ( pat_first, pat_last )), skip_ ( k_pattern_length + 1 ) { #ifdef NEW_KMP preKmp ( pat_first, pat_last ); #else init_skip_table ( pat_first, pat_last ); #endif #ifdef BOOST_ALGORITHM_KNUTH_MORRIS_PRATT_DEBUG detail::PrintTable ( skip_.begin (), skip_.end ()); #endif }
void KMP(char *x, int m, char *y, int n) { int i, j, kmpNext[XSIZE]; /* Preprocessing */ preKmp(x, m, kmpNext); /* Searching */ i = j = 0; while (j < n) { while (i > -1 && x[i] != y[j]) i = kmpNext[i]; i++; j++; if (i >= m) { OUTPUT(j - i); i = kmpNext[i]; } } }
VOID CGPacket::Replace(LPBYTE lpKey, DWORD dwKeyLen, LPBYTE lpReplace, DWORD dwReplaceLen) { int i, j, kmpNext[256]; /* Preprocessing */ preKmp((char*)lpKey, dwKeyLen, kmpNext); /* Searching */ i = j = 0; while (j < m_dwSize) { while (i > -1 && lpKey[i] != m_pRawBuf[j]) i = kmpNext[i]; i++; j++; if (i >= dwKeyLen) { //OUTPUT(j - i); memcpy_s(&m_pRawBuf[j - i], m_dwSize - j + i, lpReplace, dwReplaceLen); i = kmpNext[i]; } } }
BOOL CGPacket::Find(LPBYTE lpKey, DWORD dwKeyLen) { int i, j, kmpNext[256]; /* Preprocessing */ preKmp((char*)lpKey, dwKeyLen, kmpNext); /* Searching */ i = j = 0; while (j < m_dwSize) { while (i > -1 && lpKey[i] != m_pBuf[j]) i = kmpNext[i]; i++; j++; if (i >= dwKeyLen) { //OUTPUT(j - i); return TRUE; i = kmpNext[i]; } } return FALSE; }
char* Skmp2(char *text,char *pat, int n, int m) //KMPËã·¨ { int i,j,kmpNext[XSIZE]; /* preprocessing */ preKmp(pat,m,kmpNext); /* searching */ i=j=0; while (j<n) { while (i>-1 && pat[i]!=text[j]) i=kmpNext[i]; i++; j++; if (i>=m) { OUTPUT(j-i); i=kmpNext[i]; } } return NULL; }
int search(unsigned char *x, int m, unsigned char *y, int n) { int i, j, p, b, lf, count, test, kmpNext[XSIZE]; int tp, st; char F[256*256]; unsigned short h; if(m<Q) return -1; BEGIN_PREPROCESSING /* Preprocessing */ preKmp(x, m, kmpNext); int plen = m; if(m%Q!=0) m = m-(m%Q); int mm1 = m-1; int mq = m-Q+1; preprocessing(x,m,F); for(i=0; i<m; i++) y[n+i]=x[i]; END_PREPROCESSING BEGIN_SEARCHING /* Searching */ count = 0; tp = 0; st = 0; if( !memcmp(x,y,plen) ) count++; j=m; while (j < n) { h = HASH(j); while(!F[h]) { j += mq; h = HASH(j); } lf = b = j-m+Q; if(b<tp) b=tp-1; //b is the maximum between lf and tp-1 while((test=F[h]) && j>b) { j-=Q; h = (h<<10) + HASH(j); } // verification if(j<=b && test) { lf -= Q-1; if(lf>tp) { //restart kmp aut tp = lf; st = 0; } while(st >= tp-lf) { while(st<plen && x[st]==y[tp]) { st++; tp++; } if (st == plen && lf<=n-plen) count++; st = kmpNext[st]; if(st<0) { st++; tp++; } } j = tp+mm1-st; } else j+=m-Q+1; } END_SEARCHING return count; }
int search(unsigned char *x, int m, unsigned char *y, int n) { int i, j, k, kmpStart, per, start, wall, count; int kmpNext[XSIZE], list[XSIZE], mpNext[XSIZE], z[SIGMA]; /* Preprocessing */ BEGIN_PREPROCESSING preMp(x, m, mpNext); preKmp(x, m, kmpNext); memset(z, -1, SIGMA*sizeof(int)); memset(list, -1, m*sizeof(int)); z[x[0]] = 0; for (i = 1; i < m; ++i) { list[i] = z[x[i]]; z[x[i]] = i; } END_PREPROCESSING /* Searching */ BEGIN_SEARCHING count = 0; wall = 0; per = m - kmpNext[m]; i = j = -1; do { j += m; } while (j < n && z[y[j]] < 0); if (j >= n) return count; i = z[y[j]]; start = j - i; while (start <= n - m) { if (start > wall) wall = start; k = attempt(y, x, m, start, wall); wall = start + k; if (k == m) { OUTPUT(start); i -= per; } else i = list[i]; if (i < 0) { do { j += m; } while (j < n && z[y[j]] < 0); if (j >= n) return count; i = z[y[j]]; } kmpStart = start + k - kmpNext[k]; k = kmpNext[k]; start = j - i; while (start < kmpStart || (kmpStart < start && start < wall)) { if (start < kmpStart) { i = list[i]; if (i < 0) { do { j += m; } while (j < n && z[y[j]] < 0); if (j >= n) return count; i = z[y[j]]; } start = j - i; } else { kmpStart += (k - mpNext[k]); k = mpNext[k]; } } } END_SEARCHING return count; }
char* Skmpskip2(char *textt,char *patt,int n, int m) { int i, j, k, kmpStart, per, start, wall; unsigned char * text,*pat; text = (unsigned char *) textt; pat = (unsigned char *)patt; int kmpNext[m], list[m], mpNext[m], z[ASIZE]; /* Preprocessing */ preMp((char*)pat, m, mpNext); preKmp((char*)pat, m, kmpNext); memset(z, -1, ASIZE*sizeof(int)); memset(list, -1, m*sizeof(int)); z[pat[0]] = 0; for (i = 1; i < m; ++i) { list[i] = z[pat[i]]; z[pat[i]] = i; } /* Searching */ wall = 0; per = m - kmpNext[m]; i = j = -1; do { j += m; } while (j < n && z[text[j]] < 0); if (j >= n) return NULL; i = z[text[j]]; start = j - i; while (start <= n - m) { if (start > wall) wall = start; k = attempt((char*)text, (char*)pat, m, start, wall); wall = start + k; if (k == m) { OUTPUT(start); i -= per; } else i = list[i]; if (i < 0) { do { j += m; } while (j < n && z[text[j]] < 0); if (j >= n) return NULL; i = z[text[j]]; } kmpStart = start + k - kmpNext[k]; k = kmpNext[k]; start = j - i; while (start < kmpStart || (kmpStart < start && start < wall)) { if (start < kmpStart) { i = list[i]; if (i < 0) { do { j += m; } while (j < n && z[text[j]] < 0); if (j >= n) return NULL; i = z[text[j]]; } start = j - i; } else { kmpStart += (k - mpNext[k]); k = mpNext[k]; } } } SRET(start); }