msg_header_make.c 4.02 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
 * 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"

38 39 40 41 42 43 44
#include <sofia-sip/su_alloc.h>

#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
45 46 47 48 49
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
50 51
#include <limits.h>
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

#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>

/** Make a header from a value string. */
msg_header_t *msg_header_make(su_home_t *home, 
			      msg_hclass_t *hc,
			      char const *s)
{
68
  size_t xtra;
Pekka Pessi's avatar
Pekka Pessi committed
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
  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;
  
112 113
  int n;
  size_t xtra = 64;		/* reasonable default */
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  /* 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);
    
135
    if (n >= 0 && (size_t)n < xtra)
Pekka Pessi's avatar
Pekka Pessi committed
136 137 138 139 140
      break;

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

141 142 143
    if (xtra >= INT_MAX)
      return NULL;

Pekka Pessi's avatar
Pekka Pessi committed
144 145 146 147
    if (n >= 0)
      xtra = n + 1; /* precisely what is needed */
    else
      xtra *= 2;    /* glibc 2.0 - twice the old size */
148 149 150

    if (xtra > INT_MAX)
      xtra = INT_MAX;
Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155
    
    if (!(h = msg_header_alloc(home, hc, xtra)))
      return NULL;
  }
  
156
  if (hc->hc_parse(home, h, MSG_HEADER_DATA(h), (size_t)n) == -1) {
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    /* 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;
}