msg_mime.c 59.8 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
 * 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

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
45
#include <errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
46 47
#include <assert.h>

48
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
49 50

#include "msg_internal.h"
51 52
#include "sofia-sip/msg.h"
#include "sofia-sip/msg_mime.h"
Pekka Pessi's avatar
Pekka Pessi committed
53

54
#include <sofia-sip/su_uniqueid.h>
55
#include <sofia-sip/su_errno.h>
Martti Mela's avatar
Martti Mela committed
56

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

size_t memspn(const void *mem, size_t memlen,
	      const void *accept, size_t acceptlen);
size_t memcspn(const void *mem, size_t memlen,
	       const void *reject, size_t rejectlen);

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

70 71
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_mime_protos.h>
Pekka Pessi's avatar
Pekka Pessi committed
72 73 74

/** Define a header class for headers without any extra data to copy */
#define MSG_HEADER_CLASS_G(c, l, s, kind) \
75 76 77
  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
78 79 80

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

83
#define msg_list_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
84 85 86

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

87
/** Calculate length of line ending (0, 1 or 2). @internal */
Pekka Pessi's avatar
Pekka Pessi committed
88 89 90 91 92 93 94 95 96
#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
97
 * message parts. Its syntax is defined in  @RFC2046 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
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
 *
 * @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.
 *
129
 *   body-part := <"message" as defined in @RFC822, with all header fields
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134
 *                 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.>
 *
135
 *   encapsulation := delimiter transport-padding CRLF
Pekka Pessi's avatar
Pekka Pessi committed
136 137 138 139 140 141 142 143 144 145 146
 *                    body-part
 *
 *   close-delimiter := delimiter "--"
 *
 *   delimiter := CRLF dash-boundary
 *
 *   epilogue := discard-text
 *
 * @endcode
 *
 * @par Parsing a Multipart Message
147
 *
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152
 * 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
153
 * home object, a Content-Type header object and message body object as an
Pekka Pessi's avatar
Pekka Pessi committed
154 155
 * argument to msg_multipart_parse() function:
 * @code
156
 *    if (sip->sip_content_type &&
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159 160 161 162
 *        strncasecmp(sip->sip_content_type, "multipart/", 10) == 0) {
 *      msg_multipart_t *mp;
 *
 *      if (sip->sip_multipart)
 *        mp = sip->sip_multipart;
 *      else
163
 *        mp = msg_multipart_parse(msg_home(msg),
Pekka Pessi's avatar
Pekka Pessi committed
164 165
 *                                 sip->sip_content_type,
 *                                 (sip_payload_t *)sip->sip_payload);
166
 *
Pekka Pessi's avatar
Pekka Pessi committed
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
 *      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;
196
 *   size_t len, offset;
197
 *
Pekka Pessi's avatar
Pekka Pessi committed
198 199 200 201
 *   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");
202
 *
Pekka Pessi's avatar
Pekka Pessi committed
203 204 205 206 207 208 209 210 211
 *   // 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
 *
212
 *   // Encode all multipart components
Pekka Pessi's avatar
Pekka Pessi committed
213 214 215 216 217 218
 *   len = msg_multipart_prepare(msg, mp, 0);
 *   if (len < 0)
 *     return -1;		// Error
 *
 *   pl = sip_payload_create(home, NULL, len);
 *
219
 *   // Copy each element from multipart to pl_data
Pekka Pessi's avatar
Pekka Pessi committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
 *   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,
246
				      isize_t dlen)
Pekka Pessi's avatar
Pekka Pessi committed
247 248 249
{
  msg_multipart_t *mp;

250
  mp = (msg_multipart_t *)msg_header_alloc(home, msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
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 291 292 293 294

  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. */
295
static char const bchars[] =
Pekka Pessi's avatar
Pekka Pessi committed
296 297 298 299 300 301 302 303 304 305 306 307
"'()+_,-./:=?"
"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)
{
308 309
  size_t m;
  unsigned crlf;
Pekka Pessi's avatar
Pekka Pessi committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  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 */
    m = 2 + memspn(p + 2, len - 2, bchars, bchars_len);
    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;
    }
334
  }
Pekka Pessi's avatar
Pekka Pessi committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

  /* Look for LF -- */
  for (;(p = memmem(p, end - p, LF "--", 3)); p += 3) {
    len = end - p;
    m = 3 + memspn(p + 3, len - 3, bchars, bchars_len);
    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
363
 * common syntax of multiparts is described in @RFC2046 (section 7).
Pekka Pessi's avatar
Pekka Pessi committed
364
 *
365 366 367
 * @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
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
 *
 * 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) }}};
383
  size_t len, m, blen;
Pekka Pessi's avatar
Pekka Pessi committed
384 385 386 387 388 389 390 391 392 393 394
  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 */
395
  if (c && (param = msg_header_find_param(c->c_common, "boundary=")))
Pekka Pessi's avatar
Pekka Pessi committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    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 (;;) {
421
    while (p[0] == ' ')
Pekka Pessi's avatar
Pekka Pessi committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
      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++;

439
    mp = (msg_multipart_t *)msg_header_alloc(msg_home(msg), msg_multipart_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    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 */
457 458
      mp->mp_close_delim = (msg_payload_t *)
	msg_header_alloc(msg_home(msg), msg_payload_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462 463 464 465 466
      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;
    }

467
    b = next; p = next + m;
Pekka Pessi's avatar
Pekka Pessi committed
468 469 470 471 472 473 474 475 476 477
  }

  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) {
478
    msg->m_object = (msg_pub_t *)mp; p = mp->mp_data; next = p + mp->mp_len;
Pekka Pessi's avatar
Pekka Pessi committed
479 480 481 482 483 484 485 486 487 488 489 490

    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])) {
491
	m = msg_extract_separator(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
492 493 494 495 496
	assert(m > 0);

	p += m; len -= m;

	if (len > 0) {
497
	  m = msg_extract_payload(msg, (msg_pub_t*)mp, NULL, len, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
498 499 500 501 502 503
	  assert(m > 0);
	  assert(len == m);
	}
	break;
      }

504
      m = msg_extract_header(msg, (msg_pub_t*)mp, p, len, 1);
Pekka Pessi's avatar
Pekka Pessi committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518

      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) {
519
    mp->mp_data = boundary;
Pekka Pessi's avatar
Pekka Pessi committed
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 545 546 547 548
    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.
549
 *
550 551 552
 * 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.
553 554 555 556
 *
 * @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
557
 *
558 559
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
560
 *
561
 * @ERRORS
Pekka Pessi's avatar
Pekka Pessi committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575
 * @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;
576
  size_t blen, m;
Pekka Pessi's avatar
Pekka Pessi committed
577 578 579 580

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

581
  if (!(b = msg_header_find_param(c->c_common, "boundary="))) {
Pekka Pessi's avatar
Pekka Pessi committed
582
    /* Generate boundary */
583
    enum { tlen = 16 * 4 / 3 };
Pekka Pessi's avatar
Pekka Pessi committed
584 585 586 587 588 589 590 591 592 593 594 595 596
    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? */
597
      b = su_sprintf(home, "boundary=\"%.*s\"", (int)m, b);
Pekka Pessi's avatar
Pekka Pessi committed
598 599 600
    }
    else {
      strcpy(token, "boundary=");
601
      msg_random_token(token + strlen("boundary="), (size_t)tlen, NULL, 0);
Pekka Pessi's avatar
Pekka Pessi committed
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 634 635 636 637
      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)
638
	mp->mp_close_delim = msg_payload_format(home, "%.*s--" CR LF,
639
						(int)m, boundary);
Pekka Pessi's avatar
Pekka Pessi committed
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 731 732 733 734
      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 */
735
    for (hh = &((msg_pub_t*)mp)->msg_request;
736
	 (char *)hh < (char *)&mp->mp_separator;
Pekka Pessi's avatar
Pekka Pessi committed
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 807 808 809 810
	 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.
 */
811
issize_t msg_multipart_prepare(msg_t *msg, msg_multipart_t *mp, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
812 813 814 815
{
  if (!mp || !mp->mp_data)
    return -1;

816
  if (!mp->mp_common->h_data ||
Pekka Pessi's avatar
Pekka Pessi committed
817 818 819 820 821 822 823 824 825 826
      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. */
827
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
828 829 830 831 832 833 834 835
{
  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);
836

837
  result = (msg_multipart_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
838

839
  pl->pl_data = s;
Pekka Pessi's avatar
Pekka Pessi committed
840 841 842 843 844 845 846 847
  pl->pl_len = slen;

  mp = msg_multipart_parse(tmphome, NULL, pl);

  if (mp) {
    *result = *mp;

    if (result->mp_common->h_succ->sh_prev)
848
      result->mp_common->h_succ->sh_prev =
Pekka Pessi's avatar
Pekka Pessi committed
849 850 851
	&result->mp_common->h_succ;

    su_free(tmphome, mp);
852

Pekka Pessi's avatar
Pekka Pessi committed
853 854
    su_home_move(home, tmphome);
  }
855

Pekka Pessi's avatar
Pekka Pessi committed
856 857 858 859 860 861 862 863 864 865
  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.
 */
866
issize_t msg_multipart_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
867 868 869 870 871
{
  return msg_payload_e(b, bsiz, h, flags);
}

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

  offset = msg_payload_dup_xtra(h, offset);

879
  for (hh = (msg_header_t const **)&((msg_pub_t *)mp)->msg_request;
880
       (char *)hh <= (char *)&mp->mp_close_delim;
Pekka Pessi's avatar
Pekka Pessi committed
881 882 883 884 885 886 887 888 889 890 891 892
       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,
893
			    char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
894
{
895
  msg_multipart_t const *mp = (msg_multipart_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
896 897 898 899 900
  msg_header_t *h, **hh;
  char *end = b + xtra;

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

901
  for (hh = &((msg_pub_t*)mp)->msg_request;
902
       (char *)hh <= (char *)&mp->mp_close_delim;
Pekka Pessi's avatar
Pekka Pessi committed
903 904 905 906 907 908 909
       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);
910 911
      if (h->sh_class->hc_update)
	msg_header_update_params(h->sh_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
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 971 972 973 974
      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.
 *
975 976
 * @param[in,out] ss    string to be parsed
 * @param[out]    type  value result for media type
Pekka Pessi's avatar
Pekka Pessi committed
977 978 979 980
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
981
issize_t msg_mediatype_d(char **ss, char const **type)
Pekka Pessi's avatar
Pekka Pessi committed
982 983 984
{
  char *s = *ss;
  char const *result = s;
985
  size_t l1 = 0, l2 = 0, n;
Pekka Pessi's avatar
Pekka Pessi committed
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 1058 1059 1060 1061

  /* 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[] =
1062
MSG_HEADER_CLASS(msg_, accept, "Accept", "", ac_params, apndlist,
1063
		 msg_accept, msg_accept);
Pekka Pessi's avatar
Pekka Pessi committed
1064

1065
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
1066
{
1067
  msg_accept_t *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1068

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

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

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
  /* "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
1089 1090
}

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

  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;
}

1107
isize_t msg_accept_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1108
{
1109
  msg_accept_t const *ac = (msg_accept_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1110 1111

  if (ac->ac_type) {
1112 1113
    MSG_PARAMS_SIZE(offset, ac->ac_params);
    offset += MSG_STRING_SIZE(ac->ac_type);
Pekka Pessi's avatar
Pekka Pessi committed
1114 1115
  }

1116
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1117 1118 1119 1120
}

/** Duplicate one msg_accept_t object */
char *msg_accept_dup_one(msg_header_t *dst, msg_header_t const *src,
1121
		      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1122
{
1123 1124
  msg_accept_t *ac = (msg_accept_t *)dst;
  msg_accept_t const *o = (msg_accept_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1125 1126 1127 1128 1129 1130 1131 1132 1133
  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++;
  }

1134
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1135 1136 1137 1138

  return b;
}

1139 1140
/** Update parameter(s) for Accept header. */
int msg_accept_update(msg_common_t *h,
1141
		      char const *name, isize_t namelen,
1142
		      char const *value)
Pekka Pessi's avatar
Pekka Pessi committed
1143
{
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  msg_accept_t *ac = (msg_accept_t *)h;

  if (name == NULL) {
    ac->ac_q = NULL;
  }
  else if (namelen == 1 && strncasecmp(name, "q", 1) == 0) {
    /* XXX - check for invalid value? */
    ac->ac_q = value;
  }

  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1155 1156 1157 1158 1159
}

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

/** Decode an Accept-* header. */
1160 1161 1162
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
1163
{
1164
  /** @relatesalso msg_accept_any_s */
1165
  msg_accept_any_t *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1166

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

1170 1171
  if (*s == '\0')
    return -2;			/* Empty list */
1172

1173 1174 1175
  /* "Accept-*:" 1#(token *(SEMI accept-param)) */
  if (msg_token_d(&s, &aa->aa_value) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1176

1177 1178
  if (*s == ';' && msg_params_d(home, &s, &aa->aa_params) == -1)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1179

1180
  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1181 1182
}

1183
/** Encode an Accept-* header field. */
1184
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
1185
{
1186
  /** @relatesalso msg_accept_any_s */
Pekka Pessi's avatar
Pekka Pessi committed
1187
  char *b0 = b, *end = b + bsiz;
1188
  msg_accept_any_t const *aa = (msg_accept_any_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1189 1190

  MSG_STRING_E(b, end, aa->aa_value);
1191
  MSG_PARAMS_E(b, end, aa->aa_params, flags);
Pekka Pessi's avatar
Pekka Pessi committed
1192 1193 1194 1195 1196 1197
  MSG_TERM_E(b, end);

  return b - b0;
}

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

1203 1204
  MSG_PARAMS_SIZE(offset, aa->aa_params);
  offset += MSG_STRING_SIZE(aa->aa_value);
Pekka Pessi's avatar
Pekka Pessi committed
1205

1206
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1207 1208 1209 1210
}

/** Duplicate one msg_accept_any_t object. */
char *msg_accept_any_dup_one(msg_header_t *dst, msg_header_t const *src,
1211
			     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1212
{
1213
  /** @relatesalso msg_accept_any_s */
1214 1215
  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
1216 1217
  char *end = b + xtra;

1218
  b = msg_params_dup(&aa->aa_params, o->aa_params, b, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1219 1220
  MSG_STRING_DUP(b, aa->aa_value, o->aa_value);

1221
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1222 1223 1224 1225

  return b;
}

1226 1227
/** Update parameter(s) for Accept-* header. */
int msg_accept_any_update(msg_common_t *h,
1228
			  char const *name, isize_t namelen,
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			  char const *value)
{
  msg_accept_any_t *aa = (msg_accept_any_t *)h;

  if (name == NULL) {
    aa->aa_q = NULL;
  }
  else if (namelen == 1 && strncasecmp(name, "q", 1) == 0) {
    aa->aa_q = value;
  }

  return 0;
}

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

/**@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
1271
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1272 1273 1274 1275 1276 1277 1278
 *   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", "",
1279
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1280

1281
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
1282 1283 1284 1285
{
  return msg_accept_any_d(home, h, s, slen);
}

1286
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
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
{
  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
1299
 * defined in [H14.3, S20.2] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
 *
 * @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
1322
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1323 1324 1325 1326 1327 1328 1329
 *   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", "",
1330
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1331

1332
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
1333 1334 1335 1336
{
  return msg_accept_any_d(home, h, s, slen);
}

1337
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
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
  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
1350
 * defined in [H14.4, S20.3] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
 *
 * @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
1373
 *   msg_param_t const  *aa_params;    // Parameter list
Pekka Pessi's avatar
Pekka Pessi committed
1374 1375 1376 1377 1378 1379 1380
 *   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", "",
1381
		  aa_params, apndlist, msg_accept_any, msg_accept_any);
Pekka Pessi's avatar
Pekka Pessi committed
1382

1383
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
1384 1385 1386 1387
{
  return msg_accept_any_d(home, h, s, slen);
}

1388
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
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
{
  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
1402 1403
 * interpreted by the UAC or UAS.  Its syntax is defined in [S20.11]
 * as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
 *
 * @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 )
1423 1424 1425
 *    disposition-type        /=  "script" | "sip-cgi" | token
 *    disposition-param       /=  action-param
 *                             /  modification-date-param
Pekka Pessi's avatar
Pekka Pessi committed
1426 1427 1428 1429 1430 1431 1432
 *    action-param             =  "action" "=" action-value
 *    action-value             =  "store" | "remove" | token
 *    modification-date-param  =  "modification-date" "=" quoted-date-time
 *    quoted-date-time         =  <"> SIP-date <">
 * @endcode
 */

1433
/**@ingroup msg_content_disposition
1434
 * @typedef struct msg_content_disposition_s msg_content_disposition_t;
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
 *
 * 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
1454 1455
msg_hclass_t msg_content_disposition_class[] =
MSG_HEADER_CLASS(msg_, content_disposition, "Content-Disposition", "",
1456 1457
		 cd_params, single, msg_content_disposition,
		 msg_content_disposition);
Pekka Pessi's avatar
Pekka Pessi committed
1458

1459
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
1460
{
1461
  msg_content_disposition_t *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1462 1463 1464 1465 1466 1467

  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)
1468
    msg_header_update_params(cd->cd_common, 0);
Pekka Pessi's avatar
Pekka Pessi committed
1469 1470 1471 1472

  return 0;
}

1473
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
1474 1475
{
  char *b0 = b, *end = b + bsiz;
1476
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

  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;
}

1488
isize_t msg_content_disposition_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1489
{
1490
  msg_content_disposition_t const *cd = (msg_content_disposition_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1491

1492 1493
  MSG_PARAMS_SIZE(offset, cd->cd_params);
  offset += MSG_STRING_SIZE(cd->cd_type);
Pekka Pessi's avatar
Pekka Pessi committed
1494

1495
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1496 1497 1498 1499 1500
}

/** Duplicate one msg_content_disposition_t object */
char *msg_content_disposition_dup_one(msg_header_t *dst,
				     msg_header_t const *src,
1501
				     char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1502
{
1503 1504
  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
1505 1506 1507 1508 1509
  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);

1510
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1511 1512 1513 1514

  return b;
}

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

1522 1523 1524 1525 1526 1527 1528 1529 1530
  if (name == NULL) {
    cd->cd_handling = NULL, cd->cd_required = 0, cd->cd_optional = 0;
  }
  else if (namelen == strlen("handling") &&
	   strncasecmp(name, "handling", namelen) == 0) {
    cd->cd_handling = value;
    cd->cd_required = strcasecmp(value, "required") == 0;
    cd->cd_optional = strcasecmp(value, "optional") == 0;
  }
Pekka Pessi's avatar
Pekka Pessi committed
1531

1532
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1533 1534 1535 1536 1537 1538 1539 1540
}

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

/**@ingroup msg_mime
 * @defgroup msg_content_encoding Content-Encoding Header
 *
 * The Content-Encoding header indicates what additional content codings
1541 1542
 * 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
1543 1544
 *
 * @code
1545
 *    Content-Encoding = ( "Content-Encoding" / "e" ) ":" 1#content-coding
Pekka Pessi's avatar
Pekka Pessi committed
1546 1547 1548 1549
 *    content-coding   = token
 * @endcode
 */

1550
/**@ingroup msg_content_encoding
1551
 * @typedef struct msg_list_s msg_content_encoding_t;
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
 *
 * 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
1567
msg_hclass_t msg_content_encoding_class[] =
1568
  MSG_HEADER_CLASS_LIST(content_encoding, "Content-Encoding", "e", list);
Pekka Pessi's avatar
Pekka Pessi committed
1569

1570
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
1571
{
1572
  msg_content_encoding_t *e = (msg_content_encoding_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1573 1574 1575
  return msg_commalist_d(home, &s, &e->k_items, msg_token_scan);
}

1576
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
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 1621 1622 1623 1624
{
  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);

1625
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
1626
{
1627
  msg_content_language_t *k = (msg_content_language_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1628 1629 1630
  return msg_commalist_d(home, &s, &k->k_items, msg_token_scan);
}

1631
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
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 1669 1670 1671 1672
{
  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",
1673
		 l_common, single_critical, msg_default, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691

/**@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)
{
1692 1693
  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
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708

  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
1709
 * message-body. Its syntax is defined in [@RFC1864, H14.15] as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1710 1711 1712
 *
 * @code
 *      Content-MD5   = "Content-MD5" ":" md5-digest
1713 1714 1715 1716 1717
 *      md5-digest   = <base64 of 128 bit MD5 digest as per @RFC1864>
 * @endcode
 */

/**@ingroup msg_content_md5
1718
 * @typedef struct msg_generic_s msg_content_md5_t;
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
 *
 * 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
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
 * @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
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
/**@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
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
#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
1782
 * sent to the recipient. Its syntax is defined in [H3.7, S20.15]
Pekka Pessi's avatar
Pekka Pessi committed
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 1810 1811 1812 1813
 * 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.
 */

1814 1815
#define msg_content_type_update NULL

Pekka Pessi's avatar
Pekka Pessi committed
1816 1817
msg_hclass_t msg_content_type_class[] =
MSG_HEADER_CLASS(msg_, content_type, "Content-Type", "c", c_params,
1818
		 single, msg_content_type, msg_content_type);
Pekka Pessi's avatar
Pekka Pessi committed
1819

1820
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
1821 1822 1823 1824 1825
{
  msg_content_type_t *c;

  assert(h);

1826
  c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839

  /* "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;
}

1840
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
1841 1842
{
  char *b0 = b, *end = b + bsiz;
1843
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853

  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;
}

1854
isize_t msg_content_type_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1855
{
1856
  msg_content_type_t const *c = (msg_content_type_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1857

1858 1859
  MSG_PARAMS_SIZE(offset, c->c_params);
  offset += MSG_STRING_SIZE(c->c_type);
Pekka Pessi's avatar
Pekka Pessi committed
1860

1861
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1862 1863 1864 1865
}

/** Duplicate one msg_content_type_t object */
char *msg_content_type_dup_one(msg_header_t *dst, msg_header_t const *src,
1866
			       char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1867
{
1868 1869
  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
1870 1871 1872 1873
  char *end = b + xtra;

  b = msg_params_dup(&c->c_params, o->c_params, b, xtra);
  MSG_STRING_DUP(b, c->c_type, o->c_type);
1874 1875 1876 1877

  c->c_subtype = c->c_type ? strchr(c->c_type, '/') : NULL;
  if (c->c_subtype)
    c->c_subtype++;
Pekka Pessi's avatar
Pekka Pessi committed
1878

1879
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

  return b;
}

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

/**@ingroup msg_mime
 * @defgroup msg_mime_version MIME-Version Header
 *
 * MIME-Version header indicates what version of the protocol was used
1890 1891
 * to construct the message.  Its syntax is defined in [H19.4.1, S20.24]
 * as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1892 1893 1894 1895 1896 1897