Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / layer7 / protocols / testing / test_speed-kernel.c
blob0c9ec37987a019db3df7975d52eee05a04c68b5b
1 /* Reads in up to MAX bytes and runs regcomp against them TIMES times, using
2 the regular expression given on the command line.
4 Uses the Henry Spencer V8 regular expressions which the kernel version of
5 l7-filter uses.
7 See ../LICENCE for copyright
8 */
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "regexp/regexp.c"
17 #define MAX 1500
18 #define TIMES 100000
19 #define MAX_PATTERN_LEN 8192
21 static int hex2dec(char c)
23 switch (c)
25 case '0' ... '9':
26 return c - '0';
27 case 'a' ... 'f':
28 return c - 'a' + 10;
29 case 'A' ... 'F':
30 return c - 'A' + 10;
31 default:
32 fprintf(stderr, "hex2dec: bad value!\n");
33 exit(1);
37 /* takes a string with \xHH escapes and returns one with the characters
38 they stand for */
39 static char * pre_process(char * s)
41 char * result = malloc(strlen(s) + 1);
42 int sindex = 0, rindex = 0;
43 while( sindex < strlen(s) )
45 if( sindex + 3 < strlen(s) &&
46 s[sindex] == '\\' && s[sindex+1] == 'x' &&
47 isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
49 /* carefully remember to call tolower here... */
50 result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
51 hex2dec(s[sindex + 3] ) );
52 sindex += 3; /* 4 total */
54 else
55 result[rindex] = tolower(s[sindex]);
57 sindex++;
58 rindex++;
60 result[rindex] = '\0';
62 return result;
66 void doit(regexp * pattern, char ** argv, int verbose)
68 char input[MAX];
69 int c;
71 for(c = 0; c < MAX; c++){
72 char temp = 0;
73 while(temp == 0){
74 if(EOF == scanf("%c", &temp))
75 goto out;
76 input[c] = temp;
79 out:
81 input[c-1] = '\0';
83 for(c = 0; c < MAX; c++) input[c] = tolower(input[c]);
85 for(c = 1; c < TIMES; c++){
86 int result = regexec(pattern, input);
87 if(c == 1)
88 if(result)
89 printf("match\t");
90 else
91 printf("no_match\t");
93 if(TIMES/20 > 0 && c%(TIMES/20) == 0){ fprintf(stderr, "."); }
95 if(verbose)
96 puts("");
97 else
98 printf(" ");
101 // Syntax: test_speed regex [verbose]
102 int main(int argc, char ** argv)
104 regexp * pattern = (regexp *)malloc(sizeof(struct regexp));
105 char * s = argv[1];
106 int patternlen, i, verbose = 0;
108 if(argc < 2){
109 fprintf(stderr, "need an arg\n");
110 return 1;
112 if(argc > 2)
113 verbose = 1;
115 patternlen = strlen(s);
116 if(patternlen > MAX_PATTERN_LEN){
117 fprintf(stderr, "Pattern too long! Max is %d\n", MAX_PATTERN_LEN);
118 return 1;
121 s = pre_process(s); /* do \xHH escapes */
123 pattern = regcomp(s, &patternlen);
125 if(!pattern){
126 fprintf(stderr, "error compiling regexp\n");
127 exit(1);
130 if(verbose)
131 printf("running regexec \"%.16s...\" %d times\n", argv[1], TIMES);
133 doit(pattern, argv, verbose);
135 return 0;