Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/dom/TextEncoder.h" |
michael@0 | 6 | #include "mozilla/dom/EncodingUtils.h" |
michael@0 | 7 | #include "nsContentUtils.h" |
michael@0 | 8 | |
michael@0 | 9 | namespace mozilla { |
michael@0 | 10 | namespace dom { |
michael@0 | 11 | |
michael@0 | 12 | void |
michael@0 | 13 | TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv) |
michael@0 | 14 | { |
michael@0 | 15 | nsAutoString label(aEncoding); |
michael@0 | 16 | EncodingUtils::TrimSpaceCharacters(label); |
michael@0 | 17 | |
michael@0 | 18 | // Let encoding be the result of getting an encoding from label. |
michael@0 | 19 | // If encoding is failure, or is none of utf-8, utf-16, and utf-16be, |
michael@0 | 20 | // throw a TypeError. |
michael@0 | 21 | if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) { |
michael@0 | 22 | aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label); |
michael@0 | 23 | return; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | if (!mEncoding.EqualsLiteral("UTF-8") && |
michael@0 | 27 | !mEncoding.EqualsLiteral("UTF-16LE") && |
michael@0 | 28 | !mEncoding.EqualsLiteral("UTF-16BE")) { |
michael@0 | 29 | aRv.ThrowTypeError(MSG_DOM_ENCODING_NOT_UTF); |
michael@0 | 30 | return; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | // Create an encoder object for mEncoding. |
michael@0 | 34 | mEncoder = EncodingUtils::EncoderForEncoding(mEncoding); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | void |
michael@0 | 38 | TextEncoder::Encode(JSContext* aCx, |
michael@0 | 39 | JS::Handle<JSObject*> aObj, |
michael@0 | 40 | const nsAString& aString, |
michael@0 | 41 | const bool aStream, |
michael@0 | 42 | JS::MutableHandle<JSObject*> aRetval, |
michael@0 | 43 | ErrorResult& aRv) |
michael@0 | 44 | { |
michael@0 | 45 | // Run the steps of the encoding algorithm. |
michael@0 | 46 | int32_t srcLen = aString.Length(); |
michael@0 | 47 | int32_t maxLen; |
michael@0 | 48 | const char16_t* data = PromiseFlatString(aString).get(); |
michael@0 | 49 | nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen); |
michael@0 | 50 | if (NS_FAILED(rv)) { |
michael@0 | 51 | aRv.Throw(rv); |
michael@0 | 52 | return; |
michael@0 | 53 | } |
michael@0 | 54 | // Need a fallible allocator because the caller may be a content |
michael@0 | 55 | // and the content can specify the length of the string. |
michael@0 | 56 | static const fallible_t fallible = fallible_t(); |
michael@0 | 57 | nsAutoArrayPtr<char> buf(new (fallible) char[maxLen + 1]); |
michael@0 | 58 | if (!buf) { |
michael@0 | 59 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 60 | return; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | int32_t dstLen = maxLen; |
michael@0 | 64 | rv = mEncoder->Convert(data, &srcLen, buf, &dstLen); |
michael@0 | 65 | |
michael@0 | 66 | // If the internal streaming flag is not set, then reset |
michael@0 | 67 | // the encoding algorithm state to the default values for encoding. |
michael@0 | 68 | if (!aStream) { |
michael@0 | 69 | int32_t finishLen = maxLen - dstLen; |
michael@0 | 70 | rv = mEncoder->Finish(buf + dstLen, &finishLen); |
michael@0 | 71 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 72 | dstLen += finishLen; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | JSObject* outView = nullptr; |
michael@0 | 77 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 78 | buf[dstLen] = '\0'; |
michael@0 | 79 | JSAutoCompartment ac(aCx, aObj); |
michael@0 | 80 | outView = Uint8Array::Create(aCx, dstLen, |
michael@0 | 81 | reinterpret_cast<uint8_t*>(buf.get())); |
michael@0 | 82 | if (!outView) { |
michael@0 | 83 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | if (NS_FAILED(rv)) { |
michael@0 | 89 | aRv.Throw(rv); |
michael@0 | 90 | } |
michael@0 | 91 | aRetval.set(outView); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void |
michael@0 | 95 | TextEncoder::GetEncoding(nsAString& aEncoding) |
michael@0 | 96 | { |
michael@0 | 97 | CopyASCIItoUTF16(mEncoding, aEncoding); |
michael@0 | 98 | nsContentUtils::ASCIIToLower(aEncoding); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | } // dom |
michael@0 | 102 | } // mozilla |