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/TextEncoder.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
void
|
2013-08-23 05:17:10 +00:00
|
|
|
TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv)
|
2012-09-28 10:19:18 +00:00
|
|
|
{
|
2012-10-25 02:03:21 +00:00
|
|
|
nsAutoString label(aEncoding);
|
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
2012-09-28 10:19:18 +00:00
|
|
|
|
2012-11-06 23:23:14 +00:00
|
|
|
// Let encoding be the result of getting an encoding from label.
|
|
|
|
// If encoding is failure, or is none of utf-8, utf-16, and utf-16be,
|
|
|
|
// throw a TypeError.
|
2012-09-28 10:19:18 +00:00
|
|
|
if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
|
2012-11-06 23:23:14 +00:00
|
|
|
aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
|
2012-09-28 10:19:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-07 23:04:22 +00:00
|
|
|
if (!mEncoding.EqualsLiteral("UTF-8") &&
|
|
|
|
!mEncoding.EqualsLiteral("UTF-16LE") &&
|
|
|
|
!mEncoding.EqualsLiteral("UTF-16BE")) {
|
2012-11-06 23:23:14 +00:00
|
|
|
aRv.ThrowTypeError(MSG_DOM_ENCODING_NOT_UTF);
|
2012-09-28 10:19:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an encoder object for mEncoding.
|
2013-11-26 07:31:52 +00:00
|
|
|
mEncoder = EncodingUtils::EncoderForEncoding(mEncoding);
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 20:26:52 +00:00
|
|
|
void
|
2013-08-23 05:17:10 +00:00
|
|
|
TextEncoder::Encode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
const nsAString& aString,
|
|
|
|
const bool aStream,
|
2014-06-11 20:26:52 +00:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2013-08-23 05:17:10 +00:00
|
|
|
ErrorResult& aRv)
|
2012-09-28 10:19:18 +00:00
|
|
|
{
|
|
|
|
// Run the steps of the encoding algorithm.
|
|
|
|
int32_t srcLen = aString.Length();
|
|
|
|
int32_t maxLen;
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* data = PromiseFlatString(aString).get();
|
2012-09-28 10:19:18 +00:00
|
|
|
nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
// 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();
|
|
|
|
nsAutoArrayPtr<char> buf(new (fallible) char[maxLen + 1]);
|
|
|
|
if (!buf) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t dstLen = maxLen;
|
|
|
|
rv = mEncoder->Convert(data, &srcLen, buf, &dstLen);
|
|
|
|
|
|
|
|
// If the internal streaming flag is not set, then reset
|
|
|
|
// the encoding algorithm state to the default values for encoding.
|
2012-12-22 00:15:43 +00:00
|
|
|
if (!aStream) {
|
2012-09-28 10:19:18 +00:00
|
|
|
int32_t finishLen = maxLen - dstLen;
|
|
|
|
rv = mEncoder->Finish(buf + dstLen, &finishLen);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
dstLen += finishLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* outView = nullptr;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
buf[dstLen] = '\0';
|
2014-04-10 04:58:42 +00:00
|
|
|
JSAutoCompartment ac(aCx, aObj);
|
|
|
|
outView = Uint8Array::Create(aCx, dstLen,
|
2013-08-23 17:29:57 +00:00
|
|
|
reinterpret_cast<uint8_t*>(buf.get()));
|
2012-12-17 00:51:11 +00:00
|
|
|
if (!outView) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-11 20:26:52 +00:00
|
|
|
return;
|
2012-12-17 00:51:11 +00:00
|
|
|
}
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2014-06-11 20:26:52 +00:00
|
|
|
aRetval.set(outView);
|
2012-09-28 10:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-23 05:17:10 +00:00
|
|
|
TextEncoder::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
|