1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/EncodingUtils.h"
8 
9 #include "mozilla/ArrayUtils.h" // ArrayLength
10 #include "nsUConvPropertySearch.h"
11 #include "nsIUnicodeDecoder.h"
12 #include "nsIUnicodeEncoder.h"
13 #include "nsComponentManagerUtils.h"
14 
15 namespace mozilla {
16 namespace dom {
17 
18 static const nsUConvProp labelsEncodings[] = {
19 #include "labelsencodings.properties.h"
20 };
21 
22 static const nsUConvProp encodingsGroups[] = {
23 #include "encodingsgroups.properties.h"
24 };
25 
26 bool
FindEncodingForLabel(const nsACString & aLabel,nsACString & aOutEncoding)27 EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
28                                     nsACString& aOutEncoding)
29 {
30   // Save aLabel first because it may refer the same string as aOutEncoding.
31   nsCString label(aLabel);
32 
33   EncodingUtils::TrimSpaceCharacters(label);
34   if (label.IsEmpty()) {
35     aOutEncoding.Truncate();
36     return false;
37   }
38 
39   ToLowerCase(label);
40   return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
41       labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
42 }
43 
44 bool
FindEncodingForLabelNoReplacement(const nsACString & aLabel,nsACString & aOutEncoding)45 EncodingUtils::FindEncodingForLabelNoReplacement(const nsACString& aLabel,
46                                                  nsACString& aOutEncoding)
47 {
48   if(!FindEncodingForLabel(aLabel, aOutEncoding)) {
49     return false;
50   }
51   if (aOutEncoding.EqualsLiteral("replacement")) {
52     aOutEncoding.Truncate();
53     return false;
54   }
55   return true;
56 }
57 
58 bool
IsAsciiCompatible(const nsACString & aPreferredName)59 EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
60 {
61   // HZ and UTF-7 are no longer in mozilla-central, but keeping them here
62   // just in case for the benefit of comm-central.
63   return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
64            aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
65            aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
66            aPreferredName.LowerCaseEqualsLiteral("replacement") ||
67            aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
68            aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
69            aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
70 }
71 
72 already_AddRefed<nsIUnicodeDecoder>
DecoderForEncoding(const nsACString & aEncoding)73 EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
74 {
75   nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
76   contractId.Append(aEncoding);
77 
78   nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
79   MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
80   return decoder.forget();
81 }
82 
83 already_AddRefed<nsIUnicodeEncoder>
EncoderForEncoding(const nsACString & aEncoding)84 EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
85 {
86   nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
87   contractId.Append(aEncoding);
88 
89   nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
90   MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
91   return encoder.forget();
92 }
93 
94 void
LangGroupForEncoding(const nsACString & aEncoding,nsACString & aOutGroup)95 EncodingUtils::LangGroupForEncoding(const nsACString& aEncoding,
96                                     nsACString& aOutGroup)
97 {
98   if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
99       encodingsGroups, ArrayLength(encodingsGroups), aEncoding, aOutGroup))) {
100     aOutGroup.AssignLiteral("x-unicode");
101   }
102 }
103 
104 } // namespace dom
105 } // namespace mozilla
106