forked from jonasfj/trilite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanstr.c
42 lines (35 loc) · 1.27 KB
/
scanstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "scanstr.h"
#include "config.h"
#include <stdint.h>
#include <string.h>
/** An adoption of Railgun_Doublet by Georgi 'Kaze' <[email protected]>,
* See: http://www.sanmayce.com/Railgun/index.html */
const unsigned char* scanstr(const unsigned char *text, int nText, const unsigned char *pattern, int nPattern){
const unsigned char * pbTarget = text;
const unsigned char * pbPattern = pattern;
uint32_t cbTarget = nText;
uint32_t cbPattern = nPattern;
if(cbPattern == 0)
return pbTarget;
if(cbPattern == 1)
return memchr(pbTarget, *pbPattern, cbTarget);
const unsigned char * pbTargetMax = pbTarget + cbTarget;
register uint32_t ulHashPattern;
uint32_t ulHashTarget, count, countSTATIC;
UNUSED_PARAMETER(ulHashTarget);
if (cbPattern > cbTarget) return(NULL);
countSTATIC = cbPattern-2;
pbTarget = pbTarget+cbPattern;
ulHashPattern = (*(uint16_t *)(pbPattern));
for ( ;; ) {
if ( ulHashPattern == (*(uint16_t *)(pbTarget-cbPattern)) ) {
count = countSTATIC;
while ( count && *(const unsigned char *)(pbPattern+2+(countSTATIC-count)) == *(const unsigned char *)(pbTarget-cbPattern+2+(countSTATIC-count)) ) {
count--;
}
if ( count == 0 ) return((pbTarget-cbPattern));
}
pbTarget++;
if (pbTarget > pbTargetMax) return(NULL);
}
}