msg_tag.c 7.36 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
 *
 */

25
/**@CFILE msg_tag.c  Message tag classes
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Fri Feb 23 12:46:42 2001 ppessi
 *
 */

#include "config.h"

#include <assert.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>

40
#include <sofia-sip/su.h>
Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44

#define TAG_NAMESPACE "msg"

#include "msg_internal.h"
45 46
#include "sofia-sip/msg_header.h"
#include "sofia-sip/msg_parser.h"
Pekka Pessi's avatar
Pekka Pessi committed
47

48 49 50 51
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_inline.h>
#include <sofia-sip/su_tagarg.h>
#include "sofia-sip/msg_tag_class.h"
Pekka Pessi's avatar
Pekka Pessi committed
52 53 54 55 56 57 58 59 60

#define NONE ((void*)-1)

int msghdrtag_snprintf(tagi_t const *t, char b[], size_t size)
{
  msg_header_t const *h;

  assert(t);

61 62 63
  if (!t) {
    if (size) b[0] = 0;
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
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
  }

  h = (msg_header_t const *)t->t_value;

  if (h != MSG_HEADER_NONE && h != NULL) {
    return msg_header_field_e(b, size, h, 0);
  }
  else {
    return snprintf(b, size, "<NONE>");
  }
}

size_t msghdrtag_xtra(tagi_t const *t, size_t offset)
{
  msg_header_t const *h;
  size_t rv;
  msg_hclass_t *hc = (msg_hclass_t *)t->t_tag->tt_magic;
  assert(t);

  for (h = (msg_header_t const *)t->t_value, rv = offset;
       h != NULL;
       h = h->sh_next) {
    if (h == MSG_HEADER_NONE)
      break;
    MSG_STRUCT_SIZE_ALIGN(rv);
    if (hc)
      rv = hc->hc_dxtra(h, rv + h->sh_class->hc_size);
    else
      rv = h->sh_class->hc_dxtra(h, rv + h->sh_class->hc_size);
  }

  return rv - offset;
}

tagi_t *msghdrtag_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  msg_header_t const *o;
101
  msg_header_t *h, *h0 = NULL, **hh;
Pekka Pessi's avatar
Pekka Pessi committed
102 103
  msg_hclass_t *hc, *hc0 = (msg_hclass_t *)src->t_tag->tt_magic;
  char *b;
104
  size_t size;
Pekka Pessi's avatar
Pekka Pessi committed
105

106
  assert(src); assert(*bb);
Pekka Pessi's avatar
Pekka Pessi committed
107 108 109 110 111

  dst->t_tag = src->t_tag;
  dst->t_value = 0L;

  b = *bb;
112
  hh = &h0;
Pekka Pessi's avatar
Pekka Pessi committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  for (o = (msg_header_t const *)src->t_value;
       o != NULL;
       o = o->sh_next) {
    if (o == MSG_HEADER_NONE) {
      *hh = (msg_header_t *)o;
      break;
    }

    /* XXX assert(msg_hdr_p(o)); */

    MSG_STRUCT_ALIGN(b);
    h = (msg_header_t *)b;
    hc = hc0 ? hc0 : o->sh_class;
    b += hc->hc_size;
    memset(h, 0, hc->hc_size);
    h->sh_class = hc;
130 131 132 133 134

    size = SIZE_MAX - (uintptr_t)b;
    if (size > ISSIZE_MAX)
      size = ISSIZE_MAX;
    b = hc->hc_dup_one(h, o, b, size);
135 136 137 138

    if (hc->hc_update)
      msg_header_update_params(h->sh_common, 0);

Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143 144
    *hh = h; hh = &h->sh_next;

    assert(b != NULL);
  }
  *bb = b;

145 146
  dst->t_value = (tag_value_t)h0;

Pekka Pessi's avatar
Pekka Pessi committed
147 148 149 150 151
  return dst + 1;
}


/** Convert a string to a header structure based on to the tag. */
152 153
int msghdrtag_scan(tag_type_t tt, su_home_t *home,
		   char const *s,
Pekka Pessi's avatar
Pekka Pessi committed
154 155 156 157 158 159 160
		   tag_value_t *return_value)
{
  msg_hclass_t *hc = (msg_hclass_t *)tt->tt_magic;
  msg_header_t *h;
  int retval;

  h = msg_header_make(home, hc, s);
161

Pekka Pessi's avatar
Pekka Pessi committed
162 163 164 165
  if (h)
    *return_value = (tag_value_t)h, retval = 1;
  else
    *return_value = (tag_value_t)NULL, retval = -1;
166

Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172
  return retval;
}


tagi_t *msgstrtag_filter(tagi_t *dst,
			 tagi_t const f[],
173
			 tagi_t const *src,
Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180 181
			 void **bb);

int msgobjtag_snprintf(tagi_t const *t, char b[], size_t size)
{
  msg_pub_t const *mo;

  assert(t);

182 183 184
  if (!t || !t->t_value) {
    if (size) b[0] = 0;
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
185 186 187 188
  }

  mo = (msg_pub_t *)t->t_value;

189
  return (int)msg_object_e(b, size, mo, MSG_DO_CANONIC);
Pekka Pessi's avatar
Pekka Pessi committed
190 191 192 193 194 195 196
}


size_t msgobjtag_xtra(tagi_t const *t, size_t offset)
{
  msg_header_t const *h;
  msg_pub_t const *mo;
197
  size_t rv;
Pekka Pessi's avatar
Pekka Pessi committed
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

  assert(t);

  mo = (msg_pub_t const *)t->t_value;

  if (mo == NULL || mo == NONE)
    return 0;

  rv = offset;

  MSG_STRUCT_SIZE_ALIGN(rv);
  rv += mo->msg_size;

  if (mo->msg_request)
    h = (msg_header_t const *)mo->msg_request;
  else
    h = (msg_header_t const *)mo->msg_status;

  for (; h; h = h->sh_succ) {
    MSG_STRUCT_SIZE_ALIGN(rv);
    rv += msg_header_size(h);
  }

  return rv - offset;
}


tagi_t *msgobjtag_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  msg_pub_t const *omo;
  msg_pub_t *mo;
  msg_header_t *h;
  msg_header_t const *o;
  char *b;

233
  assert(src); assert(*bb);
Pekka Pessi's avatar
Pekka Pessi committed
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

  omo = (msg_pub_t const *)src->t_value;

  dst->t_tag = src->t_tag;
  dst->t_value = 0;

  if (omo == NULL || omo == NONE) {
    dst->t_value = src->t_value;
    return dst + 1;
  }

  b = *bb;
  MSG_STRUCT_ALIGN(b);
  mo = (msg_pub_t *)b;
  b += omo->msg_size;

  memset(mo, 0, omo->msg_size);
  mo->msg_size = omo->msg_size;
  mo->msg_flags = omo->msg_flags;

  if (mo->msg_request)
    o = mo->msg_request;
  else
    o = mo->msg_status;

  for (; o; o = o->sh_succ) {
260
    size_t size;
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265
    MSG_STRUCT_ALIGN(b);
    h = (msg_header_t *)b;
    b += o->sh_class->hc_size;
    memset(h, 0, o->sh_class->hc_size);
    h->sh_class = o->sh_class;
266 267 268 269
    size = SIZE_MAX - (uintptr_t)b;
    if (size > ISSIZE_MAX)
      size = ISSIZE_MAX;
    b = o->sh_class->hc_dup_one(h, o, b, size);
270 271
    if (o->sh_class->hc_update)
      msg_header_update_params(h->sh_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
272 273 274
    assert(b != NULL);
  }

Michael Jerris's avatar
Michael Jerris committed
275
  dst->t_value = (tag_value_t)mo;
Pekka Pessi's avatar
Pekka Pessi committed
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
  *bb = b;

  return dst + 1;
}

#if 0

int msgtag_multipart_xtra(tagi_t const *t, int offset)
{
  msg_header_t const *h;
  msg_multipart_t const *mp;
  int rv;

  assert(t);

  mp = (msg_multipart_t const *)t->t_value;

  if (mp == NULL || mp == NONE)
    return 0;

  rv = offset;

  MSG_STRUCT_SIZE_ALIGN(rv);
  rv += mo->msg_size;

  if (mo->msg_request)
    h = (msg_header_t const *)mo->msg_request;
  else
    h = (msg_header_t const *)mo->msg_status;

  for (; h; h = h->sh_succ) {
    MSG_STRUCT_SIZE_ALIGN(rv);
    rv += msg_header_size(h);
  }

  return rv - offset;
}


tagi_t *msgtag_multipart_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  msg_pub_t const *omo;
  msg_pub_t *mo;
  msg_header_t *h;
  msg_header_t const *o;
  char *b;

323
  assert(src); assert(*bb);
Pekka Pessi's avatar
Pekka Pessi committed
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

  omo = (msg_pub_t const *)src->t_value;

  dst->t_tag = src->t_tag;
  dst->t_value = 0;

  if (omo == NULL || omo == NONE) {
    dst->t_value = src->t_value;
    return dst + 1;
  }

  b = *bb;
  MSG_STRUCT_ALIGN(b);
  mo = (msg_pub_t *)b;
  b += omo->msg_size;

  memset(mo, 0, omo->msg_size);
  mo->msg_size = omo->msg_size;
  mo->msg_flags = omo->msg_flags;

  if (mo->msg_request)
    o = mo->msg_request;
  else
    o = mo->msg_status;

  for (; o; o = o->sh_succ) {
350
    size_t size;
Pekka Pessi's avatar
Pekka Pessi committed
351 352 353 354 355
    MSG_STRUCT_ALIGN(b);
    h = (msg_header_t *)b;
    b += o->sh_class->hc_size;
    memset(h, 0, o->sh_class->hc_size);
    h->sh_class = o->sh_class;
356 357 358 359
    size = SIZE_MAX - (uintptr_t)b;
    if (size > ISSIZE_MAX)
      size = ISSIZE_MAX;
    b = o->sh_class->hc_dup_one(h, o, b, size);
360 361
    if (o->sh_class->hc_update)
      msg_header_update_params(h->sh_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367 368 369 370 371
    assert(b != NULL);
  }

  dst->t_value = (long)mo;
  *bb = b;

  return dst + 1;
}

#endif