sofia-sip/bnf.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef BNF_H  
00026 #define BNF_H
00027 
00038 #include <sofia-sip/su_config.h>
00039 
00040 #include <string.h>
00041 
00042 SOFIA_BEGIN_DECLS
00043 
00044 /* Parsing tokens */
00046 #define CTL   "\001\002\003\004\005\006\007" \
00047     "\010\011\012\013\014\015\016\017" \
00048     "\020\021\022\023\024\025\026\027" \
00049     "\030\031\032\033\034\035\036\037" "\177" "\0"
00050 
00051 #define SP      " "
00052 
00053 #define HT      "\t"
00054 
00055 #define CR      "\r"
00056 
00057 #define LF      "\n"
00058 
00059 #define CRLF     CR LF
00060 
00061 #define WS       SP HT 
00062 
00063 #define LWS      SP HT CR LF
00064 
00065 #define LOALPHA "abcdefghijklmnopqrstuvwxyz"
00066 
00067 #define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00068 
00069 #define ALPHA    LOALPHA UPALPHA
00070 
00071 #define DIGIT   "0123456789"
00072 
00073 #define SAFE    "$-_." /* RTSP stuff */
00074 #define ALPHANUM DIGIT ALPHA 
00075 #define HEX      DIGIT "ABCDEF" "abcdef"
00076 
00080 #define SIP_TOKEN  ALPHANUM "-.!%*_+`'~"
00081 
00082 #define SIP_SEPARATOR  "()<>@,;:\\\"/[]?={}" SP HT
00083 
00085 #define SIP_WORD "()<>:\\\"/[]?{}"
00086 
00088 #define skip_ws(ss) (*(ss) += span_ws(*(ss)))
00089 
00091 #define skip_lws(ss) (*(ss) += span_lws(*(ss)))
00092 
00094 #define skip_alpha(ss) (*(ss) += span_alpha(*(ss)))
00095 
00097 #define skip_digit(ss) (*(ss) += span_digit(*(ss)))
00098 
00100 #define skip_alpha_digit_safe(ss) (*(ss) += span_alpha_digit_safe(*(ss)))
00101 
00103 #define skip_token(ss)  (*(ss) += span_token(*(ss)))
00104 
00106 #define skip_param(ss) (*(ss) += span_param(*(ss)))
00107 
00109 #define skip_word(ss) (*(ss) += span_word(*(ss)))
00110 
00112 #define IS_CRLF(c)       ((c) == '\r' || (c) == '\n') 
00113 
00114 #define IS_LWS(c)        ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
00115 /*#define IS_LWS(c)      ((_bnf_table[(unsigned char)(c)] & bnf_lws))*/
00117 #define IS_WS(c)         ((c) == ' ' || (c) == '\t')
00118 
00119 #define IS_NON_WS(c)     (c && !IS_WS(c))
00120 /*#define IS_NON_WS(c)     (c && !(_bnf_table[(unsigned char)c] & bnf_ws))*/
00122 #define IS_NON_LWS(c)    (c && !IS_LWS(c))
00123 /*#define IS_NON_LWS(c)    (c && !(_bnf_table[(unsigned char)c] & bnf_lws))*/
00125 #define IS_DIGIT(c)      ((c) >= '0' && (c) <= '9')
00126 
00127 #define IS_ALPHA(c)      (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
00128 
00129 #define IS_ALPHANUM(c)   (c && (IS_DIGIT(c) || IS_ALPHA(c)))
00130 
00131 #define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
00132 
00133 #define IS_RESERVED(c)   (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
00134 
00135 #define IS_TOKEN(c)      ((_bnf_table[(unsigned char)c] & bnf_token))
00136 
00137 #define IS_PARAM(c)      ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
00138 
00139 #define IS_HEX(c)        (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
00140 
00141 #define IS_TOKENLWS(c)   ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))
00142 
00143 enum {
00144   bnf_ws = 1,                                   
00145   bnf_crlf = 2,                                 
00146   bnf_lws = 3,                                  
00147   bnf_alpha = 4,                                
00148   bnf_safe = 8,                                 
00149   bnf_mark = 16,                                
00150   bnf_unreserved = bnf_alpha | bnf_mark,        
00151   bnf_separator = 32,                           
00153   bnf_token0 = 64 | bnf_safe,                           
00154   bnf_token = bnf_token0 | bnf_alpha,           
00155   bnf_param0 = 128,                             
00156   bnf_param = bnf_token | bnf_param0 
00157 };
00158 
00160 SOFIAPUBVAR unsigned char const _bnf_table[256];
00161 
00163 #define span_non_crlf(s) strcspn(s, CR LF)
00164 
00166 #define span_non_ws(s) strcspn(s, WS)
00167 
00169 #define span_ws(s) strspn(s, WS)
00170 
00172 #define span_non_lws(s) strcspn(s, LWS)
00173 
00175 static inline int span_lws(char const *s)
00176 {
00177   char const *e = s; 
00178   while (IS_LWS(*e))
00179     e++; 
00180   return e - s;
00181 }
00182 
00184 static inline int span_token_lws(char const *s)
00185 {
00186   char const *e = s; 
00187   while (_bnf_table[(unsigned char)(*e)] & (bnf_token | bnf_lws))
00188     e++; 
00189   return e - s; 
00190 }
00191 
00193 static inline int span_token(char const *s)
00194 {
00195   char const *e = s; 
00196   while (_bnf_table[(unsigned char)(*e)] & bnf_token)
00197     e++; 
00198   return e - s; 
00199 }
00200 
00202 static inline int span_alpha(char const *s)
00203 {
00204   char const *e = s; 
00205   while (_bnf_table[(unsigned char)(*e)] & bnf_alpha)
00206     e++; 
00207   return e - s; 
00208 }
00209 
00211 static inline int span_digit(char const *s)
00212 {
00213   char const *e = s; 
00214   while (*e >= '0' && *e <= '9')
00215     e++; 
00216   return e - s; 
00217 }
00218 
00220 static inline int span_hexdigit(char const *s)
00221 {
00222   char const *e = s; 
00223   while (IS_HEX(*e))
00224     e++; 
00225   return e - s; 
00226 }
00227 
00229 static inline int span_alpha_digit_safe(char const *s)
00230 {
00231   char const *e = s; 
00232   while (_bnf_table[(unsigned char)(*e)] & (bnf_alpha | bnf_safe))
00233     e++; 
00234   return e - s; 
00235 }
00236 
00238 static inline int span_param(char const *s)
00239 {
00240   char const *e = s; 
00241   while (IS_PARAM(*e))
00242     e++; 
00243   return e - s; 
00244 }
00245 
00247 static inline int span_word(char const *s)
00248 {
00249   char const *e = s; 
00250   while (*e && (IS_TOKEN(*e) || strchr(SIP_WORD, *e)))
00251     e++; 
00252   return e - s; 
00253 }
00254 
00256 static inline int span_unreserved(char const *s)
00257 {
00258   char const *e = s;
00259   while (IS_UNRESERVED(*e))
00260     e++;
00261   return e - s;
00262 }
00263 
00265 static inline int span_quoted(char const *s)
00266 {
00267   char const *b = s;
00268 
00269   if (*s++ != '"')
00270     return 0;
00271 
00272   for (;;) {
00273     s += strcspn(s, "\\\"");
00274     if (!*s)
00275       return 0;
00276     if (*s++ == '"')
00277       return s - b;
00278     if (!*s++)
00279       return 0;
00280   }
00281 }
00282 
00283 /* RFC 2396 defines URL chars */
00285 #define URL_RESERVED        ";/?:=+$,"
00286 
00288 #define URL_MARK            "-_.!~*'()"
00289 
00291 #define URL_UNRESERVED ALPHANUM URL_MARK
00292 
00294 #define URL_ESCAPED    "%"
00295 #define URL_DELIMS     "<>#%\""
00296 #define URL_UNWISE     "{}|\\^[]`"
00297 #define URL_SCHEME     ALPHANUM "+-."
00298 
00300 #define span_url_scheme(s) strspn(s, URL_SCHEME)
00301 
00302 SOFIAPUBFUN int span_ip4_address(char const *host);
00303 SOFIAPUBFUN int span_ip6_address(char const *host);
00304 SOFIAPUBFUN int span_ip6_reference(char const *host);
00305 SOFIAPUBFUN int span_ip_address(char const *host);
00306 SOFIAPUBFUN int span_domain(char const *host);
00307 SOFIAPUBFUN int span_host(char const *host);
00308 
00309 SOFIAPUBFUN int scan_ip4_address(char **inout_host);
00310 SOFIAPUBFUN int scan_ip6_address(char **inout_host);
00311 SOFIAPUBFUN int scan_ip6_reference(char **inout_host);
00312 SOFIAPUBFUN int scan_ip_address(char **inout_host);
00313 SOFIAPUBFUN int scan_domain(char **inout_host);
00314 SOFIAPUBFUN int scan_host(char **inout_host);
00315 
00316 SOFIA_END_DECLS
00317 
00318 #endif /* !defined BNF_H */

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