bnf  1.13.16
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"
51 #define SP " "
53 #define HT "\t"
55 /* CR conflicts with Windows SDK 10, so it is now _CR */
56 #define _CR "\r"
58 #define LF "\n"
60 #define CRLF _CR LF
62 #define WS SP HT
64 #define LWS SP HT _CR LF
66 #define LOALPHA "abcdefghijklmnopqrstuvwxyz"
68 #define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
70 #define ALPHA LOALPHA UPALPHA
72 #define DIGIT "0123456789"
74 #define SAFE "$-_." /* RTSP stuff */
75 #define ALPHANUM DIGIT ALPHA
76 #define HEX DIGIT "ABCDEF" "abcdef"
77 
81 #define SIP_TOKEN ALPHANUM "-.!%*_+`'~"
83 #define SIP_SEPARATOR "()<>@,;:\\\"/[]?={}" SP HT
84 
86 #define SIP_WORD "()<>:\\\"/[]?{}"
87 
89 #define skip_ws(ss) (*(ss) += span_ws(*(ss)))
90 
92 #define skip_lws(ss) (*(ss) += span_lws(*(ss)))
93 
95 #define skip_alpha(ss) (*(ss) += span_alpha(*(ss)))
96 
98 #define skip_digit(ss) (*(ss) += span_digit(*(ss)))
99 
101 #define skip_alpha_digit_safe(ss) (*(ss) += span_alpha_digit_safe(*(ss)))
102 
104 #define skip_token(ss) (*(ss) += span_token(*(ss)))
105 
107 #define skip_param(ss) (*(ss) += span_param(*(ss)))
108 
110 #define skip_word(ss) (*(ss) += span_word(*(ss)))
111 
113 #define IS_CRLF(c) ((c) == '\r' || (c) == '\n')
115 #define IS_LWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
116 /*#define IS_LWS(c) ((_bnf_table[(unsigned char)(c)] & bnf_lws))*/
118 #define IS_WS(c) ((c) == ' ' || (c) == '\t')
120 #define IS_NON_WS(c) (c && !IS_WS(c))
121 /*#define IS_NON_WS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_ws))*/
123 #define IS_NON_LWS(c) (c && !IS_LWS(c))
124 /*#define IS_NON_LWS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_lws))*/
126 #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
128 #define IS_ALPHA(c) (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
130 #define IS_ALPHANUM(c) (c && (IS_DIGIT(c) || IS_ALPHA(c)))
132 #define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
134 #define IS_RESERVED(c) (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
136 #define IS_TOKEN(c) ((_bnf_table[(unsigned char)c] & bnf_token))
138 #define IS_PARAM(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
140 #define IS_HEX(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
142 #define IS_TOKENLWS(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))
143 
144 enum {
145  bnf_ws = 1,
146  bnf_crlf = 2,
147  bnf_lws = 3,
148  bnf_alpha = 4,
149  bnf_safe = 8,
150  bnf_mark = 16,
156  bnf_param0 = 128,
158 };
159 
161 SOFIAPUBVAR unsigned char const _bnf_table[256];
162 
164 #define span_non_crlf(s) strcspn(s, _CR LF)
165 
167 #define span_non_ws(s) strcspn(s, WS)
168 
170 #define span_ws(s) strspn(s, WS)
171 
173 #define span_non_lws(s) strcspn(s, LWS)
174 
178 su_inline isize_t span_lws(char const *s)
179 {
180  char const *e = s;
181  int i = 0;
182  e += strspn(s, WS);
183  if (e[i] == '\r') i++;
184  if (e[i] == '\n') i++;
185  if (IS_WS(e[i]))
186  e += i + strspn(e + i, WS);
187  return e - s;
188 }
189 
191 su_inline isize_t span_token_lws(char const *s)
192 {
193  char const *e = s;
194  while (_bnf_table[(unsigned char)(*e)] & (bnf_token | bnf_lws))
195  e++;
196  return e - s;
197 }
198 
200 su_inline isize_t span_token(char const *s)
201 {
202  char const *e = s;
203  while (_bnf_table[(unsigned char)(*e)] & bnf_token)
204  e++;
205  return e - s;
206 }
207 
209 su_inline isize_t span_alpha(char const *s)
210 {
211  char const *e = s;
212  while (_bnf_table[(unsigned char)(*e)] & bnf_alpha)
213  e++;
214  return e - s;
215 }
216 
218 su_inline isize_t span_digit(char const *s)
219 {
220  char const *e = s;
221  while (*e >= '0' && *e <= '9')
222  e++;
223  return e - s;
224 }
225 
227 su_inline isize_t span_hexdigit(char const *s)
228 {
229  char const *e = s;
230  while (IS_HEX(*e))
231  e++;
232  return e - s;
233 }
234 
236 su_inline isize_t span_alpha_digit_safe(char const *s)
237 {
238  char const *e = s;
239  while (_bnf_table[(unsigned char)(*e)] & (bnf_alpha | bnf_safe))
240  e++;
241  return e - s;
242 }
243 
245 su_inline isize_t span_param(char const *s)
246 {
247  char const *e = s;
248  while (IS_PARAM(*e))
249  e++;
250  return e - s;
251 }
252 
254 su_inline isize_t span_word(char const *s)
255 {
256  char const *e = s;
257  while (*e && (IS_TOKEN(*e) || strchr(SIP_WORD, *e)))
258  e++;
259  return e - s;
260 }
261 
263 su_inline isize_t span_unreserved(char const *s)
264 {
265  char const *e = s;
266  while (IS_UNRESERVED(*e))
267  e++;
268  return e - s;
269 }
270 
272 su_inline isize_t span_quoted(char const *s)
273 {
274  char const *b = s;
275 
276  if (*s++ != '"')
277  return 0;
278 
279  for (;;) {
280  s += strcspn(s, "\\\"");
281  if (!*s)
282  return 0;
283  if (*s++ == '"')
284  return s - b;
285  if (!*s++)
286  return 0;
287  }
288 }
289 
290 /* RFC 2396 defines URL chars */
292 #define URL_RESERVED ";/?:=+$,"
293 
295 #define URL_MARK "-_.!~*'()"
296 
298 #define URL_UNRESERVED ALPHANUM URL_MARK
299 
301 #define URL_ESCAPED "%"
302 #define URL_DELIMS "<>#%\""
303 #define URL_UNWISE "{}|\\^[]`"
304 #define URL_SCHEME ALPHANUM "+-."
305 
307 #define span_url_scheme(s) strspn(s, URL_SCHEME)
308 
309 SOFIAPUBFUN int span_ip4_address(char const *host);
310 SOFIAPUBFUN int span_ip6_address(char const *host);
311 SOFIAPUBFUN int span_ip6_reference(char const *host);
312 SOFIAPUBFUN int span_ip_address(char const *host);
313 SOFIAPUBFUN isize_t span_domain(char const *host);
314 SOFIAPUBFUN isize_t span_host(char const *host);
315 
316 SOFIAPUBFUN int scan_ip4_address(char **inout_host);
317 SOFIAPUBFUN int scan_ip6_address(char **inout_host);
318 SOFIAPUBFUN int scan_ip6_reference(char **inout_host);
319 SOFIAPUBFUN int scan_ip_address(char **inout_host);
320 SOFIAPUBFUN issize_t scan_domain(char **inout_host);
321 SOFIAPUBFUN issize_t scan_host(char **inout_host);
322 
323 SOFIA_END_DECLS
324 
325 #endif /* !defined BNF_H */
@ bnf_unreserved
URL unreserved.
Definition: bnf.h:151
@ bnf_token0
SIP token, not alphabetic (0123456789-.
Definition: bnf.h:154
@ bnf_token
SIP token.
Definition: bnf.h:155
@ bnf_safe
RTSP safe.
Definition: bnf.h:149
@ bnf_mark
URL mark.
Definition: bnf.h:150
@ bnf_param
SIP/HTTP parameter.
Definition: bnf.h:157
@ bnf_ws
Whitespace character
Definition: bnf.h:145
@ bnf_param0
SIP parameter, not token.
Definition: bnf.h:156
@ bnf_lws
Linear whitespace.
Definition: bnf.h:147
@ bnf_crlf
Line end character.
Definition: bnf.h:146
@ bnf_alpha
Alphabetic.
Definition: bnf.h:148
@ bnf_separator
SIP separator.
Definition: bnf.h:152
int scan_ip_address(char **inout_host)
Scan valid IP4/IP6 address.
Definition: bnf.c:554
int span_ip6_address(char const *host)
Return length of valid IP6 address.
Definition: bnf.c:453
isize_t span_lws(char const *s)
Calculate span of a linear whitespace.
Definition: bnf.h:178
isize_t span_token_lws(char const *s)
Calculate span of a token or linear whitespace characters.
Definition: bnf.h:191
isize_t span_digit(char const *s)
Calculate span of a digits.
Definition: bnf.h:218
isize_t span_hexdigit(char const *s)
Calculate span of a hex.
Definition: bnf.h:227
int span_ip6_reference(char const *host)
Return length of valid IP6 reference.
Definition: bnf.c:491
int scan_ip6_address(char **inout_host)
Scan and canonize valid IP6 address.
Definition: bnf.c:467
isize_t span_alpha(char const *s)
Calculate span of a alphabetic characters.
Definition: bnf.h:209
#define IS_UNRESERVED(c)
Test if is URL-unreserved.
Definition: bnf.h:132
int scan_ip4_address(char **inout_host)
Scan and canonize a valid IP4 address.
Definition: bnf.c:213
#define WS
Whitespace.
Definition: bnf.h:62
#define IS_PARAM(c)
Test if is valid for SIP parameter value.
Definition: bnf.h:138
#define IS_WS(c)
Test if is normal whitespace.
Definition: bnf.h:118
#define IS_TOKEN(c)
Test if is valid in tokens.
Definition: bnf.h:136
isize_t span_host(char const *host)
Return length of a valid domain name or IP address.
Definition: bnf.c:662
int span_ip_address(char const *host)
Return length of valid IP4 or IP6 address.
Definition: bnf.c:535
unsigned char const _bnf_table[256]
Table for determining class of a character.
Definition: bnf.c:55
isize_t span_param(char const *s)
Calculate span of a characters valid in parameters.
Definition: bnf.h:245
isize_t span_domain(char const *host)
Return length of a valid domain name.
Definition: bnf.c:637
isize_t span_quoted(char const *s)
Calculate span of a double quoted string (with escaped chars inside)
Definition: bnf.h:272
#define SIP_WORD
SIP Word characters (that are not token characters)
Definition: bnf.h:86
issize_t scan_domain(char **inout_host)
Scan valid domain name.
Definition: bnf.c:643
int span_ip4_address(char const *host)
Return length of valid IP4 address.
Definition: bnf.c:207
isize_t span_word(char const *s)
Calculate span of a SIP word.
Definition: bnf.h:254
int scan_ip6_reference(char **inout_host)
Scan valid IP6 reference.
Definition: bnf.c:505
isize_t span_unreserved(char const *s)
Calculate span of a unreserved characters.
Definition: bnf.h:263
#define IS_HEX(c)
Test if is a hex digit.
Definition: bnf.h:140
isize_t span_token(char const *s)
Calculate span of a token characters.
Definition: bnf.h:200
issize_t scan_host(char **inout_host)
Scan valid domain name or IP address.
Definition: bnf.c:680
isize_t span_alpha_digit_safe(char const *s)
Calculate span of characters belonging to an RTSP token.
Definition: bnf.h:236
#define SOFIAPUBFUN
#define SOFIAPUBVAR
#define su_inline

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