2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2014-11-21 11:58:51 -08:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2012-09-28 03:19:18 -07:00
|
|
|
#include "nsContentUtils.h"
|
2014-11-21 11:58:51 -08:00
|
|
|
#include <stdint.h>
|
2012-09-28 03:19:18 -07:00
|
|
|
|
|
|
|
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
|
|
|
nsAutoCString encoding;
|
2012-11-06 15:23:14 -08:00
|
|
|
// Let encoding be the result of getting an encoding from label.
|
2015-02-04 03:46:26 -08:00
|
|
|
// If encoding is failure or replacement, throw a RangeError
|
|
|
|
// (https://encoding.spec.whatwg.org/#dom-textdecoder).
|
2014-05-08 02:32:00 -07:00
|
|
|
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aLabel, encoding)) {
|
|
|
|
nsAutoString label(aLabel);
|
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
2015-11-20 10:36:46 -08:00
|
|
|
aRv.ThrowRangeError<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.
|
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;
|
2016-01-05 05:48:47 -08:00
|
|
|
|
|
|
|
if (!aOutDecodedString.Append(buf, outLen, fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2015-10-05 09:38:14 -07:00
|
|
|
aRv.ThrowTypeError<MSG_DOM_DECODING_FAILED>();
|
2012-09-28 03:19:18 -07:00
|
|
|
} else {
|
|
|
|
// Need to emit a decode error manually
|
|
|
|
// to simulate the EOF handling of the Encoding spec.
|
|
|
|
aOutDecodedString.Append(kReplacementChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-10-05 09:38:14 -07:00
|
|
|
aRv.ThrowTypeError<MSG_DOM_DECODING_FAILED>();
|
2012-09-28 03:19:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 11:58:51 -08: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 03:19:18 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-13 08:25:42 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|