msg_header_copy.c 12.5 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
 * 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_headers
26 27
 * @CFILE msg_header_copy.c
 *
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * Copying and duplicating headers structures.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 *
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

44
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
45

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

48
#include "msg_internal.h"
49 50 51
#include "sofia-sip/msg.h"
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/msg_header.h"
Pekka Pessi's avatar
Pekka Pessi committed
52 53

/** Calculate size of a parameter vector */
54
su_inline
55
size_t msg_params_copy_xtra(msg_param_t const pp[], size_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
56
{
57
  size_t n = msg_params_count(pp);
Pekka Pessi's avatar
Pekka Pessi committed
58 59 60 61 62 63 64 65
  if (n) {
    MSG_STRUCT_SIZE_ALIGN(offset);
    offset += MSG_PARAMS_NUM(n + 1) * sizeof(pp[0]);
  }
  return offset;
}

/** Copy a vector of parameters */
66
su_inline
67
char *msg_params_copy(char *b, size_t size,
68
		      msg_param_t **dst,
Pekka Pessi's avatar
Pekka Pessi committed
69 70
		      msg_param_t const src[])
{
71
  size_t n = msg_params_count(src);
Pekka Pessi's avatar
Pekka Pessi committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  if (n) {
    MSG_STRUCT_ALIGN(b);
    *dst = memcpy(b, src, (n + 1) * sizeof(src[0]));
    b += MSG_PARAMS_NUM(n + 1) * sizeof(src[0]);
  }
  else {
    *dst = NULL;
  }

  return b;
}

/**Copy a header object.
 *
 * The function @c msg_header_copy_as() shallowly copies a header object.
 *
 * @param home pointer to the memory home
 * @param hc   header class for the copied header
 * @param src  pointer to a header object
 *
93
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
94 95 96
 * The function @c msg_header_copy_as() returns a pointer to the the shallow copy
 * of the header object, or @c NULL upon an error.
 */
97
static msg_header_t *msg_header_copy_one_as(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101
					    msg_hclass_t *hc,
					    msg_header_t const *src)
{
  msg_header_t *h;
102
  size_t size = hc->hc_size, xtra;
Pekka Pessi's avatar
Pekka Pessi committed
103 104 105 106 107 108 109 110 111 112 113 114
  msg_param_t const *params;
  char *end;

  if (hc->hc_params) {
    params = *(msg_param_t const **)((char const *)src + hc->hc_params);
    xtra = msg_params_copy_xtra(params, size) - size;
  }
  else {
    params = NULL;
    xtra = 0;
  }

115
  if (!(h = msg_header_alloc(home, hc, (isize_t)xtra)))
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    return NULL;			/* error */

  memcpy(&h->sh_data, &src->sh_data, size - offsetof(msg_common_t, h_data));
  h->sh_next = NULL;
  if (params) {
    msg_param_t **pparams = (msg_param_t **)((char *)h + hc->hc_params);
    end = msg_params_copy((char *)h + size, xtra, pparams, params);
    if (!end) {
      su_free(home, h);
      return NULL;
    }
  }
  else
    end = (char *)h + size;

  assert(end == (char *)h + xtra + size);

  return h;
}

/**Copy a list of header objects.
 *
 * The function @c msg_header_copy_as() shallowly copies a list of header
 * objects, and casts them to the given header class.
 *
 * @param home pointer to the memory home
 * @param hc   header class
 * @param src  pointer to a list of header objects to be copied
 *
 * @return The function @c msg_header_copy_as() returns a pointer to the
 * first of the copied msg header object(s), or @c NULL upon an error.
 */
148
msg_header_t *msg_header_copy_as(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
				 msg_hclass_t *hc,
				 msg_header_t const *src)
{
  msg_header_t *h, *rv = NULL, *prev = NULL;

  if (src == NULL || src == MSG_HEADER_NONE)
    return NULL;

  if (hc == NULL)
    hc = src->sh_class;

  for (; src; src = src->sh_next, prev = h) {
    if (!(h = msg_header_copy_one_as(home, hc, src)))
      break;

164
    if (!rv)
Pekka Pessi's avatar
Pekka Pessi committed
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
      rv = h;
    else
      prev->sh_next = h;
  }

  if (src) {
    /* Copy was not successful, free all copied headers in list */
    for (;rv; rv = h) {
      h = rv->sh_next;
      su_free(home, rv);
    }
  }

  return rv;
}

/** Copy a single header. */
msg_header_t *msg_header_copy_one(su_home_t *home, msg_header_t const *src)
{
  assert(MSG_HEADER_TEST(src));

  if (!src || !src->sh_class)
    return NULL;

  return msg_header_copy_one_as(home, src->sh_class, src);
}

/** Copy a header list. */
msg_header_t *msg_header_copy(su_home_t *home, msg_header_t const *src)
{
  assert(MSG_HEADER_TEST(src));

  if (!src || !src->sh_class)
    return NULL;

  return msg_header_copy_as(home, src->sh_class, src);
}

/** Duplicate a sigle header.
 *
 * Deeply copy a single header.
 *
 * @param home pointer to the memory home
 * @param src  pointer to asingle header object to be copied
 *
 * @return Return a pointer to the
 * the duplicated msg header object(s), or @c NULL upon an error.
 */
213
msg_header_t *msg_header_dup_one(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216
				 msg_header_t const *src)
{
  msg_hclass_t *hc;
217
  size_t size, xtra;
Pekka Pessi's avatar
Pekka Pessi committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  msg_header_t *h;
  char *end;

  if (src == NULL || src == MSG_HEADER_NONE)
    return NULL;

  hc = src->sh_class;

  assert(hc);

  size = hc->hc_size;
  xtra = hc->hc_dxtra(src, size) - size;

  if (!(h = msg_header_alloc(home, hc, xtra)))
    return NULL;

  if (!(end = hc->hc_dup_one(h, src, (char *)h + size, xtra))) {
    su_free(home, h);
    return NULL;
  }

239 240 241
  if (hc->hc_update)
    msg_header_update_params(h->sh_common, 1);

Pekka Pessi's avatar
Pekka Pessi committed
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
  assert(end == (char *)h + size + xtra);

  return h;
}

/** Duplicate a header as class @a hc.
 *
 * The function @c msg_header_dup_as() casts a list of header headers to
 * given type, and then deeply copies the list.
 *
 * @param home pointer to the memory home
 * @param hc   header class
 * @param src  pointer to a list of header objects to be copied
 *
 * @return The function @c msg_header_copy_as() returns a pointer to the
 * first of the copied msg header object(s), or @c NULL upon an error.
 */
msg_header_t *msg_header_dup_as(su_home_t *home, msg_hclass_t *hc,
				msg_header_t const *src)
{
  msg_header_t *h, *rv = NULL, **prev;

  if (src == NULL || src == MSG_HEADER_NONE)
    return NULL;

  if (hc == NULL)
    hc = src->sh_class;

  assert(hc);

  for (prev = &rv; src; src = src->sh_next, prev = &h->sh_next) {
273 274
    size_t size = hc->hc_size;
    size_t xtra = hc->hc_dxtra(src, size) - size;
Pekka Pessi's avatar
Pekka Pessi committed
275 276
    char *end;

277
    if (!(h = msg_header_alloc(home, hc, (isize_t)xtra)))
Pekka Pessi's avatar
Pekka Pessi committed
278 279
      break;			/* error */

280
    if (!rv)
Pekka Pessi's avatar
Pekka Pessi committed
281 282 283 284 285
      rv = h;

    if (!(end = hc->hc_dup_one(h, src, (char *)h + size, xtra)))
      break;			/* error */

286 287 288
    if (hc->hc_update)
      msg_header_update_params(h->sh_common, 1);

Pekka Pessi's avatar
Pekka Pessi committed
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
    assert(end == (char *)h + size + xtra);

    *prev = h;
  }

  if (src) {
    /* Copy was not successful, free all duplicated headers in list */
    for (;rv; rv = h) {
      h = rv->sh_next;
      su_free(home, rv);
    }
  }

  return rv;
}

/** Duplicate a header list.
 *
 * The function @c msg_header_dup() deeply copies a list of message headers
 * objects.
 *
 * @param home pointer to the memory home
 * @param h    pointer to a list of header objects to be copied
 *
 * @return The function @c msg_header_dup() returns a pointer to the first
 * of the copied message header object(s), or @c NULL upon an error.
 */
msg_header_t *msg_header_dup(su_home_t *home, msg_header_t const *h)
{
  if (h == NULL || h == MSG_HEADER_NONE)
    return NULL;
  assert(MSG_HEADER_TEST(h));
  return msg_header_dup_as(home, h->sh_class, h);
}

/** Calculate extra size of a plain header. */
325
isize_t msg_default_dup_xtra(msg_header_t const *header, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
  return offset;
}

/**Duplicate a header object without external references.
 *
 * The function @c msg_default_dup_one() copies the contents of header
 * object @a src to @a h. The header object should not contain external
 * references (pointers).
 *
 * @param h     pointer to newly allocated header object
 * @param src   pointer to a header object to be duplicated
 * @param b     memory buffer used to copy (not used)
 * @param xtra  number bytes in buffer @a b (not used)
 *
 * @return The function @c msg_default_dup_one() returns a pointer to the
 * memory buffer @a b.
 */
char *msg_default_dup_one(msg_header_t *h,
			  msg_header_t const *src,
346
			  char *b,
347
			  isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
348 349 350 351 352 353 354
{
  memcpy(&h->sh_header_next[1],
	 &src->sh_header_next[1],
	 h->sh_class->hc_size - offsetof(msg_header_t, sh_header_next[1]));

  return b;
}
355 356 357 358 359

/* ====================================================================== */
/* Copying or duplicating all headers in a message */

static int msg_copy_chain(msg_t *msg, msg_t const *copied);
360
static int msg_dup_or_copy_all(msg_t *msg,
361
			       msg_t const *original,
362
			       msg_header_t *(*copy_one)(su_home_t *h,
363 364 365 366 367
							 msg_header_t const *));


/**Copy a message shallowly.
 *
368
 * @relatesalso msg_s
369
 *
370 371 372 373
 * Copy a message and the header structures. The copied message will share
 * all the strings with the original message. It will keep a reference to
 * the original message, and the original message is not destroyed until all
 * the copies have been destroyed.
374 375
 *
 * @param original message to be copied
376 377 378
 *
 * @retval pointer to newly copied message object when successful
 * @retval NULL upon an error
379 380 381 382 383 384 385
 */
msg_t *msg_copy(msg_t *original)
{
  if (original) {
    msg_t *copy = msg_create(original->m_class, original->m_object->msg_flags);

    if (copy) {
386 387
      if (original->m_chain
	  ? msg_copy_chain(copy, original) < 0
388 389 390
	  : msg_dup_or_copy_all(copy, original, msg_header_copy_one) < 0) {
	msg_destroy(copy), copy = NULL;
      }
391
      else
392 393 394 395 396 397 398 399 400
	msg_set_parent(copy, original);

      return copy;
    }
  }

  return NULL;
}

401 402 403 404 405
/** Copy header chain.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
406 407 408 409 410 411 412 413 414
static
int msg_copy_chain(msg_t *msg, msg_t const *original)
{
  su_home_t *home = msg_home(msg);
  msg_pub_t *dst = msg->m_object;
  msg_header_t **tail;
  msg_header_t *dh;
  msg_header_t const *sh;
  msg_header_t **hh;
415

416
  tail = msg->m_tail;
417

418 419 420 421 422 423
  for (sh = original->m_chain; sh; sh = (msg_header_t const *)sh->sh_succ) {
    hh = msg_hclass_offset(msg->m_class, dst, sh->sh_class);
    if (!hh)
      break;
    while (*hh)
      hh = &(*hh)->sh_next;
424

425
    dh = msg_header_copy_one(home, sh);
426
    if (!dh)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
      break;

    dh->sh_prev = tail, *tail = dh, tail = &dh->sh_succ;

    *hh = dh;
  }

  msg->m_tail = tail;

  if (sh)
    return -1;

  return 0;

}

/**Deep copy a message.
 *
445
 * @relatesalso msg_s
446
 *
447 448 449
 * Copy a message, the header structures and all the related strings. The
 * duplicated message does not share any (non-const) data with original.
 * Note that the cached representation (in h_data) is not copied.
450 451
 *
 * @param original message to be duplicated
452 453 454
 *
 * @retval pointer to newly duplicated message object when successful
 * @retval NULL upon an error
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
 */
msg_t *msg_dup(msg_t const *original)
{
  if (original) {
    msg_t *dup = msg_create(original->m_class, original->m_object->msg_flags);

    if (dup && msg_dup_or_copy_all(dup, original, msg_header_dup_one) < 0) {
      msg_destroy(dup), dup = NULL;
    }

    return dup;
  }

  return NULL;
}

471
/** Copy a complete message, not keeping the header chain structure.
472 473 474 475
 *
 * @retval 0 when successful
 * @retval -1 upon an error
 */
476
static
477
int msg_dup_or_copy_all(msg_t *msg,
478
			msg_t const *original,
479
			msg_header_t *(*copy_one)(su_home_t *h,
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
						  msg_header_t const *))
{
  su_home_t *home = msg_home(msg);
  msg_pub_t *dst = msg->m_object;

  msg_pub_t const *src = original->m_object;
  msg_header_t * const *ssh;
  msg_header_t * const *end;
  msg_header_t const *sh;
  msg_header_t **hh;

  msg_header_t *h;

  assert(copy_one);

  end = (msg_header_t**)((char *)src + src->msg_size);
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
  for (ssh = &src->msg_request; ssh < end; ssh++) {
    sh = *ssh;
    if (!sh)
      continue;

    hh = msg_hclass_offset(msg->m_class, dst, sh->sh_class);
    if (hh == NULL)
	return -1;

    for (; sh; sh = sh->sh_next) {
      h = copy_one(home, sh);
      if (h == NULL)
	return -1;

      if (*hh) {
	/* If there is multiple instances of single headers,
	   put the extra headers into the list of erroneous headers */
514 515 516 517 518 519 520
	if (msg_is_single(h)) {
	  msg_error_t **e;
	  for (e = &dst->msg_error; *e; e = &(*e)->er_next)
	    ;
	  *e = (msg_error_t *)h;
	  continue;
	}
521 522 523 524 525 526 527 528 529 530 531 532 533 534

	while (*hh)
	  hh = &(*hh)->sh_next;
      }
      *hh = h;

      if (msg_is_list(sh))
	/* Copy only first list entry */
	break;
    }
  }

  return 0;
}