2015-05-03 22:32:37 +03: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 14:19:18 +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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/TextDecoder.h"
|
2014-11-21 22:58:51 +03:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2017-06-17 05:54:40 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2015-11-03 21:29:19 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2012-09-28 14:19:18 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-11-21 22:58:51 +03:00
|
|
|
#include <stdint.h>
|
2012-09-28 14:19:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-08-08 21:30:01 +03:00
|
|
|
void TextDecoder::Init(const nsAString& aLabel,
|
|
|
|
const TextDecoderOptions& aOptions, ErrorResult& aRv) {
|
2012-11-07 03:23:14 +04:00
|
|
|
// Let encoding be the result of getting an encoding from label.
|
2015-02-04 14:46:26 +03:00
|
|
|
// If encoding is failure or replacement, throw a RangeError
|
|
|
|
// (https://encoding.spec.whatwg.org/#dom-textdecoder).
|
2017-06-17 05:54:40 +03:00
|
|
|
const Encoding* encoding = Encoding::ForLabelNoReplacement(aLabel);
|
|
|
|
if (!encoding) {
|
2014-05-08 13:32:00 +04:00
|
|
|
nsAutoString label(aLabel);
|
2017-06-17 05:54:40 +03:00
|
|
|
label.Trim(" \t\n\f\r");
|
2015-11-20 21:36:46 +03:00
|
|
|
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(label);
|
2012-09-28 14:19:18 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-08-08 21:30:01 +03:00
|
|
|
InitWithEncoding(WrapNotNull(encoding), aOptions);
|
2013-12-17 14:47:25 +04:00
|
|
|
}
|
2012-09-28 14:19:18 +04:00
|
|
|
|
2017-06-17 05:54:40 +03:00
|
|
|
void TextDecoder::InitWithEncoding(NotNull<const Encoding*> aEncoding,
|
2018-08-08 21:30:01 +03:00
|
|
|
const TextDecoderOptions& aOptions) {
|
2017-06-17 05:54:40 +03:00
|
|
|
aEncoding->Name(mEncoding);
|
2018-08-08 21:30:01 +03:00
|
|
|
// Store the flags passed via our options dictionary.
|
2018-08-08 21:30:01 +03:00
|
|
|
mFatal = aOptions.mFatal;
|
2018-08-08 21:30:01 +03:00
|
|
|
mIgnoreBOM = aOptions.mIgnoreBOM;
|
2012-09-28 14:19:18 +04:00
|
|
|
|
|
|
|
// Create a decoder object for mEncoding.
|
2018-08-08 21:30:01 +03:00
|
|
|
if (mIgnoreBOM) {
|
|
|
|
mDecoder = aEncoding->NewDecoderWithoutBOMHandling();
|
|
|
|
} else {
|
|
|
|
mDecoder = aEncoding->NewDecoderWithBOMRemoval();
|
|
|
|
}
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
void TextDecoder::Decode(Span<const uint8_t> aInput, const bool aStream,
|
|
|
|
nsAString& aOutDecodedString, ErrorResult& aRv) {
|
2012-09-28 14:19:18 +04:00
|
|
|
aOutDecodedString.Truncate();
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
CheckedInt<size_t> needed = mDecoder->MaxUTF16BufferLength(aInput.Length());
|
|
|
|
if (!needed.isValid() ||
|
|
|
|
needed.value() > MaxValue<nsAString::size_type>::value) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2012-09-28 14:19:18 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
|
|
|
|
if (!aOutDecodedString.SetLength(needed.value(), fallible)) {
|
2012-09-28 14:19:18 +04:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
if (mFatal) {
|
|
|
|
Tie(result, read, written) = mDecoder->DecodeToUTF16WithoutReplacement(
|
|
|
|
aInput, aOutDecodedString, !aStream);
|
|
|
|
if (result != kInputEmpty) {
|
|
|
|
aRv.ThrowTypeError<MSG_DOM_DECODING_FAILED>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mDecoder->DecodeToUTF16(aInput, aOutDecodedString, !aStream);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(result == kInputEmpty);
|
|
|
|
MOZ_ASSERT(read == aInput.Length());
|
|
|
|
MOZ_ASSERT(written <= aOutDecodedString.Length());
|
|
|
|
Unused << hadErrors;
|
2016-01-05 16:48:47 +03:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
if (!aOutDecodedString.SetLength(written, fallible)) {
|
2016-01-05 16:48:47 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-28 14:19:18 +04: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 04:15:43 +04:00
|
|
|
if (!aStream) {
|
2017-04-27 13:27:03 +03:00
|
|
|
mDecoder->Encoding()->NewDecoderWithBOMRemovalInto(*mDecoder);
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:58:51 +03:00
|
|
|
void TextDecoder::Decode(const Optional<ArrayBufferViewOrArrayBuffer>& aBuffer,
|
|
|
|
const TextDecodeOptions& aOptions,
|
|
|
|
nsAString& aOutDecodedString, ErrorResult& aRv) {
|
|
|
|
if (!aBuffer.WasPassed()) {
|
2017-04-27 13:27:03 +03:00
|
|
|
Decode(nullptr, aOptions.mStream, aOutDecodedString, aRv);
|
2014-11-21 22:58:51 +03:00
|
|
|
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();
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
Decode(MakeSpan(data, length), aOptions.mStream, aOutDecodedString, aRv);
|
2014-11-21 22:58:51 +03:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:17:09 +04:00
|
|
|
void TextDecoder::GetEncoding(nsAString& aEncoding) {
|
2012-11-08 03:04:22 +04:00
|
|
|
CopyASCIItoUTF16(mEncoding, aEncoding);
|
|
|
|
nsContentUtils::ASCIIToLower(aEncoding);
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|