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