bnf.h 9.59 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
#ifndef BNF_H  /** Defined when <sofia-sip/bnf.h> has been included. */
Pekka Pessi's avatar
Pekka Pessi committed
26
#define BNF_H
Pekka Pessi's avatar
Pekka Pessi committed
27

Pekka Pessi's avatar
Pekka Pessi committed
28
/**@file sofia-sip/bnf.h
Pekka Pessi's avatar
Pekka Pessi committed
29 30 31 32 33 34 35 36 37
 * 
 * Parsing macros and prototypes for HTTP-like protocols.
 * 
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Tue Jun 06 10:59:34 2000 ppessi
 *
 */

38
#include <sofia-sip/su_types.h>
39

40 41
#include <string.h>

42 43
SOFIA_BEGIN_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* Parsing tokens */
/** Control characters. */
#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"
/** Space */
#define SP      " "
/** Horizontal tab */
#define HT      "\t"
/** Carriage return */
#define CR      "\r"
/** Line feed */
#define LF      "\n"
/** Line-ending characters */
#define CRLF     CR LF
/** Whitespace */
#define WS       SP HT 
/** Linear whitespace */
#define LWS      SP HT CR LF
/** Lower-case alphabetic characters */
#define LOALPHA "abcdefghijklmnopqrstuvwxyz"
/** Upper-case alphabetic characters */
#define UPALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
/** Alphabetic characters */
#define ALPHA    LOALPHA UPALPHA
/** Digits */
#define DIGIT   "0123456789"
/** RTSP safe characters */
#define SAFE    "$-_." /* RTSP stuff */
#define ALPHANUM DIGIT ALPHA 
#define HEX      DIGIT "ABCDEF" "abcdef"

/** SIP token characters.
 * @note $|&^# were token chars in RFC 2543, but no more in RFC 3261. 
 */
#define SIP_TOKEN  ALPHANUM "-.!%*_+`'~"
/** SIP separator characters */
#define SIP_SEPARATOR  "()<>@,;:\\\"/[]?={}" SP HT

/** SIP Word characters (that are not token characters) */
#define SIP_WORD "()<>:\\\"/[]?{}"

/** Skip whitespace (SP HT) */
#define skip_ws(ss) (*(ss) += span_ws(*(ss)))

/** Skip linear whitespace (SP HT CR LF) */
#define skip_lws(ss) (*(ss) += span_lws(*(ss)))

/** Skip [a-zA-Z] */
#define skip_alpha(ss) (*(ss) += span_alpha(*(ss)))

/** Skip digits */
#define skip_digit(ss) (*(ss) += span_digit(*(ss)))

/** Skip characters belonging to an RTSP token. */
#define skip_alpha_digit_safe(ss) (*(ss) += span_alpha_digit_safe(*(ss)))

/** Skip characters belonging to a SIP token. */
#define skip_token(ss)  (*(ss) += span_token(*(ss)))

/** Skip characters belonging to a SIP parameter value. */
#define skip_param(ss) (*(ss) += span_param(*(ss)))

/** Skip characters belonging to a SIP word. */
#define skip_word(ss) (*(ss) += span_word(*(ss)))

/** Test if @c is CR or LF */
#define IS_CRLF(c)       ((c) == '\r' || (c) == '\n') 
/** Test if @c is linear whitespace */
#define IS_LWS(c)  	 ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
/*#define IS_LWS(c)  	 ((_bnf_table[(unsigned char)(c)] & bnf_lws))*/
/** Test if @c is normal whitespace */
#define IS_WS(c)   	 ((c) == ' ' || (c) == '\t')
/** Test if @c is not whitespace (and not NUL). */
#define IS_NON_WS(c)     (c && !IS_WS(c))
/*#define IS_NON_WS(c)     (c && !(_bnf_table[(unsigned char)c] & bnf_ws))*/
/** Test if @c is not linear whitespace (and not NUL). */
#define IS_NON_LWS(c)    (c && !IS_LWS(c))
/*#define IS_NON_LWS(c)    (c && !(_bnf_table[(unsigned char)c] & bnf_lws))*/
/** Test if @c is a digit. */
#define IS_DIGIT(c)   	 ((c) >= '0' && (c) <= '9')
126 127 128 129
/** Test if @c is alphabetic. */
#define IS_ALPHA(c)      (c && ((_bnf_table[(unsigned char)c] & bnf_alpha)))
/** Test if @c is alphanumeric. */
#define IS_ALPHANUM(c)   (c && (IS_DIGIT(c) || IS_ALPHA(c)))
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/** Test if @c is URL-unreserved. */
#define IS_UNRESERVED(c) ((_bnf_table[(unsigned char)c] & bnf_unreserved))
/** Test if @c is URL-reserved. */
#define IS_RESERVED(c)   (c && !(_bnf_table[(unsigned char)c] & bnf_unreserved))
/** Test if @c is valid in tokens. */
#define IS_TOKEN(c)      ((_bnf_table[(unsigned char)c] & bnf_token))
/** Test if @c is valid for SIP parameter value. */
#define IS_PARAM(c)      ((_bnf_table[(unsigned char)c] & (bnf_token|bnf_param)))
/** Test if @c is a hex digit. */
#define IS_HEX(c)        (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
/** Test if @c is a linear whitespace or valid in tokens. */
#define IS_TOKENLWS(c)   ((_bnf_table[(unsigned char)c] & (bnf_token|bn_lws)))

enum {
  bnf_ws = 1,					/**< Whitespace character  */
  bnf_crlf = 2,					/**< Line end character */
  bnf_lws = 3,					/**< Linear whitespace */
  bnf_alpha = 4,				/**< Alphabetic */
  bnf_safe = 8,					/**< RTSP safe */
  bnf_mark = 16,				/**< URL mark */
  bnf_unreserved = bnf_alpha | bnf_mark,	/**< URL unreserved */
  bnf_separator = 32,				/**< SIP separator */
  /** SIP token, not alphabetic (0123456789-.!%*_+`'~) */
  bnf_token0 = 64 | bnf_safe,				
  bnf_token = bnf_token0 | bnf_alpha,		/**< SIP token */
  bnf_param0 = 128,				/**< SIP parameter, not token */
  bnf_param = bnf_token | bnf_param0 /**< SIP/HTTP parameter */
};

159
/** Table for determining class of a character. */
160
SOFIAPUBVAR unsigned char const _bnf_table[256];
Pekka Pessi's avatar
Pekka Pessi committed
161 162 163 164 165 166 167 168 169 170 171 172 173

/** Get number of characters before CRLF */
#define span_non_crlf(s) strcspn(s, CR LF)

/** Get number of characters before whitespace */
#define span_non_ws(s) strcspn(s, WS)

/** Get number of whitespace characters */
#define span_ws(s) strspn(s, WS)

/** Get number of characters before linear whitespace */
#define span_non_lws(s) strcspn(s, LWS)

174 175 176
/** Calculate span of a linear whitespace. 
 * LWS = [*WSP CRLF] 1*WSP
 */
177
su_inline isize_t span_lws(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
178
{
179 180 181 182 183 184 185
  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);
Pekka Pessi's avatar
Pekka Pessi committed
186 187 188 189
  return e - s;
}

/** Calculate span of a token or linear whitespace characters.  */
190
su_inline isize_t span_token_lws(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
191 192 193 194 195 196 197
{
  char const *e = s; 
  while (_bnf_table[(unsigned char)(*e)] & (bnf_token | bnf_lws))
    e++; 
  return e - s; 
}

198
#if 1
Pekka Pessi's avatar
Pekka Pessi committed
199
/** Calculate span of a token characters.  */
200
su_inline isize_t span_token(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
201 202 203 204 205 206
{
  char const *e = s; 
  while (_bnf_table[(unsigned char)(*e)] & bnf_token)
    e++; 
  return e - s; 
}
207 208 209 210 211
#else
size_t bnf_span_token(char const *s);
#define span_token(s) bnf_span_token((s))
#endif

Pekka Pessi's avatar
Pekka Pessi committed
212 213

/** Calculate span of a alphabetic characters.  */
214
su_inline isize_t span_alpha(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218 219 220 221 222
{
  char const *e = s; 
  while (_bnf_table[(unsigned char)(*e)] & bnf_alpha)
    e++; 
  return e - s; 
}

/** Calculate span of a digits.  */
223
su_inline isize_t span_digit(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
224 225 226 227 228 229 230 231
{
  char const *e = s; 
  while (*e >= '0' && *e <= '9')
    e++; 
  return e - s; 
}

/** Calculate span of a hex.  */
232
su_inline isize_t span_hexdigit(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
233 234 235 236 237 238 239 240
{
  char const *e = s; 
  while (IS_HEX(*e))
    e++; 
  return e - s; 
}

/** Calculate span of characters belonging to an RTSP token */
241
su_inline isize_t span_alpha_digit_safe(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244 245 246 247 248 249
{
  char const *e = s; 
  while (_bnf_table[(unsigned char)(*e)] & (bnf_alpha | bnf_safe))
    e++; 
  return e - s; 
}

/** Calculate span of a characters valid in parameters.  */
250
su_inline isize_t span_param(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
251 252 253 254 255 256 257 258
{
  char const *e = s; 
  while (IS_PARAM(*e))
    e++; 
  return e - s; 
}

/** Calculate span of a SIP word.  */
259
su_inline isize_t span_word(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
260 261 262 263 264 265 266 267
{
  char const *e = s; 
  while (*e && (IS_TOKEN(*e) || strchr(SIP_WORD, *e)))
    e++; 
  return e - s; 
}

/** Calculate span of a unreserved characters.  */
268
su_inline isize_t span_unreserved(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
269 270 271 272 273 274 275 276
{
  char const *e = s;
  while (IS_UNRESERVED(*e))
    e++;
  return e - s;
}

/** Calculate span of a double quoted string (with escaped chars inside) */
277
su_inline isize_t span_quoted(char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
  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 */
/** Reserved in URLs */
#define URL_RESERVED        ";/?:=+$,"

/** Non-alphanumeric characters without syntactical meaning. */ 
#define URL_MARK            "-_.!~*'()"

/** Unreserved characters. */ 
#define URL_UNRESERVED ALPHANUM URL_MARK

/** URL hex escape. */ 
#define URL_ESCAPED    "%"
#define URL_DELIMS     "<>#%\""
#define URL_UNWISE     "{}|\\^[]`"
#define URL_SCHEME     ALPHANUM "+-."

/** Get number of characters belonging to url scheme */
#define span_url_scheme(s) strspn(s, URL_SCHEME)

314 315 316 317
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);
318 319
SOFIAPUBFUN isize_t span_domain(char const *host);
SOFIAPUBFUN isize_t span_host(char const *host);
320 321 322 323 324

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);
325 326
SOFIAPUBFUN issize_t scan_domain(char **inout_host);
SOFIAPUBFUN issize_t scan_host(char **inout_host);
327

328 329
SOFIA_END_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
330
#endif /* !defined BNF_H */