Files
palemoon27/dom/encoding/TextDecoder.cpp
T
roytam1 6a4edbe796 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1221716 - Part 1: Make the simpler parts of BytecodeEmitter::emitTree() use the `ok` boolean consistently. r=Waldo. (8b2eae4dcf)
- Bug 1221716 - Part 2: Factor out all remaining complex cases from the switch statement in emitTree(). r=Waldo. (805c9a96da)
- Bug 1221737 - Drop some BytecodeEmitter checks for cases that the Parser rules out. r=jonco. (9726a6527b)
- Bug 1224460 - Use pwd -W to fill _topsrcdir in configure. r=gps (cc7bf71652)
- Bug 1221737 followup - Bump XDR_BYTECODE_VERSION_SUBTRAHEND to fix build errors on a CLOSED TREE. r=bustage (d2d2f41a14)
- Bug 1170913, full-update target in tools/update-packaging/ always runs automation-partial-patch, r=glandium DONTBUILD (12e5e212d8)
- Bug 1137756 - Use absolute paths in complete-patch.patch. r=gps (62fc37688b)
- Bug 1173998 - use localized package for previous mar; r=nthomas (fb6c95955c)
- Bug 1173459 - Stop generating partial MAR files and publishing complete MARs to balrog as a part of nightly automation. r=mshal (4c427b9f7f)
- Bug 1164580 - Preprocess ua-update.json via slashslash filter. r=fabrice (d934f84f15)
- Bug 1200021 - Part 2: more diagnostics. r=bas (e1cfecbbd4)
- Bug 1200021 - crash in mozilla::layers::ContentClientDoubleBuffered::FinalizeFrame(nsIntRegion const&): Diagnostics to get more data. r=bas (d782b24501)
- Bug 1222569 - fix initialization order in DataTextureSourceD3D9; r=Bas (028939600b)
- Bug 1209801 - Part 1: Add TextureFlags parameter to TextureClientPool. r=mattwoodrow (c5b73613f9)
- Bug 1209801 - Part 2: Do not allow big image textures to be used with TiledLayerBuffers. r=mattwoodrow (cb553ccdea)
- Bug 1211615: Upload the full texture on the first upload for component alpha textures. r=nical (3bd6688679)
- minor (928a95b259)
- Bug 1222569 - remove unused variable in TextureD3D9.cpp; r=Bas (ac5a86be1c)
- Bug 1223928 - Make the horizontal scrollbar on the root scrollable shift correctly with the dynamic toolbar. r=botond (6c6c0b8c24)
- Bug 1216349: Upload the old valid region as well if our texture host changed. r=nical (7d4d041bd2)
- Bug 1236227, don't OOM in TextDecoder, r=baku (9bef90eada)
- Bug 1218594 - r=smaug (de39570c72)
- remove include (62e582fe3d)
- Bug 1226176 - Compute retained sizes in dominator trees and expose them to JavaScript; r=bz,sfink (92bb08a674)
2023-01-27 10:51:45 +08:00

147 lines
4.5 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/dom/UnionTypes.h"
#include "nsContentUtils.h"
#include <stdint.h>
namespace mozilla {
namespace dom {
static const char16_t kReplacementChar = static_cast<char16_t>(0xFFFD);
void
TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
ErrorResult& aRv)
{
nsAutoCString encoding;
// Let encoding be the result of getting an encoding from label.
// If encoding is failure or replacement, throw a RangeError
// (https://encoding.spec.whatwg.org/#dom-textdecoder).
if (!EncodingUtils::FindEncodingForLabelNoReplacement(aLabel, encoding)) {
nsAutoString label(aLabel);
EncodingUtils::TrimSpaceCharacters(label);
aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(&label);
return;
}
InitWithEncoding(encoding, aFatal);
}
void
TextDecoder::InitWithEncoding(const nsACString& aEncoding, const bool aFatal)
{
mEncoding = aEncoding;
// 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.
mFatal = aFatal;
// Create a decoder object for mEncoding.
mDecoder = EncodingUtils::DecoderForEncoding(mEncoding);
if (mFatal) {
mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
}
}
void
TextDecoder::Decode(const char* aInput, const int32_t aLength,
const bool aStream, nsAString& aOutDecodedString,
ErrorResult& aRv)
{
aOutDecodedString.Truncate();
// Run or resume the decoder algorithm of the decoder object's encoder.
int32_t outLen;
nsresult rv = mDecoder->GetMaxLength(aInput, aLength, &outLen);
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.
nsAutoArrayPtr<char16_t> buf(new (fallible) char16_t[outLen + 1]);
if (!buf) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
int32_t length = aLength;
rv = mDecoder->Convert(aInput, &length, buf, &outLen);
MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT);
buf[outLen] = 0;
if (!aOutDecodedString.Append(buf, outLen, fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
// If the internal streaming flag of the decoder object is not set,
// then reset the encoding algorithm state to the default values
if (!aStream) {
mDecoder->Reset();
if (rv == NS_OK_UDEC_MOREINPUT) {
if (mFatal) {
aRv.ThrowTypeError<MSG_DOM_DECODING_FAILED>();
} 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.ThrowTypeError<MSG_DOM_DECODING_FAILED>();
}
}
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);
}
void
TextDecoder::GetEncoding(nsAString& aEncoding)
{
CopyASCIItoUTF16(mEncoding, aEncoding);
nsContentUtils::ASCIIToLower(aEncoding);
}
} // namespace dom
} // namespace mozilla