nsCString buf("");
nsCString buf;
mime_generate_attachment_headers (const char *type,                  const char *type_param,                  const char *encoding,                  const char *description,                  const char *x_mac_type,                  const char *x_mac_creator,                  const char *real_name,                  const char *base_url,                  PRBool /*digest_p*/,                  nsMsgAttachmentHandler * /*ma*/,                  const char *attachmentCharset,                  const char *bodyCharset,                  PRBool bodyIsAsciiOnly,                  const char *content_id,                   PRBool      aBodyDocument){  nsresult rv;  nsCString buf("");  NS_ASSERTION (encoding, "null encoding");  PRBool usemime = nsMsgMIMEGetConformToStandard();  PRInt32 parmFolding = 0;  nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));  if (prefs)    prefs->GetIntPref("mail.strictly_mime.parm_folding", &parmFolding);  /* Let's encode the real name */  char *encodedRealName = nsnull;  nsXPIDLCString charset;   // actual charset used for MIME encode  if (real_name)  {    // first try main body's charset to encode the file name,     // then try local file system charset if fails    NS_ConvertUTF8toUCS2 realName(real_name);    if (bodyCharset && *bodyCharset &&        nsMsgI18Ncheck_data_in_charset_range(bodyCharset, realName.get()))      charset.Assign(bodyCharset);    else    {      charset.Assign(nsMsgI18NFileSystemCharset());      if (!nsMsgI18Ncheck_data_in_charset_range(charset.get(), realName.get()))        charset.Assign("UTF-8"); // set to UTF-8 if fails again    }    encodedRealName = nsMsgI18NEncodeMimePartIIStr(real_name, PR_FALSE, charset.get(), 0, usemime);    if (!encodedRealName || !*encodedRealName)    {      PR_Free(encodedRealName);      encodedRealName = PL_strdup(real_name);      charset.Assign("us-ascii");    }    /* ... and then put backslashes before special characters (RFC 822 tells us to). */    char *qtextName = msg_make_filename_qtext(encodedRealName, (parmFolding == 0 ? PR_TRUE : PR_FALSE));        if (qtextName)    {      PR_FREEIF(encodedRealName);      encodedRealName = qtextName;    }  }  buf.Append("Content-Type: ");  buf.Append(type);  if (type_param && *type_param)  {    if (*type_param != ';')      buf.Append("; ");    buf.Append(type_param);  }  if (mime_type_needs_charset (type))   {        char charset_label[65] = "";   // Content-Type: charset    if (attachmentCharset)    {      PL_strncpy(charset_label, attachmentCharset, sizeof(charset_label)-1);      charset_label[sizeof(charset_label)-1] = '\0';    }        /* If the characters are all 7bit, arguably it's better to     claim the charset to be US-ASCII. However, it causes    a major 'interoperability problem' with MS OE, which makes it hard    to sell Mozilla/TB to people most of whose correspondents use    MS OE. MS OE turns all non-ASCII characters to question marks     in replies to messages labeled as US-ASCII if users select 'send as is'    with MIME turned on. (with MIME turned off, this happens without    any warning.) To avoid this, we use the label 'US-ASCII' only when    it's explicitly requested by setting the hidden pref.    'mail.label_ascii_only_mail_as_us_ascii'. (bug 247958) */    PRBool labelAsciiAsAscii = PR_FALSE;    if (prefs)      prefs->GetBoolPref("mail.label_ascii_only_mail_as_us_ascii",                         &labelAsciiAsAscii);    if (labelAsciiAsAscii && encoding &&        !PL_strcasecmp (encoding, "7bit") && bodyIsAsciiOnly)      PL_strcpy (charset_label, "us-ascii");        // If charset is multibyte then no charset to be specified (apply base64 instead).    // The list of file types match with PickEncoding() where we put base64 label.    if ( ((attachmentCharset && !nsMsgI18Nmultibyte_charset(attachmentCharset)) ||         ((PL_strcasecmp(type, TEXT_HTML) == 0) ||         (PL_strcasecmp(type, TEXT_MDL) == 0) ||         (PL_strcasecmp(type, TEXT_PLAIN) == 0) ||         (PL_strcasecmp(type, TEXT_RICHTEXT) == 0) ||         (PL_strcasecmp(type, TEXT_ENRICHED) == 0) ||         (PL_strcasecmp(type, TEXT_VCARD) == 0) ||         (PL_strcasecmp(type, APPLICATION_DIRECTORY) == 0) || /* text/x-vcard synonym */         (PL_strcasecmp(type, TEXT_CSS) == 0) ||         (PL_strcasecmp(type, TEXT_JSSS) == 0)) ||         (PL_strcasecmp(encoding, ENCODING_BASE64) != 0)) &&         (*charset_label))    {      buf.Append("; charset=");      buf.Append(charset_label);    }  }  // Only do this if we are in the body of a message  if (aBodyDocument)  {    // Add format=flowed as in RFC 2646 if we are using that    if(type && !PL_strcasecmp(type, "text/plain"))    {      if(UseFormatFlowed(bodyCharset))        buf.Append("; format=flowed");      // else      // {      // Don't add a markup. Could use       //        PUSH_STRING ("; format=fixed");      // but it is equivalent to nothing at all and we do want      // to save bandwidth. Don't we?      // }    }  }      if (x_mac_type && *x_mac_type) {    buf.Append("; x-mac-type=\"");    buf.Append(x_mac_type);    buf.Append("\"");  }  if (x_mac_creator && *x_mac_creator) {    buf.Append("; x-mac-creator=\"");    buf.Append(x_mac_creator);    buf.Append("\"");  }#ifdef EMIT_NAME_IN_CONTENT_TYPE  if (encodedRealName && *encodedRealName) {    if (parmFolding == 0 || parmFolding == 1) {      buf.Append(";\r\n name=\"");      buf.Append(encodedRealName);      buf.Append("\"");    }    else // if (parmFolding == 2)    {      char *rfc2231Parm = RFC2231ParmFolding("name", charset.get(),                         nsMsgI18NGetAcceptLanguage(), encodedRealName);      if (rfc2231Parm) {        buf.Append(";\r\n ");        buf.Append(rfc2231Parm);        PR_Free(rfc2231Parm);      }    }  }#endif /* EMIT_NAME_IN_CONTENT_TYPE */  buf.Append(CRLF);  buf.Append("Content-Transfer-Encoding: ");  buf.Append(encoding);	buf.Append(CRLF);  if (description && *description) {    char *s = mime_fix_header (description);    if (s) {      buf.Append("Content-Description: ");      buf.Append(s);      buf.Append(CRLF);      PR_Free(s);    }  }  if ( (content_id) && (*content_id) )  {    buf.Append("Content-ID: <");    buf.Append(content_id);    buf.Append(">");      buf.Append(CRLF);  }  if (encodedRealName && *encodedRealName) {    char *period = PL_strrchr(encodedRealName, '.');    PRInt32 pref_content_disposition = 0;    if (prefs) {      rv = prefs->GetIntPref("mail.content_disposition_type",                             &pref_content_disposition);      NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get mail.content_disposition_type");    }    buf.Append("Content-Disposition: ");    if (pref_content_disposition == 1)      buf.Append("attachment");    else      if (pref_content_disposition == 2 &&           (!PL_strcasecmp(type, TEXT_PLAIN) ||           (period && !PL_strcasecmp(period, ".txt"))))        buf.Append("attachment");      /* If this document is an anonymous binary file or a vcard,       then always show it as an attachment, never inline. */      else        if (!PL_strcasecmp(type, APPLICATION_OCTET_STREAM) ||             !PL_strcasecmp(type, TEXT_VCARD) ||            !PL_strcasecmp(type, APPLICATION_DIRECTORY)) /* text/x-vcard synonym */          buf.Append("attachment");        else          buf.Append("inline");    if (parmFolding == 0 || parmFolding == 1) {      buf.Append(";\r\n filename=\"");      buf.Append(encodedRealName);      buf.Append("\"" CRLF);    }    else // if (parmFolding == 2)    {      char *rfc2231Parm = RFC2231ParmFolding("filename", charset.get(),                       nsMsgI18NGetAcceptLanguage(), encodedRealName);      if (rfc2231Parm) {        buf.Append(";\r\n ");        buf.Append(rfc2231Parm);        buf.Append(CRLF);        PR_Free(rfc2231Parm);      }    }  }  else    if (type &&        (!PL_strcasecmp (type, MESSAGE_RFC822) ||        !PL_strcasecmp (type, MESSAGE_NEWS)))      buf.Append("Content-Disposition: inline" CRLF);  #ifdef GENERATE_CONTENT_BASE  /* If this is an HTML document, and we know the URL it originally     came from, write out a Content-Base header. */  if (type &&      (!PL_strcasecmp (type, TEXT_HTML) ||      !PL_strcasecmp (type, TEXT_MDL)) &&      base_url && *base_url)  {    PRInt32 col = 0;    const char *s = base_url;    const char *colon = PL_strchr (s, ':');    PRBool useContentLocation = PR_FALSE;   /* rhp - add this  */    if (!colon)      goto GIVE_UP_ON_CONTENT_BASE;  /* malformed URL? */    /* Don't emit a content-base that points to (or into) a news or       mail message. */    if (!PL_strncasecmp (s, "news:", 5) ||        !PL_strncasecmp (s, "snews:", 6) ||        !PL_strncasecmp (s, "IMAP:", 5) ||        !PL_strncasecmp (s, "file:", 5) ||    /* rhp: fix targets from included HTML files */        !PL_strncasecmp (s, "mailbox:", 8))      goto GIVE_UP_ON_CONTENT_BASE;    /* rhp - Put in a pref for using Content-Location instead of Content-Base.           This will get tweaked to default to true in 5.0    */    if (prefs)       prefs->GetBoolPref("mail.use_content_location_on_send", &useContentLocation);    if (useContentLocation)      buf.Append("Content-Location: \"");    else      buf.Append("Content-Base: \"");    /* rhp - Pref for Content-Location usage *//* rhp: this is to work with the Content-Location stuff */CONTENT_LOC_HACK:    while (*s != 0 && *s != '#')    {      PRUint32 ot=buf.Length();      char tmp[]="\x00\x00";      /* URLs must be wrapped at 40 characters or less. */      if (col >= 38) {        buf.Append(CRLF "\t");        col = 0;      }      if (*s == ' ')        buf.Append("%20");      else if (*s == '\t')        buf.Append("%09");      else if (*s == '\n')        buf.Append("%0A");      else if (*s == '\r')        buf.Append("%0D");      else {	      tmp[0]=*s;	      buf.Append(tmp);      }      s++;      col += (buf.Length() - ot);    }    buf.Append("\"" CRLF);    /* rhp: this is to try to get around this fun problem with Content-Location */    if (!useContentLocation) {      buf.Append("Content-Location: \"");      s = base_url;      col = 0;      useContentLocation = PR_TRUE;      goto CONTENT_LOC_HACK;    }    /* rhp: this is to try to get around this fun problem with Content-Location */GIVE_UP_ON_CONTENT_BASE:    ;  }#endif /* GENERATE_CONTENT_BASE */  /* realloc it smaller... */  PR_FREEIF(encodedRealName);  return PL_strdup(buf.get());}