summaryrefslogtreecommitdiff
path: root/dom/encoding/EncodingUtils.cpp
blob: 1272e506a5ec2605bbc9f7b2d4b7f817511c9419 (plain)
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
99
100
101
102
103
104
/* -*- Mode: C++; tab-width: 8; 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/EncodingUtils.h"

#include "mozilla/ArrayUtils.h" // ArrayLength
#include "nsUConvPropertySearch.h"
#include "nsIUnicodeDecoder.h"
#include "nsIUnicodeEncoder.h"
#include "nsComponentManagerUtils.h"

namespace mozilla {
namespace dom {

static const nsUConvProp labelsEncodings[] = {
#include "labelsencodings.properties.h"
};

static const nsUConvProp encodingsGroups[] = {
#include "encodingsgroups.properties.h"
};

bool
EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
                                    nsACString& aOutEncoding)
{
  // Save aLabel first because it may refer the same string as aOutEncoding.
  nsCString label(aLabel);

  EncodingUtils::TrimSpaceCharacters(label);
  if (label.IsEmpty()) {
    aOutEncoding.Truncate();
    return false;
  }

  ToLowerCase(label);
  return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
      labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
}

bool
EncodingUtils::FindEncodingForLabelNoReplacement(const nsACString& aLabel,
                                                 nsACString& aOutEncoding)
{
  if(!FindEncodingForLabel(aLabel, aOutEncoding)) {
    return false;
  }
  if (aOutEncoding.EqualsLiteral("replacement")) {
    aOutEncoding.Truncate();
    return false;
  }
  return true;
}

bool
EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
{
  // HZ and UTF-7 are no longer in mozilla-central, but keeping them here
  // just in case for the benefit of comm-central.
  return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
           aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
           aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
           aPreferredName.LowerCaseEqualsLiteral("replacement") ||
           aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
           aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
           aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
}

already_AddRefed<nsIUnicodeDecoder>
EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
{
  nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
  contractId.Append(aEncoding);

  nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
  MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
  return decoder.forget();
}

already_AddRefed<nsIUnicodeEncoder>
EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
{
  nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
  contractId.Append(aEncoding);

  nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
  MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
  return encoder.forget();
}

void
EncodingUtils::LangGroupForEncoding(const nsACString& aEncoding,
                                    nsACString& aOutGroup)
{
  if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
      encodingsGroups, ArrayLength(encodingsGroups), aEncoding, aOutGroup))) {
    aOutGroup.AssignLiteral("x-unicode");
  }
}

} // namespace dom
} // namespace mozilla