msg_mclass.c 10.1 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

/** Clone a message class.
 *
58
 * @relatesalso msg_mclass_s
Pekka Pessi's avatar
Pekka Pessi committed
59 60 61 62 63 64 65 66
 *
 * 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.
 * 
67 68 69
 * @param[in] old      pointer to the message class object to be copied 
 * @param[in] newsize  size of hash table in the copied object 
 * @param[in] empty    if true, resulting copy does not contain any headers 
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73
 * 
 * @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
  msg_mclass_t *mc; 
  int identical;
  unsigned short i;

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

100 101 102
  if (newsize < old->mc_hash_used ||
      newsize > USHRT_MAX / sizeof(msg_header_t *)) {

Pekka Pessi's avatar
Pekka Pessi committed
103 104 105 106 107
    errno = EINVAL;
    return NULL;
  }

  size = offsetof(msg_mclass_t, mc_hash[newsize]);
108 109 110 111 112
  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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  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;
    }
    
131 132 133
    if (shortsize) {
      if (empty)
	mc->mc_short = memset((char *)mc + size, 0, shortsize);
Pekka Pessi's avatar
Pekka Pessi committed
134
      else
135
	mc->mc_short = memcpy((char *)mc + size, old->mc_short, shortsize);
Pekka Pessi's avatar
Pekka Pessi committed
136 137 138 139 140 141 142 143
    }
  }

  return mc;
}

/**Add a new header to the message class.
 *
144
 * @relatesalso msg_mclass_s
Pekka Pessi's avatar
Pekka Pessi committed
145 146 147 148 149 150
 *
 * 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.
 *
151 152 153 154
 * @param[in,out] mc       pointer to a message class object 
 * @param[in]     hc       pointer to a header class object 
 * @param[in]     offset   offset of the header in
 *                         @ref msg_pub_t "public message structure"
Pekka Pessi's avatar
Pekka Pessi committed
155 156 157 158 159 160
 *
 * 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.
 * 
161
 * @deprecated Use msg_mclass_insert_with_mask() instead.
Pekka Pessi's avatar
Pekka Pessi committed
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
 */
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.
 *
187
 * @relatesalso msg_mclass_s
Pekka Pessi's avatar
Pekka Pessi committed
188
 *
189 190 191 192
 * Insert a header class @a hc to the message class @a mc. If the given @a
 * offset of the header in @ref msg_pub_t "public message structure" is
 * zero, extend the size of the public message structure in order to store
 * headers at the end of structure.
Pekka Pessi's avatar
Pekka Pessi committed
193
 *
194 195 196 197 198
 * @param[in,out] mc       pointer to a message class
 * @param[in]     hc       pointer to a header class
 * @param[in]     offset   offset of the header in 
 *                         @ref msg_pub_t "public message structure" 
 * @param[in]     flags    classification flags for the header 
Pekka Pessi's avatar
Pekka Pessi committed
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
 *
 * @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.
 *
228
 * @relatesalso msg_mclass_s
Pekka Pessi's avatar
Pekka Pessi committed
229
 *
230 231
 * @param[in,out] mc       pointer to a message class object 
 * @param[in]     hr       header reference object 
Pekka Pessi's avatar
Pekka Pessi committed
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
 *
 * @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;
}

283
/** Calculate length of line ending (0, 1 or 2). @internal */
Pekka Pessi's avatar
Pekka Pessi committed
284 285 286 287
#define CRLF_TEST(cr, lf) ((cr) == '\r' ? ((lf) == '\n') + 1 : (cr)=='\n')

/**Search for a header class.
 *
288
 * @relatesalso msg_mclass_s
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291 292 293
 *
 * 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.
 *
294 295 296
 * @param[in]  mc   message class object 
 * @param[in]  s    header contents 
 * @param[out] return_start_of_content start of header content (may be NULL)
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
 *
 * @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, 
312
				  isize_t *return_start_of_content)
Pekka Pessi's avatar
Pekka Pessi committed
313 314
{
  msg_href_t const *hr;
315 316
  short i, N, m;
  isize_t len;
Pekka Pessi's avatar
Pekka Pessi committed
317 318 319 320 321

  assert(mc);

  N = mc->mc_hash_size;

322
  i = msg_header_name_hash(s, &len) % N;
Pekka Pessi's avatar
Pekka Pessi committed
323

324
  if (len == 0 || len > HC_LEN_MAX) {
Pekka Pessi's avatar
Pekka Pessi committed
325 326 327 328 329
    if (return_start_of_content)
      *return_start_of_content = 0;
    return mc->mc_error;
  }

330 331
  m = (short)len;

Pekka Pessi's avatar
Pekka Pessi committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  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;

347
    /* long form */
Pekka Pessi's avatar
Pekka Pessi committed
348
    for (hr = NULL; (hc = mc->mc_hash[i].hr_class); i = (i + 1) % N) {
349
      if (m == hc->hc_len && strncasecmp(s, hc->hc_name, m) == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
350 351 352 353 354 355 356 357 358 359 360 361
	hr = &mc->mc_hash[i];
	break;
      }
    }

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

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

362 363
  if (s[len] == ':') {		/* Fast path */
    *return_start_of_content = ++len;
Pekka Pessi's avatar
Pekka Pessi committed
364 365 366
    return hr;
  }

367
  if (IS_LWS(s[len])) {
Pekka Pessi's avatar
Pekka Pessi committed
368 369
    int crlf = 0;
    do {
370 371
      len += span_ws(s + len + crlf) + crlf; /* Skip lws before colon */
      crlf = CRLF_TEST(s[len], s[len + 1]);
Pekka Pessi's avatar
Pekka Pessi committed
372
    }
373
    while (IS_WS(s[len + crlf]));
Pekka Pessi's avatar
Pekka Pessi committed
374 375
  }

376 377
  if (s[len++] != ':')		/* Colon is required in header */
    len = 0;
Pekka Pessi's avatar
Pekka Pessi committed
378

379
  *return_start_of_content = len;
Pekka Pessi's avatar
Pekka Pessi committed
380 381 382

  return hr;
}