osip_message_to_str.c 21.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 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 634 635 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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
/*
  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com

  This library is free software; you can redistribute it and/or
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <osipparser2/internal.h>

#include <osipparser2/osip_port.h>
#include <osipparser2/osip_parser.h>

#define MIME_MAX_BOUNDARY_LEN 70

extern const char *osip_protocol_version;

static int strcat_simple_header(char **_string, size_t *malloc_size, char **_message, void *ptr_header, char *header_name, size_t size_of_header, int (*xxx_to_str)(void *, char **), char **next);
static int strcat_headers_one_per_line(char **_string, size_t *malloc_size, char **_message, osip_list_t *headers, char *header, size_t size_of_header, int (*xxx_to_str)(void *, char **), char **next);

static int __osip_message_startline_to_strreq(osip_message_t *sip, char **dest) {
  const char *sip_version;
  char *tmp;
  char *rquri;
  int i;

  *dest = NULL;

  if ((sip == NULL) || (sip->req_uri == NULL) || (sip->sip_method == NULL))
    return OSIP_BADPARAMETER;

  i = osip_uri_to_str(sip->req_uri, &rquri);

  if (i != 0)
    return i;

  if (sip->sip_version == NULL)
    sip_version = osip_protocol_version;

  else
    sip_version = sip->sip_version;

  *dest = (char *) osip_malloc(strlen(sip->sip_method) + strlen(rquri) + strlen(sip_version) + 3);

  if (*dest == NULL) {
    osip_free(rquri);
    return OSIP_NOMEM;
  }

  tmp = *dest;

  tmp = osip_str_append(tmp, sip->sip_method);
  *tmp = ' ';
  tmp++;
  tmp = osip_str_append(tmp, rquri);
  *tmp = ' ';
  tmp++;
  strcpy(tmp, sip_version);

  osip_free(rquri);
  return OSIP_SUCCESS;
}

static int __osip_message_startline_to_strresp(osip_message_t *sip, char **dest) {
  char *tmp;
  const char *sip_version;
  char status_code[5];

  *dest = NULL;

  if ((sip == NULL) || (sip->reason_phrase == NULL) || (sip->status_code < 100) || (sip->status_code > 699))
    return OSIP_BADPARAMETER;

  if (sip->sip_version == NULL)
    sip_version = osip_protocol_version;

  else
    sip_version = sip->sip_version;

  sprintf(status_code, "%u", sip->status_code);

  *dest = (char *) osip_malloc(strlen(sip_version) + 3 + strlen(sip->reason_phrase) + 4);

  if (*dest == NULL)
    return OSIP_NOMEM;

  tmp = *dest;

  tmp = osip_str_append(tmp, sip_version);
  *tmp = ' ';
  tmp++;

  tmp = osip_strn_append(tmp, status_code, 3);
  *tmp = ' ';
  tmp++;
  strcpy(tmp, sip->reason_phrase);

  return OSIP_SUCCESS;
}

static int __osip_message_startline_to_str(osip_message_t *sip, char **dest) {
  if (sip->sip_method != NULL)
    return __osip_message_startline_to_strreq(sip, dest);

  if (sip->status_code != 0)
    return __osip_message_startline_to_strresp(sip, dest);

  OSIP_TRACE(osip_trace(__FILE__, __LINE__, TRACE_LEVEL1, NULL, "ERROR method has no value or status code is 0!\n"));
  return OSIP_BADPARAMETER; /* should never come here */
}

char *osip_message_get_reason_phrase(const osip_message_t *sip) {
  return sip->reason_phrase;
}

int osip_message_get_status_code(const osip_message_t *sip) {
  return sip->status_code;
}

char *osip_message_get_method(const osip_message_t *sip) {
  return sip->sip_method;
}

char *osip_message_get_version(const osip_message_t *sip) {
  return sip->sip_version;
}

osip_uri_t *osip_message_get_uri(const osip_message_t *sip) {
  return sip->req_uri;
}

static int strcat_simple_header(char **_string, size_t *malloc_size, char **_message, void *ptr_header, char *header_name, size_t size_of_header, int (*xxx_to_str)(void *, char **), char **next) {
  char *string;
  char *message;
  char *tmp;
  int i;

  string = *_string;
  message = *_message;

  if (ptr_header != NULL) {
    if (*malloc_size < message - string + 100 + size_of_header) {
      /* take some memory in order to avoid using osip_realloc too often */
      size_t size = message - string;

      *malloc_size = message - string + size_of_header + 100;
      string = osip_realloc(string, *malloc_size);

      if (string == NULL) {
        osip_free(*_string); /* pointer for string */
        *_string = NULL;
        *_message = NULL;
        return OSIP_NOMEM;
      }

      *_string = string;
      message = string + size;
    }

    message = osip_strn_append(message, header_name, size_of_header);

    i = xxx_to_str(ptr_header, &tmp);

    if (i != 0) {
      *_string = string;
      *_message = message;
      *next = NULL;
      return i;
    }

    if (*malloc_size < message - string + strlen(tmp) + 100) {
      size_t size = message - string;

      *malloc_size = message - string + strlen(tmp) + 100;
      string = osip_realloc(string, *malloc_size);

      if (string == NULL) {
        osip_free(*_string); /* pointer for string */
        *_string = NULL;
        *_message = NULL;
        return OSIP_NOMEM;
      }

      *_string = string;
      message = string + size;
    }

    message = osip_str_append(message, tmp);
    osip_free(tmp);
    message = osip_strn_append(message, OSIP_CRLF, 2);
  }

  *_string = string;
  *_message = message;
  *next = message;
  return OSIP_SUCCESS;
}

static int strcat_headers_one_per_line(char **_string, size_t *malloc_size, char **_message, osip_list_t *headers, char *header, size_t size_of_header, int (*xxx_to_str)(void *, char **), char **next) {
  char *string;
  char *message;
  char *tmp;
  int i;
  osip_list_iterator_t it;
  void *elt = osip_list_get_first(headers, &it);

  string = *_string;
  message = *_message;

  while (elt != OSIP_SUCCESS) {
    if (*malloc_size < message - string + 100 + size_of_header)
    /* take some memory avoid to osip_realloc too much often */
    {
      /* should not happen often */
      size_t size = message - string;

      *malloc_size = message - string + size_of_header + 100;
      string = osip_realloc(string, *malloc_size);

      if (string == NULL) {
        osip_free(*_string); /* pointer for string */
        *_string = NULL;
        *_message = NULL;
        return OSIP_NOMEM;
      }

      *_string = string;
      message = string + size;
    }

    osip_strncpy(message, header, size_of_header);
    i = xxx_to_str(elt, &tmp);

    if (i != 0) {
      *_string = string;
      *_message = message;
      *next = NULL;
      return i;
    }

    message = message + strlen(message);

    if (*malloc_size < message - string + strlen(tmp) + 100) {
      size_t size = message - string;

      *malloc_size = message - string + strlen(tmp) + 100;
      string = osip_realloc(string, *malloc_size);

      if (string == NULL) {
        osip_free(*_string); /* pointer for string */
        *_string = NULL;
        *_message = NULL;
        return OSIP_NOMEM;
      }

      *_string = string;
      message = string + size;
    }

    message = osip_str_append(message, tmp);
    osip_free(tmp);
    message = osip_strn_append(message, OSIP_CRLF, 2);
    elt = osip_list_get_next(&it);
  }

  *_string = string;
  *_message = message;
  *next = message;
  return OSIP_SUCCESS;
}

/* return values:
   1: structure and buffer "message" are identical.
   2: buffer "message" is not up to date with the structure info (call osip_message_to_str to update it).
   -1 on error.
 */
int osip_message_get__property(const osip_message_t *sip) {
  if (sip == NULL)
    return OSIP_BADPARAMETER;

  return sip->message_property;
}

int osip_message_force_update(osip_message_t *sip) {
  if (sip == NULL)
    return OSIP_BADPARAMETER;

  sip->message_property = 2;
  return OSIP_SUCCESS;
}

static int _osip_message_realloc(char **message, char **dest, size_t needed, size_t *malloc_size) {
  size_t size = *message - *dest;

  if (*malloc_size < (size_t)(size + needed + 100)) {
    *malloc_size = size + needed + 100;
    *dest = osip_realloc(*dest, *malloc_size);

    if (*dest == NULL)
      return OSIP_NOMEM;

    *message = *dest + size;
  }

  return OSIP_SUCCESS;
}

static int _osip_message_to_str(osip_message_t *sip, char **dest, size_t *message_length, int sipfrag) {
  size_t malloc_size;
  size_t total_length = 0;

  /* Added at SIPit day1 */
  char *start_of_bodies;
  char *content_length_to_modify = NULL;

  char *message;
  char *next;
  char *tmp;
  int pos;
  int i;
  char *boundary = NULL;

  malloc_size = SIP_MESSAGE_MAX_LENGTH;

  *dest = NULL;

  if (sip == NULL)
    return OSIP_BADPARAMETER;

  {
    if (1 == osip_message_get__property(sip)) { /* message is already available in "message" */

      *dest = osip_malloc(sip->message_length + 1);

      if (*dest == NULL)
        return OSIP_NOMEM;

      memcpy(*dest, sip->message, sip->message_length);
      (*dest)[sip->message_length] = '\0';

      if (message_length != NULL)
        *message_length = sip->message_length;

      return OSIP_SUCCESS;

    } else {
      /* message should be rebuilt: delete the old one if exists. */
      osip_free(sip->message);
      sip->message = NULL;
    }
  }

  message = (char *) osip_malloc(SIP_MESSAGE_MAX_LENGTH); /* ???? message could be > 4000  */

  if (message == NULL)
    return OSIP_NOMEM;

  *dest = message;

  /* add the first line of message */
  i = __osip_message_startline_to_str(sip, &tmp);

  if (i != 0) {
    if (!sipfrag) {
      osip_free(*dest);
      *dest = NULL;
      return i;
    }

    /* A start-line isn't required for message/sipfrag parts. */

  } else {
    size_t message_len = strlen(tmp);

    if (_osip_message_realloc(&message, dest, message_len + 3, &malloc_size) < 0) {
      osip_free(tmp);
      *dest = NULL;
      return OSIP_NOMEM;
    }

    message = osip_str_append(message, tmp);
    osip_free(tmp);
    message = osip_strn_append(message, OSIP_CRLF, 2);
  }

  {
    struct to_str_table {
      char header_name[30];
      int header_length;
      osip_list_t *header_list;
      void *header_data;
      int (*to_str)(void *, char **);
    }
#ifndef MINISIZE
    table[25] =
#else
    table[16] =
#endif
        {{"Via: ", 5, NULL, NULL, (int (*)(void *, char **)) & osip_via_to_str},
         {"Record-Route: ", 14, NULL, NULL, (int (*)(void *, char **)) & osip_record_route_to_str},
         {"Route: ", 7, NULL, NULL, (int (*)(void *, char **)) & osip_route_to_str},
         {"From: ", 6, NULL, NULL, (int (*)(void *, char **)) & osip_from_to_str},
         {"To: ", 4, NULL, NULL, (int (*)(void *, char **)) & osip_to_to_str},
         {"Call-ID: ", 9, NULL, NULL, (int (*)(void *, char **)) & osip_call_id_to_str},
         {"CSeq: ", 6, NULL, NULL, (int (*)(void *, char **)) & osip_cseq_to_str},
         {"Contact: ", 9, NULL, NULL, (int (*)(void *, char **)) & osip_contact_to_str},
         {"Authorization: ", 15, NULL, NULL, (int (*)(void *, char **)) & osip_authorization_to_str},
         {"WWW-Authenticate: ", 18, NULL, NULL, (int (*)(void *, char **)) & osip_www_authenticate_to_str},
         {"Proxy-Authenticate: ", 20, NULL, NULL, (int (*)(void *, char **)) & osip_www_authenticate_to_str},
         {"Proxy-Authorization: ", 21, NULL, NULL, (int (*)(void *, char **)) & osip_authorization_to_str},
         {"Call-Info: ", 11, NULL, NULL, (int (*)(void *, char **)) & osip_call_info_to_str},
         {"Content-Type: ", 14, NULL, NULL, (int (*)(void *, char **)) & osip_content_type_to_str},
         {"Mime-Version: ", 14, NULL, NULL, (int (*)(void *, char **)) & osip_content_length_to_str},
#ifndef MINISIZE
         {"Allow: ", 7, NULL, NULL, (int (*)(void *, char **)) & osip_allow_to_str},
         {"Content-Encoding: ", 18, NULL, NULL, (int (*)(void *, char **)) & osip_content_encoding_to_str},
         {"Alert-Info: ", 12, NULL, NULL, (int (*)(void *, char **)) & osip_call_info_to_str},
         {"Error-Info: ", 12, NULL, NULL, (int (*)(void *, char **)) & osip_call_info_to_str},
         {"Accept: ", 8, NULL, NULL, (int (*)(void *, char **)) & osip_accept_to_str},
         {"Accept-Encoding: ", 17, NULL, NULL, (int (*)(void *, char **)) & osip_accept_encoding_to_str},
         {"Accept-Language: ", 17, NULL, NULL, (int (*)(void *, char **)) & osip_accept_language_to_str},
         {"Authentication-Info: ", 21, NULL, NULL, (int (*)(void *, char **)) & osip_authentication_info_to_str},
         {"Proxy-Authentication-Info: ", 27, NULL, NULL, (int (*)(void *, char **)) & osip_authentication_info_to_str},
#endif
         {{'\0'}, 0, NULL, NULL, NULL}};
    table[0].header_list = &sip->vias;
    table[1].header_list = &sip->record_routes;
    table[2].header_list = &sip->routes;
    table[3].header_data = sip->from;
    table[4].header_data = sip->to;
    table[5].header_data = sip->call_id;
    table[6].header_data = sip->cseq;
    table[7].header_list = &sip->contacts;
    table[8].header_list = &sip->authorizations;
    table[9].header_list = &sip->www_authenticates;
    table[10].header_list = &sip->proxy_authenticates;
    table[11].header_list = &sip->proxy_authorizations;
    table[12].header_list = &sip->call_infos;
    table[13].header_data = sip->content_type;
    table[14].header_data = sip->mime_version;
#ifndef MINISIZE
    table[15].header_list = &sip->allows;
    table[16].header_list = &sip->content_encodings;
    table[17].header_list = &sip->alert_infos;
    table[18].header_list = &sip->error_infos;
    table[19].header_list = &sip->accepts;
    table[20].header_list = &sip->accept_encodings;
    table[21].header_list = &sip->accept_languages;
    table[22].header_list = &sip->authentication_infos;
    table[23].header_list = &sip->proxy_authentication_infos;
#endif

    pos = 0;

    while (table[pos].header_name[0] != '\0') {
      if (table[13].header_list == NULL)
        i = strcat_simple_header(dest, &malloc_size, &message, table[pos].header_data, table[pos].header_name, table[pos].header_length, ((int (*)(void *, char **)) table[pos].to_str), &next);

      i = strcat_headers_one_per_line(dest, &malloc_size, &message, table[pos].header_list, table[pos].header_name, table[pos].header_length, ((int (*)(void *, char **)) table[pos].to_str), &next);

      if (i != 0) {
        osip_free(*dest);
        *dest = NULL;
        return i;
      }

      message = next;

      pos++;
    }
  }

  {
    osip_list_iterator_t it;
    osip_header_t *header = (osip_header_t *) osip_list_get_first(&sip->headers, &it);

    while (header != OSIP_SUCCESS) {
      size_t header_len = 0;

      i = osip_header_to_str(header, &tmp);

      if (i != 0) {
        osip_free(*dest);
        *dest = NULL;
        return i;
      }

      header_len = strlen(tmp);

      if (_osip_message_realloc(&message, dest, header_len + 3, &malloc_size) < 0) {
        osip_free(tmp);
        *dest = NULL;
        return OSIP_NOMEM;
      }

      message = osip_str_append(message, tmp);
      osip_free(tmp);
      message = osip_strn_append(message, OSIP_CRLF, 2);

      header = (osip_header_t *) osip_list_get_next(&it);
    }
  }

  /* we have to create the body before adding the contentlength */
  /* add enough lenght for "Content-Length: " */

  if (_osip_message_realloc(&message, dest, 16, &malloc_size) < 0)
    return OSIP_NOMEM;

  if (sipfrag && osip_list_eol(&sip->bodies, 0)) {
    /* end of headers */
    osip_strncpy(message, OSIP_CRLF, 2);
    message = message + 2;

    /* same remark as at the beginning of the method */
    sip->message_property = 1;
    sip->message = osip_strdup(*dest);
    sip->message_length = message - *dest;

    if (message_length != NULL)
      *message_length = message - *dest;

    return OSIP_SUCCESS; /* it's all done */
  }

  osip_strncpy(message, "Content-Length: ", 16);
  message = message + 16;

  /* SIPit Day1
     ALWAYS RECALCULATE?
     if (sip->contentlength!=NULL)
     {
     i = osip_content_length_to_str(sip->contentlength, &tmp);
     if (i!=0) {
     osip_free(*dest);
     *dest = NULL;
     return i;
     }
     osip_strncpy(message,tmp,strlen(tmp));
     osip_free(tmp);
     }
     else
     { */
  if (osip_list_eol(&sip->bodies, 0)) /* no body */
    message = osip_strn_append(message, "0", 1);

  else {
    /* BUG: p130 (rfc2543bis-04)
       "No SP after last token or quoted string"

       In fact, if extra spaces exist: the stack can't be used
       to make user-agent that wants to make authentication...
       This should be changed...
     */

    content_length_to_modify = message;
    message = osip_str_append(message, "     ");
  }

  /*  } */

  message = osip_strn_append(message, OSIP_CRLF, 2);

  /* end of headers */
  message = osip_strn_append(message, OSIP_CRLF, 2);

  start_of_bodies = message;
  total_length = start_of_bodies - *dest;

  if (osip_list_eol(&sip->bodies, 0)) {
    /* same remark as at the beginning of the method */
    sip->message_property = 1;
    sip->message = osip_strdup(*dest);
    sip->message_length = total_length;

    if (message_length != NULL)
      *message_length = total_length;

    return OSIP_SUCCESS; /* it's all done */
  }

  if (sip->content_type && sip->content_type->type && !osip_strcasecmp(sip->content_type->type, "multipart")) {
    osip_generic_param_t *ct_param = NULL;

    /* find the boundary */
    i = osip_generic_param_get_byname(&sip->content_type->gen_params, "boundary", &ct_param);

    if ((i >= 0) && ct_param && ct_param->gvalue) {
      size_t len = strlen(ct_param->gvalue);

      if (len > MIME_MAX_BOUNDARY_LEN) {
        osip_free(*dest);
        *dest = NULL;
        return OSIP_SYNTAXERROR;
      }

      if (len == 1 && ct_param->gvalue[0] == '"') { /* fixed Jan 10,2020: avoid a negative length copy if boundary contains only one quote */
        osip_free(*dest);
        *dest = NULL;
        return OSIP_SYNTAXERROR;
      }

      if (len == 2 && ct_param->gvalue[0] == '"' && ct_param->gvalue[1] == '"') { /* do not accept empty boundary */
        osip_free(*dest);
        *dest = NULL;
        return OSIP_SYNTAXERROR;
      }

      boundary = osip_malloc(len + 5);

      if (boundary == NULL) {
        osip_free(*dest);
        *dest = NULL;
        return OSIP_NOMEM;
      }

      osip_strncpy(boundary, OSIP_CRLF, 2);
      osip_strncpy(boundary + 2, "--", 2);

      if (ct_param->gvalue[0] == '"' && ct_param->gvalue[len - 1] == '"')
        osip_strncpy(boundary + 4, ct_param->gvalue + 1, len - 2);

      else
        osip_strncpy(boundary + 4, ct_param->gvalue, len);
    }
  }

  {
    osip_list_iterator_t it;
    osip_body_t *body = (osip_body_t *) osip_list_get_first(&sip->bodies, &it);

    while (body != OSIP_SUCCESS) {
      size_t body_length;

      if (boundary) {
        /* Needs at most 77 bytes,
           last realloc allocate at least 100 bytes extra */
        message = osip_str_append(message, boundary);
        message = osip_strn_append(message, OSIP_CRLF, 2);
      }

      i = osip_body_to_str(body, &tmp, &body_length);

      if (i != 0) {
        osip_free(*dest);
        *dest = NULL;

        if (boundary)
          osip_free(boundary);

        return i;
      }

      if (malloc_size < message - *dest + 100 + body_length) {
        size_t size = message - *dest;
        int offset_of_body;
        int offset_content_length_to_modify = 0;

        offset_of_body = (int) (start_of_bodies - *dest);

        if (content_length_to_modify != NULL)
          offset_content_length_to_modify = (int) (content_length_to_modify - *dest);

        malloc_size = message - *dest + body_length + 100;
        *dest = osip_realloc(*dest, malloc_size);

        if (*dest == NULL) {
          osip_free(tmp); /* fixed 09/Jun/2005 */

          if (boundary)
            osip_free(boundary);

          return OSIP_NOMEM;
        }

        start_of_bodies = *dest + offset_of_body;

        if (content_length_to_modify != NULL)
          content_length_to_modify = *dest + offset_content_length_to_modify;

        message = *dest + size;
      }

      memcpy(message, tmp, body_length);
      message[body_length] = '\0';
      osip_free(tmp);
      message = message + body_length;

      body = (osip_body_t *) osip_list_get_next(&it);
    }
  }

  if (boundary) {
    /* Needs at most 79 bytes,
       last realloc allocate at least 100 bytes extra */
    message = osip_str_append(message, boundary);
    message = osip_strn_append(message, "--", 2);
    message = osip_strn_append(message, OSIP_CRLF, 2);

    osip_free(boundary);
    boundary = NULL;
  }

  if (content_length_to_modify == NULL) {
    osip_free(*dest);
    *dest = NULL;
    return OSIP_SYNTAXERROR;
  }

  /* we NOW have the length of bodies: */
  {
    size_t size = message - start_of_bodies;
    char tmp2[15];

    total_length += size;
    snprintf(tmp2, 15, "%i", (int) size);
    /* do not use osip_strncpy here! */
    memcpy(content_length_to_modify + 5 - strlen(tmp2), tmp2, strlen(tmp2));
  }

  /* same remark as at the beginning of the method */
  sip->message_property = 1;
  sip->message = osip_malloc(total_length + 1);

  if (sip->message != NULL) {
    memcpy(sip->message, *dest, total_length);
    sip->message[total_length] = '\0';
    sip->message_length = total_length;

    if (message_length != NULL)
      *message_length = total_length;
  }

  return OSIP_SUCCESS;
}

int osip_message_to_str(osip_message_t *sip, char **dest, size_t *message_length) {
  return _osip_message_to_str(sip, dest, message_length, 0);
}

int osip_message_to_str_sipfrag(osip_message_t *sip, char **dest, size_t *message_length) {
  return _osip_message_to_str(sip, dest, message_length, 1);
}