Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/TextDecoder.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 | static const char16_t kReplacementChar = static_cast<char16_t>(0xFFFD); |
michael@0 | 13 | |
michael@0 | 14 | void |
michael@0 | 15 | TextDecoder::Init(const nsAString& aLabel, const bool aFatal, |
michael@0 | 16 | ErrorResult& aRv) |
michael@0 | 17 | { |
michael@0 | 18 | nsAutoString label(aLabel); |
michael@0 | 19 | EncodingUtils::TrimSpaceCharacters(label); |
michael@0 | 20 | |
michael@0 | 21 | nsAutoCString encoding; |
michael@0 | 22 | // Let encoding be the result of getting an encoding from label. |
michael@0 | 23 | // If encoding is failure or replacement, throw a TypeError. |
michael@0 | 24 | if (!EncodingUtils::FindEncodingForLabel(label, encoding) || |
michael@0 | 25 | encoding.EqualsLiteral("replacement")) { |
michael@0 | 26 | aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label); |
michael@0 | 27 | return; |
michael@0 | 28 | } |
michael@0 | 29 | InitWithEncoding(encoding, aFatal); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | void |
michael@0 | 33 | TextDecoder::InitWithEncoding(const nsACString& aEncoding, const bool aFatal) |
michael@0 | 34 | { |
michael@0 | 35 | mEncoding = aEncoding; |
michael@0 | 36 | // If the constructor is called with an options argument, |
michael@0 | 37 | // and the fatal property of the dictionary is set, |
michael@0 | 38 | // set the internal fatal flag of the decoder object. |
michael@0 | 39 | mFatal = aFatal; |
michael@0 | 40 | |
michael@0 | 41 | // Create a decoder object for mEncoding. |
michael@0 | 42 | mDecoder = EncodingUtils::DecoderForEncoding(mEncoding); |
michael@0 | 43 | |
michael@0 | 44 | if (mFatal) { |
michael@0 | 45 | mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal); |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | void |
michael@0 | 50 | TextDecoder::Decode(const char* aInput, const int32_t aLength, |
michael@0 | 51 | const bool aStream, nsAString& aOutDecodedString, |
michael@0 | 52 | ErrorResult& aRv) |
michael@0 | 53 | { |
michael@0 | 54 | aOutDecodedString.Truncate(); |
michael@0 | 55 | |
michael@0 | 56 | // Run or resume the decoder algorithm of the decoder object's encoder. |
michael@0 | 57 | int32_t outLen; |
michael@0 | 58 | nsresult rv = mDecoder->GetMaxLength(aInput, aLength, &outLen); |
michael@0 | 59 | if (NS_FAILED(rv)) { |
michael@0 | 60 | aRv.Throw(rv); |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | // Need a fallible allocator because the caller may be a content |
michael@0 | 64 | // and the content can specify the length of the string. |
michael@0 | 65 | static const fallible_t fallible = fallible_t(); |
michael@0 | 66 | nsAutoArrayPtr<char16_t> buf(new (fallible) char16_t[outLen + 1]); |
michael@0 | 67 | if (!buf) { |
michael@0 | 68 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 69 | return; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | int32_t length = aLength; |
michael@0 | 73 | rv = mDecoder->Convert(aInput, &length, buf, &outLen); |
michael@0 | 74 | MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT); |
michael@0 | 75 | buf[outLen] = 0; |
michael@0 | 76 | aOutDecodedString.Append(buf, outLen); |
michael@0 | 77 | |
michael@0 | 78 | // If the internal streaming flag of the decoder object is not set, |
michael@0 | 79 | // then reset the encoding algorithm state to the default values |
michael@0 | 80 | if (!aStream) { |
michael@0 | 81 | mDecoder->Reset(); |
michael@0 | 82 | if (rv == NS_OK_UDEC_MOREINPUT) { |
michael@0 | 83 | if (mFatal) { |
michael@0 | 84 | aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR); |
michael@0 | 85 | } else { |
michael@0 | 86 | // Need to emit a decode error manually |
michael@0 | 87 | // to simulate the EOF handling of the Encoding spec. |
michael@0 | 88 | aOutDecodedString.Append(kReplacementChar); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | if (NS_FAILED(rv)) { |
michael@0 | 94 | aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | void |
michael@0 | 99 | TextDecoder::GetEncoding(nsAString& aEncoding) |
michael@0 | 100 | { |
michael@0 | 101 | CopyASCIItoUTF16(mEncoding, aEncoding); |
michael@0 | 102 | nsContentUtils::ASCIIToLower(aEncoding); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | } // dom |
michael@0 | 106 | } // mozilla |