msg_mime.c 59.7 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
 * 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_mime
 * @CFILE msg_mime.c
 *
 * MIME-related headers and MIME multipart bodies for SIP/HTTP/RTSP.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 *
 *
 */

#include "config.h"

#define _GNU_SOURCE 1

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

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

48
#include <sofia-sip/su_uniqueid.h>
49
#include <sofia-sip/su_errno.h>
Martti Mela's avatar
Martti Mela committed
50

51 52 53 54 55 56 57
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
58 59 60 61 62 63 64 65
#if !HAVE_MEMMEM
void *memmem(const void *haystack, size_t haystacklen,
	     const void *needle, size_t needlelen);
#endif

/** Protocol version of MIME */
char const msg_mime_version_1_0[] = "MIME/1.0";

66 67
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mime_protos.h>
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70

/** Define a header class for headers without any extra data to copy */
#define MSG_HEADER_CLASS_G(c, l, s, kind) \
71 72 73
  MSG_HEADER_CLASS(msg_, c, l, s, g_common, kind, msg_generic, msg_generic)

#define msg_generic_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
74 75 76

/** Define a header class for a msg_list_t kind of header */
#define MSG_HEADER_CLASS_LIST(c, l, s, kind) \
77
  MSG_HEADER_CLASS(msg_, c, l, s, k_items, kind, msg_list, msg_list)
Pekka Pessi's avatar
Pekka Pessi committed
78

79
#define msg_list_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
80 81 82

/* ====================================================================== */

83
/** Calculate length of line ending (0, 1 or 2). @internal */
Pekka Pessi's avatar
Pekka Pessi committed
84 85 86 87 88 89 90 91 92
#define CRLF_TEST(b) ((b)[0] == '\r' ? ((b)[1] == '\n') + 1 : (b)[0] =='\n')

/**@ingroup msg_mime
 * @defgroup msg_multipart MIME Multipart Body
 *
 * Representing MIME multipart bodies and their manipulation.
 *
 * The #msg_multipart_t is an object for storing MIME multipart message
 * bodies. It includes message components used for framing and identifying
93
 * message parts. Its syntax is defined in  @RFC2046 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
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
 *
 * @code
 *
 *   multipart-body := [preamble CRLF]
 *                     dash-boundary transport-padding CRLF
 *                     body-part *encapsulation
 *                     close-delimiter transport-padding
 *                     [CRLF epilogue]
 *
 *   preamble := discard-text
 *
 *   discard-text := *(*text CRLF)
 *                   ; May be ignored or discarded.
 *
 *   dash-boundary := "--" boundary
 *                    ; boundary taken from the value of boundary parameter
 *                    ; of the Content-Type field.
 *
 *   boundary := 0*69<bchars> bcharsnospace
 *
 *   bchars := bcharsnospace / " "
 *
 *   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
 *                    "+" / "_" / "," / "-" / "." /
 *                    "/" / ":" / "=" / "?"
 *
 *   transport-padding := *LWSP-char
 *                        ; Composers MUST NOT generate non-zero length
 *                        ; transport padding, but receivers MUST be able to
 *                        ; handle padding added by message transports.
 *
125
 *   body-part := <"message" as defined in @RFC822, with all header fields
Pekka Pessi's avatar
Pekka Pessi committed
126 127 128 129 130
 *                 optional, not starting with the specified dash-boundary,
 *                 and with the delimiter not occurring anywhere in the body
 *                 part. Note that the semantics of a part differ from the
 *                 semantics of a message, as described in the text.>
 *
131
 *   encapsulation := delimiter transport-padding CRLF
Pekka Pessi's avatar
Pekka Pessi committed
132 133 134 135 136 137 138 139 140 141 142
 *                    body-part
 *
 *   close-delimiter := delimiter "--"
 *
 *   delimiter := CRLF dash-boundary
 *
 *   epilogue := discard-text
 *
 * @endcode
 *
 * @par Parsing a Multipart Message
143
 *
Pekka Pessi's avatar
Pekka Pessi committed
144 145 146 147 148
 * When a message body contains a multipart entity (in other words, it has a
 * MIME media type of "multipart"), the application can split the multipart
 * entity into body parts
 *
 * The parsing is relatively simple, the application just gives a memory
149
 * home object, a Content-Type header object and message body object as an
Pekka Pessi's avatar
Pekka Pessi committed
150 151
 * argument to msg_multipart_parse() function:
 * @code
152
 *    if (sip->sip_content_type &&
153
 *        su_casenmatch(sip->sip_content_type, "multipart/", 10)) {
Pekka Pessi's avatar
Pekka Pessi committed
154 155 156 157 158
 *      msg_multipart_t *mp;
 *
 *      if (sip->sip_multipart)
 *        mp = sip->sip_multipart;
 *      else
159
 *        mp = msg_multipart_parse(msg_home(msg),
Pekka Pessi's avatar
Pekka Pessi committed
160 161
 *                                 sip->sip_content_type,
 *                                 (sip_payload_t *)sip->sip_payload);
162
 *
Pekka Pessi's avatar
Pekka Pessi committed
163 164 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
 *      if (mp)
 *        ... processing multipart ...
 *      else
 *        ... error handling ...
 *    }
 * @endcode
 *
 * The resulting list of msg_multipart_t structures contain the parts of the
 * multipart entity, each part represented by a separate #msg_multipart_t
 * structure. Please note that in order to make error recovery possible, the
 * parsing is not recursive - if multipart contains another multipart, the
 * application is responsible for scanning for it and parsing it.
 *
 * @par Constructing a Multipart Message
 *
 * Constructing a multipart body is a bit more hairy. The application needs
 * a message object (#msg_t), which is used to buffer the encoding of
 * multipart components.
 *
 * As an example, let us create a "multipart/mixed" multipart entity with a
 * HTML and GIF contents, and convert it into a #sip_payload_t structure:
 * @code
 *   msg_t *msg = msg_create(sip_default_mclass, 0);
 *   su_home_t *home = msg_home(msg);
 *   sip_t *sip = sip_object(msg);
 *   sip_content_type_t *c;
 *   msg_multipart_t *mp = NULL;
 *   msg_header_t *h = NULL;
 *   char *b;
192
 *   size_t len, offset;
193
 *
Pekka Pessi's avatar
Pekka Pessi committed
194 195 196 197
 *   mp = msg_multipart_create(home, "text/html;level=3", html, strlen(html));
 *   mp->mp_next = msg_multipart_create(home, "image/gif", gif, giflen);
 *
 *   c = sip_content_type_make(home, "multipart/mixed");
198
 *
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204 205 206 207
 *   // Add delimiters to multipart, and boundary parameter to content-type
 *   if (msg_multipart_complete(home, c, mp) < 0)
 *     return -1;		// Error
 *
 *   // Combine multipart components into the chain
 *   h = NULL;
 *   if (msg_multipart_serialize(&h, mp) < 0)
 *     return -1;		// Error
 *
208
 *   // Encode all multipart components
Pekka Pessi's avatar
Pekka Pessi committed
209 210 211 212 213 214
 *   len = msg_multipart_prepare(msg, mp, 0);
 *   if (len < 0)
 *     return -1;		// Error
 *
 *   pl = sip_payload_create(home, NULL, len);
 *
215
 *   // Copy each element from multipart to pl_data
Pekka Pessi's avatar
Pekka Pessi committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
 *   b = pl->pl_data;
 *   for (offset = 0, h = mp; offset < len; h = h->sh_succ) {
 *     memcpy(b + offset, h->sh_data, h->sh_len);
 *     offset += h->sh_len;
 *   }
 * @endcode
 *
 */

/**Create a part for MIME multipart entity.
 *
 * The function msg_multipart_create() allocates a new #msg_multipart_t
 * object from memory home @a home. If @a content_type is non-NULL, it makes
 * a #msg_content_type_t header object and adds the header to the
 * #msg_multipart_t object. If @a dlen is nonzero, it allocates a
 * msg_payload_t structure of @a dlen bytes for the payload of the newly
 * created #msg_multipart_t object. If @a data is non-NULL, it copies the @a
 * dlen bytes of of data to the payload of the newly created
 * #msg_multipart_t object.
 *
 * @return A pointer to the newly created #msg_multipart_t object, or NULL
 * upon an error.
 */
msg_multipart_t *msg_multipart_create(su_home_t *home,
				      char const *content_type,
				      void const *data,
242
				      isize_t dlen)
Pekka Pessi's avatar
Pekka Pessi committed
243 244 245
{
  msg_multipart_t *mp;

246
  mp = (msg_multipart_t *)msg_header_alloc(home, msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
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 283 284 285 286 287 288 289 290

  if (mp) {
    if (content_type)
      mp->mp_content_type = msg_content_type_make(home, content_type);
    if (dlen)
      mp->mp_payload = msg_payload_create(home, data, dlen);

    if ((!mp->mp_content_type && content_type) ||
	(!mp->mp_payload && dlen)) {
      su_free(home, mp->mp_content_type);
      su_free(home, mp->mp_payload);
      su_free(home, mp);
      mp = NULL;
    }
  }

  return mp;
}

/** Convert boundary parameter to a search string. */
static char *
msg_multipart_boundary(su_home_t *home, char const *b)
{
  char *boundary;

  if (!b || !(boundary = su_alloc(home, 2 + 2 + strlen(b) + 2 + 1)))
    return NULL;

  strcpy(boundary, CR LF "--");

  if (b[0] == '"') /* " See http://bugzilla.gnome.org/show_bug.cgi?id=134216 */

    msg_unquote(boundary + 4, b);
  else
    strcpy(boundary + 4, b);


  strcat(boundary + 4, CR LF);

  return boundary;
}


/** Boundary chars. */
291
static char const bchars[] =
Pekka Pessi's avatar
Pekka Pessi committed
292 293 294 295 296 297 298 299 300 301 302 303
"'()+_,-./:=?"
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
" ";

#define bchars_len (sizeof(bchars) - 1)

/** Search for a suitable boundary from MIME. */
static char *
msg_multipart_search_boundary(su_home_t *home, char const *p, size_t len)
{
304 305
  size_t m;
  unsigned crlf;
Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309 310 311 312 313 314
  char const *end = p + len;
  char *boundary;

  if (len < 2)
    return NULL;

  /* Boundary looks like LF -- string SP* [CR] LF */
  if (memcmp("--", p, 2) == 0) {
    /* We can be at boundary beginning, there is no CR LF */
315
    m = 2 + su_memspn(p + 2, len - 2, bchars, bchars_len);
Pekka Pessi's avatar
Pekka Pessi committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329
    if (m + 2 >= len)
      return NULL;
    crlf = p[m] == '\r' ? 1 + (p[m + 1] == '\n') : (p[m] == '\n');
    while (p[m - 1] == ' ' || p[m - 1] == '\t')
      m--;
    if (m > 2 && crlf) {
      boundary = su_alloc(home, 2 + m + 2 + 1);
      if (boundary) {
	memcpy(boundary, CR LF, 2);
	memcpy(boundary + 2, p, m);
	strcpy(boundary + m + 2, CR LF);
      }
      return boundary;
    }
330
  }
Pekka Pessi's avatar
Pekka Pessi committed
331 332 333 334

  /* Look for LF -- */
  for (;(p = memmem(p, end - p, LF "--", 3)); p += 3) {
    len = end - p;
335
    m = 3 + su_memspn(p + 3, len - 3, bchars, bchars_len);
Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    if (m + 2 >= len)
      return NULL;
    crlf = p[m] == '\r' ? 1 + (p[m + 1] == '\n') : (p[m] == '\n');
    while (p[m - 1] == ' ' || p[m - 1] == '\t')
      m--;
    m--;
    if (m > 2 && crlf) {
      boundary = su_alloc(home, 2 + m + 2 + 1);
      if (boundary) {
	memcpy(boundary, CR LF, 2);
	memcpy(boundary + 2, p + 1, m);
	strcpy(boundary + 2 + m, CR LF);
      }
      return boundary;
    }
  }

  return NULL;
}

/** Parse a MIME multipart.
 *
 * The function msg_multipart_parse() parses a MIME multipart message. The
359
 * common syntax of multiparts is described in @RFC2046 (section 7).
Pekka Pessi's avatar
Pekka Pessi committed
360
 *
361 362 363
 * @param[in,out] home home for allocating structures
 * @param[in]     c    content-type header for multipart
 * @param[in]     pl   payload structure for multipart
Pekka Pessi's avatar
Pekka Pessi committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
 *
 * After parsing, the @a pl will contain the plain-text preamble (if any).
 *
 * @note If no @b Content-Type header is given, the msg_multipart_parse()
 * tries to look for a suitable boundary. Currently, it takes first
 * boundary-looking string and uses that, so it can be fooled with, for
 * instance, signature @c "--Pekka".
 */
msg_multipart_t *msg_multipart_parse(su_home_t *home,
				     msg_content_type_t const *c,
				     msg_payload_t *pl)
{
  msg_multipart_t *mp = NULL, *all = NULL, **mmp = &all;
  /* Dummy msg object */
  msg_t msg[1] = {{{ SU_HOME_INIT(msg) }}};
379
  size_t len, m, blen;
Pekka Pessi's avatar
Pekka Pessi committed
380 381 382 383 384 385 386 387 388 389 390
  char *boundary, *p, *next, save;
  char const *b, *end;
  msg_param_t param;

  p = pl->pl_data; len = pl->pl_len; end = p + len;

  su_home_init(msg_home(msg));
  msg->m_class = msg_multipart_mclass;
  msg->m_tail = &msg->m_chain;

  /* Get boundary from Content-Type */
391
  if (c && (param = msg_header_find_param(c->c_common, "boundary=")))
Pekka Pessi's avatar
Pekka Pessi committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    boundary = msg_multipart_boundary(msg_home(msg), param);
  else
    boundary = msg_multipart_search_boundary(msg_home(msg), p, len);

  if (!boundary)
    return NULL;

  m = strlen(boundary) - 2, blen = m - 1;

  /* Find first delimiter */
  if (memcmp(boundary + 2, p, m - 2) == 0)
    b = p, p = p + m - 2, len -= m - 2;
  else if ((p = memmem(p, len, boundary + 1, m - 1))) {
    if (p != pl->pl_data && p[-1] == '\r')
      b = --p, p = p + m, len -= m;
    else
      b = p, p = p + m - 1, len -= m - 1;
  }
  else {
    su_home_deinit(msg_home(msg));
    return NULL;
  }

  /* Split multipart into parts */
  for (;;) {
417
    while (p[0] == ' ')
Pekka Pessi's avatar
Pekka Pessi committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
      p++;

    p += p[0] == '\r' ? 1 + (p[1] == '\n') : (p[0] == '\n');

    len = end - p;

    if (len < blen)
      break;

    next = memmem(p, len, boundary + 1, m = blen);

    if (!next)
      break;			/* error */

    if (next != p && next[-1] == '\r')
      next--, m++;

435
    mp = (msg_multipart_t *)msg_header_alloc(msg_home(msg), msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    if (mp == NULL)
      break;			/* error */
    *mmp = mp; mmp = &mp->mp_next;

    /* Put delimiter transport-padding CRLF here */
    mp->mp_common->h_data = b;
    mp->mp_common->h_len = p - b;

    /* .. and body-part here */
    mp->mp_data = p;
    mp->mp_len = next - p;

    if (next[m] == '-' && next[m + 1] == '-') {
      /* We found close-delimiter */
      assert(mp);
      if (!mp)
	break;			/* error */
453 454
      mp->mp_close_delim = (msg_payload_t *)
	msg_header_alloc(msg_home(msg), msg_payload_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
455 456 457 458 459 460 461 462
      if (!mp->mp_close_delim)
	break;			/* error */
      /* Include also transport-padding and epilogue in the close-delimiter */
      mp->mp_close_delim->pl_data = next;
      mp->mp_close_delim->pl_len = p + len - next;
      break;
    }

463
    b = next; p = next + m;
Pekka Pessi's avatar
Pekka Pessi committed
464 465 466 467 468 469 470 471 472 473
  }

  if (!mp || !mp->mp_close_delim) {
    su_home_deinit(msg_home(msg));
    /* Delimiter error */
    return NULL;
  }

  /* Parse each part */
  for (mp = all; mp; mp = mp->mp_next) {
474
    msg->m_object = (msg_pub_t *)mp; p = mp->mp_data; next = p + mp->mp_len;
Pekka Pessi's avatar
Pekka Pessi committed
475 476 477 478 479 480 481 482 483 484 485 486

    if (msg->m_tail)
      mp->mp_common->h_prev = msg->m_tail,
	*msg->m_tail = (msg_header_t *)mp;

    msg->m_chain = (msg_header_t *)mp;
    msg->m_tail = &mp->mp_common->h_succ;

    save = *next; *next = '\0';	/* NUL-terminate this part */

    for (len = next - p; len > 0; len -= m, p += m) {
      if (IS_CRLF(p[0])) {
487
	m = msg_extract_separator(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
488 489 490 491 492
	assert(m > 0);

	p += m; len -= m;

	if (len > 0) {
493
	  m = msg_extract_payload(msg, (msg_pub_t*)mp, NULL, len, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
494 495 496 497 498 499
	  assert(m > 0);
	  assert(len == m);
	}
	break;
      }

500
      m = msg_extract_header(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514

      if (m <= 0) {
	assert(m > 0);
	/* Xyzzy */
      }
    }

    *next = save; /* XXX - Should we leave the payload NUL-terminated? */
  }

  /* Postprocess */
  blen = strlen(boundary);

  for (mp = all; mp; mp = mp->mp_next) {
515
    mp->mp_data = boundary;
Pekka Pessi's avatar
Pekka Pessi committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    mp->mp_len = blen;

    assert(mp->mp_payload || mp->mp_separator);

    if (mp->mp_close_delim) {
      msg_header_t **tail;

      if (mp->mp_payload)
	tail = &mp->mp_payload->pl_common->h_succ;
      else
	tail = &mp->mp_separator->sep_common->h_succ;

      assert(msg->m_chain == (msg_header_t *)mp);
      assert(*tail == NULL);

      mp->mp_close_delim->pl_common->h_prev = tail;
      *tail = (msg_header_t *)mp->mp_close_delim;
    }
  }

  msg_fragment_clear(pl->pl_common);
  pl->pl_len = all->mp_data - (char *)pl->pl_data;

  su_home_move(home, msg_home(msg)); su_home_deinit(msg_home(msg));

  return all;
}

/**Add all missing parts to the multipart.
545
 *
546 547 548
 * Add missing components such as boundaries between body parts, separators
 * between body-part headers and data, and close-delimiter after last
 * body-part to the multipart message.
549 550 551 552
 *
 * @param[in,out] home home for allocating structures
 * @param[in,out] c    content-type header for multipart
 * @param[in,out] mp   pointer to first multipart structure
Pekka Pessi's avatar
Pekka Pessi committed
553
 *
554 555
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
556
 *
557
 * @ERRORS
Pekka Pessi's avatar
Pekka Pessi committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571
 * @ERROR EBADMSG
 * The @b Content-Type header @a c is malformed, or multipart message
 * contains a malformed @b Content-Type header.
 * @ERROR ENOMEM
 * A memory allocation failed.
 * @ERROR EINVAL
 * The function msg_multipart_complete() was given invalid arguments.
 */
int msg_multipart_complete(su_home_t *home,
			   msg_content_type_t *c,
			   msg_multipart_t *mp)
{
  char *boundary;
  char const *b;
572
  size_t blen, m;
Pekka Pessi's avatar
Pekka Pessi committed
573 574 575 576

  if (c == NULL || mp == NULL)
    return (errno = EINVAL), -1;

577
  if (!(b = msg_header_find_param(c->c_common, "boundary="))) {
Pekka Pessi's avatar
Pekka Pessi committed
578
    /* Generate boundary */
579
    enum { tlen = 16 * 4 / 3 };
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582 583 584 585 586 587 588 589 590 591 592
    char token[sizeof("boundary=") + tlen + 1];

    if (mp->mp_data) {
      b = mp->mp_data;
      m = mp->mp_len;

      if (strncmp(b, CR LF "--", 4) == 0)
	b += 4, m -= 4;
      else if (strncmp(b, "--", 2) == 0)
	b += 2, m -= 2;
      else
	return (errno = EBADMSG), -1;
      /* XXX - quoting? */
593
      b = su_sprintf(home, "boundary=\"%.*s\"", (int)m, b);
Pekka Pessi's avatar
Pekka Pessi committed
594 595 596
    }
    else {
      strcpy(token, "boundary=");
597
      msg_random_token(token + strlen("boundary="), (size_t)tlen, NULL, 0);
Pekka Pessi's avatar
Pekka Pessi committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
      b = su_strdup(home, token);
    }

    if (!b)
      return -1;

    msg_params_replace(home, (msg_param_t **)&c->c_params, b);

    b += strlen("boundary=");
  }

  if (!(boundary = msg_multipart_boundary(home, b)))
    return -1;

  blen = strlen(boundary); m = blen - 2;

  for (; mp; mp = mp->mp_next) {
    if (mp->mp_data == NULL) {
      mp->mp_data = boundary, mp->mp_len = blen;
    } else {
      if (mp->mp_len < 3)
	return -1;
      if (mp->mp_data[0] == '\r' && mp->mp_data[1] == '\n') {
	if (mp->mp_len < m || memcmp(mp->mp_data + 2, boundary + 2, m - 2))
	  return -1;
      } else if (mp->mp_data[0] == '\n') {
	if (mp->mp_len < m - 1 || memcmp(mp->mp_data + 1, boundary + 2, m - 2))
	  return -1;
      } else {
	if (mp->mp_len < m - 2 || memcmp(mp->mp_data, boundary + 2, m - 2))
	  return -1;
      }
    }

    if (mp->mp_next == NULL) {
      if (!mp->mp_close_delim)
634
	mp->mp_close_delim = msg_payload_format(home, "%.*s--" CR LF,
635
						(int)m, boundary);
Pekka Pessi's avatar
Pekka Pessi committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
      if (!mp->mp_close_delim)
	return -1;
    }
    else if (mp->mp_close_delim) {
      msg_payload_t *e = mp->mp_close_delim;

      mp->mp_close_delim = NULL;

      if (e->pl_common->h_prev)
	*e->pl_common->h_prev = e->pl_common->h_succ;
      if (e->pl_common->h_succ)
	e->pl_common->h_succ->sh_prev = e->pl_common->h_prev;
    }

    mp->mp_common->h_data = mp->mp_data;
    mp->mp_common->h_len = mp->mp_len;

    if (!mp->mp_separator)
      if (!(mp->mp_separator = msg_separator_make(home, CR LF)))
	return -1;

    if (mp->mp_multipart) {
      c = mp->mp_content_type;
      if (c == NULL)
	return (errno = EBADMSG), -1;

      if (msg_multipart_complete(home, c, mp->mp_multipart) < 0)
	return -1;
    }

    if (!mp->mp_payload)
      if (!(mp->mp_payload = msg_payload_create(home, NULL, 0)))
	return -1;
  }

  return 0;
}

/** Serialize a multipart message.
 *
 */
msg_header_t *msg_multipart_serialize(msg_header_t **head0,
				      msg_multipart_t *mp)
{
  msg_header_t *h_succ_all = NULL;
  msg_header_t *h, **head, **hh, *h0, *h_succ;
  void *hend;

#define is_in_chain(h) ((h) && ((msg_frg_t*)(h))->h_prev != NULL)
#define insert(head, h) \
  ((h)->sh_succ = *(head), *(head) = (h), \
   (h)->sh_prev = (head), (head) = &(h)->sh_succ)

  if (mp == NULL || head0 == NULL)
    return NULL;

  h_succ_all = *head0; head = head0;

  for (; mp; mp = mp->mp_next) {
    h0 = (msg_header_t *)mp;

    assert(mp->mp_separator); assert(mp->mp_payload);
    assert(mp->mp_next || mp->mp_close_delim);

    if (!mp->mp_separator || !mp->mp_payload ||
	(!mp->mp_next && !mp->mp_close_delim))
      return NULL;

    if ((void *)mp == h_succ_all)
      h_succ_all = NULL;

    *head0 = h0; h0->sh_prev = head;

    if (is_in_chain(mp->mp_separator))
      hend = mp->mp_separator;
    else if (is_in_chain(mp->mp_payload))
      hend = mp->mp_payload;
    else if (is_in_chain(mp->mp_multipart))
      hend = mp->mp_multipart;
    else if (is_in_chain(mp->mp_close_delim))
      hend = mp->mp_close_delim;
    else if (is_in_chain(mp->mp_next))
      hend = mp->mp_next;
    else
      hend = NULL;

    /* Search latest header in chain */
    for (head = &mp->mp_common->h_succ;
	 *head && *head != hend;
	 head = &(*head)->sh_succ)
      ;

    h_succ = *head;

    /* Serialize headers */
731
    for (hh = &((msg_pub_t*)mp)->msg_request;
732
	 (char *)hh < (char *)&mp->mp_separator;
Pekka Pessi's avatar
Pekka Pessi committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
	 hh++) {
      h = *hh; if (!h) continue;
      for (h = *hh; h; h = h->sh_next) {
	if (h == h_succ || !is_in_chain(h)) {
	  *head = h; h->sh_prev = head; head = &h->sh_succ;
	  while (*head && *head != hend)
	    head = &(*head)->sh_succ;
	  if (h == h_succ)
	    h_succ = *head;
	}
	else {
	  /* XXX Check that h is between head and hend */
	}
      }
    }

    if (!is_in_chain(mp->mp_separator)) {
      insert(head, (msg_header_t *)mp->mp_separator);
    } else {
      assert(h_succ == (msg_header_t *)mp->mp_separator);
      mp->mp_separator->sep_common->h_prev = head;
      *head = (msg_header_t *)mp->mp_separator;
      head = &mp->mp_separator->sep_common->h_succ;
      h_succ = *head;
    }

    if (!is_in_chain(mp->mp_payload)) {
      insert(head, (msg_header_t *)mp->mp_payload);
    } else {
      assert(h_succ == (msg_header_t *)mp->mp_payload);
      mp->mp_payload->pl_common->h_prev = head;
      *head = (msg_header_t *)mp->mp_payload;
      head = &mp->mp_payload->pl_common->h_succ;
      h_succ = *head;
    }

    if (mp->mp_multipart) {
      if ((*head = h_succ))
	h_succ->sh_prev = head;
      if (!(h = msg_multipart_serialize(head, mp->mp_multipart)))
	return NULL;
      head = &h->sh_succ; h_succ = *head;
    }

    if (mp->mp_close_delim) {
      if (!is_in_chain(mp->mp_close_delim)) {
	insert(head, (msg_header_t*)mp->mp_close_delim);
      } else {
	assert(h_succ == (msg_header_t *)mp->mp_close_delim);
	mp->mp_close_delim->pl_common->h_prev = head;
	*head = (msg_header_t *)mp->mp_close_delim;
	head = &mp->mp_close_delim->pl_common->h_succ;
      }

      if (h_succ_all)
	*head = h_succ_all, h_succ_all->sh_prev = head;

      return (msg_header_t *)mp->mp_close_delim;
    }

    *head = h_succ;

    head0 = head;
  }

  assert(!mp);

  return NULL;
}

/** Encode a multipart.
 *
 * @return The size of multipart in bytes, or -1 upon an error.
 */
807
issize_t msg_multipart_prepare(msg_t *msg, msg_multipart_t *mp, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
808 809 810 811
{
  if (!mp || !mp->mp_data)
    return -1;

812
  if (!mp->mp_common->h_data ||
Pekka Pessi's avatar
Pekka Pessi committed
813 814 815 816 817 818 819 820 821 822
      mp->mp_common->h_len != mp->mp_len - 2 ||
      memcmp(mp->mp_common->h_data, mp->mp_data + 2, mp->mp_len - 2)) {
    mp->mp_common->h_data = mp->mp_data + 2;
    mp->mp_common->h_len = mp->mp_len - 2;
  }

  return msg_headers_prepare(msg, (msg_header_t *)mp, flags);
}

/** Decode a multipart. */
823
issize_t msg_multipart_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
824 825 826 827 828 829 830 831
{
  su_home_t tmphome[1] = { SU_HOME_INIT(tmphome) };
  msg_payload_t pl[1];
  msg_multipart_t *mp, *result;

  assert(h && msg_is_multipart(h));

  msg_payload_init(pl);
832

833
  result = (msg_multipart_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
834

835
  pl->pl_data = s;
Pekka Pessi's avatar
Pekka Pessi committed
836 837 838 839 840 841 842 843
  pl->pl_len = slen;

  mp = msg_multipart_parse(tmphome, NULL, pl);

  if (mp) {
    *result = *mp;

    if (result->mp_common->h_succ->sh_prev)
844
      result->mp_common->h_succ->sh_prev =
Pekka Pessi's avatar
Pekka Pessi committed
845 846 847
	&result->mp_common->h_succ;

    su_free(tmphome, mp);
848

Pekka Pessi's avatar
Pekka Pessi committed
849 850
    su_home_move(home, tmphome);
  }
851

Pekka Pessi's avatar
Pekka Pessi committed
852 853 854 855 856 857 858 859 860 861
  su_home_deinit(tmphome);

  return mp ? 0 : -1;
}

/** Encode a multipart.
 *
 * Please note that here we just encode a element, the msg_multipart_t
 * itself.
 */
862
issize_t msg_multipart_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
863 864 865 866 867
{
  return msg_payload_e(b, bsiz, h, flags);
}

/** Calculate extra size of a multipart */
868
isize_t msg_multipart_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
869
{
870
  msg_multipart_t const *mp = (msg_multipart_t *)h;
871
  msg_header_t const * const *hh;
Pekka Pessi's avatar
Pekka Pessi committed
872 873 874

  offset = msg_payload_dup_xtra(h, offset);

875
  for (hh = (msg_header_t const **)&((msg_pub_t *)mp)->msg_request;
876
       (char *)hh <= (char *)&mp->mp_close_delim;
Pekka Pessi's avatar
Pekka Pessi committed
877 878 879 880 881 882 883 884 885 886 887 888
       hh++) {
    for (h = *hh; h; h = h->sh_next) {
      MSG_STRUCT_SIZE_ALIGN(offset);
      offset = h->sh_class->hc_dxtra(h, offset + h->sh_class->hc_size);
    }
  }

  return offset;
}

/** Duplicate one msg_multipart_t object */
char *msg_multipart_dup_one(msg_header_t *dst, msg_header_t const *src,
889
			    char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
890
{
891
  msg_multipart_t const *mp = (msg_multipart_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
892 893 894 895 896
  msg_header_t *h, **hh;
  char *end = b + xtra;

  b = msg_payload_dup_one(dst, src, b, xtra);

897
  for (hh = &((msg_pub_t*)mp)->msg_request;
898
       (char *)hh <= (char *)&mp->mp_close_delim;
Pekka Pessi's avatar
Pekka Pessi committed
899 900 901 902 903 904 905
       hh++) {
    for (h = *hh; h; h = h->sh_next) {
      MSG_STRUCT_ALIGN(b);
      dst = (msg_header_t *)b;
      memset(dst, 0, sizeof dst->sh_common);
      dst->sh_class = h->sh_class;
      b = h->sh_class->hc_dup_one(dst, h, b + h->sh_class->hc_size, end - b);
906 907
      if (h->sh_class->hc_update)
	msg_header_update_params(h->sh_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
      assert(b <= end);
    }
  }

  return b;
}

#if 0
msg_hclass_t msg_multipart_class[] =
MSG_HEADER_CLASS(msg_, multipart, NULL, "", mp_common, append, msg_multipart);
#endif

/**Calculate Q value.
 *
 * The function msg_q_value() converts q-value string @a q to numeric value
 * in range (0..1000).  Q values are used, for instance, to describe
 * relative priorities of registered contacts.
 *
 * @param q q-value string ("1" | "." 1,3DIGIT)
 *
 * @return
 * The function msg_q_value() returns an integer in range 0 .. 1000.
 */
unsigned msg_q_value(char const *q)
{
  unsigned value = 0;

  if (!q)
    return 500;
  if (q[0] != '0' && q[0] != '.' && q[0] != '1')
    return 500;
  while (q[0] == '0')
    q++;
  if (q[0] >= '1' && q[0] <= '9')
    return 1000;
  if (q[0] == '\0')
    return 0;
  if (q[0] != '.')
    /* Garbage... */
    return 500;

  if (q[1] >= '0' && q[1] <= '9') {
    value = (q[1] - '0') * 100;
    if (q[2] >= '0' && q[2] <= '9') {
      value += (q[2] - '0') * 10;
      if (q[3] >= '0' && q[3] <= '9') {
	value += (q[3] - '0');
	if (q[4] > '5' && q[4] <= '9')
	  /* Round upwards */
	  value += 1;
	else if (q[4] == '5')
	  value += value & 1; /* Round to even */
      }
    }
  }

  return value;
}

/** Parse media type (type/subtype).
 *
 * The function msg_mediatype_d() parses a mediatype string.
 *
971 972
 * @param[in,out] ss    string to be parsed
 * @param[out]    type  value result for media type
Pekka Pessi's avatar
Pekka Pessi committed
973 974 975 976
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
977
issize_t msg_mediatype_d(char **ss, char const **type)
Pekka Pessi's avatar
Pekka Pessi committed
978 979 980
{
  char *s = *ss;
  char const *result = s;
981
  size_t l1 = 0, l2 = 0, n;
Pekka Pessi's avatar
Pekka Pessi committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  /* Media type consists of two tokens, separated by / */

  l1 = span_token(s);
  for (n = l1; IS_LWS(s[n]); n++);
  if (s[n] == '/') {
    for (n++; IS_LWS(s[n]); n++);
    l2 = span_token(s + n);
    n += l2;
  }

  if (l1 == 0 || l2 == 0)
    return -1;

  /* If there is extra ws between tokens, compact version */
  if (n > l1 + 1 + l2) {
    s[l1] = '/';
    memmove(s + l1 + 1, s + n - l2, l2);
    s[l1 + 1 + l2] = 0;
  }

  s += n;

  while (IS_WS(*s)) *s++ = '\0';

  *ss = s;

  if (type)
    *type = result;

  return 0;
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept Accept Header
 *
 * The @b Accept request-header field can be used to specify certain media
 * types which are acceptable for the response. Its syntax is defined in
 * [H14.1, S20.1] as follows:
 *
 * @code
 *    Accept         = "Accept" ":" #( media-range [ accept-params ] )
 *
 *    media-range    = ( "*" "/" "*"
 *                     | ( type "/" "*" )
 *                     | ( type "/" subtype ) ) *( ";" parameter )
 *
 *    accept-params  = ";" "q" "=" qvalue *( accept-extension )
 *
 *    accept-extension = ";" token [ "=" ( token | quoted-string ) ]
 * @endcode
 *
 */

/**@ingroup msg_accept
 * @typedef typedef struct msg_accept_s msg_accept_t;
 *
 * The structure msg_accept_t contains representation of an @b Accept
 * header.
 *
 * The msg_accept_t is defined as follows:
 * @code
 * typedef struct msg_accept_s {
 *   msg_common_t        ac_common[1]; // Common fragment info
 *   msg_accept_t       *ac_next;      // Pointer to next Accept header
 *   char const         *ac_type;      // Pointer to type/subtype
 *   char const         *ac_subtype;   // Points after first slash in type
 *   msg_param_t const  *ac_params;    // List of parameters
 *   msg_param_t         ac_q;         // Value of q parameter
 * } msg_accept_t;
 * @endcode
 */

msg_hclass_t msg_accept_class[] =
1058
MSG_HEADER_CLASS(msg_, accept, "Accept", "", ac_params, apndlist,
1059
		 msg_accept, msg_accept);
Pekka Pessi's avatar
Pekka Pessi committed
1060

1061
issize_t msg_accept_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1062
{
1063
  msg_accept_t *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1064

1065 1066
  while (*s == ',')   /* Ignore empty entries (comma-whitespace) */
    *s = '\0', s += span_lws(s + 1) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1067

1068 1069
  if (*s == '\0') {
    /* Empty Accept list is not an error */
1070
    ac->ac_type = ac->ac_subtype = "";
1071
    return 0;
1072
  }
Pekka Pessi's avatar
Pekka Pessi committed
1073

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
  /* "Accept:" #(type/subtyp ; *(parameters))) */
  if (msg_mediatype_d(&s, &ac->ac_type) == -1)
    return -1;
  if (!(ac->ac_subtype = strchr(ac->ac_type, '/')))
    return -1;
  ac->ac_subtype++;

  if (*s == ';' && msg_params_d(home, &s, &ac->ac_params) == -1)
    return -1;

  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1085 1086
}

1087
issize_t msg_accept_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1088 1089
{
  char *b0 = b, *end = b + bsiz;
1090
  msg_accept_t const *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

  assert(msg_is_accept(h));

  if (ac->ac_type) {
    MSG_STRING_E(b, end, ac->ac_type);
    MSG_PARAMS_E(b, end, ac->ac_params, flags);
  }
  MSG_TERM_E(b, end);

  return b - b0;
}

1103
isize_t msg_accept_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1104
{
1105
  msg_accept_t const *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1106 1107

  if (ac->ac_type) {
1108 1109
    MSG_PARAMS_SIZE(offset, ac->ac_params);
    offset += MSG_STRING_SIZE(ac->ac_type);
Pekka Pessi's avatar
Pekka Pessi committed
1110 1111
  }

1112
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1113 1114 1115 1116
}

/** Duplicate one msg_accept_t object */
char *msg_accept_dup_one(msg_header_t *dst, msg_header_t const *src,
1117
		      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1118
{
1119 1120
  msg_accept_t *ac = (msg_accept_t *)dst;
  msg_accept_t const *o = (msg_accept_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1121 1122 1123 1124 1125 1126 1127 1128 1129
  char *end = b + xtra;

  if (o->ac_type) {
    b = msg_params_dup(&ac->ac_params, o->ac_params, b, xtra);
    MSG_STRING_DUP(b, ac->ac_type, o->ac_type);
    if ((ac->ac_subtype = strchr(ac->ac_type, '/')))
      ac->ac_subtype++;
  }

1130
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1131 1132 1133 1134

  return b;
}

1135 1136
/** Update parameter(s) for Accept header. */
int msg_accept_update(msg_common_t *h,
1137
		      char const *name, isize_t namelen,
1138
		      char const *value)
Pekka Pessi's avatar
Pekka Pessi committed
1139
{
1140 1141 1142 1143 1144
  msg_accept_t *ac = (msg_accept_t *)h;

  if (name == NULL) {
    ac->ac_q = NULL;
  }
1145
  else if (namelen == 1 && su_casenmatch(name, "q", 1)) {
1146 1147 1148 1149 1150
    /* XXX - check for invalid value? */
    ac->ac_q = value;
  }

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1151 1152 1153 1154 1155
}

/* ====================================================================== */

/** Decode an Accept-* header. */
1156 1157 1158
issize_t msg_accept_any_d(su_home_t *home,
			  msg_header_t *h,
			  char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1159
{
1160
  /** @relatesalso msg_accept_any_s */
1161
  msg_accept_any_t *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1162

1163 1164
  while (*s == ',')   /* Ignore empty entries (comma-whitespace) */
    *s = '\0', s += span_lws(s + 1) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1165

1166 1167
  if (*s == '\0')
    return -2;			/* Empty list */
1168

1169 1170 1171
  /* "Accept-*:" 1#(token *(SEMI accept-param)) */
  if (msg_token_d(&s, &aa->aa_value) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1172

1173 1174
  if (*s == ';' && msg_params_d(home, &s, &aa->aa_params) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1175

1176
  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1177 1178
}

1179
/** Encode an Accept-* header field. */
1180
issize_t msg_accept_any_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1181
{
1182
  /** @relatesalso msg_accept_any_s */
Pekka Pessi's avatar
Pekka Pessi committed
1183
  char *b0 = b, *end = b + bsiz;
1184
  msg_accept_any_t const *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1185 1186

  MSG_STRING_E(b, end, aa->aa_value);
1187
  MSG_PARAMS_E(b, end, aa->aa_params, flags);
Pekka Pessi's avatar
Pekka Pessi committed
1188 1189 1190 1191 1192 1193
  MSG_TERM_E(b, end);

  return b - b0;
}

/** Calculate extra memory used by accept-* headers. */
1194
isize_t msg_accept_any_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1195
{
1196
  /** @relatesalso msg_accept_any_s */
1197
  msg_accept_any_t const *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1198

1199 1200
  MSG_PARAMS_SIZE(offset, aa->aa_params);
  offset += MSG_STRING_SIZE(aa->aa_value);
Pekka Pessi's avatar
Pekka Pessi committed
1201

1202
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1203 1204 1205 1206
}

/** Duplicate one msg_accept_any_t object. */
char *msg_accept_any_dup_one(msg_header_t *dst, msg_header_t const *src,
1207
			     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1208
{
1209
  /** @relatesalso msg_accept_any_s */
1210 1211
  msg_accept_any_t *aa = (msg_accept_any_t *)dst;
  msg_accept_any_t const *o = (msg_accept_any_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1212 1213
  char *end = b + xtra;

1214
  b = msg_params_dup(&aa->aa_params, o->aa_params, b, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1215 1216
  MSG_STRING_DUP(b, aa->aa_value, o->aa_value);

1217
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1218 1219 1220 1221

  return b;
}

1222 1223
/** Update parameter(s) for Accept-* header. */
int msg_accept_any_update(msg_common_t *h,
1224
			  char const *name, isize_t namelen,
1225 1226 1227 1228 1229 1230 1231
			  char const *value)
{
  msg_accept_any_t *aa = (msg_accept_any_t *)h;

  if (name == NULL) {
    aa->aa_q = NULL;
  }
1232
  else if (namelen == 1 && su_casenmatch(name, "q", 1)) {
1233 1234 1235 1236 1237 1238
    aa->aa_q = value;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_charset Accept-Charset Header
 *
 * The Accept-Charset header is similar to Accept, but restricts the
 * character set that are acceptable in the response.  Its syntax is
 * defined in [H14.2] as follows:
 *
 * @code
 *    Accept-Charset = "Accept-Charset" ":"
 *            1#( ( charset | "*" )[ ";" "q" "=" qvalue ] )
 * @endcode
 *
 */

/**@ingroup msg_accept_charset
 * @typedef typedef struct msg_accept_charset_s msg_accept_charset_t;
 *
 * The structure msg_accept_encoding_t contains representation of @b
 * Accept-Charset header.
 *
 * The msg_accept_charset_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Charset
 *   char const         *aa_value;     // Charset
1267
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1268 1269 1270 1271 1272 1273 1274
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_charset_t;
 * @endcode
 */

msg_hclass_t msg_accept_charset_class[1] =
 MSG_HEADER_CLASS(msg_, accept_charset, "Accept-Charset", "",
1275
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1276

1277
issize_t msg_accept_charset_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1278 1279 1280 1281
{
  return msg_accept_any_d(home, h, s, slen);
}

1282
issize_t msg_accept_charset_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
{
  assert(msg_is_accept_charset(h));
  return msg_accept_any_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_encoding Accept-Encoding Header
 *
 * The Accept-Encoding header is similar to Accept, but restricts the
 * content-codings that are acceptable in the response.  Its syntax is
1295
 * defined in [H14.3, S20.2] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
 *
 * @code
 *    Accept-Encoding  = "Accept-Encoding" ":"
 *                       1#( codings [ ";" "q" "=" qvalue ] )
 *    codings          = ( content-coding | "*" )
 *    content-coding   = token
 * @endcode
 *
 */

/**@ingroup msg_accept_encoding
 * @typedef typedef struct msg_accept_encoding_s msg_accept_encoding_t;
 *
 * The structure msg_accept_encoding_t contains representation of @b
 * Accept-Encoding header.
 *
 * The msg_accept_encoding_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Encoding
 *   char const         *aa_value;     // Content-coding
1318
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1319 1320 1321 1322 1323 1324 1325
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_encoding_t;
 * @endcode
 */

msg_hclass_t msg_accept_encoding_class[1] =
 MSG_HEADER_CLASS(msg_, accept_encoding, "Accept-Encoding", "",
1326
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1327

1328
issize_t msg_accept_encoding_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1329 1330 1331 1332
{
  return msg_accept_any_d(home, h, s, slen);
}

1333
issize_t msg_accept_encoding_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
{
  return msg_accept_any_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_accept_language Accept-Language Header
 *
 * The Accept-Language header allows the client to indicate to the server in
 * which language it would prefer to receive reason phrases, session
 * descriptions or status responses carried as message bodies. Its syntax is
1346
 * defined in [H14.4, S20.3] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
 *
 * @code
 *    Accept-Language = "Accept-Language" ":"
 *                      1#( language-range [ ";" "q" "=" qvalue ] )
 *
 *    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
 * @endcode
 *
 */

/**@ingroup msg_accept_language
 * @typedef typedef struct msg_accept_language_s msg_accept_language_t;
 *
 * The structure msg_accept_language_t contains representation of @b
 * Accept-Language header.
 *
 * The msg_accept_language_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t        aa_common[1]; // Common fragment info
 *   msg_accept_any_t   *aa_next;      // Pointer to next Accept-Encoding
 *   char const         *aa_value;     // Language-range
1369
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1370 1371 1372 1373 1374 1375 1376
 *   char const         *aa_q;	       // Q-value
 * } msg_accept_language_t;
 * @endcode
 */

msg_hclass_t msg_accept_language_class[1] =
 MSG_HEADER_CLASS(msg_, accept_language, "Accept-Language", "",
1377
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1378

1379
issize_t msg_accept_language_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1380 1381 1382 1383
{
  return msg_accept_any_d(home, h, s, slen);
}

1384
issize_t msg_accept_language_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
{
  assert(msg_is_accept_language(h));
  return msg_accept_any_e(b, bsiz, h, f);
}


/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_disposition Content-Disposition Header
 *
 * The Content-Disposition header field describes how the message body or,
 * in the case of multipart messages, a message body part is to be
1398 1399
 * interpreted by the UAC or UAS.  Its syntax is defined in [S20.11]
 * as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
 *
 * @code
 *    Content-Disposition   =  "Content-Disposition" ":"
 *                             disposition-type *( ";" disposition-param )
 *    disposition-type      =  "render" | "session" | "icon" | "alert"
 *                         |   disp-extension-token
 *    disposition-param     =  "handling" "="
 *                             ( "optional" | "required" | other-handling )
 *                         |   generic-param
 *    other-handling        =  token
 *    disp-extension-token  =  token
 * @endcode
 *
 * The Content-Disposition header was extended by
 * draft-lennox-sip-reg-payload-01.txt section 3.1 as follows:
 *
 * @code
 *    Content-Disposition      =  "Content-Disposition" ":"
 *                                disposition-type *( ";" disposition-param )
1419 1420 1421
 *    disposition-type        /=  "script" | "sip-cgi" | token
 *    disposition-param       /=  action-param
 *                             /  modification-date-param
Pekka Pessi's avatar
Pekka Pessi committed
1422 1423 1424 1425 1426 1427 1428
 *    action-param             =  "action" "=" action-value
 *    action-value             =  "store" | "remove" | token
 *    modification-date-param  =  "modification-date" "=" quoted-date-time
 *    quoted-date-time         =  <"> SIP-date <">
 * @endcode
 */

1429
/**@ingroup msg_content_disposition
1430
 * @typedef struct msg_content_disposition_s msg_content_disposition_t;
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
 *
 * The structure msg_content_disposition_t contains representation of an @b
 * Content-Disposition header.
 *
 * The msg_content_disposition_t is defined as follows:
 * @code
 * typedef struct msg_content_disposition_s
 * {
 *   msg_common_t       cd_common[1];  // Common fragment info
 *   msg_error_t       *cd_next;       // Link to next (dummy)
 *   char const        *cd_type;       // Disposition type
 *   msg_param_t const *cd_params;     // List of parameters
 *   msg_param_t        cd_handling;   // Value of @b handling parameter
 *   unsigned           cd_required:1; // True if handling=required
 *   unsigned           cd_optional:1; // True if handling=optional
 * } msg_content_disposition_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1450 1451
msg_hclass_t msg_content_disposition_class[] =
MSG_HEADER_CLASS(msg_, content_disposition, "Content-Disposition", "",
1452 1453
		 cd_params, single, msg_content_disposition,
		 msg_content_disposition);
Pekka Pessi's avatar
Pekka Pessi committed
1454

1455
issize_t msg_content_disposition_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1456
{
1457
  msg_content_disposition_t *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1458 1459 1460 1461 1462 1463

  if (msg_token_d(&s, &cd->cd_type) < 0 ||
      (*s == ';' && msg_params_d(home, &s, &cd->cd_params) < 0))
      return -1;

  if (cd->cd_params)
1464
    msg_header_update_params(cd->cd_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1465 1466 1467 1468

  return 0;
}

1469
issize_t msg_content_disposition_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1470 1471
{
  char *b0 = b, *end = b + bsiz;
1472
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

  assert(msg_is_content_disposition(h));

  MSG_STRING_E(b, end, cd->cd_type);
  MSG_PARAMS_E(b, end, cd->cd_params, f);

  MSG_TERM_E(b, end);

  return b - b0;
}

1484
isize_t msg_content_disposition_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1485
{
1486
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1487

1488 1489
  MSG_PARAMS_SIZE(offset, cd->cd_params);
  offset += MSG_STRING_SIZE(cd->cd_type);
Pekka Pessi's avatar
Pekka Pessi committed
1490

1491
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1492 1493 1494 1495 1496
}

/** Duplicate one msg_content_disposition_t object */
char *msg_content_disposition_dup_one(msg_header_t *dst,
				     msg_header_t const *src,
1497
				     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1498
{
1499 1500
  msg_content_disposition_t *cd = (msg_content_disposition_t *)dst;
  msg_content_disposition_t const *o = (msg_content_disposition_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1501 1502 1503 1504 1505
  char *end = b + xtra;

  b = msg_params_dup(&cd->cd_params, o->cd_params, b, xtra);
  MSG_STRING_DUP(b, cd->cd_type, o->cd_type);

1506
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1507 1508 1509 1510

  return b;
}

1511 1512
/** Update Content-Disposition parameters */
int msg_content_disposition_update(msg_common_t *h,
1513
				   char const *name, isize_t namelen,
1514
				   char const *value)
Pekka Pessi's avatar
Pekka Pessi committed
1515
{
1516
  msg_content_disposition_t *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1517

1518 1519 1520 1521
  if (name == NULL) {
    cd->cd_handling = NULL, cd->cd_required = 0, cd->cd_optional = 0;
  }
  else if (namelen == strlen("handling") &&
1522
	   su_casenmatch(name, "handling", namelen)) {
1523
    cd->cd_handling = value;
1524 1525
    cd->cd_required = su_casematch(value, "required");
    cd->cd_optional = su_casematch(value, "optional");
1526
  }
Pekka Pessi's avatar
Pekka Pessi committed
1527

1528
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1529 1530 1531 1532 1533 1534 1535 1536
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_encoding Content-Encoding Header
 *
 * The Content-Encoding header indicates what additional content codings
1537 1538
 * have been applied to the entity-body. Its syntax is defined in [H14.11]
 * and [S20.12] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1539 1540
 *
 * @code
1541
 *    Content-Encoding = ( "Content-Encoding" / "e" ) ":" 1#content-coding
Pekka Pessi's avatar
Pekka Pessi committed
1542 1543 1544 1545
 *    content-coding   = token
 * @endcode
 */

1546
/**@ingroup msg_content_encoding
1547
 * @typedef struct msg_list_s msg_content_encoding_t;
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
 *
 * The structure msg_content_encoding_t contains representation of an @b
 * Content-Encoding header.
 *
 * The msg_content_encoding_t is defined as follows:
 * @code
 * typedef struct msg_list_s
 * {
 *   msg_common_t       k_common[1];  // Common fragment info
 *   msg_list_t        *k_next;	      // Link to next header
 *   msg_param_t       *k_items;      // List of items
 * } msg_content_encoding_t;
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1563
msg_hclass_t msg_content_encoding_class[] =
1564
  MSG_HEADER_CLASS_LIST(content_encoding, "Content-Encoding", "e", list);
Pekka Pessi's avatar
Pekka Pessi committed
1565

1566
issize_t msg_content_encoding_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1567
{
1568
  msg_content_encoding_t *e = (msg_content_encoding_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1569 1570 1571
  return msg_commalist_d(home, &s, &e->k_items, msg_token_scan);
}

1572
issize_t msg_content_encoding_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
{
  assert(msg_is_content_encoding(h));
  return msg_list_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_language Content-Language Header
 *
 * The Content-Language header describes the natural language(s) of the
 * intended audience for the enclosed message body. Note that this might not
 * be equivalent to all the languages used within the message-body. Its
 * syntax is defined in [H14.12, S20.13] as follows:
 *
 * @code
 *    Content-Language  = "Content-Language" ":" 1#language-tag
 * @endcode
 * or
 * @code
 *    Content-Language  =  "Content-Language" HCOLON
 *                         language-tag *(COMMA language-tag)
 *    language-tag      =  primary-tag *( "-" subtag )
 *    primary-tag       =  1*8ALPHA
 *    subtag            =  1*8ALPHA
 * @endcode
 *
 */

/**@ingroup msg_content_language
 * @typedef typedef struct msg_content_language_s msg_content_language_t;
 *
 * The structure msg_content_language_t contains representation of @b
 * Content-Language header.
 *
 * The msg_content_language_t is defined as follows:
 * @code
 * typedef struct {
 *   msg_common_t            k_common[1]; // Common fragment info
 *   msg_content_language_t *k_next;      // (Content-Encoding header)
 *   msg_param_t            *k_items;     // List of languages
 * } msg_content_language_t;
 * @endcode
 */

msg_hclass_t msg_content_language_class[] =
MSG_HEADER_CLASS_LIST(content_language, "Content-Language", "", list);

1621
issize_t msg_content_language_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1622
{
1623
  msg_content_language_t *k = (msg_content_language_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1624 1625 1626
  return msg_commalist_d(home, &s, &k->k_items, msg_token_scan);
}

1627
issize_t msg_content_language_e(char b[], isize_t bsiz, msg_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
{
  assert(msg_is_content_language(h));
  return msg_list_e(b, bsiz, h, f);
}

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_length Content-Length Header
 *
 * The Content-Length header indicates the size of the message-body in
 * decimal number of octets.  Its syntax is defined in [S10.18] as
 * follows:
 *
 * @code
 *    Content-Length  =  ( "Content-Length" / "l" ) HCOLON 1*DIGIT
 * @endcode
 *
 */

/**@ingroup msg_content_length
 * @typedef typedef struct msg_content_length_s msg_content_length_t;
 *
 * The structure msg_content_length_t contains representation of a
 * Content-Length header.
 *
 * The msg_content_length_t is defined as follows:
 * @code
 * typedef struct msg_content_length_s {
 *   msg_common_t   l_common[1];        // Common fragment info
 *   msg_error_t   *l_next;             // Link to next (dummy)
 *   unsigned long  l_length;           // Numeric value
 * } msg_content_length_t;
 * @endcode
 */

#define msg_content_length_d msg_numeric_d
#define msg_content_length_e msg_numeric_e

msg_hclass_t msg_content_length_class[] =
MSG_HEADER_CLASS(msg_, content_length, "Content-Length", "l",
1669
		 l_common, single_critical, msg_default, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687

/**@ingroup msg_content_length
 *Create a @b Content-Length header object.
 *
 * The function msg_content_length_create() creates a Content-Length
 * header object with the value @a n.  The memory for the header is
 * allocated from the memory home @a home.
 *
 * @param home  memory home
 * @param n     payload size in bytes
 *
 * @return
 * The function msg_content_length_create() returns a pointer to newly
 * created @b Content-Length header object when successful or NULL upon
 * an error.
 */
msg_content_length_t *msg_content_length_create(su_home_t *home, uint32_t n)
{
1688 1689
  msg_content_length_t *l = (msg_content_length_t *)
    msg_header_alloc(home, msg_content_length_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704

  if (l)
    l->l_length = n;

  return l;
}


/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_md5 Content-MD5 Header
 *
 * The Content-MD5 header is an MD5 digest of the entity-body for the
 * purpose of providing an end-to-end message integrity check (MIC) of the
1705
 * message-body. Its syntax is defined in [@RFC1864, H14.15] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1706 1707 1708
 *
 * @code
 *      Content-MD5   = "Content-MD5" ":" md5-digest
1709 1710 1711 1712 1713
 *      md5-digest   = <base64 of 128 bit MD5 digest as per @RFC1864>
 * @endcode
 */

/**@ingroup msg_content_md5
1714
 * @typedef struct msg_generic_s msg_content_md5_t;
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
 *
 * The structure msg_content_md5_t contains representation of an @b
 * Content-MD5 header.
 *
 * The msg_content_md5_t is defined as follows:
 * @code
 * typedef struct msg_generic_s
 * {
 *   msg_common_t   g_common[1];    // Common fragment info
 *   msg_generic_t *g_next;	    // Link to next header
 *   char const    *g_string;	    // Header value
 * } msg_content_md5_t;
Pekka Pessi's avatar
Pekka Pessi committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
 * @endcode
 */

#define msg_content_md5_d msg_generic_d
#define msg_content_md5_e msg_generic_e
msg_hclass_t msg_content_md5_class[] =
MSG_HEADER_CLASS_G(content_md5, "Content-MD5", "", single);

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_id Content-ID Header
 *
 * The Content-ID header is an unique identifier of an entity-body. The
 * Content-ID value may be used for uniquely identifying MIME entities in
 * several contexts, particularly for caching data referenced by the
 * message/external-body mechanism. Its syntax is defined in [RFC2045] as
 * follows:
 *
 * @code
 *      Content-ID   = "Content-ID" ":" msg-id
 *      msg-id       = [CFWS] "<" id-left "@" id-right ">" [CFWS]
 *      id-left      = dot-atom-text / no-fold-quote / obs-id-left
 *      id-right     = dot-atom-text / no-fold-literal / obs-id-right
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
/**@ingroup msg_content_id
 * @typedef msg_generic_t msg_content_id_t;
 * Content-ID Header Structure.
 * @code
 * typedef struct
 * {
 *   msg_common_t      g_common[1];    // Common fragment info
 *   msg_content_id_t *g_next;	       // Link to next header
 *   char const       *g_string;       // Header value
 * }
 * @endcode
 */

Pekka Pessi's avatar
Pekka Pessi committed
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
#define msg_content_id_d msg_generic_d
#define msg_content_id_e msg_generic_e
msg_hclass_t msg_content_id_class[] =
MSG_HEADER_CLASS_G(content_id, "Content-ID", "", single);

/* ====================================================================== */

/**@ingroup msg_mime
 * @defgroup msg_content_type Content-Type Header
 *
 * The @b Content-Type header indicates the media type of the message-body
1778
 * sent to the recipient. Its syntax is defined in [H3.7, S20.15]
Pekka Pessi's avatar
Pekka Pessi committed
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
 * as follows:
 *
 * @code
 *    Content-Type  = ( "Content-Type" | "c" ) ":" media-type
 *    media-type    = type "/" subtype *( ";" parameter )
 *    type          = token
 *    subtype       = token
 * @endcode
 */

/**@ingroup msg_content_type
 * @typedef typedef struct msg_content_type_s msg_content_type_t;
 *
 * The structure msg_content_type_t contains representation of @b
 * Content-Type header.
 *
 * The msg_content_type_t is defined as follows:
 * @code
 * typedef struct msg_content_type_s {
 *   msg_common_t        c_common[1];  // Common fragment info
 *   msg_unknown_t      *c_next;       // Dummy link to next
 *   char const         *c_type;       // Pointer to type/subtype
 *   char const         *c_subtype;    // Points after first slash in type
 *   msg_param_t const  *c_params;     // List of parameters
 * } msg_content_type_t;
 * @endcode
 *
 * The @a c_type is always void of whitespace, that is, there is no
 * whitespace around the slash.
 */

1810 1811
#define msg_content_type_update NULL

Pekka Pessi's avatar
Pekka Pessi committed
1812 1813
msg_hclass_t msg_content_type_class[] =
MSG_HEADER_CLASS(msg_, content_type, "Content-Type", "c", c_params,
1814
		 single, msg_content_type, msg_content_type);
Pekka Pessi's avatar
Pekka Pessi committed
1815

1816
issize_t msg_content_type_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1817 1818 1819 1820 1821
{
  msg_content_type_t *c;

  assert(h);

1822
  c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

  /* "Content-type:" type/subtyp *(; parameter))) */
  if (msg_mediatype_d(&s, &c->c_type) == -1 || /* compacts token / token */
      (c->c_subtype = strchr(c->c_type, '/')) == NULL ||
      (*s == ';' && msg_params_d(home, &s, &c->c_params) == -1) ||
      (*s != '\0'))
    return -1;

  c->c_subtype++;

  return 0;
}

1836
issize_t msg_content_type_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1837 1838
{
  char *b0 = b, *end = b + bsiz;
1839
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849

  assert(msg_is_content_type(h));

  MSG_STRING_E(b, end, c->c_type);
  MSG_PARAMS_E(b, end, c->c_params, flags);
  MSG_TERM_E(b, end);

  return b - b0;
}

1850
isize_t msg_content_type_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1851
{
1852
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1853

1854 1855
  MSG_PARAMS_SIZE(offset, c->c_params);
  offset += MSG_STRING_SIZE(c->c_type);
Pekka Pessi's avatar
Pekka Pessi committed
1856

1857
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1858 1859 1860 1861
}

/** Duplicate one msg_content_type_t object */
char *msg_content_type_dup_one(msg_header_t *dst, msg_header_t const *src,
1862
			       char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1863
{
1864 1865
  msg_content_type_t *c = (msg_content_type_t *)dst;
  msg_content_type_t const *o = (msg_content_type_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1866 1867 1868 1869
  char *end = b + xtra;

  b = msg_params_dup(&c->c_params, o->c_params, b, xtra);
  MSG_STRING_DUP(b, c->