xref: /OK3568_Linux_fs/u-boot/include/slre.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3*4882a593Smuzhiyun  * All rights reserved
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * "THE BEER-WARE LICENSE" (Revision 42):
6*4882a593Smuzhiyun  * Sergey Lyubka wrote this file.  As long as you retain this notice you
7*4882a593Smuzhiyun  * can do whatever you want with this stuff. If we meet some day, and you think
8*4882a593Smuzhiyun  * this stuff is worth it, you can buy me a beer in return.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * Downloaded Sat Nov  5 17:42:08 CET 2011 at
13*4882a593Smuzhiyun  * http://slre.sourceforge.net/1.0/slre.h
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun  * This is a regular expression library that implements a subset of Perl RE.
18*4882a593Smuzhiyun  * Please refer to http://slre.sourceforge.net for detailed description.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Usage example (parsing HTTP request):
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * struct slre	slre;
23*4882a593Smuzhiyun  * struct cap	captures[4 + 1];  // Number of braket pairs + 1
24*4882a593Smuzhiyun  * ...
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * if (slre_match(&slre, buf, len, captures)) {
29*4882a593Smuzhiyun  *	printf("Request line length: %d\n", captures[0].len);
30*4882a593Smuzhiyun  *	printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
31*4882a593Smuzhiyun  *	printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
32*4882a593Smuzhiyun  * }
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * Supported syntax:
35*4882a593Smuzhiyun  *	^		Match beginning of a buffer
36*4882a593Smuzhiyun  *	$		Match end of a buffer
37*4882a593Smuzhiyun  *	()		Grouping and substring capturing
38*4882a593Smuzhiyun  *	[...]		Match any character from set
39*4882a593Smuzhiyun  *	[^...]		Match any character but ones from set
40*4882a593Smuzhiyun  *	\s		Match whitespace
41*4882a593Smuzhiyun  *	\S		Match non-whitespace
42*4882a593Smuzhiyun  *	\d		Match decimal digit
43*4882a593Smuzhiyun  *	\r		Match carriage return
44*4882a593Smuzhiyun  *	\n		Match newline
45*4882a593Smuzhiyun  *	+		Match one or more times (greedy)
46*4882a593Smuzhiyun  *	+?		Match one or more times (non-greedy)
47*4882a593Smuzhiyun  *	*		Match zero or more times (greedy)
48*4882a593Smuzhiyun  *	*?		Match zero or more times (non-greedy)
49*4882a593Smuzhiyun  *	?		Match zero or once
50*4882a593Smuzhiyun  *	\xDD		Match byte with hex value 0xDD
51*4882a593Smuzhiyun  *	\meta		Match one of the meta character: ^$().[*+?\
52*4882a593Smuzhiyun  */
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #ifndef SLRE_HEADER_DEFINED
55*4882a593Smuzhiyun #define	SLRE_HEADER_DEFINED
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * Compiled regular expression
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun struct slre {
61*4882a593Smuzhiyun 	unsigned char	code[256];
62*4882a593Smuzhiyun 	unsigned char	data[256];
63*4882a593Smuzhiyun 	int		code_size;
64*4882a593Smuzhiyun 	int		data_size;
65*4882a593Smuzhiyun 	int		num_caps;	/* Number of bracket pairs	*/
66*4882a593Smuzhiyun 	int		anchored;	/* Must match from string start	*/
67*4882a593Smuzhiyun 	const char	*err_str;	/* Error string			*/
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun  * Captured substring
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun struct cap {
74*4882a593Smuzhiyun 	const char	*ptr;		/* Pointer to the substring	*/
75*4882a593Smuzhiyun 	int		len;		/* Substring length		*/
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * Compile regular expression. If success, 1 is returned.
80*4882a593Smuzhiyun  * If error, 0 is returned and slre.err_str points to the error message.
81*4882a593Smuzhiyun  */
82*4882a593Smuzhiyun int slre_compile(struct slre *, const char *re);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * Return 1 if match, 0 if no match.
86*4882a593Smuzhiyun  * If `captured_substrings' array is not NULL, then it is filled with the
87*4882a593Smuzhiyun  * values of captured substrings. captured_substrings[0] element is always
88*4882a593Smuzhiyun  * a full matched substring. The round bracket captures start from
89*4882a593Smuzhiyun  * captured_substrings[1].
90*4882a593Smuzhiyun  * It is assumed that the size of captured_substrings array is enough to
91*4882a593Smuzhiyun  * hold all captures. The caller function must make sure it is! So, the
92*4882a593Smuzhiyun  * array_size = number_of_round_bracket_pairs + 1
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun int slre_match(const struct slre *, const char *buf, int buf_len,
95*4882a593Smuzhiyun 	struct cap *captured_substrings);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #ifdef SLRE_TEST
98*4882a593Smuzhiyun void slre_dump(const struct slre *r, FILE *fp);
99*4882a593Smuzhiyun #endif /* SLRE_TEST */
100*4882a593Smuzhiyun #endif /* SLRE_HEADER_DEFINED */
101