1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/TextEncoder.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/dom/TextEncoder.h" 1.9 +#include "mozilla/dom/EncodingUtils.h" 1.10 +#include "nsContentUtils.h" 1.11 + 1.12 +namespace mozilla { 1.13 +namespace dom { 1.14 + 1.15 +void 1.16 +TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv) 1.17 +{ 1.18 + nsAutoString label(aEncoding); 1.19 + EncodingUtils::TrimSpaceCharacters(label); 1.20 + 1.21 + // Let encoding be the result of getting an encoding from label. 1.22 + // If encoding is failure, or is none of utf-8, utf-16, and utf-16be, 1.23 + // throw a TypeError. 1.24 + if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) { 1.25 + aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label); 1.26 + return; 1.27 + } 1.28 + 1.29 + if (!mEncoding.EqualsLiteral("UTF-8") && 1.30 + !mEncoding.EqualsLiteral("UTF-16LE") && 1.31 + !mEncoding.EqualsLiteral("UTF-16BE")) { 1.32 + aRv.ThrowTypeError(MSG_DOM_ENCODING_NOT_UTF); 1.33 + return; 1.34 + } 1.35 + 1.36 + // Create an encoder object for mEncoding. 1.37 + mEncoder = EncodingUtils::EncoderForEncoding(mEncoding); 1.38 +} 1.39 + 1.40 +void 1.41 +TextEncoder::Encode(JSContext* aCx, 1.42 + JS::Handle<JSObject*> aObj, 1.43 + const nsAString& aString, 1.44 + const bool aStream, 1.45 + JS::MutableHandle<JSObject*> aRetval, 1.46 + ErrorResult& aRv) 1.47 +{ 1.48 + // Run the steps of the encoding algorithm. 1.49 + int32_t srcLen = aString.Length(); 1.50 + int32_t maxLen; 1.51 + const char16_t* data = PromiseFlatString(aString).get(); 1.52 + nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen); 1.53 + if (NS_FAILED(rv)) { 1.54 + aRv.Throw(rv); 1.55 + return; 1.56 + } 1.57 + // Need a fallible allocator because the caller may be a content 1.58 + // and the content can specify the length of the string. 1.59 + static const fallible_t fallible = fallible_t(); 1.60 + nsAutoArrayPtr<char> buf(new (fallible) char[maxLen + 1]); 1.61 + if (!buf) { 1.62 + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 1.63 + return; 1.64 + } 1.65 + 1.66 + int32_t dstLen = maxLen; 1.67 + rv = mEncoder->Convert(data, &srcLen, buf, &dstLen); 1.68 + 1.69 + // If the internal streaming flag is not set, then reset 1.70 + // the encoding algorithm state to the default values for encoding. 1.71 + if (!aStream) { 1.72 + int32_t finishLen = maxLen - dstLen; 1.73 + rv = mEncoder->Finish(buf + dstLen, &finishLen); 1.74 + if (NS_SUCCEEDED(rv)) { 1.75 + dstLen += finishLen; 1.76 + } 1.77 + } 1.78 + 1.79 + JSObject* outView = nullptr; 1.80 + if (NS_SUCCEEDED(rv)) { 1.81 + buf[dstLen] = '\0'; 1.82 + JSAutoCompartment ac(aCx, aObj); 1.83 + outView = Uint8Array::Create(aCx, dstLen, 1.84 + reinterpret_cast<uint8_t*>(buf.get())); 1.85 + if (!outView) { 1.86 + aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 1.87 + return; 1.88 + } 1.89 + } 1.90 + 1.91 + if (NS_FAILED(rv)) { 1.92 + aRv.Throw(rv); 1.93 + } 1.94 + aRetval.set(outView); 1.95 +} 1.96 + 1.97 +void 1.98 +TextEncoder::GetEncoding(nsAString& aEncoding) 1.99 +{ 1.100 + CopyASCIItoUTF16(mEncoding, aEncoding); 1.101 + nsContentUtils::ASCIIToLower(aEncoding); 1.102 +} 1.103 + 1.104 +} // dom 1.105 +} // mozilla