forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEncoder.cpp
More file actions
98 lines (85 loc) · 2.82 KB
/
Copy pathTextEncoder.cpp
File metadata and controls
98 lines (85 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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
TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv)
{
nsAutoString label(aEncoding);
EncodingUtils::TrimSpaceCharacters(label);
// 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 RangeError (https://encoding.spec.whatwg.org/#dom-textencoder).
if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
aRv.ThrowRangeError(MSG_ENCODING_NOT_SUPPORTED, &label);
return;
}
if (!mEncoding.EqualsLiteral("UTF-8") &&
!mEncoding.EqualsLiteral("UTF-16LE") &&
!mEncoding.EqualsLiteral("UTF-16BE")) {
aRv.ThrowRangeError(MSG_DOM_ENCODING_NOT_UTF);
return;
}
// Create an encoder object for mEncoding.
mEncoder = EncodingUtils::EncoderForEncoding(mEncoding);
}
void
TextEncoder::Encode(JSContext* aCx,
JS::Handle<JSObject*> aObj,
const nsAString& aString,
JS::MutableHandle<JSObject*> aRetval,
ErrorResult& aRv)
{
// Run the steps of the encoding algorithm.
int32_t srcLen = aString.Length();
int32_t maxLen;
const char16_t* data = PromiseFlatString(aString).get();
nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen);
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<char> buf(new (fallible) char[maxLen + 1]);
if (!buf) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
int32_t dstLen = maxLen;
rv = mEncoder->Convert(data, &srcLen, buf, &dstLen);
// Now reset the encoding algorithm state to the default values for encoding.
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';
JSAutoCompartment ac(aCx, aObj);
outView = Uint8Array::Create(aCx, dstLen,
reinterpret_cast<uint8_t*>(buf.get()));
if (!outView) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
}
if (NS_FAILED(rv)) {
aRv.Throw(rv);
}
aRetval.set(outView);
}
void
TextEncoder::GetEncoding(nsAString& aEncoding)
{
CopyASCIItoUTF16(mEncoding, aEncoding);
nsContentUtils::ASCIIToLower(aEncoding);
}
} // dom
} // mozilla