2014-11-21 19:58:51 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-09-28 10:19:18 +00: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"
|
2014-11-21 19:58:51 +00:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2012-09-28 10:19:18 +00:00
|
|
|
#include "nsContentUtils.h"
|
2014-11-21 19:58:51 +00:00
|
|
|
#include <stdint.h>
|
2012-09-28 10:19:18 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
static const char16_t kReplacementChar = static_cast<char16_t>(0xFFFD);
|
2012-09-28 10:19:18 +00:00
|
|
|
|
|
|
|
void
|
2013-12-17 10:47:25 +00:00
|
|
|
TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
|
2013-08-23 05:17:09 +00:00
|
|
|
ErrorResult& aRv)
|
2012-09-28 10:19:18 +00:00
|
|
|
{
|
2013-12-17 10:47:25 +00:00
|
|
|
nsAutoCString encoding;
|
2012-11-06 23:23:14 +00:00
|
|
|
// Let encoding be the result of getting an encoding from label.
|
2013-11-25 08:06:56 +00:00
|
|
|
// If encoding is failure or replacement, throw a TypeError.
|
2014-05-08 09:32:00 +00:00
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aLabel, encoding)) {
|
|
|
|
nsAutoString label(aLabel);
|
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
2012-11-06 23:23:14 +00:00
|
|
|
aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
|
2012-09-28 10:19:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-17 10:47:25 +00:00
|
|
|
InitWithEncoding(encoding, aFatal);
|
|
|
|
}
|
2012-09-28 10:19:18 +00:00
|
|
|
|
2013-12-17 10:47:25 +00:00
|
|
|
void
|
|
|
|
TextDecoder::InitWithEncoding(const nsACString& aEncoding, const bool aFatal)
|
|
|
|
{
|
|
|
|
mEncoding = aEncoding;
|
2012-09-28 10:19:18 +00: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-22 00:15:43 +00:00
|
|
|
mFatal = aFatal;
|
2012-09-28 10:19:18 +00:00
|
|
|
|
|
|
|
// Create a decoder object for mEncoding.
|
2013-11-26 07:31:52 +00:00
|
|
|
mDecoder = EncodingUtils::DecoderForEncoding(mEncoding);
|
2012-09-28 10:19:18 +00:00
|
|
|
|
|
|
|
if (mFatal) {
|
|
|
|
mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-23 05:17:09 +00:00
|
|
|
TextDecoder::Decode(const char* aInput, const int32_t aLength,
|
|
|
|
const bool aStream, nsAString& aOutDecodedString,
|
|
|
|
ErrorResult& aRv)
|
2012-09-28 10:19:18 +00:00
|
|
|
{
|
|
|
|
aOutDecodedString.Truncate();
|
|
|
|
|
|
|
|
// Run or resume the decoder algorithm of the decoder object's encoder.
|
|
|
|
int32_t outLen;
|
2012-12-29 00:55:32 +00:00
|
|
|
nsresult rv = mDecoder->GetMaxLength(aInput, aLength, &outLen);
|
2012-09-28 10:19:18 +00: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 15:02:17 +00:00
|
|
|
nsAutoArrayPtr<char16_t> buf(new (fallible) char16_t[outLen + 1]);
|
2012-09-28 10:19:18 +00:00
|
|
|
if (!buf) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-29 00:55:32 +00:00
|
|
|
int32_t length = aLength;
|
|
|
|
rv = mDecoder->Convert(aInput, &length, buf, &outLen);
|
2012-12-10 14:11:15 +00:00
|
|
|
MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT);
|
|
|
|
buf[outLen] = 0;
|
|
|
|
aOutDecodedString.Append(buf, outLen);
|
2012-09-28 10:19:18 +00: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-22 00:15:43 +00:00
|
|
|
if (!aStream) {
|
2012-11-27 01:38:20 +00:00
|
|
|
mDecoder->Reset();
|
2012-09-28 10:19:18 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 19:58:51 +00:00
|
|
|
void
|
|
|
|
TextDecoder::Decode(const Optional<ArrayBufferViewOrArrayBuffer>& aBuffer,
|
|
|
|
const TextDecodeOptions& aOptions,
|
|
|
|
nsAString& aOutDecodedString,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!aBuffer.WasPassed()) {
|
|
|
|
Decode(nullptr, 0, aOptions.mStream, aOutDecodedString, aRv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ArrayBufferViewOrArrayBuffer& buf = aBuffer.Value();
|
|
|
|
uint8_t* data;
|
|
|
|
uint32_t length;
|
|
|
|
if (buf.IsArrayBufferView()) {
|
|
|
|
buf.GetAsArrayBufferView().ComputeLengthAndData();
|
|
|
|
data = buf.GetAsArrayBufferView().Data();
|
|
|
|
length = buf.GetAsArrayBufferView().Length();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(buf.IsArrayBuffer());
|
|
|
|
buf.GetAsArrayBuffer().ComputeLengthAndData();
|
|
|
|
data = buf.GetAsArrayBuffer().Data();
|
|
|
|
length = buf.GetAsArrayBuffer().Length();
|
|
|
|
}
|
|
|
|
// The other Decode signature takes a signed int, because that's
|
|
|
|
// what nsIUnicodeDecoder::Convert takes as the length. Throw if
|
|
|
|
// our length is too big.
|
|
|
|
if (length > INT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Decode(reinterpret_cast<char*>(data), length, aOptions.mStream,
|
|
|
|
aOutDecodedString, aRv);
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:19:18 +00:00
|
|
|
void
|
2013-08-23 05:17:09 +00:00
|
|
|
TextDecoder::GetEncoding(nsAString& aEncoding)
|
2012-09-28 10:19:18 +00:00
|
|
|
{
|
2012-11-07 23:04:22 +00:00
|
|
|
CopyASCIItoUTF16(mEncoding, aEncoding);
|
|
|
|
nsContentUtils::ASCIIToLower(aEncoding);
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // dom
|
|
|
|
} // mozilla
|