Loading...
Searching...
No Matches
bnf.h
Go to the documentation of this file.
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef BNF_H
#define BNF_H
#include <sofia-sip/su_types.h>
#include <string.h>
SOFIA_BEGIN_DECLS
/* Parsing tokens */
#define CTL "\001\002\003\004\005\006\007" \
"\010\011\012\013\014\015\016\017" \
"\020\021\022\023\024\025\026\027" \
"\030\031\032\033\034\035\036\037" "\177" "\0"
#define SP " "
#define HT "\t"
#define CR "\r"
#define LF "\n"
#define CRLF CR LF
#define WS SP HT
#define LWS SP HT CR LF
#define LOALPHA "abcdefghijklmnopqrstuvwxyz"
#define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define ALPHA LOALPHA UPALPHA
#define DIGIT "0123456789"
#define SAFE "$-_." /* RTSP stuff */
#define ALPHANUM DIGIT ALPHA
#define HEX DIGIT "ABCDEF" "abcdef"
#define SIP_TOKEN ALPHANUM "-.!%*_+`'~"
#define SIP_SEPARATOR "()<>@,;:\\\"/[]?={}" SP HT
#define SIP_WORD "()<>:\\\"/[]?{}"
#define skip_ws(ss) (*(ss) += span_ws(*(ss)))
#define skip_lws(ss) (*(ss) += span_lws(*(ss)))
#define skip_alpha(ss) (*(ss) += span_alpha(*(ss)))
#define skip_digit(ss) (*(ss) += span_digit(*(ss)))
#define skip_alpha_digit_safe(ss) (*(ss) += span_alpha_digit_safe(*(ss)))
#define skip_token(ss) (*(ss) += span_token(*(ss)))
#define skip_param(ss) (*(ss) += span_param(*(ss)))
#define skip_word(ss) (*(ss) += span_word(*(ss)))
#define IS_CRLF(c) ((c) == '\r' || (c) == '\n')
#define IS_LWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
/*#define IS_LWS(c) ((_bnf_table[(unsigned char)(c)] & bnf_lws))*/
#define IS_WS(c) ((c) == ' ' || (c) == '\t')
#define IS_NON_WS(c) (c && !IS_WS(c))
/*#define IS_NON_WS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_ws))*/
#define IS_NON_LWS(c) (c && !IS_LWS(c))
/*#define IS_NON_LWS(c) (c && !(_bnf_table[(unsigned char)c] & bnf_lws))*/
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
#define IS_ALPHA(c) (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
#define IS_ALPHANUM(c) (c && (IS_DIGIT(c) || IS_ALPHA(c)))
#define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
#define IS_RESERVED(c) (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
#define IS_TOKEN(c) ((_bnf_table[(unsigned char)c] & bnf_token))
#define IS_PARAM(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
#define IS_HEX(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
#define IS_TOKENLWS(c) ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))
enum {
bnf_ws = 1,
bnf_crlf = 2,
bnf_lws = 3,
bnf_alpha = 4,
bnf_safe = 8,
bnf_mark = 16,
bnf_unreserved = bnf_alpha | bnf_mark,
bnf_separator = 32,
bnf_token0 = 64 | bnf_safe,
bnf_token = bnf_token0 | bnf_alpha,
bnf_param0 = 128,
bnf_param = bnf_token | bnf_param0
};
SOFIAPUBVAR unsigned char const _bnf_table[256];
#define span_non_crlf(s) strcspn(s, CR LF)
#define span_non_ws(s) strcspn(s, WS)
#define span_ws(s) strspn(s, WS)
#define span_non_lws(s) strcspn(s, LWS)
su_inline isize_t span_lws(char const *s)
{
char const *e = s;
int i = 0;
e += strspn(s, WS);
if (e[i] == '\r') i++;
if (e[i] == '\n') i++;
if (IS_WS(e[i]))
e += i + strspn(e + i, WS);
return e - s;
}
su_inline isize_t span_token_lws(char const *s)
{
char const *e = s;
while (_bnf_table[(unsigned char)(*e)] & (bnf_token | bnf_lws))
e++;
return e - s;
}
su_inline isize_t span_token(char const *s)
{
char const *e = s;
while (_bnf_table[(unsigned char)(*e)] & bnf_token)
e++;
return e - s;
}
su_inline isize_t span_alpha(char const *s)
{
char const *e = s;
while (_bnf_table[(unsigned char)(*e)] & bnf_alpha)
e++;
return e - s;
}
su_inline isize_t span_digit(char const *s)
{
char const *e = s;
while (*e >= '0' && *e <= '9')
e++;
return e - s;
}
su_inline isize_t span_hexdigit(char const *s)
{
char const *e = s;
while (IS_HEX(*e))
e++;
return e - s;
}
su_inline isize_t span_alpha_digit_safe(char const *s)
{
char const *e = s;
while (_bnf_table[(unsigned char)(*e)] & (bnf_alpha | bnf_safe))
e++;
return e - s;
}
su_inline isize_t span_param(char const *s)
{
char const *e = s;
while (IS_PARAM(*e))
e++;
return e - s;
}
su_inline isize_t span_word(char const *s)
{
char const *e = s;
while (*e && (IS_TOKEN(*e) || strchr(SIP_WORD, *e)))
e++;
return e - s;
}
su_inline isize_t span_unreserved(char const *s)
{
char const *e = s;
while (IS_UNRESERVED(*e))
e++;
return e - s;
}
su_inline isize_t span_quoted(char const *s)
{
char const *b = s;
if (*s++ != '"')
return 0;
for (;;) {
s += strcspn(s, "\\\"");
if (!*s)
return 0;
if (*s++ == '"')
return s - b;
if (!*s++)
return 0;
}
}
/* RFC 2396 defines URL chars */
#define URL_RESERVED ";/?:=+$,"
#define URL_MARK "-_.!~*'()"
#define URL_UNRESERVED ALPHANUM URL_MARK
#define URL_ESCAPED "%"
#define URL_DELIMS "<>#%\""
#define URL_UNWISE "{}|\\^[]`"
#define URL_SCHEME ALPHANUM "+-."
#define span_url_scheme(s) strspn(s, URL_SCHEME)
SOFIAPUBFUN int span_ip4_address(char const *host);
SOFIAPUBFUN int span_ip6_address(char const *host);
SOFIAPUBFUN int span_ip6_reference(char const *host);
SOFIAPUBFUN int span_ip_address(char const *host);
SOFIAPUBFUN isize_t span_domain(char const *host);
SOFIAPUBFUN isize_t span_host(char const *host);
SOFIAPUBFUN int scan_ip4_address(char **inout_host);
SOFIAPUBFUN int scan_ip6_address(char **inout_host);
SOFIAPUBFUN int scan_ip6_reference(char **inout_host);
SOFIAPUBFUN int scan_ip_address(char **inout_host);
SOFIAPUBFUN issize_t scan_domain(char **inout_host);
SOFIAPUBFUN issize_t scan_host(char **inout_host);
SOFIA_END_DECLS
#endif /* !defined BNF_H */
scan_ip_address
int scan_ip_address(char **inout_host)
Scan valid IP4/IP6 address.
Definition bnf.c:554
span_ip6_address
int span_ip6_address(char const *host)
Return length of valid IP6 address.
Definition bnf.c:453
span_lws
isize_t span_lws(char const *s)
Calculate span of a linear whitespace.
Definition bnf.h:177
span_token_lws
isize_t span_token_lws(char const *s)
Calculate span of a token or linear whitespace characters.
Definition bnf.h:190
span_digit
isize_t span_digit(char const *s)
Calculate span of a digits.
Definition bnf.h:217
bnf_unreserved
@ bnf_unreserved
URL unreserved.
Definition bnf.h:150
bnf_safe
@ bnf_safe
RTSP safe.
Definition bnf.h:148
bnf_mark
@ bnf_mark
URL mark.
Definition bnf.h:149
bnf_param
@ bnf_param
SIP/HTTP parameter.
Definition bnf.h:156
bnf_ws
@ bnf_ws
Whitespace character.
Definition bnf.h:144
bnf_param0
@ bnf_param0
SIP token, not alphabetic (0123456789-.!
Definition bnf.h:155
bnf_lws
@ bnf_lws
Linear whitespace.
Definition bnf.h:146
bnf_crlf
@ bnf_crlf
Line end character.
Definition bnf.h:145
bnf_alpha
@ bnf_alpha
Alphabetic.
Definition bnf.h:147
bnf_separator
@ bnf_separator
SIP separator.
Definition bnf.h:151
span_hexdigit
isize_t span_hexdigit(char const *s)
Calculate span of a hex.
Definition bnf.h:226
span_ip6_reference
int span_ip6_reference(char const *host)
Return length of valid IP6 reference.
Definition bnf.c:491
scan_ip6_address
int scan_ip6_address(char **inout_host)
Scan and canonize valid IP6 address.
Definition bnf.c:467
span_alpha
isize_t span_alpha(char const *s)
Calculate span of a alphabetic characters.
Definition bnf.h:208
IS_UNRESERVED
#define IS_UNRESERVED(c)
Test if is URL-unreserved.
Definition bnf.h:131
scan_ip4_address
int scan_ip4_address(char **inout_host)
Scan and canonize a valid IP4 address.
Definition bnf.c:213
WS
#define WS
Whitespace.
Definition bnf.h:61
IS_PARAM
#define IS_PARAM(c)
Test if is valid for SIP parameter value.
Definition bnf.h:137
IS_WS
#define IS_WS(c)
Test if is normal whitespace.
Definition bnf.h:117
IS_TOKEN
#define IS_TOKEN(c)
Test if is valid in tokens.
Definition bnf.h:135
span_host
isize_t span_host(char const *host)
Return length of a valid domain name or IP address.
Definition bnf.c:662
span_ip_address
int span_ip_address(char const *host)
Return length of valid IP4 or IP6 address.
Definition bnf.c:535
_bnf_table
unsigned char const _bnf_table[256]
Table for determining class of a character.
Definition bnf.c:55
span_param
isize_t span_param(char const *s)
Calculate span of a characters valid in parameters.
Definition bnf.h:244
span_domain
isize_t span_domain(char const *host)
Return length of a valid domain name.
Definition bnf.c:637
span_quoted
isize_t span_quoted(char const *s)
Calculate span of a double quoted string (with escaped chars inside).
Definition bnf.h:271
SIP_WORD
#define SIP_WORD
SIP Word characters (that are not token characters).
Definition bnf.h:85
scan_domain
issize_t scan_domain(char **inout_host)
Scan valid domain name.
Definition bnf.c:643
span_ip4_address
int span_ip4_address(char const *host)
Return length of valid IP4 address.
Definition bnf.c:207
span_word
isize_t span_word(char const *s)
Calculate span of a SIP word.
Definition bnf.h:253
scan_ip6_reference
int scan_ip6_reference(char **inout_host)
Scan valid IP6 reference.
Definition bnf.c:505
span_unreserved
isize_t span_unreserved(char const *s)
Calculate span of a unreserved characters.
Definition bnf.h:262
IS_HEX
#define IS_HEX(c)
Test if is a hex digit.
Definition bnf.h:139
span_token
isize_t span_token(char const *s)
Calculate span of a token characters.
Definition bnf.h:199
scan_host
issize_t scan_host(char **inout_host)
Scan valid domain name or IP address.
Definition bnf.c:680
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
SOFIAPUBFUN
#define SOFIAPUBFUN
SOFIAPUBVAR
#define SOFIAPUBVAR
su_inline
#define su_inline
su_types.h

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