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/TextDecoder.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
static const char16_t kReplacementChar = static_cast<char16_t>(0xFFFD);
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
void
|
2013-12-17 02:47:25 -08:00
|
|
|
TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
|
2013-08-22 22:17:09 -07:00
|
|
|
ErrorResult& aRv)
|
2012-09-28 03:19:18 -07:00
|
|
|
{
|
2013-12-17 02:47:25 -08:00
|
|
|
nsAutoString label(aLabel);
|
2012-09-28 03:19:18 -07:00
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
nsAutoCString encoding;
|
2012-11-06 15:23:14 -08:00
|
|
|
// Let encoding be the result of getting an encoding from label.
|
2013-11-25 00:06:56 -08:00
|
|
|
// If encoding is failure or replacement, throw a TypeError.
|
2013-12-17 02:47:25 -08:00
|
|
|
if (!EncodingUtils::FindEncodingForLabel(label, encoding) ||
|
|
|
|
encoding.EqualsLiteral("replacement")) {
|
2012-11-06 15:23:14 -08:00
|
|
|
aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
|
2012-09-28 03:19:18 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-12-17 02:47:25 -08:00
|
|
|
InitWithEncoding(encoding, aFatal);
|
|
|
|
}
|
2012-09-28 03:19:18 -07:00
|
|
|
|
2013-12-17 02:47:25 -08:00
|
|
|
void
|
|
|
|
TextDecoder::InitWithEncoding(const nsACString& aEncoding, const bool aFatal)
|
|
|
|
{
|
|
|
|
mEncoding = aEncoding;
|
2012-09-28 03:19:18 -07:00
|
|
|
// If the constructor is called with an options argument,
|
|
|
|
// and the fatal property of the dictionary is set,
|
|
|
|
// set the internal fatal flag of the decoder object.
|
2012-12-21 16:15:43 -08:00
|
|
|
mFatal = aFatal;
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
// Create a decoder object for mEncoding.
|
2013-11-25 23:31:52 -08:00
|
|
|
mDecoder = EncodingUtils::DecoderForEncoding(mEncoding);
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
if (mFatal) {
|
|
|
|
mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-22 22:17:09 -07:00
|
|
|
TextDecoder::Decode(const char* aInput, const int32_t aLength,
|
|
|
|
const bool aStream, nsAString& aOutDecodedString,
|
|
|
|
ErrorResult& aRv)
|
2012-09-28 03:19:18 -07:00
|
|
|
{
|
|
|
|
aOutDecodedString.Truncate();
|
|
|
|
|
|
|
|
// Run or resume the decoder algorithm of the decoder object's encoder.
|
|
|
|
int32_t outLen;
|
2012-12-28 16:55:32 -08:00
|
|
|
nsresult rv = mDecoder->GetMaxLength(aInput, aLength, &outLen);
|
2012-09-28 03:19:18 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Need a fallible allocator because the caller may be a content
|
|
|
|
// and the content can specify the length of the string.
|
|
|
|
static const fallible_t fallible = fallible_t();
|
2014-01-04 07:02:17 -08:00
|
|
|
nsAutoArrayPtr<char16_t> buf(new (fallible) char16_t[outLen + 1]);
|
2012-09-28 03:19:18 -07:00
|
|
|
if (!buf) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-28 16:55:32 -08:00
|
|
|
int32_t length = aLength;
|
|
|
|
rv = mDecoder->Convert(aInput, &length, buf, &outLen);
|
2012-12-10 06:11:15 -08:00
|
|
|
MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT);
|
|
|
|
buf[outLen] = 0;
|
|
|
|
aOutDecodedString.Append(buf, outLen);
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
// If the internal streaming flag of the decoder object is not set,
|
|
|
|
// then reset the encoding algorithm state to the default values
|
2012-12-21 16:15:43 -08:00
|
|
|
if (!aStream) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mDecoder->Reset();
|
2012-09-28 03:19:18 -07:00
|
|
|
if (rv == NS_OK_UDEC_MOREINPUT) {
|
|
|
|
if (mFatal) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
|
|
|
|
} else {
|
|
|
|
// Need to emit a decode error manually
|
|
|
|
// to simulate the EOF handling of the Encoding spec.
|
|
|
|
aOutDecodedString.Append(kReplacementChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-22 22:17:09 -07:00
|
|
|
TextDecoder::GetEncoding(nsAString& aEncoding)
|
2012-09-28 03:19:18 -07:00
|
|
|
{
|
2012-11-07 15:04:22 -08:00
|
|
|
CopyASCIItoUTF16(mEncoding, aEncoding);
|
|
|
|
nsContentUtils::ASCIIToLower(aEncoding);
|
2012-09-28 03:19:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // dom
|
|
|
|
} // mozilla
|