dom/encoding/TextDecoder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/encoding/TextDecoder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     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/TextDecoder.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 +static const char16_t kReplacementChar = static_cast<char16_t>(0xFFFD);
    1.16 +
    1.17 +void
    1.18 +TextDecoder::Init(const nsAString& aLabel, const bool aFatal,
    1.19 +                  ErrorResult& aRv)
    1.20 +{
    1.21 +  nsAutoString label(aLabel);
    1.22 +  EncodingUtils::TrimSpaceCharacters(label);
    1.23 +
    1.24 +  nsAutoCString encoding;
    1.25 +  // Let encoding be the result of getting an encoding from label.
    1.26 +  // If encoding is failure or replacement, throw a TypeError.
    1.27 +  if (!EncodingUtils::FindEncodingForLabel(label, encoding) ||
    1.28 +      encoding.EqualsLiteral("replacement")) {
    1.29 +    aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
    1.30 +    return;
    1.31 +  }
    1.32 +  InitWithEncoding(encoding, aFatal);
    1.33 +}
    1.34 +
    1.35 +void
    1.36 +TextDecoder::InitWithEncoding(const nsACString& aEncoding, const bool aFatal)
    1.37 +{
    1.38 +  mEncoding = aEncoding;
    1.39 +  // If the constructor is called with an options argument,
    1.40 +  // and the fatal property of the dictionary is set,
    1.41 +  // set the internal fatal flag of the decoder object.
    1.42 +  mFatal = aFatal;
    1.43 +
    1.44 +  // Create a decoder object for mEncoding.
    1.45 +  mDecoder = EncodingUtils::DecoderForEncoding(mEncoding);
    1.46 +
    1.47 +  if (mFatal) {
    1.48 +    mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
    1.49 +  }
    1.50 +}
    1.51 +
    1.52 +void
    1.53 +TextDecoder::Decode(const char* aInput, const int32_t aLength,
    1.54 +                    const bool aStream, nsAString& aOutDecodedString,
    1.55 +                    ErrorResult& aRv)
    1.56 +{
    1.57 +  aOutDecodedString.Truncate();
    1.58 +
    1.59 +  // Run or resume the decoder algorithm of the decoder object's encoder.
    1.60 +  int32_t outLen;
    1.61 +  nsresult rv = mDecoder->GetMaxLength(aInput, aLength, &outLen);
    1.62 +  if (NS_FAILED(rv)) {
    1.63 +    aRv.Throw(rv);
    1.64 +    return;
    1.65 +  }
    1.66 +  // Need a fallible allocator because the caller may be a content
    1.67 +  // and the content can specify the length of the string.
    1.68 +  static const fallible_t fallible = fallible_t();
    1.69 +  nsAutoArrayPtr<char16_t> buf(new (fallible) char16_t[outLen + 1]);
    1.70 +  if (!buf) {
    1.71 +    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    1.72 +    return;
    1.73 +  }
    1.74 +
    1.75 +  int32_t length = aLength;
    1.76 +  rv = mDecoder->Convert(aInput, &length, buf, &outLen);
    1.77 +  MOZ_ASSERT(mFatal || rv != NS_ERROR_ILLEGAL_INPUT);
    1.78 +  buf[outLen] = 0;
    1.79 +  aOutDecodedString.Append(buf, outLen);
    1.80 +
    1.81 +  // If the internal streaming flag of the decoder object is not set,
    1.82 +  // then reset the encoding algorithm state to the default values
    1.83 +  if (!aStream) {
    1.84 +    mDecoder->Reset();
    1.85 +    if (rv == NS_OK_UDEC_MOREINPUT) {
    1.86 +      if (mFatal) {
    1.87 +        aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
    1.88 +      } else {
    1.89 +        // Need to emit a decode error manually
    1.90 +        // to simulate the EOF handling of the Encoding spec.
    1.91 +        aOutDecodedString.Append(kReplacementChar);
    1.92 +      }
    1.93 +    }
    1.94 +  }
    1.95 +
    1.96 +  if (NS_FAILED(rv)) {
    1.97 +    aRv.Throw(NS_ERROR_DOM_ENCODING_DECODE_ERR);
    1.98 +  }
    1.99 +}
   1.100 +
   1.101 +void
   1.102 +TextDecoder::GetEncoding(nsAString& aEncoding)
   1.103 +{
   1.104 +  CopyASCIItoUTF16(mEncoding, aEncoding);
   1.105 +  nsContentUtils::ASCIIToLower(aEncoding);
   1.106 +}
   1.107 +
   1.108 +} // dom
   1.109 +} // mozilla

mercurial