bnf 1.12.11devel
Loading...
Searching...
No Matches
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
42SOFIA_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#define CR "\r"
57#define LF "\n"
59#define CRLF CR LF
61#define WS SP HT
63#define LWS SP HT CR LF
65#define LOALPHA "abcdefghijklmnopqrstuvwxyz"
67#define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
69#define ALPHA LOALPHA UPALPHA
71#define DIGIT "0123456789"
73#define SAFE "$-_." /* RTSP stuff */
74#define ALPHANUM DIGIT ALPHA
75#define HEX DIGIT "ABCDEF" "abcdef"
76
80#define SIP_TOKEN ALPHANUM "-.!%*_+`'~"
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')
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')
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')
127#define IS_ALPHA(c) (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
129#define IS_ALPHANUM(c) (c && (IS_DIGIT(c) || IS_ALPHA(c)))
131#define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
133#define IS_RESERVED(c) (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
135#define IS_TOKEN(c) ((_bnf_table[(unsigned char)c] & bnf_token))
137#define IS_PARAM(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
139#define IS_HEX(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
141#define IS_TOKENLWS(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))
142
143enum {
144 bnf_ws = 1,
149 bnf_mark = 16,
158
160SOFIAPUBVAR 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
177su_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
190su_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
199su_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
208su_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
217su_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
226su_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
235su_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
244su_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
253su_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
262su_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
271su_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
308SOFIAPUBFUN int span_ip4_address(char const *host);
309SOFIAPUBFUN int span_ip6_address(char const *host);
310SOFIAPUBFUN int span_ip6_reference(char const *host);
311SOFIAPUBFUN int span_ip_address(char const *host);
312SOFIAPUBFUN isize_t span_domain(char const *host);
313SOFIAPUBFUN isize_t span_host(char const *host);
314
315SOFIAPUBFUN int scan_ip4_address(char **inout_host);
316SOFIAPUBFUN int scan_ip6_address(char **inout_host);
317SOFIAPUBFUN int scan_ip6_reference(char **inout_host);
318SOFIAPUBFUN int scan_ip_address(char **inout_host);
319SOFIAPUBFUN issize_t scan_domain(char **inout_host);
320SOFIAPUBFUN issize_t scan_host(char **inout_host);
321
322SOFIA_END_DECLS
323
324#endif /* !defined BNF_H */
@ bnf_unreserved
URL unreserved.
Definition bnf.h:150
@ bnf_token0
SIP token, not alphabetic (0123456789-.
Definition bnf.h:153
@ bnf_token
SIP token.
Definition bnf.h:154
@ bnf_safe
RTSP safe.
Definition bnf.h:148
@ bnf_mark
URL mark.
Definition bnf.h:149
@ bnf_param
SIP/HTTP parameter.
Definition bnf.h:156
@ bnf_ws
Whitespace character
Definition bnf.h:144
@ bnf_param0
SIP parameter, not token.
Definition bnf.h:155
@ bnf_lws
Linear whitespace.
Definition bnf.h:146
@ bnf_crlf
Line end character.
Definition bnf.h:145
@ bnf_alpha
Alphabetic.
Definition bnf.h:147
@ bnf_separator
SIP separator.
Definition bnf.h:151
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:177
isize_t span_token_lws(char const *s)
Calculate span of a token or linear whitespace characters.
Definition bnf.h:190
isize_t span_digit(char const *s)
Calculate span of a digits.
Definition bnf.h:217
isize_t span_hexdigit(char const *s)
Calculate span of a hex.
Definition bnf.h:226
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:208
#define IS_UNRESERVED(c)
Test if is URL-unreserved.
Definition bnf.h:131
int scan_ip4_address(char **inout_host)
Scan and canonize a valid IP4 address.
Definition bnf.c:213
#define WS
Whitespace.
Definition bnf.h:61
#define IS_PARAM(c)
Test if is valid for SIP parameter value.
Definition bnf.h:137
#define IS_WS(c)
Test if is normal whitespace.
Definition bnf.h:117
#define IS_TOKEN(c)
Test if is valid in tokens.
Definition bnf.h:135
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:244
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:271
#define SIP_WORD
SIP Word characters (that are not token characters)
Definition bnf.h:85
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:253
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:262
#define IS_HEX(c)
Test if is a hex digit.
Definition bnf.h:139
isize_t span_token(char const *s)
Calculate span of a token characters.
Definition bnf.h:199
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:235
#define SOFIAPUBFUN
#define SOFIAPUBVAR
#define su_inline

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