// i18n helper routines nsresult nsEncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut, bool aHeaderEncode) { if (mEncoder && !aStr.IsEmpty()) { aOut.Truncate(); nsresult rv = mEncoder->Convert(PromiseFlatString(aStr).get(), getter_Copies(aOut)); NS_ENSURE_SUCCESS(rv, rv); } else { // fall back to UTF-8 CopyUTF16toUTF8(aStr, aOut); } if (aHeaderEncode) { aOut.Adopt(nsLinebreakConverter:: ConvertLineBreaks(aOut.get(), nsLinebreakConverter::eLinebreakAny, nsLinebreakConverter::eLinebreakSpace)); aOut.ReplaceSubstring(NS_LITERAL_CSTRING("\""), NS_LITERAL_CSTRING("\\\"")); } return NS_OK; }
void nsMsgBodyHandler::StripHtml (nsCString &pBufInOut) { char *pBuf = (char*) PR_Malloc (pBufInOut.Length() + 1); if (pBuf) { char *pWalk = pBuf; char *pWalkInOut = (char *) pBufInOut.get(); bool inTag = false; while (*pWalkInOut) // throw away everything inside < > { if (!inTag) if (*pWalkInOut == '<') inTag = true; else *pWalk++ = *pWalkInOut; else if (*pWalkInOut == '>') inTag = false; pWalkInOut++; } *pWalk = 0; // null terminator pBufInOut.Adopt(pBuf); } }
nsresult nsMsgProtocol::DoNtlmStep1(const char *username, const char *password, nsCString &response) { nsresult rv; m_authModule = do_CreateInstance(NS_AUTH_MODULE_CONTRACTID_PREFIX "ntlm", &rv); // if this fails, then it means that we cannot do NTLM auth. if (NS_FAILED(rv) || !m_authModule) return rv; m_authModule->Init(nullptr, 0, nullptr, NS_ConvertUTF8toUTF16(username).get(), NS_ConvertUTF8toUTF16(password).get()); void *outBuf; uint32_t outBufLen; rv = m_authModule->GetNextToken((void *)nullptr, 0, &outBuf, &outBufLen); if (NS_SUCCEEDED(rv) && outBuf) { char *base64Str = PL_Base64Encode((char *)outBuf, outBufLen, nullptr); if (base64Str) response.Adopt(base64Str); else rv = NS_ERROR_OUT_OF_MEMORY; nsMemory::Free(outBuf); } return rv; }
nsresult nsMsgProtocol::DoGSSAPIStep1(const char *service, const char *username, nsCString &response) { nsresult rv; #ifdef DEBUG_BenB printf("GSSAPI step 1 for service %s, username %s\n", service, username); #endif // if this fails, then it means that we cannot do GSSAPI SASL. m_authModule = do_CreateInstance(NS_AUTH_MODULE_CONTRACTID_PREFIX "sasl-gssapi", &rv); NS_ENSURE_SUCCESS(rv,rv); m_authModule->Init(service, nsIAuthModule::REQ_DEFAULT, nullptr, NS_ConvertUTF8toUTF16(username).get(), nullptr); void *outBuf; uint32_t outBufLen; rv = m_authModule->GetNextToken((void *)nullptr, 0, &outBuf, &outBufLen); if (NS_SUCCEEDED(rv) && outBuf) { char *base64Str = PL_Base64Encode((char *)outBuf, outBufLen, nullptr); if (base64Str) response.Adopt(base64Str); else rv = NS_ERROR_OUT_OF_MEMORY; nsMemory::Free(outBuf); } #ifdef DEBUG_BenB printf("GSSAPI step 1 succeeded\n"); #endif return rv; }
static nsresult MangleKeywordIntoURI(const char *aKeyword, const char *aURL, nsCString& query) { query = (*aKeyword == '?') ? (aKeyword + 1) : aKeyword; query.Trim(" "); // pull leading/trailing spaces. // encode char * encQuery = nsEscape(query.get(), url_XPAlphas); if (!encQuery) return NS_ERROR_OUT_OF_MEMORY; query.Adopt(encQuery); // prepend the query with the keyword url // XXX this url should come from somewhere else query.Insert(aURL, 0); return NS_OK; }
/** * Decodes the given base64 string. * * It returns its decoded string in its input. * * @param pBufInOut (inout) a buffer of the string */ void nsMsgBodyHandler::Base64Decode (nsCString &pBufInOut) { char *decodedBody = PL_Base64Decode(pBufInOut.get(), pBufInOut.Length(), nullptr); if (decodedBody) pBufInOut.Adopt(decodedBody); int32_t offset = pBufInOut.FindChar('\n'); while (offset != -1) { pBufInOut.Replace(offset, 1, ' '); offset = pBufInOut.FindChar('\n', offset); } offset = pBufInOut.FindChar('\r'); while (offset != -1) { pBufInOut.Replace(offset, 1, ' '); offset = pBufInOut.FindChar('\r', offset); } }
// i18n helper routines nsresult nsEncodingFormSubmission::EncodeVal(const nsAString& aStr, nsCString& aOut, bool aHeaderEncode) { if (!mEncoder.Encode(aStr, aOut)) { return NS_ERROR_OUT_OF_MEMORY; } if (aHeaderEncode) { aOut.Adopt(nsLinebreakConverter:: ConvertLineBreaks(aOut.get(), nsLinebreakConverter::eLinebreakAny, nsLinebreakConverter::eLinebreakSpace)); aOut.ReplaceSubstring(NS_LITERAL_CSTRING("\""), NS_LITERAL_CSTRING("\\\"")); } return NS_OK; }
static nsresult GetCertFingerprintByOidTag(CERTCertificate* nsscert, SECOidTag aOidTag, nsCString &fp) { unsigned int hash_len = HASH_ResultLenByOidTag(aOidTag); nsRefPtr<nsStringBuffer> fingerprint = nsStringBuffer::Alloc(hash_len); if (!fingerprint) return NS_ERROR_OUT_OF_MEMORY; PK11_HashBuf(aOidTag, (unsigned char*)fingerprint->Data(), nsscert->derCert.data, nsscert->derCert.len); SECItem fpItem; fpItem.data = (unsigned char*)fingerprint->Data(); fpItem.len = hash_len; fp.Adopt(CERT_Hexify(&fpItem, 1)); return NS_OK; }
// i18n helper routines nsresult nsFSURLEncoded::URLEncode(const nsAString& aStr, nsCString& aEncoded) { // convert to CRLF breaks char16_t* convertedBuf = nsLinebreakConverter::ConvertUnicharLineBreaks(PromiseFlatString(aStr).get(), nsLinebreakConverter::eLinebreakAny, nsLinebreakConverter::eLinebreakNet); NS_ENSURE_TRUE(convertedBuf, NS_ERROR_OUT_OF_MEMORY); nsAutoCString encodedBuf; nsresult rv = EncodeVal(nsDependentString(convertedBuf), encodedBuf, false); free(convertedBuf); NS_ENSURE_SUCCESS(rv, rv); char* escapedBuf = nsEscape(encodedBuf.get(), url_XPAlphas); NS_ENSURE_TRUE(escapedBuf, NS_ERROR_OUT_OF_MEMORY); aEncoded.Adopt(escapedBuf); return NS_OK; }
nsresult nsMsgProtocol::DoNtlmStep2(nsCString &commandResponse, nsCString &response) { nsresult rv; void *inBuf, *outBuf; uint32_t inBufLen, outBufLen; uint32_t len = commandResponse.Length(); // decode into the input secbuffer inBufLen = (len * 3)/4; // sufficient size (see plbase64.h) inBuf = nsMemory::Alloc(inBufLen); if (!inBuf) return NS_ERROR_OUT_OF_MEMORY; // strip off any padding (see bug 230351) const char *challenge = commandResponse.get(); while (challenge[len - 1] == '=') len--; rv = (PL_Base64Decode(challenge, len, (char *)inBuf)) ? m_authModule->GetNextToken(inBuf, inBufLen, &outBuf, &outBufLen) : NS_ERROR_FAILURE; nsMemory::Free(inBuf); if (NS_SUCCEEDED(rv) && outBuf) { char *base64Str = PL_Base64Encode((char *)outBuf, outBufLen, nullptr); if (base64Str) response.Adopt(base64Str); else rv = NS_ERROR_OUT_OF_MEMORY; } if (NS_FAILED(rv)) response = "*"; return rv; }
void MimeHeaders_convert_header_value(MimeDisplayOptions *opt, nsCString &value, bool convert_charset_only) { char *converted; if (value.IsEmpty()) return; if (convert_charset_only) { nsCAutoString output; ConvertRawBytesToUTF8(value, opt->default_charset, output); value.Assign(output); return; } if (opt && opt->rfc1522_conversion_p) { converted = MIME_DecodeMimeHeader(value.get(), opt->default_charset, opt->override_charset, true); if (converted) { value.Adopt(converted); } } else { // This behavior, though highly unusual, was carefully preserved // from the previous implementation. It may be that this is dead // code, in which case opt->rfc1522_conversion_p is no longer // needed. value.Truncate(); } }
nsresult nsMsgProtocol::DoGSSAPIStep2(nsCString &commandResponse, nsCString &response) { #ifdef DEBUG_BenB printf("GSSAPI step 2\n"); #endif nsresult rv; void *inBuf, *outBuf; uint32_t inBufLen, outBufLen; uint32_t len = commandResponse.Length(); // Cyrus SASL may send us zero length tokens (grrrr) if (len > 0) { // decode into the input secbuffer inBufLen = (len * 3)/4; // sufficient size (see plbase64.h) inBuf = nsMemory::Alloc(inBufLen); if (!inBuf) return NS_ERROR_OUT_OF_MEMORY; // strip off any padding (see bug 230351) const char *challenge = commandResponse.get(); while (challenge[len - 1] == '=') len--; // We need to know the exact length of the decoded string to give to // the GSSAPI libraries. But NSPR's base64 routine doesn't seem capable // of telling us that. So, we figure it out for ourselves. // For every 4 characters, add 3 to the destination // If there are 3 remaining, add 2 // If there are 2 remaining, add 1 // 1 remaining is an error inBufLen = (len / 4)*3 + ((len % 4 == 3)?2:0) + ((len % 4 == 2)?1:0); rv = (PL_Base64Decode(challenge, len, (char *)inBuf)) ? m_authModule->GetNextToken(inBuf, inBufLen, &outBuf, &outBufLen) : NS_ERROR_FAILURE; nsMemory::Free(inBuf); } else { rv = m_authModule->GetNextToken(NULL, 0, &outBuf, &outBufLen); } if (NS_SUCCEEDED(rv)) { // And in return, we may need to send Cyrus zero length tokens back if (outBuf) { char *base64Str = PL_Base64Encode((char *)outBuf, outBufLen, nullptr); if (base64Str) response.Adopt(base64Str); else rv = NS_ERROR_OUT_OF_MEMORY; } else response.Adopt((char *)nsMemory::Clone("",1)); } #ifdef DEBUG_BenB printf(NS_SUCCEEDED(rv) ? "GSSAPI step 2 succeeded\n" : "GSSAPI step 2 failed\n"); #endif return rv; }