2012-09-28 03:19:18 -07: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2013-03-17 00:55:16 -07:00
|
|
|
|
|
|
|
#include "mozilla/Util.h" // ArrayLength
|
2012-11-07 15:04:22 -08:00
|
|
|
#include "nsUConvPropertySearch.h"
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2012-11-07 15:04:22 -08:00
|
|
|
static const char* labelsEncodings[][3] = {
|
|
|
|
#include "labelsencodings.properties.h"
|
2012-09-28 03:19:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
EncodingUtils::IdentifyDataOffset(const char* aData,
|
|
|
|
const uint32_t aLength,
|
2012-11-07 15:04:22 -08:00
|
|
|
nsACString& aRetval)
|
2012-09-28 03:19:18 -07:00
|
|
|
{
|
|
|
|
// Truncating to pre-clear return value in case of failure.
|
2012-11-07 15:04:22 -08:00
|
|
|
aRetval.Truncate();
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
// Minimum bytes in input stream data that represents
|
|
|
|
// the Byte Order Mark is 2. Max is 3.
|
|
|
|
if (aLength < 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData[0] == '\xFF' && aData[1] == '\xFE') {
|
2012-11-07 15:04:22 -08:00
|
|
|
aRetval.AssignLiteral("UTF-16LE");
|
2012-09-28 03:19:18 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData[0] == '\xFE' && aData[1] == '\xFF') {
|
2012-11-07 15:04:22 -08:00
|
|
|
aRetval.AssignLiteral("UTF-16BE");
|
2012-09-28 03:19:18 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking utf-8 byte order mark.
|
|
|
|
// Minimum bytes in input stream data that represents
|
|
|
|
// the Byte Order Mark for utf-8 is 3.
|
|
|
|
if (aLength < 3) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData[0] == '\xEF' && aData[1] == '\xBB' && aData[2] == '\xBF') {
|
2012-11-07 15:04:22 -08:00
|
|
|
aRetval.AssignLiteral("UTF-8");
|
2012-09-28 03:19:18 -07:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-11-07 15:04:22 -08:00
|
|
|
EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
|
|
|
|
nsACString& aOutEncoding)
|
2012-09-28 03:19:18 -07:00
|
|
|
{
|
2012-11-07 15:04:22 -08:00
|
|
|
// Save aLabel first because it may refer the same string as aOutEncoding.
|
|
|
|
nsCString label(aLabel);
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
|
|
|
if (label.IsEmpty()) {
|
2012-11-07 15:04:22 -08:00
|
|
|
aOutEncoding.Truncate();
|
2012-09-28 03:19:18 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 15:04:22 -08:00
|
|
|
ToLowerCase(label);
|
|
|
|
return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
|
|
|
|
labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
|
2012-09-28 03:19:18 -07:00
|
|
|
}
|
|
|
|
|
2013-03-04 10:09:11 -08:00
|
|
|
bool
|
|
|
|
EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
|
|
|
|
{
|
|
|
|
return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
|
|
|
|
}
|
|
|
|
|
2012-09-28 03:19:18 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|