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/TextEncoder.h"
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2015-11-03 21:29:19 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2012-09-28 14:19:18 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
void
|
2016-03-24 13:27:15 +03:00
|
|
|
TextEncoder::Init()
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
2016-03-24 13:27:15 +03:00
|
|
|
// Create an encoder object for utf-8.
|
|
|
|
mEncoder = EncodingUtils::EncoderForEncoding(NS_LITERAL_CSTRING("UTF-8"));
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2013-08-23 09:17:10 +04:00
|
|
|
TextEncoder::Encode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
const nsAString& aString,
|
2014-11-21 22:58:45 +03:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2013-08-23 09:17:10 +04:00
|
|
|
ErrorResult& aRv)
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
|
|
|
// Run the steps of the encoding algorithm.
|
|
|
|
int32_t srcLen = aString.Length();
|
|
|
|
int32_t maxLen;
|
2015-10-14 15:28:50 +03:00
|
|
|
const char16_t* data = aString.BeginReading();
|
2012-09-28 14:19:18 +04:00
|
|
|
nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
// Need a fallible allocator because the caller may be a content
|
|
|
|
// and the content can specify the length of the string.
|
2015-11-03 21:29:19 +03:00
|
|
|
auto buf = MakeUniqueFallible<char[]>(maxLen + 1);
|
2012-09-28 14:19:18 +04:00
|
|
|
if (!buf) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t dstLen = maxLen;
|
2015-11-03 21:29:19 +03:00
|
|
|
rv = mEncoder->Convert(data, &srcLen, buf.get(), &dstLen);
|
2012-09-28 14:19:18 +04:00
|
|
|
|
2014-11-21 22:58:45 +03:00
|
|
|
// Now reset the encoding algorithm state to the default values for encoding.
|
|
|
|
int32_t finishLen = maxLen - dstLen;
|
2015-11-03 21:29:19 +03:00
|
|
|
rv = mEncoder->Finish(&buf[dstLen], &finishLen);
|
2014-11-21 22:58:45 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
dstLen += finishLen;
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* outView = nullptr;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
buf[dstLen] = '\0';
|
2014-04-10 08:58:42 +04:00
|
|
|
JSAutoCompartment ac(aCx, aObj);
|
|
|
|
outView = Uint8Array::Create(aCx, dstLen,
|
2013-08-23 21:29:57 +04:00
|
|
|
reinterpret_cast<uint8_t*>(buf.get()));
|
2012-12-17 04:51:11 +04:00
|
|
|
if (!outView) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-12-17 04:51:11 +04:00
|
|
|
}
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval.set(outView);
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-23 09:17:10 +04:00
|
|
|
TextEncoder::GetEncoding(nsAString& aEncoding)
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
2016-03-24 13:27:15 +03:00
|
|
|
aEncoding.AssignLiteral("utf-8");
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|