bnf  1.12.11devel
bnf.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef BNF_H
26 #define BNF_H
27 
38 #include <sofia-sip/su_types.h>
39 
40 #include <string.h>
41 
42 SOFIA_BEGIN_DECLS
43 
44 /* Parsing tokens */
46 #define CTL "\001\002\003\004\005\006\007" \
47  "\010\011\012\013\014\015\016\017" \
48  "\020\021\022\023\024\025\026\027" \
49  "\030\031\032\033\034\035\036\037" "\177" "\0"
50 
51 #define SP " "
52 
53 #define HT "\t"
54 
55 #define CR "\r"
56 
57 #define LF "\n"
58 
59 #define CRLF CR LF
60 
61 #define WS SP HT
62 
63 #define LWS SP HT CR LF
64 
65 #define LOALPHA "abcdefghijklmnopqrstuvwxyz"
66 
67 #define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
68 
69 #define ALPHA LOALPHA UPALPHA
70 
71 #define DIGIT "0123456789"
72 
73 #define SAFE "$-_." /* RTSP stuff */
74 #define ALPHANUM DIGIT ALPHA
75 #define HEX DIGIT "ABCDEF" "abcdef"
76 
80 #define SIP_TOKEN ALPHANUM "-.!%*_+`'~"
81 
82 #define SIP_SEPARATOR "()<>@,;:\\\"/[]?={}" SP HT
83 
85 #define SIP_WORD "()<>:\\\"/[]?{}"
86 
88 #define skip_ws(ss) (*(ss) += span_ws(*(ss)))
89 
91 #define skip_lws(ss) (*(ss) += span_lws(*(ss)))
92 
94 #define skip_alpha(ss) (*(ss) += span_alpha(*(ss)))
95 
97 #define skip_digit(ss) (*(ss) += span_digit(*(ss)))
98 
100 #define skip_alpha_digit_safe(ss) (*(ss) += span_alpha_digit_safe(*(ss)))
101 
103 #define skip_token(ss) (*(ss) += span_token(*(ss)))
104 
106 #define skip_param(ss) (*(ss) += span_param(*(ss)))
107 
109 #define skip_word(ss) (*(ss) += span_word(*(ss)))
110 
112 #define IS_CRLF(c) ((c) == '\r' || (c) == '\n')
113 
114 #define IS_LWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
115 /*#define IS_LWS(c) ((_bnf_table[(unsigned char)(c)] & bnf_lws))*/
117 #define IS_WS(c) ((c) == ' ' || (c) == '\t')
118 
119 #define IS_NON_WS(c) (c && !IS_WS(c))
120 /*#define IS_NON_WS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_ws))*/
122 #define IS_NON_LWS(c) (c && !IS_LWS(c))
123 /*#define IS_NON_LWS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_lws))*/
125 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
126 
127 #define IS_ALPHA(c) (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
128 
129 #define IS_ALPHANUM(c) (c && (IS_DIGIT(c) || IS_ALPHA(c)))
130 
131 #define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
132 
133 #define IS_RESERVED(c) (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
134 
135 #define IS_TOKEN(c) ((_bnf_table[(unsigned char)c] & bnf_token))
136 
137 #define IS_PARAM(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
138 
139 #define IS_HEX(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
140 
141 #define IS_TOKENLWS(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))
142 
143 enum {
144  bnf_ws = 1,
145  bnf_crlf = 2,
146  bnf_lws = 3,
147  bnf_alpha = 4,
148  bnf_safe = 8,
149  bnf_mark = 16,
155  bnf_param0 = 128,
157 };
158 
160 SOFIAPUBVAR unsigned char const _bnf_table[256];
161 
163 #define span_non_crlf(s) strcspn(s, CR LF)
164 
166 #define span_non_ws(s) strcspn(s, WS)
167 
169 #define span_ws(s) strspn(s, WS)
170 
172 #define span_non_lws(s) strcspn(s, LWS)
173 
177 su_inline isize_t span_lws(char const *s)
178 {
179  char const *e = s;
180  int i = 0;
181  e += strspn(s, WS);
182  if (e[i] == '\r') i++;
183  if (e[i] == '\n') i++;
184  if (IS_WS(e[i]))
185  e += i + strspn(e + i, WS);
186  return e - s;
187 }
188 
190 su_inline isize_t span_token_lws(char const *s)
191 {
192  char const *e = s;
193  while (_bnf_table[(unsigned char)(*e)] & (bnf_token | bnf_lws))
194  e++;
195  return e - s;
196 }
197 
199 su_inline isize_t span_token(char const *s)
200 {
201  char const *e = s;
202  while (_bnf_table[(unsigned char)(*e)] & bnf_token)
203  e++;
204  return e - s;
205 }
206 
208 su_inline isize_t span_alpha(char const *s)
209 {
210  char const *e = s;
211  while (_bnf_table[(unsigned char)(*e)] & bnf_alpha)
212  e++;
213  return e - s;
214 }
215 
217 su_inline isize_t span_digit(char const *s)
218 {
219  char const *e = s;
220  while (*e >= '0' && *e <= '9')
221  e++;
222  return e - s;
223 }
224 
226 su_inline isize_t span_hexdigit(char const *s)
227 {
228  char const *e = s;
229  while (IS_HEX(*e))
230  e++;
231  return e - s;
232 }
233 
235 su_inline isize_t span_alpha_digit_safe(char const *s)
236 {
237  char const *e = s;
238  while (_bnf_table[(unsigned char)(*e)] & (bnf_alpha | bnf_safe))
239  e++;
240  return e - s;
241 }
242 
244 su_inline isize_t span_param(char const *s)
245 {
246  char const *e = s;
247  while (IS_PARAM(*e))
248  e++;
249  return e - s;
250 }
251 
253 su_inline isize_t span_word(char const *s)
254 {
255  char const *e = s;
256  while (*e && (IS_TOKEN(*e) || strchr(SIP_WORD, *e)))
257  e++;
258  return e - s;
259 }
260 
262 su_inline isize_t span_unreserved(char const *s)
263 {
264  char const *e = s;
265  while (IS_UNRESERVED(*e))
266  e++;
267  return e - s;
268 }
269 
271 su_inline isize_t span_quoted(char const *s)
272 {
273  char const *b = s;
274 
275  if (*s++ != '"')
276  return 0;
277 
278  for (;;) {
279  s += strcspn(s, "\\\"");
280  if (!*s)
281  return 0;
282  if (*s++ == '"')
283  return s - b;
284  if (!*s++)
285  return 0;
286  }
287 }
288 
289 /* RFC 2396 defines URL chars */
291 #define URL_RESERVED ";/?:=+$,"
292 
294 #define URL_MARK "-_.!~*'()"
295 
297 #define URL_UNRESERVED ALPHANUM URL_MARK
298 
300 #define URL_ESCAPED "%"
301 #define URL_DELIMS "<>#%\""
302 #define URL_UNWISE "{}|\\^[]`"
303 #define URL_SCHEME ALPHANUM "+-."
304 
306 #define span_url_scheme(s) strspn(s, URL_SCHEME)
307 
308 SOFIAPUBFUN int span_ip4_address(char const *host);
309 SOFIAPUBFUN int span_ip6_address(char const *host);
310 SOFIAPUBFUN int span_ip6_reference(char const *host);
311 SOFIAPUBFUN int span_ip_address(char const *host);
312 SOFIAPUBFUN isize_t span_domain(char const *host);
313 SOFIAPUBFUN isize_t span_host(char const *host);
314 
315 SOFIAPUBFUN int scan_ip4_address(char **inout_host);
316 SOFIAPUBFUN int scan_ip6_address(char **inout_host);
317 SOFIAPUBFUN int scan_ip6_reference(char **inout_host);
318 SOFIAPUBFUN int scan_ip_address(char **inout_host);
319 SOFIAPUBFUN issize_t scan_domain(char **inout_host);
320 SOFIAPUBFUN issize_t scan_host(char **inout_host);
321 
322 SOFIA_END_DECLS
323 
324 #endif /* !defined BNF_H */
span_alpha_digit_safe
isize_t span_alpha_digit_safe(char const *s)
Calculate span of characters belonging to an RTSP token.
Definition: bnf.h:235
span_digit
isize_t span_digit(char const *s)
Calculate span of a digits.
Definition: bnf.h:217
IS_UNRESERVED
#define IS_UNRESERVED(c)
Test if is URL-unreserved.
Definition: bnf.h:131
bnf_unreserved
@ bnf_unreserved
URL unreserved.
Definition: bnf.h:150
scan_ip6_address
int scan_ip6_address(char **inout_host)
Scan and canonize valid IP6 address.
Definition: bnf.c:467
IS_HEX
#define IS_HEX(c)
Test if is a hex digit.
Definition: bnf.h:139
bnf_alpha
@ bnf_alpha
Alphabetic.
Definition: bnf.h:147
span_param
isize_t span_param(char const *s)
Calculate span of a characters valid in parameters.
Definition: bnf.h:244
su_types.h
IS_TOKEN
#define IS_TOKEN(c)
Test if is valid in tokens.
Definition: bnf.h:135
IS_PARAM
#define IS_PARAM(c)
Test if is valid for SIP parameter value.
Definition: bnf.h:137
bnf_param0
@ bnf_param0
SIP parameter, not token.
Definition: bnf.h:155
span_hexdigit
isize_t span_hexdigit(char const *s)
Calculate span of a hex.
Definition: bnf.h:226
su_inline
#define su_inline
span_ip6_reference
int span_ip6_reference(char const *host)
Return length of valid IP6 reference.
Definition: bnf.c:491
bnf_token
@ bnf_token
SIP token.
Definition: bnf.h:154
bnf_mark
@ bnf_mark
URL mark.
Definition: bnf.h:149
span_domain
isize_t span_domain(char const *host)
Return length of a valid domain name.
Definition: bnf.c:637
span_alpha
isize_t span_alpha(char const *s)
Calculate span of a alphabetic characters.
Definition: bnf.h:208
span_ip6_address
int span_ip6_address(char const *host)
Return length of valid IP6 address.
Definition: bnf.c:453
SIP_WORD
#define SIP_WORD
SIP Word characters (that are not token characters)
Definition: bnf.h:85
span_word
isize_t span_word(char const *s)
Calculate span of a SIP word.
Definition: bnf.h:253
WS
#define WS
Whitespace.
Definition: bnf.h:61
scan_ip4_address
int scan_ip4_address(char **inout_host)
Scan and canonize a valid IP4 address.
Definition: bnf.c:213
span_quoted
isize_t span_quoted(char const *s)
Calculate span of a double quoted string (with escaped chars inside)
Definition: bnf.h:271
bnf_token0
@ bnf_token0
SIP token, not alphabetic (0123456789-.
Definition: bnf.h:153
span_ip4_address
int span_ip4_address(char const *host)
Return length of valid IP4 address.
Definition: bnf.c:207
IS_WS
#define IS_WS(c)
Test if is normal whitespace.
Definition: bnf.h:117
scan_host
issize_t scan_host(char **inout_host)
Scan valid domain name or IP address.
Definition: bnf.c:680
scan_ip6_reference
int scan_ip6_reference(char **inout_host)
Scan valid IP6 reference.
Definition: bnf.c:505
bnf_crlf
@ bnf_crlf
Line end character.
Definition: bnf.h:145
span_host
isize_t span_host(char const *host)
Return length of a valid domain name or IP address.
Definition: bnf.c:662
SOFIAPUBVAR
#define SOFIAPUBVAR
span_unreserved
isize_t span_unreserved(char const *s)
Calculate span of a unreserved characters.
Definition: bnf.h:262
bnf_ws
@ bnf_ws
Whitespace character
Definition: bnf.h:144
SOFIAPUBFUN
#define SOFIAPUBFUN
span_token_lws
isize_t span_token_lws(char const *s)
Calculate span of a token or linear whitespace characters.
Definition: bnf.h:190
bnf_safe
@ bnf_safe
RTSP safe.
Definition: bnf.h:148
span_token
isize_t span_token(char const *s)
Calculate span of a token characters.
Definition: bnf.h:199
span_lws
isize_t span_lws(char const *s)
Calculate span of a linear whitespace.
Definition: bnf.h:177
bnf_lws
@ bnf_lws
Linear whitespace.
Definition: bnf.h:146
_bnf_table
unsigned const char _bnf_table[256]
Table for determining class of a character.
Definition: bnf.c:55
scan_ip_address
int scan_ip_address(char **inout_host)
Scan valid IP4/IP6 address.
Definition: bnf.c:554
bnf_param
@ bnf_param
SIP/HTTP parameter.
Definition: bnf.h:156
bnf_separator
@ bnf_separator
SIP separator.
Definition: bnf.h:151
scan_domain
issize_t scan_domain(char **inout_host)
Scan valid domain name.
Definition: bnf.c:643
span_ip_address
int span_ip_address(char const *host)
Return length of valid IP4 or IP6 address.
Definition: bnf.c:535

Sofia-SIP 1.12.11devel - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.