msg_mclass.c 10 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * 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
 *
 */

/**@ingroup msg_parser 
 * @CFILE msg_mclass.c
 *
 * Message factory object.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Jun  5 14:34:24 2002 ppessi
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
43
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
44 45

#include <stdarg.h>
46
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
47

48 49
#include <sofia-sip/su.h>
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
50 51

#include "msg_internal.h"
52 53 54
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/msg_mclass.h"
#include "sofia-sip/msg_mclass_hash.h"
Pekka Pessi's avatar
Pekka Pessi committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

/** Clone a message class.
 *
 * @relates msg_mclass_s
 *
 * The function msg_mclass_clone() makes a copy of message class object @a
 * old. It is possible to resize the hash table by giving a non-zero @a
 * newsize. If @a newsize is 0, the size of hash table is not changed. If @a
 * empty is true, the copied message class object will not recognize any
 * headers. This is useful if more fine-grained control of parsing process
 * is required, for instance.
 * 
 * @param old      pointer to the message class object to be copied [IN]
 * @param newsize  size of hash table in the copied object [IN]
 * @param empty    if true, resulting copy does not contain any headers [IN]
 * 
 * @return 
 * The function msg_mclass_clone() returns a pointer to a newly
 * copied message class object, or NULL upon an error.
74
 * The returned message class object can be freed with free().
Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 *
 * @ERRORS
 * @ERROR ENOMEM
 * A memory allocation failed.
 * @ERROR EINVAL
 * The function was given invalid arguments.
 * 
 * @note The empty parser can handle request/status line. All headers are
 * put into list of unknown headers (unless they are malformed, and they are
 * put into list of erronous headers). However, SIP, RTSP, and HTTP
 * protocols all require that the parser recognizes @b Content-Length header
 * before they can extract the message body from the data received from
 * network.
 *
 */
msg_mclass_t *msg_mclass_clone(msg_mclass_t const *old, int newsize, int empty)
{
92
  size_t size, shortsize;
Pekka Pessi's avatar
Pekka Pessi committed
93 94 95 96 97 98 99 100 101 102 103 104 105
  msg_mclass_t *mc; 
  int identical;
  unsigned short i;

  if (newsize == 0)
    newsize = old->mc_hash_size;

  if (newsize < old->mc_hash_used) {
    errno = EINVAL;
    return NULL;
  }

  size = offsetof(msg_mclass_t, mc_hash[newsize]);
106 107 108 109 110
  if (old->mc_short)
    shortsize = MC_SHORT_SIZE * (sizeof old->mc_short[0]);
  else
    shortsize = 0;
  mc = malloc(size + shortsize);
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  identical = newsize == old->mc_hash_size && !empty;

  if (mc) {
    if (!identical) {
      memcpy(mc, old, offsetof(msg_mclass_t, mc_hash));
      memset(mc->mc_hash, 0, size - offsetof(msg_mclass_t, mc_hash));
      mc->mc_short = NULL;
      mc->mc_hash_size = newsize;
      mc->mc_hash_used = 0;
      for (i = 0; !empty && i < old->mc_hash_size; i++) {
	msg_mclass_insert(mc, &old->mc_hash[i]);
      }
    }
    else {
      memcpy(mc, old, size);
      mc->mc_short = NULL;
    }
    
129 130 131
    if (shortsize) {
      if (empty)
	mc->mc_short = memset((char *)mc + size, 0, shortsize);
Pekka Pessi's avatar
Pekka Pessi committed
132
      else
133
	mc->mc_short = memcpy((char *)mc + size, old->mc_short, shortsize);
Pekka Pessi's avatar
Pekka Pessi committed
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    }
  }

  return mc;
}

/**Add a new header to the message class.
 *
 * @relates msg_mclass_s
 *
 * Insert a header class @a hc to the message class object @a mc. If the
 * given @a offset of the header in @ref msg_pub_t "public message
 * structure" is zero, the function extends the public message structure in
 * order to store newly inserted header there.
 *
 * @param mc       pointer to a message class object [IN/OUT]
 * @param hc       pointer to a header class object [IN]
 * @param offset   offset of the header in 
 *                 @ref msg_pub_t "public message structure" [IN]
 *
 * If the @a offset is 0, the msg_mclass_insert_header() increases size of
 * the public message structure and places the header at the end of message.
 *
 * @return Number of collisions in hash table, or -1 upon an error.
 * 
 * @deprecated Use msg_mclass_insert_header_flags() instead.
 */
int msg_mclass_insert_header(msg_mclass_t *mc, 
			     msg_hclass_t *hc,
			     unsigned short offset)
{
  msg_href_t hr[1];

  if (mc == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (offset == 0)
    offset = mc->mc_msize, mc->mc_msize += sizeof(msg_header_t *);

  assert(offset < mc->mc_msize);

  hr->hr_class = hc;
  hr->hr_offset = offset;

  return msg_mclass_insert(mc, hr);
}

/**Add a new header to the message class.
 *
 * @relates msg_mclass_s
 *
 * Insert a header class @a hc to the message class object @a mc. If the
 * given @a offset of the header in @ref msg_pub_t "public message
 * structure" is zero, the function extends the public message structure in
 * order to store headers.
 *
 * @param mc       pointer to a message class object [IN/OUT]
 * @param hc       pointer to a header class object [IN]
 * @param offset   offset of the header in 
 *                 @ref msg_pub_t "public message structure" [IN]
 * @param flags    classification flags for the header [IN]
 *
 * If the @a offset is 0, the msg_mclass_insert_header() increases size of
 * the public message structure and places the header at the end of message.
 *
 * @return Number of collisions in hash table, or -1 upon an error.
 */
int msg_mclass_insert_with_mask(msg_mclass_t *mc, 
				msg_hclass_t *hc,
				unsigned short offset,
				unsigned short flags)
{
  msg_href_t hr[1];

  if (mc == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (offset == 0)
    offset = mc->mc_msize, mc->mc_msize += sizeof(msg_header_t *);

  assert(offset < mc->mc_msize);

  hr->hr_class = hc;
  hr->hr_offset = offset;
  hr->hr_flags = flags;

  return msg_mclass_insert(mc, hr);
}

/** Add a header reference to the message class.
 *
 * @relates msg_mclass_s
 *
 * @param mc       pointer to a message class object [IN/OUT]
 * @param hr       header reference object [IN]
 *
 * @return Number of collisions in hash table, or -1 upon an error.
 */
int msg_mclass_insert(msg_mclass_t *mc, msg_href_t const *hr)
{
  int j, j0;
  int N;
  int collisions = 0;
  msg_hclass_t *hc;

  if (mc == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (hr == NULL || (hc = hr->hr_class) == NULL) 
    return 0;

  /* Add short form */
  if (mc->mc_short && hc->hc_short[0]) {
    char compact = hc->hc_short[0];
    msg_href_t *shorts = (msg_href_t *)mc->mc_short;

    if (compact < 'a' || compact > 'z')
      return -1;

    if (shorts[compact - 'a'].hr_class && 
	shorts[compact - 'a'].hr_class != hc)
      return -1;
      
    shorts[compact - 'a'] = *hr;
  }

  N = mc->mc_hash_size;
  j0 = msg_header_name_hash(hc->hc_name, NULL) % N;

  for (j = j0; mc->mc_hash[j].hr_class; ) {
    collisions++;
    if (mc->mc_hash[j].hr_class == hc)
      return -1;
    j = (j + 1) % N;
    if (j == j0)
      return -1;
  }

  mc->mc_hash[j] = hr[0];
  mc->mc_hash_used++;
  
  return collisions;
}

/** Calculate length of line ending (0, 1 or 2) */
#define CRLF_TEST(cr, lf) ((cr) == '\r' ? ((lf) == '\n') + 1 : (cr)=='\n')

/**Search for a header class.
 *
 * @relates msg_mclass_s
 *
 * The function msg_find_hclass() searches for a header class from a message
 * class based on the contents of the header to be parsed. The buffer @a s
 * should point to the first character in the header name.
 *
 * @param mc   message class object [IN]
 * @param s    header contents [IN]
 * @param return_start_of_content start of header content [OUT]
 *                       (may be NULL)
 *
 * @return The function msg_find_hclass() returns a pointer to a header
 * reference structure. A pointer to a header reference for unknown headers
 * is returned, if the header is not included in the message class.
 *
 * @par
 * The return-value parameter @a return_start_of_content will contain the
 * start of the header contents within @a s, or 0 upon an error parsing the
 * header name and following colon.
 *
 * @par 
 * Upon a fatal error, a NULL pointer is returned.
 */
msg_href_t const *msg_find_hclass(msg_mclass_t const *mc, 
				  char const *s, 
				  int *return_start_of_content)
{
  msg_href_t const *hr;
  short i, N;
  int m;

  assert(mc);

  N = mc->mc_hash_size;

  i = msg_header_name_hash(s, &m) % N;

  if (m == 0) {
    if (return_start_of_content)
      *return_start_of_content = 0;
    return mc->mc_error;
  }

  if (m == 1 && mc->mc_short) {
    short c = s[0];
    if (c >= 'a' && c <= 'z')
      hr = &mc->mc_short[c - 'a'];
    else if (c >= 'A' && c <= 'Z')
      hr = &mc->mc_short[c - 'A'];
    else
      hr = mc->mc_unknown;

    if (hr->hr_class == NULL)
      hr = mc->mc_unknown;
  }
  else {
    msg_hclass_t *hc;

    for (hr = NULL; (hc = mc->mc_hash[i].hr_class); i = (i + 1) % N) {
      /* long form */
      if (m == hc->hc_len && strncasecmp(s, hc->hc_name, m) == 0) {
	hr = &mc->mc_hash[i];
	break;
      }
    }

    if (hr == NULL)
      hr = mc->mc_unknown;
  }

  if (!return_start_of_content)	/* Just header name */
    return hr;

  if (s[m] == ':') {		/* Fast path */
    *return_start_of_content = ++m;
    return hr;
  }

  if (IS_LWS(s[m])) {
    int crlf = 0;
    do {
      m += span_ws(s + m + crlf) + crlf; /* Skip lws before colon */
      crlf = CRLF_TEST(s[m], s[m + 1]);
    }
    while (IS_WS(s[m + crlf]));
  }

  if (s[m++] != ':')		/* Colon is required in header */
    m = 0;

  *return_start_of_content = m;

  return hr;
}