2003-06-13 01:57:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=2:tabstop=4:
|
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-06-13 01:57:49 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUTF8ConverterService.h"
|
|
|
|
#include "nsEscape.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2015-11-03 23:49:22 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
|
|
|
|
using mozilla::dom::EncodingUtils;
|
2003-06-13 01:57:49 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsUTF8ConverterService, nsIUTF8ConverterService)
|
2003-06-13 01:57:49 +04:00
|
|
|
|
|
|
|
static nsresult
|
2012-05-21 17:31:00 +04:00
|
|
|
ToUTF8(const nsACString &aString, const char *aCharset,
|
|
|
|
bool aAllowSubstitution, nsACString &aResult)
|
2003-06-13 01:57:49 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (!aCharset || !*aCharset)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2014-05-08 13:32:00 +04:00
|
|
|
nsDependentCString label(aCharset);
|
|
|
|
nsAutoCString encoding;
|
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(label, encoding)) {
|
|
|
|
return NS_ERROR_UCONV_NOCONV;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder =
|
|
|
|
EncodingUtils::DecoderForEncoding(encoding);
|
2003-06-13 01:57:49 +04:00
|
|
|
|
2012-05-21 17:31:00 +04:00
|
|
|
if (!aAllowSubstitution)
|
|
|
|
unicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srcLen = aString.Length();
|
|
|
|
int32_t dstLen;
|
2003-06-13 01:57:49 +04:00
|
|
|
const nsAFlatCString& inStr = PromiseFlatCString(aString);
|
|
|
|
rv = unicodeDecoder->GetMaxLength(inStr.get(), srcLen, &dstLen);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-11-03 23:49:22 +03:00
|
|
|
auto ustr = mozilla::MakeUnique<char16_t[]>(dstLen);
|
2003-06-13 01:57:49 +04:00
|
|
|
NS_ENSURE_TRUE(ustr, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2015-11-03 23:49:22 +03:00
|
|
|
rv = unicodeDecoder->Convert(inStr.get(), &srcLen, ustr.get(), &dstLen);
|
2003-06-24 18:31:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)){
|
2015-11-03 23:49:22 +03:00
|
|
|
CopyUTF16toUTF8(Substring(ustr.get(), ustr.get() + dstLen), aResult);
|
2003-06-24 18:31:09 +04:00
|
|
|
}
|
2003-06-13 01:57:49 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUTF8ConverterService::ConvertStringToUTF8(const nsACString &aString,
|
|
|
|
const char *aCharset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aSkipCheck,
|
2012-05-21 17:31:00 +04:00
|
|
|
bool aAllowSubstitution,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aOptionalArgc,
|
2003-06-13 01:57:49 +04:00
|
|
|
nsACString &aUTF8String)
|
|
|
|
{
|
2012-07-25 05:37:32 +04:00
|
|
|
bool allowSubstitution = (aOptionalArgc == 1) ? aAllowSubstitution : true;
|
|
|
|
|
2003-06-13 01:57:49 +04:00
|
|
|
// return if ASCII only or valid UTF-8 providing that the ASCII/UTF-8
|
|
|
|
// check is requested. It may not be asked for if a caller suspects
|
|
|
|
// that the input is in non-ASCII 7bit charset (ISO-2022-xx, HZ) or
|
|
|
|
// it's in a charset other than UTF-8 that can be mistaken for UTF-8.
|
|
|
|
if (!aSkipCheck && (IsASCII(aString) || IsUTF8(aString))) {
|
|
|
|
aUTF8String = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-24 18:31:09 +04:00
|
|
|
aUTF8String.Truncate();
|
|
|
|
|
2012-07-25 05:37:32 +04:00
|
|
|
nsresult rv = ToUTF8(aString, aCharset, allowSubstitution, aUTF8String);
|
2003-06-13 01:57:49 +04:00
|
|
|
|
|
|
|
// additional protection for cases where check is skipped and the input
|
|
|
|
// is actually in UTF-8 as opposed to aCharset. (i.e. caller's hunch
|
|
|
|
// was wrong.) We don't check ASCIIness assuming there's no charset
|
|
|
|
// incompatible with ASCII (we don't support EBCDIC).
|
|
|
|
if (aSkipCheck && NS_FAILED(rv) && IsUTF8(aString)) {
|
|
|
|
aUTF8String = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUTF8ConverterService::ConvertURISpecToUTF8(const nsACString &aSpec,
|
|
|
|
const char *aCharset,
|
|
|
|
nsACString &aUTF8Spec)
|
|
|
|
{
|
|
|
|
// assume UTF-8 if the spec contains unescaped non-ASCII characters.
|
|
|
|
// No valid spec in Mozilla would break this assumption.
|
|
|
|
if (!IsASCII(aSpec)) {
|
|
|
|
aUTF8Spec = aSpec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-24 18:31:09 +04:00
|
|
|
aUTF8Spec.Truncate();
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString unescapedSpec;
|
2005-01-24 02:13:10 +03:00
|
|
|
// NS_UnescapeURL does not fill up unescapedSpec unless there's at least
|
|
|
|
// one character to unescape.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool written = NS_UnescapeURL(PromiseFlatCString(aSpec).get(), aSpec.Length(),
|
2005-01-24 02:13:10 +03:00
|
|
|
esc_OnlyNonASCII, unescapedSpec);
|
2003-06-13 01:57:49 +04:00
|
|
|
|
2005-01-24 02:13:10 +03:00
|
|
|
if (!written) {
|
|
|
|
aUTF8Spec = aSpec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// return if ASCII only or escaped UTF-8
|
|
|
|
if (IsASCII(unescapedSpec) || IsUTF8(unescapedSpec)) {
|
|
|
|
aUTF8Spec = unescapedSpec;
|
|
|
|
return NS_OK;
|
2003-06-13 01:57:49 +04:00
|
|
|
}
|
|
|
|
|
2012-05-21 17:31:00 +04:00
|
|
|
return ToUTF8(unescapedSpec, aCharset, true, aUTF8Spec);
|
2005-01-12 02:10:58 +03:00
|
|
|
}
|
2005-01-24 02:13:10 +03:00
|
|
|
|