msg_header_make.c 3.86 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 43 44 45 46 47 48 49 50 51 52 53
 * 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
 * @CFILE msg_header_make.c
 *
 * Creating message headers from strings.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Fri Feb 23 14:06:34 2001 ppessi
 *
 */

#include "config.h"

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

#if defined(va_copy)
/* Xyzzy */
#elif defined(__va_copy)
#define va_copy(dst, src) __va_copy((dst), (src))
#else
#define va_copy(dst, src) (memcpy(&(dst), &(src), sizeof (va_list)))
#endif

#include <assert.h>

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

56 57 58 59
#include "sofia-sip/msg.h"
#include "sofia-sip/bnf.h"
#include "sofia-sip/msg_parser.h"
#include "sofia-sip/msg_header.h"
Pekka Pessi's avatar
Pekka Pessi committed
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 126 127 128 129 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172

/** Make a header from a value string. */
msg_header_t *msg_header_make(su_home_t *home, 
			      msg_hclass_t *hc,
			      char const *s)
{
  int xtra;
  msg_header_t *h;
  int normal = hc->hc_name ||
    (hc->hc_hash != msg_payload_hash && 
     hc->hc_hash != msg_separator_hash &&
     hc->hc_hash != msg_error_hash);

  if (s == NULL)
    return NULL;

  /* For normal headers, strip LWS from both ends */
  if (normal) 
    skip_lws(&s);
  xtra = strlen(s);
  if (normal)
    while (xtra > 0 && IS_LWS(s[xtra - 1]))
      xtra--;

  h = msg_header_alloc(home, hc, xtra + 1);

  if (h) {
    char *b = MSG_HEADER_DATA(h);

    strncpy(b, s, xtra)[xtra] = 0;

    if (hc->hc_parse(home, h, b, xtra) == -1) {
      /* Note: parsing function is responsible to free 
	 everything it has allocated (like parameter lists) */
      /* XXX - except header structures */
      su_free(home, h), h = NULL;
    }
  }

  return h;
}

/** Make a MSG header with formatting provided. */
msg_header_t *msg_header_vformat(su_home_t *home, 
				msg_hclass_t *hc,
				char const *fmt,
				va_list ap)
{
  msg_header_t *h;
  
  int n, xtra = 64;		/* reasonable default */


  /* Quick path */
  if (!fmt || !strchr(fmt, '%'))
    return msg_header_make(home, hc, fmt);

  /* Another quickie */
  if (strcmp(fmt, "%s") == 0) {
    fmt = va_arg(ap, char const *);
    return msg_header_make(home, hc, fmt);
  }
  
  if (!(h = msg_header_alloc(home, hc, xtra)))
    return NULL;

  for (;;) {
    va_list aq;

    va_copy(aq, ap);
    n = vsnprintf(MSG_HEADER_DATA(h), xtra, fmt, aq);
    va_end(aq);
    
    if (n >= 0 && n < xtra)
      break;

    /* Try again with more space */
    su_free(home, h);

    if (n >= 0)
      xtra = n + 1; /* precisely what is needed */
    else
      xtra *= 2;    /* glibc 2.0 - twice the old size */
    
    if (!(h = msg_header_alloc(home, hc, xtra)))
      return NULL;
  }
  
  if (hc->hc_parse(home, h, MSG_HEADER_DATA(h), n) == -1) {
    /* Note: parsing function is responsible to free 
       everything it has allocated (like parameter lists) */
    su_free(home, h), h = NULL;
  }

  return h;
}

msg_header_t *msg_header_format(su_home_t *home, 
				msg_hclass_t *hc,
				char const *fmt,
				...)
{
  msg_header_t *h;
  va_list ap;

  va_start(ap, fmt);

  h = msg_header_vformat(home, hc, fmt, ap);

  va_end(ap);

  return h;
}