if (*outCString != nsnull) { PRInt32 originalLength = unicharLength;  res = encoder->Convert(unichars, &unicharLength, *outCString, &dstLength);  if (unicharLength < originalLength) { PR_Free(*outCString); res = NS_ERROR_FAILURE;
if (nsnull != *outCString) { PRInt32 oldUnicharLength = unicharLength; char *tempCString = *outCString; PRInt32 totalCLength = 0; while (1) { res = encoder->Convert(unichars, &unicharLength, tempCString, &dstLength);   tempCString += dstLength; totalCLength += dstLength;    if (0 >= unicharLength || NS_ERROR_UENC_NOMAPPING != res) { break; }   unichars += unicharLength + 1; oldUnicharLength -= (unicharLength + 1); unicharLength = oldUnicharLength;  (void) encoder->GetMaxLength(unichars, unicharLength, &dstLength);
nsresult ConvertFromUnicode(const nsString& aCharset,                             const nsString& inString,                            char** outCString){  nsresult res;  NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &res);   if(NS_SUCCEEDED(res) && (nsnull != ccm)) {    nsIUnicodeEncoder* encoder = nsnull;    nsString convCharset;    // map to converter charset    if (aCharset.EqualsIgnoreCase("us-ascii")) {      convCharset.SetString("iso-8859-1");    }    else {      convCharset = aCharset;     }    // get an unicode converter    res = ccm->GetUnicodeEncoder(&convCharset, &encoder);    if(NS_SUCCEEDED(res) && (nsnull != encoder)) {      const PRUnichar *unichars = inString.GetUnicode();      PRInt32 unicharLength = inString.Length();      PRInt32 dstLength;      res = encoder->GetMaxLength(unichars, unicharLength, &dstLength);      // allocale an output buffer      *outCString = (char *) PR_Malloc(dstLength + 1);      if (*outCString != nsnull) {        PRInt32 originalLength = unicharLength;        // convert from unicode        res = encoder->Convert(unichars, &unicharLength, *outCString, &dstLength);        // estimation of GetMaxLength was incorrect        if (unicharLength < originalLength) {          PR_Free(*outCString);          res = NS_ERROR_FAILURE;        }        else {          (*outCString)[dstLength] = '\0';        }      }      else {        res = NS_ERROR_OUT_OF_MEMORY;      }      NS_IF_RELEASE(encoder);    }      }  return res;}