File: match-kernel.c

package info (click to toggle)
l7-protocols 20090528-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,372 kB
  • sloc: ansic: 1,128; cpp: 261; sh: 231; makefile: 30
file content (119 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Reads in a stream of bytes and tests the first MAX of them to see if 
they match the regular expression passed in on the command line.

Uses the Henry Spencer V8 regular expressions which the kernel version of 
l7-filter uses.

See ../LICENCE for copyright.
*/

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "regexp/regexp.c"

#define MAX 512
#define MAX_PATTERN_LEN 8196

static int hex2dec(char c)
{
        switch (c)
        {
                case '0' ... '9':
                        return c - '0';
                case 'a' ... 'f':
                        return c - 'a' + 10;
                case 'A' ... 'F':
                        return c - 'A' + 10;
                default:
			fprintf(stderr, "hex2dec: bad value!\n");
                        exit(1);
        }
}

/* takes a string with \xHH escapes and returns one with the characters
they stand for */
static char * pre_process(char * s)
{
        char * result = malloc(strlen(s) + 1);
        int sindex = 0, rindex = 0;
        while( sindex < strlen(s) )  
        {
            if( sindex + 3 < strlen(s) &&
                s[sindex] == '\\' && s[sindex+1] == 'x' &&
                isxdigit(s[sindex + 2]) && isxdigit(s[sindex + 3]) )
                {
                        /* carefully remember to call tolower here... */
                        result[rindex] = tolower( hex2dec(s[sindex + 2])*16 +
                                                  hex2dec(s[sindex + 3] ) );
                        sindex += 3; /* 4 total */
                }
                else 
                        result[rindex] = tolower(s[sindex]);

                sindex++;  
                rindex++;
        }
        result[rindex] = '\0';

        return result;
}

int main(int argc, char ** argv)
{
	regexp * pattern = (regexp *)malloc(sizeof(struct regexp));
	char * s = argv[1];
	char input[MAX];
	int patternlen, inputlen = 0, c = 0;

	if(argc != 2 || !argv[1]){
		fprintf(stderr, "need exactly one arg (the pattern)\n");
		return 1;
	}
	patternlen = strlen(s);
	if(patternlen > MAX_PATTERN_LEN){
		fprintf(stderr, "Pattern is too long!  Max is %d.\n", MAX_PATTERN_LEN);
		return 1;
	}

//	fprintf(stderr, "\"%s\"", s);

	s = pre_process(s); /* do \xHH escapes */

	pattern = regcomp(s, &patternlen);
		
	if(!pattern){
		fprintf(stderr, "Error compiling regular expression!\n");
		exit(1);
	}

/*
	for(c = 0; c < MAX; c++){
		// assumes there's plenty to eat
		input[inputlen] = getchar();
		inputlen++;
	}
	input[inputlen] = '\0';
*/
        for(c = 0; c < MAX; c++){
                char temp = 0;
                while(temp == 0){
                        if(EOF == scanf("%c", &temp))
                                goto out;
                        input[c] = temp;
                }
        }
        out:

        input[c-1] = '\0';

	inputlen = c;

	for(c = 0; c < inputlen; c++)	input[c] = tolower(input[c]);

	if(regexec(pattern, input))	puts("Match");
	else				puts("No match");

	return 0;
}