nsresult ConvertFromUnicode(const nsAString& aCharset,
nsresult ConvertFromUnicode(const char* aCharset,
nsresult ConvertFromUnicode(const nsAString& aCharset,                             const nsString& inString,                            char** outCString){#if 0   nsCAutoString s;  nsresult rv;  rv = nsMsgI18NConvertFromUnicode(aCharset, inString, s);  *outCString = PL_strdup(s);  return rv;#endif  *outCString = NULL;  if (inString.IsEmpty()) {    *outCString = nsCRT::strdup("");    return (NULL == *outCString) ? NS_ERROR_OUT_OF_MEMORY : NS_OK;  }  // Note: this will hide a possible error when the unicode text may contain more than one charset.  // (e.g. Latin1 + Japanese). Use nsMsgI18NSaveAsCharset instead to avoid that problem.  else if (aCharset.IsEmpty() ||           Compare(aCharset,                   NS_LITERAL_STRING("us-ascii"),                   nsCaseInsensitiveStringComparator()) == 0 ||           Compare(aCharset,                   NS_LITERAL_STRING("ISO-8859-1"),                   nsCaseInsensitiveStringComparator()) == 0) {    *outCString = ToNewCString(inString);    return (NULL == *outCString) ? NS_ERROR_OUT_OF_MEMORY : NS_OK;  }  else if (Compare(aCharset,                   NS_LITERAL_STRING("UTF-8"),                   nsCaseInsensitiveStringComparator()) == 0) {    *outCString = ToNewUTF8String(inString);    return (NULL == *outCString) ? NS_ERROR_OUT_OF_MEMORY : NS_OK;  }  nsresult res;  nsCOMPtr <nsICharsetConverterManager2> ccm2 = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);  NS_ENSURE_SUCCESS(res, res);  nsCOMPtr <nsIAtom> charsetAtom;  res = ccm2->GetCharsetAtom(PromiseFlatString(aCharset).get(),                             getter_AddRefs(charsetAtom));  NS_ENSURE_SUCCESS(res, res);  // get an unicode converter  nsCOMPtr<nsIUnicodeEncoder> encoder;  res = ccm2->GetUnicodeEncoder(charsetAtom, getter_AddRefs(encoder));  NS_ENSURE_SUCCESS(res, res);  PRUnichar *unichars = (PRUnichar *) inString.get();  PRInt32 unicharLength = inString.Length();  PRInt32 dstLength;  res = encoder->GetMaxLength(unichars, unicharLength, &dstLength);  NS_ENSURE_SUCCESS(res, res);  res = encoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, '?');  NS_ENSURE_SUCCESS(res, res);  // allocale an output buffer  *outCString = (char *) PR_Malloc(dstLength + 1);  NS_ENSURE_TRUE(*outCString, NS_ERROR_OUT_OF_MEMORY);  PRInt32 buffLength = dstLength;  **outCString = '\0';  res = encoder->Convert(unichars, &unicharLength, *outCString, &dstLength);  if (NS_SUCCEEDED(res)) {    PRInt32 finLen = buffLength - dstLength;    res = encoder->Finish((char *)(*outCString+dstLength), &finLen);    if (NS_SUCCEEDED(res)) {      dstLength += finLen;    }    (*outCString)[dstLength] = '\0';  }  return res;}